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"
39 /* Return the name of register REG. */
42 i386_linux_register_name (int reg
)
44 /* Deal with the extra "orig_eax" pseudo register. */
45 if (reg
== I386_LINUX_ORIG_EAX_REGNUM
)
48 return i386_register_name (reg
);
51 /* Return non-zero, when the register is in the corresponding register
52 group. Put the LINUX_ORIG_EAX register in the system group. */
54 i386_linux_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
55 struct reggroup
*group
)
57 if (regnum
== I386_LINUX_ORIG_EAX_REGNUM
)
58 return (group
== system_reggroup
59 || group
== save_reggroup
60 || group
== restore_reggroup
);
61 return i386_register_reggroup_p (gdbarch
, regnum
, group
);
65 /* Recognizing signal handler frames. */
67 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
68 "realtime" (RT) signals. The RT signals can provide additional
69 information to the signal handler if the SA_SIGINFO flag is set
70 when establishing a signal handler using `sigaction'. It is not
71 unlikely that future versions of GNU/Linux will support SA_SIGINFO
72 for normal signals too. */
74 /* When the i386 Linux kernel calls a signal handler and the
75 SA_RESTORER flag isn't set, the return address points to a bit of
76 code on the stack. This function returns whether the PC appears to
77 be within this bit of code.
79 The instruction sequence for normal signals is
83 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
85 Checking for the code sequence should be somewhat reliable, because
86 the effect is to call the system call sigreturn. This is unlikely
87 to occur anywhere other than in a signal trampoline.
89 It kind of sucks that we have to read memory from the process in
90 order to identify a signal trampoline, but there doesn't seem to be
91 any other way. Therefore we only do the memory reads if no
92 function name could be identified, which should be the case since
93 the code is on the stack.
95 Detection of signal trampolines for handlers that set the
96 SA_RESTORER flag is in general not possible. Unfortunately this is
97 what the GNU C Library has been doing for quite some time now.
98 However, as of version 2.1.2, the GNU C Library uses signal
99 trampolines (named __restore and __restore_rt) that are identical
100 to the ones used by the kernel. Therefore, these trampolines are
103 #define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
104 #define LINUX_SIGTRAMP_OFFSET0 0
105 #define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
106 #define LINUX_SIGTRAMP_OFFSET1 1
107 #define LINUX_SIGTRAMP_INSN2 0xcd /* int */
108 #define LINUX_SIGTRAMP_OFFSET2 6
110 static const gdb_byte linux_sigtramp_code
[] =
112 LINUX_SIGTRAMP_INSN0
, /* pop %eax */
113 LINUX_SIGTRAMP_INSN1
, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
114 LINUX_SIGTRAMP_INSN2
, 0x80 /* int $0x80 */
117 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
119 /* If NEXT_FRAME unwinds into a sigtramp routine, return the address
120 of the start of the routine. Otherwise, return 0. */
123 i386_linux_sigtramp_start (struct frame_info
*next_frame
)
125 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
126 gdb_byte buf
[LINUX_SIGTRAMP_LEN
];
128 /* We only recognize a signal trampoline if PC is at the start of
129 one of the three instructions. We optimize for finding the PC at
130 the start, as will be the case when the trampoline is not the
131 first frame on the stack. We assume that in the case where the
132 PC is not at the start of the instruction sequence, there will be
133 a few trailing readable bytes on the stack. */
135 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, LINUX_SIGTRAMP_LEN
))
138 if (buf
[0] != LINUX_SIGTRAMP_INSN0
)
144 case LINUX_SIGTRAMP_INSN1
:
145 adjust
= LINUX_SIGTRAMP_OFFSET1
;
147 case LINUX_SIGTRAMP_INSN2
:
148 adjust
= LINUX_SIGTRAMP_OFFSET2
;
156 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, LINUX_SIGTRAMP_LEN
))
160 if (memcmp (buf
, linux_sigtramp_code
, LINUX_SIGTRAMP_LEN
) != 0)
166 /* This function does the same for RT signals. Here the instruction
170 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
172 The effect is to call the system call rt_sigreturn. */
174 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
175 #define LINUX_RT_SIGTRAMP_OFFSET0 0
176 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
177 #define LINUX_RT_SIGTRAMP_OFFSET1 5
179 static const gdb_byte linux_rt_sigtramp_code
[] =
181 LINUX_RT_SIGTRAMP_INSN0
, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
182 LINUX_RT_SIGTRAMP_INSN1
, 0x80 /* int $0x80 */
185 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
187 /* If NEXT_FRAME unwinds into an RT sigtramp routine, return the
188 address of the start of the routine. Otherwise, return 0. */
191 i386_linux_rt_sigtramp_start (struct frame_info
*next_frame
)
193 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
194 gdb_byte buf
[LINUX_RT_SIGTRAMP_LEN
];
196 /* We only recognize a signal trampoline if PC is at the start of
197 one of the two instructions. We optimize for finding the PC at
198 the start, as will be the case when the trampoline is not the
199 first frame on the stack. We assume that in the case where the
200 PC is not at the start of the instruction sequence, there will be
201 a few trailing readable bytes on the stack. */
203 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, LINUX_RT_SIGTRAMP_LEN
))
206 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN0
)
208 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN1
)
211 pc
-= LINUX_RT_SIGTRAMP_OFFSET1
;
213 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
,
214 LINUX_RT_SIGTRAMP_LEN
))
218 if (memcmp (buf
, linux_rt_sigtramp_code
, LINUX_RT_SIGTRAMP_LEN
) != 0)
224 /* Return whether the frame preceding NEXT_FRAME corresponds to a
225 GNU/Linux sigtramp routine. */
228 i386_linux_sigtramp_p (struct frame_info
*next_frame
)
230 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
233 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
235 /* If we have NAME, we can optimize the search. The trampolines are
236 named __restore and __restore_rt. However, they aren't dynamically
237 exported from the shared C library, so the trampoline may appear to
238 be part of the preceding function. This should always be sigaction,
239 __sigaction, or __libc_sigaction (all aliases to the same function). */
240 if (name
== NULL
|| strstr (name
, "sigaction") != NULL
)
241 return (i386_linux_sigtramp_start (next_frame
) != 0
242 || i386_linux_rt_sigtramp_start (next_frame
) != 0);
244 return (strcmp ("__restore", name
) == 0
245 || strcmp ("__restore_rt", name
) == 0);
248 /* Return one if the unwound PC from NEXT_FRAME is in a signal trampoline
249 which may have DWARF-2 CFI. */
252 i386_linux_dwarf_signal_frame_p (struct gdbarch
*gdbarch
,
253 struct frame_info
*next_frame
)
255 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
258 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
260 /* If a vsyscall DSO is in use, the signal trampolines may have these
262 if (name
&& (strcmp (name
, "__kernel_sigreturn") == 0
263 || strcmp (name
, "__kernel_rt_sigreturn") == 0))
269 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
270 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
272 /* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
273 routine, return the address of the associated sigcontext structure. */
276 i386_linux_sigcontext_addr (struct frame_info
*next_frame
)
282 frame_unwind_register (next_frame
, I386_ESP_REGNUM
, buf
);
283 sp
= extract_unsigned_integer (buf
, 4);
285 pc
= i386_linux_sigtramp_start (next_frame
);
288 /* The sigcontext structure lives on the stack, right after
289 the signum argument. We determine the address of the
290 sigcontext structure by looking at the frame's stack
291 pointer. Keep in mind that the first instruction of the
292 sigtramp code is "pop %eax". If the PC is after this
293 instruction, adjust the returned value accordingly. */
294 if (pc
== frame_pc_unwind (next_frame
))
299 pc
= i386_linux_rt_sigtramp_start (next_frame
);
302 CORE_ADDR ucontext_addr
;
304 /* The sigcontext structure is part of the user context. A
305 pointer to the user context is passed as the third argument
306 to the signal handler. */
307 read_memory (sp
+ 8, buf
, 4);
308 ucontext_addr
= extract_unsigned_integer (buf
, 4);
309 return ucontext_addr
+ I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET
;
312 error (_("Couldn't recognize signal trampoline."));
316 /* Set the program counter for process PTID to PC. */
319 i386_linux_write_pc (CORE_ADDR pc
, ptid_t ptid
)
321 write_register_pid (I386_EIP_REGNUM
, pc
, ptid
);
323 /* We must be careful with modifying the program counter. If we
324 just interrupted a system call, the kernel might try to restart
325 it when we resume the inferior. On restarting the system call,
326 the kernel will try backing up the program counter even though it
327 no longer points at the system call. This typically results in a
328 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
329 "orig_eax" pseudo-register.
331 Note that "orig_eax" is saved when setting up a dummy call frame.
332 This means that it is properly restored when that frame is
333 popped, and that the interrupted system call will be restarted
334 when we resume the inferior on return from a function call from
335 within GDB. In all other cases the system call will not be
337 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM
, -1, ptid
);
341 /* The register sets used in GNU/Linux ELF core-dumps are identical to
342 the register sets in `struct user' that are used for a.out
343 core-dumps. These are also used by ptrace(2). The corresponding
344 types are `elf_gregset_t' for the general-purpose registers (with
345 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
346 for the floating-point registers.
348 Those types used to be available under the names `gregset_t' and
349 `fpregset_t' too, and GDB used those names in the past. But those
350 names are now used for the register sets used in the `mcontext_t'
351 type, which have a different size and layout. */
353 /* Mapping between the general-purpose registers in `struct user'
354 format and GDB's register cache layout. */
356 /* From <sys/reg.h>. */
357 static int i386_linux_gregset_reg_offset
[] =
368 14 * 4, /* %eflags */
375 -1, -1, -1, -1, -1, -1, -1, -1,
376 -1, -1, -1, -1, -1, -1, -1, -1,
377 -1, -1, -1, -1, -1, -1, -1, -1,
379 11 * 4 /* "orig_eax" */
382 /* Mapping between the general-purpose registers in `struct
383 sigcontext' format and GDB's register cache layout. */
385 /* From <asm/sigcontext.h>. */
386 static int i386_linux_sc_reg_offset
[] =
397 16 * 4, /* %eflags */
407 i386_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
409 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
411 /* GNU/Linux uses ELF. */
412 i386_elf_init_abi (info
, gdbarch
);
414 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
415 to adjust a few things. */
417 set_gdbarch_write_pc (gdbarch
, i386_linux_write_pc
);
418 set_gdbarch_num_regs (gdbarch
, I386_LINUX_NUM_REGS
);
419 set_gdbarch_register_name (gdbarch
, i386_linux_register_name
);
420 set_gdbarch_register_reggroup_p (gdbarch
, i386_linux_register_reggroup_p
);
422 tdep
->gregset_reg_offset
= i386_linux_gregset_reg_offset
;
423 tdep
->gregset_num_regs
= ARRAY_SIZE (i386_linux_gregset_reg_offset
);
424 tdep
->sizeof_gregset
= 17 * 4;
426 tdep
->jb_pc_offset
= 20; /* From <bits/setjmp.h>. */
428 tdep
->sigtramp_p
= i386_linux_sigtramp_p
;
429 tdep
->sigcontext_addr
= i386_linux_sigcontext_addr
;
430 tdep
->sc_reg_offset
= i386_linux_sc_reg_offset
;
431 tdep
->sc_num_regs
= ARRAY_SIZE (i386_linux_sc_reg_offset
);
433 /* GNU/Linux uses SVR4-style shared libraries. */
434 set_solib_svr4_fetch_link_map_offsets
435 (gdbarch
, svr4_ilp32_fetch_link_map_offsets
);
437 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
438 set_gdbarch_skip_solib_resolver (gdbarch
, glibc_skip_solib_resolver
);
440 dwarf2_frame_set_signal_frame_p (gdbarch
, i386_linux_dwarf_signal_frame_p
);
442 /* Enable TLS support. */
443 set_gdbarch_fetch_tls_load_module_address (gdbarch
,
444 svr4_fetch_objfile_link_map
);
447 /* Provide a prototype to silence -Wmissing-prototypes. */
448 extern void _initialize_i386_linux_tdep (void);
451 _initialize_i386_linux_tdep (void)
453 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_LINUX
,
454 i386_linux_init_abi
);