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