Remove i486 subdirectory
[glibc.git] / sysdeps / x86_64 / dl-machine.h
blob32d25da01ec0b214529b2db5afdd526a9f3ed87d
1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001-2015 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/>. */
20 #ifndef dl_machine_h
21 #define dl_machine_h
23 #define ELF_MACHINE_NAME "x86_64"
25 #include <sys/param.h>
26 #include <sysdep.h>
27 #include <tls.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
41 uses global data. */
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)
68 Elf64_Addr *got;
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_avx512 (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;
76 if (l->l_info[DT_JMPREL] && lazy)
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])
88 l->l_mach.plt = got[1] + l->l_addr;
89 l->l_mach.gotplt = (ElfW(Addr)) &got[3];
91 /* Identify this shared object. */
92 *(ElfW(Addr) *) (got + 1) = (ElfW(Addr)) l;
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))
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;
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;
115 else
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 (HAS_ARCH_FEATURE (AVX512F_Usable))
121 *(ElfW(Addr) *) (got + 2) = (ElfW(Addr)) &_dl_runtime_resolve_avx512;
122 else if (HAS_ARCH_FEATURE (AVX_Usable))
123 *(ElfW(Addr) *) (got + 2) = (ElfW(Addr)) &_dl_runtime_resolve_avx;
124 else
125 *(ElfW(Addr) *) (got + 2) = (ElfW(Addr)) &_dl_runtime_resolve_sse;
129 if (l->l_info[ADDRIDX (DT_TLSDESC_GOT)] && lazy)
130 *(ElfW(Addr)*)(D_PTR (l, l_info[ADDRIDX (DT_TLSDESC_GOT)]) + l->l_addr)
131 = (ElfW(Addr)) &_dl_tlsdesc_resolve_rela;
133 return lazy;
136 /* Initial entry point code for the dynamic linker.
137 The C function `_dl_start' is the real entry point;
138 its return value is the user program's entry point. */
139 #define RTLD_START asm ("\n\
140 .text\n\
141 .align 16\n\
142 .globl _start\n\
143 .globl _dl_start_user\n\
144 _start:\n\
145 movq %rsp, %rdi\n\
146 call _dl_start\n\
147 _dl_start_user:\n\
148 # Save the user entry point address in %r12.\n\
149 movq %rax, %r12\n\
150 # See if we were run as a command with the executable file\n\
151 # name as an extra leading argument.\n\
152 movl _dl_skip_args(%rip), %eax\n\
153 # Pop the original argument count.\n\
154 popq %rdx\n\
155 # Adjust the stack pointer to skip _dl_skip_args words.\n\
156 leaq (%rsp,%rax,8), %rsp\n\
157 # Subtract _dl_skip_args from argc.\n\
158 subl %eax, %edx\n\
159 # Push argc back on the stack.\n\
160 pushq %rdx\n\
161 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
162 # argc -> rsi\n\
163 movq %rdx, %rsi\n\
164 # Save %rsp value in %r13.\n\
165 movq %rsp, %r13\n\
166 # And align stack for the _dl_init call. \n\
167 andq $-16, %rsp\n\
168 # _dl_loaded -> rdi\n\
169 movq _rtld_local(%rip), %rdi\n\
170 # env -> rcx\n\
171 leaq 16(%r13,%rdx,8), %rcx\n\
172 # argv -> rdx\n\
173 leaq 8(%r13), %rdx\n\
174 # Clear %rbp to mark outermost frame obviously even for constructors.\n\
175 xorl %ebp, %ebp\n\
176 # Call the function to run the initializers.\n\
177 call _dl_init\n\
178 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
179 leaq _dl_fini(%rip), %rdx\n\
180 # And make sure %rsp points to argc stored on the stack.\n\
181 movq %r13, %rsp\n\
182 # Jump to the user's entry point.\n\
183 jmp *%r12\n\
184 .previous\n\
187 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
188 TLS variable, so undefined references should not be allowed to
189 define the value.
190 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
191 of the main executable's symbols, as for a COPY reloc.
192 ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA iff TYPE describes relocation may
193 against protected data whose address be external due to copy relocation.
195 #define elf_machine_type_class(type) \
196 ((((type) == R_X86_64_JUMP_SLOT \
197 || (type) == R_X86_64_DTPMOD64 \
198 || (type) == R_X86_64_DTPOFF64 \
199 || (type) == R_X86_64_TPOFF64 \
200 || (type) == R_X86_64_TLSDESC) \
201 * ELF_RTYPE_CLASS_PLT) \
202 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY) \
203 | (((type) == R_X86_64_GLOB_DAT) * ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA))
205 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
206 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
208 /* The relative ifunc relocation. */
209 // XXX This is a work-around for a broken linker. Remove!
210 #define ELF_MACHINE_IRELATIVE R_X86_64_IRELATIVE
212 /* The x86-64 never uses Elf64_Rel/Elf32_Rel relocations. */
213 #define ELF_MACHINE_NO_REL 1
214 #define ELF_MACHINE_NO_RELA 0
216 /* We define an initialization function. This is called very early in
217 _dl_sysdep_start. */
218 #define DL_PLATFORM_INIT dl_platform_init ()
220 static inline void __attribute__ ((unused))
221 dl_platform_init (void)
223 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
224 /* Avoid an empty string which would disturb us. */
225 GLRO(dl_platform) = NULL;
227 init_cpu_features (&GLRO(dl_x86_cpu_features));
230 static inline ElfW(Addr)
231 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
232 const ElfW(Rela) *reloc,
233 ElfW(Addr) *reloc_addr, ElfW(Addr) value)
235 return *reloc_addr = value;
238 /* Return the final value of a PLT relocation. On x86-64 the
239 JUMP_SLOT relocation ignores the addend. */
240 static inline ElfW(Addr)
241 elf_machine_plt_value (struct link_map *map, const ElfW(Rela) *reloc,
242 ElfW(Addr) value)
244 return value;
248 /* Names of the architecture-specific auditing callback functions. */
249 #define ARCH_LA_PLTENTER x86_64_gnu_pltenter
250 #define ARCH_LA_PLTEXIT x86_64_gnu_pltexit
252 #endif /* !dl_machine_h */
254 #ifdef RESOLVE_MAP
256 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
257 MAP is the object containing the reloc. */
259 auto inline void
260 __attribute__ ((always_inline))
261 elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
262 const ElfW(Sym) *sym, const struct r_found_version *version,
263 void *const reloc_addr_arg, int skip_ifunc)
265 ElfW(Addr) *const reloc_addr = reloc_addr_arg;
266 const unsigned long int r_type = ELFW(R_TYPE) (reloc->r_info);
268 # if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
269 if (__glibc_unlikely (r_type == R_X86_64_RELATIVE))
271 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
272 /* This is defined in rtld.c, but nowhere in the static libc.a;
273 make the reference weak so static programs can still link.
274 This declaration cannot be done when compiling rtld.c
275 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
276 common defn for _dl_rtld_map, which is incompatible with a
277 weak decl in the same file. */
278 # ifndef SHARED
279 weak_extern (GL(dl_rtld_map));
280 # endif
281 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
282 # endif
283 *reloc_addr = map->l_addr + reloc->r_addend;
285 else
286 # endif
287 # if !defined RTLD_BOOTSTRAP
288 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
289 relocation updates the whole 64-bit entry. */
290 if (__glibc_unlikely (r_type == R_X86_64_RELATIVE64))
291 *(Elf64_Addr *) reloc_addr = (Elf64_Addr) map->l_addr + reloc->r_addend;
292 else
293 # endif
294 if (__glibc_unlikely (r_type == R_X86_64_NONE))
295 return;
296 else
298 # ifndef RTLD_BOOTSTRAP
299 const ElfW(Sym) *const refsym = sym;
300 # endif
301 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
302 ElfW(Addr) value = (sym == NULL ? 0
303 : (ElfW(Addr)) sym_map->l_addr + sym->st_value);
305 if (sym != NULL
306 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC,
308 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
309 && __builtin_expect (!skip_ifunc, 1))
310 value = ((ElfW(Addr) (*) (void)) value) ();
312 switch (r_type)
314 # ifndef RTLD_BOOTSTRAP
315 # ifdef __ILP32__
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;
320 break;
322 case R_X86_64_SIZE32:
323 # else
324 case R_X86_64_SIZE64:
325 # endif
326 /* Set to symbol size plus addend. */
327 value = sym->st_size;
328 # endif
329 case R_X86_64_GLOB_DAT:
330 case R_X86_64_JUMP_SLOT:
331 *reloc_addr = value + reloc->r_addend;
332 break;
334 # ifndef RESOLVE_CONFLICT_FIND_MAP
335 case R_X86_64_DTPMOD64:
336 # ifdef RTLD_BOOTSTRAP
337 /* During startup the dynamic linker is always the module
338 with index 1.
339 XXX If this relocation is necessary move before RESOLVE
340 call. */
341 *reloc_addr = 1;
342 # else
343 /* Get the information from the link map returned by the
344 resolve function. */
345 if (sym_map != NULL)
346 *reloc_addr = sym_map->l_tls_modid;
347 # endif
348 break;
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. */
353 if (sym != NULL)
355 value = sym->st_value + reloc->r_addend;
356 # ifdef __ILP32__
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;
363 # else
364 *reloc_addr = value;
365 # endif
367 # endif
368 break;
369 case R_X86_64_TLSDESC:
371 struct tlsdesc volatile *td =
372 (struct tlsdesc volatile *)reloc_addr;
374 # ifndef RTLD_BOOTSTRAP
375 if (! sym)
377 td->arg = (void*)reloc->r_addend;
378 td->entry = _dl_tlsdesc_undefweak;
380 else
381 # endif
383 # ifndef RTLD_BOOTSTRAP
384 # ifndef SHARED
385 CHECK_STATIC_TLS (map, sym_map);
386 # else
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;
393 else
394 # endif
395 # endif
397 td->arg = (void*)(sym->st_value - sym_map->l_tls_offset
398 + reloc->r_addend);
399 td->entry = _dl_tlsdesc_return;
402 break;
404 case R_X86_64_TPOFF64:
405 /* The offset is negative, forward from the thread pointer. */
406 # ifndef RTLD_BOOTSTRAP
407 if (sym != NULL)
408 # endif
410 # ifndef RTLD_BOOTSTRAP
411 CHECK_STATIC_TLS (map, sym_map);
412 # endif
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
415 thread pointer. */
416 value = (sym->st_value + reloc->r_addend
417 - sym_map->l_tls_offset);
418 # ifdef __ILP32__
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;
424 # else
425 *reloc_addr = value;
426 # endif
428 break;
429 # endif
431 # ifndef RTLD_BOOTSTRAP
432 case R_X86_64_64:
433 /* value + r_addend may be > 0xffffffff and R_X86_64_64
434 relocation updates the whole 64-bit entry. */
435 *(Elf64_Addr *) reloc_addr = (Elf64_Addr) value + reloc->r_addend;
436 break;
437 # ifndef __ILP32__
438 case R_X86_64_SIZE32:
439 /* Set to symbol size plus addend. */
440 value = sym->st_size;
441 # endif
442 case R_X86_64_32:
443 value += reloc->r_addend;
444 *(unsigned int *) reloc_addr = value;
446 const char *fmt;
447 if (__glibc_unlikely (value > UINT_MAX))
449 const char *strtab;
451 fmt = "\
452 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n";
453 # ifndef RESOLVE_CONFLICT_FIND_MAP
454 print_err:
455 # endif
456 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
458 _dl_error_printf (fmt, RTLD_PROGNAME, strtab + refsym->st_name);
460 break;
461 # ifndef RESOLVE_CONFLICT_FIND_MAP
462 /* Not needed for dl-conflict.c. */
463 case R_X86_64_PC32:
464 value += reloc->r_addend - (ElfW(Addr)) reloc_addr;
465 *(unsigned int *) reloc_addr = value;
466 if (__glibc_unlikely (value != (int) value))
468 fmt = "\
469 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n";
470 goto print_err;
472 break;
473 case R_X86_64_COPY:
474 if (sym == NULL)
475 /* This can happen in trace mode if an object could not be
476 found. */
477 break;
478 memcpy (reloc_addr_arg, (void *) value,
479 MIN (sym->st_size, refsym->st_size));
480 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
481 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
482 && GLRO(dl_verbose)))
484 fmt = "\
485 %s: Symbol `%s' has different size in shared object, consider re-linking\n";
486 goto print_err;
488 break;
489 # endif
490 case R_X86_64_IRELATIVE:
491 value = map->l_addr + reloc->r_addend;
492 value = ((ElfW(Addr) (*) (void)) value) ();
493 *reloc_addr = value;
494 break;
495 default:
496 _dl_reloc_bad_type (map, r_type, 0);
497 break;
498 # endif
503 auto inline void
504 __attribute ((always_inline))
505 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
506 void *const reloc_addr_arg)
508 ElfW(Addr) *const reloc_addr = reloc_addr_arg;
509 #if !defined RTLD_BOOTSTRAP
510 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
511 relocation updates the whole 64-bit entry. */
512 if (__glibc_unlikely (ELFW(R_TYPE) (reloc->r_info) == R_X86_64_RELATIVE64))
513 *(Elf64_Addr *) reloc_addr = (Elf64_Addr) l_addr + reloc->r_addend;
514 else
515 #endif
517 assert (ELFW(R_TYPE) (reloc->r_info) == R_X86_64_RELATIVE);
518 *reloc_addr = l_addr + reloc->r_addend;
522 auto inline void
523 __attribute ((always_inline))
524 elf_machine_lazy_rel (struct link_map *map,
525 ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
526 int skip_ifunc)
528 ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
529 const unsigned long int r_type = ELFW(R_TYPE) (reloc->r_info);
531 /* Check for unexpected PLT reloc type. */
532 if (__glibc_likely (r_type == R_X86_64_JUMP_SLOT))
534 if (__builtin_expect (map->l_mach.plt, 0) == 0)
535 *reloc_addr += l_addr;
536 else
537 *reloc_addr =
538 map->l_mach.plt
539 + (((ElfW(Addr)) reloc_addr) - map->l_mach.gotplt) * 2;
541 else if (__glibc_likely (r_type == R_X86_64_TLSDESC))
543 struct tlsdesc volatile * __attribute__((__unused__)) td =
544 (struct tlsdesc volatile *)reloc_addr;
546 td->arg = (void*)reloc;
547 td->entry = (void*)(D_PTR (map, l_info[ADDRIDX (DT_TLSDESC_PLT)])
548 + map->l_addr);
550 else if (__glibc_unlikely (r_type == R_X86_64_IRELATIVE))
552 ElfW(Addr) value = map->l_addr + reloc->r_addend;
553 if (__glibc_likely (!skip_ifunc))
554 value = ((ElfW(Addr) (*) (void)) value) ();
555 *reloc_addr = value;
557 else
558 _dl_reloc_bad_type (map, r_type, 1);
561 #endif /* RESOLVE_MAP */