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