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