1 /* GNU/Linux on ARM target support.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
27 #include "floatformat.h"
32 #include "solib-svr4.h"
34 #include "trad-frame.h"
35 #include "tramp-frame.h"
38 #include "glibc-tdep.h"
40 #include "gdb_string.h"
42 /* Under ARM GNU/Linux the traditional way of performing a breakpoint
43 is to execute a particular software interrupt, rather than use a
44 particular undefined instruction to provoke a trap. Upon exection
45 of the software interrupt the kernel stops the inferior with a
46 SIGTRAP, and wakes the debugger. */
48 static const char arm_linux_arm_le_breakpoint
[] = { 0x01, 0x00, 0x9f, 0xef };
50 static const char arm_linux_arm_be_breakpoint
[] = { 0xef, 0x9f, 0x00, 0x01 };
52 /* However, the EABI syscall interface (new in Nov. 2005) does not look at
53 the operand of the swi if old-ABI compatibility is disabled. Therefore,
54 use an undefined instruction instead. This is supported as of kernel
55 version 2.5.70 (May 2003), so should be a safe assumption for EABI
58 static const char eabi_linux_arm_le_breakpoint
[] = { 0xf0, 0x01, 0xf0, 0xe7 };
60 static const char eabi_linux_arm_be_breakpoint
[] = { 0xe7, 0xf0, 0x01, 0xf0 };
62 /* All the kernels which support Thumb support using a specific undefined
63 instruction for the Thumb breakpoint. */
65 static const char arm_linux_thumb_be_breakpoint
[] = {0xde, 0x01};
67 static const char arm_linux_thumb_le_breakpoint
[] = {0x01, 0xde};
69 /* Description of the longjmp buffer. */
70 #define ARM_LINUX_JB_ELEMENT_SIZE INT_REGISTER_SIZE
71 #define ARM_LINUX_JB_PC 21
73 /* Extract from an array REGBUF containing the (raw) register state
74 a function return value of type TYPE, and copy that, in virtual format,
76 /* FIXME rearnsha/2002-02-23: This function shouldn't be necessary.
77 The ARM generic one should be able to handle the model used by
78 linux and the low-level formatting of the registers should be
79 hidden behind the regcache abstraction. */
81 arm_linux_extract_return_value (struct type
*type
,
85 /* ScottB: This needs to be looked at to handle the different
86 floating point emulators on ARM GNU/Linux. Right now the code
87 assumes that fetch inferior registers does the right thing for
88 GDB. I suspect this won't handle NWFPE registers correctly, nor
89 will the default ARM version (arm_extract_return_value()). */
91 int regnum
= ((TYPE_CODE_FLT
== TYPE_CODE (type
))
92 ? ARM_F0_REGNUM
: ARM_A1_REGNUM
);
93 memcpy (valbuf
, ®buf
[DEPRECATED_REGISTER_BYTE (regnum
)], TYPE_LENGTH (type
));
97 Dynamic Linking on ARM GNU/Linux
98 --------------------------------
100 Note: PLT = procedure linkage table
101 GOT = global offset table
103 As much as possible, ELF dynamic linking defers the resolution of
104 jump/call addresses until the last minute. The technique used is
105 inspired by the i386 ELF design, and is based on the following
108 1) The calling technique should not force a change in the assembly
109 code produced for apps; it MAY cause changes in the way assembly
110 code is produced for position independent code (i.e. shared
113 2) The technique must be such that all executable areas must not be
114 modified; and any modified areas must not be executed.
116 To do this, there are three steps involved in a typical jump:
120 3) using a pointer from the GOT
122 When the executable or library is first loaded, each GOT entry is
123 initialized to point to the code which implements dynamic name
124 resolution and code finding. This is normally a function in the
125 program interpreter (on ARM GNU/Linux this is usually
126 ld-linux.so.2, but it does not have to be). On the first
127 invocation, the function is located and the GOT entry is replaced
128 with the real function address. Subsequent calls go through steps
129 1, 2 and 3 and end up calling the real code.
136 This is typical ARM code using the 26 bit relative branch or branch
137 and link instructions. The target of the instruction
138 (function_call is usually the address of the function to be called.
139 In position independent code, the target of the instruction is
140 actually an entry in the PLT when calling functions in a shared
141 library. Note that this call is identical to a normal function
142 call, only the target differs.
146 The PLT is a synthetic area, created by the linker. It exists in
147 both executables and libraries. It is an array of stubs, one per
148 imported function call. It looks like this:
151 str lr, [sp, #-4]! @push the return address (lr)
152 ldr lr, [pc, #16] @load from 6 words ahead
153 add lr, pc, lr @form an address for GOT[0]
154 ldr pc, [lr, #8]! @jump to the contents of that addr
156 The return address (lr) is pushed on the stack and used for
157 calculations. The load on the second line loads the lr with
158 &GOT[3] - . - 20. The addition on the third leaves:
160 lr = (&GOT[3] - . - 20) + (. + 8)
164 On the fourth line, the pc and lr are both updated, so that:
170 NOTE: PLT[0] borrows an offset .word from PLT[1]. This is a little
171 "tight", but allows us to keep all the PLT entries the same size.
174 ldr ip, [pc, #4] @load offset from gotoff
175 add ip, pc, ip @add the offset to the pc
176 ldr pc, [ip] @jump to that address
177 gotoff: .word GOT[n+3] - .
179 The load on the first line, gets an offset from the fourth word of
180 the PLT entry. The add on the second line makes ip = &GOT[n+3],
181 which contains either a pointer to PLT[0] (the fixup trampoline) or
182 a pointer to the actual code.
186 The GOT contains helper pointers for both code (PLT) fixups and
187 data fixups. The first 3 entries of the GOT are special. The next
188 M entries (where M is the number of entries in the PLT) belong to
189 the PLT fixups. The next D (all remaining) entries belong to
190 various data fixups. The actual size of the GOT is 3 + M + D.
192 The GOT is also a synthetic area, created by the linker. It exists
193 in both executables and libraries. When the GOT is first
194 initialized , all the GOT entries relating to PLT fixups are
195 pointing to code back at PLT[0].
197 The special entries in the GOT are:
199 GOT[0] = linked list pointer used by the dynamic loader
200 GOT[1] = pointer to the reloc table for this module
201 GOT[2] = pointer to the fixup/resolver code
203 The first invocation of function call comes through and uses the
204 fixup/resolver code. On the entry to the fixup/resolver code:
208 stack[0] = return address (lr) of the function call
209 [r0, r1, r2, r3] are still the arguments to the function call
211 This is enough information for the fixup/resolver code to work
212 with. Before the fixup/resolver code returns, it actually calls
213 the requested function and repairs &GOT[n+3]. */
215 /* Fetch, and possibly build, an appropriate link_map_offsets structure
216 for ARM linux targets using the struct offsets defined in <link.h>.
217 Note, however, that link.h is not actually referred to in this file.
218 Instead, the relevant structs offsets were obtained from examining
219 link.h. (We can't refer to link.h from this file because the host
220 system won't necessarily have it, or if it does, the structs which
221 it defines will refer to the host system, not the target). */
223 static struct link_map_offsets
*
224 arm_linux_svr4_fetch_link_map_offsets (void)
226 static struct link_map_offsets lmo
;
227 static struct link_map_offsets
*lmp
= 0;
233 lmo
.r_debug_size
= 8; /* Actual size is 20, but this is all we
236 lmo
.r_map_offset
= 4;
239 lmo
.link_map_size
= 20; /* Actual size is 552, but this is all we
242 lmo
.l_addr_offset
= 0;
245 lmo
.l_name_offset
= 4;
248 lmo
.l_next_offset
= 12;
251 lmo
.l_prev_offset
= 16;
258 /* The constants below were determined by examining the following files
259 in the linux kernel sources:
261 arch/arm/kernel/signal.c
262 - see SWI_SYS_SIGRETURN and SWI_SYS_RT_SIGRETURN
263 include/asm-arm/unistd.h
264 - see __NR_sigreturn, __NR_rt_sigreturn, and __NR_SYSCALL_BASE */
266 #define ARM_LINUX_SIGRETURN_INSTR 0xef900077
267 #define ARM_LINUX_RT_SIGRETURN_INSTR 0xef9000ad
269 /* For ARM EABI, recognize the pattern that glibc uses... alternatively,
270 we could arrange to do this by function name, but they are not always
272 #define ARM_SET_R7_SIGRETURN 0xe3a07077
273 #define ARM_SET_R7_RT_SIGRETURN 0xe3a070ad
274 #define ARM_EABI_SYSCALL 0xef000000
277 arm_linux_sigtramp_cache (struct frame_info
*next_frame
,
278 struct trad_frame_cache
*this_cache
,
279 CORE_ADDR func
, int regs_offset
)
281 CORE_ADDR sp
= frame_unwind_register_unsigned (next_frame
, ARM_SP_REGNUM
);
282 CORE_ADDR base
= sp
+ regs_offset
;
285 for (i
= 0; i
< 16; i
++)
286 trad_frame_set_reg_addr (this_cache
, i
, base
+ i
* 4);
288 trad_frame_set_reg_addr (this_cache
, ARM_PS_REGNUM
, base
+ 16 * 4);
290 /* The VFP or iWMMXt registers may be saved on the stack, but there's
291 no reliable way to restore them (yet). */
293 /* Save a frame ID. */
294 trad_frame_set_id (this_cache
, frame_id_build (sp
, func
));
298 arm_linux_sigreturn_init (const struct tramp_frame
*self
,
299 struct frame_info
*next_frame
,
300 struct trad_frame_cache
*this_cache
,
303 arm_linux_sigtramp_cache (next_frame
, this_cache
, func
,
304 0x0c /* Offset to registers. */);
308 arm_linux_rt_sigreturn_init (const struct tramp_frame
*self
,
309 struct frame_info
*next_frame
,
310 struct trad_frame_cache
*this_cache
,
313 arm_linux_sigtramp_cache (next_frame
, this_cache
, func
,
314 0x88 /* Offset to ucontext_t. */
315 + 0x14 /* Offset to sigcontext. */
316 + 0x0c /* Offset to registers. */);
319 static struct tramp_frame arm_linux_sigreturn_tramp_frame
= {
323 { ARM_LINUX_SIGRETURN_INSTR
, -1 },
324 { TRAMP_SENTINEL_INSN
}
326 arm_linux_sigreturn_init
329 static struct tramp_frame arm_linux_rt_sigreturn_tramp_frame
= {
333 { ARM_LINUX_RT_SIGRETURN_INSTR
, -1 },
334 { TRAMP_SENTINEL_INSN
}
336 arm_linux_rt_sigreturn_init
339 static struct tramp_frame arm_eabi_linux_sigreturn_tramp_frame
= {
343 { ARM_SET_R7_SIGRETURN
, -1 },
344 { ARM_EABI_SYSCALL
, -1 },
345 { TRAMP_SENTINEL_INSN
}
347 arm_linux_sigreturn_init
350 static struct tramp_frame arm_eabi_linux_rt_sigreturn_tramp_frame
= {
354 { ARM_SET_R7_RT_SIGRETURN
, -1 },
355 { ARM_EABI_SYSCALL
, -1 },
356 { TRAMP_SENTINEL_INSN
}
358 arm_linux_rt_sigreturn_init
362 arm_linux_init_abi (struct gdbarch_info info
,
363 struct gdbarch
*gdbarch
)
365 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
367 tdep
->lowest_pc
= 0x8000;
368 if (info
.byte_order
== BFD_ENDIAN_BIG
)
370 if (tdep
->arm_abi
== ARM_ABI_AAPCS
)
371 tdep
->arm_breakpoint
= eabi_linux_arm_be_breakpoint
;
373 tdep
->arm_breakpoint
= arm_linux_arm_be_breakpoint
;
374 tdep
->thumb_breakpoint
= arm_linux_thumb_be_breakpoint
;
378 if (tdep
->arm_abi
== ARM_ABI_AAPCS
)
379 tdep
->arm_breakpoint
= eabi_linux_arm_le_breakpoint
;
381 tdep
->arm_breakpoint
= arm_linux_arm_le_breakpoint
;
382 tdep
->thumb_breakpoint
= arm_linux_thumb_le_breakpoint
;
384 tdep
->arm_breakpoint_size
= sizeof (arm_linux_arm_le_breakpoint
);
385 tdep
->thumb_breakpoint_size
= sizeof (arm_linux_thumb_le_breakpoint
);
387 if (tdep
->fp_model
== ARM_FLOAT_AUTO
)
388 tdep
->fp_model
= ARM_FLOAT_FPA
;
390 tdep
->jb_pc
= ARM_LINUX_JB_PC
;
391 tdep
->jb_elt_size
= ARM_LINUX_JB_ELEMENT_SIZE
;
393 set_solib_svr4_fetch_link_map_offsets
394 (gdbarch
, arm_linux_svr4_fetch_link_map_offsets
);
396 /* The following override shouldn't be needed. */
397 set_gdbarch_deprecated_extract_return_value (gdbarch
, arm_linux_extract_return_value
);
399 /* Shared library handling. */
400 set_gdbarch_skip_trampoline_code (gdbarch
, find_solib_trampoline_target
);
401 set_gdbarch_skip_solib_resolver (gdbarch
, glibc_skip_solib_resolver
);
403 /* Enable TLS support. */
404 set_gdbarch_fetch_tls_load_module_address (gdbarch
,
405 svr4_fetch_objfile_link_map
);
407 tramp_frame_prepend_unwinder (gdbarch
,
408 &arm_linux_sigreturn_tramp_frame
);
409 tramp_frame_prepend_unwinder (gdbarch
,
410 &arm_linux_rt_sigreturn_tramp_frame
);
411 tramp_frame_prepend_unwinder (gdbarch
,
412 &arm_eabi_linux_sigreturn_tramp_frame
);
413 tramp_frame_prepend_unwinder (gdbarch
,
414 &arm_eabi_linux_rt_sigreturn_tramp_frame
);
418 _initialize_arm_linux_tdep (void)
420 gdbarch_register_osabi (bfd_arch_arm
, 0, GDB_OSABI_LINUX
,