Create new file regcache.h. Update all uses.
[deliverable/binutils-gdb.git] / gdb / i386-linux-tdep.c
CommitLineData
e7ee86a9 1/* Target-dependent code for Linux running on i386's, for GDB.
4e052eda 2 Copyright 2000, 2001 Free Software Foundation, Inc.
e7ee86a9
JB
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"
4e052eda 25#include "regcache.h"
e7ee86a9 26
bafda96e
MS
27/* For i386_linux_skip_solib_resolver. */
28#include "symtab.h"
29#include "symfile.h"
30#include "objfiles.h"
1a8629c7 31#include "solib-svr4.h" /* for struct link_map_offsets */
bafda96e 32
e7ee86a9
JB
33\f
34/* Recognizing signal handler frames. */
35
36/* Linux has two flavors of signals. Normal signal handlers, and
37 "realtime" (RT) signals. The RT signals can provide additional
38 information to the signal handler if the SA_SIGINFO flag is set
39 when establishing a signal handler using `sigaction'. It is not
40 unlikely that future versions of Linux will support SA_SIGINFO for
41 normal signals too. */
42
43/* When the i386 Linux kernel calls a signal handler and the
44 SA_RESTORER flag isn't set, the return address points to a bit of
45 code on the stack. This function returns whether the PC appears to
46 be within this bit of code.
47
48 The instruction sequence for normal signals is
49 pop %eax
50 mov $0x77,%eax
51 int $0x80
52 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
53
54 Checking for the code sequence should be somewhat reliable, because
55 the effect is to call the system call sigreturn. This is unlikely
56 to occur anywhere other than a signal trampoline.
57
58 It kind of sucks that we have to read memory from the process in
59 order to identify a signal trampoline, but there doesn't seem to be
60 any other way. The IN_SIGTRAMP macro in tm-linux.h arranges to
61 only call us if no function name could be identified, which should
62 be the case since the code is on the stack.
63
64 Detection of signal trampolines for handlers that set the
65 SA_RESTORER flag is in general not possible. Unfortunately this is
66 what the GNU C Library has been doing for quite some time now.
67 However, as of version 2.1.2, the GNU C Library uses signal
68 trampolines (named __restore and __restore_rt) that are identical
69 to the ones used by the kernel. Therefore, these trampolines are
70 supported too. */
71
72#define LINUX_SIGTRAMP_INSN0 (0x58) /* pop %eax */
73#define LINUX_SIGTRAMP_OFFSET0 (0)
74#define LINUX_SIGTRAMP_INSN1 (0xb8) /* mov $NNNN,%eax */
75#define LINUX_SIGTRAMP_OFFSET1 (1)
76#define LINUX_SIGTRAMP_INSN2 (0xcd) /* int */
77#define LINUX_SIGTRAMP_OFFSET2 (6)
78
79static const unsigned char linux_sigtramp_code[] =
80{
81 LINUX_SIGTRAMP_INSN0, /* pop %eax */
82 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77,%eax */
83 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
84};
85
86#define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
87
88/* If PC is in a sigtramp routine, return the address of the start of
89 the routine. Otherwise, return 0. */
90
91static CORE_ADDR
92i386_linux_sigtramp_start (CORE_ADDR pc)
93{
94 unsigned char buf[LINUX_SIGTRAMP_LEN];
95
96 /* We only recognize a signal trampoline if PC is at the start of
97 one of the three instructions. We optimize for finding the PC at
98 the start, as will be the case when the trampoline is not the
99 first frame on the stack. We assume that in the case where the
100 PC is not at the start of the instruction sequence, there will be
101 a few trailing readable bytes on the stack. */
102
103 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
104 return 0;
105
106 if (buf[0] != LINUX_SIGTRAMP_INSN0)
107 {
108 int adjust;
109
110 switch (buf[0])
111 {
112 case LINUX_SIGTRAMP_INSN1:
113 adjust = LINUX_SIGTRAMP_OFFSET1;
114 break;
115 case LINUX_SIGTRAMP_INSN2:
116 adjust = LINUX_SIGTRAMP_OFFSET2;
117 break;
118 default:
119 return 0;
120 }
121
122 pc -= adjust;
123
124 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
125 return 0;
126 }
127
128 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
129 return 0;
130
131 return pc;
132}
133
134/* This function does the same for RT signals. Here the instruction
135 sequence is
136 mov $0xad,%eax
137 int $0x80
138 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
139
140 The effect is to call the system call rt_sigreturn. */
141
142#define LINUX_RT_SIGTRAMP_INSN0 (0xb8) /* mov $NNNN,%eax */
143#define LINUX_RT_SIGTRAMP_OFFSET0 (0)
144#define LINUX_RT_SIGTRAMP_INSN1 (0xcd) /* int */
145#define LINUX_RT_SIGTRAMP_OFFSET1 (5)
146
147static const unsigned char linux_rt_sigtramp_code[] =
148{
149 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad,%eax */
150 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
151};
152
153#define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
154
155/* If PC is in a RT sigtramp routine, return the address of the start
156 of the routine. Otherwise, return 0. */
157
158static CORE_ADDR
159i386_linux_rt_sigtramp_start (CORE_ADDR pc)
160{
161 unsigned char buf[LINUX_RT_SIGTRAMP_LEN];
162
163 /* We only recognize a signal trampoline if PC is at the start of
164 one of the two instructions. We optimize for finding the PC at
165 the start, as will be the case when the trampoline is not the
166 first frame on the stack. We assume that in the case where the
167 PC is not at the start of the instruction sequence, there will be
168 a few trailing readable bytes on the stack. */
169
170 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
171 return 0;
172
173 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
174 {
175 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
176 return 0;
177
178 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
179
180 if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
181 return 0;
182 }
183
184 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
185 return 0;
186
187 return pc;
188}
189
190/* Return whether PC is in a Linux sigtramp routine. */
191
192int
193i386_linux_in_sigtramp (CORE_ADDR pc, char *name)
194{
195 if (name)
196 return STREQ ("__restore", name) || STREQ ("__restore_rt", name);
197
198 return (i386_linux_sigtramp_start (pc) != 0
199 || i386_linux_rt_sigtramp_start (pc) != 0);
200}
201
202/* Assuming FRAME is for a Linux sigtramp routine, return the address
203 of the associated sigcontext structure. */
204
205CORE_ADDR
206i386_linux_sigcontext_addr (struct frame_info *frame)
207{
208 CORE_ADDR pc;
209
210 pc = i386_linux_sigtramp_start (frame->pc);
211 if (pc)
212 {
213 CORE_ADDR sp;
214
215 if (frame->next)
216 /* If this isn't the top frame, the next frame must be for the
217 signal handler itself. The sigcontext structure lives on
218 the stack, right after the signum argument. */
219 return frame->next->frame + 12;
220
221 /* This is the top frame. We'll have to find the address of the
222 sigcontext structure by looking at the stack pointer. Keep
223 in mind that the first instruction of the sigtramp code is
224 "pop %eax". If the PC is at this instruction, adjust the
225 returned value accordingly. */
226 sp = read_register (SP_REGNUM);
227 if (pc == frame->pc)
228 return sp + 4;
229 return sp;
230 }
231
232 pc = i386_linux_rt_sigtramp_start (frame->pc);
233 if (pc)
234 {
235 if (frame->next)
236 /* If this isn't the top frame, the next frame must be for the
237 signal handler itself. The sigcontext structure is part of
238 the user context. A pointer to the user context is passed
239 as the third argument to the signal handler. */
240 return read_memory_integer (frame->next->frame + 16, 4) + 20;
241
242 /* This is the top frame. Again, use the stack pointer to find
243 the address of the sigcontext structure. */
244 return read_memory_integer (read_register (SP_REGNUM) + 8, 4) + 20;
245 }
246
247 error ("Couldn't recognize signal trampoline.");
248 return 0;
249}
250
251/* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
252#define LINUX_SIGCONTEXT_PC_OFFSET (56)
253
254/* Assuming FRAME is for a Linux sigtramp routine, return the saved
255 program counter. */
256
257CORE_ADDR
258i386_linux_sigtramp_saved_pc (struct frame_info *frame)
259{
260 CORE_ADDR addr;
261 addr = i386_linux_sigcontext_addr (frame);
262 return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 4);
263}
264
265/* Offset to saved SP in sigcontext, from <asm/sigcontext.h>. */
266#define LINUX_SIGCONTEXT_SP_OFFSET (28)
267
268/* Assuming FRAME is for a Linux sigtramp routine, return the saved
269 stack pointer. */
270
271CORE_ADDR
272i386_linux_sigtramp_saved_sp (struct frame_info *frame)
273{
274 CORE_ADDR addr;
275 addr = i386_linux_sigcontext_addr (frame);
276 return read_memory_integer (addr + LINUX_SIGCONTEXT_SP_OFFSET, 4);
277}
278
279/* Immediately after a function call, return the saved pc. */
280
281CORE_ADDR
282i386_linux_saved_pc_after_call (struct frame_info *frame)
283{
284 if (frame->signal_handler_caller)
285 return i386_linux_sigtramp_saved_pc (frame);
286
287 return read_memory_integer (read_register (SP_REGNUM), 4);
288}
bafda96e
MS
289
290\f
291
292/* Calling functions in shared libraries. */
293/* Find the minimal symbol named NAME, and return both the minsym
294 struct and its objfile. This probably ought to be in minsym.c, but
295 everything there is trying to deal with things like C++ and
296 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
297 be considered too special-purpose for general consumption. */
298
299static struct minimal_symbol *
300find_minsym_and_objfile (char *name, struct objfile **objfile_p)
301{
302 struct objfile *objfile;
303
304 ALL_OBJFILES (objfile)
305 {
306 struct minimal_symbol *msym;
307
308 ALL_OBJFILE_MSYMBOLS (objfile, msym)
309 {
310 if (SYMBOL_NAME (msym)
311 && STREQ (SYMBOL_NAME (msym), name))
312 {
313 *objfile_p = objfile;
314 return msym;
315 }
316 }
317 }
318
319 return 0;
320}
321
322static CORE_ADDR
323skip_hurd_resolver (CORE_ADDR pc)
324{
325 /* The HURD dynamic linker is part of the GNU C library, so many
326 GNU/Linux distributions use it. (All ELF versions, as far as I
327 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
328 which calls "fixup" to patch the PLT, and then passes control to
329 the function.
330
331 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
332 the same objfile. If we are at the entry point of `fixup', then
333 we set a breakpoint at the return address (at the top of the
334 stack), and continue.
335
336 It's kind of gross to do all these checks every time we're
337 called, since they don't change once the executable has gotten
338 started. But this is only a temporary hack --- upcoming versions
339 of Linux will provide a portable, efficient interface for
340 debugging programs that use shared libraries. */
341
342 struct objfile *objfile;
343 struct minimal_symbol *resolver
344 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
345
346 if (resolver)
347 {
348 struct minimal_symbol *fixup
349 = lookup_minimal_symbol ("fixup", 0, objfile);
350
351 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
352 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
353 }
354
355 return 0;
356}
357
358/* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
359 This function:
360 1) decides whether a PLT has sent us into the linker to resolve
361 a function reference, and
362 2) if so, tells us where to set a temporary breakpoint that will
363 trigger when the dynamic linker is done. */
364
365CORE_ADDR
366i386_linux_skip_solib_resolver (CORE_ADDR pc)
367{
368 CORE_ADDR result;
369
370 /* Plug in functions for other kinds of resolvers here. */
371 result = skip_hurd_resolver (pc);
372 if (result)
373 return result;
374
375 return 0;
376}
1a8629c7
MS
377
378/* Fetch (and possibly build) an appropriate link_map_offsets structure
379 for native i386 linux targets using the struct offsets defined in
380 link.h (but without actual reference to that file).
381
382 This makes it possible to access i386-linux shared libraries from
383 a gdb that was not built on an i386-linux host (for cross debugging).
384 */
385
386struct link_map_offsets *
387i386_linux_svr4_fetch_link_map_offsets (void)
388{
389 static struct link_map_offsets lmo;
390 static struct link_map_offsets *lmp = 0;
391
392 if (lmp == 0)
393 {
394 lmp = &lmo;
395
396 lmo.r_debug_size = 8; /* 20 not actual size but all we need */
397
398 lmo.r_map_offset = 4;
399 lmo.r_map_size = 4;
400
401 lmo.link_map_size = 20; /* 552 not actual size but all we need */
402
403 lmo.l_addr_offset = 0;
404 lmo.l_addr_size = 4;
405
406 lmo.l_name_offset = 4;
407 lmo.l_name_size = 4;
408
409 lmo.l_next_offset = 12;
410 lmo.l_next_size = 4;
411
412 lmo.l_prev_offset = 16;
413 lmo.l_prev_size = 4;
414 }
415
416 return lmp;
417}
418
This page took 0.083 seconds and 4 git commands to generate.