12a2ab9004d1c14cc04801abb9127a6964725067
[deliverable/binutils-gdb.git] / gdb / i386-linux-tdep.c
1 /* Target-dependent code for GNU/Linux running on i386's, for GDB.
2
3 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
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"
26 #include "regcache.h"
27 #include "inferior.h"
28
29 /* For i386_linux_skip_solib_resolver. */
30 #include "symtab.h"
31 #include "symfile.h"
32 #include "objfiles.h"
33
34 #include "solib-svr4.h" /* For struct link_map_offsets. */
35
36 #include "i386-tdep.h"
37 #include "i386-linux-tdep.h"
38
39 /* Return the name of register REG. */
40
41 static char *
42 i386_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 }
50
51 static int
52 i386_linux_register_byte (int reg)
53 {
54 /* Deal with the extra "orig_eax" pseudo register. */
55 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
56 return (i386_register_byte (I386_LINUX_ORIG_EAX_REGNUM - 1)
57 + i386_register_raw_size (I386_LINUX_ORIG_EAX_REGNUM - 1));
58
59 return i386_register_byte (reg);
60 }
61
62 static int
63 i386_linux_register_raw_size (int reg)
64 {
65 /* Deal with the extra "orig_eax" pseudo register. */
66 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
67 return 4;
68
69 return i386_register_raw_size (reg);
70 }
71 \f
72 /* Recognizing signal handler frames. */
73
74 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
75 "realtime" (RT) signals. The RT signals can provide additional
76 information to the signal handler if the SA_SIGINFO flag is set
77 when establishing a signal handler using `sigaction'. It is not
78 unlikely that future versions of GNU/Linux will support SA_SIGINFO
79 for normal signals too. */
80
81 /* When the i386 Linux kernel calls a signal handler and the
82 SA_RESTORER flag isn't set, the return address points to a bit of
83 code on the stack. This function returns whether the PC appears to
84 be within this bit of code.
85
86 The instruction sequence for normal signals is
87 pop %eax
88 mov $0x77,%eax
89 int $0x80
90 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
91
92 Checking for the code sequence should be somewhat reliable, because
93 the effect is to call the system call sigreturn. This is unlikely
94 to occur anywhere other than a signal trampoline.
95
96 It kind of sucks that we have to read memory from the process in
97 order to identify a signal trampoline, but there doesn't seem to be
98 any other way. The PC_IN_SIGTRAMP macro in tm-linux.h arranges to
99 only call us if no function name could be identified, which should
100 be the case since the code is on the stack.
101
102 Detection of signal trampolines for handlers that set the
103 SA_RESTORER flag is in general not possible. Unfortunately this is
104 what the GNU C Library has been doing for quite some time now.
105 However, as of version 2.1.2, the GNU C Library uses signal
106 trampolines (named __restore and __restore_rt) that are identical
107 to the ones used by the kernel. Therefore, these trampolines are
108 supported too. */
109
110 #define LINUX_SIGTRAMP_INSN0 (0x58) /* pop %eax */
111 #define LINUX_SIGTRAMP_OFFSET0 (0)
112 #define LINUX_SIGTRAMP_INSN1 (0xb8) /* mov $NNNN,%eax */
113 #define LINUX_SIGTRAMP_OFFSET1 (1)
114 #define LINUX_SIGTRAMP_INSN2 (0xcd) /* int */
115 #define LINUX_SIGTRAMP_OFFSET2 (6)
116
117 static const unsigned char linux_sigtramp_code[] =
118 {
119 LINUX_SIGTRAMP_INSN0, /* pop %eax */
120 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77,%eax */
121 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
122 };
123
124 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
125
126 /* If PC is in a sigtramp routine, return the address of the start of
127 the routine. Otherwise, return 0. */
128
129 static CORE_ADDR
130 i386_linux_sigtramp_start (CORE_ADDR pc)
131 {
132 unsigned char buf[LINUX_SIGTRAMP_LEN];
133
134 /* We only recognize a signal trampoline if PC is at the start of
135 one of the three instructions. We optimize for finding the PC at
136 the start, as will be the case when the trampoline is not the
137 first frame on the stack. We assume that in the case where the
138 PC is not at the start of the instruction sequence, there will be
139 a few trailing readable bytes on the stack. */
140
141 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
142 return 0;
143
144 if (buf[0] != LINUX_SIGTRAMP_INSN0)
145 {
146 int adjust;
147
148 switch (buf[0])
149 {
150 case LINUX_SIGTRAMP_INSN1:
151 adjust = LINUX_SIGTRAMP_OFFSET1;
152 break;
153 case LINUX_SIGTRAMP_INSN2:
154 adjust = LINUX_SIGTRAMP_OFFSET2;
155 break;
156 default:
157 return 0;
158 }
159
160 pc -= adjust;
161
162 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
163 return 0;
164 }
165
166 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
167 return 0;
168
169 return pc;
170 }
171
172 /* This function does the same for RT signals. Here the instruction
173 sequence is
174 mov $0xad,%eax
175 int $0x80
176 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
177
178 The effect is to call the system call rt_sigreturn. */
179
180 #define LINUX_RT_SIGTRAMP_INSN0 (0xb8) /* mov $NNNN,%eax */
181 #define LINUX_RT_SIGTRAMP_OFFSET0 (0)
182 #define LINUX_RT_SIGTRAMP_INSN1 (0xcd) /* int */
183 #define LINUX_RT_SIGTRAMP_OFFSET1 (5)
184
185 static const unsigned char linux_rt_sigtramp_code[] =
186 {
187 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad,%eax */
188 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
189 };
190
191 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
192
193 /* If PC is in a RT sigtramp routine, return the address of the start
194 of the routine. Otherwise, return 0. */
195
196 static CORE_ADDR
197 i386_linux_rt_sigtramp_start (CORE_ADDR pc)
198 {
199 unsigned char buf[LINUX_RT_SIGTRAMP_LEN];
200
201 /* We only recognize a signal trampoline if PC is at the start of
202 one of the two instructions. We optimize for finding the PC at
203 the start, as will be the case when the trampoline is not the
204 first frame on the stack. We assume that in the case where the
205 PC is not at the start of the instruction sequence, there will be
206 a few trailing readable bytes on the stack. */
207
208 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
209 return 0;
210
211 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
212 {
213 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
214 return 0;
215
216 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
217
218 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
219 return 0;
220 }
221
222 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
223 return 0;
224
225 return pc;
226 }
227
228 /* Return whether PC is in a GNU/Linux sigtramp routine. */
229
230 static int
231 i386_linux_pc_in_sigtramp (CORE_ADDR pc, char *name)
232 {
233 if (name)
234 return STREQ ("__restore", name) || STREQ ("__restore_rt", name);
235
236 return (i386_linux_sigtramp_start (pc) != 0
237 || i386_linux_rt_sigtramp_start (pc) != 0);
238 }
239
240 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
241 address of the associated sigcontext structure. */
242
243 CORE_ADDR
244 i386_linux_sigcontext_addr (struct frame_info *frame)
245 {
246 CORE_ADDR pc;
247
248 pc = i386_linux_sigtramp_start (frame->pc);
249 if (pc)
250 {
251 CORE_ADDR sp;
252
253 if (frame->next)
254 /* If this isn't the top frame, the next frame must be for the
255 signal handler itself. The sigcontext structure lives on
256 the stack, right after the signum argument. */
257 return frame->next->frame + 12;
258
259 /* This is the top frame. We'll have to find the address of the
260 sigcontext structure by looking at the stack pointer. Keep
261 in mind that the first instruction of the sigtramp code is
262 "pop %eax". If the PC is at this instruction, adjust the
263 returned value accordingly. */
264 sp = read_register (SP_REGNUM);
265 if (pc == frame->pc)
266 return sp + 4;
267 return sp;
268 }
269
270 pc = i386_linux_rt_sigtramp_start (frame->pc);
271 if (pc)
272 {
273 if (frame->next)
274 /* If this isn't the top frame, the next frame must be for the
275 signal handler itself. The sigcontext structure is part of
276 the user context. A pointer to the user context is passed
277 as the third argument to the signal handler. */
278 return read_memory_integer (frame->next->frame + 16, 4) + 20;
279
280 /* This is the top frame. Again, use the stack pointer to find
281 the address of the sigcontext structure. */
282 return read_memory_integer (read_register (SP_REGNUM) + 8, 4) + 20;
283 }
284
285 error ("Couldn't recognize signal trampoline.");
286 return 0;
287 }
288
289 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
290 #define LINUX_SIGCONTEXT_PC_OFFSET (56)
291
292 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
293 saved program counter. */
294
295 static CORE_ADDR
296 i386_linux_sigtramp_saved_pc (struct frame_info *frame)
297 {
298 CORE_ADDR addr;
299 addr = i386_linux_sigcontext_addr (frame);
300 return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 4);
301 }
302
303 /* Offset to saved SP in sigcontext, from <asm/sigcontext.h>. */
304 #define LINUX_SIGCONTEXT_SP_OFFSET (28)
305
306 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
307 saved stack pointer. */
308
309 static CORE_ADDR
310 i386_linux_sigtramp_saved_sp (struct frame_info *frame)
311 {
312 CORE_ADDR addr;
313 addr = i386_linux_sigcontext_addr (frame);
314 return read_memory_integer (addr + LINUX_SIGCONTEXT_SP_OFFSET, 4);
315 }
316
317 /* Signal trampolines don't have a meaningful frame. As in
318 "i386/tm-i386.h", the frame pointer value we use is actually the
319 frame pointer of the calling frame -- that is, the frame which was
320 in progress when the signal trampoline was entered. GDB mostly
321 treats this frame pointer value as a magic cookie. We detect the
322 case of a signal trampoline by looking at the SIGNAL_HANDLER_CALLER
323 field, which is set based on PC_IN_SIGTRAMP.
324
325 When a signal trampoline is invoked from a frameless function, we
326 essentially have two frameless functions in a row. In this case,
327 we use the same magic cookie for three frames in a row. We detect
328 this case by seeing whether the next frame has
329 SIGNAL_HANDLER_CALLER set, and, if it does, checking whether the
330 current frame is actually frameless. In this case, we need to get
331 the PC by looking at the SP register value stored in the signal
332 context.
333
334 This should work in most cases except in horrible situations where
335 a signal occurs just as we enter a function but before the frame
336 has been set up. */
337
338 #define FRAMELESS_SIGNAL(frame) \
339 ((frame)->next != NULL \
340 && (frame)->next->signal_handler_caller \
341 && frameless_look_for_prologue (frame))
342
343 CORE_ADDR
344 i386_linux_frame_chain (struct frame_info *frame)
345 {
346 if (frame->signal_handler_caller || FRAMELESS_SIGNAL (frame))
347 return frame->frame;
348
349 if (! inside_entry_file (frame->pc))
350 return read_memory_unsigned_integer (frame->frame, 4);
351
352 return 0;
353 }
354
355 /* Return the saved program counter for FRAME. */
356
357 CORE_ADDR
358 i386_linux_frame_saved_pc (struct frame_info *frame)
359 {
360 if (frame->signal_handler_caller)
361 return i386_linux_sigtramp_saved_pc (frame);
362
363 if (FRAMELESS_SIGNAL (frame))
364 {
365 CORE_ADDR sp = i386_linux_sigtramp_saved_sp (frame->next);
366 return read_memory_unsigned_integer (sp, 4);
367 }
368
369 return read_memory_unsigned_integer (frame->frame + 4, 4);
370 }
371
372 /* Immediately after a function call, return the saved pc. */
373
374 CORE_ADDR
375 i386_linux_saved_pc_after_call (struct frame_info *frame)
376 {
377 if (frame->signal_handler_caller)
378 return i386_linux_sigtramp_saved_pc (frame);
379
380 return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
381 }
382
383 /* Set the program counter for process PTID to PC. */
384
385 static void
386 i386_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
387 {
388 write_register_pid (PC_REGNUM, pc, ptid);
389
390 /* We must be careful with modifying the program counter. If we
391 just interrupted a system call, the kernel might try to restart
392 it when we resume the inferior. On restarting the system call,
393 the kernel will try backing up the program counter even though it
394 no longer points at the system call. This typically results in a
395 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
396 "orig_eax" pseudo-register.
397
398 Note that "orig_eax" is saved when setting up a dummy call frame.
399 This means that it is properly restored when that frame is
400 popped, and that the interrupted system call will be restarted
401 when we resume the inferior on return from a function call from
402 within GDB. In all other cases the system call will not be
403 restarted. */
404 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM, -1, ptid);
405 }
406 \f
407 /* Calling functions in shared libraries. */
408
409 /* Find the minimal symbol named NAME, and return both the minsym
410 struct and its objfile. This probably ought to be in minsym.c, but
411 everything there is trying to deal with things like C++ and
412 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
413 be considered too special-purpose for general consumption. */
414
415 static struct minimal_symbol *
416 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
417 {
418 struct objfile *objfile;
419
420 ALL_OBJFILES (objfile)
421 {
422 struct minimal_symbol *msym;
423
424 ALL_OBJFILE_MSYMBOLS (objfile, msym)
425 {
426 if (SYMBOL_NAME (msym)
427 && STREQ (SYMBOL_NAME (msym), name))
428 {
429 *objfile_p = objfile;
430 return msym;
431 }
432 }
433 }
434
435 return 0;
436 }
437
438 static CORE_ADDR
439 skip_hurd_resolver (CORE_ADDR pc)
440 {
441 /* The HURD dynamic linker is part of the GNU C library, so many
442 GNU/Linux distributions use it. (All ELF versions, as far as I
443 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
444 which calls "fixup" to patch the PLT, and then passes control to
445 the function.
446
447 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
448 the same objfile. If we are at the entry point of `fixup', then
449 we set a breakpoint at the return address (at the top of the
450 stack), and continue.
451
452 It's kind of gross to do all these checks every time we're
453 called, since they don't change once the executable has gotten
454 started. But this is only a temporary hack --- upcoming versions
455 of GNU/Linux will provide a portable, efficient interface for
456 debugging programs that use shared libraries. */
457
458 struct objfile *objfile;
459 struct minimal_symbol *resolver
460 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
461
462 if (resolver)
463 {
464 struct minimal_symbol *fixup
465 = lookup_minimal_symbol ("fixup", NULL, objfile);
466
467 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
468 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
469 }
470
471 return 0;
472 }
473
474 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
475 This function:
476 1) decides whether a PLT has sent us into the linker to resolve
477 a function reference, and
478 2) if so, tells us where to set a temporary breakpoint that will
479 trigger when the dynamic linker is done. */
480
481 CORE_ADDR
482 i386_linux_skip_solib_resolver (CORE_ADDR pc)
483 {
484 CORE_ADDR result;
485
486 /* Plug in functions for other kinds of resolvers here. */
487 result = skip_hurd_resolver (pc);
488 if (result)
489 return result;
490
491 return 0;
492 }
493
494 /* Fetch (and possibly build) an appropriate link_map_offsets
495 structure for native GNU/Linux x86 targets using the struct offsets
496 defined in link.h (but without actual reference to that file).
497
498 This makes it possible to access GNU/Linux x86 shared libraries
499 from a GDB that was not built on an GNU/Linux x86 host (for cross
500 debugging). */
501
502 static struct link_map_offsets *
503 i386_linux_svr4_fetch_link_map_offsets (void)
504 {
505 static struct link_map_offsets lmo;
506 static struct link_map_offsets *lmp = NULL;
507
508 if (lmp == NULL)
509 {
510 lmp = &lmo;
511
512 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but
513 this is all we need. */
514 lmo.r_map_offset = 4;
515 lmo.r_map_size = 4;
516
517 lmo.link_map_size = 20; /* The actual size is 552 bytes, but
518 this is all we need. */
519 lmo.l_addr_offset = 0;
520 lmo.l_addr_size = 4;
521
522 lmo.l_name_offset = 4;
523 lmo.l_name_size = 4;
524
525 lmo.l_next_offset = 12;
526 lmo.l_next_size = 4;
527
528 lmo.l_prev_offset = 16;
529 lmo.l_prev_size = 4;
530 }
531
532 return lmp;
533 }
534 \f
535
536 static void
537 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
538 {
539 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
540
541 /* GNU/Linux uses ELF. */
542 i386_elf_init_abi (info, gdbarch);
543
544 /* We support the SSE registers on GNU/Linux. */
545 tdep->num_xmm_regs = I386_NUM_XREGS - 1;
546 /* set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS); */
547
548 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
549 to adjust a few things. */
550
551 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
552 set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS + 1);
553 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
554 set_gdbarch_register_bytes (gdbarch, I386_SSE_SIZEOF_REGS + 4);
555 set_gdbarch_register_byte (gdbarch, i386_linux_register_byte);
556 set_gdbarch_register_raw_size (gdbarch, i386_linux_register_raw_size);
557
558 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
559
560 /* When the i386 Linux kernel calls a signal handler, the return
561 address points to a bit of code on the stack. These definitions
562 are used to identify this bit of code as a signal trampoline in
563 order to support backtracing through calls to signal handlers. */
564
565 set_gdbarch_pc_in_sigtramp (gdbarch, i386_linux_pc_in_sigtramp);
566 set_gdbarch_frame_chain (gdbarch, i386_linux_frame_chain);
567 set_gdbarch_frame_saved_pc (gdbarch, i386_linux_frame_saved_pc);
568 set_gdbarch_saved_pc_after_call (gdbarch, i386_linux_saved_pc_after_call);
569 tdep->sigtramp_saved_pc = i386_linux_sigtramp_saved_pc;
570
571 set_solib_svr4_fetch_link_map_offsets (gdbarch,
572 i386_linux_svr4_fetch_link_map_offsets);
573 }
574
575 /* Provide a prototype to silence -Wmissing-prototypes. */
576 extern void _initialize_i386_linux_tdep (void);
577
578 void
579 _initialize_i386_linux_tdep (void)
580 {
581 gdbarch_register_osabi (bfd_arch_i386, GDB_OSABI_LINUX,
582 i386_linux_init_abi);
583 }
This page took 0.041905 seconds and 4 git commands to generate.