Warning: This file is not a C or C++ file. It does not have highlighting.
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. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
19 | #ifndef dl_machine_h |
20 | #define dl_machine_h |
21 | |
22 | #define ELF_MACHINE_NAME "ARM" |
23 | |
24 | #include <assert.h> |
25 | #include <sys/param.h> |
26 | #include <tls.h> |
27 | #include <dl-tlsdesc.h> |
28 | #include <dl-irel.h> |
29 | #include <dl-static-tls.h> |
30 | #include <dl-machine-rel.h> |
31 | |
32 | #ifndef CLEAR_CACHE |
33 | # error CLEAR_CACHE definition required to handle TEXTREL |
34 | #endif |
35 | |
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) |
39 | { |
40 | return ehdr->e_machine == EM_ARM; |
41 | } |
42 | |
43 | /* Return the run-time load address of the shared object. */ |
44 | static inline ElfW(Addr) __attribute__ ((unused)) |
45 | elf_machine_load_address (void) |
46 | { |
47 | extern const ElfW(Ehdr) __ehdr_start attribute_hidden; |
48 | return (ElfW(Addr)) &__ehdr_start; |
49 | } |
50 | |
51 | /* Return the link-time address of _DYNAMIC. */ |
52 | static inline ElfW(Addr) __attribute__ ((unused)) |
53 | elf_machine_dynamic (void) |
54 | { |
55 | extern ElfW(Dyn) _DYNAMIC[] attribute_hidden; |
56 | return (ElfW(Addr)) _DYNAMIC - elf_machine_load_address (); |
57 | } |
58 | |
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. */ |
61 | |
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) |
65 | { |
66 | Elf32_Addr *got; |
67 | extern void _dl_runtime_resolve (Elf32_Word); |
68 | |
69 | if (l->l_info[DT_JMPREL] && lazy) |
70 | { |
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. */ |
80 | if (got[1]) |
81 | l->l_mach.plt = got[1] + l->l_addr; |
82 | got[1] = (Elf32_Addr) l; /* Identify this shared object. */ |
83 | |
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. */ |
90 | #ifdef SHARED |
91 | extern void _dl_runtime_profile (Elf32_Word); |
92 | if (profile) |
93 | { |
94 | got[2] = (Elf32_Addr) &_dl_runtime_profile; |
95 | |
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 |
99 | started. */ |
100 | GL(dl_profile_map) = l; |
101 | } |
102 | else |
103 | #endif |
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; |
107 | } |
108 | |
109 | return lazy; |
110 | } |
111 | |
112 | #if defined(ARCH_HAS_BX) |
113 | #define BX(x) "bx\t" #x |
114 | #else |
115 | #define BX(x) "mov\tpc, " #x |
116 | #endif |
117 | |
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 |
121 | |
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. */ |
125 | |
126 | #define RTLD_START asm ("\ |
127 | .text\n\ |
128 | .globl _start\n\ |
129 | .type _start, %function\n\ |
130 | .globl _dl_start_user\n\ |
131 | .type _dl_start_user, %function\n\ |
132 | _start:\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\ |
136 | mov r0, sp\n\ |
137 | bl _dl_start\n\ |
138 | @ returns user entry point in r0\n\ |
139 | _dl_start_user:\n\ |
140 | adr r6, .L_GET_GOT\n\ |
141 | add sl, sl, r6\n\ |
142 | @ save the entry point in another register\n\ |
143 | mov r6, r0\n\ |
144 | @ get the original arg count\n\ |
145 | ldr r1, [sp]\n\ |
146 | @ get the argv address\n\ |
147 | add r2, sp, #4\n\ |
148 | @ compute envp\n\ |
149 | add r3, r2, r1, lsl #2\n\ |
150 | add r3, r3, #4\n\ |
151 | @ now we call _dl_init\n\ |
152 | ldr r0, .L_LOADED\n\ |
153 | ldr r0, [sl, r0]\n\ |
154 | @ call _dl_init\n\ |
155 | bl _dl_init(PLT)\n\ |
156 | @ load the finalizer function\n\ |
157 | ldr r0, .L_FINI_PROC\n\ |
158 | add r0, sl, r0\n\ |
159 | @ jump to the user_s entry point\n\ |
160 | " BX(r6) "\n\ |
161 | \n\ |
162 | .L_GET_GOT:\n\ |
163 | .word _GLOBAL_OFFSET_TABLE_ - .L_GET_GOT\n\ |
164 | .L_FINI_PROC:\n\ |
165 | .word _dl_fini(GOTOFF)\n\ |
166 | .L_ARGV:\n\ |
167 | .word __GI__dl_argv(GOTOFF)\n\ |
168 | .L_LOADED:\n\ |
169 | .word _rtld_local(GOTOFF)\n\ |
170 | .previous\n\ |
171 | "); |
172 | |
173 | /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or |
174 | TLS variable, so undefined references should not be allowed to |
175 | define the value. |
176 | ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one |
177 | of the main executable's symbols, as for a COPY reloc. */ |
178 | #ifndef RTLD_BOOTSTRAP |
179 | # define elf_machine_type_class(type) \ |
180 | ((((type) == R_ARM_JUMP_SLOT || (type) == R_ARM_TLS_DTPMOD32 \ |
181 | || (type) == R_ARM_TLS_DTPOFF32 || (type) == R_ARM_TLS_TPOFF32 \ |
182 | || (type) == R_ARM_TLS_DESC) \ |
183 | * ELF_RTYPE_CLASS_PLT) \ |
184 | | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY)) |
185 | #else |
186 | #define elf_machine_type_class(type) \ |
187 | ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \ |
188 | | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY)) |
189 | #endif |
190 | |
191 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ |
192 | #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT |
193 | |
194 | /* We define an initialization functions. This is called very early in |
195 | _dl_sysdep_start. */ |
196 | #define DL_PLATFORM_INIT dl_platform_init () |
197 | |
198 | static inline void __attribute__ ((unused)) |
199 | dl_platform_init (void) |
200 | { |
201 | if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0') |
202 | /* Avoid an empty string which would disturb us. */ |
203 | GLRO(dl_platform) = NULL; |
204 | } |
205 | |
206 | static inline Elf32_Addr |
207 | elf_machine_fixup_plt (struct link_map *map, lookup_t t, |
208 | const ElfW(Sym) *refsym, const ElfW(Sym) *sym, |
209 | const Elf32_Rel *reloc, |
210 | Elf32_Addr *reloc_addr, Elf32_Addr value) |
211 | { |
212 | return *reloc_addr = value; |
213 | } |
214 | |
215 | /* Return the final value of a plt relocation. */ |
216 | static inline Elf32_Addr |
217 | elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc, |
218 | Elf32_Addr value) |
219 | { |
220 | return value; |
221 | } |
222 | |
223 | #endif /* !dl_machine_h */ |
224 | |
225 | |
226 | /* Names of the architecture-specific auditing callback functions. */ |
227 | #define ARCH_LA_PLTENTER arm_gnu_pltenter |
228 | #define ARCH_LA_PLTEXIT arm_gnu_pltexit |
229 | |
230 | #ifdef RESOLVE_MAP |
231 | /* Set NEW_VALUE based on V, and return true iff it overflows 24 bits. */ |
232 | static inline bool set_new_value (Elf32_Addr *new_value, Elf32_Addr v, |
233 | Elf32_Addr *const reloc_addr, |
234 | Elf32_Sword addend) |
235 | { |
236 | *new_value = v + addend - (Elf32_Addr) reloc_addr; |
237 | Elf32_Addr topbits = *new_value & 0xfe000000; |
238 | return topbits != 0xfe000000 && topbits != 0x00000000; |
239 | } |
240 | |
241 | /* Handle a PC24 reloc, including the out-of-range case. */ |
242 | static void |
243 | relocate_pc24 (struct link_map *map, Elf32_Addr value, |
244 | Elf32_Addr *const reloc_addr, Elf32_Sword addend) |
245 | { |
246 | Elf32_Addr new_value; |
247 | |
248 | if (set_new_value (&new_value, value, reloc_addr, addend)) |
249 | { |
250 | /* The PC-relative address doesn't fit in 24 bits! */ |
251 | |
252 | static void *fix_page; |
253 | static size_t fix_offset; |
254 | if (fix_page == NULL) |
255 | { |
256 | void *new_page = __mmap (NULL, GLRO(dl_pagesize), |
257 | PROT_READ | PROT_WRITE | PROT_EXEC, |
258 | MAP_PRIVATE | MAP_ANON, -1, 0); |
259 | if (new_page == MAP_FAILED) |
260 | _dl_signal_error (0, map->l_name, NULL, |
261 | "could not map page for fixup"); |
262 | fix_page = new_page; |
263 | assert (fix_offset == 0); |
264 | } |
265 | |
266 | Elf32_Word *fix_address = fix_page + fix_offset; |
267 | fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */ |
268 | fix_address[1] = value; |
269 | |
270 | fix_offset += sizeof fix_address[0] * 2; |
271 | if (fix_offset >= GLRO(dl_pagesize)) |
272 | { |
273 | fix_page = NULL; |
274 | fix_offset = 0; |
275 | } |
276 | |
277 | if (set_new_value (&new_value, (Elf32_Addr) fix_address, reloc_addr, |
278 | addend)) |
279 | _dl_signal_error (0, map->l_name, NULL, |
280 | "R_ARM_PC24 relocation out of range"); |
281 | } |
282 | |
283 | *reloc_addr = (*reloc_addr & 0xff000000) | ((new_value >> 2) & 0x00ffffff); |
284 | } |
285 | |
286 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). |
287 | MAP is the object containing the reloc. */ |
288 | |
289 | static inline void |
290 | __attribute__ ((always_inline)) |
291 | elf_machine_rel (struct link_map *map, struct r_scope_elem *scope[], |
292 | const Elf32_Rel *reloc, const Elf32_Sym *sym, |
293 | const struct r_found_version *version, |
294 | void *const reloc_addr_arg, int skip_ifunc) |
295 | { |
296 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
297 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
298 | |
299 | #if !defined RTLD_BOOTSTRAP |
300 | if (__builtin_expect (r_type == R_ARM_RELATIVE, 0)) |
301 | *reloc_addr += map->l_addr; |
302 | # ifndef RTLD_BOOTSTRAP |
303 | else if (__builtin_expect (r_type == R_ARM_NONE, 0)) |
304 | return; |
305 | # endif |
306 | else |
307 | #endif |
308 | { |
309 | const Elf32_Sym *const refsym = sym; |
310 | struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version, |
311 | r_type); |
312 | Elf32_Addr value = SYMBOL_ADDRESS (sym_map, sym, true); |
313 | |
314 | if (sym != NULL |
315 | && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0) |
316 | && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1) |
317 | && __builtin_expect (!skip_ifunc, 1)) |
318 | value = elf_ifunc_invoke (value); |
319 | |
320 | switch (r_type) |
321 | { |
322 | case R_ARM_COPY: |
323 | if (sym == NULL) |
324 | /* This can happen in trace mode if an object could not be |
325 | found. */ |
326 | break; |
327 | if (sym->st_size > refsym->st_size |
328 | || (GLRO(dl_verbose) && sym->st_size < refsym->st_size)) |
329 | { |
330 | const char *strtab; |
331 | |
332 | strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); |
333 | _dl_error_printf ("\ |
334 | %s: Symbol `%s' has different size in shared object, consider re-linking\n", |
335 | RTLD_PROGNAME, strtab + refsym->st_name); |
336 | } |
337 | memcpy (reloc_addr_arg, (void *) value, |
338 | MIN (sym->st_size, refsym->st_size)); |
339 | break; |
340 | case R_ARM_GLOB_DAT: |
341 | case R_ARM_JUMP_SLOT: |
342 | # ifdef RTLD_BOOTSTRAP |
343 | /* Fix weak undefined references. */ |
344 | if (sym != NULL && sym->st_value == 0) |
345 | *reloc_addr = 0; |
346 | else |
347 | # endif |
348 | *reloc_addr = value; |
349 | break; |
350 | case R_ARM_ABS32: |
351 | { |
352 | struct unaligned |
353 | { |
354 | Elf32_Addr x; |
355 | } __attribute__ ((packed, may_alias)); |
356 | # ifndef RTLD_BOOTSTRAP |
357 | /* This is defined in rtld.c, but nowhere in the static |
358 | libc.a; make the reference weak so static programs can |
359 | still link. This declaration cannot be done when |
360 | compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because |
361 | rtld.c contains the common defn for _dl_rtld_map, which |
362 | is incompatible with a weak decl in the same file. */ |
363 | # ifndef SHARED |
364 | weak_extern (_dl_rtld_map); |
365 | # endif |
366 | if (map == &GL(dl_rtld_map)) |
367 | /* Undo the relocation done here during bootstrapping. |
368 | Now we will relocate it anew, possibly using a |
369 | binding found in the user program or a loaded library |
370 | rather than the dynamic linker's built-in definitions |
371 | used while loading those libraries. */ |
372 | value -= SYMBOL_ADDRESS (map, refsym, true); |
373 | # endif |
374 | /* Support relocations on mis-aligned offsets. */ |
375 | ((struct unaligned *) reloc_addr)->x += value; |
376 | break; |
377 | } |
378 | case R_ARM_TLS_DESC: |
379 | { |
380 | struct tlsdesc *td = (struct tlsdesc *)reloc_addr; |
381 | |
382 | # ifndef RTLD_BOOTSTRAP |
383 | if (! sym) |
384 | td->entry = _dl_tlsdesc_undefweak; |
385 | else |
386 | # endif |
387 | { |
388 | if (ELF32_R_SYM (reloc->r_info) == STN_UNDEF) |
389 | value = td->argument.value; |
390 | else |
391 | value = sym->st_value; |
392 | |
393 | # ifndef RTLD_BOOTSTRAP |
394 | # ifndef SHARED |
395 | CHECK_STATIC_TLS (map, sym_map); |
396 | # else |
397 | if (!TRY_STATIC_TLS (map, sym_map)) |
398 | { |
399 | td->argument.pointer |
400 | = _dl_make_tlsdesc_dynamic (sym_map, value); |
401 | td->entry = _dl_tlsdesc_dynamic; |
402 | } |
403 | else |
404 | # endif |
405 | # endif |
406 | { |
407 | td->argument.value = value + sym_map->l_tls_offset; |
408 | td->entry = _dl_tlsdesc_return; |
409 | } |
410 | } |
411 | } |
412 | break; |
413 | case R_ARM_PC24: |
414 | relocate_pc24 (map, value, reloc_addr, |
415 | /* Sign-extend the 24-bit addend in the |
416 | instruction (which counts instructions), and |
417 | then shift it up two so as to count bytes. */ |
418 | (((Elf32_Sword) *reloc_addr << 8) >> 8) << 2); |
419 | break; |
420 | #if !defined RTLD_BOOTSTRAP |
421 | case R_ARM_TLS_DTPMOD32: |
422 | /* Get the information from the link map returned by the |
423 | resolv function. */ |
424 | if (sym_map != NULL) |
425 | *reloc_addr = sym_map->l_tls_modid; |
426 | break; |
427 | |
428 | case R_ARM_TLS_DTPOFF32: |
429 | if (sym != NULL) |
430 | *reloc_addr += sym->st_value; |
431 | break; |
432 | |
433 | case R_ARM_TLS_TPOFF32: |
434 | if (sym != NULL) |
435 | { |
436 | CHECK_STATIC_TLS (map, sym_map); |
437 | *reloc_addr += sym->st_value + sym_map->l_tls_offset; |
438 | } |
439 | break; |
440 | case R_ARM_IRELATIVE: |
441 | value = map->l_addr + *reloc_addr; |
442 | if (__glibc_likely (!skip_ifunc)) |
443 | value = ((Elf32_Addr (*) (int)) value) (GLRO(dl_hwcap)); |
444 | *reloc_addr = value; |
445 | break; |
446 | #endif |
447 | default: |
448 | _dl_reloc_bad_type (map, r_type, 0); |
449 | break; |
450 | } |
451 | } |
452 | } |
453 | |
454 | |
455 | static inline void |
456 | __attribute__ ((always_inline)) |
457 | elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc, |
458 | void *const reloc_addr_arg) |
459 | { |
460 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
461 | *reloc_addr += l_addr; |
462 | } |
463 | |
464 | |
465 | static inline void |
466 | __attribute__ ((always_inline)) |
467 | elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[], |
468 | Elf32_Addr l_addr, const Elf32_Rel *reloc, |
469 | int skip_ifunc) |
470 | { |
471 | Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset); |
472 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
473 | /* Check for unexpected PLT reloc type. */ |
474 | if (__builtin_expect (r_type == R_ARM_JUMP_SLOT, 1)) |
475 | { |
476 | if (__builtin_expect (map->l_mach.plt, 0) == 0) |
477 | *reloc_addr += l_addr; |
478 | else |
479 | *reloc_addr = map->l_mach.plt; |
480 | } |
481 | else if (__builtin_expect (r_type == R_ARM_TLS_DESC, 1)) |
482 | { |
483 | const Elf_Symndx symndx = ELFW (R_SYM) (reloc->r_info); |
484 | const ElfW (Sym) *symtab = (const void *)D_PTR (map, l_info[DT_SYMTAB]); |
485 | const ElfW (Sym) *sym = &symtab[symndx]; |
486 | const struct r_found_version *version = NULL; |
487 | |
488 | if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL) |
489 | { |
490 | const ElfW (Half) *vernum = |
491 | (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]); |
492 | version = &map->l_versions[vernum[symndx] & 0x7fff]; |
493 | } |
494 | |
495 | /* Always initialize TLS descriptors completely, because lazy |
496 | initialization requires synchronization at every TLS access. */ |
497 | elf_machine_rel (map, scope, reloc, sym, version, reloc_addr, skip_ifunc); |
498 | } |
499 | else |
500 | _dl_reloc_bad_type (map, r_type, 1); |
501 | } |
502 | |
503 | #endif /* RESOLVE_MAP */ |
504 |
Warning: This file is not a C or C++ file. It does not have highlighting.