]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/i386-linux-tdep.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / i386-linux-tdep.c
CommitLineData
871fbe6a 1/* Target-dependent code for GNU/Linux i386.
ca557f44 2
6aba47ca 3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007
4252dc94 4 Free Software Foundation, Inc.
e7ee86a9
JB
5
6 This file is part of GDB.
7
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.
12
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.
17
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
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
e7ee86a9
JB
22
23#include "defs.h"
24#include "gdbcore.h"
25#include "frame.h"
26#include "value.h"
4e052eda 27#include "regcache.h"
6441c4a0 28#include "inferior.h"
0670c0aa 29#include "osabi.h"
38c968cf 30#include "reggroups.h"
5cb2fe25 31#include "dwarf2-frame.h"
0670c0aa 32#include "gdb_string.h"
4be87837 33
8201327c
MK
34#include "i386-tdep.h"
35#include "i386-linux-tdep.h"
0670c0aa 36#include "glibc-tdep.h"
871fbe6a 37#include "solib-svr4.h"
982e9687 38#include "symtab.h"
8201327c 39
6441c4a0
MK
40/* Return the name of register REG. */
41
16775908 42static const char *
6441c4a0
MK
43i386_linux_register_name (int reg)
44{
45 /* Deal with the extra "orig_eax" pseudo register. */
46 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
47 return "orig_eax";
48
49 return i386_register_name (reg);
50}
38c968cf
AC
51
52/* Return non-zero, when the register is in the corresponding register
53 group. Put the LINUX_ORIG_EAX register in the system group. */
54static int
55i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
56 struct reggroup *group)
57{
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);
63}
64
e7ee86a9
JB
65\f
66/* Recognizing signal handler frames. */
67
ca557f44 68/* GNU/Linux has two flavors of signals. Normal signal handlers, and
e7ee86a9
JB
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
ca557f44
AC
72 unlikely that future versions of GNU/Linux will support SA_SIGINFO
73 for normal signals too. */
e7ee86a9
JB
74
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.
79
80 The instruction sequence for normal signals is
81 pop %eax
acd5c798 82 mov $0x77, %eax
e7ee86a9
JB
83 int $0x80
84 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
85
86 Checking for the code sequence should be somewhat reliable, because
87 the effect is to call the system call sigreturn. This is unlikely
911bc6ee 88 to occur anywhere other than in a signal trampoline.
e7ee86a9
JB
89
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
911bc6ee
MK
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.
e7ee86a9
JB
95
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
102 supported too. */
103
acd5c798
MK
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
e7ee86a9 110
4252dc94 111static const gdb_byte linux_sigtramp_code[] =
e7ee86a9
JB
112{
113 LINUX_SIGTRAMP_INSN0, /* pop %eax */
acd5c798 114 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
e7ee86a9
JB
115 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
116};
117
118#define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
119
8e6bed05
MK
120/* If NEXT_FRAME unwinds into a sigtramp routine, return the address
121 of the start of the routine. Otherwise, return 0. */
e7ee86a9
JB
122
123static CORE_ADDR
8e6bed05 124i386_linux_sigtramp_start (struct frame_info *next_frame)
e7ee86a9 125{
8e6bed05 126 CORE_ADDR pc = frame_pc_unwind (next_frame);
4252dc94 127 gdb_byte buf[LINUX_SIGTRAMP_LEN];
e7ee86a9
JB
128
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. */
135
8e6bed05 136 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_SIGTRAMP_LEN))
e7ee86a9
JB
137 return 0;
138
139 if (buf[0] != LINUX_SIGTRAMP_INSN0)
140 {
141 int adjust;
142
143 switch (buf[0])
144 {
145 case LINUX_SIGTRAMP_INSN1:
146 adjust = LINUX_SIGTRAMP_OFFSET1;
147 break;
148 case LINUX_SIGTRAMP_INSN2:
149 adjust = LINUX_SIGTRAMP_OFFSET2;
150 break;
151 default:
152 return 0;
153 }
154
155 pc -= adjust;
156
8e6bed05 157 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_SIGTRAMP_LEN))
e7ee86a9
JB
158 return 0;
159 }
160
161 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
162 return 0;
163
164 return pc;
165}
166
167/* This function does the same for RT signals. Here the instruction
168 sequence is
acd5c798 169 mov $0xad, %eax
e7ee86a9
JB
170 int $0x80
171 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
172
173 The effect is to call the system call rt_sigreturn. */
174
acd5c798
MK
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
e7ee86a9 179
4252dc94 180static const gdb_byte linux_rt_sigtramp_code[] =
e7ee86a9 181{
acd5c798 182 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
e7ee86a9
JB
183 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
184};
185
186#define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
187
8e6bed05
MK
188/* If NEXT_FRAME unwinds into an RT sigtramp routine, return the
189 address of the start of the routine. Otherwise, return 0. */
e7ee86a9
JB
190
191static CORE_ADDR
8e6bed05 192i386_linux_rt_sigtramp_start (struct frame_info *next_frame)
e7ee86a9 193{
8e6bed05 194 CORE_ADDR pc = frame_pc_unwind (next_frame);
4252dc94 195 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
e7ee86a9
JB
196
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. */
203
8e6bed05 204 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
e7ee86a9
JB
205 return 0;
206
207 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
208 {
209 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
210 return 0;
211
212 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
213
8e6bed05
MK
214 if (!safe_frame_unwind_memory (next_frame, pc, buf,
215 LINUX_RT_SIGTRAMP_LEN))
e7ee86a9
JB
216 return 0;
217 }
218
219 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
220 return 0;
221
222 return pc;
223}
224
377d9ebd 225/* Return whether the frame preceding NEXT_FRAME corresponds to a
911bc6ee 226 GNU/Linux sigtramp routine. */
e7ee86a9 227
8201327c 228static int
911bc6ee 229i386_linux_sigtramp_p (struct frame_info *next_frame)
e7ee86a9 230{
911bc6ee
MK
231 CORE_ADDR pc = frame_pc_unwind (next_frame);
232 char *name;
233
234 find_pc_partial_function (pc, &name, NULL, NULL);
235
ef17e74b
DJ
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)
8e6bed05
MK
242 return (i386_linux_sigtramp_start (next_frame) != 0
243 || i386_linux_rt_sigtramp_start (next_frame) != 0);
ef17e74b
DJ
244
245 return (strcmp ("__restore", name) == 0
246 || strcmp ("__restore_rt", name) == 0);
e7ee86a9
JB
247}
248
12b8a2cb
DJ
249/* Return one if the unwound PC from NEXT_FRAME is in a signal trampoline
250 which may have DWARF-2 CFI. */
251
252static int
253i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
254 struct frame_info *next_frame)
255{
256 CORE_ADDR pc = frame_pc_unwind (next_frame);
257 char *name;
258
259 find_pc_partial_function (pc, &name, NULL, NULL);
260
261 /* If a vsyscall DSO is in use, the signal trampolines may have these
262 names. */
263 if (name && (strcmp (name, "__kernel_sigreturn") == 0
264 || strcmp (name, "__kernel_rt_sigreturn") == 0))
265 return 1;
266
267 return 0;
268}
269
acd5c798
MK
270/* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
271#define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
272
273/* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
274 routine, return the address of the associated sigcontext structure. */
e7ee86a9 275
b7d15bf7 276static CORE_ADDR
acd5c798 277i386_linux_sigcontext_addr (struct frame_info *next_frame)
e7ee86a9
JB
278{
279 CORE_ADDR pc;
acd5c798 280 CORE_ADDR sp;
4252dc94 281 gdb_byte buf[4];
acd5c798 282
c7f16359 283 frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
acd5c798 284 sp = extract_unsigned_integer (buf, 4);
e7ee86a9 285
8e6bed05 286 pc = i386_linux_sigtramp_start (next_frame);
e7ee86a9
JB
287 if (pc)
288 {
acd5c798
MK
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))
e7ee86a9
JB
296 return sp + 4;
297 return sp;
298 }
299
8e6bed05 300 pc = i386_linux_rt_sigtramp_start (next_frame);
e7ee86a9
JB
301 if (pc)
302 {
acd5c798
MK
303 CORE_ADDR ucontext_addr;
304
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);
9fbfb822 309 ucontext_addr = extract_unsigned_integer (buf, 4);
acd5c798 310 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
e7ee86a9
JB
311 }
312
8a3fe4f8 313 error (_("Couldn't recognize signal trampoline."));
e7ee86a9
JB
314 return 0;
315}
316
6441c4a0
MK
317/* Set the program counter for process PTID to PC. */
318
8201327c 319static void
6441c4a0
MK
320i386_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
321{
c7f16359 322 write_register_pid (I386_EIP_REGNUM, pc, ptid);
6441c4a0
MK
323
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.
331
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
337 restarted. */
338 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM, -1, ptid);
339}
340\f
8201327c 341
e9f1aad5
MK
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.
348
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. */
353
354/* Mapping between the general-purpose registers in `struct user'
355 format and GDB's register cache layout. */
356
357/* From <sys/reg.h>. */
358static int i386_linux_gregset_reg_offset[] =
359{
360 6 * 4, /* %eax */
361 1 * 4, /* %ecx */
362 2 * 4, /* %edx */
363 0 * 4, /* %ebx */
364 15 * 4, /* %esp */
365 5 * 4, /* %ebp */
366 3 * 4, /* %esi */
367 4 * 4, /* %edi */
368 12 * 4, /* %eip */
369 14 * 4, /* %eflags */
370 13 * 4, /* %cs */
371 16 * 4, /* %ss */
372 7 * 4, /* %ds */
373 8 * 4, /* %es */
374 9 * 4, /* %fs */
375 10 * 4, /* %gs */
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,
379 -1,
380 11 * 4 /* "orig_eax" */
381};
382
383/* Mapping between the general-purpose registers in `struct
384 sigcontext' format and GDB's register cache layout. */
385
a3386186 386/* From <asm/sigcontext.h>. */
bb489b3c 387static int i386_linux_sc_reg_offset[] =
a3386186
MK
388{
389 11 * 4, /* %eax */
390 10 * 4, /* %ecx */
391 9 * 4, /* %edx */
392 8 * 4, /* %ebx */
393 7 * 4, /* %esp */
394 6 * 4, /* %ebp */
395 5 * 4, /* %esi */
396 4 * 4, /* %edi */
397 14 * 4, /* %eip */
398 16 * 4, /* %eflags */
399 15 * 4, /* %cs */
400 18 * 4, /* %ss */
401 3 * 4, /* %ds */
402 2 * 4, /* %es */
403 1 * 4, /* %fs */
404 0 * 4 /* %gs */
405};
406
8201327c
MK
407static void
408i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
409{
410 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
411
412 /* GNU/Linux uses ELF. */
413 i386_elf_init_abi (info, gdbarch);
414
8201327c
MK
415 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
416 to adjust a few things. */
417
418 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
bb489b3c 419 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
8201327c 420 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
38c968cf 421 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
8201327c 422
e9f1aad5
MK
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;
426
8201327c
MK
427 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
428
911bc6ee 429 tdep->sigtramp_p = i386_linux_sigtramp_p;
b7d15bf7 430 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
a3386186 431 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
bb489b3c 432 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
8201327c 433
871fbe6a 434 /* GNU/Linux uses SVR4-style shared libraries. */
982e9687 435 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
871fbe6a
MK
436 set_solib_svr4_fetch_link_map_offsets
437 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
438
439 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
bb41a796 440 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
12b8a2cb
DJ
441
442 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
b2756930
KB
443
444 /* Enable TLS support. */
445 set_gdbarch_fetch_tls_load_module_address (gdbarch,
446 svr4_fetch_objfile_link_map);
8201327c
MK
447}
448
449/* Provide a prototype to silence -Wmissing-prototypes. */
450extern void _initialize_i386_linux_tdep (void);
451
452void
453_initialize_i386_linux_tdep (void)
454{
05816f70 455 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
8201327c
MK
456 i386_linux_init_abi);
457}