Commit | Line | Data |
---|---|---|
faf5f7ad | 1 | /* GNU/Linux on ARM target support. |
0fd88904 | 2 | |
76a9d10f | 3 | Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 |
8e9d1a24 | 4 | Free Software Foundation, Inc. |
faf5f7ad SB |
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 | |
197e01b6 EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
faf5f7ad SB |
22 | |
23 | #include "defs.h" | |
c20f6dea SB |
24 | #include "target.h" |
25 | #include "value.h" | |
faf5f7ad | 26 | #include "gdbtypes.h" |
134e61c4 | 27 | #include "floatformat.h" |
2a451106 KB |
28 | #include "gdbcore.h" |
29 | #include "frame.h" | |
4e052eda | 30 | #include "regcache.h" |
d16aafd8 | 31 | #include "doublest.h" |
7aa1783e | 32 | #include "solib-svr4.h" |
4be87837 | 33 | #include "osabi.h" |
8e9d1a24 DJ |
34 | #include "trad-frame.h" |
35 | #include "tramp-frame.h" | |
faf5f7ad | 36 | |
34e8f22d | 37 | #include "arm-tdep.h" |
0670c0aa | 38 | #include "glibc-tdep.h" |
a52e6aac | 39 | |
8e9d1a24 DJ |
40 | #include "gdb_string.h" |
41 | ||
fdf39c9a RE |
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 | |
498b1f87 | 46 | SIGTRAP, and wakes the debugger. */ |
66e810cd | 47 | |
2ef47cd0 DJ |
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 }; | |
66e810cd | 51 | |
c75a2cc8 DJ |
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 | ||
498b1f87 DJ |
65 | static const char arm_linux_thumb_be_breakpoint[] = {0xde, 0x01}; |
66 | ||
67 | static const char arm_linux_thumb_le_breakpoint[] = {0x01, 0xde}; | |
68 | ||
9df628e0 | 69 | /* Description of the longjmp buffer. */ |
7a5ea0d4 | 70 | #define ARM_LINUX_JB_ELEMENT_SIZE INT_REGISTER_SIZE |
a6cdd8c5 | 71 | #define ARM_LINUX_JB_PC 21 |
faf5f7ad | 72 | |
faf5f7ad SB |
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. */ | |
19d3fc80 RE |
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 | |
faf5f7ad | 81 | arm_linux_extract_return_value (struct type *type, |
03926e1f DJ |
82 | gdb_byte regbuf[], |
83 | gdb_byte *valbuf) | |
faf5f7ad SB |
84 | { |
85 | /* ScottB: This needs to be looked at to handle the different | |
fdf39c9a | 86 | floating point emulators on ARM GNU/Linux. Right now the code |
faf5f7ad SB |
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 | ||
34e8f22d RE |
91 | int regnum = ((TYPE_CODE_FLT == TYPE_CODE (type)) |
92 | ? ARM_F0_REGNUM : ARM_A1_REGNUM); | |
62700349 | 93 | memcpy (valbuf, ®buf[DEPRECATED_REGISTER_BYTE (regnum)], TYPE_LENGTH (type)); |
faf5f7ad | 94 | } |
134e61c4 | 95 | |
f38e884d | 96 | /* |
fdf39c9a RE |
97 | Dynamic Linking on ARM GNU/Linux |
98 | -------------------------------- | |
f38e884d SB |
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 | |
fdf39c9a RE |
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. | |
f38e884d SB |
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 | ||
2a451106 KB |
215 | /* The constants below were determined by examining the following files |
216 | in the linux kernel sources: | |
217 | ||
218 | arch/arm/kernel/signal.c | |
219 | - see SWI_SYS_SIGRETURN and SWI_SYS_RT_SIGRETURN | |
220 | include/asm-arm/unistd.h | |
221 | - see __NR_sigreturn, __NR_rt_sigreturn, and __NR_SYSCALL_BASE */ | |
222 | ||
223 | #define ARM_LINUX_SIGRETURN_INSTR 0xef900077 | |
224 | #define ARM_LINUX_RT_SIGRETURN_INSTR 0xef9000ad | |
225 | ||
8e9d1a24 DJ |
226 | /* For ARM EABI, recognize the pattern that glibc uses... alternatively, |
227 | we could arrange to do this by function name, but they are not always | |
228 | exported. */ | |
229 | #define ARM_SET_R7_SIGRETURN 0xe3a07077 | |
230 | #define ARM_SET_R7_RT_SIGRETURN 0xe3a070ad | |
231 | #define ARM_EABI_SYSCALL 0xef000000 | |
2a451106 | 232 | |
8e9d1a24 DJ |
233 | static void |
234 | arm_linux_sigtramp_cache (struct frame_info *next_frame, | |
235 | struct trad_frame_cache *this_cache, | |
236 | CORE_ADDR func, int regs_offset) | |
2a451106 | 237 | { |
8e9d1a24 DJ |
238 | CORE_ADDR sp = frame_unwind_register_unsigned (next_frame, ARM_SP_REGNUM); |
239 | CORE_ADDR base = sp + regs_offset; | |
240 | int i; | |
2a451106 | 241 | |
8e9d1a24 DJ |
242 | for (i = 0; i < 16; i++) |
243 | trad_frame_set_reg_addr (this_cache, i, base + i * 4); | |
2a451106 | 244 | |
8e9d1a24 | 245 | trad_frame_set_reg_addr (this_cache, ARM_PS_REGNUM, base + 16 * 4); |
2a451106 | 246 | |
8e9d1a24 DJ |
247 | /* The VFP or iWMMXt registers may be saved on the stack, but there's |
248 | no reliable way to restore them (yet). */ | |
2a451106 | 249 | |
8e9d1a24 DJ |
250 | /* Save a frame ID. */ |
251 | trad_frame_set_id (this_cache, frame_id_build (sp, func)); | |
252 | } | |
2a451106 | 253 | |
8e9d1a24 DJ |
254 | static void |
255 | arm_linux_sigreturn_init (const struct tramp_frame *self, | |
256 | struct frame_info *next_frame, | |
257 | struct trad_frame_cache *this_cache, | |
258 | CORE_ADDR func) | |
2a451106 | 259 | { |
8e9d1a24 DJ |
260 | arm_linux_sigtramp_cache (next_frame, this_cache, func, |
261 | 0x0c /* Offset to registers. */); | |
262 | } | |
2a451106 | 263 | |
8e9d1a24 DJ |
264 | static void |
265 | arm_linux_rt_sigreturn_init (const struct tramp_frame *self, | |
266 | struct frame_info *next_frame, | |
267 | struct trad_frame_cache *this_cache, | |
268 | CORE_ADDR func) | |
269 | { | |
270 | arm_linux_sigtramp_cache (next_frame, this_cache, func, | |
271 | 0x88 /* Offset to ucontext_t. */ | |
272 | + 0x14 /* Offset to sigcontext. */ | |
273 | + 0x0c /* Offset to registers. */); | |
2a451106 KB |
274 | } |
275 | ||
8e9d1a24 DJ |
276 | static struct tramp_frame arm_linux_sigreturn_tramp_frame = { |
277 | SIGTRAMP_FRAME, | |
278 | 4, | |
279 | { | |
280 | { ARM_LINUX_SIGRETURN_INSTR, -1 }, | |
281 | { TRAMP_SENTINEL_INSN } | |
282 | }, | |
283 | arm_linux_sigreturn_init | |
284 | }; | |
285 | ||
286 | static struct tramp_frame arm_linux_rt_sigreturn_tramp_frame = { | |
287 | SIGTRAMP_FRAME, | |
288 | 4, | |
289 | { | |
290 | { ARM_LINUX_RT_SIGRETURN_INSTR, -1 }, | |
291 | { TRAMP_SENTINEL_INSN } | |
292 | }, | |
293 | arm_linux_rt_sigreturn_init | |
294 | }; | |
295 | ||
296 | static struct tramp_frame arm_eabi_linux_sigreturn_tramp_frame = { | |
297 | SIGTRAMP_FRAME, | |
298 | 4, | |
299 | { | |
300 | { ARM_SET_R7_SIGRETURN, -1 }, | |
301 | { ARM_EABI_SYSCALL, -1 }, | |
302 | { TRAMP_SENTINEL_INSN } | |
303 | }, | |
304 | arm_linux_sigreturn_init | |
305 | }; | |
306 | ||
307 | static struct tramp_frame arm_eabi_linux_rt_sigreturn_tramp_frame = { | |
308 | SIGTRAMP_FRAME, | |
309 | 4, | |
310 | { | |
311 | { ARM_SET_R7_RT_SIGRETURN, -1 }, | |
312 | { ARM_EABI_SYSCALL, -1 }, | |
313 | { TRAMP_SENTINEL_INSN } | |
314 | }, | |
315 | arm_linux_rt_sigreturn_init | |
316 | }; | |
317 | ||
97e03143 RE |
318 | static void |
319 | arm_linux_init_abi (struct gdbarch_info info, | |
320 | struct gdbarch *gdbarch) | |
321 | { | |
322 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
323 | ||
324 | tdep->lowest_pc = 0x8000; | |
2ef47cd0 | 325 | if (info.byte_order == BFD_ENDIAN_BIG) |
498b1f87 | 326 | { |
c75a2cc8 DJ |
327 | if (tdep->arm_abi == ARM_ABI_AAPCS) |
328 | tdep->arm_breakpoint = eabi_linux_arm_be_breakpoint; | |
329 | else | |
330 | tdep->arm_breakpoint = arm_linux_arm_be_breakpoint; | |
498b1f87 DJ |
331 | tdep->thumb_breakpoint = arm_linux_thumb_be_breakpoint; |
332 | } | |
2ef47cd0 | 333 | else |
498b1f87 | 334 | { |
c75a2cc8 DJ |
335 | if (tdep->arm_abi == ARM_ABI_AAPCS) |
336 | tdep->arm_breakpoint = eabi_linux_arm_le_breakpoint; | |
337 | else | |
338 | tdep->arm_breakpoint = arm_linux_arm_le_breakpoint; | |
498b1f87 DJ |
339 | tdep->thumb_breakpoint = arm_linux_thumb_le_breakpoint; |
340 | } | |
66e810cd | 341 | tdep->arm_breakpoint_size = sizeof (arm_linux_arm_le_breakpoint); |
498b1f87 | 342 | tdep->thumb_breakpoint_size = sizeof (arm_linux_thumb_le_breakpoint); |
9df628e0 | 343 | |
28e97307 DJ |
344 | if (tdep->fp_model == ARM_FLOAT_AUTO) |
345 | tdep->fp_model = ARM_FLOAT_FPA; | |
fd50bc42 | 346 | |
a6cdd8c5 RE |
347 | tdep->jb_pc = ARM_LINUX_JB_PC; |
348 | tdep->jb_elt_size = ARM_LINUX_JB_ELEMENT_SIZE; | |
19d3fc80 | 349 | |
7aa1783e | 350 | set_solib_svr4_fetch_link_map_offsets |
76a9d10f | 351 | (gdbarch, svr4_ilp32_fetch_link_map_offsets); |
7aa1783e | 352 | |
84320456 | 353 | /* The following override shouldn't be needed. */ |
26e9b323 | 354 | set_gdbarch_deprecated_extract_return_value (gdbarch, arm_linux_extract_return_value); |
0e18d038 RE |
355 | |
356 | /* Shared library handling. */ | |
0e18d038 | 357 | set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target); |
bb41a796 | 358 | set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver); |
b2756930 KB |
359 | |
360 | /* Enable TLS support. */ | |
361 | set_gdbarch_fetch_tls_load_module_address (gdbarch, | |
362 | svr4_fetch_objfile_link_map); | |
8e9d1a24 DJ |
363 | |
364 | tramp_frame_prepend_unwinder (gdbarch, | |
365 | &arm_linux_sigreturn_tramp_frame); | |
366 | tramp_frame_prepend_unwinder (gdbarch, | |
367 | &arm_linux_rt_sigreturn_tramp_frame); | |
368 | tramp_frame_prepend_unwinder (gdbarch, | |
369 | &arm_eabi_linux_sigreturn_tramp_frame); | |
370 | tramp_frame_prepend_unwinder (gdbarch, | |
371 | &arm_eabi_linux_rt_sigreturn_tramp_frame); | |
97e03143 RE |
372 | } |
373 | ||
faf5f7ad SB |
374 | void |
375 | _initialize_arm_linux_tdep (void) | |
376 | { | |
05816f70 MK |
377 | gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_LINUX, |
378 | arm_linux_init_abi); | |
faf5f7ad | 379 | } |