Update.
[glibc.git] / sysdeps / i386 / dl-machine.h
blob7b3336588dbaba8e525a1c8a16a899d918790adf
1 /* Machine-dependent ELF dynamic relocation inline functions. i386 version.
2 Copyright (C) 1995, 1996, 1997 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 <assert.h>
27 /* Return nonzero iff E_MACHINE is compatible with the running host. */
28 static inline int __attribute__ ((unused))
29 elf_machine_matches_host (Elf32_Half e_machine)
31 switch (e_machine)
33 case EM_386:
34 case EM_486:
35 return 1;
36 default:
37 return 0;
42 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
43 first element of the GOT. This must be inlined in a function which
44 uses global data. */
45 static inline Elf32_Addr
46 elf_machine_dynamic (void)
48 register Elf32_Addr *got asm ("%ebx");
49 return *got;
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 Elf32_Addr addr;
58 asm (" call .Lhere\n"
59 ".Lhere: popl %0\n"
60 " subl $.Lhere, %0"
61 : "=r" (addr));
62 return addr;
64 /* The `subl' insn above will contain an R_386_32 relocation entry
65 intended to insert the run-time address of the label `.Lhere'.
66 This will be the first relocation in the text of the dynamic linker;
67 we skip it to avoid trying to modify read-only text in this early stage. */
68 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
69 ++(const Elf32_Rel *) (dynamic_info)[DT_REL]->d_un.d_ptr; \
70 (dynamic_info)[DT_RELSZ]->d_un.d_val -= sizeof (Elf32_Rel);
73 #ifndef PROF
74 /* We add a declaration of this function here so that in dl-runtime.c
75 the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters
76 in registers.
78 We cannot use this scheme for profiling because the _mcount call
79 destroys the passed register information. */
80 static ElfW(Addr) fixup (struct link_map *l, ElfW(Word) reloc_offset)
81 __attribute__ ((regparm (2), unused));
82 #endif
84 /* Set up the loaded object described by L so its unrelocated PLT
85 entries will jump to the on-demand fixup code in dl-runtime.c. */
87 static inline void __attribute__ ((unused))
88 elf_machine_runtime_setup (struct link_map *l, int lazy)
90 Elf32_Addr *got;
91 extern void _dl_runtime_resolve (Elf32_Word);
93 if (l->l_info[DT_JMPREL] && lazy)
95 /* The GOT entries for functions in the PLT have not yet been filled
96 in. Their initial contents will arrange when called to push an
97 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
98 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
99 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
100 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
101 /* This function will get called to fix up the GOT entry indicated by
102 the offset on the stack, and then jump to the resolved address. */
103 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
107 /* This code is used in dl-runtime.c to call the `fixup' function
108 and then redirect to the address it returns. */
109 #ifndef PROF
110 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
111 .globl _dl_runtime_resolve
112 .type _dl_runtime_resolve, @function
113 _dl_runtime_resolve:
114 pushl %eax # Preserve registers otherwise clobbered.
115 pushl %ecx
116 pushl %edx
117 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note
118 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.
119 call fixup # Call resolver.
120 popl %edx # Get register content back.
121 popl %ecx
122 xchgl %eax, (%esp) # Get %eax contents end store function address.
123 ret $8 # Jump to function address.
124 .size _dl_runtime_resolve, .-_dl_runtime_resolve
126 #else
127 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
128 .globl _dl_runtime_resolve
129 .type _dl_runtime_resolve, @function
130 _dl_runtime_resolve:
131 pushl %eax # Preserve registers otherwise clobbered.
132 pushl %ecx
133 pushl %edx
134 movl 16(%esp), %edx # Push the arguments for `fixup'
135 movl 12(%esp), %eax
136 pushl %edx
137 pushl %eax
138 call fixup # Call resolver.
139 popl %edx # Pop the parameters
140 popl %ecx
141 popl %edx # Get register content back.
142 popl %ecx
143 xchgl %eax, (%esp) # Get %eax contents end store function address.
144 ret $8 # Jump to function address.
145 .size _dl_runtime_resolve, .-_dl_runtime_resolve
147 #endif
148 /* The PLT uses Elf32_Rel relocs. */
149 #define elf_machine_relplt elf_machine_rel
152 /* Mask identifying addresses reserved for the user program,
153 where the dynamic linker should not map anything. */
154 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
158 /* Initial entry point code for the dynamic linker.
159 The C function `_dl_start' is the real entry point;
160 its return value is the user program's entry point. */
162 #define RTLD_START asm ("\
163 .text\n\
164 .globl _start\n\
165 .globl _dl_start_user\n\
166 _start:\n\
167 pushl %esp\n\
168 call _dl_start\n\
169 popl %ebx\n\
170 _dl_start_user:\n\
171 # Save the user entry point address in %edi.\n\
172 movl %eax, %edi\n\
173 # Point %ebx at the GOT.
174 call 0f\n\
175 0: popl %ebx\n\
176 addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\
177 # See if we were run as a command with the executable file\n\
178 # name as an extra leading argument.\n\
179 movl _dl_skip_args@GOT(%ebx), %eax\n\
180 movl (%eax), %eax\n\
181 # Pop the original argument count.\n\
182 popl %ecx\n\
183 # Subtract _dl_skip_args from it.\n\
184 subl %eax, %ecx\n\
185 # Adjust the stack pointer to skip _dl_skip_args words.\n\
186 leal (%esp,%eax,4), %esp\n\
187 # Push back the modified argument count.\n\
188 pushl %ecx\n\
189 # Push _dl_default_scope[2] as argument in _dl_init_next call below.\n\
190 movl _dl_default_scope@GOT(%ebx), %eax\n\
191 movl 8(%eax), %esi\n\
192 0: pushl %esi\n\
193 # Call _dl_init_next to return the address of an initializer\n\
194 # function to run.\n\
195 call _dl_init_next@PLT\n\
196 addl $4, %esp # Pop argument.\n\
197 # Check for zero return, when out of initializers.\n\
198 testl %eax, %eax\n\
199 jz 1f\n\
200 # Call the shared object initializer function.\n\
201 # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\
202 # and the return address pushed by this call;\n\
203 # the initializer is called with the stack just\n\
204 # as it appears on entry, and it is free to move\n\
205 # the stack around, as long as it winds up jumping to\n\
206 # the return address on the top of the stack.\n\
207 call *%eax\n\
208 # Loop to call _dl_init_next for the next initializer.\n\
209 jmp 0b\n\
210 1: # Clear the startup flag.\n\
211 movl _dl_starting_up@GOT(%ebx), %eax\n\
212 movl $0, (%eax)\n\
213 # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
214 movl _dl_fini@GOT(%ebx), %edx\n\
215 # Jump to the user's entry point.\n\
216 jmp *%edi\n\
219 /* Nonzero iff TYPE should not be allowed to resolve to one of
220 the main executable's symbols, as for a COPY reloc. */
221 #define elf_machine_lookup_noexec_p(type) ((type) == R_386_COPY)
223 /* Nonzero iff TYPE describes relocation of a PLT entry, so
224 PLT entries should not be allowed to define the value. */
225 #define elf_machine_lookup_noplt_p(type) ((type) == R_386_JMP_SLOT)
227 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
228 #define ELF_MACHINE_RELOC_NOPLT R_386_JMP_SLOT
230 /* The i386 never uses Elf32_Rela relocations. */
231 #define ELF_MACHINE_NO_RELA 1
233 #endif /* !dl_machine_h */
235 #ifdef RESOLVE
237 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
238 MAP is the object containing the reloc. */
240 static inline void
241 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
242 const Elf32_Sym *sym, const struct r_found_version *version)
244 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
246 if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE)
248 #ifndef RTLD_BOOTSTRAP
249 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
250 #endif
251 *reloc_addr += map->l_addr;
253 else
255 #ifndef RTLD_BOOTSTRAP
256 const Elf32_Sym *const refsym = sym;
257 #endif
258 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
259 if (sym)
260 value += sym->st_value;
262 switch (ELF32_R_TYPE (reloc->r_info))
264 case R_386_COPY:
265 memcpy (reloc_addr, (void *) value, sym->st_size);
266 break;
267 case R_386_GLOB_DAT:
268 case R_386_JMP_SLOT:
269 *reloc_addr = value;
270 break;
271 case R_386_32:
273 #ifndef RTLD_BOOTSTRAP
274 /* This is defined in rtld.c, but nowhere in the static
275 libc.a; make the reference weak so static programs can
276 still link. This declaration cannot be done when
277 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
278 rtld.c contains the common defn for _dl_rtld_map, which
279 is incompatible with a weak decl in the same file. */
280 weak_extern (_dl_rtld_map);
281 if (map == &_dl_rtld_map)
282 /* Undo the relocation done here during bootstrapping.
283 Now we will relocate it anew, possibly using a
284 binding found in the user program or a loaded library
285 rather than the dynamic linker's built-in definitions
286 used while loading those libraries. */
287 value -= map->l_addr + refsym->st_value;
288 #endif
289 *reloc_addr += value;
290 break;
292 case R_386_PC32:
293 *reloc_addr += (value - (Elf32_Addr) reloc_addr);
294 break;
295 case R_386_NONE: /* Alright, Wilbur. */
296 break;
297 default:
298 assert (! "unexpected dynamic reloc type");
299 break;
304 static inline void
305 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc)
307 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
308 switch (ELF32_R_TYPE (reloc->r_info))
310 case R_386_JMP_SLOT:
311 *reloc_addr += map->l_addr;
312 break;
313 default:
314 assert (! "unexpected PLT reloc type");
315 break;
319 #endif /* RESOLVE */