Update.
[glibc.git] / sysdeps / i386 / dl-machine.h
blobc49987502380e59960c32e5d5a948343de6d5a78
1 /* Machine-dependent ELF dynamic relocation inline functions. i386 version.
2 Copyright (C) 1995,96,97,98,99,2000,2001,2002 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>
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 #if !defined PROF && !__BOUNDED_POINTERS__
96 /* We add a declaration of this function here so that in dl-runtime.c
97 the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters
98 in registers.
100 We cannot use this scheme for profiling because the _mcount call
101 destroys the passed register information. */
102 /* GKM FIXME: Fix trampoline to pass bounds so we can do
103 without the `__unbounded' qualifier. */
104 static ElfW(Addr) fixup (struct link_map *__unbounded l, ElfW(Word) reloc_offset)
105 __attribute__ ((regparm (2), unused));
106 static ElfW(Addr) profile_fixup (struct link_map *l, ElfW(Word) reloc_offset,
107 ElfW(Addr) retaddr)
108 __attribute__ ((regparm (3), unused));
109 #endif
111 /* Set up the loaded object described by L so its unrelocated PLT
112 entries will jump to the on-demand fixup code in dl-runtime.c. */
114 static inline int __attribute__ ((unused))
115 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
117 Elf32_Addr *got;
118 extern void _dl_runtime_resolve (Elf32_Word) attribute_hidden;
119 extern void _dl_runtime_profile (Elf32_Word) attribute_hidden;
121 if (l->l_info[DT_JMPREL] && lazy)
123 /* The GOT entries for functions in the PLT have not yet been filled
124 in. Their initial contents will arrange when called to push an
125 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
126 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
127 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
128 /* If a library is prelinked but we have to relocate anyway,
129 we have to be able to undo the prelinking of .got.plt.
130 The prelinker saved us here address of .plt + 0x16. */
131 if (got[1])
133 l->l_mach.plt = got[1] + l->l_addr;
134 l->l_mach.gotplt = (Elf32_Addr) &got[3];
136 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
138 /* The got[2] entry contains the address of a function which gets
139 called to get the address of a so far unresolved function and
140 jump to it. The profiling extension of the dynamic linker allows
141 to intercept the calls to collect information. In this case we
142 don't store the address in the GOT so that all future calls also
143 end in this function. */
144 if (__builtin_expect (profile, 0))
146 got[2] = (Elf32_Addr) &_dl_runtime_profile;
148 if (_dl_name_match_p (GL(dl_profile), l))
149 /* This is the object we are looking for. Say that we really
150 want profiling and the timers are started. */
151 GL(dl_profile_map) = l;
153 else
154 /* This function will get called to fix up the GOT entry indicated by
155 the offset on the stack, and then jump to the resolved address. */
156 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
159 return lazy;
162 /* This code is used in dl-runtime.c to call the `fixup' function
163 and then redirect to the address it returns. */
164 #if !defined PROF && !__BOUNDED_POINTERS__
165 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
166 .text\n\
167 .globl _dl_runtime_resolve\n\
168 .type _dl_runtime_resolve, @function\n\
169 .align 16\n\
170 _dl_runtime_resolve:\n\
171 pushl %eax # Preserve registers otherwise clobbered.\n\
172 pushl %ecx\n\
173 pushl %edx\n\
174 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note\n\
175 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.\n\
176 call fixup # Call resolver.\n\
177 popl %edx # Get register content back.\n\
178 popl %ecx\n\
179 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
180 ret $8 # Jump to function address.\n\
181 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
183 .globl _dl_runtime_profile\n\
184 .type _dl_runtime_profile, @function\n\
185 .align 16\n\
186 _dl_runtime_profile:\n\
187 pushl %eax # Preserve registers otherwise clobbered.\n\
188 pushl %ecx\n\
189 pushl %edx\n\
190 movl 20(%esp), %ecx # Load return address\n\
191 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note\n\
192 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.\n\
193 call profile_fixup # Call resolver.\n\
194 popl %edx # Get register content back.\n\
195 popl %ecx\n\
196 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
197 ret $8 # Jump to function address.\n\
198 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
199 .previous\n\
201 #else
202 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
203 .text\n\
204 .globl _dl_runtime_resolve\n\
205 .globl _dl_runtime_profile\n\
206 .type _dl_runtime_resolve, @function\n\
207 .type _dl_runtime_profile, @function\n\
208 .align 16\n\
209 _dl_runtime_resolve:\n\
210 _dl_runtime_profile:\n\
211 pushl %eax # Preserve registers otherwise clobbered.\n\
212 pushl %ecx\n\
213 pushl %edx\n\
214 movl 16(%esp), %edx # Push the arguments for `fixup'\n\
215 movl 12(%esp), %eax\n\
216 pushl %edx\n\
217 pushl %eax\n\
218 call fixup # Call resolver.\n\
219 popl %edx # Pop the parameters\n\
220 popl %ecx\n\
221 popl %edx # Get register content back.\n\
222 popl %ecx\n\
223 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
224 ret $8 # Jump to function address.\n\
225 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
226 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
227 .previous\n\
229 #endif
231 /* Mask identifying addresses reserved for the user program,
232 where the dynamic linker should not map anything. */
233 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
235 /* Initial entry point code for the dynamic linker.
236 The C function `_dl_start' is the real entry point;
237 its return value is the user program's entry point. */
239 #define RTLD_START asm ("\n\
240 .text\n\
241 .align 16\n\
242 0: movl (%esp), %ebx\n\
243 ret\n\
244 .align 16\n\
245 .globl _start\n\
246 .globl _dl_start_user\n\
247 _start:\n\
248 # Note that _dl_start gets the parameter in %eax.\n\
249 movl %esp, %eax\n\
250 call _dl_start\n\
251 _dl_start_user:\n\
252 # Save the user entry point address in %edi.\n\
253 movl %eax, %edi\n\
254 # Point %ebx at the GOT.\n\
255 call 0b\n\
256 addl $_GLOBAL_OFFSET_TABLE_, %ebx\n\
257 # Store the highest stack address\n\
258 movl __libc_stack_end@GOT(%ebx), %eax\n\
259 movl %esp, (%eax)\n\
260 # See if we were run as a command with the executable file\n\
261 # name as an extra leading argument.\n\
262 movl _dl_skip_args@GOTOFF(%ebx), %eax\n\
263 # Pop the original argument count.\n\
264 popl %edx\n\
265 # Adjust the stack pointer to skip _dl_skip_args words.\n\
266 leal (%esp,%eax,4), %esp\n\
267 # Subtract _dl_skip_args from argc.\n\
268 subl %eax, %edx\n\
269 # Push argc back on the stack.\n\
270 push %edx\n\
271 # The special initializer gets called with the stack just\n\
272 # as the application's entry point will see it; it can\n\
273 # switch stacks if it moves these contents over.\n\
274 " RTLD_START_SPECIAL_INIT "\n\
275 # Load the parameters again.\n\
276 # (eax, edx, ecx, *--esp) = (_dl_loaded, argc, argv, envp)\n\
277 movl _rtld_local@GOTOFF(%ebx), %eax\n\
278 leal 8(%esp,%edx,4), %esi\n\
279 leal 4(%esp), %ecx\n\
280 pushl %esi\n\
281 # Call the function to run the initializers.\n\
282 call _dl_init_internal@PLT\n\
283 # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
284 leal _dl_fini@GOTOFF(%ebx), %edx\n\
285 # Jump to the user's entry point.\n\
286 jmp *%edi\n\
287 .previous\n\
290 #ifndef RTLD_START_SPECIAL_INIT
291 # define RTLD_START_SPECIAL_INIT /* nothing */
292 #endif
294 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
295 TLS variable, so undefined references should not be allowed to
296 define the value.
297 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
298 of the main executable's symbols, as for a COPY reloc. */
299 #ifdef USE_TLS
300 # define elf_machine_type_class(type) \
301 ((((type) == R_386_JMP_SLOT || (type) == R_386_TLS_DTPMOD32 \
302 || (type) == R_386_TLS_DTPOFF32 || (type) == R_386_TLS_TPOFF32) \
303 * ELF_RTYPE_CLASS_PLT) \
304 | (((type) == R_386_COPY) * ELF_RTYPE_CLASS_COPY))
305 #else
306 # define elf_machine_type_class(type) \
307 ((((type) == R_386_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
308 | (((type) == R_386_COPY) * ELF_RTYPE_CLASS_COPY))
309 #endif
311 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
312 #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT
314 /* The i386 never uses Elf32_Rela relocations for the dynamic linker.
315 Prelinked libraries may use Elf32_Rela though. */
316 #define ELF_MACHINE_PLT_REL 1
318 /* We define an initialization functions. This is called very early in
319 _dl_sysdep_start. */
320 #define DL_PLATFORM_INIT dl_platform_init ()
322 static inline void __attribute__ ((unused))
323 dl_platform_init (void)
325 if (GL(dl_platform) != NULL && *GL(dl_platform) == '\0')
326 /* Avoid an empty string which would disturb us. */
327 GL(dl_platform) = NULL;
330 static inline Elf32_Addr
331 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
332 const Elf32_Rel *reloc,
333 Elf32_Addr *reloc_addr, Elf32_Addr value)
335 return *reloc_addr = value;
338 /* Return the final value of a plt relocation. */
339 static inline Elf32_Addr
340 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
341 Elf32_Addr value)
343 return value;
346 #endif /* !dl_machine_h */
348 #ifdef RESOLVE
350 /* The i386 never uses Elf32_Rela relocations for the dynamic linker.
351 Prelinked libraries may use Elf32_Rela though. */
352 #ifdef RTLD_BOOTSTRAP
353 # define ELF_MACHINE_NO_RELA 1
354 #endif
356 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
357 MAP is the object containing the reloc. */
359 static inline void
360 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
361 const Elf32_Sym *sym, const struct r_found_version *version,
362 Elf32_Addr *const reloc_addr)
364 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
366 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
367 if (__builtin_expect (r_type == R_386_RELATIVE, 0))
369 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
370 /* This is defined in rtld.c, but nowhere in the static libc.a;
371 make the reference weak so static programs can still link.
372 This declaration cannot be done when compiling rtld.c
373 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
374 common defn for _dl_rtld_map, which is incompatible with a
375 weak decl in the same file. */
376 # ifndef SHARED
377 weak_extern (_dl_rtld_map);
378 # endif
379 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
380 # endif
381 *reloc_addr += map->l_addr;
383 # ifndef RTLD_BOOTSTRAP
384 else if (__builtin_expect (r_type == R_386_NONE, 0))
385 return;
386 # endif
387 else
388 #endif
390 const Elf32_Sym *const refsym = sym;
391 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
392 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
393 Elf32_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
394 #else
395 Elf32_Addr value = RESOLVE (&sym, version, r_type);
397 # ifndef RTLD_BOOTSTRAP
398 if (sym != NULL)
399 # endif
400 value += sym->st_value;
401 #endif
403 switch (r_type)
405 case R_386_GLOB_DAT:
406 case R_386_JMP_SLOT:
407 *reloc_addr = value;
408 break;
410 /* XXX Remove TLS relocations which are not needed. */
412 #ifdef USE_TLS
413 case R_386_TLS_DTPMOD32:
414 # ifdef RTLD_BOOTSTRAP
415 /* During startup the dynamic linker is always the module
416 with index 1.
417 XXX If this relocation is necessary move before RESOLVE
418 call. */
419 *reloc_addr = 1;
420 # else
421 /* Get the information from the link map returned by the
422 resolv function. */
423 if (sym_map != NULL)
424 *reloc_addr = sym_map->l_tls_modid;
425 # endif
426 break;
427 case R_386_TLS_DTPOFF32:
428 # ifndef RTLD_BOOTSTRAP
429 /* During relocation all TLS symbols are defined and used.
430 Therefore the offset is already correct. */
431 if (sym != NULL)
432 *reloc_addr += sym->st_value;
433 # endif
434 break;
435 case R_386_TLS_TPOFF32:
436 /* The offset is positive, backward from the thread pointer. */
437 # ifdef RTLD_BOOTSTRAP
438 *reloc_addr += map->l_tls_offset - sym->st_value;
439 # else
440 /* We know the offset of object the symbol is contained in.
441 It is a positive value which will be subtracted from the
442 thread pointer. To get the variable position in the TLS
443 block we subtract the offset from that of the TLS block. */
444 if (sym_map != NULL && sym != NULL)
445 *reloc_addr += sym_map->l_tls_offset - sym->st_value;
446 # endif
447 break;
448 #endif /* use TLS */
450 #ifndef RTLD_BOOTSTRAP
451 case R_386_32:
452 *reloc_addr += value;
453 break;
454 case R_386_PC32:
455 *reloc_addr += (value - (Elf32_Addr) reloc_addr);
456 break;
457 case R_386_COPY:
458 if (sym == NULL)
459 /* This can happen in trace mode if an object could not be
460 found. */
461 break;
462 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
463 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
464 && GL(dl_verbose)))
466 const char *strtab;
468 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
469 _dl_error_printf ("\
470 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
471 rtld_progname ?: "<program name unknown>",
472 strtab + refsym->st_name);
474 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
475 refsym->st_size));
476 break;
477 default:
478 _dl_reloc_bad_type (map, r_type, 0);
479 break;
480 #endif
485 #ifndef RTLD_BOOTSTRAP
486 static inline void
487 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
488 const Elf32_Sym *sym, const struct r_found_version *version,
489 Elf32_Addr *const reloc_addr)
491 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
493 if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE)
494 *reloc_addr = map->l_addr + reloc->r_addend;
495 else if (r_type != R_386_NONE)
497 # ifdef USE_TLS
498 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
499 Elf32_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
500 # else
501 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
502 if (sym != NULL)
503 value += sym->st_value;
504 #endif
506 switch (ELF32_R_TYPE (reloc->r_info))
508 case R_386_GLOB_DAT:
509 case R_386_JMP_SLOT:
510 case R_386_32:
511 *reloc_addr = value + reloc->r_addend;
512 break;
513 case R_386_PC32:
514 *reloc_addr = (value + reloc->r_addend - (Elf32_Addr) reloc_addr);
515 break;
516 /* XXX Do we have to handle the TLS relocation here? */
517 default:
518 /* We add these checks in the version to relocate ld.so only
519 if we are still debugging. */
520 _dl_reloc_bad_type (map, r_type, 0);
521 break;
525 #endif
527 static inline void
528 elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc,
529 Elf32_Addr *const reloc_addr)
531 assert (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE);
532 *reloc_addr += l_addr;
535 #ifndef RTLD_BOOTSTRAP
536 static inline void
537 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
538 Elf32_Addr *const reloc_addr)
540 *reloc_addr = l_addr + reloc->r_addend;
542 #endif
544 static inline void
545 elf_machine_lazy_rel (struct link_map *map,
546 Elf32_Addr l_addr, const Elf32_Rel *reloc)
548 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
549 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
550 /* Check for unexpected PLT reloc type. */
551 if (__builtin_expect (r_type == R_386_JMP_SLOT, 1))
553 if (__builtin_expect (map->l_mach.plt, 0) == 0)
554 *reloc_addr += l_addr;
555 else
556 *reloc_addr = (map->l_mach.plt
557 + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 4);
559 else
560 _dl_reloc_bad_type (map, r_type, 1);
563 #ifndef RTLD_BOOTSTRAP
565 static inline void
566 elf_machine_lazy_rela (struct link_map *map,
567 Elf32_Addr l_addr, const Elf32_Rela *reloc)
571 #endif
573 #endif /* RESOLVE */