]>
sourceware.org Git - glibc.git/blob - sysdeps/x86_64/dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001, 2002, 2003 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
24 #define ELF_MACHINE_NAME "x86_64"
26 #include <sys/param.h>
28 /* Return nonzero iff ELF header is compatible with the running host. */
29 static inline int __attribute__ ((unused
))
30 elf_machine_matches_host (const Elf64_Ehdr
*ehdr
)
32 return ehdr
->e_machine
== EM_X86_64
;
36 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
37 first element of the GOT. This must be inlined in a function which
39 static inline Elf64_Addr
__attribute__ ((unused
))
40 elf_machine_dynamic (void)
44 /* This works because we have our GOT address available in the small PIC
46 addr
= (Elf64_Addr
) &_DYNAMIC
;
52 /* Return the run-time load address of the shared object. */
53 static inline Elf64_Addr
__attribute__ ((unused
))
54 elf_machine_load_address (void)
56 register Elf64_Addr addr
, tmp
;
58 /* The easy way is just the same as on x86:
60 leaq _dl_start(%%rip), %1
62 but this does not work with binutils since we then have
63 a R_X86_64_32S relocation in a shared lib.
65 Instead we store the address of _dl_start in the data section
66 and compare it with the current value that we can get via
67 an RIP relative addressing mode. */
69 asm ("movq 1f(%%rip), %1\n"
70 "0:\tleaq _dl_start(%%rip), %0\n\t"
73 "1:\t.quad _dl_start\n\t"
75 : "=r" (addr
), "=r" (tmp
) : : "cc");
80 /* Set up the loaded object described by L so its unrelocated PLT
81 entries will jump to the on-demand fixup code in dl-runtime.c. */
83 static inline int __attribute__ ((unused
))
84 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
87 extern void _dl_runtime_resolve (Elf64_Word
) attribute_hidden
;
88 extern void _dl_runtime_profile (Elf64_Word
) attribute_hidden
;
90 if (l
->l_info
[DT_JMPREL
] && lazy
)
92 /* The GOT entries for functions in the PLT have not yet been filled
93 in. Their initial contents will arrange when called to push an
94 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
95 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
96 got
= (Elf64_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
97 /* If a library is prelinked but we have to relocate anyway,
98 we have to be able to undo the prelinking of .got.plt.
99 The prelinker saved us here address of .plt + 0x16. */
102 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
103 l
->l_mach
.gotplt
= (Elf64_Addr
) &got
[3];
105 got
[1] = (Elf64_Addr
) l
; /* Identify this shared object. */
107 /* The got[2] entry contains the address of a function which gets
108 called to get the address of a so far unresolved function and
109 jump to it. The profiling extension of the dynamic linker allows
110 to intercept the calls to collect information. In this case we
111 don't store the address in the GOT so that all future calls also
112 end in this function. */
113 if (__builtin_expect (profile
, 0))
115 got
[2] = (Elf64_Addr
) &_dl_runtime_profile
;
117 if (_dl_name_match_p (GL(dl_profile
), l
))
118 /* This is the object we are looking for. Say that we really
119 want profiling and the timers are started. */
120 GL(dl_profile_map
) = l
;
123 /* This function will get called to fix up the GOT entry indicated by
124 the offset on the stack, and then jump to the resolved address. */
125 got
[2] = (Elf64_Addr
) &_dl_runtime_resolve
;
131 /* This code is used in dl-runtime.c to call the `fixup' function
132 and then redirect to the address it returns. */
134 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
136 .globl _dl_runtime_resolve\n\
137 .type _dl_runtime_resolve, @function\n\
139 _dl_runtime_resolve:\n\
140 pushq %rax # Preserve registers otherwise clobbered.\n\
147 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
148 movq %rsi,%r11 # Multiply by 24\n\
152 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
153 call fixup # Call resolver.\n\
154 movq %rax, %r11 # Save return value\n\
155 popq %r9 # Get register content back.\n\
162 addq $16,%rsp # Adjust stack\n\
163 jmp *%r11 # Jump to function address.\n\
164 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
166 .globl _dl_runtime_profile\n\
167 .type _dl_runtime_profile, @function\n\
169 _dl_runtime_profile:\n\
170 pushq %rax # Preserve registers otherwise clobbered.\n\
177 movq 72(%rsp), %rdx # Load return address if needed\n\
178 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
179 movq %rsi,%r11 # Multiply by 24\n\
183 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
184 call profile_fixup # Call resolver.\n\
185 movq %rax, %r11 # Save return value\n\
186 popq %r9 # Get register content back.\n\
193 addq $16,%rsp # Adjust stack\n\
194 jmp *%r11 # Jump to function address.\n\
195 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
199 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
201 .globl _dl_runtime_resolve\n\
202 .globl _dl_runtime_profile\n\
203 .type _dl_runtime_resolve, @function\n\
204 .type _dl_runtime_profile, @function\n\
206 _dl_runtime_resolve:\n\
207 _dl_runtime_profile:\n\
208 pushq %rax # Preserve registers otherwise clobbered.\n\
215 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
216 movq %rsi,%r11 # Multiply by 24\n\
220 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
221 call fixup # Call resolver.\n\
222 movq %rax, %r11 # Save return value\n\
223 popq %r9 # Get register content back.\n\
230 addq $16,%rsp # Adjust stack\n\
231 jmp *%r11 # Jump to function address.\n\
232 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
233 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
238 /* Initial entry point code for the dynamic linker.
239 The C function `_dl_start' is the real entry point;
240 its return value is the user program's entry point. */
241 #define RTLD_START asm ("\n\
245 .globl _dl_start_user\n\
250 # Save the user entry point address in %r12.\n\
252 # Store the highest stack address\n\
253 movq __libc_stack_end@GOTPCREL(%rip), %rax\n\
255 # See if we were run as a command with the executable file\n\
256 # name as an extra leading argument.\n\
257 movq _dl_skip_args@GOTPCREL(%rip), %rax\n\
259 # Pop the original argument count.\n\
261 # Adjust the stack pointer to skip _dl_skip_args words.\n\
262 leaq (%rsp,%rax,8), %rsp\n\
263 # Subtract _dl_skip_args from argc.\n\
265 # Push argc back on the stack.\n\
267 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
270 # _dl_loaded -> rdi\n\
271 movq _rtld_local@GOTPCREL(%rip), %rdi\n\
274 leaq 16(%rsp,%rdx,8), %rcx\n\
276 leaq 8(%rsp), %rdx\n\
277 # Call the function to run the initializers.\n\
278 call _dl_init_internal@PLT\n\
279 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
280 movq _dl_fini@GOTPCREL(%rip), %rdx\n\
281 # Jump to the user's entry point.\n\
286 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
287 TLS variable, so undefined references should not be allowed to
289 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
290 of the main executable's symbols, as for a COPY reloc. */
291 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
292 # define elf_machine_type_class(type) \
293 ((((type) == R_X86_64_JUMP_SLOT \
294 || (type) == R_X86_64_DTPMOD64 \
295 || (type) == R_X86_64_DTPOFF64 || (type) == R_X86_64_TPOFF64) \
296 * ELF_RTYPE_CLASS_PLT) \
297 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
299 # define elf_machine_type_class(type) \
300 ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
301 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
304 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
305 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
307 /* The x86-64 never uses Elf64_Rel relocations. */
308 #define ELF_MACHINE_NO_REL 1
310 /* We define an initialization functions. This is called very early in
312 #define DL_PLATFORM_INIT dl_platform_init ()
314 static inline void __attribute__ ((unused
))
315 dl_platform_init (void)
317 if (GL(dl_platform
) != NULL
&& *GL(dl_platform
) == '\0')
318 /* Avoid an empty string which would disturb us. */
319 GL(dl_platform
) = NULL
;
322 static inline Elf64_Addr
323 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
324 const Elf64_Rela
*reloc
,
325 Elf64_Addr
*reloc_addr
, Elf64_Addr value
)
327 return *reloc_addr
= value
;
330 /* Return the final value of a plt relocation. On x86-64 the
331 JUMP_SLOT relocation ignores the addend. */
332 static inline Elf64_Addr
333 elf_machine_plt_value (struct link_map
*map
, const Elf64_Rela
*reloc
,
339 #endif /* !dl_machine_h */
343 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
344 MAP is the object containing the reloc. */
347 elf_machine_rela (struct link_map
*map
, const Elf64_Rela
*reloc
,
348 const Elf64_Sym
*sym
, const struct r_found_version
*version
,
349 Elf64_Addr
*const reloc_addr
)
351 const unsigned long int r_type
= ELF64_R_TYPE (reloc
->r_info
);
353 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
354 if (__builtin_expect (r_type
== R_X86_64_RELATIVE
, 0))
356 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
357 /* This is defined in rtld.c, but nowhere in the static libc.a;
358 make the reference weak so static programs can still link.
359 This declaration cannot be done when compiling rtld.c
360 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
361 common defn for _dl_rtld_map, which is incompatible with a
362 weak decl in the same file. */
364 weak_extern (GL(dl_rtld_map
));
366 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
368 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
372 if (__builtin_expect (r_type
== R_X86_64_NONE
, 0))
376 #ifndef RTLD_BOOTSTRAP
377 const Elf64_Sym
*const refsym
= sym
;
379 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
380 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
381 Elf64_Addr value
= (sym
== NULL
? 0
382 : (Elf64_Addr
) sym_map
->l_addr
+ sym
->st_value
);
384 Elf64_Addr value
= RESOLVE (&sym
, version
, r_type
);
386 # ifndef RTLD_BOOTSTRAP
389 value
+= sym
->st_value
;
392 #if defined RTLD_BOOTSTRAP && !USE___THREAD
393 assert (r_type
== R_X86_64_GLOB_DAT
|| r_type
== R_X86_64_JUMP_SLOT
);
394 *reloc_addr
= value
+ reloc
->r_addend
;
398 case R_X86_64_GLOB_DAT
:
399 case R_X86_64_JUMP_SLOT
:
400 *reloc_addr
= value
+ reloc
->r_addend
;
403 #if defined USE_TLS && !defined RESOLVE_CONFLICT_FIND_MAP
404 case R_X86_64_DTPMOD64
:
405 # ifdef RTLD_BOOTSTRAP
406 /* During startup the dynamic linker is always the module
408 XXX If this relocation is necessary move before RESOLVE
412 /* Get the information from the link map returned by the
415 *reloc_addr
= sym_map
->l_tls_modid
;
418 case R_X86_64_DTPOFF64
:
419 # ifndef RTLD_BOOTSTRAP
420 /* During relocation all TLS symbols are defined and used.
421 Therefore the offset is already correct. */
423 *reloc_addr
= sym
->st_value
+ reloc
->r_addend
;
426 case R_X86_64_TPOFF64
:
427 /* The offset is negative, forward from the thread pointer. */
428 # ifndef RTLD_BOOTSTRAP
432 # ifndef RTLD_BOOTSTRAP
433 CHECK_STATIC_TLS (map
, sym_map
);
435 /* We know the offset of the object the symbol is contained in.
436 It is a negative value which will be added to the
438 *reloc_addr
= (sym
->st_value
+ reloc
->r_addend
439 - sym_map
->l_tls_offset
);
444 #ifndef RTLD_BOOTSTRAP
446 *reloc_addr
= value
+ reloc
->r_addend
;
449 *(unsigned int *) reloc_addr
= value
+ reloc
->r_addend
;
450 if (value
+ reloc
->r_addend
> UINT_MAX
)
454 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
457 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n",
458 rtld_progname
?: "<program name unknown>",
459 strtab
+ refsym
->st_name
);
462 # ifndef RESOLVE_CONFLICT_FIND_MAP
463 /* Not needed for dl-conflict.c. */
465 *(unsigned int *) reloc_addr
= value
+ reloc
->r_addend
466 - (Elf64_Addr
) reloc_addr
;
467 if (value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
468 != (int)(value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
))
472 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
475 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n",
476 rtld_progname
?: "<program name unknown>",
477 strtab
+ refsym
->st_name
);
482 /* This can happen in trace mode if an object could not be
485 if (__builtin_expect (sym
->st_size
> refsym
->st_size
, 0)
486 || (__builtin_expect (sym
->st_size
< refsym
->st_size
, 0)
491 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
493 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
494 rtld_progname
?: "<program name unknown>",
495 strtab
+ refsym
->st_name
);
497 memcpy (reloc_addr
, (void *) value
, MIN (sym
->st_size
,
502 _dl_reloc_bad_type (map
, r_type
, 0);
511 elf_machine_rela_relative (Elf64_Addr l_addr
, const Elf64_Rela
*reloc
,
512 Elf64_Addr
*const reloc_addr
)
514 assert (ELF64_R_TYPE (reloc
->r_info
) == R_X86_64_RELATIVE
);
515 *reloc_addr
= l_addr
+ reloc
->r_addend
;
519 elf_machine_lazy_rel (struct link_map
*map
,
520 Elf64_Addr l_addr
, const Elf64_Rela
*reloc
)
522 Elf64_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
523 const unsigned long int r_type
= ELF64_R_TYPE (reloc
->r_info
);
525 /* Check for unexpected PLT reloc type. */
526 if (__builtin_expect (r_type
== R_X86_64_JUMP_SLOT
, 1))
528 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
529 *reloc_addr
+= l_addr
;
533 + (((Elf64_Addr
) reloc_addr
) - map
->l_mach
.gotplt
) * 2;
536 _dl_reloc_bad_type (map
, r_type
, 1);
This page took 0.057299 seconds and 5 git commands to generate.