1 /* Target-dependent code for GNU/Linux m32r.
3 Copyright (C) 2004, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "reggroups.h"
31 #include "gdb_string.h"
33 #include "glibc-tdep.h"
34 #include "solib-svr4.h"
37 #include "trad-frame.h"
38 #include "frame-unwind.h"
40 #include "m32r-tdep.h"
41 #include "linux-tdep.h"
45 /* Recognizing signal handler frames. */
47 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
48 "realtime" (RT) signals. The RT signals can provide additional
49 information to the signal handler if the SA_SIGINFO flag is set
50 when establishing a signal handler using `sigaction'. It is not
51 unlikely that future versions of GNU/Linux will support SA_SIGINFO
52 for normal signals too. */
54 /* When the m32r Linux kernel calls a signal handler and the
55 SA_RESTORER flag isn't set, the return address points to a bit of
56 code on the stack. This function returns whether the PC appears to
57 be within this bit of code.
59 The instruction sequence for normal signals is
60 ldi r7, #__NR_sigreturn
62 or 0x67 0x77 0x10 0xf2.
64 Checking for the code sequence should be somewhat reliable, because
65 the effect is to call the system call sigreturn. This is unlikely
66 to occur anywhere other than in a signal trampoline.
68 It kind of sucks that we have to read memory from the process in
69 order to identify a signal trampoline, but there doesn't seem to be
70 any other way. Therefore we only do the memory reads if no
71 function name could be identified, which should be the case since
72 the code is on the stack.
74 Detection of signal trampolines for handlers that set the
75 SA_RESTORER flag is in general not possible. Unfortunately this is
76 what the GNU C Library has been doing for quite some time now.
77 However, as of version 2.1.2, the GNU C Library uses signal
78 trampolines (named __restore and __restore_rt) that are identical
79 to the ones used by the kernel. Therefore, these trampolines are
82 static const gdb_byte linux_sigtramp_code
[] = {
83 0x67, 0x77, 0x10, 0xf2,
86 /* If PC is in a sigtramp routine, return the address of the start of
87 the routine. Otherwise, return 0. */
90 m32r_linux_sigtramp_start (CORE_ADDR pc
, struct frame_info
*this_frame
)
94 /* We only recognize a signal trampoline if PC is at the start of
95 one of the instructions. We optimize for finding the PC at the
96 start of the instruction sequence, as will be the case when the
97 trampoline is not the first frame on the stack. We assume that
98 in the case where the PC is not at the start of the instruction
99 sequence, there will be a few trailing readable bytes on the
104 if (!safe_frame_unwind_memory (this_frame
, pc
, buf
, 2))
107 if (memcmp (buf
, linux_sigtramp_code
, 2) == 0)
113 if (!safe_frame_unwind_memory (this_frame
, pc
, buf
, 4))
116 if (memcmp (buf
, linux_sigtramp_code
, 4) != 0)
122 /* This function does the same for RT signals. Here the instruction
124 ldi r7, #__NR_rt_sigreturn
126 or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00.
128 The effect is to call the system call rt_sigreturn. */
130 static const gdb_byte linux_rt_sigtramp_code
[] = {
131 0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00,
134 /* If PC is in a RT sigtramp routine, return the address of the start
135 of the routine. Otherwise, return 0. */
138 m32r_linux_rt_sigtramp_start (CORE_ADDR pc
, struct frame_info
*this_frame
)
142 /* We only recognize a signal trampoline if PC is at the start of
143 one of the instructions. We optimize for finding the PC at the
144 start of the instruction sequence, as will be the case when the
145 trampoline is not the first frame on the stack. We assume that
146 in the case where the PC is not at the start of the instruction
147 sequence, there will be a few trailing readable bytes on the
153 if (!safe_frame_unwind_memory (this_frame
, pc
, buf
, 4))
156 if (memcmp (buf
, linux_rt_sigtramp_code
, 4) == 0)
158 if (!safe_frame_unwind_memory (this_frame
, pc
+ 4, buf
, 4))
161 if (memcmp (buf
, linux_rt_sigtramp_code
+ 4, 4) == 0)
164 else if (memcmp (buf
, linux_rt_sigtramp_code
+ 4, 4) == 0)
166 if (!safe_frame_unwind_memory (this_frame
, pc
- 4, buf
, 4))
169 if (memcmp (buf
, linux_rt_sigtramp_code
, 4) == 0)
177 m32r_linux_pc_in_sigtramp (CORE_ADDR pc
, char *name
,
178 struct frame_info
*this_frame
)
180 /* If we have NAME, we can optimize the search. The trampolines are
181 named __restore and __restore_rt. However, they aren't dynamically
182 exported from the shared C library, so the trampoline may appear to
183 be part of the preceding function. This should always be sigaction,
184 __sigaction, or __libc_sigaction (all aliases to the same function). */
185 if (name
== NULL
|| strstr (name
, "sigaction") != NULL
)
186 return (m32r_linux_sigtramp_start (pc
, this_frame
) != 0
187 || m32r_linux_rt_sigtramp_start (pc
, this_frame
) != 0);
189 return (strcmp ("__restore", name
) == 0
190 || strcmp ("__restore_rt", name
) == 0);
193 /* From <asm/sigcontext.h>. */
194 static int m32r_linux_sc_reg_offset
[] = {
221 struct m32r_frame_cache
224 struct trad_frame_saved_reg
*saved_regs
;
227 static struct m32r_frame_cache
*
228 m32r_linux_sigtramp_frame_cache (struct frame_info
*this_frame
,
231 struct m32r_frame_cache
*cache
;
232 CORE_ADDR sigcontext_addr
, addr
;
235 if ((*this_cache
) != NULL
)
236 return (*this_cache
);
237 cache
= FRAME_OBSTACK_ZALLOC (struct m32r_frame_cache
);
238 (*this_cache
) = cache
;
239 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
241 cache
->base
= get_frame_register_unsigned (this_frame
, M32R_SP_REGNUM
);
242 sigcontext_addr
= cache
->base
+ 4;
244 cache
->pc
= get_frame_pc (this_frame
);
245 addr
= m32r_linux_sigtramp_start (cache
->pc
, this_frame
);
248 /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR
250 addr
= m32r_linux_rt_sigtramp_start (cache
->pc
, this_frame
);
252 sigcontext_addr
+= 128;
254 addr
= get_frame_func (this_frame
);
258 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
260 for (regnum
= 0; regnum
< sizeof (m32r_linux_sc_reg_offset
) / 4; regnum
++)
262 if (m32r_linux_sc_reg_offset
[regnum
] >= 0)
263 cache
->saved_regs
[regnum
].addr
=
264 sigcontext_addr
+ m32r_linux_sc_reg_offset
[regnum
];
271 m32r_linux_sigtramp_frame_this_id (struct frame_info
*this_frame
,
273 struct frame_id
*this_id
)
275 struct m32r_frame_cache
*cache
=
276 m32r_linux_sigtramp_frame_cache (this_frame
, this_cache
);
278 (*this_id
) = frame_id_build (cache
->base
, cache
->pc
);
281 static struct value
*
282 m32r_linux_sigtramp_frame_prev_register (struct frame_info
*this_frame
,
283 void **this_cache
, int regnum
)
285 struct m32r_frame_cache
*cache
=
286 m32r_linux_sigtramp_frame_cache (this_frame
, this_cache
);
288 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
, regnum
);
292 m32r_linux_sigtramp_frame_sniffer (const struct frame_unwind
*self
,
293 struct frame_info
*this_frame
,
296 CORE_ADDR pc
= get_frame_pc (this_frame
);
299 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
300 if (m32r_linux_pc_in_sigtramp (pc
, name
, this_frame
))
306 static const struct frame_unwind m32r_linux_sigtramp_frame_unwind
= {
308 default_frame_unwind_stop_reason
,
309 m32r_linux_sigtramp_frame_this_id
,
310 m32r_linux_sigtramp_frame_prev_register
,
312 m32r_linux_sigtramp_frame_sniffer
315 /* Mapping between the registers in `struct pt_regs'
316 format and GDB's register array layout. */
318 static int m32r_pt_regs_offset
[] = {
345 #define PSW_OFFSET (4 * 19)
346 #define BBPSW_OFFSET (4 * 21)
347 #define SPU_OFFSET (4 * 23)
348 #define SPI_OFFSET (4 * 26)
351 m32r_linux_supply_gregset (const struct regset
*regset
,
352 struct regcache
*regcache
, int regnum
,
353 const void *gregs
, size_t size
)
355 const char *regs
= gregs
;
356 unsigned long psw
, bbpsw
;
359 psw
= *((unsigned long *) (regs
+ PSW_OFFSET
));
360 bbpsw
= *((unsigned long *) (regs
+ BBPSW_OFFSET
));
362 for (i
= 0; i
< sizeof (m32r_pt_regs_offset
) / 4; i
++)
364 if (regnum
!= -1 && regnum
!= i
)
370 *((unsigned long *) (regs
+ m32r_pt_regs_offset
[i
])) =
371 ((0x00c1 & bbpsw
) << 8) | ((0xc100 & psw
) >> 8);
374 *((unsigned long *) (regs
+ m32r_pt_regs_offset
[i
])) =
379 *((unsigned long *) (regs
+ m32r_pt_regs_offset
[i
])) =
380 *((unsigned long *) (regs
+ SPU_OFFSET
));
382 *((unsigned long *) (regs
+ m32r_pt_regs_offset
[i
])) =
383 *((unsigned long *) (regs
+ SPI_OFFSET
));
387 regcache_raw_supply (regcache
, i
,
388 regs
+ m32r_pt_regs_offset
[i
]);
392 static struct regset m32r_linux_gregset
= {
393 NULL
, m32r_linux_supply_gregset
396 static const struct regset
*
397 m32r_linux_regset_from_core_section (struct gdbarch
*core_arch
,
398 const char *sect_name
, size_t sect_size
)
400 struct gdbarch_tdep
*tdep
= gdbarch_tdep (core_arch
);
401 if (strcmp (sect_name
, ".reg") == 0)
402 return &m32r_linux_gregset
;
407 m32r_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
409 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
411 linux_init_abi (info
, gdbarch
);
413 /* Since EVB register is not available for native debug, we reduce
414 the number of registers. */
415 set_gdbarch_num_regs (gdbarch
, M32R_NUM_REGS
- 1);
417 frame_unwind_append_unwinder (gdbarch
, &m32r_linux_sigtramp_frame_unwind
);
419 /* GNU/Linux uses SVR4-style shared libraries. */
420 set_gdbarch_skip_trampoline_code (gdbarch
, find_solib_trampoline_target
);
421 set_solib_svr4_fetch_link_map_offsets
422 (gdbarch
, svr4_ilp32_fetch_link_map_offsets
);
424 /* Core file support. */
425 set_gdbarch_regset_from_core_section
426 (gdbarch
, m32r_linux_regset_from_core_section
);
428 /* Enable TLS support. */
429 set_gdbarch_fetch_tls_load_module_address (gdbarch
,
430 svr4_fetch_objfile_link_map
);
433 /* Provide a prototype to silence -Wmissing-prototypes. */
434 extern void _initialize_m32r_linux_tdep (void);
437 _initialize_m32r_linux_tdep (void)
439 gdbarch_register_osabi (bfd_arch_m32r
, 0, GDB_OSABI_LINUX
,
440 m32r_linux_init_abi
);