1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995-2016 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
22 #define ELF_MACHINE_NAME "ARM"
24 #include <sys/param.h>
26 #include <dl-tlsdesc.h>
30 # error CLEAR_CACHE definition required to handle TEXTREL
33 /* Return nonzero iff ELF header is compatible with the running host. */
34 static inline int __attribute__ ((unused
))
35 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
37 return ehdr
->e_machine
== EM_ARM
;
41 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
42 first element of the GOT. */
43 static inline Elf32_Addr
__attribute__ ((unused
))
44 elf_machine_dynamic (void)
46 /* Declaring this hidden ensures that a PC-relative reference is used. */
47 extern const Elf32_Addr _GLOBAL_OFFSET_TABLE_
[] attribute_hidden
;
48 return _GLOBAL_OFFSET_TABLE_
[0];
52 /* Return the run-time load address of the shared object. */
53 static inline Elf32_Addr
__attribute__ ((unused
))
54 elf_machine_load_address (void)
56 extern Elf32_Addr internal_function
__dl_start (void *) asm ("_dl_start");
57 Elf32_Addr got_addr
= (Elf32_Addr
) &__dl_start
;
58 Elf32_Addr pcrel_addr
;
60 /* Clear the low bit of the funciton address. */
61 got_addr
&= ~(Elf32_Addr
) 1;
63 asm ("adr %0, _dl_start" : "=r" (pcrel_addr
));
64 return pcrel_addr
- got_addr
;
68 /* Set up the loaded object described by L so its unrelocated PLT
69 entries will jump to the on-demand fixup code in dl-runtime.c. */
71 static inline int __attribute__ ((unused
))
72 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
75 extern void _dl_runtime_resolve (Elf32_Word
);
76 extern void _dl_runtime_profile (Elf32_Word
);
78 if (l
->l_info
[DT_JMPREL
] && lazy
)
80 /* patb: this is different than i386 */
81 /* The GOT entries for functions in the PLT have not yet been filled
82 in. Their initial contents will arrange when called to push an
83 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
84 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
85 got
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
86 /* If a library is prelinked but we have to relocate anyway,
87 we have to be able to undo the prelinking of .got.plt.
88 The prelinker saved us here address of .plt. */
90 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
91 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
93 /* The got[2] entry contains the address of a function which gets
94 called to get the address of a so far unresolved function and
95 jump to it. The profiling extension of the dynamic linker allows
96 to intercept the calls to collect information. In this case we
97 don't store the address in the GOT so that all future calls also
98 end in this function. */
101 got
[2] = (Elf32_Addr
) &_dl_runtime_profile
;
103 if (GLRO(dl_profile
) != NULL
104 && _dl_name_match_p (GLRO(dl_profile
), l
))
105 /* Say that we really want profiling and the timers are
107 GL(dl_profile_map
) = l
;
110 /* This function will get called to fix up the GOT entry indicated by
111 the offset on the stack, and then jump to the resolved address. */
112 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
115 if (l
->l_info
[ADDRIDX (DT_TLSDESC_GOT
)] && lazy
)
116 *(Elf32_Addr
*)(D_PTR (l
, l_info
[ADDRIDX (DT_TLSDESC_GOT
)]) + l
->l_addr
)
117 = (Elf32_Addr
) &_dl_tlsdesc_lazy_resolver
;
122 #if defined(ARCH_HAS_BX)
123 #define BX(x) "bx\t" #x
125 #define BX(x) "mov\tpc, " #x
128 /* Mask identifying addresses reserved for the user program,
129 where the dynamic linker should not map anything. */
130 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
132 /* Initial entry point code for the dynamic linker.
133 The C function `_dl_start' is the real entry point;
134 its return value is the user program's entry point. */
136 #define RTLD_START asm ("\
139 .type _start, %function\n\
140 .globl _dl_start_user\n\
141 .type _dl_start_user, %function\n\
143 @ we are PIC code, so get global offset table\n\
144 ldr sl, .L_GET_GOT\n\
145 @ See if we were run as a command with the executable file\n\
146 @ name as an extra leading argument.\n\
147 ldr r4, .L_SKIP_ARGS\n\
148 @ at start time, all the args are on the stack\n\
151 @ returns user entry point in r0\n\
153 adr r6, .L_GET_GOT\n\
156 @ save the entry point in another register\n\
158 @ get the original arg count\n\
160 @ get the argv address\n\
162 @ Fix up the stack if necessary.\n\
164 bne .L_fixup_stack\n\
167 add r3, r2, r1, lsl #2\n\
169 @ now we call _dl_init\n\
174 @ load the finalizer function\n\
175 ldr r0, .L_FINI_PROC\n\
177 @ jump to the user_s entry point\n\
180 @ iWMMXt and EABI targets require the stack to be eight byte\n\
181 @ aligned - shuffle arguments etc.\n\
183 @ subtract _dl_skip_args from original arg count\n\
185 @ store the new argc in the new stack location\n\
187 @ find the first unskipped argument\n\
189 add r4, r2, r4, lsl #2\n\
190 @ shuffle argv down\n\
191 1: ldr r5, [r4], #4\n\
195 @ shuffle envp down\n\
196 1: ldr r5, [r4], #4\n\
200 @ shuffle auxv down\n\
201 1: ldmia r4!, {r0, r5}\n\
202 stmia r3!, {r0, r5}\n\
211 .word _GLOBAL_OFFSET_TABLE_ - .L_GET_GOT\n\
213 .word _dl_skip_args(GOTOFF)\n\
215 .word _dl_fini(GOTOFF)\n\
217 .word _dl_argv(GOTOFF)\n\
219 .word _rtld_local(GOTOFF)\n\
223 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
224 TLS variable, so undefined references should not be allowed to
226 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
227 of the main executable's symbols, as for a COPY reloc.
228 ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA iff TYPE describes relocation against
229 protected data whose address may be external due to copy relocation. */
230 #ifndef RTLD_BOOTSTRAP
231 # define elf_machine_type_class(type) \
232 ((((type) == R_ARM_JUMP_SLOT || (type) == R_ARM_TLS_DTPMOD32 \
233 || (type) == R_ARM_TLS_DTPOFF32 || (type) == R_ARM_TLS_TPOFF32 \
234 || (type) == R_ARM_TLS_DESC) \
235 * ELF_RTYPE_CLASS_PLT) \
236 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY) \
237 | (((type) == R_ARM_GLOB_DAT) * ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA))
239 #define elf_machine_type_class(type) \
240 ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
241 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY) \
242 | (((type) == R_ARM_GLOB_DAT) * ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA))
245 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
246 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
248 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
249 Prelinked libraries may use Elf32_Rela though. */
250 #define ELF_MACHINE_PLT_REL 1
252 /* We define an initialization functions. This is called very early in
254 #define DL_PLATFORM_INIT dl_platform_init ()
256 static inline void __attribute__ ((unused
))
257 dl_platform_init (void)
259 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
260 /* Avoid an empty string which would disturb us. */
261 GLRO(dl_platform
) = NULL
;
264 static inline Elf32_Addr
265 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
266 const Elf32_Rel
*reloc
,
267 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
269 return *reloc_addr
= value
;
272 /* Return the final value of a plt relocation. */
273 static inline Elf32_Addr
274 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rel
*reloc
,
280 #endif /* !dl_machine_h */
283 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
284 Prelinked libraries may use Elf32_Rela though. */
285 #define ELF_MACHINE_NO_RELA defined RTLD_BOOTSTRAP
286 #define ELF_MACHINE_NO_REL 0
288 /* Names of the architecture-specific auditing callback functions. */
289 #define ARCH_LA_PLTENTER arm_gnu_pltenter
290 #define ARCH_LA_PLTEXIT arm_gnu_pltexit
293 /* Handle a PC24 reloc, including the out-of-range case. */
295 relocate_pc24 (struct link_map
*map
, Elf32_Addr value
,
296 Elf32_Addr
*const reloc_addr
, Elf32_Sword addend
)
298 Elf32_Addr new_value
;
300 /* Set NEW_VALUE based on V, and return true iff it overflows 24 bits. */
301 inline bool set_new_value (Elf32_Addr v
)
303 new_value
= v
+ addend
- (Elf32_Addr
) reloc_addr
;
304 Elf32_Addr topbits
= new_value
& 0xfe000000;
305 return topbits
!= 0xfe000000 && topbits
!= 0x00000000;
308 if (set_new_value (value
))
310 /* The PC-relative address doesn't fit in 24 bits! */
312 static void *fix_page
;
313 static size_t fix_offset
;
314 if (fix_page
== NULL
)
316 void *new_page
= __mmap (NULL
, GLRO(dl_pagesize
),
317 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
318 MAP_PRIVATE
| MAP_ANON
, -1, 0);
319 if (new_page
== MAP_FAILED
)
320 _dl_signal_error (0, map
->l_name
, NULL
,
321 "could not map page for fixup");
323 assert (fix_offset
== 0);
326 Elf32_Word
*fix_address
= fix_page
+ fix_offset
;
327 fix_address
[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
328 fix_address
[1] = value
;
330 fix_offset
+= sizeof fix_address
[0] * 2;
331 if (fix_offset
>= GLRO(dl_pagesize
))
337 if (set_new_value ((Elf32_Addr
) fix_address
))
338 _dl_signal_error (0, map
->l_name
, NULL
,
339 "R_ARM_PC24 relocation out of range");
342 *reloc_addr
= (*reloc_addr
& 0xff000000) | ((new_value
>> 2) & 0x00ffffff);
345 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
346 MAP is the object containing the reloc. */
349 __attribute__ ((always_inline
))
350 elf_machine_rel (struct link_map
*map
, const Elf32_Rel
*reloc
,
351 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
352 void *const reloc_addr_arg
, int skip_ifunc
)
354 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
355 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
357 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
358 if (__builtin_expect (r_type
== R_ARM_RELATIVE
, 0))
360 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
361 /* This is defined in rtld.c, but nowhere in the static libc.a;
362 make the reference weak so static programs can still link.
363 This declaration cannot be done when compiling rtld.c
364 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
365 common defn for _dl_rtld_map, which is incompatible with a
366 weak decl in the same file. */
368 weak_extern (_dl_rtld_map
);
370 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
372 *reloc_addr
+= map
->l_addr
;
374 # ifndef RTLD_BOOTSTRAP
375 else if (__builtin_expect (r_type
== R_ARM_NONE
, 0))
381 const Elf32_Sym
*const refsym
= sym
;
382 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
383 Elf32_Addr value
= sym_map
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
386 && __builtin_expect (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
, 0)
387 && __builtin_expect (sym
->st_shndx
!= SHN_UNDEF
, 1)
388 && __builtin_expect (!skip_ifunc
, 1))
389 value
= elf_ifunc_invoke (value
);
395 /* This can happen in trace mode if an object could not be
398 if (sym
->st_size
> refsym
->st_size
399 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
403 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
405 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
406 RTLD_PROGNAME
, strtab
+ refsym
->st_name
);
408 memcpy (reloc_addr_arg
, (void *) value
,
409 MIN (sym
->st_size
, refsym
->st_size
));
412 case R_ARM_JUMP_SLOT
:
413 # ifdef RTLD_BOOTSTRAP
414 /* Fix weak undefined references. */
415 if (sym
!= NULL
&& sym
->st_value
== 0)
426 } __attribute__ ((packed
, may_alias
));
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. */
435 weak_extern (_dl_rtld_map
);
437 if (map
== &GL(dl_rtld_map
))
438 /* Undo the relocation done here during bootstrapping.
439 Now we will relocate it anew, possibly using a
440 binding found in the user program or a loaded library
441 rather than the dynamic linker's built-in definitions
442 used while loading those libraries. */
443 value
-= map
->l_addr
+ refsym
->st_value
;
445 /* Support relocations on mis-aligned offsets. */
446 ((struct unaligned
*) reloc_addr
)->x
+= value
;
451 struct tlsdesc
volatile *td
=
452 (struct tlsdesc
volatile *)reloc_addr
;
454 # ifndef RTLD_BOOTSTRAP
456 td
->entry
= _dl_tlsdesc_undefweak
;
460 if (ELF32_R_SYM (reloc
->r_info
) == STN_UNDEF
)
461 value
= td
->argument
.value
;
463 value
= sym
->st_value
;
465 # ifndef RTLD_BOOTSTRAP
467 CHECK_STATIC_TLS (map
, sym_map
);
469 if (!TRY_STATIC_TLS (map
, sym_map
))
472 = _dl_make_tlsdesc_dynamic (sym_map
, value
);
473 td
->entry
= _dl_tlsdesc_dynamic
;
479 td
->argument
.value
= value
+ sym_map
->l_tls_offset
;
480 td
->entry
= _dl_tlsdesc_return
;
486 relocate_pc24 (map
, value
, reloc_addr
,
487 /* Sign-extend the 24-bit addend in the
488 instruction (which counts instructions), and
489 then shift it up two so as to count bytes. */
490 (((Elf32_Sword
) *reloc_addr
<< 8) >> 8) << 2);
492 #if !defined RTLD_BOOTSTRAP
493 case R_ARM_TLS_DTPMOD32
:
494 /* Get the information from the link map returned by the
497 *reloc_addr
= sym_map
->l_tls_modid
;
500 case R_ARM_TLS_DTPOFF32
:
502 *reloc_addr
+= sym
->st_value
;
505 case R_ARM_TLS_TPOFF32
:
508 CHECK_STATIC_TLS (map
, sym_map
);
509 *reloc_addr
+= sym
->st_value
+ sym_map
->l_tls_offset
;
512 case R_ARM_IRELATIVE
:
513 value
= map
->l_addr
+ *reloc_addr
;
514 value
= ((Elf32_Addr (*) (int)) value
) (GLRO(dl_hwcap
));
519 _dl_reloc_bad_type (map
, r_type
, 0);
525 # ifndef RTLD_BOOTSTRAP
527 __attribute__ ((always_inline
))
528 elf_machine_rela (struct link_map
*map
, const Elf32_Rela
*reloc
,
529 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
530 void *const reloc_addr_arg
, int skip_ifunc
)
532 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
533 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
535 if (__builtin_expect (r_type
== R_ARM_RELATIVE
, 0))
536 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
537 else if (__builtin_expect (r_type
== R_ARM_NONE
, 0))
541 # ifndef RESOLVE_CONFLICT_FIND_MAP
542 const Elf32_Sym
*const refsym
= sym
;
544 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
545 Elf32_Addr value
= sym_map
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
548 && __builtin_expect (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
, 0)
549 && __builtin_expect (sym
->st_shndx
!= SHN_UNDEF
, 1)
550 && __builtin_expect (!skip_ifunc
, 1))
551 value
= elf_ifunc_invoke (value
);
555 # ifndef RESOLVE_CONFLICT_FIND_MAP
556 /* Not needed for dl-conflict.c. */
559 /* This can happen in trace mode if an object could not be
562 if (sym
->st_size
> refsym
->st_size
563 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
567 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
569 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
570 RTLD_PROGNAME
, strtab
+ refsym
->st_name
);
572 memcpy (reloc_addr_arg
, (void *) value
,
573 MIN (sym
->st_size
, refsym
->st_size
));
575 # endif /* !RESOLVE_CONFLICT_FIND_MAP */
577 case R_ARM_JUMP_SLOT
:
579 *reloc_addr
= value
+ reloc
->r_addend
;
581 # ifdef RESOLVE_CONFLICT_FIND_MAP
584 struct tlsdesc
volatile *td
=
585 (struct tlsdesc
volatile *) reloc_addr
;
587 RESOLVE_CONFLICT_FIND_MAP (map
, reloc_addr
);
589 /* Make sure we know what's going on. */
591 == (void *) (D_PTR (map
, l_info
[ADDRIDX (DT_TLSDESC_PLT
)])
593 assert (map
->l_info
[ADDRIDX (DT_TLSDESC_GOT
)]);
595 /* Set up the lazy resolver and store the pointer to our link
596 map in _GLOBAL_OFFSET_TABLE[1] now as for a prelinked
597 binary elf_machine_runtime_setup() is not called and hence
598 neither has been initialized. */
599 *(Elf32_Addr
*) (D_PTR (map
, l_info
[ADDRIDX (DT_TLSDESC_GOT
)])
601 = (Elf32_Addr
) &_dl_tlsdesc_lazy_resolver
;
602 ((Elf32_Addr
*) D_PTR (map
, l_info
[DT_PLTGOT
]))[1]
606 # endif /* RESOLVE_CONFLICT_FIND_MAP */
608 relocate_pc24 (map
, value
, reloc_addr
, reloc
->r_addend
);
610 #if !defined RTLD_BOOTSTRAP
611 case R_ARM_TLS_DTPMOD32
:
612 /* Get the information from the link map returned by the
615 *reloc_addr
= sym_map
->l_tls_modid
;
618 case R_ARM_TLS_DTPOFF32
:
619 *reloc_addr
= (sym
== NULL
? 0 : sym
->st_value
) + reloc
->r_addend
;
622 case R_ARM_TLS_TPOFF32
:
625 CHECK_STATIC_TLS (map
, sym_map
);
626 *reloc_addr
= (sym
->st_value
+ sym_map
->l_tls_offset
630 case R_ARM_IRELATIVE
:
631 value
= map
->l_addr
+ reloc
->r_addend
;
632 value
= ((Elf32_Addr (*) (int)) value
) (GLRO(dl_hwcap
));
637 _dl_reloc_bad_type (map
, r_type
, 0);
645 __attribute__ ((always_inline
))
646 elf_machine_rel_relative (Elf32_Addr l_addr
, const Elf32_Rel
*reloc
,
647 void *const reloc_addr_arg
)
649 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
650 *reloc_addr
+= l_addr
;
653 # ifndef RTLD_BOOTSTRAP
655 __attribute__ ((always_inline
))
656 elf_machine_rela_relative (Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
657 void *const reloc_addr_arg
)
659 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
660 *reloc_addr
= l_addr
+ reloc
->r_addend
;
665 __attribute__ ((always_inline
))
666 elf_machine_lazy_rel (struct link_map
*map
,
667 Elf32_Addr l_addr
, const Elf32_Rel
*reloc
,
670 Elf32_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
671 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
672 /* Check for unexpected PLT reloc type. */
673 if (__builtin_expect (r_type
== R_ARM_JUMP_SLOT
, 1))
675 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
676 *reloc_addr
+= l_addr
;
678 *reloc_addr
= map
->l_mach
.plt
;
680 else if (__builtin_expect (r_type
== R_ARM_TLS_DESC
, 1))
682 struct tlsdesc
volatile *td
=
683 (struct tlsdesc
volatile *)reloc_addr
;
685 /* The linker must have given us the parameter we need in the
686 first GOT entry, and left the second one empty. The latter
687 will have been preset by the prelinker if used though.
688 We fill it with the resolver address. */
689 assert (td
->entry
== 0
690 || map
->l_info
[VALIDX (DT_GNU_PRELINKED
)] != NULL
);
691 td
->entry
= (void*)(D_PTR (map
, l_info
[ADDRIDX (DT_TLSDESC_PLT
)])
695 _dl_reloc_bad_type (map
, r_type
, 1);
698 #endif /* RESOLVE_MAP */