Update.
[glibc.git] / sysdeps / sparc / sparc32 / dl-machine.h
blob69054572b82088a9a53e2dcb5ac660a1f19e09f4
1 /* Machine-dependent ELF dynamic relocation inline functions. SPARC version.
2 Copyright (C) 1996-2001, 2002 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #define ELF_MACHINE_NAME "sparc"
22 #include <string.h>
23 #include <sys/param.h>
24 #include <ldsodefs.h>
26 #ifndef VALIDX
27 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
28 + DT_EXTRANUM + DT_VALTAGIDX (tag))
29 #endif
31 /* Some SPARC opcodes we need to use for self-modifying code. */
32 #define OPCODE_NOP 0x01000000 /* nop */
33 #define OPCODE_CALL 0x40000000 /* call ?; add PC-rel word address */
34 #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
35 #define OPCODE_JMP_G1 0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
36 #define OPCODE_SAVE_SP 0x9de3bfa8 /* save %sp, -(16+6)*4, %sp */
37 #define OPCODE_BA 0x30800000 /* b,a ?; add PC-rel word address */
39 /* Protect some broken versions of gcc from misinterpreting weak addresses. */
40 #define WEAKADDR(x) ({ __typeof(x) *_px = &x; \
41 __asm ("" : "=r" (_px) : "0" (_px)); \
42 _px; })
45 /* Use a different preload file when running in 32-bit emulation mode
46 on a 64-bit host. */
47 #define LD_SO_PRELOAD ((GL(dl_hwcap) & HWCAP_SPARC_V9) \
48 ? "/etc/ld.so.preload32" \
49 : "/etc/ld.so.preload")
52 /* Return nonzero iff ELF header is compatible with the running host. */
53 static inline int
54 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
56 if (ehdr->e_machine == EM_SPARC)
57 return 1;
58 else if (ehdr->e_machine == EM_SPARC32PLUS)
60 unsigned long *hwcap;
61 #ifndef SHARED
62 weak_extern (_dl_hwcap);
63 weak_extern (_dl_hwcap_mask);
64 #endif
66 hwcap = WEAKADDR (GL(dl_hwcap));
67 /* XXX The following is wrong! Dave Miller rejected to implement it
68 correctly. If this causes problems shoot *him*! */
69 return hwcap == NULL || (*hwcap & GL(dl_hwcap_mask) & HWCAP_SPARC_V9);
71 else
72 return 0;
76 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
77 first element of the GOT. This must be inlined in a function which
78 uses global data. */
79 static inline Elf32_Addr
80 elf_machine_dynamic (void)
82 register Elf32_Addr *got asm ("%l7");
83 return *got;
86 /* Return the run-time load address of the shared object. */
87 static inline Elf32_Addr
88 elf_machine_load_address (void)
90 register Elf32_Addr pc __asm("%o7"), pic __asm("%l7"), got;
92 /* Utilize the fact that a local .got entry will be partially
93 initialized at startup awaiting its RELATIVE fixup. */
95 __asm("sethi %%hi(.Load_address),%1\n"
96 ".Load_address:\n\t"
97 "call 1f\n\t"
98 "or %1,%%lo(.Load_address),%1\n"
99 "1:\tld [%2+%1],%1"
100 : "=r"(pc), "=r"(got) : "r"(pic));
102 return pc - got;
105 /* Set up the loaded object described by L so its unrelocated PLT
106 entries will jump to the on-demand fixup code in dl-runtime.c. */
108 static inline int
109 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
111 Elf32_Addr *plt;
112 extern void _dl_runtime_resolve (Elf32_Word);
113 extern void _dl_runtime_profile (Elf32_Word);
115 if (l->l_info[DT_JMPREL] && lazy)
117 Elf32_Addr rfunc;
119 /* The entries for functions in the PLT have not yet been filled in.
120 Their initial contents will arrange when called to set the high 22
121 bits of %g1 with an offset into the .rela.plt section and jump to
122 the beginning of the PLT. */
123 plt = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
124 if (! profile)
125 rfunc = (Elf32_Addr) &_dl_runtime_resolve;
126 else
128 rfunc = (Elf32_Addr) &_dl_runtime_profile;
130 if (_dl_name_match_p (GL(dl_profile), l))
131 GL(dl_profile_map) = l;
134 /* The beginning of the PLT does:
136 save %sp, -64, %sp
137 pltpc: call _dl_runtime_resolve
139 .word MAP
141 This saves the register window containing the arguments, and the
142 PC value (pltpc) implicitly saved in %o7 by the call points near the
143 location where we store the link_map pointer for this object. */
145 plt[0] = OPCODE_SAVE_SP;
146 /* Construct PC-relative word address. */
147 plt[1] = OPCODE_CALL | ((rfunc - (Elf32_Addr) &plt[1]) >> 2);
148 plt[2] = OPCODE_NOP; /* Fill call delay slot. */
149 plt[3] = (Elf32_Addr) l;
150 if (__builtin_expect (l->l_info[VALIDX(DT_GNU_PRELINKED)] != NULL, 0)
151 || __builtin_expect (l->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL, 0))
153 /* Need to reinitialize .plt to undo prelinking. */
154 unsigned long *hwcap;
155 int do_flush;
156 Elf32_Rela *rela = (Elf32_Rela *) D_PTR (l, l_info[DT_JMPREL]);
157 Elf32_Rela *relaend
158 = (Elf32_Rela *) ((char *) rela
159 + l->l_info[DT_PLTRELSZ]->d_un.d_val);
160 #ifndef SHARED
161 weak_extern (_dl_hwcap);
162 #endif
163 hwcap = WEAKADDR (GL(dl_hwcap));
164 do_flush = (!hwcap || (*hwcap & HWCAP_SPARC_FLUSH));
166 /* prelink must ensure there are no R_SPARC_NONE relocs left
167 in .rela.plt. */
168 while (rela < relaend)
170 *(unsigned int *) rela->r_offset
171 = OPCODE_SETHI_G1 | (rela->r_offset - (Elf32_Addr) plt);
172 *(unsigned int *) (rela->r_offset + 4)
173 = OPCODE_BA | ((((Elf32_Addr) plt
174 - rela->r_offset - 4) >> 2) & 0x3fffff);
175 if (do_flush)
177 __asm __volatile ("flush %0" : : "r"(rela->r_offset));
178 __asm __volatile ("flush %0+4" : : "r"(rela->r_offset));
180 ++rela;
185 return lazy;
188 /* This code is used in dl-runtime.c to call the `fixup' function
189 and then redirect to the address it returns. */
190 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name) \
191 asm ( "\
192 .text
193 .globl " #tramp_name "
194 .type " #tramp_name ", @function
195 .align 32
196 " #tramp_name ":
197 /* Set up the arguments to fixup --
198 %o0 = link_map out of plt0
199 %o1 = offset of reloc entry
200 %o2 = return address */
201 ld [%o7 + 8], %o0
202 srl %g1, 10, %o1
203 mov %i7, %o2
204 call " #fixup_name "
205 sub %o1, 4*12, %o1
206 jmp %o0
207 restore
208 .size " #tramp_name ", . - " #tramp_name "
209 .previous")
211 #ifndef PROF
212 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
213 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
214 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup);
215 #else
216 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
217 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
218 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, fixup);
219 #endif
221 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
222 PLT entries should not be allowed to define the value.
223 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
224 of the main executable's symbols, as for a COPY reloc. */
225 #define elf_machine_type_class(type) \
226 ((((type) == R_SPARC_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
227 | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
229 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
230 #define ELF_MACHINE_JMP_SLOT R_SPARC_JMP_SLOT
232 /* The SPARC never uses Elf32_Rel relocations. */
233 #define ELF_MACHINE_NO_REL 1
235 /* The SPARC overlaps DT_RELA and DT_PLTREL. */
236 #define ELF_MACHINE_PLTREL_OVERLAP 1
238 /* Initial entry point code for the dynamic linker.
239 The C function `_dl_start' is the real entry point;
240 its return value is the user program's entry point. */
242 #define RTLD_START __asm__ ("\
243 .text
244 .globl _start
245 .type _start, @function
246 .align 32
247 _start:
248 /* Allocate space for functions to drop their arguments. */
249 sub %sp, 6*4, %sp
250 /* Pass pointer to argument block to _dl_start. */
251 call _dl_start
252 add %sp, 22*4, %o0
253 /* FALTHRU */
254 .globl _dl_start_user
255 .type _dl_start_user, @function
256 _dl_start_user:
257 /* Load the PIC register. */
258 1: call 2f
259 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7
260 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7
261 add %l7, %o7, %l7
262 /* Save the user entry point address in %l0 */
263 mov %o0, %l0
264 /* Store the highest stack address. */
265 sethi %hi(__libc_stack_end), %g2
266 or %g2, %lo(__libc_stack_end), %g2
267 ld [%l7 + %g2], %l1
268 sethi %hi(_dl_skip_args), %g2
269 add %sp, 6*4, %l2
270 or %g2, %lo(_dl_skip_args), %g2
271 st %l2, [%l1]
272 /* See if we were run as a command with the executable file name as an
273 extra leading argument. If so, adjust the contents of the stack. */
274 ld [%l7+%g2], %i0
275 ld [%i0], %i0
276 tst %i0
277 beq 3f
278 ld [%sp+22*4], %i5 /* load argc */
279 /* Find out how far to shift. */
280 sethi %hi(_dl_argv), %l3
281 or %l3, %lo(_dl_argv), %l3
282 ld [%l7+%l3], %l3
283 sub %i5, %i0, %i5
284 ld [%l3], %l4
285 sll %i0, 2, %i2
286 st %i5, [%sp+22*4]
287 sub %l4, %i2, %l4
288 add %sp, 23*4, %i1
289 add %i1, %i2, %i2
290 st %l4, [%l3]
291 /* Copy down argv */
292 21: ld [%i2], %i3
293 add %i2, 4, %i2
294 tst %i3
295 st %i3, [%i1]
296 bne 21b
297 add %i1, 4, %i1
298 /* Copy down env */
299 22: ld [%i2], %i3
300 add %i2, 4, %i2
301 tst %i3
302 st %i3, [%i1]
303 bne 22b
304 add %i1, 4, %i1
305 /* Copy down auxiliary table. */
306 23: ld [%i2], %i3
307 ld [%i2+4], %i4
308 add %i2, 8, %i2
309 tst %i3
310 st %i3, [%i1]
311 st %i4, [%i1+4]
312 bne 23b
313 add %i1, 8, %i1
314 /* %o0 = _dl_loaded, %o1 = argc, %o2 = argv, %o3 = envp. */
315 3: sethi %hi(_rtld_global), %o0
316 add %sp, 23*4, %o2
317 orcc %o0, %lo(_rtld_global), %o0
318 sll %i5, 2, %o3
319 ld [%l7+%o0], %o0
320 add %o3, 4, %o3
321 mov %i5, %o1
322 add %o2, %o3, %o3
323 call _dl_init_internal
324 ld [%o0], %o0
325 /* Pass our finalizer function to the user in %g1. */
326 sethi %hi(_dl_fini), %g1
327 or %g1, %lo(_dl_fini), %g1
328 ld [%l7+%g1], %g1
329 /* Jump to the user's entry point and deallocate the extra stack we got. */
330 jmp %l0
331 add %sp, 6*4, %sp
332 .size _dl_start_user, . - _dl_start_user
333 .previous");
335 static inline Elf32_Addr
336 sparc_fixup_plt (const Elf32_Rela *reloc, Elf32_Addr *reloc_addr,
337 Elf32_Addr value, int t)
339 Elf32_Sword disp = value - (Elf32_Addr) reloc_addr;
340 #ifndef RTLD_BOOTSTRAP
341 /* Note that we don't mask the hwcap here, as the flush is essential to
342 functionality on those cpu's that implement it. */
343 unsigned long *hwcap;
344 int do_flush;
345 # ifndef SHARED
346 weak_extern (_dl_hwcap);
347 # endif
348 hwcap = WEAKADDR (GL(dl_hwcap));
349 do_flush = (!hwcap || (*hwcap & HWCAP_SPARC_FLUSH));
350 #else
351 /* Unfortunately, this is necessary, so that we can ensure
352 ld.so will not execute corrupt PLT entry instructions. */
353 const int do_flush = 1;
354 #endif
356 if (0 && disp >= -0x800000 && disp < 0x800000)
358 /* Don't need to worry about thread safety. We're writing just one
359 instruction. */
361 reloc_addr[0] = OPCODE_BA | ((disp >> 2) & 0x3fffff);
362 if (do_flush)
363 __asm __volatile ("flush %0" : : "r"(reloc_addr));
365 else
367 /* For thread safety, write the instructions from the bottom and
368 flush before we overwrite the critical "b,a". This of course
369 need not be done during bootstrapping, since there are no threads.
370 But we also can't tell if we _can_ use flush, so don't. */
372 reloc_addr += t;
373 reloc_addr[1] = OPCODE_JMP_G1 | (value & 0x3ff);
374 if (do_flush)
375 __asm __volatile ("flush %0+4" : : "r"(reloc_addr));
377 reloc_addr[0] = OPCODE_SETHI_G1 | (value >> 10);
378 if (do_flush)
379 __asm __volatile ("flush %0" : : "r"(reloc_addr));
382 return value;
385 static inline Elf32_Addr
386 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
387 const Elf32_Rela *reloc,
388 Elf32_Addr *reloc_addr, Elf32_Addr value)
390 return sparc_fixup_plt (reloc, reloc_addr, value, 1);
393 /* Return the final value of a plt relocation. */
394 static inline Elf32_Addr
395 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
396 Elf32_Addr value)
398 return value + reloc->r_addend;
401 #ifdef RESOLVE
403 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
404 MAP is the object containing the reloc. */
406 static inline void
407 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
408 const Elf32_Sym *sym, const struct r_found_version *version,
409 Elf32_Addr *const reloc_addr)
411 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
413 #if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
414 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
415 reference weak so static programs can still link. This declaration
416 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
417 because rtld.c contains the common defn for _dl_rtld_map, which is
418 incompatible with a weak decl in the same file. */
419 weak_extern (_dl_rtld_map);
420 #endif
422 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
423 if (__builtin_expect (r_type == R_SPARC_RELATIVE, 0))
425 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
426 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
427 # endif
428 *reloc_addr += map->l_addr + reloc->r_addend;
430 else
431 #endif
433 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
434 const Elf32_Sym *const refsym = sym;
435 #endif
436 Elf32_Addr value;
437 #ifndef RESOLVE_CONFLICT_FIND_MAP
438 if (sym->st_shndx != SHN_UNDEF &&
439 ELF32_ST_BIND (sym->st_info) == STB_LOCAL)
440 value = map->l_addr;
441 else
443 value = RESOLVE (&sym, version, r_type);
444 if (sym)
445 value += sym->st_value;
447 #else
448 value = 0;
449 #endif
450 value += reloc->r_addend; /* Assume copy relocs have zero addend. */
452 switch (r_type)
454 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
455 case R_SPARC_COPY:
456 if (sym == NULL)
457 /* This can happen in trace mode if an object could not be
458 found. */
459 break;
460 if (sym->st_size > refsym->st_size
461 || (GL(dl_verbose) && sym->st_size < refsym->st_size))
463 extern char **_dl_argv;
464 const char *strtab;
466 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
467 _dl_error_printf ("\
468 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
469 _dl_argv[0] ?: "<program name unknown>",
470 strtab + refsym->st_name);
472 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
473 refsym->st_size));
474 break;
475 #endif
476 case R_SPARC_GLOB_DAT:
477 case R_SPARC_32:
478 *reloc_addr = value;
479 break;
480 case R_SPARC_JMP_SLOT:
481 /* At this point we don't need to bother with thread safety,
482 so we can optimize the first instruction of .plt out. */
483 sparc_fixup_plt (reloc, reloc_addr, value, 0);
484 break;
485 #ifndef RTLD_BOOTSTRAP
486 case R_SPARC_8:
487 *(char *) reloc_addr = value;
488 break;
489 case R_SPARC_16:
490 *(short *) reloc_addr = value;
491 break;
492 case R_SPARC_DISP8:
493 *(char *) reloc_addr = (value - (Elf32_Addr) reloc_addr);
494 break;
495 case R_SPARC_DISP16:
496 *(short *) reloc_addr = (value - (Elf32_Addr) reloc_addr);
497 break;
498 case R_SPARC_DISP32:
499 *reloc_addr = (value - (Elf32_Addr) reloc_addr);
500 break;
501 case R_SPARC_LO10:
502 *reloc_addr = (*reloc_addr & ~0x3ff) | (value & 0x3ff);
503 break;
504 case R_SPARC_WDISP30:
505 *reloc_addr = ((*reloc_addr & 0xc0000000)
506 | ((value - (unsigned int) reloc_addr) >> 2));
507 break;
508 case R_SPARC_HI22:
509 *reloc_addr = (*reloc_addr & 0xffc00000) | (value >> 10);
510 break;
511 case R_SPARC_UA16:
512 ((unsigned char *) reloc_addr) [0] = value >> 8;
513 ((unsigned char *) reloc_addr) [1] = value;
514 break;
515 case R_SPARC_UA32:
516 ((unsigned char *) reloc_addr) [0] = value >> 24;
517 ((unsigned char *) reloc_addr) [1] = value >> 16;
518 ((unsigned char *) reloc_addr) [2] = value >> 8;
519 ((unsigned char *) reloc_addr) [3] = value;
520 break;
521 #endif
522 case R_SPARC_NONE: /* Alright, Wilbur. */
523 break;
524 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
525 default:
526 _dl_reloc_bad_type (map, r_type, 0);
527 break;
528 #endif
533 static inline void
534 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
535 Elf32_Addr *const reloc_addr)
537 *reloc_addr += l_addr + reloc->r_addend;
540 static inline void
541 elf_machine_lazy_rel (struct link_map *map,
542 Elf32_Addr l_addr, const Elf32_Rela *reloc)
544 switch (ELF32_R_TYPE (reloc->r_info))
546 case R_SPARC_NONE:
547 break;
548 case R_SPARC_JMP_SLOT:
549 break;
550 default:
551 _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 1);
552 break;
556 #endif /* RESOLVE */