1 /* Target-dependent code for GNU/Linux i386.
3 Copyright 2000, 2001, 2002, 2003, 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
29 #include "reggroups.h"
31 #include "gdb_string.h"
33 #include "i386-tdep.h"
34 #include "i386-linux-tdep.h"
35 #include "glibc-tdep.h"
36 #include "solib-svr4.h"
38 /* Return the name of register REG. */
41 i386_linux_register_name (int reg
)
43 /* Deal with the extra "orig_eax" pseudo register. */
44 if (reg
== I386_LINUX_ORIG_EAX_REGNUM
)
47 return i386_register_name (reg
);
50 /* Return non-zero, when the register is in the corresponding register
51 group. Put the LINUX_ORIG_EAX register in the system group. */
53 i386_linux_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
54 struct reggroup
*group
)
56 if (regnum
== I386_LINUX_ORIG_EAX_REGNUM
)
57 return (group
== system_reggroup
58 || group
== save_reggroup
59 || group
== restore_reggroup
);
60 return i386_register_reggroup_p (gdbarch
, regnum
, group
);
64 /* Recognizing signal handler frames. */
66 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
67 "realtime" (RT) signals. The RT signals can provide additional
68 information to the signal handler if the SA_SIGINFO flag is set
69 when establishing a signal handler using `sigaction'. It is not
70 unlikely that future versions of GNU/Linux will support SA_SIGINFO
71 for normal signals too. */
73 /* When the i386 Linux kernel calls a signal handler and the
74 SA_RESTORER flag isn't set, the return address points to a bit of
75 code on the stack. This function returns whether the PC appears to
76 be within this bit of code.
78 The instruction sequence for normal signals is
82 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
84 Checking for the code sequence should be somewhat reliable, because
85 the effect is to call the system call sigreturn. This is unlikely
86 to occur anywhere other than in a signal trampoline.
88 It kind of sucks that we have to read memory from the process in
89 order to identify a signal trampoline, but there doesn't seem to be
90 any other way. Therefore we only do the memory reads if no
91 function name could be identified, which should be the case since
92 the code is on the stack.
94 Detection of signal trampolines for handlers that set the
95 SA_RESTORER flag is in general not possible. Unfortunately this is
96 what the GNU C Library has been doing for quite some time now.
97 However, as of version 2.1.2, the GNU C Library uses signal
98 trampolines (named __restore and __restore_rt) that are identical
99 to the ones used by the kernel. Therefore, these trampolines are
102 #define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
103 #define LINUX_SIGTRAMP_OFFSET0 0
104 #define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
105 #define LINUX_SIGTRAMP_OFFSET1 1
106 #define LINUX_SIGTRAMP_INSN2 0xcd /* int */
107 #define LINUX_SIGTRAMP_OFFSET2 6
109 static const unsigned char linux_sigtramp_code
[] =
111 LINUX_SIGTRAMP_INSN0
, /* pop %eax */
112 LINUX_SIGTRAMP_INSN1
, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
113 LINUX_SIGTRAMP_INSN2
, 0x80 /* int $0x80 */
116 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
118 /* If NEXT_FRAME unwinds into a sigtramp routine, return the address
119 of the start of the routine. Otherwise, return 0. */
122 i386_linux_sigtramp_start (struct frame_info
*next_frame
)
124 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
125 unsigned char buf
[LINUX_SIGTRAMP_LEN
];
127 /* We only recognize a signal trampoline if PC is at the start of
128 one of the three instructions. We optimize for finding the PC at
129 the start, as will be the case when the trampoline is not the
130 first frame on the stack. We assume that in the case where the
131 PC is not at the start of the instruction sequence, there will be
132 a few trailing readable bytes on the stack. */
134 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, LINUX_SIGTRAMP_LEN
))
137 if (buf
[0] != LINUX_SIGTRAMP_INSN0
)
143 case LINUX_SIGTRAMP_INSN1
:
144 adjust
= LINUX_SIGTRAMP_OFFSET1
;
146 case LINUX_SIGTRAMP_INSN2
:
147 adjust
= LINUX_SIGTRAMP_OFFSET2
;
155 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, LINUX_SIGTRAMP_LEN
))
159 if (memcmp (buf
, linux_sigtramp_code
, LINUX_SIGTRAMP_LEN
) != 0)
165 /* This function does the same for RT signals. Here the instruction
169 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
171 The effect is to call the system call rt_sigreturn. */
173 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
174 #define LINUX_RT_SIGTRAMP_OFFSET0 0
175 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
176 #define LINUX_RT_SIGTRAMP_OFFSET1 5
178 static const unsigned char linux_rt_sigtramp_code
[] =
180 LINUX_RT_SIGTRAMP_INSN0
, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
181 LINUX_RT_SIGTRAMP_INSN1
, 0x80 /* int $0x80 */
184 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
186 /* If NEXT_FRAME unwinds into an RT sigtramp routine, return the
187 address of the start of the routine. Otherwise, return 0. */
190 i386_linux_rt_sigtramp_start (struct frame_info
*next_frame
)
192 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
193 unsigned char buf
[LINUX_RT_SIGTRAMP_LEN
];
195 /* We only recognize a signal trampoline if PC is at the start of
196 one of the two instructions. We optimize for finding the PC at
197 the start, as will be the case when the trampoline is not the
198 first frame on the stack. We assume that in the case where the
199 PC is not at the start of the instruction sequence, there will be
200 a few trailing readable bytes on the stack. */
202 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, LINUX_RT_SIGTRAMP_LEN
))
205 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN0
)
207 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN1
)
210 pc
-= LINUX_RT_SIGTRAMP_OFFSET1
;
212 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
,
213 LINUX_RT_SIGTRAMP_LEN
))
217 if (memcmp (buf
, linux_rt_sigtramp_code
, LINUX_RT_SIGTRAMP_LEN
) != 0)
223 /* Return whether the frame preceding NEXT_FRAME corresponds to a
224 GNU/Linux sigtramp routine. */
227 i386_linux_sigtramp_p (struct frame_info
*next_frame
)
229 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
232 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
234 /* If we have NAME, we can optimize the search. The trampolines are
235 named __restore and __restore_rt. However, they aren't dynamically
236 exported from the shared C library, so the trampoline may appear to
237 be part of the preceding function. This should always be sigaction,
238 __sigaction, or __libc_sigaction (all aliases to the same function). */
239 if (name
== NULL
|| strstr (name
, "sigaction") != NULL
)
240 return (i386_linux_sigtramp_start (next_frame
) != 0
241 || i386_linux_rt_sigtramp_start (next_frame
) != 0);
243 return (strcmp ("__restore", name
) == 0
244 || strcmp ("__restore_rt", name
) == 0);
247 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
248 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
250 /* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
251 routine, return the address of the associated sigcontext structure. */
254 i386_linux_sigcontext_addr (struct frame_info
*next_frame
)
260 frame_unwind_register (next_frame
, I386_ESP_REGNUM
, buf
);
261 sp
= extract_unsigned_integer (buf
, 4);
263 pc
= i386_linux_sigtramp_start (next_frame
);
266 /* The sigcontext structure lives on the stack, right after
267 the signum argument. We determine the address of the
268 sigcontext structure by looking at the frame's stack
269 pointer. Keep in mind that the first instruction of the
270 sigtramp code is "pop %eax". If the PC is after this
271 instruction, adjust the returned value accordingly. */
272 if (pc
== frame_pc_unwind (next_frame
))
277 pc
= i386_linux_rt_sigtramp_start (next_frame
);
280 CORE_ADDR ucontext_addr
;
282 /* The sigcontext structure is part of the user context. A
283 pointer to the user context is passed as the third argument
284 to the signal handler. */
285 read_memory (sp
+ 8, buf
, 4);
286 ucontext_addr
= extract_unsigned_integer (buf
, 4);
287 return ucontext_addr
+ I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET
;
290 error ("Couldn't recognize signal trampoline.");
294 /* Set the program counter for process PTID to PC. */
297 i386_linux_write_pc (CORE_ADDR pc
, ptid_t ptid
)
299 write_register_pid (I386_EIP_REGNUM
, pc
, ptid
);
301 /* We must be careful with modifying the program counter. If we
302 just interrupted a system call, the kernel might try to restart
303 it when we resume the inferior. On restarting the system call,
304 the kernel will try backing up the program counter even though it
305 no longer points at the system call. This typically results in a
306 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
307 "orig_eax" pseudo-register.
309 Note that "orig_eax" is saved when setting up a dummy call frame.
310 This means that it is properly restored when that frame is
311 popped, and that the interrupted system call will be restarted
312 when we resume the inferior on return from a function call from
313 within GDB. In all other cases the system call will not be
315 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM
, -1, ptid
);
319 /* The register sets used in GNU/Linux ELF core-dumps are identical to
320 the register sets in `struct user' that are used for a.out
321 core-dumps. These are also used by ptrace(2). The corresponding
322 types are `elf_gregset_t' for the general-purpose registers (with
323 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
324 for the floating-point registers.
326 Those types used to be available under the names `gregset_t' and
327 `fpregset_t' too, and GDB used those names in the past. But those
328 names are now used for the register sets used in the `mcontext_t'
329 type, which have a different size and layout. */
331 /* Mapping between the general-purpose registers in `struct user'
332 format and GDB's register cache layout. */
334 /* From <sys/reg.h>. */
335 static int i386_linux_gregset_reg_offset
[] =
346 14 * 4, /* %eflags */
353 -1, -1, -1, -1, -1, -1, -1, -1,
354 -1, -1, -1, -1, -1, -1, -1, -1,
355 -1, -1, -1, -1, -1, -1, -1, -1,
357 11 * 4 /* "orig_eax" */
360 /* Mapping between the general-purpose registers in `struct
361 sigcontext' format and GDB's register cache layout. */
363 /* From <asm/sigcontext.h>. */
364 static int i386_linux_sc_reg_offset
[] =
375 16 * 4, /* %eflags */
385 i386_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
387 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
389 /* GNU/Linux uses ELF. */
390 i386_elf_init_abi (info
, gdbarch
);
392 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
393 to adjust a few things. */
395 set_gdbarch_write_pc (gdbarch
, i386_linux_write_pc
);
396 set_gdbarch_num_regs (gdbarch
, I386_LINUX_NUM_REGS
);
397 set_gdbarch_register_name (gdbarch
, i386_linux_register_name
);
398 set_gdbarch_register_reggroup_p (gdbarch
, i386_linux_register_reggroup_p
);
400 tdep
->gregset_reg_offset
= i386_linux_gregset_reg_offset
;
401 tdep
->gregset_num_regs
= ARRAY_SIZE (i386_linux_gregset_reg_offset
);
402 tdep
->sizeof_gregset
= 17 * 4;
404 tdep
->jb_pc_offset
= 20; /* From <bits/setjmp.h>. */
406 tdep
->sigtramp_p
= i386_linux_sigtramp_p
;
407 tdep
->sigcontext_addr
= i386_linux_sigcontext_addr
;
408 tdep
->sc_reg_offset
= i386_linux_sc_reg_offset
;
409 tdep
->sc_num_regs
= ARRAY_SIZE (i386_linux_sc_reg_offset
);
411 /* GNU/Linux uses SVR4-style shared libraries. */
412 set_solib_svr4_fetch_link_map_offsets
413 (gdbarch
, svr4_ilp32_fetch_link_map_offsets
);
415 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
416 set_gdbarch_skip_solib_resolver (gdbarch
, glibc_skip_solib_resolver
);
419 /* Provide a prototype to silence -Wmissing-prototypes. */
420 extern void _initialize_i386_linux_tdep (void);
423 _initialize_i386_linux_tdep (void)
425 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_LINUX
,
426 i386_linux_init_abi
);