gdb/mipsnbsd-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

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

  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 "gdbcore.h"
  17. #include "regcache.h"
  18. #include "regset.h"
  19. #include "target.h"
  20. #include "value.h"
  21. #include "osabi.h"

  22. #include "nbsd-tdep.h"
  23. #include "mipsnbsd-tdep.h"
  24. #include "mips-tdep.h"

  25. #include "solib-svr4.h"

  26. /* Shorthand for some register numbers used below.  */
  27. #define MIPS_PC_REGNUM  MIPS_EMBED_PC_REGNUM
  28. #define MIPS_FP0_REGNUM MIPS_EMBED_FP0_REGNUM
  29. #define MIPS_FSR_REGNUM MIPS_EMBED_FP0_REGNUM + 32

  30. /* Core file support.  */

  31. /* Number of registers in `struct reg' from <machine/reg.h>.  */
  32. #define MIPSNBSD_NUM_GREGS        38

  33. /* Number of registers in `struct fpreg' from <machine/reg.h>.  */
  34. #define MIPSNBSD_NUM_FPREGS        33

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

  38. static void
  39. mipsnbsd_supply_fpregset (const struct regset *regset,
  40.                           struct regcache *regcache,
  41.                           int regnum, const void *fpregs, size_t len)
  42. {
  43.   size_t regsize = mips_isa_regsize (get_regcache_arch (regcache));
  44.   const char *regs = fpregs;
  45.   int i;

  46.   gdb_assert (len >= MIPSNBSD_NUM_FPREGS * regsize);

  47.   for (i = MIPS_FP0_REGNUM; i <= MIPS_FSR_REGNUM; i++)
  48.     {
  49.       if (regnum == i || regnum == -1)
  50.         regcache_raw_supply (regcache, i,
  51.                              regs + (i - MIPS_FP0_REGNUM) * regsize);
  52.     }
  53. }

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

  57. static void
  58. mipsnbsd_supply_gregset (const struct regset *regset,
  59.                          struct regcache *regcache, int regnum,
  60.                          const void *gregs, size_t len)
  61. {
  62.   size_t regsize = mips_isa_regsize (get_regcache_arch (regcache));
  63.   const char *regs = gregs;
  64.   int i;

  65.   gdb_assert (len >= MIPSNBSD_NUM_GREGS * regsize);

  66.   for (i = 0; i <= MIPS_PC_REGNUM; i++)
  67.     {
  68.       if (regnum == i || regnum == -1)
  69.         regcache_raw_supply (regcache, i, regs + i * regsize);
  70.     }

  71.   if (len >= (MIPSNBSD_NUM_GREGS + MIPSNBSD_NUM_FPREGS) * regsize)
  72.     {
  73.       regs += MIPSNBSD_NUM_GREGS * regsize;
  74.       len -= MIPSNBSD_NUM_GREGS * regsize;
  75.       mipsnbsd_supply_fpregset (regset, regcache, regnum, regs, len);
  76.     }
  77. }

  78. /* NetBSD/mips register sets.  */

  79. static const struct regset mipsnbsd_gregset =
  80. {
  81.   NULL,
  82.   mipsnbsd_supply_gregset
  83. };

  84. static const struct regset mipsnbsd_fpregset =
  85. {
  86.   NULL,
  87.   mipsnbsd_supply_fpregset
  88. };

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

  90. static void
  91. mipsnbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  92.                                        iterate_over_regset_sections_cb *cb,
  93.                                        void *cb_data,
  94.                                        const struct regcache *regcache)
  95. {
  96.   size_t regsize = mips_isa_regsize (gdbarch);

  97.   cb (".reg", MIPSNBSD_NUM_GREGS * regsize, &mipsnbsd_gregset,
  98.       NULL, cb_data);
  99.   cb (".reg2", MIPSNBSD_NUM_FPREGS * regsize, &mipsnbsd_fpregset,
  100.       NULL, cb_data);
  101. }


  102. /* Conveniently, GDB uses the same register numbering as the
  103.    ptrace register structure used by NetBSD/mips.  */

  104. void
  105. mipsnbsd_supply_reg (struct regcache *regcache, const char *regs, int regno)
  106. {
  107.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  108.   int i;

  109.   for (i = 0; i <= gdbarch_pc_regnum (gdbarch); i++)
  110.     {
  111.       if (regno == i || regno == -1)
  112.         {
  113.           if (gdbarch_cannot_fetch_register (gdbarch, i))
  114.             regcache_raw_supply (regcache, i, NULL);
  115.           else
  116.             regcache_raw_supply (regcache, i,
  117.                                  regs + (i * mips_isa_regsize (gdbarch)));
  118.         }
  119.     }
  120. }

  121. void
  122. mipsnbsd_fill_reg (const struct regcache *regcache, char *regs, int regno)
  123. {
  124.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  125.   int i;

  126.   for (i = 0; i <= gdbarch_pc_regnum (gdbarch); i++)
  127.     if ((regno == i || regno == -1)
  128.         && ! gdbarch_cannot_store_register (gdbarch, i))
  129.       regcache_raw_collect (regcache, i,
  130.                             regs + (i * mips_isa_regsize (gdbarch)));
  131. }

  132. void
  133. mipsnbsd_supply_fpreg (struct regcache *regcache,
  134.                        const char *fpregs, int regno)
  135. {
  136.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  137.   int i;

  138.   for (i = gdbarch_fp0_regnum (gdbarch);
  139.        i <= mips_regnum (gdbarch)->fp_implementation_revision;
  140.        i++)
  141.     {
  142.       if (regno == i || regno == -1)
  143.         {
  144.           if (gdbarch_cannot_fetch_register (gdbarch, i))
  145.             regcache_raw_supply (regcache, i, NULL);
  146.           else
  147.             regcache_raw_supply (regcache, i,
  148.                                  fpregs
  149.                                  + ((i - gdbarch_fp0_regnum (gdbarch))
  150.                                     * mips_isa_regsize (gdbarch)));
  151.         }
  152.     }
  153. }

  154. void
  155. mipsnbsd_fill_fpreg (const struct regcache *regcache, char *fpregs, int regno)
  156. {
  157.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  158.   int i;

  159.   for (i = gdbarch_fp0_regnum (gdbarch);
  160.        i <= mips_regnum (gdbarch)->fp_control_status;
  161.        i++)
  162.     if ((regno == i || regno == -1)
  163.         && ! gdbarch_cannot_store_register (gdbarch, i))
  164.       regcache_raw_collect (regcache, i,
  165.                             fpregs + ((i - gdbarch_fp0_regnum (gdbarch))
  166.                               * mips_isa_regsize (gdbarch)));
  167. }

  168. #if 0

  169. /* Under NetBSD/mips, signal handler invocations can be identified by the
  170.    designated code sequence that is used to return from a signal handler.
  171.    In particular, the return address of a signal handler points to the
  172.    following code sequence:

  173.         addu        a0, sp, 16
  174.         li        v0, 295                        # __sigreturn14
  175.         syscall

  176.    Each instruction has a unique encoding, so we simply attempt to match
  177.    the instruction the PC is pointing to with any of the above instructions.
  178.    If there is a hit, we know the offset to the start of the designated
  179.    sequence and can then check whether we really are executing in the
  180.    signal trampoline.  If not, -1 is returned, otherwise the offset from the
  181.    start of the return sequence is returned.  */

  182. #define RETCODE_NWORDS        3
  183. #define RETCODE_SIZE        (RETCODE_NWORDS * 4)

  184. static const unsigned char sigtramp_retcode_mipsel[RETCODE_SIZE] =
  185. {
  186.   0x10, 0x00, 0xa4, 0x27,        /* addu a0, sp, 16 */
  187.   0x27, 0x01, 0x02, 0x24,        /* li v0, 295 */
  188.   0x0c, 0x00, 0x00, 0x00,        /* syscall */
  189. };

  190. static const unsigned char sigtramp_retcode_mipseb[RETCODE_SIZE] =
  191. {
  192.   0x27, 0xa4, 0x00, 0x10,        /* addu a0, sp, 16 */
  193.   0x24, 0x02, 0x01, 0x27,        /* li v0, 295 */
  194.   0x00, 0x00, 0x00, 0x0c,        /* syscall */
  195. };

  196. #endif

  197. /* Figure out where the longjmp will land.  We expect that we have
  198.    just entered longjmp and haven't yet setup the stack frame, so the
  199.    args are still in the argument regs.  MIPS_A0_REGNUM points at the
  200.    jmp_buf structure from which we extract the PC that we will land
  201.    at.  The PC is copied into *pc.  This routine returns true on
  202.    success.  */

  203. #define NBSD_MIPS_JB_PC                        (2 * 4)
  204. #define NBSD_MIPS_JB_ELEMENT_SIZE(gdbarch)        mips_isa_regsize (gdbarch)
  205. #define NBSD_MIPS_JB_OFFSET(gdbarch)                (NBSD_MIPS_JB_PC * \
  206.                                          NBSD_MIPS_JB_ELEMENT_SIZE (gdbarch))

  207. static int
  208. mipsnbsd_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  209. {
  210.   struct gdbarch *gdbarch = get_frame_arch (frame);
  211.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  212.   CORE_ADDR jb_addr;
  213.   gdb_byte *buf;

  214.   buf = alloca (NBSD_MIPS_JB_ELEMENT_SIZE (gdbarch));

  215.   jb_addr = get_frame_register_unsigned (frame, MIPS_A0_REGNUM);

  216.   if (target_read_memory (jb_addr + NBSD_MIPS_JB_OFFSET (gdbarch), buf,
  217.                             NBSD_MIPS_JB_ELEMENT_SIZE (gdbarch)))
  218.     return 0;

  219.   *pc = extract_unsigned_integer (buf, NBSD_MIPS_JB_ELEMENT_SIZE (gdbarch),
  220.                                   byte_order);
  221.   return 1;
  222. }

  223. static int
  224. mipsnbsd_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
  225. {
  226.   return (regno == MIPS_ZERO_REGNUM
  227.           || regno == mips_regnum (gdbarch)->fp_implementation_revision);
  228. }

  229. static int
  230. mipsnbsd_cannot_store_register (struct gdbarch *gdbarch, int regno)
  231. {
  232.   return (regno == MIPS_ZERO_REGNUM
  233.           || regno == mips_regnum (gdbarch)->fp_implementation_revision);
  234. }

  235. /* Shared library support.  */

  236. /* NetBSD/mips uses a slightly different `struct link_map' than the
  237.    other NetBSD platforms.  */

  238. static struct link_map_offsets *
  239. mipsnbsd_ilp32_fetch_link_map_offsets (void)
  240. {
  241.   static struct link_map_offsets lmo;
  242.   static struct link_map_offsets *lmp = NULL;

  243.   if (lmp == NULL)
  244.     {
  245.       lmp = &lmo;

  246.       lmo.r_version_offset = 0;
  247.       lmo.r_version_size = 4;
  248.       lmo.r_map_offset = 4;
  249.       lmo.r_brk_offset = 8;
  250.       lmo.r_ldsomap_offset = -1;

  251.       /* Everything we need is in the first 24 bytes.  */
  252.       lmo.link_map_size = 24;
  253.       lmo.l_addr_offset = 4;
  254.       lmo.l_name_offset = 8;
  255.       lmo.l_ld_offset = 12;
  256.       lmo.l_next_offset = 16;
  257.       lmo.l_prev_offset = 20;
  258.     }

  259.   return lmp;
  260. }

  261. static struct link_map_offsets *
  262. mipsnbsd_lp64_fetch_link_map_offsets (void)
  263. {
  264.   static struct link_map_offsets lmo;
  265.   static struct link_map_offsets *lmp = NULL;

  266.   if (lmp == NULL)
  267.     {
  268.       lmp = &lmo;

  269.       lmo.r_version_offset = 0;
  270.       lmo.r_version_size = 4;
  271.       lmo.r_map_offset = 8;
  272.       lmo.r_brk_offset = 16;
  273.       lmo.r_ldsomap_offset = -1;

  274.       /* Everything we need is in the first 40 bytes.  */
  275.       lmo.link_map_size = 48;
  276.       lmo.l_addr_offset = 0;
  277.       lmo.l_name_offset = 16;
  278.       lmo.l_ld_offset = 24;
  279.       lmo.l_next_offset = 32;
  280.       lmo.l_prev_offset = 40;
  281.     }

  282.   return lmp;
  283. }


  284. static void
  285. mipsnbsd_init_abi (struct gdbarch_info info,
  286.                    struct gdbarch *gdbarch)
  287. {
  288.   set_gdbarch_iterate_over_regset_sections
  289.     (gdbarch, mipsnbsd_iterate_over_regset_sections);

  290.   set_gdbarch_get_longjmp_target (gdbarch, mipsnbsd_get_longjmp_target);

  291.   set_gdbarch_cannot_fetch_register (gdbarch, mipsnbsd_cannot_fetch_register);
  292.   set_gdbarch_cannot_store_register (gdbarch, mipsnbsd_cannot_store_register);

  293.   set_gdbarch_software_single_step (gdbarch, mips_software_single_step);

  294.   /* NetBSD/mips has SVR4-style shared libraries.  */
  295.   set_solib_svr4_fetch_link_map_offsets
  296.     (gdbarch, (gdbarch_ptr_bit (gdbarch) == 32 ?
  297.                mipsnbsd_ilp32_fetch_link_map_offsets :
  298.                mipsnbsd_lp64_fetch_link_map_offsets));
  299. }


  300. /* Provide a prototype to silence -Wmissing-prototypes.  */
  301. extern initialize_file_ftype _initialize_mipsnbsd_tdep;

  302. void
  303. _initialize_mipsnbsd_tdep (void)
  304. {
  305.   gdbarch_register_osabi (bfd_arch_mips, 0, GDB_OSABI_NETBSD_ELF,
  306.                           mipsnbsd_init_abi);
  307. }