]> sourceware.org Git - glibc.git/blob - sysdeps/x86_64/dl-machine.h
* elf/dl-reloc.c (allocate_static_tls): Rename to...
[glibc.git] / sysdeps / x86_64 / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001, 2002, 2003 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21 #ifndef dl_machine_h
22 #define dl_machine_h
23
24 #define ELF_MACHINE_NAME "x86_64"
25
26 #include <sys/param.h>
27
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)
31 {
32 return ehdr->e_machine == EM_X86_64;
33 }
34
35
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)
41 {
42 Elf64_Addr addr;
43
44 /* This works because we have our GOT address available in the small PIC
45 model. */
46 addr = (Elf64_Addr) &_DYNAMIC;
47
48 return addr;
49 }
50
51
52 /* Return the run-time load address of the shared object. */
53 static inline Elf64_Addr __attribute__ ((unused))
54 elf_machine_load_address (void)
55 {
56 register Elf64_Addr addr, tmp;
57
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.
64
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. */
68
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");
76
77 return addr;
78 }
79
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. */
82
83 static inline int __attribute__ ((unused))
84 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
85 {
86 Elf64_Addr *got;
87 extern void _dl_runtime_resolve (Elf64_Word) attribute_hidden;
88 extern void _dl_runtime_profile (Elf64_Word) attribute_hidden;
89
90 if (l->l_info[DT_JMPREL] && lazy)
91 {
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])
101 {
102 l->l_mach.plt = got[1] + l->l_addr;
103 l->l_mach.gotplt = (Elf64_Addr) &got[3];
104 }
105 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
106
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))
114 {
115 got[2] = (Elf64_Addr) &_dl_runtime_profile;
116
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;
121 }
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;
126 }
127
128 return lazy;
129 }
130
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\
165 \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\
197 ");
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\
235 ");
236 #endif
237
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\
284 ");
285
286 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
287 TLS variable, so undefined references should not be allowed to
288 define the value.
289 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
290 of the main executable's symbols, as for a COPY reloc. */
291 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
292 # define elf_machine_type_class(type) \
293 ((((type) == R_X86_64_JUMP_SLOT \
294 || (type) == R_X86_64_DTPMOD64 \
295 || (type) == R_X86_64_DTPOFF64 || (type) == R_X86_64_TPOFF64) \
296 * ELF_RTYPE_CLASS_PLT) \
297 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
298 #else
299 # define elf_machine_type_class(type) \
300 ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
301 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
302 #endif
303
304 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
305 #define ELF_MACHINE_JMP_SLOT R_X86_64_JUMP_SLOT
306
307 /* The x86-64 never uses Elf64_Rel relocations. */
308 #define ELF_MACHINE_NO_REL 1
309
310 /* We define an initialization functions. This is called very early in
311 _dl_sysdep_start. */
312 #define DL_PLATFORM_INIT dl_platform_init ()
313
314 static inline void __attribute__ ((unused))
315 dl_platform_init (void)
316 {
317 if (GL(dl_platform) != NULL && *GL(dl_platform) == '\0')
318 /* Avoid an empty string which would disturb us. */
319 GL(dl_platform) = NULL;
320 }
321
322 static inline Elf64_Addr
323 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
324 const Elf64_Rela *reloc,
325 Elf64_Addr *reloc_addr, Elf64_Addr value)
326 {
327 return *reloc_addr = value;
328 }
329
330 /* Return the final value of a plt relocation. On x86-64 the
331 JUMP_SLOT relocation ignores the addend. */
332 static inline Elf64_Addr
333 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
334 Elf64_Addr value)
335 {
336 return value;
337 }
338
339 #endif /* !dl_machine_h */
340
341 #ifdef RESOLVE
342
343 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
344 MAP is the object containing the reloc. */
345
346 static inline void
347 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
348 const Elf64_Sym *sym, const struct r_found_version *version,
349 Elf64_Addr *const reloc_addr)
350 {
351 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
352
353 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
354 if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0))
355 {
356 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
357 /* This is defined in rtld.c, but nowhere in the static libc.a;
358 make the reference weak so static programs can still link.
359 This declaration cannot be done when compiling rtld.c
360 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
361 common defn for _dl_rtld_map, which is incompatible with a
362 weak decl in the same file. */
363 # ifndef SHARED
364 weak_extern (GL(dl_rtld_map));
365 # endif
366 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
367 # endif
368 *reloc_addr = map->l_addr + reloc->r_addend;
369 }
370 else
371 #endif
372 if (__builtin_expect (r_type == R_X86_64_NONE, 0))
373 return;
374 else
375 {
376 #ifndef RTLD_BOOTSTRAP
377 const Elf64_Sym *const refsym = sym;
378 #endif
379 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
380 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
381 Elf64_Addr value = (sym == NULL ? 0
382 : (Elf64_Addr) sym_map->l_addr + sym->st_value);
383 #else
384 Elf64_Addr value = RESOLVE (&sym, version, r_type);
385
386 # ifndef RTLD_BOOTSTRAP
387 if (sym != NULL)
388 # endif
389 value += sym->st_value;
390 #endif
391
392 #if defined RTLD_BOOTSTRAP && !USE___THREAD
393 assert (r_type == R_X86_64_GLOB_DAT || r_type == R_X86_64_JUMP_SLOT);
394 *reloc_addr = value + reloc->r_addend;
395 #else
396 switch (r_type)
397 {
398 case R_X86_64_GLOB_DAT:
399 case R_X86_64_JUMP_SLOT:
400 *reloc_addr = value + reloc->r_addend;
401 break;
402
403 #if defined USE_TLS && !defined RESOLVE_CONFLICT_FIND_MAP
404 case R_X86_64_DTPMOD64:
405 # ifdef RTLD_BOOTSTRAP
406 /* During startup the dynamic linker is always the module
407 with index 1.
408 XXX If this relocation is necessary move before RESOLVE
409 call. */
410 *reloc_addr = 1;
411 # else
412 /* Get the information from the link map returned by the
413 resolve function. */
414 if (sym_map != NULL)
415 *reloc_addr = sym_map->l_tls_modid;
416 # endif
417 break;
418 case R_X86_64_DTPOFF64:
419 # ifndef RTLD_BOOTSTRAP
420 /* During relocation all TLS symbols are defined and used.
421 Therefore the offset is already correct. */
422 if (sym != NULL)
423 *reloc_addr = sym->st_value + reloc->r_addend;
424 # endif
425 break;
426 case R_X86_64_TPOFF64:
427 /* The offset is negative, forward from the thread pointer. */
428 # ifndef RTLD_BOOTSTRAP
429 if (sym != NULL)
430 # endif
431 {
432 # ifndef RTLD_BOOTSTRAP
433 CHECK_STATIC_TLS (map, sym_map);
434 # endif
435 /* We know the offset of the object the symbol is contained in.
436 It is a negative value which will be added to the
437 thread pointer. */
438 *reloc_addr = (sym->st_value + reloc->r_addend
439 - sym_map->l_tls_offset);
440 }
441 break;
442 #endif /* use TLS */
443
444 #ifndef RTLD_BOOTSTRAP
445 case R_X86_64_64:
446 *reloc_addr = value + reloc->r_addend;
447 break;
448 case R_X86_64_32:
449 *(unsigned int *) reloc_addr = value + reloc->r_addend;
450 if (value + reloc->r_addend > UINT_MAX)
451 {
452 const char *strtab;
453
454 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
455
456 _dl_error_printf ("\
457 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n",
458 rtld_progname ?: "<program name unknown>",
459 strtab + refsym->st_name);
460 }
461 break;
462 # ifndef RESOLVE_CONFLICT_FIND_MAP
463 /* Not needed for dl-conflict.c. */
464 case R_X86_64_PC32:
465 *(unsigned int *) reloc_addr = value + reloc->r_addend
466 - (Elf64_Addr) reloc_addr;
467 if (value + reloc->r_addend - (Elf64_Addr) reloc_addr
468 != (int)(value + reloc->r_addend - (Elf64_Addr) reloc_addr))
469 {
470 const char *strtab;
471
472 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
473
474 _dl_error_printf ("\
475 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n",
476 rtld_progname ?: "<program name unknown>",
477 strtab + refsym->st_name);
478 }
479 break;
480 case R_X86_64_COPY:
481 if (sym == NULL)
482 /* This can happen in trace mode if an object could not be
483 found. */
484 break;
485 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
486 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
487 && GL(dl_verbose)))
488 {
489 const char *strtab;
490
491 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
492 _dl_error_printf ("\
493 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
494 rtld_progname ?: "<program name unknown>",
495 strtab + refsym->st_name);
496 }
497 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
498 refsym->st_size));
499 break;
500 # endif
501 default:
502 _dl_reloc_bad_type (map, r_type, 0);
503 break;
504 #endif
505 }
506 #endif
507 }
508 }
509
510 static inline void
511 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
512 Elf64_Addr *const reloc_addr)
513 {
514 assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
515 *reloc_addr = l_addr + reloc->r_addend;
516 }
517
518 static inline void
519 elf_machine_lazy_rel (struct link_map *map,
520 Elf64_Addr l_addr, const Elf64_Rela *reloc)
521 {
522 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
523 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
524
525 /* Check for unexpected PLT reloc type. */
526 if (__builtin_expect (r_type == R_X86_64_JUMP_SLOT, 1))
527 {
528 if (__builtin_expect (map->l_mach.plt, 0) == 0)
529 *reloc_addr += l_addr;
530 else
531 *reloc_addr =
532 map->l_mach.plt
533 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 2;
534 }
535 else
536 _dl_reloc_bad_type (map, r_type, 1);
537 }
538
539 #endif /* RESOLVE */
This page took 0.057299 seconds and 5 git commands to generate.