1 /* Machine-dependent ELF dynamic relocation inline functions. SPARC version.
2 Copyright (C) 1996-2003, 2004, 2005, 2006 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 /* Return nonzero iff ELF header is compatible with the running host. */
45 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
47 if (ehdr
->e_machine
== EM_SPARC
)
49 else if (ehdr
->e_machine
== EM_SPARC32PLUS
)
51 /* XXX The following is wrong! Dave Miller rejected to implement it
52 correctly. If this causes problems shoot *him*! */
54 return GLRO(dl_hwcap
) & GLRO(dl_hwcap_mask
) & HWCAP_SPARC_V9
;
56 return GLRO(dl_hwcap
) & HWCAP_SPARC_V9
;
63 /* We have to do this because elf_machine_{dynamic,load_address} can be
64 invoked from functions that have no GOT references, and thus the compiler
65 has no obligation to load the PIC register. */
66 #define LOAD_PIC_REG(PIC_REG) \
67 do { register Elf32_Addr pc __asm("o7"); \
68 __asm("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t" \
70 "add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n" \
71 "1:\tadd %1, %0, %1" \
72 : "=r" (pc), "=r" (PIC_REG)); \
75 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
76 first element of the GOT. This must be inlined in a function which
78 static inline Elf32_Addr
79 elf_machine_dynamic (void)
81 register Elf32_Addr
*got
asm ("%l7");
88 /* Return the run-time load address of the shared object. */
89 static inline Elf32_Addr
90 elf_machine_load_address (void)
92 register Elf32_Addr
*pc
__asm ("%o7"), *got
__asm ("%l7");
94 __asm ("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t"
96 " add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n\t"
98 "call _GLOBAL_OFFSET_TABLE_\n"
99 "1:\tadd %1, %0, %1\n\t" : "=r" (pc
), "=r" (got
));
101 /* got is now l_addr + _GLOBAL_OFFSET_TABLE_
103 pc[2]*4 is l_addr + _DYNAMIC - (long)pc - 8
104 pc[3]*4 is l_addr + _GLOBAL_OFFSET_TABLE_ - (long)pc - 12 */
105 return (Elf32_Addr
) got
- *got
+ (pc
[2] - pc
[3]) * 4 - 4;
108 /* Set up the loaded object described by L so its unrelocated PLT
109 entries will jump to the on-demand fixup code in dl-runtime.c. */
112 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
115 extern void _dl_runtime_resolve (Elf32_Word
);
116 extern void _dl_runtime_profile (Elf32_Word
);
118 if (l
->l_info
[DT_JMPREL
] && lazy
)
122 /* The entries for functions in the PLT have not yet been filled in.
123 Their initial contents will arrange when called to set the high 22
124 bits of %g1 with an offset into the .rela.plt section and jump to
125 the beginning of the PLT. */
126 plt
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
127 if (__builtin_expect(profile
, 0))
129 rfunc
= (Elf32_Addr
) &_dl_runtime_profile
;
131 if (GLRO(dl_profile
) != NULL
132 && _dl_name_match_p (GLRO(dl_profile
), l
))
133 GL(dl_profile_map
) = l
;
137 rfunc
= (Elf32_Addr
) &_dl_runtime_resolve
;
140 /* The beginning of the PLT does:
142 sethi %hi(_dl_runtime_{resolve,profile}), %g2
143 pltpc: jmpl %g2 + %lo(_dl_runtime_{resolve,profile}), %g2
147 The PC value (pltpc) saved in %g2 by the jmpl points near the
148 location where we store the link_map pointer for this object. */
150 plt
[0] = 0x05000000 | ((rfunc
>> 10) & 0x003fffff);
151 plt
[1] = 0x85c0a000 | (rfunc
& 0x3ff);
152 plt
[2] = OPCODE_NOP
; /* Fill call delay slot. */
153 plt
[3] = (Elf32_Addr
) l
;
154 if (__builtin_expect (l
->l_info
[VALIDX(DT_GNU_PRELINKED
)] != NULL
, 0)
155 || __builtin_expect (l
->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)] != NULL
, 0))
157 /* Need to reinitialize .plt to undo prelinking. */
159 Elf32_Rela
*rela
= (Elf32_Rela
*) D_PTR (l
, l_info
[DT_JMPREL
]);
161 = (Elf32_Rela
*) ((char *) rela
162 + l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
);
163 do_flush
= GLRO(dl_hwcap
) & HWCAP_SPARC_FLUSH
;
165 /* prelink must ensure there are no R_SPARC_NONE relocs left
167 while (rela
< relaend
)
169 *(unsigned int *) rela
->r_offset
170 = OPCODE_SETHI_G1
| (rela
->r_offset
- (Elf32_Addr
) plt
);
171 *(unsigned int *) (rela
->r_offset
+ 4)
172 = OPCODE_BA
| ((((Elf32_Addr
) plt
173 - rela
->r_offset
- 4) >> 2) & 0x3fffff);
176 __asm
__volatile ("flush %0" : : "r"(rela
->r_offset
));
177 __asm
__volatile ("flush %0+4" : : "r"(rela
->r_offset
));
187 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
188 PLT entries should not be allowed to define the value.
189 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
190 of the main executable's symbols, as for a COPY reloc. */
191 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
192 # define elf_machine_type_class(type) \
193 ((((type) == R_SPARC_JMP_SLOT \
194 || ((type) >= R_SPARC_TLS_GD_HI22 && (type) <= R_SPARC_TLS_TPOFF64)) \
195 * ELF_RTYPE_CLASS_PLT) \
196 | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
198 # define elf_machine_type_class(type) \
199 ((((type) == R_SPARC_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
200 | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
203 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
204 #define ELF_MACHINE_JMP_SLOT R_SPARC_JMP_SLOT
206 /* The SPARC never uses Elf32_Rel relocations. */
207 #define ELF_MACHINE_NO_REL 1
209 /* The SPARC overlaps DT_RELA and DT_PLTREL. */
210 #define ELF_MACHINE_PLTREL_OVERLAP 1
212 /* Undo the sub %sp, 6*4, %sp; add %sp, 22*4, %o0 below to get at the
213 value we want in __libc_stack_end. */
214 #define DL_STACK_END(cookie) \
215 ((void *) (((long) (cookie)) - (22 - 6) * 4))
217 /* Initial entry point code for the dynamic linker.
218 The C function `_dl_start' is the real entry point;
219 its return value is the user program's entry point. */
221 #define RTLD_START __asm__ ("\
224 .type _start, @function\n\
227 /* Allocate space for functions to drop their arguments. */\n\
229 /* Pass pointer to argument block to _dl_start. */\n\
231 add %sp, 22*4, %o0\n\
233 .globl _dl_start_user\n\
234 .type _dl_start_user, @function\n\
236 /* Load the PIC register. */\n\
238 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n\
239 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n\
241 /* Save the user entry point address in %l0 */\n\
243 /* See if we were run as a command with the executable file name as an\n\
244 extra leading argument. If so, adjust the contents of the stack. */\n\
245 sethi %hi(_dl_skip_args), %g2\n\
246 or %g2, %lo(_dl_skip_args), %g2\n\
251 ld [%sp+22*4], %i5 /* load argc */\n\
252 /* Find out how far to shift. */\n\
253 sethi %hi(_dl_argv), %l3\n\
254 or %l3, %lo(_dl_argv), %l3\n\
259 st %i5, [%sp+22*4]\n\
261 add %sp, 23*4, %i1\n\
264 /* Copy down argv */\n\
271 /* Copy down env */\n\
278 /* Copy down auxiliary table. */\n\
287 /* %o0 = _dl_loaded, %o1 = argc, %o2 = argv, %o3 = envp. */\n\
288 3: sethi %hi(_rtld_local), %o0\n\
289 add %sp, 23*4, %o2\n\
290 orcc %o0, %lo(_rtld_local), %o0\n\
296 call _dl_init_internal\n\
298 /* Pass our finalizer function to the user in %g1. */\n\
299 sethi %hi(_dl_fini), %g1\n\
300 or %g1, %lo(_dl_fini), %g1\n\
302 /* Jump to the user's entry point and deallocate the extra stack we got. */\n\
305 .size _dl_start_user, . - _dl_start_user\n\
308 static inline Elf32_Addr
309 sparc_fixup_plt (const Elf32_Rela
*reloc
, Elf32_Addr
*reloc_addr
,
310 Elf32_Addr value
, int t
)
312 Elf32_Sword disp
= value
- (Elf32_Addr
) reloc_addr
;
313 #ifndef RTLD_BOOTSTRAP
314 /* Note that we don't mask the hwcap here, as the flush is essential to
315 functionality on those cpu's that implement it. */
316 int do_flush
= GLRO(dl_hwcap
) & HWCAP_SPARC_FLUSH
;
318 /* Unfortunately, this is necessary, so that we can ensure
319 ld.so will not execute corrupt PLT entry instructions. */
320 const int do_flush
= 1;
323 if (0 && disp
>= -0x800000 && disp
< 0x800000)
325 /* Don't need to worry about thread safety. We're writing just one
328 reloc_addr
[0] = OPCODE_BA
| ((disp
>> 2) & 0x3fffff);
330 __asm
__volatile ("flush %0" : : "r"(reloc_addr
));
334 /* For thread safety, write the instructions from the bottom and
335 flush before we overwrite the critical "b,a". This of course
336 need not be done during bootstrapping, since there are no threads.
337 But we also can't tell if we _can_ use flush, so don't. */
340 reloc_addr
[1] = OPCODE_JMP_G1
| (value
& 0x3ff);
342 __asm
__volatile ("flush %0+4" : : "r"(reloc_addr
));
344 reloc_addr
[0] = OPCODE_SETHI_G1
| (value
>> 10);
346 __asm
__volatile ("flush %0" : : "r"(reloc_addr
));
352 static inline Elf32_Addr
353 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
354 const Elf32_Rela
*reloc
,
355 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
357 return sparc_fixup_plt (reloc
, reloc_addr
, value
, 1);
360 /* Return the final value of a plt relocation. */
361 static inline Elf32_Addr
362 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rela
*reloc
,
365 return value
+ reloc
->r_addend
;
368 #endif /* dl_machine_h */
370 #define ARCH_LA_PLTENTER sparc32_gnu_pltenter
371 #define ARCH_LA_PLTEXIT sparc32_gnu_pltexit
375 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
376 MAP is the object containing the reloc. */
379 __attribute__ ((always_inline
))
380 elf_machine_rela (struct link_map
*map
, const Elf32_Rela
*reloc
,
381 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
382 void *const reloc_addr_arg
)
384 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
385 const Elf32_Sym
*const refsym
= sym
;
387 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
388 struct link_map
*sym_map
= NULL
;
390 #if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
391 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
392 reference weak so static programs can still link. This declaration
393 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
394 because rtld.c contains the common defn for _dl_rtld_map, which is
395 incompatible with a weak decl in the same file. */
396 weak_extern (_dl_rtld_map
);
399 if (__builtin_expect (r_type
== R_SPARC_NONE
, 0))
402 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
403 if (__builtin_expect (r_type
== R_SPARC_RELATIVE
, 0))
405 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
406 if (map
!= &_dl_rtld_map
) /* Already done in rtld itself. */
408 *reloc_addr
+= map
->l_addr
+ reloc
->r_addend
;
413 #ifndef RESOLVE_CONFLICT_FIND_MAP
414 if (__builtin_expect (ELF32_ST_BIND (sym
->st_info
) == STB_LOCAL
, 0)
415 && sym
->st_shndx
!= SHN_UNDEF
)
421 sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
422 value
= sym_map
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
428 value
+= reloc
->r_addend
; /* Assume copy relocs have zero addend. */
432 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
435 /* This can happen in trace mode if an object could not be
438 if (sym
->st_size
> refsym
->st_size
439 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
443 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
445 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
446 rtld_progname
?: "<program name unknown>",
447 strtab
+ refsym
->st_name
);
449 memcpy (reloc_addr_arg
, (void *) value
,
450 MIN (sym
->st_size
, refsym
->st_size
));
453 case R_SPARC_GLOB_DAT
:
457 case R_SPARC_JMP_SLOT
:
458 /* At this point we don't need to bother with thread safety,
459 so we can optimize the first instruction of .plt out. */
460 sparc_fixup_plt (reloc
, reloc_addr
, value
, 0);
462 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD) \
463 && !defined RESOLVE_CONFLICT_FIND_MAP
464 case R_SPARC_TLS_DTPMOD32
:
465 /* Get the information from the link map returned by the
468 *reloc_addr
= sym_map
->l_tls_modid
;
470 case R_SPARC_TLS_DTPOFF32
:
471 /* During relocation all TLS symbols are defined and used.
472 Therefore the offset is already correct. */
473 *reloc_addr
= (sym
== NULL
? 0 : sym
->st_value
) + reloc
->r_addend
;
475 case R_SPARC_TLS_TPOFF32
:
476 /* The offset is negative, forward from the thread pointer. */
477 /* We know the offset of object the symbol is contained in.
478 It is a negative value which will be added to the
482 CHECK_STATIC_TLS (map
, sym_map
);
483 *reloc_addr
= sym
->st_value
- sym_map
->l_tls_offset
487 # ifndef RTLD_BOOTSTRAP
488 case R_SPARC_TLS_LE_HIX22
:
489 case R_SPARC_TLS_LE_LOX10
:
492 CHECK_STATIC_TLS (map
, sym_map
);
493 value
= sym
->st_value
- sym_map
->l_tls_offset
495 if (r_type
== R_SPARC_TLS_LE_HIX22
)
496 *reloc_addr
= (*reloc_addr
& 0xffc00000) | ((~value
) >> 10);
498 *reloc_addr
= (*reloc_addr
& 0xffffe000) | (value
& 0x3ff)
504 #ifndef RTLD_BOOTSTRAP
506 *(char *) reloc_addr
= value
;
509 *(short *) reloc_addr
= value
;
512 *(char *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
515 *(short *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
518 *reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
521 *reloc_addr
= (*reloc_addr
& ~0x3ff) | (value
& 0x3ff);
523 case R_SPARC_WDISP30
:
524 *reloc_addr
= ((*reloc_addr
& 0xc0000000)
525 | ((value
- (unsigned int) reloc_addr
) >> 2));
528 *reloc_addr
= (*reloc_addr
& 0xffc00000) | (value
>> 10);
531 ((unsigned char *) reloc_addr_arg
) [0] = value
>> 8;
532 ((unsigned char *) reloc_addr_arg
) [1] = value
;
535 ((unsigned char *) reloc_addr_arg
) [0] = value
>> 24;
536 ((unsigned char *) reloc_addr_arg
) [1] = value
>> 16;
537 ((unsigned char *) reloc_addr_arg
) [2] = value
>> 8;
538 ((unsigned char *) reloc_addr_arg
) [3] = value
;
541 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
543 _dl_reloc_bad_type (map
, r_type
, 0);
550 __attribute__ ((always_inline
))
551 elf_machine_rela_relative (Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
552 void *const reloc_addr_arg
)
554 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
555 *reloc_addr
+= l_addr
+ reloc
->r_addend
;
559 __attribute__ ((always_inline
))
560 elf_machine_lazy_rel (struct link_map
*map
,
561 Elf32_Addr l_addr
, const Elf32_Rela
*reloc
)
563 switch (ELF32_R_TYPE (reloc
->r_info
))
567 case R_SPARC_JMP_SLOT
:
570 _dl_reloc_bad_type (map
, ELFW(R_TYPE
) (reloc
->r_info
), 1);
575 #endif /* RESOLVE_MAP */