Update.
[glibc.git] / sysdeps / i386 / dl-machine.h
blob69a0c5550ea4596d62ec524c4b76f24b41d53d53
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 /* Return nonzero iff ELF header is compatible with the running host. */
28 static inline int __attribute__ ((unused))
29 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
31 return ehdr->e_machine == EM_386;
35 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
36 first element of the GOT. This must be inlined in a function which
37 uses global data. */
38 static inline Elf32_Addr __attribute__ ((unused))
39 elf_machine_dynamic (void)
41 register Elf32_Addr *got asm ("%ebx");
42 return *got;
46 /* Return the run-time load address of the shared object. */
47 static inline Elf32_Addr __attribute__ ((unused))
48 elf_machine_load_address (void)
50 Elf32_Addr addr;
51 asm ("leal _dl_start@GOTOFF(%%ebx), %0\n"
52 "subl _dl_start@GOT(%%ebx), %0"
53 : "=r" (addr) : : "cc");
54 return addr;
57 #if !defined PROF && !__BOUNDED_POINTERS__
58 /* We add a declaration of this function here so that in dl-runtime.c
59 the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters
60 in registers.
62 We cannot use this scheme for profiling because the _mcount call
63 destroys the passed register information. */
64 /* GKM FIXME: Fix trampoline to pass bounds so we can do
65 without the `__unbounded' qualifier. */
66 static ElfW(Addr) fixup (struct link_map *__unbounded l, ElfW(Word) reloc_offset)
67 __attribute__ ((regparm (2), unused));
68 static ElfW(Addr) profile_fixup (struct link_map *l, ElfW(Word) reloc_offset,
69 ElfW(Addr) retaddr)
70 __attribute__ ((regparm (3), unused));
71 #endif
73 /* Set up the loaded object described by L so its unrelocated PLT
74 entries will jump to the on-demand fixup code in dl-runtime.c. */
76 static inline int __attribute__ ((unused))
77 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
79 Elf32_Addr *got;
80 extern void _dl_runtime_resolve (Elf32_Word);
81 extern void _dl_runtime_profile (Elf32_Word);
83 if (l->l_info[DT_JMPREL] && lazy)
85 /* The GOT entries for functions in the PLT have not yet been filled
86 in. Their initial contents will arrange when called to push an
87 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
88 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
89 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
90 /* If a library is prelinked but we have to relocate anyway,
91 we have to be able to undo the prelinking of .got.plt.
92 The prelinker saved us here address of .plt + 0x16. */
93 if (got[1])
95 l->l_mach.plt = got[1] + l->l_addr;
96 l->l_mach.gotplt = (Elf32_Addr) &got[3];
98 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
100 /* The got[2] entry contains the address of a function which gets
101 called to get the address of a so far unresolved function and
102 jump to it. The profiling extension of the dynamic linker allows
103 to intercept the calls to collect information. In this case we
104 don't store the address in the GOT so that all future calls also
105 end in this function. */
106 if (__builtin_expect (profile, 0))
108 got[2] = (Elf32_Addr) &_dl_runtime_profile;
110 if (_dl_name_match_p (GL(dl_profile), l))
111 /* This is the object we are looking for. Say that we really
112 want profiling and the timers are started. */
113 GL(dl_profile_map) = l;
115 else
116 /* This function will get called to fix up the GOT entry indicated by
117 the offset on the stack, and then jump to the resolved address. */
118 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
121 return lazy;
124 /* This code is used in dl-runtime.c to call the `fixup' function
125 and then redirect to the address it returns. */
126 #if !defined PROF && !__BOUNDED_POINTERS__
127 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
128 .text\n\
129 .globl _dl_runtime_resolve\n\
130 .type _dl_runtime_resolve, @function\n\
131 .align 16\n\
132 _dl_runtime_resolve:\n\
133 pushl %eax # Preserve registers otherwise clobbered.\n\
134 pushl %ecx\n\
135 pushl %edx\n\
136 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note\n\
137 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.\n\
138 call fixup # Call resolver.\n\
139 popl %edx # Get register content back.\n\
140 popl %ecx\n\
141 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
142 ret $8 # Jump to function address.\n\
143 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
145 .globl _dl_runtime_profile\n\
146 .type _dl_runtime_profile, @function\n\
147 .align 16\n\
148 _dl_runtime_profile:\n\
149 pushl %eax # Preserve registers otherwise clobbered.\n\
150 pushl %ecx\n\
151 pushl %edx\n\
152 movl 20(%esp), %ecx # Load return address\n\
153 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note\n\
154 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.\n\
155 call profile_fixup # Call resolver.\n\
156 popl %edx # Get register content back.\n\
157 popl %ecx\n\
158 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
159 ret $8 # Jump to function address.\n\
160 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
161 .previous\n\
163 #else
164 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
165 .text\n\
166 .globl _dl_runtime_resolve\n\
167 .globl _dl_runtime_profile\n\
168 .type _dl_runtime_resolve, @function\n\
169 .type _dl_runtime_profile, @function\n\
170 .align 16\n\
171 _dl_runtime_resolve:\n\
172 _dl_runtime_profile:\n\
173 pushl %eax # Preserve registers otherwise clobbered.\n\
174 pushl %ecx\n\
175 pushl %edx\n\
176 movl 16(%esp), %edx # Push the arguments for `fixup'\n\
177 movl 12(%esp), %eax\n\
178 pushl %edx\n\
179 pushl %eax\n\
180 call fixup # Call resolver.\n\
181 popl %edx # Pop the parameters\n\
182 popl %ecx\n\
183 popl %edx # Get register content back.\n\
184 popl %ecx\n\
185 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
186 ret $8 # Jump to function address.\n\
187 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
188 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
189 .previous\n\
191 #endif
193 /* Mask identifying addresses reserved for the user program,
194 where the dynamic linker should not map anything. */
195 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
197 /* Initial entry point code for the dynamic linker.
198 The C function `_dl_start' is the real entry point;
199 its return value is the user program's entry point. */
201 #define RTLD_START asm ("\n\
202 .text\n\
203 .align 16\n\
204 0: movl (%esp), %ebx\n\
205 ret\n\
206 .align 16\n\
207 .globl _start\n\
208 .globl _dl_start_user\n\
209 _start:\n\
210 # Note that _dl_start gets the parameter in %eax.\n\
211 movl %esp, %eax\n\
212 call _dl_start\n\
213 _dl_start_user:\n\
214 # Save the user entry point address in %edi.\n\
215 movl %eax, %edi\n\
216 # Point %ebx at the GOT.\n\
217 call 0b\n\
218 addl $_GLOBAL_OFFSET_TABLE_, %ebx\n\
219 # Store the highest stack address\n\
220 movl __libc_stack_end@GOT(%ebx), %eax\n\
221 movl %esp, (%eax)\n\
222 # See if we were run as a command with the executable file\n\
223 # name as an extra leading argument.\n\
224 movl _dl_skip_args@GOT(%ebx), %eax\n\
225 movl (%eax), %eax\n\
226 # Pop the original argument count.\n\
227 popl %edx\n\
228 # Adjust the stack pointer to skip _dl_skip_args words.\n\
229 leal (%esp,%eax,4), %esp\n\
230 # Subtract _dl_skip_args from argc.\n\
231 subl %eax, %edx\n\
232 # Push argc back on the stack.\n\
233 push %edx\n\
234 # The special initializer gets called with the stack just\n\
235 # as the application's entry point will see it; it can\n\
236 # switch stacks if it moves these contents over.\n\
237 " RTLD_START_SPECIAL_INIT "\n\
238 # Load the parameters again.\n\
239 # (eax, edx, ecx, *--esp) = (_dl_loaded, argc, argv, envp)\n\
240 movl _rtld_global@GOT(%ebx), %esi\n\
241 leal 8(%esp,%edx,4), %eax\n\
242 leal 4(%esp), %ecx\n\
243 pushl %eax\n\
244 movl (%esi), %eax\n\
245 # Call the function to run the initializers.\n\
246 call _dl_init_internal@PLT\n\
247 # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
248 movl _dl_fini@GOT(%ebx), %edx\n\
249 # Jump to the user's entry point.\n\
250 jmp *%edi\n\
251 .previous\n\
254 #ifndef RTLD_START_SPECIAL_INIT
255 # define RTLD_START_SPECIAL_INIT /* nothing */
256 #endif
258 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
259 PLT entries should not be allowed to define the value.
260 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
261 of the main executable's symbols, as for a COPY reloc. */
262 #define elf_machine_type_class(type) \
263 ((((type) == R_386_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
264 | (((type) == R_386_COPY) * ELF_RTYPE_CLASS_COPY))
266 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
267 #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT
269 /* The i386 never uses Elf32_Rela relocations for the dynamic linker.
270 Prelinked libraries may use Elf32_Rela though. */
271 #define ELF_MACHINE_PLT_REL 1
273 /* We define an initialization functions. This is called very early in
274 _dl_sysdep_start. */
275 #define DL_PLATFORM_INIT dl_platform_init ()
277 static inline void __attribute__ ((unused))
278 dl_platform_init (void)
280 if (GL(dl_platform) != NULL && *GL(dl_platform) == '\0')
281 /* Avoid an empty string which would disturb us. */
282 GL(dl_platform) = NULL;
285 static inline Elf32_Addr
286 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
287 const Elf32_Rel *reloc,
288 Elf32_Addr *reloc_addr, Elf32_Addr value)
290 return *reloc_addr = value;
293 /* Return the final value of a plt relocation. */
294 static inline Elf32_Addr
295 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
296 Elf32_Addr value)
298 return value;
301 #endif /* !dl_machine_h */
303 #ifdef RESOLVE
305 /* The i386 never uses Elf32_Rela relocations for the dynamic linker.
306 Prelinked libraries may use Elf32_Rela though. */
307 #ifdef RTLD_BOOTSTRAP
308 # define ELF_MACHINE_NO_RELA 1
309 #endif
311 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
312 MAP is the object containing the reloc. */
314 static inline void
315 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
316 const Elf32_Sym *sym, const struct r_found_version *version,
317 Elf32_Addr *const reloc_addr)
319 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
321 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
322 if (__builtin_expect (r_type == R_386_RELATIVE, 0))
324 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
325 /* This is defined in rtld.c, but nowhere in the static libc.a;
326 make the reference weak so static programs can still link.
327 This declaration cannot be done when compiling rtld.c
328 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
329 common defn for _dl_rtld_map, which is incompatible with a
330 weak decl in the same file. */
331 # ifndef SHARED
332 weak_extern (_dl_rtld_map);
333 # endif
334 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
335 # endif
336 *reloc_addr += map->l_addr;
338 # ifndef RTLD_BOOTSTRAP
339 else if (__builtin_expect (r_type == R_386_NONE, 0))
340 return;
341 # endif
342 else
343 #endif
345 #ifdef RTLD_BOOTSTRAP
346 Elf32_Addr value;
348 assert (r_type == R_386_GLOB_DAT || r_type == R_386_JMP_SLOT);
350 value = RESOLVE (&sym, version, r_type);
351 *reloc_addr = value + sym->st_value;
352 #else
353 const Elf32_Sym *const refsym = sym;
354 Elf32_Addr value = RESOLVE (&sym, version, r_type);
355 if (sym)
356 value += sym->st_value;
358 switch (r_type)
360 case R_386_GLOB_DAT:
361 case R_386_JMP_SLOT:
362 *reloc_addr = value;
363 break;
364 case R_386_32:
365 *reloc_addr += value;
366 break;
367 case R_386_PC32:
368 *reloc_addr += (value - (Elf32_Addr) reloc_addr);
369 break;
370 case R_386_COPY:
371 if (sym == NULL)
372 /* This can happen in trace mode if an object could not be
373 found. */
374 break;
375 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
376 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
377 && GL(dl_verbose)))
379 const char *strtab;
381 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
382 _dl_error_printf ("\
383 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
384 _dl_argv[0] ?: "<program name unknown>",
385 strtab + refsym->st_name);
387 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
388 refsym->st_size));
389 break;
390 default:
391 _dl_reloc_bad_type (map, r_type, 0);
392 break;
394 #endif
398 #ifndef RTLD_BOOTSTRAP
399 static inline void
400 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
401 const Elf32_Sym *sym, const struct r_found_version *version,
402 Elf32_Addr *const reloc_addr)
404 if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE)
405 *reloc_addr = map->l_addr + reloc->r_addend;
406 else if (ELF32_R_TYPE (reloc->r_info) != R_386_NONE)
408 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
409 if (sym)
410 value += sym->st_value;
412 switch (ELF32_R_TYPE (reloc->r_info))
414 case R_386_GLOB_DAT:
415 case R_386_JMP_SLOT:
416 case R_386_32:
417 *reloc_addr = value + reloc->r_addend;
418 break;
419 case R_386_PC32:
420 *reloc_addr = (value + reloc->r_addend - (Elf32_Addr) reloc_addr);
421 break;
422 default:
423 /* We add these checks in the version to relocate ld.so only
424 if we are still debugging. */
425 _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 0);
426 break;
430 #endif
432 static inline void
433 elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc,
434 Elf32_Addr *const reloc_addr)
436 assert (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE);
437 *reloc_addr += l_addr;
440 #ifndef RTLD_BOOTSTRAP
441 static inline void
442 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
443 Elf32_Addr *const reloc_addr)
445 *reloc_addr = l_addr + reloc->r_addend;
447 #endif
449 static inline void
450 elf_machine_lazy_rel (struct link_map *map,
451 Elf32_Addr l_addr, const Elf32_Rel *reloc)
453 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
454 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
455 /* Check for unexpected PLT reloc type. */
456 if (__builtin_expect (r_type == R_386_JMP_SLOT, 1))
458 if (__builtin_expect (map->l_mach.plt, 0) == 0)
459 *reloc_addr += l_addr;
460 else
461 *reloc_addr = (map->l_mach.plt
462 + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 4);
464 else
465 _dl_reloc_bad_type (map, r_type, 1);
468 #ifndef RTLD_BOOTSTRAP
470 static inline void
471 elf_machine_lazy_rela (struct link_map *map,
472 Elf32_Addr l_addr, const Elf32_Rela *reloc)
476 #endif
478 #endif /* RESOLVE */