m32r-linux-tdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* Target-dependent code for GNU/Linux m32r.
  2. Copyright (C) 2004-2022 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 "gdbcore.h"
  16. #include "frame.h"
  17. #include "value.h"
  18. #include "regcache.h"
  19. #include "inferior.h"
  20. #include "osabi.h"
  21. #include "reggroups.h"
  22. #include "regset.h"
  23. #include "glibc-tdep.h"
  24. #include "solib-svr4.h"
  25. #include "symtab.h"
  26. #include "trad-frame.h"
  27. #include "frame-unwind.h"
  28. #include "m32r-tdep.h"
  29. #include "linux-tdep.h"
  30. #include "gdbarch.h"
  31. /* Recognizing signal handler frames. */
  32. /* GNU/Linux has two flavors of signals. Normal signal handlers, and
  33. "realtime" (RT) signals. The RT signals can provide additional
  34. information to the signal handler if the SA_SIGINFO flag is set
  35. when establishing a signal handler using `sigaction'. It is not
  36. unlikely that future versions of GNU/Linux will support SA_SIGINFO
  37. for normal signals too. */
  38. /* When the m32r Linux kernel calls a signal handler and the
  39. SA_RESTORER flag isn't set, the return address points to a bit of
  40. code on the stack. This function returns whether the PC appears to
  41. be within this bit of code.
  42. The instruction sequence for normal signals is
  43. ldi r7, #__NR_sigreturn
  44. trap #2
  45. or 0x67 0x77 0x10 0xf2.
  46. Checking for the code sequence should be somewhat reliable, because
  47. the effect is to call the system call sigreturn. This is unlikely
  48. to occur anywhere other than in a signal trampoline.
  49. It kind of sucks that we have to read memory from the process in
  50. order to identify a signal trampoline, but there doesn't seem to be
  51. any other way. Therefore we only do the memory reads if no
  52. function name could be identified, which should be the case since
  53. the code is on the stack.
  54. Detection of signal trampolines for handlers that set the
  55. SA_RESTORER flag is in general not possible. Unfortunately this is
  56. what the GNU C Library has been doing for quite some time now.
  57. However, as of version 2.1.2, the GNU C Library uses signal
  58. trampolines (named __restore and __restore_rt) that are identical
  59. to the ones used by the kernel. Therefore, these trampolines are
  60. supported too. */
  61. static const gdb_byte linux_sigtramp_code[] = {
  62. 0x67, 0x77, 0x10, 0xf2,
  63. };
  64. /* If PC is in a sigtramp routine, return the address of the start of
  65. the routine. Otherwise, return 0. */
  66. static CORE_ADDR
  67. m32r_linux_sigtramp_start (CORE_ADDR pc, struct frame_info *this_frame)
  68. {
  69. gdb_byte buf[4];
  70. /* We only recognize a signal trampoline if PC is at the start of
  71. one of the instructions. We optimize for finding the PC at the
  72. start of the instruction sequence, as will be the case when the
  73. trampoline is not the first frame on the stack. We assume that
  74. in the case where the PC is not at the start of the instruction
  75. sequence, there will be a few trailing readable bytes on the
  76. stack. */
  77. if (pc % 2 != 0)
  78. {
  79. if (!safe_frame_unwind_memory (this_frame, pc, {buf, 2}))
  80. return 0;
  81. if (memcmp (buf, linux_sigtramp_code, 2) == 0)
  82. pc -= 2;
  83. else
  84. return 0;
  85. }
  86. if (!safe_frame_unwind_memory (this_frame, pc, {buf, 4}))
  87. return 0;
  88. if (memcmp (buf, linux_sigtramp_code, 4) != 0)
  89. return 0;
  90. return pc;
  91. }
  92. /* This function does the same for RT signals. Here the instruction
  93. sequence is
  94. ldi r7, #__NR_rt_sigreturn
  95. trap #2
  96. or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00.
  97. The effect is to call the system call rt_sigreturn. */
  98. static const gdb_byte linux_rt_sigtramp_code[] = {
  99. 0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00,
  100. };
  101. /* If PC is in a RT sigtramp routine, return the address of the start
  102. of the routine. Otherwise, return 0. */
  103. static CORE_ADDR
  104. m32r_linux_rt_sigtramp_start (CORE_ADDR pc, struct frame_info *this_frame)
  105. {
  106. gdb_byte buf[4];
  107. /* We only recognize a signal trampoline if PC is at the start of
  108. one of the instructions. We optimize for finding the PC at the
  109. start of the instruction sequence, as will be the case when the
  110. trampoline is not the first frame on the stack. We assume that
  111. in the case where the PC is not at the start of the instruction
  112. sequence, there will be a few trailing readable bytes on the
  113. stack. */
  114. if (pc % 2 != 0)
  115. return 0;
  116. if (!safe_frame_unwind_memory (this_frame, pc, {buf, 4}))
  117. return 0;
  118. if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
  119. {
  120. if (!safe_frame_unwind_memory (this_frame, pc + 4, {buf, 4}))
  121. return 0;
  122. if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
  123. return pc;
  124. }
  125. else if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
  126. {
  127. if (!safe_frame_unwind_memory (this_frame, pc - 4, {buf, 4}))
  128. return 0;
  129. if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
  130. return pc - 4;
  131. }
  132. return 0;
  133. }
  134. static int
  135. m32r_linux_pc_in_sigtramp (CORE_ADDR pc, const char *name,
  136. struct frame_info *this_frame)
  137. {
  138. /* If we have NAME, we can optimize the search. The trampolines are
  139. named __restore and __restore_rt. However, they aren't dynamically
  140. exported from the shared C library, so the trampoline may appear to
  141. be part of the preceding function. This should always be sigaction,
  142. __sigaction, or __libc_sigaction (all aliases to the same function). */
  143. if (name == NULL || strstr (name, "sigaction") != NULL)
  144. return (m32r_linux_sigtramp_start (pc, this_frame) != 0
  145. || m32r_linux_rt_sigtramp_start (pc, this_frame) != 0);
  146. return (strcmp ("__restore", name) == 0
  147. || strcmp ("__restore_rt", name) == 0);
  148. }
  149. /* From <asm/sigcontext.h>. */
  150. static int m32r_linux_sc_reg_offset[] = {
  151. 4 * 4, /* r0 */
  152. 5 * 4, /* r1 */
  153. 6 * 4, /* r2 */
  154. 7 * 4, /* r3 */
  155. 0 * 4, /* r4 */
  156. 1 * 4, /* r5 */
  157. 2 * 4, /* r6 */
  158. 8 * 4, /* r7 */
  159. 9 * 4, /* r8 */
  160. 10 * 4, /* r9 */
  161. 11 * 4, /* r10 */
  162. 12 * 4, /* r11 */
  163. 13 * 4, /* r12 */
  164. 21 * 4, /* fp */
  165. 22 * 4, /* lr */
  166. -1 * 4, /* sp */
  167. 16 * 4, /* psw */
  168. -1 * 4, /* cbr */
  169. 23 * 4, /* spi */
  170. 20 * 4, /* spu */
  171. 19 * 4, /* bpc */
  172. 17 * 4, /* pc */
  173. 15 * 4, /* accl */
  174. 14 * 4 /* acch */
  175. };
  176. struct m32r_frame_cache
  177. {
  178. CORE_ADDR base, pc;
  179. trad_frame_saved_reg *saved_regs;
  180. };
  181. static struct m32r_frame_cache *
  182. m32r_linux_sigtramp_frame_cache (struct frame_info *this_frame,
  183. void **this_cache)
  184. {
  185. struct m32r_frame_cache *cache;
  186. CORE_ADDR sigcontext_addr, addr;
  187. int regnum;
  188. if ((*this_cache) != NULL)
  189. return (struct m32r_frame_cache *) (*this_cache);
  190. cache = FRAME_OBSTACK_ZALLOC (struct m32r_frame_cache);
  191. (*this_cache) = cache;
  192. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  193. cache->base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
  194. sigcontext_addr = cache->base + 4;
  195. cache->pc = get_frame_pc (this_frame);
  196. addr = m32r_linux_sigtramp_start (cache->pc, this_frame);
  197. if (addr == 0)
  198. {
  199. /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR
  200. accordingly. */
  201. addr = m32r_linux_rt_sigtramp_start (cache->pc, this_frame);
  202. if (addr)
  203. sigcontext_addr += 128;
  204. else
  205. addr = get_frame_func (this_frame);
  206. }
  207. cache->pc = addr;
  208. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  209. for (regnum = 0; regnum < sizeof (m32r_linux_sc_reg_offset) / 4; regnum++)
  210. {
  211. if (m32r_linux_sc_reg_offset[regnum] >= 0)
  212. cache->saved_regs[regnum].set_addr (sigcontext_addr
  213. + m32r_linux_sc_reg_offset[regnum]);
  214. }
  215. return cache;
  216. }
  217. static void
  218. m32r_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
  219. void **this_cache,
  220. struct frame_id *this_id)
  221. {
  222. struct m32r_frame_cache *cache =
  223. m32r_linux_sigtramp_frame_cache (this_frame, this_cache);
  224. (*this_id) = frame_id_build (cache->base, cache->pc);
  225. }
  226. static struct value *
  227. m32r_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
  228. void **this_cache, int regnum)
  229. {
  230. struct m32r_frame_cache *cache =
  231. m32r_linux_sigtramp_frame_cache (this_frame, this_cache);
  232. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  233. }
  234. static int
  235. m32r_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
  236. struct frame_info *this_frame,
  237. void **this_cache)
  238. {
  239. CORE_ADDR pc = get_frame_pc (this_frame);
  240. const char *name;
  241. find_pc_partial_function (pc, &name, NULL, NULL);
  242. if (m32r_linux_pc_in_sigtramp (pc, name, this_frame))
  243. return 1;
  244. return 0;
  245. }
  246. static const struct frame_unwind m32r_linux_sigtramp_frame_unwind = {
  247. "m32r linux sigtramp",
  248. SIGTRAMP_FRAME,
  249. default_frame_unwind_stop_reason,
  250. m32r_linux_sigtramp_frame_this_id,
  251. m32r_linux_sigtramp_frame_prev_register,
  252. NULL,
  253. m32r_linux_sigtramp_frame_sniffer
  254. };
  255. /* Mapping between the registers in `struct pt_regs'
  256. format and GDB's register array layout. */
  257. static int m32r_pt_regs_offset[] = {
  258. 4 * 4, /* r0 */
  259. 4 * 5, /* r1 */
  260. 4 * 6, /* r2 */
  261. 4 * 7, /* r3 */
  262. 4 * 0, /* r4 */
  263. 4 * 1, /* r5 */
  264. 4 * 2, /* r6 */
  265. 4 * 8, /* r7 */
  266. 4 * 9, /* r8 */
  267. 4 * 10, /* r9 */
  268. 4 * 11, /* r10 */
  269. 4 * 12, /* r11 */
  270. 4 * 13, /* r12 */
  271. 4 * 24, /* fp */
  272. 4 * 25, /* lr */
  273. 4 * 23, /* sp */
  274. 4 * 19, /* psw */
  275. 4 * 19, /* cbr */
  276. 4 * 26, /* spi */
  277. 4 * 23, /* spu */
  278. 4 * 22, /* bpc */
  279. 4 * 20, /* pc */
  280. 4 * 16, /* accl */
  281. 4 * 15 /* acch */
  282. };
  283. #define PSW_OFFSET (4 * 19)
  284. #define BBPSW_OFFSET (4 * 21)
  285. #define SPU_OFFSET (4 * 23)
  286. #define SPI_OFFSET (4 * 26)
  287. #define M32R_LINUX_GREGS_SIZE (4 * 28)
  288. static void
  289. m32r_linux_supply_gregset (const struct regset *regset,
  290. struct regcache *regcache, int regnum,
  291. const void *gregs, size_t size)
  292. {
  293. const gdb_byte *regs = (const gdb_byte *) gregs;
  294. enum bfd_endian byte_order =
  295. gdbarch_byte_order (regcache->arch ());
  296. ULONGEST psw, bbpsw;
  297. gdb_byte buf[4];
  298. const gdb_byte *p;
  299. int i;
  300. psw = extract_unsigned_integer (regs + PSW_OFFSET, 4, byte_order);
  301. bbpsw = extract_unsigned_integer (regs + BBPSW_OFFSET, 4, byte_order);
  302. psw = ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8);
  303. for (i = 0; i < ARRAY_SIZE (m32r_pt_regs_offset); i++)
  304. {
  305. if (regnum != -1 && regnum != i)
  306. continue;
  307. switch (i)
  308. {
  309. case PSW_REGNUM:
  310. store_unsigned_integer (buf, 4, byte_order, psw);
  311. p = buf;
  312. break;
  313. case CBR_REGNUM:
  314. store_unsigned_integer (buf, 4, byte_order, psw & 1);
  315. p = buf;
  316. break;
  317. case M32R_SP_REGNUM:
  318. p = regs + ((psw & 0x80) ? SPU_OFFSET : SPI_OFFSET);
  319. break;
  320. default:
  321. p = regs + m32r_pt_regs_offset[i];
  322. }
  323. regcache->raw_supply (i, p);
  324. }
  325. }
  326. static void
  327. m32r_linux_collect_gregset (const struct regset *regset,
  328. const struct regcache *regcache,
  329. int regnum, void *gregs, size_t size)
  330. {
  331. gdb_byte *regs = (gdb_byte *) gregs;
  332. int i;
  333. enum bfd_endian byte_order =
  334. gdbarch_byte_order (regcache->arch ());
  335. ULONGEST psw;
  336. gdb_byte buf[4];
  337. regcache->raw_collect (PSW_REGNUM, buf);
  338. psw = extract_unsigned_integer (buf, 4, byte_order);
  339. for (i = 0; i < ARRAY_SIZE (m32r_pt_regs_offset); i++)
  340. {
  341. if (regnum != -1 && regnum != i)
  342. continue;
  343. switch (i)
  344. {
  345. case PSW_REGNUM:
  346. store_unsigned_integer (regs + PSW_OFFSET, 4, byte_order,
  347. (psw & 0xc1) << 8);
  348. store_unsigned_integer (regs + BBPSW_OFFSET, 4, byte_order,
  349. (psw >> 8) & 0xc1);
  350. break;
  351. case CBR_REGNUM:
  352. break;
  353. case M32R_SP_REGNUM:
  354. regcache->raw_collect
  355. (i, regs + ((psw & 0x80) ? SPU_OFFSET : SPI_OFFSET));
  356. break;
  357. default:
  358. regcache->raw_collect (i, regs + m32r_pt_regs_offset[i]);
  359. }
  360. }
  361. }
  362. static const struct regset m32r_linux_gregset = {
  363. NULL,
  364. m32r_linux_supply_gregset, m32r_linux_collect_gregset
  365. };
  366. static void
  367. m32r_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  368. iterate_over_regset_sections_cb *cb,
  369. void *cb_data,
  370. const struct regcache *regcache)
  371. {
  372. cb (".reg", M32R_LINUX_GREGS_SIZE, M32R_LINUX_GREGS_SIZE, &m32r_linux_gregset,
  373. NULL, cb_data);
  374. }
  375. static void
  376. m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  377. {
  378. linux_init_abi (info, gdbarch, 0);
  379. /* Since EVB register is not available for native debug, we reduce
  380. the number of registers. */
  381. set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS - 1);
  382. frame_unwind_append_unwinder (gdbarch, &m32r_linux_sigtramp_frame_unwind);
  383. /* GNU/Linux uses SVR4-style shared libraries. */
  384. set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  385. set_solib_svr4_fetch_link_map_offsets
  386. (gdbarch, linux_ilp32_fetch_link_map_offsets);
  387. /* Core file support. */
  388. set_gdbarch_iterate_over_regset_sections
  389. (gdbarch, m32r_linux_iterate_over_regset_sections);
  390. /* Enable TLS support. */
  391. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  392. svr4_fetch_objfile_link_map);
  393. }
  394. void _initialize_m32r_linux_tdep ();
  395. void
  396. _initialize_m32r_linux_tdep ()
  397. {
  398. gdbarch_register_osabi (bfd_arch_m32r, 0, GDB_OSABI_LINUX,
  399. m32r_linux_init_abi);
  400. }