(_exit): Don't use ABORT_INSTRUCTION if it is not defined.
[glibc.git] / sysdeps / x86_64 / dl-machine.h
blob2800a9e9584e60e618b974e23a636d9ea1c1f804
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
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>
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
38 uses global data. */
39 static inline Elf64_Addr __attribute__ ((unused))
40 elf_machine_dynamic (void)
42 Elf64_Addr addr;
44 /* This works because we have our GOT address available in the small PIC
45 model. */
46 addr = (Elf64_Addr) &_DYNAMIC;
48 return addr;
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:
59 leaq _dl_start, %0
60 leaq _dl_start(%%rip), %1
61 subq %0, %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"
71 "subq %1, %0\n\t"
72 ".section\t.data\n"
73 "1:\t.quad _dl_start\n\t"
74 ".previous\n\t"
75 : "=r" (addr), "=r" (tmp) : : "cc");
77 return addr;
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)
86 Elf64_Addr *got;
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. */
100 if (got[1])
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;
122 else
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;
128 return lazy;
131 /* This code is used in dl-runtime.c to call the `fixup' function
132 and then redirect to the address it returns. */
133 #ifndef PROF
134 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
135 .text\n\
136 .globl _dl_runtime_resolve\n\
137 .type _dl_runtime_resolve, @function\n\
138 .align 16\n\
139 _dl_runtime_resolve:\n\
140 pushq %rax # Preserve registers otherwise clobbered.\n\
141 pushq %rcx\n\
142 pushq %rdx\n\
143 pushq %rsi\n\
144 pushq %rdi\n\
145 pushq %r8\n\
146 pushq %r9\n\
147 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
148 movq %rsi,%r11 # Multiply by 24\n\
149 addq %r11,%rsi\n\
150 addq %r11,%rsi\n\
151 shlq $3, %rsi\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\
156 popq %r8\n\
157 popq %rdi\n\
158 popq %rsi\n\
159 popq %rdx\n\
160 popq %rcx\n\
161 popq %rax\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\
168 .align 16\n\
169 _dl_runtime_profile:\n\
170 pushq %rax # Preserve registers otherwise clobbered.\n\
171 pushq %rcx\n\
172 pushq %rdx\n\
173 pushq %rsi\n\
174 pushq %rdi\n\
175 pushq %r8\n\
176 pushq %r9\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\
180 addq %r11,%rsi\n\
181 addq %r11,%rsi\n\
182 shlq $3, %rsi\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\
187 popq %r8\n\
188 popq %rdi\n\
189 popq %rsi\n\
190 popq %rdx\n\
191 popq %rcx\n\
192 popq %rax\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\
196 .previous\n\
198 #else
199 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
200 .text\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\
205 .align 16\n\
206 _dl_runtime_resolve:\n\
207 _dl_runtime_profile:\n\
208 pushq %rax # Preserve registers otherwise clobbered.\n\
209 pushq %rcx\n\
210 pushq %rdx\n\
211 pushq %rsi\n\
212 pushq %rdi\n\
213 pushq %r8\n\
214 pushq %r9\n\
215 movq 64(%rsp), %rsi # Copy args pushed by PLT in register.\n\
216 movq %rsi,%r11 # Multiply by 24\n\
217 addq %r11,%rsi\n\
218 addq %r11,%rsi\n\
219 shlq $3, %rsi\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\
224 popq %r8\n\
225 popq %rdi\n\
226 popq %rsi\n\
227 popq %rdx\n\
228 popq %rcx\n\
229 popq %rax\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\
234 .previous\n\
236 #endif
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\
242 .text\n\
243 .align 16\n\
244 .globl _start\n\
245 .globl _dl_start_user\n\
246 _start:\n\
247 movq %rsp, %rdi\n\
248 call _dl_start\n\
249 _dl_start_user:\n\
250 # Save the user entry point address in %r12.\n\
251 movq %rax, %r12\n\
252 # Store the highest stack address\n\
253 movq __libc_stack_end@GOTPCREL(%rip), %rax\n\
254 movq %rsp, (%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\
258 movl (%rax), %eax\n\
259 # Pop the original argument count.\n\
260 popq %rdx\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\
264 subl %eax, %edx\n\
265 # Push argc back on the stack.\n\
266 pushq %rdx\n\
267 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
268 # argc -> rsi\n\
269 movq %rdx, %rsi\n\
270 # _dl_loaded -> rdi\n\
271 movq _rtld_local@GOTPCREL(%rip), %rdi\n\
272 movq (%rdi), %rdi\n\
273 # env -> rcx\n\
274 leaq 16(%rsp,%rdx,8), %rcx\n\
275 # argv -> rdx\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\
282 jmp *%r12\n\
283 .previous\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
301 _dl_sysdep_start. */
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,
324 Elf64_Addr value)
326 return value;
329 #endif /* !dl_machine_h */
331 #ifdef RESOLVE
333 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
334 MAP is the object containing the reloc. */
336 static inline void
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. */
353 # ifndef SHARED
354 weak_extern (GL(dl_rtld_map));
355 # endif
356 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
357 # endif
358 *reloc_addr = map->l_addr + reloc->r_addend;
360 else
361 #endif
362 if (__builtin_expect (r_type == R_X86_64_NONE, 0))
363 return;
364 else
366 #ifndef RTLD_BOOTSTRAP
367 const Elf64_Sym *const refsym = sym;
368 #endif
369 Elf64_Addr value = RESOLVE (&sym, version, r_type);
371 # ifndef RTLD_BOOTSTRAP
372 if (sym != NULL)
373 # endif
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;
379 #else
380 switch (r_type)
382 case R_X86_64_GLOB_DAT:
383 case R_X86_64_JUMP_SLOT:
384 *reloc_addr = value + reloc->r_addend;
385 break;
386 case R_X86_64_64:
387 *reloc_addr = value + reloc->r_addend;
388 break;
389 case R_X86_64_32:
390 *(unsigned int *) reloc_addr = value + reloc->r_addend;
391 if (value + reloc->r_addend > UINT_MAX)
393 const char *strtab;
395 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
397 _dl_error_printf ("\
398 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n",
399 rtld_progname ?: "<program name unknown>",
400 strtab + refsym->st_name);
402 break;
403 case R_X86_64_PC32:
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))
409 const char *strtab;
411 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
413 _dl_error_printf ("\
414 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n",
415 rtld_progname ?: "<program name unknown>",
416 strtab + refsym->st_name);
418 break;
419 case R_X86_64_COPY:
420 if (sym == NULL)
421 /* This can happen in trace mode if an object could not be
422 found. */
423 break;
424 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
425 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
426 && GL(dl_verbose)))
428 const char *strtab;
430 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
431 _dl_error_printf ("\
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,
437 refsym->st_size));
438 break;
439 default:
440 _dl_reloc_bad_type (map, r_type, 0);
441 break;
443 #endif
447 static inline void
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;
455 static inline void
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;
467 else
468 *reloc_addr =
469 map->l_mach.plt
470 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 2;
472 else
473 _dl_reloc_bad_type (map, r_type, 1);
476 #endif /* RESOLVE */