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)
42 register Elf64_Addr addr
;
44 asm ("leaq _DYNAMIC, %0\n" : "=r" (addr
));
49 /* Return the run-time load address of the shared object. */
50 static inline Elf64_Addr
__attribute__ ((unused
))
51 elf_machine_load_address (void)
53 register Elf64_Addr addr
, tmp
;
55 asm ("leaq _dl_start, %0\n"
56 "leaq _dl_start(%%rip), %1\n"
58 : "=r" (tmp
), "=r" (addr
) : : "cc");
62 /* Set up the loaded object described by L so its unrelocated PLT
63 entries will jump to the on-demand fixup code in dl-runtime.c. */
65 static inline int __attribute__ ((unused
))
66 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
69 extern void _dl_runtime_resolve (Elf64_Word
);
70 extern void _dl_runtime_profile (Elf64_Word
);
72 if (l
->l_info
[DT_JMPREL
] && lazy
)
74 /* The GOT entries for functions in the PLT have not yet been filled
75 in. Their initial contents will arrange when called to push an
76 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
77 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
78 got
= (Elf64_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
79 /* If a library is prelinked but we have to relocate anyway,
80 we have to be able to undo the prelinking of .got.plt.
81 The prelinker saved us here address of .plt + 0x16. */
84 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
85 l
->l_mach
.gotplt
= (Elf64_Addr
) &got
[3];
87 got
[1] = (Elf64_Addr
) l
; /* Identify this shared object. */
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 (__builtin_expect (profile
, 0))
97 got
[2] = (Elf64_Addr
) &_dl_runtime_profile
;
99 if (_dl_name_match_p (GL(dl_profile
), l
))
100 /* This is the object we are looking for. Say that we really
101 want profiling and the timers are started. */
102 GL(dl_profile_map
) = l
;
105 /* This function will get called to fix up the GOT entry indicated by
106 the offset on the stack, and then jump to the resolved address. */
107 got
[2] = (Elf64_Addr
) &_dl_runtime_resolve
;
113 /* This code is used in dl-runtime.c to call the `fixup' function
114 and then redirect to the address it returns. */
116 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
118 .globl _dl_runtime_resolve\n\
119 .type _dl_runtime_resolve, @function\n\
121 _dl_runtime_resolve:\n\
122 pushq %rax # Preserve registers otherwise clobbered.\n\
129 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
130 movq %rsi,%r11 # Multiply by 24\n\
134 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
135 call fixup # Call resolver.\n\
136 movq %rax, %r11 # Save return value\n\
137 popq %r9 # Get register content back.\n\
144 addq $16,%rsp # Adjust stack\n\
145 jmp *%r11 # Jump to function address.\n\
146 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
148 .globl _dl_runtime_profile\n\
149 .type _dl_runtime_profile, @function\n\
151 _dl_runtime_profile:\n\
152 pushq %rax # Preserve registers otherwise clobbered.\n\
159 movq 72(%rsp), %rdx # Load return address if needed\n\
160 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
161 movq %rsi,%r11 # Multiply by 24\n\
165 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
166 call profile_fixup # Call resolver.\n\
167 movq %rax, %r11 # Save return value\n\
168 popq %r9 # Get register content back.\n\
175 addq $16,%rsp # Adjust stack\n\
176 jmp *%r11 # Jump to function address.\n\
177 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
181 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
183 .globl _dl_runtime_resolve\n\
184 .globl _dl_runtime_profile\n\
185 .type _dl_runtime_resolve, @function\n\
186 .type _dl_runtime_profile, @function\n\
188 _dl_runtime_resolve:\n\
189 _dl_runtime_profile:\n\
190 pushq %rax # Preserve registers otherwise clobbered.\n\
197 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
198 movq %rsi,%r11 # Multiply by 24\n\
202 movq 56(%rsp), %rdi # %rdi: link_map, %rsi: reloc_offset\n\
203 call fixup # Call resolver.\n\
204 movq %rax, %r11 # Save return value\n\
205 popq %r9 # Get register content back.\n\
212 addq $16,%rsp # Adjust stack\n\
213 jmp *%r11 # Jump to function address.\n\
214 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
215 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
220 /* Initial entry point code for the dynamic linker.
221 The C function `_dl_start' is the real entry point;
222 its return value is the user program's entry point. */
223 #define RTLD_START asm ("\n\
227 .globl _dl_start_user\n\
232 # Save the user entry point address in %r12.\n\
234 # Store the highest stack address\n\
235 movq __libc_stack_end@GOTPCREL(%rip), %rax\n\
237 # See if we were run as a command with the executable file\n\
238 # name as an extra leading argument.\n\
239 movq _dl_skip_args@GOTPCREL(%rip), %rax\n\
241 # Pop the original argument count.\n\
243 # Adjust the stack pointer to skip _dl_skip_args words.\n\
244 leaq (%rsp,%rax,8), %rsp\n\
245 # Subtract _dl_skip_args from argc.\n\
247 # Push argc back on the stack.\n\
249 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
252 # _dl_loaded -> rdi\n\
253 movq _rtld_global@GOTPCREL(%rip), %rdi\n\
256 leaq 16(%rsp,%rdx,8), %rcx\n\
258 leaq 8(%rsp), %rdx\n\
259 # Call the function to run the initializers.\n\
261 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
262 movq _dl_fini@GOTPCREL(%rip), %rdx\n\
263 # Jump to the user's entry point.\n\
268 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
269 PLT entries should not be allowed to define the value.
270 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
271 of the main executable's symbols, as for a COPY reloc. */
272 #define elf_machine_type_class(type) \
273 ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
274 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
276 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
277 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
279 /* The x86-64 never uses Elf64_Rel relocations. */
280 #define ELF_MACHINE_NO_REL 1
282 /* We define an initialization functions. This is called very early in
284 #define DL_PLATFORM_INIT dl_platform_init ()
286 static inline void __attribute__ ((unused
))
287 dl_platform_init (void)
289 if (GL(dl_platform
) != NULL
&& *GL(dl_platform
) == '\0')
290 /* Avoid an empty string which would disturb us. */
291 GL(dl_platform
) = NULL
;
294 static inline Elf64_Addr
295 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
296 const Elf64_Rela
*reloc
,
297 Elf64_Addr
*reloc_addr
, Elf64_Addr value
)
299 return *reloc_addr
= value
;
302 /* Return the final value of a plt relocation. On x86-64 the
303 JUMP_SLOT relocation ignores the addend. */
304 static inline Elf64_Addr
305 elf_machine_plt_value (struct link_map
*map
, const Elf64_Rela
*reloc
,
311 #endif /* !dl_machine_h */
315 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
316 MAP is the object containing the reloc. */
319 elf_machine_rela (struct link_map
*map
, const Elf64_Rela
*reloc
,
320 const Elf64_Sym
*sym
, const struct r_found_version
*version
,
321 Elf64_Addr
*const reloc_addr
)
323 const unsigned long int r_type
= ELF64_R_TYPE (reloc
->r_info
);
325 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
326 if (__builtin_expect (r_type
== R_X86_64_RELATIVE
, 0))
328 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
329 /* This is defined in rtld.c, but nowhere in the static libc.a;
330 make the reference weak so static programs can still link.
331 This declaration cannot be done when compiling rtld.c
332 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
333 common defn for _dl_rtld_map, which is incompatible with a
334 weak decl in the same file. */
336 weak_extern (GL(dl_rtld_map
));
338 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
340 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
344 if (__builtin_expect (r_type
== R_X86_64_NONE
, 0))
348 #ifndef RTLD_BOOTSTRAP
349 const Elf64_Sym
*const refsym
= sym
;
351 Elf64_Addr value
= RESOLVE (&sym
, version
, r_type
);
353 value
+= sym
->st_value
;
355 #ifdef RTLD_BOOTSTRAP
356 assert (r_type
== R_X86_64_GLOB_DAT
|| r_type
== R_X86_64_JUMP_SLOT
);
357 *reloc_addr
= value
+ reloc
->r_addend
;
361 case R_X86_64_GLOB_DAT
:
362 case R_X86_64_JUMP_SLOT
:
363 *reloc_addr
= value
+ reloc
->r_addend
;
366 *reloc_addr
= value
+ reloc
->r_addend
;
369 *(unsigned int *) reloc_addr
= value
+ reloc
->r_addend
;
372 *(unsigned int *) reloc_addr
= value
+ reloc
->r_addend
373 - (Elf64_Addr
) reloc_addr
;
377 /* This can happen in trace mode if an object could not be
380 if (__builtin_expect (sym
->st_size
> refsym
->st_size
, 0)
381 || (__builtin_expect (sym
->st_size
< refsym
->st_size
, 0)
386 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
388 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
389 _dl_argv
[0] ?: "<program name unknown>",
390 strtab
+ refsym
->st_name
);
392 memcpy (reloc_addr
, (void *) value
, MIN (sym
->st_size
,
396 _dl_reloc_bad_type (map
, r_type
, 0);
404 elf_machine_rela_relative (Elf64_Addr l_addr
, const Elf64_Rela
*reloc
,
405 Elf64_Addr
*const reloc_addr
)
407 assert (ELF64_R_TYPE (reloc
->r_info
) == R_X86_64_RELATIVE
);
408 *reloc_addr
= l_addr
+ reloc
->r_addend
;
412 elf_machine_lazy_rel (struct link_map
*map
,
413 Elf64_Addr l_addr
, const Elf64_Rela
*reloc
)
415 Elf64_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
416 const unsigned long int r_type
= ELF64_R_TYPE (reloc
->r_info
);
418 /* Check for unexpected PLT reloc type. */
419 if (__builtin_expect (r_type
== R_X86_64_JUMP_SLOT
, 1))
421 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
422 *reloc_addr
+= l_addr
;
426 + (((Elf64_Addr
) reloc_addr
) - map
->l_mach
.gotplt
) * 2;
429 _dl_reloc_bad_type (map
, r_type
, 1);