Commit | Line | Data |
---|---|---|
8d7f0635 AK |
1 | /* Target dependent code for GNU/Linux ARC. |
2 | ||
3 | Copyright 2020 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | /* GDB header files. */ | |
21 | #include "defs.h" | |
22 | #include "linux-tdep.h" | |
23 | #include "objfiles.h" | |
24 | #include "opcode/arc.h" | |
25 | #include "osabi.h" | |
26 | #include "solib-svr4.h" | |
27 | ||
28 | /* ARC header files. */ | |
29 | #include "opcodes/arc-dis.h" | |
cc463201 | 30 | #include "arc-linux-tdep.h" |
8d7f0635 | 31 | #include "arc-tdep.h" |
cc463201 AK |
32 | #include "arch/arc.h" |
33 | ||
34 | #define REGOFF(offset) (offset * ARC_REGISTER_SIZE) | |
35 | ||
36 | /* arc_linux_core_reg_offsets[i] is the offset in the .reg section of GDB | |
37 | regnum i. Array index is an internal GDB register number, as defined in | |
38 | arc-tdep.h:arc_regnum. | |
39 | ||
40 | From include/uapi/asm/ptrace.h in the ARC Linux sources. */ | |
41 | ||
42 | /* The layout of this struct is tightly bound to "arc_regnum" enum | |
43 | in arc-tdep.h. Any change of order in there, must be reflected | |
44 | here as well. */ | |
45 | static const int arc_linux_core_reg_offsets[] = { | |
46 | /* R0 - R12. */ | |
47 | REGOFF (22), REGOFF (21), REGOFF (20), REGOFF (19), | |
48 | REGOFF (18), REGOFF (17), REGOFF (16), REGOFF (15), | |
49 | REGOFF (14), REGOFF (13), REGOFF (12), REGOFF (11), | |
50 | REGOFF (10), | |
51 | ||
52 | /* R13 - R25. */ | |
53 | REGOFF (37), REGOFF (36), REGOFF (35), REGOFF (34), | |
54 | REGOFF (33), REGOFF (32), REGOFF (31), REGOFF (30), | |
55 | REGOFF (29), REGOFF (28), REGOFF (27), REGOFF (26), | |
56 | REGOFF (25), | |
57 | ||
58 | REGOFF (9), /* R26 (GP) */ | |
59 | REGOFF (8), /* FP */ | |
60 | REGOFF (23), /* SP */ | |
61 | ARC_OFFSET_NO_REGISTER, /* ILINK */ | |
62 | ARC_OFFSET_NO_REGISTER, /* R30 */ | |
63 | REGOFF (7), /* BLINK */ | |
64 | ||
65 | /* R32 - R59. */ | |
66 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
67 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
68 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
69 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
70 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
71 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
72 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
73 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
74 | ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, ARC_OFFSET_NO_REGISTER, | |
75 | ARC_OFFSET_NO_REGISTER, | |
76 | ||
77 | REGOFF (4), /* LP_COUNT */ | |
78 | ARC_OFFSET_NO_REGISTER, /* RESERVED */ | |
79 | ARC_OFFSET_NO_REGISTER, /* LIMM */ | |
80 | ARC_OFFSET_NO_REGISTER, /* PCL */ | |
81 | ||
82 | REGOFF (39), /* PC */ | |
83 | REGOFF (5), /* STATUS32 */ | |
84 | REGOFF (2), /* LP_START */ | |
85 | REGOFF (3), /* LP_END */ | |
86 | REGOFF (1), /* BTA */ | |
87 | REGOFF (6) /* ERET */ | |
88 | }; | |
8d7f0635 AK |
89 | |
90 | /* Implement the "cannot_fetch_register" gdbarch method. */ | |
91 | ||
92 | static int | |
93 | arc_linux_cannot_fetch_register (struct gdbarch *gdbarch, int regnum) | |
94 | { | |
95 | /* Assume that register is readable if it is unknown. */ | |
96 | switch (regnum) | |
97 | { | |
98 | case ARC_ILINK_REGNUM: | |
99 | case ARC_RESERVED_REGNUM: | |
100 | case ARC_LIMM_REGNUM: | |
101 | return true; | |
102 | case ARC_R30_REGNUM: | |
103 | case ARC_R58_REGNUM: | |
104 | case ARC_R59_REGNUM: | |
105 | return !arc_mach_is_arcv2 (gdbarch); | |
106 | } | |
107 | return (regnum > ARC_BLINK_REGNUM) && (regnum < ARC_LP_COUNT_REGNUM); | |
108 | } | |
109 | ||
110 | /* Implement the "cannot_store_register" gdbarch method. */ | |
111 | ||
112 | static int | |
113 | arc_linux_cannot_store_register (struct gdbarch *gdbarch, int regnum) | |
114 | { | |
115 | /* Assume that register is writable if it is unknown. */ | |
116 | switch (regnum) | |
117 | { | |
118 | case ARC_ILINK_REGNUM: | |
119 | case ARC_RESERVED_REGNUM: | |
120 | case ARC_LIMM_REGNUM: | |
121 | case ARC_PCL_REGNUM: | |
122 | return true; | |
123 | case ARC_R30_REGNUM: | |
124 | case ARC_R58_REGNUM: | |
125 | case ARC_R59_REGNUM: | |
126 | return !arc_mach_is_arcv2 (gdbarch); | |
127 | } | |
128 | return (regnum > ARC_BLINK_REGNUM) && (regnum < ARC_LP_COUNT_REGNUM); | |
129 | } | |
130 | ||
131 | /* For ARC Linux, breakpoints use the 16-bit TRAP_S 1 instruction, which | |
132 | is 0x3e78 (little endian) or 0x783e (big endian). */ | |
133 | ||
134 | static const gdb_byte arc_linux_trap_s_be[] = { 0x78, 0x3e }; | |
135 | static const gdb_byte arc_linux_trap_s_le[] = { 0x3e, 0x78 }; | |
136 | static const int trap_size = 2; /* Number of bytes to insert "trap". */ | |
137 | ||
138 | /* Implement the "breakpoint_kind_from_pc" gdbarch method. */ | |
139 | ||
140 | static int | |
141 | arc_linux_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr) | |
142 | { | |
143 | return trap_size; | |
144 | } | |
145 | ||
146 | /* Implement the "sw_breakpoint_from_kind" gdbarch method. */ | |
147 | ||
148 | static const gdb_byte * | |
149 | arc_linux_sw_breakpoint_from_kind (struct gdbarch *gdbarch, | |
150 | int kind, int *size) | |
151 | { | |
6f2643db | 152 | gdb_assert (kind == trap_size); |
8d7f0635 AK |
153 | *size = kind; |
154 | return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) | |
155 | ? arc_linux_trap_s_be | |
156 | : arc_linux_trap_s_le); | |
157 | } | |
158 | ||
159 | /* Implement the "software_single_step" gdbarch method. */ | |
160 | ||
161 | static std::vector<CORE_ADDR> | |
162 | arc_linux_software_single_step (struct regcache *regcache) | |
163 | { | |
164 | struct gdbarch *gdbarch = regcache->arch (); | |
165 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
166 | struct disassemble_info di = arc_disassemble_info (gdbarch); | |
167 | ||
168 | /* Read current instruction. */ | |
169 | struct arc_instruction curr_insn; | |
170 | arc_insn_decode (regcache_read_pc (regcache), &di, arc_delayed_print_insn, | |
171 | &curr_insn); | |
172 | CORE_ADDR next_pc = arc_insn_get_linear_next_pc (curr_insn); | |
173 | ||
174 | std::vector<CORE_ADDR> next_pcs; | |
175 | ||
176 | /* For instructions with delay slots, the fall thru is not the | |
177 | instruction immediately after the current instruction, but the one | |
178 | after that. */ | |
179 | if (curr_insn.has_delay_slot) | |
180 | { | |
181 | struct arc_instruction next_insn; | |
182 | arc_insn_decode (next_pc, &di, arc_delayed_print_insn, &next_insn); | |
183 | next_pcs.push_back (arc_insn_get_linear_next_pc (next_insn)); | |
184 | } | |
185 | else | |
186 | next_pcs.push_back (next_pc); | |
187 | ||
188 | ULONGEST status32; | |
189 | regcache_cooked_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch), | |
190 | &status32); | |
191 | ||
192 | if (curr_insn.is_control_flow) | |
193 | { | |
194 | CORE_ADDR branch_pc = arc_insn_get_branch_target (curr_insn); | |
195 | if (branch_pc != next_pc) | |
196 | next_pcs.push_back (branch_pc); | |
197 | } | |
198 | /* Is current instruction the last in a loop body? */ | |
199 | else if (tdep->has_hw_loops) | |
200 | { | |
201 | /* If STATUS32.L is 1, then ZD-loops are disabled. */ | |
202 | if ((status32 & ARC_STATUS32_L_MASK) == 0) | |
203 | { | |
204 | ULONGEST lp_end, lp_start, lp_count; | |
205 | regcache_cooked_read_unsigned (regcache, ARC_LP_START_REGNUM, | |
206 | &lp_start); | |
207 | regcache_cooked_read_unsigned (regcache, ARC_LP_END_REGNUM, &lp_end); | |
208 | regcache_cooked_read_unsigned (regcache, ARC_LP_COUNT_REGNUM, | |
209 | &lp_count); | |
210 | ||
211 | if (arc_debug) | |
212 | { | |
213 | debug_printf ("arc-linux: lp_start = %s, lp_end = %s, " | |
214 | "lp_count = %s, next_pc = %s\n", | |
215 | paddress (gdbarch, lp_start), | |
216 | paddress (gdbarch, lp_end), | |
217 | pulongest (lp_count), | |
218 | paddress (gdbarch, next_pc)); | |
219 | } | |
220 | ||
221 | if (next_pc == lp_end && lp_count > 1) | |
222 | { | |
223 | /* The instruction is in effect a jump back to the start of | |
224 | the loop. */ | |
225 | next_pcs.push_back (lp_start); | |
226 | } | |
227 | } | |
228 | } | |
229 | ||
230 | /* Is this a delay slot? Then next PC is in BTA register. */ | |
231 | if ((status32 & ARC_STATUS32_DE_MASK) != 0) | |
232 | { | |
233 | ULONGEST bta; | |
234 | regcache_cooked_read_unsigned (regcache, ARC_BTA_REGNUM, &bta); | |
235 | next_pcs.push_back (bta); | |
236 | } | |
237 | ||
238 | return next_pcs; | |
239 | } | |
240 | ||
241 | /* Implement the "skip_solib_resolver" gdbarch method. | |
242 | ||
243 | See glibc_skip_solib_resolver for details. */ | |
244 | ||
245 | static CORE_ADDR | |
246 | arc_linux_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc) | |
247 | { | |
248 | /* For uClibc 0.9.26+. | |
249 | ||
250 | An unresolved PLT entry points to "__dl_linux_resolve", which calls | |
251 | "_dl_linux_resolver" to do the resolving and then eventually jumps to | |
252 | the function. | |
253 | ||
254 | So we look for the symbol `_dl_linux_resolver', and if we are there, | |
255 | gdb sets a breakpoint at the return address, and continues. */ | |
256 | struct bound_minimal_symbol resolver | |
257 | = lookup_minimal_symbol ("_dl_linux_resolver", NULL, NULL); | |
258 | ||
259 | if (arc_debug) | |
260 | { | |
261 | if (resolver.minsym != nullptr) | |
262 | { | |
263 | CORE_ADDR res_addr = BMSYMBOL_VALUE_ADDRESS (resolver); | |
264 | debug_printf ("arc-linux: skip_solib_resolver (): " | |
265 | "pc = %s, resolver at %s\n", | |
266 | print_core_address (gdbarch, pc), | |
267 | print_core_address (gdbarch, res_addr)); | |
268 | } | |
269 | else | |
270 | { | |
271 | debug_printf ("arc-linux: skip_solib_resolver (): " | |
272 | "pc = %s, no resolver found\n", | |
273 | print_core_address (gdbarch, pc)); | |
274 | } | |
275 | } | |
276 | ||
277 | if (resolver.minsym != nullptr && BMSYMBOL_VALUE_ADDRESS (resolver) == pc) | |
278 | { | |
279 | /* Find the return address. */ | |
280 | return frame_unwind_caller_pc (get_current_frame ()); | |
281 | } | |
282 | else | |
283 | { | |
284 | /* No breakpoint required. */ | |
285 | return 0; | |
286 | } | |
287 | } | |
288 | ||
cc463201 AK |
289 | void |
290 | arc_linux_supply_gregset (const struct regset *regset, | |
291 | struct regcache *regcache, | |
292 | int regnum, const void *gregs, size_t size) | |
293 | { | |
294 | gdb_static_assert (ARC_LAST_REGNUM | |
295 | < ARRAY_SIZE (arc_linux_core_reg_offsets)); | |
296 | ||
297 | const bfd_byte *buf = (const bfd_byte *) gregs; | |
298 | ||
299 | for (int reg = 0; reg <= ARC_LAST_REGNUM; reg++) | |
300 | if (arc_linux_core_reg_offsets[reg] != ARC_OFFSET_NO_REGISTER) | |
301 | regcache->raw_supply (reg, buf + arc_linux_core_reg_offsets[reg]); | |
302 | } | |
303 | ||
304 | void | |
305 | arc_linux_supply_v2_regset (const struct regset *regset, | |
306 | struct regcache *regcache, int regnum, | |
307 | const void *v2_regs, size_t size) | |
308 | { | |
309 | const bfd_byte *buf = (const bfd_byte *) v2_regs; | |
310 | ||
311 | /* user_regs_arcv2 is defined in linux arch/arc/include/uapi/asm/ptrace.h. */ | |
312 | regcache->raw_supply (ARC_R30_REGNUM, buf); | |
313 | regcache->raw_supply (ARC_R58_REGNUM, buf + REGOFF (1)); | |
314 | regcache->raw_supply (ARC_R59_REGNUM, buf + REGOFF (2)); | |
315 | } | |
316 | ||
317 | /* Populate BUF with register REGNUM from the REGCACHE. */ | |
318 | ||
319 | static void | |
320 | collect_register (const struct regcache *regcache, struct gdbarch *gdbarch, | |
321 | int regnum, gdb_byte *buf) | |
322 | { | |
10c19fad SV |
323 | int offset; |
324 | ||
cc463201 | 325 | /* Skip non-existing registers. */ |
10c19fad | 326 | if (arc_linux_core_reg_offsets[regnum] == ARC_OFFSET_NO_REGISTER) |
cc463201 AK |
327 | return; |
328 | ||
329 | /* The address where the execution has stopped is in pseudo-register | |
330 | STOP_PC. However, when kernel code is returning from the exception, | |
331 | it uses the value from ERET register. Since, TRAP_S (the breakpoint | |
332 | instruction) commits, the ERET points to the next instruction. In | |
333 | other words: ERET != STOP_PC. To jump back from the kernel code to | |
334 | the correct address, ERET must be overwritten by GDB's STOP_PC. Else, | |
335 | the program will continue at the address after the current instruction. | |
336 | */ | |
337 | if (regnum == gdbarch_pc_regnum (gdbarch)) | |
10c19fad SV |
338 | offset = arc_linux_core_reg_offsets[ARC_ERET_REGNUM]; |
339 | else | |
340 | offset = arc_linux_core_reg_offsets[regnum]; | |
341 | regcache->raw_collect (regnum, buf + offset); | |
cc463201 AK |
342 | } |
343 | ||
344 | void | |
345 | arc_linux_collect_gregset (const struct regset *regset, | |
346 | const struct regcache *regcache, | |
347 | int regnum, void *gregs, size_t size) | |
348 | { | |
349 | gdb_static_assert (ARC_LAST_REGNUM | |
350 | < ARRAY_SIZE (arc_linux_core_reg_offsets)); | |
351 | ||
352 | gdb_byte *buf = (gdb_byte *) gregs; | |
353 | struct gdbarch *gdbarch = regcache->arch (); | |
354 | ||
355 | /* regnum == -1 means writing all the registers. */ | |
356 | if (regnum == -1) | |
357 | for (int reg = 0; reg <= ARC_LAST_REGNUM; reg++) | |
358 | collect_register (regcache, gdbarch, reg, buf); | |
359 | else if (regnum <= ARC_LAST_REGNUM) | |
360 | collect_register (regcache, gdbarch, regnum, buf); | |
361 | else | |
362 | gdb_assert_not_reached ("Invalid regnum in arc_linux_collect_gregset."); | |
363 | } | |
364 | ||
365 | void | |
366 | arc_linux_collect_v2_regset (const struct regset *regset, | |
367 | const struct regcache *regcache, int regnum, | |
368 | void *v2_regs, size_t size) | |
369 | { | |
370 | bfd_byte *buf = (bfd_byte *) v2_regs; | |
371 | ||
372 | regcache->raw_collect (ARC_R30_REGNUM, buf); | |
373 | regcache->raw_collect (ARC_R58_REGNUM, buf + REGOFF (1)); | |
374 | regcache->raw_collect (ARC_R59_REGNUM, buf + REGOFF (2)); | |
375 | } | |
376 | ||
377 | /* Linux regset definitions. */ | |
378 | ||
379 | static const struct regset arc_linux_gregset = { | |
380 | arc_linux_core_reg_offsets, | |
381 | arc_linux_supply_gregset, | |
382 | arc_linux_collect_gregset, | |
383 | }; | |
384 | ||
385 | static const struct regset arc_linux_v2_regset = { | |
386 | NULL, | |
387 | arc_linux_supply_v2_regset, | |
388 | arc_linux_collect_v2_regset, | |
389 | }; | |
390 | ||
391 | /* Implement the `iterate_over_regset_sections` gdbarch method. */ | |
392 | ||
393 | static void | |
394 | arc_linux_iterate_over_regset_sections (struct gdbarch *gdbarch, | |
395 | iterate_over_regset_sections_cb *cb, | |
396 | void *cb_data, | |
397 | const struct regcache *regcache) | |
398 | { | |
399 | /* There are 40 registers in Linux user_regs_struct, although some of | |
400 | them are now just a mere paddings, kept to maintain binary | |
401 | compatibility with older tools. */ | |
402 | const int sizeof_gregset = 40 * ARC_REGISTER_SIZE; | |
403 | ||
404 | cb (".reg", sizeof_gregset, sizeof_gregset, &arc_linux_gregset, NULL, | |
405 | cb_data); | |
406 | cb (".reg-arc-v2", ARC_LINUX_SIZEOF_V2_REGSET, ARC_LINUX_SIZEOF_V2_REGSET, | |
407 | &arc_linux_v2_regset, NULL, cb_data); | |
408 | } | |
409 | ||
410 | /* Implement the `core_read_description` gdbarch method. */ | |
411 | ||
412 | static const struct target_desc * | |
413 | arc_linux_core_read_description (struct gdbarch *gdbarch, | |
414 | struct target_ops *target, | |
415 | bfd *abfd) | |
416 | { | |
417 | arc_arch_features features | |
418 | = arc_arch_features_create (abfd, | |
419 | gdbarch_bfd_arch_info (gdbarch)->mach); | |
420 | return arc_lookup_target_description (features); | |
421 | } | |
422 | ||
8d7f0635 AK |
423 | /* Initialization specific to Linux environment. */ |
424 | ||
425 | static void | |
426 | arc_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch) | |
427 | { | |
428 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
429 | ||
430 | if (arc_debug) | |
431 | debug_printf ("arc-linux: GNU/Linux OS/ABI initialization.\n"); | |
432 | ||
433 | /* If we are using Linux, we have in uClibc | |
434 | (libc/sysdeps/linux/arc/bits/setjmp.h): | |
435 | ||
436 | typedef int __jmp_buf[13+1+1+1]; //r13-r25, fp, sp, blink | |
437 | ||
438 | Where "blink" is a stored PC of a caller function. | |
439 | */ | |
440 | tdep->jb_pc = 15; | |
441 | ||
442 | linux_init_abi (info, gdbarch); | |
443 | ||
444 | /* Set up target dependent GDB architecture entries. */ | |
445 | set_gdbarch_cannot_fetch_register (gdbarch, arc_linux_cannot_fetch_register); | |
446 | set_gdbarch_cannot_store_register (gdbarch, arc_linux_cannot_store_register); | |
447 | set_gdbarch_breakpoint_kind_from_pc (gdbarch, | |
448 | arc_linux_breakpoint_kind_from_pc); | |
449 | set_gdbarch_sw_breakpoint_from_kind (gdbarch, | |
450 | arc_linux_sw_breakpoint_from_kind); | |
451 | set_gdbarch_fetch_tls_load_module_address (gdbarch, | |
452 | svr4_fetch_objfile_link_map); | |
453 | set_gdbarch_software_single_step (gdbarch, arc_linux_software_single_step); | |
454 | set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target); | |
455 | set_gdbarch_skip_solib_resolver (gdbarch, arc_linux_skip_solib_resolver); | |
cc463201 AK |
456 | set_gdbarch_iterate_over_regset_sections |
457 | (gdbarch, arc_linux_iterate_over_regset_sections); | |
458 | set_gdbarch_core_read_description (gdbarch, arc_linux_core_read_description); | |
8d7f0635 AK |
459 | |
460 | /* GNU/Linux uses SVR4-style shared libraries, with 32-bit ints, longs | |
461 | and pointers (ILP32). */ | |
462 | set_solib_svr4_fetch_link_map_offsets (gdbarch, | |
463 | svr4_ilp32_fetch_link_map_offsets); | |
464 | } | |
465 | ||
466 | /* Suppress warning from -Wmissing-prototypes. */ | |
467 | extern initialize_file_ftype _initialize_arc_linux_tdep; | |
468 | ||
469 | void | |
470 | _initialize_arc_linux_tdep () | |
471 | { | |
472 | gdbarch_register_osabi (bfd_arch_arc, 0, GDB_OSABI_LINUX, | |
473 | arc_linux_init_osabi); | |
474 | } |