Commit | Line | Data |
---|---|---|
9b32d526 KI |
1 | /* Target-dependent code for GNU/Linux m32r. |
2 | ||
6aba47ca | 3 | Copyright (C) 2004, 2007 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 | |
9 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
18 | along with this program; if not, write to the Free Software | |
197e01b6 EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
9b32d526 KI |
21 | |
22 | #include "defs.h" | |
23 | #include "gdbcore.h" | |
24 | #include "frame.h" | |
25 | #include "value.h" | |
26 | #include "regcache.h" | |
27 | #include "inferior.h" | |
28 | #include "osabi.h" | |
29 | #include "reggroups.h" | |
9b098f24 | 30 | #include "regset.h" |
9b32d526 KI |
31 | |
32 | #include "gdb_string.h" | |
33 | ||
34 | #include "glibc-tdep.h" | |
35 | #include "solib-svr4.h" | |
982e9687 | 36 | #include "symtab.h" |
9b32d526 KI |
37 | |
38 | #include "trad-frame.h" | |
39 | #include "frame-unwind.h" | |
40 | ||
41 | #include "m32r-tdep.h" | |
42 | \f | |
43 | ||
44 | /* Recognizing signal handler frames. */ | |
45 | ||
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. */ | |
52 | ||
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. | |
57 | ||
58 | The instruction sequence for normal signals is | |
59 | ldi r7, #__NR_sigreturn | |
60 | trap #2 | |
61 | or 0x67 0x77 0x10 0xf2. | |
62 | ||
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. | |
66 | ||
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. | |
72 | ||
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 | |
79 | supported too. */ | |
80 | ||
16ac4ab5 | 81 | static const gdb_byte linux_sigtramp_code[] = { |
9b32d526 KI |
82 | 0x67, 0x77, 0x10, 0xf2, |
83 | }; | |
84 | ||
85 | /* If PC is in a sigtramp routine, return the address of the start of | |
86 | the routine. Otherwise, return 0. */ | |
87 | ||
88 | static CORE_ADDR | |
89 | m32r_linux_sigtramp_start (CORE_ADDR pc, struct frame_info *next_frame) | |
90 | { | |
16ac4ab5 | 91 | gdb_byte buf[4]; |
9b32d526 KI |
92 | |
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 | |
99 | stack. */ | |
100 | ||
101 | if (pc % 2 != 0) | |
102 | { | |
103 | if (!safe_frame_unwind_memory (next_frame, pc, buf, 2)) | |
104 | return 0; | |
105 | ||
106 | if (memcmp (buf, linux_sigtramp_code, 2) == 0) | |
107 | pc -= 2; | |
108 | else | |
109 | return 0; | |
110 | } | |
111 | ||
112 | if (!safe_frame_unwind_memory (next_frame, pc, buf, 4)) | |
113 | return 0; | |
114 | ||
115 | if (memcmp (buf, linux_sigtramp_code, 4) != 0) | |
116 | return 0; | |
117 | ||
118 | return pc; | |
119 | } | |
120 | ||
121 | /* This function does the same for RT signals. Here the instruction | |
122 | sequence is | |
123 | ldi r7, #__NR_rt_sigreturn | |
124 | trap #2 | |
125 | or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00. | |
126 | ||
127 | The effect is to call the system call rt_sigreturn. */ | |
128 | ||
16ac4ab5 | 129 | static const gdb_byte linux_rt_sigtramp_code[] = { |
9b32d526 KI |
130 | 0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00, |
131 | }; | |
132 | ||
133 | /* If PC is in a RT sigtramp routine, return the address of the start | |
134 | of the routine. Otherwise, return 0. */ | |
135 | ||
136 | static CORE_ADDR | |
137 | m32r_linux_rt_sigtramp_start (CORE_ADDR pc, struct frame_info *next_frame) | |
138 | { | |
16ac4ab5 | 139 | gdb_byte buf[4]; |
9b32d526 KI |
140 | |
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 | |
147 | stack. */ | |
148 | ||
149 | if (pc % 2 != 0) | |
150 | return 0; | |
151 | ||
152 | if (!safe_frame_unwind_memory (next_frame, pc, buf, 4)) | |
153 | return 0; | |
154 | ||
155 | if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0) | |
156 | { | |
157 | if (!safe_frame_unwind_memory (next_frame, pc + 4, buf, 4)) | |
158 | return 0; | |
159 | ||
160 | if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0) | |
161 | return pc; | |
162 | } | |
163 | else if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0) | |
164 | { | |
165 | if (!safe_frame_unwind_memory (next_frame, pc - 4, buf, 4)) | |
166 | return 0; | |
167 | ||
168 | if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0) | |
169 | return pc - 4; | |
170 | } | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | static int | |
176 | m32r_linux_pc_in_sigtramp (CORE_ADDR pc, char *name, | |
177 | struct frame_info *next_frame) | |
178 | { | |
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, next_frame) != 0 | |
186 | || m32r_linux_rt_sigtramp_start (pc, next_frame) != 0); | |
187 | ||
188 | return (strcmp ("__restore", name) == 0 | |
189 | || strcmp ("__restore_rt", name) == 0); | |
190 | } | |
191 | ||
192 | /* From <asm/sigcontext.h>. */ | |
193 | static int m32r_linux_sc_reg_offset[] = { | |
194 | 4 * 4, /* r0 */ | |
195 | 5 * 4, /* r1 */ | |
196 | 6 * 4, /* r2 */ | |
197 | 7 * 4, /* r3 */ | |
198 | 0 * 4, /* r4 */ | |
199 | 1 * 4, /* r5 */ | |
200 | 2 * 4, /* r6 */ | |
201 | 8 * 4, /* r7 */ | |
202 | 9 * 4, /* r8 */ | |
203 | 10 * 4, /* r9 */ | |
204 | 11 * 4, /* r10 */ | |
205 | 12 * 4, /* r11 */ | |
206 | 13 * 4, /* r12 */ | |
207 | 21 * 4, /* fp */ | |
208 | 22 * 4, /* lr */ | |
209 | -1 * 4, /* sp */ | |
210 | 16 * 4, /* psw */ | |
211 | -1 * 4, /* cbr */ | |
212 | 23 * 4, /* spi */ | |
213 | 20 * 4, /* spu */ | |
214 | 19 * 4, /* bpc */ | |
215 | 17 * 4, /* pc */ | |
216 | 15 * 4, /* accl */ | |
217 | 14 * 4 /* acch */ | |
218 | }; | |
219 | ||
220 | struct m32r_frame_cache | |
221 | { | |
222 | CORE_ADDR base, pc; | |
223 | struct trad_frame_saved_reg *saved_regs; | |
224 | }; | |
225 | ||
226 | static struct m32r_frame_cache * | |
227 | m32r_linux_sigtramp_frame_cache (struct frame_info *next_frame, | |
228 | void **this_cache) | |
229 | { | |
230 | struct m32r_frame_cache *cache; | |
231 | CORE_ADDR sigcontext_addr, addr; | |
232 | int regnum; | |
233 | ||
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 (next_frame); | |
239 | ||
240 | cache->base = frame_unwind_register_unsigned (next_frame, M32R_SP_REGNUM); | |
241 | sigcontext_addr = cache->base + 4; | |
242 | ||
243 | cache->pc = frame_pc_unwind (next_frame); | |
244 | addr = m32r_linux_sigtramp_start (cache->pc, next_frame); | |
245 | if (addr == 0) | |
246 | { | |
247 | /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR | |
248 | accordingly. */ | |
249 | addr = m32r_linux_rt_sigtramp_start (cache->pc, next_frame); | |
250 | if (addr) | |
251 | sigcontext_addr += 128; | |
252 | else | |
93d42b30 | 253 | addr = frame_func_unwind (next_frame, NORMAL_FRAME); |
9b32d526 KI |
254 | } |
255 | cache->pc = addr; | |
256 | ||
257 | cache->saved_regs = trad_frame_alloc_saved_regs (next_frame); | |
258 | ||
259 | for (regnum = 0; regnum < sizeof (m32r_linux_sc_reg_offset) / 4; regnum++) | |
260 | { | |
261 | if (m32r_linux_sc_reg_offset[regnum] >= 0) | |
262 | cache->saved_regs[regnum].addr = | |
263 | sigcontext_addr + m32r_linux_sc_reg_offset[regnum]; | |
264 | } | |
265 | ||
266 | return cache; | |
267 | } | |
268 | ||
269 | static void | |
270 | m32r_linux_sigtramp_frame_this_id (struct frame_info *next_frame, | |
271 | void **this_cache, | |
272 | struct frame_id *this_id) | |
273 | { | |
274 | struct m32r_frame_cache *cache = | |
275 | m32r_linux_sigtramp_frame_cache (next_frame, this_cache); | |
276 | ||
277 | (*this_id) = frame_id_build (cache->base, cache->pc); | |
278 | } | |
279 | ||
280 | static void | |
281 | m32r_linux_sigtramp_frame_prev_register (struct frame_info *next_frame, | |
282 | void **this_cache, | |
283 | int regnum, int *optimizedp, | |
284 | enum lval_type *lvalp, | |
285 | CORE_ADDR *addrp, | |
16ac4ab5 | 286 | int *realnump, gdb_byte *valuep) |
9b32d526 KI |
287 | { |
288 | struct m32r_frame_cache *cache = | |
289 | m32r_linux_sigtramp_frame_cache (next_frame, this_cache); | |
290 | ||
291 | trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum, | |
292 | optimizedp, lvalp, addrp, realnump, valuep); | |
293 | } | |
294 | ||
295 | static const struct frame_unwind m32r_linux_sigtramp_frame_unwind = { | |
296 | SIGTRAMP_FRAME, | |
297 | m32r_linux_sigtramp_frame_this_id, | |
298 | m32r_linux_sigtramp_frame_prev_register | |
299 | }; | |
300 | ||
301 | static const struct frame_unwind * | |
302 | m32r_linux_sigtramp_frame_sniffer (struct frame_info *next_frame) | |
303 | { | |
304 | CORE_ADDR pc = frame_pc_unwind (next_frame); | |
305 | char *name; | |
306 | ||
307 | find_pc_partial_function (pc, &name, NULL, NULL); | |
308 | if (m32r_linux_pc_in_sigtramp (pc, name, next_frame)) | |
309 | return &m32r_linux_sigtramp_frame_unwind; | |
310 | ||
311 | return NULL; | |
312 | } | |
313 | ||
9b098f24 KI |
314 | /* Mapping between the registers in `struct pt_regs' |
315 | format and GDB's register array layout. */ | |
316 | ||
317 | static 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 | ||
349 | static void | |
350 | m32r_linux_supply_gregset (const struct regset *regset, | |
351 | struct regcache *regcache, int regnum, | |
352 | const void *gregs, size_t size) | |
353 | { | |
354 | const char *regs = gregs; | |
355 | unsigned long psw, bbpsw; | |
356 | int i; | |
357 | ||
358 | psw = *((unsigned long *) (regs + PSW_OFFSET)); | |
359 | bbpsw = *((unsigned long *) (regs + BBPSW_OFFSET)); | |
360 | ||
361 | for (i = 0; i < sizeof (m32r_pt_regs_offset) / 4; i++) | |
362 | { | |
363 | if (regnum != -1 && regnum != i) | |
364 | continue; | |
365 | ||
366 | switch (i) | |
367 | { | |
368 | case PSW_REGNUM: | |
369 | *((unsigned long *) (regs + m32r_pt_regs_offset[i])) = | |
370 | ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8); | |
371 | break; | |
372 | case CBR_REGNUM: | |
373 | *((unsigned long *) (regs + m32r_pt_regs_offset[i])) = | |
374 | ((psw >> 8) & 1); | |
375 | break; | |
376 | case M32R_SP_REGNUM: | |
377 | if (psw & 0x8000) | |
378 | *((unsigned long *) (regs + m32r_pt_regs_offset[i])) = | |
379 | *((unsigned long *) (regs + SPU_OFFSET)); | |
380 | else | |
381 | *((unsigned long *) (regs + m32r_pt_regs_offset[i])) = | |
382 | *((unsigned long *) (regs + SPI_OFFSET)); | |
383 | break; | |
384 | } | |
385 | ||
9c9acae0 | 386 | regcache_raw_supply (regcache, i, |
9b098f24 KI |
387 | regs + m32r_pt_regs_offset[i]); |
388 | } | |
389 | } | |
390 | ||
391 | static struct regset m32r_linux_gregset = { | |
392 | NULL, m32r_linux_supply_gregset | |
393 | }; | |
394 | ||
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) | |
398 | { | |
399 | struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch); | |
400 | if (strcmp (sect_name, ".reg") == 0) | |
401 | return &m32r_linux_gregset; | |
402 | return NULL; | |
403 | } | |
404 | ||
9b32d526 KI |
405 | static void |
406 | m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) | |
407 | { | |
408 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
409 | ||
410 | /* Since EVB register is not available for native debug, we reduce | |
411 | the number of registers. */ | |
412 | set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS - 1); | |
413 | ||
414 | frame_unwind_append_sniffer (gdbarch, m32r_linux_sigtramp_frame_sniffer); | |
415 | ||
416 | /* GNU/Linux uses SVR4-style shared libraries. */ | |
982e9687 | 417 | set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target); |
9b32d526 KI |
418 | set_solib_svr4_fetch_link_map_offsets |
419 | (gdbarch, svr4_ilp32_fetch_link_map_offsets); | |
9b098f24 KI |
420 | |
421 | /* Core file support. */ | |
422 | set_gdbarch_regset_from_core_section | |
423 | (gdbarch, m32r_linux_regset_from_core_section); | |
b2756930 KB |
424 | |
425 | /* Enable TLS support. */ | |
426 | set_gdbarch_fetch_tls_load_module_address (gdbarch, | |
427 | svr4_fetch_objfile_link_map); | |
9b32d526 KI |
428 | } |
429 | ||
430 | /* Provide a prototype to silence -Wmissing-prototypes. */ | |
431 | extern void _initialize_m32r_linux_tdep (void); | |
432 | ||
433 | void | |
434 | _initialize_m32r_linux_tdep (void) | |
435 | { | |
436 | gdbarch_register_osabi (bfd_arch_m32r, 0, GDB_OSABI_LINUX, | |
437 | m32r_linux_init_abi); | |
438 | } |