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
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
38 static inline Elf32_Addr
__attribute__ ((unused
))
39 elf_machine_dynamic (void)
41 register Elf32_Addr
*got
asm ("%ebx");
46 /* Return the run-time load address of the shared object. */
47 static inline Elf32_Addr
__attribute__ ((unused
))
48 elf_machine_load_address (void)
51 asm ("leal _dl_start@GOTOFF(%%ebx), %0\n"
52 "subl _dl_start@GOT(%%ebx), %0"
53 : "=r" (addr
) : : "cc");
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
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
,
70 __attribute__ ((regparm (3), unused
));
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
)
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. */
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
;
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
;
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 ("\
129 .globl _dl_runtime_resolve\n\
130 .type _dl_runtime_resolve, @function\n\
132 _dl_runtime_resolve:\n\
133 pushl %eax # Preserve registers otherwise clobbered.\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\
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\
148 _dl_runtime_profile:\n\
149 pushl %eax # Preserve registers otherwise clobbered.\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\
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\
164 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\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\
171 _dl_runtime_resolve:\n\
172 _dl_runtime_profile:\n\
173 pushl %eax # Preserve registers otherwise clobbered.\n\
176 movl 16(%esp), %edx # Push the arguments for `fixup'\n\
177 movl 12(%esp), %eax\n\
180 call fixup # Call resolver.\n\
181 popl %edx # Pop the parameters\n\
183 popl %edx # Get register content back.\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\
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\
204 0: movl (%esp), %ebx\n\
208 .globl _dl_start_user\n\
210 # Note that _dl_start gets the parameter in %eax.\n\
214 # Save the user entry point address in %edi.\n\
216 # Point %ebx at the GOT.\n\
218 addl $_GLOBAL_OFFSET_TABLE_, %ebx\n\
219 # Store the highest stack address\n\
220 movl __libc_stack_end@GOT(%ebx), %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\
226 # Pop the original argument count.\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\
232 # Push argc back on the stack.\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\
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\
254 #ifndef RTLD_START_SPECIAL_INIT
255 # define RTLD_START_SPECIAL_INIT /* nothing */
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
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
,
301 #endif /* !dl_machine_h */
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
311 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
312 MAP is the object containing the reloc. */
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. */
332 weak_extern (_dl_rtld_map
);
334 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
336 *reloc_addr
+= map
->l_addr
;
338 # ifndef RTLD_BOOTSTRAP
339 else if (__builtin_expect (r_type
== R_386_NONE
, 0))
345 #ifdef RTLD_BOOTSTRAP
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
;
353 const Elf32_Sym
*const refsym
= sym
;
354 Elf32_Addr value
= RESOLVE (&sym
, version
, r_type
);
356 value
+= sym
->st_value
;
365 *reloc_addr
+= value
;
368 *reloc_addr
+= (value
- (Elf32_Addr
) reloc_addr
);
372 /* This can happen in trace mode if an object could not be
375 if (__builtin_expect (sym
->st_size
> refsym
->st_size
, 0)
376 || (__builtin_expect (sym
->st_size
< refsym
->st_size
, 0)
381 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
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
,
391 _dl_reloc_bad_type (map
, r_type
, 0);
398 #ifndef RTLD_BOOTSTRAP
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
));
410 value
+= sym
->st_value
;
412 switch (ELF32_R_TYPE (reloc
->r_info
))
417 *reloc_addr
= value
+ reloc
->r_addend
;
420 *reloc_addr
= (value
+ reloc
->r_addend
- (Elf32_Addr
) reloc_addr
);
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);
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
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
;
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
;
461 *reloc_addr
= (map
->l_mach
.plt
462 + (((Elf32_Addr
) reloc_addr
) - map
->l_mach
.gotplt
) * 4);
465 _dl_reloc_bad_type (map
, r_type
, 1);
468 #ifndef RTLD_BOOTSTRAP
471 elf_machine_lazy_rela (struct link_map
*map
,
472 Elf32_Addr l_addr
, const Elf32_Rela
*reloc
)