gdb/sparcnbsd-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

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

  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 "frame-unwind.h"
  18. #include "gdbcore.h"
  19. #include "gdbtypes.h"
  20. #include "osabi.h"
  21. #include "regcache.h"
  22. #include "regset.h"
  23. #include "solib-svr4.h"
  24. #include "symtab.h"
  25. #include "trad-frame.h"

  26. #include "sparc-tdep.h"
  27. #include "nbsd-tdep.h"

  28. /* Macros to extract fields from SPARC instructions.  */
  29. #define X_RS1(i) (((i) >> 14) & 0x1f)
  30. #define X_RS2(i) ((i) & 0x1f)
  31. #define X_I(i) (((i) >> 13) & 1)

  32. const struct sparc_gregmap sparc32nbsd_gregmap =
  33. {
  34.   0 * 4,                        /* %psr */
  35.   1 * 4,                        /* %pc */
  36.   2 * 4,                        /* %npc */
  37.   3 * 4,                        /* %y */
  38.   -1,                                /* %wim */
  39.   -1,                                /* %tbr */
  40.   5 * 4,                        /* %g1 */
  41.   -1                                /* %l0 */
  42. };

  43. static void
  44. sparc32nbsd_supply_gregset (const struct regset *regset,
  45.                             struct regcache *regcache,
  46.                             int regnum, const void *gregs, size_t len)
  47. {
  48.   sparc32_supply_gregset (&sparc32nbsd_gregmap, regcache, regnum, gregs);

  49.   /* Traditional NetBSD core files don't use multiple register sets.
  50.      Instead, the general-purpose and floating-point registers are
  51.      lumped together in a single section.  */
  52.   if (len >= 212)
  53.     sparc32_supply_fpregset (&sparc32_bsd_fpregmap, regcache, regnum,
  54.                              (const char *) gregs + 80);
  55. }

  56. static void
  57. sparc32nbsd_supply_fpregset (const struct regset *regset,
  58.                              struct regcache *regcache,
  59.                              int regnum, const void *fpregs, size_t len)
  60. {
  61.   sparc32_supply_fpregset (&sparc32_bsd_fpregmap, regcache, regnum, fpregs);
  62. }


  63. /* Signal trampolines.  */

  64. /* The following variables describe the location of an on-stack signal
  65.    trampoline.  The current values correspond to the memory layout for
  66.    NetBSD 1.3 and up.  These shouldn't be necessary for NetBSD 2.0 and
  67.    up, since NetBSD uses signal trampolines provided by libc now.  */

  68. static const CORE_ADDR sparc32nbsd_sigtramp_start = 0xeffffef0;
  69. static const CORE_ADDR sparc32nbsd_sigtramp_end = 0xeffffff0;

  70. static int
  71. sparc32nbsd_pc_in_sigtramp (CORE_ADDR pc, const char *name)
  72. {
  73.   if (pc >= sparc32nbsd_sigtramp_start && pc < sparc32nbsd_sigtramp_end)
  74.     return 1;

  75.   return nbsd_pc_in_sigtramp (pc, name);
  76. }

  77. struct trad_frame_saved_reg *
  78. sparc32nbsd_sigcontext_saved_regs (struct frame_info *this_frame)
  79. {
  80.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  81.   struct trad_frame_saved_reg *saved_regs;
  82.   CORE_ADDR addr, sigcontext_addr;
  83.   int regnum, delta;
  84.   ULONGEST psr;

  85.   saved_regs = trad_frame_alloc_saved_regs (this_frame);

  86.   /* We find the appropriate instance of `struct sigcontext' at a
  87.      fixed offset in the signal frame.  */
  88.   addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  89.   sigcontext_addr = addr + 64 + 16;

  90.   /* The registers are saved in bits and pieces scattered all over the
  91.      place.  The code below records their location on the assumption
  92.      that the part of the signal trampoline that saves the state has
  93.      been executed.  */

  94.   saved_regs[SPARC_SP_REGNUM].addr = sigcontext_addr + 8;
  95.   saved_regs[SPARC32_PC_REGNUM].addr = sigcontext_addr + 12;
  96.   saved_regs[SPARC32_NPC_REGNUM].addr = sigcontext_addr + 16;
  97.   saved_regs[SPARC32_PSR_REGNUM].addr = sigcontext_addr + 20;
  98.   saved_regs[SPARC_G1_REGNUM].addr = sigcontext_addr + 24;
  99.   saved_regs[SPARC_O0_REGNUM].addr = sigcontext_addr + 28;

  100.   /* The remaining `global' registers and %y are saved in the `local'
  101.      registers.  */
  102.   delta = SPARC_L0_REGNUM - SPARC_G0_REGNUM;
  103.   for (regnum = SPARC_G2_REGNUM; regnum <= SPARC_G7_REGNUM; regnum++)
  104.     saved_regs[regnum].realreg = regnum + delta;
  105.   saved_regs[SPARC32_Y_REGNUM].realreg = SPARC_L1_REGNUM;

  106.   /* The remaining `out' registers can be found in the current frame's
  107.      `in' registers.  */
  108.   delta = SPARC_I0_REGNUM - SPARC_O0_REGNUM;
  109.   for (regnum = SPARC_O1_REGNUM; regnum <= SPARC_O5_REGNUM; regnum++)
  110.     saved_regs[regnum].realreg = regnum + delta;
  111.   saved_regs[SPARC_O7_REGNUM].realreg = SPARC_I7_REGNUM;

  112.   /* The `local' and `in' registers have been saved in the register
  113.      save area.  */
  114.   addr = saved_regs[SPARC_SP_REGNUM].addr;
  115.   addr = get_frame_memory_unsigned (this_frame, addr, 4);
  116.   for (regnum = SPARC_L0_REGNUM;
  117.        regnum <= SPARC_I7_REGNUM; regnum++, addr += 4)
  118.     saved_regs[regnum].addr = addr;

  119.   /* Handle StackGhost.  */
  120.   {
  121.     ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);

  122.     if (wcookie != 0)
  123.       {
  124.         ULONGEST i7;

  125.         addr = saved_regs[SPARC_I7_REGNUM].addr;
  126.         i7 = get_frame_memory_unsigned (this_frame, addr, 4);
  127.         trad_frame_set_value (saved_regs, SPARC_I7_REGNUM, i7 ^ wcookie);
  128.       }
  129.   }

  130.   /* The floating-point registers are only saved if the EF bit in %prs
  131.      has been set.  */

  132. #define PSR_EF        0x00001000

  133.   addr = saved_regs[SPARC32_PSR_REGNUM].addr;
  134.   psr = get_frame_memory_unsigned (this_frame, addr, 4);
  135.   if (psr & PSR_EF)
  136.     {
  137.       CORE_ADDR sp;

  138.       sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  139.       saved_regs[SPARC32_FSR_REGNUM].addr = sp + 96;
  140.       for (regnum = SPARC_F0_REGNUM, addr = sp + 96 + 8;
  141.            regnum <= SPARC_F31_REGNUM; regnum++, addr += 4)
  142.         saved_regs[regnum].addr = addr;
  143.     }

  144.   return saved_regs;
  145. }

  146. static struct sparc_frame_cache *
  147. sparc32nbsd_sigcontext_frame_cache (struct frame_info *this_frame,
  148.                                     void **this_cache)
  149. {
  150.   struct sparc_frame_cache *cache;
  151.   CORE_ADDR addr;

  152.   if (*this_cache)
  153.     return *this_cache;

  154.   cache = sparc_frame_cache (this_frame, this_cache);
  155.   gdb_assert (cache == *this_cache);

  156.   /* If we couldn't find the frame's function, we're probably dealing
  157.      with an on-stack signal trampoline.  */
  158.   if (cache->pc == 0)
  159.     {
  160.       cache->pc = sparc32nbsd_sigtramp_start;

  161.       /* Since we couldn't find the frame's function, the cache was
  162.          initialized under the assumption that we're frameless.  */
  163.       sparc_record_save_insn (cache);
  164.       addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  165.       cache->base = addr;
  166.     }

  167.   cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (this_frame);

  168.   return cache;
  169. }

  170. static void
  171. sparc32nbsd_sigcontext_frame_this_id (struct frame_info *this_frame,
  172.                                       void **this_cache,
  173.                                       struct frame_id *this_id)
  174. {
  175.   struct sparc_frame_cache *cache =
  176.     sparc32nbsd_sigcontext_frame_cache (this_frame, this_cache);

  177.   (*this_id) = frame_id_build (cache->base, cache->pc);
  178. }

  179. static struct value *
  180. sparc32nbsd_sigcontext_frame_prev_register (struct frame_info *this_frame,
  181.                                             void **this_cache, int regnum)
  182. {
  183.   struct sparc_frame_cache *cache =
  184.     sparc32nbsd_sigcontext_frame_cache (this_frame, this_cache);

  185.   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  186. }

  187. static int
  188. sparc32nbsd_sigcontext_frame_sniffer (const struct frame_unwind *self,
  189.                                       struct frame_info *this_frame,
  190.                                       void **this_cache)
  191. {
  192.   CORE_ADDR pc = get_frame_pc (this_frame);
  193.   const char *name;

  194.   find_pc_partial_function (pc, &name, NULL, NULL);
  195.   if (sparc32nbsd_pc_in_sigtramp (pc, name))
  196.     {
  197.       if (name == NULL || strncmp (name, "__sigtramp_sigcontext", 21))
  198.         return 1;
  199.     }

  200.   return 0;
  201. }

  202. static const struct frame_unwind sparc32nbsd_sigcontext_frame_unwind =
  203. {
  204.   SIGTRAMP_FRAME,
  205.   default_frame_unwind_stop_reason,
  206.   sparc32nbsd_sigcontext_frame_this_id,
  207.   sparc32nbsd_sigcontext_frame_prev_register,
  208.   NULL,
  209.   sparc32nbsd_sigcontext_frame_sniffer
  210. };

  211. /* Return the address of a system call's alternative return
  212.    address.  */

  213. CORE_ADDR
  214. sparcnbsd_step_trap (struct frame_info *frame, unsigned long insn)
  215. {
  216.   if ((X_I (insn) == 0 && X_RS1 (insn) == 0 && X_RS2 (insn) == 0)
  217.       || (X_I (insn) == 1 && X_RS1 (insn) == 0 && (insn & 0x7f) == 0))
  218.     {
  219.       /* "New" system call.  */
  220.       ULONGEST number = get_frame_register_unsigned (frame, SPARC_G1_REGNUM);

  221.       if (number & 0x400)
  222.         return get_frame_register_unsigned (frame, SPARC_G2_REGNUM);
  223.       if (number & 0x800)
  224.         return get_frame_register_unsigned (frame, SPARC_G7_REGNUM);
  225.     }

  226.   return 0;
  227. }


  228. static const struct regset sparc32nbsd_gregset =
  229.   {
  230.     NULL, sparc32nbsd_supply_gregset, NULL
  231.   };

  232. static const struct regset sparc32nbsd_fpregset =
  233.   {
  234.     NULL, sparc32nbsd_supply_fpregset, NULL
  235.   };

  236. static void
  237. sparc32nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  238. {
  239.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  240.   /* NetBSD doesn't support the 128-bit `long double' from the psABI.  */
  241.   set_gdbarch_long_double_bit (gdbarch, 64);
  242.   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);

  243.   tdep->gregset = &sparc32nbsd_gregset;
  244.   tdep->sizeof_gregset = 20 * 4;

  245.   tdep->fpregset = &sparc32nbsd_fpregset;
  246.   tdep->sizeof_fpregset = 33 * 4;

  247.   /* Make sure we can single-step "new" syscalls.  */
  248.   tdep->step_trap = sparcnbsd_step_trap;

  249.   frame_unwind_append_unwinder (gdbarch, &sparc32nbsd_sigcontext_frame_unwind);
  250. }

  251. static void
  252. sparc32nbsd_aout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  253. {
  254.   sparc32nbsd_init_abi (info, gdbarch);
  255. }

  256. void
  257. sparc32nbsd_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  258. {
  259.   sparc32nbsd_init_abi (info, gdbarch);

  260.   set_solib_svr4_fetch_link_map_offsets
  261.     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
  262. }

  263. static enum gdb_osabi
  264. sparcnbsd_aout_osabi_sniffer (bfd *abfd)
  265. {
  266.   if (strcmp (bfd_get_target (abfd), "a.out-sparc-netbsd") == 0)
  267.     return GDB_OSABI_NETBSD_AOUT;

  268.   return GDB_OSABI_UNKNOWN;
  269. }

  270. /* OpenBSD uses the traditional NetBSD core file format, even for
  271.    ports that use ELF.  Therefore, if the default OS ABI is OpenBSD
  272.    ELF, we return that instead of NetBSD a.out.  This is mainly for
  273.    the benfit of OpenBSD/sparc64, which inherits the sniffer below
  274.    since we include this file for an OpenBSD/sparc64 target.  For
  275.    OpenBSD/sparc, the NetBSD a.out OS ABI is probably similar enough
  276.    to both the OpenBSD a.out and the OpenBSD ELF OS ABI.  */
  277. #if defined (GDB_OSABI_DEFAULT) && (GDB_OSABI_DEFAULT == GDB_OSABI_OPENBSD_ELF)
  278. #define GDB_OSABI_NETBSD_CORE GDB_OSABI_OPENBSD_ELF
  279. #else
  280. #define GDB_OSABI_NETBSD_CORE GDB_OSABI_NETBSD_AOUT
  281. #endif

  282. static enum gdb_osabi
  283. sparcnbsd_core_osabi_sniffer (bfd *abfd)
  284. {
  285.   if (strcmp (bfd_get_target (abfd), "netbsd-core") == 0)
  286.     return GDB_OSABI_NETBSD_CORE;

  287.   return GDB_OSABI_UNKNOWN;
  288. }


  289. /* Provide a prototype to silence -Wmissing-prototypes.  */
  290. void _initialize_sparcnbsd_tdep (void);

  291. void
  292. _initialize_sparcnbsd_tdep (void)
  293. {
  294.   gdbarch_register_osabi_sniffer (bfd_arch_sparc, bfd_target_aout_flavour,
  295.                                   sparcnbsd_aout_osabi_sniffer);

  296.   /* BFD doesn't set a flavour for NetBSD style a.out core files.  */
  297.   gdbarch_register_osabi_sniffer (bfd_arch_sparc, bfd_target_unknown_flavour,
  298.                                   sparcnbsd_core_osabi_sniffer);

  299.   gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_NETBSD_AOUT,
  300.                           sparc32nbsd_aout_init_abi);
  301.   gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_NETBSD_ELF,
  302.                           sparc32nbsd_elf_init_abi);
  303. }