]>
Commit | Line | Data |
---|---|---|
1 | /* Machine-dependent ELF dynamic relocation inline functions. i386 version. | |
2 | Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
19 | ||
20 | #ifndef dl_machine_h | |
21 | #define dl_machine_h | |
22 | ||
23 | #define ELF_MACHINE_NAME "i386" | |
24 | ||
25 | #include <sys/param.h> | |
26 | ||
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) | |
30 | { | |
31 | switch (e_machine) | |
32 | { | |
33 | case EM_386: | |
34 | return 1; | |
35 | default: | |
36 | return 0; | |
37 | } | |
38 | } | |
39 | ||
40 | ||
41 | /* Return the link-time address of _DYNAMIC. Conveniently, this is the | |
42 | first element of the GOT. This must be inlined in a function which | |
43 | uses global data. */ | |
44 | static inline Elf32_Addr __attribute__ ((unused)) | |
45 | elf_machine_dynamic (void) | |
46 | { | |
47 | register Elf32_Addr *got asm ("%ebx"); | |
48 | return *got; | |
49 | } | |
50 | ||
51 | ||
52 | /* Return the run-time load address of the shared object. */ | |
53 | static inline Elf32_Addr __attribute__ ((unused)) | |
54 | elf_machine_load_address (void) | |
55 | { | |
56 | Elf32_Addr addr; | |
57 | asm ("leal _dl_start@GOTOFF(%%ebx), %0\n" | |
58 | "subl _dl_start@GOT(%%ebx), %0" | |
59 | : "=r" (addr) : : "cc"); | |
60 | return addr; | |
61 | } | |
62 | ||
63 | #ifndef PROF | |
64 | /* We add a declaration of this function here so that in dl-runtime.c | |
65 | the ELF_MACHINE_RUNTIME_TRAMPOLINE macro really can pass the parameters | |
66 | in registers. | |
67 | ||
68 | We cannot use this scheme for profiling because the _mcount call | |
69 | destroys the passed register information. */ | |
70 | static ElfW(Addr) fixup (struct link_map *l, ElfW(Word) reloc_offset) | |
71 | __attribute__ ((regparm (2), unused)); | |
72 | static ElfW(Addr) profile_fixup (struct link_map *l, ElfW(Word) reloc_offset, | |
73 | ElfW(Addr) retaddr) | |
74 | __attribute__ ((regparm (3), unused)); | |
75 | #endif | |
76 | ||
77 | /* Set up the loaded object described by L so its unrelocated PLT | |
78 | entries will jump to the on-demand fixup code in dl-runtime.c. */ | |
79 | ||
80 | static inline int __attribute__ ((unused)) | |
81 | elf_machine_runtime_setup (struct link_map *l, int lazy, int profile) | |
82 | { | |
83 | Elf32_Addr *got; | |
84 | extern void _dl_runtime_resolve (Elf32_Word); | |
85 | extern void _dl_runtime_profile (Elf32_Word); | |
86 | ||
87 | if (l->l_info[DT_JMPREL] && lazy) | |
88 | { | |
89 | /* The GOT entries for functions in the PLT have not yet been filled | |
90 | in. Their initial contents will arrange when called to push an | |
91 | offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1], | |
92 | and then jump to _GLOBAL_OFFSET_TABLE[2]. */ | |
93 | got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]); | |
94 | got[1] = (Elf32_Addr) l; /* Identify this shared object. */ | |
95 | ||
96 | /* The got[2] entry contains the address of a function which gets | |
97 | called to get the address of a so far unresolved function and | |
98 | jump to it. The profiling extension of the dynamic linker allows | |
99 | to intercept the calls to collect information. In this case we | |
100 | don't store the address in the GOT so that all future calls also | |
101 | end in this function. */ | |
102 | if (profile) | |
103 | { | |
104 | got[2] = (Elf32_Addr) &_dl_runtime_profile; | |
105 | ||
106 | if (_dl_name_match_p (_dl_profile, l)) | |
107 | /* This is the object we are looking for. Say that we really | |
108 | 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; | |
115 | } | |
116 | ||
117 | return lazy; | |
118 | } | |
119 | ||
120 | /* This code is used in dl-runtime.c to call the `fixup' function | |
121 | and then redirect to the address it returns. */ | |
122 | #ifndef PROF | |
123 | # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\ | |
124 | .text | |
125 | .globl _dl_runtime_resolve | |
126 | .type _dl_runtime_resolve, @function | |
127 | .align 16 | |
128 | _dl_runtime_resolve: | |
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 | |
140 | ||
141 | .globl _dl_runtime_profile | |
142 | .type _dl_runtime_profile, @function | |
143 | .align 16 | |
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 | |
157 | .previous | |
158 | "); | |
159 | #else | |
160 | # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\ | |
161 | .text | |
162 | .globl _dl_runtime_resolve | |
163 | .globl _dl_runtime_profile | |
164 | .type _dl_runtime_resolve, @function | |
165 | .type _dl_runtime_profile, @function | |
166 | .align 16 | |
167 | _dl_runtime_resolve: | |
168 | _dl_runtime_profile: | |
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 | |
184 | .size _dl_runtime_profile, .-_dl_runtime_profile | |
185 | .previous | |
186 | "); | |
187 | #endif | |
188 | ||
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 | ||
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 | .align 16\n\ | |
200 | 0: movl (%esp), %ebx\n\ | |
201 | ret\n\ | |
202 | .align 16\n\ | |
203 | .globl _start\n\ | |
204 | .globl _dl_start_user\n\ | |
205 | _start:\n\ | |
206 | pushl %esp\n\ | |
207 | call _dl_start\n\ | |
208 | popl %ebx\n\ | |
209 | _dl_start_user:\n\ | |
210 | # Save the user entry point address in %edi.\n\ | |
211 | movl %eax, %edi\n\ | |
212 | # Point %ebx at the GOT. | |
213 | call 0b\n\ | |
214 | addl $_GLOBAL_OFFSET_TABLE_, %ebx\n\ | |
215 | # Store the highest stack address\n\ | |
216 | movl __libc_stack_end@GOT(%ebx), %eax\n\ | |
217 | movl %esp, (%eax)\n\ | |
218 | # See if we were run as a command with the executable file\n\ | |
219 | # name as an extra leading argument.\n\ | |
220 | movl _dl_skip_args@GOT(%ebx), %eax\n\ | |
221 | movl (%eax), %eax\n\ | |
222 | # Pop the original argument count.\n\ | |
223 | popl %edx\n\ | |
224 | # Adjust the stack pointer to skip _dl_skip_args words.\n\ | |
225 | leal (%esp,%eax,4), %esp\n\ | |
226 | # Subtract _dl_skip_args from argc.\n\ | |
227 | subl %eax, %edx\n\ | |
228 | # Push argc back on the stack.\n\ | |
229 | push %edx\n\ | |
230 | # The special initializer gets called with the stack just\n\ | |
231 | # as the application's entry point will see it; it can\n\ | |
232 | # switch stacks if it moves these contents over.\n\ | |
233 | " RTLD_START_SPECIAL_INIT "\n\ | |
234 | # Load the parameters again.\n\ | |
235 | # (eax, edx, ecx, *--esp) = (_dl_loaded, argc, argv, envp)\n\ | |
236 | movl _dl_loaded@GOT(%ebx), %esi\n\ | |
237 | leal 8(%esp,%edx,4), %eax\n\ | |
238 | leal 4(%esp), %ecx\n\ | |
239 | pushl %eax\n\ | |
240 | movl (%esi), %eax\n\ | |
241 | # Call the function to run the initializers.\n\ | |
242 | call _dl_init@PLT\n\ | |
243 | # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\ | |
244 | movl _dl_fini@GOT(%ebx), %edx\n\ | |
245 | # Jump to the user's entry point.\n\ | |
246 | jmp *%edi\n\ | |
247 | .previous\n\ | |
248 | "); | |
249 | ||
250 | #ifndef RTLD_START_SPECIAL_INIT | |
251 | #define RTLD_START_SPECIAL_INIT /* nothing */ | |
252 | #endif | |
253 | ||
254 | /* Nonzero iff TYPE should not be allowed to resolve to one of | |
255 | the main executable's symbols, as for a COPY reloc. */ | |
256 | #define elf_machine_lookup_noexec_p(type) ((type) == R_386_COPY) | |
257 | ||
258 | /* Nonzero iff TYPE describes relocation of a PLT entry, so | |
259 | PLT entries should not be allowed to define the value. */ | |
260 | #define elf_machine_lookup_noplt_p(type) ((type) == R_386_JMP_SLOT) | |
261 | ||
262 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ | |
263 | #define ELF_MACHINE_JMP_SLOT R_386_JMP_SLOT | |
264 | ||
265 | /* The i386 never uses Elf32_Rela relocations. */ | |
266 | #define ELF_MACHINE_NO_RELA 1 | |
267 | ||
268 | /* We define an initialization functions. This is called very early in | |
269 | _dl_sysdep_start. */ | |
270 | #define DL_PLATFORM_INIT dl_platform_init () | |
271 | ||
272 | extern const char *_dl_platform; | |
273 | ||
274 | static inline void __attribute__ ((unused)) | |
275 | dl_platform_init (void) | |
276 | { | |
277 | if (_dl_platform != NULL && *_dl_platform == '\0') | |
278 | /* Avoid an empty string which would disturb us. */ | |
279 | _dl_platform = NULL; | |
280 | } | |
281 | ||
282 | static inline void | |
283 | elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc, | |
284 | Elf32_Addr *reloc_addr, Elf32_Addr value) | |
285 | { | |
286 | *reloc_addr = value; | |
287 | } | |
288 | ||
289 | /* Return the final value of a plt relocation. */ | |
290 | static inline Elf32_Addr | |
291 | elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc, | |
292 | Elf32_Addr value) | |
293 | { | |
294 | return value; | |
295 | } | |
296 | ||
297 | #endif /* !dl_machine_h */ | |
298 | ||
299 | #ifdef RESOLVE | |
300 | ||
301 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). | |
302 | MAP is the object containing the reloc. */ | |
303 | ||
304 | static inline void | |
305 | elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc, | |
306 | const Elf32_Sym *sym, const struct r_found_version *version, | |
307 | Elf32_Addr *const reloc_addr) | |
308 | { | |
309 | if (ELF32_R_TYPE (reloc->r_info) == R_386_RELATIVE) | |
310 | { | |
311 | #ifndef RTLD_BOOTSTRAP | |
312 | if (map != &_dl_rtld_map) /* Already done in rtld itself. */ | |
313 | #endif | |
314 | *reloc_addr += map->l_addr; | |
315 | } | |
316 | else if (ELF32_R_TYPE (reloc->r_info) != R_386_NONE) | |
317 | { | |
318 | const Elf32_Sym *const refsym = sym; | |
319 | Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info)); | |
320 | if (sym) | |
321 | value += sym->st_value; | |
322 | ||
323 | switch (ELF32_R_TYPE (reloc->r_info)) | |
324 | { | |
325 | case R_386_COPY: | |
326 | if (sym == NULL) | |
327 | /* This can happen in trace mode if an object could not be | |
328 | found. */ | |
329 | break; | |
330 | if (sym->st_size > refsym->st_size | |
331 | || (sym->st_size < refsym->st_size && _dl_verbose)) | |
332 | { | |
333 | const char *strtab; | |
334 | ||
335 | strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]); | |
336 | _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", | |
337 | ": Symbol `", strtab + refsym->st_name, | |
338 | "' has different size in shared object, " | |
339 | "consider re-linking\n", NULL); | |
340 | } | |
341 | memcpy (reloc_addr, (void *) value, MIN (sym->st_size, | |
342 | refsym->st_size)); | |
343 | break; | |
344 | case R_386_GLOB_DAT: | |
345 | case R_386_JMP_SLOT: | |
346 | *reloc_addr = value; | |
347 | break; | |
348 | case R_386_32: | |
349 | { | |
350 | #ifndef RTLD_BOOTSTRAP | |
351 | /* This is defined in rtld.c, but nowhere in the static | |
352 | libc.a; make the reference weak so static programs can | |
353 | still link. This declaration cannot be done when | |
354 | compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because | |
355 | rtld.c contains the common defn for _dl_rtld_map, which | |
356 | is incompatible with a weak decl in the same file. */ | |
357 | weak_extern (_dl_rtld_map); | |
358 | if (map == &_dl_rtld_map) | |
359 | /* Undo the relocation done here during bootstrapping. | |
360 | Now we will relocate it anew, possibly using a | |
361 | binding found in the user program or a loaded library | |
362 | rather than the dynamic linker's built-in definitions | |
363 | used while loading those libraries. */ | |
364 | value -= map->l_addr + refsym->st_value; | |
365 | #endif | |
366 | *reloc_addr += value; | |
367 | break; | |
368 | } | |
369 | case R_386_PC32: | |
370 | *reloc_addr += (value - (Elf32_Addr) reloc_addr); | |
371 | break; | |
372 | default: | |
373 | _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 0); | |
374 | break; | |
375 | } | |
376 | } | |
377 | } | |
378 | ||
379 | static inline void | |
380 | elf_machine_lazy_rel (struct link_map *map, | |
381 | Elf32_Addr l_addr, const Elf32_Rel *reloc) | |
382 | { | |
383 | Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset); | |
384 | /* Check for unexpected PLT reloc type. */ | |
385 | if (ELF32_R_TYPE (reloc->r_info) == R_386_JMP_SLOT) | |
386 | *reloc_addr += l_addr; | |
387 | else | |
388 | _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 1); | |
389 | } | |
390 | ||
391 | #endif /* RESOLVE */ |