1 /* Target-dependent code for GNU/Linux m32r.
3 Copyright (C) 2004-2024 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/>. */
26 #include "reggroups.h"
29 #include "glibc-tdep.h"
30 #include "solib-svr4.h"
33 #include "trad-frame.h"
34 #include "frame-unwind.h"
36 #include "m32r-tdep.h"
37 #include "linux-tdep.h"
42 /* Recognizing signal handler frames. */
44 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
45 "realtime" (RT) signals. The RT signals can provide additional
46 information to the signal handler if the SA_SIGINFO flag is set
47 when establishing a signal handler using `sigaction'. It is not
48 unlikely that future versions of GNU/Linux will support SA_SIGINFO
49 for normal signals too. */
51 /* When the m32r Linux kernel calls a signal handler and the
52 SA_RESTORER flag isn't set, the return address points to a bit of
53 code on the stack. This function returns whether the PC appears to
54 be within this bit of code.
56 The instruction sequence for normal signals is
57 ldi r7, #__NR_sigreturn
59 or 0x67 0x77 0x10 0xf2.
61 Checking for the code sequence should be somewhat reliable, because
62 the effect is to call the system call sigreturn. This is unlikely
63 to occur anywhere other than in a signal trampoline.
65 It kind of sucks that we have to read memory from the process in
66 order to identify a signal trampoline, but there doesn't seem to be
67 any other way. Therefore we only do the memory reads if no
68 function name could be identified, which should be the case since
69 the code is on the stack.
71 Detection of signal trampolines for handlers that set the
72 SA_RESTORER flag is in general not possible. Unfortunately this is
73 what the GNU C Library has been doing for quite some time now.
74 However, as of version 2.1.2, the GNU C Library uses signal
75 trampolines (named __restore and __restore_rt) that are identical
76 to the ones used by the kernel. Therefore, these trampolines are
79 static const gdb_byte linux_sigtramp_code
[] = {
80 0x67, 0x77, 0x10, 0xf2,
83 /* If PC is in a sigtramp routine, return the address of the start of
84 the routine. Otherwise, return 0. */
87 m32r_linux_sigtramp_start (CORE_ADDR pc
, const frame_info_ptr
&this_frame
)
91 /* We only recognize a signal trampoline if PC is at the start of
92 one of the instructions. We optimize for finding the PC at the
93 start of the instruction sequence, as will be the case when the
94 trampoline is not the first frame on the stack. We assume that
95 in the case where the PC is not at the start of the instruction
96 sequence, there will be a few trailing readable bytes on the
101 if (!safe_frame_unwind_memory (this_frame
, pc
, {buf
, 2}))
104 if (memcmp (buf
, linux_sigtramp_code
, 2) == 0)
110 if (!safe_frame_unwind_memory (this_frame
, pc
, {buf
, 4}))
113 if (memcmp (buf
, linux_sigtramp_code
, 4) != 0)
119 /* This function does the same for RT signals. Here the instruction
121 ldi r7, #__NR_rt_sigreturn
123 or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00.
125 The effect is to call the system call rt_sigreturn. */
127 static const gdb_byte linux_rt_sigtramp_code
[] = {
128 0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00,
131 /* If PC is in a RT sigtramp routine, return the address of the start
132 of the routine. Otherwise, return 0. */
135 m32r_linux_rt_sigtramp_start (CORE_ADDR pc
, const frame_info_ptr
&this_frame
)
139 /* We only recognize a signal trampoline if PC is at the start of
140 one of the instructions. We optimize for finding the PC at the
141 start of the instruction sequence, as will be the case when the
142 trampoline is not the first frame on the stack. We assume that
143 in the case where the PC is not at the start of the instruction
144 sequence, there will be a few trailing readable bytes on the
150 if (!safe_frame_unwind_memory (this_frame
, pc
, {buf
, 4}))
153 if (memcmp (buf
, linux_rt_sigtramp_code
, 4) == 0)
155 if (!safe_frame_unwind_memory (this_frame
, pc
+ 4, {buf
, 4}))
158 if (memcmp (buf
, linux_rt_sigtramp_code
+ 4, 4) == 0)
161 else if (memcmp (buf
, linux_rt_sigtramp_code
+ 4, 4) == 0)
163 if (!safe_frame_unwind_memory (this_frame
, pc
- 4, {buf
, 4}))
166 if (memcmp (buf
, linux_rt_sigtramp_code
, 4) == 0)
174 m32r_linux_pc_in_sigtramp (CORE_ADDR pc
, const char *name
,
175 const frame_info_ptr
&this_frame
)
177 /* If we have NAME, we can optimize the search. The trampolines are
178 named __restore and __restore_rt. However, they aren't dynamically
179 exported from the shared C library, so the trampoline may appear to
180 be part of the preceding function. This should always be sigaction,
181 __sigaction, or __libc_sigaction (all aliases to the same function). */
182 if (name
== NULL
|| strstr (name
, "sigaction") != NULL
)
183 return (m32r_linux_sigtramp_start (pc
, this_frame
) != 0
184 || m32r_linux_rt_sigtramp_start (pc
, this_frame
) != 0);
186 return (strcmp ("__restore", name
) == 0
187 || strcmp ("__restore_rt", name
) == 0);
190 /* From <asm/sigcontext.h>. */
191 static int m32r_linux_sc_reg_offset
[] = {
218 struct m32r_frame_cache
221 trad_frame_saved_reg
*saved_regs
;
224 static struct m32r_frame_cache
*
225 m32r_linux_sigtramp_frame_cache (const frame_info_ptr
&this_frame
,
228 struct m32r_frame_cache
*cache
;
229 CORE_ADDR sigcontext_addr
, addr
;
232 if ((*this_cache
) != NULL
)
233 return (struct m32r_frame_cache
*) (*this_cache
);
234 cache
= FRAME_OBSTACK_ZALLOC (struct m32r_frame_cache
);
235 (*this_cache
) = cache
;
236 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
238 cache
->base
= get_frame_register_unsigned (this_frame
, M32R_SP_REGNUM
);
239 sigcontext_addr
= cache
->base
+ 4;
241 cache
->pc
= get_frame_pc (this_frame
);
242 addr
= m32r_linux_sigtramp_start (cache
->pc
, this_frame
);
245 /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR
247 addr
= m32r_linux_rt_sigtramp_start (cache
->pc
, this_frame
);
249 sigcontext_addr
+= 128;
251 addr
= get_frame_func (this_frame
);
255 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
257 for (regnum
= 0; regnum
< sizeof (m32r_linux_sc_reg_offset
) / 4; regnum
++)
259 if (m32r_linux_sc_reg_offset
[regnum
] >= 0)
260 cache
->saved_regs
[regnum
].set_addr (sigcontext_addr
261 + m32r_linux_sc_reg_offset
[regnum
]);
268 m32r_linux_sigtramp_frame_this_id (const frame_info_ptr
&this_frame
,
270 struct frame_id
*this_id
)
272 struct m32r_frame_cache
*cache
=
273 m32r_linux_sigtramp_frame_cache (this_frame
, this_cache
);
275 (*this_id
) = frame_id_build (cache
->base
, cache
->pc
);
278 static struct value
*
279 m32r_linux_sigtramp_frame_prev_register (const frame_info_ptr
&this_frame
,
280 void **this_cache
, int regnum
)
282 struct m32r_frame_cache
*cache
=
283 m32r_linux_sigtramp_frame_cache (this_frame
, this_cache
);
285 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
, regnum
);
289 m32r_linux_sigtramp_frame_sniffer (const struct frame_unwind
*self
,
290 const frame_info_ptr
&this_frame
,
293 CORE_ADDR pc
= get_frame_pc (this_frame
);
296 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
297 if (m32r_linux_pc_in_sigtramp (pc
, name
, this_frame
))
303 static const struct frame_unwind m32r_linux_sigtramp_frame_unwind
= {
304 "m32r linux sigtramp",
306 default_frame_unwind_stop_reason
,
307 m32r_linux_sigtramp_frame_this_id
,
308 m32r_linux_sigtramp_frame_prev_register
,
310 m32r_linux_sigtramp_frame_sniffer
313 /* Mapping between the registers in `struct pt_regs'
314 format and GDB's register array layout. */
316 static int m32r_pt_regs_offset
[] = {
343 #define PSW_OFFSET (4 * 19)
344 #define BBPSW_OFFSET (4 * 21)
345 #define SPU_OFFSET (4 * 23)
346 #define SPI_OFFSET (4 * 26)
348 #define M32R_LINUX_GREGS_SIZE (4 * 28)
351 m32r_linux_supply_gregset (const struct regset
*regset
,
352 struct regcache
*regcache
, int regnum
,
353 const void *gregs
, size_t size
)
355 const gdb_byte
*regs
= (const gdb_byte
*) gregs
;
356 enum bfd_endian byte_order
=
357 gdbarch_byte_order (regcache
->arch ());
363 psw
= extract_unsigned_integer (regs
+ PSW_OFFSET
, 4, byte_order
);
364 bbpsw
= extract_unsigned_integer (regs
+ BBPSW_OFFSET
, 4, byte_order
);
365 psw
= ((0x00c1 & bbpsw
) << 8) | ((0xc100 & psw
) >> 8);
367 for (i
= 0; i
< ARRAY_SIZE (m32r_pt_regs_offset
); i
++)
369 if (regnum
!= -1 && regnum
!= i
)
375 store_unsigned_integer (buf
, 4, byte_order
, psw
);
379 store_unsigned_integer (buf
, 4, byte_order
, psw
& 1);
383 p
= regs
+ ((psw
& 0x80) ? SPU_OFFSET
: SPI_OFFSET
);
386 p
= regs
+ m32r_pt_regs_offset
[i
];
389 regcache
->raw_supply (i
, p
);
394 m32r_linux_collect_gregset (const struct regset
*regset
,
395 const struct regcache
*regcache
,
396 int regnum
, void *gregs
, size_t size
)
398 gdb_byte
*regs
= (gdb_byte
*) gregs
;
400 enum bfd_endian byte_order
=
401 gdbarch_byte_order (regcache
->arch ());
405 regcache
->raw_collect (PSW_REGNUM
, buf
);
406 psw
= extract_unsigned_integer (buf
, 4, byte_order
);
408 for (i
= 0; i
< ARRAY_SIZE (m32r_pt_regs_offset
); i
++)
410 if (regnum
!= -1 && regnum
!= i
)
416 store_unsigned_integer (regs
+ PSW_OFFSET
, 4, byte_order
,
418 store_unsigned_integer (regs
+ BBPSW_OFFSET
, 4, byte_order
,
424 regcache
->raw_collect
425 (i
, regs
+ ((psw
& 0x80) ? SPU_OFFSET
: SPI_OFFSET
));
428 regcache
->raw_collect (i
, regs
+ m32r_pt_regs_offset
[i
]);
433 static const struct regset m32r_linux_gregset
= {
435 m32r_linux_supply_gregset
, m32r_linux_collect_gregset
439 m32r_linux_iterate_over_regset_sections (struct gdbarch
*gdbarch
,
440 iterate_over_regset_sections_cb
*cb
,
442 const struct regcache
*regcache
)
444 cb (".reg", M32R_LINUX_GREGS_SIZE
, M32R_LINUX_GREGS_SIZE
, &m32r_linux_gregset
,
449 m32r_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
452 linux_init_abi (info
, gdbarch
, 0);
454 /* Since EVB register is not available for native debug, we reduce
455 the number of registers. */
456 set_gdbarch_num_regs (gdbarch
, M32R_NUM_REGS
- 1);
458 frame_unwind_append_unwinder (gdbarch
, &m32r_linux_sigtramp_frame_unwind
);
460 /* GNU/Linux uses SVR4-style shared libraries. */
461 set_gdbarch_skip_trampoline_code (gdbarch
, find_solib_trampoline_target
);
462 set_solib_svr4_fetch_link_map_offsets
463 (gdbarch
, linux_ilp32_fetch_link_map_offsets
);
465 /* Core file support. */
466 set_gdbarch_iterate_over_regset_sections
467 (gdbarch
, m32r_linux_iterate_over_regset_sections
);
469 /* Enable TLS support. */
470 set_gdbarch_fetch_tls_load_module_address (gdbarch
,
471 svr4_fetch_objfile_link_map
);
474 void _initialize_m32r_linux_tdep ();
476 _initialize_m32r_linux_tdep ()
478 gdbarch_register_osabi (bfd_arch_m32r
, 0, GDB_OSABI_LINUX
,
479 m32r_linux_init_abi
);