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), map: 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 *reloc_addr = value + reloc->r_addend;
343 break;
344# endif
345
346 case R_X86_64_GLOB_DAT:
347 case R_X86_64_JUMP_SLOT:
348 *reloc_addr = value;
349 break;
350
351# ifndef RESOLVE_CONFLICT_FIND_MAP
352 case R_X86_64_DTPMOD64:
353# ifdef RTLD_BOOTSTRAP
354 /* During startup the dynamic linker is always the module
355 with index 1.
356 XXX If this relocation is necessary move before RESOLVE
357 call. */
358 *reloc_addr = 1;
359# else
360 /* Get the information from the link map returned by the
361 resolve function. */
362 if (sym_map != NULL)
363 *reloc_addr = sym_map->l_tls_modid;
364# endif
365 break;
366 case R_X86_64_DTPOFF64:
367# ifndef RTLD_BOOTSTRAP
368 /* During relocation all TLS symbols are defined and used.
369 Therefore the offset is already correct. */
370 if (sym != NULL)
371 {
372 value = sym->st_value + reloc->r_addend;
373# ifdef __ILP32__
374 /* This relocation type computes a signed offset that is
375 usually negative. The symbol and addend values are 32
376 bits but the GOT entry is 64 bits wide and the whole
377 64-bit entry is used as a signed quantity, so we need
378 to sign-extend the computed value to 64 bits. */
379 *(Elf64_Sxword *) reloc_addr = (Elf64_Sxword) (Elf32_Sword) value;
380# else
381 *reloc_addr = value;
382# endif
383 }
384# endif
385 break;
386 case R_X86_64_TLSDESC:
387 {
388 struct tlsdesc volatile *td =
389 (struct tlsdesc volatile *)reloc_addr;
390
391# ifndef RTLD_BOOTSTRAP
392 if (! sym)
393 {
394 td->arg = (void*)reloc->r_addend;
395 td->entry = _dl_tlsdesc_undefweak;
396 }
397 else
398# endif
399 {
400# ifndef RTLD_BOOTSTRAP
401# ifndef SHARED
402 CHECK_STATIC_TLS (map, sym_map);
403# else
404 if (!TRY_STATIC_TLS (map, sym_map))
405 {
406 td->arg = _dl_make_tlsdesc_dynamic
407 (sym_map, sym->st_value + reloc->r_addend);
408 td->entry = _dl_tlsdesc_dynamic;
409 }
410 else
411# endif
412# endif
413 {
414 td->arg = (void*)(sym->st_value - sym_map->l_tls_offset
415 + reloc->r_addend);
416 td->entry = _dl_tlsdesc_return;
417 }
418 }
419 break;
420 }
421 case R_X86_64_TPOFF64:
422 /* The offset is negative, forward from the thread pointer. */
423# ifndef RTLD_BOOTSTRAP
424 if (sym != NULL)
425# endif
426 {
427# ifndef RTLD_BOOTSTRAP
428 CHECK_STATIC_TLS (map, sym_map);
429# endif
430 /* We know the offset of the object the symbol is contained in.
431 It is a negative value which will be added to the
432 thread pointer. */
433 value = (sym->st_value + reloc->r_addend
434 - sym_map->l_tls_offset);
435# ifdef __ILP32__
436 /* The symbol and addend values are 32 bits but the GOT
437 entry is 64 bits wide and the whole 64-bit entry is used
438 as a signed quantity, so we need to sign-extend the
439 computed value to 64 bits. */
440 *(Elf64_Sxword *) reloc_addr = (Elf64_Sxword) (Elf32_Sword) value;
441# else
442 *reloc_addr = value;
443# endif
444 }
445 break;
446# endif
447
448# ifndef RTLD_BOOTSTRAP
449 case R_X86_64_64:
450 /* value + r_addend may be > 0xffffffff and R_X86_64_64
451 relocation updates the whole 64-bit entry. */
452 *(Elf64_Addr *) reloc_addr = (Elf64_Addr) value + reloc->r_addend;
453 break;
454# ifndef __ILP32__
455 case R_X86_64_SIZE32:
456 /* Set to symbol size plus addend. */
457 value = sym->st_size;
458# endif
459 /* Fall through. */
460 case R_X86_64_32:
461 value += reloc->r_addend;
462 *(unsigned int *) reloc_addr = value;
463
464 const char *fmt;
465 if (__glibc_unlikely (value > UINT_MAX))
466 {
467 const char *strtab;
468
469 fmt = "\
470%s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n";
471# ifndef RESOLVE_CONFLICT_FIND_MAP
472 print_err:
473# endif
474 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
475
476 _dl_error_printf (fmt, RTLD_PROGNAME, strtab + refsym->st_name);
477 }
478 break;
479# ifndef RESOLVE_CONFLICT_FIND_MAP
480 /* Not needed for dl-conflict.c. */
481 case R_X86_64_PC32:
482 value += reloc->r_addend - (ElfW(Addr)) reloc_addr;
483 *(unsigned int *) reloc_addr = value;
484 if (__glibc_unlikely (value != (int) value))
485 {
486 fmt = "\
487%s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n";
488 goto print_err;
489 }
490 break;
491 case R_X86_64_COPY:
492 if (sym == NULL)
493 /* This can happen in trace mode if an object could not be
494 found. */
495 break;
496 memcpy (reloc_addr_arg, (void *) value,
497 MIN (sym->st_size, refsym->st_size));
498 if (__glibc_unlikely (sym->st_size > refsym->st_size)
499 || (__glibc_unlikely (sym->st_size < refsym->st_size)
500 && GLRO(dl_verbose)))
501 {
502 fmt = "\
503%s: Symbol `%s' has different size in shared object, consider re-linking\n";
504 goto print_err;
505 }
506 break;
507# endif
508 case R_X86_64_IRELATIVE:
509 value = map->l_addr + reloc->r_addend;
510 if (__glibc_likely (!skip_ifunc))
511 value = ((ElfW(Addr) (*) (void)) value) ();
512 *reloc_addr = value;
513 break;
514 default:
515 _dl_reloc_bad_type (map, r_type, 0);
516 break;
517# endif
518 }
519 }
520}
521
522static inline void
523__attribute ((always_inline))
524elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
525 void *const reloc_addr_arg)
526{
527 ElfW(Addr) *const reloc_addr = reloc_addr_arg;
528#if !defined RTLD_BOOTSTRAP
529 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
530 relocation updates the whole 64-bit entry. */
531 if (__glibc_unlikely (ELFW(R_TYPE) (reloc->r_info) == R_X86_64_RELATIVE64))
532 *(Elf64_Addr *) reloc_addr = (Elf64_Addr) l_addr + reloc->r_addend;
533 else
534#endif
535 {
536 assert (ELFW(R_TYPE) (reloc->r_info) == R_X86_64_RELATIVE);
537 *reloc_addr = l_addr + reloc->r_addend;
538 }
539}
540
541static inline void
542__attribute ((always_inline))
543elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
544 ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
545 int skip_ifunc)
546{
547 ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
548 const unsigned long int r_type = ELFW(R_TYPE) (reloc->r_info);
549
550 /* Check for unexpected PLT reloc type. */
551 if (__glibc_likely (r_type == R_X86_64_JUMP_SLOT))
552 {
553 /* Prelink has been deprecated. */
554 if (__glibc_likely (map->l_mach.plt == 0))
555 *reloc_addr += l_addr;
556 else
557 *reloc_addr =
558 map->l_mach.plt
559 + (((ElfW(Addr)) reloc_addr) - map->l_mach.gotplt) * 2;
560 }
561 else if (__glibc_likely (r_type == R_X86_64_TLSDESC))
562 {
563 const Elf_Symndx symndx = ELFW (R_SYM) (reloc->r_info);
564 const ElfW (Sym) *symtab = (const void *)D_PTR (map, l_info[DT_SYMTAB]);
565 const ElfW (Sym) *sym = &symtab[symndx];
566 const struct r_found_version *version = NULL;
567
568 if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
569 {
570 const ElfW (Half) *vernum =
571 (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
572 version = &map->l_versions[vernum[symndx] & 0x7fff];
573 }
574
575 /* Always initialize TLS descriptors completely at load time, in
576 case static TLS is allocated for it that requires locking. */
577 elf_machine_rela (map, scope, reloc, sym, version, reloc_addr, skip_ifunc);
578 }
579 else if (__glibc_unlikely (r_type == R_X86_64_IRELATIVE))
580 {
581 ElfW(Addr) value = map->l_addr + reloc->r_addend;
582 if (__glibc_likely (!skip_ifunc))
583 value = ((ElfW(Addr) (*) (void)) value) ();
584 *reloc_addr = value;
585 }
586 else
587 _dl_reloc_bad_type (map, r_type, 1);
588}
589
590#endif /* RESOLVE_MAP */
591

source code of glibc/sysdeps/x86_64/dl-machine.h