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