1 /* Target-dependent code for GNU/Linux running on i386's, for GDB.
3 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
29 /* For i386_linux_skip_solib_resolver. */
34 #include "solib-svr4.h" /* For struct link_map_offsets. */
36 /* Return the name of register REG. */
39 i386_linux_register_name (int reg
)
41 /* Deal with the extra "orig_eax" pseudo register. */
42 if (reg
== I386_LINUX_ORIG_EAX_REGNUM
)
45 return i386_register_name (reg
);
49 i386_linux_register_byte (int reg
)
51 /* Deal with the extra "orig_eax" pseudo register. */
52 if (reg
== I386_LINUX_ORIG_EAX_REGNUM
)
53 return (i386_register_byte (I386_LINUX_ORIG_EAX_REGNUM
- 1)
54 + i386_register_raw_size (I386_LINUX_ORIG_EAX_REGNUM
- 1));
56 return i386_register_byte (reg
);
60 i386_linux_register_raw_size (int reg
)
62 /* Deal with the extra "orig_eax" pseudo register. */
63 if (reg
== I386_LINUX_ORIG_EAX_REGNUM
)
66 return i386_register_raw_size (reg
);
69 /* Recognizing signal handler frames. */
71 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
72 "realtime" (RT) signals. The RT signals can provide additional
73 information to the signal handler if the SA_SIGINFO flag is set
74 when establishing a signal handler using `sigaction'. It is not
75 unlikely that future versions of GNU/Linux will support SA_SIGINFO
76 for normal signals too. */
78 /* When the i386 Linux kernel calls a signal handler and the
79 SA_RESTORER flag isn't set, the return address points to a bit of
80 code on the stack. This function returns whether the PC appears to
81 be within this bit of code.
83 The instruction sequence for normal signals is
87 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
89 Checking for the code sequence should be somewhat reliable, because
90 the effect is to call the system call sigreturn. This is unlikely
91 to occur anywhere other than a signal trampoline.
93 It kind of sucks that we have to read memory from the process in
94 order to identify a signal trampoline, but there doesn't seem to be
95 any other way. The IN_SIGTRAMP macro in tm-linux.h arranges to
96 only call us if no function name could be identified, which should
97 be the case since the code is on the stack.
99 Detection of signal trampolines for handlers that set the
100 SA_RESTORER flag is in general not possible. Unfortunately this is
101 what the GNU C Library has been doing for quite some time now.
102 However, as of version 2.1.2, the GNU C Library uses signal
103 trampolines (named __restore and __restore_rt) that are identical
104 to the ones used by the kernel. Therefore, these trampolines are
107 #define LINUX_SIGTRAMP_INSN0 (0x58) /* pop %eax */
108 #define LINUX_SIGTRAMP_OFFSET0 (0)
109 #define LINUX_SIGTRAMP_INSN1 (0xb8) /* mov $NNNN,%eax */
110 #define LINUX_SIGTRAMP_OFFSET1 (1)
111 #define LINUX_SIGTRAMP_INSN2 (0xcd) /* int */
112 #define LINUX_SIGTRAMP_OFFSET2 (6)
114 static const unsigned char linux_sigtramp_code
[] =
116 LINUX_SIGTRAMP_INSN0
, /* pop %eax */
117 LINUX_SIGTRAMP_INSN1
, 0x77, 0x00, 0x00, 0x00, /* mov $0x77,%eax */
118 LINUX_SIGTRAMP_INSN2
, 0x80 /* int $0x80 */
121 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
123 /* If PC is in a sigtramp routine, return the address of the start of
124 the routine. Otherwise, return 0. */
127 i386_linux_sigtramp_start (CORE_ADDR pc
)
129 unsigned char buf
[LINUX_SIGTRAMP_LEN
];
131 /* We only recognize a signal trampoline if PC is at the start of
132 one of the three instructions. We optimize for finding the PC at
133 the start, as will be the case when the trampoline is not the
134 first frame on the stack. We assume that in the case where the
135 PC is not at the start of the instruction sequence, there will be
136 a few trailing readable bytes on the stack. */
138 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SIGTRAMP_LEN
) != 0)
141 if (buf
[0] != LINUX_SIGTRAMP_INSN0
)
147 case LINUX_SIGTRAMP_INSN1
:
148 adjust
= LINUX_SIGTRAMP_OFFSET1
;
150 case LINUX_SIGTRAMP_INSN2
:
151 adjust
= LINUX_SIGTRAMP_OFFSET2
;
159 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SIGTRAMP_LEN
) != 0)
163 if (memcmp (buf
, linux_sigtramp_code
, LINUX_SIGTRAMP_LEN
) != 0)
169 /* This function does the same for RT signals. Here the instruction
173 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
175 The effect is to call the system call rt_sigreturn. */
177 #define LINUX_RT_SIGTRAMP_INSN0 (0xb8) /* mov $NNNN,%eax */
178 #define LINUX_RT_SIGTRAMP_OFFSET0 (0)
179 #define LINUX_RT_SIGTRAMP_INSN1 (0xcd) /* int */
180 #define LINUX_RT_SIGTRAMP_OFFSET1 (5)
182 static const unsigned char linux_rt_sigtramp_code
[] =
184 LINUX_RT_SIGTRAMP_INSN0
, 0xad, 0x00, 0x00, 0x00, /* mov $0xad,%eax */
185 LINUX_RT_SIGTRAMP_INSN1
, 0x80 /* int $0x80 */
188 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
190 /* If PC is in a RT sigtramp routine, return the address of the start
191 of the routine. Otherwise, return 0. */
194 i386_linux_rt_sigtramp_start (CORE_ADDR pc
)
196 unsigned char buf
[LINUX_RT_SIGTRAMP_LEN
];
198 /* We only recognize a signal trampoline if PC is at the start of
199 one of the two instructions. We optimize for finding the PC at
200 the start, as will be the case when the trampoline is not the
201 first frame on the stack. We assume that in the case where the
202 PC is not at the start of the instruction sequence, there will be
203 a few trailing readable bytes on the stack. */
205 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_RT_SIGTRAMP_LEN
) != 0)
208 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN0
)
210 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN1
)
213 pc
-= LINUX_RT_SIGTRAMP_OFFSET1
;
215 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_RT_SIGTRAMP_LEN
) != 0)
219 if (memcmp (buf
, linux_rt_sigtramp_code
, LINUX_RT_SIGTRAMP_LEN
) != 0)
225 /* Return whether PC is in a GNU/Linux sigtramp routine. */
228 i386_linux_in_sigtramp (CORE_ADDR pc
, char *name
)
231 return STREQ ("__restore", name
) || STREQ ("__restore_rt", name
);
233 return (i386_linux_sigtramp_start (pc
) != 0
234 || i386_linux_rt_sigtramp_start (pc
) != 0);
237 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
238 address of the associated sigcontext structure. */
241 i386_linux_sigcontext_addr (struct frame_info
*frame
)
245 pc
= i386_linux_sigtramp_start (frame
->pc
);
251 /* If this isn't the top frame, the next frame must be for the
252 signal handler itself. The sigcontext structure lives on
253 the stack, right after the signum argument. */
254 return frame
->next
->frame
+ 12;
256 /* This is the top frame. We'll have to find the address of the
257 sigcontext structure by looking at the stack pointer. Keep
258 in mind that the first instruction of the sigtramp code is
259 "pop %eax". If the PC is at this instruction, adjust the
260 returned value accordingly. */
261 sp
= read_register (SP_REGNUM
);
267 pc
= i386_linux_rt_sigtramp_start (frame
->pc
);
271 /* If this isn't the top frame, the next frame must be for the
272 signal handler itself. The sigcontext structure is part of
273 the user context. A pointer to the user context is passed
274 as the third argument to the signal handler. */
275 return read_memory_integer (frame
->next
->frame
+ 16, 4) + 20;
277 /* This is the top frame. Again, use the stack pointer to find
278 the address of the sigcontext structure. */
279 return read_memory_integer (read_register (SP_REGNUM
) + 8, 4) + 20;
282 error ("Couldn't recognize signal trampoline.");
286 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
287 #define LINUX_SIGCONTEXT_PC_OFFSET (56)
289 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
290 saved program counter. */
293 i386_linux_sigtramp_saved_pc (struct frame_info
*frame
)
296 addr
= i386_linux_sigcontext_addr (frame
);
297 return read_memory_integer (addr
+ LINUX_SIGCONTEXT_PC_OFFSET
, 4);
300 /* Offset to saved SP in sigcontext, from <asm/sigcontext.h>. */
301 #define LINUX_SIGCONTEXT_SP_OFFSET (28)
303 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
304 saved stack pointer. */
307 i386_linux_sigtramp_saved_sp (struct frame_info
*frame
)
310 addr
= i386_linux_sigcontext_addr (frame
);
311 return read_memory_integer (addr
+ LINUX_SIGCONTEXT_SP_OFFSET
, 4);
314 /* Signal trampolines don't have a meaningful frame. As in
315 "i386/tm-i386.h", the frame pointer value we use is actually the
316 frame pointer of the calling frame -- that is, the frame which was
317 in progress when the signal trampoline was entered. GDB mostly
318 treats this frame pointer value as a magic cookie. We detect the
319 case of a signal trampoline by looking at the SIGNAL_HANDLER_CALLER
320 field, which is set based on IN_SIGTRAMP.
322 When a signal trampoline is invoked from a frameless function, we
323 essentially have two frameless functions in a row. In this case,
324 we use the same magic cookie for three frames in a row. We detect
325 this case by seeing whether the next frame has
326 SIGNAL_HANDLER_CALLER set, and, if it does, checking whether the
327 current frame is actually frameless. In this case, we need to get
328 the PC by looking at the SP register value stored in the signal
331 This should work in most cases except in horrible situations where
332 a signal occurs just as we enter a function but before the frame
335 #define FRAMELESS_SIGNAL(frame) \
336 ((frame)->next != NULL \
337 && (frame)->next->signal_handler_caller \
338 && frameless_look_for_prologue (frame))
341 i386_linux_frame_chain (struct frame_info
*frame
)
343 if (frame
->signal_handler_caller
|| FRAMELESS_SIGNAL (frame
))
346 if (! inside_entry_file (frame
->pc
))
347 return read_memory_unsigned_integer (frame
->frame
, 4);
352 /* Return the saved program counter for FRAME. */
355 i386_linux_frame_saved_pc (struct frame_info
*frame
)
357 if (frame
->signal_handler_caller
)
358 return i386_linux_sigtramp_saved_pc (frame
);
360 if (FRAMELESS_SIGNAL (frame
))
362 CORE_ADDR sp
= i386_linux_sigtramp_saved_sp (frame
->next
);
363 return read_memory_unsigned_integer (sp
, 4);
366 return read_memory_unsigned_integer (frame
->frame
+ 4, 4);
369 /* Immediately after a function call, return the saved pc. */
372 i386_linux_saved_pc_after_call (struct frame_info
*frame
)
374 if (frame
->signal_handler_caller
)
375 return i386_linux_sigtramp_saved_pc (frame
);
377 return read_memory_unsigned_integer (read_register (SP_REGNUM
), 4);
380 /* Set the program counter for process PTID to PC. */
383 i386_linux_write_pc (CORE_ADDR pc
, ptid_t ptid
)
385 write_register_pid (PC_REGNUM
, pc
, ptid
);
387 /* We must be careful with modifying the program counter. If we
388 just interrupted a system call, the kernel might try to restart
389 it when we resume the inferior. On restarting the system call,
390 the kernel will try backing up the program counter even though it
391 no longer points at the system call. This typically results in a
392 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
393 "orig_eax" pseudo-register.
395 Note that "orig_eax" is saved when setting up a dummy call frame.
396 This means that it is properly restored when that frame is
397 popped, and that the interrupted system call will be restarted
398 when we resume the inferior on return from a function call from
399 within GDB. In all other cases the system call will not be
401 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM
, -1, ptid
);
404 /* Calling functions in shared libraries. */
406 /* Find the minimal symbol named NAME, and return both the minsym
407 struct and its objfile. This probably ought to be in minsym.c, but
408 everything there is trying to deal with things like C++ and
409 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
410 be considered too special-purpose for general consumption. */
412 static struct minimal_symbol
*
413 find_minsym_and_objfile (char *name
, struct objfile
**objfile_p
)
415 struct objfile
*objfile
;
417 ALL_OBJFILES (objfile
)
419 struct minimal_symbol
*msym
;
421 ALL_OBJFILE_MSYMBOLS (objfile
, msym
)
423 if (SYMBOL_NAME (msym
)
424 && STREQ (SYMBOL_NAME (msym
), name
))
426 *objfile_p
= objfile
;
436 skip_hurd_resolver (CORE_ADDR pc
)
438 /* The HURD dynamic linker is part of the GNU C library, so many
439 GNU/Linux distributions use it. (All ELF versions, as far as I
440 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
441 which calls "fixup" to patch the PLT, and then passes control to
444 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
445 the same objfile. If we are at the entry point of `fixup', then
446 we set a breakpoint at the return address (at the top of the
447 stack), and continue.
449 It's kind of gross to do all these checks every time we're
450 called, since they don't change once the executable has gotten
451 started. But this is only a temporary hack --- upcoming versions
452 of GNU/Linux will provide a portable, efficient interface for
453 debugging programs that use shared libraries. */
455 struct objfile
*objfile
;
456 struct minimal_symbol
*resolver
457 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile
);
461 struct minimal_symbol
*fixup
462 = lookup_minimal_symbol ("fixup", NULL
, objfile
);
464 if (fixup
&& SYMBOL_VALUE_ADDRESS (fixup
) == pc
)
465 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
471 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
473 1) decides whether a PLT has sent us into the linker to resolve
474 a function reference, and
475 2) if so, tells us where to set a temporary breakpoint that will
476 trigger when the dynamic linker is done. */
479 i386_linux_skip_solib_resolver (CORE_ADDR pc
)
483 /* Plug in functions for other kinds of resolvers here. */
484 result
= skip_hurd_resolver (pc
);
491 /* Fetch (and possibly build) an appropriate link_map_offsets
492 structure for native GNU/Linux x86 targets using the struct offsets
493 defined in link.h (but without actual reference to that file).
495 This makes it possible to access GNU/Linux x86 shared libraries
496 from a GDB that was not built on an GNU/Linux x86 host (for cross
499 struct link_map_offsets
*
500 i386_linux_svr4_fetch_link_map_offsets (void)
502 static struct link_map_offsets lmo
;
503 static struct link_map_offsets
*lmp
= NULL
;
509 lmo
.r_debug_size
= 8; /* The actual size is 20 bytes, but
510 this is all we need. */
511 lmo
.r_map_offset
= 4;
514 lmo
.link_map_size
= 20; /* The actual size is 552 bytes, but
515 this is all we need. */
516 lmo
.l_addr_offset
= 0;
519 lmo
.l_name_offset
= 4;
522 lmo
.l_next_offset
= 12;
525 lmo
.l_prev_offset
= 16;