gdb/ppcobsd-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for OpenBSD/powerpc.

  2.    Copyright (C) 2004-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 "frame-unwind.h"
  18. #include "gdbtypes.h"
  19. #include "osabi.h"
  20. #include "regcache.h"
  21. #include "regset.h"
  22. #include "symtab.h"
  23. #include "trad-frame.h"

  24. #include "ppc-tdep.h"
  25. #include "ppcobsd-tdep.h"
  26. #include "solib-svr4.h"

  27. /* Register offsets from <machine/reg.h>.  */
  28. struct ppc_reg_offsets ppcobsd_reg_offsets;
  29. struct ppc_reg_offsets ppcobsd_fpreg_offsets;


  30. /* Core file support.  */

  31. /* Supply register REGNUM in the general-purpose register set REGSET
  32.    from the buffer specified by GREGS and LEN to register cache
  33.    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */

  34. void
  35. ppcobsd_supply_gregset (const struct regset *regset,
  36.                         struct regcache *regcache, int regnum,
  37.                         const void *gregs, size_t len)
  38. {
  39.   ppc_supply_gregset (regset, regcache, regnum, gregs, len);
  40.   ppc_supply_fpregset (regset, regcache, regnum, gregs, len);
  41. }

  42. /* Collect register REGNUM in the general-purpose register set
  43.    REGSET, from register cache REGCACHE into the buffer specified by
  44.    GREGS and LEN.  If REGNUM is -1, do this for all registers in
  45.    REGSET.  */

  46. void
  47. ppcobsd_collect_gregset (const struct regset *regset,
  48.                          const struct regcache *regcache, int regnum,
  49.                          void *gregs, size_t len)
  50. {
  51.   ppc_collect_gregset (regset, regcache, regnum, gregs, len);
  52.   ppc_collect_fpregset (regset, regcache, regnum, gregs, len);
  53. }

  54. /* OpenBSD/powerpc register set.  */

  55. const struct regset ppcobsd_gregset =
  56. {
  57.   &ppcobsd_reg_offsets,
  58.   ppcobsd_supply_gregset
  59. };

  60. const struct regset ppcobsd_fpregset =
  61. {
  62.   &ppcobsd_fpreg_offsets,
  63.   ppc_supply_fpregset
  64. };

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

  66. static void
  67. ppcobsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  68.                                       iterate_over_regset_sections_cb *cb,
  69.                                       void *cb_data,
  70.                                       const struct regcache *regcache)
  71. {
  72.   cb (".reg", 412, &ppcobsd_gregset, NULL, cb_data);
  73. }


  74. /* Signal trampolines.  */

  75. /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
  76.    in virtual memory.  The randomness makes it somewhat tricky to
  77.    detect it, but fortunately we can rely on the fact that the start
  78.    of the sigtramp routine is page-aligned.  We recognize the
  79.    trampoline by looking for the code that invokes the sigreturn
  80.    system call.  The offset where we can find that code varies from
  81.    release to release.

  82.    By the way, the mapping mentioned above is read-only, so you cannot
  83.    place a breakpoint in the signal trampoline.  */

  84. /* Default page size.  */
  85. static const int ppcobsd_page_size = 4096;

  86. /* Offset for sigreturn(2).  */
  87. static const int ppcobsd_sigreturn_offset[] = {
  88.   0x98,                                /* OpenBSD 3.8 */
  89.   0x0c,                                /* OpenBSD 3.2 */
  90.   -1
  91. };

  92. static int
  93. ppcobsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
  94.                                 struct frame_info *this_frame,
  95.                                 void **this_cache)
  96. {
  97.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  98.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  99.   CORE_ADDR pc = get_frame_pc (this_frame);
  100.   CORE_ADDR start_pc = (pc & ~(ppcobsd_page_size - 1));
  101.   const int *offset;
  102.   const char *name;

  103.   find_pc_partial_function (pc, &name, NULL, NULL);
  104.   if (name)
  105.     return 0;

  106.   for (offset = ppcobsd_sigreturn_offset; *offset != -1; offset++)
  107.     {
  108.       gdb_byte buf[2 * PPC_INSN_SIZE];
  109.       unsigned long insn;

  110.       if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
  111.                                      buf, sizeof buf))
  112.         continue;

  113.       /* Check for "li r0,SYS_sigreturn".  */
  114.       insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
  115.       if (insn != 0x38000067)
  116.         continue;

  117.       /* Check for "sc".  */
  118.       insn = extract_unsigned_integer (buf + PPC_INSN_SIZE,
  119.                                        PPC_INSN_SIZE, byte_order);
  120.       if (insn != 0x44000002)
  121.         continue;

  122.       return 1;
  123.     }

  124.   return 0;
  125. }

  126. static struct trad_frame_cache *
  127. ppcobsd_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
  128. {
  129.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  130.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  131.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  132.   struct trad_frame_cache *cache;
  133.   CORE_ADDR addr, base, func;
  134.   gdb_byte buf[PPC_INSN_SIZE];
  135.   unsigned long insn, sigcontext_offset;
  136.   int i;

  137.   if (*this_cache)
  138.     return *this_cache;

  139.   cache = trad_frame_cache_zalloc (this_frame);
  140.   *this_cache = cache;

  141.   func = get_frame_pc (this_frame);
  142.   func &= ~(ppcobsd_page_size - 1);
  143.   if (!safe_frame_unwind_memory (this_frame, func, buf, sizeof buf))
  144.     return cache;

  145.   /* Calculate the offset where we can find `struct sigcontext'.  We
  146.      base our calculation on the amount of stack space reserved by the
  147.      first instruction of the signal trampoline.  */
  148.   insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
  149.   sigcontext_offset = (0x10000 - (insn & 0x0000ffff)) + 8;

  150.   base = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
  151.   addr = base + sigcontext_offset + 2 * tdep->wordsize;
  152.   for (i = 0; i < ppc_num_gprs; i++, addr += tdep->wordsize)
  153.     {
  154.       int regnum = i + tdep->ppc_gp0_regnum;
  155.       trad_frame_set_reg_addr (cache, regnum, addr);
  156.     }
  157.   trad_frame_set_reg_addr (cache, tdep->ppc_lr_regnum, addr);
  158.   addr += tdep->wordsize;
  159.   trad_frame_set_reg_addr (cache, tdep->ppc_cr_regnum, addr);
  160.   addr += tdep->wordsize;
  161.   trad_frame_set_reg_addr (cache, tdep->ppc_xer_regnum, addr);
  162.   addr += tdep->wordsize;
  163.   trad_frame_set_reg_addr (cache, tdep->ppc_ctr_regnum, addr);
  164.   addr += tdep->wordsize;
  165.   trad_frame_set_reg_addr (cache, gdbarch_pc_regnum (gdbarch), addr);
  166.   /* SRR0?  */
  167.   addr += tdep->wordsize;

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

  170.   return cache;
  171. }

  172. static void
  173. ppcobsd_sigtramp_frame_this_id (struct frame_info *this_frame,
  174.                                 void **this_cache, struct frame_id *this_id)
  175. {
  176.   struct trad_frame_cache *cache =
  177.     ppcobsd_sigtramp_frame_cache (this_frame, this_cache);

  178.   trad_frame_get_id (cache, this_id);
  179. }

  180. static struct value *
  181. ppcobsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
  182.                                       void **this_cache, int regnum)
  183. {
  184.   struct trad_frame_cache *cache =
  185.     ppcobsd_sigtramp_frame_cache (this_frame, this_cache);

  186.   return trad_frame_get_register (cache, this_frame, regnum);
  187. }

  188. static const struct frame_unwind ppcobsd_sigtramp_frame_unwind = {
  189.   SIGTRAMP_FRAME,
  190.   default_frame_unwind_stop_reason,
  191.   ppcobsd_sigtramp_frame_this_id,
  192.   ppcobsd_sigtramp_frame_prev_register,
  193.   NULL,
  194.   ppcobsd_sigtramp_frame_sniffer
  195. };


  196. static void
  197. ppcobsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  198. {
  199.   /* OpenBSD doesn't support the 128-bit `long double' from the psABI.  */
  200.   set_gdbarch_long_double_bit (gdbarch, 64);
  201.   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);

  202.   /* OpenBSD currently uses a broken GCC.  */
  203.   set_gdbarch_return_value (gdbarch, ppc_sysv_abi_broken_return_value);

  204.   /* OpenBSD uses SVR4-style shared libraries.  */
  205.   set_solib_svr4_fetch_link_map_offsets
  206.     (gdbarch, svr4_ilp32_fetch_link_map_offsets);

  207.   set_gdbarch_iterate_over_regset_sections
  208.     (gdbarch, ppcobsd_iterate_over_regset_sections);

  209.   frame_unwind_append_unwinder (gdbarch, &ppcobsd_sigtramp_frame_unwind);
  210. }


  211. /* OpenBSD uses uses the traditional NetBSD core file format, even for
  212.    ports that use ELF.  */
  213. #define GDB_OSABI_NETBSD_CORE GDB_OSABI_OPENBSD_ELF

  214. static enum gdb_osabi
  215. ppcobsd_core_osabi_sniffer (bfd *abfd)
  216. {
  217.   if (strcmp (bfd_get_target (abfd), "netbsd-core") == 0)
  218.     return GDB_OSABI_NETBSD_CORE;

  219.   return GDB_OSABI_UNKNOWN;
  220. }


  221. /* Provide a prototype to silence -Wmissing-prototypes.  */
  222. void _initialize_ppcobsd_tdep (void);

  223. void
  224. _initialize_ppcobsd_tdep (void)
  225. {
  226.   /* BFD doesn't set a flavour for NetBSD style a.out core files.  */
  227.   gdbarch_register_osabi_sniffer (bfd_arch_powerpc, bfd_target_unknown_flavour,
  228.                                   ppcobsd_core_osabi_sniffer);

  229.   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_OPENBSD_ELF,
  230.                           ppcobsd_init_abi);
  231.   gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_OPENBSD_ELF,
  232.                           ppcobsd_init_abi);

  233.   /* Avoid initializing the register offsets again if they were
  234.      already initailized by ppcobsd-nat.c.  */
  235.   if (ppcobsd_reg_offsets.pc_offset == 0)
  236.     {
  237.       /* General-purpose registers.  */
  238.       ppcobsd_reg_offsets.r0_offset = 0;
  239.       ppcobsd_reg_offsets.gpr_size = 4;
  240.       ppcobsd_reg_offsets.xr_size = 4;
  241.       ppcobsd_reg_offsets.pc_offset = 384;
  242.       ppcobsd_reg_offsets.ps_offset = 388;
  243.       ppcobsd_reg_offsets.cr_offset = 392;
  244.       ppcobsd_reg_offsets.lr_offset = 396;
  245.       ppcobsd_reg_offsets.ctr_offset = 400;
  246.       ppcobsd_reg_offsets.xer_offset = 404;
  247.       ppcobsd_reg_offsets.mq_offset = 408;

  248.       /* Floating-point registers.  */
  249.       ppcobsd_reg_offsets.f0_offset = 128;
  250.       ppcobsd_reg_offsets.fpscr_offset = -1;

  251.       /* AltiVec registers.  */
  252.       ppcobsd_reg_offsets.vr0_offset = 0;
  253.       ppcobsd_reg_offsets.vscr_offset = 512;
  254.       ppcobsd_reg_offsets.vrsave_offset = 520;
  255.     }

  256.   if (ppcobsd_fpreg_offsets.fpscr_offset == 0)
  257.     {
  258.       /* Floating-point registers.  */
  259.       ppcobsd_reg_offsets.f0_offset = 0;
  260.       ppcobsd_reg_offsets.fpscr_offset = 256;
  261.       ppcobsd_reg_offsets.fpscr_size = 4;
  262.     }
  263. }