Update.
[glibc.git] / sysdeps / sparc / sparc32 / dl-machine.h
blob224c6bafff8f9c7495e246370ee13e31b12eb81a
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.
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.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #define ELF_MACHINE_NAME "sparc"
22 #include <assert.h>
23 #include <string.h>
24 #include <link.h>
25 #include <sys/param.h>
28 /* Some SPARC opcodes we need to use for self-modifying code. */
29 #define OPCODE_NOP 0x01000000 /* nop */
30 #define OPCODE_CALL 0x40000000 /* call ?; add PC-rel word address */
31 #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
32 #define OPCODE_JMP_G1 0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
33 #define OPCODE_SAVE_SP 0x9de3bfa8 /* save %sp, -(16+6)*4, %sp */
36 /* Return nonzero iff E_MACHINE is compatible with the running host. */
37 static inline int
38 elf_machine_matches_host (Elf32_Half e_machine)
40 return e_machine == EM_SPARC;
44 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
45 first element of the GOT. This must be inlined in a function which
46 uses global data. */
47 static inline Elf32_Addr
48 elf_machine_dynamic (void)
50 register Elf32_Addr *got asm ("%l7");
51 return *got;
54 /* Return the run-time load address of the shared object. */
55 static inline Elf32_Addr
56 elf_machine_load_address (void)
58 register Elf32_Addr pc __asm("%o7"), pic __asm("%l7"), got;
60 /* Utilize the fact that a local .got entry will be partially
61 initialized at startup awaiting its RELATIVE fixup. */
63 __asm("sethi %%hi(.Load_address),%1\n"
64 ".Load_address:\n\t"
65 "call 1f\n\t"
66 "or %1,%%lo(.Load_address),%1\n"
67 "1:\tld [%2+%1],%1"
68 : "=r"(pc), "=r"(got) : "r"(pic));
70 return pc - got;
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. */
76 static inline int
77 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
79 Elf32_Addr *plt;
80 extern void _dl_runtime_resolve (Elf32_Word);
82 if (l->l_info[DT_JMPREL] && lazy)
84 /* The entries for functions in the PLT have not yet been filled in.
85 Their initial contents will arrange when called to set the high 22
86 bits of %g1 with an offset into the .rela.plt section and jump to
87 the beginning of the PLT. */
88 plt = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
90 /* The beginning of the PLT does:
92 save %sp, -64, %sp
93 pltpc: call _dl_runtime_resolve
94 nop
95 .word MAP
97 This saves the register window containing the arguments, and the
98 PC value (pltpc) implicitly saved in %o7 by the call points near the
99 location where we store the link_map pointer for this object. */
101 plt[0] = OPCODE_SAVE_SP;
102 /* Construct PC-relative word address. */
103 plt[1] = OPCODE_CALL | (((Elf32_Addr) &_dl_runtime_resolve -
104 (Elf32_Addr) &plt[1]) >> 2);
105 plt[2] = OPCODE_NOP; /* Fill call delay slot. */
106 plt[3] = (Elf32_Addr) l;
109 return lazy;
112 /* This code is used in dl-runtime.c to call the `fixup' function
113 and then redirect to the address it returns. */
114 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
115 .globl _dl_runtime_resolve
116 .type _dl_runtime_resolve, @function
117 _dl_runtime_resolve:
118 /* Set up the arguments to fixup --
119 %o0 = link_map out of plt0
120 %o1 = offset of reloc entry */
121 ld [%o7 + 8], %o0
122 srl %g1, 10, %o1
123 call fixup
124 sub %o1, 4*12, %o1
125 jmp %o0
126 restore
127 .size _dl_runtime_resolve, . - _dl_runtime_resolve");
129 /* Nonzero iff TYPE should not be allowed to resolve to one of
130 the main executable's symbols, as for a COPY reloc. */
131 #define elf_machine_lookup_noexec_p(type) ((type) == R_SPARC_COPY)
133 /* Nonzero iff TYPE describes relocation of a PLT entry, so
134 PLT entries should not be allowed to define the value. */
135 #define elf_machine_lookup_noplt_p(type) ((type) == R_SPARC_JMP_SLOT)
137 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
138 #define ELF_MACHINE_JMP_SLOT R_SPARC_JMP_SLOT
140 /* The SPARC never uses Elf32_Rel relocations. */
141 #define ELF_MACHINE_NO_REL 1
143 /* The SPARC overlaps DT_RELA and DT_PLTREL. */
144 #define ELF_MACHINE_PLTREL_OVERLAP 1
146 /* Initial entry point code for the dynamic linker.
147 The C function `_dl_start' is the real entry point;
148 its return value is the user program's entry point. */
150 #define RTLD_START __asm__ ("\
151 .text
152 .globl _start
153 .type _start,@function
154 _start:
155 /* Allocate space for functions to drop their arguments. */
156 sub %sp, 6*4, %sp
157 /* Pass pointer to argument block to _dl_start. */
158 call _dl_start
159 add %sp, 22*4, %o0
160 /* FALTHRU */
161 .globl _dl_start_user
162 .type _dl_start_user,@function
163 _dl_start_user:
164 /* Load the PIC register. */
165 1: call 2f
166 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7
167 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7
168 add %l7, %o7, %l7
169 /* Save the user entry point address in %l0 */
170 mov %o0, %l0
171 /* See if we were run as a command with the executable file name as an
172 extra leading argument. If so, adjust the contents of the stack. */
173 sethi %hi(_dl_skip_args), %g2
174 or %g2, %lo(_dl_skip_args), %g2
175 ld [%l7+%g2], %i0
176 ld [%i0], %i0
177 tst %i0
178 beq 3f
180 /* Find out how far to shift. */
181 ld [%sp+22*4], %i1 /* load argc */
182 sub %i1, %i0, %i1
183 sll %i0, 2, %i2
184 st %i1, [%sp+22*4]
185 add %sp, 23*4, %i1
186 add %i1, %i2, %i2
187 /* Copy down argv */
188 21: ld [%i2], %i3
189 add %i2, 4, %i2
190 tst %i3
191 st %i3, [%i1]
192 bne 21b
193 add %i1, 4, %i1
194 /* Copy down env */
195 22: ld [%i2], %i3
196 add %i2, 4, %i2
197 tst %i3
198 st %i3, [%i1]
199 bne 22b
200 add %i1, 4, %i1
201 /* Copy down auxiliary table. */
202 23: ld [%i2], %i3
203 ld [%i2+4], %i4
204 add %i2, 8, %i2
205 tst %i3
206 st %i3, [%i1]
207 st %i4, [%i1+4]
208 bne 23b
209 add %i1, 8, %i1
210 /* Load _dl_default_scope[2] to pass to _dl_init_next. */
211 3: sethi %hi(_dl_default_scope), %g1
212 or %g1, %lo(_dl_default_scope), %g1
213 ld [%l7+%g1], %l1
214 ld [%l1+2*4], %l1
215 /* Call _dl_init_next to return the address of an initializer to run. */
216 4: call _dl_init_next
217 mov %l1, %o0
218 tst %o0
219 beq 5f
221 jmpl %o0, %o7
223 ba,a 4b
224 /* Clear the startup flag. */
225 5: sethi %hi(_dl_starting_up), %g1
226 or %g1, %lo(_dl_starting_up), %g1
227 ld [%l7+%g1], %g1
228 st %g0, [%g1]
229 /* Pass our finalizer function to the user in %g1. */
230 sethi %hi(_dl_fini), %g1
231 or %g1, %lo(_dl_fini), %g1
232 ld [%l7+%g1], %g1
233 /* Jump to the user's entry point and deallocate the extra stack we got. */
234 jmp %l0
235 add %sp, 6*4, %sp
236 .size _dl_start_user,.-_dl_start_user
237 .previous");
239 static inline void
240 elf_machine_fixup_plt (struct link_map *map, const Elf32_Rela *reloc,
241 Elf32_Addr *reloc_addr, Elf32_Addr value)
243 extern unsigned long _dl_hwcap;
245 /* For thread safety, write the instructions from the bottom and
246 flush before we overwrite the critical "b,a". This of course
247 need not be done during bootstrapping, since there are no threads.
248 But we also can't tell if we _can_ use flush, so don't. */
250 reloc_addr[2] = OPCODE_JMP_G1 | (value & 0x3ff);
251 #ifndef RTLD_BOOTSTRAP
252 if (_dl_hwcap & HWCAP_SPARC_FLUSH)
253 __asm __volatile ("flush %0+8" : : "r"(reloc_addr));
254 #endif
256 reloc_addr[1] = OPCODE_SETHI_G1 | (value >> 10);
257 #ifndef RTLD_BOOTSTRAP
258 if (_dl_hwcap & HWCAP_SPARC_FLUSH)
259 __asm __volatile ("flush %0+4" : : "r"(reloc_addr));
260 #endif
263 /* Return the final value of a plt relocation. */
264 static inline Elf32_Addr
265 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
266 Elf32_Addr value)
268 return value + reloc->r_addend;
271 #ifdef RESOLVE
273 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
274 MAP is the object containing the reloc. */
276 static inline void
277 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
278 const Elf32_Sym *sym, const struct r_found_version *version,
279 Elf32_Addr *const reloc_addr)
281 if (ELF32_R_TYPE (reloc->r_info) == R_SPARC_RELATIVE)
283 #ifndef RTLD_BOOTSTRAP
284 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
285 #endif
286 *reloc_addr += map->l_addr + reloc->r_addend;
288 else
290 const Elf32_Sym *const refsym = sym;
291 Elf32_Addr value;
292 if (sym->st_shndx != SHN_UNDEF &&
293 ELF32_ST_BIND (sym->st_info) == STB_LOCAL)
294 value = map->l_addr;
295 else
297 value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
298 if (sym)
299 value += sym->st_value;
301 value += reloc->r_addend; /* Assume copy relocs have zero addend. */
303 switch (ELF32_R_TYPE (reloc->r_info))
305 case R_SPARC_COPY:
306 #ifndef RTLD_BOOTSTRAP
307 if (sym->st_size > refsym->st_size
308 || (_dl_verbose && sym->st_size < refsym->st_size))
310 extern char **_dl_argv;
311 const char *strtab;
313 strtab = ((void *) map->l_addr
314 + map->l_info[DT_STRTAB]->d_un.d_ptr);
315 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
316 ": Symbol `", strtab + refsym->st_name,
317 "' has different size in shared object, "
318 "consider re-linking\n", NULL);
320 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
321 refsym->st_size));
322 #endif
323 break;
324 case R_SPARC_GLOB_DAT:
325 case R_SPARC_32:
326 *reloc_addr = value;
327 break;
328 case R_SPARC_JMP_SLOT:
329 elf_machine_fixup_plt(map, reloc, reloc_addr, value);
330 break;
331 case R_SPARC_8:
332 *(char *) reloc_addr = value;
333 break;
334 case R_SPARC_16:
335 *(short *) reloc_addr = value;
336 break;
337 case R_SPARC_DISP8:
338 *(char *) reloc_addr = (value - (Elf32_Addr) reloc_addr);
339 break;
340 case R_SPARC_DISP16:
341 *(short *) reloc_addr = (value - (Elf32_Addr) reloc_addr);
342 break;
343 case R_SPARC_DISP32:
344 *reloc_addr = (value - (Elf32_Addr) reloc_addr);
345 break;
346 case R_SPARC_LO10:
347 *reloc_addr = (*reloc_addr & ~0x3ff) | (value & 0x3ff);
348 break;
349 case R_SPARC_WDISP30:
350 *reloc_addr = ((*reloc_addr & 0xc0000000)
351 | ((value - (unsigned int) reloc_addr) >> 2));
352 break;
353 case R_SPARC_HI22:
354 *reloc_addr = (*reloc_addr & 0xffc00000) | (value >> 10);
355 break;
356 case R_SPARC_NONE: /* Alright, Wilbur. */
357 break;
358 default:
359 assert (! "unexpected dynamic reloc type");
360 break;
365 static inline void
366 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
368 switch (ELF32_R_TYPE (reloc->r_info))
370 case R_SPARC_NONE:
371 break;
372 case R_SPARC_JMP_SLOT:
373 break;
374 default:
375 assert (! "unexpected PLT reloc type");
376 break;
380 #endif /* RESOLVE */