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>
29 /* Return nonzero iff E_MACHINE is compatible with the running host. */
30 static inline int __attribute__ ((unused))
31 elf_machine_matches_host (Elf32_Half e_machine)
43 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
44 first element of the GOT. This must be inlined in a function which
46 static inline Elf32_Addr __attribute__ ((unused))
47 elf_machine_dynamic (void)
49 register Elf32_Addr *got asm ("r10");
54 /* Return the run-time load address of the shared object. */
55 static inline Elf32_Addr __attribute__ ((unused))
56 elf_machine_load_address (void)
58 extern void __dl_start asm ("_dl_start");
59 Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
60 Elf32_Addr pcrel_addr;
61 asm ("adr %0, _dl_start" : "=r" (pcrel_addr));
62 return pcrel_addr - got_addr;
66 /* Set up the loaded object described by L so its unrelocated PLT
67 entries will jump to the on-demand fixup code in dl-runtime.c. */
69 static inline int __attribute__ ((unused))
70 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
73 extern void _dl_runtime_resolve (Elf32_Word);
74 extern void _dl_runtime_profile (Elf32_Word);
76 if (l->l_info[DT_JMPREL] && lazy)
78 /* patb: this is different than i386 */
79 /* The GOT entries for functions in the PLT have not yet been filled
80 in. Their initial contents will arrange when called to push an
81 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
82 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
83 got = (Elf32_Addr *) l->l_info[DT_PLTGOT]->d_un.d_ptr;
84 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
86 /* The got[2] entry contains the address of a function which gets
87 called to get the address of a so far unresolved function and
88 jump to it. The profiling extension of the dynamic linker allows
89 to intercept the calls to collect information. In this case we
90 don't store the address in the GOT so that all future calls also
91 end in this function. */
94 got[2] = (Elf32_Addr) &_dl_runtime_profile;
95 /* Say that we really want profiling and the timers are started. */
99 /* This function will get called to fix up the GOT entry indicated by
100 the offset on the stack, and then jump to the resolved address. */
101 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
106 /* This code is used in dl-runtime.c to call the `fixup' function
107 and then redirect to the address it returns. */
108 // macro for handling PIC situation....
110 #define CALL_ROUTINE(x) " ldr sl,0f
116 0: .word _GLOBAL_OFFSET_TABLE_ - 1b - 4
117 2: .word " #x "(GOTOFF)
120 #define CALL_ROUTINE(x) " bl " #x
124 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
126 .globl _dl_runtime_resolve
127 .type _dl_runtime_resolve, #function
131 @ stack[0] contains the return address from this call
132 @ ip contains &GOT[n+3] (pointer to function)
133 @ lr points to &GOT[2]
135 @ save almost everything; lr is already on the stack
136 stmdb sp!,{r0-r3,sl,fp}
138 @ prepare to call fixup()
139 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
144 @ get pointer to linker struct
148 " CALL_ROUTINE(fixup) "
154 ldmia sp!,{r0-r3,sl,fp,lr}
156 @ jump to the newly found address
159 .size _dl_runtime_resolve, .-_dl_runtime_resolve
161 .globl _dl_runtime_profile
162 .type _dl_runtime_profile, #function
165 @ save almost everything; lr is already on the stack
166 stmdb sp!,{r0-r3,sl,fp}
168 @ prepare to call fixup()
169 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
174 @ get pointer to linker struct
177 @ call profiling fixup routine
178 " CALL_ROUTINE(profile_fixup) "
184 ldmia sp!,{r0-r3,sl,fp,lr}
186 @ jump to the newly found address
189 .size _dl_runtime_resolve, .-_dl_runtime_resolve
193 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
195 .globl _dl_runtime_resolve
196 .globl _dl_runtime_profile
197 .type _dl_runtime_resolve, #function
198 .type _dl_runtime_profile, #function
203 @ stack[0] contains the return address from this call
204 @ ip contains &GOT[n+3] (pointer to function)
205 @ lr points to &GOT[2]
207 @ save almost everything; return add is already on the stack
208 stmdb sp!,{r0-r3,sl,fp}
210 @ prepare to call fixup()
211 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
216 @ get pointer to linker struct
219 @ call profiling fixup routine
220 " CALL_ROUTINE(fixup) "
226 ldmia sp!,{r0-r3,sl,fp,lr}
228 @ jump to the newly found address
231 .size _dl_runtime_profile, .-_dl_runtime_profile
236 /* Mask identifying addresses reserved for the user program,
237 where the dynamic linker should not map anything. */
238 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
240 /* Initial entry point code for the dynamic linker.
241 The C function `_dl_start' is the real entry point;
242 its return value is the user program's entry point. */
244 #define RTLD_START asm ("\
247 .globl _dl_start_user
249 @ at start time, all the args are on the stack
252 @ returns user entry point in r0
255 @ we are PIC code, so get global offset table
259 @ Store the highest stack address
263 @ See if we were run as a command with the executable file
264 @ name as an extra leading argument.
267 @ get the original arg count
269 @ subtract _dl_skip_args from it
271 @ adjust the stack pointer to skip them
272 add sp, sp, r1, lsl #2
273 @ store the new argc in the new stack location
276 @ now we enter a _dl_init_next loop
277 ldr r4, .L_MAIN_SEARCHLIST
280 @ call _dl_init_next to get the address of an initalizer
282 bl _dl_init_next(PLT)
285 @ call the shared-object initializer
286 @ during this call, the stack may get moved around
289 @ go back and look for another initializer
291 1: @ clear the startup flag
292 ldr r2, .L_STARTUP_FLAG
294 @ we know r0==0 at this point
296 @ load the finalizer function
299 @ jump to the user_s entry point
302 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
304 .word _dl_skip_args(GOTOFF) \n\
306 .word _dl_starting_up(GOT)
310 .word __libc_stack_end(GOT)
312 .word _dl_main_searchlist(GOT)
316 /* Nonzero iff TYPE should not be allowed to resolve to one of
317 the main executable's symbols, as for a COPY reloc. */
318 #define elf_machine_lookup_noexec_p(type) ((type) == R_ARM_COPY)
320 /* Nonzero iff TYPE describes relocation of a PLT entry, so
321 PLT entries should not be allowed to define the value. */
322 #define elf_machine_lookup_noplt_p(type) ((type) == R_ARM_JUMP_SLOT)
324 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
325 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
327 /* The ARM never uses Elf32_Rela relocations. */
328 #define ELF_MACHINE_NO_RELA 1
330 /* We define an initialization functions. This is called very early in
332 #define DL_PLATFORM_INIT dl_platform_init ()
334 extern const char *_dl_platform;
336 static inline void __attribute__ ((unused))
337 dl_platform_init (void)
339 if (_dl_platform == NULL)
341 This is where processors could be distinguished arm2, arm6, sa110, etc */
342 _dl_platform = "ARM";
346 elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc,
347 Elf32_Addr *reloc_addr, Elf32_Addr value)
352 /* Return the final value of a plt relocation. */
353 static inline Elf32_Addr
354 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
360 #endif /* !dl_machine_h */
364 extern char **_dl_argv;
366 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
367 MAP is the object containing the reloc. */
370 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
371 const Elf32_Sym *sym, const struct r_found_version *version,
372 Elf32_Addr *const reloc_addr)
374 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_RELATIVE)
376 #ifndef RTLD_BOOTSTRAP
377 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
379 *reloc_addr += map->l_addr;
381 else if (ELF32_R_TYPE (reloc->r_info) != R_ARM_NONE)
383 const Elf32_Sym *const refsym = sym;
384 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
386 value += sym->st_value;
388 switch (ELF32_R_TYPE (reloc->r_info))
392 /* This can happen in trace mode if an object could not be
395 if (sym->st_size > refsym->st_size
396 || (_dl_verbose && sym->st_size < refsym->st_size))
400 strtab = (const void *) map->l_info[DT_STRTAB]->d_un.d_ptr;
401 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
402 ": Symbol `", strtab + refsym->st_name,
403 "' has different size in shared object, "
404 "consider re-linking\n", NULL);
406 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
410 case R_ARM_JUMP_SLOT:
411 #ifdef RTLD_BOOTSTRAP
412 /* Fix weak undefined references. */
413 if (sym != NULL && sym->st_value == 0)
421 #ifndef RTLD_BOOTSTRAP
422 /* This is defined in rtld.c, but nowhere in the static
423 libc.a; make the reference weak so static programs can
424 still link. This declaration cannot be done when
425 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
426 rtld.c contains the common defn for _dl_rtld_map, which
427 is incompatible with a weak decl in the same file. */
428 weak_extern (_dl_rtld_map);
429 if (map == &_dl_rtld_map)
430 /* Undo the relocation done here during bootstrapping.
431 Now we will relocate it anew, possibly using a
432 binding found in the user program or a loaded library
433 rather than the dynamic linker's built-in definitions
434 used while loading those libraries. */
435 value -= map->l_addr + refsym->st_value;
437 *reloc_addr += value;
441 assert (! "unexpected dynamic reloc type");
448 elf_machine_lazy_rel (Elf32_Addr l_addr, const Elf32_Rel *reloc)
450 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
451 /* Check for unexpected PLT reloc type. */
452 assert (ELF32_R_TYPE (reloc->r_info) == R_ARM_JUMP_SLOT);
453 *reloc_addr += l_addr;