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
20 #define ELF_MACHINE_NAME "sparc"
23 #include <sys/param.h>
27 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
28 + DT_EXTRANUM + DT_VALTAGIDX (tag))
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)); \
45 /* Use a different preload file when running in 32-bit emulation mode
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. */
54 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
56 if (ehdr
->e_machine
== EM_SPARC
)
58 else if (ehdr
->e_machine
== EM_SPARC32PLUS
)
62 weak_extern (_dl_hwcap
);
63 weak_extern (_dl_hwcap_mask
);
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
);
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
79 static inline Elf32_Addr
80 elf_machine_dynamic (void)
82 register Elf32_Addr
*got
asm ("%l7");
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"
98 "or %1,%%lo(.Load_address),%1\n"
100 : "=r"(pc
), "=r"(got
) : "r"(pic
));
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. */
109 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
112 extern void _dl_runtime_resolve (Elf32_Word
);
113 extern void _dl_runtime_profile (Elf32_Word
);
115 if (l
->l_info
[DT_JMPREL
] && lazy
)
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
]);
125 rfunc
= (Elf32_Addr
) &_dl_runtime_resolve
;
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:
137 pltpc: call _dl_runtime_resolve
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
;
156 Elf32_Rela
*rela
= (Elf32_Rela
*) D_PTR (l
, l_info
[DT_JMPREL
]);
158 = (Elf32_Rela
*) ((char *) rela
159 + l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
);
161 weak_extern (_dl_hwcap
);
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
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);
177 __asm
__volatile ("flush %0" : : "r"(rela
->r_offset
));
178 __asm
__volatile ("flush %0+4" : : "r"(rela
->r_offset
));
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) \
193 .globl " #tramp_name "
194 .type " #tramp_name ", @function
197 /* Set up the arguments to fixup --
198 %o0 = link_map out of plt0
199 %o1 = offset of reloc entry
200 %o2 = return address */
208 .size " #tramp_name ", . - " #tramp_name "
212 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
213 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
214 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup);
216 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
217 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
218 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, fixup);
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__ ("\
245 .type _start, @function
248 /* Allocate space for functions to drop their arguments. */
250 /* Pass pointer to argument block to _dl_start. */
254 .globl _dl_start_user
255 .type _dl_start_user, @function
257 /* Load the PIC register. */
259 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7
260 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7
262 /* Save the user entry point address in %l0 */
264 /* Store the highest stack address. */
265 sethi %hi(__libc_stack_end), %g2
266 or %g2, %lo(__libc_stack_end), %g2
268 sethi %hi(_dl_skip_args), %g2
270 or %g2, %lo(_dl_skip_args), %g2
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. */
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
305 /* Copy down auxiliary table. */
314 /* %o0 = _dl_loaded, %o1 = argc, %o2 = argv, %o3 = envp. */
315 3: sethi %hi(_rtld_global), %o0
317 orcc %o0, %lo(_rtld_global), %o0
323 call _dl_init_internal
325 /* Pass our finalizer function to the user in %g1. */
326 sethi %hi(_dl_fini), %g1
327 or %g1, %lo(_dl_fini), %g1
329 /* Jump to the user's entry point and deallocate the extra stack we got. */
332 .size _dl_start_user, . - _dl_start_user
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
;
346 weak_extern (_dl_hwcap
);
348 hwcap
= WEAKADDR (GL(dl_hwcap
));
349 do_flush
= (!hwcap
|| (*hwcap
& HWCAP_SPARC_FLUSH
));
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;
356 if (0 && disp
>= -0x800000 && disp
< 0x800000)
358 /* Don't need to worry about thread safety. We're writing just one
361 reloc_addr
[0] = OPCODE_BA
| ((disp
>> 2) & 0x3fffff);
363 __asm
__volatile ("flush %0" : : "r"(reloc_addr
));
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. */
373 reloc_addr
[1] = OPCODE_JMP_G1
| (value
& 0x3ff);
375 __asm
__volatile ("flush %0+4" : : "r"(reloc_addr
));
377 reloc_addr
[0] = OPCODE_SETHI_G1
| (value
>> 10);
379 __asm
__volatile ("flush %0" : : "r"(reloc_addr
));
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
,
398 return value
+ reloc
->r_addend
;
403 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
404 MAP is the object containing the reloc. */
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
);
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. */
428 *reloc_addr
+= map
->l_addr
+ reloc
->r_addend
;
433 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
434 const Elf32_Sym
*const refsym
= sym
;
437 #ifndef RESOLVE_CONFLICT_FIND_MAP
438 if (sym
->st_shndx
!= SHN_UNDEF
&&
439 ELF32_ST_BIND (sym
->st_info
) == STB_LOCAL
)
443 value
= RESOLVE (&sym
, version
, r_type
);
445 value
+= sym
->st_value
;
450 value
+= reloc
->r_addend
; /* Assume copy relocs have zero addend. */
454 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
457 /* This can happen in trace mode if an object could not be
460 if (sym
->st_size
> refsym
->st_size
461 || (GL(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
463 extern char **_dl_argv
;
466 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
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
,
476 case R_SPARC_GLOB_DAT
:
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);
485 #ifndef RTLD_BOOTSTRAP
487 *(char *) reloc_addr
= value
;
490 *(short *) reloc_addr
= value
;
493 *(char *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
496 *(short *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
499 *reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
502 *reloc_addr
= (*reloc_addr
& ~0x3ff) | (value
& 0x3ff);
504 case R_SPARC_WDISP30
:
505 *reloc_addr
= ((*reloc_addr
& 0xc0000000)
506 | ((value
- (unsigned int) reloc_addr
) >> 2));
509 *reloc_addr
= (*reloc_addr
& 0xffc00000) | (value
>> 10);
512 ((unsigned char *) reloc_addr
) [0] = value
>> 8;
513 ((unsigned char *) reloc_addr
) [1] = value
;
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
;
522 case R_SPARC_NONE
: /* Alright, Wilbur. */
524 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
526 _dl_reloc_bad_type (map
, r_type
, 0);
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
;
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
))
548 case R_SPARC_JMP_SLOT
:
551 _dl_reloc_bad_type (map
, ELFW(R_TYPE
) (reloc
->r_info
), 1);