* elfxx-mips.c (_bfd_mips_elf_discard_info): Correct loop index.
[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"
38c968cf 28#include "reggroups.h"
e7ee86a9 29
bafda96e
MS
30/* For i386_linux_skip_solib_resolver. */
31#include "symtab.h"
32#include "symfile.h"
33#include "objfiles.h"
305d65ca
MK
34
35#include "solib-svr4.h" /* For struct link_map_offsets. */
bafda96e 36
4be87837
DJ
37#include "osabi.h"
38
8201327c
MK
39#include "i386-tdep.h"
40#include "i386-linux-tdep.h"
41
6441c4a0
MK
42/* Return the name of register REG. */
43
16775908 44static const char *
6441c4a0
MK
45i386_linux_register_name (int reg)
46{
47 /* Deal with the extra "orig_eax" pseudo register. */
48 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
49 return "orig_eax";
50
51 return i386_register_name (reg);
52}
38c968cf
AC
53
54/* Return non-zero, when the register is in the corresponding register
55 group. Put the LINUX_ORIG_EAX register in the system group. */
56static int
57i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
58 struct reggroup *group)
59{
60 if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
61 return (group == system_reggroup
62 || group == save_reggroup
63 || group == restore_reggroup);
64 return i386_register_reggroup_p (gdbarch, regnum, group);
65}
66
e7ee86a9
JB
67\f
68/* Recognizing signal handler frames. */
69
ca557f44 70/* GNU/Linux has two flavors of signals. Normal signal handlers, and
e7ee86a9
JB
71 "realtime" (RT) signals. The RT signals can provide additional
72 information to the signal handler if the SA_SIGINFO flag is set
73 when establishing a signal handler using `sigaction'. It is not
ca557f44
AC
74 unlikely that future versions of GNU/Linux will support SA_SIGINFO
75 for normal signals too. */
e7ee86a9
JB
76
77/* When the i386 Linux kernel calls a signal handler and the
78 SA_RESTORER flag isn't set, the return address points to a bit of
79 code on the stack. This function returns whether the PC appears to
80 be within this bit of code.
81
82 The instruction sequence for normal signals is
83 pop %eax
84 mov $0x77,%eax
85 int $0x80
86 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
87
88 Checking for the code sequence should be somewhat reliable, because
89 the effect is to call the system call sigreturn. This is unlikely
90 to occur anywhere other than a signal trampoline.
91
92 It kind of sucks that we have to read memory from the process in
93 order to identify a signal trampoline, but there doesn't seem to be
d7bd68ca 94 any other way. The PC_IN_SIGTRAMP macro in tm-linux.h arranges to
e7ee86a9
JB
95 only call us if no function name could be identified, which should
96 be the case since the code is on the stack.
97
98 Detection of signal trampolines for handlers that set the
99 SA_RESTORER flag is in general not possible. Unfortunately this is
100 what the GNU C Library has been doing for quite some time now.
101 However, as of version 2.1.2, the GNU C Library uses signal
102 trampolines (named __restore and __restore_rt) that are identical
103 to the ones used by the kernel. Therefore, these trampolines are
104 supported too. */
105
106#define LINUX_SIGTRAMP_INSN0 (0x58) /* pop %eax */
107#define LINUX_SIGTRAMP_OFFSET0 (0)
108#define LINUX_SIGTRAMP_INSN1 (0xb8) /* mov $NNNN,%eax */
109#define LINUX_SIGTRAMP_OFFSET1 (1)
110#define LINUX_SIGTRAMP_INSN2 (0xcd) /* int */
111#define LINUX_SIGTRAMP_OFFSET2 (6)
112
113static const unsigned char linux_sigtramp_code[] =
114{
115 LINUX_SIGTRAMP_INSN0, /* pop %eax */
116 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77,%eax */
117 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
118};
119
120#define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
121
122/* If PC is in a sigtramp routine, return the address of the start of
123 the routine. Otherwise, return 0. */
124
125static CORE_ADDR
126i386_linux_sigtramp_start (CORE_ADDR pc)
127{
128 unsigned char buf[LINUX_SIGTRAMP_LEN];
129
130 /* We only recognize a signal trampoline if PC is at the start of
131 one of the three instructions. We optimize for finding the PC at
132 the start, as will be the case when the trampoline is not the
133 first frame on the stack. We assume that in the case where the
134 PC is not at the start of the instruction sequence, there will be
135 a few trailing readable bytes on the stack. */
136
137 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
138 return 0;
139
140 if (buf[0] != LINUX_SIGTRAMP_INSN0)
141 {
142 int adjust;
143
144 switch (buf[0])
145 {
146 case LINUX_SIGTRAMP_INSN1:
147 adjust = LINUX_SIGTRAMP_OFFSET1;
148 break;
149 case LINUX_SIGTRAMP_INSN2:
150 adjust = LINUX_SIGTRAMP_OFFSET2;
151 break;
152 default:
153 return 0;
154 }
155
156 pc -= adjust;
157
158 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
159 return 0;
160 }
161
162 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
163 return 0;
164
165 return pc;
166}
167
168/* This function does the same for RT signals. Here the instruction
169 sequence is
170 mov $0xad,%eax
171 int $0x80
172 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
173
174 The effect is to call the system call rt_sigreturn. */
175
176#define LINUX_RT_SIGTRAMP_INSN0 (0xb8) /* mov $NNNN,%eax */
177#define LINUX_RT_SIGTRAMP_OFFSET0 (0)
178#define LINUX_RT_SIGTRAMP_INSN1 (0xcd) /* int */
179#define LINUX_RT_SIGTRAMP_OFFSET1 (5)
180
181static const unsigned char linux_rt_sigtramp_code[] =
182{
183 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad,%eax */
184 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
185};
186
187#define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
188
189/* If PC is in a RT sigtramp routine, return the address of the start
190 of the routine. Otherwise, return 0. */
191
192static CORE_ADDR
193i386_linux_rt_sigtramp_start (CORE_ADDR pc)
194{
195 unsigned char buf[LINUX_RT_SIGTRAMP_LEN];
196
197 /* We only recognize a signal trampoline if PC is at the start of
198 one of the two instructions. We optimize for finding the PC at
199 the start, as will be the case when the trampoline is not the
200 first frame on the stack. We assume that in the case where the
201 PC is not at the start of the instruction sequence, there will be
202 a few trailing readable bytes on the stack. */
203
204 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
205 return 0;
206
207 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
208 {
209 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
210 return 0;
211
212 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
213
214 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
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
ca557f44 224/* Return whether PC is in a GNU/Linux sigtramp routine. */
e7ee86a9 225
8201327c
MK
226static int
227i386_linux_pc_in_sigtramp (CORE_ADDR pc, char *name)
e7ee86a9 228{
ef17e74b
DJ
229 /* If we have NAME, we can optimize the search. The trampolines are
230 named __restore and __restore_rt. However, they aren't dynamically
231 exported from the shared C library, so the trampoline may appear to
232 be part of the preceding function. This should always be sigaction,
233 __sigaction, or __libc_sigaction (all aliases to the same function). */
234 if (name == NULL || strstr (name, "sigaction") != NULL)
235 return (i386_linux_sigtramp_start (pc) != 0
236 || i386_linux_rt_sigtramp_start (pc) != 0);
237
238 return (strcmp ("__restore", name) == 0
239 || strcmp ("__restore_rt", name) == 0);
e7ee86a9
JB
240}
241
ca557f44
AC
242/* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
243 address of the associated sigcontext structure. */
e7ee86a9 244
b7d15bf7 245static CORE_ADDR
e7ee86a9
JB
246i386_linux_sigcontext_addr (struct frame_info *frame)
247{
248 CORE_ADDR pc;
249
8d357cca 250 pc = i386_linux_sigtramp_start (get_frame_pc (frame));
e7ee86a9
JB
251 if (pc)
252 {
253 CORE_ADDR sp;
254
8d357cca 255 if (get_next_frame (frame))
e7ee86a9
JB
256 /* If this isn't the top frame, the next frame must be for the
257 signal handler itself. The sigcontext structure lives on
258 the stack, right after the signum argument. */
8d357cca 259 return get_frame_base (get_next_frame (frame)) + 12;
e7ee86a9
JB
260
261 /* This is the top frame. We'll have to find the address of the
262 sigcontext structure by looking at the stack pointer. Keep
263 in mind that the first instruction of the sigtramp code is
264 "pop %eax". If the PC is at this instruction, adjust the
265 returned value accordingly. */
266 sp = read_register (SP_REGNUM);
8d357cca 267 if (pc == get_frame_pc (frame))
e7ee86a9
JB
268 return sp + 4;
269 return sp;
270 }
271
8d357cca 272 pc = i386_linux_rt_sigtramp_start (get_frame_pc (frame));
e7ee86a9
JB
273 if (pc)
274 {
8d357cca 275 if (get_next_frame (frame))
e7ee86a9
JB
276 /* If this isn't the top frame, the next frame must be for the
277 signal handler itself. The sigcontext structure is part of
278 the user context. A pointer to the user context is passed
279 as the third argument to the signal handler. */
8d357cca
AC
280 return read_memory_integer (get_frame_base (get_next_frame (frame))
281 + 16, 4) + 20;
e7ee86a9
JB
282
283 /* This is the top frame. Again, use the stack pointer to find
284 the address of the sigcontext structure. */
285 return read_memory_integer (read_register (SP_REGNUM) + 8, 4) + 20;
286 }
287
288 error ("Couldn't recognize signal trampoline.");
289 return 0;
290}
291
6441c4a0
MK
292/* Set the program counter for process PTID to PC. */
293
8201327c 294static void
6441c4a0
MK
295i386_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
296{
297 write_register_pid (PC_REGNUM, pc, ptid);
298
299 /* We must be careful with modifying the program counter. If we
300 just interrupted a system call, the kernel might try to restart
301 it when we resume the inferior. On restarting the system call,
302 the kernel will try backing up the program counter even though it
303 no longer points at the system call. This typically results in a
304 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
305 "orig_eax" pseudo-register.
306
307 Note that "orig_eax" is saved when setting up a dummy call frame.
308 This means that it is properly restored when that frame is
309 popped, and that the interrupted system call will be restarted
310 when we resume the inferior on return from a function call from
311 within GDB. In all other cases the system call will not be
312 restarted. */
313 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM, -1, ptid);
314}
315\f
bafda96e 316/* Calling functions in shared libraries. */
6441c4a0 317
bafda96e
MS
318/* Find the minimal symbol named NAME, and return both the minsym
319 struct and its objfile. This probably ought to be in minsym.c, but
320 everything there is trying to deal with things like C++ and
321 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
322 be considered too special-purpose for general consumption. */
323
324static struct minimal_symbol *
325find_minsym_and_objfile (char *name, struct objfile **objfile_p)
326{
327 struct objfile *objfile;
328
329 ALL_OBJFILES (objfile)
330 {
331 struct minimal_symbol *msym;
332
333 ALL_OBJFILE_MSYMBOLS (objfile, msym)
334 {
eadc1c87
MK
335 if (SYMBOL_LINKAGE_NAME (msym)
336 && strcmp (SYMBOL_LINKAGE_NAME (msym), name) == 0)
bafda96e
MS
337 {
338 *objfile_p = objfile;
339 return msym;
340 }
341 }
342 }
343
344 return 0;
345}
346
347static CORE_ADDR
348skip_hurd_resolver (CORE_ADDR pc)
349{
350 /* The HURD dynamic linker is part of the GNU C library, so many
351 GNU/Linux distributions use it. (All ELF versions, as far as I
352 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
353 which calls "fixup" to patch the PLT, and then passes control to
354 the function.
355
356 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
357 the same objfile. If we are at the entry point of `fixup', then
358 we set a breakpoint at the return address (at the top of the
359 stack), and continue.
360
361 It's kind of gross to do all these checks every time we're
362 called, since they don't change once the executable has gotten
363 started. But this is only a temporary hack --- upcoming versions
ca557f44 364 of GNU/Linux will provide a portable, efficient interface for
bafda96e
MS
365 debugging programs that use shared libraries. */
366
367 struct objfile *objfile;
368 struct minimal_symbol *resolver
369 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
370
371 if (resolver)
372 {
373 struct minimal_symbol *fixup
9b27852e 374 = lookup_minimal_symbol ("fixup", NULL, objfile);
bafda96e
MS
375
376 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
6913c89a 377 return (DEPRECATED_SAVED_PC_AFTER_CALL (get_current_frame ()));
bafda96e
MS
378 }
379
380 return 0;
381}
382
383/* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
384 This function:
385 1) decides whether a PLT has sent us into the linker to resolve
386 a function reference, and
387 2) if so, tells us where to set a temporary breakpoint that will
388 trigger when the dynamic linker is done. */
389
390CORE_ADDR
391i386_linux_skip_solib_resolver (CORE_ADDR pc)
392{
393 CORE_ADDR result;
394
395 /* Plug in functions for other kinds of resolvers here. */
396 result = skip_hurd_resolver (pc);
397 if (result)
398 return result;
399
400 return 0;
401}
1a8629c7 402
305d65ca 403/* Fetch (and possibly build) an appropriate link_map_offsets
ca557f44 404 structure for native GNU/Linux x86 targets using the struct offsets
305d65ca 405 defined in link.h (but without actual reference to that file).
1a8629c7 406
ca557f44
AC
407 This makes it possible to access GNU/Linux x86 shared libraries
408 from a GDB that was not built on an GNU/Linux x86 host (for cross
409 debugging). */
1a8629c7 410
8201327c 411static struct link_map_offsets *
1a8629c7
MS
412i386_linux_svr4_fetch_link_map_offsets (void)
413{
414 static struct link_map_offsets lmo;
305d65ca 415 static struct link_map_offsets *lmp = NULL;
1a8629c7 416
305d65ca 417 if (lmp == NULL)
1a8629c7
MS
418 {
419 lmp = &lmo;
420
305d65ca
MK
421 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but
422 this is all we need. */
1a8629c7
MS
423 lmo.r_map_offset = 4;
424 lmo.r_map_size = 4;
425
305d65ca
MK
426 lmo.link_map_size = 20; /* The actual size is 552 bytes, but
427 this is all we need. */
1a8629c7
MS
428 lmo.l_addr_offset = 0;
429 lmo.l_addr_size = 4;
430
431 lmo.l_name_offset = 4;
432 lmo.l_name_size = 4;
433
434 lmo.l_next_offset = 12;
435 lmo.l_next_size = 4;
436
437 lmo.l_prev_offset = 16;
438 lmo.l_prev_size = 4;
439 }
440
305d65ca 441 return lmp;
1a8629c7 442}
8201327c
MK
443\f
444
445static void
446i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
447{
448 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
449
450 /* GNU/Linux uses ELF. */
451 i386_elf_init_abi (info, gdbarch);
452
453 /* We support the SSE registers on GNU/Linux. */
454 tdep->num_xmm_regs = I386_NUM_XREGS - 1;
455 /* set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS); */
456
457 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
458 to adjust a few things. */
459
460 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
461 set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS + 1);
462 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
38c968cf 463 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
b8b527c5 464 set_gdbarch_deprecated_register_bytes (gdbarch, I386_SSE_SIZEOF_REGS + 4);
8201327c
MK
465
466 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
467
b7d15bf7
MK
468 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
469 tdep->sc_pc_offset = 14 * 4; /* From <asm/sigcontext.h>. */
470 tdep->sc_sp_offset = 7 * 4;
8201327c 471
b7d15bf7
MK
472 /* When the i386 Linux kernel calls a signal handler, the return
473 address points to a bit of code on the stack. This function is
474 used to identify this bit of code as a signal trampoline in order
475 to support backtracing through calls to signal handlers. */
8201327c 476 set_gdbarch_pc_in_sigtramp (gdbarch, i386_linux_pc_in_sigtramp);
8201327c
MK
477
478 set_solib_svr4_fetch_link_map_offsets (gdbarch,
479 i386_linux_svr4_fetch_link_map_offsets);
480}
481
482/* Provide a prototype to silence -Wmissing-prototypes. */
483extern void _initialize_i386_linux_tdep (void);
484
485void
486_initialize_i386_linux_tdep (void)
487{
05816f70 488 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
8201327c
MK
489 i386_linux_init_abi);
490}
This page took 0.291713 seconds and 4 git commands to generate.