update from main archive 961203
[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.,
18    59 Temple Place - Suite 330, 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 <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 link-time address of _DYNAMIC.  Conveniently, this is the
43    first element of the GOT.  This must be inlined in a function which
44    uses global data.  */
45 static inline Elf32_Addr
46 elf_machine_dynamic (void)
47 {
48   register Elf32_Addr *got asm ("%ebx");
49   return *got;
50 }
51
52
53 /* Return the run-time load address of the shared object.  */
54 static inline Elf32_Addr __attribute__ ((unused))
55 elf_machine_load_address (void)
56 {
57   Elf32_Addr addr;
58   asm ("        call here\n"
59        "here:   popl %0\n"
60        "        subl $here, %0"
61        : "=r" (addr));
62   return addr;
63 }
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);
71
72
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.  */
75
76 static inline void __attribute__ ((unused))
77 elf_machine_runtime_setup (struct link_map *l, int lazy)
78 {
79   Elf32_Addr *got;
80   extern void _dl_runtime_resolve (Elf32_Word);
81
82   if (l->l_info[DT_JMPREL] && lazy)
83     {
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;
93     }
94
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
100 _dl_runtime_resolve:
101         call fixup      # Args pushed by PLT.
102         addl $8, %esp   # Pop args.
103         jmp *%eax       # Jump to function address.
104 ");
105 /* The PLT uses Elf32_Rel relocs.  */
106 #define elf_machine_relplt elf_machine_rel
107 }
108
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
112
113
114
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.  */
118
119 #define RTLD_START asm ("\
120 .text\n\
121 .globl _start\n\
122 .globl _dl_start_user\n\
123 _start:\n\
124         pushl %esp\n\
125         call _dl_start\n\
126         popl %ebx\n\
127 _dl_start_user:\n\
128         # Save the user entry point address in %edi.\n\
129         movl %eax, %edi\n\
130         # Point %ebx at the GOT.
131         call 0f\n\
132 0:      popl %ebx\n\
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\
137         movl (%eax), %eax\n\
138         # Pop the original argument count.\n\
139         popl %ecx\n\
140         # Subtract _dl_skip_args from it.\n\
141         subl %eax, %ecx\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\
145         pushl %ecx\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\
149 0:      pushl %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\
155         testl %eax, %eax\n\
156         jz 1f\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\
164         call *%eax\n\
165         # Loop to call _dl_init_next for the next initializer.\n\
166         jmp 0b\n\
167 1:      # Clear the startup flag.\n\
168         movl _dl_starting_up@GOT(%ebx), %eax\n\
169         movl $0, (%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\
173         jmp *%edi\n\
174 ");
175
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)
179
180 /* The i386 never uses Elf32_Rela relocations.  */
181 #define ELF_MACHINE_NO_RELA 1
182
183 #endif /* !dl_machine_h */
184
185 #ifdef RESOLVE
186
187 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
188    MAP is the object containing the reloc.  */
189
190 static inline void
191 elf_machine_rel (struct link_map *map,
192                  const Elf32_Rel *reloc, const Elf32_Sym *sym)
193 {
194   Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
195   Elf32_Addr loadbase;
196
197   switch (ELF32_R_TYPE (reloc->r_info))
198     {
199     case R_386_COPY:
200       loadbase = RESOLVE (&sym, DL_LOOKUP_NOEXEC);
201       memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
202       break;
203     case R_386_GLOB_DAT:
204       loadbase = RESOLVE (&sym, 0);
205       *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
206       break;
207     case R_386_JMP_SLOT:
208       loadbase = RESOLVE (&sym, DL_LOOKUP_NOPLT);
209       *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
210       break;
211     case R_386_32:
212       {
213         Elf32_Addr undo = 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
220            file.  */
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;
228 #endif
229         loadbase = RESOLVE (&sym, 0);
230         *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
231         break;
232       }
233     case R_386_RELATIVE:
234 #ifndef RTLD_BOOTSTRAP
235       if (map != &_dl_rtld_map) /* Already done in rtld itself.  */
236 #endif
237         *reloc_addr += map->l_addr;
238       break;
239     case R_386_PC32:
240       loadbase = RESOLVE (&sym, 0);
241       *reloc_addr += ((sym ? (loadbase + sym->st_value) : 0) -
242                       (Elf32_Addr) reloc_addr);
243       break;
244     case R_386_NONE:            /* Alright, Wilbur.  */
245       break;
246     default:
247       assert (! "unexpected dynamic reloc type");
248       break;
249     }
250
251 }
252
253 static inline void
254 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc)
255 {
256   Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
257   switch (ELF32_R_TYPE (reloc->r_info))
258     {
259     case R_386_JMP_SLOT:
260       *reloc_addr += map->l_addr;
261       break;
262     default:
263       assert (! "unexpected PLT reloc type");
264       break;
265     }
266 }
267
268 #endif /* RESOLVE */