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.
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 "x86_64"
25 #include <sys/param.h>
28 #include <dl-tlsdesc.h>
29 #include <dl-static-tls.h>
30 #include <dl-machine-rel.h>
32 /* Return nonzero iff ELF header is compatible with the running host. */
33 static inline int __attribute__ ((unused
))
34 elf_machine_matches_host (const ElfW(Ehdr
) *ehdr
)
36 return ehdr
->e_machine
== EM_X86_64
;
40 /* Return the run-time load address of the shared object. */
41 static inline ElfW(Addr
) __attribute__ ((unused
))
42 elf_machine_load_address (void)
44 extern const ElfW(Ehdr
) __ehdr_start attribute_hidden
;
45 return (ElfW(Addr
)) &__ehdr_start
;
48 /* Return the link-time address of _DYNAMIC. */
49 static inline ElfW(Addr
) __attribute__ ((unused
))
50 elf_machine_dynamic (void)
52 extern ElfW(Dyn
) _DYNAMIC
[] attribute_hidden
;
53 return (ElfW(Addr
)) _DYNAMIC
- elf_machine_load_address ();
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. */
59 static inline int __attribute__ ((unused
, always_inline
))
60 elf_machine_runtime_setup (struct link_map
*l
, struct r_scope_elem
*scope
[],
61 int lazy
, int profile
)
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
;
71 if (l
->l_info
[DT_JMPREL
] && lazy
)
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. */
83 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
84 l
->l_mach
.gotplt
= (ElfW(Addr
)) &got
[3];
86 /* Identify this shared object. */
87 *(ElfW(Addr
) *) (got
+ 1) = (ElfW(Addr
)) l
;
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
))
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
;
102 *(ElfW(Addr
) *) (got
+ 2) = (ElfW(Addr
)) &_dl_runtime_profile_sse
;
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
;
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
);
121 *(ElfW(Addr
) *) (got
+ 2)
122 = (ElfW(Addr
)) &_dl_runtime_resolve_fxsave
;
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\
136 .globl _dl_start_user\n\
141 # Save the user entry point address in %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\
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\
152 # Push argc back on the stack.\n\
154 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
157 # Save %rsp value in %r13.\n\
159 # And align stack for the _dl_init call. \n\
161 # _dl_loaded -> rdi\n\
162 movq _rtld_local(%rip), %rdi\n\
164 leaq 16(%r13,%rdx,8), %rcx\n\
166 leaq 8(%r13), %rdx\n\
167 # Clear %rbp to mark outermost frame obviously even for constructors.\n\
169 # Call the function to run the initializers.\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\
175 # Jump to the user's entry point.\n\
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
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.
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))
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
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
205 /* We define an initialization function. This is called very early in
207 #define DL_PLATFORM_INIT dl_platform_init ()
209 static inline void __attribute__ ((unused
))
210 dl_platform_init (void)
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 ();
217 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
218 /* Avoid an empty string which would disturb us. */
219 GLRO(dl_platform
) = NULL
;
223 static inline ElfW(Addr
)
224 elf_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
)
229 return *reloc_addr
= value
;
232 /* Return the final value of a PLT relocation. On x86-64 the
233 JUMP_SLOT relocation ignores the addend. */
234 static inline ElfW(Addr
)
235 elf_machine_plt_value (struct link_map
*map
, const ElfW(Rela
) *reloc
,
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
246 #endif /* !dl_machine_h */
250 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
251 MAP is the object containing the reloc. */
253 static inline void __attribute__((always_inline
))
254 elf_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
);
261 # if !defined RTLD_BOOTSTRAP
262 if (__glibc_unlikely (r_type
== R_X86_64_RELATIVE
))
263 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
266 # if !defined RTLD_BOOTSTRAP
267 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
268 relocation updates the whole 64-bit entry. */
269 if (__glibc_unlikely (r_type
== R_X86_64_RELATIVE64
))
270 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) map
->l_addr
+ reloc
->r_addend
;
273 if (__glibc_unlikely (r_type
== R_X86_64_NONE
))
277 # ifndef RTLD_BOOTSTRAP
278 const ElfW(Sym
) *const refsym
= sym
;
280 struct link_map
*sym_map
= RESOLVE_MAP (map
, scope
, &sym
, version
,
282 ElfW(Addr
) value
= SYMBOL_ADDRESS (sym_map
, sym
, true);
285 && __glibc_unlikely (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
)
286 && __glibc_likely (sym
->st_shndx
!= SHN_UNDEF
)
287 && __glibc_likely (!skip_ifunc
))
289 # ifndef RTLD_BOOTSTRAP
291 && !sym_map
->l_relocated
)
294 = (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
295 if (sym_map
->l_type
== lt_executable
)
297 %s: IFUNC symbol '%s' referenced in '%s' is defined in the executable \
298 and creates an unsatisfiable circular dependency.\n",
299 RTLD_PROGNAME
, strtab
+ refsym
->st_name
,
303 %s: Relink `%s' with `%s' for IFUNC symbol `%s'\n",
304 RTLD_PROGNAME
, map
->l_name
,
306 strtab
+ refsym
->st_name
);
309 value
= ((ElfW(Addr
) (*) (void)) value
) ();
314 # ifndef RTLD_BOOTSTRAP
316 case R_X86_64_SIZE64
:
317 /* Set to symbol size plus addend. */
318 *(Elf64_Addr
*) (uintptr_t) reloc_addr
319 = (Elf64_Addr
) sym
->st_size
+ reloc
->r_addend
;
322 case R_X86_64_SIZE32
:
324 case R_X86_64_SIZE64
:
326 /* Set to symbol size plus addend. */
327 value
= sym
->st_size
;
330 case R_X86_64_GLOB_DAT
:
331 case R_X86_64_JUMP_SLOT
:
332 *reloc_addr
= value
+ reloc
->r_addend
;
335 case R_X86_64_DTPMOD64
:
336 # ifdef RTLD_BOOTSTRAP
337 /* During startup the dynamic linker is always the module
339 XXX If this relocation is necessary move before RESOLVE
343 /* Get the information from the link map returned by the
346 *reloc_addr
= sym_map
->l_tls_modid
;
349 case R_X86_64_DTPOFF64
:
350 # ifndef RTLD_BOOTSTRAP
351 /* During relocation all TLS symbols are defined and used.
352 Therefore the offset is already correct. */
355 value
= sym
->st_value
+ reloc
->r_addend
;
357 /* This relocation type computes a signed offset that is
358 usually negative. The symbol and addend values are 32
359 bits but the GOT entry is 64 bits wide and the whole
360 64-bit entry is used as a signed quantity, so we need
361 to sign-extend the computed value to 64 bits. */
362 *(Elf64_Sxword
*) reloc_addr
= (Elf64_Sxword
) (Elf32_Sword
) value
;
369 case R_X86_64_TLSDESC
:
371 struct tlsdesc
volatile *td
=
372 (struct tlsdesc
volatile *)reloc_addr
;
374 # ifndef RTLD_BOOTSTRAP
377 td
->arg
= (void*)reloc
->r_addend
;
378 td
->entry
= _dl_tlsdesc_undefweak
;
383 # ifndef RTLD_BOOTSTRAP
385 CHECK_STATIC_TLS (map
, sym_map
);
387 if (!TRY_STATIC_TLS (map
, sym_map
))
389 td
->arg
= _dl_make_tlsdesc_dynamic
390 (sym_map
, sym
->st_value
+ reloc
->r_addend
);
391 td
->entry
= _dl_tlsdesc_dynamic
;
397 td
->arg
= (void*)(sym
->st_value
- sym_map
->l_tls_offset
399 td
->entry
= _dl_tlsdesc_return
;
404 case R_X86_64_TPOFF64
:
405 /* The offset is negative, forward from the thread pointer. */
406 # ifndef RTLD_BOOTSTRAP
410 # ifndef RTLD_BOOTSTRAP
411 CHECK_STATIC_TLS (map
, sym_map
);
413 /* We know the offset of the object the symbol is contained in.
414 It is a negative value which will be added to the
416 value
= (sym
->st_value
+ reloc
->r_addend
417 - sym_map
->l_tls_offset
);
419 /* The symbol and addend values are 32 bits but the GOT
420 entry is 64 bits wide and the whole 64-bit entry is used
421 as a signed quantity, so we need to sign-extend the
422 computed value to 64 bits. */
423 *(Elf64_Sxword
*) reloc_addr
= (Elf64_Sxword
) (Elf32_Sword
) value
;
430 # ifndef RTLD_BOOTSTRAP
432 /* value + r_addend may be > 0xffffffff and R_X86_64_64
433 relocation updates the whole 64-bit entry. */
434 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) value
+ reloc
->r_addend
;
437 case R_X86_64_SIZE32
:
438 /* Set to symbol size plus addend. */
439 value
= sym
->st_size
;
443 value
+= reloc
->r_addend
;
444 *(unsigned int *) reloc_addr
= value
;
447 if (__glibc_unlikely (value
> UINT_MAX
))
452 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n";
454 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
456 _dl_error_printf (fmt
, RTLD_PROGNAME
, strtab
+ refsym
->st_name
);
459 /* Not needed for dl-conflict.c. */
461 value
+= reloc
->r_addend
- (ElfW(Addr
)) reloc_addr
;
462 *(unsigned int *) reloc_addr
= value
;
463 if (__glibc_unlikely (value
!= (int) value
))
466 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n";
472 /* This can happen in trace mode if an object could not be
475 memcpy (reloc_addr_arg
, (void *) value
,
476 MIN (sym
->st_size
, refsym
->st_size
));
477 if (__glibc_unlikely (sym
->st_size
> refsym
->st_size
)
478 || (__glibc_unlikely (sym
->st_size
< refsym
->st_size
)
479 && GLRO(dl_verbose
)))
482 %s: Symbol `%s' has different size in shared object, consider re-linking\n";
486 case R_X86_64_IRELATIVE
:
487 value
= map
->l_addr
+ reloc
->r_addend
;
488 if (__glibc_likely (!skip_ifunc
))
489 value
= ((ElfW(Addr
) (*) (void)) value
) ();
493 _dl_reloc_bad_type (map
, r_type
, 0);
501 __attribute ((always_inline
))
502 elf_machine_rela_relative (ElfW(Addr
) l_addr
, const ElfW(Rela
) *reloc
,
503 void *const reloc_addr_arg
)
505 ElfW(Addr
) *const reloc_addr
= reloc_addr_arg
;
506 #if !defined RTLD_BOOTSTRAP
507 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
508 relocation updates the whole 64-bit entry. */
509 if (__glibc_unlikely (ELFW(R_TYPE
) (reloc
->r_info
) == R_X86_64_RELATIVE64
))
510 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) l_addr
+ reloc
->r_addend
;
514 assert (ELFW(R_TYPE
) (reloc
->r_info
) == R_X86_64_RELATIVE
);
515 *reloc_addr
= l_addr
+ reloc
->r_addend
;
520 __attribute ((always_inline
))
521 elf_machine_lazy_rel (struct link_map
*map
, struct r_scope_elem
*scope
[],
522 ElfW(Addr
) l_addr
, const ElfW(Rela
) *reloc
,
525 ElfW(Addr
) *const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
526 const unsigned long int r_type
= ELFW(R_TYPE
) (reloc
->r_info
);
528 /* Check for unexpected PLT reloc type. */
529 if (__glibc_likely (r_type
== R_X86_64_JUMP_SLOT
))
531 /* Prelink has been deprecated. */
532 if (__glibc_likely (map
->l_mach
.plt
== 0))
533 *reloc_addr
+= l_addr
;
537 + (((ElfW(Addr
)) reloc_addr
) - map
->l_mach
.gotplt
) * 2;
539 else if (__glibc_likely (r_type
== R_X86_64_TLSDESC
))
541 const Elf_Symndx symndx
= ELFW (R_SYM
) (reloc
->r_info
);
542 const ElfW (Sym
) *symtab
= (const void *)D_PTR (map
, l_info
[DT_SYMTAB
]);
543 const ElfW (Sym
) *sym
= &symtab
[symndx
];
544 const struct r_found_version
*version
= NULL
;
546 if (map
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
548 const ElfW (Half
) *vernum
=
549 (const void *)D_PTR (map
, l_info
[VERSYMIDX (DT_VERSYM
)]);
550 version
= &map
->l_versions
[vernum
[symndx
] & 0x7fff];
553 /* Always initialize TLS descriptors completely at load time, in
554 case static TLS is allocated for it that requires locking. */
555 elf_machine_rela (map
, scope
, reloc
, sym
, version
, reloc_addr
, skip_ifunc
);
557 else if (__glibc_unlikely (r_type
== R_X86_64_IRELATIVE
))
559 ElfW(Addr
) value
= map
->l_addr
+ reloc
->r_addend
;
560 if (__glibc_likely (!skip_ifunc
))
561 value
= ((ElfW(Addr
) (*) (void)) value
) ();
565 _dl_reloc_bad_type (map
, r_type
, 1);
568 #endif /* RESOLVE_MAP */