gdb
[deliverable/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, 2007, 2008, 2009
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 3 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, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "gdbcore.h"
23 #include "frame.h"
24 #include "value.h"
25 #include "regcache.h"
26 #include "inferior.h"
27 #include "osabi.h"
28 #include "reggroups.h"
29 #include "dwarf2-frame.h"
30 #include "gdb_string.h"
31
32 #include "i386-tdep.h"
33 #include "i386-linux-tdep.h"
34 #include "linux-tdep.h"
35 #include "glibc-tdep.h"
36 #include "solib-svr4.h"
37 #include "symtab.h"
38 #include "arch-utils.h"
39 #include "regset.h"
40
41 /* Supported register note sections. */
42 static struct core_regset_section i386_linux_regset_sections[] =
43 {
44 { ".reg", 144 },
45 { ".reg2", 108 },
46 { ".reg-xfp", 512 },
47 { NULL, 0 }
48 };
49
50 /* Return the name of register REG. */
51
52 static const char *
53 i386_linux_register_name (struct gdbarch *gdbarch, int reg)
54 {
55 /* Deal with the extra "orig_eax" pseudo register. */
56 if (reg == I386_LINUX_ORIG_EAX_REGNUM)
57 return "orig_eax";
58
59 return i386_register_name (gdbarch, reg);
60 }
61
62 /* Return non-zero, when the register is in the corresponding register
63 group. Put the LINUX_ORIG_EAX register in the system group. */
64 static int
65 i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
66 struct reggroup *group)
67 {
68 if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
69 return (group == system_reggroup
70 || group == save_reggroup
71 || group == restore_reggroup);
72 return i386_register_reggroup_p (gdbarch, regnum, group);
73 }
74
75 \f
76 /* Recognizing signal handler frames. */
77
78 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
79 "realtime" (RT) signals. The RT signals can provide additional
80 information to the signal handler if the SA_SIGINFO flag is set
81 when establishing a signal handler using `sigaction'. It is not
82 unlikely that future versions of GNU/Linux will support SA_SIGINFO
83 for normal signals too. */
84
85 /* When the i386 Linux kernel calls a signal handler and the
86 SA_RESTORER flag isn't set, the return address points to a bit of
87 code on the stack. This function returns whether the PC appears to
88 be within this bit of code.
89
90 The instruction sequence for normal signals is
91 pop %eax
92 mov $0x77, %eax
93 int $0x80
94 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
95
96 Checking for the code sequence should be somewhat reliable, because
97 the effect is to call the system call sigreturn. This is unlikely
98 to occur anywhere other than in a signal trampoline.
99
100 It kind of sucks that we have to read memory from the process in
101 order to identify a signal trampoline, but there doesn't seem to be
102 any other way. Therefore we only do the memory reads if no
103 function name could be identified, which should be the case since
104 the code is on the stack.
105
106 Detection of signal trampolines for handlers that set the
107 SA_RESTORER flag is in general not possible. Unfortunately this is
108 what the GNU C Library has been doing for quite some time now.
109 However, as of version 2.1.2, the GNU C Library uses signal
110 trampolines (named __restore and __restore_rt) that are identical
111 to the ones used by the kernel. Therefore, these trampolines are
112 supported too. */
113
114 #define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
115 #define LINUX_SIGTRAMP_OFFSET0 0
116 #define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
117 #define LINUX_SIGTRAMP_OFFSET1 1
118 #define LINUX_SIGTRAMP_INSN2 0xcd /* int */
119 #define LINUX_SIGTRAMP_OFFSET2 6
120
121 static const gdb_byte linux_sigtramp_code[] =
122 {
123 LINUX_SIGTRAMP_INSN0, /* pop %eax */
124 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
125 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
126 };
127
128 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
129
130 /* If THIS_FRAME is a sigtramp routine, return the address of the
131 start of the routine. Otherwise, return 0. */
132
133 static CORE_ADDR
134 i386_linux_sigtramp_start (struct frame_info *this_frame)
135 {
136 CORE_ADDR pc = get_frame_pc (this_frame);
137 gdb_byte buf[LINUX_SIGTRAMP_LEN];
138
139 /* We only recognize a signal trampoline if PC is at the start of
140 one of the three instructions. We optimize for finding the PC at
141 the start, as will be the case when the trampoline is not the
142 first frame on the stack. We assume that in the case where the
143 PC is not at the start of the instruction sequence, there will be
144 a few trailing readable bytes on the stack. */
145
146 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
147 return 0;
148
149 if (buf[0] != LINUX_SIGTRAMP_INSN0)
150 {
151 int adjust;
152
153 switch (buf[0])
154 {
155 case LINUX_SIGTRAMP_INSN1:
156 adjust = LINUX_SIGTRAMP_OFFSET1;
157 break;
158 case LINUX_SIGTRAMP_INSN2:
159 adjust = LINUX_SIGTRAMP_OFFSET2;
160 break;
161 default:
162 return 0;
163 }
164
165 pc -= adjust;
166
167 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
168 return 0;
169 }
170
171 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
172 return 0;
173
174 return pc;
175 }
176
177 /* This function does the same for RT signals. Here the instruction
178 sequence is
179 mov $0xad, %eax
180 int $0x80
181 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
182
183 The effect is to call the system call rt_sigreturn. */
184
185 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
186 #define LINUX_RT_SIGTRAMP_OFFSET0 0
187 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
188 #define LINUX_RT_SIGTRAMP_OFFSET1 5
189
190 static const gdb_byte linux_rt_sigtramp_code[] =
191 {
192 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
193 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
194 };
195
196 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
197
198 /* If THIS_FRAME is an RT sigtramp routine, return the address of the
199 start of the routine. Otherwise, return 0. */
200
201 static CORE_ADDR
202 i386_linux_rt_sigtramp_start (struct frame_info *this_frame)
203 {
204 CORE_ADDR pc = get_frame_pc (this_frame);
205 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
206
207 /* We only recognize a signal trampoline if PC is at the start of
208 one of the two instructions. We optimize for finding the PC at
209 the start, as will be the case when the trampoline is not the
210 first frame on the stack. We assume that in the case where the
211 PC is not at the start of the instruction sequence, there will be
212 a few trailing readable bytes on the stack. */
213
214 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
215 return 0;
216
217 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
218 {
219 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
220 return 0;
221
222 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
223
224 if (!safe_frame_unwind_memory (this_frame, pc, buf,
225 LINUX_RT_SIGTRAMP_LEN))
226 return 0;
227 }
228
229 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
230 return 0;
231
232 return pc;
233 }
234
235 /* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
236 routine. */
237
238 static int
239 i386_linux_sigtramp_p (struct frame_info *this_frame)
240 {
241 CORE_ADDR pc = get_frame_pc (this_frame);
242 char *name;
243
244 find_pc_partial_function (pc, &name, NULL, NULL);
245
246 /* If we have NAME, we can optimize the search. The trampolines are
247 named __restore and __restore_rt. However, they aren't dynamically
248 exported from the shared C library, so the trampoline may appear to
249 be part of the preceding function. This should always be sigaction,
250 __sigaction, or __libc_sigaction (all aliases to the same function). */
251 if (name == NULL || strstr (name, "sigaction") != NULL)
252 return (i386_linux_sigtramp_start (this_frame) != 0
253 || i386_linux_rt_sigtramp_start (this_frame) != 0);
254
255 return (strcmp ("__restore", name) == 0
256 || strcmp ("__restore_rt", name) == 0);
257 }
258
259 /* Return one if the PC of THIS_FRAME is in a signal trampoline which
260 may have DWARF-2 CFI. */
261
262 static int
263 i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
264 struct frame_info *this_frame)
265 {
266 CORE_ADDR pc = get_frame_pc (this_frame);
267 char *name;
268
269 find_pc_partial_function (pc, &name, NULL, NULL);
270
271 /* If a vsyscall DSO is in use, the signal trampolines may have these
272 names. */
273 if (name && (strcmp (name, "__kernel_sigreturn") == 0
274 || strcmp (name, "__kernel_rt_sigreturn") == 0))
275 return 1;
276
277 return 0;
278 }
279
280 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
281 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
282
283 /* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
284 address of the associated sigcontext structure. */
285
286 static CORE_ADDR
287 i386_linux_sigcontext_addr (struct frame_info *this_frame)
288 {
289 CORE_ADDR pc;
290 CORE_ADDR sp;
291 gdb_byte buf[4];
292
293 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
294 sp = extract_unsigned_integer (buf, 4);
295
296 pc = i386_linux_sigtramp_start (this_frame);
297 if (pc)
298 {
299 /* The sigcontext structure lives on the stack, right after
300 the signum argument. We determine the address of the
301 sigcontext structure by looking at the frame's stack
302 pointer. Keep in mind that the first instruction of the
303 sigtramp code is "pop %eax". If the PC is after this
304 instruction, adjust the returned value accordingly. */
305 if (pc == get_frame_pc (this_frame))
306 return sp + 4;
307 return sp;
308 }
309
310 pc = i386_linux_rt_sigtramp_start (this_frame);
311 if (pc)
312 {
313 CORE_ADDR ucontext_addr;
314
315 /* The sigcontext structure is part of the user context. A
316 pointer to the user context is passed as the third argument
317 to the signal handler. */
318 read_memory (sp + 8, buf, 4);
319 ucontext_addr = extract_unsigned_integer (buf, 4);
320 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
321 }
322
323 error (_("Couldn't recognize signal trampoline."));
324 return 0;
325 }
326
327 /* Set the program counter for process PTID to PC. */
328
329 static void
330 i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
331 {
332 regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);
333
334 /* We must be careful with modifying the program counter. If we
335 just interrupted a system call, the kernel might try to restart
336 it when we resume the inferior. On restarting the system call,
337 the kernel will try backing up the program counter even though it
338 no longer points at the system call. This typically results in a
339 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
340 "orig_eax" pseudo-register.
341
342 Note that "orig_eax" is saved when setting up a dummy call frame.
343 This means that it is properly restored when that frame is
344 popped, and that the interrupted system call will be restarted
345 when we resume the inferior on return from a function call from
346 within GDB. In all other cases the system call will not be
347 restarted. */
348 regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
349 }
350 \f
351
352 /* The register sets used in GNU/Linux ELF core-dumps are identical to
353 the register sets in `struct user' that are used for a.out
354 core-dumps. These are also used by ptrace(2). The corresponding
355 types are `elf_gregset_t' for the general-purpose registers (with
356 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
357 for the floating-point registers.
358
359 Those types used to be available under the names `gregset_t' and
360 `fpregset_t' too, and GDB used those names in the past. But those
361 names are now used for the register sets used in the `mcontext_t'
362 type, which have a different size and layout. */
363
364 /* Mapping between the general-purpose registers in `struct user'
365 format and GDB's register cache layout. */
366
367 /* From <sys/reg.h>. */
368 static int i386_linux_gregset_reg_offset[] =
369 {
370 6 * 4, /* %eax */
371 1 * 4, /* %ecx */
372 2 * 4, /* %edx */
373 0 * 4, /* %ebx */
374 15 * 4, /* %esp */
375 5 * 4, /* %ebp */
376 3 * 4, /* %esi */
377 4 * 4, /* %edi */
378 12 * 4, /* %eip */
379 14 * 4, /* %eflags */
380 13 * 4, /* %cs */
381 16 * 4, /* %ss */
382 7 * 4, /* %ds */
383 8 * 4, /* %es */
384 9 * 4, /* %fs */
385 10 * 4, /* %gs */
386 -1, -1, -1, -1, -1, -1, -1, -1,
387 -1, -1, -1, -1, -1, -1, -1, -1,
388 -1, -1, -1, -1, -1, -1, -1, -1,
389 -1,
390 11 * 4 /* "orig_eax" */
391 };
392
393 /* Mapping between the general-purpose registers in `struct
394 sigcontext' format and GDB's register cache layout. */
395
396 /* From <asm/sigcontext.h>. */
397 static int i386_linux_sc_reg_offset[] =
398 {
399 11 * 4, /* %eax */
400 10 * 4, /* %ecx */
401 9 * 4, /* %edx */
402 8 * 4, /* %ebx */
403 7 * 4, /* %esp */
404 6 * 4, /* %ebp */
405 5 * 4, /* %esi */
406 4 * 4, /* %edi */
407 14 * 4, /* %eip */
408 16 * 4, /* %eflags */
409 15 * 4, /* %cs */
410 18 * 4, /* %ss */
411 3 * 4, /* %ds */
412 2 * 4, /* %es */
413 1 * 4, /* %fs */
414 0 * 4 /* %gs */
415 };
416
417 static void
418 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
419 {
420 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
421
422 /* GNU/Linux uses ELF. */
423 i386_elf_init_abi (info, gdbarch);
424
425 /* Since we have the extra "orig_eax" register on GNU/Linux, we have
426 to adjust a few things. */
427
428 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
429 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
430 set_gdbarch_register_name (gdbarch, i386_linux_register_name);
431 set_gdbarch_register_reggroup_p (gdbarch, i386_linux_register_reggroup_p);
432
433 tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
434 tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
435 tdep->sizeof_gregset = 17 * 4;
436
437 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
438
439 tdep->sigtramp_p = i386_linux_sigtramp_p;
440 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
441 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
442 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
443
444 /* N_FUN symbols in shared libaries have 0 for their values and need
445 to be relocated. */
446 set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
447
448 /* GNU/Linux uses SVR4-style shared libraries. */
449 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
450 set_solib_svr4_fetch_link_map_offsets
451 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
452
453 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
454 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
455
456 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
457
458 /* Enable TLS support. */
459 set_gdbarch_fetch_tls_load_module_address (gdbarch,
460 svr4_fetch_objfile_link_map);
461
462 /* Install supported register note sections. */
463 set_gdbarch_core_regset_sections (gdbarch, i386_linux_regset_sections);
464
465 /* Displaced stepping. */
466 set_gdbarch_displaced_step_copy_insn (gdbarch,
467 simple_displaced_step_copy_insn);
468 set_gdbarch_displaced_step_fixup (gdbarch, i386_displaced_step_fixup);
469 set_gdbarch_displaced_step_free_closure (gdbarch,
470 simple_displaced_step_free_closure);
471 set_gdbarch_displaced_step_location (gdbarch,
472 displaced_step_at_entry_point);
473
474 set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
475 }
476
477 /* Provide a prototype to silence -Wmissing-prototypes. */
478 extern void _initialize_i386_linux_tdep (void);
479
480 void
481 _initialize_i386_linux_tdep (void)
482 {
483 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
484 i386_linux_init_abi);
485 }
This page took 0.040323 seconds and 4 git commands to generate.