1 /* Target-dependent code for Linux running on i386's, for GDB.
2 Copyright (C) 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 /* For i386_linux_skip_solib_resolver. */
32 /* Recognizing signal handler frames. */
34 /* Linux has two flavors of signals. Normal signal handlers, and
35 "realtime" (RT) signals. The RT signals can provide additional
36 information to the signal handler if the SA_SIGINFO flag is set
37 when establishing a signal handler using `sigaction'. It is not
38 unlikely that future versions of Linux will support SA_SIGINFO for
39 normal signals too. */
41 /* When the i386 Linux kernel calls a signal handler and the
42 SA_RESTORER flag isn't set, the return address points to a bit of
43 code on the stack. This function returns whether the PC appears to
44 be within this bit of code.
46 The instruction sequence for normal signals is
50 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
52 Checking for the code sequence should be somewhat reliable, because
53 the effect is to call the system call sigreturn. This is unlikely
54 to occur anywhere other than a signal trampoline.
56 It kind of sucks that we have to read memory from the process in
57 order to identify a signal trampoline, but there doesn't seem to be
58 any other way. The IN_SIGTRAMP macro in tm-linux.h arranges to
59 only call us if no function name could be identified, which should
60 be the case since the code is on the stack.
62 Detection of signal trampolines for handlers that set the
63 SA_RESTORER flag is in general not possible. Unfortunately this is
64 what the GNU C Library has been doing for quite some time now.
65 However, as of version 2.1.2, the GNU C Library uses signal
66 trampolines (named __restore and __restore_rt) that are identical
67 to the ones used by the kernel. Therefore, these trampolines are
70 #define LINUX_SIGTRAMP_INSN0 (0x58) /* pop %eax */
71 #define LINUX_SIGTRAMP_OFFSET0 (0)
72 #define LINUX_SIGTRAMP_INSN1 (0xb8) /* mov $NNNN,%eax */
73 #define LINUX_SIGTRAMP_OFFSET1 (1)
74 #define LINUX_SIGTRAMP_INSN2 (0xcd) /* int */
75 #define LINUX_SIGTRAMP_OFFSET2 (6)
77 static const unsigned char linux_sigtramp_code
[] =
79 LINUX_SIGTRAMP_INSN0
, /* pop %eax */
80 LINUX_SIGTRAMP_INSN1
, 0x77, 0x00, 0x00, 0x00, /* mov $0x77,%eax */
81 LINUX_SIGTRAMP_INSN2
, 0x80 /* int $0x80 */
84 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
86 /* If PC is in a sigtramp routine, return the address of the start of
87 the routine. Otherwise, return 0. */
90 i386_linux_sigtramp_start (CORE_ADDR pc
)
92 unsigned char buf
[LINUX_SIGTRAMP_LEN
];
94 /* We only recognize a signal trampoline if PC is at the start of
95 one of the three instructions. We optimize for finding the PC at
96 the start, as will be the case when the trampoline is not the
97 first frame on the stack. We assume that in the case where the
98 PC is not at the start of the instruction sequence, there will be
99 a few trailing readable bytes on the stack. */
101 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SIGTRAMP_LEN
) != 0)
104 if (buf
[0] != LINUX_SIGTRAMP_INSN0
)
110 case LINUX_SIGTRAMP_INSN1
:
111 adjust
= LINUX_SIGTRAMP_OFFSET1
;
113 case LINUX_SIGTRAMP_INSN2
:
114 adjust
= LINUX_SIGTRAMP_OFFSET2
;
122 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SIGTRAMP_LEN
) != 0)
126 if (memcmp (buf
, linux_sigtramp_code
, LINUX_SIGTRAMP_LEN
) != 0)
132 /* This function does the same for RT signals. Here the instruction
136 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
138 The effect is to call the system call rt_sigreturn. */
140 #define LINUX_RT_SIGTRAMP_INSN0 (0xb8) /* mov $NNNN,%eax */
141 #define LINUX_RT_SIGTRAMP_OFFSET0 (0)
142 #define LINUX_RT_SIGTRAMP_INSN1 (0xcd) /* int */
143 #define LINUX_RT_SIGTRAMP_OFFSET1 (5)
145 static const unsigned char linux_rt_sigtramp_code
[] =
147 LINUX_RT_SIGTRAMP_INSN0
, 0xad, 0x00, 0x00, 0x00, /* mov $0xad,%eax */
148 LINUX_RT_SIGTRAMP_INSN1
, 0x80 /* int $0x80 */
151 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
153 /* If PC is in a RT sigtramp routine, return the address of the start
154 of the routine. Otherwise, return 0. */
157 i386_linux_rt_sigtramp_start (CORE_ADDR pc
)
159 unsigned char buf
[LINUX_RT_SIGTRAMP_LEN
];
161 /* We only recognize a signal trampoline if PC is at the start of
162 one of the two instructions. We optimize for finding the PC at
163 the start, as will be the case when the trampoline is not the
164 first frame on the stack. We assume that in the case where the
165 PC is not at the start of the instruction sequence, there will be
166 a few trailing readable bytes on the stack. */
168 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_RT_SIGTRAMP_LEN
) != 0)
171 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN0
)
173 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN1
)
176 pc
-= LINUX_RT_SIGTRAMP_OFFSET1
;
178 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_RT_SIGTRAMP_LEN
) != 0)
182 if (memcmp (buf
, linux_rt_sigtramp_code
, LINUX_RT_SIGTRAMP_LEN
) != 0)
188 /* Return whether PC is in a Linux sigtramp routine. */
191 i386_linux_in_sigtramp (CORE_ADDR pc
, char *name
)
194 return STREQ ("__restore", name
) || STREQ ("__restore_rt", name
);
196 return (i386_linux_sigtramp_start (pc
) != 0
197 || i386_linux_rt_sigtramp_start (pc
) != 0);
200 /* Assuming FRAME is for a Linux sigtramp routine, return the address
201 of the associated sigcontext structure. */
204 i386_linux_sigcontext_addr (struct frame_info
*frame
)
208 pc
= i386_linux_sigtramp_start (frame
->pc
);
214 /* If this isn't the top frame, the next frame must be for the
215 signal handler itself. The sigcontext structure lives on
216 the stack, right after the signum argument. */
217 return frame
->next
->frame
+ 12;
219 /* This is the top frame. We'll have to find the address of the
220 sigcontext structure by looking at the stack pointer. Keep
221 in mind that the first instruction of the sigtramp code is
222 "pop %eax". If the PC is at this instruction, adjust the
223 returned value accordingly. */
224 sp
= read_register (SP_REGNUM
);
230 pc
= i386_linux_rt_sigtramp_start (frame
->pc
);
234 /* If this isn't the top frame, the next frame must be for the
235 signal handler itself. The sigcontext structure is part of
236 the user context. A pointer to the user context is passed
237 as the third argument to the signal handler. */
238 return read_memory_integer (frame
->next
->frame
+ 16, 4) + 20;
240 /* This is the top frame. Again, use the stack pointer to find
241 the address of the sigcontext structure. */
242 return read_memory_integer (read_register (SP_REGNUM
) + 8, 4) + 20;
245 error ("Couldn't recognize signal trampoline.");
249 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
250 #define LINUX_SIGCONTEXT_PC_OFFSET (56)
252 /* Assuming FRAME is for a Linux sigtramp routine, return the saved
256 i386_linux_sigtramp_saved_pc (struct frame_info
*frame
)
259 addr
= i386_linux_sigcontext_addr (frame
);
260 return read_memory_integer (addr
+ LINUX_SIGCONTEXT_PC_OFFSET
, 4);
263 /* Offset to saved SP in sigcontext, from <asm/sigcontext.h>. */
264 #define LINUX_SIGCONTEXT_SP_OFFSET (28)
266 /* Assuming FRAME is for a Linux sigtramp routine, return the saved
270 i386_linux_sigtramp_saved_sp (struct frame_info
*frame
)
273 addr
= i386_linux_sigcontext_addr (frame
);
274 return read_memory_integer (addr
+ LINUX_SIGCONTEXT_SP_OFFSET
, 4);
277 /* Immediately after a function call, return the saved pc. */
280 i386_linux_saved_pc_after_call (struct frame_info
*frame
)
282 if (frame
->signal_handler_caller
)
283 return i386_linux_sigtramp_saved_pc (frame
);
285 return read_memory_integer (read_register (SP_REGNUM
), 4);
290 /* Calling functions in shared libraries. */
291 /* Find the minimal symbol named NAME, and return both the minsym
292 struct and its objfile. This probably ought to be in minsym.c, but
293 everything there is trying to deal with things like C++ and
294 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
295 be considered too special-purpose for general consumption. */
297 static struct minimal_symbol
*
298 find_minsym_and_objfile (char *name
, struct objfile
**objfile_p
)
300 struct objfile
*objfile
;
302 ALL_OBJFILES (objfile
)
304 struct minimal_symbol
*msym
;
306 ALL_OBJFILE_MSYMBOLS (objfile
, msym
)
308 if (SYMBOL_NAME (msym
)
309 && STREQ (SYMBOL_NAME (msym
), name
))
311 *objfile_p
= objfile
;
321 skip_hurd_resolver (CORE_ADDR pc
)
323 /* The HURD dynamic linker is part of the GNU C library, so many
324 GNU/Linux distributions use it. (All ELF versions, as far as I
325 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
326 which calls "fixup" to patch the PLT, and then passes control to
329 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
330 the same objfile. If we are at the entry point of `fixup', then
331 we set a breakpoint at the return address (at the top of the
332 stack), and continue.
334 It's kind of gross to do all these checks every time we're
335 called, since they don't change once the executable has gotten
336 started. But this is only a temporary hack --- upcoming versions
337 of Linux will provide a portable, efficient interface for
338 debugging programs that use shared libraries. */
340 struct objfile
*objfile
;
341 struct minimal_symbol
*resolver
342 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile
);
346 struct minimal_symbol
*fixup
347 = lookup_minimal_symbol ("fixup", 0, objfile
);
349 if (fixup
&& SYMBOL_VALUE_ADDRESS (fixup
) == pc
)
350 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
356 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
358 1) decides whether a PLT has sent us into the linker to resolve
359 a function reference, and
360 2) if so, tells us where to set a temporary breakpoint that will
361 trigger when the dynamic linker is done. */
364 i386_linux_skip_solib_resolver (CORE_ADDR pc
)
368 /* Plug in functions for other kinds of resolvers here. */
369 result
= skip_hurd_resolver (pc
);