1 /* Machine-dependent ELF dynamic relocation inline functions. SPARC version.
2 Copyright (C) 1996, 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #define ELF_MACHINE_NAME "sparc"
25 #include <sys/param.h>
28 /* Some SPARC opcodes we need to use for self-modifying code. */
29 #define OPCODE_NOP 0x01000000 /* nop */
30 #define OPCODE_CALL 0x40000000 /* call ?; add PC-rel word address */
31 #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
32 #define OPCODE_JMP_G1 0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
33 #define OPCODE_SAVE_SP 0x9de3bfa8 /* save %sp, -(16+6)*4, %sp */
36 /* Return nonzero iff E_MACHINE is compatible with the running host. */
38 elf_machine_matches_host (Elf32_Half e_machine
)
40 return e_machine
== EM_SPARC
;
44 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
45 first element of the GOT. This must be inlined in a function which
47 static inline Elf32_Addr
48 elf_machine_dynamic (void)
50 register Elf32_Addr
*got
asm ("%l7");
54 /* Return the run-time load address of the shared object. */
55 static inline Elf32_Addr
56 elf_machine_load_address (void)
58 register Elf32_Addr pc
__asm("%o7"), pic
__asm("%l7"), got
;
60 /* Utilize the fact that a local .got entry will be partially
61 initialized at startup awaiting its RELATIVE fixup. */
63 __asm("sethi %%hi(.Load_address),%1\n"
66 "or %1,%%lo(.Load_address),%1\n"
68 : "=r"(pc
), "=r"(got
) : "r"(pic
));
73 /* Set up the loaded object described by L so its unrelocated PLT
74 entries will jump to the on-demand fixup code in dl-runtime.c. */
77 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
80 extern void _dl_runtime_resolve (Elf32_Word
);
82 if (l
->l_info
[DT_JMPREL
] && lazy
)
84 /* The entries for functions in the PLT have not yet been filled in.
85 Their initial contents will arrange when called to set the high 22
86 bits of %g1 with an offset into the .rela.plt section and jump to
87 the beginning of the PLT. */
88 plt
= (Elf32_Addr
*) (l
->l_addr
+ l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
90 /* The beginning of the PLT does:
93 pltpc: call _dl_runtime_resolve
97 This saves the register window containing the arguments, and the
98 PC value (pltpc) implicitly saved in %o7 by the call points near the
99 location where we store the link_map pointer for this object. */
101 plt
[0] = OPCODE_SAVE_SP
;
102 /* Construct PC-relative word address. */
103 plt
[1] = OPCODE_CALL
| (((Elf32_Addr
) &_dl_runtime_resolve
-
104 (Elf32_Addr
) &plt
[1]) >> 2);
105 plt
[2] = OPCODE_NOP
; /* Fill call delay slot. */
106 plt
[3] = (Elf32_Addr
) l
;
112 /* This code is used in dl-runtime.c to call the `fixup' function
113 and then redirect to the address it returns. */
114 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
115 .globl _dl_runtime_resolve
116 .type _dl_runtime_resolve, @function
118 /* Set up the arguments to fixup --
119 %o0 = link_map out of plt0
120 %o1 = offset of reloc entry */
127 .size _dl_runtime_resolve, . - _dl_runtime_resolve");
129 /* Nonzero iff TYPE should not be allowed to resolve to one of
130 the main executable's symbols, as for a COPY reloc. */
131 #define elf_machine_lookup_noexec_p(type) ((type) == R_SPARC_COPY)
133 /* Nonzero iff TYPE describes relocation of a PLT entry, so
134 PLT entries should not be allowed to define the value. */
135 #define elf_machine_lookup_noplt_p(type) ((type) == R_SPARC_JMP_SLOT)
137 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
138 #define ELF_MACHINE_JMP_SLOT R_SPARC_JMP_SLOT
140 /* The SPARC never uses Elf32_Rel relocations. */
141 #define ELF_MACHINE_NO_REL 1
143 /* The SPARC overlaps DT_RELA and DT_PLTREL. */
144 #define ELF_MACHINE_PLTREL_OVERLAP 1
146 /* Initial entry point code for the dynamic linker.
147 The C function `_dl_start' is the real entry point;
148 its return value is the user program's entry point. */
150 #define RTLD_START __asm__ ("\
153 .type _start,@function
155 /* Allocate space for functions to drop their arguments. */
157 /* Pass pointer to argument block to _dl_start. */
161 .globl _dl_start_user
162 .type _dl_start_user,@function
164 /* Load the PIC register. */
166 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7
167 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7
169 /* Save the user entry point address in %l0 */
171 /* See if we were run as a command with the executable file name as an
172 extra leading argument. If so, adjust the contents of the stack. */
173 sethi %hi(_dl_skip_args), %g2
174 or %g2, %lo(_dl_skip_args), %g2
180 /* Find out how far to shift. */
181 ld [%sp+22*4], %i1 /* load argc */
201 /* Copy down auxiliary table. */
210 /* Load _dl_default_scope[2] to pass to _dl_init_next. */
211 3: sethi %hi(_dl_default_scope), %g1
212 or %g1, %lo(_dl_default_scope), %g1
215 /* Call _dl_init_next to return the address of an initializer to run. */
216 4: call _dl_init_next
224 /* Clear the startup flag. */
225 5: sethi %hi(_dl_starting_up), %g1
226 or %g1, %lo(_dl_starting_up), %g1
229 /* Pass our finalizer function to the user in %g1. */
230 sethi %hi(_dl_fini), %g1
231 or %g1, %lo(_dl_fini), %g1
233 /* Jump to the user's entry point and deallocate the extra stack we got. */
236 .size _dl_start_user,.-_dl_start_user
240 elf_machine_fixup_plt (struct link_map
*map
, const Elf32_Rela
*reloc
,
241 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
243 extern unsigned long _dl_hwcap
;
245 /* For thread safety, write the instructions from the bottom and
246 flush before we overwrite the critical "b,a". This of course
247 need not be done during bootstrapping, since there are no threads.
248 But we also can't tell if we _can_ use flush, so don't. */
250 reloc_addr
[2] = OPCODE_JMP_G1
| (value
& 0x3ff);
251 #ifndef RTLD_BOOTSTRAP
252 if (_dl_hwcap
& HWCAP_SPARC_FLUSH
)
253 __asm
__volatile ("flush %0+8" : : "r"(reloc_addr
));
256 reloc_addr
[1] = OPCODE_SETHI_G1
| (value
>> 10);
257 #ifndef RTLD_BOOTSTRAP
258 if (_dl_hwcap
& HWCAP_SPARC_FLUSH
)
259 __asm
__volatile ("flush %0+4" : : "r"(reloc_addr
));
263 /* Return the final value of a plt relocation. */
264 static inline Elf32_Addr
265 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rela
*reloc
,
268 return value
+ reloc
->r_addend
;
273 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
274 MAP is the object containing the reloc. */
277 elf_machine_rela (struct link_map
*map
, const Elf32_Rela
*reloc
,
278 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
279 Elf32_Addr
*const reloc_addr
)
281 if (ELF32_R_TYPE (reloc
->r_info
) == R_SPARC_RELATIVE
)
283 #ifndef RTLD_BOOTSTRAP
284 if (map
!= &_dl_rtld_map
) /* Already done in rtld itself. */
286 *reloc_addr
+= map
->l_addr
+ reloc
->r_addend
;
290 const Elf32_Sym
*const refsym
= sym
;
292 if (sym
->st_shndx
!= SHN_UNDEF
&&
293 ELF32_ST_BIND (sym
->st_info
) == STB_LOCAL
)
297 value
= RESOLVE (&sym
, version
, ELF32_R_TYPE (reloc
->r_info
));
299 value
+= sym
->st_value
;
301 value
+= reloc
->r_addend
; /* Assume copy relocs have zero addend. */
303 switch (ELF32_R_TYPE (reloc
->r_info
))
306 #ifndef RTLD_BOOTSTRAP
307 if (sym
->st_size
> refsym
->st_size
308 || (_dl_verbose
&& sym
->st_size
< refsym
->st_size
))
310 extern char **_dl_argv
;
313 strtab
= ((void *) map
->l_addr
314 + map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
315 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>",
316 ": Symbol `", strtab
+ refsym
->st_name
,
317 "' has different size in shared object, "
318 "consider re-linking\n", NULL
);
320 memcpy (reloc_addr
, (void *) value
, MIN (sym
->st_size
,
324 case R_SPARC_GLOB_DAT
:
328 case R_SPARC_JMP_SLOT
:
329 elf_machine_fixup_plt(map
, reloc
, reloc_addr
, value
);
332 *(char *) reloc_addr
= value
;
335 *(short *) reloc_addr
= value
;
338 *(char *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
341 *(short *) reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
344 *reloc_addr
= (value
- (Elf32_Addr
) reloc_addr
);
347 *reloc_addr
= (*reloc_addr
& ~0x3ff) | (value
& 0x3ff);
349 case R_SPARC_WDISP30
:
350 *reloc_addr
= ((*reloc_addr
& 0xc0000000)
351 | ((value
- (unsigned int) reloc_addr
) >> 2));
354 *reloc_addr
= (*reloc_addr
& 0xffc00000) | (value
>> 10);
356 case R_SPARC_NONE
: /* Alright, Wilbur. */
359 assert (! "unexpected dynamic reloc type");
366 elf_machine_lazy_rel (struct link_map
*map
, const Elf32_Rela
*reloc
)
368 switch (ELF32_R_TYPE (reloc
->r_info
))
372 case R_SPARC_JMP_SLOT
:
375 assert (! "unexpected PLT reloc type");