gdb/alphanbsd-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for NetBSD/alpha.

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

  3.    Contributed by Wasabi Systems, Inc.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "frame.h"
  17. #include "gdbcore.h"
  18. #include "osabi.h"
  19. #include "regcache.h"
  20. #include "regset.h"
  21. #include "value.h"

  22. #include "alpha-tdep.h"
  23. #include "alphabsd-tdep.h"
  24. #include "nbsd-tdep.h"
  25. #include "solib-svr4.h"
  26. #include "target.h"

  27. /* Core file support.  */

  28. /* Even though NetBSD/alpha used ELF since day one, it used the
  29.    traditional a.out-style core dump format before NetBSD 1.6.  */

  30. /* Sizeof `struct reg' in <machine/reg.h>.  */
  31. #define ALPHANBSD_SIZEOF_GREGS        (32 * 8)

  32. /* Sizeof `struct fpreg' in <machine/reg.h.  */
  33. #define ALPHANBSD_SIZEOF_FPREGS        ((32 * 8) + 8)

  34. /* Supply register REGNUM from the buffer specified by FPREGS and LEN
  35.    in the floating-point register set REGSET to register cache
  36.    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */

  37. static void
  38. alphanbsd_supply_fpregset (const struct regset *regset,
  39.                            struct regcache *regcache,
  40.                            int regnum, const void *fpregs, size_t len)
  41. {
  42.   const gdb_byte *regs = fpregs;
  43.   int i;

  44.   gdb_assert (len >= ALPHANBSD_SIZEOF_FPREGS);

  45.   for (i = ALPHA_FP0_REGNUM; i < ALPHA_FP0_REGNUM + 31; i++)
  46.     {
  47.       if (regnum == i || regnum == -1)
  48.         regcache_raw_supply (regcache, i, regs + (i - ALPHA_FP0_REGNUM) * 8);
  49.     }

  50.   if (regnum == ALPHA_FPCR_REGNUM || regnum == -1)
  51.     regcache_raw_supply (regcache, ALPHA_FPCR_REGNUM, regs + 32 * 8);
  52. }

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

  56. static void
  57. alphanbsd_aout_supply_gregset (const struct regset *regset,
  58.                                struct regcache *regcache,
  59.                                int regnum, const void *gregs, size_t len)
  60. {
  61.   const gdb_byte *regs = gregs;
  62.   int i;

  63.   /* Table to map a GDB register number to a trapframe register index.  */
  64.   static const int regmap[] =
  65.   {
  66.      0,   1,   2,   3,
  67.      4,   5,   6,   7,
  68.      8,   91011,
  69.     12131415,
  70.     30313216,
  71.     17181920,
  72.     21222324,
  73.     252926
  74.   };

  75.   gdb_assert (len >= ALPHANBSD_SIZEOF_GREGS);

  76.   for (i = 0; i < ARRAY_SIZE(regmap); i++)
  77.     {
  78.       if (regnum == i || regnum == -1)
  79.         regcache_raw_supply (regcache, i, regs + regmap[i] * 8);
  80.     }

  81.   if (regnum == ALPHA_PC_REGNUM || regnum == -1)
  82.     regcache_raw_supply (regcache, ALPHA_PC_REGNUM, regs + 31 * 8);

  83.   if (len >= ALPHANBSD_SIZEOF_GREGS + ALPHANBSD_SIZEOF_FPREGS)
  84.     {
  85.       regs += ALPHANBSD_SIZEOF_GREGS;
  86.       len -= ALPHANBSD_SIZEOF_GREGS;
  87.       alphanbsd_supply_fpregset (regset, regcache, regnum, regs, len);
  88.     }
  89. }

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

  93. static void
  94. alphanbsd_supply_gregset (const struct regset *regset,
  95.                           struct regcache *regcache,
  96.                           int regnum, const void *gregs, size_t len)
  97. {
  98.   const gdb_byte *regs = gregs;
  99.   int i;

  100.   if (len >= ALPHANBSD_SIZEOF_GREGS + ALPHANBSD_SIZEOF_FPREGS)
  101.     {
  102.       alphanbsd_aout_supply_gregset (regset, regcache, regnum, gregs, len);
  103.       return;
  104.     }

  105.   for (i = 0; i < ALPHA_ZERO_REGNUM; i++)
  106.     {
  107.       if (regnum == i || regnum == -1)
  108.         regcache_raw_supply (regcache, i, regs + i * 8);
  109.     }

  110.   if (regnum == ALPHA_PC_REGNUM || regnum == -1)
  111.     regcache_raw_supply (regcache, ALPHA_PC_REGNUM, regs + 31 * 8);
  112. }

  113. /* NetBSD/alpha register sets.  */

  114. static const struct regset alphanbsd_gregset =
  115. {
  116.   NULL,
  117.   alphanbsd_supply_gregset
  118. };

  119. static const struct regset alphanbsd_fpregset =
  120. {
  121.   NULL,
  122.   alphanbsd_supply_fpregset
  123. };

  124. /* Iterate over supported core file register note sections. */

  125. void
  126. alphanbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  127.                                         iterate_over_regset_sections_cb *cb,
  128.                                         void *cb_data,
  129.                                         const struct regcache *regcache)
  130. {
  131.   cb (".reg", ALPHANBSD_SIZEOF_GREGS, &alphanbsd_gregset, NULL, cb_data);
  132.   cb (".reg2", ALPHANBSD_SIZEOF_FPREGS, &alphanbsd_fpregset, NULL, cb_data);
  133. }


  134. /* Signal trampolines.  */

  135. /* Under NetBSD/alpha, signal handler invocations can be identified by the
  136.    designated code sequence that is used to return from a signal handler.
  137.    In particular, the return address of a signal handler points to the
  138.    following code sequence:

  139.         ldq        a0, 0(sp)
  140.         lda        sp, 16(sp)
  141.         lda        v0, 295(zero)        # __sigreturn14
  142.         call_pal callsys

  143.    Each instruction has a unique encoding, so we simply attempt to match
  144.    the instruction the PC is pointing to with any of the above instructions.
  145.    If there is a hit, we know the offset to the start of the designated
  146.    sequence and can then check whether we really are executing in the
  147.    signal trampoline.  If not, -1 is returned, otherwise the offset from the
  148.    start of the return sequence is returned.  */
  149. static const gdb_byte sigtramp_retcode[] =
  150. {
  151.   0x00, 0x00, 0x1e, 0xa6,        /* ldq a0, 0(sp) */
  152.   0x10, 0x00, 0xde, 0x23,        /* lda sp, 16(sp) */
  153.   0x27, 0x01, 0x1f, 0x20,        /* lda v0, 295(zero) */
  154.   0x83, 0x00, 0x00, 0x00,        /* call_pal callsys */
  155. };
  156. #define RETCODE_NWORDS                4
  157. #define RETCODE_SIZE                (RETCODE_NWORDS * 4)

  158. static LONGEST
  159. alphanbsd_sigtramp_offset (struct gdbarch *gdbarch, CORE_ADDR pc)
  160. {
  161.   gdb_byte ret[RETCODE_SIZE], w[4];
  162.   LONGEST off;
  163.   int i;

  164.   if (target_read_memory (pc, w, 4) != 0)
  165.     return -1;

  166.   for (i = 0; i < RETCODE_NWORDS; i++)
  167.     {
  168.       if (memcmp (w, sigtramp_retcode + (i * 4), 4) == 0)
  169.         break;
  170.     }
  171.   if (i == RETCODE_NWORDS)
  172.     return (-1);

  173.   off = i * 4;
  174.   pc -= off;

  175.   if (target_read_memory (pc, ret, sizeof (ret)) != 0)
  176.     return -1;

  177.   if (memcmp (ret, sigtramp_retcode, RETCODE_SIZE) == 0)
  178.     return off;

  179.   return -1;
  180. }

  181. static int
  182. alphanbsd_pc_in_sigtramp (struct gdbarch *gdbarch,
  183.                            CORE_ADDR pc, const char *func_name)
  184. {
  185.   return (nbsd_pc_in_sigtramp (pc, func_name)
  186.           || alphanbsd_sigtramp_offset (gdbarch, pc) >= 0);
  187. }

  188. static CORE_ADDR
  189. alphanbsd_sigcontext_addr (struct frame_info *frame)
  190. {
  191.   /* FIXME: This is not correct for all versions of NetBSD/alpha.
  192.      We will probably need to disassemble the trampoline to figure
  193.      out which trampoline frame type we have.  */
  194.   if (!get_next_frame (frame))
  195.     return 0;
  196.   return get_frame_base (get_next_frame (frame));
  197. }


  198. static void
  199. alphanbsd_init_abi (struct gdbarch_info info,
  200.                     struct gdbarch *gdbarch)
  201. {
  202.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  203.   /* Hook into the DWARF CFI frame unwinder.  */
  204.   alpha_dwarf2_init_abi (info, gdbarch);

  205.   /* Hook into the MDEBUG frame unwinder.  */
  206.   alpha_mdebug_init_abi (info, gdbarch);

  207.   /* NetBSD/alpha does not provide single step support via ptrace(2); we
  208.      must use software single-stepping.  */
  209.   set_gdbarch_software_single_step (gdbarch, alpha_software_single_step);

  210.   /* NetBSD/alpha has SVR4-style shared libraries.  */
  211.   set_solib_svr4_fetch_link_map_offsets
  212.     (gdbarch, svr4_lp64_fetch_link_map_offsets);

  213.   tdep->dynamic_sigtramp_offset = alphanbsd_sigtramp_offset;
  214.   tdep->pc_in_sigtramp = alphanbsd_pc_in_sigtramp;
  215.   tdep->sigcontext_addr = alphanbsd_sigcontext_addr;

  216.   tdep->jb_pc = 2;
  217.   tdep->jb_elt_size = 8;

  218.   set_gdbarch_iterate_over_regset_sections
  219.     (gdbarch, alphanbsd_iterate_over_regset_sections);
  220. }


  221. static enum gdb_osabi
  222. alphanbsd_core_osabi_sniffer (bfd *abfd)
  223. {
  224.   if (strcmp (bfd_get_target (abfd), "netbsd-core") == 0)
  225.     return GDB_OSABI_NETBSD_ELF;

  226.   return GDB_OSABI_UNKNOWN;
  227. }


  228. /* Provide a prototype to silence -Wmissing-prototypes.  */
  229. void _initialize_alphanbsd_tdep (void);

  230. void
  231. _initialize_alphanbsd_tdep (void)
  232. {
  233.   /* BFD doesn't set a flavour for NetBSD style a.out core files.  */
  234.   gdbarch_register_osabi_sniffer (bfd_arch_alpha, bfd_target_unknown_flavour,
  235.                                   alphanbsd_core_osabi_sniffer);

  236.   gdbarch_register_osabi (bfd_arch_alpha, 0, GDB_OSABI_NETBSD_ELF,
  237.                           alphanbsd_init_abi);
  238. }