1 /* Machine-dependent ELF dynamic relocation inline functions. i386 version.
2 Copyright (C) 1995, 1996 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
17 not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #define ELF_MACHINE_NAME "i386"
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)
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
45 static inline Elf32_Addr
46 elf_machine_dynamic (void)
48 register Elf32_Addr *got asm ("%ebx");
53 /* Return the run-time load address of the shared object. */
54 static inline Elf32_Addr __attribute__ ((unused))
55 elf_machine_load_address (void)
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 /* Set up the loaded object described by L so its unrelocated PLT
74 entries will jump to the on-demand fixup code in dl-runtime.c. */
76 static inline void __attribute__ ((unused))
77 elf_machine_runtime_setup (struct link_map *l, int lazy)
80 extern void _dl_runtime_resolve (Elf32_Word);
82 if (l->l_info[DT_JMPREL] && lazy)
84 /* The GOT entries for functions in the PLT have not yet been filled
85 in. Their initial contents will arrange when called to push an
86 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
87 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
88 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
89 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
90 /* This function will get called to fix up the GOT entry indicated by
91 the offset on the stack, and then jump to the resolved address. */
92 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
95 /* This code is used in dl-runtime.c to call the `fixup' function
96 and then redirect to the address it returns. */
97 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
98 .globl _dl_runtime_resolve
99 .type _dl_runtime_resolve, @function
101 call fixup # Args pushed by PLT.
102 addl $8, %esp # Pop args.
103 jmp *%eax # Jump to function address.
105 /* The PLT uses Elf32_Rel relocs. */
106 #define elf_machine_relplt elf_machine_rel
109 /* Mask identifying addresses reserved for the user program,
110 where the dynamic linker should not map anything. */
111 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
115 /* Initial entry point code for the dynamic linker.
116 The C function `_dl_start' is the real entry point;
117 its return value is the user program's entry point. */
119 #define RTLD_START asm ("\
122 .globl _dl_start_user\n\
128 # Save the user entry point address in %edi.\n\
130 # Point %ebx at the GOT.
133 addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\
134 # See if we were run as a command with the executable file\n\
135 # name as an extra leading argument.\n\
136 movl _dl_skip_args@GOT(%ebx), %eax\n\
138 # Pop the original argument count.\n\
140 # Subtract _dl_skip_args from it.\n\
142 # Adjust the stack pointer to skip _dl_skip_args words.\n\
143 leal (%esp,%eax,4), %esp\n\
144 # Push back the modified argument count.\n\
146 # Push _dl_default_scope[2] as argument in _dl_init_next call below.\n\
147 movl _dl_default_scope@GOT(%ebx), %eax\n\
148 movl 8(%eax), %esi\n\
150 # Call _dl_init_next to return the address of an initializer\n\
151 # function to run.\n\
152 call _dl_init_next@PLT\n\
153 addl $4, %esp # Pop argument.\n\
154 # Check for zero return, when out of initializers.\n\
157 # Call the shared object initializer function.\n\
158 # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\
159 # and the return address pushed by this call;\n\
160 # the initializer is called with the stack just\n\
161 # as it appears on entry, and it is free to move\n\
162 # the stack around, as long as it winds up jumping to\n\
163 # the return address on the top of the stack.\n\
165 # Loop to call _dl_init_next for the next initializer.\n\
167 1: # Clear the startup flag.\n\
168 movl _dl_starting_up@GOT(%ebx), %eax\n\
170 # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
171 movl _dl_fini@GOT(%ebx), %edx\n\
172 # Jump to the user's entry point.\n\
176 /* Nonzero iff TYPE describes relocation of a PLT entry, so
177 PLT entries should not be allowed to define the value. */
178 #define elf_machine_pltrel_p(type) ((type) == R_386_JMP_SLOT)
180 /* The i386 never uses Elf32_Rela relocations. */
181 #define ELF_MACHINE_NO_RELA 1
183 #endif /* !dl_machine_h */
187 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
188 MAP is the object containing the reloc. */
191 elf_machine_rel (struct link_map *map,
192 const Elf32_Rel *reloc, const Elf32_Sym *sym)
194 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
197 switch (ELF32_R_TYPE (reloc->r_info))
200 loadbase = RESOLVE (&sym, DL_LOOKUP_NOEXEC);
201 memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
204 loadbase = RESOLVE (&sym, 0);
205 *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
208 loadbase = RESOLVE (&sym, DL_LOOKUP_NOPLT);
209 *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
214 #ifndef RTLD_BOOTSTRAP
215 /* This is defined in rtld.c, but nowhere in the static libc.a;
216 make the reference weak so static programs can still link. This
217 declaration cannot be done when compiling rtld.c (i.e. #ifdef
218 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
219 _dl_rtld_map, which is incompatible with a weak decl in the same
221 weak_extern (_dl_rtld_map);
222 if (map == &_dl_rtld_map)
223 /* Undo the relocation done here during bootstrapping. Now we will
224 relocate it anew, possibly using a binding found in the user
225 program or a loaded library rather than the dynamic linker's
226 built-in definitions used while loading those libraries. */
227 undo = map->l_addr + sym->st_value;
229 loadbase = RESOLVE (&sym, 0);
230 *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
234 #ifndef RTLD_BOOTSTRAP
235 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
237 *reloc_addr += map->l_addr;
240 loadbase = RESOLVE (&sym, 0);
241 *reloc_addr += ((sym ? (loadbase + sym->st_value) : 0) -
242 (Elf32_Addr) reloc_addr);
244 case R_386_NONE: /* Alright, Wilbur. */
247 assert (! "unexpected dynamic reloc type");
254 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc)
256 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
257 switch (ELF32_R_TYPE (reloc->r_info))
260 *reloc_addr += map->l_addr;
263 assert (! "unexpected PLT reloc type");