xref: /openbsd-src/gnu/usr.bin/binutils/gdb/arm-linux-tdep.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1*b725ae77Skettenis /* GNU/Linux on ARM target support.
2*b725ae77Skettenis    Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3*b725ae77Skettenis 
4*b725ae77Skettenis    This file is part of GDB.
5*b725ae77Skettenis 
6*b725ae77Skettenis    This program is free software; you can redistribute it and/or modify
7*b725ae77Skettenis    it under the terms of the GNU General Public License as published by
8*b725ae77Skettenis    the Free Software Foundation; either version 2 of the License, or
9*b725ae77Skettenis    (at your option) any later version.
10*b725ae77Skettenis 
11*b725ae77Skettenis    This program is distributed in the hope that it will be useful,
12*b725ae77Skettenis    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*b725ae77Skettenis    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*b725ae77Skettenis    GNU General Public License for more details.
15*b725ae77Skettenis 
16*b725ae77Skettenis    You should have received a copy of the GNU General Public License
17*b725ae77Skettenis    along with this program; if not, write to the Free Software
18*b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
19*b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
20*b725ae77Skettenis 
21*b725ae77Skettenis #include "defs.h"
22*b725ae77Skettenis #include "target.h"
23*b725ae77Skettenis #include "value.h"
24*b725ae77Skettenis #include "gdbtypes.h"
25*b725ae77Skettenis #include "floatformat.h"
26*b725ae77Skettenis #include "gdbcore.h"
27*b725ae77Skettenis #include "frame.h"
28*b725ae77Skettenis #include "regcache.h"
29*b725ae77Skettenis #include "doublest.h"
30*b725ae77Skettenis #include "solib-svr4.h"
31*b725ae77Skettenis #include "osabi.h"
32*b725ae77Skettenis 
33*b725ae77Skettenis #include "arm-tdep.h"
34*b725ae77Skettenis #include "glibc-tdep.h"
35*b725ae77Skettenis 
36*b725ae77Skettenis /* Under ARM GNU/Linux the traditional way of performing a breakpoint
37*b725ae77Skettenis    is to execute a particular software interrupt, rather than use a
38*b725ae77Skettenis    particular undefined instruction to provoke a trap.  Upon exection
39*b725ae77Skettenis    of the software interrupt the kernel stops the inferior with a
40*b725ae77Skettenis    SIGTRAP, and wakes the debugger.  Since ARM GNU/Linux doesn't support
41*b725ae77Skettenis    Thumb at the moment we only override the ARM breakpoints.  */
42*b725ae77Skettenis 
43*b725ae77Skettenis static const char arm_linux_arm_le_breakpoint[] = { 0x01, 0x00, 0x9f, 0xef };
44*b725ae77Skettenis 
45*b725ae77Skettenis static const char arm_linux_arm_be_breakpoint[] = { 0xef, 0x9f, 0x00, 0x01 };
46*b725ae77Skettenis 
47*b725ae77Skettenis /* Description of the longjmp buffer.  */
48*b725ae77Skettenis #define ARM_LINUX_JB_ELEMENT_SIZE	INT_REGISTER_SIZE
49*b725ae77Skettenis #define ARM_LINUX_JB_PC			21
50*b725ae77Skettenis 
51*b725ae77Skettenis /* Extract from an array REGBUF containing the (raw) register state
52*b725ae77Skettenis    a function return value of type TYPE, and copy that, in virtual format,
53*b725ae77Skettenis    into VALBUF.  */
54*b725ae77Skettenis /* FIXME rearnsha/2002-02-23: This function shouldn't be necessary.
55*b725ae77Skettenis    The ARM generic one should be able to handle the model used by
56*b725ae77Skettenis    linux and the low-level formatting of the registers should be
57*b725ae77Skettenis    hidden behind the regcache abstraction.  */
58*b725ae77Skettenis static void
arm_linux_extract_return_value(struct type * type,char regbuf[],char * valbuf)59*b725ae77Skettenis arm_linux_extract_return_value (struct type *type,
60*b725ae77Skettenis 				char regbuf[],
61*b725ae77Skettenis 				char *valbuf)
62*b725ae77Skettenis {
63*b725ae77Skettenis   /* ScottB: This needs to be looked at to handle the different
64*b725ae77Skettenis      floating point emulators on ARM GNU/Linux.  Right now the code
65*b725ae77Skettenis      assumes that fetch inferior registers does the right thing for
66*b725ae77Skettenis      GDB.  I suspect this won't handle NWFPE registers correctly, nor
67*b725ae77Skettenis      will the default ARM version (arm_extract_return_value()).  */
68*b725ae77Skettenis 
69*b725ae77Skettenis   int regnum = ((TYPE_CODE_FLT == TYPE_CODE (type))
70*b725ae77Skettenis 		? ARM_F0_REGNUM : ARM_A1_REGNUM);
71*b725ae77Skettenis   memcpy (valbuf, &regbuf[DEPRECATED_REGISTER_BYTE (regnum)], TYPE_LENGTH (type));
72*b725ae77Skettenis }
73*b725ae77Skettenis 
74*b725ae77Skettenis /* Note: ScottB
75*b725ae77Skettenis 
76*b725ae77Skettenis    This function does not support passing parameters using the FPA
77*b725ae77Skettenis    variant of the APCS.  It passes any floating point arguments in the
78*b725ae77Skettenis    general registers and/or on the stack.
79*b725ae77Skettenis 
80*b725ae77Skettenis    FIXME:  This and arm_push_arguments should be merged.  However this
81*b725ae77Skettenis    	   function breaks on a little endian host, big endian target
82*b725ae77Skettenis    	   using the COFF file format.  ELF is ok.
83*b725ae77Skettenis 
84*b725ae77Skettenis    	   ScottB.  */
85*b725ae77Skettenis 
86*b725ae77Skettenis /* Addresses for calling Thumb functions have the bit 0 set.
87*b725ae77Skettenis    Here are some macros to test, set, or clear bit 0 of addresses.  */
88*b725ae77Skettenis #define IS_THUMB_ADDR(addr)	((addr) & 1)
89*b725ae77Skettenis #define MAKE_THUMB_ADDR(addr)	((addr) | 1)
90*b725ae77Skettenis #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
91*b725ae77Skettenis 
92*b725ae77Skettenis static CORE_ADDR
arm_linux_push_arguments(int nargs,struct value ** args,CORE_ADDR sp,int struct_return,CORE_ADDR struct_addr)93*b725ae77Skettenis arm_linux_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
94*b725ae77Skettenis 		          int struct_return, CORE_ADDR struct_addr)
95*b725ae77Skettenis {
96*b725ae77Skettenis   char *fp;
97*b725ae77Skettenis   int argnum, argreg, nstack_size;
98*b725ae77Skettenis 
99*b725ae77Skettenis   /* Walk through the list of args and determine how large a temporary
100*b725ae77Skettenis      stack is required.  Need to take care here as structs may be
101*b725ae77Skettenis      passed on the stack, and we have to to push them.  */
102*b725ae77Skettenis   nstack_size = -4 * DEPRECATED_REGISTER_SIZE;	/* Some arguments go into A1-A4.  */
103*b725ae77Skettenis 
104*b725ae77Skettenis   if (struct_return)			/* The struct address goes in A1.  */
105*b725ae77Skettenis     nstack_size += DEPRECATED_REGISTER_SIZE;
106*b725ae77Skettenis 
107*b725ae77Skettenis   /* Walk through the arguments and add their size to nstack_size.  */
108*b725ae77Skettenis   for (argnum = 0; argnum < nargs; argnum++)
109*b725ae77Skettenis     {
110*b725ae77Skettenis       int len;
111*b725ae77Skettenis       struct type *arg_type;
112*b725ae77Skettenis 
113*b725ae77Skettenis       arg_type = check_typedef (VALUE_TYPE (args[argnum]));
114*b725ae77Skettenis       len = TYPE_LENGTH (arg_type);
115*b725ae77Skettenis 
116*b725ae77Skettenis       /* ANSI C code passes float arguments as integers, K&R code
117*b725ae77Skettenis          passes float arguments as doubles.  Correct for this here.  */
118*b725ae77Skettenis       if (TYPE_CODE_FLT == TYPE_CODE (arg_type) && DEPRECATED_REGISTER_SIZE == len)
119*b725ae77Skettenis 	nstack_size += TARGET_DOUBLE_BIT / TARGET_CHAR_BIT;
120*b725ae77Skettenis       else
121*b725ae77Skettenis 	nstack_size += len;
122*b725ae77Skettenis     }
123*b725ae77Skettenis 
124*b725ae77Skettenis   /* Allocate room on the stack, and initialize our stack frame
125*b725ae77Skettenis      pointer.  */
126*b725ae77Skettenis   fp = NULL;
127*b725ae77Skettenis   if (nstack_size > 0)
128*b725ae77Skettenis     {
129*b725ae77Skettenis       sp -= nstack_size;
130*b725ae77Skettenis       fp = (char *) sp;
131*b725ae77Skettenis     }
132*b725ae77Skettenis 
133*b725ae77Skettenis   /* Initialize the integer argument register pointer.  */
134*b725ae77Skettenis   argreg = ARM_A1_REGNUM;
135*b725ae77Skettenis 
136*b725ae77Skettenis   /* The struct_return pointer occupies the first parameter passing
137*b725ae77Skettenis      register.  */
138*b725ae77Skettenis   if (struct_return)
139*b725ae77Skettenis     write_register (argreg++, struct_addr);
140*b725ae77Skettenis 
141*b725ae77Skettenis   /* Process arguments from left to right.  Store as many as allowed
142*b725ae77Skettenis      in the parameter passing registers (A1-A4), and save the rest on
143*b725ae77Skettenis      the temporary stack.  */
144*b725ae77Skettenis   for (argnum = 0; argnum < nargs; argnum++)
145*b725ae77Skettenis     {
146*b725ae77Skettenis       int len;
147*b725ae77Skettenis       char *val;
148*b725ae77Skettenis       CORE_ADDR regval;
149*b725ae77Skettenis       enum type_code typecode;
150*b725ae77Skettenis       struct type *arg_type, *target_type;
151*b725ae77Skettenis 
152*b725ae77Skettenis       arg_type = check_typedef (VALUE_TYPE (args[argnum]));
153*b725ae77Skettenis       target_type = TYPE_TARGET_TYPE (arg_type);
154*b725ae77Skettenis       len = TYPE_LENGTH (arg_type);
155*b725ae77Skettenis       typecode = TYPE_CODE (arg_type);
156*b725ae77Skettenis       val = (char *) VALUE_CONTENTS (args[argnum]);
157*b725ae77Skettenis 
158*b725ae77Skettenis       /* ANSI C code passes float arguments as integers, K&R code
159*b725ae77Skettenis          passes float arguments as doubles.  The .stabs record for
160*b725ae77Skettenis          for ANSI prototype floating point arguments records the
161*b725ae77Skettenis          type as FP_INTEGER, while a K&R style (no prototype)
162*b725ae77Skettenis          .stabs records the type as FP_FLOAT.  In this latter case
163*b725ae77Skettenis          the compiler converts the float arguments to double before
164*b725ae77Skettenis          calling the function.  */
165*b725ae77Skettenis       if (TYPE_CODE_FLT == typecode && DEPRECATED_REGISTER_SIZE == len)
166*b725ae77Skettenis 	{
167*b725ae77Skettenis 	  DOUBLEST dblval;
168*b725ae77Skettenis 	  dblval = deprecated_extract_floating (val, len);
169*b725ae77Skettenis 	  len = TARGET_DOUBLE_BIT / TARGET_CHAR_BIT;
170*b725ae77Skettenis 	  val = alloca (len);
171*b725ae77Skettenis 	  deprecated_store_floating (val, len, dblval);
172*b725ae77Skettenis 	}
173*b725ae77Skettenis 
174*b725ae77Skettenis       /* If the argument is a pointer to a function, and it is a Thumb
175*b725ae77Skettenis          function, set the low bit of the pointer.  */
176*b725ae77Skettenis       if (TYPE_CODE_PTR == typecode
177*b725ae77Skettenis 	  && NULL != target_type
178*b725ae77Skettenis 	  && TYPE_CODE_FUNC == TYPE_CODE (target_type))
179*b725ae77Skettenis 	{
180*b725ae77Skettenis 	  CORE_ADDR regval = extract_unsigned_integer (val, len);
181*b725ae77Skettenis 	  if (arm_pc_is_thumb (regval))
182*b725ae77Skettenis 	    store_unsigned_integer (val, len, MAKE_THUMB_ADDR (regval));
183*b725ae77Skettenis 	}
184*b725ae77Skettenis 
185*b725ae77Skettenis       /* Copy the argument to general registers or the stack in
186*b725ae77Skettenis          register-sized pieces.  Large arguments are split between
187*b725ae77Skettenis          registers and stack.  */
188*b725ae77Skettenis       while (len > 0)
189*b725ae77Skettenis 	{
190*b725ae77Skettenis 	  int partial_len = len < DEPRECATED_REGISTER_SIZE ? len : DEPRECATED_REGISTER_SIZE;
191*b725ae77Skettenis 
192*b725ae77Skettenis 	  if (argreg <= ARM_LAST_ARG_REGNUM)
193*b725ae77Skettenis 	    {
194*b725ae77Skettenis 	      /* It's an argument being passed in a general register.  */
195*b725ae77Skettenis 	      regval = extract_unsigned_integer (val, partial_len);
196*b725ae77Skettenis 	      write_register (argreg++, regval);
197*b725ae77Skettenis 	    }
198*b725ae77Skettenis 	  else
199*b725ae77Skettenis 	    {
200*b725ae77Skettenis 	      /* Push the arguments onto the stack.  */
201*b725ae77Skettenis 	      write_memory ((CORE_ADDR) fp, val, DEPRECATED_REGISTER_SIZE);
202*b725ae77Skettenis 	      fp += DEPRECATED_REGISTER_SIZE;
203*b725ae77Skettenis 	    }
204*b725ae77Skettenis 
205*b725ae77Skettenis 	  len -= partial_len;
206*b725ae77Skettenis 	  val += partial_len;
207*b725ae77Skettenis 	}
208*b725ae77Skettenis     }
209*b725ae77Skettenis 
210*b725ae77Skettenis   /* Return adjusted stack pointer.  */
211*b725ae77Skettenis   return sp;
212*b725ae77Skettenis }
213*b725ae77Skettenis 
214*b725ae77Skettenis /*
215*b725ae77Skettenis    Dynamic Linking on ARM GNU/Linux
216*b725ae77Skettenis    --------------------------------
217*b725ae77Skettenis 
218*b725ae77Skettenis    Note: PLT = procedure linkage table
219*b725ae77Skettenis    GOT = global offset table
220*b725ae77Skettenis 
221*b725ae77Skettenis    As much as possible, ELF dynamic linking defers the resolution of
222*b725ae77Skettenis    jump/call addresses until the last minute. The technique used is
223*b725ae77Skettenis    inspired by the i386 ELF design, and is based on the following
224*b725ae77Skettenis    constraints.
225*b725ae77Skettenis 
226*b725ae77Skettenis    1) The calling technique should not force a change in the assembly
227*b725ae77Skettenis    code produced for apps; it MAY cause changes in the way assembly
228*b725ae77Skettenis    code is produced for position independent code (i.e. shared
229*b725ae77Skettenis    libraries).
230*b725ae77Skettenis 
231*b725ae77Skettenis    2) The technique must be such that all executable areas must not be
232*b725ae77Skettenis    modified; and any modified areas must not be executed.
233*b725ae77Skettenis 
234*b725ae77Skettenis    To do this, there are three steps involved in a typical jump:
235*b725ae77Skettenis 
236*b725ae77Skettenis    1) in the code
237*b725ae77Skettenis    2) through the PLT
238*b725ae77Skettenis    3) using a pointer from the GOT
239*b725ae77Skettenis 
240*b725ae77Skettenis    When the executable or library is first loaded, each GOT entry is
241*b725ae77Skettenis    initialized to point to the code which implements dynamic name
242*b725ae77Skettenis    resolution and code finding.  This is normally a function in the
243*b725ae77Skettenis    program interpreter (on ARM GNU/Linux this is usually
244*b725ae77Skettenis    ld-linux.so.2, but it does not have to be).  On the first
245*b725ae77Skettenis    invocation, the function is located and the GOT entry is replaced
246*b725ae77Skettenis    with the real function address.  Subsequent calls go through steps
247*b725ae77Skettenis    1, 2 and 3 and end up calling the real code.
248*b725ae77Skettenis 
249*b725ae77Skettenis    1) In the code:
250*b725ae77Skettenis 
251*b725ae77Skettenis    b    function_call
252*b725ae77Skettenis    bl   function_call
253*b725ae77Skettenis 
254*b725ae77Skettenis    This is typical ARM code using the 26 bit relative branch or branch
255*b725ae77Skettenis    and link instructions.  The target of the instruction
256*b725ae77Skettenis    (function_call is usually the address of the function to be called.
257*b725ae77Skettenis    In position independent code, the target of the instruction is
258*b725ae77Skettenis    actually an entry in the PLT when calling functions in a shared
259*b725ae77Skettenis    library.  Note that this call is identical to a normal function
260*b725ae77Skettenis    call, only the target differs.
261*b725ae77Skettenis 
262*b725ae77Skettenis    2) In the PLT:
263*b725ae77Skettenis 
264*b725ae77Skettenis    The PLT is a synthetic area, created by the linker. It exists in
265*b725ae77Skettenis    both executables and libraries. It is an array of stubs, one per
266*b725ae77Skettenis    imported function call. It looks like this:
267*b725ae77Skettenis 
268*b725ae77Skettenis    PLT[0]:
269*b725ae77Skettenis    str     lr, [sp, #-4]!       @push the return address (lr)
270*b725ae77Skettenis    ldr     lr, [pc, #16]   @load from 6 words ahead
271*b725ae77Skettenis    add     lr, pc, lr      @form an address for GOT[0]
272*b725ae77Skettenis    ldr     pc, [lr, #8]!   @jump to the contents of that addr
273*b725ae77Skettenis 
274*b725ae77Skettenis    The return address (lr) is pushed on the stack and used for
275*b725ae77Skettenis    calculations.  The load on the second line loads the lr with
276*b725ae77Skettenis    &GOT[3] - . - 20.  The addition on the third leaves:
277*b725ae77Skettenis 
278*b725ae77Skettenis    lr = (&GOT[3] - . - 20) + (. + 8)
279*b725ae77Skettenis    lr = (&GOT[3] - 12)
280*b725ae77Skettenis    lr = &GOT[0]
281*b725ae77Skettenis 
282*b725ae77Skettenis    On the fourth line, the pc and lr are both updated, so that:
283*b725ae77Skettenis 
284*b725ae77Skettenis    pc = GOT[2]
285*b725ae77Skettenis    lr = &GOT[0] + 8
286*b725ae77Skettenis    = &GOT[2]
287*b725ae77Skettenis 
288*b725ae77Skettenis    NOTE: PLT[0] borrows an offset .word from PLT[1]. This is a little
289*b725ae77Skettenis    "tight", but allows us to keep all the PLT entries the same size.
290*b725ae77Skettenis 
291*b725ae77Skettenis    PLT[n+1]:
292*b725ae77Skettenis    ldr     ip, [pc, #4]    @load offset from gotoff
293*b725ae77Skettenis    add     ip, pc, ip      @add the offset to the pc
294*b725ae77Skettenis    ldr     pc, [ip]        @jump to that address
295*b725ae77Skettenis    gotoff: .word   GOT[n+3] - .
296*b725ae77Skettenis 
297*b725ae77Skettenis    The load on the first line, gets an offset from the fourth word of
298*b725ae77Skettenis    the PLT entry.  The add on the second line makes ip = &GOT[n+3],
299*b725ae77Skettenis    which contains either a pointer to PLT[0] (the fixup trampoline) or
300*b725ae77Skettenis    a pointer to the actual code.
301*b725ae77Skettenis 
302*b725ae77Skettenis    3) In the GOT:
303*b725ae77Skettenis 
304*b725ae77Skettenis    The GOT contains helper pointers for both code (PLT) fixups and
305*b725ae77Skettenis    data fixups.  The first 3 entries of the GOT are special. The next
306*b725ae77Skettenis    M entries (where M is the number of entries in the PLT) belong to
307*b725ae77Skettenis    the PLT fixups. The next D (all remaining) entries belong to
308*b725ae77Skettenis    various data fixups. The actual size of the GOT is 3 + M + D.
309*b725ae77Skettenis 
310*b725ae77Skettenis    The GOT is also a synthetic area, created by the linker. It exists
311*b725ae77Skettenis    in both executables and libraries.  When the GOT is first
312*b725ae77Skettenis    initialized , all the GOT entries relating to PLT fixups are
313*b725ae77Skettenis    pointing to code back at PLT[0].
314*b725ae77Skettenis 
315*b725ae77Skettenis    The special entries in the GOT are:
316*b725ae77Skettenis 
317*b725ae77Skettenis    GOT[0] = linked list pointer used by the dynamic loader
318*b725ae77Skettenis    GOT[1] = pointer to the reloc table for this module
319*b725ae77Skettenis    GOT[2] = pointer to the fixup/resolver code
320*b725ae77Skettenis 
321*b725ae77Skettenis    The first invocation of function call comes through and uses the
322*b725ae77Skettenis    fixup/resolver code.  On the entry to the fixup/resolver code:
323*b725ae77Skettenis 
324*b725ae77Skettenis    ip = &GOT[n+3]
325*b725ae77Skettenis    lr = &GOT[2]
326*b725ae77Skettenis    stack[0] = return address (lr) of the function call
327*b725ae77Skettenis    [r0, r1, r2, r3] are still the arguments to the function call
328*b725ae77Skettenis 
329*b725ae77Skettenis    This is enough information for the fixup/resolver code to work
330*b725ae77Skettenis    with.  Before the fixup/resolver code returns, it actually calls
331*b725ae77Skettenis    the requested function and repairs &GOT[n+3].  */
332*b725ae77Skettenis 
333*b725ae77Skettenis /* Fetch, and possibly build, an appropriate link_map_offsets structure
334*b725ae77Skettenis    for ARM linux targets using the struct offsets defined in <link.h>.
335*b725ae77Skettenis    Note, however, that link.h is not actually referred to in this file.
336*b725ae77Skettenis    Instead, the relevant structs offsets were obtained from examining
337*b725ae77Skettenis    link.h.  (We can't refer to link.h from this file because the host
338*b725ae77Skettenis    system won't necessarily have it, or if it does, the structs which
339*b725ae77Skettenis    it defines will refer to the host system, not the target).  */
340*b725ae77Skettenis 
341*b725ae77Skettenis static struct link_map_offsets *
arm_linux_svr4_fetch_link_map_offsets(void)342*b725ae77Skettenis arm_linux_svr4_fetch_link_map_offsets (void)
343*b725ae77Skettenis {
344*b725ae77Skettenis   static struct link_map_offsets lmo;
345*b725ae77Skettenis   static struct link_map_offsets *lmp = 0;
346*b725ae77Skettenis 
347*b725ae77Skettenis   if (lmp == 0)
348*b725ae77Skettenis     {
349*b725ae77Skettenis       lmp = &lmo;
350*b725ae77Skettenis 
351*b725ae77Skettenis       lmo.r_debug_size = 8;	/* Actual size is 20, but this is all we
352*b725ae77Skettenis                                    need.  */
353*b725ae77Skettenis 
354*b725ae77Skettenis       lmo.r_map_offset = 4;
355*b725ae77Skettenis       lmo.r_map_size   = 4;
356*b725ae77Skettenis 
357*b725ae77Skettenis       lmo.link_map_size = 20;	/* Actual size is 552, but this is all we
358*b725ae77Skettenis                                    need.  */
359*b725ae77Skettenis 
360*b725ae77Skettenis       lmo.l_addr_offset = 0;
361*b725ae77Skettenis       lmo.l_addr_size   = 4;
362*b725ae77Skettenis 
363*b725ae77Skettenis       lmo.l_name_offset = 4;
364*b725ae77Skettenis       lmo.l_name_size   = 4;
365*b725ae77Skettenis 
366*b725ae77Skettenis       lmo.l_next_offset = 12;
367*b725ae77Skettenis       lmo.l_next_size   = 4;
368*b725ae77Skettenis 
369*b725ae77Skettenis       lmo.l_prev_offset = 16;
370*b725ae77Skettenis       lmo.l_prev_size   = 4;
371*b725ae77Skettenis     }
372*b725ae77Skettenis 
373*b725ae77Skettenis     return lmp;
374*b725ae77Skettenis }
375*b725ae77Skettenis 
376*b725ae77Skettenis /* The constants below were determined by examining the following files
377*b725ae77Skettenis    in the linux kernel sources:
378*b725ae77Skettenis 
379*b725ae77Skettenis       arch/arm/kernel/signal.c
380*b725ae77Skettenis 	  - see SWI_SYS_SIGRETURN and SWI_SYS_RT_SIGRETURN
381*b725ae77Skettenis       include/asm-arm/unistd.h
382*b725ae77Skettenis 	  - see __NR_sigreturn, __NR_rt_sigreturn, and __NR_SYSCALL_BASE */
383*b725ae77Skettenis 
384*b725ae77Skettenis #define ARM_LINUX_SIGRETURN_INSTR	0xef900077
385*b725ae77Skettenis #define ARM_LINUX_RT_SIGRETURN_INSTR	0xef9000ad
386*b725ae77Skettenis 
387*b725ae77Skettenis /* arm_linux_in_sigtramp determines if PC points at one of the
388*b725ae77Skettenis    instructions which cause control to return to the Linux kernel upon
389*b725ae77Skettenis    return from a signal handler.  FUNC_NAME is unused.  */
390*b725ae77Skettenis 
391*b725ae77Skettenis int
arm_linux_in_sigtramp(CORE_ADDR pc,char * func_name)392*b725ae77Skettenis arm_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
393*b725ae77Skettenis {
394*b725ae77Skettenis   unsigned long inst;
395*b725ae77Skettenis 
396*b725ae77Skettenis   inst = read_memory_integer (pc, 4);
397*b725ae77Skettenis 
398*b725ae77Skettenis   return (inst == ARM_LINUX_SIGRETURN_INSTR
399*b725ae77Skettenis 	  || inst == ARM_LINUX_RT_SIGRETURN_INSTR);
400*b725ae77Skettenis 
401*b725ae77Skettenis }
402*b725ae77Skettenis 
403*b725ae77Skettenis /* arm_linux_sigcontext_register_address returns the address in the
404*b725ae77Skettenis    sigcontext of register REGNO given a stack pointer value SP and
405*b725ae77Skettenis    program counter value PC.  The value 0 is returned if PC is not
406*b725ae77Skettenis    pointing at one of the signal return instructions or if REGNO is
407*b725ae77Skettenis    not saved in the sigcontext struct.  */
408*b725ae77Skettenis 
409*b725ae77Skettenis CORE_ADDR
arm_linux_sigcontext_register_address(CORE_ADDR sp,CORE_ADDR pc,int regno)410*b725ae77Skettenis arm_linux_sigcontext_register_address (CORE_ADDR sp, CORE_ADDR pc, int regno)
411*b725ae77Skettenis {
412*b725ae77Skettenis   unsigned long inst;
413*b725ae77Skettenis   CORE_ADDR reg_addr = 0;
414*b725ae77Skettenis 
415*b725ae77Skettenis   inst = read_memory_integer (pc, 4);
416*b725ae77Skettenis 
417*b725ae77Skettenis   if (inst == ARM_LINUX_SIGRETURN_INSTR
418*b725ae77Skettenis       || inst == ARM_LINUX_RT_SIGRETURN_INSTR)
419*b725ae77Skettenis     {
420*b725ae77Skettenis       CORE_ADDR sigcontext_addr;
421*b725ae77Skettenis 
422*b725ae77Skettenis       /* The sigcontext structure is at different places for the two
423*b725ae77Skettenis          signal return instructions.  For ARM_LINUX_SIGRETURN_INSTR,
424*b725ae77Skettenis 	 it starts at the SP value.  For ARM_LINUX_RT_SIGRETURN_INSTR,
425*b725ae77Skettenis 	 it is at SP+8.  For the latter instruction, it may also be
426*b725ae77Skettenis 	 the case that the address of this structure may be determined
427*b725ae77Skettenis 	 by reading the 4 bytes at SP, but I'm not convinced this is
428*b725ae77Skettenis 	 reliable.
429*b725ae77Skettenis 
430*b725ae77Skettenis 	 In any event, these magic constants (0 and 8) may be
431*b725ae77Skettenis 	 determined by examining struct sigframe and struct
432*b725ae77Skettenis 	 rt_sigframe in arch/arm/kernel/signal.c in the Linux kernel
433*b725ae77Skettenis 	 sources.  */
434*b725ae77Skettenis 
435*b725ae77Skettenis       if (inst == ARM_LINUX_RT_SIGRETURN_INSTR)
436*b725ae77Skettenis 	sigcontext_addr = sp + 8;
437*b725ae77Skettenis       else /* inst == ARM_LINUX_SIGRETURN_INSTR */
438*b725ae77Skettenis         sigcontext_addr = sp + 0;
439*b725ae77Skettenis 
440*b725ae77Skettenis       /* The layout of the sigcontext structure for ARM GNU/Linux is
441*b725ae77Skettenis          in include/asm-arm/sigcontext.h in the Linux kernel sources.
442*b725ae77Skettenis 
443*b725ae77Skettenis 	 There are three 4-byte fields which precede the saved r0
444*b725ae77Skettenis 	 field.  (This accounts for the 12 in the code below.)  The
445*b725ae77Skettenis 	 sixteen registers (4 bytes per field) follow in order.  The
446*b725ae77Skettenis 	 PSR value follows the sixteen registers which accounts for
447*b725ae77Skettenis 	 the constant 19 below. */
448*b725ae77Skettenis 
449*b725ae77Skettenis       if (0 <= regno && regno <= ARM_PC_REGNUM)
450*b725ae77Skettenis 	reg_addr = sigcontext_addr + 12 + (4 * regno);
451*b725ae77Skettenis       else if (regno == ARM_PS_REGNUM)
452*b725ae77Skettenis 	reg_addr = sigcontext_addr + 19 * 4;
453*b725ae77Skettenis     }
454*b725ae77Skettenis 
455*b725ae77Skettenis   return reg_addr;
456*b725ae77Skettenis }
457*b725ae77Skettenis 
458*b725ae77Skettenis static void
arm_linux_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)459*b725ae77Skettenis arm_linux_init_abi (struct gdbarch_info info,
460*b725ae77Skettenis 		    struct gdbarch *gdbarch)
461*b725ae77Skettenis {
462*b725ae77Skettenis   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
463*b725ae77Skettenis 
464*b725ae77Skettenis   tdep->lowest_pc = 0x8000;
465*b725ae77Skettenis   if (info.byte_order == BFD_ENDIAN_BIG)
466*b725ae77Skettenis     tdep->arm_breakpoint = arm_linux_arm_be_breakpoint;
467*b725ae77Skettenis   else
468*b725ae77Skettenis     tdep->arm_breakpoint = arm_linux_arm_le_breakpoint;
469*b725ae77Skettenis   tdep->arm_breakpoint_size = sizeof (arm_linux_arm_le_breakpoint);
470*b725ae77Skettenis 
471*b725ae77Skettenis   tdep->fp_model = ARM_FLOAT_FPA;
472*b725ae77Skettenis 
473*b725ae77Skettenis   tdep->jb_pc = ARM_LINUX_JB_PC;
474*b725ae77Skettenis   tdep->jb_elt_size = ARM_LINUX_JB_ELEMENT_SIZE;
475*b725ae77Skettenis 
476*b725ae77Skettenis   set_solib_svr4_fetch_link_map_offsets
477*b725ae77Skettenis     (gdbarch, arm_linux_svr4_fetch_link_map_offsets);
478*b725ae77Skettenis 
479*b725ae77Skettenis   /* The following two overrides shouldn't be needed.  */
480*b725ae77Skettenis   set_gdbarch_deprecated_extract_return_value (gdbarch, arm_linux_extract_return_value);
481*b725ae77Skettenis   set_gdbarch_deprecated_push_arguments (gdbarch, arm_linux_push_arguments);
482*b725ae77Skettenis 
483*b725ae77Skettenis   /* Shared library handling.  */
484*b725ae77Skettenis   set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
485*b725ae77Skettenis   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
486*b725ae77Skettenis   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
487*b725ae77Skettenis }
488*b725ae77Skettenis 
489*b725ae77Skettenis void
_initialize_arm_linux_tdep(void)490*b725ae77Skettenis _initialize_arm_linux_tdep (void)
491*b725ae77Skettenis {
492*b725ae77Skettenis   gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_LINUX,
493*b725ae77Skettenis 			  arm_linux_init_abi);
494*b725ae77Skettenis }
495