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