* top.c (print_gdb_version): Update copyright year.
[deliverable/binutils-gdb.git] / gdb / i386-linux-tdep.c
CommitLineData
871fbe6a 1/* Target-dependent code for GNU/Linux i386.
ca557f44 2
9b254dd1 3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
e7ee86a9
JB
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
e7ee86a9
JB
20
21#include "defs.h"
22#include "gdbcore.h"
23#include "frame.h"
24#include "value.h"
4e052eda 25#include "regcache.h"
6441c4a0 26#include "inferior.h"
0670c0aa 27#include "osabi.h"
38c968cf 28#include "reggroups.h"
5cb2fe25 29#include "dwarf2-frame.h"
0670c0aa 30#include "gdb_string.h"
4be87837 31
8201327c
MK
32#include "i386-tdep.h"
33#include "i386-linux-tdep.h"
0670c0aa 34#include "glibc-tdep.h"
871fbe6a 35#include "solib-svr4.h"
982e9687 36#include "symtab.h"
237fc4c9 37#include "arch-utils.h"
17ea7499
CES
38#include "regset.h"
39
40/* Supported register note sections. */
41static struct core_regset_section i386_linux_regset_sections[] =
42{
43 { ".reg", 144 },
44 { ".reg2", 108 },
45 { ".reg-xfp", 512 },
46 { NULL, 0 }
47};
8201327c 48
6441c4a0
MK
49/* Return the name of register REG. */
50
16775908 51static const char *
d93859e2 52i386_linux_register_name (struct gdbarch *gdbarch, int reg)
6441c4a0
MK
53{
54 /* Deal with the extra "orig_eax" pseudo register. */
55 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
56 return "orig_eax";
57
d93859e2 58 return i386_register_name (gdbarch, reg);
6441c4a0 59}
38c968cf
AC
60
61/* Return non-zero, when the register is in the corresponding register
62 group. Put the LINUX_ORIG_EAX register in the system group. */
63static int
64i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
65 struct reggroup *group)
66{
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);
72}
73
e7ee86a9
JB
74\f
75/* Recognizing signal handler frames. */
76
ca557f44 77/* GNU/Linux has two flavors of signals. Normal signal handlers, and
e7ee86a9
JB
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
ca557f44
AC
81 unlikely that future versions of GNU/Linux will support SA_SIGINFO
82 for normal signals too. */
e7ee86a9
JB
83
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.
88
89 The instruction sequence for normal signals is
90 pop %eax
acd5c798 91 mov $0x77, %eax
e7ee86a9
JB
92 int $0x80
93 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
94
95 Checking for the code sequence should be somewhat reliable, because
96 the effect is to call the system call sigreturn. This is unlikely
911bc6ee 97 to occur anywhere other than in a signal trampoline.
e7ee86a9
JB
98
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
911bc6ee
MK
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.
e7ee86a9
JB
104
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
111 supported too. */
112
acd5c798
MK
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
e7ee86a9 119
4252dc94 120static const gdb_byte linux_sigtramp_code[] =
e7ee86a9
JB
121{
122 LINUX_SIGTRAMP_INSN0, /* pop %eax */
acd5c798 123 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
e7ee86a9
JB
124 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
125};
126
127#define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
128
10458914
DJ
129/* If THIS_FRAME is a sigtramp routine, return the address of the
130 start of the routine. Otherwise, return 0. */
e7ee86a9
JB
131
132static CORE_ADDR
10458914 133i386_linux_sigtramp_start (struct frame_info *this_frame)
e7ee86a9 134{
10458914 135 CORE_ADDR pc = get_frame_pc (this_frame);
4252dc94 136 gdb_byte buf[LINUX_SIGTRAMP_LEN];
e7ee86a9
JB
137
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. */
144
10458914 145 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
e7ee86a9
JB
146 return 0;
147
148 if (buf[0] != LINUX_SIGTRAMP_INSN0)
149 {
150 int adjust;
151
152 switch (buf[0])
153 {
154 case LINUX_SIGTRAMP_INSN1:
155 adjust = LINUX_SIGTRAMP_OFFSET1;
156 break;
157 case LINUX_SIGTRAMP_INSN2:
158 adjust = LINUX_SIGTRAMP_OFFSET2;
159 break;
160 default:
161 return 0;
162 }
163
164 pc -= adjust;
165
10458914 166 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
e7ee86a9
JB
167 return 0;
168 }
169
170 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
171 return 0;
172
173 return pc;
174}
175
176/* This function does the same for RT signals. Here the instruction
177 sequence is
acd5c798 178 mov $0xad, %eax
e7ee86a9
JB
179 int $0x80
180 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
181
182 The effect is to call the system call rt_sigreturn. */
183
acd5c798
MK
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
e7ee86a9 188
4252dc94 189static const gdb_byte linux_rt_sigtramp_code[] =
e7ee86a9 190{
acd5c798 191 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
e7ee86a9
JB
192 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
193};
194
195#define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
196
10458914
DJ
197/* If THIS_FRAME is an RT sigtramp routine, return the address of the
198 start of the routine. Otherwise, return 0. */
e7ee86a9
JB
199
200static CORE_ADDR
10458914 201i386_linux_rt_sigtramp_start (struct frame_info *this_frame)
e7ee86a9 202{
10458914 203 CORE_ADDR pc = get_frame_pc (this_frame);
4252dc94 204 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
e7ee86a9
JB
205
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. */
212
10458914 213 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
e7ee86a9
JB
214 return 0;
215
216 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
217 {
218 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
219 return 0;
220
221 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
222
10458914 223 if (!safe_frame_unwind_memory (this_frame, pc, buf,
8e6bed05 224 LINUX_RT_SIGTRAMP_LEN))
e7ee86a9
JB
225 return 0;
226 }
227
228 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
229 return 0;
230
231 return pc;
232}
233
10458914
DJ
234/* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
235 routine. */
e7ee86a9 236
8201327c 237static int
10458914 238i386_linux_sigtramp_p (struct frame_info *this_frame)
e7ee86a9 239{
10458914 240 CORE_ADDR pc = get_frame_pc (this_frame);
911bc6ee
MK
241 char *name;
242
243 find_pc_partial_function (pc, &name, NULL, NULL);
244
ef17e74b
DJ
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)
10458914
DJ
251 return (i386_linux_sigtramp_start (this_frame) != 0
252 || i386_linux_rt_sigtramp_start (this_frame) != 0);
ef17e74b
DJ
253
254 return (strcmp ("__restore", name) == 0
255 || strcmp ("__restore_rt", name) == 0);
e7ee86a9
JB
256}
257
4a4e5149
DJ
258/* Return one if the PC of THIS_FRAME is in a signal trampoline which
259 may have DWARF-2 CFI. */
12b8a2cb
DJ
260
261static int
262i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
4a4e5149 263 struct frame_info *this_frame)
12b8a2cb 264{
4a4e5149 265 CORE_ADDR pc = get_frame_pc (this_frame);
12b8a2cb
DJ
266 char *name;
267
268 find_pc_partial_function (pc, &name, NULL, NULL);
269
270 /* If a vsyscall DSO is in use, the signal trampolines may have these
271 names. */
272 if (name && (strcmp (name, "__kernel_sigreturn") == 0
273 || strcmp (name, "__kernel_rt_sigreturn") == 0))
274 return 1;
275
276 return 0;
277}
278
acd5c798
MK
279/* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
280#define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
281
10458914
DJ
282/* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
283 address of the associated sigcontext structure. */
e7ee86a9 284
b7d15bf7 285static CORE_ADDR
10458914 286i386_linux_sigcontext_addr (struct frame_info *this_frame)
e7ee86a9
JB
287{
288 CORE_ADDR pc;
acd5c798 289 CORE_ADDR sp;
4252dc94 290 gdb_byte buf[4];
acd5c798 291
10458914 292 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
acd5c798 293 sp = extract_unsigned_integer (buf, 4);
e7ee86a9 294
10458914 295 pc = i386_linux_sigtramp_start (this_frame);
e7ee86a9
JB
296 if (pc)
297 {
acd5c798
MK
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. */
10458914 304 if (pc == get_frame_pc (this_frame))
e7ee86a9
JB
305 return sp + 4;
306 return sp;
307 }
308
10458914 309 pc = i386_linux_rt_sigtramp_start (this_frame);
e7ee86a9
JB
310 if (pc)
311 {
acd5c798
MK
312 CORE_ADDR ucontext_addr;
313
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);
9fbfb822 318 ucontext_addr = extract_unsigned_integer (buf, 4);
acd5c798 319 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
e7ee86a9
JB
320 }
321
8a3fe4f8 322 error (_("Couldn't recognize signal trampoline."));
e7ee86a9
JB
323 return 0;
324}
325
6441c4a0
MK
326/* Set the program counter for process PTID to PC. */
327
8201327c 328static void
61a1198a 329i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
6441c4a0 330{
61a1198a 331 regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);
6441c4a0
MK
332
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.
340
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
346 restarted. */
61a1198a 347 regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
6441c4a0
MK
348}
349\f
8201327c 350
e9f1aad5
MK
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.
357
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. */
362
363/* Mapping between the general-purpose registers in `struct user'
364 format and GDB's register cache layout. */
365
366/* From <sys/reg.h>. */
367static int i386_linux_gregset_reg_offset[] =
368{
369 6 * 4, /* %eax */
370 1 * 4, /* %ecx */
371 2 * 4, /* %edx */
372 0 * 4, /* %ebx */
373 15 * 4, /* %esp */
374 5 * 4, /* %ebp */
375 3 * 4, /* %esi */
376 4 * 4, /* %edi */
377 12 * 4, /* %eip */
378 14 * 4, /* %eflags */
379 13 * 4, /* %cs */
380 16 * 4, /* %ss */
381 7 * 4, /* %ds */
382 8 * 4, /* %es */
383 9 * 4, /* %fs */
384 10 * 4, /* %gs */
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,
388 -1,
389 11 * 4 /* "orig_eax" */
390};
391
392/* Mapping between the general-purpose registers in `struct
393 sigcontext' format and GDB's register cache layout. */
394
a3386186 395/* From <asm/sigcontext.h>. */
bb489b3c 396static int i386_linux_sc_reg_offset[] =
a3386186
MK
397{
398 11 * 4, /* %eax */
399 10 * 4, /* %ecx */
400 9 * 4, /* %edx */
401 8 * 4, /* %ebx */
402 7 * 4, /* %esp */
403 6 * 4, /* %ebp */
404 5 * 4, /* %esi */
405 4 * 4, /* %edi */
406 14 * 4, /* %eip */
407 16 * 4, /* %eflags */
408 15 * 4, /* %cs */
409 18 * 4, /* %ss */
410 3 * 4, /* %ds */
411 2 * 4, /* %es */
412 1 * 4, /* %fs */
413 0 * 4 /* %gs */
414};
415
8201327c
MK
416static void
417i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
418{
419 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
420
421 /* GNU/Linux uses ELF. */
422 i386_elf_init_abi (info, gdbarch);
423
8201327c
MK
424 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
425 to adjust a few things. */
426
427 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
bb489b3c 428 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
8201327c 429 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
38c968cf 430 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
8201327c 431
e9f1aad5
MK
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;
435
8201327c
MK
436 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
437
911bc6ee 438 tdep->sigtramp_p = i386_linux_sigtramp_p;
b7d15bf7 439 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
a3386186 440 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
bb489b3c 441 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
8201327c 442
203c3895
UW
443 /* N_FUN symbols in shared libaries have 0 for their values and need
444 to be relocated. */
445 set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
446
871fbe6a 447 /* GNU/Linux uses SVR4-style shared libraries. */
982e9687 448 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
871fbe6a
MK
449 set_solib_svr4_fetch_link_map_offsets
450 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
451
452 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
bb41a796 453 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
12b8a2cb
DJ
454
455 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
b2756930
KB
456
457 /* Enable TLS support. */
458 set_gdbarch_fetch_tls_load_module_address (gdbarch,
459 svr4_fetch_objfile_link_map);
237fc4c9 460
17ea7499
CES
461 /* Install supported register note sections. */
462 set_gdbarch_core_regset_sections (gdbarch, i386_linux_regset_sections);
463
237fc4c9
PA
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);
8201327c
MK
472}
473
474/* Provide a prototype to silence -Wmissing-prototypes. */
475extern void _initialize_i386_linux_tdep (void);
476
477void
478_initialize_i386_linux_tdep (void)
479{
05816f70 480 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
8201327c
MK
481 i386_linux_init_abi);
482}
This page took 1.079964 seconds and 4 git commands to generate.