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