Update.
[glibc.git] / sysdeps / x86_64 / dl-machine.h
blob302545f68f1573d60a82685267920327e2b12deb
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 static inline void
359 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
360 const Elf64_Sym *sym, const struct r_found_version *version,
361 void *const reloc_addr_arg)
363 Elf64_Addr *const reloc_addr = reloc_addr_arg;
364 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
366 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
367 if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0))
369 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
370 /* This is defined in rtld.c, but nowhere in the static libc.a;
371 make the reference weak so static programs can still link.
372 This declaration cannot be done when compiling rtld.c
373 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
374 common defn for _dl_rtld_map, which is incompatible with a
375 weak decl in the same file. */
376 # ifndef SHARED
377 weak_extern (GL(dl_rtld_map));
378 # endif
379 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
380 # endif
381 *reloc_addr = map->l_addr + reloc->r_addend;
383 else
384 #endif
385 if (__builtin_expect (r_type == R_X86_64_NONE, 0))
386 return;
387 else
389 #ifndef RTLD_BOOTSTRAP
390 const Elf64_Sym *const refsym = sym;
391 #endif
392 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
393 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
394 Elf64_Addr value = (sym == NULL ? 0
395 : (Elf64_Addr) sym_map->l_addr + sym->st_value);
396 #else
397 Elf64_Addr value = RESOLVE (&sym, version, r_type);
399 # ifndef RTLD_BOOTSTRAP
400 if (sym != NULL)
401 # endif
402 value += sym->st_value;
403 #endif
405 #if defined RTLD_BOOTSTRAP && !USE___THREAD
406 assert (r_type == R_X86_64_GLOB_DAT || r_type == R_X86_64_JUMP_SLOT);
407 *reloc_addr = value + reloc->r_addend;
408 #else
409 switch (r_type)
411 case R_X86_64_GLOB_DAT:
412 case R_X86_64_JUMP_SLOT:
413 *reloc_addr = value + reloc->r_addend;
414 break;
416 #if defined USE_TLS && !defined RESOLVE_CONFLICT_FIND_MAP
417 case R_X86_64_DTPMOD64:
418 # ifdef RTLD_BOOTSTRAP
419 /* During startup the dynamic linker is always the module
420 with index 1.
421 XXX If this relocation is necessary move before RESOLVE
422 call. */
423 *reloc_addr = 1;
424 # else
425 /* Get the information from the link map returned by the
426 resolve function. */
427 if (sym_map != NULL)
428 *reloc_addr = sym_map->l_tls_modid;
429 # endif
430 break;
431 case R_X86_64_DTPOFF64:
432 # ifndef RTLD_BOOTSTRAP
433 /* During relocation all TLS symbols are defined and used.
434 Therefore the offset is already correct. */
435 if (sym != NULL)
436 *reloc_addr = sym->st_value + reloc->r_addend;
437 # endif
438 break;
439 case R_X86_64_TPOFF64:
440 /* The offset is negative, forward from the thread pointer. */
441 # ifndef RTLD_BOOTSTRAP
442 if (sym != NULL)
443 # endif
445 # ifndef RTLD_BOOTSTRAP
446 CHECK_STATIC_TLS (map, sym_map);
447 # endif
448 /* We know the offset of the object the symbol is contained in.
449 It is a negative value which will be added to the
450 thread pointer. */
451 *reloc_addr = (sym->st_value + reloc->r_addend
452 - sym_map->l_tls_offset);
454 break;
455 #endif /* use TLS */
457 #ifndef RTLD_BOOTSTRAP
458 case R_X86_64_64:
459 *reloc_addr = value + reloc->r_addend;
460 break;
461 case R_X86_64_32:
462 *(unsigned int *) reloc_addr = value + reloc->r_addend;
463 if (value + reloc->r_addend > UINT_MAX)
465 const char *strtab;
467 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
469 _dl_error_printf ("\
470 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n",
471 rtld_progname ?: "<program name unknown>",
472 strtab + refsym->st_name);
474 break;
475 # ifndef RESOLVE_CONFLICT_FIND_MAP
476 /* Not needed for dl-conflict.c. */
477 case R_X86_64_PC32:
478 *(unsigned int *) reloc_addr = value + reloc->r_addend
479 - (Elf64_Addr) reloc_addr;
480 if (value + reloc->r_addend - (Elf64_Addr) reloc_addr
481 != (int)(value + reloc->r_addend - (Elf64_Addr) reloc_addr))
483 const char *strtab;
485 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
487 _dl_error_printf ("\
488 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n",
489 rtld_progname ?: "<program name unknown>",
490 strtab + refsym->st_name);
492 break;
493 case R_X86_64_COPY:
494 if (sym == NULL)
495 /* This can happen in trace mode if an object could not be
496 found. */
497 break;
498 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
499 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
500 && GLRO(dl_verbose)))
502 const char *strtab;
504 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
505 _dl_error_printf ("\
506 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
507 rtld_progname ?: "<program name unknown>",
508 strtab + refsym->st_name);
510 memcpy (reloc_addr_arg, (void *) value,
511 MIN (sym->st_size, refsym->st_size));
512 break;
513 # endif
514 default:
515 _dl_reloc_bad_type (map, r_type, 0);
516 break;
517 #endif
519 #endif
523 static inline void
524 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
525 void *const reloc_addr_arg)
527 Elf64_Addr *const reloc_addr = reloc_addr_arg;
528 assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
529 *reloc_addr = l_addr + reloc->r_addend;
532 static inline void
533 elf_machine_lazy_rel (struct link_map *map,
534 Elf64_Addr l_addr, const Elf64_Rela *reloc)
536 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
537 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
539 /* Check for unexpected PLT reloc type. */
540 if (__builtin_expect (r_type == R_X86_64_JUMP_SLOT, 1))
542 if (__builtin_expect (map->l_mach.plt, 0) == 0)
543 *reloc_addr += l_addr;
544 else
545 *reloc_addr =
546 map->l_mach.plt
547 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 2;
549 else
550 _dl_reloc_bad_type (map, r_type, 1);
553 #endif /* RESOLVE */