5bd0f8254bbcc55f78342202cef2129d21849e54
[platform/upstream/glibc.git] / 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.
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.  */
19
20 #ifndef dl_machine_h
21 #define dl_machine_h
22
23 #define ELF_MACHINE_NAME "i386"
24
25 #include <assert.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     case EM_486:
35       return 1;
36     default:
37       return 0;
38     }
39 }
40
41
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 * __attribute__ ((unused))
45 elf_machine_got (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 ("        call here\n"
58        "here:   popl %0\n"
59        "        subl $here, %0"
60        : "=r" (addr));
61   return addr;
62 }
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);
70
71
72 /* Set up the loaded object described by L so its unrelocated PLT
73    entries will jump to the on-demand fixup code in dl-runtime.c.  */
74
75 static inline void __attribute__ ((unused))
76 elf_machine_runtime_setup (struct link_map *l, int lazy)
77 {
78   Elf32_Addr *got;
79   extern void _dl_runtime_resolve (Elf32_Word);
80
81   if (l->l_info[DT_JMPREL] && lazy)
82     {
83       /* The GOT entries for functions in the PLT have not yet been filled
84          in.  Their initial contents will arrange when called to push an
85          offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
86          and then jump to _GLOBAL_OFFSET_TABLE[2].  */
87       got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
88       got[1] = (Elf32_Addr) l;  /* Identify this shared object.  */
89       /* This function will get called to fix up the GOT entry indicated by
90          the offset on the stack, and then jump to the resolved address.  */
91       got[2] = (Elf32_Addr) &_dl_runtime_resolve;
92     }
93
94   /* This code is used in dl-runtime.c to call the `fixup' function
95      and then redirect to the address it returns.  */
96 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
97         .globl _dl_runtime_resolve
98         .type _dl_runtime_resolve, @function
99 _dl_runtime_resolve:
100         call fixup      # Args pushed by PLT.
101         addl $8, %esp   # Pop args.
102         jmp *%eax       # Jump to function address.
103 ");
104 /* The PLT uses Elf32_Rel relocs.  */
105 #define elf_machine_relplt elf_machine_rel
106 }
107
108 /* Mask identifying addresses reserved for the user program,
109    where the dynamic linker should not map anything.  */
110 #define ELF_MACHINE_USER_ADDRESS_MASK   0xf8000000UL
111
112
113
114 /* Initial entry point code for the dynamic linker.
115    The C function `_dl_start' is the real entry point;
116    its return value is the user program's entry point.  */
117
118 #define RTLD_START asm ("\
119 .text\n\
120 .globl _start\n\
121 .globl _dl_start_user\n\
122 _start:\n\
123         pushl %esp\n\
124         call _dl_start\n\
125         popl %ebx\n\
126 _dl_start_user:\n\
127         # Save the user entry point address in %edi.\n\
128         movl %eax, %edi\n\
129         # Point %ebx at the GOT.
130         call 0f\n\
131 0:      popl %ebx\n\
132         addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx\n\
133         # See if we were run as a command with the executable file\n\
134         # name as an extra leading argument.\n\
135         movl _dl_skip_args@GOT(%ebx), %eax\n\
136         movl (%eax), %eax\n\
137         # Pop the original argument count.\n\
138         popl %ecx\n\
139         # Subtract _dl_skip_args from it.\n\
140         subl %eax, %ecx\n\
141         # Adjust the stack pointer to skip _dl_skip_args words.\n\
142         leal (%esp,%eax,4), %esp\n\
143         # Push back the modified argument count.\n\
144         pushl %ecx\n\
145         # Push _dl_default_scope[2] as argument in _dl_init_next call below.\n\
146         movl _dl_default_scope@GOT(%ebx), %eax\n\
147         movl 8(%eax), %esi\n\
148 0:      pushl %esi\n\
149         # Call _dl_init_next to return the address of an initializer\n\
150         # function to run.\n\
151         call _dl_init_next@PLT\n\
152         addl $4, %esp # Pop argument.\n\
153         # Check for zero return, when out of initializers.\n\
154         testl %eax, %eax\n\
155         jz 1f\n\
156         # Call the shared object initializer function.\n\
157         # NOTE: We depend only on the registers (%ebx, %esi and %edi)\n\
158         # and the return address pushed by this call;\n\
159         # the initializer is called with the stack just\n\
160         # as it appears on entry, and it is free to move\n\
161         # the stack around, as long as it winds up jumping to\n\
162         # the return address on the top of the stack.\n\
163         call *%eax\n\
164         # Loop to call _dl_init_next for the next initializer.\n\
165         jmp 0b\n\
166 1:      # Clear the startup flag.\n\
167         movl _dl_starting_up@GOT(%ebx), %eax\n\
168         movl $0, (%eax)\n\
169         # Pass our finalizer function to the user in %edx, as per ELF ABI.\n\
170         movl _dl_fini@GOT(%ebx), %edx\n\
171         # Jump to the user's entry point.\n\
172         jmp *%edi\n\
173 ");
174
175 /* Nonzero iff TYPE describes relocation of a PLT entry, so
176    PLT entries should not be allowed to define the value.  */
177 #define elf_machine_pltrel_p(type) ((type) == R_386_JMP_SLOT)
178
179 /* The i386 never uses Elf32_Rela relocations.  */
180 #define ELF_MACHINE_NO_RELA 1
181
182 #endif /* !dl_machine_h */
183
184 #ifdef RESOLVE
185
186 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
187    MAP is the object containing the reloc.  */
188
189 static inline void
190 elf_machine_rel (struct link_map *map,
191                  const Elf32_Rel *reloc, const Elf32_Sym *sym)
192 {
193   Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
194   Elf32_Addr loadbase;
195
196   switch (ELF32_R_TYPE (reloc->r_info))
197     {
198     case R_386_COPY:
199       loadbase = RESOLVE (&sym, DL_LOOKUP_NOEXEC);
200       memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
201       break;
202     case R_386_GLOB_DAT:
203       loadbase = RESOLVE (&sym, 0);
204       *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
205       break;
206     case R_386_JMP_SLOT:
207       loadbase = RESOLVE (&sym, DL_LOOKUP_NOPLT);
208       *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
209       break;
210     case R_386_32:
211       {
212         Elf32_Addr undo = 0;
213 #ifndef RTLD_BOOTSTRAP
214         /* This is defined in rtld.c, but nowhere in the static libc.a;
215            make the reference weak so static programs can still link.  This
216            declaration cannot be done when compiling rtld.c (i.e.  #ifdef
217            RTLD_BOOTSTRAP) because rtld.c contains the common defn for
218            _dl_rtld_map, which is incompatible with a weak decl in the same
219            file.  */
220         weak_extern (_dl_rtld_map);
221         if (map == &_dl_rtld_map)
222           /* Undo the relocation done here during bootstrapping.  Now we will
223              relocate it anew, possibly using a binding found in the user
224              program or a loaded library rather than the dynamic linker's
225              built-in definitions used while loading those libraries.  */
226           undo = map->l_addr + sym->st_value;
227 #endif
228         loadbase = RESOLVE (&sym, 0);
229         *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
230         break;
231       }
232     case R_386_RELATIVE:
233 #ifndef RTLD_BOOTSTRAP
234       if (map != &_dl_rtld_map) /* Already done in rtld itself.  */
235 #endif
236         *reloc_addr += map->l_addr;
237       break;
238     case R_386_PC32:
239       loadbase = RESOLVE (&sym, 0);
240       *reloc_addr += ((sym ? (loadbase + sym->st_value) : 0) -
241                       (Elf32_Addr) reloc_addr);
242       break;
243     case R_386_NONE:            /* Alright, Wilbur.  */
244       break;
245     default:
246       assert (! "unexpected dynamic reloc type");
247       break;
248     }
249
250 }
251
252 static inline void
253 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc)
254 {
255   Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
256   switch (ELF32_R_TYPE (reloc->r_info))
257     {
258     case R_386_JMP_SLOT:
259       *reloc_addr += map->l_addr;
260       break;
261     default:
262       assert (! "unexpected PLT reloc type");
263       break;
264     }
265 }
266
267 #endif /* RESOLVE */