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