1 /* Target-dependent code for GNU/Linux m32r.
3 Copyright (C) 2004 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
29 #include "reggroups.h"
32 #include "gdb_string.h"
34 #include "glibc-tdep.h"
35 #include "solib-svr4.h"
37 #include "trad-frame.h"
38 #include "frame-unwind.h"
40 #include "m32r-tdep.h"
43 /* Recognizing signal handler frames. */
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. */
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.
57 The instruction sequence for normal signals is
58 ldi r7, #__NR_sigreturn
60 or 0x67 0x77 0x10 0xf2.
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.
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.
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
80 static const gdb_byte linux_sigtramp_code
[] = {
81 0x67, 0x77, 0x10, 0xf2,
84 /* If PC is in a sigtramp routine, return the address of the start of
85 the routine. Otherwise, return 0. */
88 m32r_linux_sigtramp_start (CORE_ADDR pc
, struct frame_info
*next_frame
)
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
102 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, 2))
105 if (memcmp (buf
, linux_sigtramp_code
, 2) == 0)
111 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, 4))
114 if (memcmp (buf
, linux_sigtramp_code
, 4) != 0)
120 /* This function does the same for RT signals. Here the instruction
122 ldi r7, #__NR_rt_sigreturn
124 or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00.
126 The effect is to call the system call rt_sigreturn. */
128 static const gdb_byte linux_rt_sigtramp_code
[] = {
129 0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00,
132 /* If PC is in a RT sigtramp routine, return the address of the start
133 of the routine. Otherwise, return 0. */
136 m32r_linux_rt_sigtramp_start (CORE_ADDR pc
, struct frame_info
*next_frame
)
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
151 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, 4))
154 if (memcmp (buf
, linux_rt_sigtramp_code
, 4) == 0)
156 if (!safe_frame_unwind_memory (next_frame
, pc
+ 4, buf
, 4))
159 if (memcmp (buf
, linux_rt_sigtramp_code
+ 4, 4) == 0)
162 else if (memcmp (buf
, linux_rt_sigtramp_code
+ 4, 4) == 0)
164 if (!safe_frame_unwind_memory (next_frame
, pc
- 4, buf
, 4))
167 if (memcmp (buf
, linux_rt_sigtramp_code
, 4) == 0)
175 m32r_linux_pc_in_sigtramp (CORE_ADDR pc
, char *name
,
176 struct frame_info
*next_frame
)
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
)
184 return (m32r_linux_sigtramp_start (pc
, next_frame
) != 0
185 || m32r_linux_rt_sigtramp_start (pc
, next_frame
) != 0);
187 return (strcmp ("__restore", name
) == 0
188 || strcmp ("__restore_rt", name
) == 0);
191 /* From <asm/sigcontext.h>. */
192 static int m32r_linux_sc_reg_offset
[] = {
219 struct m32r_frame_cache
222 struct trad_frame_saved_reg
*saved_regs
;
225 static struct m32r_frame_cache
*
226 m32r_linux_sigtramp_frame_cache (struct frame_info
*next_frame
,
229 struct m32r_frame_cache
*cache
;
230 CORE_ADDR sigcontext_addr
, addr
;
233 if ((*this_cache
) != NULL
)
234 return (*this_cache
);
235 cache
= FRAME_OBSTACK_ZALLOC (struct m32r_frame_cache
);
236 (*this_cache
) = cache
;
237 cache
->saved_regs
= trad_frame_alloc_saved_regs (next_frame
);
239 cache
->base
= frame_unwind_register_unsigned (next_frame
, M32R_SP_REGNUM
);
240 sigcontext_addr
= cache
->base
+ 4;
242 cache
->pc
= frame_pc_unwind (next_frame
);
243 addr
= m32r_linux_sigtramp_start (cache
->pc
, next_frame
);
246 /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR
248 addr
= m32r_linux_rt_sigtramp_start (cache
->pc
, next_frame
);
250 sigcontext_addr
+= 128;
252 addr
= frame_func_unwind (next_frame
);
256 cache
->saved_regs
= trad_frame_alloc_saved_regs (next_frame
);
258 for (regnum
= 0; regnum
< sizeof (m32r_linux_sc_reg_offset
) / 4; regnum
++)
260 if (m32r_linux_sc_reg_offset
[regnum
] >= 0)
261 cache
->saved_regs
[regnum
].addr
=
262 sigcontext_addr
+ m32r_linux_sc_reg_offset
[regnum
];
269 m32r_linux_sigtramp_frame_this_id (struct frame_info
*next_frame
,
271 struct frame_id
*this_id
)
273 struct m32r_frame_cache
*cache
=
274 m32r_linux_sigtramp_frame_cache (next_frame
, this_cache
);
276 (*this_id
) = frame_id_build (cache
->base
, cache
->pc
);
280 m32r_linux_sigtramp_frame_prev_register (struct frame_info
*next_frame
,
282 int regnum
, int *optimizedp
,
283 enum lval_type
*lvalp
,
285 int *realnump
, gdb_byte
*valuep
)
287 struct m32r_frame_cache
*cache
=
288 m32r_linux_sigtramp_frame_cache (next_frame
, this_cache
);
290 trad_frame_get_prev_register (next_frame
, cache
->saved_regs
, regnum
,
291 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
294 static const struct frame_unwind m32r_linux_sigtramp_frame_unwind
= {
296 m32r_linux_sigtramp_frame_this_id
,
297 m32r_linux_sigtramp_frame_prev_register
300 static const struct frame_unwind
*
301 m32r_linux_sigtramp_frame_sniffer (struct frame_info
*next_frame
)
303 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
306 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
307 if (m32r_linux_pc_in_sigtramp (pc
, name
, next_frame
))
308 return &m32r_linux_sigtramp_frame_unwind
;
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)
349 m32r_linux_supply_gregset (const struct regset
*regset
,
350 struct regcache
*regcache
, int regnum
,
351 const void *gregs
, size_t size
)
353 const char *regs
= gregs
;
354 unsigned long psw
, bbpsw
;
357 psw
= *((unsigned long *) (regs
+ PSW_OFFSET
));
358 bbpsw
= *((unsigned long *) (regs
+ BBPSW_OFFSET
));
360 for (i
= 0; i
< sizeof (m32r_pt_regs_offset
) / 4; i
++)
362 if (regnum
!= -1 && regnum
!= i
)
368 *((unsigned long *) (regs
+ m32r_pt_regs_offset
[i
])) =
369 ((0x00c1 & bbpsw
) << 8) | ((0xc100 & psw
) >> 8);
372 *((unsigned long *) (regs
+ m32r_pt_regs_offset
[i
])) =
377 *((unsigned long *) (regs
+ m32r_pt_regs_offset
[i
])) =
378 *((unsigned long *) (regs
+ SPU_OFFSET
));
380 *((unsigned long *) (regs
+ m32r_pt_regs_offset
[i
])) =
381 *((unsigned long *) (regs
+ SPI_OFFSET
));
385 regcache_raw_supply (current_regcache
, i
,
386 regs
+ m32r_pt_regs_offset
[i
]);
390 static struct regset m32r_linux_gregset
= {
391 NULL
, m32r_linux_supply_gregset
394 static const struct regset
*
395 m32r_linux_regset_from_core_section (struct gdbarch
*core_arch
,
396 const char *sect_name
, size_t sect_size
)
398 struct gdbarch_tdep
*tdep
= gdbarch_tdep (core_arch
);
399 if (strcmp (sect_name
, ".reg") == 0)
400 return &m32r_linux_gregset
;
405 m32r_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
407 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
409 /* Since EVB register is not available for native debug, we reduce
410 the number of registers. */
411 set_gdbarch_num_regs (gdbarch
, M32R_NUM_REGS
- 1);
413 frame_unwind_append_sniffer (gdbarch
, m32r_linux_sigtramp_frame_sniffer
);
415 /* GNU/Linux uses SVR4-style shared libraries. */
416 set_solib_svr4_fetch_link_map_offsets
417 (gdbarch
, svr4_ilp32_fetch_link_map_offsets
);
419 /* Core file support. */
420 set_gdbarch_regset_from_core_section
421 (gdbarch
, m32r_linux_regset_from_core_section
);
423 /* Enable TLS support. */
424 set_gdbarch_fetch_tls_load_module_address (gdbarch
,
425 svr4_fetch_objfile_link_map
);
428 /* Provide a prototype to silence -Wmissing-prototypes. */
429 extern void _initialize_m32r_linux_tdep (void);
432 _initialize_m32r_linux_tdep (void)
434 gdbarch_register_osabi (bfd_arch_m32r
, 0, GDB_OSABI_LINUX
,
435 m32r_linux_init_abi
);