]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Machine-dependent ELF dynamic relocation inline functions. i386 version. |
831372e7 | 2 | Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. |
47707456 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
47707456 UD |
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. | |
d66e34cd | 9 | |
47707456 UD |
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. | |
d66e34cd | 14 | |
47707456 | 15 | You should have received a copy of the GNU Library General Public |
01c901a5 UD |
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. */ | |
d66e34cd | 19 | |
f51d1dfd RM |
20 | #ifndef dl_machine_h |
21 | #define dl_machine_h | |
22 | ||
d66e34cd RM |
23 | #define ELF_MACHINE_NAME "i386" |
24 | ||
1f07e617 UD |
25 | #include <sys/param.h> |
26 | ||
d66e34cd | 27 | #include <assert.h> |
d66e34cd RM |
28 | |
29 | /* Return nonzero iff E_MACHINE is compatible with the running host. */ | |
e75154a6 | 30 | static inline int __attribute__ ((unused)) |
d66e34cd RM |
31 | elf_machine_matches_host (Elf32_Half e_machine) |
32 | { | |
33 | switch (e_machine) | |
34 | { | |
35 | case EM_386: | |
36 | case EM_486: | |
37 | return 1; | |
38 | default: | |
39 | return 0; | |
40 | } | |
41 | } | |
42 | ||
43 | ||
47707456 UD |
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 | |
46 | uses global data. */ | |
47 | static inline Elf32_Addr | |
48 | elf_machine_dynamic (void) | |
d66e34cd RM |
49 | { |
50 | register Elf32_Addr *got asm ("%ebx"); | |
47707456 | 51 | return *got; |
d66e34cd RM |
52 | } |
53 | ||
54 | ||
55 | /* Return the run-time load address of the shared object. */ | |
e75154a6 | 56 | static inline Elf32_Addr __attribute__ ((unused)) |
d66e34cd RM |
57 | elf_machine_load_address (void) |
58 | { | |
59 | Elf32_Addr addr; | |
779ae82e UD |
60 | asm (" call .Lhere\n" |
61 | ".Lhere: popl %0\n" | |
62 | " subl $.Lhere, %0" | |
d66e34cd RM |
63 | : "=r" (addr)); |
64 | return addr; | |
65 | } | |
66 | /* The `subl' insn above will contain an R_386_32 relocation entry | |
779ae82e | 67 | intended to insert the run-time address of the label `.Lhere'. |
d66e34cd RM |
68 | This will be the first relocation in the text of the dynamic linker; |
69 | we skip it to avoid trying to modify read-only text in this early stage. */ | |
70 | #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \ | |
44c8d1a2 | 71 | ++(const Elf32_Rel *) (dynamic_info)[DT_REL]->d_un.d_ptr; \ |
a993273c | 72 | (dynamic_info)[DT_RELSZ]->d_un.d_val -= sizeof (Elf32_Rel); |
d66e34cd | 73 | |
d66e34cd | 74 | |
5ae9d168 | 75 | #ifndef PROF |
831372e7 UD |
76 | /* We add a declaration of this function here so that in dl-runtime.c |
77 | the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters | |
5ae9d168 UD |
78 | in registers. |
79 | ||
80 | We cannot use this scheme for profiling because the _mcount call | |
81 | destroys the passed register information. */ | |
831372e7 UD |
82 | static ElfW(Addr) fixup (struct link_map *l, ElfW(Word) reloc_offset) |
83 | __attribute__ ((regparm (2), unused)); | |
5ae9d168 | 84 | #endif |
831372e7 | 85 | |
d66e34cd RM |
86 | /* Set up the loaded object described by L so its unrelocated PLT |
87 | entries will jump to the on-demand fixup code in dl-runtime.c. */ | |
88 | ||
0501d603 | 89 | static inline int |
a2e1b046 | 90 | elf_machine_runtime_setup (struct link_map *l, int lazy) |
d66e34cd | 91 | { |
a1a9d215 | 92 | Elf32_Addr *got; |
d66e34cd | 93 | extern void _dl_runtime_resolve (Elf32_Word); |
a1a9d215 | 94 | |
a2e1b046 RM |
95 | if (l->l_info[DT_JMPREL] && lazy) |
96 | { | |
97 | /* The GOT entries for functions in the PLT have not yet been filled | |
98 | in. Their initial contents will arrange when called to push an | |
99 | offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1], | |
100 | and then jump to _GLOBAL_OFFSET_TABLE[2]. */ | |
101 | got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr); | |
102 | got[1] = (Elf32_Addr) l; /* Identify this shared object. */ | |
103 | /* This function will get called to fix up the GOT entry indicated by | |
104 | the offset on the stack, and then jump to the resolved address. */ | |
105 | got[2] = (Elf32_Addr) &_dl_runtime_resolve; | |
106 | } | |
d66e34cd | 107 | |
0501d603 UD |
108 | return lazy; |
109 | } | |
831372e7 | 110 | |
0501d603 UD |
111 | /* This code is used in dl-runtime.c to call the `fixup' function |
112 | and then redirect to the address it returns. */ | |
5ae9d168 UD |
113 | #ifndef PROF |
114 | # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\ | |
38334018 RM |
115 | .globl _dl_runtime_resolve |
116 | .type _dl_runtime_resolve, @function | |
117 | _dl_runtime_resolve: | |
831372e7 UD |
118 | pushl %eax # Preserve registers otherwise clobbered. |
119 | pushl %ecx | |
120 | pushl %edx | |
121 | movl 16(%esp), %edx # Copy args pushed by PLT in register. Note | |
122 | movl 12(%esp), %eax # that `fixup' takes its parameters in regs. | |
123 | call fixup # Call resolver. | |
124 | popl %edx # Get register content back. | |
125 | popl %ecx | |
126 | xchgl %eax, (%esp) # Get %eax contents end store function address. | |
127 | ret $8 # Jump to function address. | |
128 | .size _dl_runtime_resolve, .-_dl_runtime_resolve | |
38334018 | 129 | "); |
5ae9d168 UD |
130 | #else |
131 | # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\ | |
132 | .globl _dl_runtime_resolve | |
133 | .type _dl_runtime_resolve, @function | |
134 | _dl_runtime_resolve: | |
135 | pushl %eax # Preserve registers otherwise clobbered. | |
136 | pushl %ecx | |
137 | pushl %edx | |
138 | movl 16(%esp), %edx # Push the arguments for `fixup' | |
139 | movl 12(%esp), %eax | |
140 | pushl %edx | |
141 | pushl %eax | |
142 | call fixup # Call resolver. | |
143 | popl %edx # Pop the parameters | |
144 | popl %ecx | |
145 | popl %edx # Get register content back. | |
146 | popl %ecx | |
147 | xchgl %eax, (%esp) # Get %eax contents end store function address. | |
148 | ret $8 # Jump to function address. | |
149 | .size _dl_runtime_resolve, .-_dl_runtime_resolve | |
150 | "); | |
151 | #endif | |
38334018 RM |
152 | /* The PLT uses Elf32_Rel relocs. */ |
153 | #define elf_machine_relplt elf_machine_rel | |
d66e34cd | 154 | |
5bf62f2d RM |
155 | /* Mask identifying addresses reserved for the user program, |
156 | where the dynamic linker should not map anything. */ | |
157 | #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL | |
158 | ||
159 | ||
160 | ||
d66e34cd RM |
161 | /* Initial entry point code for the dynamic linker. |
162 | The C function `_dl_start' is the real entry point; | |
163 | its return value is the user program's entry point. */ | |
164 | ||
165 | #define RTLD_START asm ("\ | |
166 | .text\n\ | |
167 | .globl _start\n\ | |
421f82e5 RM |
168 | .globl _dl_start_user\n\ |
169 | _start:\n\ | |
8d6468d0 | 170 | pushl %esp\n\ |
421f82e5 | 171 | call _dl_start\n\ |
8d6468d0 | 172 | popl %ebx\n\ |
421f82e5 RM |
173 | _dl_start_user:\n\ |
174 | # Save the user entry point address in %edi.\n\ | |
175 | movl %eax, %edi\n\ | |
176 | # Point %ebx at the GOT. | |
a1a9d215 RM |
177 | call 0f\n\ |
178 | 0: popl %ebx\n\ | |
179 | addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\ | |
180 | # See if we were run as a command with the executable file\n\ | |
181 | # name as an extra leading argument.\n\ | |
24906b43 | 182 | movl _dl_skip_args@GOT(%ebx), %eax\n\ |
edf5b2d7 | 183 | movl (%eax), %eax\n\ |
24906b43 RM |
184 | # Pop the original argument count.\n\ |
185 | popl %ecx\n\ | |
186 | # Subtract _dl_skip_args from it.\n\ | |
187 | subl %eax, %ecx\n\ | |
188 | # Adjust the stack pointer to skip _dl_skip_args words.\n\ | |
189 | leal (%esp,%eax,4), %esp\n\ | |
190 | # Push back the modified argument count.\n\ | |
191 | pushl %ecx\n\ | |
ba79d61b RM |
192 | # Push _dl_default_scope[2] as argument in _dl_init_next call below.\n\ |
193 | movl _dl_default_scope@GOT(%ebx), %eax\n\ | |
194 | movl 8(%eax), %esi\n\ | |
f68b86cc | 195 | 0: pushl %esi\n\ |
d66e34cd RM |
196 | # Call _dl_init_next to return the address of an initializer\n\ |
197 | # function to run.\n\ | |
f68b86cc RM |
198 | call _dl_init_next@PLT\n\ |
199 | addl $4, %esp # Pop argument.\n\ | |
d66e34cd | 200 | # Check for zero return, when out of initializers.\n\ |
edf5b2d7 | 201 | testl %eax, %eax\n\ |
d66e34cd RM |
202 | jz 1f\n\ |
203 | # Call the shared object initializer function.\n\ | |
f68b86cc | 204 | # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\ |
d66e34cd RM |
205 | # and the return address pushed by this call;\n\ |
206 | # the initializer is called with the stack just\n\ | |
207 | # as it appears on entry, and it is free to move\n\ | |
208 | # the stack around, as long as it winds up jumping to\n\ | |
209 | # the return address on the top of the stack.\n\ | |
210 | call *%eax\n\ | |
211 | # Loop to call _dl_init_next for the next initializer.\n\ | |
212 | jmp 0b\n\ | |
39778c6c | 213 | 1: # Clear the startup flag.\n\ |
edf5b2d7 UD |
214 | movl _dl_starting_up@GOT(%ebx), %eax\n\ |
215 | movl $0, (%eax)\n\ | |
39778c6c | 216 | # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\ |
a1a9d215 | 217 | movl _dl_fini@GOT(%ebx), %edx\n\ |
421f82e5 RM |
218 | # Jump to the user's entry point.\n\ |
219 | jmp *%edi\n\ | |
d66e34cd | 220 | "); |
f51d1dfd | 221 | |
34b402e5 UD |
222 | /* Nonzero iff TYPE should not be allowed to resolve to one of |
223 | the main executable's symbols, as for a COPY reloc. */ | |
bc9f6000 UD |
224 | #define elf_machine_lookup_noexec_p(type) ((type) == R_386_COPY) |
225 | ||
226 | /* Nonzero iff TYPE describes relocation of a PLT entry, so | |
227 | PLT entries should not be allowed to define the value. */ | |
228 | #define elf_machine_lookup_noplt_p(type) ((type) == R_386_JMP_SLOT) | |
229 | ||
230 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ | |
231 | #define ELF_MACHINE_RELOC_NOPLT R_386_JMP_SLOT | |
f51d1dfd RM |
232 | |
233 | /* The i386 never uses Elf32_Rela relocations. */ | |
234 | #define ELF_MACHINE_NO_RELA 1 | |
235 | ||
236 | #endif /* !dl_machine_h */ | |
237 | ||
238 | #ifdef RESOLVE | |
239 | ||
f21acc89 UD |
240 | extern char **_dl_argv; |
241 | ||
f51d1dfd RM |
242 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). |
243 | MAP is the object containing the reloc. */ | |
244 | ||
245 | static inline void | |
c84142e8 | 246 | elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc, |
1fb05e3d | 247 | const Elf32_Sym *sym, const struct r_found_version *version) |
f51d1dfd RM |
248 | { |
249 | Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset); | |
f51d1dfd | 250 | |
bc9f6000 | 251 | if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE) |
f51d1dfd | 252 | { |
f51d1dfd RM |
253 | #ifndef RTLD_BOOTSTRAP |
254 | if (map != &_dl_rtld_map) /* Already done in rtld itself. */ | |
255 | #endif | |
256 | *reloc_addr += map->l_addr; | |
f51d1dfd | 257 | } |
714a562f | 258 | else if (ELF32_R_TYPE (reloc->r_info) != R_386_NONE) |
bc9f6000 | 259 | { |
bc9f6000 | 260 | const Elf32_Sym *const refsym = sym; |
bc9f6000 UD |
261 | Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info)); |
262 | if (sym) | |
263 | value += sym->st_value; | |
264 | ||
265 | switch (ELF32_R_TYPE (reloc->r_info)) | |
266 | { | |
267 | case R_386_COPY: | |
5107cf1d UD |
268 | if (sym == NULL) |
269 | /* This can happen in trace mode if an object could not be | |
270 | found. */ | |
271 | break; | |
cf29ffbe UD |
272 | if (sym->st_size > refsym->st_size |
273 | || (_dl_verbose && sym->st_size < refsym->st_size)) | |
1f07e617 UD |
274 | { |
275 | const char *strtab; | |
276 | ||
277 | strtab = ((void *) map->l_addr | |
278 | + map->l_info[DT_STRTAB]->d_un.d_ptr); | |
60c96635 UD |
279 | _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", |
280 | ": Symbol `", strtab + refsym->st_name, | |
1f07e617 UD |
281 | "' has different size in shared object, " |
282 | "consider re-linking\n", NULL); | |
283 | } | |
284 | memcpy (reloc_addr, (void *) value, MIN (sym->st_size, | |
285 | refsym->st_size)); | |
bc9f6000 UD |
286 | break; |
287 | case R_386_GLOB_DAT: | |
288 | case R_386_JMP_SLOT: | |
289 | *reloc_addr = value; | |
290 | break; | |
291 | case R_386_32: | |
292 | { | |
293 | #ifndef RTLD_BOOTSTRAP | |
294 | /* This is defined in rtld.c, but nowhere in the static | |
295 | libc.a; make the reference weak so static programs can | |
296 | still link. This declaration cannot be done when | |
297 | compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because | |
298 | rtld.c contains the common defn for _dl_rtld_map, which | |
299 | is incompatible with a weak decl in the same file. */ | |
300 | weak_extern (_dl_rtld_map); | |
301 | if (map == &_dl_rtld_map) | |
302 | /* Undo the relocation done here during bootstrapping. | |
303 | Now we will relocate it anew, possibly using a | |
304 | binding found in the user program or a loaded library | |
305 | rather than the dynamic linker's built-in definitions | |
306 | used while loading those libraries. */ | |
307 | value -= map->l_addr + refsym->st_value; | |
308 | #endif | |
309 | *reloc_addr += value; | |
310 | break; | |
311 | } | |
312 | case R_386_PC32: | |
313 | *reloc_addr += (value - (Elf32_Addr) reloc_addr); | |
314 | break; | |
bc9f6000 UD |
315 | default: |
316 | assert (! "unexpected dynamic reloc type"); | |
317 | break; | |
318 | } | |
319 | } | |
f51d1dfd RM |
320 | } |
321 | ||
322 | static inline void | |
323 | elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc) | |
324 | { | |
325 | Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset); | |
326 | switch (ELF32_R_TYPE (reloc->r_info)) | |
327 | { | |
328 | case R_386_JMP_SLOT: | |
329 | *reloc_addr += map->l_addr; | |
330 | break; | |
331 | default: | |
332 | assert (! "unexpected PLT reloc type"); | |
333 | break; | |
334 | } | |
335 | } | |
336 | ||
337 | #endif /* RESOLVE */ |