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 "glibc-tdep.h"
35 #include "solib-svr4.h"
37 #include "arch-utils.h"
40 /* Supported register note sections. */
41 static struct core_regset_section i386_linux_regset_sections[] =
49 /* Return the name of register REG. */
52 i386_linux_register_name (struct gdbarch *gdbarch, int reg)
54 /* Deal with the extra "orig_eax" pseudo register. */
55 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
58 return i386_register_name (gdbarch, reg);
61 /* Return non-zero, when the register is in the corresponding register
62 group. Put the LINUX_ORIG_EAX register in the system group. */
64 i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
65 struct reggroup *group)
67 if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
68 return (group == system_reggroup
69 || group == save_reggroup
70 || group == restore_reggroup);
71 return i386_register_reggroup_p (gdbarch, regnum, group);
75 /* Recognizing signal handler frames. */
77 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
78 "realtime" (RT) signals. The RT signals can provide additional
79 information to the signal handler if the SA_SIGINFO flag is set
80 when establishing a signal handler using `sigaction'. It is not
81 unlikely that future versions of GNU/Linux will support SA_SIGINFO
82 for normal signals too. */
84 /* When the i386 Linux kernel calls a signal handler and the
85 SA_RESTORER flag isn't set, the return address points to a bit of
86 code on the stack. This function returns whether the PC appears to
87 be within this bit of code.
89 The instruction sequence for normal signals is
93 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
95 Checking for the code sequence should be somewhat reliable, because
96 the effect is to call the system call sigreturn. This is unlikely
97 to occur anywhere other than in a signal trampoline.
99 It kind of sucks that we have to read memory from the process in
100 order to identify a signal trampoline, but there doesn't seem to be
101 any other way. Therefore we only do the memory reads if no
102 function name could be identified, which should be the case since
103 the code is on the stack.
105 Detection of signal trampolines for handlers that set the
106 SA_RESTORER flag is in general not possible. Unfortunately this is
107 what the GNU C Library has been doing for quite some time now.
108 However, as of version 2.1.2, the GNU C Library uses signal
109 trampolines (named __restore and __restore_rt) that are identical
110 to the ones used by the kernel. Therefore, these trampolines are
113 #define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
114 #define LINUX_SIGTRAMP_OFFSET0 0
115 #define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
116 #define LINUX_SIGTRAMP_OFFSET1 1
117 #define LINUX_SIGTRAMP_INSN2 0xcd /* int */
118 #define LINUX_SIGTRAMP_OFFSET2 6
120 static const gdb_byte linux_sigtramp_code[] =
122 LINUX_SIGTRAMP_INSN0, /* pop %eax */
123 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
124 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
127 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
129 /* If THIS_FRAME is a sigtramp routine, return the address of the
130 start of the routine. Otherwise, return 0. */
133 i386_linux_sigtramp_start (struct frame_info *this_frame)
135 CORE_ADDR pc = get_frame_pc (this_frame);
136 gdb_byte buf[LINUX_SIGTRAMP_LEN];
138 /* We only recognize a signal trampoline if PC is at the start of
139 one of the three instructions. We optimize for finding the PC at
140 the start, as will be the case when the trampoline is not the
141 first frame on the stack. We assume that in the case where the
142 PC is not at the start of the instruction sequence, there will be
143 a few trailing readable bytes on the stack. */
145 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
148 if (buf[0] != LINUX_SIGTRAMP_INSN0)
154 case LINUX_SIGTRAMP_INSN1:
155 adjust = LINUX_SIGTRAMP_OFFSET1;
157 case LINUX_SIGTRAMP_INSN2:
158 adjust = LINUX_SIGTRAMP_OFFSET2;
166 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
170 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
176 /* This function does the same for RT signals. Here the instruction
180 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
182 The effect is to call the system call rt_sigreturn. */
184 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
185 #define LINUX_RT_SIGTRAMP_OFFSET0 0
186 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
187 #define LINUX_RT_SIGTRAMP_OFFSET1 5
189 static const gdb_byte linux_rt_sigtramp_code[] =
191 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
192 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
195 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
197 /* If THIS_FRAME is an RT sigtramp routine, return the address of the
198 start of the routine. Otherwise, return 0. */
201 i386_linux_rt_sigtramp_start (struct frame_info *this_frame)
203 CORE_ADDR pc = get_frame_pc (this_frame);
204 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
206 /* We only recognize a signal trampoline if PC is at the start of
207 one of the two instructions. We optimize for finding the PC at
208 the start, as will be the case when the trampoline is not the
209 first frame on the stack. We assume that in the case where the
210 PC is not at the start of the instruction sequence, there will be
211 a few trailing readable bytes on the stack. */
213 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
216 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
218 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
221 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
223 if (!safe_frame_unwind_memory (this_frame, pc, buf,
224 LINUX_RT_SIGTRAMP_LEN))
228 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
234 /* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
238 i386_linux_sigtramp_p (struct frame_info *this_frame)
240 CORE_ADDR pc = get_frame_pc (this_frame);
243 find_pc_partial_function (pc, &name, NULL, NULL);
245 /* If we have NAME, we can optimize the search. The trampolines are
246 named __restore and __restore_rt. However, they aren't dynamically
247 exported from the shared C library, so the trampoline may appear to
248 be part of the preceding function. This should always be sigaction,
249 __sigaction, or __libc_sigaction (all aliases to the same function). */
250 if (name == NULL || strstr (name, "sigaction") != NULL)
251 return (i386_linux_sigtramp_start (this_frame) != 0
252 || i386_linux_rt_sigtramp_start (this_frame) != 0);
254 return (strcmp ("__restore", name) == 0
255 || strcmp ("__restore_rt", name) == 0);
258 /* Return one if the PC of THIS_FRAME is in a signal trampoline which
259 may have DWARF-2 CFI. */
262 i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
263 struct frame_info *this_frame)
265 CORE_ADDR pc = get_frame_pc (this_frame);
268 find_pc_partial_function (pc, &name, NULL, NULL);
270 /* If a vsyscall DSO is in use, the signal trampolines may have these
272 if (name && (strcmp (name, "__kernel_sigreturn") == 0
273 || strcmp (name, "__kernel_rt_sigreturn") == 0))
279 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
280 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
282 /* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
283 address of the associated sigcontext structure. */
286 i386_linux_sigcontext_addr (struct frame_info *this_frame)
292 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
293 sp = extract_unsigned_integer (buf, 4);
295 pc = i386_linux_sigtramp_start (this_frame);
298 /* The sigcontext structure lives on the stack, right after
299 the signum argument. We determine the address of the
300 sigcontext structure by looking at the frame's stack
301 pointer. Keep in mind that the first instruction of the
302 sigtramp code is "pop %eax". If the PC is after this
303 instruction, adjust the returned value accordingly. */
304 if (pc == get_frame_pc (this_frame))
309 pc = i386_linux_rt_sigtramp_start (this_frame);
312 CORE_ADDR ucontext_addr;
314 /* The sigcontext structure is part of the user context. A
315 pointer to the user context is passed as the third argument
316 to the signal handler. */
317 read_memory (sp + 8, buf, 4);
318 ucontext_addr = extract_unsigned_integer (buf, 4);
319 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
322 error (_("Couldn't recognize signal trampoline."));
326 /* Set the program counter for process PTID to PC. */
329 i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
331 regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);
333 /* We must be careful with modifying the program counter. If we
334 just interrupted a system call, the kernel might try to restart
335 it when we resume the inferior. On restarting the system call,
336 the kernel will try backing up the program counter even though it
337 no longer points at the system call. This typically results in a
338 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
339 "orig_eax" pseudo-register.
341 Note that "orig_eax" is saved when setting up a dummy call frame.
342 This means that it is properly restored when that frame is
343 popped, and that the interrupted system call will be restarted
344 when we resume the inferior on return from a function call from
345 within GDB. In all other cases the system call will not be
347 regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
351 /* The register sets used in GNU/Linux ELF core-dumps are identical to
352 the register sets in `struct user' that are used for a.out
353 core-dumps. These are also used by ptrace(2). The corresponding
354 types are `elf_gregset_t' for the general-purpose registers (with
355 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
356 for the floating-point registers.
358 Those types used to be available under the names `gregset_t' and
359 `fpregset_t' too, and GDB used those names in the past. But those
360 names are now used for the register sets used in the `mcontext_t'
361 type, which have a different size and layout. */
363 /* Mapping between the general-purpose registers in `struct user'
364 format and GDB's register cache layout. */
366 /* From <sys/reg.h>. */
367 static int i386_linux_gregset_reg_offset[] =
378 14 * 4, /* %eflags */
385 -1, -1, -1, -1, -1, -1, -1, -1,
386 -1, -1, -1, -1, -1, -1, -1, -1,
387 -1, -1, -1, -1, -1, -1, -1, -1,
389 11 * 4 /* "orig_eax" */
392 /* Mapping between the general-purpose registers in `struct
393 sigcontext' format and GDB's register cache layout. */
395 /* From <asm/sigcontext.h>. */
396 static int i386_linux_sc_reg_offset[] =
407 16 * 4, /* %eflags */
417 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
419 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
421 /* GNU/Linux uses ELF. */
422 i386_elf_init_abi (info, gdbarch);
424 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
425 to adjust a few things. */
427 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
428 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
429 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
430 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
432 tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
433 tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
434 tdep->sizeof_gregset = 17 * 4;
436 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
438 tdep->sigtramp_p = i386_linux_sigtramp_p;
439 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
440 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
441 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
443 /* N_FUN symbols in shared libaries have 0 for their values and need
445 set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
447 /* GNU/Linux uses SVR4-style shared libraries. */
448 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
449 set_solib_svr4_fetch_link_map_offsets
450 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
452 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
453 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
455 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
457 /* Enable TLS support. */
458 set_gdbarch_fetch_tls_load_module_address (gdbarch,
459 svr4_fetch_objfile_link_map);
461 /* Install supported register note sections. */
462 set_gdbarch_core_regset_sections (gdbarch, i386_linux_regset_sections);
464 /* Displaced stepping. */
465 set_gdbarch_displaced_step_copy_insn (gdbarch,
466 simple_displaced_step_copy_insn);
467 set_gdbarch_displaced_step_fixup (gdbarch, i386_displaced_step_fixup);
468 set_gdbarch_displaced_step_free_closure (gdbarch,
469 simple_displaced_step_free_closure);
470 set_gdbarch_displaced_step_location (gdbarch,
471 displaced_step_at_entry_point);
474 /* Provide a prototype to silence -Wmissing-prototypes. */
475 extern void _initialize_i386_linux_tdep (void);
478 _initialize_i386_linux_tdep (void)
480 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
481 i386_linux_init_abi);