1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995,96,97,98,99,2000 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 #define CLEAR_CACHE(BEG,END) \
37 register unsigned long _beg __asm ("a1") = (unsigned long)(BEG); \
38 register unsigned long _end __asm ("a2") = (unsigned long)(END); \
39 register unsigned long _flg __asm ("a3") = 0; \
40 __asm __volatile ("swi 0x9f0002 @ sys_cacheflush" \
46 /* Return nonzero iff E_MACHINE is compatible with the running host. */
47 static inline int __attribute__ ((unused
))
48 elf_machine_matches_host (Elf32_Half e_machine
)
60 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
61 first element of the GOT. This must be inlined in a function which
63 static inline Elf32_Addr
__attribute__ ((unused
))
64 elf_machine_dynamic (void)
66 register Elf32_Addr
*got
asm ("r10");
71 /* Return the run-time load address of the shared object. */
72 static inline Elf32_Addr
__attribute__ ((unused
))
73 elf_machine_load_address (void)
75 extern void __dl_start
asm ("_dl_start");
76 Elf32_Addr got_addr
= (Elf32_Addr
) &__dl_start
;
77 Elf32_Addr pcrel_addr
;
78 asm ("adr %0, _dl_start" : "=r" (pcrel_addr
));
79 return pcrel_addr
- got_addr
;
83 /* Set up the loaded object described by L so its unrelocated PLT
84 entries will jump to the on-demand fixup code in dl-runtime.c. */
86 static inline int __attribute__ ((unused
))
87 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
90 extern void _dl_runtime_resolve (Elf32_Word
);
91 extern void _dl_runtime_profile (Elf32_Word
);
93 if (l
->l_info
[DT_JMPREL
] && lazy
)
95 /* patb: this is different than i386 */
96 /* The GOT entries for functions in the PLT have not yet been filled
97 in. Their initial contents will arrange when called to push an
98 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
99 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
100 got
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
101 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
103 /* The got[2] entry contains the address of a function which gets
104 called to get the address of a so far unresolved function and
105 jump to it. The profiling extension of the dynamic linker allows
106 to intercept the calls to collect information. In this case we
107 don't store the address in the GOT so that all future calls also
108 end in this function. */
111 got
[2] = (Elf32_Addr
) &_dl_runtime_profile
;
112 /* Say that we really want profiling and the timers are started. */
116 /* This function will get called to fix up the GOT entry indicated by
117 the offset on the stack, and then jump to the resolved address. */
118 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
123 /* This code is used in dl-runtime.c to call the `fixup' function
124 and then redirect to the address it returns. */
125 // macro for handling PIC situation....
127 #define CALL_ROUTINE(x) " ldr sl,0f
133 0: .word _GLOBAL_OFFSET_TABLE_ - 1b - 4
134 2: .word " #x "(GOTOFF)
137 #define CALL_ROUTINE(x) " bl " #x
141 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
143 .globl _dl_runtime_resolve
144 .type _dl_runtime_resolve, #function
148 @ stack[0] contains the return address from this call
149 @ ip contains &GOT[n+3] (pointer to function)
150 @ lr points to &GOT[2]
152 @ save almost everything; lr is already on the stack
153 stmdb sp!,{r0-r3,sl,fp}
155 @ prepare to call fixup()
156 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
161 @ get pointer to linker struct
165 " CALL_ROUTINE(fixup) "
171 ldmia sp!,{r0-r3,sl,fp,lr}
173 @ jump to the newly found address
176 .size _dl_runtime_resolve, .-_dl_runtime_resolve
178 .globl _dl_runtime_profile
179 .type _dl_runtime_profile, #function
182 @ save almost everything; lr is already on the stack
183 stmdb sp!,{r0-r3,sl,fp}
185 @ prepare to call fixup()
186 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
191 @ get pointer to linker struct
194 @ call profiling fixup routine
195 " CALL_ROUTINE(profile_fixup) "
201 ldmia sp!,{r0-r3,sl,fp,lr}
203 @ jump to the newly found address
206 .size _dl_runtime_resolve, .-_dl_runtime_resolve
210 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
212 .globl _dl_runtime_resolve
213 .globl _dl_runtime_profile
214 .type _dl_runtime_resolve, #function
215 .type _dl_runtime_profile, #function
220 @ stack[0] contains the return address from this call
221 @ ip contains &GOT[n+3] (pointer to function)
222 @ lr points to &GOT[2]
224 @ save almost everything; return add is already on the stack
225 stmdb sp!,{r0-r3,sl,fp}
227 @ prepare to call fixup()
228 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
233 @ get pointer to linker struct
236 @ call profiling fixup routine
237 " CALL_ROUTINE(fixup) "
243 ldmia sp!,{r0-r3,sl,fp,lr}
245 @ jump to the newly found address
248 .size _dl_runtime_profile, .-_dl_runtime_profile
253 /* Mask identifying addresses reserved for the user program,
254 where the dynamic linker should not map anything. */
255 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
257 /* Initial entry point code for the dynamic linker.
258 The C function `_dl_start' is the real entry point;
259 its return value is the user program's entry point. */
261 #define RTLD_START asm ("\
264 .globl _dl_start_user
266 @ at start time, all the args are on the stack
269 @ returns user entry point in r0
272 @ we are PIC code, so get global offset table
276 @ Store the highest stack address
280 @ See if we were run as a command with the executable file
281 @ name as an extra leading argument.
284 @ get the original arg count
286 @ subtract _dl_skip_args from it
288 @ adjust the stack pointer to skip them
289 add sp, sp, r4, lsl #2
290 @ get the argv address
292 @ store the new argc in the new stack location
295 add r3, r2, r1, lsl #2
298 @ now we call _dl_init
304 @ clear the startup flag
305 ldr r2, .L_STARTUP_FLAG
309 @ load the finalizer function
312 @ jump to the user_s entry point
315 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
317 .word _dl_skip_args(GOTOFF) \n\
319 .word _dl_starting_up(GOT)
323 .word __libc_stack_end(GOT)
325 .word _dl_loaded(GOT)
329 /* Nonzero iff TYPE should not be allowed to resolve to one of
330 the main executable's symbols, as for a COPY reloc. */
331 #define elf_machine_lookup_noexec_p(type) ((type) == R_ARM_COPY)
333 /* Nonzero iff TYPE describes relocation of a PLT entry, so
334 PLT entries should not be allowed to define the value. */
335 #define elf_machine_lookup_noplt_p(type) ((type) == R_ARM_JUMP_SLOT)
337 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
338 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
340 /* The ARM never uses Elf32_Rela relocations. */
341 #define ELF_MACHINE_NO_RELA 1
343 /* We define an initialization functions. This is called very early in
345 #define DL_PLATFORM_INIT dl_platform_init ()
347 extern const char *_dl_platform
;
349 static inline void __attribute__ ((unused
))
350 dl_platform_init (void)
352 if (_dl_platform
!= NULL
&& *_dl_platform
== '\0')
353 /* Avoid an empty string which would disturb us. */
357 static inline Elf32_Addr
358 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
359 const Elf32_Rel
*reloc
,
360 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
362 return *reloc_addr
= value
;
365 /* Return the final value of a plt relocation. */
366 static inline Elf32_Addr
367 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rel
*reloc
,
373 #endif /* !dl_machine_h */
377 extern char **_dl_argv
;
379 /* Deal with an out-of-range PC24 reloc. */
381 fix_bad_pc24 (Elf32_Addr
*const reloc_addr
, Elf32_Addr value
)
383 static void *fix_page
;
384 static unsigned int fix_offset
;
385 static size_t pagesize
;
386 Elf32_Word
*fix_address
;
391 pagesize
= getpagesize ();
392 fix_page
= mmap (NULL
, pagesize
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
393 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
395 assert (! "could not map page for fixup");
399 fix_address
= (Elf32_Word
*)(fix_page
+ fix_offset
);
400 fix_address
[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
401 fix_address
[1] = value
;
404 if (fix_offset
>= pagesize
)
407 return (Elf32_Addr
)fix_address
;
410 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
411 MAP is the object containing the reloc. */
414 elf_machine_rel (struct link_map
*map
, const Elf32_Rel
*reloc
,
415 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
416 Elf32_Addr
*const reloc_addr
)
418 if (ELF32_R_TYPE (reloc
->r_info
) == R_ARM_RELATIVE
)
420 #ifndef RTLD_BOOTSTRAP
421 if (map
!= &_dl_rtld_map
) /* Already done in rtld itself. */
423 *reloc_addr
+= map
->l_addr
;
425 else if (ELF32_R_TYPE (reloc
->r_info
) != R_ARM_NONE
)
427 const Elf32_Sym
*const refsym
= sym
;
428 Elf32_Addr value
= RESOLVE (&sym
, version
, ELF32_R_TYPE (reloc
->r_info
));
430 value
+= sym
->st_value
;
432 switch (ELF32_R_TYPE (reloc
->r_info
))
436 /* This can happen in trace mode if an object could not be
439 if (sym
->st_size
> refsym
->st_size
440 || (_dl_verbose
&& sym
->st_size
< refsym
->st_size
))
444 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
445 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>",
446 ": Symbol `", strtab
+ refsym
->st_name
,
447 "' has different size in shared object, "
448 "consider re-linking\n", NULL
);
450 memcpy (reloc_addr
, (void *) value
, MIN (sym
->st_size
,
454 case R_ARM_JUMP_SLOT
:
455 #ifdef RTLD_BOOTSTRAP
456 /* Fix weak undefined references. */
457 if (sym
!= NULL
&& sym
->st_value
== 0)
465 #ifndef RTLD_BOOTSTRAP
466 /* This is defined in rtld.c, but nowhere in the static
467 libc.a; make the reference weak so static programs can
468 still link. This declaration cannot be done when
469 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
470 rtld.c contains the common defn for _dl_rtld_map, which
471 is incompatible with a weak decl in the same file. */
472 weak_extern (_dl_rtld_map
);
473 if (map
== &_dl_rtld_map
)
474 /* Undo the relocation done here during bootstrapping.
475 Now we will relocate it anew, possibly using a
476 binding found in the user program or a loaded library
477 rather than the dynamic linker's built-in definitions
478 used while loading those libraries. */
479 value
-= map
->l_addr
+ refsym
->st_value
;
481 *reloc_addr
+= value
;
489 addend
= *reloc_addr
& 0x00ffffff;
490 if (addend
& 0x00800000) addend
|= 0xff000000;
492 newvalue
= value
- (Elf32_Addr
)reloc_addr
+ (addend
<< 2);
493 if (newvalue
& 0xfc000003)
494 newvalue
= fix_bad_pc24(reloc_addr
, value
)
495 - (Elf32_Addr
)reloc_addr
+ (addend
<< 2);
497 newvalue
= newvalue
>> 2;
498 value
= (*reloc_addr
& 0xff000000) | (newvalue
& 0x00ffffff);
503 _dl_reloc_bad_type (map
, ELF32_R_TYPE (reloc
->r_info
), 0);
510 elf_machine_lazy_rel (struct link_map
*map
,
511 Elf32_Addr l_addr
, const Elf32_Rel
*reloc
)
513 Elf32_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
514 /* Check for unexpected PLT reloc type. */
515 if (ELF32_R_TYPE (reloc
->r_info
) == R_ARM_JUMP_SLOT
)
516 *reloc_addr
+= l_addr
;
518 _dl_reloc_bad_type (map
, ELF32_R_TYPE (reloc
->r_info
), 1);