]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/x86_64/dl-machine.h
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/>. */
23 #define ELF_MACHINE_NAME "x86_64"
25 #include <sys/param.h>
28 #include <dl-tlsdesc.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 ElfW(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
41 static inline ElfW(Addr
) __attribute__ ((unused
))
42 elf_machine_dynamic (void)
44 /* This produces an IP-relative reloc which is resolved at link time. */
45 extern const ElfW(Addr
) _GLOBAL_OFFSET_TABLE_
[] attribute_hidden
;
46 return _GLOBAL_OFFSET_TABLE_
[0];
50 /* Return the run-time load address of the shared object. */
51 static inline ElfW(Addr
) __attribute__ ((unused
))
52 elf_machine_load_address (void)
54 /* Compute the difference between the runtime address of _DYNAMIC as seen
55 by an IP-relative reference, and the link-time address found in the
56 special unrelocated first GOT entry. */
57 extern ElfW(Dyn
) _DYNAMIC
[] attribute_hidden
;
58 return (ElfW(Addr
)) &_DYNAMIC
- elf_machine_dynamic ();
61 /* Set up the loaded object described by L so its unrelocated PLT
62 entries will jump to the on-demand fixup code in dl-runtime.c. */
64 static inline int __attribute__ ((unused
, always_inline
))
65 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
68 extern void _dl_runtime_resolve (ElfW(Word
)) attribute_hidden
;
69 extern void _dl_runtime_profile (ElfW(Word
)) attribute_hidden
;
71 if (l
->l_info
[DT_JMPREL
] && lazy
)
73 /* The GOT entries for functions in the PLT have not yet been filled
74 in. Their initial contents will arrange when called to push an
75 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
76 and then jump to _GLOBAL_OFFSET_TABLE_[2]. */
77 got
= (Elf64_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
78 /* If a library is prelinked but we have to relocate anyway,
79 we have to be able to undo the prelinking of .got.plt.
80 The prelinker saved us here address of .plt + 0x16. */
83 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
84 l
->l_mach
.gotplt
= (ElfW(Addr
)) &got
[3];
86 /* Identify this shared object. */
87 *(ElfW(Addr
) *) (got
+ 1) = (ElfW(Addr
)) l
;
89 /* The got[2] entry contains the address of a function which gets
90 called to get the address of a so far unresolved function and
91 jump to it. The profiling extension of the dynamic linker allows
92 to intercept the calls to collect information. In this case we
93 don't store the address in the GOT so that all future calls also
94 end in this function. */
95 if (__glibc_unlikely (profile
))
97 *(ElfW(Addr
) *) (got
+ 2) = (ElfW(Addr
)) &_dl_runtime_profile
;
99 if (GLRO(dl_profile
) != NULL
100 && _dl_name_match_p (GLRO(dl_profile
), l
))
101 /* This is the object we are looking for. Say that we really
102 want profiling and the timers are started. */
103 GL(dl_profile_map
) = l
;
106 /* This function will get called to fix up the GOT entry indicated by
107 the offset on the stack, and then jump to the resolved address. */
108 *(ElfW(Addr
) *) (got
+ 2) = (ElfW(Addr
)) &_dl_runtime_resolve
;
111 if (l
->l_info
[ADDRIDX (DT_TLSDESC_GOT
)] && lazy
)
112 *(ElfW(Addr
)*)(D_PTR (l
, l_info
[ADDRIDX (DT_TLSDESC_GOT
)]) + l
->l_addr
)
113 = (ElfW(Addr
)) &_dl_tlsdesc_resolve_rela
;
118 /* Initial entry point code for the dynamic linker.
119 The C function `_dl_start' is the real entry point;
120 its return value is the user program's entry point. */
121 #define RTLD_START asm ("\n\
125 .globl _dl_start_user\n\
130 # Save the user entry point address in %r12.\n\
132 # See if we were run as a command with the executable file\n\
133 # name as an extra leading argument.\n\
134 movl _dl_skip_args(%rip), %eax\n\
135 # Pop the original argument count.\n\
137 # Adjust the stack pointer to skip _dl_skip_args words.\n\
138 leaq (%rsp,%rax,8), %rsp\n\
139 # Subtract _dl_skip_args from argc.\n\
141 # Push argc back on the stack.\n\
143 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
146 # Save %rsp value in %r13.\n\
148 # And align stack for the _dl_init call. \n\
150 # _dl_loaded -> rdi\n\
151 movq _rtld_local(%rip), %rdi\n\
153 leaq 16(%r13,%rdx,8), %rcx\n\
155 leaq 8(%r13), %rdx\n\
156 # Clear %rbp to mark outermost frame obviously even for constructors.\n\
158 # Call the function to run the initializers.\n\
160 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
161 leaq _dl_fini(%rip), %rdx\n\
162 # And make sure %rsp points to argc stored on the stack.\n\
164 # Jump to the user's entry point.\n\
169 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
170 TLS variable, so undefined references should not be allowed to
172 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
173 of the main executable's symbols, as for a COPY reloc.
174 ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA iff TYPE describes relocation may
175 against protected data whose address be external due to copy relocation.
177 #define elf_machine_type_class(type) \
178 ((((type) == R_X86_64_JUMP_SLOT \
179 || (type) == R_X86_64_DTPMOD64 \
180 || (type) == R_X86_64_DTPOFF64 \
181 || (type) == R_X86_64_TPOFF64 \
182 || (type) == R_X86_64_TLSDESC) \
183 * ELF_RTYPE_CLASS_PLT) \
184 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY) \
185 | (((type) == R_X86_64_GLOB_DAT) * ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA))
187 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
188 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
190 /* The relative ifunc relocation. */
191 // XXX This is a work-around for a broken linker. Remove!
192 #define ELF_MACHINE_IRELATIVE R_X86_64_IRELATIVE
194 /* The x86-64 never uses Elf64_Rel/Elf32_Rel relocations. */
195 #define ELF_MACHINE_NO_REL 1
196 #define ELF_MACHINE_NO_RELA 0
198 /* We define an initialization function. This is called very early in
200 #define DL_PLATFORM_INIT dl_platform_init ()
202 static inline void __attribute__ ((unused
))
203 dl_platform_init (void)
205 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
206 /* Avoid an empty string which would disturb us. */
207 GLRO(dl_platform
) = NULL
;
210 static inline ElfW(Addr
)
211 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
212 const ElfW(Rela
) *reloc
,
213 ElfW(Addr
) *reloc_addr
, ElfW(Addr
) value
)
215 return *reloc_addr
= value
;
218 /* Return the final value of a PLT relocation. On x86-64 the
219 JUMP_SLOT relocation ignores the addend. */
220 static inline ElfW(Addr
)
221 elf_machine_plt_value (struct link_map
*map
, const ElfW(Rela
) *reloc
,
228 /* Names of the architecture-specific auditing callback functions. */
229 #define ARCH_LA_PLTENTER x86_64_gnu_pltenter
230 #define ARCH_LA_PLTEXIT x86_64_gnu_pltexit
232 #endif /* !dl_machine_h */
236 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
237 MAP is the object containing the reloc. */
240 __attribute__ ((always_inline
))
241 elf_machine_rela (struct link_map
*map
, const ElfW(Rela
) *reloc
,
242 const ElfW(Sym
) *sym
, const struct r_found_version
*version
,
243 void *const reloc_addr_arg
, int skip_ifunc
)
245 ElfW(Addr
) *const reloc_addr
= reloc_addr_arg
;
246 const unsigned long int r_type
= ELFW(R_TYPE
) (reloc
->r_info
);
248 # if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
249 if (__glibc_unlikely (r_type
== R_X86_64_RELATIVE
))
251 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
252 /* This is defined in rtld.c, but nowhere in the static libc.a;
253 make the reference weak so static programs can still link.
254 This declaration cannot be done when compiling rtld.c
255 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
256 common defn for _dl_rtld_map, which is incompatible with a
257 weak decl in the same file. */
259 weak_extern (GL(dl_rtld_map
));
261 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
263 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
267 # if !defined RTLD_BOOTSTRAP
268 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
269 relocation updates the whole 64-bit entry. */
270 if (__glibc_unlikely (r_type
== R_X86_64_RELATIVE64
))
271 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) map
->l_addr
+ reloc
->r_addend
;
274 if (__glibc_unlikely (r_type
== R_X86_64_NONE
))
278 # ifndef RTLD_BOOTSTRAP
279 const ElfW(Sym
) *const refsym
= sym
;
281 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
282 ElfW(Addr
) value
= (sym
== NULL
? 0
283 : (ElfW(Addr
)) sym_map
->l_addr
+ sym
->st_value
);
286 && __builtin_expect (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
,
288 && __builtin_expect (sym
->st_shndx
!= SHN_UNDEF
, 1)
289 && __builtin_expect (!skip_ifunc
, 1))
290 value
= ((ElfW(Addr
) (*) (void)) value
) ();
294 # ifndef RTLD_BOOTSTRAP
296 case R_X86_64_SIZE64
:
297 /* Set to symbol size plus addend. */
298 *(Elf64_Addr
*) (uintptr_t) reloc_addr
299 = (Elf64_Addr
) sym
->st_size
+ reloc
->r_addend
;
302 case R_X86_64_SIZE32
:
304 case R_X86_64_SIZE64
:
306 /* Set to symbol size plus addend. */
307 value
= sym
->st_size
;
309 case R_X86_64_GLOB_DAT
:
310 case R_X86_64_JUMP_SLOT
:
311 *reloc_addr
= value
+ reloc
->r_addend
;
314 # ifndef RESOLVE_CONFLICT_FIND_MAP
315 case R_X86_64_DTPMOD64
:
316 # ifdef RTLD_BOOTSTRAP
317 /* During startup the dynamic linker is always the module
319 XXX If this relocation is necessary move before RESOLVE
323 /* Get the information from the link map returned by the
326 *reloc_addr
= sym_map
->l_tls_modid
;
329 case R_X86_64_DTPOFF64
:
330 # ifndef RTLD_BOOTSTRAP
331 /* During relocation all TLS symbols are defined and used.
332 Therefore the offset is already correct. */
335 value
= sym
->st_value
+ reloc
->r_addend
;
337 /* This relocation type computes a signed offset that is
338 usually negative. The symbol and addend values are 32
339 bits but the GOT entry is 64 bits wide and the whole
340 64-bit entry is used as a signed quantity, so we need
341 to sign-extend the computed value to 64 bits. */
342 *(Elf64_Sxword
*) reloc_addr
= (Elf64_Sxword
) (Elf32_Sword
) value
;
349 case R_X86_64_TLSDESC
:
351 struct tlsdesc
volatile *td
=
352 (struct tlsdesc
volatile *)reloc_addr
;
354 # ifndef RTLD_BOOTSTRAP
357 td
->arg
= (void*)reloc
->r_addend
;
358 td
->entry
= _dl_tlsdesc_undefweak
;
363 # ifndef RTLD_BOOTSTRAP
365 CHECK_STATIC_TLS (map
, sym_map
);
367 if (!TRY_STATIC_TLS (map
, sym_map
))
369 td
->arg
= _dl_make_tlsdesc_dynamic
370 (sym_map
, sym
->st_value
+ reloc
->r_addend
);
371 td
->entry
= _dl_tlsdesc_dynamic
;
377 td
->arg
= (void*)(sym
->st_value
- sym_map
->l_tls_offset
379 td
->entry
= _dl_tlsdesc_return
;
384 case R_X86_64_TPOFF64
:
385 /* The offset is negative, forward from the thread pointer. */
386 # ifndef RTLD_BOOTSTRAP
390 # ifndef RTLD_BOOTSTRAP
391 CHECK_STATIC_TLS (map
, sym_map
);
393 /* We know the offset of the object the symbol is contained in.
394 It is a negative value which will be added to the
396 value
= (sym
->st_value
+ reloc
->r_addend
397 - sym_map
->l_tls_offset
);
399 /* The symbol and addend values are 32 bits but the GOT
400 entry is 64 bits wide and the whole 64-bit entry is used
401 as a signed quantity, so we need to sign-extend the
402 computed value to 64 bits. */
403 *(Elf64_Sxword
*) reloc_addr
= (Elf64_Sxword
) (Elf32_Sword
) value
;
411 # ifndef RTLD_BOOTSTRAP
413 /* value + r_addend may be > 0xffffffff and R_X86_64_64
414 relocation updates the whole 64-bit entry. */
415 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) value
+ reloc
->r_addend
;
418 case R_X86_64_SIZE32
:
419 /* Set to symbol size plus addend. */
420 value
= sym
->st_size
;
423 value
+= reloc
->r_addend
;
424 *(unsigned int *) reloc_addr
= value
;
427 if (__glibc_unlikely (value
> UINT_MAX
))
432 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n";
433 # ifndef RESOLVE_CONFLICT_FIND_MAP
436 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
438 _dl_error_printf (fmt
, RTLD_PROGNAME
, strtab
+ refsym
->st_name
);
441 # ifndef RESOLVE_CONFLICT_FIND_MAP
442 /* Not needed for dl-conflict.c. */
444 value
+= reloc
->r_addend
- (ElfW(Addr
)) reloc_addr
;
445 *(unsigned int *) reloc_addr
= value
;
446 if (__glibc_unlikely (value
!= (int) value
))
449 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n";
455 /* This can happen in trace mode if an object could not be
458 memcpy (reloc_addr_arg
, (void *) value
,
459 MIN (sym
->st_size
, refsym
->st_size
));
460 if (__builtin_expect (sym
->st_size
> refsym
->st_size
, 0)
461 || (__builtin_expect (sym
->st_size
< refsym
->st_size
, 0)
462 && GLRO(dl_verbose
)))
465 %s: Symbol `%s' has different size in shared object, consider re-linking\n";
470 case R_X86_64_IRELATIVE
:
471 value
= map
->l_addr
+ reloc
->r_addend
;
472 value
= ((ElfW(Addr
) (*) (void)) value
) ();
476 _dl_reloc_bad_type (map
, r_type
, 0);
484 __attribute ((always_inline
))
485 elf_machine_rela_relative (ElfW(Addr
) l_addr
, const ElfW(Rela
) *reloc
,
486 void *const reloc_addr_arg
)
488 ElfW(Addr
) *const reloc_addr
= reloc_addr_arg
;
489 #if !defined RTLD_BOOTSTRAP
490 /* l_addr + r_addend may be > 0xffffffff and R_X86_64_RELATIVE64
491 relocation updates the whole 64-bit entry. */
492 if (__glibc_unlikely (ELFW(R_TYPE
) (reloc
->r_info
) == R_X86_64_RELATIVE64
))
493 *(Elf64_Addr
*) reloc_addr
= (Elf64_Addr
) l_addr
+ reloc
->r_addend
;
497 assert (ELFW(R_TYPE
) (reloc
->r_info
) == R_X86_64_RELATIVE
);
498 *reloc_addr
= l_addr
+ reloc
->r_addend
;
503 __attribute ((always_inline
))
504 elf_machine_lazy_rel (struct link_map
*map
,
505 ElfW(Addr
) l_addr
, const ElfW(Rela
) *reloc
,
508 ElfW(Addr
) *const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
509 const unsigned long int r_type
= ELFW(R_TYPE
) (reloc
->r_info
);
511 /* Check for unexpected PLT reloc type. */
512 if (__glibc_likely (r_type
== R_X86_64_JUMP_SLOT
))
514 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
515 *reloc_addr
+= l_addr
;
519 + (((ElfW(Addr
)) reloc_addr
) - map
->l_mach
.gotplt
) * 2;
521 else if (__glibc_likely (r_type
== R_X86_64_TLSDESC
))
523 struct tlsdesc
volatile * __attribute__((__unused__
)) td
=
524 (struct tlsdesc
volatile *)reloc_addr
;
526 td
->arg
= (void*)reloc
;
527 td
->entry
= (void*)(D_PTR (map
, l_info
[ADDRIDX (DT_TLSDESC_PLT
)])
530 else if (__glibc_unlikely (r_type
== R_X86_64_IRELATIVE
))
532 ElfW(Addr
) value
= map
->l_addr
+ reloc
->r_addend
;
533 if (__glibc_likely (!skip_ifunc
))
534 value
= ((ElfW(Addr
) (*) (void)) value
) ();
538 _dl_reloc_bad_type (map
, r_type
, 1);
541 #endif /* RESOLVE_MAP */