]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/sparc/dl-machine.h
Update.
[thirdparty/glibc.git] / sysdeps / sparc / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. SPARC version.
2 Copyright (C) 1996, 1997 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 #define ELF_MACHINE_NAME "sparc"
21
22 #include <assert.h>
23 #include <string.h>
24 #include <link.h>
25
26
27 /* Some SPARC opcodes we need to use for self-modifying code. */
28 #define OPCODE_NOP 0x01000000 /* nop */
29 #define OPCODE_CALL 0x40000000 /* call ?; add PC-rel word address */
30 #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
31 #define OPCODE_JMP_G1 0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
32 #define OPCODE_SAVE_SP64 0x9de3bfc0 /* save %sp, -64, %sp */
33
34
35 /* Return nonzero iff E_MACHINE is compatible with the running host. */
36 static inline int
37 elf_machine_matches_host (Elf32_Half e_machine)
38 {
39 return e_machine == EM_SPARC;
40 }
41
42
43 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
44 first element of the GOT. This must be inlined in a function which
45 uses global data. */
46 static inline Elf32_Addr
47 elf_machine_dynamic (void)
48 {
49 register Elf32_Addr *got asm ("%l7");
50 return *got;
51 }
52
53
54 /* Return the run-time load address of the shared object. */
55 static inline Elf32_Addr
56 elf_machine_load_address (void)
57 {
58 Elf32_Addr addr;
59
60 asm (
61 "add %%fp,0x44,%%o2\n\t" /* o2 = point to argc */
62 "ld [%%o2 - 4],%%o0\n\t" /* o0 = load argc */
63 "sll %%o0, 2, %%o0\n\t" /* o0 = argc * sizeof (int) */
64 "add %%o2,%%o0,%%o2\n\t" /* o2 = skip over argv */
65 "add %%o2,4,%%o2\n\t" /* skip over null after argv */
66
67 /* Now %o2 is pointing to env, skip over that as well. */
68 "1:\n\t"
69 "ld [%%o2],%%o0\n\t"
70 "cmp %%o0,0\n\t"
71 "bnz 1b\n\t"
72 "add %%o2,4,%%o2\n\t"
73
74 /* Note that above, we want to advance the NULL after envp so
75 we always add 4. */
76
77 /* Now, search for the AT_BASE property. */
78 "2:\n\t"
79 "ld [%%o2],%%o0\n\t"
80 "cmp %%o0,0\n\t"
81 "be,a 3f\n\t"
82 "or %%g0,%%g0,%0\n\t"
83 "cmp %%o0,7\n\t" /* AT_BASE = 7 */
84 "be,a 3f\n\t"
85 "ld [%%o2+4],%0\n\t"
86 "b 2b\n\t"
87 "add %%o2,8,%%o2\n\t"
88 /* At this point %0 has the load address for the interpreter */
89 "3:\n\t"
90 : "=r" (addr)
91 : /* no inputs */
92 : "o0", "o2");
93 return addr;
94 }
95
96 #ifdef RESOLVE
97 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
98 MAP is the object containing the reloc. */
99
100 static inline void
101 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
102 const Elf32_Sym *sym, const struct r_found_version *version)
103 {
104 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
105 Elf32_Addr loadbase;
106
107 if (ELF32_R_TYPE (reloc->r_info) == R_SPARC_RELATIVE)
108 {
109 #ifndef RTLD_BOOTSTRAP
110 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
111 #endif
112 *reloc_addr += map->l_addr + reloc->r_addend;
113 }
114 else
115 {
116 Elf32_Addr value;
117 if (sym->st_shndx != SHN_UNDEF &&
118 ELF32_ST_BIND (sym->st_info) == STB_LOCAL)
119 value = map->l_addr;
120 else
121 {
122 value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
123 if (sym)
124 value += sym->st_value;
125 }
126 value += reloc->r_addend; /* Assume copy relocs have zero addend. */
127
128 switch (ELF32_R_TYPE (reloc->r_info))
129 {
130 case R_SPARC_COPY:
131 memcpy (reloc_addr, (void *) value, sym->st_size);
132 break;
133 case R_SPARC_GLOB_DAT:
134 case R_SPARC_32:
135 *reloc_addr = value;
136 break;
137 case R_SPARC_JMP_SLOT:
138 reloc_addr[1] = OPCODE_SETHI_G1 | (value >> 10);
139 reloc_addr[2] = OPCODE_JMP_G1 | (value & 0x3ff);
140 break;
141 case R_SPARC_8:
142 *(char *) reloc_addr = value;
143 break;
144 case R_SPARC_16:
145 *(short *) reloc_addr = value;
146 break;
147 case R_SPARC_DISP8:
148 *(char *) reloc_addr = (value - (Elf32_Addr) reloc_addr);
149 break;
150 case R_SPARC_DISP16:
151 *(short *) reloc_addr = (value - (Elf32_Addr) reloc_addr);
152 break;
153 case R_SPARC_DISP32:
154 *reloc_addr = (value - (Elf32_Addr) reloc_addr);
155 break;
156 case R_SPARC_LO10:
157 *reloc_addr = (*reloc_addr & ~0x3ff) | (value & 0x3ff);
158 break;
159 case R_SPARC_WDISP30:
160 *reloc_addr = ((*reloc_addr & 0xc0000000)
161 | ((value - (unsigned int) reloc_addr) >> 2));
162 break;
163 case R_SPARC_HI22:
164 *reloc_addr = (*reloc_addr & 0xffc00000) | (value >> 10);
165 break;
166 case R_SPARC_NONE: /* Alright, Wilbur. */
167 break;
168 default:
169 assert (! "unexpected dynamic reloc type");
170 break;
171 }
172 }
173 }
174
175 static inline void
176 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
177 {
178 switch (ELF32_R_TYPE (reloc->r_info))
179 {
180 case R_SPARC_NONE:
181 break;
182 case R_SPARC_JMP_SLOT:
183 break;
184 default:
185 assert (! "unexpected PLT reloc type");
186 break;
187 }
188 }
189
190 #endif /* RESOLVE */
191
192 /* Nonzero iff TYPE should not be allowed to resolve to one of
193 the main executable's symbols, as for a COPY reloc. */
194 #define elf_machine_lookup_noexec_p(type) ((type) == R_SPARC_COPY)
195
196 /* Nonzero iff TYPE describes relocation of a PLT entry, so
197 PLT entries should not be allowed to define the value. */
198 #define elf_machine_lookup_noplt_p(type) ((type) == R_SPARC_JMP_SLOT)
199
200 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
201 #define ELF_MACHINE_RELOC_NOPLT R_SPARC_JMP_SLOT
202
203 /* The SPARC never uses Elf32_Rel relocations. */
204 #define ELF_MACHINE_NO_REL 1
205
206
207 /* Set up the loaded object described by L so its unrelocated PLT
208 entries will jump to the on-demand fixup code in dl-runtime.c. */
209
210 static inline void
211 elf_machine_runtime_setup (struct link_map *l, int lazy)
212 {
213 Elf32_Addr *plt;
214 extern void _dl_runtime_resolve (Elf32_Word);
215
216 if (l->l_info[DT_JMPREL] && lazy)
217 {
218 /* The entries for functions in the PLT have not yet been filled in.
219 Their initial contents will arrange when called to set the high 22
220 bits of %g1 with an offset into the .rela.plt section and jump to
221 the beginning of the PLT. */
222 plt = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
223
224 /* The beginning of the PLT does:
225
226 save %sp, -64, %sp
227 pltpc: call _dl_runtime_resolve
228 nop
229 .word MAP
230
231 This saves the register window containing the arguments, and the
232 PC value (pltpc) implicitly saved in %o7 by the call points near the
233 location where we store the link_map pointer for this object. */
234
235 plt[0] = OPCODE_SAVE_SP64; /* save %sp, -64, %sp */
236 /* Construct PC-relative word address. */
237 plt[1] = OPCODE_CALL | (((Elf32_Addr) &_dl_runtime_resolve -
238 (Elf32_Addr) &plt[1]) >> 2);
239 plt[2] = OPCODE_NOP; /* Fill call delay slot. */
240 plt[3] = (Elf32_Addr *) l;
241 }
242
243 /* This code is used in dl-runtime.c to call the `fixup' function
244 and then redirect to the address it returns. */
245 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
246 # Trampoline for _dl_runtime_resolver
247 .globl _dl_runtime_resolve
248 .type _dl_runtime_resolve, @function
249 _dl_runtime_resolve:
250 t 1
251 #call %g0
252 # Pass two args to fixup: the PLT address computed from the PC saved
253 # in the PLT's call insn, and the reloc offset passed in %g1.
254 #ld [%o7 + 8], %o1 | Second arg, loaded from PLTPC[2].
255 #call fixup
256 #shrl %g1, 22, %o0 | First arg, set in delay slot of call.
257 # Jump to the real function.
258 #jmpl %o0, %g0
259 # In the delay slot of that jump, restore the register window
260 # saved by the first insn of the PLT.
261 #restore
262 .size _dl_runtime_resolve, . - _dl_runtime_resolve
263 ");
264 /* The PLT uses Elf32_Rela relocs. */
265 #define elf_machine_relplt elf_machine_rela
266 }
267
268
269 /* Mask identifying addresses reserved for the user program,
270 where the dynamic linker should not map anything. */
271 #define ELF_MACHINE_USER_ADDRESS_MASK ???
272
273 /* Initial entry point code for the dynamic linker.
274 The C function `_dl_start' is the real entry point;
275 its return value is the user program's entry point. */
276
277 #define RTLD_START __asm__ ( \
278 ".text\n\
279 .globl _start\n\
280 .type _start,@function\n\
281 _start:\n\
282 /* Pass pointer to argument block to _dl_start. */\n\
283 add %sp,64,%o0\n\
284 call _dl_start\n\
285 nop\n\
286 \n\
287 mov %o0,%l0\n\
288 \n\
289 2:\n\
290 call 1f\n\
291 nop\n\
292 1:\n\
293 sethi %hi(_GLOBAL_OFFSET_TABLE_-(2b-.)),%l2\n\
294 sethi %hi(_dl_default_scope),%l3\n\
295 or %l2,%lo(_GLOBAL_OFFSET_TABLE_-(2b-.)),%l2\n\
296 or %l3,%lo(_dl_default_scope),%l3\n\
297 add %o7,%l2,%l1\n\
298 # %l1 has the GOT. %l3 has _dl_default_scope offset\n\
299 # Now, load _dl_default_scope [2]\n\
300 add %l3,4,%l3\n\
301 ld [%l1+%l3],%l4\n\
302 # %l4 has _dl_default_scope [2]\n\
303 # call _dl_init_next until it returns 0, pass _dl_default_scope [2]\n\
304 3:\n\
305 call _dl_init_next\n\
306 mov %l4,%o0\n\
307 cmp %o0,%g0\n\
308 bz,a 4f\n\
309 nop\n\
310 call %o0\n\
311 nop\n\
312 b,a 3b\n\
313 4:\n\
314 # Clear the _dl_starting_up variable and pass _dl_fini in %g1 as per ELF ABI.\n\
315 sethi %hi(_dl_starting_up),%l4\n\
316 sethi %hi(_dl_fini),%l3\n\
317 or %l4,%lo(_dl_starting_up),%l4\n\
318 or %l3,%lo(_dl_fini),%l3\n\
319 # clear _dl_starting_up\n\
320 ld [%l1+%l4],%l5\n\
321 st %g0,[%l5]\n\
322 # load out fini function for atexit in %g1\n\
323 ld [%l3+%l1],%g1\n\
324 # jump to the user program entry point.\n\
325 jmpl %l0,%g0\n\
326 nop\n\
327 ");