gdb/ppcfbsd-tdep.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Target-dependent code for PowerPC systems running FreeBSD.

  2.    Copyright (C) 2013-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "frame.h"
  17. #include "gdbcore.h"
  18. #include "frame-unwind.h"
  19. #include "gdbtypes.h"
  20. #include "osabi.h"
  21. #include "regcache.h"
  22. #include "regset.h"
  23. #include "symtab.h"
  24. #include "target.h"
  25. #include "trad-frame.h"

  26. #include "ppc-tdep.h"
  27. #include "ppc64-tdep.h"
  28. #include "ppcfbsd-tdep.h"
  29. #include "fbsd-tdep.h"
  30. #include "solib-svr4.h"


  31. /* 32-bit regset descriptions.  */

  32. static const struct ppc_reg_offsets ppc32_fbsd_reg_offsets =
  33.   {
  34.         /* General-purpose registers.  */
  35.         /* .r0_offset = */     0,
  36.         /* .gpr_size = */      4,
  37.         /* .xr_size = */       4,
  38.         /* .pc_offset = */     144,
  39.         /* .ps_offset = */     -1,
  40.         /* .cr_offset = */     132,
  41.         /* .lr_offset = */     128,
  42.         /* .ctr_offset = */    140,
  43.         /* .xer_offset = */    136,
  44.         /* .mq_offset = */     -1,

  45.         /* Floating-point registers.  */
  46.         /* .f0_offset = */     0,
  47.         /* .fpscr_offset = */  256,
  48.         /* .fpscr_size = */    8,
  49. #ifdef NOTYET
  50.         /* AltiVec registers.  */
  51.         /* .vr0_offset = */    0,
  52.         /* .vscr_offset = */   512 + 12,
  53.         /* .vrsave_offset = */ 512
  54. #endif
  55.   };

  56. /* 64-bit regset descriptions.  */

  57. static const struct ppc_reg_offsets ppc64_fbsd_reg_offsets =
  58.   {
  59.         /* General-purpose registers.  */
  60.         /* .r0_offset = */     0,
  61.         /* .gpr_size = */      8,
  62.         /* .xr_size = */       8,
  63.         /* .pc_offset = */     288,
  64.         /* .ps_offset = */     -1,
  65.         /* .cr_offset = */     264,
  66.         /* .lr_offset = */     256,
  67.         /* .ctr_offset = */    280,
  68.         /* .xer_offset = */    272,
  69.         /* .mq_offset = */     -1,

  70.         /* Floating-point registers.  */
  71.         /* .f0_offset = */     0,
  72.         /* .fpscr_offset = */  256,
  73.         /* .fpscr_size = */    8,
  74. #ifdef NOYET
  75.         /* AltiVec registers.  */
  76.         /* .vr0_offset = */    0,
  77.         /* .vscr_offset = */   512 + 12,
  78.         /* .vrsave_offset = */ 528
  79. #endif
  80.   };

  81. /* 32-bit general-purpose register set.  */

  82. static const struct regset ppc32_fbsd_gregset = {
  83.   &ppc32_fbsd_reg_offsets,
  84.   ppc_supply_gregset,
  85.   ppc_collect_gregset
  86. };

  87. /* 64-bit general-purpose register set.  */

  88. static const struct regset ppc64_fbsd_gregset = {
  89.   &ppc64_fbsd_reg_offsets,
  90.   ppc_supply_gregset,
  91.   ppc_collect_gregset
  92. };

  93. /* 32-/64-bit floating-point register set.  */

  94. static const struct regset ppc32_fbsd_fpregset = {
  95.   &ppc32_fbsd_reg_offsets,
  96.   ppc_supply_fpregset,
  97.   ppc_collect_fpregset
  98. };

  99. const struct regset *
  100. ppc_fbsd_gregset (int wordsize)
  101. {
  102.   return wordsize == 8 ? &ppc64_fbsd_gregset : &ppc32_fbsd_gregset;
  103. }

  104. const struct regset *
  105. ppc_fbsd_fpregset (void)
  106. {
  107.   return &ppc32_fbsd_fpregset;
  108. }

  109. /* Iterate over core file register note sections.  */

  110. static void
  111. ppcfbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  112.                                       iterate_over_regset_sections_cb *cb,
  113.                                       void *cb_data,
  114.                                       const struct regcache *regcache)
  115. {
  116.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  117.   if (tdep->wordsize == 4)
  118.     cb (".reg", 148, &ppc32_fbsd_gregset, NULL, cb_data);
  119.   else
  120.     cb (".reg", 296, &ppc64_fbsd_gregset, NULL, cb_data);
  121.   cb (".reg2", 264, &ppc32_fbsd_fpregset, NULL, cb_data);
  122. }

  123. /* Default page size.  */

  124. static const int ppcfbsd_page_size = 4096;

  125. /* Offset for sigreturn(2).  */

  126. static const int ppcfbsd_sigreturn_offset[] = {
  127.   0xc,                                /* FreeBSD 32-bit  */
  128.   -1
  129. };

  130. /* Signal trampolines.  */

  131. static int
  132. ppcfbsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
  133.                                 struct frame_info *this_frame,
  134.                                 void **this_cache)
  135. {
  136.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  137.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  138.   CORE_ADDR pc = get_frame_pc (this_frame);
  139.   CORE_ADDR start_pc = (pc & ~(ppcfbsd_page_size - 1));
  140.   const int *offset;
  141.   const char *name;

  142.   /* A stack trampoline is detected if no name is associated
  143.    to the current pc and if it points inside a trampoline
  144.    sequence.  */

  145.   find_pc_partial_function (pc, &name, NULL, NULL);

  146.   /* If we have a name, we have no trampoline, return.  */
  147.   if (name)
  148.     return 0;

  149.   for (offset = ppcfbsd_sigreturn_offset; *offset != -1; offset++)
  150.     {
  151.       gdb_byte buf[2 * PPC_INSN_SIZE];
  152.       unsigned long insn;

  153.       if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
  154.                                      buf, sizeof buf))
  155.         continue;

  156.       /* Check for "li r0,SYS_sigreturn".  */
  157.       insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
  158.       if (insn != 0x380001a1)
  159.         continue;

  160.       /* Check for "sc".  */
  161.       insn = extract_unsigned_integer (buf + PPC_INSN_SIZE,
  162.                                        PPC_INSN_SIZE, byte_order);
  163.       if (insn != 0x44000002)
  164.         continue;

  165.       return 1;
  166.     }

  167.   return 0;
  168. }

  169. static struct trad_frame_cache *
  170. ppcfbsd_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
  171. {
  172.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  173.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  174.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  175.   struct trad_frame_cache *cache;
  176.   CORE_ADDR addr, base, func;
  177.   gdb_byte buf[PPC_INSN_SIZE];
  178.   int i;

  179.   if (*this_cache)
  180.     return *this_cache;

  181.   cache = trad_frame_cache_zalloc (this_frame);
  182.   *this_cache = cache;

  183.   func = get_frame_pc (this_frame);
  184.   func &= ~(ppcfbsd_page_size - 1);
  185.   if (!safe_frame_unwind_memory (this_frame, func, buf, sizeof buf))
  186.     return cache;

  187.   base = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
  188.   addr = base + 0x10 + 2 * tdep->wordsize;
  189.   for (i = 0; i < ppc_num_gprs; i++, addr += tdep->wordsize)
  190.     {
  191.       int regnum = i + tdep->ppc_gp0_regnum;
  192.       trad_frame_set_reg_addr (cache, regnum, addr);
  193.     }
  194.   trad_frame_set_reg_addr (cache, tdep->ppc_lr_regnum, addr);
  195.   addr += tdep->wordsize;
  196.   trad_frame_set_reg_addr (cache, tdep->ppc_cr_regnum, addr);
  197.   addr += tdep->wordsize;
  198.   trad_frame_set_reg_addr (cache, tdep->ppc_xer_regnum, addr);
  199.   addr += tdep->wordsize;
  200.   trad_frame_set_reg_addr (cache, tdep->ppc_ctr_regnum, addr);
  201.   addr += tdep->wordsize;
  202.   trad_frame_set_reg_addr (cache, gdbarch_pc_regnum (gdbarch), addr);
  203.   /* SRR0?  */
  204.   addr += tdep->wordsize;

  205.   /* Construct the frame ID using the function start.  */
  206.   trad_frame_set_id (cache, frame_id_build (base, func));

  207.   return cache;
  208. }

  209. static void
  210. ppcfbsd_sigtramp_frame_this_id (struct frame_info *this_frame,
  211.                                 void **this_cache, struct frame_id *this_id)
  212. {
  213.   struct trad_frame_cache *cache =
  214.     ppcfbsd_sigtramp_frame_cache (this_frame, this_cache);

  215.   trad_frame_get_id (cache, this_id);
  216. }

  217. static struct value *
  218. ppcfbsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
  219.                                       void **this_cache, int regnum)
  220. {
  221.   struct trad_frame_cache *cache =
  222.     ppcfbsd_sigtramp_frame_cache (this_frame, this_cache);

  223.   return trad_frame_get_register (cache, this_frame, regnum);
  224. }

  225. static const struct frame_unwind ppcfbsd_sigtramp_frame_unwind = {
  226.   SIGTRAMP_FRAME,
  227.   default_frame_unwind_stop_reason,
  228.   ppcfbsd_sigtramp_frame_this_id,
  229.   ppcfbsd_sigtramp_frame_prev_register,
  230.   NULL,
  231.   ppcfbsd_sigtramp_frame_sniffer
  232. };

  233. static enum return_value_convention
  234. ppcfbsd_return_value (struct gdbarch *gdbarch, struct value *function,
  235.                       struct type *valtype, struct regcache *regcache,
  236.                       gdb_byte *readbuf, const gdb_byte *writebuf)
  237. {
  238.   return ppc_sysv_abi_broken_return_value (gdbarch, function, valtype,
  239.                                            regcache, readbuf, writebuf);
  240. }


  241. static void
  242. ppcfbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  243. {
  244.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  245.   /* Generic FreeBSD support. */
  246.   fbsd_init_abi (info, gdbarch);

  247.   /* FreeBSD doesn't support the 128-bit `long double' from the psABI.  */
  248.   set_gdbarch_long_double_bit (gdbarch, 64);
  249.   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);

  250.   if (tdep->wordsize == 4)
  251.     {
  252.       set_gdbarch_return_value (gdbarch, ppcfbsd_return_value);

  253.       set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  254.       set_solib_svr4_fetch_link_map_offsets (gdbarch,
  255.                                              svr4_ilp32_fetch_link_map_offsets);

  256.       frame_unwind_append_unwinder (gdbarch, &ppcfbsd_sigtramp_frame_unwind);
  257.       set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpc");
  258.     }

  259.   if (tdep->wordsize == 8)
  260.     {
  261.       set_gdbarch_convert_from_func_ptr_addr
  262.         (gdbarch, ppc64_convert_from_func_ptr_addr);
  263.       set_gdbarch_elf_make_msymbol_special (gdbarch,
  264.                                             ppc64_elf_make_msymbol_special);

  265.       set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
  266.       set_solib_svr4_fetch_link_map_offsets (gdbarch,
  267.                                              svr4_lp64_fetch_link_map_offsets);
  268.       set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpc");
  269.     }

  270.   set_gdbarch_iterate_over_regset_sections
  271.     (gdbarch, ppcfbsd_iterate_over_regset_sections);

  272.   set_gdbarch_fetch_tls_load_module_address (gdbarch,
  273.                                              svr4_fetch_objfile_link_map);
  274. }

  275. /* Provide a prototype to silence -Wmissing-prototypes.  */

  276. void _initialize_ppcfbsd_tdep (void);

  277. void
  278. _initialize_ppcfbsd_tdep (void)
  279. {
  280.   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_FREEBSD_ELF,
  281.                           ppcfbsd_init_abi);
  282.   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64,
  283.                           GDB_OSABI_FREEBSD_ELF,
  284.                           ppcfbsd_init_abi);
  285.   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_FREEBSD_ELF,
  286.                           ppcfbsd_init_abi);
  287. }