]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/m32r-linux-tdep.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / m32r-linux-tdep.c
CommitLineData
9b32d526
KI
1/* Target-dependent code for GNU/Linux m32r.
2
1d506c26 3 Copyright (C) 2004-2024 Free Software Foundation, Inc.
9b32d526
KI
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
9b32d526
KI
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9b32d526 19
ec452525 20#include "extract-store-integer.h"
9b32d526
KI
21#include "gdbcore.h"
22#include "frame.h"
23#include "value.h"
24#include "regcache.h"
25#include "inferior.h"
26#include "osabi.h"
27#include "reggroups.h"
9b098f24 28#include "regset.h"
9b32d526 29
9b32d526
KI
30#include "glibc-tdep.h"
31#include "solib-svr4.h"
982e9687 32#include "symtab.h"
9b32d526
KI
33
34#include "trad-frame.h"
35#include "frame-unwind.h"
36
37#include "m32r-tdep.h"
a5ee0f0c 38#include "linux-tdep.h"
0d12e84c 39#include "gdbarch.h"
a5ee0f0c 40
9b32d526
KI
41\f
42
43/* Recognizing signal handler frames. */
44
45/* GNU/Linux has two flavors of signals. Normal signal handlers, and
46 "realtime" (RT) signals. The RT signals can provide additional
47 information to the signal handler if the SA_SIGINFO flag is set
48 when establishing a signal handler using `sigaction'. It is not
49 unlikely that future versions of GNU/Linux will support SA_SIGINFO
50 for normal signals too. */
51
52/* When the m32r Linux kernel calls a signal handler and the
53 SA_RESTORER flag isn't set, the return address points to a bit of
54 code on the stack. This function returns whether the PC appears to
55 be within this bit of code.
56
57 The instruction sequence for normal signals is
58 ldi r7, #__NR_sigreturn
59 trap #2
60 or 0x67 0x77 0x10 0xf2.
61
62 Checking for the code sequence should be somewhat reliable, because
63 the effect is to call the system call sigreturn. This is unlikely
64 to occur anywhere other than in a signal trampoline.
65
66 It kind of sucks that we have to read memory from the process in
67 order to identify a signal trampoline, but there doesn't seem to be
68 any other way. Therefore we only do the memory reads if no
69 function name could be identified, which should be the case since
70 the code is on the stack.
71
72 Detection of signal trampolines for handlers that set the
73 SA_RESTORER flag is in general not possible. Unfortunately this is
74 what the GNU C Library has been doing for quite some time now.
75 However, as of version 2.1.2, the GNU C Library uses signal
76 trampolines (named __restore and __restore_rt) that are identical
77 to the ones used by the kernel. Therefore, these trampolines are
78 supported too. */
79
16ac4ab5 80static const gdb_byte linux_sigtramp_code[] = {
9b32d526
KI
81 0x67, 0x77, 0x10, 0xf2,
82};
83
84/* If PC is in a sigtramp routine, return the address of the start of
85 the routine. Otherwise, return 0. */
86
87static CORE_ADDR
8480a37e 88m32r_linux_sigtramp_start (CORE_ADDR pc, const frame_info_ptr &this_frame)
9b32d526 89{
16ac4ab5 90 gdb_byte buf[4];
9b32d526
KI
91
92 /* We only recognize a signal trampoline if PC is at the start of
93 one of the instructions. We optimize for finding the PC at the
94 start of the instruction sequence, as will be the case when the
95 trampoline is not the first frame on the stack. We assume that
96 in the case where the PC is not at the start of the instruction
97 sequence, there will be a few trailing readable bytes on the
98 stack. */
99
100 if (pc % 2 != 0)
101 {
bdec2917 102 if (!safe_frame_unwind_memory (this_frame, pc, {buf, 2}))
9b32d526
KI
103 return 0;
104
105 if (memcmp (buf, linux_sigtramp_code, 2) == 0)
106 pc -= 2;
107 else
108 return 0;
109 }
110
bdec2917 111 if (!safe_frame_unwind_memory (this_frame, pc, {buf, 4}))
9b32d526
KI
112 return 0;
113
114 if (memcmp (buf, linux_sigtramp_code, 4) != 0)
115 return 0;
116
117 return pc;
118}
119
120/* This function does the same for RT signals. Here the instruction
121 sequence is
122 ldi r7, #__NR_rt_sigreturn
123 trap #2
124 or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00.
125
126 The effect is to call the system call rt_sigreturn. */
127
16ac4ab5 128static const gdb_byte linux_rt_sigtramp_code[] = {
9b32d526
KI
129 0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00,
130};
131
132/* If PC is in a RT sigtramp routine, return the address of the start
133 of the routine. Otherwise, return 0. */
134
135static CORE_ADDR
8480a37e 136m32r_linux_rt_sigtramp_start (CORE_ADDR pc, const frame_info_ptr &this_frame)
9b32d526 137{
16ac4ab5 138 gdb_byte buf[4];
9b32d526
KI
139
140 /* We only recognize a signal trampoline if PC is at the start of
141 one of the instructions. We optimize for finding the PC at the
142 start of the instruction sequence, as will be the case when the
143 trampoline is not the first frame on the stack. We assume that
144 in the case where the PC is not at the start of the instruction
145 sequence, there will be a few trailing readable bytes on the
146 stack. */
147
148 if (pc % 2 != 0)
149 return 0;
150
bdec2917 151 if (!safe_frame_unwind_memory (this_frame, pc, {buf, 4}))
9b32d526
KI
152 return 0;
153
154 if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
155 {
bdec2917 156 if (!safe_frame_unwind_memory (this_frame, pc + 4, {buf, 4}))
9b32d526
KI
157 return 0;
158
159 if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
160 return pc;
161 }
162 else if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
163 {
bdec2917 164 if (!safe_frame_unwind_memory (this_frame, pc - 4, {buf, 4}))
9b32d526
KI
165 return 0;
166
167 if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
168 return pc - 4;
169 }
170
171 return 0;
172}
173
174static int
2c02bd72 175m32r_linux_pc_in_sigtramp (CORE_ADDR pc, const char *name,
8480a37e 176 const frame_info_ptr &this_frame)
9b32d526
KI
177{
178 /* If we have NAME, we can optimize the search. The trampolines are
179 named __restore and __restore_rt. However, they aren't dynamically
180 exported from the shared C library, so the trampoline may appear to
181 be part of the preceding function. This should always be sigaction,
182 __sigaction, or __libc_sigaction (all aliases to the same function). */
183 if (name == NULL || strstr (name, "sigaction") != NULL)
94afd7a6
UW
184 return (m32r_linux_sigtramp_start (pc, this_frame) != 0
185 || m32r_linux_rt_sigtramp_start (pc, this_frame) != 0);
9b32d526
KI
186
187 return (strcmp ("__restore", name) == 0
188 || strcmp ("__restore_rt", name) == 0);
189}
190
191/* From <asm/sigcontext.h>. */
192static int m32r_linux_sc_reg_offset[] = {
193 4 * 4, /* r0 */
194 5 * 4, /* r1 */
195 6 * 4, /* r2 */
196 7 * 4, /* r3 */
197 0 * 4, /* r4 */
198 1 * 4, /* r5 */
199 2 * 4, /* r6 */
200 8 * 4, /* r7 */
201 9 * 4, /* r8 */
202 10 * 4, /* r9 */
203 11 * 4, /* r10 */
204 12 * 4, /* r11 */
205 13 * 4, /* r12 */
206 21 * 4, /* fp */
207 22 * 4, /* lr */
208 -1 * 4, /* sp */
209 16 * 4, /* psw */
210 -1 * 4, /* cbr */
211 23 * 4, /* spi */
212 20 * 4, /* spu */
213 19 * 4, /* bpc */
214 17 * 4, /* pc */
215 15 * 4, /* accl */
216 14 * 4 /* acch */
217};
218
219struct m32r_frame_cache
220{
221 CORE_ADDR base, pc;
098caef4 222 trad_frame_saved_reg *saved_regs;
9b32d526
KI
223};
224
225static struct m32r_frame_cache *
8480a37e 226m32r_linux_sigtramp_frame_cache (const frame_info_ptr &this_frame,
9b32d526
KI
227 void **this_cache)
228{
229 struct m32r_frame_cache *cache;
230 CORE_ADDR sigcontext_addr, addr;
231 int regnum;
232
233 if ((*this_cache) != NULL)
9a3c8263 234 return (struct m32r_frame_cache *) (*this_cache);
9b32d526
KI
235 cache = FRAME_OBSTACK_ZALLOC (struct m32r_frame_cache);
236 (*this_cache) = cache;
94afd7a6 237 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
9b32d526 238
94afd7a6 239 cache->base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
9b32d526
KI
240 sigcontext_addr = cache->base + 4;
241
94afd7a6
UW
242 cache->pc = get_frame_pc (this_frame);
243 addr = m32r_linux_sigtramp_start (cache->pc, this_frame);
9b32d526
KI
244 if (addr == 0)
245 {
246 /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR
dda83cd7 247 accordingly. */
94afd7a6 248 addr = m32r_linux_rt_sigtramp_start (cache->pc, this_frame);
9b32d526
KI
249 if (addr)
250 sigcontext_addr += 128;
251 else
94afd7a6 252 addr = get_frame_func (this_frame);
9b32d526
KI
253 }
254 cache->pc = addr;
255
94afd7a6 256 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
9b32d526
KI
257
258 for (regnum = 0; regnum < sizeof (m32r_linux_sc_reg_offset) / 4; regnum++)
259 {
260 if (m32r_linux_sc_reg_offset[regnum] >= 0)
098caef4
LM
261 cache->saved_regs[regnum].set_addr (sigcontext_addr
262 + m32r_linux_sc_reg_offset[regnum]);
9b32d526
KI
263 }
264
265 return cache;
266}
267
268static void
8480a37e 269m32r_linux_sigtramp_frame_this_id (const frame_info_ptr &this_frame,
9b32d526
KI
270 void **this_cache,
271 struct frame_id *this_id)
272{
273 struct m32r_frame_cache *cache =
94afd7a6 274 m32r_linux_sigtramp_frame_cache (this_frame, this_cache);
9b32d526
KI
275
276 (*this_id) = frame_id_build (cache->base, cache->pc);
277}
278
94afd7a6 279static struct value *
8480a37e 280m32r_linux_sigtramp_frame_prev_register (const frame_info_ptr &this_frame,
94afd7a6 281 void **this_cache, int regnum)
9b32d526
KI
282{
283 struct m32r_frame_cache *cache =
94afd7a6 284 m32r_linux_sigtramp_frame_cache (this_frame, this_cache);
9b32d526 285
94afd7a6 286 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
9b32d526
KI
287}
288
94afd7a6
UW
289static int
290m32r_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
8480a37e 291 const frame_info_ptr &this_frame,
94afd7a6 292 void **this_cache)
9b32d526 293{
94afd7a6 294 CORE_ADDR pc = get_frame_pc (this_frame);
2c02bd72 295 const char *name;
9b32d526
KI
296
297 find_pc_partial_function (pc, &name, NULL, NULL);
94afd7a6
UW
298 if (m32r_linux_pc_in_sigtramp (pc, name, this_frame))
299 return 1;
9b32d526 300
94afd7a6 301 return 0;
9b32d526
KI
302}
303
94afd7a6 304static const struct frame_unwind m32r_linux_sigtramp_frame_unwind = {
a154d838 305 "m32r linux sigtramp",
94afd7a6 306 SIGTRAMP_FRAME,
8fbca658 307 default_frame_unwind_stop_reason,
94afd7a6
UW
308 m32r_linux_sigtramp_frame_this_id,
309 m32r_linux_sigtramp_frame_prev_register,
310 NULL,
311 m32r_linux_sigtramp_frame_sniffer
312};
313
9b098f24
KI
314/* Mapping between the registers in `struct pt_regs'
315 format and GDB's register array layout. */
316
317static int m32r_pt_regs_offset[] = {
318 4 * 4, /* r0 */
319 4 * 5, /* r1 */
320 4 * 6, /* r2 */
321 4 * 7, /* r3 */
322 4 * 0, /* r4 */
323 4 * 1, /* r5 */
324 4 * 2, /* r6 */
325 4 * 8, /* r7 */
326 4 * 9, /* r8 */
327 4 * 10, /* r9 */
328 4 * 11, /* r10 */
329 4 * 12, /* r11 */
330 4 * 13, /* r12 */
331 4 * 24, /* fp */
332 4 * 25, /* lr */
333 4 * 23, /* sp */
334 4 * 19, /* psw */
335 4 * 19, /* cbr */
336 4 * 26, /* spi */
337 4 * 23, /* spu */
338 4 * 22, /* bpc */
339 4 * 20, /* pc */
340 4 * 16, /* accl */
341 4 * 15 /* acch */
342};
343
344#define PSW_OFFSET (4 * 19)
345#define BBPSW_OFFSET (4 * 21)
346#define SPU_OFFSET (4 * 23)
347#define SPI_OFFSET (4 * 26)
348
5fac247f
AA
349#define M32R_LINUX_GREGS_SIZE (4 * 28)
350
9b098f24
KI
351static void
352m32r_linux_supply_gregset (const struct regset *regset,
353 struct regcache *regcache, int regnum,
354 const void *gregs, size_t size)
355{
9a3c8263 356 const gdb_byte *regs = (const gdb_byte *) gregs;
ba199d7d 357 enum bfd_endian byte_order =
ac7936df 358 gdbarch_byte_order (regcache->arch ());
ba199d7d
AA
359 ULONGEST psw, bbpsw;
360 gdb_byte buf[4];
361 const gdb_byte *p;
9b098f24
KI
362 int i;
363
ba199d7d
AA
364 psw = extract_unsigned_integer (regs + PSW_OFFSET, 4, byte_order);
365 bbpsw = extract_unsigned_integer (regs + BBPSW_OFFSET, 4, byte_order);
366 psw = ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8);
9b098f24 367
ba199d7d 368 for (i = 0; i < ARRAY_SIZE (m32r_pt_regs_offset); i++)
9b098f24
KI
369 {
370 if (regnum != -1 && regnum != i)
371 continue;
372
373 switch (i)
374 {
375 case PSW_REGNUM:
ba199d7d
AA
376 store_unsigned_integer (buf, 4, byte_order, psw);
377 p = buf;
9b098f24
KI
378 break;
379 case CBR_REGNUM:
ba199d7d
AA
380 store_unsigned_integer (buf, 4, byte_order, psw & 1);
381 p = buf;
9b098f24
KI
382 break;
383 case M32R_SP_REGNUM:
ba199d7d 384 p = regs + ((psw & 0x80) ? SPU_OFFSET : SPI_OFFSET);
9b098f24 385 break;
ba199d7d
AA
386 default:
387 p = regs + m32r_pt_regs_offset[i];
9b098f24
KI
388 }
389
73e1c03f 390 regcache->raw_supply (i, p);
ba199d7d
AA
391 }
392}
393
394static void
395m32r_linux_collect_gregset (const struct regset *regset,
396 const struct regcache *regcache,
397 int regnum, void *gregs, size_t size)
398{
9a3c8263 399 gdb_byte *regs = (gdb_byte *) gregs;
ba199d7d
AA
400 int i;
401 enum bfd_endian byte_order =
ac7936df 402 gdbarch_byte_order (regcache->arch ());
ba199d7d
AA
403 ULONGEST psw;
404 gdb_byte buf[4];
405
34a79281 406 regcache->raw_collect (PSW_REGNUM, buf);
ba199d7d
AA
407 psw = extract_unsigned_integer (buf, 4, byte_order);
408
409 for (i = 0; i < ARRAY_SIZE (m32r_pt_regs_offset); i++)
410 {
411 if (regnum != -1 && regnum != i)
412 continue;
413
414 switch (i)
415 {
416 case PSW_REGNUM:
417 store_unsigned_integer (regs + PSW_OFFSET, 4, byte_order,
418 (psw & 0xc1) << 8);
419 store_unsigned_integer (regs + BBPSW_OFFSET, 4, byte_order,
420 (psw >> 8) & 0xc1);
421 break;
422 case CBR_REGNUM:
423 break;
424 case M32R_SP_REGNUM:
34a79281
SM
425 regcache->raw_collect
426 (i, regs + ((psw & 0x80) ? SPU_OFFSET : SPI_OFFSET));
ba199d7d
AA
427 break;
428 default:
34a79281 429 regcache->raw_collect (i, regs + m32r_pt_regs_offset[i]);
ba199d7d 430 }
9b098f24
KI
431 }
432}
433
3ca7dae4 434static const struct regset m32r_linux_gregset = {
ba199d7d
AA
435 NULL,
436 m32r_linux_supply_gregset, m32r_linux_collect_gregset
9b098f24
KI
437};
438
5fac247f
AA
439static void
440m32r_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
441 iterate_over_regset_sections_cb *cb,
442 void *cb_data,
443 const struct regcache *regcache)
9b098f24 444{
a616bb94
AH
445 cb (".reg", M32R_LINUX_GREGS_SIZE, M32R_LINUX_GREGS_SIZE, &m32r_linux_gregset,
446 NULL, cb_data);
9b098f24
KI
447}
448
9b32d526
KI
449static void
450m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
451{
9b32d526 452
480af54c 453 linux_init_abi (info, gdbarch, 0);
a5ee0f0c 454
9b32d526
KI
455 /* Since EVB register is not available for native debug, we reduce
456 the number of registers. */
457 set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS - 1);
458
94afd7a6 459 frame_unwind_append_unwinder (gdbarch, &m32r_linux_sigtramp_frame_unwind);
9b32d526
KI
460
461 /* GNU/Linux uses SVR4-style shared libraries. */
982e9687 462 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
9b32d526 463 set_solib_svr4_fetch_link_map_offsets
c0154a4a 464 (gdbarch, linux_ilp32_fetch_link_map_offsets);
9b098f24
KI
465
466 /* Core file support. */
5fac247f
AA
467 set_gdbarch_iterate_over_regset_sections
468 (gdbarch, m32r_linux_iterate_over_regset_sections);
b2756930
KB
469
470 /* Enable TLS support. */
471 set_gdbarch_fetch_tls_load_module_address (gdbarch,
dda83cd7 472 svr4_fetch_objfile_link_map);
9b32d526
KI
473}
474
6c265988 475void _initialize_m32r_linux_tdep ();
9b32d526 476void
6c265988 477_initialize_m32r_linux_tdep ()
9b32d526
KI
478{
479 gdbarch_register_osabi (bfd_arch_m32r, 0, GDB_OSABI_LINUX,
480 m32r_linux_init_abi);
481}