1 /* Target-dependent code for GNU/Linux m32r.
3 Copyright (C) 2004-2013 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/>. */
27 #include "reggroups.h"
30 #include "gdb_string.h"
32 #include "glibc-tdep.h"
33 #include "solib-svr4.h"
36 #include "trad-frame.h"
37 #include "frame-unwind.h"
39 #include "m32r-tdep.h"
40 #include "linux-tdep.h"
44 /* Recognizing signal handler frames. */
46 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
47 "realtime" (RT) signals. The RT signals can provide additional
48 information to the signal handler if the SA_SIGINFO flag is set
49 when establishing a signal handler using `sigaction'. It is not
50 unlikely that future versions of GNU/Linux will support SA_SIGINFO
51 for normal signals too. */
53 /* When the m32r Linux kernel calls a signal handler and the
54 SA_RESTORER flag isn't set, the return address points to a bit of
55 code on the stack. This function returns whether the PC appears to
56 be within this bit of code.
58 The instruction sequence for normal signals is
59 ldi r7, #__NR_sigreturn
61 or 0x67 0x77 0x10 0xf2.
63 Checking for the code sequence should be somewhat reliable, because
64 the effect is to call the system call sigreturn. This is unlikely
65 to occur anywhere other than in a signal trampoline.
67 It kind of sucks that we have to read memory from the process in
68 order to identify a signal trampoline, but there doesn't seem to be
69 any other way. Therefore we only do the memory reads if no
70 function name could be identified, which should be the case since
71 the code is on the stack.
73 Detection of signal trampolines for handlers that set the
74 SA_RESTORER flag is in general not possible. Unfortunately this is
75 what the GNU C Library has been doing for quite some time now.
76 However, as of version 2.1.2, the GNU C Library uses signal
77 trampolines (named __restore and __restore_rt) that are identical
78 to the ones used by the kernel. Therefore, these trampolines are
81 static const gdb_byte linux_sigtramp_code[] = {
82 0x67, 0x77, 0x10, 0xf2,
85 /* If PC is in a sigtramp routine, return the address of the start of
86 the routine. Otherwise, return 0. */
89 m32r_linux_sigtramp_start (CORE_ADDR pc, struct frame_info *this_frame)
93 /* We only recognize a signal trampoline if PC is at the start of
94 one of the instructions. We optimize for finding the PC at the
95 start of the instruction sequence, as will be the case when the
96 trampoline is not the first frame on the stack. We assume that
97 in the case where the PC is not at the start of the instruction
98 sequence, there will be a few trailing readable bytes on the
103 if (!safe_frame_unwind_memory (this_frame, pc, buf, 2))
106 if (memcmp (buf, linux_sigtramp_code, 2) == 0)
112 if (!safe_frame_unwind_memory (this_frame, pc, buf, 4))
115 if (memcmp (buf, linux_sigtramp_code, 4) != 0)
121 /* This function does the same for RT signals. Here the instruction
123 ldi r7, #__NR_rt_sigreturn
125 or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00.
127 The effect is to call the system call rt_sigreturn. */
129 static const gdb_byte linux_rt_sigtramp_code[] = {
130 0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00,
133 /* If PC is in a RT sigtramp routine, return the address of the start
134 of the routine. Otherwise, return 0. */
137 m32r_linux_rt_sigtramp_start (CORE_ADDR pc, struct frame_info *this_frame)
141 /* We only recognize a signal trampoline if PC is at the start of
142 one of the instructions. We optimize for finding the PC at the
143 start of the instruction sequence, as will be the case when the
144 trampoline is not the first frame on the stack. We assume that
145 in the case where the PC is not at the start of the instruction
146 sequence, there will be a few trailing readable bytes on the
152 if (!safe_frame_unwind_memory (this_frame, pc, buf, 4))
155 if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
157 if (!safe_frame_unwind_memory (this_frame, pc + 4, buf, 4))
160 if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
163 else if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
165 if (!safe_frame_unwind_memory (this_frame, pc - 4, buf, 4))
168 if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
176 m32r_linux_pc_in_sigtramp (CORE_ADDR pc, const char *name,
177 struct frame_info *this_frame)
179 /* If we have NAME, we can optimize the search. The trampolines are
180 named __restore and __restore_rt. However, they aren't dynamically
181 exported from the shared C library, so the trampoline may appear to
182 be part of the preceding function. This should always be sigaction,
183 __sigaction, or __libc_sigaction (all aliases to the same function). */
184 if (name == NULL || strstr (name, "sigaction") != NULL)
185 return (m32r_linux_sigtramp_start (pc, this_frame) != 0
186 || m32r_linux_rt_sigtramp_start (pc, this_frame) != 0);
188 return (strcmp ("__restore", name) == 0
189 || strcmp ("__restore_rt", name) == 0);
192 /* From <asm/sigcontext.h>. */
193 static int m32r_linux_sc_reg_offset[] = {
220 struct m32r_frame_cache
223 struct trad_frame_saved_reg *saved_regs;
226 static struct m32r_frame_cache *
227 m32r_linux_sigtramp_frame_cache (struct frame_info *this_frame,
230 struct m32r_frame_cache *cache;
231 CORE_ADDR sigcontext_addr, addr;
234 if ((*this_cache) != NULL)
235 return (*this_cache);
236 cache = FRAME_OBSTACK_ZALLOC (struct m32r_frame_cache);
237 (*this_cache) = cache;
238 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
240 cache->base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
241 sigcontext_addr = cache->base + 4;
243 cache->pc = get_frame_pc (this_frame);
244 addr = m32r_linux_sigtramp_start (cache->pc, this_frame);
247 /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR
249 addr = m32r_linux_rt_sigtramp_start (cache->pc, this_frame);
251 sigcontext_addr += 128;
253 addr = get_frame_func (this_frame);
257 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
259 for (regnum = 0; regnum < sizeof (m32r_linux_sc_reg_offset) / 4; regnum++)
261 if (m32r_linux_sc_reg_offset[regnum] >= 0)
262 cache->saved_regs[regnum].addr =
263 sigcontext_addr + m32r_linux_sc_reg_offset[regnum];
270 m32r_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
272 struct frame_id *this_id)
274 struct m32r_frame_cache *cache =
275 m32r_linux_sigtramp_frame_cache (this_frame, this_cache);
277 (*this_id) = frame_id_build (cache->base, cache->pc);
280 static struct value *
281 m32r_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
282 void **this_cache, int regnum)
284 struct m32r_frame_cache *cache =
285 m32r_linux_sigtramp_frame_cache (this_frame, this_cache);
287 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
291 m32r_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
292 struct frame_info *this_frame,
295 CORE_ADDR pc = get_frame_pc (this_frame);
298 find_pc_partial_function (pc, &name, NULL, NULL);
299 if (m32r_linux_pc_in_sigtramp (pc, name, this_frame))
305 static const struct frame_unwind m32r_linux_sigtramp_frame_unwind = {
307 default_frame_unwind_stop_reason,
308 m32r_linux_sigtramp_frame_this_id,
309 m32r_linux_sigtramp_frame_prev_register,
311 m32r_linux_sigtramp_frame_sniffer
314 /* Mapping between the registers in `struct pt_regs'
315 format and GDB's register array layout. */
317 static int m32r_pt_regs_offset[] = {
344 #define PSW_OFFSET (4 * 19)
345 #define BBPSW_OFFSET (4 * 21)
346 #define SPU_OFFSET (4 * 23)
347 #define SPI_OFFSET (4 * 26)
350 m32r_linux_supply_gregset (const struct regset *regset,
351 struct regcache *regcache, int regnum,
352 const void *gregs, size_t size)
354 const char *regs = gregs;
355 unsigned long psw, bbpsw;
358 psw = *((unsigned long *) (regs + PSW_OFFSET));
359 bbpsw = *((unsigned long *) (regs + BBPSW_OFFSET));
361 for (i = 0; i < sizeof (m32r_pt_regs_offset) / 4; i++)
363 if (regnum != -1 && regnum != i)
369 *((unsigned long *) (regs + m32r_pt_regs_offset[i])) =
370 ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8);
373 *((unsigned long *) (regs + m32r_pt_regs_offset[i])) =
378 *((unsigned long *) (regs + m32r_pt_regs_offset[i])) =
379 *((unsigned long *) (regs + SPU_OFFSET));
381 *((unsigned long *) (regs + m32r_pt_regs_offset[i])) =
382 *((unsigned long *) (regs + SPI_OFFSET));
386 regcache_raw_supply (regcache, i,
387 regs + m32r_pt_regs_offset[i]);
391 static struct regset m32r_linux_gregset = {
392 NULL, m32r_linux_supply_gregset
395 static const struct regset *
396 m32r_linux_regset_from_core_section (struct gdbarch *core_arch,
397 const char *sect_name, size_t sect_size)
399 struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch);
400 if (strcmp (sect_name, ".reg") == 0)
401 return &m32r_linux_gregset;
406 m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
408 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
410 linux_init_abi (info, gdbarch);
412 /* Since EVB register is not available for native debug, we reduce
413 the number of registers. */
414 set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS - 1);
416 frame_unwind_append_unwinder (gdbarch, &m32r_linux_sigtramp_frame_unwind);
418 /* GNU/Linux uses SVR4-style shared libraries. */
419 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
420 set_solib_svr4_fetch_link_map_offsets
421 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
423 /* Core file support. */
424 set_gdbarch_regset_from_core_section
425 (gdbarch, m32r_linux_regset_from_core_section);
427 /* Enable TLS support. */
428 set_gdbarch_fetch_tls_load_module_address (gdbarch,
429 svr4_fetch_objfile_link_map);
432 /* Provide a prototype to silence -Wmissing-prototypes. */
433 extern void _initialize_m32r_linux_tdep (void);
436 _initialize_m32r_linux_tdep (void)
438 gdbarch_register_osabi (bfd_arch_m32r, 0, GDB_OSABI_LINUX,
439 m32r_linux_init_abi);