m68k-linux-tdep.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* Motorola m68k target-dependent support for GNU/Linux.
  2. Copyright (C) 1996-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 "target.h"
  18. #include "gdbtypes.h"
  19. #include "osabi.h"
  20. #include "regcache.h"
  21. #include "objfiles.h"
  22. #include "symtab.h"
  23. #include "m68k-tdep.h"
  24. #include "trad-frame.h"
  25. #include "frame-unwind.h"
  26. #include "glibc-tdep.h"
  27. #include "solib-svr4.h"
  28. #include "auxv.h"
  29. #include "observable.h"
  30. #include "elf/common.h"
  31. #include "linux-tdep.h"
  32. #include "regset.h"
  33. /* Offsets (in target ints) into jmp_buf. */
  34. #define M68K_LINUX_JB_ELEMENT_SIZE 4
  35. #define M68K_LINUX_JB_PC 7
  36. /* Check whether insn1 and insn2 are parts of a signal trampoline. */
  37. #define IS_SIGTRAMP(insn1, insn2) \
  38. (/* addaw #20,sp; moveq #119,d0; trap #0 */ \
  39. (insn1 == 0xdefc0014 && insn2 == 0x70774e40) \
  40. /* moveq #119,d0; trap #0 */ \
  41. || insn1 == 0x70774e40)
  42. #define IS_RT_SIGTRAMP(insn1, insn2) \
  43. (/* movel #173,d0; trap #0 */ \
  44. (insn1 == 0x203c0000 && insn2 == 0x00ad4e40) \
  45. /* moveq #82,d0; notb d0; trap #0 */ \
  46. || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))
  47. /* Return non-zero if THIS_FRAME corresponds to a signal trampoline. For
  48. the sake of m68k_linux_get_sigtramp_info we also distinguish between
  49. non-RT and RT signal trampolines. */
  50. static int
  51. m68k_linux_pc_in_sigtramp (struct frame_info *this_frame)
  52. {
  53. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  54. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  55. gdb_byte buf[12];
  56. unsigned long insn0, insn1, insn2;
  57. CORE_ADDR pc = get_frame_pc (this_frame);
  58. if (!safe_frame_unwind_memory (this_frame, pc - 4, {buf, sizeof (buf)}))
  59. return 0;
  60. insn1 = extract_unsigned_integer (buf + 4, 4, byte_order);
  61. insn2 = extract_unsigned_integer (buf + 8, 4, byte_order);
  62. if (IS_SIGTRAMP (insn1, insn2))
  63. return 1;
  64. if (IS_RT_SIGTRAMP (insn1, insn2))
  65. return 2;
  66. insn0 = extract_unsigned_integer (buf, 4, byte_order);
  67. if (IS_SIGTRAMP (insn0, insn1))
  68. return 1;
  69. if (IS_RT_SIGTRAMP (insn0, insn1))
  70. return 2;
  71. insn0 = ((insn0 << 16) & 0xffffffff) | (insn1 >> 16);
  72. insn1 = ((insn1 << 16) & 0xffffffff) | (insn2 >> 16);
  73. if (IS_SIGTRAMP (insn0, insn1))
  74. return 1;
  75. if (IS_RT_SIGTRAMP (insn0, insn1))
  76. return 2;
  77. return 0;
  78. }
  79. /* From <asm/sigcontext.h>. */
  80. static int m68k_linux_sigcontext_reg_offset[M68K_NUM_REGS] =
  81. {
  82. 2 * 4, /* %d0 */
  83. 3 * 4, /* %d1 */
  84. -1, /* %d2 */
  85. -1, /* %d3 */
  86. -1, /* %d4 */
  87. -1, /* %d5 */
  88. -1, /* %d6 */
  89. -1, /* %d7 */
  90. 4 * 4, /* %a0 */
  91. 5 * 4, /* %a1 */
  92. -1, /* %a2 */
  93. -1, /* %a3 */
  94. -1, /* %a4 */
  95. -1, /* %a5 */
  96. -1, /* %fp */
  97. 1 * 4, /* %sp */
  98. 6 * 4, /* %sr */
  99. 6 * 4 + 2, /* %pc */
  100. 8 * 4, /* %fp0 */
  101. 11 * 4, /* %fp1 */
  102. -1, /* %fp2 */
  103. -1, /* %fp3 */
  104. -1, /* %fp4 */
  105. -1, /* %fp5 */
  106. -1, /* %fp6 */
  107. -1, /* %fp7 */
  108. 14 * 4, /* %fpcr */
  109. 15 * 4, /* %fpsr */
  110. 16 * 4 /* %fpiaddr */
  111. };
  112. static int m68k_uclinux_sigcontext_reg_offset[M68K_NUM_REGS] =
  113. {
  114. 2 * 4, /* %d0 */
  115. 3 * 4, /* %d1 */
  116. -1, /* %d2 */
  117. -1, /* %d3 */
  118. -1, /* %d4 */
  119. -1, /* %d5 */
  120. -1, /* %d6 */
  121. -1, /* %d7 */
  122. 4 * 4, /* %a0 */
  123. 5 * 4, /* %a1 */
  124. -1, /* %a2 */
  125. -1, /* %a3 */
  126. -1, /* %a4 */
  127. 6 * 4, /* %a5 */
  128. -1, /* %fp */
  129. 1 * 4, /* %sp */
  130. 7 * 4, /* %sr */
  131. 7 * 4 + 2, /* %pc */
  132. -1, /* %fp0 */
  133. -1, /* %fp1 */
  134. -1, /* %fp2 */
  135. -1, /* %fp3 */
  136. -1, /* %fp4 */
  137. -1, /* %fp5 */
  138. -1, /* %fp6 */
  139. -1, /* %fp7 */
  140. -1, /* %fpcr */
  141. -1, /* %fpsr */
  142. -1 /* %fpiaddr */
  143. };
  144. /* From <asm/ucontext.h>. */
  145. static int m68k_linux_ucontext_reg_offset[M68K_NUM_REGS] =
  146. {
  147. 6 * 4, /* %d0 */
  148. 7 * 4, /* %d1 */
  149. 8 * 4, /* %d2 */
  150. 9 * 4, /* %d3 */
  151. 10 * 4, /* %d4 */
  152. 11 * 4, /* %d5 */
  153. 12 * 4, /* %d6 */
  154. 13 * 4, /* %d7 */
  155. 14 * 4, /* %a0 */
  156. 15 * 4, /* %a1 */
  157. 16 * 4, /* %a2 */
  158. 17 * 4, /* %a3 */
  159. 18 * 4, /* %a4 */
  160. 19 * 4, /* %a5 */
  161. 20 * 4, /* %fp */
  162. 21 * 4, /* %sp */
  163. 23 * 4, /* %sr */
  164. 22 * 4, /* %pc */
  165. 27 * 4, /* %fp0 */
  166. 30 * 4, /* %fp1 */
  167. 33 * 4, /* %fp2 */
  168. 36 * 4, /* %fp3 */
  169. 39 * 4, /* %fp4 */
  170. 42 * 4, /* %fp5 */
  171. 45 * 4, /* %fp6 */
  172. 48 * 4, /* %fp7 */
  173. 24 * 4, /* %fpcr */
  174. 25 * 4, /* %fpsr */
  175. 26 * 4 /* %fpiaddr */
  176. };
  177. /* Get info about saved registers in sigtramp. */
  178. struct m68k_linux_sigtramp_info
  179. {
  180. /* Address of sigcontext. */
  181. CORE_ADDR sigcontext_addr;
  182. /* Offset of registers in `struct sigcontext'. */
  183. int *sc_reg_offset;
  184. };
  185. /* Nonzero if running on uClinux. */
  186. static int target_is_uclinux;
  187. static void
  188. m68k_linux_inferior_created (inferior *inf)
  189. {
  190. /* Record that we will need to re-evaluate whether we are running on a
  191. uClinux or normal GNU/Linux target (see m68k_linux_get_sigtramp_info). */
  192. target_is_uclinux = -1;
  193. }
  194. static struct m68k_linux_sigtramp_info
  195. m68k_linux_get_sigtramp_info (struct frame_info *this_frame)
  196. {
  197. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  198. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  199. CORE_ADDR sp;
  200. struct m68k_linux_sigtramp_info info;
  201. /* Determine whether we are running on a uClinux or normal GNU/Linux
  202. target so we can use the correct sigcontext layouts. */
  203. if (target_is_uclinux == -1)
  204. target_is_uclinux = linux_is_uclinux ();
  205. sp = get_frame_register_unsigned (this_frame, M68K_SP_REGNUM);
  206. /* Get sigcontext address, it is the third parameter on the stack. */
  207. info.sigcontext_addr = read_memory_unsigned_integer (sp + 8, 4, byte_order);
  208. if (m68k_linux_pc_in_sigtramp (this_frame) == 2)
  209. info.sc_reg_offset = m68k_linux_ucontext_reg_offset;
  210. else
  211. info.sc_reg_offset = (target_is_uclinux
  212. ? m68k_uclinux_sigcontext_reg_offset
  213. : m68k_linux_sigcontext_reg_offset);
  214. return info;
  215. }
  216. /* Signal trampolines. */
  217. static struct trad_frame_cache *
  218. m68k_linux_sigtramp_frame_cache (struct frame_info *this_frame,
  219. void **this_cache)
  220. {
  221. struct frame_id this_id;
  222. struct trad_frame_cache *cache;
  223. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  224. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  225. struct m68k_linux_sigtramp_info info;
  226. gdb_byte buf[4];
  227. int i;
  228. if (*this_cache)
  229. return (struct trad_frame_cache *) *this_cache;
  230. cache = trad_frame_cache_zalloc (this_frame);
  231. /* FIXME: cagney/2004-05-01: This is is long standing broken code.
  232. The frame ID's code address should be the start-address of the
  233. signal trampoline and not the current PC within that
  234. trampoline. */
  235. get_frame_register (this_frame, M68K_SP_REGNUM, buf);
  236. /* See the end of m68k_push_dummy_call. */
  237. this_id = frame_id_build (extract_unsigned_integer (buf, 4, byte_order)
  238. - 4 + 8, get_frame_pc (this_frame));
  239. trad_frame_set_id (cache, this_id);
  240. info = m68k_linux_get_sigtramp_info (this_frame);
  241. for (i = 0; i < M68K_NUM_REGS; i++)
  242. if (info.sc_reg_offset[i] != -1)
  243. trad_frame_set_reg_addr (cache, i,
  244. info.sigcontext_addr + info.sc_reg_offset[i]);
  245. *this_cache = cache;
  246. return cache;
  247. }
  248. static void
  249. m68k_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
  250. void **this_cache,
  251. struct frame_id *this_id)
  252. {
  253. struct trad_frame_cache *cache =
  254. m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
  255. trad_frame_get_id (cache, this_id);
  256. }
  257. static struct value *
  258. m68k_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
  259. void **this_cache,
  260. int regnum)
  261. {
  262. /* Make sure we've initialized the cache. */
  263. struct trad_frame_cache *cache =
  264. m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
  265. return trad_frame_get_register (cache, this_frame, regnum);
  266. }
  267. static int
  268. m68k_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
  269. struct frame_info *this_frame,
  270. void **this_prologue_cache)
  271. {
  272. return m68k_linux_pc_in_sigtramp (this_frame);
  273. }
  274. static const struct frame_unwind m68k_linux_sigtramp_frame_unwind =
  275. {
  276. "m68k linux sigtramp",
  277. SIGTRAMP_FRAME,
  278. default_frame_unwind_stop_reason,
  279. m68k_linux_sigtramp_frame_this_id,
  280. m68k_linux_sigtramp_frame_prev_register,
  281. NULL,
  282. m68k_linux_sigtramp_frame_sniffer
  283. };
  284. /* Register maps for supply/collect regset functions. */
  285. static const struct regcache_map_entry m68k_linux_gregmap[] =
  286. {
  287. { 7, M68K_D1_REGNUM, 4 }, /* d1 ... d7 */
  288. { 7, M68K_A0_REGNUM, 4 }, /* a0 ... a6 */
  289. { 1, M68K_D0_REGNUM, 4 },
  290. { 1, M68K_SP_REGNUM, 4 },
  291. { 1, REGCACHE_MAP_SKIP, 4 }, /* orig_d0 (skip) */
  292. { 1, M68K_PS_REGNUM, 4 },
  293. { 1, M68K_PC_REGNUM, 4 },
  294. /* Ignore 16-bit fields 'fmtvec' and '__fill'. */
  295. { 0 }
  296. };
  297. #define M68K_LINUX_GREGS_SIZE (20 * 4)
  298. static const struct regcache_map_entry m68k_linux_fpregmap[] =
  299. {
  300. { 8, M68K_FP0_REGNUM, 12 }, /* fp0 ... fp7 */
  301. { 1, M68K_FPC_REGNUM, 4 },
  302. { 1, M68K_FPS_REGNUM, 4 },
  303. { 1, M68K_FPI_REGNUM, 4 },
  304. { 0 }
  305. };
  306. #define M68K_LINUX_FPREGS_SIZE (27 * 4)
  307. /* Register sets. */
  308. static const struct regset m68k_linux_gregset =
  309. {
  310. m68k_linux_gregmap,
  311. regcache_supply_regset, regcache_collect_regset
  312. };
  313. static const struct regset m68k_linux_fpregset =
  314. {
  315. m68k_linux_fpregmap,
  316. regcache_supply_regset, regcache_collect_regset
  317. };
  318. /* Iterate over core file register note sections. */
  319. static void
  320. m68k_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  321. iterate_over_regset_sections_cb *cb,
  322. void *cb_data,
  323. const struct regcache *regcache)
  324. {
  325. cb (".reg", M68K_LINUX_GREGS_SIZE, M68K_LINUX_GREGS_SIZE, &m68k_linux_gregset,
  326. NULL, cb_data);
  327. cb (".reg2", M68K_LINUX_FPREGS_SIZE, M68K_LINUX_FPREGS_SIZE,
  328. &m68k_linux_fpregset, NULL, cb_data);
  329. }
  330. static void
  331. m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  332. {
  333. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  334. linux_init_abi (info, gdbarch, 0);
  335. tdep->jb_pc = M68K_LINUX_JB_PC;
  336. tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
  337. /* GNU/Linux uses a calling convention that's similar to SVR4. It
  338. returns integer values in %d0/%d1, pointer values in %a0 and
  339. floating values in %fp0, just like SVR4, but uses %a1 to pass the
  340. address to store a structure value. It also returns small
  341. structures in registers instead of memory. */
  342. m68k_svr4_init_abi (info, gdbarch);
  343. tdep->struct_value_regnum = M68K_A1_REGNUM;
  344. tdep->struct_return = reg_struct_return;
  345. set_gdbarch_decr_pc_after_break (gdbarch, 2);
  346. frame_unwind_append_unwinder (gdbarch, &m68k_linux_sigtramp_frame_unwind);
  347. /* Shared library handling. */
  348. /* GNU/Linux uses SVR4-style shared libraries. */
  349. set_solib_svr4_fetch_link_map_offsets (gdbarch,
  350. linux_ilp32_fetch_link_map_offsets);
  351. /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
  352. set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
  353. set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  354. /* Core file support. */
  355. set_gdbarch_iterate_over_regset_sections
  356. (gdbarch, m68k_linux_iterate_over_regset_sections);
  357. /* Enable TLS support. */
  358. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  359. svr4_fetch_objfile_link_map);
  360. }
  361. void _initialize_m68k_linux_tdep ();
  362. void
  363. _initialize_m68k_linux_tdep ()
  364. {
  365. gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_LINUX,
  366. m68k_linux_init_abi);
  367. gdb::observers::inferior_created.attach (m68k_linux_inferior_created,
  368. "m68k-linux-tdep");
  369. }