Update to 2.1.x development version
[glibc.git] / sysdeps / i386 / dl-machine.h
blob570e0d906634fd124bc4438ccf287371442e15d5
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 here\n"
59 "here: popl %0\n"
60 " subl $here, %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 `here'.
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 /* We add a declaration of this function here so that in dl-runtime.c
74 the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters
75 in registers. */
76 static ElfW(Addr) fixup (struct link_map *l, ElfW(Word) reloc_offset)
77 __attribute__ ((regparm (2), unused));
79 /* Set up the loaded object described by L so its unrelocated PLT
80 entries will jump to the on-demand fixup code in dl-runtime.c. */
82 static inline void __attribute__ ((unused))
83 elf_machine_runtime_setup (struct link_map *l, int lazy)
85 Elf32_Addr *got;
86 extern void _dl_runtime_resolve (Elf32_Word);
88 if (l->l_info[DT_JMPREL] && lazy)
90 /* The GOT entries for functions in the PLT have not yet been filled
91 in. Their initial contents will arrange when called to push an
92 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
93 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
94 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
95 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
96 /* This function will get called to fix up the GOT entry indicated by
97 the offset on the stack, and then jump to the resolved address. */
98 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
102 /* This code is used in dl-runtime.c to call the `fixup' function
103 and then redirect to the address it returns. */
104 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
105 .globl _dl_runtime_resolve
106 .type _dl_runtime_resolve, @function
107 _dl_runtime_resolve:
108 pushl %eax # Preserve registers otherwise clobbered.
109 pushl %ecx
110 pushl %edx
111 movl 16(%esp), %edx # Copy args pushed by PLT in register. Note
112 movl 12(%esp), %eax # that `fixup' takes its parameters in regs.
113 call fixup # Call resolver.
114 popl %edx # Get register content back.
115 popl %ecx
116 xchgl %eax, (%esp) # Get %eax contents end store function address.
117 ret $8 # Jump to function address.
118 .size _dl_runtime_resolve, .-_dl_runtime_resolve
120 /* The PLT uses Elf32_Rel relocs. */
121 #define elf_machine_relplt elf_machine_rel
124 /* Mask identifying addresses reserved for the user program,
125 where the dynamic linker should not map anything. */
126 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
130 /* Initial entry point code for the dynamic linker.
131 The C function `_dl_start' is the real entry point;
132 its return value is the user program's entry point. */
134 #define RTLD_START asm ("\
135 .text\n\
136 .globl _start\n\
137 .globl _dl_start_user\n\
138 _start:\n\
139 pushl %esp\n\
140 call _dl_start\n\
141 popl %ebx\n\
142 _dl_start_user:\n\
143 # Save the user entry point address in %edi.\n\
144 movl %eax, %edi\n\
145 # Point %ebx at the GOT.
146 call 0f\n\
147 0: popl %ebx\n\
148 addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\
149 # See if we were run as a command with the executable file\n\
150 # name as an extra leading argument.\n\
151 movl _dl_skip_args@GOT(%ebx), %eax\n\
152 movl (%eax), %eax\n\
153 # Pop the original argument count.\n\
154 popl %ecx\n\
155 # Subtract _dl_skip_args from it.\n\
156 subl %eax, %ecx\n\
157 # Adjust the stack pointer to skip _dl_skip_args words.\n\
158 leal (%esp,%eax,4), %esp\n\
159 # Push back the modified argument count.\n\
160 pushl %ecx\n\
161 # Push _dl_default_scope[2] as argument in _dl_init_next call below.\n\
162 movl _dl_default_scope@GOT(%ebx), %eax\n\
163 movl 8(%eax), %esi\n\
164 0: pushl %esi\n\
165 # Call _dl_init_next to return the address of an initializer\n\
166 # function to run.\n\
167 call _dl_init_next@PLT\n\
168 addl $4, %esp # Pop argument.\n\
169 # Check for zero return, when out of initializers.\n\
170 testl %eax, %eax\n\
171 jz 1f\n\
172 # Call the shared object initializer function.\n\
173 # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\
174 # and the return address pushed by this call;\n\
175 # the initializer is called with the stack just\n\
176 # as it appears on entry, and it is free to move\n\
177 # the stack around, as long as it winds up jumping to\n\
178 # the return address on the top of the stack.\n\
179 call *%eax\n\
180 # Loop to call _dl_init_next for the next initializer.\n\
181 jmp 0b\n\
182 1: # Clear the startup flag.\n\
183 movl _dl_starting_up@GOT(%ebx), %eax\n\
184 movl $0, (%eax)\n\
185 # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
186 movl _dl_fini@GOT(%ebx), %edx\n\
187 # Jump to the user's entry point.\n\
188 jmp *%edi\n\
191 /* Nonzero iff TYPE describes relocation of a PLT entry, so
192 PLT entries should not be allowed to define the value. */
193 #define elf_machine_pltrel_p(type) ((type) == R_386_JMP_SLOT)
195 /* The i386 never uses Elf32_Rela relocations. */
196 #define ELF_MACHINE_NO_RELA 1
198 #endif /* !dl_machine_h */
200 #ifdef RESOLVE
202 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
203 MAP is the object containing the reloc. */
205 static inline void
206 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
207 const Elf32_Sym *sym, const hash_name_pair *version)
209 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
210 Elf32_Addr loadbase;
212 switch (ELF32_R_TYPE (reloc->r_info))
214 case R_386_COPY:
215 loadbase = RESOLVE (&sym, version, DL_LOOKUP_NOEXEC);
216 memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
217 break;
218 case R_386_GLOB_DAT:
219 loadbase = RESOLVE (&sym, version, 0);
220 *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
221 break;
222 case R_386_JMP_SLOT:
223 loadbase = RESOLVE (&sym, version, DL_LOOKUP_NOPLT);
224 *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
225 break;
226 case R_386_32:
228 Elf32_Addr undo = 0;
229 #ifndef RTLD_BOOTSTRAP
230 /* This is defined in rtld.c, but nowhere in the static libc.a;
231 make the reference weak so static programs can still link. This
232 declaration cannot be done when compiling rtld.c (i.e. #ifdef
233 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
234 _dl_rtld_map, which is incompatible with a weak decl in the same
235 file. */
236 weak_extern (_dl_rtld_map);
237 if (map == &_dl_rtld_map)
238 /* Undo the relocation done here during bootstrapping. Now we will
239 relocate it anew, possibly using a binding found in the user
240 program or a loaded library rather than the dynamic linker's
241 built-in definitions used while loading those libraries. */
242 undo = map->l_addr + sym->st_value;
243 #endif
244 loadbase = RESOLVE (&sym, version, 0);
245 *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
246 break;
248 case R_386_RELATIVE:
249 #ifndef RTLD_BOOTSTRAP
250 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
251 #endif
252 *reloc_addr += map->l_addr;
253 break;
254 case R_386_PC32:
255 loadbase = RESOLVE (&sym, version, 0);
256 *reloc_addr += ((sym ? (loadbase + sym->st_value) : 0) -
257 (Elf32_Addr) reloc_addr);
258 break;
259 case R_386_NONE: /* Alright, Wilbur. */
260 break;
261 default:
262 assert (! "unexpected dynamic reloc type");
263 break;
268 static inline void
269 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc)
271 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
272 switch (ELF32_R_TYPE (reloc->r_info))
274 case R_386_JMP_SLOT:
275 *reloc_addr += map->l_addr;
276 break;
277 default:
278 assert (! "unexpected PLT reloc type");
279 break;
283 #endif /* RESOLVE */