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