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