Commit | Line | Data |
---|---|---|
faf5f7ad | 1 | /* GNU/Linux on ARM target support. |
0fd88904 AC |
2 | |
3 | Copyright 1999, 2000, 2001, 2002, 2003, 2005 Free Software | |
4 | 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 | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
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" |
faf5f7ad | 34 | |
34e8f22d | 35 | #include "arm-tdep.h" |
0670c0aa | 36 | #include "glibc-tdep.h" |
a52e6aac | 37 | |
fdf39c9a RE |
38 | /* Under ARM GNU/Linux the traditional way of performing a breakpoint |
39 | is to execute a particular software interrupt, rather than use a | |
40 | particular undefined instruction to provoke a trap. Upon exection | |
41 | of the software interrupt the kernel stops the inferior with a | |
498b1f87 | 42 | SIGTRAP, and wakes the debugger. */ |
66e810cd | 43 | |
2ef47cd0 DJ |
44 | static const char arm_linux_arm_le_breakpoint[] = { 0x01, 0x00, 0x9f, 0xef }; |
45 | ||
46 | static const char arm_linux_arm_be_breakpoint[] = { 0xef, 0x9f, 0x00, 0x01 }; | |
66e810cd | 47 | |
c75a2cc8 DJ |
48 | /* However, the EABI syscall interface (new in Nov. 2005) does not look at |
49 | the operand of the swi if old-ABI compatibility is disabled. Therefore, | |
50 | use an undefined instruction instead. This is supported as of kernel | |
51 | version 2.5.70 (May 2003), so should be a safe assumption for EABI | |
52 | binaries. */ | |
53 | ||
54 | static const char eabi_linux_arm_le_breakpoint[] = { 0xf0, 0x01, 0xf0, 0xe7 }; | |
55 | ||
56 | static const char eabi_linux_arm_be_breakpoint[] = { 0xe7, 0xf0, 0x01, 0xf0 }; | |
57 | ||
58 | /* All the kernels which support Thumb support using a specific undefined | |
59 | instruction for the Thumb breakpoint. */ | |
60 | ||
498b1f87 DJ |
61 | static const char arm_linux_thumb_be_breakpoint[] = {0xde, 0x01}; |
62 | ||
63 | static const char arm_linux_thumb_le_breakpoint[] = {0x01, 0xde}; | |
64 | ||
9df628e0 | 65 | /* Description of the longjmp buffer. */ |
7a5ea0d4 | 66 | #define ARM_LINUX_JB_ELEMENT_SIZE INT_REGISTER_SIZE |
a6cdd8c5 | 67 | #define ARM_LINUX_JB_PC 21 |
faf5f7ad | 68 | |
faf5f7ad SB |
69 | /* Extract from an array REGBUF containing the (raw) register state |
70 | a function return value of type TYPE, and copy that, in virtual format, | |
71 | into VALBUF. */ | |
19d3fc80 RE |
72 | /* FIXME rearnsha/2002-02-23: This function shouldn't be necessary. |
73 | The ARM generic one should be able to handle the model used by | |
74 | linux and the low-level formatting of the registers should be | |
75 | hidden behind the regcache abstraction. */ | |
76 | static void | |
faf5f7ad | 77 | arm_linux_extract_return_value (struct type *type, |
b8b527c5 | 78 | char regbuf[], |
faf5f7ad SB |
79 | char *valbuf) |
80 | { | |
81 | /* ScottB: This needs to be looked at to handle the different | |
fdf39c9a | 82 | floating point emulators on ARM GNU/Linux. Right now the code |
faf5f7ad SB |
83 | assumes that fetch inferior registers does the right thing for |
84 | GDB. I suspect this won't handle NWFPE registers correctly, nor | |
85 | will the default ARM version (arm_extract_return_value()). */ | |
86 | ||
34e8f22d RE |
87 | int regnum = ((TYPE_CODE_FLT == TYPE_CODE (type)) |
88 | ? ARM_F0_REGNUM : ARM_A1_REGNUM); | |
62700349 | 89 | memcpy (valbuf, ®buf[DEPRECATED_REGISTER_BYTE (regnum)], TYPE_LENGTH (type)); |
faf5f7ad | 90 | } |
134e61c4 | 91 | |
f38e884d | 92 | /* |
fdf39c9a RE |
93 | Dynamic Linking on ARM GNU/Linux |
94 | -------------------------------- | |
f38e884d SB |
95 | |
96 | Note: PLT = procedure linkage table | |
97 | GOT = global offset table | |
98 | ||
99 | As much as possible, ELF dynamic linking defers the resolution of | |
100 | jump/call addresses until the last minute. The technique used is | |
101 | inspired by the i386 ELF design, and is based on the following | |
102 | constraints. | |
103 | ||
104 | 1) The calling technique should not force a change in the assembly | |
105 | code produced for apps; it MAY cause changes in the way assembly | |
106 | code is produced for position independent code (i.e. shared | |
107 | libraries). | |
108 | ||
109 | 2) The technique must be such that all executable areas must not be | |
110 | modified; and any modified areas must not be executed. | |
111 | ||
112 | To do this, there are three steps involved in a typical jump: | |
113 | ||
114 | 1) in the code | |
115 | 2) through the PLT | |
116 | 3) using a pointer from the GOT | |
117 | ||
118 | When the executable or library is first loaded, each GOT entry is | |
119 | initialized to point to the code which implements dynamic name | |
120 | resolution and code finding. This is normally a function in the | |
fdf39c9a RE |
121 | program interpreter (on ARM GNU/Linux this is usually |
122 | ld-linux.so.2, but it does not have to be). On the first | |
123 | invocation, the function is located and the GOT entry is replaced | |
124 | with the real function address. Subsequent calls go through steps | |
125 | 1, 2 and 3 and end up calling the real code. | |
f38e884d SB |
126 | |
127 | 1) In the code: | |
128 | ||
129 | b function_call | |
130 | bl function_call | |
131 | ||
132 | This is typical ARM code using the 26 bit relative branch or branch | |
133 | and link instructions. The target of the instruction | |
134 | (function_call is usually the address of the function to be called. | |
135 | In position independent code, the target of the instruction is | |
136 | actually an entry in the PLT when calling functions in a shared | |
137 | library. Note that this call is identical to a normal function | |
138 | call, only the target differs. | |
139 | ||
140 | 2) In the PLT: | |
141 | ||
142 | The PLT is a synthetic area, created by the linker. It exists in | |
143 | both executables and libraries. It is an array of stubs, one per | |
144 | imported function call. It looks like this: | |
145 | ||
146 | PLT[0]: | |
147 | str lr, [sp, #-4]! @push the return address (lr) | |
148 | ldr lr, [pc, #16] @load from 6 words ahead | |
149 | add lr, pc, lr @form an address for GOT[0] | |
150 | ldr pc, [lr, #8]! @jump to the contents of that addr | |
151 | ||
152 | The return address (lr) is pushed on the stack and used for | |
153 | calculations. The load on the second line loads the lr with | |
154 | &GOT[3] - . - 20. The addition on the third leaves: | |
155 | ||
156 | lr = (&GOT[3] - . - 20) + (. + 8) | |
157 | lr = (&GOT[3] - 12) | |
158 | lr = &GOT[0] | |
159 | ||
160 | On the fourth line, the pc and lr are both updated, so that: | |
161 | ||
162 | pc = GOT[2] | |
163 | lr = &GOT[0] + 8 | |
164 | = &GOT[2] | |
165 | ||
166 | NOTE: PLT[0] borrows an offset .word from PLT[1]. This is a little | |
167 | "tight", but allows us to keep all the PLT entries the same size. | |
168 | ||
169 | PLT[n+1]: | |
170 | ldr ip, [pc, #4] @load offset from gotoff | |
171 | add ip, pc, ip @add the offset to the pc | |
172 | ldr pc, [ip] @jump to that address | |
173 | gotoff: .word GOT[n+3] - . | |
174 | ||
175 | The load on the first line, gets an offset from the fourth word of | |
176 | the PLT entry. The add on the second line makes ip = &GOT[n+3], | |
177 | which contains either a pointer to PLT[0] (the fixup trampoline) or | |
178 | a pointer to the actual code. | |
179 | ||
180 | 3) In the GOT: | |
181 | ||
182 | The GOT contains helper pointers for both code (PLT) fixups and | |
183 | data fixups. The first 3 entries of the GOT are special. The next | |
184 | M entries (where M is the number of entries in the PLT) belong to | |
185 | the PLT fixups. The next D (all remaining) entries belong to | |
186 | various data fixups. The actual size of the GOT is 3 + M + D. | |
187 | ||
188 | The GOT is also a synthetic area, created by the linker. It exists | |
189 | in both executables and libraries. When the GOT is first | |
190 | initialized , all the GOT entries relating to PLT fixups are | |
191 | pointing to code back at PLT[0]. | |
192 | ||
193 | The special entries in the GOT are: | |
194 | ||
195 | GOT[0] = linked list pointer used by the dynamic loader | |
196 | GOT[1] = pointer to the reloc table for this module | |
197 | GOT[2] = pointer to the fixup/resolver code | |
198 | ||
199 | The first invocation of function call comes through and uses the | |
200 | fixup/resolver code. On the entry to the fixup/resolver code: | |
201 | ||
202 | ip = &GOT[n+3] | |
203 | lr = &GOT[2] | |
204 | stack[0] = return address (lr) of the function call | |
205 | [r0, r1, r2, r3] are still the arguments to the function call | |
206 | ||
207 | This is enough information for the fixup/resolver code to work | |
208 | with. Before the fixup/resolver code returns, it actually calls | |
209 | the requested function and repairs &GOT[n+3]. */ | |
210 | ||
7aa1783e RE |
211 | /* Fetch, and possibly build, an appropriate link_map_offsets structure |
212 | for ARM linux targets using the struct offsets defined in <link.h>. | |
213 | Note, however, that link.h is not actually referred to in this file. | |
214 | Instead, the relevant structs offsets were obtained from examining | |
215 | link.h. (We can't refer to link.h from this file because the host | |
216 | system won't necessarily have it, or if it does, the structs which | |
217 | it defines will refer to the host system, not the target). */ | |
218 | ||
219 | static struct link_map_offsets * | |
220 | arm_linux_svr4_fetch_link_map_offsets (void) | |
221 | { | |
222 | static struct link_map_offsets lmo; | |
223 | static struct link_map_offsets *lmp = 0; | |
224 | ||
225 | if (lmp == 0) | |
226 | { | |
227 | lmp = &lmo; | |
228 | ||
229 | lmo.r_debug_size = 8; /* Actual size is 20, but this is all we | |
230 | need. */ | |
231 | ||
232 | lmo.r_map_offset = 4; | |
233 | lmo.r_map_size = 4; | |
234 | ||
235 | lmo.link_map_size = 20; /* Actual size is 552, but this is all we | |
236 | need. */ | |
237 | ||
238 | lmo.l_addr_offset = 0; | |
239 | lmo.l_addr_size = 4; | |
240 | ||
241 | lmo.l_name_offset = 4; | |
242 | lmo.l_name_size = 4; | |
243 | ||
244 | lmo.l_next_offset = 12; | |
245 | lmo.l_next_size = 4; | |
246 | ||
247 | lmo.l_prev_offset = 16; | |
248 | lmo.l_prev_size = 4; | |
249 | } | |
250 | ||
251 | return lmp; | |
252 | } | |
253 | ||
2a451106 KB |
254 | /* The constants below were determined by examining the following files |
255 | in the linux kernel sources: | |
256 | ||
257 | arch/arm/kernel/signal.c | |
258 | - see SWI_SYS_SIGRETURN and SWI_SYS_RT_SIGRETURN | |
259 | include/asm-arm/unistd.h | |
260 | - see __NR_sigreturn, __NR_rt_sigreturn, and __NR_SYSCALL_BASE */ | |
261 | ||
262 | #define ARM_LINUX_SIGRETURN_INSTR 0xef900077 | |
263 | #define ARM_LINUX_RT_SIGRETURN_INSTR 0xef9000ad | |
264 | ||
265 | /* arm_linux_in_sigtramp determines if PC points at one of the | |
266 | instructions which cause control to return to the Linux kernel upon | |
267 | return from a signal handler. FUNC_NAME is unused. */ | |
268 | ||
269 | int | |
270 | arm_linux_in_sigtramp (CORE_ADDR pc, char *func_name) | |
271 | { | |
272 | unsigned long inst; | |
273 | ||
274 | inst = read_memory_integer (pc, 4); | |
275 | ||
276 | return (inst == ARM_LINUX_SIGRETURN_INSTR | |
277 | || inst == ARM_LINUX_RT_SIGRETURN_INSTR); | |
278 | ||
279 | } | |
280 | ||
281 | /* arm_linux_sigcontext_register_address returns the address in the | |
282 | sigcontext of register REGNO given a stack pointer value SP and | |
283 | program counter value PC. The value 0 is returned if PC is not | |
284 | pointing at one of the signal return instructions or if REGNO is | |
285 | not saved in the sigcontext struct. */ | |
286 | ||
287 | CORE_ADDR | |
288 | arm_linux_sigcontext_register_address (CORE_ADDR sp, CORE_ADDR pc, int regno) | |
289 | { | |
290 | unsigned long inst; | |
291 | CORE_ADDR reg_addr = 0; | |
292 | ||
293 | inst = read_memory_integer (pc, 4); | |
294 | ||
fdf39c9a RE |
295 | if (inst == ARM_LINUX_SIGRETURN_INSTR |
296 | || inst == ARM_LINUX_RT_SIGRETURN_INSTR) | |
2a451106 KB |
297 | { |
298 | CORE_ADDR sigcontext_addr; | |
299 | ||
300 | /* The sigcontext structure is at different places for the two | |
301 | signal return instructions. For ARM_LINUX_SIGRETURN_INSTR, | |
302 | it starts at the SP value. For ARM_LINUX_RT_SIGRETURN_INSTR, | |
303 | it is at SP+8. For the latter instruction, it may also be | |
304 | the case that the address of this structure may be determined | |
305 | by reading the 4 bytes at SP, but I'm not convinced this is | |
306 | reliable. | |
307 | ||
308 | In any event, these magic constants (0 and 8) may be | |
309 | determined by examining struct sigframe and struct | |
310 | rt_sigframe in arch/arm/kernel/signal.c in the Linux kernel | |
311 | sources. */ | |
312 | ||
313 | if (inst == ARM_LINUX_RT_SIGRETURN_INSTR) | |
314 | sigcontext_addr = sp + 8; | |
315 | else /* inst == ARM_LINUX_SIGRETURN_INSTR */ | |
316 | sigcontext_addr = sp + 0; | |
317 | ||
318 | /* The layout of the sigcontext structure for ARM GNU/Linux is | |
319 | in include/asm-arm/sigcontext.h in the Linux kernel sources. | |
320 | ||
321 | There are three 4-byte fields which precede the saved r0 | |
322 | field. (This accounts for the 12 in the code below.) The | |
323 | sixteen registers (4 bytes per field) follow in order. The | |
324 | PSR value follows the sixteen registers which accounts for | |
325 | the constant 19 below. */ | |
326 | ||
34e8f22d | 327 | if (0 <= regno && regno <= ARM_PC_REGNUM) |
2a451106 | 328 | reg_addr = sigcontext_addr + 12 + (4 * regno); |
34e8f22d | 329 | else if (regno == ARM_PS_REGNUM) |
2a451106 KB |
330 | reg_addr = sigcontext_addr + 19 * 4; |
331 | } | |
332 | ||
333 | return reg_addr; | |
334 | } | |
335 | ||
97e03143 RE |
336 | static void |
337 | arm_linux_init_abi (struct gdbarch_info info, | |
338 | struct gdbarch *gdbarch) | |
339 | { | |
340 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
341 | ||
342 | tdep->lowest_pc = 0x8000; | |
2ef47cd0 | 343 | if (info.byte_order == BFD_ENDIAN_BIG) |
498b1f87 | 344 | { |
c75a2cc8 DJ |
345 | if (tdep->arm_abi == ARM_ABI_AAPCS) |
346 | tdep->arm_breakpoint = eabi_linux_arm_be_breakpoint; | |
347 | else | |
348 | tdep->arm_breakpoint = arm_linux_arm_be_breakpoint; | |
498b1f87 DJ |
349 | tdep->thumb_breakpoint = arm_linux_thumb_be_breakpoint; |
350 | } | |
2ef47cd0 | 351 | else |
498b1f87 | 352 | { |
c75a2cc8 DJ |
353 | if (tdep->arm_abi == ARM_ABI_AAPCS) |
354 | tdep->arm_breakpoint = eabi_linux_arm_le_breakpoint; | |
355 | else | |
356 | tdep->arm_breakpoint = arm_linux_arm_le_breakpoint; | |
498b1f87 DJ |
357 | tdep->thumb_breakpoint = arm_linux_thumb_le_breakpoint; |
358 | } | |
66e810cd | 359 | tdep->arm_breakpoint_size = sizeof (arm_linux_arm_le_breakpoint); |
498b1f87 | 360 | tdep->thumb_breakpoint_size = sizeof (arm_linux_thumb_le_breakpoint); |
9df628e0 | 361 | |
28e97307 DJ |
362 | if (tdep->fp_model == ARM_FLOAT_AUTO) |
363 | tdep->fp_model = ARM_FLOAT_FPA; | |
fd50bc42 | 364 | |
a6cdd8c5 RE |
365 | tdep->jb_pc = ARM_LINUX_JB_PC; |
366 | tdep->jb_elt_size = ARM_LINUX_JB_ELEMENT_SIZE; | |
19d3fc80 | 367 | |
7aa1783e RE |
368 | set_solib_svr4_fetch_link_map_offsets |
369 | (gdbarch, arm_linux_svr4_fetch_link_map_offsets); | |
370 | ||
84320456 | 371 | /* The following override shouldn't be needed. */ |
26e9b323 | 372 | set_gdbarch_deprecated_extract_return_value (gdbarch, arm_linux_extract_return_value); |
0e18d038 RE |
373 | |
374 | /* Shared library handling. */ | |
0e18d038 | 375 | set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target); |
bb41a796 | 376 | set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver); |
b2756930 KB |
377 | |
378 | /* Enable TLS support. */ | |
379 | set_gdbarch_fetch_tls_load_module_address (gdbarch, | |
380 | svr4_fetch_objfile_link_map); | |
97e03143 RE |
381 | } |
382 | ||
faf5f7ad SB |
383 | void |
384 | _initialize_arm_linux_tdep (void) | |
385 | { | |
05816f70 MK |
386 | gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_LINUX, |
387 | arm_linux_init_abi); | |
faf5f7ad | 388 | } |