* elf/Makefile ($(objpfx)$(rtld-installed-name)): New target to make
[glibc.git] / sysdeps / i386 / dl-machine.h
bloba718792e0a17dce314606400ca2faf7cabb889d7
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., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #define ELF_MACHINE_NAME "i386"
22 #include <assert.h>
23 #include <string.h>
24 #include <link.h>
27 /* Return nonzero iff E_MACHINE is compatible with the running host. */
28 static inline int
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 run-time address of the _GLOBAL_OFFSET_TABLE_.
43 Must be inlined in a function which uses global data. */
44 static inline Elf32_Addr *
45 elf_machine_got (void)
47 register Elf32_Addr *got asm ("%ebx");
48 return got;
52 /* Return the run-time load address of the shared object. */
53 static inline Elf32_Addr
54 elf_machine_load_address (void)
56 Elf32_Addr addr;
57 asm (" call here\n"
58 "here: popl %0\n"
59 " subl $here, %0"
60 : "=r" (addr));
61 return addr;
63 /* The `subl' insn above will contain an R_386_32 relocation entry
64 intended to insert the run-time address of the label `here'.
65 This will be the first relocation in the text of the dynamic linker;
66 we skip it to avoid trying to modify read-only text in this early stage. */
67 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
68 ++(const Elf32_Rel *) (dynamic_info)[DT_REL]->d_un.d_ptr; \
69 (dynamic_info)[DT_RELSZ]->d_un.d_val -= sizeof (Elf32_Rel);
71 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
72 MAP is the object containing the reloc. */
74 static inline void
75 elf_machine_rel (struct link_map *map,
76 const Elf32_Rel *reloc, const Elf32_Sym *sym,
77 Elf32_Addr (*resolve) (const Elf32_Sym **ref,
78 Elf32_Addr reloc_addr,
79 int noplt))
81 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
82 Elf32_Addr loadbase, undo;
84 switch (ELF32_R_TYPE (reloc->r_info))
86 case R_386_COPY:
87 loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
88 memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
89 break;
90 case R_386_GLOB_DAT:
91 loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0) :
92 /* RESOLVE is null during bootstrap relocation. */
93 map->l_addr);
94 *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
95 break;
96 case R_386_JMP_SLOT:
97 loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 1) :
98 /* RESOLVE is null during bootstrap relocation. */
99 map->l_addr);
100 *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
101 break;
102 case R_386_32:
103 if (resolve && map == &_dl_rtld_map)
104 /* Undo the relocation done here during bootstrapping. Now we will
105 relocate it anew, possibly using a binding found in the user
106 program or a loaded library rather than the dynamic linker's
107 built-in definitions used while loading those libraries. */
108 undo = map->l_addr + sym->st_value;
109 else
110 undo = 0;
111 loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0) :
112 /* RESOLVE is null during bootstrap relocation. */
113 map->l_addr);
114 *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
115 break;
116 case R_386_RELATIVE:
117 if (!resolve || map != &_dl_rtld_map) /* Already done in rtld itself. */
118 *reloc_addr += map->l_addr;
119 break;
120 case R_386_PC32:
121 loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0) :
122 /* RESOLVE is null during bootstrap relocation. */
123 map->l_addr);
124 *reloc_addr += ((sym ? (loadbase + sym->st_value) : 0) -
125 (Elf32_Addr) reloc_addr);
126 break;
127 case R_386_NONE: /* Alright, Wilbur. */
128 break;
129 default:
130 assert (! "unexpected dynamic reloc type");
131 break;
135 static inline void
136 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc)
138 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
139 switch (ELF32_R_TYPE (reloc->r_info))
141 case R_386_JMP_SLOT:
142 *reloc_addr += map->l_addr;
143 break;
144 default:
145 assert (! "unexpected PLT reloc type");
146 break;
150 /* Nonzero iff TYPE describes relocation of a PLT entry, so
151 PLT entries should not be allowed to define the value. */
152 #define elf_machine_pltrel_p(type) ((type) == R_386_JMP_SLOT)
154 /* The i386 never uses Elf32_Rela relocations. */
155 #define ELF_MACHINE_NO_RELA 1
158 /* Set up the loaded object described by L so its unrelocated PLT
159 entries will jump to the on-demand fixup code in dl-runtime.c. */
161 static inline void
162 elf_machine_runtime_setup (struct link_map *l, int lazy)
164 Elf32_Addr *got;
165 extern void _dl_runtime_resolve (Elf32_Word);
167 if (l->l_info[DT_JMPREL] && lazy)
169 /* The GOT entries for functions in the PLT have not yet been filled
170 in. Their initial contents will arrange when called to push an
171 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
172 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
173 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
174 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
175 /* This function will get called to fix up the GOT entry indicated by
176 the offset on the stack, and then jump to the resolved address. */
177 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
180 /* This code is used in dl-runtime.c to call the `fixup' function
181 and then redirect to the address it returns. */
182 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
183 .globl _dl_runtime_resolve
184 .type _dl_runtime_resolve, @function
185 _dl_runtime_resolve:
186 call fixup # Args pushed by PLT.
187 addl $8, %esp # Pop args.
188 jmp *%eax # Jump to function address.
190 /* The PLT uses Elf32_Rel relocs. */
191 #define elf_machine_relplt elf_machine_rel
194 /* Mask identifying addresses reserved for the user program,
195 where the dynamic linker should not map anything. */
196 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
200 /* Initial entry point code for the dynamic linker.
201 The C function `_dl_start' is the real entry point;
202 its return value is the user program's entry point. */
204 #define RTLD_START asm ("\
205 .text\n\
206 .globl _start\n\
207 .globl _dl_start_user\n\
208 _start:\n\
209 call _dl_start\n\
210 _dl_start_user:\n\
211 # Save the user entry point address in %edi.\n\
212 movl %eax, %edi\n\
213 # Point %ebx at the GOT.
214 call 0f\n\
215 0: popl %ebx\n\
216 addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\
217 # See if we were run as a command with the executable file\n\
218 # name as an extra leading argument.\n\
219 movl _dl_skip_args@GOT(%ebx), %eax\n\
220 movl (%eax),%eax\n\
221 # Pop the original argument count.\n\
222 popl %ecx\n\
223 # Subtract _dl_skip_args from it.\n\
224 subl %eax, %ecx\n\
225 # Adjust the stack pointer to skip _dl_skip_args words.\n\
226 leal (%esp,%eax,4), %esp\n\
227 # Push back the modified argument count.\n\
228 pushl %ecx\n\
229 # Push _dl_default_scope[2] as argument in _dl_init_next call below.\n\
230 movl _dl_default_scope@GOT(%ebx), %eax\n\
231 movl 8(%eax), %esi\n\
232 0: pushl %esi\n\
233 # Call _dl_init_next to return the address of an initializer\n\
234 # function to run.\n\
235 call _dl_init_next@PLT\n\
236 addl $4, %esp # Pop argument.\n\
237 # Check for zero return, when out of initializers.\n\
238 testl %eax,%eax\n\
239 jz 1f\n\
240 # Call the shared object initializer function.\n\
241 # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\
242 # and the return address pushed by this call;\n\
243 # the initializer is called with the stack just\n\
244 # as it appears on entry, and it is free to move\n\
245 # the stack around, as long as it winds up jumping to\n\
246 # the return address on the top of the stack.\n\
247 call *%eax\n\
248 # Loop to call _dl_init_next for the next initializer.\n\
249 jmp 0b\n\
250 1: # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
251 movl _dl_fini@GOT(%ebx), %edx\n\
252 # Jump to the user's entry point.\n\
253 jmp *%edi\n\