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