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