]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/x86_64/dl-machine.h
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / sysdeps / x86_64 / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001-2006, 2008-2010, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Andreas Jaeger <aj@suse.de>.
5
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.
10
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.
15
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/>. */
19
20 #ifndef dl_machine_h
21 #define dl_machine_h
22
23 #define ELF_MACHINE_NAME "x86_64"
24
25 #include <sys/param.h>
26 #include <sysdep.h>
27 #include <tls.h>
28 #include <dl-tlsdesc.h>
29
30 /* Return nonzero iff ELF header is compatible with the running host. */
31 static inline int __attribute__ ((unused))
32 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
33 {
34 return ehdr->e_machine == EM_X86_64;
35 }
36
37
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
40 uses global data. */
41 static inline Elf64_Addr __attribute__ ((unused))
42 elf_machine_dynamic (void)
43 {
44 Elf64_Addr addr;
45
46 /* This works because we have our GOT address available in the small PIC
47 model. */
48 addr = (Elf64_Addr) &_DYNAMIC;
49
50 return addr;
51 }
52
53
54 /* Return the run-time load address of the shared object. */
55 static inline Elf64_Addr __attribute__ ((unused))
56 elf_machine_load_address (void)
57 {
58 Elf64_Addr addr;
59
60 /* The easy way is just the same as on x86:
61 leaq _dl_start, %0
62 leaq _dl_start(%%rip), %1
63 subq %0, %1
64 but this does not work with binutils since we then have
65 a R_X86_64_32S relocation in a shared lib.
66
67 Instead we store the address of _dl_start in the data section
68 and compare it with the current value that we can get via
69 an RIP relative addressing mode. Note that this is the address
70 of _dl_start before any relocation performed at runtime. In case
71 the binary is prelinked the resulting "address" is actually a
72 load offset which is zero if the binary was loaded at the address
73 it is prelinked for. */
74
75 asm ("leaq _dl_start(%%rip), %0\n\t"
76 "subq 1f(%%rip), %0\n\t"
77 ".section\t.data.rel.ro\n"
78 "1:\t.quad _dl_start\n\t"
79 ".previous\n\t"
80 : "=r" (addr) : : "cc");
81
82 return addr;
83 }
84
85 /* Set up the loaded object described by L so its unrelocated PLT
86 entries will jump to the on-demand fixup code in dl-runtime.c. */
87
88 static inline int __attribute__ ((unused, always_inline))
89 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
90 {
91 Elf64_Addr *got;
92 extern void _dl_runtime_resolve (Elf64_Word) attribute_hidden;
93 extern void _dl_runtime_profile (Elf64_Word) attribute_hidden;
94
95 if (l->l_info[DT_JMPREL] && lazy)
96 {
97 /* The GOT entries for functions in the PLT have not yet been filled
98 in. Their initial contents will arrange when called to push an
99 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
100 and then jump to _GLOBAL_OFFSET_TABLE_[2]. */
101 got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
102 /* If a library is prelinked but we have to relocate anyway,
103 we have to be able to undo the prelinking of .got.plt.
104 The prelinker saved us here address of .plt + 0x16. */
105 if (got[1])
106 {
107 l->l_mach.plt = got[1] + l->l_addr;
108 l->l_mach.gotplt = (Elf64_Addr) &got[3];
109 }
110 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
111
112 /* The got[2] entry contains the address of a function which gets
113 called to get the address of a so far unresolved function and
114 jump to it. The profiling extension of the dynamic linker allows
115 to intercept the calls to collect information. In this case we
116 don't store the address in the GOT so that all future calls also
117 end in this function. */
118 if (__builtin_expect (profile, 0))
119 {
120 got[2] = (Elf64_Addr) &_dl_runtime_profile;
121
122 if (GLRO(dl_profile) != NULL
123 && _dl_name_match_p (GLRO(dl_profile), l))
124 /* This is the object we are looking for. Say that we really
125 want profiling and the timers are started. */
126 GL(dl_profile_map) = l;
127 }
128 else
129 /* This function will get called to fix up the GOT entry indicated by
130 the offset on the stack, and then jump to the resolved address. */
131 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
132 }
133
134 if (l->l_info[ADDRIDX (DT_TLSDESC_GOT)] && lazy)
135 *(Elf64_Addr*)(D_PTR (l, l_info[ADDRIDX (DT_TLSDESC_GOT)]) + l->l_addr)
136 = (Elf64_Addr) &_dl_tlsdesc_resolve_rela;
137
138 return lazy;
139 }
140
141 /* Initial entry point code for the dynamic linker.
142 The C function `_dl_start' is the real entry point;
143 its return value is the user program's entry point. */
144 #define RTLD_START asm ("\n\
145 .text\n\
146 .align 16\n\
147 .globl _start\n\
148 .globl _dl_start_user\n\
149 _start:\n\
150 movq %rsp, %rdi\n\
151 call _dl_start\n\
152 _dl_start_user:\n\
153 # Save the user entry point address in %r12.\n\
154 movq %rax, %r12\n\
155 # See if we were run as a command with the executable file\n\
156 # name as an extra leading argument.\n\
157 movl _dl_skip_args(%rip), %eax\n\
158 # Pop the original argument count.\n\
159 popq %rdx\n\
160 # Adjust the stack pointer to skip _dl_skip_args words.\n\
161 leaq (%rsp,%rax,8), %rsp\n\
162 # Subtract _dl_skip_args from argc.\n\
163 subl %eax, %edx\n\
164 # Push argc back on the stack.\n\
165 pushq %rdx\n\
166 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
167 # argc -> rsi\n\
168 movq %rdx, %rsi\n\
169 # Save %rsp value in %r13.\n\
170 movq %rsp, %r13\n\
171 # And align stack for the _dl_init_internal call. \n\
172 andq $-16, %rsp\n\
173 # _dl_loaded -> rdi\n\
174 movq _rtld_local(%rip), %rdi\n\
175 # env -> rcx\n\
176 leaq 16(%r13,%rdx,8), %rcx\n\
177 # argv -> rdx\n\
178 leaq 8(%r13), %rdx\n\
179 # Clear %rbp to mark outermost frame obviously even for constructors.\n\
180 xorl %ebp, %ebp\n\
181 # Call the function to run the initializers.\n\
182 call _dl_init_internal@PLT\n\
183 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
184 leaq _dl_fini(%rip), %rdx\n\
185 # And make sure %rsp points to argc stored on the stack.\n\
186 movq %r13, %rsp\n\
187 # Jump to the user's entry point.\n\
188 jmp *%r12\n\
189 .previous\n\
190 ");
191
192 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
193 TLS variable, so undefined references should not be allowed to
194 define the value.
195 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
196 of the main executable's symbols, as for a COPY reloc. */
197 #define elf_machine_type_class(type) \
198 ((((type) == R_X86_64_JUMP_SLOT \
199 || (type) == R_X86_64_DTPMOD64 \
200 || (type) == R_X86_64_DTPOFF64 \
201 || (type) == R_X86_64_TPOFF64 \
202 || (type) == R_X86_64_TLSDESC) \
203 * ELF_RTYPE_CLASS_PLT) \
204 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
205
206 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
207 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
208
209 /* The relative ifunc relocation. */
210 // XXX This is a work-around for a broken linker. Remove!
211 #define ELF_MACHINE_IRELATIVE R_X86_64_IRELATIVE
212
213 /* The x86-64 never uses Elf64_Rel relocations. */
214 #define ELF_MACHINE_NO_REL 1
215
216 /* We define an initialization function. This is called very early in
217 _dl_sysdep_start. */
218 #define DL_PLATFORM_INIT dl_platform_init ()
219
220 static inline void __attribute__ ((unused))
221 dl_platform_init (void)
222 {
223 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
224 /* Avoid an empty string which would disturb us. */
225 GLRO(dl_platform) = NULL;
226 }
227
228 static inline Elf64_Addr
229 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
230 const Elf64_Rela *reloc,
231 Elf64_Addr *reloc_addr, Elf64_Addr value)
232 {
233 return *reloc_addr = value;
234 }
235
236 /* Return the final value of a PLT relocation. On x86-64 the
237 JUMP_SLOT relocation ignores the addend. */
238 static inline Elf64_Addr
239 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
240 Elf64_Addr value)
241 {
242 return value;
243 }
244
245
246 /* Names of the architecture-specific auditing callback functions. */
247 #define ARCH_LA_PLTENTER x86_64_gnu_pltenter
248 #define ARCH_LA_PLTEXIT x86_64_gnu_pltexit
249
250 #endif /* !dl_machine_h */
251
252 #ifdef RESOLVE_MAP
253
254 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
255 MAP is the object containing the reloc. */
256
257 auto inline void
258 __attribute__ ((always_inline))
259 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
260 const Elf64_Sym *sym, const struct r_found_version *version,
261 void *const reloc_addr_arg, int skip_ifunc)
262 {
263 Elf64_Addr *const reloc_addr = reloc_addr_arg;
264 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
265
266 # if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
267 if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0))
268 {
269 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
270 /* This is defined in rtld.c, but nowhere in the static libc.a;
271 make the reference weak so static programs can still link.
272 This declaration cannot be done when compiling rtld.c
273 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
274 common defn for _dl_rtld_map, which is incompatible with a
275 weak decl in the same file. */
276 # ifndef SHARED
277 weak_extern (GL(dl_rtld_map));
278 # endif
279 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
280 # endif
281 *reloc_addr = map->l_addr + reloc->r_addend;
282 }
283 else
284 # endif
285 if (__builtin_expect (r_type == R_X86_64_NONE, 0))
286 return;
287 else
288 {
289 # ifndef RTLD_BOOTSTRAP
290 const Elf64_Sym *const refsym = sym;
291 # endif
292 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
293 Elf64_Addr value = (sym == NULL ? 0
294 : (Elf64_Addr) sym_map->l_addr + sym->st_value);
295
296 if (sym != NULL
297 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC,
298 0)
299 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
300 && __builtin_expect (!skip_ifunc, 1))
301 value = ((Elf64_Addr (*) (void)) value) ();
302
303 switch (r_type)
304 {
305 case R_X86_64_GLOB_DAT:
306 case R_X86_64_JUMP_SLOT:
307 *reloc_addr = value + reloc->r_addend;
308 break;
309
310 # ifndef RESOLVE_CONFLICT_FIND_MAP
311 case R_X86_64_DTPMOD64:
312 # ifdef RTLD_BOOTSTRAP
313 /* During startup the dynamic linker is always the module
314 with index 1.
315 XXX If this relocation is necessary move before RESOLVE
316 call. */
317 *reloc_addr = 1;
318 # else
319 /* Get the information from the link map returned by the
320 resolve function. */
321 if (sym_map != NULL)
322 *reloc_addr = sym_map->l_tls_modid;
323 # endif
324 break;
325 case R_X86_64_DTPOFF64:
326 # ifndef RTLD_BOOTSTRAP
327 /* During relocation all TLS symbols are defined and used.
328 Therefore the offset is already correct. */
329 if (sym != NULL)
330 *reloc_addr = sym->st_value + reloc->r_addend;
331 # endif
332 break;
333 case R_X86_64_TLSDESC:
334 {
335 struct tlsdesc volatile *td =
336 (struct tlsdesc volatile *)reloc_addr;
337
338 # ifndef RTLD_BOOTSTRAP
339 if (! sym)
340 {
341 td->arg = (void*)reloc->r_addend;
342 td->entry = _dl_tlsdesc_undefweak;
343 }
344 else
345 # endif
346 {
347 # ifndef RTLD_BOOTSTRAP
348 # ifndef SHARED
349 CHECK_STATIC_TLS (map, sym_map);
350 # else
351 if (!TRY_STATIC_TLS (map, sym_map))
352 {
353 td->arg = _dl_make_tlsdesc_dynamic
354 (sym_map, sym->st_value + reloc->r_addend);
355 td->entry = _dl_tlsdesc_dynamic;
356 }
357 else
358 # endif
359 # endif
360 {
361 td->arg = (void*)(sym->st_value - sym_map->l_tls_offset
362 + reloc->r_addend);
363 td->entry = _dl_tlsdesc_return;
364 }
365 }
366 break;
367 }
368 case R_X86_64_TPOFF64:
369 /* The offset is negative, forward from the thread pointer. */
370 # ifndef RTLD_BOOTSTRAP
371 if (sym != NULL)
372 # endif
373 {
374 # ifndef RTLD_BOOTSTRAP
375 CHECK_STATIC_TLS (map, sym_map);
376 # endif
377 /* We know the offset of the object the symbol is contained in.
378 It is a negative value which will be added to the
379 thread pointer. */
380 *reloc_addr = (sym->st_value + reloc->r_addend
381 - sym_map->l_tls_offset);
382 }
383 break;
384 # endif
385
386 # ifndef RTLD_BOOTSTRAP
387 case R_X86_64_64:
388 *reloc_addr = value + reloc->r_addend;
389 break;
390 case R_X86_64_32:
391 value += reloc->r_addend;
392 *(unsigned int *) reloc_addr = value;
393
394 const char *fmt;
395 if (__builtin_expect (value > UINT_MAX, 0))
396 {
397 const char *strtab;
398
399 fmt = "\
400 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n";
401 # ifndef RESOLVE_CONFLICT_FIND_MAP
402 print_err:
403 # endif
404 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
405
406 _dl_error_printf (fmt,
407 rtld_progname ?: "<program name unknown>",
408 strtab + refsym->st_name);
409 }
410 break;
411 # ifndef RESOLVE_CONFLICT_FIND_MAP
412 /* Not needed for dl-conflict.c. */
413 case R_X86_64_PC32:
414 value += reloc->r_addend - (Elf64_Addr) reloc_addr;
415 *(unsigned int *) reloc_addr = value;
416 if (__builtin_expect (value != (int) value, 0))
417 {
418 fmt = "\
419 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n";
420 goto print_err;
421 }
422 break;
423 case R_X86_64_COPY:
424 if (sym == NULL)
425 /* This can happen in trace mode if an object could not be
426 found. */
427 break;
428 memcpy (reloc_addr_arg, (void *) value,
429 MIN (sym->st_size, refsym->st_size));
430 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
431 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
432 && GLRO(dl_verbose)))
433 {
434 fmt = "\
435 %s: Symbol `%s' has different size in shared object, consider re-linking\n";
436 goto print_err;
437 }
438 break;
439 # endif
440 case R_X86_64_IRELATIVE:
441 value = map->l_addr + reloc->r_addend;
442 value = ((Elf64_Addr (*) (void)) value) ();
443 *reloc_addr = value;
444 break;
445 default:
446 _dl_reloc_bad_type (map, r_type, 0);
447 break;
448 # endif
449 }
450 }
451 }
452
453 auto inline void
454 __attribute ((always_inline))
455 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
456 void *const reloc_addr_arg)
457 {
458 Elf64_Addr *const reloc_addr = reloc_addr_arg;
459 assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
460 *reloc_addr = l_addr + reloc->r_addend;
461 }
462
463 auto inline void
464 __attribute ((always_inline))
465 elf_machine_lazy_rel (struct link_map *map,
466 Elf64_Addr l_addr, const Elf64_Rela *reloc,
467 int skip_ifunc)
468 {
469 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
470 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
471
472 /* Check for unexpected PLT reloc type. */
473 if (__builtin_expect (r_type == R_X86_64_JUMP_SLOT, 1))
474 {
475 if (__builtin_expect (map->l_mach.plt, 0) == 0)
476 *reloc_addr += l_addr;
477 else
478 *reloc_addr =
479 map->l_mach.plt
480 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 2;
481 }
482 else if (__builtin_expect (r_type == R_X86_64_TLSDESC, 1))
483 {
484 struct tlsdesc volatile * __attribute__((__unused__)) td =
485 (struct tlsdesc volatile *)reloc_addr;
486
487 td->arg = (void*)reloc;
488 td->entry = (void*)(D_PTR (map, l_info[ADDRIDX (DT_TLSDESC_PLT)])
489 + map->l_addr);
490 }
491 else if (__builtin_expect (r_type == R_X86_64_IRELATIVE, 0))
492 {
493 Elf64_Addr value = map->l_addr + reloc->r_addend;
494 if (__builtin_expect (!skip_ifunc, 1))
495 value = ((Elf64_Addr (*) (void)) value) ();
496 *reloc_addr = value;
497 }
498 else
499 _dl_reloc_bad_type (map, r_type, 1);
500 }
501
502 #endif /* RESOLVE_MAP */