* README: Mention gdbserver/README.
[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"
8201327c 37
6441c4a0
MK
38/* Return the name of register REG. */
39
16775908 40static const char *
d93859e2 41i386_linux_register_name (struct gdbarch *gdbarch, int reg)
6441c4a0
MK
42{
43 /* Deal with the extra "orig_eax" pseudo register. */
44 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
45 return "orig_eax";
46
d93859e2 47 return i386_register_name (gdbarch, reg);
6441c4a0 48}
38c968cf
AC
49
50/* Return non-zero, when the register is in the corresponding register
51 group. Put the LINUX_ORIG_EAX register in the system group. */
52static int
53i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
54 struct reggroup *group)
55{
56 if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
57 return (group == system_reggroup
58 || group == save_reggroup
59 || group == restore_reggroup);
60 return i386_register_reggroup_p (gdbarch, regnum, group);
61}
62
e7ee86a9
JB
63\f
64/* Recognizing signal handler frames. */
65
ca557f44 66/* GNU/Linux has two flavors of signals. Normal signal handlers, and
e7ee86a9
JB
67 "realtime" (RT) signals. The RT signals can provide additional
68 information to the signal handler if the SA_SIGINFO flag is set
69 when establishing a signal handler using `sigaction'. It is not
ca557f44
AC
70 unlikely that future versions of GNU/Linux will support SA_SIGINFO
71 for normal signals too. */
e7ee86a9
JB
72
73/* When the i386 Linux kernel calls a signal handler and the
74 SA_RESTORER flag isn't set, the return address points to a bit of
75 code on the stack. This function returns whether the PC appears to
76 be within this bit of code.
77
78 The instruction sequence for normal signals is
79 pop %eax
acd5c798 80 mov $0x77, %eax
e7ee86a9
JB
81 int $0x80
82 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
83
84 Checking for the code sequence should be somewhat reliable, because
85 the effect is to call the system call sigreturn. This is unlikely
911bc6ee 86 to occur anywhere other than in a signal trampoline.
e7ee86a9
JB
87
88 It kind of sucks that we have to read memory from the process in
89 order to identify a signal trampoline, but there doesn't seem to be
911bc6ee
MK
90 any other way. Therefore we only do the memory reads if no
91 function name could be identified, which should be the case since
92 the code is on the stack.
e7ee86a9
JB
93
94 Detection of signal trampolines for handlers that set the
95 SA_RESTORER flag is in general not possible. Unfortunately this is
96 what the GNU C Library has been doing for quite some time now.
97 However, as of version 2.1.2, the GNU C Library uses signal
98 trampolines (named __restore and __restore_rt) that are identical
99 to the ones used by the kernel. Therefore, these trampolines are
100 supported too. */
101
acd5c798
MK
102#define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
103#define LINUX_SIGTRAMP_OFFSET0 0
104#define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
105#define LINUX_SIGTRAMP_OFFSET1 1
106#define LINUX_SIGTRAMP_INSN2 0xcd /* int */
107#define LINUX_SIGTRAMP_OFFSET2 6
e7ee86a9 108
4252dc94 109static const gdb_byte linux_sigtramp_code[] =
e7ee86a9
JB
110{
111 LINUX_SIGTRAMP_INSN0, /* pop %eax */
acd5c798 112 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
e7ee86a9
JB
113 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
114};
115
116#define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
117
8e6bed05
MK
118/* If NEXT_FRAME unwinds into a sigtramp routine, return the address
119 of the start of the routine. Otherwise, return 0. */
e7ee86a9
JB
120
121static CORE_ADDR
8e6bed05 122i386_linux_sigtramp_start (struct frame_info *next_frame)
e7ee86a9 123{
8e6bed05 124 CORE_ADDR pc = frame_pc_unwind (next_frame);
4252dc94 125 gdb_byte buf[LINUX_SIGTRAMP_LEN];
e7ee86a9
JB
126
127 /* We only recognize a signal trampoline if PC is at the start of
128 one of the three instructions. We optimize for finding the PC at
129 the start, as will be the case when the trampoline is not the
130 first frame on the stack. We assume that in the case where the
131 PC is not at the start of the instruction sequence, there will be
132 a few trailing readable bytes on the stack. */
133
8e6bed05 134 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_SIGTRAMP_LEN))
e7ee86a9
JB
135 return 0;
136
137 if (buf[0] != LINUX_SIGTRAMP_INSN0)
138 {
139 int adjust;
140
141 switch (buf[0])
142 {
143 case LINUX_SIGTRAMP_INSN1:
144 adjust = LINUX_SIGTRAMP_OFFSET1;
145 break;
146 case LINUX_SIGTRAMP_INSN2:
147 adjust = LINUX_SIGTRAMP_OFFSET2;
148 break;
149 default:
150 return 0;
151 }
152
153 pc -= adjust;
154
8e6bed05 155 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_SIGTRAMP_LEN))
e7ee86a9
JB
156 return 0;
157 }
158
159 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
160 return 0;
161
162 return pc;
163}
164
165/* This function does the same for RT signals. Here the instruction
166 sequence is
acd5c798 167 mov $0xad, %eax
e7ee86a9
JB
168 int $0x80
169 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
170
171 The effect is to call the system call rt_sigreturn. */
172
acd5c798
MK
173#define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
174#define LINUX_RT_SIGTRAMP_OFFSET0 0
175#define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
176#define LINUX_RT_SIGTRAMP_OFFSET1 5
e7ee86a9 177
4252dc94 178static const gdb_byte linux_rt_sigtramp_code[] =
e7ee86a9 179{
acd5c798 180 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
e7ee86a9
JB
181 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
182};
183
184#define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
185
8e6bed05
MK
186/* If NEXT_FRAME unwinds into an RT sigtramp routine, return the
187 address of the start of the routine. Otherwise, return 0. */
e7ee86a9
JB
188
189static CORE_ADDR
8e6bed05 190i386_linux_rt_sigtramp_start (struct frame_info *next_frame)
e7ee86a9 191{
8e6bed05 192 CORE_ADDR pc = frame_pc_unwind (next_frame);
4252dc94 193 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
e7ee86a9
JB
194
195 /* We only recognize a signal trampoline if PC is at the start of
196 one of the two instructions. We optimize for finding the PC at
197 the start, as will be the case when the trampoline is not the
198 first frame on the stack. We assume that in the case where the
199 PC is not at the start of the instruction sequence, there will be
200 a few trailing readable bytes on the stack. */
201
8e6bed05 202 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
e7ee86a9
JB
203 return 0;
204
205 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
206 {
207 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
208 return 0;
209
210 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
211
8e6bed05
MK
212 if (!safe_frame_unwind_memory (next_frame, pc, buf,
213 LINUX_RT_SIGTRAMP_LEN))
e7ee86a9
JB
214 return 0;
215 }
216
217 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
218 return 0;
219
220 return pc;
221}
222
377d9ebd 223/* Return whether the frame preceding NEXT_FRAME corresponds to a
911bc6ee 224 GNU/Linux sigtramp routine. */
e7ee86a9 225
8201327c 226static int
911bc6ee 227i386_linux_sigtramp_p (struct frame_info *next_frame)
e7ee86a9 228{
911bc6ee
MK
229 CORE_ADDR pc = frame_pc_unwind (next_frame);
230 char *name;
231
232 find_pc_partial_function (pc, &name, NULL, NULL);
233
ef17e74b
DJ
234 /* If we have NAME, we can optimize the search. The trampolines are
235 named __restore and __restore_rt. However, they aren't dynamically
236 exported from the shared C library, so the trampoline may appear to
237 be part of the preceding function. This should always be sigaction,
238 __sigaction, or __libc_sigaction (all aliases to the same function). */
239 if (name == NULL || strstr (name, "sigaction") != NULL)
8e6bed05
MK
240 return (i386_linux_sigtramp_start (next_frame) != 0
241 || i386_linux_rt_sigtramp_start (next_frame) != 0);
ef17e74b
DJ
242
243 return (strcmp ("__restore", name) == 0
244 || strcmp ("__restore_rt", name) == 0);
e7ee86a9
JB
245}
246
12b8a2cb
DJ
247/* Return one if the unwound PC from NEXT_FRAME is in a signal trampoline
248 which may have DWARF-2 CFI. */
249
250static int
251i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
252 struct frame_info *next_frame)
253{
254 CORE_ADDR pc = frame_pc_unwind (next_frame);
255 char *name;
256
257 find_pc_partial_function (pc, &name, NULL, NULL);
258
259 /* If a vsyscall DSO is in use, the signal trampolines may have these
260 names. */
261 if (name && (strcmp (name, "__kernel_sigreturn") == 0
262 || strcmp (name, "__kernel_rt_sigreturn") == 0))
263 return 1;
264
265 return 0;
266}
267
acd5c798
MK
268/* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
269#define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
270
271/* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
272 routine, return the address of the associated sigcontext structure. */
e7ee86a9 273
b7d15bf7 274static CORE_ADDR
acd5c798 275i386_linux_sigcontext_addr (struct frame_info *next_frame)
e7ee86a9
JB
276{
277 CORE_ADDR pc;
acd5c798 278 CORE_ADDR sp;
4252dc94 279 gdb_byte buf[4];
acd5c798 280
c7f16359 281 frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
acd5c798 282 sp = extract_unsigned_integer (buf, 4);
e7ee86a9 283
8e6bed05 284 pc = i386_linux_sigtramp_start (next_frame);
e7ee86a9
JB
285 if (pc)
286 {
acd5c798
MK
287 /* The sigcontext structure lives on the stack, right after
288 the signum argument. We determine the address of the
289 sigcontext structure by looking at the frame's stack
290 pointer. Keep in mind that the first instruction of the
291 sigtramp code is "pop %eax". If the PC is after this
292 instruction, adjust the returned value accordingly. */
293 if (pc == frame_pc_unwind (next_frame))
e7ee86a9
JB
294 return sp + 4;
295 return sp;
296 }
297
8e6bed05 298 pc = i386_linux_rt_sigtramp_start (next_frame);
e7ee86a9
JB
299 if (pc)
300 {
acd5c798
MK
301 CORE_ADDR ucontext_addr;
302
303 /* The sigcontext structure is part of the user context. A
304 pointer to the user context is passed as the third argument
305 to the signal handler. */
306 read_memory (sp + 8, buf, 4);
9fbfb822 307 ucontext_addr = extract_unsigned_integer (buf, 4);
acd5c798 308 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
e7ee86a9
JB
309 }
310
8a3fe4f8 311 error (_("Couldn't recognize signal trampoline."));
e7ee86a9
JB
312 return 0;
313}
314
6441c4a0
MK
315/* Set the program counter for process PTID to PC. */
316
8201327c 317static void
61a1198a 318i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
6441c4a0 319{
61a1198a 320 regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);
6441c4a0
MK
321
322 /* We must be careful with modifying the program counter. If we
323 just interrupted a system call, the kernel might try to restart
324 it when we resume the inferior. On restarting the system call,
325 the kernel will try backing up the program counter even though it
326 no longer points at the system call. This typically results in a
327 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
328 "orig_eax" pseudo-register.
329
330 Note that "orig_eax" is saved when setting up a dummy call frame.
331 This means that it is properly restored when that frame is
332 popped, and that the interrupted system call will be restarted
333 when we resume the inferior on return from a function call from
334 within GDB. In all other cases the system call will not be
335 restarted. */
61a1198a 336 regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
6441c4a0
MK
337}
338\f
8201327c 339
e9f1aad5
MK
340/* The register sets used in GNU/Linux ELF core-dumps are identical to
341 the register sets in `struct user' that are used for a.out
342 core-dumps. These are also used by ptrace(2). The corresponding
343 types are `elf_gregset_t' for the general-purpose registers (with
344 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
345 for the floating-point registers.
346
347 Those types used to be available under the names `gregset_t' and
348 `fpregset_t' too, and GDB used those names in the past. But those
349 names are now used for the register sets used in the `mcontext_t'
350 type, which have a different size and layout. */
351
352/* Mapping between the general-purpose registers in `struct user'
353 format and GDB's register cache layout. */
354
355/* From <sys/reg.h>. */
356static int i386_linux_gregset_reg_offset[] =
357{
358 6 * 4, /* %eax */
359 1 * 4, /* %ecx */
360 2 * 4, /* %edx */
361 0 * 4, /* %ebx */
362 15 * 4, /* %esp */
363 5 * 4, /* %ebp */
364 3 * 4, /* %esi */
365 4 * 4, /* %edi */
366 12 * 4, /* %eip */
367 14 * 4, /* %eflags */
368 13 * 4, /* %cs */
369 16 * 4, /* %ss */
370 7 * 4, /* %ds */
371 8 * 4, /* %es */
372 9 * 4, /* %fs */
373 10 * 4, /* %gs */
374 -1, -1, -1, -1, -1, -1, -1, -1,
375 -1, -1, -1, -1, -1, -1, -1, -1,
376 -1, -1, -1, -1, -1, -1, -1, -1,
377 -1,
378 11 * 4 /* "orig_eax" */
379};
380
381/* Mapping between the general-purpose registers in `struct
382 sigcontext' format and GDB's register cache layout. */
383
a3386186 384/* From <asm/sigcontext.h>. */
bb489b3c 385static int i386_linux_sc_reg_offset[] =
a3386186
MK
386{
387 11 * 4, /* %eax */
388 10 * 4, /* %ecx */
389 9 * 4, /* %edx */
390 8 * 4, /* %ebx */
391 7 * 4, /* %esp */
392 6 * 4, /* %ebp */
393 5 * 4, /* %esi */
394 4 * 4, /* %edi */
395 14 * 4, /* %eip */
396 16 * 4, /* %eflags */
397 15 * 4, /* %cs */
398 18 * 4, /* %ss */
399 3 * 4, /* %ds */
400 2 * 4, /* %es */
401 1 * 4, /* %fs */
402 0 * 4 /* %gs */
403};
404
8201327c
MK
405static void
406i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
407{
408 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
409
410 /* GNU/Linux uses ELF. */
411 i386_elf_init_abi (info, gdbarch);
412
8201327c
MK
413 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
414 to adjust a few things. */
415
416 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
bb489b3c 417 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
8201327c 418 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
38c968cf 419 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
8201327c 420
e9f1aad5
MK
421 tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
422 tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
423 tdep->sizeof_gregset = 17 * 4;
424
8201327c
MK
425 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
426
911bc6ee 427 tdep->sigtramp_p = i386_linux_sigtramp_p;
b7d15bf7 428 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
a3386186 429 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
bb489b3c 430 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
8201327c 431
203c3895
UW
432 /* N_FUN symbols in shared libaries have 0 for their values and need
433 to be relocated. */
434 set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
435
871fbe6a 436 /* GNU/Linux uses SVR4-style shared libraries. */
982e9687 437 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
871fbe6a
MK
438 set_solib_svr4_fetch_link_map_offsets
439 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
440
441 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
bb41a796 442 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
12b8a2cb
DJ
443
444 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
b2756930
KB
445
446 /* Enable TLS support. */
447 set_gdbarch_fetch_tls_load_module_address (gdbarch,
448 svr4_fetch_objfile_link_map);
8201327c
MK
449}
450
451/* Provide a prototype to silence -Wmissing-prototypes. */
452extern void _initialize_i386_linux_tdep (void);
453
454void
455_initialize_i386_linux_tdep (void)
456{
05816f70 457 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
8201327c
MK
458 i386_linux_init_abi);
459}
This page took 0.559254 seconds and 4 git commands to generate.