* arch-utils.h: Update copyright.
[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 /* Return the name of register REG.  */
37
38 char *
39 i386_linux_register_name (int reg)
40 {
41   /* Deal with the extra "orig_eax" pseudo register.  */
42   if (reg == I386_LINUX_ORIG_EAX_REGNUM)
43     return "orig_eax";
44
45   return i386_register_name (reg);
46 }
47
48 int
49 i386_linux_register_byte (int reg)
50 {
51   /* Deal with the extra "orig_eax" pseudo register.  */
52   if (reg == I386_LINUX_ORIG_EAX_REGNUM)
53     return (i386_register_byte (I386_LINUX_ORIG_EAX_REGNUM - 1)
54             + i386_register_raw_size (I386_LINUX_ORIG_EAX_REGNUM - 1));
55
56   return i386_register_byte (reg);
57 }
58
59 int
60 i386_linux_register_raw_size (int reg)
61 {
62   /* Deal with the extra "orig_eax" pseudo register.  */
63   if (reg == I386_LINUX_ORIG_EAX_REGNUM)
64     return 4;
65
66   return i386_register_raw_size (reg);
67 }
68 \f
69 /* Recognizing signal handler frames.  */
70
71 /* GNU/Linux has two flavors of signals.  Normal signal handlers, and
72    "realtime" (RT) signals.  The RT signals can provide additional
73    information to the signal handler if the SA_SIGINFO flag is set
74    when establishing a signal handler using `sigaction'.  It is not
75    unlikely that future versions of GNU/Linux will support SA_SIGINFO
76    for normal signals too.  */
77
78 /* When the i386 Linux kernel calls a signal handler and the
79    SA_RESTORER flag isn't set, the return address points to a bit of
80    code on the stack.  This function returns whether the PC appears to
81    be within this bit of code.
82
83    The instruction sequence for normal signals is
84        pop    %eax
85        mov    $0x77,%eax
86        int    $0x80
87    or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
88
89    Checking for the code sequence should be somewhat reliable, because
90    the effect is to call the system call sigreturn.  This is unlikely
91    to occur anywhere other than a signal trampoline.
92
93    It kind of sucks that we have to read memory from the process in
94    order to identify a signal trampoline, but there doesn't seem to be
95    any other way.  The PC_IN_SIGTRAMP macro in tm-linux.h arranges to
96    only call us if no function name could be identified, which should
97    be the case since the code is on the stack.
98
99    Detection of signal trampolines for handlers that set the
100    SA_RESTORER flag is in general not possible.  Unfortunately this is
101    what the GNU C Library has been doing for quite some time now.
102    However, as of version 2.1.2, the GNU C Library uses signal
103    trampolines (named __restore and __restore_rt) that are identical
104    to the ones used by the kernel.  Therefore, these trampolines are
105    supported too.  */
106
107 #define LINUX_SIGTRAMP_INSN0 (0x58)     /* pop %eax */
108 #define LINUX_SIGTRAMP_OFFSET0 (0)
109 #define LINUX_SIGTRAMP_INSN1 (0xb8)     /* mov $NNNN,%eax */
110 #define LINUX_SIGTRAMP_OFFSET1 (1)
111 #define LINUX_SIGTRAMP_INSN2 (0xcd)     /* int */
112 #define LINUX_SIGTRAMP_OFFSET2 (6)
113
114 static const unsigned char linux_sigtramp_code[] =
115 {
116   LINUX_SIGTRAMP_INSN0,                                 /* pop %eax */
117   LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00,         /* mov $0x77,%eax */
118   LINUX_SIGTRAMP_INSN2, 0x80                            /* int $0x80 */
119 };
120
121 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
122
123 /* If PC is in a sigtramp routine, return the address of the start of
124    the routine.  Otherwise, return 0.  */
125
126 static CORE_ADDR
127 i386_linux_sigtramp_start (CORE_ADDR pc)
128 {
129   unsigned char buf[LINUX_SIGTRAMP_LEN];
130
131   /* We only recognize a signal trampoline if PC is at the start of
132      one of the three instructions.  We optimize for finding the PC at
133      the start, as will be the case when the trampoline is not the
134      first frame on the stack.  We assume that in the case where the
135      PC is not at the start of the instruction sequence, there will be
136      a few trailing readable bytes on the stack.  */
137
138   if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
139     return 0;
140
141   if (buf[0] != LINUX_SIGTRAMP_INSN0)
142     {
143       int adjust;
144
145       switch (buf[0])
146         {
147         case LINUX_SIGTRAMP_INSN1:
148           adjust = LINUX_SIGTRAMP_OFFSET1;
149           break;
150         case LINUX_SIGTRAMP_INSN2:
151           adjust = LINUX_SIGTRAMP_OFFSET2;
152           break;
153         default:
154           return 0;
155         }
156
157       pc -= adjust;
158
159       if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
160         return 0;
161     }
162
163   if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
164     return 0;
165
166   return pc;
167 }
168
169 /* This function does the same for RT signals.  Here the instruction
170    sequence is
171        mov    $0xad,%eax
172        int    $0x80
173    or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
174
175    The effect is to call the system call rt_sigreturn.  */
176
177 #define LINUX_RT_SIGTRAMP_INSN0 (0xb8)  /* mov $NNNN,%eax */
178 #define LINUX_RT_SIGTRAMP_OFFSET0 (0)
179 #define LINUX_RT_SIGTRAMP_INSN1 (0xcd)  /* int */
180 #define LINUX_RT_SIGTRAMP_OFFSET1 (5)
181
182 static const unsigned char linux_rt_sigtramp_code[] =
183 {
184   LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00,      /* mov $0xad,%eax */
185   LINUX_RT_SIGTRAMP_INSN1, 0x80                         /* int $0x80 */
186 };
187
188 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
189
190 /* If PC is in a RT sigtramp routine, return the address of the start
191    of the routine.  Otherwise, return 0.  */
192
193 static CORE_ADDR
194 i386_linux_rt_sigtramp_start (CORE_ADDR pc)
195 {
196   unsigned char buf[LINUX_RT_SIGTRAMP_LEN];
197
198   /* We only recognize a signal trampoline if PC is at the start of
199      one of the two instructions.  We optimize for finding the PC at
200      the start, as will be the case when the trampoline is not the
201      first frame on the stack.  We assume that in the case where the
202      PC is not at the start of the instruction sequence, there will be
203      a few trailing readable bytes on the stack.  */
204
205   if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
206     return 0;
207
208   if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
209     {
210       if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
211         return 0;
212
213       pc -= LINUX_RT_SIGTRAMP_OFFSET1;
214
215       if (read_memory_nobpt (pc, (char *) buf, LINUX_RT_SIGTRAMP_LEN) != 0)
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 PC is in a GNU/Linux sigtramp routine.  */
226
227 int
228 i386_linux_in_sigtramp (CORE_ADDR pc, char *name)
229 {
230   if (name)
231     return STREQ ("__restore", name) || STREQ ("__restore_rt", name);
232   
233   return (i386_linux_sigtramp_start (pc) != 0
234           || i386_linux_rt_sigtramp_start (pc) != 0);
235 }
236
237 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
238    address of the associated sigcontext structure.  */
239
240 CORE_ADDR
241 i386_linux_sigcontext_addr (struct frame_info *frame)
242 {
243   CORE_ADDR pc;
244
245   pc = i386_linux_sigtramp_start (frame->pc);
246   if (pc)
247     {
248       CORE_ADDR sp;
249
250       if (frame->next)
251         /* If this isn't the top frame, the next frame must be for the
252            signal handler itself.  The sigcontext structure lives on
253            the stack, right after the signum argument.  */
254         return frame->next->frame + 12;
255
256       /* This is the top frame.  We'll have to find the address of the
257          sigcontext structure by looking at the stack pointer.  Keep
258          in mind that the first instruction of the sigtramp code is
259          "pop %eax".  If the PC is at this instruction, adjust the
260          returned value accordingly.  */
261       sp = read_register (SP_REGNUM);
262       if (pc == frame->pc)
263         return sp + 4;
264       return sp;
265     }
266
267   pc = i386_linux_rt_sigtramp_start (frame->pc);
268   if (pc)
269     {
270       if (frame->next)
271         /* If this isn't the top frame, the next frame must be for the
272            signal handler itself.  The sigcontext structure is part of
273            the user context.  A pointer to the user context is passed
274            as the third argument to the signal handler.  */
275         return read_memory_integer (frame->next->frame + 16, 4) + 20;
276
277       /* This is the top frame.  Again, use the stack pointer to find
278          the address of the sigcontext structure.  */
279       return read_memory_integer (read_register (SP_REGNUM) + 8, 4) + 20;
280     }
281
282   error ("Couldn't recognize signal trampoline.");
283   return 0;
284 }
285
286 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>.  */
287 #define LINUX_SIGCONTEXT_PC_OFFSET (56)
288
289 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
290    saved program counter.  */
291
292 static CORE_ADDR
293 i386_linux_sigtramp_saved_pc (struct frame_info *frame)
294 {
295   CORE_ADDR addr;
296   addr = i386_linux_sigcontext_addr (frame);
297   return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 4);
298 }
299
300 /* Offset to saved SP in sigcontext, from <asm/sigcontext.h>.  */
301 #define LINUX_SIGCONTEXT_SP_OFFSET (28)
302
303 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
304    saved stack pointer.  */
305
306 static CORE_ADDR
307 i386_linux_sigtramp_saved_sp (struct frame_info *frame)
308 {
309   CORE_ADDR addr;
310   addr = i386_linux_sigcontext_addr (frame);
311   return read_memory_integer (addr + LINUX_SIGCONTEXT_SP_OFFSET, 4);
312 }
313
314 /* Signal trampolines don't have a meaningful frame.  As in
315    "i386/tm-i386.h", the frame pointer value we use is actually the
316    frame pointer of the calling frame -- that is, the frame which was
317    in progress when the signal trampoline was entered.  GDB mostly
318    treats this frame pointer value as a magic cookie.  We detect the
319    case of a signal trampoline by looking at the SIGNAL_HANDLER_CALLER
320    field, which is set based on PC_IN_SIGTRAMP.
321
322    When a signal trampoline is invoked from a frameless function, we
323    essentially have two frameless functions in a row.  In this case,
324    we use the same magic cookie for three frames in a row.  We detect
325    this case by seeing whether the next frame has
326    SIGNAL_HANDLER_CALLER set, and, if it does, checking whether the
327    current frame is actually frameless.  In this case, we need to get
328    the PC by looking at the SP register value stored in the signal
329    context.
330
331    This should work in most cases except in horrible situations where
332    a signal occurs just as we enter a function but before the frame
333    has been set up.  */
334
335 #define FRAMELESS_SIGNAL(frame)                                 \
336   ((frame)->next != NULL                                        \
337    && (frame)->next->signal_handler_caller                      \
338    && frameless_look_for_prologue (frame))
339
340 CORE_ADDR
341 i386_linux_frame_chain (struct frame_info *frame)
342 {
343   if (frame->signal_handler_caller || FRAMELESS_SIGNAL (frame))
344     return frame->frame;
345
346   if (! inside_entry_file (frame->pc))
347     return read_memory_unsigned_integer (frame->frame, 4);
348
349   return 0;
350 }
351
352 /* Return the saved program counter for FRAME.  */
353
354 CORE_ADDR
355 i386_linux_frame_saved_pc (struct frame_info *frame)
356 {
357   if (frame->signal_handler_caller)
358     return i386_linux_sigtramp_saved_pc (frame);
359
360   if (FRAMELESS_SIGNAL (frame))
361     {
362       CORE_ADDR sp = i386_linux_sigtramp_saved_sp (frame->next);
363       return read_memory_unsigned_integer (sp, 4);
364     }
365
366   return read_memory_unsigned_integer (frame->frame + 4, 4);
367 }
368
369 /* Immediately after a function call, return the saved pc.  */
370
371 CORE_ADDR
372 i386_linux_saved_pc_after_call (struct frame_info *frame)
373 {
374   if (frame->signal_handler_caller)
375     return i386_linux_sigtramp_saved_pc (frame);
376
377   return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
378 }
379
380 /* Set the program counter for process PTID to PC.  */
381
382 void
383 i386_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
384 {
385   write_register_pid (PC_REGNUM, pc, ptid);
386
387   /* We must be careful with modifying the program counter.  If we
388      just interrupted a system call, the kernel might try to restart
389      it when we resume the inferior.  On restarting the system call,
390      the kernel will try backing up the program counter even though it
391      no longer points at the system call.  This typically results in a
392      SIGSEGV or SIGILL.  We can prevent this by writing `-1' in the
393      "orig_eax" pseudo-register.
394
395      Note that "orig_eax" is saved when setting up a dummy call frame.
396      This means that it is properly restored when that frame is
397      popped, and that the interrupted system call will be restarted
398      when we resume the inferior on return from a function call from
399      within GDB.  In all other cases the system call will not be
400      restarted.  */
401   write_register_pid (I386_LINUX_ORIG_EAX_REGNUM, -1, ptid);
402 }
403 \f
404 /* Calling functions in shared libraries.  */
405
406 /* Find the minimal symbol named NAME, and return both the minsym
407    struct and its objfile.  This probably ought to be in minsym.c, but
408    everything there is trying to deal with things like C++ and
409    SOFUN_ADDRESS_MAYBE_TURQUOISE, ...  Since this is so simple, it may
410    be considered too special-purpose for general consumption.  */
411
412 static struct minimal_symbol *
413 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
414 {
415   struct objfile *objfile;
416
417   ALL_OBJFILES (objfile)
418     {
419       struct minimal_symbol *msym;
420
421       ALL_OBJFILE_MSYMBOLS (objfile, msym)
422         {
423           if (SYMBOL_NAME (msym)
424               && STREQ (SYMBOL_NAME (msym), name))
425             {
426               *objfile_p = objfile;
427               return msym;
428             }
429         }
430     }
431
432   return 0;
433 }
434
435 static CORE_ADDR
436 skip_hurd_resolver (CORE_ADDR pc)
437 {
438   /* The HURD dynamic linker is part of the GNU C library, so many
439      GNU/Linux distributions use it.  (All ELF versions, as far as I
440      know.)  An unresolved PLT entry points to "_dl_runtime_resolve",
441      which calls "fixup" to patch the PLT, and then passes control to
442      the function.
443
444      We look for the symbol `_dl_runtime_resolve', and find `fixup' in
445      the same objfile.  If we are at the entry point of `fixup', then
446      we set a breakpoint at the return address (at the top of the
447      stack), and continue.
448   
449      It's kind of gross to do all these checks every time we're
450      called, since they don't change once the executable has gotten
451      started.  But this is only a temporary hack --- upcoming versions
452      of GNU/Linux will provide a portable, efficient interface for
453      debugging programs that use shared libraries.  */
454
455   struct objfile *objfile;
456   struct minimal_symbol *resolver 
457     = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
458
459   if (resolver)
460     {
461       struct minimal_symbol *fixup
462         = lookup_minimal_symbol ("fixup", NULL, objfile);
463
464       if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
465         return (SAVED_PC_AFTER_CALL (get_current_frame ()));
466     }
467
468   return 0;
469 }      
470
471 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
472    This function:
473    1) decides whether a PLT has sent us into the linker to resolve
474       a function reference, and 
475    2) if so, tells us where to set a temporary breakpoint that will
476       trigger when the dynamic linker is done.  */
477
478 CORE_ADDR
479 i386_linux_skip_solib_resolver (CORE_ADDR pc)
480 {
481   CORE_ADDR result;
482
483   /* Plug in functions for other kinds of resolvers here.  */
484   result = skip_hurd_resolver (pc);
485   if (result)
486     return result;
487
488   return 0;
489 }
490
491 /* Fetch (and possibly build) an appropriate link_map_offsets
492    structure for native GNU/Linux x86 targets using the struct offsets
493    defined in link.h (but without actual reference to that file).
494
495    This makes it possible to access GNU/Linux x86 shared libraries
496    from a GDB that was not built on an GNU/Linux x86 host (for cross
497    debugging).  */
498
499 struct link_map_offsets *
500 i386_linux_svr4_fetch_link_map_offsets (void)
501 {
502   static struct link_map_offsets lmo;
503   static struct link_map_offsets *lmp = NULL;
504
505   if (lmp == NULL)
506     {
507       lmp = &lmo;
508
509       lmo.r_debug_size = 8;     /* The actual size is 20 bytes, but
510                                    this is all we need.  */
511       lmo.r_map_offset = 4;
512       lmo.r_map_size   = 4;
513
514       lmo.link_map_size = 20;   /* The actual size is 552 bytes, but
515                                    this is all we need.  */
516       lmo.l_addr_offset = 0;
517       lmo.l_addr_size   = 4;
518
519       lmo.l_name_offset = 4;
520       lmo.l_name_size   = 4;
521
522       lmo.l_next_offset = 12;
523       lmo.l_next_size   = 4;
524
525       lmo.l_prev_offset = 16;
526       lmo.l_prev_size   = 4;
527     }
528
529   return lmp;
530 }