arc-linux-tdep.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* Target dependent code for GNU/Linux ARC.
  2. Copyright 2020-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. /* GDB header files. */
  15. #include "defs.h"
  16. #include "linux-tdep.h"
  17. #include "objfiles.h"
  18. #include "opcode/arc.h"
  19. #include "osabi.h"
  20. #include "solib-svr4.h"
  21. #include "disasm.h"
  22. /* ARC header files. */
  23. #include "opcodes/arc-dis.h"
  24. #include "arc-linux-tdep.h"
  25. #include "arc-tdep.h"
  26. #include "arch/arc.h"
  27. /* Print an "arc-linux" debug statement. */
  28. #define arc_linux_debug_printf(fmt, ...) \
  29. debug_prefixed_printf_cond (arc_debug, "arc-linux", fmt, ##__VA_ARGS__)
  30. #define REGOFF(offset) (offset * ARC_REGISTER_SIZE)
  31. /* arc_linux_sc_reg_offsets[i] is the offset of register i in the `struct
  32. sigcontext'. Array index is an internal GDB register number, as defined in
  33. arc-tdep.h:arc_regnum.
  34. From <include/uapi/asm/sigcontext.h> and <include/uapi/asm/ptrace.h>.
  35. The layout of this struct is tightly bound to "arc_regnum" enum
  36. in arc-tdep.h. Any change of order in there, must be reflected
  37. here as well. */
  38. static const int arc_linux_sc_reg_offsets[] = {
  39. /* R0 - R12. */
  40. REGOFF (22), REGOFF (21), REGOFF (20), REGOFF (19),
  41. REGOFF (18), REGOFF (17), REGOFF (16), REGOFF (15),
  42. REGOFF (14), REGOFF (13), REGOFF (12), REGOFF (11),
  43. REGOFF (10),
  44. /* R13 - R25. */
  45. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  46. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  47. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  48. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  49. ARC_OFFSET_NO_REGISTER,
  50. REGOFF (9), /* R26 (GP) */
  51. REGOFF (8), /* FP */
  52. REGOFF (23), /* SP */
  53. ARC_OFFSET_NO_REGISTER, /* ILINK */
  54. ARC_OFFSET_NO_REGISTER, /* R30 */
  55. REGOFF (7), /* BLINK */
  56. /* R32 - R59. */
  57. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  58. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  59. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  60. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  61. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  62. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  63. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  64. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  65. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  66. ARC_OFFSET_NO_REGISTER,
  67. REGOFF (4), /* LP_COUNT */
  68. ARC_OFFSET_NO_REGISTER, /* RESERVED */
  69. ARC_OFFSET_NO_REGISTER, /* LIMM */
  70. ARC_OFFSET_NO_REGISTER, /* PCL */
  71. REGOFF (6), /* PC */
  72. REGOFF (5), /* STATUS32 */
  73. REGOFF (2), /* LP_START */
  74. REGOFF (3), /* LP_END */
  75. REGOFF (1), /* BTA */
  76. };
  77. /* arc_linux_core_reg_offsets[i] is the offset in the .reg section of GDB
  78. regnum i. Array index is an internal GDB register number, as defined in
  79. arc-tdep.h:arc_regnum.
  80. From include/uapi/asm/ptrace.h in the ARC Linux sources. */
  81. /* The layout of this struct is tightly bound to "arc_regnum" enum
  82. in arc-tdep.h. Any change of order in there, must be reflected
  83. here as well. */
  84. static const int arc_linux_core_reg_offsets[] = {
  85. /* R0 - R12. */
  86. REGOFF (22), REGOFF (21), REGOFF (20), REGOFF (19),
  87. REGOFF (18), REGOFF (17), REGOFF (16), REGOFF (15),
  88. REGOFF (14), REGOFF (13), REGOFF (12), REGOFF (11),
  89. REGOFF (10),
  90. /* R13 - R25. */
  91. REGOFF (37), REGOFF (36), REGOFF (35), REGOFF (34),
  92. REGOFF (33), REGOFF (32), REGOFF (31), REGOFF (30),
  93. REGOFF (29), REGOFF (28), REGOFF (27), REGOFF (26),
  94. REGOFF (25),
  95. REGOFF (9), /* R26 (GP) */
  96. REGOFF (8), /* FP */
  97. REGOFF (23), /* SP */
  98. ARC_OFFSET_NO_REGISTER, /* ILINK */
  99. ARC_OFFSET_NO_REGISTER, /* R30 */
  100. REGOFF (7), /* BLINK */
  101. /* R32 - R59. */
  102. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  103. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  104. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  105. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  106. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  107. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  108. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  109. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  110. ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER,
  111. ARC_OFFSET_NO_REGISTER,
  112. REGOFF (4), /* LP_COUNT */
  113. ARC_OFFSET_NO_REGISTER, /* RESERVED */
  114. ARC_OFFSET_NO_REGISTER, /* LIMM */
  115. ARC_OFFSET_NO_REGISTER, /* PCL */
  116. REGOFF (39), /* PC */
  117. REGOFF (5), /* STATUS32 */
  118. REGOFF (2), /* LP_START */
  119. REGOFF (3), /* LP_END */
  120. REGOFF (1), /* BTA */
  121. REGOFF (6) /* ERET */
  122. };
  123. /* Is THIS_FRAME a sigtramp function - the function that returns from
  124. signal handler into normal execution flow? This is the case if the PC is
  125. either at the start of, or in the middle of the two instructions:
  126. mov r8, __NR_rt_sigreturn ; __NR_rt_sigreturn == 139
  127. trap_s 0 ; `swi' for ARC700
  128. On ARC uClibc Linux this function is called __default_rt_sa_restorer.
  129. Returns TRUE if this is a sigtramp frame. */
  130. static bool
  131. arc_linux_is_sigtramp (struct frame_info *this_frame)
  132. {
  133. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  134. CORE_ADDR pc = get_frame_pc (this_frame);
  135. arc_linux_debug_printf ("pc=%s", paddress(gdbarch, pc));
  136. static const gdb_byte insns_be_hs[] = {
  137. 0x20, 0x8a, 0x12, 0xc2, /* mov r8,nr_rt_sigreturn */
  138. 0x78, 0x1e /* trap_s 0 */
  139. };
  140. static const gdb_byte insns_be_700[] = {
  141. 0x20, 0x8a, 0x12, 0xc2, /* mov r8,nr_rt_sigreturn */
  142. 0x22, 0x6f, 0x00, 0x3f /* swi */
  143. };
  144. gdb_byte arc_sigtramp_insns[sizeof (insns_be_700)];
  145. size_t insns_sz;
  146. if (arc_mach_is_arcv2 (gdbarch))
  147. {
  148. insns_sz = sizeof (insns_be_hs);
  149. memcpy (arc_sigtramp_insns, insns_be_hs, insns_sz);
  150. }
  151. else
  152. {
  153. insns_sz = sizeof (insns_be_700);
  154. memcpy (arc_sigtramp_insns, insns_be_700, insns_sz);
  155. }
  156. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  157. {
  158. /* On little endian targets, ARC code section is in what is called
  159. "middle endian", where half-words are in the big-endian order,
  160. only bytes inside the halfwords are in the little endian order.
  161. As a result it is very easy to convert big endian instruction to
  162. little endian, since it is needed to swap bytes in the halfwords,
  163. so there is no need to have information on whether that is a
  164. 4-byte instruction or 2-byte. */
  165. gdb_assert ((insns_sz % 2) == 0);
  166. for (int i = 0; i < insns_sz; i += 2)
  167. std::swap (arc_sigtramp_insns[i], arc_sigtramp_insns[i+1]);
  168. }
  169. gdb_byte buf[insns_sz];
  170. /* Read the memory at the PC. Since we are stopped, any breakpoint must
  171. have been removed. */
  172. if (!safe_frame_unwind_memory (this_frame, pc, {buf, insns_sz}))
  173. {
  174. /* Failed to unwind frame. */
  175. return FALSE;
  176. }
  177. /* Is that code the sigtramp instruction sequence? */
  178. if (memcmp (buf, arc_sigtramp_insns, insns_sz) == 0)
  179. return TRUE;
  180. /* No - look one instruction earlier in the code... */
  181. if (!safe_frame_unwind_memory (this_frame, pc - 4, {buf, insns_sz}))
  182. {
  183. /* Failed to unwind frame. */
  184. return FALSE;
  185. }
  186. return (memcmp (buf, arc_sigtramp_insns, insns_sz) == 0);
  187. }
  188. /* Get sigcontext structure of sigtramp frame - it contains saved
  189. registers of interrupted frame.
  190. Stack pointer points to the rt_sigframe structure, and sigcontext can
  191. be found as in:
  192. struct rt_sigframe {
  193. struct siginfo info;
  194. struct ucontext uc;
  195. ...
  196. };
  197. struct ucontext {
  198. unsigned long uc_flags;
  199. struct ucontext *uc_link;
  200. stack_t uc_stack;
  201. struct sigcontext uc_mcontext;
  202. sigset_t uc_sigmask;
  203. };
  204. sizeof (struct siginfo) == 0x80
  205. offsetof (struct ucontext, uc_mcontext) == 0x14
  206. GDB cannot include linux headers and use offsetof () because those are
  207. target headers and GDB might be built for a different run host. There
  208. doesn't seem to be an established mechanism to figure out those offsets
  209. via gdbserver, so the only way is to hardcode values in the GDB,
  210. meaning that GDB will be broken if values will change. That seems to
  211. be a very unlikely scenario and other arches (aarch64, alpha, amd64,
  212. etc) in GDB hardcode values. */
  213. static CORE_ADDR
  214. arc_linux_sigcontext_addr (struct frame_info *this_frame)
  215. {
  216. const int ucontext_offset = 0x80;
  217. const int sigcontext_offset = 0x14;
  218. return get_frame_sp (this_frame) + ucontext_offset + sigcontext_offset;
  219. }
  220. /* Implement the "cannot_fetch_register" gdbarch method. */
  221. static int
  222. arc_linux_cannot_fetch_register (struct gdbarch *gdbarch, int regnum)
  223. {
  224. /* Assume that register is readable if it is unknown. */
  225. switch (regnum)
  226. {
  227. case ARC_ILINK_REGNUM:
  228. case ARC_RESERVED_REGNUM:
  229. case ARC_LIMM_REGNUM:
  230. return true;
  231. case ARC_R30_REGNUM:
  232. case ARC_R58_REGNUM:
  233. case ARC_R59_REGNUM:
  234. return !arc_mach_is_arcv2 (gdbarch);
  235. }
  236. return (regnum > ARC_BLINK_REGNUM) && (regnum < ARC_LP_COUNT_REGNUM);
  237. }
  238. /* Implement the "cannot_store_register" gdbarch method. */
  239. static int
  240. arc_linux_cannot_store_register (struct gdbarch *gdbarch, int regnum)
  241. {
  242. /* Assume that register is writable if it is unknown. */
  243. switch (regnum)
  244. {
  245. case ARC_ILINK_REGNUM:
  246. case ARC_RESERVED_REGNUM:
  247. case ARC_LIMM_REGNUM:
  248. case ARC_PCL_REGNUM:
  249. return true;
  250. case ARC_R30_REGNUM:
  251. case ARC_R58_REGNUM:
  252. case ARC_R59_REGNUM:
  253. return !arc_mach_is_arcv2 (gdbarch);
  254. }
  255. return (regnum > ARC_BLINK_REGNUM) && (regnum < ARC_LP_COUNT_REGNUM);
  256. }
  257. /* For ARC Linux, breakpoints use the 16-bit TRAP_S 1 instruction, which
  258. is 0x3e78 (little endian) or 0x783e (big endian). */
  259. static const gdb_byte arc_linux_trap_s_be[] = { 0x78, 0x3e };
  260. static const gdb_byte arc_linux_trap_s_le[] = { 0x3e, 0x78 };
  261. static const int trap_size = 2; /* Number of bytes to insert "trap". */
  262. /* Implement the "breakpoint_kind_from_pc" gdbarch method. */
  263. static int
  264. arc_linux_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  265. {
  266. return trap_size;
  267. }
  268. /* Implement the "sw_breakpoint_from_kind" gdbarch method. */
  269. static const gdb_byte *
  270. arc_linux_sw_breakpoint_from_kind (struct gdbarch *gdbarch,
  271. int kind, int *size)
  272. {
  273. gdb_assert (kind == trap_size);
  274. *size = kind;
  275. return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  276. ? arc_linux_trap_s_be
  277. : arc_linux_trap_s_le);
  278. }
  279. /* Check for an atomic sequence of instructions beginning with an
  280. LLOCK instruction and ending with a SCOND instruction.
  281. These patterns are hand coded in libc's (glibc and uclibc). Take
  282. a look at [1] for instance:
  283. main+14: llock r2,[r0]
  284. main+18: brne.nt r2,0,main+30
  285. main+22: scond r3,[r0]
  286. main+26: bne main+14
  287. main+30: mov_s r0,0
  288. If such a sequence is found, attempt to step over it.
  289. A breakpoint is placed at the end of the sequence.
  290. This function expects the INSN to be a "llock(d)" instruction.
  291. [1]
  292. https://cgit.uclibc-ng.org/cgi/cgit/uclibc-ng.git/tree/libc/ \
  293. sysdeps/linux/arc/bits/atomic.h#n46
  294. */
  295. static std::vector<CORE_ADDR>
  296. handle_atomic_sequence (arc_instruction insn, disassemble_info &di)
  297. {
  298. const int atomic_seq_len = 24; /* Instruction sequence length. */
  299. std::vector<CORE_ADDR> next_pcs;
  300. /* Sanity check. */
  301. gdb_assert (insn.insn_class == LLOCK);
  302. /* Data size we are dealing with: LLOCK vs. LLOCKD */
  303. arc_ldst_data_size llock_data_size_mode = insn.data_size_mode;
  304. /* Indicator if any conditional branch is found in the sequence. */
  305. bool found_bc = false;
  306. /* Becomes true if "LLOCK(D) .. SCOND(D)" sequence is found. */
  307. bool is_pattern_valid = false;
  308. for (int insn_count = 0; insn_count < atomic_seq_len; ++insn_count)
  309. {
  310. arc_insn_decode (arc_insn_get_linear_next_pc (insn),
  311. &di, arc_delayed_print_insn, &insn);
  312. if (insn.insn_class == BRCC)
  313. {
  314. /* If more than one conditional branch is found, this is not
  315. the pattern we are interested in. */
  316. if (found_bc)
  317. break;
  318. found_bc = true;
  319. continue;
  320. }
  321. /* This is almost a happy ending. */
  322. if (insn.insn_class == SCOND)
  323. {
  324. /* SCOND should match the LLOCK's data size. */
  325. if (insn.data_size_mode == llock_data_size_mode)
  326. is_pattern_valid = true;
  327. break;
  328. }
  329. }
  330. if (is_pattern_valid)
  331. {
  332. /* Get next instruction after scond(d). There is no limm. */
  333. next_pcs.push_back (insn.address + insn.length);
  334. }
  335. return next_pcs;
  336. }
  337. /* Implement the "software_single_step" gdbarch method. */
  338. static std::vector<CORE_ADDR>
  339. arc_linux_software_single_step (struct regcache *regcache)
  340. {
  341. struct gdbarch *gdbarch = regcache->arch ();
  342. arc_gdbarch_tdep *tdep = (arc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  343. struct disassemble_info di = arc_disassemble_info (gdbarch);
  344. /* Read current instruction. */
  345. struct arc_instruction curr_insn;
  346. arc_insn_decode (regcache_read_pc (regcache), &di, arc_delayed_print_insn,
  347. &curr_insn);
  348. if (curr_insn.insn_class == LLOCK)
  349. return handle_atomic_sequence (curr_insn, di);
  350. CORE_ADDR next_pc = arc_insn_get_linear_next_pc (curr_insn);
  351. std::vector<CORE_ADDR> next_pcs;
  352. /* For instructions with delay slots, the fall thru is not the
  353. instruction immediately after the current instruction, but the one
  354. after that. */
  355. if (curr_insn.has_delay_slot)
  356. {
  357. struct arc_instruction next_insn;
  358. arc_insn_decode (next_pc, &di, arc_delayed_print_insn, &next_insn);
  359. next_pcs.push_back (arc_insn_get_linear_next_pc (next_insn));
  360. }
  361. else
  362. next_pcs.push_back (next_pc);
  363. ULONGEST status32;
  364. regcache_cooked_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
  365. &status32);
  366. if (curr_insn.is_control_flow)
  367. {
  368. CORE_ADDR branch_pc = arc_insn_get_branch_target (curr_insn);
  369. if (branch_pc != next_pc)
  370. next_pcs.push_back (branch_pc);
  371. }
  372. /* Is current instruction the last in a loop body? */
  373. else if (tdep->has_hw_loops)
  374. {
  375. /* If STATUS32.L is 1, then ZD-loops are disabled. */
  376. if ((status32 & ARC_STATUS32_L_MASK) == 0)
  377. {
  378. ULONGEST lp_end, lp_start, lp_count;
  379. regcache_cooked_read_unsigned (regcache, ARC_LP_START_REGNUM,
  380. &lp_start);
  381. regcache_cooked_read_unsigned (regcache, ARC_LP_END_REGNUM, &lp_end);
  382. regcache_cooked_read_unsigned (regcache, ARC_LP_COUNT_REGNUM,
  383. &lp_count);
  384. arc_linux_debug_printf ("lp_start = %s, lp_end = %s, "
  385. "lp_count = %s, next_pc = %s",
  386. paddress (gdbarch, lp_start),
  387. paddress (gdbarch, lp_end),
  388. pulongest (lp_count),
  389. paddress (gdbarch, next_pc));
  390. if (next_pc == lp_end && lp_count > 1)
  391. {
  392. /* The instruction is in effect a jump back to the start of
  393. the loop. */
  394. next_pcs.push_back (lp_start);
  395. }
  396. }
  397. }
  398. /* Is this a delay slot? Then next PC is in BTA register. */
  399. if ((status32 & ARC_STATUS32_DE_MASK) != 0)
  400. {
  401. ULONGEST bta;
  402. regcache_cooked_read_unsigned (regcache, ARC_BTA_REGNUM, &bta);
  403. next_pcs.push_back (bta);
  404. }
  405. return next_pcs;
  406. }
  407. /* Implement the "skip_solib_resolver" gdbarch method.
  408. See glibc_skip_solib_resolver for details. */
  409. static CORE_ADDR
  410. arc_linux_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
  411. {
  412. /* For uClibc 0.9.26+.
  413. An unresolved PLT entry points to "__dl_linux_resolve", which calls
  414. "_dl_linux_resolver" to do the resolving and then eventually jumps to
  415. the function.
  416. So we look for the symbol `_dl_linux_resolver', and if we are there,
  417. gdb sets a breakpoint at the return address, and continues. */
  418. struct bound_minimal_symbol resolver
  419. = lookup_minimal_symbol ("_dl_linux_resolver", NULL, NULL);
  420. if (arc_debug)
  421. {
  422. if (resolver.minsym != nullptr)
  423. {
  424. CORE_ADDR res_addr = BMSYMBOL_VALUE_ADDRESS (resolver);
  425. arc_linux_debug_printf ("pc = %s, resolver at %s",
  426. print_core_address (gdbarch, pc),
  427. print_core_address (gdbarch, res_addr));
  428. }
  429. else
  430. arc_linux_debug_printf ("pc = %s, no resolver found",
  431. print_core_address (gdbarch, pc));
  432. }
  433. if (resolver.minsym != nullptr && BMSYMBOL_VALUE_ADDRESS (resolver) == pc)
  434. {
  435. /* Find the return address. */
  436. return frame_unwind_caller_pc (get_current_frame ());
  437. }
  438. else
  439. {
  440. /* No breakpoint required. */
  441. return 0;
  442. }
  443. }
  444. /* Populate REGCACHE with register REGNUM from BUF. */
  445. static void
  446. supply_register (struct regcache *regcache, int regnum, const gdb_byte *buf)
  447. {
  448. /* Skip non-existing registers. */
  449. if ((arc_linux_core_reg_offsets[regnum] == ARC_OFFSET_NO_REGISTER))
  450. return;
  451. regcache->raw_supply (regnum, buf + arc_linux_core_reg_offsets[regnum]);
  452. }
  453. void
  454. arc_linux_supply_gregset (const struct regset *regset,
  455. struct regcache *regcache,
  456. int regnum, const void *gregs, size_t size)
  457. {
  458. gdb_static_assert (ARC_LAST_REGNUM
  459. < ARRAY_SIZE (arc_linux_core_reg_offsets));
  460. const bfd_byte *buf = (const bfd_byte *) gregs;
  461. /* REGNUM == -1 means writing all the registers. */
  462. if (regnum == -1)
  463. for (int reg = 0; reg <= ARC_LAST_REGNUM; reg++)
  464. supply_register (regcache, reg, buf);
  465. else if (regnum <= ARC_LAST_REGNUM)
  466. supply_register (regcache, regnum, buf);
  467. else
  468. gdb_assert_not_reached ("Invalid regnum in arc_linux_supply_gregset.");
  469. }
  470. void
  471. arc_linux_supply_v2_regset (const struct regset *regset,
  472. struct regcache *regcache, int regnum,
  473. const void *v2_regs, size_t size)
  474. {
  475. const bfd_byte *buf = (const bfd_byte *) v2_regs;
  476. /* user_regs_arcv2 is defined in linux arch/arc/include/uapi/asm/ptrace.h. */
  477. if (regnum == -1 || regnum == ARC_R30_REGNUM)
  478. regcache->raw_supply (ARC_R30_REGNUM, buf);
  479. if (regnum == -1 || regnum == ARC_R58_REGNUM)
  480. regcache->raw_supply (ARC_R58_REGNUM, buf + REGOFF (1));
  481. if (regnum == -1 || regnum == ARC_R59_REGNUM)
  482. regcache->raw_supply (ARC_R59_REGNUM, buf + REGOFF (2));
  483. }
  484. /* Populate BUF with register REGNUM from the REGCACHE. */
  485. static void
  486. collect_register (const struct regcache *regcache, struct gdbarch *gdbarch,
  487. int regnum, gdb_byte *buf)
  488. {
  489. int offset;
  490. /* Skip non-existing registers. */
  491. if (arc_linux_core_reg_offsets[regnum] == ARC_OFFSET_NO_REGISTER)
  492. return;
  493. /* The address where the execution has stopped is in pseudo-register
  494. STOP_PC. However, when kernel code is returning from the exception,
  495. it uses the value from ERET register. Since, TRAP_S (the breakpoint
  496. instruction) commits, the ERET points to the next instruction. In
  497. other words: ERET != STOP_PC. To jump back from the kernel code to
  498. the correct address, ERET must be overwritten by GDB's STOP_PC. Else,
  499. the program will continue at the address after the current instruction.
  500. */
  501. if (regnum == gdbarch_pc_regnum (gdbarch))
  502. offset = arc_linux_core_reg_offsets[ARC_ERET_REGNUM];
  503. else
  504. offset = arc_linux_core_reg_offsets[regnum];
  505. regcache->raw_collect (regnum, buf + offset);
  506. }
  507. void
  508. arc_linux_collect_gregset (const struct regset *regset,
  509. const struct regcache *regcache,
  510. int regnum, void *gregs, size_t size)
  511. {
  512. gdb_static_assert (ARC_LAST_REGNUM
  513. < ARRAY_SIZE (arc_linux_core_reg_offsets));
  514. gdb_byte *buf = (gdb_byte *) gregs;
  515. struct gdbarch *gdbarch = regcache->arch ();
  516. /* REGNUM == -1 means writing all the registers. */
  517. if (regnum == -1)
  518. for (int reg = 0; reg <= ARC_LAST_REGNUM; reg++)
  519. collect_register (regcache, gdbarch, reg, buf);
  520. else if (regnum <= ARC_LAST_REGNUM)
  521. collect_register (regcache, gdbarch, regnum, buf);
  522. else
  523. gdb_assert_not_reached ("Invalid regnum in arc_linux_collect_gregset.");
  524. }
  525. void
  526. arc_linux_collect_v2_regset (const struct regset *regset,
  527. const struct regcache *regcache, int regnum,
  528. void *v2_regs, size_t size)
  529. {
  530. bfd_byte *buf = (bfd_byte *) v2_regs;
  531. if (regnum == -1 || regnum == ARC_R30_REGNUM)
  532. regcache->raw_collect (ARC_R30_REGNUM, buf);
  533. if (regnum == -1 || regnum == ARC_R58_REGNUM)
  534. regcache->raw_collect (ARC_R58_REGNUM, buf + REGOFF (1));
  535. if (regnum == -1 || regnum == ARC_R59_REGNUM)
  536. regcache->raw_collect (ARC_R59_REGNUM, buf + REGOFF (2));
  537. }
  538. /* Linux regset definitions. */
  539. static const struct regset arc_linux_gregset = {
  540. arc_linux_core_reg_offsets,
  541. arc_linux_supply_gregset,
  542. arc_linux_collect_gregset,
  543. };
  544. static const struct regset arc_linux_v2_regset = {
  545. NULL,
  546. arc_linux_supply_v2_regset,
  547. arc_linux_collect_v2_regset,
  548. };
  549. /* Implement the `iterate_over_regset_sections` gdbarch method. */
  550. static void
  551. arc_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  552. iterate_over_regset_sections_cb *cb,
  553. void *cb_data,
  554. const struct regcache *regcache)
  555. {
  556. /* There are 40 registers in Linux user_regs_struct, although some of
  557. them are now just a mere paddings, kept to maintain binary
  558. compatibility with older tools. */
  559. const int sizeof_gregset = 40 * ARC_REGISTER_SIZE;
  560. cb (".reg", sizeof_gregset, sizeof_gregset, &arc_linux_gregset, NULL,
  561. cb_data);
  562. cb (".reg-arc-v2", ARC_LINUX_SIZEOF_V2_REGSET, ARC_LINUX_SIZEOF_V2_REGSET,
  563. &arc_linux_v2_regset, NULL, cb_data);
  564. }
  565. /* Implement the `core_read_description` gdbarch method. */
  566. static const struct target_desc *
  567. arc_linux_core_read_description (struct gdbarch *gdbarch,
  568. struct target_ops *target,
  569. bfd *abfd)
  570. {
  571. arc_arch_features features
  572. = arc_arch_features_create (abfd,
  573. gdbarch_bfd_arch_info (gdbarch)->mach);
  574. return arc_lookup_target_description (features);
  575. }
  576. /* Initialization specific to Linux environment. */
  577. static void
  578. arc_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
  579. {
  580. arc_gdbarch_tdep *tdep = (arc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  581. arc_linux_debug_printf ("GNU/Linux OS/ABI initialization.");
  582. /* Fill in target-dependent info in ARC-private structure. */
  583. tdep->is_sigtramp = arc_linux_is_sigtramp;
  584. tdep->sigcontext_addr = arc_linux_sigcontext_addr;
  585. tdep->sc_reg_offset = arc_linux_sc_reg_offsets;
  586. tdep->sc_num_regs = ARRAY_SIZE (arc_linux_sc_reg_offsets);
  587. /* If we are using Linux, we have in uClibc
  588. (libc/sysdeps/linux/arc/bits/setjmp.h):
  589. typedef int __jmp_buf[13+1+1+1]; //r13-r25, fp, sp, blink
  590. Where "blink" is a stored PC of a caller function.
  591. */
  592. tdep->jb_pc = 15;
  593. linux_init_abi (info, gdbarch, 0);
  594. /* Set up target dependent GDB architecture entries. */
  595. set_gdbarch_cannot_fetch_register (gdbarch, arc_linux_cannot_fetch_register);
  596. set_gdbarch_cannot_store_register (gdbarch, arc_linux_cannot_store_register);
  597. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  598. arc_linux_breakpoint_kind_from_pc);
  599. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  600. arc_linux_sw_breakpoint_from_kind);
  601. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  602. svr4_fetch_objfile_link_map);
  603. set_gdbarch_software_single_step (gdbarch, arc_linux_software_single_step);
  604. set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  605. set_gdbarch_skip_solib_resolver (gdbarch, arc_linux_skip_solib_resolver);
  606. set_gdbarch_iterate_over_regset_sections
  607. (gdbarch, arc_linux_iterate_over_regset_sections);
  608. set_gdbarch_core_read_description (gdbarch, arc_linux_core_read_description);
  609. /* GNU/Linux uses SVR4-style shared libraries, with 32-bit ints, longs
  610. and pointers (ILP32). */
  611. set_solib_svr4_fetch_link_map_offsets (gdbarch,
  612. linux_ilp32_fetch_link_map_offsets);
  613. }
  614. /* Suppress warning from -Wmissing-prototypes. */
  615. extern initialize_file_ftype _initialize_arc_linux_tdep;
  616. void
  617. _initialize_arc_linux_tdep ()
  618. {
  619. gdbarch_register_osabi (bfd_arch_arc, 0, GDB_OSABI_LINUX,
  620. arc_linux_init_osabi);
  621. }