1 /* Target dependent code for GNU/Linux ARC.
3 Copyright 2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* GDB header files. */
22 #include "linux-tdep.h"
24 #include "opcode/arc.h"
26 #include "solib-svr4.h"
28 /* ARC header files. */
29 #include "opcodes/arc-dis.h"
30 #include "arc-linux-tdep.h"
34 #define REGOFF(offset) (offset * ARC_REGISTER_SIZE)
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.
40 From include/uapi/asm/ptrace.h in the ARC Linux sources. */
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
45 static const int arc_linux_core_reg_offsets
[] = {
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),
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),
58 REGOFF (9), /* R26 (GP) */
61 ARC_OFFSET_NO_REGISTER
, /* ILINK */
62 ARC_OFFSET_NO_REGISTER
, /* R30 */
63 REGOFF (7), /* BLINK */
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
,
77 REGOFF (4), /* LP_COUNT */
78 ARC_OFFSET_NO_REGISTER
, /* RESERVED */
79 ARC_OFFSET_NO_REGISTER
, /* LIMM */
80 ARC_OFFSET_NO_REGISTER
, /* PCL */
83 REGOFF (5), /* STATUS32 */
84 REGOFF (2), /* LP_START */
85 REGOFF (3), /* LP_END */
90 /* Implement the "cannot_fetch_register" gdbarch method. */
93 arc_linux_cannot_fetch_register (struct gdbarch
*gdbarch
, int regnum
)
95 /* Assume that register is readable if it is unknown. */
98 case ARC_ILINK_REGNUM
:
99 case ARC_RESERVED_REGNUM
:
100 case ARC_LIMM_REGNUM
:
105 return !arc_mach_is_arcv2 (gdbarch
);
107 return (regnum
> ARC_BLINK_REGNUM
) && (regnum
< ARC_LP_COUNT_REGNUM
);
110 /* Implement the "cannot_store_register" gdbarch method. */
113 arc_linux_cannot_store_register (struct gdbarch
*gdbarch
, int regnum
)
115 /* Assume that register is writable if it is unknown. */
118 case ARC_ILINK_REGNUM
:
119 case ARC_RESERVED_REGNUM
:
120 case ARC_LIMM_REGNUM
:
126 return !arc_mach_is_arcv2 (gdbarch
);
128 return (regnum
> ARC_BLINK_REGNUM
) && (regnum
< ARC_LP_COUNT_REGNUM
);
131 /* For ARC Linux, breakpoints use the 16-bit TRAP_S 1 instruction, which
132 is 0x3e78 (little endian) or 0x783e (big endian). */
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". */
138 /* Implement the "breakpoint_kind_from_pc" gdbarch method. */
141 arc_linux_breakpoint_kind_from_pc (struct gdbarch
*gdbarch
, CORE_ADDR
*pcptr
)
146 /* Implement the "sw_breakpoint_from_kind" gdbarch method. */
148 static const gdb_byte
*
149 arc_linux_sw_breakpoint_from_kind (struct gdbarch
*gdbarch
,
152 gdb_assert (kind
== trap_size
);
154 return ((gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
155 ? arc_linux_trap_s_be
156 : arc_linux_trap_s_le
);
159 /* Implement the "software_single_step" gdbarch method. */
161 static std::vector
<CORE_ADDR
>
162 arc_linux_software_single_step (struct regcache
*regcache
)
164 struct gdbarch
*gdbarch
= regcache
->arch ();
165 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
166 struct disassemble_info di
= arc_disassemble_info (gdbarch
);
168 /* Read current instruction. */
169 struct arc_instruction curr_insn
;
170 arc_insn_decode (regcache_read_pc (regcache
), &di
, arc_delayed_print_insn
,
172 CORE_ADDR next_pc
= arc_insn_get_linear_next_pc (curr_insn
);
174 std::vector
<CORE_ADDR
> next_pcs
;
176 /* For instructions with delay slots, the fall thru is not the
177 instruction immediately after the current instruction, but the one
179 if (curr_insn
.has_delay_slot
)
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
));
186 next_pcs
.push_back (next_pc
);
189 regcache_cooked_read_unsigned (regcache
, gdbarch_ps_regnum (gdbarch
),
192 if (curr_insn
.is_control_flow
)
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
);
198 /* Is current instruction the last in a loop body? */
199 else if (tdep
->has_hw_loops
)
201 /* If STATUS32.L is 1, then ZD-loops are disabled. */
202 if ((status32
& ARC_STATUS32_L_MASK
) == 0)
204 ULONGEST lp_end
, lp_start
, lp_count
;
205 regcache_cooked_read_unsigned (regcache
, ARC_LP_START_REGNUM
,
207 regcache_cooked_read_unsigned (regcache
, ARC_LP_END_REGNUM
, &lp_end
);
208 regcache_cooked_read_unsigned (regcache
, ARC_LP_COUNT_REGNUM
,
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
));
221 if (next_pc
== lp_end
&& lp_count
> 1)
223 /* The instruction is in effect a jump back to the start of
225 next_pcs
.push_back (lp_start
);
230 /* Is this a delay slot? Then next PC is in BTA register. */
231 if ((status32
& ARC_STATUS32_DE_MASK
) != 0)
234 regcache_cooked_read_unsigned (regcache
, ARC_BTA_REGNUM
, &bta
);
235 next_pcs
.push_back (bta
);
241 /* Implement the "skip_solib_resolver" gdbarch method.
243 See glibc_skip_solib_resolver for details. */
246 arc_linux_skip_solib_resolver (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
248 /* For uClibc 0.9.26+.
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
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
);
261 if (resolver
.minsym
!= nullptr)
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
));
271 debug_printf ("arc-linux: skip_solib_resolver (): "
272 "pc = %s, no resolver found\n",
273 print_core_address (gdbarch
, pc
));
277 if (resolver
.minsym
!= nullptr && BMSYMBOL_VALUE_ADDRESS (resolver
) == pc
)
279 /* Find the return address. */
280 return frame_unwind_caller_pc (get_current_frame ());
284 /* No breakpoint required. */
290 arc_linux_supply_gregset (const struct regset
*regset
,
291 struct regcache
*regcache
,
292 int regnum
, const void *gregs
, size_t size
)
294 gdb_static_assert (ARC_LAST_REGNUM
295 < ARRAY_SIZE (arc_linux_core_reg_offsets
));
297 const bfd_byte
*buf
= (const bfd_byte
*) gregs
;
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
]);
305 arc_linux_supply_v2_regset (const struct regset
*regset
,
306 struct regcache
*regcache
, int regnum
,
307 const void *v2_regs
, size_t size
)
309 const bfd_byte
*buf
= (const bfd_byte
*) v2_regs
;
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));
317 /* Populate BUF with register REGNUM from the REGCACHE. */
320 collect_register (const struct regcache
*regcache
, struct gdbarch
*gdbarch
,
321 int regnum
, gdb_byte
*buf
)
325 /* Skip non-existing registers. */
326 if (arc_linux_core_reg_offsets
[regnum
] == ARC_OFFSET_NO_REGISTER
)
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.
337 if (regnum
== gdbarch_pc_regnum (gdbarch
))
338 offset
= arc_linux_core_reg_offsets
[ARC_ERET_REGNUM
];
340 offset
= arc_linux_core_reg_offsets
[regnum
];
341 regcache
->raw_collect (regnum
, buf
+ offset
);
345 arc_linux_collect_gregset (const struct regset
*regset
,
346 const struct regcache
*regcache
,
347 int regnum
, void *gregs
, size_t size
)
349 gdb_static_assert (ARC_LAST_REGNUM
350 < ARRAY_SIZE (arc_linux_core_reg_offsets
));
352 gdb_byte
*buf
= (gdb_byte
*) gregs
;
353 struct gdbarch
*gdbarch
= regcache
->arch ();
355 /* regnum == -1 means writing all the registers. */
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
);
362 gdb_assert_not_reached ("Invalid regnum in arc_linux_collect_gregset.");
366 arc_linux_collect_v2_regset (const struct regset
*regset
,
367 const struct regcache
*regcache
, int regnum
,
368 void *v2_regs
, size_t size
)
370 bfd_byte
*buf
= (bfd_byte
*) v2_regs
;
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));
377 /* Linux regset definitions. */
379 static const struct regset arc_linux_gregset
= {
380 arc_linux_core_reg_offsets
,
381 arc_linux_supply_gregset
,
382 arc_linux_collect_gregset
,
385 static const struct regset arc_linux_v2_regset
= {
387 arc_linux_supply_v2_regset
,
388 arc_linux_collect_v2_regset
,
391 /* Implement the `iterate_over_regset_sections` gdbarch method. */
394 arc_linux_iterate_over_regset_sections (struct gdbarch
*gdbarch
,
395 iterate_over_regset_sections_cb
*cb
,
397 const struct regcache
*regcache
)
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
;
404 cb (".reg", sizeof_gregset
, sizeof_gregset
, &arc_linux_gregset
, NULL
,
406 cb (".reg-arc-v2", ARC_LINUX_SIZEOF_V2_REGSET
, ARC_LINUX_SIZEOF_V2_REGSET
,
407 &arc_linux_v2_regset
, NULL
, cb_data
);
410 /* Implement the `core_read_description` gdbarch method. */
412 static const struct target_desc
*
413 arc_linux_core_read_description (struct gdbarch
*gdbarch
,
414 struct target_ops
*target
,
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
);
423 /* Initialization specific to Linux environment. */
426 arc_linux_init_osabi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
428 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
431 debug_printf ("arc-linux: GNU/Linux OS/ABI initialization.\n");
433 /* If we are using Linux, we have in uClibc
434 (libc/sysdeps/linux/arc/bits/setjmp.h):
436 typedef int __jmp_buf[13+1+1+1]; //r13-r25, fp, sp, blink
438 Where "blink" is a stored PC of a caller function.
442 linux_init_abi (info
, gdbarch
, false);
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
);
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
);
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
);
466 /* Suppress warning from -Wmissing-prototypes. */
467 extern initialize_file_ftype _initialize_arc_linux_tdep
;
470 _initialize_arc_linux_tdep ()
472 gdbarch_register_osabi (bfd_arch_arc
, 0, GDB_OSABI_LINUX
,
473 arc_linux_init_osabi
);