2002-09-24 Jakub Jelinek <jakub@redhat.com>
[glibc.git] / sysdeps / sparc / sparc32 / dl-machine.h
blob51060b652ad9372deff96dfba0d4ac360474c219
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;
75 /* We have to do this because elf_machine_{dynamic,load_address} can be
76 invoked from functions that have no GOT references, and thus the compiler
77 has no obligation to load the PIC register. */
78 #define LOAD_PIC_REG(PIC_REG) \
79 do { register Elf32_Addr pc __asm("o7"); \
80 __asm("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t" \
81 "call 1f\n\t" \
82 "add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n" \
83 "1:\tadd %1, %0, %1" \
84 : "=r" (pc), "=r" (PIC_REG)); \
85 } while (0)
87 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
88 first element of the GOT. This must be inlined in a function which
89 uses global data. */
90 static inline Elf32_Addr
91 elf_machine_dynamic (void)
93 register Elf32_Addr *got asm ("%l7");
95 LOAD_PIC_REG (got);
97 return *got;
100 /* Return the run-time load address of the shared object. */
101 static inline Elf32_Addr
102 elf_machine_load_address (void)
104 register Elf32_Addr *pc __asm ("%o7"), *got __asm ("%l7");
106 __asm ("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t"
107 "call 1f\n\t"
108 " add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n\t"
109 "call _DYNAMIC\n\t"
110 "call _GLOBAL_OFFSET_TABLE_\n"
111 "1:\tadd %1, %0, %1\n\t" : "=r" (pc), "=r" (got));
113 /* got is now l_addr + _GLOBAL_OFFSET_TABLE_
114 *got is _DYNAMIC
115 pc[2]*4 is l_addr + _DYNAMIC - (long)pc - 8
116 pc[3]*4 is l_addr + _GLOBAL_OFFSET_TABLE_ - (long)pc - 12 */
117 return (Elf32_Addr) got - *got + (pc[2] - pc[3]) * 4 - 4;
120 /* Set up the loaded object described by L so its unrelocated PLT
121 entries will jump to the on-demand fixup code in dl-runtime.c. */
123 static inline int
124 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
126 Elf32_Addr *plt;
127 extern void _dl_runtime_resolve (Elf32_Word);
128 extern void _dl_runtime_profile (Elf32_Word);
130 if (l->l_info[DT_JMPREL] && lazy)
132 Elf32_Addr rfunc;
134 /* The entries for functions in the PLT have not yet been filled in.
135 Their initial contents will arrange when called to set the high 22
136 bits of %g1 with an offset into the .rela.plt section and jump to
137 the beginning of the PLT. */
138 plt = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
139 if (! profile)
140 rfunc = (Elf32_Addr) &_dl_runtime_resolve;
141 else
143 rfunc = (Elf32_Addr) &_dl_runtime_profile;
145 if (_dl_name_match_p (GL(dl_profile), l))
146 GL(dl_profile_map) = l;
149 /* The beginning of the PLT does:
151 save %sp, -64, %sp
152 pltpc: call _dl_runtime_resolve
154 .word MAP
156 This saves the register window containing the arguments, and the
157 PC value (pltpc) implicitly saved in %o7 by the call points near the
158 location where we store the link_map pointer for this object. */
160 plt[0] = OPCODE_SAVE_SP;
161 /* Construct PC-relative word address. */
162 plt[1] = OPCODE_CALL | ((rfunc - (Elf32_Addr) &plt[1]) >> 2);
163 plt[2] = OPCODE_NOP; /* Fill call delay slot. */
164 plt[3] = (Elf32_Addr) l;
165 if (__builtin_expect (l->l_info[VALIDX(DT_GNU_PRELINKED)] != NULL, 0)
166 || __builtin_expect (l->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL, 0))
168 /* Need to reinitialize .plt to undo prelinking. */
169 unsigned long *hwcap;
170 int do_flush;
171 Elf32_Rela *rela = (Elf32_Rela *) D_PTR (l, l_info[DT_JMPREL]);
172 Elf32_Rela *relaend
173 = (Elf32_Rela *) ((char *) rela
174 + l->l_info[DT_PLTRELSZ]->d_un.d_val);
175 #ifndef SHARED
176 weak_extern (_dl_hwcap);
177 #endif
178 hwcap = WEAKADDR (GL(dl_hwcap));
179 do_flush = (!hwcap || (*hwcap & HWCAP_SPARC_FLUSH));
181 /* prelink must ensure there are no R_SPARC_NONE relocs left
182 in .rela.plt. */
183 while (rela < relaend)
185 *(unsigned int *) rela->r_offset
186 = OPCODE_SETHI_G1 | (rela->r_offset - (Elf32_Addr) plt);
187 *(unsigned int *) (rela->r_offset + 4)
188 = OPCODE_BA | ((((Elf32_Addr) plt
189 - rela->r_offset - 4) >> 2) & 0x3fffff);
190 if (do_flush)
192 __asm __volatile ("flush %0" : : "r"(rela->r_offset));
193 __asm __volatile ("flush %0+4" : : "r"(rela->r_offset));
195 ++rela;
200 return lazy;
203 /* This code is used in dl-runtime.c to call the `fixup' function
204 and then redirect to the address it returns. */
205 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name) \
206 asm ( "\
207 .text\n\
208 .globl " #tramp_name "\n\
209 .type " #tramp_name ", @function\n\
210 .align 32\n\
211 " #tramp_name ":\n\
212 /* Set up the arguments to fixup --\n\
213 %o0 = link_map out of plt0\n\
214 %o1 = offset of reloc entry\n\
215 %o2 = return address */\n\
216 ld [%o7 + 8], %o0\n\
217 srl %g1, 10, %o1\n\
218 mov %i7, %o2\n\
219 call " #fixup_name "\n\
220 sub %o1, 4*12, %o1\n\
221 jmp %o0\n\
222 restore\n\
223 .size " #tramp_name ", . - " #tramp_name "\n\
224 .previous")
226 #ifndef PROF
227 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
228 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
229 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup);
230 #else
231 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
232 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
233 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, fixup);
234 #endif
236 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
237 PLT entries should not be allowed to define the value.
238 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
239 of the main executable's symbols, as for a COPY reloc. */
240 #define elf_machine_type_class(type) \
241 ((((type) == R_SPARC_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
242 | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
244 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
245 #define ELF_MACHINE_JMP_SLOT R_SPARC_JMP_SLOT
247 /* The SPARC never uses Elf32_Rel relocations. */
248 #define ELF_MACHINE_NO_REL 1
250 /* The SPARC overlaps DT_RELA and DT_PLTREL. */
251 #define ELF_MACHINE_PLTREL_OVERLAP 1
253 /* Initial entry point code for the dynamic linker.
254 The C function `_dl_start' is the real entry point;
255 its return value is the user program's entry point. */
257 #define RTLD_START __asm__ ("\
258 .text\n\
259 .globl _start\n\
260 .type _start, @function\n\
261 .align 32\n\
262 _start:\n\
263 /* Allocate space for functions to drop their arguments. */\n\
264 sub %sp, 6*4, %sp\n\
265 /* Pass pointer to argument block to _dl_start. */\n\
266 call _dl_start\n\
267 add %sp, 22*4, %o0\n\
268 /* FALTHRU */\n\
269 .globl _dl_start_user\n\
270 .type _dl_start_user, @function\n\
271 _dl_start_user:\n\
272 /* Load the PIC register. */\n\
273 1: call 2f\n\
274 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n\
275 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n\
276 add %l7, %o7, %l7\n\
277 /* Save the user entry point address in %l0 */\n\
278 mov %o0, %l0\n\
279 /* Store the highest stack address. */\n\
280 sethi %hi(__libc_stack_end), %g2\n\
281 or %g2, %lo(__libc_stack_end), %g2\n\
282 ld [%l7 + %g2], %l1\n\
283 sethi %hi(_dl_skip_args), %g2\n\
284 add %sp, 6*4, %l2\n\
285 or %g2, %lo(_dl_skip_args), %g2\n\
286 st %l2, [%l1]\n\
287 /* See if we were run as a command with the executable file name as an\n\
288 extra leading argument. If so, adjust the contents of the stack. */\n\
289 ld [%l7+%g2], %i0\n\
290 ld [%i0], %i0\n\
291 tst %i0\n\
292 beq 3f\n\
293 ld [%sp+22*4], %i5 /* load argc */\n\
294 /* Find out how far to shift. */\n\
295 sethi %hi(_dl_argv), %l3\n\
296 or %l3, %lo(_dl_argv), %l3\n\
297 ld [%l7+%l3], %l3\n\
298 sub %i5, %i0, %i5\n\
299 ld [%l3], %l4\n\
300 sll %i0, 2, %i2\n\
301 st %i5, [%sp+22*4]\n\
302 sub %l4, %i2, %l4\n\
303 add %sp, 23*4, %i1\n\
304 add %i1, %i2, %i2\n\
305 st %l4, [%l3]\n\
306 /* Copy down argv */\n\
307 21: ld [%i2], %i3\n\
308 add %i2, 4, %i2\n\
309 tst %i3\n\
310 st %i3, [%i1]\n\
311 bne 21b\n\
312 add %i1, 4, %i1\n\
313 /* Copy down env */\n\
314 22: ld [%i2], %i3\n\
315 add %i2, 4, %i2\n\
316 tst %i3\n\
317 st %i3, [%i1]\n\
318 bne 22b\n\
319 add %i1, 4, %i1\n\
320 /* Copy down auxiliary table. */\n\
321 23: ld [%i2], %i3\n\
322 ld [%i2+4], %i4\n\
323 add %i2, 8, %i2\n\
324 tst %i3\n\
325 st %i3, [%i1]\n\
326 st %i4, [%i1+4]\n\
327 bne 23b\n\
328 add %i1, 8, %i1\n\
329 /* %o0 = _dl_loaded, %o1 = argc, %o2 = argv, %o3 = envp. */\n\
330 3: sethi %hi(_rtld_local), %o0\n\
331 add %sp, 23*4, %o2\n\
332 orcc %o0, %lo(_rtld_local), %o0\n\
333 sll %i5, 2, %o3\n\
334 ld [%l7+%o0], %o0\n\
335 add %o3, 4, %o3\n\
336 mov %i5, %o1\n\
337 add %o2, %o3, %o3\n\
338 call _dl_init_internal\n\
339 ld [%o0], %o0\n\
340 /* Pass our finalizer function to the user in %g1. */\n\
341 sethi %hi(_dl_fini), %g1\n\
342 or %g1, %lo(_dl_fini), %g1\n\
343 ld [%l7+%g1], %g1\n\
344 /* Jump to the user's entry point and deallocate the extra stack we got. */\n\
345 jmp %l0\n\
346 add %sp, 6*4, %sp\n\
347 .size _dl_start_user, . - _dl_start_user\n\
348 .previous");
350 static inline Elf32_Addr
351 sparc_fixup_plt (const Elf32_Rela *reloc, Elf32_Addr *reloc_addr,
352 Elf32_Addr value, int t)
354 Elf32_Sword disp = value - (Elf32_Addr) reloc_addr;
355 #ifndef RTLD_BOOTSTRAP
356 /* Note that we don't mask the hwcap here, as the flush is essential to
357 functionality on those cpu's that implement it. */
358 unsigned long *hwcap;
359 int do_flush;
360 # ifndef SHARED
361 weak_extern (_dl_hwcap);
362 # endif
363 hwcap = WEAKADDR (GL(dl_hwcap));
364 do_flush = (!hwcap || (*hwcap & HWCAP_SPARC_FLUSH));
365 #else
366 /* Unfortunately, this is necessary, so that we can ensure
367 ld.so will not execute corrupt PLT entry instructions. */
368 const int do_flush = 1;
369 #endif
371 if (0 && disp >= -0x800000 && disp < 0x800000)
373 /* Don't need to worry about thread safety. We're writing just one
374 instruction. */
376 reloc_addr[0] = OPCODE_BA | ((disp >> 2) & 0x3fffff);
377 if (do_flush)
378 __asm __volatile ("flush %0" : : "r"(reloc_addr));
380 else
382 /* For thread safety, write the instructions from the bottom and
383 flush before we overwrite the critical "b,a". This of course
384 need not be done during bootstrapping, since there are no threads.
385 But we also can't tell if we _can_ use flush, so don't. */
387 reloc_addr += t;
388 reloc_addr[1] = OPCODE_JMP_G1 | (value & 0x3ff);
389 if (do_flush)
390 __asm __volatile ("flush %0+4" : : "r"(reloc_addr));
392 reloc_addr[0] = OPCODE_SETHI_G1 | (value >> 10);
393 if (do_flush)
394 __asm __volatile ("flush %0" : : "r"(reloc_addr));
397 return value;
400 static inline Elf32_Addr
401 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
402 const Elf32_Rela *reloc,
403 Elf32_Addr *reloc_addr, Elf32_Addr value)
405 return sparc_fixup_plt (reloc, reloc_addr, value, 1);
408 /* Return the final value of a plt relocation. */
409 static inline Elf32_Addr
410 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
411 Elf32_Addr value)
413 return value + reloc->r_addend;
416 #ifdef RESOLVE
418 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
419 MAP is the object containing the reloc. */
421 static inline void
422 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
423 const Elf32_Sym *sym, const struct r_found_version *version,
424 Elf32_Addr *const reloc_addr)
426 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
428 #if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
429 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
430 reference weak so static programs can still link. This declaration
431 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
432 because rtld.c contains the common defn for _dl_rtld_map, which is
433 incompatible with a weak decl in the same file. */
434 weak_extern (_dl_rtld_map);
435 #endif
437 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
438 if (__builtin_expect (r_type == R_SPARC_RELATIVE, 0))
440 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
441 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
442 # endif
443 *reloc_addr += map->l_addr + reloc->r_addend;
445 else
446 #endif
448 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
449 const Elf32_Sym *const refsym = sym;
450 #endif
451 Elf32_Addr value;
452 #ifndef RESOLVE_CONFLICT_FIND_MAP
453 if (sym->st_shndx != SHN_UNDEF &&
454 ELF32_ST_BIND (sym->st_info) == STB_LOCAL)
455 value = map->l_addr;
456 else
458 value = RESOLVE (&sym, version, r_type);
459 if (sym)
460 value += sym->st_value;
462 #else
463 value = 0;
464 #endif
465 value += reloc->r_addend; /* Assume copy relocs have zero addend. */
467 switch (r_type)
469 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
470 case R_SPARC_COPY:
471 if (sym == NULL)
472 /* This can happen in trace mode if an object could not be
473 found. */
474 break;
475 if (sym->st_size > refsym->st_size
476 || (GL(dl_verbose) && sym->st_size < refsym->st_size))
478 const char *strtab;
480 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
481 _dl_error_printf ("\
482 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
483 rtld_progname ?: "<program name unknown>",
484 strtab + refsym->st_name);
486 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
487 refsym->st_size));
488 break;
489 #endif
490 case R_SPARC_GLOB_DAT:
491 case R_SPARC_32:
492 *reloc_addr = value;
493 break;
494 case R_SPARC_JMP_SLOT:
495 /* At this point we don't need to bother with thread safety,
496 so we can optimize the first instruction of .plt out. */
497 sparc_fixup_plt (reloc, reloc_addr, value, 0);
498 break;
499 #ifndef RTLD_BOOTSTRAP
500 case R_SPARC_8:
501 *(char *) reloc_addr = value;
502 break;
503 case R_SPARC_16:
504 *(short *) reloc_addr = value;
505 break;
506 case R_SPARC_DISP8:
507 *(char *) reloc_addr = (value - (Elf32_Addr) reloc_addr);
508 break;
509 case R_SPARC_DISP16:
510 *(short *) reloc_addr = (value - (Elf32_Addr) reloc_addr);
511 break;
512 case R_SPARC_DISP32:
513 *reloc_addr = (value - (Elf32_Addr) reloc_addr);
514 break;
515 case R_SPARC_LO10:
516 *reloc_addr = (*reloc_addr & ~0x3ff) | (value & 0x3ff);
517 break;
518 case R_SPARC_WDISP30:
519 *reloc_addr = ((*reloc_addr & 0xc0000000)
520 | ((value - (unsigned int) reloc_addr) >> 2));
521 break;
522 case R_SPARC_HI22:
523 *reloc_addr = (*reloc_addr & 0xffc00000) | (value >> 10);
524 break;
525 case R_SPARC_UA16:
526 ((unsigned char *) reloc_addr) [0] = value >> 8;
527 ((unsigned char *) reloc_addr) [1] = value;
528 break;
529 case R_SPARC_UA32:
530 ((unsigned char *) reloc_addr) [0] = value >> 24;
531 ((unsigned char *) reloc_addr) [1] = value >> 16;
532 ((unsigned char *) reloc_addr) [2] = value >> 8;
533 ((unsigned char *) reloc_addr) [3] = value;
534 break;
535 #endif
536 case R_SPARC_NONE: /* Alright, Wilbur. */
537 break;
538 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
539 default:
540 _dl_reloc_bad_type (map, r_type, 0);
541 break;
542 #endif
547 static inline void
548 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
549 Elf32_Addr *const reloc_addr)
551 *reloc_addr += l_addr + reloc->r_addend;
554 static inline void
555 elf_machine_lazy_rel (struct link_map *map,
556 Elf32_Addr l_addr, const Elf32_Rela *reloc)
558 switch (ELF32_R_TYPE (reloc->r_info))
560 case R_SPARC_NONE:
561 break;
562 case R_SPARC_JMP_SLOT:
563 break;
564 default:
565 _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 1);
566 break;
570 #endif /* RESOLVE */