]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/i386-linux-tdep.c
6726b68ad096776b52dd4e0ffcb74c55d53ad084
[thirdparty/binutils-gdb.git] / gdb / i386-linux-tdep.c
1 /* Target-dependent code for GNU/Linux i386.
2
3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "defs.h"
24 #include "gdbcore.h"
25 #include "frame.h"
26 #include "value.h"
27 #include "regcache.h"
28 #include "inferior.h"
29 #include "osabi.h"
30 #include "reggroups.h"
31 #include "dwarf2-frame.h"
32 #include "gdb_string.h"
33
34 #include "i386-tdep.h"
35 #include "i386-linux-tdep.h"
36 #include "glibc-tdep.h"
37 #include "solib-svr4.h"
38 #include "symtab.h"
39
40 /* Return the name of register REG. */
41
42 static const char *
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 }
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
65 \f
66 /* Recognizing signal handler frames. */
67
68 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
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
72 unlikely that future versions of GNU/Linux will support SA_SIGINFO
73 for normal signals too. */
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 in 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
92 any other way. Therefore we only do the memory reads if no
93 function name could be identified, which should be the case since
94 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 gdb_byte 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 NEXT_FRAME unwinds into a sigtramp routine, return the address
121 of the start of the routine. Otherwise, return 0. */
122
123 static CORE_ADDR
124 i386_linux_sigtramp_start (struct frame_info *next_frame)
125 {
126 CORE_ADDR pc = frame_pc_unwind (next_frame);
127 gdb_byte buf[LINUX_SIGTRAMP_LEN];
128
129 /* We only recognize a signal trampoline if PC is at the start of
130 one of the three instructions. We optimize for finding the PC at
131 the start, as will be the case when the trampoline is not the
132 first frame on the stack. We assume that in the case where the
133 PC is not at the start of the instruction sequence, there will be
134 a few trailing readable bytes on the stack. */
135
136 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_SIGTRAMP_LEN))
137 return 0;
138
139 if (buf[0] != LINUX_SIGTRAMP_INSN0)
140 {
141 int adjust;
142
143 switch (buf[0])
144 {
145 case LINUX_SIGTRAMP_INSN1:
146 adjust = LINUX_SIGTRAMP_OFFSET1;
147 break;
148 case LINUX_SIGTRAMP_INSN2:
149 adjust = LINUX_SIGTRAMP_OFFSET2;
150 break;
151 default:
152 return 0;
153 }
154
155 pc -= adjust;
156
157 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_SIGTRAMP_LEN))
158 return 0;
159 }
160
161 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
162 return 0;
163
164 return pc;
165 }
166
167 /* This function does the same for RT signals. Here the instruction
168 sequence is
169 mov $0xad, %eax
170 int $0x80
171 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
172
173 The effect is to call the system call rt_sigreturn. */
174
175 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
176 #define LINUX_RT_SIGTRAMP_OFFSET0 0
177 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
178 #define LINUX_RT_SIGTRAMP_OFFSET1 5
179
180 static const gdb_byte linux_rt_sigtramp_code[] =
181 {
182 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
183 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
184 };
185
186 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
187
188 /* If NEXT_FRAME unwinds into an RT sigtramp routine, return the
189 address of the start of the routine. Otherwise, return 0. */
190
191 static CORE_ADDR
192 i386_linux_rt_sigtramp_start (struct frame_info *next_frame)
193 {
194 CORE_ADDR pc = frame_pc_unwind (next_frame);
195 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
196
197 /* We only recognize a signal trampoline if PC is at the start of
198 one of the two instructions. We optimize for finding the PC at
199 the start, as will be the case when the trampoline is not the
200 first frame on the stack. We assume that in the case where the
201 PC is not at the start of the instruction sequence, there will be
202 a few trailing readable bytes on the stack. */
203
204 if (!safe_frame_unwind_memory (next_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
205 return 0;
206
207 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
208 {
209 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
210 return 0;
211
212 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
213
214 if (!safe_frame_unwind_memory (next_frame, pc, buf,
215 LINUX_RT_SIGTRAMP_LEN))
216 return 0;
217 }
218
219 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
220 return 0;
221
222 return pc;
223 }
224
225 /* Return whether the frame preceding NEXT_FRAME corresponds to a
226 GNU/Linux sigtramp routine. */
227
228 static int
229 i386_linux_sigtramp_p (struct frame_info *next_frame)
230 {
231 CORE_ADDR pc = frame_pc_unwind (next_frame);
232 char *name;
233
234 find_pc_partial_function (pc, &name, NULL, NULL);
235
236 /* If we have NAME, we can optimize the search. The trampolines are
237 named __restore and __restore_rt. However, they aren't dynamically
238 exported from the shared C library, so the trampoline may appear to
239 be part of the preceding function. This should always be sigaction,
240 __sigaction, or __libc_sigaction (all aliases to the same function). */
241 if (name == NULL || strstr (name, "sigaction") != NULL)
242 return (i386_linux_sigtramp_start (next_frame) != 0
243 || i386_linux_rt_sigtramp_start (next_frame) != 0);
244
245 return (strcmp ("__restore", name) == 0
246 || strcmp ("__restore_rt", name) == 0);
247 }
248
249 /* Return one if the unwound PC from NEXT_FRAME is in a signal trampoline
250 which may have DWARF-2 CFI. */
251
252 static int
253 i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
254 struct frame_info *next_frame)
255 {
256 CORE_ADDR pc = frame_pc_unwind (next_frame);
257 char *name;
258
259 find_pc_partial_function (pc, &name, NULL, NULL);
260
261 /* If a vsyscall DSO is in use, the signal trampolines may have these
262 names. */
263 if (name && (strcmp (name, "__kernel_sigreturn") == 0
264 || strcmp (name, "__kernel_rt_sigreturn") == 0))
265 return 1;
266
267 return 0;
268 }
269
270 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
271 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
272
273 /* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
274 routine, return the address of the associated sigcontext structure. */
275
276 static CORE_ADDR
277 i386_linux_sigcontext_addr (struct frame_info *next_frame)
278 {
279 CORE_ADDR pc;
280 CORE_ADDR sp;
281 gdb_byte buf[4];
282
283 frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
284 sp = extract_unsigned_integer (buf, 4);
285
286 pc = i386_linux_sigtramp_start (next_frame);
287 if (pc)
288 {
289 /* The sigcontext structure lives on the stack, right after
290 the signum argument. We determine the address of the
291 sigcontext structure by looking at the frame's stack
292 pointer. Keep in mind that the first instruction of the
293 sigtramp code is "pop %eax". If the PC is after this
294 instruction, adjust the returned value accordingly. */
295 if (pc == frame_pc_unwind (next_frame))
296 return sp + 4;
297 return sp;
298 }
299
300 pc = i386_linux_rt_sigtramp_start (next_frame);
301 if (pc)
302 {
303 CORE_ADDR ucontext_addr;
304
305 /* The sigcontext structure is part of the user context. A
306 pointer to the user context is passed as the third argument
307 to the signal handler. */
308 read_memory (sp + 8, buf, 4);
309 ucontext_addr = extract_unsigned_integer (buf, 4);
310 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
311 }
312
313 error (_("Couldn't recognize signal trampoline."));
314 return 0;
315 }
316
317 /* Set the program counter for process PTID to PC. */
318
319 static void
320 i386_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
321 {
322 write_register_pid (I386_EIP_REGNUM, pc, ptid);
323
324 /* We must be careful with modifying the program counter. If we
325 just interrupted a system call, the kernel might try to restart
326 it when we resume the inferior. On restarting the system call,
327 the kernel will try backing up the program counter even though it
328 no longer points at the system call. This typically results in a
329 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
330 "orig_eax" pseudo-register.
331
332 Note that "orig_eax" is saved when setting up a dummy call frame.
333 This means that it is properly restored when that frame is
334 popped, and that the interrupted system call will be restarted
335 when we resume the inferior on return from a function call from
336 within GDB. In all other cases the system call will not be
337 restarted. */
338 write_register_pid (I386_LINUX_ORIG_EAX_REGNUM, -1, ptid);
339 }
340 \f
341
342 /* The register sets used in GNU/Linux ELF core-dumps are identical to
343 the register sets in `struct user' that are used for a.out
344 core-dumps. These are also used by ptrace(2). The corresponding
345 types are `elf_gregset_t' for the general-purpose registers (with
346 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
347 for the floating-point registers.
348
349 Those types used to be available under the names `gregset_t' and
350 `fpregset_t' too, and GDB used those names in the past. But those
351 names are now used for the register sets used in the `mcontext_t'
352 type, which have a different size and layout. */
353
354 /* Mapping between the general-purpose registers in `struct user'
355 format and GDB's register cache layout. */
356
357 /* From <sys/reg.h>. */
358 static int i386_linux_gregset_reg_offset[] =
359 {
360 6 * 4, /* %eax */
361 1 * 4, /* %ecx */
362 2 * 4, /* %edx */
363 0 * 4, /* %ebx */
364 15 * 4, /* %esp */
365 5 * 4, /* %ebp */
366 3 * 4, /* %esi */
367 4 * 4, /* %edi */
368 12 * 4, /* %eip */
369 14 * 4, /* %eflags */
370 13 * 4, /* %cs */
371 16 * 4, /* %ss */
372 7 * 4, /* %ds */
373 8 * 4, /* %es */
374 9 * 4, /* %fs */
375 10 * 4, /* %gs */
376 -1, -1, -1, -1, -1, -1, -1, -1,
377 -1, -1, -1, -1, -1, -1, -1, -1,
378 -1, -1, -1, -1, -1, -1, -1, -1,
379 -1,
380 11 * 4 /* "orig_eax" */
381 };
382
383 /* Mapping between the general-purpose registers in `struct
384 sigcontext' format and GDB's register cache layout. */
385
386 /* From <asm/sigcontext.h>. */
387 static int i386_linux_sc_reg_offset[] =
388 {
389 11 * 4, /* %eax */
390 10 * 4, /* %ecx */
391 9 * 4, /* %edx */
392 8 * 4, /* %ebx */
393 7 * 4, /* %esp */
394 6 * 4, /* %ebp */
395 5 * 4, /* %esi */
396 4 * 4, /* %edi */
397 14 * 4, /* %eip */
398 16 * 4, /* %eflags */
399 15 * 4, /* %cs */
400 18 * 4, /* %ss */
401 3 * 4, /* %ds */
402 2 * 4, /* %es */
403 1 * 4, /* %fs */
404 0 * 4 /* %gs */
405 };
406
407 static void
408 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
409 {
410 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
411
412 /* GNU/Linux uses ELF. */
413 i386_elf_init_abi (info, gdbarch);
414
415 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
416 to adjust a few things. */
417
418 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
419 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
420 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
421 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
422
423 tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
424 tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
425 tdep->sizeof_gregset = 17 * 4;
426
427 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
428
429 tdep->sigtramp_p = i386_linux_sigtramp_p;
430 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
431 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
432 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
433
434 /* GNU/Linux uses SVR4-style shared libraries. */
435 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
436 set_solib_svr4_fetch_link_map_offsets
437 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
438
439 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
440 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
441
442 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
443
444 /* Enable TLS support. */
445 set_gdbarch_fetch_tls_load_module_address (gdbarch,
446 svr4_fetch_objfile_link_map);
447 }
448
449 /* Provide a prototype to silence -Wmissing-prototypes. */
450 extern void _initialize_i386_linux_tdep (void);
451
452 void
453 _initialize_i386_linux_tdep (void)
454 {
455 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
456 i386_linux_init_abi);
457 }