Define bit_SSE2 and index_SSE2.
[glibc.git] / sysdeps / x86_64 / dl-machine.h
blob61a0556d5e4b6a7e119ce68a6470a9389e23b924
1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001-2005, 2006, 2008, 2009 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>
29 #include <dl-tlsdesc.h>
31 /* Return nonzero iff ELF header is compatible with the running host. */
32 static inline int __attribute__ ((unused))
33 elf_machine_matches_host (const Elf64_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 Elf64_Addr __attribute__ ((unused))
43 elf_machine_dynamic (void)
45 Elf64_Addr addr;
47 /* This works because we have our GOT address available in the small PIC
48 model. */
49 addr = (Elf64_Addr) &_DYNAMIC;
51 return addr;
55 /* Return the run-time load address of the shared object. */
56 static inline Elf64_Addr __attribute__ ((unused))
57 elf_machine_load_address (void)
59 Elf64_Addr addr;
61 /* The easy way is just the same as on x86:
62 leaq _dl_start, %0
63 leaq _dl_start(%%rip), %1
64 subq %0, %1
65 but this does not work with binutils since we then have
66 a R_X86_64_32S relocation in a shared lib.
68 Instead we store the address of _dl_start in the data section
69 and compare it with the current value that we can get via
70 an RIP relative addressing mode. Note that this is the address
71 of _dl_start before any relocation performed at runtime. In case
72 the binary is prelinked the resulting "address" is actually a
73 load offset which is zero if the binary was loaded at the address
74 it is prelinked for. */
76 asm ("leaq _dl_start(%%rip), %0\n\t"
77 "subq 1f(%%rip), %0\n\t"
78 ".section\t.data.rel.ro\n"
79 "1:\t.quad _dl_start\n\t"
80 ".previous\n\t"
81 : "=r" (addr) : : "cc");
83 return addr;
86 /* Set up the loaded object described by L so its unrelocated PLT
87 entries will jump to the on-demand fixup code in dl-runtime.c. */
89 static inline int __attribute__ ((unused, always_inline))
90 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
92 Elf64_Addr *got;
93 extern void _dl_runtime_resolve (Elf64_Word) attribute_hidden;
94 extern void _dl_runtime_profile (Elf64_Word) attribute_hidden;
96 if (l->l_info[DT_JMPREL] && lazy)
98 /* The GOT entries for functions in the PLT have not yet been filled
99 in. Their initial contents will arrange when called to push an
100 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
101 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
102 got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
103 /* If a library is prelinked but we have to relocate anyway,
104 we have to be able to undo the prelinking of .got.plt.
105 The prelinker saved us here address of .plt + 0x16. */
106 if (got[1])
108 l->l_mach.plt = got[1] + l->l_addr;
109 l->l_mach.gotplt = (Elf64_Addr) &got[3];
111 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
113 /* The got[2] entry contains the address of a function which gets
114 called to get the address of a so far unresolved function and
115 jump to it. The profiling extension of the dynamic linker allows
116 to intercept the calls to collect information. In this case we
117 don't store the address in the GOT so that all future calls also
118 end in this function. */
119 if (__builtin_expect (profile, 0))
121 got[2] = (Elf64_Addr) &_dl_runtime_profile;
123 if (GLRO(dl_profile) != NULL
124 && _dl_name_match_p (GLRO(dl_profile), l))
125 /* This is the object we are looking for. Say that we really
126 want profiling and the timers are started. */
127 GL(dl_profile_map) = l;
129 else
130 /* This function will get called to fix up the GOT entry indicated by
131 the offset on the stack, and then jump to the resolved address. */
132 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
135 if (l->l_info[ADDRIDX (DT_TLSDESC_GOT)] && lazy)
136 *(Elf64_Addr*)(D_PTR (l, l_info[ADDRIDX (DT_TLSDESC_GOT)]) + l->l_addr)
137 = (Elf64_Addr) &_dl_tlsdesc_resolve_rela;
139 return lazy;
142 /* Initial entry point code for the dynamic linker.
143 The C function `_dl_start' is the real entry point;
144 its return value is the user program's entry point. */
145 #define RTLD_START asm ("\n\
146 .text\n\
147 .align 16\n\
148 .globl _start\n\
149 .globl _dl_start_user\n\
150 _start:\n\
151 movq %rsp, %rdi\n\
152 call _dl_start\n\
153 _dl_start_user:\n\
154 # Save the user entry point address in %r12.\n\
155 movq %rax, %r12\n\
156 # See if we were run as a command with the executable file\n\
157 # name as an extra leading argument.\n\
158 movl _dl_skip_args(%rip), %eax\n\
159 # Pop the original argument count.\n\
160 popq %rdx\n\
161 # Adjust the stack pointer to skip _dl_skip_args words.\n\
162 leaq (%rsp,%rax,8), %rsp\n\
163 # Subtract _dl_skip_args from argc.\n\
164 subl %eax, %edx\n\
165 # Push argc back on the stack.\n\
166 pushq %rdx\n\
167 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
168 # argc -> rsi\n\
169 movq %rdx, %rsi\n\
170 # Save %rsp value in %r13.\n\
171 movq %rsp, %r13\n\
172 # And align stack for the _dl_init_internal call. \n\
173 andq $-16, %rsp\n\
174 # _dl_loaded -> rdi\n\
175 movq _rtld_local(%rip), %rdi\n\
176 # env -> rcx\n\
177 leaq 16(%r13,%rdx,8), %rcx\n\
178 # argv -> rdx\n\
179 leaq 8(%r13), %rdx\n\
180 # Clear %rbp to mark outermost frame obviously even for constructors.\n\
181 xorl %ebp, %ebp\n\
182 # Call the function to run the initializers.\n\
183 call _dl_init_internal@PLT\n\
184 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
185 leaq _dl_fini(%rip), %rdx\n\
186 # And make sure %rsp points to argc stored on the stack.\n\
187 movq %r13, %rsp\n\
188 # Jump to the user's entry point.\n\
189 jmp *%r12\n\
190 .previous\n\
193 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
194 TLS variable, so undefined references should not be allowed to
195 define the value.
196 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
197 of the main executable's symbols, as for a COPY reloc. */
198 #if !defined RTLD_BOOTSTRAP || USE___THREAD
199 # define elf_machine_type_class(type) \
200 ((((type) == R_X86_64_JUMP_SLOT \
201 || (type) == R_X86_64_DTPMOD64 \
202 || (type) == R_X86_64_DTPOFF64 \
203 || (type) == R_X86_64_TPOFF64 \
204 || (type) == R_X86_64_TLSDESC) \
205 * ELF_RTYPE_CLASS_PLT) \
206 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
207 #else
208 # define elf_machine_type_class(type) \
209 ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
210 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
211 #endif
213 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
214 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
216 /* The x86-64 never uses Elf64_Rel relocations. */
217 #define ELF_MACHINE_NO_REL 1
219 /* We define an initialization functions. This is called very early in
220 _dl_sysdep_start. */
221 #define DL_PLATFORM_INIT dl_platform_init ()
223 static inline void __attribute__ ((unused))
224 dl_platform_init (void)
226 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
227 /* Avoid an empty string which would disturb us. */
228 GLRO(dl_platform) = NULL;
231 static inline Elf64_Addr
232 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
233 const Elf64_Rela *reloc,
234 Elf64_Addr *reloc_addr, Elf64_Addr value)
236 return *reloc_addr = value;
239 /* Return the final value of a plt relocation. On x86-64 the
240 JUMP_SLOT relocation ignores the addend. */
241 static inline Elf64_Addr
242 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
243 Elf64_Addr value)
245 return value;
249 /* Names of the architecture-specific auditing callback functions. */
250 #define ARCH_LA_PLTENTER x86_64_gnu_pltenter
251 #define ARCH_LA_PLTEXIT x86_64_gnu_pltexit
253 #endif /* !dl_machine_h */
255 #ifdef RESOLVE_MAP
257 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
258 MAP is the object containing the reloc. */
260 auto inline void
261 __attribute__ ((always_inline))
262 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
263 const Elf64_Sym *sym, const struct r_found_version *version,
264 void *const reloc_addr_arg)
266 Elf64_Addr *const reloc_addr = reloc_addr_arg;
267 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
269 # if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
270 if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0))
272 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
273 /* This is defined in rtld.c, but nowhere in the static libc.a;
274 make the reference weak so static programs can still link.
275 This declaration cannot be done when compiling rtld.c
276 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
277 common defn for _dl_rtld_map, which is incompatible with a
278 weak decl in the same file. */
279 # ifndef SHARED
280 weak_extern (GL(dl_rtld_map));
281 # endif
282 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
283 # endif
284 *reloc_addr = map->l_addr + reloc->r_addend;
286 else
287 # endif
288 if (__builtin_expect (r_type == R_X86_64_NONE, 0))
289 return;
290 else
292 # ifndef RTLD_BOOTSTRAP
293 const Elf64_Sym *const refsym = sym;
294 # endif
295 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
296 Elf64_Addr value = (sym == NULL ? 0
297 : (Elf64_Addr) sym_map->l_addr + sym->st_value);
299 if (sym != NULL
300 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC,
302 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1))
303 value = ((Elf64_Addr (*) (void)) value) ();
305 # if defined RTLD_BOOTSTRAP && !USE___THREAD
306 assert (r_type == R_X86_64_GLOB_DAT || r_type == R_X86_64_JUMP_SLOT);
307 *reloc_addr = value + reloc->r_addend;
308 # else
309 switch (r_type)
311 case R_X86_64_GLOB_DAT:
312 case R_X86_64_JUMP_SLOT:
313 *reloc_addr = value + reloc->r_addend;
314 break;
316 # ifndef RESOLVE_CONFLICT_FIND_MAP
317 case R_X86_64_DTPMOD64:
318 # ifdef RTLD_BOOTSTRAP
319 /* During startup the dynamic linker is always the module
320 with index 1.
321 XXX If this relocation is necessary move before RESOLVE
322 call. */
323 *reloc_addr = 1;
324 # else
325 /* Get the information from the link map returned by the
326 resolve function. */
327 if (sym_map != NULL)
328 *reloc_addr = sym_map->l_tls_modid;
329 # endif
330 break;
331 case R_X86_64_DTPOFF64:
332 # ifndef RTLD_BOOTSTRAP
333 /* During relocation all TLS symbols are defined and used.
334 Therefore the offset is already correct. */
335 if (sym != NULL)
336 *reloc_addr = sym->st_value + reloc->r_addend;
337 # endif
338 break;
339 case R_X86_64_TLSDESC:
341 struct tlsdesc volatile *td =
342 (struct tlsdesc volatile *)reloc_addr;
344 # ifndef RTLD_BOOTSTRAP
345 if (! sym)
347 td->arg = (void*)reloc->r_addend;
348 td->entry = _dl_tlsdesc_undefweak;
350 else
351 # endif
353 # ifndef RTLD_BOOTSTRAP
354 # ifndef SHARED
355 CHECK_STATIC_TLS (map, sym_map);
356 # else
357 if (!TRY_STATIC_TLS (map, sym_map))
359 td->arg = _dl_make_tlsdesc_dynamic
360 (sym_map, sym->st_value + reloc->r_addend);
361 td->entry = _dl_tlsdesc_dynamic;
363 else
364 # endif
365 # endif
367 td->arg = (void*)(sym->st_value - sym_map->l_tls_offset
368 + reloc->r_addend);
369 td->entry = _dl_tlsdesc_return;
372 break;
374 case R_X86_64_TPOFF64:
375 /* The offset is negative, forward from the thread pointer. */
376 # ifndef RTLD_BOOTSTRAP
377 if (sym != NULL)
378 # endif
380 # ifndef RTLD_BOOTSTRAP
381 CHECK_STATIC_TLS (map, sym_map);
382 # endif
383 /* We know the offset of the object the symbol is contained in.
384 It is a negative value which will be added to the
385 thread pointer. */
386 *reloc_addr = (sym->st_value + reloc->r_addend
387 - sym_map->l_tls_offset);
389 break;
390 # endif
392 # ifndef RTLD_BOOTSTRAP
393 case R_X86_64_64:
394 *reloc_addr = value + reloc->r_addend;
395 break;
396 case R_X86_64_32:
397 value += reloc->r_addend;
398 *(unsigned int *) reloc_addr = value;
400 const char *fmt;
401 if (__builtin_expect (value > UINT_MAX, 0))
403 const char *strtab;
405 fmt = "\
406 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n";
407 # ifndef RESOLVE_CONFLICT_FIND_MAP
408 print_err:
409 # endif
410 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
412 _dl_error_printf (fmt,
413 rtld_progname ?: "<program name unknown>",
414 strtab + refsym->st_name);
416 break;
417 # ifndef RESOLVE_CONFLICT_FIND_MAP
418 /* Not needed for dl-conflict.c. */
419 case R_X86_64_PC32:
420 value += reloc->r_addend - (Elf64_Addr) reloc_addr;
421 *(unsigned int *) reloc_addr = value;
422 if (__builtin_expect (value != (unsigned int) value, 0))
424 fmt = "\
425 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n";
426 goto print_err;
428 break;
429 case R_X86_64_COPY:
430 if (sym == NULL)
431 /* This can happen in trace mode if an object could not be
432 found. */
433 break;
434 memcpy (reloc_addr_arg, (void *) value,
435 MIN (sym->st_size, refsym->st_size));
436 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
437 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
438 && GLRO(dl_verbose)))
440 fmt = "\
441 %s: Symbol `%s' has different size in shared object, consider re-linking\n";
442 goto print_err;
444 break;
445 # endif
446 case R_X86_64_IRELATIVE:
447 value = map->l_addr + reloc->r_addend;
448 value = ((Elf64_Addr (*) (void)) value) ();
449 *reloc_addr = value;
450 break;
451 default:
452 _dl_reloc_bad_type (map, r_type, 0);
453 break;
454 # endif
456 #endif
460 auto inline void
461 __attribute ((always_inline))
462 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
463 void *const reloc_addr_arg)
465 Elf64_Addr *const reloc_addr = reloc_addr_arg;
466 assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
467 *reloc_addr = l_addr + reloc->r_addend;
470 auto inline void
471 __attribute ((always_inline))
472 elf_machine_lazy_rel (struct link_map *map,
473 Elf64_Addr l_addr, const Elf64_Rela *reloc)
475 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
476 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
478 /* Check for unexpected PLT reloc type. */
479 if (__builtin_expect (r_type == R_X86_64_JUMP_SLOT, 1))
481 if (__builtin_expect (map->l_mach.plt, 0) == 0)
482 *reloc_addr += l_addr;
483 else
484 *reloc_addr =
485 map->l_mach.plt
486 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 2;
488 else if (__builtin_expect (r_type == R_X86_64_TLSDESC, 1))
490 struct tlsdesc volatile * __attribute__((__unused__)) td =
491 (struct tlsdesc volatile *)reloc_addr;
493 td->arg = (void*)reloc;
494 td->entry = (void*)(D_PTR (map, l_info[ADDRIDX (DT_TLSDESC_PLT)])
495 + map->l_addr);
497 else if (__builtin_expect (r_type == R_X86_64_IRELATIVE, 0))
499 Elf64_Addr value = map->l_addr + reloc->r_addend;
500 value = ((Elf64_Addr (*) (void)) value) ();
501 *reloc_addr = value;
503 else
504 _dl_reloc_bad_type (map, r_type, 1);
507 #endif /* RESOLVE_MAP */