Adjusted.
[glibc.git] / sysdeps / i386 / dl-machine.h
blobd9247b8a8099b243e5b377d05e522a22a32de571
1 /* Machine-dependent ELF dynamic relocation inline functions. i386 version.
2 Copyright (C) 1995,96,97,98,99,2000,2001 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. */
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 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
92 /* The got[2] entry contains the address of a function which gets
93 called to get the address of a so far unresolved function and
94 jump to it. The profiling extension of the dynamic linker allows
95 to intercept the calls to collect information. In this case we
96 don't store the address in the GOT so that all future calls also
97 end in this function. */
98 if (__builtin_expect (profile, 0))
100 got[2] = (Elf32_Addr) &_dl_runtime_profile;
102 if (_dl_name_match_p (_dl_profile, l))
103 /* This is the object we are looking for. Say that we really
104 want profiling and the timers are started. */
105 _dl_profile_map = l;
107 else
108 /* This function will get called to fix up the GOT entry indicated by
109 the offset on the stack, and then jump to the resolved address. */
110 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
113 return lazy;
116 /* This code is used in dl-runtime.c to call the `fixup' function
117 and then redirect to the address it returns. */
118 #if !defined PROF && !__BOUNDED_POINTERS__
119 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
120 .text\n\
121 .globl _dl_runtime_resolve\n\
122 .type _dl_runtime_resolve, @function\n\
123 .align 16\n\
124 _dl_runtime_resolve:\n\
125 pushl %eax # Preserve registers otherwise clobbered.\n\
126 pushl %ecx\n\
127 pushl %edx\n\
128 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note\n\
129 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.\n\
130 call fixup # Call resolver.\n\
131 popl %edx # Get register content back.\n\
132 popl %ecx\n\
133 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
134 ret $8 # Jump to function address.\n\
135 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
137 .globl _dl_runtime_profile\n\
138 .type _dl_runtime_profile, @function\n\
139 .align 16\n\
140 _dl_runtime_profile:\n\
141 pushl %eax # Preserve registers otherwise clobbered.\n\
142 pushl %ecx\n\
143 pushl %edx\n\
144 movl 20(%esp), %ecx # Load return address\n\
145 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note\n\
146 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.\n\
147 call profile_fixup # Call resolver.\n\
148 popl %edx # Get register content back.\n\
149 popl %ecx\n\
150 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
151 ret $8 # Jump to function address.\n\
152 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
153 .previous\n\
155 #else
156 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
157 .text\n\
158 .globl _dl_runtime_resolve\n\
159 .globl _dl_runtime_profile\n\
160 .type _dl_runtime_resolve, @function\n\
161 .type _dl_runtime_profile, @function\n\
162 .align 16\n\
163 _dl_runtime_resolve:\n\
164 _dl_runtime_profile:\n\
165 pushl %eax # Preserve registers otherwise clobbered.\n\
166 pushl %ecx\n\
167 pushl %edx\n\
168 movl 16(%esp), %edx # Push the arguments for `fixup'\n\
169 movl 12(%esp), %eax\n\
170 pushl %edx\n\
171 pushl %eax\n\
172 call fixup # Call resolver.\n\
173 popl %edx # Pop the parameters\n\
174 popl %ecx\n\
175 popl %edx # Get register content back.\n\
176 popl %ecx\n\
177 xchgl %eax, (%esp) # Get %eax contents end store function address.\n\
178 ret $8 # Jump to function address.\n\
179 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
180 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
181 .previous\n\
183 #endif
185 /* Mask identifying addresses reserved for the user program,
186 where the dynamic linker should not map anything. */
187 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
189 /* Initial entry point code for the dynamic linker.
190 The C function `_dl_start' is the real entry point;
191 its return value is the user program's entry point. */
193 #define RTLD_START asm ("\n\
194 .text\n\
195 .align 16\n\
196 0: movl (%esp), %ebx\n\
197 ret\n\
198 .align 16\n\
199 .globl _start\n\
200 .globl _dl_start_user\n\
201 _start:\n\
202 pushl %esp\n\
203 call _dl_start\n\
204 popl %ebx\n\
205 _dl_start_user:\n\
206 # Save the user entry point address in %edi.\n\
207 movl %eax, %edi\n\
208 # Point %ebx at the GOT.\n\
209 call 0b\n\
210 addl $_GLOBAL_OFFSET_TABLE_, %ebx\n\
211 # Store the highest stack address\n\
212 movl __libc_stack_end@GOT(%ebx), %eax\n\
213 movl %esp, (%eax)\n\
214 # See if we were run as a command with the executable file\n\
215 # name as an extra leading argument.\n\
216 movl _dl_skip_args@GOT(%ebx), %eax\n\
217 movl (%eax), %eax\n\
218 # Pop the original argument count.\n\
219 popl %edx\n\
220 # Adjust the stack pointer to skip _dl_skip_args words.\n\
221 leal (%esp,%eax,4), %esp\n\
222 # Subtract _dl_skip_args from argc.\n\
223 subl %eax, %edx\n\
224 # Push argc back on the stack.\n\
225 push %edx\n\
226 # The special initializer gets called with the stack just\n\
227 # as the application's entry point will see it; it can\n\
228 # switch stacks if it moves these contents over.\n\
229 " RTLD_START_SPECIAL_INIT "\n\
230 # Load the parameters again.\n\
231 # (eax, edx, ecx, *--esp) = (_dl_loaded, argc, argv, envp)\n\
232 movl _dl_loaded@GOT(%ebx), %esi\n\
233 leal 8(%esp,%edx,4), %eax\n\
234 leal 4(%esp), %ecx\n\
235 pushl %eax\n\
236 movl (%esi), %eax\n\
237 # Call the function to run the initializers.\n\
238 call _dl_init@PLT\n\
239 # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
240 movl _dl_fini@GOT(%ebx), %edx\n\
241 # Jump to the user's entry point.\n\
242 jmp *%edi\n\
243 .previous\n\
246 #ifndef RTLD_START_SPECIAL_INIT
247 #define RTLD_START_SPECIAL_INIT /* nothing */
248 #endif
250 /* Nonzero iff TYPE should not be allowed to resolve to one of
251 the main executable's symbols, as for a COPY reloc. */
252 #define elf_machine_lookup_noexec_p(type) ((type) == R_386_COPY)
254 /* Nonzero iff TYPE describes relocation of a PLT entry, so
255 PLT entries should not be allowed to define the value. */
256 #define elf_machine_lookup_noplt_p(type) ((type) == R_386_JMP_SLOT)
258 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
259 #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT
261 /* The i386 never uses Elf32_Rela relocations. */
262 #define ELF_MACHINE_NO_RELA 1
264 /* We define an initialization functions. This is called very early in
265 _dl_sysdep_start. */
266 #define DL_PLATFORM_INIT dl_platform_init ()
268 extern const char *_dl_platform;
270 static inline void __attribute__ ((unused))
271 dl_platform_init (void)
273 if (_dl_platform != NULL && *_dl_platform == '\0')
274 /* Avoid an empty string which would disturb us. */
275 _dl_platform = NULL;
278 static inline Elf32_Addr
279 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
280 const Elf32_Rel *reloc,
281 Elf32_Addr *reloc_addr, Elf32_Addr value)
283 return *reloc_addr = value;
286 /* Return the final value of a plt relocation. */
287 static inline Elf32_Addr
288 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
289 Elf32_Addr value)
291 return value;
294 #endif /* !dl_machine_h */
296 #ifdef RESOLVE
298 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
299 MAP is the object containing the reloc. */
301 static inline void
302 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
303 const Elf32_Sym *sym, const struct r_found_version *version,
304 Elf32_Addr *const reloc_addr)
306 if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE)
308 #ifndef RTLD_BOOTSTRAP
309 /* This is defined in rtld.c, but nowhere in the static libc.a;
310 make the reference weak so static programs can still link.
311 This declaration cannot be done when compiling rtld.c
312 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
313 common defn for _dl_rtld_map, which is incompatible with a
314 weak decl in the same file. */
315 weak_extern (_dl_rtld_map);
316 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
317 #endif
318 *reloc_addr += map->l_addr;
320 else if (ELF32_R_TYPE (reloc->r_info) != R_386_NONE)
322 #ifndef RTLD_BOOTSTRAP
323 const Elf32_Sym *const refsym = sym;
324 #endif
325 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
326 if (sym)
327 value += sym->st_value;
329 switch (ELF32_R_TYPE (reloc->r_info))
331 case R_386_GLOB_DAT:
332 case R_386_JMP_SLOT:
333 *reloc_addr = value;
334 break;
335 #ifndef RTLD_BOOTSTRAP
336 case R_386_32:
337 *reloc_addr += value;
338 break;
339 case R_386_PC32:
340 *reloc_addr += (value - (Elf32_Addr) reloc_addr);
341 break;
342 case R_386_COPY:
343 if (sym == NULL)
344 /* This can happen in trace mode if an object could not be
345 found. */
346 break;
347 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
348 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
349 && __builtin_expect (_dl_verbose, 0)))
351 const char *strtab;
353 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
354 _dl_error_printf ("\
355 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
356 _dl_argv[0] ?: "<program name unknown>",
357 strtab + refsym->st_name);
359 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
360 refsym->st_size));
361 break;
362 #endif
363 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
364 default:
365 /* We add these checks in the version to relocate ld.so only
366 if we are still debugging. */
367 _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 0);
368 break;
369 #endif
374 static inline void
375 elf_machine_lazy_rel (struct link_map *map,
376 Elf32_Addr l_addr, const Elf32_Rel *reloc)
378 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
379 /* Check for unexpected PLT reloc type. */
380 if (__builtin_expect (ELF32_R_TYPE (reloc->r_info), R_386_JMP_SLOT)
381 == R_386_JMP_SLOT)
382 *reloc_addr += l_addr;
383 else
384 _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 1);
387 #endif /* RESOLVE */