* arm-linux-nat.c: Linux -> GNU/Linux when not talking about the
[external/binutils.git] / gdb / arm-linux-tdep.c
1 /* GNU/Linux on ARM target support.
2    Copyright 1999, 2000, 2001, 2002 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 "target.h"
23 #include "value.h"
24 #include "gdbtypes.h"
25 #include "floatformat.h"
26 #include "gdbcore.h"
27 #include "frame.h"
28 #include "regcache.h"
29 #include "doublest.h"
30
31 #include "arm-tdep.h"
32
33 /* For arm_linux_skip_solib_resolver.  */
34 #include "symtab.h"
35 #include "symfile.h"
36 #include "objfiles.h"
37
38 /* Under ARM GNU/Linux the traditional way of performing a breakpoint
39    is to execute a particular software interrupt, rather than use a
40    particular undefined instruction to provoke a trap.  Upon exection
41    of the software interrupt the kernel stops the inferior with a
42    SIGTRAP, and wakes the debugger.  Since ARM GNU/Linux is little
43    endian, and doesn't support Thumb at the moment we only override
44    the ARM little-endian breakpoint.  */
45
46 static const char arm_linux_arm_le_breakpoint[] = {0x01,0x00,0x9f,0xef};
47
48 /* CALL_DUMMY_WORDS:
49    This sequence of words is the instructions
50
51    mov  lr, pc
52    mov  pc, r4
53    swi  bkpt_swi
54
55    Note this is 12 bytes.  */
56
57 LONGEST arm_linux_call_dummy_words[] =
58 {
59   0xe1a0e00f, 0xe1a0f004, 0xef9f001
60 };
61
62 /* Description of the longjmp buffer.  */
63 #define JB_ELEMENT_SIZE         INT_REGISTER_RAW_SIZE
64 #define JB_PC                   21
65
66 /* Extract from an array REGBUF containing the (raw) register state
67    a function return value of type TYPE, and copy that, in virtual format,
68    into VALBUF.  */
69
70 void
71 arm_linux_extract_return_value (struct type *type,
72                                 char regbuf[REGISTER_BYTES],
73                                 char *valbuf)
74 {
75   /* ScottB: This needs to be looked at to handle the different
76      floating point emulators on ARM GNU/Linux.  Right now the code
77      assumes that fetch inferior registers does the right thing for
78      GDB.  I suspect this won't handle NWFPE registers correctly, nor
79      will the default ARM version (arm_extract_return_value()).  */
80
81   int regnum = ((TYPE_CODE_FLT == TYPE_CODE (type))
82                 ? ARM_F0_REGNUM : ARM_A1_REGNUM);
83   memcpy (valbuf, &regbuf[REGISTER_BYTE (regnum)], TYPE_LENGTH (type));
84 }
85
86 /* Note: ScottB
87
88    This function does not support passing parameters using the FPA
89    variant of the APCS.  It passes any floating point arguments in the
90    general registers and/or on the stack.
91    
92    FIXME:  This and arm_push_arguments should be merged.  However this 
93            function breaks on a little endian host, big endian target
94            using the COFF file format.  ELF is ok.  
95            
96            ScottB.  */
97            
98 /* Addresses for calling Thumb functions have the bit 0 set.
99    Here are some macros to test, set, or clear bit 0 of addresses.  */
100 #define IS_THUMB_ADDR(addr)     ((addr) & 1)
101 #define MAKE_THUMB_ADDR(addr)   ((addr) | 1)
102 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
103           
104 CORE_ADDR
105 arm_linux_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
106                           int struct_return, CORE_ADDR struct_addr)
107 {
108   char *fp;
109   int argnum, argreg, nstack_size;
110
111   /* Walk through the list of args and determine how large a temporary
112      stack is required.  Need to take care here as structs may be
113      passed on the stack, and we have to to push them.  */
114   nstack_size = -4 * REGISTER_SIZE;     /* Some arguments go into A1-A4.  */
115
116   if (struct_return)                    /* The struct address goes in A1.  */
117     nstack_size += REGISTER_SIZE;
118
119   /* Walk through the arguments and add their size to nstack_size.  */
120   for (argnum = 0; argnum < nargs; argnum++)
121     {
122       int len;
123       struct type *arg_type;
124
125       arg_type = check_typedef (VALUE_TYPE (args[argnum]));
126       len = TYPE_LENGTH (arg_type);
127
128       /* ANSI C code passes float arguments as integers, K&R code
129          passes float arguments as doubles.  Correct for this here.  */
130       if (TYPE_CODE_FLT == TYPE_CODE (arg_type) && REGISTER_SIZE == len)
131         nstack_size += FP_REGISTER_VIRTUAL_SIZE;
132       else
133         nstack_size += len;
134     }
135
136   /* Allocate room on the stack, and initialize our stack frame
137      pointer.  */
138   fp = NULL;
139   if (nstack_size > 0)
140     {
141       sp -= nstack_size;
142       fp = (char *) sp;
143     }
144
145   /* Initialize the integer argument register pointer.  */
146   argreg = ARM_A1_REGNUM;
147
148   /* The struct_return pointer occupies the first parameter passing
149      register.  */
150   if (struct_return)
151     write_register (argreg++, struct_addr);
152
153   /* Process arguments from left to right.  Store as many as allowed
154      in the parameter passing registers (A1-A4), and save the rest on
155      the temporary stack.  */
156   for (argnum = 0; argnum < nargs; argnum++)
157     {
158       int len;
159       char *val;
160       CORE_ADDR regval;
161       enum type_code typecode;
162       struct type *arg_type, *target_type;
163
164       arg_type = check_typedef (VALUE_TYPE (args[argnum]));
165       target_type = TYPE_TARGET_TYPE (arg_type);
166       len = TYPE_LENGTH (arg_type);
167       typecode = TYPE_CODE (arg_type);
168       val = (char *) VALUE_CONTENTS (args[argnum]);
169
170       /* ANSI C code passes float arguments as integers, K&R code
171          passes float arguments as doubles.  The .stabs record for 
172          for ANSI prototype floating point arguments records the
173          type as FP_INTEGER, while a K&R style (no prototype)
174          .stabs records the type as FP_FLOAT.  In this latter case
175          the compiler converts the float arguments to double before
176          calling the function.  */
177       if (TYPE_CODE_FLT == typecode && REGISTER_SIZE == len)
178         {
179           DOUBLEST dblval;
180           dblval = extract_floating (val, len);
181           len = TARGET_DOUBLE_BIT / TARGET_CHAR_BIT;
182           val = alloca (len);
183           store_floating (val, len, dblval);
184         }
185
186       /* If the argument is a pointer to a function, and it is a Thumb
187          function, set the low bit of the pointer.  */
188       if (TYPE_CODE_PTR == typecode
189           && NULL != target_type
190           && TYPE_CODE_FUNC == TYPE_CODE (target_type))
191         {
192           CORE_ADDR regval = extract_address (val, len);
193           if (arm_pc_is_thumb (regval))
194             store_address (val, len, MAKE_THUMB_ADDR (regval));
195         }
196
197       /* Copy the argument to general registers or the stack in
198          register-sized pieces.  Large arguments are split between
199          registers and stack.  */
200       while (len > 0)
201         {
202           int partial_len = len < REGISTER_SIZE ? len : REGISTER_SIZE;
203
204           if (argreg <= ARM_LAST_ARG_REGNUM)
205             {
206               /* It's an argument being passed in a general register.  */
207               regval = extract_address (val, partial_len);
208               write_register (argreg++, regval);
209             }
210           else
211             {
212               /* Push the arguments onto the stack.  */
213               write_memory ((CORE_ADDR) fp, val, REGISTER_SIZE);
214               fp += REGISTER_SIZE;
215             }
216
217           len -= partial_len;
218           val += partial_len;
219         }
220     }
221
222   /* Return adjusted stack pointer.  */
223   return sp;
224 }
225
226 /*
227    Dynamic Linking on ARM GNU/Linux
228    --------------------------------
229
230    Note: PLT = procedure linkage table
231    GOT = global offset table
232
233    As much as possible, ELF dynamic linking defers the resolution of
234    jump/call addresses until the last minute. The technique used is
235    inspired by the i386 ELF design, and is based on the following
236    constraints.
237
238    1) The calling technique should not force a change in the assembly
239    code produced for apps; it MAY cause changes in the way assembly
240    code is produced for position independent code (i.e. shared
241    libraries).
242
243    2) The technique must be such that all executable areas must not be
244    modified; and any modified areas must not be executed.
245
246    To do this, there are three steps involved in a typical jump:
247
248    1) in the code
249    2) through the PLT
250    3) using a pointer from the GOT
251
252    When the executable or library is first loaded, each GOT entry is
253    initialized to point to the code which implements dynamic name
254    resolution and code finding.  This is normally a function in the
255    program interpreter (on ARM GNU/Linux this is usually
256    ld-linux.so.2, but it does not have to be).  On the first
257    invocation, the function is located and the GOT entry is replaced
258    with the real function address.  Subsequent calls go through steps
259    1, 2 and 3 and end up calling the real code.
260
261    1) In the code: 
262
263    b    function_call
264    bl   function_call
265
266    This is typical ARM code using the 26 bit relative branch or branch
267    and link instructions.  The target of the instruction
268    (function_call is usually the address of the function to be called.
269    In position independent code, the target of the instruction is
270    actually an entry in the PLT when calling functions in a shared
271    library.  Note that this call is identical to a normal function
272    call, only the target differs.
273
274    2) In the PLT:
275
276    The PLT is a synthetic area, created by the linker. It exists in
277    both executables and libraries. It is an array of stubs, one per
278    imported function call. It looks like this:
279
280    PLT[0]:
281    str     lr, [sp, #-4]!       @push the return address (lr)
282    ldr     lr, [pc, #16]   @load from 6 words ahead
283    add     lr, pc, lr      @form an address for GOT[0]
284    ldr     pc, [lr, #8]!   @jump to the contents of that addr
285
286    The return address (lr) is pushed on the stack and used for
287    calculations.  The load on the second line loads the lr with
288    &GOT[3] - . - 20.  The addition on the third leaves:
289
290    lr = (&GOT[3] - . - 20) + (. + 8)
291    lr = (&GOT[3] - 12)
292    lr = &GOT[0]
293
294    On the fourth line, the pc and lr are both updated, so that:
295
296    pc = GOT[2]
297    lr = &GOT[0] + 8
298    = &GOT[2]
299
300    NOTE: PLT[0] borrows an offset .word from PLT[1]. This is a little
301    "tight", but allows us to keep all the PLT entries the same size.
302
303    PLT[n+1]:
304    ldr     ip, [pc, #4]    @load offset from gotoff
305    add     ip, pc, ip      @add the offset to the pc
306    ldr     pc, [ip]        @jump to that address
307    gotoff: .word   GOT[n+3] - .
308
309    The load on the first line, gets an offset from the fourth word of
310    the PLT entry.  The add on the second line makes ip = &GOT[n+3],
311    which contains either a pointer to PLT[0] (the fixup trampoline) or
312    a pointer to the actual code.
313
314    3) In the GOT:
315
316    The GOT contains helper pointers for both code (PLT) fixups and
317    data fixups.  The first 3 entries of the GOT are special. The next
318    M entries (where M is the number of entries in the PLT) belong to
319    the PLT fixups. The next D (all remaining) entries belong to
320    various data fixups. The actual size of the GOT is 3 + M + D.
321
322    The GOT is also a synthetic area, created by the linker. It exists
323    in both executables and libraries.  When the GOT is first
324    initialized , all the GOT entries relating to PLT fixups are
325    pointing to code back at PLT[0].
326
327    The special entries in the GOT are:
328
329    GOT[0] = linked list pointer used by the dynamic loader
330    GOT[1] = pointer to the reloc table for this module
331    GOT[2] = pointer to the fixup/resolver code
332
333    The first invocation of function call comes through and uses the
334    fixup/resolver code.  On the entry to the fixup/resolver code:
335
336    ip = &GOT[n+3]
337    lr = &GOT[2]
338    stack[0] = return address (lr) of the function call
339    [r0, r1, r2, r3] are still the arguments to the function call
340
341    This is enough information for the fixup/resolver code to work
342    with.  Before the fixup/resolver code returns, it actually calls
343    the requested function and repairs &GOT[n+3].  */
344
345 /* Find the minimal symbol named NAME, and return both the minsym
346    struct and its objfile.  This probably ought to be in minsym.c, but
347    everything there is trying to deal with things like C++ and
348    SOFUN_ADDRESS_MAYBE_TURQUOISE, ...  Since this is so simple, it may
349    be considered too special-purpose for general consumption.  */
350
351 static struct minimal_symbol *
352 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
353 {
354   struct objfile *objfile;
355
356   ALL_OBJFILES (objfile)
357     {
358       struct minimal_symbol *msym;
359
360       ALL_OBJFILE_MSYMBOLS (objfile, msym)
361         {
362           if (SYMBOL_NAME (msym)
363               && STREQ (SYMBOL_NAME (msym), name))
364             {
365               *objfile_p = objfile;
366               return msym;
367             }
368         }
369     }
370
371   return 0;
372 }
373
374
375 static CORE_ADDR
376 skip_hurd_resolver (CORE_ADDR pc)
377 {
378   /* The HURD dynamic linker is part of the GNU C library, so many
379      GNU/Linux distributions use it.  (All ELF versions, as far as I
380      know.)  An unresolved PLT entry points to "_dl_runtime_resolve",
381      which calls "fixup" to patch the PLT, and then passes control to
382      the function.
383
384      We look for the symbol `_dl_runtime_resolve', and find `fixup' in
385      the same objfile.  If we are at the entry point of `fixup', then
386      we set a breakpoint at the return address (at the top of the
387      stack), and continue.
388   
389      It's kind of gross to do all these checks every time we're
390      called, since they don't change once the executable has gotten
391      started.  But this is only a temporary hack --- upcoming versions
392      of GNU/Linux will provide a portable, efficient interface for
393      debugging programs that use shared libraries.  */
394
395   struct objfile *objfile;
396   struct minimal_symbol *resolver 
397     = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
398
399   if (resolver)
400     {
401       struct minimal_symbol *fixup
402         = lookup_minimal_symbol ("fixup", NULL, objfile);
403
404       if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
405         return (SAVED_PC_AFTER_CALL (get_current_frame ()));
406     }
407
408   return 0;
409 }      
410
411 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
412    This function:
413    1) decides whether a PLT has sent us into the linker to resolve
414       a function reference, and 
415    2) if so, tells us where to set a temporary breakpoint that will
416       trigger when the dynamic linker is done.  */
417
418 CORE_ADDR
419 arm_linux_skip_solib_resolver (CORE_ADDR pc)
420 {
421   CORE_ADDR result;
422
423   /* Plug in functions for other kinds of resolvers here.  */
424   result = skip_hurd_resolver (pc);
425
426   if (result)
427     return result;
428   
429   return 0;
430 }
431
432 /* The constants below were determined by examining the following files
433    in the linux kernel sources:
434
435       arch/arm/kernel/signal.c
436           - see SWI_SYS_SIGRETURN and SWI_SYS_RT_SIGRETURN
437       include/asm-arm/unistd.h
438           - see __NR_sigreturn, __NR_rt_sigreturn, and __NR_SYSCALL_BASE */
439
440 #define ARM_LINUX_SIGRETURN_INSTR       0xef900077
441 #define ARM_LINUX_RT_SIGRETURN_INSTR    0xef9000ad
442
443 /* arm_linux_in_sigtramp determines if PC points at one of the
444    instructions which cause control to return to the Linux kernel upon
445    return from a signal handler.  FUNC_NAME is unused.  */
446
447 int
448 arm_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
449 {
450   unsigned long inst;
451
452   inst = read_memory_integer (pc, 4);
453
454   return (inst == ARM_LINUX_SIGRETURN_INSTR
455           || inst == ARM_LINUX_RT_SIGRETURN_INSTR);
456
457 }
458
459 /* arm_linux_sigcontext_register_address returns the address in the
460    sigcontext of register REGNO given a stack pointer value SP and
461    program counter value PC.  The value 0 is returned if PC is not
462    pointing at one of the signal return instructions or if REGNO is
463    not saved in the sigcontext struct.  */
464
465 CORE_ADDR
466 arm_linux_sigcontext_register_address (CORE_ADDR sp, CORE_ADDR pc, int regno)
467 {
468   unsigned long inst;
469   CORE_ADDR reg_addr = 0;
470
471   inst = read_memory_integer (pc, 4);
472
473   if (inst == ARM_LINUX_SIGRETURN_INSTR
474       || inst == ARM_LINUX_RT_SIGRETURN_INSTR)
475     {
476       CORE_ADDR sigcontext_addr;
477
478       /* The sigcontext structure is at different places for the two
479          signal return instructions.  For ARM_LINUX_SIGRETURN_INSTR,
480          it starts at the SP value.  For ARM_LINUX_RT_SIGRETURN_INSTR,
481          it is at SP+8.  For the latter instruction, it may also be
482          the case that the address of this structure may be determined
483          by reading the 4 bytes at SP, but I'm not convinced this is
484          reliable.
485
486          In any event, these magic constants (0 and 8) may be
487          determined by examining struct sigframe and struct
488          rt_sigframe in arch/arm/kernel/signal.c in the Linux kernel
489          sources.  */
490
491       if (inst == ARM_LINUX_RT_SIGRETURN_INSTR)
492         sigcontext_addr = sp + 8;
493       else /* inst == ARM_LINUX_SIGRETURN_INSTR */
494         sigcontext_addr = sp + 0;
495
496       /* The layout of the sigcontext structure for ARM GNU/Linux is
497          in include/asm-arm/sigcontext.h in the Linux kernel sources.
498
499          There are three 4-byte fields which precede the saved r0
500          field.  (This accounts for the 12 in the code below.)  The
501          sixteen registers (4 bytes per field) follow in order.  The
502          PSR value follows the sixteen registers which accounts for
503          the constant 19 below. */
504
505       if (0 <= regno && regno <= ARM_PC_REGNUM)
506         reg_addr = sigcontext_addr + 12 + (4 * regno);
507       else if (regno == ARM_PS_REGNUM)
508         reg_addr = sigcontext_addr + 19 * 4;
509     }
510
511   return reg_addr;
512 }
513
514 static void
515 arm_linux_init_abi (struct gdbarch_info info,
516                     struct gdbarch *gdbarch)
517 {
518   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
519
520   tdep->lowest_pc = 0x8000;
521   tdep->arm_breakpoint = arm_linux_arm_le_breakpoint;
522   tdep->arm_breakpoint_size = sizeof (arm_linux_arm_le_breakpoint);
523
524   tdep->jb_pc = JB_PC;
525   tdep->jb_elt_size = JB_ELEMENT_SIZE;
526 }
527
528 void
529 _initialize_arm_linux_tdep (void)
530 {
531   arm_gdbarch_register_os_abi (ARM_ABI_LINUX, arm_linux_init_abi);
532 }