1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995-2024 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 <https://www.gnu.org/licenses/>. */
22 #define ELF_MACHINE_NAME "ARM"
25 #include <sys/param.h>
27 #include <dl-tlsdesc.h>
29 #include <dl-static-tls.h>
30 #include <dl-machine-rel.h>
33 # error CLEAR_CACHE definition required to handle TEXTREL
36 /* Return nonzero iff ELF header is compatible with the running host. */
37 static inline int __attribute__ ((unused
))
38 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
40 return ehdr
->e_machine
== EM_ARM
;
43 /* Return the run-time load address of the shared object. */
44 static inline ElfW(Addr
) __attribute__ ((unused
))
45 elf_machine_load_address (void)
47 extern const ElfW(Ehdr
) __ehdr_start attribute_hidden
;
48 return (ElfW(Addr
)) &__ehdr_start
;
51 /* Return the link-time address of _DYNAMIC. */
52 static inline ElfW(Addr
) __attribute__ ((unused
))
53 elf_machine_dynamic (void)
55 extern ElfW(Dyn
) _DYNAMIC
[] attribute_hidden
;
56 return (ElfW(Addr
)) _DYNAMIC
- elf_machine_load_address ();
59 /* Set up the loaded object described by L so its unrelocated PLT
60 entries will jump to the on-demand fixup code in dl-runtime.c. */
62 static inline int __attribute__ ((unused
))
63 elf_machine_runtime_setup (struct link_map
*l
, struct r_scope_elem
*scope
[],
64 int lazy
, int profile
)
67 extern void _dl_runtime_resolve (Elf32_Word
);
69 if (l
->l_info
[DT_JMPREL
] && lazy
)
71 /* patb: this is different than i386 */
72 /* The GOT entries for functions in the PLT have not yet been filled
73 in. Their initial contents will arrange when called to push an
74 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
75 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
76 got
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
77 /* If a library is prelinked but we have to relocate anyway,
78 we have to be able to undo the prelinking of .got.plt.
79 The prelinker saved us here address of .plt. */
81 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
82 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
84 /* The got[2] entry contains the address of a function which gets
85 called to get the address of a so far unresolved function and
86 jump to it. The profiling extension of the dynamic linker allows
87 to intercept the calls to collect information. In this case we
88 don't store the address in the GOT so that all future calls also
89 end in this function. */
91 extern void _dl_runtime_profile (Elf32_Word
);
94 got
[2] = (Elf32_Addr
) &_dl_runtime_profile
;
96 if (GLRO(dl_profile
) != NULL
97 && _dl_name_match_p (GLRO(dl_profile
), l
))
98 /* Say that we really want profiling and the timers are
100 GL(dl_profile_map
) = l
;
104 /* This function will get called to fix up the GOT entry indicated by
105 the offset on the stack, and then jump to the resolved address. */
106 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
112 #if defined(ARCH_HAS_BX)
113 #define BX(x) "bx\t" #x
115 #define BX(x) "mov\tpc, " #x
118 /* Mask identifying addresses reserved for the user program,
119 where the dynamic linker should not map anything. */
120 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
122 /* Initial entry point code for the dynamic linker.
123 The C function `_dl_start' is the real entry point;
124 its return value is the user program's entry point. */
126 #define RTLD_START asm ("\
129 .type _start, %function\n\
130 .globl _dl_start_user\n\
131 .type _dl_start_user, %function\n\
133 @ we are PIC code, so get global offset table\n\
134 ldr sl, .L_GET_GOT\n\
135 @ at start time, all the args are on the stack\n\
138 @ returns user entry point in r0\n\
140 adr r6, .L_GET_GOT\n\
143 @ save the entry point in another register\n\
145 @ get the original arg count\n\
147 @ get the argv address\n\
150 add r3, r2, r1, lsl #2\n\
152 @ now we call _dl_init\n\
157 @ load the finalizer function\n\
158 ldr r0, .L_FINI_PROC\n\
160 @ jump to the user_s entry point\n\
164 .word _GLOBAL_OFFSET_TABLE_ - .L_GET_GOT\n\
166 .word _dl_fini(GOTOFF)\n\
168 .word __GI__dl_argv(GOTOFF)\n\
170 .word _rtld_local(GOTOFF)\n\
174 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
175 TLS variable, so undefined references should not be allowed to
177 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
178 of the main executable's symbols, as for a COPY reloc. */
179 #ifndef RTLD_BOOTSTRAP
180 # define elf_machine_type_class(type) \
181 ((((type) == R_ARM_JUMP_SLOT || (type) == R_ARM_TLS_DTPMOD32 \
182 || (type) == R_ARM_TLS_DTPOFF32 || (type) == R_ARM_TLS_TPOFF32 \
183 || (type) == R_ARM_TLS_DESC) \
184 * ELF_RTYPE_CLASS_PLT) \
185 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
187 #define elf_machine_type_class(type) \
188 ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
189 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
192 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
193 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
195 /* We define an initialization functions. This is called very early in
197 #define DL_PLATFORM_INIT dl_platform_init ()
199 static inline void __attribute__ ((unused
))
200 dl_platform_init (void)
202 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
203 /* Avoid an empty string which would disturb us. */
204 GLRO(dl_platform
) = NULL
;
207 static inline Elf32_Addr
208 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
209 const ElfW(Sym
) *refsym
, const ElfW(Sym
) *sym
,
210 const Elf32_Rel
*reloc
,
211 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
213 return *reloc_addr
= value
;
216 /* Return the final value of a plt relocation. */
217 static inline Elf32_Addr
218 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rel
*reloc
,
224 #endif /* !dl_machine_h */
227 /* Names of the architecture-specific auditing callback functions. */
228 #define ARCH_LA_PLTENTER arm_gnu_pltenter
229 #define ARCH_LA_PLTEXIT arm_gnu_pltexit
232 /* Set NEW_VALUE based on V, and return true iff it overflows 24 bits. */
233 static inline bool set_new_value (Elf32_Addr
*new_value
, Elf32_Addr v
,
234 Elf32_Addr
*const reloc_addr
,
237 *new_value
= v
+ addend
- (Elf32_Addr
) reloc_addr
;
238 Elf32_Addr topbits
= *new_value
& 0xfe000000;
239 return topbits
!= 0xfe000000 && topbits
!= 0x00000000;
242 /* Handle a PC24 reloc, including the out-of-range case. */
244 relocate_pc24 (struct link_map
*map
, Elf32_Addr value
,
245 Elf32_Addr
*const reloc_addr
, Elf32_Sword addend
)
247 Elf32_Addr new_value
;
249 if (set_new_value (&new_value
, value
, reloc_addr
, addend
))
251 /* The PC-relative address doesn't fit in 24 bits! */
253 static void *fix_page
;
254 static size_t fix_offset
;
255 if (fix_page
== NULL
)
257 void *new_page
= __mmap (NULL
, GLRO(dl_pagesize
),
258 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
259 MAP_PRIVATE
| MAP_ANON
, -1, 0);
260 if (new_page
== MAP_FAILED
)
261 _dl_signal_error (0, map
->l_name
, NULL
,
262 "could not map page for fixup");
264 assert (fix_offset
== 0);
267 Elf32_Word
*fix_address
= fix_page
+ fix_offset
;
268 fix_address
[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
269 fix_address
[1] = value
;
271 fix_offset
+= sizeof fix_address
[0] * 2;
272 if (fix_offset
>= GLRO(dl_pagesize
))
278 if (set_new_value (&new_value
, (Elf32_Addr
) fix_address
, reloc_addr
,
280 _dl_signal_error (0, map
->l_name
, NULL
,
281 "R_ARM_PC24 relocation out of range");
284 *reloc_addr
= (*reloc_addr
& 0xff000000) | ((new_value
>> 2) & 0x00ffffff);
287 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
288 MAP is the object containing the reloc. */
291 __attribute__ ((always_inline
))
292 elf_machine_rel (struct link_map
*map
, struct r_scope_elem
*scope
[],
293 const Elf32_Rel
*reloc
, const Elf32_Sym
*sym
,
294 const struct r_found_version
*version
,
295 void *const reloc_addr_arg
, int skip_ifunc
)
297 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
298 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
300 #if !defined RTLD_BOOTSTRAP
301 if (__builtin_expect (r_type
== R_ARM_RELATIVE
, 0))
302 *reloc_addr
+= map
->l_addr
;
303 # ifndef RTLD_BOOTSTRAP
304 else if (__builtin_expect (r_type
== R_ARM_NONE
, 0))
310 const Elf32_Sym
*const refsym
= sym
;
311 struct link_map
*sym_map
= RESOLVE_MAP (map
, scope
, &sym
, version
,
313 Elf32_Addr value
= SYMBOL_ADDRESS (sym_map
, sym
, true);
316 && __builtin_expect (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
, 0)
317 && __builtin_expect (sym
->st_shndx
!= SHN_UNDEF
, 1)
318 && __builtin_expect (!skip_ifunc
, 1))
319 value
= elf_ifunc_invoke (value
);
325 /* This can happen in trace mode if an object could not be
328 if (sym
->st_size
> refsym
->st_size
329 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
333 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
335 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
336 RTLD_PROGNAME
, strtab
+ refsym
->st_name
);
338 memcpy (reloc_addr_arg
, (void *) value
,
339 MIN (sym
->st_size
, refsym
->st_size
));
342 case R_ARM_JUMP_SLOT
:
343 # ifdef RTLD_BOOTSTRAP
344 /* Fix weak undefined references. */
345 if (sym
!= NULL
&& sym
->st_value
== 0)
356 } __attribute__ ((packed
, may_alias
));
357 # ifndef RTLD_BOOTSTRAP
358 /* This is defined in rtld.c, but nowhere in the static
359 libc.a; make the reference weak so static programs can
360 still link. This declaration cannot be done when
361 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
362 rtld.c contains the common defn for _dl_rtld_map, which
363 is incompatible with a weak decl in the same file. */
365 weak_extern (_dl_rtld_map
);
367 if (map
== &GL(dl_rtld_map
))
368 /* Undo the relocation done here during bootstrapping.
369 Now we will relocate it anew, possibly using a
370 binding found in the user program or a loaded library
371 rather than the dynamic linker's built-in definitions
372 used while loading those libraries. */
373 value
-= SYMBOL_ADDRESS (map
, refsym
, true);
375 /* Support relocations on mis-aligned offsets. */
376 ((struct unaligned
*) reloc_addr
)->x
+= value
;
381 struct tlsdesc
*td
= (struct tlsdesc
*)reloc_addr
;
383 # ifndef RTLD_BOOTSTRAP
385 td
->entry
= _dl_tlsdesc_undefweak
;
389 if (ELF32_R_SYM (reloc
->r_info
) == STN_UNDEF
)
390 value
= td
->argument
.value
;
392 value
= sym
->st_value
;
394 # ifndef RTLD_BOOTSTRAP
396 CHECK_STATIC_TLS (map
, sym_map
);
398 if (!TRY_STATIC_TLS (map
, sym_map
))
401 = _dl_make_tlsdesc_dynamic (sym_map
, value
);
402 td
->entry
= _dl_tlsdesc_dynamic
;
408 td
->argument
.value
= value
+ sym_map
->l_tls_offset
;
409 td
->entry
= _dl_tlsdesc_return
;
415 relocate_pc24 (map
, value
, reloc_addr
,
416 /* Sign-extend the 24-bit addend in the
417 instruction (which counts instructions), and
418 then shift it up two so as to count bytes. */
419 (((Elf32_Sword
) *reloc_addr
<< 8) >> 8) << 2);
421 #if !defined RTLD_BOOTSTRAP
422 case R_ARM_TLS_DTPMOD32
:
423 /* Get the information from the link map returned by the
426 *reloc_addr
= sym_map
->l_tls_modid
;
429 case R_ARM_TLS_DTPOFF32
:
431 *reloc_addr
+= sym
->st_value
;
434 case R_ARM_TLS_TPOFF32
:
437 CHECK_STATIC_TLS (map
, sym_map
);
438 *reloc_addr
+= sym
->st_value
+ sym_map
->l_tls_offset
;
441 case R_ARM_IRELATIVE
:
442 value
= map
->l_addr
+ *reloc_addr
;
443 if (__glibc_likely (!skip_ifunc
))
444 value
= ((Elf32_Addr (*) (int)) value
) (GLRO(dl_hwcap
));
449 _dl_reloc_bad_type (map
, r_type
, 0);
457 __attribute__ ((always_inline
))
458 elf_machine_rel_relative (Elf32_Addr l_addr
, const Elf32_Rel
*reloc
,
459 void *const reloc_addr_arg
)
461 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
462 *reloc_addr
+= l_addr
;
467 __attribute__ ((always_inline
))
468 elf_machine_lazy_rel (struct link_map
*map
, struct r_scope_elem
*scope
[],
469 Elf32_Addr l_addr
, const Elf32_Rel
*reloc
,
472 Elf32_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
473 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
474 /* Check for unexpected PLT reloc type. */
475 if (__builtin_expect (r_type
== R_ARM_JUMP_SLOT
, 1))
477 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
478 *reloc_addr
+= l_addr
;
480 *reloc_addr
= map
->l_mach
.plt
;
482 else if (__builtin_expect (r_type
== R_ARM_TLS_DESC
, 1))
484 const Elf_Symndx symndx
= ELFW (R_SYM
) (reloc
->r_info
);
485 const ElfW (Sym
) *symtab
= (const void *)D_PTR (map
, l_info
[DT_SYMTAB
]);
486 const ElfW (Sym
) *sym
= &symtab
[symndx
];
487 const struct r_found_version
*version
= NULL
;
489 if (map
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
491 const ElfW (Half
) *vernum
=
492 (const void *)D_PTR (map
, l_info
[VERSYMIDX (DT_VERSYM
)]);
493 version
= &map
->l_versions
[vernum
[symndx
] & 0x7fff];
496 /* Always initialize TLS descriptors completely, because lazy
497 initialization requires synchronization at every TLS access. */
498 elf_machine_rel (map
, scope
, reloc
, sym
, version
, reloc_addr
, skip_ifunc
);
501 _dl_reloc_bad_type (map
, r_type
, 1);
504 #endif /* RESOLVE_MAP */