1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Andreas Jaeger <aj@suse.de>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
23 #define ELF_MACHINE_NAME "x86_64"
25 #include <sys/param.h>
28 #include <dl-tlsdesc.h>
29 #include <cpu-features.c>
31 /* Return nonzero iff ELF header is compatible with the running host. */
32 static inline int __attribute__ ((unused
))
33 elf_machine_matches_host (const ElfW(Ehdr
) *ehdr
)
35 return ehdr
->e_machine
== EM_X86_64
;
39 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
40 first element of the GOT. This must be inlined in a function which
42 static inline ElfW(Addr
) __attribute__ ((unused
))
43 elf_machine_dynamic (void)
45 /* This produces an IP-relative reloc which is resolved at link time. */
46 extern const ElfW(Addr
) _GLOBAL_OFFSET_TABLE_
[] attribute_hidden
;
47 return _GLOBAL_OFFSET_TABLE_
[0];
51 /* Return the run-time load address of the shared object. */
52 static inline ElfW(Addr
) __attribute__ ((unused
))
53 elf_machine_load_address (void)
55 /* Compute the difference between the runtime address of _DYNAMIC as seen
56 by an IP-relative reference, and the link-time address found in the
57 special unrelocated first GOT entry. */
58 extern ElfW(Dyn
) _DYNAMIC
[] attribute_hidden
;
59 return (ElfW(Addr
)) &_DYNAMIC
- elf_machine_dynamic ();
62 /* Set up the loaded object described by L so its unrelocated PLT
63 entries will jump to the on-demand fixup code in dl-runtime.c. */
65 static inline int __attribute__ ((unused
, always_inline
))
66 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
69 extern void _dl_runtime_resolve_sse (ElfW(Word
)) attribute_hidden
;
70 extern void _dl_runtime_resolve_avx (ElfW(Word
)) attribute_hidden
;
71 extern void _dl_runtime_resolve_avx_slow (ElfW(Word
)) attribute_hidden
;
72 extern void _dl_runtime_resolve_avx_opt (ElfW(Word
)) attribute_hidden
;
73 extern void _dl_runtime_resolve_avx512 (ElfW(Word
)) attribute_hidden
;
74 extern void _dl_runtime_resolve_avx512_opt (ElfW(Word
)) attribute_hidden
;
75 extern void _dl_runtime_profile_sse (ElfW(Word
)) attribute_hidden
;
76 extern void _dl_runtime_profile_avx (ElfW(Word
)) attribute_hidden
;
77 extern void _dl_runtime_profile_avx512 (ElfW(Word
)) attribute_hidden
;
79 if (l
->l_info
[DT_JMPREL
] && lazy
)
81 /* The GOT entries for functions in the PLT have not yet been filled
82 in. Their initial contents will arrange when called to push an
83 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
84 and then jump to _GLOBAL_OFFSET_TABLE_[2]. */
85 got
= (Elf64_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
86 /* If a library is prelinked but we have to relocate anyway,
87 we have to be able to undo the prelinking of .got.plt.
88 The prelinker saved us here address of .plt + 0x16. */
91 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
92 l
->l_mach
.gotplt
= (ElfW(Addr
)) &got
[3];
94 /* Identify this shared object. */
95 *(ElfW(Addr
) *) (got
+ 1) = (ElfW(Addr
)) l
;
97 /* The got[2] entry contains the address of a function which gets
98 called to get the address of a so far unresolved function and
99 jump to it. The profiling extension of the dynamic linker allows
100 to intercept the calls to collect information. In this case we
101 don't store the address in the GOT so that all future calls also
102 end in this function. */
103 if (__glibc_unlikely (profile
))
105 if (HAS_ARCH_FEATURE (AVX512F_Usable
))
106 *(ElfW(Addr
) *) (got
+ 2) = (ElfW(Addr
)) &_dl_runtime_profile_avx512
;
107 else if (HAS_ARCH_FEATURE (AVX_Usable
))
108 *(ElfW(Addr
) *) (got
+ 2) = (ElfW(Addr
)) &_dl_runtime_profile_avx
;
110 *(ElfW(Addr
) *) (got
+ 2) = (ElfW(Addr
)) &_dl_runtime_profile_sse
;
112 if (GLRO(dl_profile
) != NULL
113 && _dl_name_match_p (GLRO(dl_profile
), l
))
114 /* This is the object we are looking for. Say that we really
115 want profiling and the timers are started. */
116 GL(dl_profile_map
) = l
;
120 /* This function will get called to fix up the GOT entry
121 indicated by the offset on the stack, and then jump to
122 the resolved address. */
123 if (HAS_ARCH_FEATURE (AVX512F_Usable
))
125 if (HAS_ARCH_FEATURE (Use_dl_runtime_resolve_opt
))
126 *(ElfW(Addr
) *) (got
+ 2)
127 = (ElfW(Addr
)) &_dl_runtime_resolve_avx512_opt
;
129 *(ElfW(Addr
) *) (got
+ 2)
130 = (ElfW(Addr
)) &_dl_runtime_resolve_avx512
;
132 else if (HAS_ARCH_FEATURE (AVX_Usable
))
134 if (HAS_ARCH_FEATURE (Use_dl_runtime_resolve_opt
))
135 *(ElfW(Addr
) *) (got
+ 2)
136 = (ElfW(Addr
)) &_dl_runtime_resolve_avx_opt
;
137 else if (HAS_ARCH_FEATURE (Use_dl_runtime_resolve_slow
))
138 *(ElfW(Addr
) *) (got
+ 2)
139 = (ElfW(Addr
)) &_dl_runtime_resolve_avx_slow
;
141 *(ElfW(Addr
) *) (got
+ 2)
142 = (ElfW(Addr
)) &_dl_runtime_resolve_avx
;
145 *(ElfW(Addr
) *) (got
+ 2) = (ElfW(Addr
)) &_dl_runtime_resolve_sse
;
149 if (l
->l_info
[ADDRIDX (DT_TLSDESC_GOT
)] && lazy
)
150 *(ElfW(Addr
)*)(D_PTR (l
, l_info
[ADDRIDX (DT_TLSDESC_GOT
)]) + l
->l_addr
)
151 = (ElfW(Addr
)) &_dl_tlsdesc_resolve_rela
;
156 /* Initial entry point code for the dynamic linker.
157 The C function `_dl_start' is the real entry point;
158 its return value is the user program's entry point. */
159 #define RTLD_START asm ("\n\
163 .globl _dl_start_user\n\
168 # Save the user entry point address in %r12.\n\
170 # See if we were run as a command with the executable file\n\
171 # name as an extra leading argument.\n\
172 movl _dl_skip_args(%rip), %eax\n\
173 # Pop the original argument count.\n\
175 # Adjust the stack pointer to skip _dl_skip_args words.\n\
176 leaq (%rsp,%rax,8), %rsp\n\
177 # Subtract _dl_skip_args from argc.\n\
179 # Push argc back on the stack.\n\
181 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
184 # Save %rsp value in %r13.\n\
186 # And align stack for the _dl_init call. \n\
188 # _dl_loaded -> rdi\n\
189 movq _rtld_local(%rip), %rdi\n\
191 leaq 16(%r13,%rdx,8), %rcx\n\
193 leaq 8(%r13), %rdx\n\
194 # Clear %rbp to mark outermost frame obviously even for constructors.\n\
196 # Call the function to run the initializers.\n\
198 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
199 leaq _dl_fini(%rip), %rdx\n\
200 # And make sure %rsp points to argc stored on the stack.\n\
202 # Jump to the user's entry point.\n\
207 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
208 TLS variable, so undefined references should not be allowed to
210 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
211 of the main executable's symbols, as for a COPY reloc.
212 ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA iff TYPE describes relocation may
213 against protected data whose address be external due to copy relocation.
215 #define elf_machine_type_class(type) \
216 ((((type) == R_X86_64_JUMP_SLOT \
217 || (type) == R_X86_64_DTPMOD64 \
218 || (type) == R_X86_64_DTPOFF64 \
219 || (type) == R_X86_64_TPOFF64 \
220 || (type) == R_X86_64_TLSDESC) \
221 * ELF_RTYPE_CLASS_PLT) \
222 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY) \
223 | (((type) == R_X86_64_GLOB_DAT) * ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA))
225 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
226 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
228 /* The relative ifunc relocation. */
229 // XXX This is a work-around for a broken linker. Remove!
230 #define ELF_MACHINE_IRELATIVE R_X86_64_IRELATIVE
232 /* The x86-64 never uses Elf64_Rel/Elf32_Rel relocations. */
233 #define ELF_MACHINE_NO_REL 1
234 #define ELF_MACHINE_NO_RELA 0
236 /* We define an initialization function. This is called very early in
238 #define DL_PLATFORM_INIT dl_platform_init ()
240 static inline void __attribute__ ((unused
))
241 dl_platform_init (void)
244 /* init_cpu_features has been called early from __libc_start_main in
245 static executable. */
246 init_cpu_features (&GLRO(dl_x86_cpu_features
));
248 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
249 /* Avoid an empty string which would disturb us. */
250 GLRO(dl_platform
) = NULL
;
254 static inline ElfW(Addr
)
255 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
256 const ElfW(Sym
) *refsym
, const ElfW(Sym
) *sym
,
257 const ElfW(Rela
) *reloc
,
258 ElfW(Addr
) *reloc_addr
, ElfW(Addr
) value
)
260 return *reloc_addr
= value
;
263 /* Return the final value of a PLT relocation. On x86-64 the
264 JUMP_SLOT relocation ignores the addend. */
265 static inline ElfW(Addr
)
266 elf_machine_plt_value (struct link_map
*map
, const ElfW(Rela
) *reloc
,
273 /* Names of the architecture-specific auditing callback functions. */
274 #define ARCH_LA_PLTENTER x86_64_gnu_pltenter
275 #define ARCH_LA_PLTEXIT x86_64_gnu_pltexit
277 #endif /* !dl_machine_h */
281 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
282 MAP is the object containing the reloc. */
285 __attribute__ ((always_inline
))
286 elf_machine_rela (struct link_map
*map
, const ElfW(Rela
) *reloc
,
287 const ElfW(Sym
) *sym
, const struct r_found_version
*version
,
288 void *const reloc_addr_arg
, int skip_ifunc
)
290 ElfW(Addr
) *const reloc_addr
= reloc_addr_arg
;
291 const unsigned long int r_type
= ELFW(R_TYPE
) (reloc
->r_info
);
293 # if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
294 if (__glibc_unlikely (r_type
== R_X86_64_RELATIVE
))
296 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
297 /* This is defined in rtld.c, but nowhere in the static libc.a;
298 make the reference weak so static programs can still link.
299 This declaration cannot be done when compiling rtld.c
300 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
301 common defn for _dl_rtld_map, which is incompatible with a
302 weak decl in the same file. */
304 weak_extern (GL(dl_rtld_map
));
306 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
308 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
312 # if !defined RTLD_BOOTSTRAP
313 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
314 relocation updates the whole 64-bit entry. */
315 if (__glibc_unlikely (r_type
== R_X86_64_RELATIVE64
))
316 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) map
->l_addr
+ reloc
->r_addend
;
319 if (__glibc_unlikely (r_type
== R_X86_64_NONE
))
323 # ifndef RTLD_BOOTSTRAP
324 const ElfW(Sym
) *const refsym
= sym
;
326 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
327 ElfW(Addr
) value
= (sym
== NULL
? 0
328 : (ElfW(Addr
)) sym_map
->l_addr
+ sym
->st_value
);
331 && __builtin_expect (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
,
333 && __builtin_expect (sym
->st_shndx
!= SHN_UNDEF
, 1)
334 && __builtin_expect (!skip_ifunc
, 1))
336 # ifndef RTLD_BOOTSTRAP
338 && sym_map
->l_type
!= lt_executable
339 && !sym_map
->l_relocated
)
342 = (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
344 %s: Relink `%s' with `%s' for IFUNC symbol `%s'\n",
345 RTLD_PROGNAME
, map
->l_name
,
347 strtab
+ refsym
->st_name
);
350 value
= ((ElfW(Addr
) (*) (void)) value
) ();
355 # ifndef RTLD_BOOTSTRAP
357 case R_X86_64_SIZE64
:
358 /* Set to symbol size plus addend. */
359 *(Elf64_Addr
*) (uintptr_t) reloc_addr
360 = (Elf64_Addr
) sym
->st_size
+ reloc
->r_addend
;
363 case R_X86_64_SIZE32
:
365 case R_X86_64_SIZE64
:
367 /* Set to symbol size plus addend. */
368 value
= sym
->st_size
;
370 case R_X86_64_GLOB_DAT
:
371 case R_X86_64_JUMP_SLOT
:
372 *reloc_addr
= value
+ reloc
->r_addend
;
375 # ifndef RESOLVE_CONFLICT_FIND_MAP
376 case R_X86_64_DTPMOD64
:
377 # ifdef RTLD_BOOTSTRAP
378 /* During startup the dynamic linker is always the module
380 XXX If this relocation is necessary move before RESOLVE
384 /* Get the information from the link map returned by the
387 *reloc_addr
= sym_map
->l_tls_modid
;
390 case R_X86_64_DTPOFF64
:
391 # ifndef RTLD_BOOTSTRAP
392 /* During relocation all TLS symbols are defined and used.
393 Therefore the offset is already correct. */
396 value
= sym
->st_value
+ reloc
->r_addend
;
398 /* This relocation type computes a signed offset that is
399 usually negative. The symbol and addend values are 32
400 bits but the GOT entry is 64 bits wide and the whole
401 64-bit entry is used as a signed quantity, so we need
402 to sign-extend the computed value to 64 bits. */
403 *(Elf64_Sxword
*) reloc_addr
= (Elf64_Sxword
) (Elf32_Sword
) value
;
410 case R_X86_64_TLSDESC
:
412 struct tlsdesc
volatile *td
=
413 (struct tlsdesc
volatile *)reloc_addr
;
415 # ifndef RTLD_BOOTSTRAP
418 td
->arg
= (void*)reloc
->r_addend
;
419 td
->entry
= _dl_tlsdesc_undefweak
;
424 # ifndef RTLD_BOOTSTRAP
426 CHECK_STATIC_TLS (map
, sym_map
);
428 if (!TRY_STATIC_TLS (map
, sym_map
))
430 td
->arg
= _dl_make_tlsdesc_dynamic
431 (sym_map
, sym
->st_value
+ reloc
->r_addend
);
432 td
->entry
= _dl_tlsdesc_dynamic
;
438 td
->arg
= (void*)(sym
->st_value
- sym_map
->l_tls_offset
440 td
->entry
= _dl_tlsdesc_return
;
445 case R_X86_64_TPOFF64
:
446 /* The offset is negative, forward from the thread pointer. */
447 # ifndef RTLD_BOOTSTRAP
451 # ifndef RTLD_BOOTSTRAP
452 CHECK_STATIC_TLS (map
, sym_map
);
454 /* We know the offset of the object the symbol is contained in.
455 It is a negative value which will be added to the
457 value
= (sym
->st_value
+ reloc
->r_addend
458 - sym_map
->l_tls_offset
);
460 /* The symbol and addend values are 32 bits but the GOT
461 entry is 64 bits wide and the whole 64-bit entry is used
462 as a signed quantity, so we need to sign-extend the
463 computed value to 64 bits. */
464 *(Elf64_Sxword
*) reloc_addr
= (Elf64_Sxword
) (Elf32_Sword
) value
;
472 # ifndef RTLD_BOOTSTRAP
474 /* value + r_addend may be > 0xffffffff and R_X86_64_64
475 relocation updates the whole 64-bit entry. */
476 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) value
+ reloc
->r_addend
;
479 case R_X86_64_SIZE32
:
480 /* Set to symbol size plus addend. */
481 value
= sym
->st_size
;
484 value
+= reloc
->r_addend
;
485 *(unsigned int *) reloc_addr
= value
;
488 if (__glibc_unlikely (value
> UINT_MAX
))
493 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n";
494 # ifndef RESOLVE_CONFLICT_FIND_MAP
497 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
499 _dl_error_printf (fmt
, RTLD_PROGNAME
, strtab
+ refsym
->st_name
);
502 # ifndef RESOLVE_CONFLICT_FIND_MAP
503 /* Not needed for dl-conflict.c. */
505 value
+= reloc
->r_addend
- (ElfW(Addr
)) reloc_addr
;
506 *(unsigned int *) reloc_addr
= value
;
507 if (__glibc_unlikely (value
!= (int) value
))
510 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n";
516 /* This can happen in trace mode if an object could not be
519 memcpy (reloc_addr_arg
, (void *) value
,
520 MIN (sym
->st_size
, refsym
->st_size
));
521 if (__builtin_expect (sym
->st_size
> refsym
->st_size
, 0)
522 || (__builtin_expect (sym
->st_size
< refsym
->st_size
, 0)
523 && GLRO(dl_verbose
)))
526 %s: Symbol `%s' has different size in shared object, consider re-linking\n";
531 case R_X86_64_IRELATIVE
:
532 value
= map
->l_addr
+ reloc
->r_addend
;
533 value
= ((ElfW(Addr
) (*) (void)) value
) ();
537 _dl_reloc_bad_type (map
, r_type
, 0);
545 __attribute ((always_inline
))
546 elf_machine_rela_relative (ElfW(Addr
) l_addr
, const ElfW(Rela
) *reloc
,
547 void *const reloc_addr_arg
)
549 ElfW(Addr
) *const reloc_addr
= reloc_addr_arg
;
550 #if !defined RTLD_BOOTSTRAP
551 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
552 relocation updates the whole 64-bit entry. */
553 if (__glibc_unlikely (ELFW(R_TYPE
) (reloc
->r_info
) == R_X86_64_RELATIVE64
))
554 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) l_addr
+ reloc
->r_addend
;
558 assert (ELFW(R_TYPE
) (reloc
->r_info
) == R_X86_64_RELATIVE
);
559 *reloc_addr
= l_addr
+ reloc
->r_addend
;
564 __attribute ((always_inline
))
565 elf_machine_lazy_rel (struct link_map
*map
,
566 ElfW(Addr
) l_addr
, const ElfW(Rela
) *reloc
,
569 ElfW(Addr
) *const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
570 const unsigned long int r_type
= ELFW(R_TYPE
) (reloc
->r_info
);
572 /* Check for unexpected PLT reloc type. */
573 if (__glibc_likely (r_type
== R_X86_64_JUMP_SLOT
))
575 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
576 *reloc_addr
+= l_addr
;
580 + (((ElfW(Addr
)) reloc_addr
) - map
->l_mach
.gotplt
) * 2;
582 else if (__glibc_likely (r_type
== R_X86_64_TLSDESC
))
584 struct tlsdesc
volatile * __attribute__((__unused__
)) td
=
585 (struct tlsdesc
volatile *)reloc_addr
;
587 td
->arg
= (void*)reloc
;
588 td
->entry
= (void*)(D_PTR (map
, l_info
[ADDRIDX (DT_TLSDESC_PLT
)])
591 else if (__glibc_unlikely (r_type
== R_X86_64_IRELATIVE
))
593 ElfW(Addr
) value
= map
->l_addr
+ reloc
->r_addend
;
594 if (__glibc_likely (!skip_ifunc
))
595 value
= ((ElfW(Addr
) (*) (void)) value
) ();
599 _dl_reloc_bad_type (map
, r_type
, 1);
602 #endif /* RESOLVE_MAP */