]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Machine-dependent ELF dynamic relocation inline functions. i386 version. |
7dea968e | 2 | Copyright (C) 1995, 1996, 1997, 1998 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. */ | |
0a54e401 | 47 | static inline Elf32_Addr __attribute__ ((unused)) |
47707456 | 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; | |
92f1da4d UD |
60 | asm ("leal _dl_start@GOTOFF(%%ebx), %0\n" |
61 | "subl _dl_start@GOT(%%ebx), %0" | |
62 | : "=r" (addr) : : "cc"); | |
d66e34cd RM |
63 | return addr; |
64 | } | |
d66e34cd | 65 | |
5ae9d168 | 66 | #ifndef PROF |
831372e7 UD |
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 | |
5ae9d168 UD |
69 | in registers. |
70 | ||
71 | We cannot use this scheme for profiling because the _mcount call | |
72 | destroys the passed register information. */ | |
831372e7 UD |
73 | static ElfW(Addr) fixup (struct link_map *l, ElfW(Word) reloc_offset) |
74 | __attribute__ ((regparm (2), unused)); | |
3996f34b UD |
75 | static ElfW(Addr) profile_fixup (struct link_map *l, ElfW(Word) reloc_offset, |
76 | ElfW(Addr) retaddr) | |
77 | __attribute__ ((regparm (3), unused)); | |
5ae9d168 | 78 | #endif |
831372e7 | 79 | |
d66e34cd RM |
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. */ | |
82 | ||
0a54e401 | 83 | static inline int __attribute__ ((unused)) |
3996f34b | 84 | elf_machine_runtime_setup (struct link_map *l, int lazy, int profile) |
d66e34cd | 85 | { |
a1a9d215 | 86 | Elf32_Addr *got; |
d66e34cd | 87 | extern void _dl_runtime_resolve (Elf32_Word); |
3996f34b | 88 | extern void _dl_runtime_profile (Elf32_Word); |
a1a9d215 | 89 | |
a2e1b046 RM |
90 | if (l->l_info[DT_JMPREL] && lazy) |
91 | { | |
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. */ | |
3996f34b UD |
98 | |
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. */ | |
105 | if (profile) | |
106 | { | |
107 | got[2] = (Elf32_Addr) &_dl_runtime_profile; | |
108 | /* Say that we really want profiling and the timers are started. */ | |
109 | _dl_profile_map = l; | |
110 | } | |
111 | else | |
112 | /* This function will get called to fix up the GOT entry indicated by | |
113 | the offset on the stack, and then jump to the resolved address. */ | |
114 | got[2] = (Elf32_Addr) &_dl_runtime_resolve; | |
a2e1b046 | 115 | } |
d66e34cd | 116 | |
0501d603 UD |
117 | return lazy; |
118 | } | |
831372e7 | 119 | |
0501d603 UD |
120 | /* This code is used in dl-runtime.c to call the `fixup' function |
121 | and then redirect to the address it returns. */ | |
5ae9d168 UD |
122 | #ifndef PROF |
123 | # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\ | |
650425ce | 124 | .text |
38334018 RM |
125 | .globl _dl_runtime_resolve |
126 | .type _dl_runtime_resolve, @function | |
f41c8091 | 127 | .align 16 |
38334018 | 128 | _dl_runtime_resolve: |
831372e7 UD |
129 | pushl %eax # Preserve registers otherwise clobbered. |
130 | pushl %ecx | |
131 | pushl %edx | |
132 | movl 16(%esp), %edx # Copy args pushed by PLT in register. Note | |
133 | movl 12(%esp), %eax # that `fixup' takes its parameters in regs. | |
134 | call fixup # Call resolver. | |
135 | popl %edx # Get register content back. | |
136 | popl %ecx | |
137 | xchgl %eax, (%esp) # Get %eax contents end store function address. | |
138 | ret $8 # Jump to function address. | |
139 | .size _dl_runtime_resolve, .-_dl_runtime_resolve | |
3996f34b UD |
140 | |
141 | .globl _dl_runtime_profile | |
142 | .type _dl_runtime_profile, @function | |
f41c8091 | 143 | .align 16 |
3996f34b UD |
144 | _dl_runtime_profile: |
145 | pushl %eax # Preserve registers otherwise clobbered. | |
146 | pushl %ecx | |
147 | pushl %edx | |
148 | movl 20(%esp), %ecx # Load return address | |
149 | movl 16(%esp), %edx # Copy args pushed by PLT in register. Note | |
150 | movl 12(%esp), %eax # that `fixup' takes its parameters in regs. | |
151 | call profile_fixup # Call resolver. | |
152 | popl %edx # Get register content back. | |
153 | popl %ecx | |
154 | xchgl %eax, (%esp) # Get %eax contents end store function address. | |
155 | ret $8 # Jump to function address. | |
156 | .size _dl_runtime_profile, .-_dl_runtime_profile | |
650425ce | 157 | .previous |
38334018 | 158 | "); |
5ae9d168 UD |
159 | #else |
160 | # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\ | |
650425ce | 161 | .text |
5ae9d168 | 162 | .globl _dl_runtime_resolve |
3996f34b | 163 | .globl _dl_runtime_profile |
5ae9d168 | 164 | .type _dl_runtime_resolve, @function |
3996f34b | 165 | .type _dl_runtime_profile, @function |
f41c8091 | 166 | .align 16 |
5ae9d168 | 167 | _dl_runtime_resolve: |
3996f34b | 168 | _dl_runtime_profile: |
5ae9d168 UD |
169 | pushl %eax # Preserve registers otherwise clobbered. |
170 | pushl %ecx | |
171 | pushl %edx | |
172 | movl 16(%esp), %edx # Push the arguments for `fixup' | |
173 | movl 12(%esp), %eax | |
174 | pushl %edx | |
175 | pushl %eax | |
176 | call fixup # Call resolver. | |
177 | popl %edx # Pop the parameters | |
178 | popl %ecx | |
179 | popl %edx # Get register content back. | |
180 | popl %ecx | |
181 | xchgl %eax, (%esp) # Get %eax contents end store function address. | |
182 | ret $8 # Jump to function address. | |
183 | .size _dl_runtime_resolve, .-_dl_runtime_resolve | |
3996f34b | 184 | .size _dl_runtime_profile, .-_dl_runtime_profile |
650425ce | 185 | .previous |
5ae9d168 UD |
186 | "); |
187 | #endif | |
d66e34cd | 188 | |
5bf62f2d RM |
189 | /* Mask identifying addresses reserved for the user program, |
190 | where the dynamic linker should not map anything. */ | |
191 | #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL | |
192 | ||
d66e34cd RM |
193 | /* Initial entry point code for the dynamic linker. |
194 | The C function `_dl_start' is the real entry point; | |
195 | its return value is the user program's entry point. */ | |
196 | ||
197 | #define RTLD_START asm ("\ | |
198 | .text\n\ | |
199 | .globl _start\n\ | |
421f82e5 RM |
200 | .globl _dl_start_user\n\ |
201 | _start:\n\ | |
8d6468d0 | 202 | pushl %esp\n\ |
421f82e5 | 203 | call _dl_start\n\ |
8d6468d0 | 204 | popl %ebx\n\ |
421f82e5 RM |
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. | |
a1a9d215 RM |
209 | call 0f\n\ |
210 | 0: popl %ebx\n\ | |
211 | addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\ | |
212 | # See if we were run as a command with the executable file\n\ | |
213 | # name as an extra leading argument.\n\ | |
24906b43 | 214 | movl _dl_skip_args@GOT(%ebx), %eax\n\ |
edf5b2d7 | 215 | movl (%eax), %eax\n\ |
24906b43 RM |
216 | # Pop the original argument count.\n\ |
217 | popl %ecx\n\ | |
218 | # Subtract _dl_skip_args from it.\n\ | |
219 | subl %eax, %ecx\n\ | |
220 | # Adjust the stack pointer to skip _dl_skip_args words.\n\ | |
221 | leal (%esp,%eax,4), %esp\n\ | |
222 | # Push back the modified argument count.\n\ | |
223 | pushl %ecx\n\ | |
ba79d61b RM |
224 | # Push _dl_default_scope[2] as argument in _dl_init_next call below.\n\ |
225 | movl _dl_default_scope@GOT(%ebx), %eax\n\ | |
226 | movl 8(%eax), %esi\n\ | |
f68b86cc | 227 | 0: pushl %esi\n\ |
d66e34cd RM |
228 | # Call _dl_init_next to return the address of an initializer\n\ |
229 | # function to run.\n\ | |
f68b86cc RM |
230 | call _dl_init_next@PLT\n\ |
231 | addl $4, %esp # Pop argument.\n\ | |
d66e34cd | 232 | # Check for zero return, when out of initializers.\n\ |
edf5b2d7 | 233 | testl %eax, %eax\n\ |
d66e34cd RM |
234 | jz 1f\n\ |
235 | # Call the shared object initializer function.\n\ | |
f68b86cc | 236 | # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\ |
d66e34cd RM |
237 | # and the return address pushed by this call;\n\ |
238 | # the initializer is called with the stack just\n\ | |
239 | # as it appears on entry, and it is free to move\n\ | |
240 | # the stack around, as long as it winds up jumping to\n\ | |
241 | # the return address on the top of the stack.\n\ | |
242 | call *%eax\n\ | |
243 | # Loop to call _dl_init_next for the next initializer.\n\ | |
244 | jmp 0b\n\ | |
39778c6c | 245 | 1: # Clear the startup flag.\n\ |
edf5b2d7 UD |
246 | movl _dl_starting_up@GOT(%ebx), %eax\n\ |
247 | movl $0, (%eax)\n\ | |
39778c6c | 248 | # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\ |
a1a9d215 | 249 | movl _dl_fini@GOT(%ebx), %edx\n\ |
421f82e5 RM |
250 | # Jump to the user's entry point.\n\ |
251 | jmp *%edi\n\ | |
92f1da4d | 252 | .previous\n\ |
d66e34cd | 253 | "); |
f51d1dfd | 254 | |
34b402e5 UD |
255 | /* Nonzero iff TYPE should not be allowed to resolve to one of |
256 | the main executable's symbols, as for a COPY reloc. */ | |
bc9f6000 UD |
257 | #define elf_machine_lookup_noexec_p(type) ((type) == R_386_COPY) |
258 | ||
259 | /* Nonzero iff TYPE describes relocation of a PLT entry, so | |
260 | PLT entries should not be allowed to define the value. */ | |
261 | #define elf_machine_lookup_noplt_p(type) ((type) == R_386_JMP_SLOT) | |
262 | ||
263 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ | |
a2b08ee5 | 264 | #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT |
f51d1dfd RM |
265 | |
266 | /* The i386 never uses Elf32_Rela relocations. */ | |
267 | #define ELF_MACHINE_NO_RELA 1 | |
268 | ||
0a54e401 UD |
269 | /* We define an initialization functions. This is called very early in |
270 | _dl_sysdep_start. */ | |
271 | #define DL_PLATFORM_INIT dl_platform_init () | |
272 | ||
273 | extern const char *_dl_platform; | |
274 | ||
275 | static inline void __attribute__ ((unused)) | |
276 | dl_platform_init (void) | |
277 | { | |
278 | if (_dl_platform == NULL) | |
279 | /* We default to i386 since all instructions understood by the i386 | |
280 | are also understood by later processors. */ | |
281 | _dl_platform = "i386"; | |
282 | else if (*_dl_platform == '\0') | |
283 | _dl_platform = NULL; | |
284 | } | |
285 | ||
a2b08ee5 UD |
286 | static inline void |
287 | elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc, | |
288 | Elf32_Addr *reloc_addr, Elf32_Addr value) | |
289 | { | |
290 | *reloc_addr = value; | |
291 | } | |
292 | ||
dfd2257a UD |
293 | /* Return the final value of a plt relocation. */ |
294 | static inline Elf32_Addr | |
295 | elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc, | |
296 | Elf32_Addr value) | |
297 | { | |
298 | return value; | |
299 | } | |
300 | ||
f51d1dfd RM |
301 | #endif /* !dl_machine_h */ |
302 | ||
303 | #ifdef RESOLVE | |
304 | ||
305 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). | |
306 | MAP is the object containing the reloc. */ | |
307 | ||
308 | static inline void | |
c84142e8 | 309 | elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc, |
3996f34b UD |
310 | const Elf32_Sym *sym, const struct r_found_version *version, |
311 | Elf32_Addr *const reloc_addr) | |
f51d1dfd | 312 | { |
bc9f6000 | 313 | if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE) |
f51d1dfd | 314 | { |
f51d1dfd RM |
315 | #ifndef RTLD_BOOTSTRAP |
316 | if (map != &_dl_rtld_map) /* Already done in rtld itself. */ | |
317 | #endif | |
318 | *reloc_addr += map->l_addr; | |
f51d1dfd | 319 | } |
714a562f | 320 | else if (ELF32_R_TYPE (reloc->r_info) != R_386_NONE) |
bc9f6000 | 321 | { |
bc9f6000 | 322 | const Elf32_Sym *const refsym = sym; |
bc9f6000 UD |
323 | Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info)); |
324 | if (sym) | |
325 | value += sym->st_value; | |
326 | ||
327 | switch (ELF32_R_TYPE (reloc->r_info)) | |
328 | { | |
329 | case R_386_COPY: | |
5107cf1d UD |
330 | if (sym == NULL) |
331 | /* This can happen in trace mode if an object could not be | |
332 | found. */ | |
333 | break; | |
cf29ffbe UD |
334 | if (sym->st_size > refsym->st_size |
335 | || (_dl_verbose && sym->st_size < refsym->st_size)) | |
1f07e617 UD |
336 | { |
337 | const char *strtab; | |
338 | ||
bd355af0 | 339 | strtab = ((const char *) map->l_addr |
1f07e617 | 340 | + map->l_info[DT_STRTAB]->d_un.d_ptr); |
60c96635 UD |
341 | _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", |
342 | ": Symbol `", strtab + refsym->st_name, | |
1f07e617 UD |
343 | "' has different size in shared object, " |
344 | "consider re-linking\n", NULL); | |
345 | } | |
346 | memcpy (reloc_addr, (void *) value, MIN (sym->st_size, | |
347 | refsym->st_size)); | |
bc9f6000 UD |
348 | break; |
349 | case R_386_GLOB_DAT: | |
350 | case R_386_JMP_SLOT: | |
351 | *reloc_addr = value; | |
352 | break; | |
353 | case R_386_32: | |
354 | { | |
355 | #ifndef RTLD_BOOTSTRAP | |
356 | /* This is defined in rtld.c, but nowhere in the static | |
357 | libc.a; make the reference weak so static programs can | |
358 | still link. This declaration cannot be done when | |
359 | compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because | |
360 | rtld.c contains the common defn for _dl_rtld_map, which | |
361 | is incompatible with a weak decl in the same file. */ | |
362 | weak_extern (_dl_rtld_map); | |
363 | if (map == &_dl_rtld_map) | |
364 | /* Undo the relocation done here during bootstrapping. | |
365 | Now we will relocate it anew, possibly using a | |
366 | binding found in the user program or a loaded library | |
367 | rather than the dynamic linker's built-in definitions | |
368 | used while loading those libraries. */ | |
369 | value -= map->l_addr + refsym->st_value; | |
370 | #endif | |
371 | *reloc_addr += value; | |
372 | break; | |
373 | } | |
374 | case R_386_PC32: | |
375 | *reloc_addr += (value - (Elf32_Addr) reloc_addr); | |
376 | break; | |
bc9f6000 UD |
377 | default: |
378 | assert (! "unexpected dynamic reloc type"); | |
379 | break; | |
380 | } | |
381 | } | |
f51d1dfd RM |
382 | } |
383 | ||
384 | static inline void | |
385 | elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc) | |
386 | { | |
387 | Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset); | |
388 | switch (ELF32_R_TYPE (reloc->r_info)) | |
389 | { | |
390 | case R_386_JMP_SLOT: | |
391 | *reloc_addr += map->l_addr; | |
392 | break; | |
393 | default: | |
394 | assert (! "unexpected PLT reloc type"); | |
395 | break; | |
396 | } | |
397 | } | |
398 | ||
399 | #endif /* RESOLVE */ |