alpha-netbsd-tdep.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* Target-dependent code for NetBSD/alpha.
  2. Copyright (C) 2002-2022 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 "alpha-bsd-tdep.h"
  24. #include "netbsd-tdep.h"
  25. #include "solib-svr4.h"
  26. #include "target.h"
  27. /* Core file support. */
  28. /* Sizeof `struct reg' in <machine/reg.h>. */
  29. #define ALPHANBSD_SIZEOF_GREGS (32 * 8)
  30. /* Sizeof `struct fpreg' in <machine/reg.h. */
  31. #define ALPHANBSD_SIZEOF_FPREGS ((32 * 8) + 8)
  32. /* Supply register REGNUM from the buffer specified by FPREGS and LEN
  33. in the floating-point register set REGSET to register cache
  34. REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
  35. static void
  36. alphanbsd_supply_fpregset (const struct regset *regset,
  37. struct regcache *regcache,
  38. int regnum, const void *fpregs, size_t len)
  39. {
  40. const gdb_byte *regs = (const gdb_byte *) fpregs;
  41. int i;
  42. gdb_assert (len >= ALPHANBSD_SIZEOF_FPREGS);
  43. for (i = ALPHA_FP0_REGNUM; i < ALPHA_FP0_REGNUM + 31; i++)
  44. {
  45. if (regnum == i || regnum == -1)
  46. regcache->raw_supply (i, regs + (i - ALPHA_FP0_REGNUM) * 8);
  47. }
  48. if (regnum == ALPHA_FPCR_REGNUM || regnum == -1)
  49. regcache->raw_supply (ALPHA_FPCR_REGNUM, regs + 32 * 8);
  50. }
  51. /* Supply register REGNUM from the buffer specified by GREGS and LEN
  52. in the general-purpose register set REGSET to register cache
  53. REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
  54. static void
  55. alphanbsd_aout_supply_gregset (const struct regset *regset,
  56. struct regcache *regcache,
  57. int regnum, const void *gregs, size_t len)
  58. {
  59. const gdb_byte *regs = (const gdb_byte *) gregs;
  60. int i;
  61. /* Table to map a GDB register number to a trapframe register index. */
  62. static const int regmap[] =
  63. {
  64. 0, 1, 2, 3,
  65. 4, 5, 6, 7,
  66. 8, 9, 10, 11,
  67. 12, 13, 14, 15,
  68. 30, 31, 32, 16,
  69. 17, 18, 19, 20,
  70. 21, 22, 23, 24,
  71. 25, 29, 26
  72. };
  73. gdb_assert (len >= ALPHANBSD_SIZEOF_GREGS);
  74. for (i = 0; i < ARRAY_SIZE(regmap); i++)
  75. {
  76. if (regnum == i || regnum == -1)
  77. regcache->raw_supply (i, regs + regmap[i] * 8);
  78. }
  79. if (regnum == ALPHA_PC_REGNUM || regnum == -1)
  80. regcache->raw_supply (ALPHA_PC_REGNUM, regs + 31 * 8);
  81. if (len >= ALPHANBSD_SIZEOF_GREGS + ALPHANBSD_SIZEOF_FPREGS)
  82. {
  83. regs += ALPHANBSD_SIZEOF_GREGS;
  84. len -= ALPHANBSD_SIZEOF_GREGS;
  85. alphanbsd_supply_fpregset (regset, regcache, regnum, regs, len);
  86. }
  87. }
  88. /* Supply register REGNUM from the buffer specified by GREGS and LEN
  89. in the general-purpose register set REGSET to register cache
  90. REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
  91. static void
  92. alphanbsd_supply_gregset (const struct regset *regset,
  93. struct regcache *regcache,
  94. int regnum, const void *gregs, size_t len)
  95. {
  96. const gdb_byte *regs = (const gdb_byte *) gregs;
  97. int i;
  98. if (len >= ALPHANBSD_SIZEOF_GREGS + ALPHANBSD_SIZEOF_FPREGS)
  99. {
  100. alphanbsd_aout_supply_gregset (regset, regcache, regnum, gregs, len);
  101. return;
  102. }
  103. for (i = 0; i < ALPHA_ZERO_REGNUM; i++)
  104. {
  105. if (regnum == i || regnum == -1)
  106. regcache->raw_supply (i, regs + i * 8);
  107. }
  108. if (regnum == ALPHA_PC_REGNUM || regnum == -1)
  109. regcache->raw_supply (ALPHA_PC_REGNUM, regs + 31 * 8);
  110. }
  111. /* NetBSD/alpha register sets. */
  112. static const struct regset alphanbsd_gregset =
  113. {
  114. NULL,
  115. alphanbsd_supply_gregset,
  116. NULL,
  117. REGSET_VARIABLE_SIZE
  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_SIZEOF_GREGS,
  132. &alphanbsd_gregset, NULL, cb_data);
  133. cb (".reg2", ALPHANBSD_SIZEOF_FPREGS, ALPHANBSD_SIZEOF_FPREGS,
  134. &alphanbsd_fpregset, NULL, cb_data);
  135. }
  136. /* Signal trampolines. */
  137. /* Under NetBSD/alpha, signal handler invocations can be identified by the
  138. designated code sequence that is used to return from a signal handler.
  139. In particular, the return address of a signal handler points to the
  140. following code sequence:
  141. ldq a0, 0(sp)
  142. lda sp, 16(sp)
  143. lda v0, 295(zero) # __sigreturn14
  144. call_pal callsys
  145. Each instruction has a unique encoding, so we simply attempt to match
  146. the instruction the PC is pointing to with any of the above instructions.
  147. If there is a hit, we know the offset to the start of the designated
  148. sequence and can then check whether we really are executing in the
  149. signal trampoline. If not, -1 is returned, otherwise the offset from the
  150. start of the return sequence is returned. */
  151. static const gdb_byte sigtramp_retcode[] =
  152. {
  153. 0x00, 0x00, 0x1e, 0xa6, /* ldq a0, 0(sp) */
  154. 0x10, 0x00, 0xde, 0x23, /* lda sp, 16(sp) */
  155. 0x27, 0x01, 0x1f, 0x20, /* lda v0, 295(zero) */
  156. 0x83, 0x00, 0x00, 0x00, /* call_pal callsys */
  157. };
  158. #define RETCODE_NWORDS 4
  159. #define RETCODE_SIZE (RETCODE_NWORDS * 4)
  160. static LONGEST
  161. alphanbsd_sigtramp_offset (struct gdbarch *gdbarch, CORE_ADDR pc)
  162. {
  163. gdb_byte ret[RETCODE_SIZE], w[4];
  164. LONGEST off;
  165. int i;
  166. if (target_read_memory (pc, w, 4) != 0)
  167. return -1;
  168. for (i = 0; i < RETCODE_NWORDS; i++)
  169. {
  170. if (memcmp (w, sigtramp_retcode + (i * 4), 4) == 0)
  171. break;
  172. }
  173. if (i == RETCODE_NWORDS)
  174. return (-1);
  175. off = i * 4;
  176. pc -= off;
  177. if (target_read_memory (pc, ret, sizeof (ret)) != 0)
  178. return -1;
  179. if (memcmp (ret, sigtramp_retcode, RETCODE_SIZE) == 0)
  180. return off;
  181. return -1;
  182. }
  183. static int
  184. alphanbsd_pc_in_sigtramp (struct gdbarch *gdbarch,
  185. CORE_ADDR pc, const char *func_name)
  186. {
  187. return (nbsd_pc_in_sigtramp (pc, func_name)
  188. || alphanbsd_sigtramp_offset (gdbarch, pc) >= 0);
  189. }
  190. static CORE_ADDR
  191. alphanbsd_sigcontext_addr (struct frame_info *frame)
  192. {
  193. /* FIXME: This is not correct for all versions of NetBSD/alpha.
  194. We will probably need to disassemble the trampoline to figure
  195. out which trampoline frame type we have. */
  196. if (!get_next_frame (frame))
  197. return 0;
  198. return get_frame_base (get_next_frame (frame));
  199. }
  200. static void
  201. alphanbsd_init_abi (struct gdbarch_info info,
  202. struct gdbarch *gdbarch)
  203. {
  204. alpha_gdbarch_tdep *tdep = (alpha_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  205. /* Hook into the DWARF CFI frame unwinder. */
  206. alpha_dwarf2_init_abi (info, gdbarch);
  207. /* Hook into the MDEBUG frame unwinder. */
  208. alpha_mdebug_init_abi (info, gdbarch);
  209. nbsd_init_abi (info, gdbarch);
  210. /* NetBSD/alpha does not provide single step support via ptrace(2); we
  211. must use software single-stepping. */
  212. set_gdbarch_software_single_step (gdbarch, alpha_software_single_step);
  213. /* NetBSD/alpha has SVR4-style shared libraries. */
  214. set_solib_svr4_fetch_link_map_offsets
  215. (gdbarch, svr4_lp64_fetch_link_map_offsets);
  216. tdep->dynamic_sigtramp_offset = alphanbsd_sigtramp_offset;
  217. tdep->pc_in_sigtramp = alphanbsd_pc_in_sigtramp;
  218. tdep->sigcontext_addr = alphanbsd_sigcontext_addr;
  219. tdep->jb_pc = 2;
  220. tdep->jb_elt_size = 8;
  221. set_gdbarch_iterate_over_regset_sections
  222. (gdbarch, alphanbsd_iterate_over_regset_sections);
  223. }
  224. void _initialize_alphanbsd_tdep ();
  225. void
  226. _initialize_alphanbsd_tdep ()
  227. {
  228. /* Even though NetBSD/alpha used ELF since day one, it used the
  229. traditional a.out-style core dump format before NetBSD 1.6, but
  230. we don't support those. */
  231. gdbarch_register_osabi (bfd_arch_alpha, 0, GDB_OSABI_NETBSD,
  232. alphanbsd_init_abi);
  233. }