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