Move arm_skip_solib_resolver from arm-linux-nat.c to arm-linux-tdep.c.
[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 #ifdef GET_LONGJMP_TARGET
28
29 /* Figure out where the longjmp will land.  We expect that we have
30    just entered longjmp and haven't yet altered r0, r1, so the
31    arguments are still in the registers.  (A1_REGNUM) points at the
32    jmp_buf structure from which we extract the pc (JB_PC) that we will
33    land at.  The pc is copied into ADDR.  This routine returns true on
34    success. */
35
36 #define LONGJMP_TARGET_SIZE     sizeof(int)
37 #define JB_ELEMENT_SIZE         sizeof(int)
38 #define JB_SL                   18
39 #define JB_FP                   19
40 #define JB_SP                   20
41 #define JB_PC                   21
42
43 int
44 arm_get_longjmp_target (CORE_ADDR * pc)
45 {
46   CORE_ADDR jb_addr;
47   char buf[LONGJMP_TARGET_SIZE];
48
49   jb_addr = read_register (A1_REGNUM);
50
51   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
52                           LONGJMP_TARGET_SIZE))
53     return 0;
54
55   *pc = extract_address (buf, LONGJMP_TARGET_SIZE);
56   return 1;
57 }
58
59 #endif /* GET_LONGJMP_TARGET */
60
61 /* Extract from an array REGBUF containing the (raw) register state
62    a function return value of type TYPE, and copy that, in virtual format,
63    into VALBUF.  */
64
65 void
66 arm_linux_extract_return_value (struct type *type,
67                                 char regbuf[REGISTER_BYTES],
68                                 char *valbuf)
69 {
70   /* ScottB: This needs to be looked at to handle the different
71      floating point emulators on ARM Linux.  Right now the code
72      assumes that fetch inferior registers does the right thing for
73      GDB.  I suspect this won't handle NWFPE registers correctly, nor
74      will the default ARM version (arm_extract_return_value()).  */
75
76   int regnum = (TYPE_CODE_FLT == TYPE_CODE (type)) ? F0_REGNUM : A1_REGNUM;
77   memcpy (valbuf, &regbuf[REGISTER_BYTE (regnum)], TYPE_LENGTH (type));
78 }
79
80 /* Note: ScottB
81
82    This function does not support passing parameters using the FPA
83    variant of the APCS.  It passes any floating point arguments in the
84    general registers and/or on the stack.
85    
86    FIXME:  This and arm_push_arguments should be merged.  However this 
87            function breaks on a little endian host, big endian target
88            using the COFF file format.  ELF is ok.  
89            
90            ScottB.  */
91            
92 /* Addresses for calling Thumb functions have the bit 0 set.
93    Here are some macros to test, set, or clear bit 0 of addresses.  */
94 #define IS_THUMB_ADDR(addr)     ((addr) & 1)
95 #define MAKE_THUMB_ADDR(addr)   ((addr) | 1)
96 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
97           
98 CORE_ADDR
99 arm_linux_push_arguments (int nargs, value_ptr * args, CORE_ADDR sp,
100                           int struct_return, CORE_ADDR struct_addr)
101 {
102   char *fp;
103   int argnum, argreg, nstack_size;
104
105   /* Walk through the list of args and determine how large a temporary
106      stack is required.  Need to take care here as structs may be
107      passed on the stack, and we have to to push them.  */
108   nstack_size = -4 * REGISTER_SIZE;     /* Some arguments go into A1-A4.  */
109
110   if (struct_return)                    /* The struct address goes in A1.  */
111     nstack_size += REGISTER_SIZE;
112
113   /* Walk through the arguments and add their size to nstack_size.  */
114   for (argnum = 0; argnum < nargs; argnum++)
115     {
116       int len;
117       struct type *arg_type;
118
119       arg_type = check_typedef (VALUE_TYPE (args[argnum]));
120       len = TYPE_LENGTH (arg_type);
121
122       /* ANSI C code passes float arguments as integers, K&R code
123          passes float arguments as doubles.  Correct for this here.  */
124       if (TYPE_CODE_FLT == TYPE_CODE (arg_type) && REGISTER_SIZE == len)
125         nstack_size += FP_REGISTER_VIRTUAL_SIZE;
126       else
127         nstack_size += len;
128     }
129
130   /* Allocate room on the stack, and initialize our stack frame
131      pointer.  */
132   fp = NULL;
133   if (nstack_size > 0)
134     {
135       sp -= nstack_size;
136       fp = (char *) sp;
137     }
138
139   /* Initialize the integer argument register pointer.  */
140   argreg = A1_REGNUM;
141
142   /* The struct_return pointer occupies the first parameter passing
143      register.  */
144   if (struct_return)
145     write_register (argreg++, struct_addr);
146
147   /* Process arguments from left to right.  Store as many as allowed
148      in the parameter passing registers (A1-A4), and save the rest on
149      the temporary stack.  */
150   for (argnum = 0; argnum < nargs; argnum++)
151     {
152       int len;
153       char *val;
154       double dbl_arg;
155       CORE_ADDR regval;
156       enum type_code typecode;
157       struct type *arg_type, *target_type;
158
159       arg_type = check_typedef (VALUE_TYPE (args[argnum]));
160       target_type = TYPE_TARGET_TYPE (arg_type);
161       len = TYPE_LENGTH (arg_type);
162       typecode = TYPE_CODE (arg_type);
163       val = (char *) VALUE_CONTENTS (args[argnum]);
164
165       /* ANSI C code passes float arguments as integers, K&R code
166          passes float arguments as doubles.  The .stabs record for 
167          for ANSI prototype floating point arguments records the
168          type as FP_INTEGER, while a K&R style (no prototype)
169          .stabs records the type as FP_FLOAT.  In this latter case
170          the compiler converts the float arguments to double before
171          calling the function.  */
172       if (TYPE_CODE_FLT == typecode && REGISTER_SIZE == len)
173         {
174           /* Float argument in buffer is in host format.  Read it and 
175              convert to DOUBLEST, and store it in target double.  */
176           DOUBLEST dblval;
177           
178           len = TARGET_DOUBLE_BIT / TARGET_CHAR_BIT;
179           floatformat_to_doublest (HOST_FLOAT_FORMAT, val, &dblval);
180           store_floating (&dbl_arg, len, dblval);
181           val = (char *) &dbl_arg;
182         }
183
184       /* If the argument is a pointer to a function, and it is a Thumb
185          function, set the low bit of the pointer.  */
186       if (TYPE_CODE_PTR == typecode
187           && NULL != target_type
188           && TYPE_CODE_FUNC == TYPE_CODE (target_type))
189         {
190           CORE_ADDR regval = extract_address (val, len);
191           if (arm_pc_is_thumb (regval))
192             store_address (val, len, MAKE_THUMB_ADDR (regval));
193         }
194
195       /* Copy the argument to general registers or the stack in
196          register-sized pieces.  Large arguments are split between
197          registers and stack.  */
198       while (len > 0)
199         {
200           int partial_len = len < REGISTER_SIZE ? len : REGISTER_SIZE;
201
202           if (argreg <= ARM_LAST_ARG_REGNUM)
203             {
204               /* It's an argument being passed in a general register.  */
205               regval = extract_address (val, partial_len);
206               write_register (argreg++, regval);
207             }
208           else
209             {
210               /* Push the arguments onto the stack.  */
211               write_memory ((CORE_ADDR) fp, val, REGISTER_SIZE);
212               fp += REGISTER_SIZE;
213             }
214
215           len -= partial_len;
216           val += partial_len;
217         }
218     }
219
220   /* Return adjusted stack pointer.  */
221   return sp;
222 }
223
224 /*
225    Dynamic Linking on ARM Linux
226    ----------------------------
227
228    Note: PLT = procedure linkage table
229    GOT = global offset table
230
231    As much as possible, ELF dynamic linking defers the resolution of
232    jump/call addresses until the last minute. The technique used is
233    inspired by the i386 ELF design, and is based on the following
234    constraints.
235
236    1) The calling technique should not force a change in the assembly
237    code produced for apps; it MAY cause changes in the way assembly
238    code is produced for position independent code (i.e. shared
239    libraries).
240
241    2) The technique must be such that all executable areas must not be
242    modified; and any modified areas must not be executed.
243
244    To do this, there are three steps involved in a typical jump:
245
246    1) in the code
247    2) through the PLT
248    3) using a pointer from the GOT
249
250    When the executable or library is first loaded, each GOT entry is
251    initialized to point to the code which implements dynamic name
252    resolution and code finding.  This is normally a function in the
253    program interpreter (on ARM Linux this is usually ld-linux.so.2,
254    but it does not have to be).  On the first invocation, the function
255    is located and the GOT entry is replaced with the real function
256    address.  Subsequent calls go through steps 1, 2 and 3 and end up
257    calling the real code.
258
259    1) In the code: 
260
261    b    function_call
262    bl   function_call
263
264    This is typical ARM code using the 26 bit relative branch or branch
265    and link instructions.  The target of the instruction
266    (function_call is usually the address of the function to be called.
267    In position independent code, the target of the instruction is
268    actually an entry in the PLT when calling functions in a shared
269    library.  Note that this call is identical to a normal function
270    call, only the target differs.
271
272    2) In the PLT:
273
274    The PLT is a synthetic area, created by the linker. It exists in
275    both executables and libraries. It is an array of stubs, one per
276    imported function call. It looks like this:
277
278    PLT[0]:
279    str     lr, [sp, #-4]!       @push the return address (lr)
280    ldr     lr, [pc, #16]   @load from 6 words ahead
281    add     lr, pc, lr      @form an address for GOT[0]
282    ldr     pc, [lr, #8]!   @jump to the contents of that addr
283
284    The return address (lr) is pushed on the stack and used for
285    calculations.  The load on the second line loads the lr with
286    &GOT[3] - . - 20.  The addition on the third leaves:
287
288    lr = (&GOT[3] - . - 20) + (. + 8)
289    lr = (&GOT[3] - 12)
290    lr = &GOT[0]
291
292    On the fourth line, the pc and lr are both updated, so that:
293
294    pc = GOT[2]
295    lr = &GOT[0] + 8
296    = &GOT[2]
297
298    NOTE: PLT[0] borrows an offset .word from PLT[1]. This is a little
299    "tight", but allows us to keep all the PLT entries the same size.
300
301    PLT[n+1]:
302    ldr     ip, [pc, #4]    @load offset from gotoff
303    add     ip, pc, ip      @add the offset to the pc
304    ldr     pc, [ip]        @jump to that address
305    gotoff: .word   GOT[n+3] - .
306
307    The load on the first line, gets an offset from the fourth word of
308    the PLT entry.  The add on the second line makes ip = &GOT[n+3],
309    which contains either a pointer to PLT[0] (the fixup trampoline) or
310    a pointer to the actual code.
311
312    3) In the GOT:
313
314    The GOT contains helper pointers for both code (PLT) fixups and
315    data fixups.  The first 3 entries of the GOT are special. The next
316    M entries (where M is the number of entries in the PLT) belong to
317    the PLT fixups. The next D (all remaining) entries belong to
318    various data fixups. The actual size of the GOT is 3 + M + D.
319
320    The GOT is also a synthetic area, created by the linker. It exists
321    in both executables and libraries.  When the GOT is first
322    initialized , all the GOT entries relating to PLT fixups are
323    pointing to code back at PLT[0].
324
325    The special entries in the GOT are:
326
327    GOT[0] = linked list pointer used by the dynamic loader
328    GOT[1] = pointer to the reloc table for this module
329    GOT[2] = pointer to the fixup/resolver code
330
331    The first invocation of function call comes through and uses the
332    fixup/resolver code.  On the entry to the fixup/resolver code:
333
334    ip = &GOT[n+3]
335    lr = &GOT[2]
336    stack[0] = return address (lr) of the function call
337    [r0, r1, r2, r3] are still the arguments to the function call
338
339    This is enough information for the fixup/resolver code to work
340    with.  Before the fixup/resolver code returns, it actually calls
341    the requested function and repairs &GOT[n+3].  */
342
343 CORE_ADDR
344 arm_skip_solib_resolver (CORE_ADDR pc)
345 {
346   /* FIXME */
347   return 0;
348 }
349
350 void
351 _initialize_arm_linux_tdep (void)
352 {
353 }