1 /* Target-dependent code for GNU/Linux i386.
3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
30 #include "reggroups.h"
31 #include "dwarf2-frame.h"
32 #include "gdb_string.h"
34 #include "i386-tdep.h"
35 #include "i386-linux-tdep.h"
36 #include "glibc-tdep.h"
37 #include "solib-svr4.h"
40 /* Return the name of register REG. */
43 i386_linux_register_name (int reg)
45 /* Deal with the extra "orig_eax" pseudo register. */
46 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
49 return i386_register_name (reg);
52 /* Return non-zero, when the register is in the corresponding register
53 group. Put the LINUX_ORIG_EAX register in the system group. */
55 i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
56 struct reggroup *group)
58 if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
59 return (group == system_reggroup
60 || group == save_reggroup
61 || group == restore_reggroup);
62 return i386_register_reggroup_p (gdbarch, regnum, group);
66 /* Recognizing signal handler frames. */
68 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
69 "realtime" (RT) signals. The RT signals can provide additional
70 information to the signal handler if the SA_SIGINFO flag is set
71 when establishing a signal handler using `sigaction'. It is not
72 unlikely that future versions of GNU/Linux will support SA_SIGINFO
73 for normal signals too. */
75 /* When the i386 Linux kernel calls a signal handler and the
76 SA_RESTORER flag isn't set, the return address points to a bit of
77 code on the stack. This function returns whether the PC appears to
78 be within this bit of code.
80 The instruction sequence for normal signals is
84 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
86 Checking for the code sequence should be somewhat reliable, because
87 the effect is to call the system call sigreturn. This is unlikely
88 to occur anywhere other than in a signal trampoline.
90 It kind of sucks that we have to read memory from the process in
91 order to identify a signal trampoline, but there doesn't seem to be
92 any other way. Therefore we only do the memory reads if no
93 function name could be identified, which should be the case since
94 the code is on the stack.
96 Detection of signal trampolines for handlers that set the
97 SA_RESTORER flag is in general not possible. Unfortunately this is
98 what the GNU C Library has been doing for quite some time now.
99 However, as of version 2.1.2, the GNU C Library uses signal
100 trampolines (named __restore and __restore_rt) that are identical
101 to the ones used by the kernel. Therefore, these trampolines are
104 #define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
105 #define LINUX_SIGTRAMP_OFFSET0 0
106 #define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
107 #define LINUX_SIGTRAMP_OFFSET1 1
108 #define LINUX_SIGTRAMP_INSN2 0xcd /* int */
109 #define LINUX_SIGTRAMP_OFFSET2 6
111 static const gdb_byte linux_sigtramp_code[] =
113 LINUX_SIGTRAMP_INSN0, /* pop %eax */
114 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
115 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
118 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
120 /* If NEXT_FRAME unwinds into a sigtramp routine, return the address
121 of the start of the routine. Otherwise, return 0. */
124 i386_linux_sigtramp_start (struct frame_info *next_frame)
126 CORE_ADDR pc = frame_pc_unwind (next_frame);
127 gdb_byte buf[LINUX_SIGTRAMP_LEN];
129 /* We only recognize a signal trampoline if PC is at the start of
130 one of the three instructions. We optimize for finding the PC at
131 the start, as will be the case when the trampoline is not the
132 first frame on the stack. We assume that in the case where the
133 PC is not at the start of the instruction sequence, there will be
134 a few trailing readable bytes on the stack. */
136 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_SIGTRAMP_LEN))
139 if (buf[0] != LINUX_SIGTRAMP_INSN0)
145 case LINUX_SIGTRAMP_INSN1:
146 adjust = LINUX_SIGTRAMP_OFFSET1;
148 case LINUX_SIGTRAMP_INSN2:
149 adjust = LINUX_SIGTRAMP_OFFSET2;
157 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_SIGTRAMP_LEN))
161 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
167 /* This function does the same for RT signals. Here the instruction
171 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
173 The effect is to call the system call rt_sigreturn. */
175 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
176 #define LINUX_RT_SIGTRAMP_OFFSET0 0
177 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
178 #define LINUX_RT_SIGTRAMP_OFFSET1 5
180 static const gdb_byte linux_rt_sigtramp_code[] =
182 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
183 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
186 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
188 /* If NEXT_FRAME unwinds into an RT sigtramp routine, return the
189 address of the start of the routine. Otherwise, return 0. */
192 i386_linux_rt_sigtramp_start (struct frame_info *next_frame)
194 CORE_ADDR pc = frame_pc_unwind (next_frame);
195 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
197 /* We only recognize a signal trampoline if PC is at the start of
198 one of the two instructions. We optimize for finding the PC at
199 the start, as will be the case when the trampoline is not the
200 first frame on the stack. We assume that in the case where the
201 PC is not at the start of the instruction sequence, there will be
202 a few trailing readable bytes on the stack. */
204 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
207 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
209 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
212 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
214 if (!safe_frame_unwind_memory (next_frame, pc, buf,
215 LINUX_RT_SIGTRAMP_LEN))
219 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
225 /* Return whether the frame preceding NEXT_FRAME corresponds to a
226 GNU/Linux sigtramp routine. */
229 i386_linux_sigtramp_p (struct frame_info *next_frame)
231 CORE_ADDR pc = frame_pc_unwind (next_frame);
234 find_pc_partial_function (pc, &name, NULL, NULL);
236 /* If we have NAME, we can optimize the search. The trampolines are
237 named __restore and __restore_rt. However, they aren't dynamically
238 exported from the shared C library, so the trampoline may appear to
239 be part of the preceding function. This should always be sigaction,
240 __sigaction, or __libc_sigaction (all aliases to the same function). */
241 if (name == NULL || strstr (name, "sigaction") != NULL)
242 return (i386_linux_sigtramp_start (next_frame) != 0
243 || i386_linux_rt_sigtramp_start (next_frame) != 0);
245 return (strcmp ("__restore", name) == 0
246 || strcmp ("__restore_rt", name) == 0);
249 /* Return one if the unwound PC from NEXT_FRAME is in a signal trampoline
250 which may have DWARF-2 CFI. */
253 i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
254 struct frame_info *next_frame)
256 CORE_ADDR pc = frame_pc_unwind (next_frame);
259 find_pc_partial_function (pc, &name, NULL, NULL);
261 /* If a vsyscall DSO is in use, the signal trampolines may have these
263 if (name && (strcmp (name, "__kernel_sigreturn") == 0
264 || strcmp (name, "__kernel_rt_sigreturn") == 0))
270 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
271 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
273 /* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
274 routine, return the address of the associated sigcontext structure. */
277 i386_linux_sigcontext_addr (struct frame_info *next_frame)
283 frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
284 sp = extract_unsigned_integer (buf, 4);
286 pc = i386_linux_sigtramp_start (next_frame);
289 /* The sigcontext structure lives on the stack, right after
290 the signum argument. We determine the address of the
291 sigcontext structure by looking at the frame's stack
292 pointer. Keep in mind that the first instruction of the
293 sigtramp code is "pop %eax". If the PC is after this
294 instruction, adjust the returned value accordingly. */
295 if (pc == frame_pc_unwind (next_frame))
300 pc = i386_linux_rt_sigtramp_start (next_frame);
303 CORE_ADDR ucontext_addr;
305 /* The sigcontext structure is part of the user context. A
306 pointer to the user context is passed as the third argument
307 to the signal handler. */
308 read_memory (sp + 8, buf, 4);
309 ucontext_addr = extract_unsigned_integer (buf, 4);
310 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
313 error (_("Couldn't recognize signal trampoline."));
317 /* Set the program counter for process PTID to PC. */
320 i386_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
322 write_register_pid (I386_EIP_REGNUM, pc, ptid);
324 /* We must be careful with modifying the program counter. If we
325 just interrupted a system call, the kernel might try to restart
326 it when we resume the inferior. On restarting the system call,
327 the kernel will try backing up the program counter even though it
328 no longer points at the system call. This typically results in a
329 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
330 "orig_eax" pseudo-register.
332 Note that "orig_eax" is saved when setting up a dummy call frame.
333 This means that it is properly restored when that frame is
334 popped, and that the interrupted system call will be restarted
335 when we resume the inferior on return from a function call from
336 within GDB. In all other cases the system call will not be
338 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM, -1, ptid);
342 /* The register sets used in GNU/Linux ELF core-dumps are identical to
343 the register sets in `struct user' that are used for a.out
344 core-dumps. These are also used by ptrace(2). The corresponding
345 types are `elf_gregset_t' for the general-purpose registers (with
346 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
347 for the floating-point registers.
349 Those types used to be available under the names `gregset_t' and
350 `fpregset_t' too, and GDB used those names in the past. But those
351 names are now used for the register sets used in the `mcontext_t'
352 type, which have a different size and layout. */
354 /* Mapping between the general-purpose registers in `struct user'
355 format and GDB's register cache layout. */
357 /* From <sys/reg.h>. */
358 static int i386_linux_gregset_reg_offset[] =
369 14 * 4, /* %eflags */
376 -1, -1, -1, -1, -1, -1, -1, -1,
377 -1, -1, -1, -1, -1, -1, -1, -1,
378 -1, -1, -1, -1, -1, -1, -1, -1,
380 11 * 4 /* "orig_eax" */
383 /* Mapping between the general-purpose registers in `struct
384 sigcontext' format and GDB's register cache layout. */
386 /* From <asm/sigcontext.h>. */
387 static int i386_linux_sc_reg_offset[] =
398 16 * 4, /* %eflags */
408 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
410 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
412 /* GNU/Linux uses ELF. */
413 i386_elf_init_abi (info, gdbarch);
415 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
416 to adjust a few things. */
418 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
419 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
420 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
421 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
423 tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
424 tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
425 tdep->sizeof_gregset = 17 * 4;
427 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
429 tdep->sigtramp_p = i386_linux_sigtramp_p;
430 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
431 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
432 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
434 /* GNU/Linux uses SVR4-style shared libraries. */
435 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
436 set_solib_svr4_fetch_link_map_offsets
437 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
439 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
440 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
442 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
444 /* Enable TLS support. */
445 set_gdbarch_fetch_tls_load_module_address (gdbarch,
446 svr4_fetch_objfile_link_map);
449 /* Provide a prototype to silence -Wmissing-prototypes. */
450 extern void _initialize_i386_linux_tdep (void);
453 _initialize_i386_linux_tdep (void)
455 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
456 i386_linux_init_abi);