1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001, 2002 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, so
287 PLT entries should not be allowed to define the value.
288 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
289 of the main executable's symbols, as for a COPY reloc. */
290 #define elf_machine_type_class(type) \
291 ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
292 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
294 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
295 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
297 /* The x86-64 never uses Elf64_Rel relocations. */
298 #define ELF_MACHINE_NO_REL 1
300 /* We define an initialization functions. This is called very early in
302 #define DL_PLATFORM_INIT dl_platform_init ()
304 static inline void __attribute__ ((unused
))
305 dl_platform_init (void)
307 if (GL(dl_platform
) != NULL
&& *GL(dl_platform
) == '\0')
308 /* Avoid an empty string which would disturb us. */
309 GL(dl_platform
) = NULL
;
312 static inline Elf64_Addr
313 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
314 const Elf64_Rela
*reloc
,
315 Elf64_Addr
*reloc_addr
, Elf64_Addr value
)
317 return *reloc_addr
= value
;
320 /* Return the final value of a plt relocation. On x86-64 the
321 JUMP_SLOT relocation ignores the addend. */
322 static inline Elf64_Addr
323 elf_machine_plt_value (struct link_map
*map
, const Elf64_Rela
*reloc
,
329 #endif /* !dl_machine_h */
333 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
334 MAP is the object containing the reloc. */
337 elf_machine_rela (struct link_map
*map
, const Elf64_Rela
*reloc
,
338 const Elf64_Sym
*sym
, const struct r_found_version
*version
,
339 Elf64_Addr
*const reloc_addr
)
341 const unsigned long int r_type
= ELF64_R_TYPE (reloc
->r_info
);
343 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
344 if (__builtin_expect (r_type
== R_X86_64_RELATIVE
, 0))
346 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
347 /* This is defined in rtld.c, but nowhere in the static libc.a;
348 make the reference weak so static programs can still link.
349 This declaration cannot be done when compiling rtld.c
350 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
351 common defn for _dl_rtld_map, which is incompatible with a
352 weak decl in the same file. */
354 weak_extern (GL(dl_rtld_map
));
356 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
358 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
362 if (__builtin_expect (r_type
== R_X86_64_NONE
, 0))
366 #ifndef RTLD_BOOTSTRAP
367 const Elf64_Sym
*const refsym
= sym
;
369 Elf64_Addr value
= RESOLVE (&sym
, version
, r_type
);
371 # ifndef RTLD_BOOTSTRAP
374 value
+= sym
->st_value
;
376 #ifdef RTLD_BOOTSTRAP
377 assert (r_type
== R_X86_64_GLOB_DAT
|| r_type
== R_X86_64_JUMP_SLOT
);
378 *reloc_addr
= value
+ reloc
->r_addend
;
382 case R_X86_64_GLOB_DAT
:
383 case R_X86_64_JUMP_SLOT
:
384 *reloc_addr
= value
+ reloc
->r_addend
;
387 *reloc_addr
= value
+ reloc
->r_addend
;
390 *(unsigned int *) reloc_addr
= value
+ reloc
->r_addend
;
391 if (value
+ reloc
->r_addend
> UINT_MAX
)
395 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
398 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n",
399 rtld_progname
?: "<program name unknown>",
400 strtab
+ refsym
->st_name
);
404 *(unsigned int *) reloc_addr
= value
+ reloc
->r_addend
405 - (Elf64_Addr
) reloc_addr
;
406 if (value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
407 != (unsigned int)(value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
))
411 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
414 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n",
415 rtld_progname
?: "<program name unknown>",
416 strtab
+ refsym
->st_name
);
421 /* This can happen in trace mode if an object could not be
424 if (__builtin_expect (sym
->st_size
> refsym
->st_size
, 0)
425 || (__builtin_expect (sym
->st_size
< refsym
->st_size
, 0)
430 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
432 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
433 rtld_progname
?: "<program name unknown>",
434 strtab
+ refsym
->st_name
);
436 memcpy (reloc_addr
, (void *) value
, MIN (sym
->st_size
,
440 _dl_reloc_bad_type (map
, r_type
, 0);
448 elf_machine_rela_relative (Elf64_Addr l_addr
, const Elf64_Rela
*reloc
,
449 Elf64_Addr
*const reloc_addr
)
451 assert (ELF64_R_TYPE (reloc
->r_info
) == R_X86_64_RELATIVE
);
452 *reloc_addr
= l_addr
+ reloc
->r_addend
;
456 elf_machine_lazy_rel (struct link_map
*map
,
457 Elf64_Addr l_addr
, const Elf64_Rela
*reloc
)
459 Elf64_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
460 const unsigned long int r_type
= ELF64_R_TYPE (reloc
->r_info
);
462 /* Check for unexpected PLT reloc type. */
463 if (__builtin_expect (r_type
== R_X86_64_JUMP_SLOT
, 1))
465 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
466 *reloc_addr
+= l_addr
;
470 + (((Elf64_Addr
) reloc_addr
) - map
->l_mach
.gotplt
) * 2;
473 _dl_reloc_bad_type (map
, r_type
, 1);