| 1 | /* GNU/Linux on ARM target support. |
| 2 | |
| 3 | Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 |
| 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, ®buf[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 | /* 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 | |
| 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 |
| 232 | |
| 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) |
| 237 | { |
| 238 | CORE_ADDR sp = frame_unwind_register_unsigned (next_frame, ARM_SP_REGNUM); |
| 239 | CORE_ADDR base = sp + regs_offset; |
| 240 | int i; |
| 241 | |
| 242 | for (i = 0; i < 16; i++) |
| 243 | trad_frame_set_reg_addr (this_cache, i, base + i * 4); |
| 244 | |
| 245 | trad_frame_set_reg_addr (this_cache, ARM_PS_REGNUM, base + 16 * 4); |
| 246 | |
| 247 | /* The VFP or iWMMXt registers may be saved on the stack, but there's |
| 248 | no reliable way to restore them (yet). */ |
| 249 | |
| 250 | /* Save a frame ID. */ |
| 251 | trad_frame_set_id (this_cache, frame_id_build (sp, func)); |
| 252 | } |
| 253 | |
| 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) |
| 259 | { |
| 260 | arm_linux_sigtramp_cache (next_frame, this_cache, func, |
| 261 | 0x0c /* Offset to registers. */); |
| 262 | } |
| 263 | |
| 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. */); |
| 274 | } |
| 275 | |
| 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 | |
| 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; |
| 325 | if (info.byte_order == BFD_ENDIAN_BIG) |
| 326 | { |
| 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; |
| 331 | tdep->thumb_breakpoint = arm_linux_thumb_be_breakpoint; |
| 332 | } |
| 333 | else |
| 334 | { |
| 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; |
| 339 | tdep->thumb_breakpoint = arm_linux_thumb_le_breakpoint; |
| 340 | } |
| 341 | tdep->arm_breakpoint_size = sizeof (arm_linux_arm_le_breakpoint); |
| 342 | tdep->thumb_breakpoint_size = sizeof (arm_linux_thumb_le_breakpoint); |
| 343 | |
| 344 | if (tdep->fp_model == ARM_FLOAT_AUTO) |
| 345 | tdep->fp_model = ARM_FLOAT_FPA; |
| 346 | |
| 347 | tdep->jb_pc = ARM_LINUX_JB_PC; |
| 348 | tdep->jb_elt_size = ARM_LINUX_JB_ELEMENT_SIZE; |
| 349 | |
| 350 | set_solib_svr4_fetch_link_map_offsets |
| 351 | (gdbarch, svr4_ilp32_fetch_link_map_offsets); |
| 352 | |
| 353 | /* The following override shouldn't be needed. */ |
| 354 | set_gdbarch_deprecated_extract_return_value (gdbarch, arm_linux_extract_return_value); |
| 355 | |
| 356 | /* Shared library handling. */ |
| 357 | set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target); |
| 358 | set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver); |
| 359 | |
| 360 | /* Enable TLS support. */ |
| 361 | set_gdbarch_fetch_tls_load_module_address (gdbarch, |
| 362 | svr4_fetch_objfile_link_map); |
| 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); |
| 372 | } |
| 373 | |
| 374 | void |
| 375 | _initialize_arm_linux_tdep (void) |
| 376 | { |
| 377 | gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_LINUX, |
| 378 | arm_linux_init_abi); |
| 379 | } |