]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/dl-machine.h
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"
27 /* Return nonzero iff E_MACHINE is compatible with the running host. */
29 elf_machine_matches_host (Elf32_Half e_machine
)
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");
52 /* Return the run-time load address of the shared object. */
53 static inline Elf32_Addr
54 elf_machine_load_address (void)
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. */
75 elf_machine_rel (struct link_map
*map
,
76 const Elf32_Rel
*reloc
,
77 Elf32_Addr sym_loadaddr
, const Elf32_Sym
*sym
)
79 Elf32_Addr
*const reloc_addr
= (void *) (map
->l_addr
+ reloc
->r_offset
);
80 const Elf32_Addr sym_value
= sym
? sym_loadaddr
+ sym
->st_value
: 0;
82 switch (ELF32_R_TYPE (reloc
->r_info
))
85 memcpy (reloc_addr
, (void *) sym_value
, sym
->st_size
);
89 *reloc_addr
= sym_value
;
92 if (map
->l_type
== lt_interpreter
)
94 /* Undo the relocation done here during bootstrapping. Now we will
95 relocate it anew, possibly using a binding found in the user
96 program or a loaded library rather than the dynamic linker's
97 built-in definitions used while loading those libraries. */
98 const Elf32_Sym
*const dlsymtab
99 = (void *) (map
->l_addr
+ map
->l_info
[DT_SYMTAB
]->d_un
.d_ptr
);
100 *reloc_addr
-= (map
->l_addr
+
101 dlsymtab
[ELF32_R_SYM (reloc
->r_info
)].st_value
);
103 *reloc_addr
+= sym_value
;
106 if (map
->l_type
!= lt_interpreter
) /* Already done in dynamic linker. */
107 *reloc_addr
+= map
->l_addr
;
110 *reloc_addr
+= sym_value
- (Elf32_Addr
) reloc_addr
;
112 case R_386_NONE
: /* Alright, Wilbur. */
115 assert (! "unexpected dynamic reloc type");
121 elf_machine_lazy_rel (struct link_map
*map
, const Elf32_Rel
*reloc
)
123 Elf32_Addr
*const reloc_addr
= (void *) (map
->l_addr
+ reloc
->r_offset
);
124 switch (ELF32_R_TYPE (reloc
->r_info
))
127 *reloc_addr
+= map
->l_addr
;
130 assert (! "unexpected PLT reloc type");
135 /* Nonzero iff TYPE describes relocation of a PLT entry, so
136 PLT entries should not be allowed to define the value. */
137 #define elf_machine_pltrel_p(type) ((type) == R_386_JMP_SLOT)
139 /* The i386 never uses Elf32_Rela relocations. */
140 #define ELF_MACHINE_NO_RELA 1
143 /* Set up the loaded object described by L so its unrelocated PLT
144 entries will jump to the on-demand fixup code in dl-runtime.c. */
147 elf_machine_runtime_setup (struct link_map
*l
, int lazy
)
150 extern void _dl_runtime_resolve (Elf32_Word
);
152 if (l
->l_info
[DT_JMPREL
] && lazy
)
154 /* The GOT entries for functions in the PLT have not yet been filled
155 in. Their initial contents will arrange when called to push an
156 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
157 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
158 got
= (Elf32_Addr
*) (l
->l_addr
+ l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
159 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
160 /* This function will get called to fix up the GOT entry indicated by
161 the offset on the stack, and then jump to the resolved address. */
162 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
165 /* This code is used in dl-runtime.c to call the `fixup' function
166 and then redirect to the address it returns. */
167 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
168 .globl _dl_runtime_resolve
169 .type _dl_runtime_resolve, @function
171 call fixup # Args pushed by PLT.
172 addl $8, %esp # Pop args.
173 jmp *%eax # Jump to function address.
175 /* The PLT uses Elf32_Rel relocs. */
176 #define elf_machine_relplt elf_machine_rel
179 /* Mask identifying addresses reserved for the user program,
180 where the dynamic linker should not map anything. */
181 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
185 /* Initial entry point code for the dynamic linker.
186 The C function `_dl_start' is the real entry point;
187 its return value is the user program's entry point. */
189 #define RTLD_START asm ("\
192 .globl _dl_start_user\n\
196 # Save the user entry point address in %edi.\n\
198 # Point %ebx at the GOT.
201 addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\
202 # See if we were run as a command with the executable file\n\
203 # name as an extra leading argument.\n\
204 movl _dl_skip_args@GOT(%ebx), %eax\n\
206 # Pop the original argument count.\n\
208 # Subtract _dl_skip_args from it.\n\
210 # Adjust the stack pointer to skip _dl_skip_args words.\n\
211 leal (%esp,%eax,4), %esp\n\
212 # Push back the modified argument count.\n\
214 # Call _dl_init_next to return the address of an initializer\n\
215 # function to run.\n\
216 0: call _dl_init_next@PLT\n\
217 # Check for zero return, when out of initializers.\n\
220 # Call the shared object initializer function.\n\
221 # NOTE: We depend only on the registers (%ebx and %edi)\n\
222 # and the return address pushed by this call;\n\
223 # the initializer is called with the stack just\n\
224 # as it appears on entry, and it is free to move\n\
225 # the stack around, as long as it winds up jumping to\n\
226 # the return address on the top of the stack.\n\
228 # Loop to call _dl_init_next for the next initializer.\n\
230 1: # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
231 movl _dl_fini@GOT(%ebx), %edx\n\
232 # Jump to the user's entry point.\n\