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