This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gdb / arm-linux-tdep.c
1 /* GNU/Linux on ARM target support.
2    Copyright 1999, 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 "target.h"
23 #include "value.h"
24 #include "gdbtypes.h"
25 #include "floatformat.h"
26
27 /* For arm_linux_skip_solib_resolver.  */
28 #include "symtab.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31
32 #ifdef GET_LONGJMP_TARGET
33
34 /* Figure out where the longjmp will land.  We expect that we have
35    just entered longjmp and haven't yet altered r0, r1, so the
36    arguments are still in the registers.  (A1_REGNUM) points at the
37    jmp_buf structure from which we extract the pc (JB_PC) that we will
38    land at.  The pc is copied into ADDR.  This routine returns true on
39    success. */
40
41 #define LONGJMP_TARGET_SIZE     sizeof(int)
42 #define JB_ELEMENT_SIZE         sizeof(int)
43 #define JB_SL                   18
44 #define JB_FP                   19
45 #define JB_SP                   20
46 #define JB_PC                   21
47
48 int
49 arm_get_longjmp_target (CORE_ADDR * pc)
50 {
51   CORE_ADDR jb_addr;
52   char buf[LONGJMP_TARGET_SIZE];
53
54   jb_addr = read_register (A1_REGNUM);
55
56   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
57                           LONGJMP_TARGET_SIZE))
58     return 0;
59
60   *pc = extract_address (buf, LONGJMP_TARGET_SIZE);
61   return 1;
62 }
63
64 #endif /* GET_LONGJMP_TARGET */
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 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)) ? F0_REGNUM : A1_REGNUM;
82   memcpy (valbuf, &regbuf[REGISTER_BYTE (regnum)], TYPE_LENGTH (type));
83 }
84
85 /* Note: ScottB
86
87    This function does not support passing parameters using the FPA
88    variant of the APCS.  It passes any floating point arguments in the
89    general registers and/or on the stack.
90    
91    FIXME:  This and arm_push_arguments should be merged.  However this 
92            function breaks on a little endian host, big endian target
93            using the COFF file format.  ELF is ok.  
94            
95            ScottB.  */
96            
97 /* Addresses for calling Thumb functions have the bit 0 set.
98    Here are some macros to test, set, or clear bit 0 of addresses.  */
99 #define IS_THUMB_ADDR(addr)     ((addr) & 1)
100 #define MAKE_THUMB_ADDR(addr)   ((addr) | 1)
101 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
102           
103 CORE_ADDR
104 arm_linux_push_arguments (int nargs, value_ptr * args, CORE_ADDR sp,
105                           int struct_return, CORE_ADDR struct_addr)
106 {
107   char *fp;
108   int argnum, argreg, nstack_size;
109
110   /* Walk through the list of args and determine how large a temporary
111      stack is required.  Need to take care here as structs may be
112      passed on the stack, and we have to to push them.  */
113   nstack_size = -4 * REGISTER_SIZE;     /* Some arguments go into A1-A4.  */
114
115   if (struct_return)                    /* The struct address goes in A1.  */
116     nstack_size += REGISTER_SIZE;
117
118   /* Walk through the arguments and add their size to nstack_size.  */
119   for (argnum = 0; argnum < nargs; argnum++)
120     {
121       int len;
122       struct type *arg_type;
123
124       arg_type = check_typedef (VALUE_TYPE (args[argnum]));
125       len = TYPE_LENGTH (arg_type);
126
127       /* ANSI C code passes float arguments as integers, K&R code
128          passes float arguments as doubles.  Correct for this here.  */
129       if (TYPE_CODE_FLT == TYPE_CODE (arg_type) && REGISTER_SIZE == len)
130         nstack_size += FP_REGISTER_VIRTUAL_SIZE;
131       else
132         nstack_size += len;
133     }
134
135   /* Allocate room on the stack, and initialize our stack frame
136      pointer.  */
137   fp = NULL;
138   if (nstack_size > 0)
139     {
140       sp -= nstack_size;
141       fp = (char *) sp;
142     }
143
144   /* Initialize the integer argument register pointer.  */
145   argreg = A1_REGNUM;
146
147   /* The struct_return pointer occupies the first parameter passing
148      register.  */
149   if (struct_return)
150     write_register (argreg++, struct_addr);
151
152   /* Process arguments from left to right.  Store as many as allowed
153      in the parameter passing registers (A1-A4), and save the rest on
154      the temporary stack.  */
155   for (argnum = 0; argnum < nargs; argnum++)
156     {
157       int len;
158       char *val;
159       double dbl_arg;
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           /* Float argument in buffer is in host format.  Read it and 
180              convert to DOUBLEST, and store it in target double.  */
181           DOUBLEST dblval;
182           
183           len = TARGET_DOUBLE_BIT / TARGET_CHAR_BIT;
184           floatformat_to_doublest (HOST_FLOAT_FORMAT, val, &dblval);
185           store_floating (&dbl_arg, len, dblval);
186           val = (char *) &dbl_arg;
187         }
188
189       /* If the argument is a pointer to a function, and it is a Thumb
190          function, set the low bit of the pointer.  */
191       if (TYPE_CODE_PTR == typecode
192           && NULL != target_type
193           && TYPE_CODE_FUNC == TYPE_CODE (target_type))
194         {
195           CORE_ADDR regval = extract_address (val, len);
196           if (arm_pc_is_thumb (regval))
197             store_address (val, len, MAKE_THUMB_ADDR (regval));
198         }
199
200       /* Copy the argument to general registers or the stack in
201          register-sized pieces.  Large arguments are split between
202          registers and stack.  */
203       while (len > 0)
204         {
205           int partial_len = len < REGISTER_SIZE ? len : REGISTER_SIZE;
206
207           if (argreg <= ARM_LAST_ARG_REGNUM)
208             {
209               /* It's an argument being passed in a general register.  */
210               regval = extract_address (val, partial_len);
211               write_register (argreg++, regval);
212             }
213           else
214             {
215               /* Push the arguments onto the stack.  */
216               write_memory ((CORE_ADDR) fp, val, REGISTER_SIZE);
217               fp += REGISTER_SIZE;
218             }
219
220           len -= partial_len;
221           val += partial_len;
222         }
223     }
224
225   /* Return adjusted stack pointer.  */
226   return sp;
227 }
228
229 /*
230    Dynamic Linking on ARM Linux
231    ----------------------------
232
233    Note: PLT = procedure linkage table
234    GOT = global offset table
235
236    As much as possible, ELF dynamic linking defers the resolution of
237    jump/call addresses until the last minute. The technique used is
238    inspired by the i386 ELF design, and is based on the following
239    constraints.
240
241    1) The calling technique should not force a change in the assembly
242    code produced for apps; it MAY cause changes in the way assembly
243    code is produced for position independent code (i.e. shared
244    libraries).
245
246    2) The technique must be such that all executable areas must not be
247    modified; and any modified areas must not be executed.
248
249    To do this, there are three steps involved in a typical jump:
250
251    1) in the code
252    2) through the PLT
253    3) using a pointer from the GOT
254
255    When the executable or library is first loaded, each GOT entry is
256    initialized to point to the code which implements dynamic name
257    resolution and code finding.  This is normally a function in the
258    program interpreter (on ARM Linux this is usually ld-linux.so.2,
259    but it does not have to be).  On the first invocation, the function
260    is located and the GOT entry is replaced with the real function
261    address.  Subsequent calls go through steps 1, 2 and 3 and end up
262    calling the real code.
263
264    1) In the code: 
265
266    b    function_call
267    bl   function_call
268
269    This is typical ARM code using the 26 bit relative branch or branch
270    and link instructions.  The target of the instruction
271    (function_call is usually the address of the function to be called.
272    In position independent code, the target of the instruction is
273    actually an entry in the PLT when calling functions in a shared
274    library.  Note that this call is identical to a normal function
275    call, only the target differs.
276
277    2) In the PLT:
278
279    The PLT is a synthetic area, created by the linker. It exists in
280    both executables and libraries. It is an array of stubs, one per
281    imported function call. It looks like this:
282
283    PLT[0]:
284    str     lr, [sp, #-4]!       @push the return address (lr)
285    ldr     lr, [pc, #16]   @load from 6 words ahead
286    add     lr, pc, lr      @form an address for GOT[0]
287    ldr     pc, [lr, #8]!   @jump to the contents of that addr
288
289    The return address (lr) is pushed on the stack and used for
290    calculations.  The load on the second line loads the lr with
291    &GOT[3] - . - 20.  The addition on the third leaves:
292
293    lr = (&GOT[3] - . - 20) + (. + 8)
294    lr = (&GOT[3] - 12)
295    lr = &GOT[0]
296
297    On the fourth line, the pc and lr are both updated, so that:
298
299    pc = GOT[2]
300    lr = &GOT[0] + 8
301    = &GOT[2]
302
303    NOTE: PLT[0] borrows an offset .word from PLT[1]. This is a little
304    "tight", but allows us to keep all the PLT entries the same size.
305
306    PLT[n+1]:
307    ldr     ip, [pc, #4]    @load offset from gotoff
308    add     ip, pc, ip      @add the offset to the pc
309    ldr     pc, [ip]        @jump to that address
310    gotoff: .word   GOT[n+3] - .
311
312    The load on the first line, gets an offset from the fourth word of
313    the PLT entry.  The add on the second line makes ip = &GOT[n+3],
314    which contains either a pointer to PLT[0] (the fixup trampoline) or
315    a pointer to the actual code.
316
317    3) In the GOT:
318
319    The GOT contains helper pointers for both code (PLT) fixups and
320    data fixups.  The first 3 entries of the GOT are special. The next
321    M entries (where M is the number of entries in the PLT) belong to
322    the PLT fixups. The next D (all remaining) entries belong to
323    various data fixups. The actual size of the GOT is 3 + M + D.
324
325    The GOT is also a synthetic area, created by the linker. It exists
326    in both executables and libraries.  When the GOT is first
327    initialized , all the GOT entries relating to PLT fixups are
328    pointing to code back at PLT[0].
329
330    The special entries in the GOT are:
331
332    GOT[0] = linked list pointer used by the dynamic loader
333    GOT[1] = pointer to the reloc table for this module
334    GOT[2] = pointer to the fixup/resolver code
335
336    The first invocation of function call comes through and uses the
337    fixup/resolver code.  On the entry to the fixup/resolver code:
338
339    ip = &GOT[n+3]
340    lr = &GOT[2]
341    stack[0] = return address (lr) of the function call
342    [r0, r1, r2, r3] are still the arguments to the function call
343
344    This is enough information for the fixup/resolver code to work
345    with.  Before the fixup/resolver code returns, it actually calls
346    the requested function and repairs &GOT[n+3].  */
347
348 /* Find the minimal symbol named NAME, and return both the minsym
349    struct and its objfile.  This probably ought to be in minsym.c, but
350    everything there is trying to deal with things like C++ and
351    SOFUN_ADDRESS_MAYBE_TURQUOISE, ...  Since this is so simple, it may
352    be considered too special-purpose for general consumption.  */
353
354 static struct minimal_symbol *
355 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
356 {
357   struct objfile *objfile;
358
359   ALL_OBJFILES (objfile)
360     {
361       struct minimal_symbol *msym;
362
363       ALL_OBJFILE_MSYMBOLS (objfile, msym)
364         {
365           if (SYMBOL_NAME (msym)
366               && STREQ (SYMBOL_NAME (msym), name))
367             {
368               *objfile_p = objfile;
369               return msym;
370             }
371         }
372     }
373
374   return 0;
375 }
376
377
378 static CORE_ADDR
379 skip_hurd_resolver (CORE_ADDR pc)
380 {
381   /* The HURD dynamic linker is part of the GNU C library, so many
382      GNU/Linux distributions use it.  (All ELF versions, as far as I
383      know.)  An unresolved PLT entry points to "_dl_runtime_resolve",
384      which calls "fixup" to patch the PLT, and then passes control to
385      the function.
386
387      We look for the symbol `_dl_runtime_resolve', and find `fixup' in
388      the same objfile.  If we are at the entry point of `fixup', then
389      we set a breakpoint at the return address (at the top of the
390      stack), and continue.
391   
392      It's kind of gross to do all these checks every time we're
393      called, since they don't change once the executable has gotten
394      started.  But this is only a temporary hack --- upcoming versions
395      of Linux will provide a portable, efficient interface for
396      debugging programs that use shared libraries.  */
397
398   struct objfile *objfile;
399   struct minimal_symbol *resolver 
400     = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
401
402   if (resolver)
403     {
404       struct minimal_symbol *fixup
405         = lookup_minimal_symbol ("fixup", 0, objfile);
406
407       if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
408         return (SAVED_PC_AFTER_CALL (get_current_frame ()));
409     }
410
411   return 0;
412 }      
413
414 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
415    This function:
416    1) decides whether a PLT has sent us into the linker to resolve
417       a function reference, and 
418    2) if so, tells us where to set a temporary breakpoint that will
419       trigger when the dynamic linker is done.  */
420
421 CORE_ADDR
422 arm_linux_skip_solib_resolver (CORE_ADDR pc)
423 {
424   CORE_ADDR result;
425
426   /* Plug in functions for other kinds of resolvers here.  */
427   result = skip_hurd_resolver (pc);
428   printf ("Result = 0x%08x\n");
429   if (result)
430     return result;
431
432   
433   return 0;
434 }
435
436 void
437 _initialize_arm_linux_tdep (void)
438 {
439 }