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