1 /* Machine-dependent ELF dynamic relocation inline functions. SPARC version.
2 Copyright (C) 1996 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., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #define ELF_MACHINE_NAME "sparc"
27 /* Some SPARC opcodes we need to use for self-modifying code. */
28 #define OPCODE_NOP 0x01000000 /* nop */
29 #define OPCODE_CALL 0x04000000 /* call ?; add PC-rel word address */
30 #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
31 #define OPCODE_JMP_G1 0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
32 #define OPCODE_SAVE_SP64 0x9de3bfc0 /* save %sp, -64, %sp */
35 /* Return nonzero iff E_MACHINE is compatible with the running host. */
37 elf_machine_matches_host (Elf32_Half e_machine
)
49 /* Return the run-time address of the _GLOBAL_OFFSET_TABLE_.
50 Must be inlined in a function which uses global data. */
51 static inline Elf32_Addr
*
52 elf_machine_got (void)
54 register Elf32_Addr
*got
asm ("%l7");
59 /* Return the run-time load address of the shared object. */
60 static inline Elf32_Addr
61 elf_machine_load_address (void)
68 /* The `subl' insn above will contain an R_68K_RELATIVE relocation
69 entry intended to insert the run-time address of the label `here'.
70 This will be the first relocation in the text of the dynamic
71 linker; we skip it to avoid trying to modify read-only text in this
73 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
74 ((dynamic_info)[DT_RELA]->d_un.d_ptr += sizeof (Elf32_Rela), \
75 (dynamic_info)[DT_RELASZ]->d_un.d_val -= sizeof (Elf32_Rela))
77 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
78 MAP is the object containing the reloc. */
81 elf_machine_rela (struct link_map
*map
,
82 const Elf32_Rela
*reloc
, const Elf32_Sym
*sym
,
83 Elf32_Addr (*resolve
) (const Elf32_Sym
**ref
,
84 Elf32_Addr reloc_addr
,
87 Elf32_Addr
*const reloc_addr
= (void *) (map
->l_addr
+ reloc
->r_offset
);
90 switch (ELF32_R_TYPE (reloc
->r_info
))
93 loadbase
= (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 0);
94 memcpy (reloc_addr
, (void *) (loadbase
+ sym
->st_value
), sym
->st_size
);
96 case R_SPARC_GLOB_DAT
:
98 loadbase
= (resolve
? (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 0) :
99 /* RESOLVE is null during bootstrap relocation. */
101 *reloc_addr
= ((sym
? (loadbase
+ sym
->st_value
) : 0)
104 case R_SPARC_JMP_SLOT
:
105 loadbase
= (resolve
? (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 1) :
106 /* RESOLVE is null during bootstrap relocation. */
109 Elf32_Addr value
= ((sym
? (loadbase
+ sym
->st_value
) : 0)
111 reloc_addr
[1] = OPCODE_SETHI
| (value
>> 10);
112 reloc_addr
[2] = OPCODE_JMP_G1
| (value
& 0x3ff);
116 loadbase
= (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 0);
117 *(char *) reloc_addr
= ((sym
? (loadbase
+ sym
->st_value
) : 0)
121 loadbase
= (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 0);
122 *(short *) reloc_addr
= ((sym
? (loadbase
+ sym
->st_value
) : 0)
126 loadbase
= (resolve
? (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 0) :
127 /* RESOLVE is null during bootstrap relocation. */
130 case R_SPARC_RELATIVE
:
131 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
134 loadbase
= (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 0);
135 *(char *) reloc_addr
= ((sym
? (loadbase
+ sym
->st_value
) : 0)
137 - (Elf32_Addr
) reloc_addr
);
140 loadbase
= (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 0);
141 *(short *) reloc_addr
= ((sym
? (loadbase
+ sym
->st_value
) : 0)
143 - (Elf32_Addr
) reloc_addr
);
146 loadbase
= (*resolve
) (&sym
, (Elf32_Addr
) reloc_addr
, 0);
147 *reloc_addr
= ((sym
? (loadbase
+ sym
->st_value
) : 0)
149 - (Elf32_Addr
) reloc_addr
);
151 case R_SPARC_NONE
: /* Alright, Wilbur. */
154 assert (! "unexpected dynamic reloc type");
160 elf_machine_lazy_rel (struct link_map
*map
, const Elf32_Rela
*reloc
)
162 switch (ELF32_R_TYPE (reloc
->r_info
))
166 case R_SPARC_JMP_SLOT
:
169 assert (! "unexpected PLT reloc type");
174 /* Nonzero iff TYPE describes relocation of a PLT entry, so
175 PLT entries should not be allowed to define the value. */
176 #define elf_machine_pltrel_p(type) ((type) == R_SPARC_JMP_SLOT)
178 /* The SPARC never uses Elf32_Rel relocations. */
179 #define ELF_MACHINE_NO_REL 1
182 /* Set up the loaded object described by L so its unrelocated PLT
183 entries will jump to the on-demand fixup code in dl-runtime.c. */
186 elf_machine_runtime_setup (struct link_map
*l
, int lazy
)
189 extern void _dl_runtime_resolve (Elf32_Word
);
191 if (l
->l_info
[DT_JMPREL
] && lazy
)
193 /* The entries for functions in the PLT have not yet been filled in.
194 Their initial contents will arrange when called to set the high 22
195 bits of %g1 with an offset into the .rela.plt section and jump to
196 the beginning of the PLT. */
197 plt
= (Elf32_Addr
*) (l
->l_addr
+ l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
199 /* The beginning of the PLT does:
202 pltpc: call _dl_runtime_resolve
206 This saves the register window containing the arguments, and the
207 PC value (pltpc) implicitly saved in %o7 by the call points near the
208 location where we store the link_map pointer for this object. */
210 plt
[0] = OPCODE_SAVE_SP64
; /* save %sp, -64, %sp */
211 /* Construct PC-relative word address. */
212 plt
[1] = OPCODE_CALL
| (((Elf32_Addr
) &_dl_runtime_resolve
-
213 (Elf32_Addr
) &plt
[1]) >> 2);
214 plt
[2] = OPCODE_NOP
; /* Fill call delay slot. */
218 /* This code is used in dl-runtime.c to call the `fixup' function
219 and then redirect to the address it returns. */
220 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
221 | Trampoline for _dl_runtime_resolver
222 .globl _dl_runtime_resolve
223 .type _dl_runtime_resolve, @function
225 | Pass two args to fixup: the PLT address computed from the PC saved
226 | in the PLT's call insn, and the reloc offset passed in %g1.
227 ld [%o7 + 8], %o1 | Second arg, loaded from PLTPC[2].
229 shrl %g1, 22, %o0 | First arg, set in delay slot of call.
230 | Jump to the real function.
232 | In the delay slot of that jump, restore the register window
233 | saved by the first insn of the PLT.
235 .size _dl_runtime_resolve, . - _dl_runtime_resolve
237 /* The PLT uses Elf32_Rela relocs. */
238 #define elf_machine_relplt elf_machine_rela
242 /* Mask identifying addresses reserved for the user program,
243 where the dynamic linker should not map anything. */
244 #define ELF_MACHINE_USER_ADDRESS_MASK ???
246 /* Initial entry point code for the dynamic linker.
247 The C function `_dl_start' is the real entry point;
248 its return value is the user program's entry point. */
250 #define RTLD_START asm (???)