1 /* Target-dependent code for GNU/Linux i386.
3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
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"
29 #include "dwarf2-frame.h"
30 #include "gdb_string.h"
32 #include "i386-tdep.h"
33 #include "i386-linux-tdep.h"
34 #include "linux-tdep.h"
35 #include "glibc-tdep.h"
36 #include "solib-svr4.h"
38 #include "arch-utils.h"
41 /* Supported register note sections. */
42 static struct core_regset_section i386_linux_regset_sections[] =
50 /* Return the name of register REG. */
53 i386_linux_register_name (struct gdbarch *gdbarch, int reg)
55 /* Deal with the extra "orig_eax" pseudo register. */
56 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
59 return i386_register_name (gdbarch, reg);
62 /* Return non-zero, when the register is in the corresponding register
63 group. Put the LINUX_ORIG_EAX register in the system group. */
65 i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
66 struct reggroup *group)
68 if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
69 return (group == system_reggroup
70 || group == save_reggroup
71 || group == restore_reggroup);
72 return i386_register_reggroup_p (gdbarch, regnum, group);
76 /* Recognizing signal handler frames. */
78 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
79 "realtime" (RT) signals. The RT signals can provide additional
80 information to the signal handler if the SA_SIGINFO flag is set
81 when establishing a signal handler using `sigaction'. It is not
82 unlikely that future versions of GNU/Linux will support SA_SIGINFO
83 for normal signals too. */
85 /* When the i386 Linux kernel calls a signal handler and the
86 SA_RESTORER flag isn't set, the return address points to a bit of
87 code on the stack. This function returns whether the PC appears to
88 be within this bit of code.
90 The instruction sequence for normal signals is
94 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
96 Checking for the code sequence should be somewhat reliable, because
97 the effect is to call the system call sigreturn. This is unlikely
98 to occur anywhere other than in a signal trampoline.
100 It kind of sucks that we have to read memory from the process in
101 order to identify a signal trampoline, but there doesn't seem to be
102 any other way. Therefore we only do the memory reads if no
103 function name could be identified, which should be the case since
104 the code is on the stack.
106 Detection of signal trampolines for handlers that set the
107 SA_RESTORER flag is in general not possible. Unfortunately this is
108 what the GNU C Library has been doing for quite some time now.
109 However, as of version 2.1.2, the GNU C Library uses signal
110 trampolines (named __restore and __restore_rt) that are identical
111 to the ones used by the kernel. Therefore, these trampolines are
114 #define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
115 #define LINUX_SIGTRAMP_OFFSET0 0
116 #define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
117 #define LINUX_SIGTRAMP_OFFSET1 1
118 #define LINUX_SIGTRAMP_INSN2 0xcd /* int */
119 #define LINUX_SIGTRAMP_OFFSET2 6
121 static const gdb_byte linux_sigtramp_code[] =
123 LINUX_SIGTRAMP_INSN0, /* pop %eax */
124 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
125 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
128 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
130 /* If THIS_FRAME is a sigtramp routine, return the address of the
131 start of the routine. Otherwise, return 0. */
134 i386_linux_sigtramp_start (struct frame_info *this_frame)
136 CORE_ADDR pc = get_frame_pc (this_frame);
137 gdb_byte buf[LINUX_SIGTRAMP_LEN];
139 /* We only recognize a signal trampoline if PC is at the start of
140 one of the three instructions. We optimize for finding the PC at
141 the start, as will be the case when the trampoline is not the
142 first frame on the stack. We assume that in the case where the
143 PC is not at the start of the instruction sequence, there will be
144 a few trailing readable bytes on the stack. */
146 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
149 if (buf[0] != LINUX_SIGTRAMP_INSN0)
155 case LINUX_SIGTRAMP_INSN1:
156 adjust = LINUX_SIGTRAMP_OFFSET1;
158 case LINUX_SIGTRAMP_INSN2:
159 adjust = LINUX_SIGTRAMP_OFFSET2;
167 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
171 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
177 /* This function does the same for RT signals. Here the instruction
181 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
183 The effect is to call the system call rt_sigreturn. */
185 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
186 #define LINUX_RT_SIGTRAMP_OFFSET0 0
187 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
188 #define LINUX_RT_SIGTRAMP_OFFSET1 5
190 static const gdb_byte linux_rt_sigtramp_code[] =
192 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
193 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
196 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
198 /* If THIS_FRAME is an RT sigtramp routine, return the address of the
199 start of the routine. Otherwise, return 0. */
202 i386_linux_rt_sigtramp_start (struct frame_info *this_frame)
204 CORE_ADDR pc = get_frame_pc (this_frame);
205 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
207 /* We only recognize a signal trampoline if PC is at the start of
208 one of the two instructions. We optimize for finding the PC at
209 the start, as will be the case when the trampoline is not the
210 first frame on the stack. We assume that in the case where the
211 PC is not at the start of the instruction sequence, there will be
212 a few trailing readable bytes on the stack. */
214 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
217 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
219 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
222 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
224 if (!safe_frame_unwind_memory (this_frame, pc, buf,
225 LINUX_RT_SIGTRAMP_LEN))
229 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
235 /* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
239 i386_linux_sigtramp_p (struct frame_info *this_frame)
241 CORE_ADDR pc = get_frame_pc (this_frame);
244 find_pc_partial_function (pc, &name, NULL, NULL);
246 /* If we have NAME, we can optimize the search. The trampolines are
247 named __restore and __restore_rt. However, they aren't dynamically
248 exported from the shared C library, so the trampoline may appear to
249 be part of the preceding function. This should always be sigaction,
250 __sigaction, or __libc_sigaction (all aliases to the same function). */
251 if (name == NULL || strstr (name, "sigaction") != NULL)
252 return (i386_linux_sigtramp_start (this_frame) != 0
253 || i386_linux_rt_sigtramp_start (this_frame) != 0);
255 return (strcmp ("__restore", name) == 0
256 || strcmp ("__restore_rt", name) == 0);
259 /* Return one if the PC of THIS_FRAME is in a signal trampoline which
260 may have DWARF-2 CFI. */
263 i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
264 struct frame_info *this_frame)
266 CORE_ADDR pc = get_frame_pc (this_frame);
269 find_pc_partial_function (pc, &name, NULL, NULL);
271 /* If a vsyscall DSO is in use, the signal trampolines may have these
273 if (name && (strcmp (name, "__kernel_sigreturn") == 0
274 || strcmp (name, "__kernel_rt_sigreturn") == 0))
280 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
281 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
283 /* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
284 address of the associated sigcontext structure. */
287 i386_linux_sigcontext_addr (struct frame_info *this_frame)
293 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
294 sp = extract_unsigned_integer (buf, 4);
296 pc = i386_linux_sigtramp_start (this_frame);
299 /* The sigcontext structure lives on the stack, right after
300 the signum argument. We determine the address of the
301 sigcontext structure by looking at the frame's stack
302 pointer. Keep in mind that the first instruction of the
303 sigtramp code is "pop %eax". If the PC is after this
304 instruction, adjust the returned value accordingly. */
305 if (pc == get_frame_pc (this_frame))
310 pc = i386_linux_rt_sigtramp_start (this_frame);
313 CORE_ADDR ucontext_addr;
315 /* The sigcontext structure is part of the user context. A
316 pointer to the user context is passed as the third argument
317 to the signal handler. */
318 read_memory (sp + 8, buf, 4);
319 ucontext_addr = extract_unsigned_integer (buf, 4);
320 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
323 error (_("Couldn't recognize signal trampoline."));
327 /* Set the program counter for process PTID to PC. */
330 i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
332 regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);
334 /* We must be careful with modifying the program counter. If we
335 just interrupted a system call, the kernel might try to restart
336 it when we resume the inferior. On restarting the system call,
337 the kernel will try backing up the program counter even though it
338 no longer points at the system call. This typically results in a
339 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
340 "orig_eax" pseudo-register.
342 Note that "orig_eax" is saved when setting up a dummy call frame.
343 This means that it is properly restored when that frame is
344 popped, and that the interrupted system call will be restarted
345 when we resume the inferior on return from a function call from
346 within GDB. In all other cases the system call will not be
348 regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
352 /* The register sets used in GNU/Linux ELF core-dumps are identical to
353 the register sets in `struct user' that are used for a.out
354 core-dumps. These are also used by ptrace(2). The corresponding
355 types are `elf_gregset_t' for the general-purpose registers (with
356 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
357 for the floating-point registers.
359 Those types used to be available under the names `gregset_t' and
360 `fpregset_t' too, and GDB used those names in the past. But those
361 names are now used for the register sets used in the `mcontext_t'
362 type, which have a different size and layout. */
364 /* Mapping between the general-purpose registers in `struct user'
365 format and GDB's register cache layout. */
367 /* From <sys/reg.h>. */
368 static int i386_linux_gregset_reg_offset[] =
379 14 * 4, /* %eflags */
386 -1, -1, -1, -1, -1, -1, -1, -1,
387 -1, -1, -1, -1, -1, -1, -1, -1,
388 -1, -1, -1, -1, -1, -1, -1, -1,
390 11 * 4 /* "orig_eax" */
393 /* Mapping between the general-purpose registers in `struct
394 sigcontext' format and GDB's register cache layout. */
396 /* From <asm/sigcontext.h>. */
397 static int i386_linux_sc_reg_offset[] =
408 16 * 4, /* %eflags */
418 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
420 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
422 /* GNU/Linux uses ELF. */
423 i386_elf_init_abi (info, gdbarch);
425 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
426 to adjust a few things. */
428 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
429 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
430 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
431 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
433 tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
434 tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
435 tdep->sizeof_gregset = 17 * 4;
437 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
439 tdep->sigtramp_p = i386_linux_sigtramp_p;
440 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
441 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
442 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
444 /* N_FUN symbols in shared libaries have 0 for their values and need
446 set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
448 /* GNU/Linux uses SVR4-style shared libraries. */
449 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
450 set_solib_svr4_fetch_link_map_offsets
451 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
453 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
454 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
456 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
458 /* Enable TLS support. */
459 set_gdbarch_fetch_tls_load_module_address (gdbarch,
460 svr4_fetch_objfile_link_map);
462 /* Install supported register note sections. */
463 set_gdbarch_core_regset_sections (gdbarch, i386_linux_regset_sections);
465 /* Displaced stepping. */
466 set_gdbarch_displaced_step_copy_insn (gdbarch,
467 simple_displaced_step_copy_insn);
468 set_gdbarch_displaced_step_fixup (gdbarch, i386_displaced_step_fixup);
469 set_gdbarch_displaced_step_free_closure (gdbarch,
470 simple_displaced_step_free_closure);
471 set_gdbarch_displaced_step_location (gdbarch,
472 displaced_step_at_entry_point);
474 set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
477 /* Provide a prototype to silence -Wmissing-prototypes. */
478 extern void _initialize_i386_linux_tdep (void);
481 _initialize_i386_linux_tdep (void)
483 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
484 i386_linux_init_abi);