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