1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #define ELF_MACHINE_NAME "ARM"
26 #include <sys/param.h>
29 #define VALID_ELF_ABIVERSION(ver) (ver == 0)
30 #define VALID_ELF_OSABI(osabi) \
31 (osabi == ELFOSABI_SYSV || osabi == ELFOSABI_ARM)
32 #define VALID_ELF_HEADER(hdr,exp,size) \
33 memcmp (hdr,exp,size-2) == 0 \
34 && VALID_ELF_OSABI (hdr[EI_OSABI]) \
35 && VALID_ELF_ABIVERSION (hdr[EI_ABIVERSION])
37 #define CLEAR_CACHE(BEG,END) \
38 INTERNAL_SYSCALL_ARM (cacheflush, , 3, (BEG), (END), 0)
40 /* Return nonzero iff ELF header is compatible with the running host. */
41 static inline int __attribute__ ((unused
))
42 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
44 return ehdr
->e_machine
== EM_ARM
;
48 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
49 first element of the GOT. This must be inlined in a function which
51 static inline Elf32_Addr
__attribute__ ((unused
))
52 elf_machine_dynamic (void)
54 register Elf32_Addr
*got
asm ("r10");
59 /* Return the run-time load address of the shared object. */
60 static inline Elf32_Addr
__attribute__ ((unused
))
61 elf_machine_load_address (void)
63 extern void __dl_start
asm ("_dl_start");
64 Elf32_Addr got_addr
= (Elf32_Addr
) &__dl_start
;
65 Elf32_Addr pcrel_addr
;
66 asm ("adr %0, _dl_start" : "=r" (pcrel_addr
));
67 return pcrel_addr
- got_addr
;
71 /* Set up the loaded object described by L so its unrelocated PLT
72 entries will jump to the on-demand fixup code in dl-runtime.c. */
74 static inline int __attribute__ ((unused
))
75 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
78 extern void _dl_runtime_resolve (Elf32_Word
);
79 extern void _dl_runtime_profile (Elf32_Word
);
81 if (l
->l_info
[DT_JMPREL
] && lazy
)
83 /* patb: this is different than i386 */
84 /* The GOT entries for functions in the PLT have not yet been filled
85 in. Their initial contents will arrange when called to push an
86 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
87 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
88 got
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
89 /* If a library is prelinked but we have to relocate anyway,
90 we have to be able to undo the prelinking of .got.plt.
91 The prelinker saved us here address of .plt. */
93 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
94 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
96 /* The got[2] entry contains the address of a function which gets
97 called to get the address of a so far unresolved function and
98 jump to it. The profiling extension of the dynamic linker allows
99 to intercept the calls to collect information. In this case we
100 don't store the address in the GOT so that all future calls also
101 end in this function. */
104 got
[2] = (Elf32_Addr
) &_dl_runtime_profile
;
106 if (GLRO(dl_profile
) != NULL
107 && _dl_name_match_p (GLRO(dl_profile
), l
))
108 /* Say that we really want profiling and the timers are
110 GL(dl_profile_map
) = l
;
113 /* This function will get called to fix up the GOT entry indicated by
114 the offset on the stack, and then jump to the resolved address. */
115 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
120 #if defined(__USE_BX__)
121 #define BX(x) "bx\t" #x
123 #define BX(x) "mov\tpc, " #x
126 /* Mask identifying addresses reserved for the user program,
127 where the dynamic linker should not map anything. */
128 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
130 /* Initial entry point code for the dynamic linker.
131 The C function `_dl_start' is the real entry point;
132 its return value is the user program's entry point. */
134 #define RTLD_START asm ("\
137 .globl _dl_start_user\n\
139 @ we are PIC code, so get global offset table\n\
140 ldr sl, .L_GET_GOT\n\
141 @ See if we were run as a command with the executable file\n\
142 @ name as an extra leading argument.\n\
143 ldr r4, .L_SKIP_ARGS\n\
144 @ at start time, all the args are on the stack\n\
147 @ returns user entry point in r0\n\
152 @ save the entry point in another register\n\
154 @ get the original arg count\n\
156 @ get the argv address\n\
158 @ Fix up the stack if necessary.\n\
160 bne .L_fixup_stack\n\
163 add r3, r2, r1, lsl #2\n\
165 @ now we call _dl_init\n\
169 bl _dl_init_internal(PLT)\n\
170 @ load the finalizer function\n\
171 ldr r0, .L_FINI_PROC\n\
173 @ jump to the user_s entry point\n\
176 @ iWMMXt and EABI targets require the stack to be eight byte\n\
177 @ aligned - shuffle arguments etc.\n\
179 @ subtract _dl_skip_args from original arg count\n\
181 @ store the new argc in the new stack location\n\
183 @ find the first unskipped argument\n\
185 add r4, r2, r4, lsl #2\n\
186 @ shuffle argv down\n\
187 1: ldr r5, [r4], #4\n\
191 @ shuffle envp down\n\
192 1: ldr r5, [r4], #4\n\
196 @ shuffle auxv down\n\
197 1: ldmia r4!, {r0, r5}\n\
198 stmia r3!, {r0, r5}\n\
207 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4\n\
209 .word _dl_skip_args(GOTOFF)\n\
211 .word _dl_fini(GOTOFF)\n\
213 .word _dl_argv(GOTOFF)\n\
215 .word _rtld_local(GOTOFF)\n\
219 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
220 TLS variable, so undefined references should not be allowed to
222 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
223 of the main executable's symbols, as for a COPY reloc. */
224 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
225 # define elf_machine_type_class(type) \
226 ((((type) == R_ARM_JUMP_SLOT || (type) == R_ARM_TLS_DTPMOD32 \
227 || (type) == R_ARM_TLS_DTPOFF32 || (type) == R_ARM_TLS_TPOFF32) \
228 * ELF_RTYPE_CLASS_PLT) \
229 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
231 #define elf_machine_type_class(type) \
232 ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
233 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
236 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
237 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
239 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
240 Prelinked libraries may use Elf32_Rela though. */
241 #define ELF_MACHINE_PLT_REL 1
243 /* We define an initialization functions. This is called very early in
245 #define DL_PLATFORM_INIT dl_platform_init ()
247 static inline void __attribute__ ((unused
))
248 dl_platform_init (void)
250 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
251 /* Avoid an empty string which would disturb us. */
252 GLRO(dl_platform
) = NULL
;
255 static inline Elf32_Addr
256 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
257 const Elf32_Rel
*reloc
,
258 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
260 return *reloc_addr
= value
;
263 /* Return the final value of a plt relocation. */
264 static inline Elf32_Addr
265 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rel
*reloc
,
271 #endif /* !dl_machine_h */
274 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
275 Prelinked libraries may use Elf32_Rela though. */
276 #define ELF_MACHINE_NO_RELA defined RTLD_BOOTSTRAP
278 /* Names of the architecture-specific auditing callback functions. */
279 #define ARCH_LA_PLTENTER arm_gnu_pltenter
280 #define ARCH_LA_PLTEXIT arm_gnu_pltexit
284 /* Deal with an out-of-range PC24 reloc. */
286 fix_bad_pc24 (Elf32_Addr
*const reloc_addr
, Elf32_Addr value
)
288 static void *fix_page
;
289 static unsigned int fix_offset
;
290 static size_t pagesize
;
291 Elf32_Word
*fix_address
;
296 pagesize
= getpagesize ();
297 fix_page
= mmap (NULL
, pagesize
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
298 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
300 assert (! "could not map page for fixup");
304 fix_address
= (Elf32_Word
*)(fix_page
+ fix_offset
);
305 fix_address
[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
306 fix_address
[1] = value
;
309 if (fix_offset
>= pagesize
)
312 return (Elf32_Addr
)fix_address
;
315 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
316 MAP is the object containing the reloc. */
319 __attribute__ ((always_inline
))
320 elf_machine_rel (struct link_map
*map
, const Elf32_Rel
*reloc
,
321 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
322 void *const reloc_addr_arg
)
324 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
325 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
327 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
328 if (__builtin_expect (r_type
== R_ARM_RELATIVE
, 0))
330 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
331 /* This is defined in rtld.c, but nowhere in the static libc.a;
332 make the reference weak so static programs can still link.
333 This declaration cannot be done when compiling rtld.c
334 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
335 common defn for _dl_rtld_map, which is incompatible with a
336 weak decl in the same file. */
338 weak_extern (_dl_rtld_map
);
340 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
342 *reloc_addr
+= map
->l_addr
;
344 # ifndef RTLD_BOOTSTRAP
345 else if (__builtin_expect (r_type
== R_ARM_NONE
, 0))
351 const Elf32_Sym
*const refsym
= sym
;
352 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
353 Elf32_Addr value
= sym_map
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
359 /* This can happen in trace mode if an object could not be
362 if (sym
->st_size
> refsym
->st_size
363 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
367 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
369 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
370 rtld_progname
?: "<program name unknown>",
371 strtab
+ refsym
->st_name
);
373 memcpy (reloc_addr_arg
, (void *) value
,
374 MIN (sym
->st_size
, refsym
->st_size
));
377 case R_ARM_JUMP_SLOT
:
378 # ifdef RTLD_BOOTSTRAP
379 /* Fix weak undefined references. */
380 if (sym
!= NULL
&& sym
->st_value
== 0)
388 # ifndef RTLD_BOOTSTRAP
389 /* This is defined in rtld.c, but nowhere in the static
390 libc.a; make the reference weak so static programs can
391 still link. This declaration cannot be done when
392 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
393 rtld.c contains the common defn for _dl_rtld_map, which
394 is incompatible with a weak decl in the same file. */
396 weak_extern (_dl_rtld_map
);
398 if (map
== &GL(dl_rtld_map
))
399 /* Undo the relocation done here during bootstrapping.
400 Now we will relocate it anew, possibly using a
401 binding found in the user program or a loaded library
402 rather than the dynamic linker's built-in definitions
403 used while loading those libraries. */
404 value
-= map
->l_addr
+ refsym
->st_value
;
406 *reloc_addr
+= value
;
412 Elf32_Addr newvalue
, topbits
;
414 addend
= *reloc_addr
& 0x00ffffff;
415 if (addend
& 0x00800000) addend
|= 0xff000000;
417 newvalue
= value
- (Elf32_Addr
)reloc_addr
+ (addend
<< 2);
418 topbits
= newvalue
& 0xfe000000;
419 if (topbits
!= 0xfe000000 && topbits
!= 0x00000000)
421 newvalue
= fix_bad_pc24(reloc_addr
, value
)
422 - (Elf32_Addr
)reloc_addr
+ (addend
<< 2);
423 topbits
= newvalue
& 0xfe000000;
424 if (topbits
!= 0xfe000000 && topbits
!= 0x00000000)
426 _dl_signal_error (0, map
->l_name
, NULL
,
427 "R_ARM_PC24 relocation out of range");
431 value
= (*reloc_addr
& 0xff000000) | (newvalue
& 0x00ffffff);
435 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
436 case R_ARM_TLS_DTPMOD32
:
437 /* Get the information from the link map returned by the
440 *reloc_addr
= sym_map
->l_tls_modid
;
443 case R_ARM_TLS_DTPOFF32
:
444 *reloc_addr
+= sym
->st_value
;
447 case R_ARM_TLS_TPOFF32
:
448 CHECK_STATIC_TLS (map
, sym_map
);
449 *reloc_addr
+= sym
->st_value
+ sym_map
->l_tls_offset
;
453 _dl_reloc_bad_type (map
, r_type
, 0);
459 # ifndef RTLD_BOOTSTRAP
461 __attribute__ ((always_inline
))
462 elf_machine_rela (struct link_map
*map
, const Elf32_Rela
*reloc
,
463 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
464 void *const reloc_addr_arg
)
466 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
467 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
469 if (__builtin_expect (r_type
== R_ARM_RELATIVE
, 0))
470 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
471 else if (__builtin_expect (r_type
== R_ARM_NONE
, 0))
475 # ifndef RESOLVE_CONFLICT_FIND_MAP
476 const Elf32_Sym
*const refsym
= sym
;
478 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
479 Elf32_Addr value
= sym_map
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
483 # ifndef RESOLVE_CONFLICT_FIND_MAP
484 /* Not needed for dl-conflict.c. */
487 /* This can happen in trace mode if an object could not be
490 if (sym
->st_size
> refsym
->st_size
491 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
495 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
497 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
498 rtld_progname
?: "<program name unknown>",
499 strtab
+ refsym
->st_name
);
501 memcpy (reloc_addr_arg
, (void *) value
,
502 MIN (sym
->st_size
, refsym
->st_size
));
504 # endif /* !RESOLVE_CONFLICT_FIND_MAP */
506 case R_ARM_JUMP_SLOT
:
508 *reloc_addr
= value
+ reloc
->r_addend
;
512 Elf32_Addr newvalue
, topbits
;
514 newvalue
= value
+ reloc
->r_addend
- (Elf32_Addr
)reloc_addr
;
515 topbits
= newvalue
& 0xfe000000;
516 if (topbits
!= 0xfe000000 && topbits
!= 0x00000000)
518 newvalue
= fix_bad_pc24(reloc_addr
, value
)
519 - (Elf32_Addr
)reloc_addr
+ (reloc
->r_addend
<< 2);
520 topbits
= newvalue
& 0xfe000000;
521 if (topbits
!= 0xfe000000 && topbits
!= 0x00000000)
523 _dl_signal_error (0, map
->l_name
, NULL
,
524 "R_ARM_PC24 relocation out of range");
528 value
= (*reloc_addr
& 0xff000000) | (newvalue
& 0x00ffffff);
532 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
533 case R_ARM_TLS_DTPMOD32
:
534 /* Get the information from the link map returned by the
537 *reloc_addr
= sym_map
->l_tls_modid
;
540 case R_ARM_TLS_DTPOFF32
:
541 *reloc_addr
= sym
->st_value
+ reloc
->r_addend
;
544 case R_ARM_TLS_TPOFF32
:
545 CHECK_STATIC_TLS (map
, sym_map
);
546 *reloc_addr
= (sym
->st_value
+ sym_map
->l_tls_offset
551 _dl_reloc_bad_type (map
, r_type
, 0);
559 __attribute__ ((always_inline
))
560 elf_machine_rel_relative (Elf32_Addr l_addr
, const Elf32_Rel
*reloc
,
561 void *const reloc_addr_arg
)
563 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
564 *reloc_addr
+= l_addr
;
567 # ifndef RTLD_BOOTSTRAP
569 __attribute__ ((always_inline
))
570 elf_machine_rela_relative (Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
571 void *const reloc_addr_arg
)
573 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
574 *reloc_addr
= l_addr
+ reloc
->r_addend
;
579 __attribute__ ((always_inline
))
580 elf_machine_lazy_rel (struct link_map
*map
,
581 Elf32_Addr l_addr
, const Elf32_Rel
*reloc
)
583 Elf32_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
584 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
585 /* Check for unexpected PLT reloc type. */
586 if (__builtin_expect (r_type
== R_ARM_JUMP_SLOT
, 1))
588 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
589 *reloc_addr
+= l_addr
;
591 *reloc_addr
= map
->l_mach
.plt
;
594 _dl_reloc_bad_type (map
, r_type
, 1);
597 #endif /* RESOLVE_MAP */