[BZ #3313]
[glibc.git] / sysdeps / x86_64 / dl-machine.h
blob73e271775aaee05c64ac66b1b8eb7e1dcc11e369
1 /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 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>
27 #include <sysdep.h>
28 #include <tls.h>
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)
34 return ehdr->e_machine == EM_X86_64;
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)
44 Elf64_Addr addr;
46 /* This works because we have our GOT address available in the small PIC
47 model. */
48 addr = (Elf64_Addr) &_DYNAMIC;
50 return addr;
54 /* Return the run-time load address of the shared object. */
55 static inline Elf64_Addr __attribute__ ((unused))
56 elf_machine_load_address (void)
58 Elf64_Addr addr;
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.
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. */
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");
82 return addr;
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. */
88 static inline int __attribute__ ((unused, always_inline))
89 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
91 Elf64_Addr *got;
92 extern void _dl_runtime_resolve (Elf64_Word) attribute_hidden;
93 extern void _dl_runtime_profile (Elf64_Word) attribute_hidden;
95 if (l->l_info[DT_JMPREL] && lazy)
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])
107 l->l_mach.plt = got[1] + l->l_addr;
108 l->l_mach.gotplt = (Elf64_Addr) &got[3];
110 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
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))
120 got[2] = (Elf64_Addr) &_dl_runtime_profile;
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;
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;
134 return lazy;
137 /* Initial entry point code for the dynamic linker.
138 The C function `_dl_start' is the real entry point;
139 its return value is the user program's entry point. */
140 #define RTLD_START asm ("\n\
141 .text\n\
142 .align 16\n\
143 .globl _start\n\
144 .globl _dl_start_user\n\
145 _start:\n\
146 movq %rsp, %rdi\n\
147 call _dl_start\n\
148 _dl_start_user:\n\
149 # Save the user entry point address in %r12.\n\
150 movq %rax, %r12\n\
151 # See if we were run as a command with the executable file\n\
152 # name as an extra leading argument.\n\
153 movl _dl_skip_args(%rip), %eax\n\
154 # Pop the original argument count.\n\
155 popq %rdx\n\
156 # Adjust the stack pointer to skip _dl_skip_args words.\n\
157 leaq (%rsp,%rax,8), %rsp\n\
158 # Subtract _dl_skip_args from argc.\n\
159 subl %eax, %edx\n\
160 # Push argc back on the stack.\n\
161 pushq %rdx\n\
162 # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
163 # argc -> rsi\n\
164 movq %rdx, %rsi\n\
165 # Save %rsp value in %r13.\n\
166 movq %rsp, %r13\n\
167 # And align stack for the _dl_init_internal call. \n\
168 andq $-16, %rsp\n\
169 # _dl_loaded -> rdi\n\
170 movq _rtld_local(%rip), %rdi\n\
171 # env -> rcx\n\
172 leaq 16(%r13,%rdx,8), %rcx\n\
173 # argv -> rdx\n\
174 leaq 8(%r13), %rdx\n\
175 # Clear %rbp to mark outermost frame obviously even for constructors.\n\
176 xorl %ebp, %ebp\n\
177 # Call the function to run the initializers.\n\
178 call _dl_init_internal@PLT\n\
179 # Pass our finalizer function to the user in %rdx, as per ELF ABI.\n\
180 leaq _dl_fini(%rip), %rdx\n\
181 # And make sure %rsp points to argc stored on the stack.\n\
182 movq %r13, %rsp\n\
183 # Jump to the user's entry point.\n\
184 jmp *%r12\n\
185 .previous\n\
188 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
189 TLS variable, so undefined references should not be allowed to
190 define the value.
191 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
192 of the main executable's symbols, as for a COPY reloc. */
193 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
194 # define elf_machine_type_class(type) \
195 ((((type) == R_X86_64_JUMP_SLOT \
196 || (type) == R_X86_64_DTPMOD64 \
197 || (type) == R_X86_64_DTPOFF64 || (type) == R_X86_64_TPOFF64) \
198 * ELF_RTYPE_CLASS_PLT) \
199 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
200 #else
201 # define elf_machine_type_class(type) \
202 ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
203 | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
204 #endif
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
209 /* The x86-64 never uses Elf64_Rel relocations. */
210 #define ELF_MACHINE_NO_REL 1
212 /* We define an initialization functions. This is called very early in
213 _dl_sysdep_start. */
214 #define DL_PLATFORM_INIT dl_platform_init ()
216 static inline void __attribute__ ((unused))
217 dl_platform_init (void)
219 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
220 /* Avoid an empty string which would disturb us. */
221 GLRO(dl_platform) = NULL;
224 static inline Elf64_Addr
225 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
226 const Elf64_Rela *reloc,
227 Elf64_Addr *reloc_addr, Elf64_Addr value)
229 return *reloc_addr = value;
232 /* Return the final value of a plt relocation. On x86-64 the
233 JUMP_SLOT relocation ignores the addend. */
234 static inline Elf64_Addr
235 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
236 Elf64_Addr value)
238 return value;
242 /* Names of the architecture-specific auditing callback functions. */
243 #define ARCH_LA_PLTENTER x86_64_gnu_pltenter
244 #define ARCH_LA_PLTEXIT x86_64_gnu_pltexit
246 #endif /* !dl_machine_h */
248 #ifdef RESOLVE_MAP
250 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
251 MAP is the object containing the reloc. */
253 auto inline void
254 __attribute__ ((always_inline))
255 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
256 const Elf64_Sym *sym, const struct r_found_version *version,
257 void *const reloc_addr_arg)
259 Elf64_Addr *const reloc_addr = reloc_addr_arg;
260 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
262 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
263 if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0))
265 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
266 /* This is defined in rtld.c, but nowhere in the static libc.a;
267 make the reference weak so static programs can still link.
268 This declaration cannot be done when compiling rtld.c
269 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
270 common defn for _dl_rtld_map, which is incompatible with a
271 weak decl in the same file. */
272 # ifndef SHARED
273 weak_extern (GL(dl_rtld_map));
274 # endif
275 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
276 # endif
277 *reloc_addr = map->l_addr + reloc->r_addend;
279 else
280 #endif
281 if (__builtin_expect (r_type == R_X86_64_NONE, 0))
282 return;
283 else
285 #ifndef RTLD_BOOTSTRAP
286 const Elf64_Sym *const refsym = sym;
287 #endif
288 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
289 Elf64_Addr value = (sym == NULL ? 0
290 : (Elf64_Addr) sym_map->l_addr + sym->st_value);
292 #if defined RTLD_BOOTSTRAP && !USE___THREAD
293 assert (r_type == R_X86_64_GLOB_DAT || r_type == R_X86_64_JUMP_SLOT);
294 *reloc_addr = value + reloc->r_addend;
295 #else
296 switch (r_type)
298 case R_X86_64_GLOB_DAT:
299 case R_X86_64_JUMP_SLOT:
300 *reloc_addr = value + reloc->r_addend;
301 break;
303 #if defined USE_TLS && !defined RESOLVE_CONFLICT_FIND_MAP
304 case R_X86_64_DTPMOD64:
305 # ifdef RTLD_BOOTSTRAP
306 /* During startup the dynamic linker is always the module
307 with index 1.
308 XXX If this relocation is necessary move before RESOLVE
309 call. */
310 *reloc_addr = 1;
311 # else
312 /* Get the information from the link map returned by the
313 resolve function. */
314 if (sym_map != NULL)
315 *reloc_addr = sym_map->l_tls_modid;
316 # endif
317 break;
318 case R_X86_64_DTPOFF64:
319 # ifndef RTLD_BOOTSTRAP
320 /* During relocation all TLS symbols are defined and used.
321 Therefore the offset is already correct. */
322 if (sym != NULL)
323 *reloc_addr = sym->st_value + reloc->r_addend;
324 # endif
325 break;
326 case R_X86_64_TPOFF64:
327 /* The offset is negative, forward from the thread pointer. */
328 # ifndef RTLD_BOOTSTRAP
329 if (sym != NULL)
330 # endif
332 # ifndef RTLD_BOOTSTRAP
333 CHECK_STATIC_TLS (map, sym_map);
334 # endif
335 /* We know the offset of the object the symbol is contained in.
336 It is a negative value which will be added to the
337 thread pointer. */
338 *reloc_addr = (sym->st_value + reloc->r_addend
339 - sym_map->l_tls_offset);
341 break;
342 #endif /* use TLS */
344 #ifndef RTLD_BOOTSTRAP
345 case R_X86_64_64:
346 *reloc_addr = value + reloc->r_addend;
347 break;
348 case R_X86_64_32:
349 *(unsigned int *) reloc_addr = value + reloc->r_addend;
350 if (value + reloc->r_addend > UINT_MAX)
352 const char *strtab;
354 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
356 _dl_error_printf ("\
357 %s: Symbol `%s' causes overflow in R_X86_64_32 relocation\n",
358 rtld_progname ?: "<program name unknown>",
359 strtab + refsym->st_name);
361 break;
362 # ifndef RESOLVE_CONFLICT_FIND_MAP
363 /* Not needed for dl-conflict.c. */
364 case R_X86_64_PC32:
365 *(unsigned int *) reloc_addr = value + reloc->r_addend
366 - (Elf64_Addr) reloc_addr;
367 if (value + reloc->r_addend - (Elf64_Addr) reloc_addr
368 != (int)(value + reloc->r_addend - (Elf64_Addr) reloc_addr))
370 const char *strtab;
372 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
374 _dl_error_printf ("\
375 %s: Symbol `%s' causes overflow in R_X86_64_PC32 relocation\n",
376 rtld_progname ?: "<program name unknown>",
377 strtab + refsym->st_name);
379 break;
380 case R_X86_64_COPY:
381 if (sym == NULL)
382 /* This can happen in trace mode if an object could not be
383 found. */
384 break;
385 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
386 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
387 && GLRO(dl_verbose)))
389 const char *strtab;
391 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
392 _dl_error_printf ("\
393 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
394 rtld_progname ?: "<program name unknown>",
395 strtab + refsym->st_name);
397 memcpy (reloc_addr_arg, (void *) value,
398 MIN (sym->st_size, refsym->st_size));
399 break;
400 # endif
401 default:
402 _dl_reloc_bad_type (map, r_type, 0);
403 break;
404 #endif
406 #endif
410 auto inline void
411 __attribute ((always_inline))
412 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
413 void *const reloc_addr_arg)
415 Elf64_Addr *const reloc_addr = reloc_addr_arg;
416 assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
417 *reloc_addr = l_addr + reloc->r_addend;
420 auto inline void
421 __attribute ((always_inline))
422 elf_machine_lazy_rel (struct link_map *map,
423 Elf64_Addr l_addr, const Elf64_Rela *reloc)
425 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
426 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
428 /* Check for unexpected PLT reloc type. */
429 if (__builtin_expect (r_type == R_X86_64_JUMP_SLOT, 1))
431 if (__builtin_expect (map->l_mach.plt, 0) == 0)
432 *reloc_addr += l_addr;
433 else
434 *reloc_addr =
435 map->l_mach.plt
436 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 2;
438 else
439 _dl_reloc_bad_type (map, r_type, 1);
442 #endif /* RESOLVE_MAP */