* lin-lwp.c (stop_wait_callback): Remove bogus assertions in the
[deliverable/binutils-gdb.git] / gdb / i386-linux-tdep.c
CommitLineData
e7ee86a9
JB
1/* Target-dependent code for Linux running on i386's, for GDB.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21#include "defs.h"
22#include "gdbcore.h"
23#include "frame.h"
24#include "value.h"
25
bafda96e
MS
26/* For i386_linux_skip_solib_resolver. */
27#include "symtab.h"
28#include "symfile.h"
29#include "objfiles.h"
30
e7ee86a9
JB
31\f
32/* Recognizing signal handler frames. */
33
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. */
40
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.
45
46 The instruction sequence for normal signals is
47 pop %eax
48 mov $0x77,%eax
49 int $0x80
50 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
51
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.
55
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.
61
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
68 supported too. */
69
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)
76
77static const unsigned char linux_sigtramp_code[] =
78{
79 LINUX_SIGTRAMP_INSN0, /* pop %eax */
80 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77,%eax */
81 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
82};
83
84#define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
85
86/* If PC is in a sigtramp routine, return the address of the start of
87 the routine. Otherwise, return 0. */
88
89static CORE_ADDR
90i386_linux_sigtramp_start (CORE_ADDR pc)
91{
92 unsigned char buf[LINUX_SIGTRAMP_LEN];
93
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. */
100
101 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
102 return 0;
103
104 if (buf[0] != LINUX_SIGTRAMP_INSN0)
105 {
106 int adjust;
107
108 switch (buf[0])
109 {
110 case LINUX_SIGTRAMP_INSN1:
111 adjust = LINUX_SIGTRAMP_OFFSET1;
112 break;
113 case LINUX_SIGTRAMP_INSN2:
114 adjust = LINUX_SIGTRAMP_OFFSET2;
115 break;
116 default:
117 return 0;
118 }
119
120 pc -= adjust;
121
122 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
123 return 0;
124 }
125
126 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
127 return 0;
128
129 return pc;
130}
131
132/* This function does the same for RT signals. Here the instruction
133 sequence is
134 mov $0xad,%eax
135 int $0x80
136 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
137
138 The effect is to call the system call rt_sigreturn. */
139
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)
144
145static const unsigned char linux_rt_sigtramp_code[] =
146{
147 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad,%eax */
148 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
149};
150
151#define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
152
153/* If PC is in a RT sigtramp routine, return the address of the start
154 of the routine. Otherwise, return 0. */
155
156static CORE_ADDR
157i386_linux_rt_sigtramp_start (CORE_ADDR pc)
158{
159 unsigned char buf[LINUX_RT_SIGTRAMP_LEN];
160
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. */
167
168 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
169 return 0;
170
171 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
172 {
173 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
174 return 0;
175
176 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
177
178 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
179 return 0;
180 }
181
182 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
183 return 0;
184
185 return pc;
186}
187
188/* Return whether PC is in a Linux sigtramp routine. */
189
190int
191i386_linux_in_sigtramp (CORE_ADDR pc, char *name)
192{
193 if (name)
194 return STREQ ("__restore", name) || STREQ ("__restore_rt", name);
195
196 return (i386_linux_sigtramp_start (pc) != 0
197 || i386_linux_rt_sigtramp_start (pc) != 0);
198}
199
200/* Assuming FRAME is for a Linux sigtramp routine, return the address
201 of the associated sigcontext structure. */
202
203CORE_ADDR
204i386_linux_sigcontext_addr (struct frame_info *frame)
205{
206 CORE_ADDR pc;
207
208 pc = i386_linux_sigtramp_start (frame->pc);
209 if (pc)
210 {
211 CORE_ADDR sp;
212
213 if (frame->next)
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;
218
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);
225 if (pc == frame->pc)
226 return sp + 4;
227 return sp;
228 }
229
230 pc = i386_linux_rt_sigtramp_start (frame->pc);
231 if (pc)
232 {
233 if (frame->next)
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;
239
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;
243 }
244
245 error ("Couldn't recognize signal trampoline.");
246 return 0;
247}
248
249/* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
250#define LINUX_SIGCONTEXT_PC_OFFSET (56)
251
252/* Assuming FRAME is for a Linux sigtramp routine, return the saved
253 program counter. */
254
255CORE_ADDR
256i386_linux_sigtramp_saved_pc (struct frame_info *frame)
257{
258 CORE_ADDR addr;
259 addr = i386_linux_sigcontext_addr (frame);
260 return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 4);
261}
262
263/* Offset to saved SP in sigcontext, from <asm/sigcontext.h>. */
264#define LINUX_SIGCONTEXT_SP_OFFSET (28)
265
266/* Assuming FRAME is for a Linux sigtramp routine, return the saved
267 stack pointer. */
268
269CORE_ADDR
270i386_linux_sigtramp_saved_sp (struct frame_info *frame)
271{
272 CORE_ADDR addr;
273 addr = i386_linux_sigcontext_addr (frame);
274 return read_memory_integer (addr + LINUX_SIGCONTEXT_SP_OFFSET, 4);
275}
276
277/* Immediately after a function call, return the saved pc. */
278
279CORE_ADDR
280i386_linux_saved_pc_after_call (struct frame_info *frame)
281{
282 if (frame->signal_handler_caller)
283 return i386_linux_sigtramp_saved_pc (frame);
284
285 return read_memory_integer (read_register (SP_REGNUM), 4);
286}
bafda96e
MS
287
288\f
289
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. */
296
297static struct minimal_symbol *
298find_minsym_and_objfile (char *name, struct objfile **objfile_p)
299{
300 struct objfile *objfile;
301
302 ALL_OBJFILES (objfile)
303 {
304 struct minimal_symbol *msym;
305
306 ALL_OBJFILE_MSYMBOLS (objfile, msym)
307 {
308 if (SYMBOL_NAME (msym)
309 && STREQ (SYMBOL_NAME (msym), name))
310 {
311 *objfile_p = objfile;
312 return msym;
313 }
314 }
315 }
316
317 return 0;
318}
319
320static CORE_ADDR
321skip_hurd_resolver (CORE_ADDR pc)
322{
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
327 the function.
328
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.
333
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. */
339
340 struct objfile *objfile;
341 struct minimal_symbol *resolver
342 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
343
344 if (resolver)
345 {
346 struct minimal_symbol *fixup
347 = lookup_minimal_symbol ("fixup", 0, objfile);
348
349 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
350 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
351 }
352
353 return 0;
354}
355
356/* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
357 This function:
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. */
362
363CORE_ADDR
364i386_linux_skip_solib_resolver (CORE_ADDR pc)
365{
366 CORE_ADDR result;
367
368 /* Plug in functions for other kinds of resolvers here. */
369 result = skip_hurd_resolver (pc);
370 if (result)
371 return result;
372
373 return 0;
374}
This page took 0.059104 seconds and 4 git commands to generate.