Move primary cache code to libminixfs.
[minix.git] / libexec / ld.elf_so / arch / sparc / mdreloc.c
blob6b10b5861538d9b8e0b4baed162f823bbfced4dd
1 /* $NetBSD: mdreloc.c,v 1.44 2010/08/06 16:33:18 joerg Exp $ */
3 /*-
4 * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg and by Charles M. Hannum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: mdreloc.c,v 1.44 2010/08/06 16:33:18 joerg Exp $");
35 #endif /* not lint */
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include "rtldenv.h"
44 #include "debug.h"
45 #include "rtld.h"
48 * The following table holds for each relocation type:
49 * - the width in bits of the memory location the relocation
50 * applies to (not currently used)
51 * - the number of bits the relocation value must be shifted to the
52 * right (i.e. discard least significant bits) to fit into
53 * the appropriate field in the instruction word.
54 * - flags indicating whether
55 * * the relocation involves a symbol
56 * * the relocation is relative to the current position
57 * * the relocation is for a GOT entry
58 * * the relocation is relative to the load address
61 #define _RF_S 0x80000000 /* Resolve symbol */
62 #define _RF_A 0x40000000 /* Use addend */
63 #define _RF_P 0x20000000 /* Location relative */
64 #define _RF_G 0x10000000 /* GOT offset */
65 #define _RF_B 0x08000000 /* Load address relative */
66 #define _RF_U 0x04000000 /* Unaligned */
67 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
68 #define _RF_RS(s) ( (s) & 0xff) /* right shift */
69 static const int reloc_target_flags[] = {
70 0, /* NONE */
71 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
72 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
73 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
74 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
75 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
76 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
77 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
78 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
79 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
80 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
81 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
82 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
83 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
84 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
85 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
86 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
87 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
88 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
89 _RF_SZ(32) | _RF_RS(0), /* COPY */
90 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */
91 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
92 _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */
93 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
96 #ifdef RTLD_DEBUG_RELOC
97 static const char *reloc_names[] = {
98 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
99 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
100 "22", "13", "LO10", "GOT10", "GOT13",
101 "GOT22", "PC10", "PC22", "WPLT30", "COPY",
102 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32"
104 #endif
106 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
107 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
108 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
109 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0)
110 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
111 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
112 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
114 static const int reloc_target_bitmask[] = {
115 #define _BM(x) (~(-(1ULL << (x))))
116 0, /* NONE */
117 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
118 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
119 _BM(30), _BM(22), /* WDISP30, WDISP22 */
120 _BM(22), _BM(22), /* HI22, _22 */
121 _BM(13), _BM(10), /* RELOC_13, _LO10 */
122 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
123 _BM(10), _BM(22), /* _PC10, _PC22 */
124 _BM(30), 0, /* _WPLT30, _COPY */
125 -1, -1, -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
126 _BM(32) /* _UA32 */
127 #undef _BM
129 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
131 void _rtld_bind_start(void);
132 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
133 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
134 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
135 const Elf_Rela *, Elf_Addr *);
137 void
138 _rtld_setup_pltgot(const Obj_Entry *obj)
141 * PLTGOT is the PLT on the sparc.
142 * The first entry holds the call the dynamic linker.
143 * We construct a `call' sequence that transfers
144 * to `_rtld_bind_start()'.
145 * The second entry holds the object identification.
146 * Note: each PLT entry is three words long.
148 #define SAVE 0x9de3bfa0 /* i.e. `save %sp,-96,%sp' */
149 #define CALL 0x40000000
150 #define NOP 0x01000000
151 obj->pltgot[0] = SAVE;
152 obj->pltgot[1] = CALL |
153 ((Elf_Addr) &_rtld_bind_start - (Elf_Addr) &obj->pltgot[1]) >> 2;
154 obj->pltgot[2] = NOP;
155 obj->pltgot[3] = (Elf_Addr) obj;
158 void
159 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
161 const Elf_Rela *rela = 0, *relalim;
162 Elf_Addr relasz = 0;
163 Elf_Addr *where;
165 for (; dynp->d_tag != DT_NULL; dynp++) {
166 switch (dynp->d_tag) {
167 case DT_RELA:
168 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
169 break;
170 case DT_RELASZ:
171 relasz = dynp->d_un.d_val;
172 break;
175 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
176 for (; rela < relalim; rela++) {
177 where = (Elf_Addr *)(relocbase + rela->r_offset);
178 *where += (Elf_Addr)(relocbase + rela->r_addend);
183 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
185 const Elf_Rela *rela;
187 for (rela = obj->rela; rela < obj->relalim; rela++) {
188 Elf_Addr *where;
189 Elf_Word type, value, mask;
190 const Elf_Sym *def = NULL;
191 const Obj_Entry *defobj = NULL;
192 unsigned long symnum;
194 where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
195 symnum = ELF_R_SYM(rela->r_info);
197 type = ELF_R_TYPE(rela->r_info);
198 if (type == R_TYPE(NONE))
199 continue;
201 /* We do JMP_SLOTs in _rtld_bind() below */
202 if (type == R_TYPE(JMP_SLOT))
203 continue;
205 /* COPY relocs are also handled elsewhere */
206 if (type == R_TYPE(COPY))
207 continue;
210 * We use the fact that relocation types are an `enum'
211 * Note: R_SPARC_6 is currently numerically largest.
213 if (type > R_TYPE(6))
214 return (-1);
216 value = rela->r_addend;
219 * Handle relative relocs here, as an optimization.
221 if (type == R_TYPE(RELATIVE)) {
222 *where += (Elf_Addr)(obj->relocbase + value);
223 rdbg(("RELATIVE in %s --> %p", obj->path,
224 (void *)*where));
225 continue;
228 if (RELOC_RESOLVE_SYMBOL(type)) {
230 /* Find the symbol */
231 def = _rtld_find_symdef(symnum, obj, &defobj, false);
232 if (def == NULL)
233 return (-1);
235 /* Add in the symbol's absolute address */
236 value += (Elf_Word)(defobj->relocbase + def->st_value);
239 if (RELOC_PC_RELATIVE(type)) {
240 value -= (Elf_Word)where;
243 if (RELOC_BASE_RELATIVE(type)) {
245 * Note that even though sparcs use `Elf_rela'
246 * exclusively we still need the implicit memory addend
247 * in relocations referring to GOT entries.
248 * Undoubtedly, someone f*cked this up in the distant
249 * past, and now we're stuck with it in the name of
250 * compatibility for all eternity..
252 * In any case, the implicit and explicit should be
253 * mutually exclusive. We provide a check for that
254 * here.
256 #define DIAGNOSTIC
257 #ifdef DIAGNOSTIC
258 if (value != 0 && *where != 0) {
259 xprintf("BASE_REL(%s): where=%p, *where 0x%x, "
260 "addend=0x%x, base %p\n",
261 obj->path, where, *where,
262 rela->r_addend, obj->relocbase);
264 #endif
265 value += (Elf_Word)(obj->relocbase + *where);
268 mask = RELOC_VALUE_BITMASK(type);
269 value >>= RELOC_VALUE_RIGHTSHIFT(type);
270 value &= mask;
272 if (RELOC_UNALIGNED(type)) {
273 /* Handle unaligned relocations. */
274 Elf_Addr tmp = 0;
275 char *ptr = (char *)where;
276 int i, size = RELOC_TARGET_SIZE(type)/8;
278 /* Read it in one byte at a time. */
279 for (i=0; i<size; i++)
280 tmp = (tmp << 8) | ptr[i];
282 tmp &= ~mask;
283 tmp |= value;
285 /* Write it back out. */
286 for (i=0; i<size; i++)
287 ptr[i] = ((tmp >> (8*i)) & 0xff);
288 #ifdef RTLD_DEBUG_RELOC
289 value = (Elf_Word)tmp;
290 #endif
292 } else {
293 *where &= ~mask;
294 *where |= value;
295 #ifdef RTLD_DEBUG_RELOC
296 value = (Elf_Word)*where;
297 #endif
299 #ifdef RTLD_DEBUG_RELOC
300 if (RELOC_RESOLVE_SYMBOL(type)) {
301 rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
302 obj->strtab + obj->symtab[symnum].st_name,
303 obj->path, (void *)value, defobj->path));
304 } else {
305 rdbg(("%s in %s --> %p", reloc_names[type],
306 obj->path, (void *)value));
308 #endif
310 return (0);
314 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
316 return (0);
319 caddr_t
320 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
322 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
323 Elf_Addr value;
324 int err;
326 value = 0; /* XXX gcc */
328 err = _rtld_relocate_plt_object(obj, rela, &value);
329 if (err)
330 _rtld_die();
332 return (caddr_t)value;
336 _rtld_relocate_plt_objects(const Obj_Entry *obj)
338 const Elf_Rela *rela = obj->pltrela;
340 for (; rela < obj->pltrelalim; rela++)
341 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
342 return -1;
344 return 0;
347 static inline int
348 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
350 const Elf_Sym *def;
351 const Obj_Entry *defobj;
352 Elf_Word *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
353 Elf_Addr value;
354 unsigned long info = rela->r_info;
356 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
358 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
359 if (__predict_false(def == NULL))
360 return -1;
361 if (__predict_false(def == &_rtld_sym_zero))
362 return 0;
364 value = (Elf_Addr)(defobj->relocbase + def->st_value);
365 rdbg(("bind now/fixup in %s --> new=%p",
366 defobj->strtab + def->st_name, (void *)value));
369 * At the PLT entry pointed at by `where', we now construct
370 * a direct transfer to the now fully resolved function
371 * address. The resulting code in the jump slot is:
373 * sethi %hi(roffset), %g1
374 * sethi %hi(addr), %g1
375 * jmp %g1+%lo(addr)
377 * We write the third instruction first, since that leaves the
378 * previous `b,a' at the second word in place. Hence the whole
379 * PLT slot can be atomically change to the new sequence by
380 * writing the `sethi' instruction at word 2.
382 #define SETHI 0x03000000
383 #define JMP 0x81c06000
384 #define NOP 0x01000000
385 where[2] = JMP | (value & 0x000003ff);
386 where[1] = SETHI | ((value >> 10) & 0x003fffff);
387 __asm volatile("iflush %0+8" : : "r" (where));
388 __asm volatile("iflush %0+4" : : "r" (where));
390 if (tp)
391 *tp = value;
393 return 0;