* sysdeps/i386/dl-machine.h (ELF_MACHINE_NO_RELA): Define
[glibc.git] / sysdeps / i386 / dl-machine.h
blob367a4fb1a8e0cfdad93edc1d05f9025429be4ce3
1 /* Machine-dependent ELF dynamic relocation inline functions. i386 version.
2 Copyright (C) 1995-2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef dl_machine_h
21 #define dl_machine_h
23 #define ELF_MACHINE_NAME "i386"
25 #include <sys/param.h>
26 #include <sysdep.h>
27 #include <tls.h>
29 /* Return nonzero iff ELF header is compatible with the running host. */
30 static inline int __attribute__ ((unused))
31 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
33 return ehdr->e_machine == EM_386;
37 #if defined PI_STATIC_AND_HIDDEN \
38 && defined HAVE_VISIBILITY_ATTRIBUTE && defined HAVE_HIDDEN \
39 && !defined HAVE_BROKEN_VISIBILITY_ATTRIBUTE
41 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
42 first element of the GOT, a special entry that is never relocated. */
43 static inline Elf32_Addr __attribute__ ((unused, const))
44 elf_machine_dynamic (void)
46 /* This produces a GOTOFF reloc that resolves to zero at link time, so in
47 fact just loads from the GOT register directly. By doing it without
48 an asm we can let the compiler choose any register. */
49 extern const Elf32_Addr _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
50 return _GLOBAL_OFFSET_TABLE_[0];
53 /* Return the run-time load address of the shared object. */
54 static inline Elf32_Addr __attribute__ ((unused))
55 elf_machine_load_address (void)
57 /* Compute the difference between the runtime address of _DYNAMIC as seen
58 by a GOTOFF reference, and the link-time address found in the special
59 unrelocated first GOT entry. */
60 extern Elf32_Dyn bygotoff[] asm ("_DYNAMIC") attribute_hidden;
61 return (Elf32_Addr) &bygotoff - elf_machine_dynamic ();
64 #else /* Without .hidden support, we can't compile the code above. */
66 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
67 first element of the GOT. This must be inlined in a function which
68 uses global data. */
69 static inline Elf32_Addr __attribute__ ((unused))
70 elf_machine_dynamic (void)
72 register Elf32_Addr *got asm ("%ebx");
73 return *got;
77 /* Return the run-time load address of the shared object. */
78 static inline Elf32_Addr __attribute__ ((unused))
79 elf_machine_load_address (void)
81 /* It doesn't matter what variable this is, the reference never makes
82 it to assembly. We need a dummy reference to some global variable
83 via the GOT to make sure the compiler initialized %ebx in time. */
84 extern int _dl_argc;
85 Elf32_Addr addr;
86 asm ("leal _dl_start@GOTOFF(%%ebx), %0\n"
87 "subl _dl_start@GOT(%%ebx), %0"
88 : "=r" (addr) : "m" (_dl_argc) : "cc");
89 return addr;
92 #endif
95 /* Set up the loaded object described by L so its unrelocated PLT
96 entries will jump to the on-demand fixup code in dl-runtime.c. */
98 static inline int __attribute__ ((unused, always_inline))
99 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
101 Elf32_Addr *got;
102 extern void _dl_runtime_resolve (Elf32_Word) attribute_hidden;
103 extern void _dl_runtime_profile (Elf32_Word) attribute_hidden;
105 if (l->l_info[DT_JMPREL] && lazy)
107 /* The GOT entries for functions in the PLT have not yet been filled
108 in. Their initial contents will arrange when called to push an
109 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
110 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
111 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
112 /* If a library is prelinked but we have to relocate anyway,
113 we have to be able to undo the prelinking of .got.plt.
114 The prelinker saved us here address of .plt + 0x16. */
115 if (got[1])
117 l->l_mach.plt = got[1] + l->l_addr;
118 l->l_mach.gotplt = (Elf32_Addr) &got[3];
120 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
122 /* The got[2] entry contains the address of a function which gets
123 called to get the address of a so far unresolved function and
124 jump to it. The profiling extension of the dynamic linker allows
125 to intercept the calls to collect information. In this case we
126 don't store the address in the GOT so that all future calls also
127 end in this function. */
128 if (__builtin_expect (profile, 0))
130 got[2] = (Elf32_Addr) &_dl_runtime_profile;
132 if (GLRO(dl_profile) != NULL
133 && _dl_name_match_p (GLRO(dl_profile), l))
134 /* This is the object we are looking for. Say that we really
135 want profiling and the timers are started. */
136 GL(dl_profile_map) = l;
138 else
139 /* This function will get called to fix up the GOT entry indicated by
140 the offset on the stack, and then jump to the resolved address. */
141 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
144 return lazy;
147 #ifdef IN_DL_RUNTIME
149 # if !defined PROF && !__BOUNDED_POINTERS__
150 /* We add a declaration of this function here so that in dl-runtime.c
151 the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters
152 in registers.
154 We cannot use this scheme for profiling because the _mcount call
155 destroys the passed register information. */
156 /* GKM FIXME: Fix trampoline to pass bounds so we can do
157 without the `__unbounded' qualifier. */
158 #define ARCH_FIXUP_ATTRIBUTE __attribute__ ((regparm (3), stdcall, unused))
160 extern ElfW(Addr) _dl_fixup (struct link_map *__unbounded l,
161 ElfW(Word) reloc_offset)
162 ARCH_FIXUP_ATTRIBUTE;
163 extern ElfW(Addr) _dl_profile_fixup (struct link_map *l,
164 ElfW(Word) reloc_offset,
165 ElfW(Addr) retaddr, void *regs,
166 long int *framesizep)
167 ARCH_FIXUP_ATTRIBUTE;
168 # endif
170 #endif
172 /* Mask identifying addresses reserved for the user program,
173 where the dynamic linker should not map anything. */
174 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
176 /* Initial entry point code for the dynamic linker.
177 The C function `_dl_start' is the real entry point;
178 its return value is the user program's entry point. */
180 #define RTLD_START asm ("\n\
181 .text\n\
182 .align 16\n\
183 0: movl (%esp), %ebx\n\
184 ret\n\
185 .align 16\n\
186 .globl _start\n\
187 .globl _dl_start_user\n\
188 _start:\n\
189 # Note that _dl_start gets the parameter in %eax.\n\
190 movl %esp, %eax\n\
191 call _dl_start\n\
192 _dl_start_user:\n\
193 # Save the user entry point address in %edi.\n\
194 movl %eax, %edi\n\
195 # Point %ebx at the GOT.\n\
196 call 0b\n\
197 addl $_GLOBAL_OFFSET_TABLE_, %ebx\n\
198 # See if we were run as a command with the executable file\n\
199 # name as an extra leading argument.\n\
200 movl _dl_skip_args@GOTOFF(%ebx), %eax\n\
201 # Pop the original argument count.\n\
202 popl %edx\n\
203 # Adjust the stack pointer to skip _dl_skip_args words.\n\
204 leal (%esp,%eax,4), %esp\n\
205 # Subtract _dl_skip_args from argc.\n\
206 subl %eax, %edx\n\
207 # Push argc back on the stack.\n\
208 push %edx\n\
209 # The special initializer gets called with the stack just\n\
210 # as the application's entry point will see it; it can\n\
211 # switch stacks if it moves these contents over.\n\
212 " RTLD_START_SPECIAL_INIT "\n\
213 # Load the parameters again.\n\
214 # (eax, edx, ecx, *--esp) = (_dl_loaded, argc, argv, envp)\n\
215 movl _rtld_local@GOTOFF(%ebx), %eax\n\
216 leal 8(%esp,%edx,4), %esi\n\
217 leal 4(%esp), %ecx\n\
218 movl %esp, %ebp\n\
219 # Make sure _dl_init is run with 16 byte aligned stack.\n\
220 andl $-16, %esp\n\
221 pushl %eax\n\
222 pushl %eax\n\
223 pushl %ebp\n\
224 pushl %esi\n\
225 # Clear %ebp, so that even constructors have terminated backchain.\n\
226 xorl %ebp, %ebp\n\
227 # Call the function to run the initializers.\n\
228 call _dl_init_internal@PLT\n\
229 # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
230 leal _dl_fini@GOTOFF(%ebx), %edx\n\
231 # Restore %esp _start expects.\n\
232 movl (%esp), %esp\n\
233 # Jump to the user's entry point.\n\
234 jmp *%edi\n\
235 .previous\n\
238 #ifndef RTLD_START_SPECIAL_INIT
239 # define RTLD_START_SPECIAL_INIT /* nothing */
240 #endif
242 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
243 TLS variable, so undefined references should not be allowed to
244 define the value.
245 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
246 of the main executable's symbols, as for a COPY reloc. */
247 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
248 # define elf_machine_type_class(type) \
249 ((((type) == R_386_JMP_SLOT || (type) == R_386_TLS_DTPMOD32 \
250 || (type) == R_386_TLS_DTPOFF32 || (type) == R_386_TLS_TPOFF32 \
251 || (type) == R_386_TLS_TPOFF) \
252 * ELF_RTYPE_CLASS_PLT) \
253 | (((type) == R_386_COPY) * ELF_RTYPE_CLASS_COPY))
254 #else
255 # define elf_machine_type_class(type) \
256 ((((type) == R_386_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
257 | (((type) == R_386_COPY) * ELF_RTYPE_CLASS_COPY))
258 #endif
260 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
261 #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT
263 /* The i386 never uses Elf32_Rela relocations for the dynamic linker.
264 Prelinked libraries may use Elf32_Rela though. */
265 #define ELF_MACHINE_PLT_REL 1
267 /* We define an initialization functions. This is called very early in
268 _dl_sysdep_start. */
269 #define DL_PLATFORM_INIT dl_platform_init ()
271 static inline void __attribute__ ((unused))
272 dl_platform_init (void)
274 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
275 /* Avoid an empty string which would disturb us. */
276 GLRO(dl_platform) = NULL;
279 static inline Elf32_Addr
280 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
281 const Elf32_Rel *reloc,
282 Elf32_Addr *reloc_addr, Elf32_Addr value)
284 return *reloc_addr = value;
287 /* Return the final value of a plt relocation. */
288 static inline Elf32_Addr
289 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
290 Elf32_Addr value)
292 return value;
296 /* Names of the architecture-specific auditing callback functions. */
297 #define ARCH_LA_PLTENTER i86_gnu_pltenter
298 #define ARCH_LA_PLTEXIT i86_gnu_pltexit
300 #endif /* !dl_machine_h */
302 /* The i386 never uses Elf32_Rela relocations for the dynamic linker.
303 Prelinked libraries may use Elf32_Rela though. */
304 #define ELF_MACHINE_NO_RELA (defined RTLD_BOOTSTRAP)
306 #ifdef RESOLVE_MAP
308 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
309 MAP is the object containing the reloc. */
311 auto inline void
312 __attribute ((always_inline))
313 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
314 const Elf32_Sym *sym, const struct r_found_version *version,
315 void *const reloc_addr_arg)
317 Elf32_Addr *const reloc_addr = reloc_addr_arg;
318 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
320 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
321 if (__builtin_expect (r_type == R_386_RELATIVE, 0))
323 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
324 /* This is defined in rtld.c, but nowhere in the static libc.a;
325 make the reference weak so static programs can still link.
326 This declaration cannot be done when compiling rtld.c
327 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
328 common defn for _dl_rtld_map, which is incompatible with a
329 weak decl in the same file. */
330 # ifndef SHARED
331 weak_extern (_dl_rtld_map);
332 # endif
333 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
334 # endif
335 *reloc_addr += map->l_addr;
337 # ifndef RTLD_BOOTSTRAP
338 else if (__builtin_expect (r_type == R_386_NONE, 0))
339 return;
340 # endif
341 else
342 #endif /* !RTLD_BOOTSTRAP and have no -z combreloc */
344 const Elf32_Sym *const refsym = sym;
345 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
346 Elf32_Addr value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
348 switch (r_type)
350 case R_386_GLOB_DAT:
351 case R_386_JMP_SLOT:
352 *reloc_addr = value;
353 break;
355 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
356 case R_386_TLS_DTPMOD32:
357 # ifdef RTLD_BOOTSTRAP
358 /* During startup the dynamic linker is always the module
359 with index 1.
360 XXX If this relocation is necessary move before RESOLVE
361 call. */
362 *reloc_addr = 1;
363 # else
364 /* Get the information from the link map returned by the
365 resolv function. */
366 if (sym_map != NULL)
367 *reloc_addr = sym_map->l_tls_modid;
368 # endif
369 break;
370 case R_386_TLS_DTPOFF32:
371 # ifndef RTLD_BOOTSTRAP
372 /* During relocation all TLS symbols are defined and used.
373 Therefore the offset is already correct. */
374 if (sym != NULL)
375 *reloc_addr = sym->st_value;
376 # endif
377 break;
378 case R_386_TLS_TPOFF32:
379 /* The offset is positive, backward from the thread pointer. */
380 # ifdef RTLD_BOOTSTRAP
381 *reloc_addr += map->l_tls_offset - sym->st_value;
382 # else
383 /* We know the offset of object the symbol is contained in.
384 It is a positive value which will be subtracted from the
385 thread pointer. To get the variable position in the TLS
386 block we subtract the offset from that of the TLS block. */
387 if (sym != NULL)
389 CHECK_STATIC_TLS (map, sym_map);
390 *reloc_addr += sym_map->l_tls_offset - sym->st_value;
392 # endif
393 break;
394 case R_386_TLS_TPOFF:
395 /* The offset is negative, forward from the thread pointer. */
396 # ifdef RTLD_BOOTSTRAP
397 *reloc_addr += sym->st_value - map->l_tls_offset;
398 # else
399 /* We know the offset of object the symbol is contained in.
400 It is a negative value which will be added to the
401 thread pointer. */
402 if (sym != NULL)
404 CHECK_STATIC_TLS (map, sym_map);
405 *reloc_addr += sym->st_value - sym_map->l_tls_offset;
407 # endif
408 break;
409 #endif /* use TLS */
411 #ifndef RTLD_BOOTSTRAP
412 case R_386_32:
413 *reloc_addr += value;
414 break;
415 case R_386_PC32:
416 *reloc_addr += (value - (Elf32_Addr) reloc_addr);
417 break;
418 case R_386_COPY:
419 if (sym == NULL)
420 /* This can happen in trace mode if an object could not be
421 found. */
422 break;
423 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
424 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
425 && GLRO(dl_verbose)))
427 const char *strtab;
429 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
430 _dl_error_printf ("\
431 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
432 rtld_progname ?: "<program name unknown>",
433 strtab + refsym->st_name);
435 memcpy (reloc_addr_arg, (void *) value,
436 MIN (sym->st_size, refsym->st_size));
437 break;
438 default:
439 _dl_reloc_bad_type (map, r_type, 0);
440 break;
441 #endif /* !RTLD_BOOTSTRAP */
446 #ifndef RTLD_BOOTSTRAP
447 auto inline void
448 __attribute__ ((always_inline))
449 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
450 const Elf32_Sym *sym, const struct r_found_version *version,
451 void *const reloc_addr_arg)
453 Elf32_Addr *const reloc_addr = reloc_addr_arg;
454 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
456 if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE)
457 *reloc_addr = map->l_addr + reloc->r_addend;
458 else if (r_type != R_386_NONE)
460 # ifndef RESOLVE_CONFLICT_FIND_MAP
461 const Elf32_Sym *const refsym = sym;
462 # endif
463 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
464 Elf32_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
466 switch (ELF32_R_TYPE (reloc->r_info))
468 case R_386_GLOB_DAT:
469 case R_386_JMP_SLOT:
470 case R_386_32:
471 *reloc_addr = value + reloc->r_addend;
472 break;
473 # ifndef RESOLVE_CONFLICT_FIND_MAP
474 /* Not needed for dl-conflict.c. */
475 case R_386_PC32:
476 *reloc_addr = (value + reloc->r_addend - (Elf32_Addr) reloc_addr);
477 break;
479 # ifdef USE_TLS
480 case R_386_TLS_DTPMOD32:
481 /* Get the information from the link map returned by the
482 resolv function. */
483 if (sym_map != NULL)
484 *reloc_addr = sym_map->l_tls_modid;
485 break;
486 case R_386_TLS_DTPOFF32:
487 /* During relocation all TLS symbols are defined and used.
488 Therefore the offset is already correct. */
489 *reloc_addr = (sym == NULL ? 0 : sym->st_value) + reloc->r_addend;
490 break;
491 case R_386_TLS_TPOFF32:
492 /* The offset is positive, backward from the thread pointer. */
493 /* We know the offset of object the symbol is contained in.
494 It is a positive value which will be subtracted from the
495 thread pointer. To get the variable position in the TLS
496 block we subtract the offset from that of the TLS block. */
497 if (sym != NULL)
499 CHECK_STATIC_TLS (map, sym_map);
500 *reloc_addr = sym_map->l_tls_offset - sym->st_value
501 + reloc->r_addend;
503 break;
504 case R_386_TLS_TPOFF:
505 /* The offset is negative, forward from the thread pointer. */
506 /* We know the offset of object the symbol is contained in.
507 It is a negative value which will be added to the
508 thread pointer. */
509 if (sym != NULL)
511 CHECK_STATIC_TLS (map, sym_map);
512 *reloc_addr = sym->st_value - sym_map->l_tls_offset
513 + reloc->r_addend;
515 break;
516 # endif /* use TLS */
517 case R_386_COPY:
518 if (sym == NULL)
519 /* This can happen in trace mode if an object could not be
520 found. */
521 break;
522 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
523 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
524 && GLRO(dl_verbose)))
526 const char *strtab;
528 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
529 _dl_error_printf ("\
530 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
531 rtld_progname ?: "<program name unknown>",
532 strtab + refsym->st_name);
534 memcpy (reloc_addr_arg, (void *) value,
535 MIN (sym->st_size, refsym->st_size));
536 break;
537 # endif /* !RESOLVE_CONFLICT_FIND_MAP */
538 default:
539 /* We add these checks in the version to relocate ld.so only
540 if we are still debugging. */
541 _dl_reloc_bad_type (map, r_type, 0);
542 break;
546 #endif /* !RTLD_BOOTSTRAP */
548 auto inline void
549 __attribute ((always_inline))
550 elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc,
551 void *const reloc_addr_arg)
553 Elf32_Addr *const reloc_addr = reloc_addr_arg;
554 assert (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE);
555 *reloc_addr += l_addr;
558 #ifndef RTLD_BOOTSTRAP
559 auto inline void
560 __attribute__ ((always_inline))
561 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
562 void *const reloc_addr_arg)
564 Elf32_Addr *const reloc_addr = reloc_addr_arg;
565 *reloc_addr = l_addr + reloc->r_addend;
567 #endif /* !RTLD_BOOTSTRAP */
569 auto inline void
570 __attribute__ ((always_inline))
571 elf_machine_lazy_rel (struct link_map *map,
572 Elf32_Addr l_addr, const Elf32_Rel *reloc)
574 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
575 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
576 /* Check for unexpected PLT reloc type. */
577 if (__builtin_expect (r_type == R_386_JMP_SLOT, 1))
579 if (__builtin_expect (map->l_mach.plt, 0) == 0)
580 *reloc_addr += l_addr;
581 else
582 *reloc_addr = (map->l_mach.plt
583 + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 4);
585 else
586 _dl_reloc_bad_type (map, r_type, 1);
589 #ifndef RTLD_BOOTSTRAP
591 auto inline void
592 __attribute__ ((always_inline))
593 elf_machine_lazy_rela (struct link_map *map,
594 Elf32_Addr l_addr, const Elf32_Rela *reloc)
598 #endif /* !RTLD_BOOTSTRAP */
600 #endif /* RESOLVE_MAP */