(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / x86_64 / dl-machine.h
blobb932f51d1552a03a3a160bba7097b41d702938e8
1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001, 2002, 2003, 2004 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef dl_machine_h
22 #define dl_machine_h
24 #define ELF_MACHINE_NAME "x86_64"
26 #include <sys/param.h>
27 #include <sysdep.h>
28 #include <tls.h>
30 /* Return nonzero iff ELF header is compatible with the running host. */
31 static inline int __attribute__ ((unused))
32 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
34 return ehdr->e_machine == EM_X86_64;
38 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
39 first element of the GOT. This must be inlined in a function which
40 uses global data. */
41 static inline Elf64_Addr __attribute__ ((unused))
42 elf_machine_dynamic (void)
44 Elf64_Addr addr;
46 /* This works because we have our GOT address available in the small PIC
47 model. */
48 addr = (Elf64_Addr) &_DYNAMIC;
50 return addr;
54 /* Return the run-time load address of the shared object. */
55 static inline Elf64_Addr __attribute__ ((unused))
56 elf_machine_load_address (void)
58 register Elf64_Addr addr, tmp;
60 /* The easy way is just the same as on x86:
61 leaq _dl_start, %0
62 leaq _dl_start(%%rip), %1
63 subq %0, %1
64 but this does not work with binutils since we then have
65 a R_X86_64_32S relocation in a shared lib.
67 Instead we store the address of _dl_start in the data section
68 and compare it with the current value that we can get via
69 an RIP relative addressing mode. */
71 asm ("movq 1f(%%rip), %1\n"
72 "0:\tleaq _dl_start(%%rip), %0\n\t"
73 "subq %1, %0\n\t"
74 ".section\t.data\n"
75 "1:\t.quad _dl_start\n\t"
76 ".previous\n\t"
77 : "=r" (addr), "=r" (tmp) : : "cc");
79 return addr;
82 /* Set up the loaded object described by L so its unrelocated PLT
83 entries will jump to the on-demand fixup code in dl-runtime.c. */
85 static inline int __attribute__ ((unused, always_inline))
86 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
88 Elf64_Addr *got;
89 extern void _dl_runtime_resolve (Elf64_Word) attribute_hidden;
90 extern void _dl_runtime_profile (Elf64_Word) attribute_hidden;
92 if (l->l_info[DT_JMPREL] && lazy)
94 /* The GOT entries for functions in the PLT have not yet been filled
95 in. Their initial contents will arrange when called to push an
96 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
97 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
98 got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
99 /* If a library is prelinked but we have to relocate anyway,
100 we have to be able to undo the prelinking of .got.plt.
101 The prelinker saved us here address of .plt + 0x16. */
102 if (got[1])
104 l->l_mach.plt = got[1] + l->l_addr;
105 l->l_mach.gotplt = (Elf64_Addr) &got[3];
107 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
109 /* The got[2] entry contains the address of a function which gets
110 called to get the address of a so far unresolved function and
111 jump to it. The profiling extension of the dynamic linker allows
112 to intercept the calls to collect information. In this case we
113 don't store the address in the GOT so that all future calls also
114 end in this function. */
115 if (__builtin_expect (profile, 0))
117 got[2] = (Elf64_Addr) &_dl_runtime_profile;
119 if (_dl_name_match_p (GLRO(dl_profile), l))
120 /* This is the object we are looking for. Say that we really
121 want profiling and the timers are started. */
122 GL(dl_profile_map) = l;
124 else
125 /* This function will get called to fix up the GOT entry indicated by
126 the offset on the stack, and then jump to the resolved address. */
127 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
130 return lazy;
133 /* This code is used in dl-runtime.c to call the `fixup' function
134 and then redirect to the address it returns. */
135 #ifndef PROF
136 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
137 .text\n\
138 .globl _dl_runtime_resolve\n\
139 .type _dl_runtime_resolve, @function\n\
140 .align 16\n\
141 " CFI_STARTPROC "\n\
142 _dl_runtime_resolve:\n\
143 subq $56,%rsp\n\
144 " CFI_ADJUST_CFA_OFFSET(72)" # Incorporate PLT\n\
145 movq %rax,(%rsp) # Preserve registers otherwise clobbered.\n\
146 movq %rcx,8(%rsp)\n\
147 movq %rdx,16(%rsp)\n\
148 movq %rsi,24(%rsp)\n\
149 movq %rdi,32(%rsp)\n\
150 movq %r8,40(%rsp)\n\
151 movq %r9,48(%rsp)\n\
152 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
153 movq %rsi,%r11 # Multiply by 24\n\
154 addq %r11,%rsi\n\
155 addq %r11,%rsi\n\
156 shlq $3, %rsi\n\
157 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
158 call fixup # Call resolver.\n\
159 movq %rax, %r11 # Save return value\n\
160 movq 48(%rsp),%r9 # Get register content back.\n\
161 movq 40(%rsp),%r8\n\
162 movq 32(%rsp),%rdi\n\
163 movq 24(%rsp),%rsi\n\
164 movq 16(%rsp),%rdx\n\
165 movq 8(%rsp),%rcx\n\
166 movq (%rsp),%rax\n\
167 addq $72,%rsp # Adjust stack(PLT did 2 pushes)\n\
168 " CFI_ADJUST_CFA_OFFSET(-72)" \n\
169 jmp *%r11 # Jump to function address.\n\
170 " CFI_ENDPROC "\n\
171 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
173 .globl _dl_runtime_profile\n\
174 .type _dl_runtime_profile, @function\n\
175 .align 16\n\
176 " CFI_STARTPROC "\n\
177 _dl_runtime_profile:\n\
178 subq $56,%rsp\n\
179 " CFI_ADJUST_CFA_OFFSET(72)" # Incorporate PLT\n\
180 movq %rax,(%rsp) # Preserve registers otherwise clobbered.\n\
181 movq %rcx,8(%rsp)\n\
182 movq %rdx,16(%rsp)\n\
183 movq %rsi,24(%rsp)\n\
184 movq %rdi,32(%rsp)\n\
185 movq %r8,40(%rsp)\n\
186 movq %r9,48(%rsp)\n\
187 movq 72(%rsp), %rdx # Load return address if needed\n\
188 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
189 movq %rsi,%r11 # Multiply by 24\n\
190 addq %r11,%rsi\n\
191 addq %r11,%rsi\n\
192 shlq $3, %rsi\n\
193 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
194 call profile_fixup # Call resolver.\n\
195 movq %rax, %r11 # Save return value\n\
196 movq 48(%rsp),%r9 # Get register content back.\n\
197 movq 40(%rsp),%r8\n\
198 movq 32(%rsp),%rdi\n\
199 movq 24(%rsp),%rsi\n\
200 movq 16(%rsp),%rdx\n\
201 movq 8(%rsp),%rcx\n\
202 movq (%rsp),%rax\n\
203 addq $72,%rsp # Adjust stack\n\
204 " CFI_ADJUST_CFA_OFFSET(-72)"\n\
205 jmp *%r11 # Jump to function address.\n\
206 " CFI_ENDPROC "\n\
207 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
208 .previous\n\
210 #else
211 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
212 .text\n\
213 .globl _dl_runtime_resolve\n\
214 .globl _dl_runtime_profile\n\
215 .type _dl_runtime_resolve, @function\n\
216 .type _dl_runtime_profile, @function\n\
217 .align 16\n\
218 " CFI_STARTPROC "\n\
219 _dl_runtime_resolve:\n\
220 _dl_runtime_profile:\n\
221 subq $56,%rsp\n\
222 " CFI_ADJUST_CFA_OFFSET(72)" # Incorporate PLT\n\
223 movq %rax,(%rsp) # Preserve registers otherwise clobbered.\n\
224 movq %rcx,8(%rsp)\n\
225 movq %rdx,16(%rsp)\n\
226 movq %rsi,24(%rsp)\n\
227 movq %rdi,32(%rsp)\n\
228 movq %r8,40(%rsp)\n\
229 movq %r9,48(%rsp)\n\
230 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
231 movq %rsi,%r11 # Multiply by 24\n\
232 addq %r11,%rsi\n\
233 addq %r11,%rsi\n\
234 shlq $3, %rsi\n\
235 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
236 call fixup # Call resolver.\n\
237 movq %rax, %r11 # Save return value\n\
238 movq 48(%rsp),%r9 # Get register content back.\n\
239 movq 40(%rsp),%r8\n\
240 movq 32(%rsp),%rdi\n\
241 movq 24(%rsp),%rsi\n\
242 movq 16(%rsp),%rdx\n\
243 movq 8(%rsp),%rcx\n\
244 movq (%rsp),%rax\n\
245 addq $72,%rsp # Adjust stack\n\
246 " CFI_ADJUST_CFA_OFFSET(-72)"\n\
247 jmp *%r11 # Jump to function address.\n\
248 " CFI_ENDPROC "\n\
249 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
250 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
251 .previous\n\
253 #endif
255 /* Initial entry point code for the dynamic linker.
256 The C function `_dl_start' is the real entry point;
257 its return value is the user program's entry point. */
258 #define RTLD_START asm ("\n\
259 .text\n\
260 .align 16\n\
261 .globl _start\n\
262 .globl _dl_start_user\n\
263 _start:\n\
264 movq %rsp, %rdi\n\
265 call _dl_start\n\
266 _dl_start_user:\n\
267 # Save the user entry point address in %r12.\n\
268 movq %rax, %r12\n\
269 # See if we were run as a command with the executable file\n\
270 # name as an extra leading argument.\n\
271 movl _dl_skip_args(%rip), %eax\n\
272 # Pop the original argument count.\n\
273 popq %rdx\n\
274 # Adjust the stack pointer to skip _dl_skip_args words.\n\
275 leaq (%rsp,%rax,8), %rsp\n\
276 # Subtract _dl_skip_args from argc.\n\
277 subl %eax, %edx\n\
278 # Push argc back on the stack.\n\
279 pushq %rdx\n\
280 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
281 # argc -> rsi\n\
282 movq %rdx, %rsi\n\
283 # _dl_loaded -> rdi\n\
284 movq _rtld_local(%rip), %rdi\n\
285 # env -> rcx\n\
286 leaq 16(%rsp,%rdx,8), %rcx\n\
287 # argv -> rdx\n\
288 leaq 8(%rsp), %rdx\n\
289 # Call the function to run the initializers.\n\
290 call _dl_init_internal@PLT\n\
291 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
292 leaq _dl_fini(%rip), %rdx\n\
293 # Jump to the user's entry point.\n\
294 jmp *%r12\n\
295 .previous\n\
298 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
299 TLS variable, so undefined references should not be allowed to
300 define the value.
301 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
302 of the main executable's symbols, as for a COPY reloc. */
303 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
304 # define elf_machine_type_class(type) \
305 ((((type) == R_X86_64_JUMP_SLOT \
306 || (type) == R_X86_64_DTPMOD64 \
307 || (type) == R_X86_64_DTPOFF64 || (type) == R_X86_64_TPOFF64) \
308 * ELF_RTYPE_CLASS_PLT) \
309 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
310 #else
311 # define elf_machine_type_class(type) \
312 ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
313 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
314 #endif
316 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
317 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
319 /* The x86-64 never uses Elf64_Rel relocations. */
320 #define ELF_MACHINE_NO_REL 1
322 /* We define an initialization functions. This is called very early in
323 _dl_sysdep_start. */
324 #define DL_PLATFORM_INIT dl_platform_init ()
326 static inline void __attribute__ ((unused))
327 dl_platform_init (void)
329 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
330 /* Avoid an empty string which would disturb us. */
331 GLRO(dl_platform) = NULL;
334 static inline Elf64_Addr
335 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
336 const Elf64_Rela *reloc,
337 Elf64_Addr *reloc_addr, Elf64_Addr value)
339 return *reloc_addr = value;
342 /* Return the final value of a plt relocation. On x86-64 the
343 JUMP_SLOT relocation ignores the addend. */
344 static inline Elf64_Addr
345 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
346 Elf64_Addr value)
348 return value;
351 #endif /* !dl_machine_h */
353 #ifdef RESOLVE
355 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
356 MAP is the object containing the reloc. */
358 auto inline void
359 __attribute__ ((always_inline))
360 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
361 const Elf64_Sym *sym, const struct r_found_version *version,
362 void *const reloc_addr_arg)
364 Elf64_Addr *const reloc_addr = reloc_addr_arg;
365 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
367 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
368 if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0))
370 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
371 /* This is defined in rtld.c, but nowhere in the static libc.a;
372 make the reference weak so static programs can still link.
373 This declaration cannot be done when compiling rtld.c
374 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
375 common defn for _dl_rtld_map, which is incompatible with a
376 weak decl in the same file. */
377 # ifndef SHARED
378 weak_extern (GL(dl_rtld_map));
379 # endif
380 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
381 # endif
382 *reloc_addr = map->l_addr + reloc->r_addend;
384 else
385 #endif
386 if (__builtin_expect (r_type == R_X86_64_NONE, 0))
387 return;
388 else
390 #ifndef RTLD_BOOTSTRAP
391 const Elf64_Sym *const refsym = sym;
392 #endif
393 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
394 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
395 Elf64_Addr value = (sym == NULL ? 0
396 : (Elf64_Addr) sym_map->l_addr + sym->st_value);
397 #else
398 Elf64_Addr value = RESOLVE (&sym, version, r_type);
400 # ifndef RTLD_BOOTSTRAP
401 if (sym != NULL)
402 # endif
403 value += sym->st_value;
404 #endif
406 #if defined RTLD_BOOTSTRAP && !USE___THREAD
407 assert (r_type == R_X86_64_GLOB_DAT || r_type == R_X86_64_JUMP_SLOT);
408 *reloc_addr = value + reloc->r_addend;
409 #else
410 switch (r_type)
412 case R_X86_64_GLOB_DAT:
413 case R_X86_64_JUMP_SLOT:
414 *reloc_addr = value + reloc->r_addend;
415 break;
417 #if defined USE_TLS && !defined RESOLVE_CONFLICT_FIND_MAP
418 case R_X86_64_DTPMOD64:
419 # ifdef RTLD_BOOTSTRAP
420 /* During startup the dynamic linker is always the module
421 with index 1.
422 XXX If this relocation is necessary move before RESOLVE
423 call. */
424 *reloc_addr = 1;
425 # else
426 /* Get the information from the link map returned by the
427 resolve function. */
428 if (sym_map != NULL)
429 *reloc_addr = sym_map->l_tls_modid;
430 # endif
431 break;
432 case R_X86_64_DTPOFF64:
433 # ifndef RTLD_BOOTSTRAP
434 /* During relocation all TLS symbols are defined and used.
435 Therefore the offset is already correct. */
436 if (sym != NULL)
437 *reloc_addr = sym->st_value + reloc->r_addend;
438 # endif
439 break;
440 case R_X86_64_TPOFF64:
441 /* The offset is negative, forward from the thread pointer. */
442 # ifndef RTLD_BOOTSTRAP
443 if (sym != NULL)
444 # endif
446 # ifndef RTLD_BOOTSTRAP
447 CHECK_STATIC_TLS (map, sym_map);
448 # endif
449 /* We know the offset of the object the symbol is contained in.
450 It is a negative value which will be added to the
451 thread pointer. */
452 *reloc_addr = (sym->st_value + reloc->r_addend
453 - sym_map->l_tls_offset);
455 break;
456 #endif /* use TLS */
458 #ifndef RTLD_BOOTSTRAP
459 case R_X86_64_64:
460 *reloc_addr = value + reloc->r_addend;
461 break;
462 case R_X86_64_32:
463 *(unsigned int *) reloc_addr = value + reloc->r_addend;
464 if (value + reloc->r_addend > UINT_MAX)
466 const char *strtab;
468 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
470 _dl_error_printf ("\
471 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n",
472 rtld_progname ?: "<program name unknown>",
473 strtab + refsym->st_name);
475 break;
476 # ifndef RESOLVE_CONFLICT_FIND_MAP
477 /* Not needed for dl-conflict.c. */
478 case R_X86_64_PC32:
479 *(unsigned int *) reloc_addr = value + reloc->r_addend
480 - (Elf64_Addr) reloc_addr;
481 if (value + reloc->r_addend - (Elf64_Addr) reloc_addr
482 != (int)(value + reloc->r_addend - (Elf64_Addr) reloc_addr))
484 const char *strtab;
486 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
488 _dl_error_printf ("\
489 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n",
490 rtld_progname ?: "<program name unknown>",
491 strtab + refsym->st_name);
493 break;
494 case R_X86_64_COPY:
495 if (sym == NULL)
496 /* This can happen in trace mode if an object could not be
497 found. */
498 break;
499 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
500 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
501 && GLRO(dl_verbose)))
503 const char *strtab;
505 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
506 _dl_error_printf ("\
507 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
508 rtld_progname ?: "<program name unknown>",
509 strtab + refsym->st_name);
511 memcpy (reloc_addr_arg, (void *) value,
512 MIN (sym->st_size, refsym->st_size));
513 break;
514 # endif
515 default:
516 _dl_reloc_bad_type (map, r_type, 0);
517 break;
518 #endif
520 #endif
524 auto inline void
525 __attribute ((always_inline))
526 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
527 void *const reloc_addr_arg)
529 Elf64_Addr *const reloc_addr = reloc_addr_arg;
530 assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
531 *reloc_addr = l_addr + reloc->r_addend;
534 auto inline void
535 __attribute ((always_inline))
536 elf_machine_lazy_rel (struct link_map *map,
537 Elf64_Addr l_addr, const Elf64_Rela *reloc)
539 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
540 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
542 /* Check for unexpected PLT reloc type. */
543 if (__builtin_expect (r_type == R_X86_64_JUMP_SLOT, 1))
545 if (__builtin_expect (map->l_mach.plt, 0) == 0)
546 *reloc_addr += l_addr;
547 else
548 *reloc_addr =
549 map->l_mach.plt
550 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 2;
552 else
553 _dl_reloc_bad_type (map, r_type, 1);
556 #endif /* RESOLVE */