1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
23 #define ELF_MACHINE_NAME "ARM"
25 #include <sys/param.h>
27 #define VALID_ELF_ABIVERSION(ver) (ver == 0)
28 #define VALID_ELF_OSABI(osabi) \
29 (osabi == ELFOSABI_SYSV || osabi == ELFOSABI_ARM)
30 #define VALID_ELF_HEADER(hdr,exp,size) \
31 memcmp (hdr,exp,size-2) == 0 \
32 && VALID_ELF_OSABI (hdr[EI_OSABI]) \
33 && VALID_ELF_ABIVERSION (hdr[EI_ABIVERSION])
35 /* Return nonzero iff E_MACHINE is compatible with the running host. */
36 static inline int __attribute__ ((unused))
37 elf_machine_matches_host (Elf32_Half e_machine)
49 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
50 first element of the GOT. This must be inlined in a function which
52 static inline Elf32_Addr __attribute__ ((unused))
53 elf_machine_dynamic (void)
55 register Elf32_Addr *got asm ("r10");
60 /* Return the run-time load address of the shared object. */
61 static inline Elf32_Addr __attribute__ ((unused))
62 elf_machine_load_address (void)
64 extern void __dl_start asm ("_dl_start");
65 Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
66 Elf32_Addr pcrel_addr;
67 asm ("adr %0, _dl_start" : "=r" (pcrel_addr));
68 return pcrel_addr - got_addr;
72 /* Set up the loaded object described by L so its unrelocated PLT
73 entries will jump to the on-demand fixup code in dl-runtime.c. */
75 static inline int __attribute__ ((unused))
76 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
79 extern void _dl_runtime_resolve (Elf32_Word);
80 extern void _dl_runtime_profile (Elf32_Word);
82 if (l->l_info[DT_JMPREL] && lazy)
84 /* patb: this is different than i386 */
85 /* The GOT entries for functions in the PLT have not yet been filled
86 in. Their initial contents will arrange when called to push an
87 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
88 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
89 got = (Elf32_Addr *) l->l_info[DT_PLTGOT]->d_un.d_ptr;
90 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
92 /* The got[2] entry contains the address of a function which gets
93 called to get the address of a so far unresolved function and
94 jump to it. The profiling extension of the dynamic linker allows
95 to intercept the calls to collect information. In this case we
96 don't store the address in the GOT so that all future calls also
97 end in this function. */
100 got[2] = (Elf32_Addr) &_dl_runtime_profile;
101 /* Say that we really want profiling and the timers are started. */
105 /* This function will get called to fix up the GOT entry indicated by
106 the offset on the stack, and then jump to the resolved address. */
107 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
112 /* This code is used in dl-runtime.c to call the `fixup' function
113 and then redirect to the address it returns. */
114 // macro for handling PIC situation....
116 #define CALL_ROUTINE(x) " ldr sl,0f
122 0: .word _GLOBAL_OFFSET_TABLE_ - 1b - 4
123 2: .word " #x "(GOTOFF)
126 #define CALL_ROUTINE(x) " bl " #x
130 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
132 .globl _dl_runtime_resolve
133 .type _dl_runtime_resolve, #function
137 @ stack[0] contains the return address from this call
138 @ ip contains &GOT[n+3] (pointer to function)
139 @ lr points to &GOT[2]
141 @ save almost everything; lr is already on the stack
142 stmdb sp!,{r0-r3,sl,fp}
144 @ prepare to call fixup()
145 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
150 @ get pointer to linker struct
154 " CALL_ROUTINE(fixup) "
160 ldmia sp!,{r0-r3,sl,fp,lr}
162 @ jump to the newly found address
165 .size _dl_runtime_resolve, .-_dl_runtime_resolve
167 .globl _dl_runtime_profile
168 .type _dl_runtime_profile, #function
171 @ save almost everything; lr is already on the stack
172 stmdb sp!,{r0-r3,sl,fp}
174 @ prepare to call fixup()
175 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
180 @ get pointer to linker struct
183 @ call profiling fixup routine
184 " CALL_ROUTINE(profile_fixup) "
190 ldmia sp!,{r0-r3,sl,fp,lr}
192 @ jump to the newly found address
195 .size _dl_runtime_resolve, .-_dl_runtime_resolve
199 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
201 .globl _dl_runtime_resolve
202 .globl _dl_runtime_profile
203 .type _dl_runtime_resolve, #function
204 .type _dl_runtime_profile, #function
209 @ stack[0] contains the return address from this call
210 @ ip contains &GOT[n+3] (pointer to function)
211 @ lr points to &GOT[2]
213 @ save almost everything; return add is already on the stack
214 stmdb sp!,{r0-r3,sl,fp}
216 @ prepare to call fixup()
217 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
222 @ get pointer to linker struct
225 @ call profiling fixup routine
226 " CALL_ROUTINE(fixup) "
232 ldmia sp!,{r0-r3,sl,fp,lr}
234 @ jump to the newly found address
237 .size _dl_runtime_profile, .-_dl_runtime_profile
242 /* Mask identifying addresses reserved for the user program,
243 where the dynamic linker should not map anything. */
244 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
246 /* Initial entry point code for the dynamic linker.
247 The C function `_dl_start' is the real entry point;
248 its return value is the user program's entry point. */
250 #define RTLD_START asm ("\
253 .globl _dl_start_user
255 @ at start time, all the args are on the stack
258 @ returns user entry point in r0
261 @ we are PIC code, so get global offset table
265 @ Store the highest stack address
269 @ See if we were run as a command with the executable file
270 @ name as an extra leading argument.
273 @ get the original arg count
275 @ subtract _dl_skip_args from it
277 @ adjust the stack pointer to skip them
278 add sp, sp, r1, lsl #2
279 @ store the new argc in the new stack location
282 @ now we enter a _dl_init_next loop
283 ldr r4, .L_MAIN_SEARCHLIST
286 @ call _dl_init_next to get the address of an initalizer
288 bl _dl_init_next(PLT)
291 @ call the shared-object initializer
292 @ during this call, the stack may get moved around
295 @ go back and look for another initializer
297 1: @ clear the startup flag
298 ldr r2, .L_STARTUP_FLAG
300 @ we know r0==0 at this point
302 @ load the finalizer function
305 @ jump to the user_s entry point
308 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
310 .word _dl_skip_args(GOTOFF) \n\
312 .word _dl_starting_up(GOT)
316 .word __libc_stack_end(GOT)
318 .word _dl_main_searchlist(GOT)
322 /* Nonzero iff TYPE should not be allowed to resolve to one of
323 the main executable's symbols, as for a COPY reloc. */
324 #define elf_machine_lookup_noexec_p(type) ((type) == R_ARM_COPY)
326 /* Nonzero iff TYPE describes relocation of a PLT entry, so
327 PLT entries should not be allowed to define the value. */
328 #define elf_machine_lookup_noplt_p(type) ((type) == R_ARM_JUMP_SLOT)
330 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
331 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
333 /* The ARM never uses Elf32_Rela relocations. */
334 #define ELF_MACHINE_NO_RELA 1
336 /* We define an initialization functions. This is called very early in
338 #define DL_PLATFORM_INIT dl_platform_init ()
340 extern const char *_dl_platform;
342 static inline void __attribute__ ((unused))
343 dl_platform_init (void)
345 if (_dl_platform == NULL)
347 This is where processors could be distinguished arm2, arm6, sa110, etc */
348 _dl_platform = "ARM";
352 elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc,
353 Elf32_Addr *reloc_addr, Elf32_Addr value)
358 /* Return the final value of a plt relocation. */
359 static inline Elf32_Addr
360 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
366 #endif /* !dl_machine_h */
370 extern char **_dl_argv;
372 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
373 MAP is the object containing the reloc. */
376 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
377 const Elf32_Sym *sym, const struct r_found_version *version,
378 Elf32_Addr *const reloc_addr)
380 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_RELATIVE)
382 #ifndef RTLD_BOOTSTRAP
383 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
385 *reloc_addr += map->l_addr;
387 else if (ELF32_R_TYPE (reloc->r_info) != R_ARM_NONE)
389 const Elf32_Sym *const refsym = sym;
390 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
392 value += sym->st_value;
394 switch (ELF32_R_TYPE (reloc->r_info))
398 /* This can happen in trace mode if an object could not be
401 if (sym->st_size > refsym->st_size
402 || (_dl_verbose && sym->st_size < refsym->st_size))
406 strtab = (const void *) map->l_info[DT_STRTAB]->d_un.d_ptr;
407 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
408 ": Symbol `", strtab + refsym->st_name,
409 "' has different size in shared object, "
410 "consider re-linking\n", NULL);
412 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
416 case R_ARM_JUMP_SLOT:
417 #ifdef RTLD_BOOTSTRAP
418 /* Fix weak undefined references. */
419 if (sym != NULL && sym->st_value == 0)
427 #ifndef RTLD_BOOTSTRAP
428 /* This is defined in rtld.c, but nowhere in the static
429 libc.a; make the reference weak so static programs can
430 still link. This declaration cannot be done when
431 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
432 rtld.c contains the common defn for _dl_rtld_map, which
433 is incompatible with a weak decl in the same file. */
434 weak_extern (_dl_rtld_map);
435 if (map == &_dl_rtld_map)
436 /* Undo the relocation done here during bootstrapping.
437 Now we will relocate it anew, possibly using a
438 binding found in the user program or a loaded library
439 rather than the dynamic linker's built-in definitions
440 used while loading those libraries. */
441 value -= map->l_addr + refsym->st_value;
443 *reloc_addr += value;
450 addend = *reloc_addr & 0x00ffffff;
451 if (addend & 0x00800000) addend |= 0xff000000;
453 value = value - (unsigned int)reloc_addr + (addend << 2);
454 if (value & 0xfc000003)
455 _dl_signal_error (0, map->l_name,
456 "R_ARM_PC24 relocation out of range");
459 value = (*reloc_addr & 0xff000000) | (value & 0x00ffffff);
464 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 0);
471 elf_machine_lazy_rel (struct link_map *map,
472 Elf32_Addr l_addr, const Elf32_Rel *reloc)
474 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
475 /* Check for unexpected PLT reloc type. */
476 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_JUMP_SLOT)
477 *reloc_addr += l_addr;
479 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);