1/* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001-2022 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 "x86_64"
23
24#include <assert.h>
25#include <sys/param.h>
26#include <sysdep.h>
27#include <tls.h>
28#include <dl-tlsdesc.h>
29#include <dl-static-tls.h>
30#include <dl-machine-rel.h>
31
32/* Return nonzero iff ELF header is compatible with the running host. */
33static inline int __attribute__ ((unused))
34elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
35{
36 return ehdr->e_machine == EM_X86_64;
37}
38
39
40/* Return the run-time load address of the shared object. */
41static inline ElfW(Addr) __attribute__ ((unused))
42elf_machine_load_address (void)
43{
44 extern const ElfW(Ehdr) __ehdr_start attribute_hidden;
45 return (ElfW(Addr)) &__ehdr_start;
46}
47
48/* Return the link-time address of _DYNAMIC. */
49static inline ElfW(Addr) __attribute__ ((unused))
50elf_machine_dynamic (void)
51{
52 extern ElfW(Dyn) _DYNAMIC[] attribute_hidden;
53 return (ElfW(Addr)) _DYNAMIC - elf_machine_load_address ();
54}
55
56/* Set up the loaded object described by L so its unrelocated PLT
57 entries will jump to the on-demand fixup code in dl-runtime.c. */
58
59static inline int __attribute__ ((unused, always_inline))
60elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
61 int lazy, int profile)
62{
63 Elf64_Addr *got;
64 extern void _dl_runtime_resolve_fxsave (ElfW(Word)) attribute_hidden;
65 extern void _dl_runtime_resolve_xsave (ElfW(Word)) attribute_hidden;
66 extern void _dl_runtime_resolve_xsavec (ElfW(Word)) attribute_hidden;
67 extern void _dl_runtime_profile_sse (ElfW(Word)) attribute_hidden;
68 extern void _dl_runtime_profile_avx (ElfW(Word)) attribute_hidden;
69 extern void _dl_runtime_profile_avx512 (ElfW(Word)) attribute_hidden;
70
71 if (l->l_info[DT_JMPREL] && lazy)
72 {
73 /* The GOT entries for functions in the PLT have not yet been filled
74 in. Their initial contents will arrange when called to push an
75 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
76 and then jump to _GLOBAL_OFFSET_TABLE_[2]. */
77 got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
78 /* If a library is prelinked but we have to relocate anyway,
79 we have to be able to undo the prelinking of .got.plt.
80 The prelinker saved us here address of .plt + 0x16. */
81 if (got[1])
82 {
83 l->l_mach.plt = got[1] + l->l_addr;
84 l->l_mach.gotplt = (ElfW(Addr)) &got[3];
85 }
86 /* Identify this shared object. */
87 *(ElfW(Addr) *) (got + 1) = (ElfW(Addr)) l;
88
89 /* The got[2] entry contains the address of a function which gets
90 called to get the address of a so far unresolved function and
91 jump to it. The profiling extension of the dynamic linker allows
92 to intercept the calls to collect information. In this case we
93 don't store the address in the GOT so that all future calls also
94 end in this function. */
95 if (__glibc_unlikely (profile))
96 {
97 if (CPU_FEATURE_USABLE (AVX512F))
98 *(ElfW(Addr) *) (got + 2) = (ElfW(Addr)) &_dl_runtime_profile_avx512;
99 else if (CPU_FEATURE_USABLE (AVX))
100 *(ElfW(Addr) *) (got + 2) = (ElfW(Addr)) &_dl_runtime_profile_avx;
101 else
102 *(ElfW(Addr) *) (got + 2) = (ElfW(Addr)) &_dl_runtime_profile_sse;
103
104 if (GLRO(dl_profile) != NULL
105 && _dl_name_match_p (GLRO(dl_profile), l))
106 /* This is the object we are looking for. Say that we really
107 want profiling and the timers are started. */
108 GL(dl_profile_map) = l;
109 }
110 else
111 {
112 /* This function will get called to fix up the GOT entry
113 indicated by the offset on the stack, and then jump to
114 the resolved address. */
115 if (GLRO(dl_x86_cpu_features).xsave_state_size != 0)
116 *(ElfW(Addr) *) (got + 2)
117 = (CPU_FEATURE_USABLE (XSAVEC)
118 ? (ElfW(Addr)) &_dl_runtime_resolve_xsavec
119 : (ElfW(Addr)) &_dl_runtime_resolve_xsave);
120 else
121 *(ElfW(Addr) *) (got + 2)
122 = (ElfW(Addr)) &_dl_runtime_resolve_fxsave;
123 }
124 }
125
126 return lazy;
127}
128
129/* Initial entry point code for the dynamic linker.
130 The C function `_dl_start' is the real entry point;
131 its return value is the user program's entry point. */
132#define RTLD_START asm ("\n\
133.text\n\
134 .align 16\n\
135.globl _start\n\
136.globl _dl_start_user\n\
137_start:\n\
138 movq %rsp, %rdi\n\
139 call _dl_start\n\
140_dl_start_user:\n\
141 # Save the user entry point address in %r12.\n\
142 movq %rax, %r12\n\
143 # See if we were run as a command with the executable file\n\
144 # name as an extra leading argument.\n\
145 movl _dl_skip_args(%rip), %eax\n\
146 # Pop the original argument count.\n\
147 popq %rdx\n\
148 # Adjust the stack pointer to skip _dl_skip_args words.\n\
149 leaq (%rsp,%rax,8), %rsp\n\
150 # Subtract _dl_skip_args from argc.\n\
151 subl %eax, %edx\n\
152 # Push argc back on the stack.\n\
153 pushq %rdx\n\
154 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
155 # argc -> rsi\n\
156 movq %rdx, %rsi\n\
157 # Save %rsp value in %r13.\n\
158 movq %rsp, %r13\n\
159 # And align stack for the _dl_init call. \n\
160 andq $-16, %rsp\n\
161 # _dl_loaded -> rdi\n\
162 movq _rtld_local(%rip), %rdi\n\
163 # env -> rcx\n\
164 leaq 16(%r13,%rdx,8), %rcx\n\
165 # argv -> rdx\n\
166 leaq 8(%r13), %rdx\n\
167 # Clear %rbp to mark outermost frame obviously even for constructors.\n\
168 xorl %ebp, %ebp\n\
169 # Call the function to run the initializers.\n\
170 call _dl_init\n\
171 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
172 leaq _dl_fini(%rip), %rdx\n\
173 # And make sure %rsp points to argc stored on the stack.\n\
174 movq %r13, %rsp\n\
175 # Jump to the user's entry point.\n\
176 jmp *%r12\n\
177.previous\n\
178");
179
180/* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
181 TLS variable, so undefined references should not be allowed to
182 define the value.
183 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
184 of the main executable's symbols, as for a COPY reloc.
185 ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA iff TYPE describes relocation may
186 against protected data whose address be external due to copy relocation.
187 */
188#define elf_machine_type_class(type) \
189 ((((type) == R_X86_64_JUMP_SLOT \
190 || (type) == R_X86_64_DTPMOD64 \
191 || (type) == R_X86_64_DTPOFF64 \
192 || (type) == R_X86_64_TPOFF64 \
193 || (type) == R_X86_64_TLSDESC) \
194 * ELF_RTYPE_CLASS_PLT) \
195 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY) \
196 | (((type) == R_X86_64_GLOB_DAT) * ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA))
197
198/* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
199#define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
200
201/* The relative ifunc relocation. */
202// XXX This is a work-around for a broken linker. Remove!
203#define ELF_MACHINE_IRELATIVE R_X86_64_IRELATIVE
204
205/* We define an initialization function. This is called very early in
206 _dl_sysdep_start. */
207#define DL_PLATFORM_INIT dl_platform_init ()
208
209static inline void __attribute__ ((unused))
210dl_platform_init (void)
211{
212#if IS_IN (rtld)
213 /* _dl_x86_init_cpu_features is a wrapper for init_cpu_features which
214 has been called early from __libc_start_main in static executable. */
215 _dl_x86_init_cpu_features ();
216#else
217 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
218 /* Avoid an empty string which would disturb us. */
219 GLRO(dl_platform) = NULL;
220#endif
221}
222
223static inline ElfW(Addr)
224elf_machine_fixup_plt (struct link_map *map, lookup_t t,
225 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
226 const ElfW(Rela) *reloc,
227 ElfW(Addr) *reloc_addr, ElfW(Addr) value)
228{
229 return *reloc_addr = value;
230}
231
232/* Return the final value of a PLT relocation. On x86-64 the
233 JUMP_SLOT relocation ignores the addend. */
234static inline ElfW(Addr)
235elf_machine_plt_value (struct link_map *map, const ElfW(Rela) *reloc,
236 ElfW(Addr) value)
237{
238 return value;
239}
240
241
242/* Names of the architecture-specific auditing callback functions. */
243#define ARCH_LA_PLTENTER x86_64_gnu_pltenter
244#define ARCH_LA_PLTEXIT x86_64_gnu_pltexit
245
246#endif /* !dl_machine_h */
247
248#ifdef RESOLVE_MAP
249
250/* Perform the relocation specified by RELOC and SYM (which is fully resolved).
251 MAP is the object containing the reloc. */
252
253static inline void __attribute__((always_inline))
254elf_machine_rela(struct link_map *map, struct r_scope_elem *scope[],
255 const ElfW(Rela) *reloc, const ElfW(Sym) *sym,
256 const struct r_found_version *version,
257 void *const reloc_addr_arg, int skip_ifunc) {
258 ElfW(Addr) *const reloc_addr = reloc_addr_arg;
259 const unsigned long int r_type = ELFW(R_TYPE) (reloc->r_info);
260
261# if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
262 if (__glibc_unlikely (r_type == R_X86_64_RELATIVE))
263 {
264# if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
265 /* This is defined in rtld.c, but nowhere in the static libc.a;
266 make the reference weak so static programs can still link.
267 This declaration cannot be done when compiling rtld.c
268 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
269 common defn for _dl_rtld_map, which is incompatible with a
270 weak decl in the same file. */
271# ifndef SHARED
272 weak_extern (GL(dl_rtld_map));
273# endif
274 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
275# endif
276 *reloc_addr = map->l_addr + reloc->r_addend;
277 }
278 else
279# endif
280# if !defined RTLD_BOOTSTRAP
281 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
282 relocation updates the whole 64-bit entry. */
283 if (__glibc_unlikely (r_type == R_X86_64_RELATIVE64))
284 *(Elf64_Addr *) reloc_addr = (Elf64_Addr) map->l_addr + reloc->r_addend;
285 else
286# endif
287 if (__glibc_unlikely (r_type == R_X86_64_NONE))
288 return;
289 else
290 {
291# ifndef RTLD_BOOTSTRAP
292 const ElfW(Sym) *const refsym = sym;
293# endif
294 struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version,
295 r_type);
296 ElfW(Addr) value = SYMBOL_ADDRESS (sym_map, sym, true);
297
298 if (sym != NULL
299 && __glibc_unlikely (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC)
300 && __glibc_likely (sym->st_shndx != SHN_UNDEF)
301 && __glibc_likely (!skip_ifunc))
302 {
303# ifndef RTLD_BOOTSTRAP
304 if (sym_map != map
305 && !sym_map->l_relocated)
306 {
307 const char *strtab
308 = (const char *) D_PTR (map, l_info[DT_STRTAB]);
309 if (sym_map->l_type == lt_executable)
310 _dl_fatal_printf ("\
311%s: IFUNC symbol '%s' referenced in '%s' is defined in the executable \
312and creates an unsatisfiable circular dependency.\n",
313 RTLD_PROGNAME, strtab + refsym->st_name,
314 map->l_name);
315 else
316 _dl_error_printf ("\
317%s: Relink `%s' with `%s' for IFUNC symbol `%s'\n",
318 RTLD_PROGNAME, map->l_name,
319 sym_map->l_name,
320 strtab + refsym->st_name);
321 }
322# endif
323 value = ((ElfW(Addr) (*) (void)) value) ();
324 }
325
326 switch (r_type)
327 {
328# ifndef RTLD_BOOTSTRAP
329# ifdef __ILP32__
330 case R_X86_64_SIZE64:
331 /* Set to symbol size plus addend. */
332 *(Elf64_Addr *) (uintptr_t) reloc_addr
333 = (Elf64_Addr) sym->st_size + reloc->r_addend;
334 break;
335
336 case R_X86_64_SIZE32:
337# else
338 case R_X86_64_SIZE64:
339# endif
340 /* Set to symbol size plus addend. */
341 value = sym->st_size;
342# endif
343 /* Fall through. */
344 case R_X86_64_GLOB_DAT:
345 case R_X86_64_JUMP_SLOT:
346 *reloc_addr = value + reloc->r_addend;
347 break;
348
349# ifndef RESOLVE_CONFLICT_FIND_MAP
350 case R_X86_64_DTPMOD64:
351# ifdef RTLD_BOOTSTRAP
352 /* During startup the dynamic linker is always the module
353 with index 1.
354 XXX If this relocation is necessary move before RESOLVE
355 call. */
356 *reloc_addr = 1;
357# else
358 /* Get the information from the link map returned by the
359 resolve function. */
360 if (sym_map != NULL)
361 *reloc_addr = sym_map->l_tls_modid;
362# endif
363 break;
364 case R_X86_64_DTPOFF64:
365# ifndef RTLD_BOOTSTRAP
366 /* During relocation all TLS symbols are defined and used.
367 Therefore the offset is already correct. */
368 if (sym != NULL)
369 {
370 value = sym->st_value + reloc->r_addend;
371# ifdef __ILP32__
372 /* This relocation type computes a signed offset that is
373 usually negative. The symbol and addend values are 32
374 bits but the GOT entry is 64 bits wide and the whole
375 64-bit entry is used as a signed quantity, so we need
376 to sign-extend the computed value to 64 bits. */
377 *(Elf64_Sxword *) reloc_addr = (Elf64_Sxword) (Elf32_Sword) value;
378# else
379 *reloc_addr = value;
380# endif
381 }
382# endif
383 break;
384 case R_X86_64_TLSDESC:
385 {
386 struct tlsdesc volatile *td =
387 (struct tlsdesc volatile *)reloc_addr;
388
389# ifndef RTLD_BOOTSTRAP
390 if (! sym)
391 {
392 td->arg = (void*)reloc->r_addend;
393 td->entry = _dl_tlsdesc_undefweak;
394 }
395 else
396# endif
397 {
398# ifndef RTLD_BOOTSTRAP
399# ifndef SHARED
400 CHECK_STATIC_TLS (map, sym_map);
401# else
402 if (!TRY_STATIC_TLS (map, sym_map))
403 {
404 td->arg = _dl_make_tlsdesc_dynamic
405 (sym_map, sym->st_value + reloc->r_addend);
406 td->entry = _dl_tlsdesc_dynamic;
407 }
408 else
409# endif
410# endif
411 {
412 td->arg = (void*)(sym->st_value - sym_map->l_tls_offset
413 + reloc->r_addend);
414 td->entry = _dl_tlsdesc_return;
415 }
416 }
417 break;
418 }
419 case R_X86_64_TPOFF64:
420 /* The offset is negative, forward from the thread pointer. */
421# ifndef RTLD_BOOTSTRAP
422 if (sym != NULL)
423# endif
424 {
425# ifndef RTLD_BOOTSTRAP
426 CHECK_STATIC_TLS (map, sym_map);
427# endif
428 /* We know the offset of the object the symbol is contained in.
429 It is a negative value which will be added to the
430 thread pointer. */
431 value = (sym->st_value + reloc->r_addend
432 - sym_map->l_tls_offset);
433# ifdef __ILP32__
434 /* The symbol and addend values are 32 bits but the GOT
435 entry is 64 bits wide and the whole 64-bit entry is used
436 as a signed quantity, so we need to sign-extend the
437 computed value to 64 bits. */
438 *(Elf64_Sxword *) reloc_addr = (Elf64_Sxword) (Elf32_Sword) value;
439# else
440 *reloc_addr = value;
441# endif
442 }
443 break;
444# endif
445
446# ifndef RTLD_BOOTSTRAP
447 case R_X86_64_64:
448 /* value + r_addend may be > 0xffffffff and R_X86_64_64
449 relocation updates the whole 64-bit entry. */
450 *(Elf64_Addr *) reloc_addr = (Elf64_Addr) value + reloc->r_addend;
451 break;
452# ifndef __ILP32__
453 case R_X86_64_SIZE32:
454 /* Set to symbol size plus addend. */
455 value = sym->st_size;
456# endif
457 /* Fall through. */
458 case R_X86_64_32:
459 value += reloc->r_addend;
460 *(unsigned int *) reloc_addr = value;
461
462 const char *fmt;
463 if (__glibc_unlikely (value > UINT_MAX))
464 {
465 const char *strtab;
466
467 fmt = "\
468%s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n";
469# ifndef RESOLVE_CONFLICT_FIND_MAP
470 print_err:
471# endif
472 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
473
474 _dl_error_printf (fmt, RTLD_PROGNAME, strtab + refsym->st_name);
475 }
476 break;
477# ifndef RESOLVE_CONFLICT_FIND_MAP
478 /* Not needed for dl-conflict.c. */
479 case R_X86_64_PC32:
480 value += reloc->r_addend - (ElfW(Addr)) reloc_addr;
481 *(unsigned int *) reloc_addr = value;
482 if (__glibc_unlikely (value != (int) value))
483 {
484 fmt = "\
485%s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n";
486 goto print_err;
487 }
488 break;
489 case R_X86_64_COPY:
490 if (sym == NULL)
491 /* This can happen in trace mode if an object could not be
492 found. */
493 break;
494 memcpy (reloc_addr_arg, (void *) value,
495 MIN (sym->st_size, refsym->st_size));
496 if (__glibc_unlikely (sym->st_size > refsym->st_size)
497 || (__glibc_unlikely (sym->st_size < refsym->st_size)
498 && GLRO(dl_verbose)))
499 {
500 fmt = "\
501%s: Symbol `%s' has different size in shared object, consider re-linking\n";
502 goto print_err;
503 }
504 break;
505# endif
506 case R_X86_64_IRELATIVE:
507 value = map->l_addr + reloc->r_addend;
508 if (__glibc_likely (!skip_ifunc))
509 value = ((ElfW(Addr) (*) (void)) value) ();
510 *reloc_addr = value;
511 break;
512 default:
513 _dl_reloc_bad_type (map, r_type, 0);
514 break;
515# endif
516 }
517 }
518}
519
520static inline void
521__attribute ((always_inline))
522elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
523 void *const reloc_addr_arg)
524{
525 ElfW(Addr) *const reloc_addr = reloc_addr_arg;
526#if !defined RTLD_BOOTSTRAP
527 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
528 relocation updates the whole 64-bit entry. */
529 if (__glibc_unlikely (ELFW(R_TYPE) (reloc->r_info) == R_X86_64_RELATIVE64))
530 *(Elf64_Addr *) reloc_addr = (Elf64_Addr) l_addr + reloc->r_addend;
531 else
532#endif
533 {
534 assert (ELFW(R_TYPE) (reloc->r_info) == R_X86_64_RELATIVE);
535 *reloc_addr = l_addr + reloc->r_addend;
536 }
537}
538
539static inline void
540__attribute ((always_inline))
541elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
542 ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
543 int skip_ifunc)
544{
545 ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
546 const unsigned long int r_type = ELFW(R_TYPE) (reloc->r_info);
547
548 /* Check for unexpected PLT reloc type. */
549 if (__glibc_likely (r_type == R_X86_64_JUMP_SLOT))
550 {
551 /* Prelink has been deprecated. */
552 if (__glibc_likely (map->l_mach.plt == 0))
553 *reloc_addr += l_addr;
554 else
555 *reloc_addr =
556 map->l_mach.plt
557 + (((ElfW(Addr)) reloc_addr) - map->l_mach.gotplt) * 2;
558 }
559 else if (__glibc_likely (r_type == R_X86_64_TLSDESC))
560 {
561 const Elf_Symndx symndx = ELFW (R_SYM) (reloc->r_info);
562 const ElfW (Sym) *symtab = (const void *)D_PTR (map, l_info[DT_SYMTAB]);
563 const ElfW (Sym) *sym = &symtab[symndx];
564 const struct r_found_version *version = NULL;
565
566 if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
567 {
568 const ElfW (Half) *vernum =
569 (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
570 version = &map->l_versions[vernum[symndx] & 0x7fff];
571 }
572
573 /* Always initialize TLS descriptors completely at load time, in
574 case static TLS is allocated for it that requires locking. */
575 elf_machine_rela (map, scope, reloc, sym, version, reloc_addr, skip_ifunc);
576 }
577 else if (__glibc_unlikely (r_type == R_X86_64_IRELATIVE))
578 {
579 ElfW(Addr) value = map->l_addr + reloc->r_addend;
580 if (__glibc_likely (!skip_ifunc))
581 value = ((ElfW(Addr) (*) (void)) value) ();
582 *reloc_addr = value;
583 }
584 else
585 _dl_reloc_bad_type (map, r_type, 1);
586}
587
588#endif /* RESOLVE_MAP */
589