1 /* Machine-dependent ELF dynamic relocation inline functions. SPARC version.
2 Copyright (C) 1996-2003, 2004 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
23 #define ELF_MACHINE_NAME "sparc"
26 #include <sys/param.h>
31 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
32 + DT_EXTRANUM + DT_VALTAGIDX (tag))
35 /* Some SPARC opcodes we need to use for self-modifying code. */
36 #define OPCODE_NOP 0x01000000 /* nop */
37 #define OPCODE_CALL 0x40000000 /* call ?; add PC-rel word address */
38 #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
39 #define OPCODE_JMP_G1 0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
40 #define OPCODE_SAVE_SP 0x9de3bfa8 /* save %sp, -(16+6)*4, %sp */
41 #define OPCODE_BA 0x30800000 /* b,a ?; add PC-rel word address */
43 /* Use a different preload file when running in 32-bit emulation mode
45 #define LD_SO_PRELOAD ((GLRO(dl_hwcap) & HWCAP_SPARC_V9) \
46 ? "/etc/ld.so.preload32" \
47 : "/etc/ld.so.preload")
50 /* Return nonzero iff ELF header is compatible with the running host. */
52 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
54 if (ehdr
->e_machine
== EM_SPARC
)
56 else if (ehdr
->e_machine
== EM_SPARC32PLUS
)
58 /* XXX The following is wrong! Dave Miller rejected to implement it
59 correctly. If this causes problems shoot *him*! */
61 return GLRO(dl_hwcap
) & GLRO(dl_hwcap_mask
) & HWCAP_SPARC_V9
;
63 return GLRO(dl_hwcap
) & HWCAP_SPARC_V9
;
70 /* We have to do this because elf_machine_{dynamic,load_address} can be
71 invoked from functions that have no GOT references, and thus the compiler
72 has no obligation to load the PIC register. */
73 #define LOAD_PIC_REG(PIC_REG) \
74 do { register Elf32_Addr pc __asm("o7"); \
75 __asm("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t" \
77 "add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n" \
78 "1:\tadd %1, %0, %1" \
79 : "=r" (pc), "=r" (PIC_REG)); \
82 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
83 first element of the GOT. This must be inlined in a function which
85 static inline Elf32_Addr
86 elf_machine_dynamic (void)
88 register Elf32_Addr
*got
asm ("%l7");
95 /* Return the run-time load address of the shared object. */
96 static inline Elf32_Addr
97 elf_machine_load_address (void)
99 register Elf32_Addr
*pc
__asm ("%o7"), *got
__asm ("%l7");
101 __asm ("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t"
103 " add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n\t"
105 "call _GLOBAL_OFFSET_TABLE_\n"
106 "1:\tadd %1, %0, %1\n\t" : "=r" (pc
), "=r" (got
));
108 /* got is now l_addr + _GLOBAL_OFFSET_TABLE_
110 pc[2]*4 is l_addr + _DYNAMIC - (long)pc - 8
111 pc[3]*4 is l_addr + _GLOBAL_OFFSET_TABLE_ - (long)pc - 12 */
112 return (Elf32_Addr
) got
- *got
+ (pc
[2] - pc
[3]) * 4 - 4;
115 /* Set up the loaded object described by L so its unrelocated PLT
116 entries will jump to the on-demand fixup code in dl-runtime.c. */
119 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
122 extern void _dl_runtime_resolve (Elf32_Word
);
123 extern void _dl_runtime_profile (Elf32_Word
);
125 if (l
->l_info
[DT_JMPREL
] && lazy
)
129 /* The entries for functions in the PLT have not yet been filled in.
130 Their initial contents will arrange when called to set the high 22
131 bits of %g1 with an offset into the .rela.plt section and jump to
132 the beginning of the PLT. */
133 plt
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
135 rfunc
= (Elf32_Addr
) &_dl_runtime_resolve
;
138 rfunc
= (Elf32_Addr
) &_dl_runtime_profile
;
140 if (_dl_name_match_p (GLRO(dl_profile
), l
))
141 GL(dl_profile_map
) = l
;
144 /* The beginning of the PLT does:
147 pltpc: call _dl_runtime_resolve
151 This saves the register window containing the arguments, and the
152 PC value (pltpc) implicitly saved in %o7 by the call points near the
153 location where we store the link_map pointer for this object. */
155 plt
[0] = OPCODE_SAVE_SP
;
156 /* Construct PC-relative word address. */
157 plt
[1] = OPCODE_CALL
| ((rfunc
- (Elf32_Addr
) &plt
[1]) >> 2);
158 plt
[2] = OPCODE_NOP
; /* Fill call delay slot. */
159 plt
[3] = (Elf32_Addr
) l
;
160 if (__builtin_expect (l
->l_info
[VALIDX(DT_GNU_PRELINKED
)] != NULL
, 0)
161 || __builtin_expect (l
->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)] != NULL
, 0))
163 /* Need to reinitialize .plt to undo prelinking. */
165 Elf32_Rela
*rela
= (Elf32_Rela
*) D_PTR (l
, l_info
[DT_JMPREL
]);
167 = (Elf32_Rela
*) ((char *) rela
168 + l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
);
169 do_flush
= GLRO(dl_hwcap
) & HWCAP_SPARC_FLUSH
;
171 /* prelink must ensure there are no R_SPARC_NONE relocs left
173 while (rela
< relaend
)
175 *(unsigned int *) rela
->r_offset
176 = OPCODE_SETHI_G1
| (rela
->r_offset
- (Elf32_Addr
) plt
);
177 *(unsigned int *) (rela
->r_offset
+ 4)
178 = OPCODE_BA
| ((((Elf32_Addr
) plt
179 - rela
->r_offset
- 4) >> 2) & 0x3fffff);
182 __asm
__volatile ("flush %0" : : "r"(rela
->r_offset
));
183 __asm
__volatile ("flush %0+4" : : "r"(rela
->r_offset
));
193 /* This code is used in dl-runtime.c to call the `fixup' function
194 and then redirect to the address it returns. */
195 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name) \
198 .globl " #tramp_name "\n\
199 .type " #tramp_name ", @function\n\
202 /* Set up the arguments to fixup --\n\
203 %o0 = link_map out of plt0\n\
204 %o1 = offset of reloc entry\n\
205 %o2 = return address */\n\
209 call " #fixup_name "\n\
210 sub %o1, 4*12, %o1\n\
213 .size " #tramp_name ", . - " #tramp_name "\n\
217 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
218 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
219 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup);
221 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
222 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
223 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, fixup);
226 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
227 PLT entries should not be allowed to define the value.
228 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
229 of the main executable's symbols, as for a COPY reloc. */
230 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
231 # define elf_machine_type_class(type) \
232 ((((type) == R_SPARC_JMP_SLOT \
233 || ((type) >= R_SPARC_TLS_GD_HI22 && (type) <= R_SPARC_TLS_TPOFF64)) \
234 * ELF_RTYPE_CLASS_PLT) \
235 | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
237 # define elf_machine_type_class(type) \
238 ((((type) == R_SPARC_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
239 | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
242 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
243 #define ELF_MACHINE_JMP_SLOT R_SPARC_JMP_SLOT
245 /* The SPARC never uses Elf32_Rel relocations. */
246 #define ELF_MACHINE_NO_REL 1
248 /* The SPARC overlaps DT_RELA and DT_PLTREL. */
249 #define ELF_MACHINE_PLTREL_OVERLAP 1
251 /* Undo the sub %sp, 6*4, %sp; add %sp, 22*4, %o0 below to get at the
252 value we want in __libc_stack_end. */
253 #define DL_STACK_END(cookie) \
254 ((void *) (((long) (cookie)) - (22 - 6) * 4))
256 /* Initial entry point code for the dynamic linker.
257 The C function `_dl_start' is the real entry point;
258 its return value is the user program's entry point. */
260 #define RTLD_START __asm__ ("\
263 .type _start, @function\n\
266 /* Allocate space for functions to drop their arguments. */\n\
268 /* Pass pointer to argument block to _dl_start. */\n\
270 add %sp, 22*4, %o0\n\
272 .globl _dl_start_user\n\
273 .type _dl_start_user, @function\n\
275 /* Load the PIC register. */\n\
277 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n\
278 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n\
280 /* Save the user entry point address in %l0 */\n\
282 /* See if we were run as a command with the executable file name as an\n\
283 extra leading argument. If so, adjust the contents of the stack. */\n\
284 sethi %hi(_dl_skip_args), %g2\n\
285 or %g2, %lo(_dl_skip_args), %g2\n\
290 ld [%sp+22*4], %i5 /* load argc */\n\
291 /* Find out how far to shift. */\n\
292 sethi %hi(_dl_argv), %l3\n\
293 or %l3, %lo(_dl_argv), %l3\n\
298 st %i5, [%sp+22*4]\n\
300 add %sp, 23*4, %i1\n\
303 /* Copy down argv */\n\
310 /* Copy down env */\n\
317 /* Copy down auxiliary table. */\n\
326 /* %o0 = _dl_loaded, %o1 = argc, %o2 = argv, %o3 = envp. */\n\
327 3: sethi %hi(_rtld_local), %o0\n\
328 add %sp, 23*4, %o2\n\
329 orcc %o0, %lo(_rtld_local), %o0\n\
335 call _dl_init_internal\n\
337 /* Pass our finalizer function to the user in %g1. */\n\
338 sethi %hi(_dl_fini), %g1\n\
339 or %g1, %lo(_dl_fini), %g1\n\
341 /* Jump to the user's entry point and deallocate the extra stack we got. */\n\
344 .size _dl_start_user, . - _dl_start_user\n\
347 static inline Elf32_Addr
348 sparc_fixup_plt (const Elf32_Rela
*reloc
, Elf32_Addr
*reloc_addr
,
349 Elf32_Addr value
, int t
)
351 Elf32_Sword disp
= value
- (Elf32_Addr
) reloc_addr
;
352 #ifndef RTLD_BOOTSTRAP
353 /* Note that we don't mask the hwcap here, as the flush is essential to
354 functionality on those cpu's that implement it. */
355 int do_flush
= GLRO(dl_hwcap
) & HWCAP_SPARC_FLUSH
;
357 /* Unfortunately, this is necessary, so that we can ensure
358 ld.so will not execute corrupt PLT entry instructions. */
359 const int do_flush
= 1;
362 if (0 && disp
>= -0x800000 && disp
< 0x800000)
364 /* Don't need to worry about thread safety. We're writing just one
367 reloc_addr
[0] = OPCODE_BA
| ((disp
>> 2) & 0x3fffff);
369 __asm
__volatile ("flush %0" : : "r"(reloc_addr
));
373 /* For thread safety, write the instructions from the bottom and
374 flush before we overwrite the critical "b,a". This of course
375 need not be done during bootstrapping, since there are no threads.
376 But we also can't tell if we _can_ use flush, so don't. */
379 reloc_addr
[1] = OPCODE_JMP_G1
| (value
& 0x3ff);
381 __asm
__volatile ("flush %0+4" : : "r"(reloc_addr
));
383 reloc_addr
[0] = OPCODE_SETHI_G1
| (value
>> 10);
385 __asm
__volatile ("flush %0" : : "r"(reloc_addr
));
391 static inline Elf32_Addr
392 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
393 const Elf32_Rela
*reloc
,
394 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
396 return sparc_fixup_plt (reloc
, reloc_addr
, value
, 1);
399 /* Return the final value of a plt relocation. */
400 static inline Elf32_Addr
401 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rela
*reloc
,
404 return value
+ reloc
->r_addend
;
407 #endif /* dl_machine_h */
411 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
412 MAP is the object containing the reloc. */
415 elf_machine_rela (struct link_map
*map
, const Elf32_Rela
*reloc
,
416 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
417 void *const reloc_addr_arg
)
419 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
420 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
422 #if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
423 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
424 reference weak so static programs can still link. This declaration
425 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
426 because rtld.c contains the common defn for _dl_rtld_map, which is
427 incompatible with a weak decl in the same file. */
428 weak_extern (_dl_rtld_map
);
431 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
432 if (__builtin_expect (r_type
== R_SPARC_RELATIVE
, 0))
434 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
435 if (map
!= &_dl_rtld_map
) /* Already done in rtld itself. */
437 *reloc_addr
+= map
->l_addr
+ reloc
->r_addend
;
442 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
443 const Elf32_Sym
*const refsym
= sym
;
445 struct link_map
*sym_map
;
449 #ifndef RESOLVE_CONFLICT_FIND_MAP
450 if (sym
->st_shndx
!= SHN_UNDEF
&&
451 ELF32_ST_BIND (sym
->st_info
) == STB_LOCAL
)
454 # if defined USE_TLS && !defined RTLD_BOOTSTRAP
460 # if defined USE_TLS && !defined RTLD_BOOTSTRAP
461 sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
462 value
= sym
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
464 value
= RESOLVE (&sym
, version
, r_type
);
466 value
+= sym
->st_value
;
472 value
+= reloc
->r_addend
; /* Assume copy relocs have zero addend. */
476 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
479 /* This can happen in trace mode if an object could not be
482 if (sym
->st_size
> refsym
->st_size
483 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
487 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
489 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
490 rtld_progname
?: "<program name unknown>",
491 strtab
+ refsym
->st_name
);
493 memcpy (reloc_addr_arg
, (void *) value
,
494 MIN (sym
->st_size
, refsym
->st_size
));
497 case R_SPARC_GLOB_DAT
:
501 case R_SPARC_JMP_SLOT
:
502 /* At this point we don't need to bother with thread safety,
503 so we can optimize the first instruction of .plt out. */
504 sparc_fixup_plt (reloc
, reloc_addr
, value
, 0);
506 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD) \
507 && !defined RESOLVE_CONFLICT_FIND_MAP
508 case R_SPARC_TLS_DTPMOD32
:
509 /* Get the information from the link map returned by the
512 *reloc_addr
= sym_map
->l_tls_modid
;
514 case R_SPARC_TLS_DTPOFF32
:
515 /* During relocation all TLS symbols are defined and used.
516 Therefore the offset is already correct. */
517 *reloc_addr
= (sym
== NULL
? 0 : sym
->st_value
) + reloc
->r_addend
;
519 case R_SPARC_TLS_TPOFF32
:
520 /* The offset is negative, forward from the thread pointer. */
521 /* We know the offset of object the symbol is contained in.
522 It is a negative value which will be added to the
526 CHECK_STATIC_TLS (map
, sym_map
);
527 *reloc_addr
= sym
->st_value
- sym_map
->l_tls_offset
531 # ifndef RTLD_BOOTSTRAP
532 case R_SPARC_TLS_LE_HIX22
:
533 case R_SPARC_TLS_LE_LOX10
:
536 CHECK_STATIC_TLS (map
, sym_map
);
537 value
= sym
->st_value
- sym_map
->l_tls_offset
539 if (r_type
== R_SPARC_TLS_LE_HIX22
)
540 *reloc_addr
= (*reloc_addr
& 0xffc00000) | ((~value
) >> 10);
542 *reloc_addr
= (*reloc_addr
& 0xffffe000) | (value
& 0x3ff)
548 #ifndef RTLD_BOOTSTRAP
550 *(char *) reloc_addr
= value
;
553 *(short *) reloc_addr
= value
;
556 *(char *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
559 *(short *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
562 *reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
565 *reloc_addr
= (*reloc_addr
& ~0x3ff) | (value
& 0x3ff);
567 case R_SPARC_WDISP30
:
568 *reloc_addr
= ((*reloc_addr
& 0xc0000000)
569 | ((value
- (unsigned int) reloc_addr
) >> 2));
572 *reloc_addr
= (*reloc_addr
& 0xffc00000) | (value
>> 10);
575 ((unsigned char *) reloc_addr_arg
) [0] = value
>> 8;
576 ((unsigned char *) reloc_addr_arg
) [1] = value
;
579 ((unsigned char *) reloc_addr_arg
) [0] = value
>> 24;
580 ((unsigned char *) reloc_addr_arg
) [1] = value
>> 16;
581 ((unsigned char *) reloc_addr_arg
) [2] = value
>> 8;
582 ((unsigned char *) reloc_addr_arg
) [3] = value
;
585 case R_SPARC_NONE
: /* Alright, Wilbur. */
587 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
589 _dl_reloc_bad_type (map
, r_type
, 0);
597 elf_machine_rela_relative (Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
598 void *const reloc_addr_arg
)
600 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
601 *reloc_addr
+= l_addr
+ reloc
->r_addend
;
605 elf_machine_lazy_rel (struct link_map
*map
,
606 Elf32_Addr l_addr
, const Elf32_Rela
*reloc
)
608 switch (ELF32_R_TYPE (reloc
->r_info
))
612 case R_SPARC_JMP_SLOT
:
615 _dl_reloc_bad_type (map
, ELFW(R_TYPE
) (reloc
->r_info
), 1);