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