PR 1150
[deliverable/binutils-gdb.git] / gdb / arm-linux-tdep.c
1 /* GNU/Linux on ARM target support.
2
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "defs.h"
24 #include "target.h"
25 #include "value.h"
26 #include "gdbtypes.h"
27 #include "floatformat.h"
28 #include "gdbcore.h"
29 #include "frame.h"
30 #include "regcache.h"
31 #include "doublest.h"
32 #include "solib-svr4.h"
33 #include "osabi.h"
34 #include "trad-frame.h"
35 #include "tramp-frame.h"
36
37 #include "arm-tdep.h"
38 #include "glibc-tdep.h"
39
40 #include "gdb_string.h"
41
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. */
47
48 static const char arm_linux_arm_le_breakpoint[] = { 0x01, 0x00, 0x9f, 0xef };
49
50 static const char arm_linux_arm_be_breakpoint[] = { 0xef, 0x9f, 0x00, 0x01 };
51
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
56 binaries. */
57
58 static const char eabi_linux_arm_le_breakpoint[] = { 0xf0, 0x01, 0xf0, 0xe7 };
59
60 static const char eabi_linux_arm_be_breakpoint[] = { 0xe7, 0xf0, 0x01, 0xf0 };
61
62 /* All the kernels which support Thumb support using a specific undefined
63 instruction for the Thumb breakpoint. */
64
65 static const char arm_linux_thumb_be_breakpoint[] = {0xde, 0x01};
66
67 static const char arm_linux_thumb_le_breakpoint[] = {0x01, 0xde};
68
69 /* Description of the longjmp buffer. */
70 #define ARM_LINUX_JB_ELEMENT_SIZE INT_REGISTER_SIZE
71 #define ARM_LINUX_JB_PC 21
72
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,
75 into VALBUF. */
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. */
80 static void
81 arm_linux_extract_return_value (struct type *type,
82 char regbuf[],
83 char *valbuf)
84 {
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()). */
90
91 int regnum = ((TYPE_CODE_FLT == TYPE_CODE (type))
92 ? ARM_F0_REGNUM : ARM_A1_REGNUM);
93 memcpy (valbuf, &regbuf[DEPRECATED_REGISTER_BYTE (regnum)], TYPE_LENGTH (type));
94 }
95
96 /*
97 Dynamic Linking on ARM GNU/Linux
98 --------------------------------
99
100 Note: PLT = procedure linkage table
101 GOT = global offset table
102
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
106 constraints.
107
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
111 libraries).
112
113 2) The technique must be such that all executable areas must not be
114 modified; and any modified areas must not be executed.
115
116 To do this, there are three steps involved in a typical jump:
117
118 1) in the code
119 2) through the PLT
120 3) using a pointer from the GOT
121
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.
130
131 1) In the code:
132
133 b function_call
134 bl function_call
135
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.
143
144 2) In the PLT:
145
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:
149
150 PLT[0]:
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
155
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:
159
160 lr = (&GOT[3] - . - 20) + (. + 8)
161 lr = (&GOT[3] - 12)
162 lr = &GOT[0]
163
164 On the fourth line, the pc and lr are both updated, so that:
165
166 pc = GOT[2]
167 lr = &GOT[0] + 8
168 = &GOT[2]
169
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.
172
173 PLT[n+1]:
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] - .
178
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.
183
184 3) In the GOT:
185
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.
191
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].
196
197 The special entries in the GOT are:
198
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
202
203 The first invocation of function call comes through and uses the
204 fixup/resolver code. On the entry to the fixup/resolver code:
205
206 ip = &GOT[n+3]
207 lr = &GOT[2]
208 stack[0] = return address (lr) of the function call
209 [r0, r1, r2, r3] are still the arguments to the function call
210
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]. */
214
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). */
222
223 static struct link_map_offsets *
224 arm_linux_svr4_fetch_link_map_offsets (void)
225 {
226 static struct link_map_offsets lmo;
227 static struct link_map_offsets *lmp = 0;
228
229 if (lmp == 0)
230 {
231 lmp = &lmo;
232
233 lmo.r_debug_size = 8; /* Actual size is 20, but this is all we
234 need. */
235
236 lmo.r_map_offset = 4;
237 lmo.r_map_size = 4;
238
239 lmo.link_map_size = 20; /* Actual size is 552, but this is all we
240 need. */
241
242 lmo.l_addr_offset = 0;
243 lmo.l_addr_size = 4;
244
245 lmo.l_name_offset = 4;
246 lmo.l_name_size = 4;
247
248 lmo.l_next_offset = 12;
249 lmo.l_next_size = 4;
250
251 lmo.l_prev_offset = 16;
252 lmo.l_prev_size = 4;
253 }
254
255 return lmp;
256 }
257
258 /* The constants below were determined by examining the following files
259 in the linux kernel sources:
260
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 */
265
266 #define ARM_LINUX_SIGRETURN_INSTR 0xef900077
267 #define ARM_LINUX_RT_SIGRETURN_INSTR 0xef9000ad
268
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
271 exported. */
272 #define ARM_SET_R7_SIGRETURN 0xe3a07077
273 #define ARM_SET_R7_RT_SIGRETURN 0xe3a070ad
274 #define ARM_EABI_SYSCALL 0xef000000
275
276 static void
277 arm_linux_sigtramp_cache (struct frame_info *next_frame,
278 struct trad_frame_cache *this_cache,
279 CORE_ADDR func, int regs_offset)
280 {
281 CORE_ADDR sp = frame_unwind_register_unsigned (next_frame, ARM_SP_REGNUM);
282 CORE_ADDR base = sp + regs_offset;
283 int i;
284
285 for (i = 0; i < 16; i++)
286 trad_frame_set_reg_addr (this_cache, i, base + i * 4);
287
288 trad_frame_set_reg_addr (this_cache, ARM_PS_REGNUM, base + 16 * 4);
289
290 /* The VFP or iWMMXt registers may be saved on the stack, but there's
291 no reliable way to restore them (yet). */
292
293 /* Save a frame ID. */
294 trad_frame_set_id (this_cache, frame_id_build (sp, func));
295 }
296
297 static void
298 arm_linux_sigreturn_init (const struct tramp_frame *self,
299 struct frame_info *next_frame,
300 struct trad_frame_cache *this_cache,
301 CORE_ADDR func)
302 {
303 arm_linux_sigtramp_cache (next_frame, this_cache, func,
304 0x0c /* Offset to registers. */);
305 }
306
307 static void
308 arm_linux_rt_sigreturn_init (const struct tramp_frame *self,
309 struct frame_info *next_frame,
310 struct trad_frame_cache *this_cache,
311 CORE_ADDR func)
312 {
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. */);
317 }
318
319 static struct tramp_frame arm_linux_sigreturn_tramp_frame = {
320 SIGTRAMP_FRAME,
321 4,
322 {
323 { ARM_LINUX_SIGRETURN_INSTR, -1 },
324 { TRAMP_SENTINEL_INSN }
325 },
326 arm_linux_sigreturn_init
327 };
328
329 static struct tramp_frame arm_linux_rt_sigreturn_tramp_frame = {
330 SIGTRAMP_FRAME,
331 4,
332 {
333 { ARM_LINUX_RT_SIGRETURN_INSTR, -1 },
334 { TRAMP_SENTINEL_INSN }
335 },
336 arm_linux_rt_sigreturn_init
337 };
338
339 static struct tramp_frame arm_eabi_linux_sigreturn_tramp_frame = {
340 SIGTRAMP_FRAME,
341 4,
342 {
343 { ARM_SET_R7_SIGRETURN, -1 },
344 { ARM_EABI_SYSCALL, -1 },
345 { TRAMP_SENTINEL_INSN }
346 },
347 arm_linux_sigreturn_init
348 };
349
350 static struct tramp_frame arm_eabi_linux_rt_sigreturn_tramp_frame = {
351 SIGTRAMP_FRAME,
352 4,
353 {
354 { ARM_SET_R7_RT_SIGRETURN, -1 },
355 { ARM_EABI_SYSCALL, -1 },
356 { TRAMP_SENTINEL_INSN }
357 },
358 arm_linux_rt_sigreturn_init
359 };
360
361 static void
362 arm_linux_init_abi (struct gdbarch_info info,
363 struct gdbarch *gdbarch)
364 {
365 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
366
367 tdep->lowest_pc = 0x8000;
368 if (info.byte_order == BFD_ENDIAN_BIG)
369 {
370 if (tdep->arm_abi == ARM_ABI_AAPCS)
371 tdep->arm_breakpoint = eabi_linux_arm_be_breakpoint;
372 else
373 tdep->arm_breakpoint = arm_linux_arm_be_breakpoint;
374 tdep->thumb_breakpoint = arm_linux_thumb_be_breakpoint;
375 }
376 else
377 {
378 if (tdep->arm_abi == ARM_ABI_AAPCS)
379 tdep->arm_breakpoint = eabi_linux_arm_le_breakpoint;
380 else
381 tdep->arm_breakpoint = arm_linux_arm_le_breakpoint;
382 tdep->thumb_breakpoint = arm_linux_thumb_le_breakpoint;
383 }
384 tdep->arm_breakpoint_size = sizeof (arm_linux_arm_le_breakpoint);
385 tdep->thumb_breakpoint_size = sizeof (arm_linux_thumb_le_breakpoint);
386
387 if (tdep->fp_model == ARM_FLOAT_AUTO)
388 tdep->fp_model = ARM_FLOAT_FPA;
389
390 tdep->jb_pc = ARM_LINUX_JB_PC;
391 tdep->jb_elt_size = ARM_LINUX_JB_ELEMENT_SIZE;
392
393 set_solib_svr4_fetch_link_map_offsets
394 (gdbarch, arm_linux_svr4_fetch_link_map_offsets);
395
396 /* The following override shouldn't be needed. */
397 set_gdbarch_deprecated_extract_return_value (gdbarch, arm_linux_extract_return_value);
398
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);
402
403 /* Enable TLS support. */
404 set_gdbarch_fetch_tls_load_module_address (gdbarch,
405 svr4_fetch_objfile_link_map);
406
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);
415 }
416
417 void
418 _initialize_arm_linux_tdep (void)
419 {
420 gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_LINUX,
421 arm_linux_init_abi);
422 }
This page took 0.040082 seconds and 4 git commands to generate.