1 /* Machine-dependent ELF dynamic relocation inline functions. i386 version.
2 Copyright (C) 1995, 1996, 1997, 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
23 #define ELF_MACHINE_NAME "i386"
25 #include <sys/param.h>
29 /* Return nonzero iff E_MACHINE is compatible with the running host. */
30 static inline int __attribute__ ((unused
))
31 elf_machine_matches_host (Elf32_Half e_machine
)
44 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
45 first element of the GOT. This must be inlined in a function which
47 static inline Elf32_Addr
__attribute__ ((unused
))
48 elf_machine_dynamic (void)
50 register Elf32_Addr
*got
asm ("%ebx");
55 /* Return the run-time load address of the shared object. */
56 static inline Elf32_Addr
__attribute__ ((unused
))
57 elf_machine_load_address (void)
60 asm ("leal _dl_start@GOTOFF(%%ebx), %0\n"
61 "subl _dl_start@GOT(%%ebx), %0"
62 : "=r" (addr
) : : "cc");
67 /* We add a declaration of this function here so that in dl-runtime.c
68 the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters
71 We cannot use this scheme for profiling because the _mcount call
72 destroys the passed register information. */
73 static ElfW(Addr
) fixup (struct link_map
*l
, ElfW(Word
) reloc_offset
)
74 __attribute__ ((regparm (2), unused
));
75 static ElfW(Addr
) profile_fixup (struct link_map
*l
, ElfW(Word
) reloc_offset
,
77 __attribute__ ((regparm (3), unused
));
80 /* Set up the loaded object described by L so its unrelocated PLT
81 entries will jump to the on-demand fixup code in dl-runtime.c. */
83 static inline int __attribute__ ((unused
))
84 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
87 extern void _dl_runtime_resolve (Elf32_Word
);
88 extern void _dl_runtime_profile (Elf32_Word
);
90 if (l
->l_info
[DT_JMPREL
] && lazy
)
92 /* The GOT entries for functions in the PLT have not yet been filled
93 in. Their initial contents will arrange when called to push an
94 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
95 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
96 got
= (Elf32_Addr
*) (l
->l_addr
+ l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
97 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
99 /* The got[2] entry contains the address of a function which gets
100 called to get the address of a so far unresolved function and
101 jump to it. The profiling extension of the dynamic linker allows
102 to intercept the calls to collect information. In this case we
103 don't store the address in the GOT so that all future calls also
104 end in this function. */
107 got
[2] = (Elf32_Addr
) &_dl_runtime_profile
;
109 if (_dl_name_match_p (_dl_profile
, l
))
110 /* This is the object we are looking for. Say that we really
111 want profiling and the timers are started. */
115 /* This function will get called to fix up the GOT entry indicated by
116 the offset on the stack, and then jump to the resolved address. */
117 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
123 /* This code is used in dl-runtime.c to call the `fixup' function
124 and then redirect to the address it returns. */
126 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
128 .globl _dl_runtime_resolve
129 .type _dl_runtime_resolve, @function
132 pushl %eax # Preserve registers otherwise clobbered.
135 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note
136 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.
137 call fixup # Call resolver.
138 popl %edx # Get register content back.
140 xchgl %eax, (%esp) # Get %eax contents end store function address.
141 ret $8 # Jump to function address.
142 .size _dl_runtime_resolve, .-_dl_runtime_resolve
144 .globl _dl_runtime_profile
145 .type _dl_runtime_profile, @function
148 pushl %eax # Preserve registers otherwise clobbered.
151 movl 20(%esp), %ecx # Load return address
152 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note
153 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.
154 call profile_fixup # Call resolver.
155 popl %edx # Get register content back.
157 xchgl %eax, (%esp) # Get %eax contents end store function address.
158 ret $8 # Jump to function address.
159 .size _dl_runtime_profile, .-_dl_runtime_profile
163 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
165 .globl _dl_runtime_resolve
166 .globl _dl_runtime_profile
167 .type _dl_runtime_resolve, @function
168 .type _dl_runtime_profile, @function
172 pushl %eax # Preserve registers otherwise clobbered.
175 movl 16(%esp), %edx # Push the arguments for `fixup'
179 call fixup # Call resolver.
180 popl %edx # Pop the parameters
182 popl %edx # Get register content back.
184 xchgl %eax, (%esp) # Get %eax contents end store function address.
185 ret $8 # Jump to function address.
186 .size _dl_runtime_resolve, .-_dl_runtime_resolve
187 .size _dl_runtime_profile, .-_dl_runtime_profile
192 /* Mask identifying addresses reserved for the user program,
193 where the dynamic linker should not map anything. */
194 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
196 /* Initial entry point code for the dynamic linker.
197 The C function `_dl_start' is the real entry point;
198 its return value is the user program's entry point. */
200 #define RTLD_START asm ("\
203 .globl _dl_start_user\n\
209 # Save the user entry point address in %edi.\n\
211 # Point %ebx at the GOT.
214 addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\
215 # Store the highest stack address\n\
216 movl __libc_stack_end@GOT(%ebx), %eax\n\
218 # See if we were run as a command with the executable file\n\
219 # name as an extra leading argument.\n\
220 movl _dl_skip_args@GOT(%ebx), %eax\n\
222 # Pop the original argument count.\n\
224 # Subtract _dl_skip_args from it.\n\
226 # Adjust the stack pointer to skip _dl_skip_args words.\n\
227 leal (%esp,%eax,4), %esp\n\
228 # Push back the modified argument count.\n\
230 # Push _dl_default_scope[2] as argument in _dl_init_next call below.\n\
231 movl _dl_default_scope@GOT(%ebx), %eax\n\
232 movl 8(%eax), %esi\n\
234 # Call _dl_init_next to return the address of an initializer\n\
235 # function to run.\n\
236 call _dl_init_next@PLT\n\
237 # Check for zero return, when out of initializers.\n\
240 # Call the shared object initializer function.\n\
241 # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\
242 # and the return address pushed by this call;\n\
243 # the initializer is called with the stack just\n\
244 # as it appears on entry, and it is free to move\n\
245 # the stack around, as long as it winds up jumping to\n\
246 # the return address on the top of the stack.\n\
248 # Loop to call _dl_init_next for the next initializer.\n\
250 1: # Clear the startup flag.\n\
251 movl _dl_starting_up@GOT(%ebx), %eax\n\
253 # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
254 movl _dl_fini@GOT(%ebx), %edx\n\
255 # Jump to the user's entry point.\n\
260 /* Nonzero iff TYPE should not be allowed to resolve to one of
261 the main executable's symbols, as for a COPY reloc. */
262 #define elf_machine_lookup_noexec_p(type) ((type) == R_386_COPY)
264 /* Nonzero iff TYPE describes relocation of a PLT entry, so
265 PLT entries should not be allowed to define the value. */
266 #define elf_machine_lookup_noplt_p(type) ((type) == R_386_JMP_SLOT)
268 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
269 #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT
271 /* The i386 never uses Elf32_Rela relocations. */
272 #define ELF_MACHINE_NO_RELA 1
274 /* We define an initialization functions. This is called very early in
276 #define DL_PLATFORM_INIT dl_platform_init ()
278 extern const char *_dl_platform
;
280 static inline void __attribute__ ((unused
))
281 dl_platform_init (void)
283 if (_dl_platform
!= NULL
&& *_dl_platform
== '\0')
284 /* Avoid an empty string which would disturb us. */
289 elf_machine_fixup_plt (struct link_map
*map
, const Elf32_Rel
*reloc
,
290 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
295 /* Return the final value of a plt relocation. */
296 static inline Elf32_Addr
297 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rel
*reloc
,
303 #endif /* !dl_machine_h */
307 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
308 MAP is the object containing the reloc. */
311 elf_machine_rel (struct link_map
*map
, const Elf32_Rel
*reloc
,
312 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
313 Elf32_Addr
*const reloc_addr
)
315 if (ELF32_R_TYPE (reloc
->r_info
) == R_386_RELATIVE
)
317 #ifndef RTLD_BOOTSTRAP
318 if (map
!= &_dl_rtld_map
) /* Already done in rtld itself. */
320 *reloc_addr
+= map
->l_addr
;
322 else if (ELF32_R_TYPE (reloc
->r_info
) != R_386_NONE
)
324 const Elf32_Sym
*const refsym
= sym
;
325 Elf32_Addr value
= RESOLVE (&sym
, version
, ELF32_R_TYPE (reloc
->r_info
));
327 value
+= sym
->st_value
;
329 switch (ELF32_R_TYPE (reloc
->r_info
))
333 /* This can happen in trace mode if an object could not be
336 if (sym
->st_size
> refsym
->st_size
337 || (_dl_verbose
&& sym
->st_size
< refsym
->st_size
))
341 strtab
= ((const char *) map
->l_addr
342 + map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
343 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>",
344 ": Symbol `", strtab
+ refsym
->st_name
,
345 "' has different size in shared object, "
346 "consider re-linking\n", NULL
);
348 memcpy (reloc_addr
, (void *) value
, MIN (sym
->st_size
,
357 #ifndef RTLD_BOOTSTRAP
358 /* This is defined in rtld.c, but nowhere in the static
359 libc.a; make the reference weak so static programs can
360 still link. This declaration cannot be done when
361 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
362 rtld.c contains the common defn for _dl_rtld_map, which
363 is incompatible with a weak decl in the same file. */
364 weak_extern (_dl_rtld_map
);
365 if (map
== &_dl_rtld_map
)
366 /* Undo the relocation done here during bootstrapping.
367 Now we will relocate it anew, possibly using a
368 binding found in the user program or a loaded library
369 rather than the dynamic linker's built-in definitions
370 used while loading those libraries. */
371 value
-= map
->l_addr
+ refsym
->st_value
;
373 *reloc_addr
+= value
;
377 *reloc_addr
+= (value
- (Elf32_Addr
) reloc_addr
);
380 assert (! "unexpected dynamic reloc type");
387 elf_machine_lazy_rel (struct link_map
*map
, const Elf32_Rel
*reloc
)
389 Elf32_Addr
*const reloc_addr
= (void *) (map
->l_addr
+ reloc
->r_offset
);
390 switch (ELF32_R_TYPE (reloc
->r_info
))
393 *reloc_addr
+= map
->l_addr
;
396 assert (! "unexpected PLT reloc type");