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
);
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" \
82 "add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n" \
83 "1:\tadd %1, %0, %1" \
84 : "=r" (pc), "=r" (PIC_REG)); \
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
90 static inline Elf32_Addr
91 elf_machine_dynamic (void)
93 register Elf32_Addr
*got
asm ("%l7");
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"
108 " add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\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_
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. */
124 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
127 extern void _dl_runtime_resolve (Elf32_Word
);
128 extern void _dl_runtime_profile (Elf32_Word
);
130 if (l
->l_info
[DT_JMPREL
] && lazy
)
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
]);
140 rfunc
= (Elf32_Addr
) &_dl_runtime_resolve
;
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:
152 pltpc: call _dl_runtime_resolve
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
;
171 Elf32_Rela
*rela
= (Elf32_Rela
*) D_PTR (l
, l_info
[DT_JMPREL
]);
173 = (Elf32_Rela
*) ((char *) rela
174 + l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
);
176 weak_extern (_dl_hwcap
);
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
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);
192 __asm
__volatile ("flush %0" : : "r"(rela
->r_offset
));
193 __asm
__volatile ("flush %0+4" : : "r"(rela
->r_offset
));
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) \
208 .globl " #tramp_name "\n\
209 .type " #tramp_name ", @function\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\
219 call " #fixup_name "\n\
220 sub %o1, 4*12, %o1\n\
223 .size " #tramp_name ", . - " #tramp_name "\n\
227 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
228 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
229 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup);
231 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
232 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
233 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, fixup);
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__ ("\
260 .type _start, @function\n\
263 /* Allocate space for functions to drop their arguments. */\n\
265 /* Pass pointer to argument block to _dl_start. */\n\
267 add %sp, 22*4, %o0\n\
269 .globl _dl_start_user\n\
270 .type _dl_start_user, @function\n\
272 /* Load the PIC register. */\n\
274 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n\
275 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n\
277 /* Save the user entry point address in %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\
285 or %g2, %lo(_dl_skip_args), %g2\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\
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\
301 st %i5, [%sp+22*4]\n\
303 add %sp, 23*4, %i1\n\
306 /* Copy down argv */\n\
313 /* Copy down env */\n\
320 /* Copy down auxiliary table. */\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\
338 call _dl_init_internal\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\
344 /* Jump to the user's entry point and deallocate the extra stack we got. */\n\
347 .size _dl_start_user, . - _dl_start_user\n\
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
;
361 weak_extern (_dl_hwcap
);
363 hwcap
= WEAKADDR (GL(dl_hwcap
));
364 do_flush
= (!hwcap
|| (*hwcap
& HWCAP_SPARC_FLUSH
));
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;
371 if (0 && disp
>= -0x800000 && disp
< 0x800000)
373 /* Don't need to worry about thread safety. We're writing just one
376 reloc_addr
[0] = OPCODE_BA
| ((disp
>> 2) & 0x3fffff);
378 __asm
__volatile ("flush %0" : : "r"(reloc_addr
));
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. */
388 reloc_addr
[1] = OPCODE_JMP_G1
| (value
& 0x3ff);
390 __asm
__volatile ("flush %0+4" : : "r"(reloc_addr
));
392 reloc_addr
[0] = OPCODE_SETHI_G1
| (value
>> 10);
394 __asm
__volatile ("flush %0" : : "r"(reloc_addr
));
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
,
413 return value
+ reloc
->r_addend
;
418 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
419 MAP is the object containing the reloc. */
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
);
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. */
443 *reloc_addr
+= map
->l_addr
+ reloc
->r_addend
;
448 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
449 const Elf32_Sym
*const refsym
= sym
;
452 #ifndef RESOLVE_CONFLICT_FIND_MAP
453 if (sym
->st_shndx
!= SHN_UNDEF
&&
454 ELF32_ST_BIND (sym
->st_info
) == STB_LOCAL
)
458 value
= RESOLVE (&sym
, version
, r_type
);
460 value
+= sym
->st_value
;
465 value
+= reloc
->r_addend
; /* Assume copy relocs have zero addend. */
469 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
472 /* This can happen in trace mode if an object could not be
475 if (sym
->st_size
> refsym
->st_size
476 || (GL(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
480 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
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
,
490 case R_SPARC_GLOB_DAT
:
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);
499 #ifndef RTLD_BOOTSTRAP
501 *(char *) reloc_addr
= value
;
504 *(short *) reloc_addr
= value
;
507 *(char *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
510 *(short *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
513 *reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
516 *reloc_addr
= (*reloc_addr
& ~0x3ff) | (value
& 0x3ff);
518 case R_SPARC_WDISP30
:
519 *reloc_addr
= ((*reloc_addr
& 0xc0000000)
520 | ((value
- (unsigned int) reloc_addr
) >> 2));
523 *reloc_addr
= (*reloc_addr
& 0xffc00000) | (value
>> 10);
526 ((unsigned char *) reloc_addr
) [0] = value
>> 8;
527 ((unsigned char *) reloc_addr
) [1] = value
;
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
;
536 case R_SPARC_NONE
: /* Alright, Wilbur. */
538 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
540 _dl_reloc_bad_type (map
, r_type
, 0);
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
;
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
))
562 case R_SPARC_JMP_SLOT
:
565 _dl_reloc_bad_type (map
, ELFW(R_TYPE
) (reloc
->r_info
), 1);