2004-02-15 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / i386-linux-tdep.c
CommitLineData
ca557f44
AC
1/* Target-dependent code for GNU/Linux running on i386's, for GDB.
2
4be87837 3 Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
e7ee86a9
JB
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "gdbcore.h"
24#include "frame.h"
25#include "value.h"
4e052eda 26#include "regcache.h"
6441c4a0 27#include "inferior.h"
0670c0aa 28#include "osabi.h"
38c968cf 29#include "reggroups.h"
0670c0aa 30#include "solib-svr4.h"
e7ee86a9 31
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"
8201327c 37
6441c4a0
MK
38/* Return the name of register REG. */
39
16775908 40static const char *
6441c4a0
MK
41i386_linux_register_name (int reg)
42{
43 /* Deal with the extra "orig_eax" pseudo register. */
44 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
45 return "orig_eax";
46
47 return i386_register_name (reg);
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
86 to occur anywhere other than a signal trampoline.
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
d7bd68ca 90 any other way. The PC_IN_SIGTRAMP macro in tm-linux.h arranges to
e7ee86a9
JB
91 only call us if no function name could be identified, which should
92 be the case since the code is on the stack.
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
JB
108
109static const unsigned char linux_sigtramp_code[] =
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
118/* If PC is in a sigtramp routine, return the address of the start of
119 the routine. Otherwise, return 0. */
120
121static CORE_ADDR
122i386_linux_sigtramp_start (CORE_ADDR pc)
123{
124 unsigned char buf[LINUX_SIGTRAMP_LEN];
125
126 /* We only recognize a signal trampoline if PC is at the start of
127 one of the three instructions. We optimize for finding the PC at
128 the start, as will be the case when the trampoline is not the
129 first frame on the stack. We assume that in the case where the
130 PC is not at the start of the instruction sequence, there will be
131 a few trailing readable bytes on the stack. */
132
133 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
134 return 0;
135
136 if (buf[0] != LINUX_SIGTRAMP_INSN0)
137 {
138 int adjust;
139
140 switch (buf[0])
141 {
142 case LINUX_SIGTRAMP_INSN1:
143 adjust = LINUX_SIGTRAMP_OFFSET1;
144 break;
145 case LINUX_SIGTRAMP_INSN2:
146 adjust = LINUX_SIGTRAMP_OFFSET2;
147 break;
148 default:
149 return 0;
150 }
151
152 pc -= adjust;
153
154 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
155 return 0;
156 }
157
158 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
159 return 0;
160
161 return pc;
162}
163
164/* This function does the same for RT signals. Here the instruction
165 sequence is
acd5c798 166 mov $0xad, %eax
e7ee86a9
JB
167 int $0x80
168 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
169
170 The effect is to call the system call rt_sigreturn. */
171
acd5c798
MK
172#define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
173#define LINUX_RT_SIGTRAMP_OFFSET0 0
174#define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
175#define LINUX_RT_SIGTRAMP_OFFSET1 5
e7ee86a9
JB
176
177static const unsigned char linux_rt_sigtramp_code[] =
178{
acd5c798 179 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
e7ee86a9
JB
180 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
181};
182
183#define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
184
185/* If PC is in a RT sigtramp routine, return the address of the start
186 of the routine. Otherwise, return 0. */
187
188static CORE_ADDR
189i386_linux_rt_sigtramp_start (CORE_ADDR pc)
190{
191 unsigned char buf[LINUX_RT_SIGTRAMP_LEN];
192
193 /* We only recognize a signal trampoline if PC is at the start of
194 one of the two instructions. We optimize for finding the PC at
195 the start, as will be the case when the trampoline is not the
196 first frame on the stack. We assume that in the case where the
197 PC is not at the start of the instruction sequence, there will be
198 a few trailing readable bytes on the stack. */
199
200 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
201 return 0;
202
203 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
204 {
205 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
206 return 0;
207
208 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
209
210 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
211 return 0;
212 }
213
214 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
215 return 0;
216
217 return pc;
218}
219
ca557f44 220/* Return whether PC is in a GNU/Linux sigtramp routine. */
e7ee86a9 221
8201327c
MK
222static int
223i386_linux_pc_in_sigtramp (CORE_ADDR pc, char *name)
e7ee86a9 224{
ef17e74b
DJ
225 /* If we have NAME, we can optimize the search. The trampolines are
226 named __restore and __restore_rt. However, they aren't dynamically
227 exported from the shared C library, so the trampoline may appear to
228 be part of the preceding function. This should always be sigaction,
229 __sigaction, or __libc_sigaction (all aliases to the same function). */
230 if (name == NULL || strstr (name, "sigaction") != NULL)
231 return (i386_linux_sigtramp_start (pc) != 0
232 || i386_linux_rt_sigtramp_start (pc) != 0);
233
234 return (strcmp ("__restore", name) == 0
235 || strcmp ("__restore_rt", name) == 0);
e7ee86a9
JB
236}
237
acd5c798
MK
238/* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
239#define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
240
241/* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
242 routine, return the address of the associated sigcontext structure. */
e7ee86a9 243
b7d15bf7 244static CORE_ADDR
acd5c798 245i386_linux_sigcontext_addr (struct frame_info *next_frame)
e7ee86a9
JB
246{
247 CORE_ADDR pc;
acd5c798
MK
248 CORE_ADDR sp;
249 char buf[4];
250
c7f16359 251 frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
acd5c798 252 sp = extract_unsigned_integer (buf, 4);
e7ee86a9 253
acd5c798 254 pc = i386_linux_sigtramp_start (frame_pc_unwind (next_frame));
e7ee86a9
JB
255 if (pc)
256 {
acd5c798
MK
257 /* The sigcontext structure lives on the stack, right after
258 the signum argument. We determine the address of the
259 sigcontext structure by looking at the frame's stack
260 pointer. Keep in mind that the first instruction of the
261 sigtramp code is "pop %eax". If the PC is after this
262 instruction, adjust the returned value accordingly. */
263 if (pc == frame_pc_unwind (next_frame))
e7ee86a9
JB
264 return sp + 4;
265 return sp;
266 }
267
acd5c798 268 pc = i386_linux_rt_sigtramp_start (frame_pc_unwind (next_frame));
e7ee86a9
JB
269 if (pc)
270 {
acd5c798
MK
271 CORE_ADDR ucontext_addr;
272
273 /* The sigcontext structure is part of the user context. A
274 pointer to the user context is passed as the third argument
275 to the signal handler. */
276 read_memory (sp + 8, buf, 4);
9fbfb822 277 ucontext_addr = extract_unsigned_integer (buf, 4);
acd5c798 278 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
e7ee86a9
JB
279 }
280
281 error ("Couldn't recognize signal trampoline.");
282 return 0;
283}
284
6441c4a0
MK
285/* Set the program counter for process PTID to PC. */
286
8201327c 287static void
6441c4a0
MK
288i386_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
289{
c7f16359 290 write_register_pid (I386_EIP_REGNUM, pc, ptid);
6441c4a0
MK
291
292 /* We must be careful with modifying the program counter. If we
293 just interrupted a system call, the kernel might try to restart
294 it when we resume the inferior. On restarting the system call,
295 the kernel will try backing up the program counter even though it
296 no longer points at the system call. This typically results in a
297 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
298 "orig_eax" pseudo-register.
299
300 Note that "orig_eax" is saved when setting up a dummy call frame.
301 This means that it is properly restored when that frame is
302 popped, and that the interrupted system call will be restarted
303 when we resume the inferior on return from a function call from
304 within GDB. In all other cases the system call will not be
305 restarted. */
306 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM, -1, ptid);
307}
308\f
305d65ca 309/* Fetch (and possibly build) an appropriate link_map_offsets
ca557f44 310 structure for native GNU/Linux x86 targets using the struct offsets
305d65ca 311 defined in link.h (but without actual reference to that file).
1a8629c7 312
ca557f44
AC
313 This makes it possible to access GNU/Linux x86 shared libraries
314 from a GDB that was not built on an GNU/Linux x86 host (for cross
315 debugging). */
1a8629c7 316
8201327c 317static struct link_map_offsets *
1a8629c7
MS
318i386_linux_svr4_fetch_link_map_offsets (void)
319{
320 static struct link_map_offsets lmo;
305d65ca 321 static struct link_map_offsets *lmp = NULL;
1a8629c7 322
305d65ca 323 if (lmp == NULL)
1a8629c7
MS
324 {
325 lmp = &lmo;
326
305d65ca
MK
327 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but
328 this is all we need. */
1a8629c7
MS
329 lmo.r_map_offset = 4;
330 lmo.r_map_size = 4;
331
305d65ca
MK
332 lmo.link_map_size = 20; /* The actual size is 552 bytes, but
333 this is all we need. */
1a8629c7
MS
334 lmo.l_addr_offset = 0;
335 lmo.l_addr_size = 4;
336
337 lmo.l_name_offset = 4;
338 lmo.l_name_size = 4;
339
340 lmo.l_next_offset = 12;
341 lmo.l_next_size = 4;
342
343 lmo.l_prev_offset = 16;
344 lmo.l_prev_size = 4;
345 }
346
305d65ca 347 return lmp;
1a8629c7 348}
8201327c
MK
349\f
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
b7d15bf7 438 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
a3386186 439 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
bb489b3c 440 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
8201327c 441
b7d15bf7
MK
442 /* When the i386 Linux kernel calls a signal handler, the return
443 address points to a bit of code on the stack. This function is
444 used to identify this bit of code as a signal trampoline in order
445 to support backtracing through calls to signal handlers. */
8201327c 446 set_gdbarch_pc_in_sigtramp (gdbarch, i386_linux_pc_in_sigtramp);
8201327c 447
bb41a796 448 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
8201327c
MK
449 set_solib_svr4_fetch_link_map_offsets (gdbarch,
450 i386_linux_svr4_fetch_link_map_offsets);
451}
452
453/* Provide a prototype to silence -Wmissing-prototypes. */
454extern void _initialize_i386_linux_tdep (void);
455
456void
457_initialize_i386_linux_tdep (void)
458{
05816f70 459 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
8201327c
MK
460 i386_linux_init_abi);
461}
This page took 0.348555 seconds and 4 git commands to generate.