mdreloc.c revision 1.34 1 /* $NetBSD: mdreloc.c,v 1.34 2005/01/05 09:18:53 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg and by Charles M. Hannum.
9 *
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <sys/stat.h>
45
46 #include "rtldenv.h"
47 #include "debug.h"
48 #include "rtld.h"
49
50 /*
51 * The following table holds for each relocation type:
52 * - the width in bits of the memory location the relocation
53 * applies to (not currently used)
54 * - the number of bits the relocation value must be shifted to the
55 * right (i.e. discard least significant bits) to fit into
56 * the appropriate field in the instruction word.
57 * - flags indicating whether
58 * * the relocation involves a symbol
59 * * the relocation is relative to the current position
60 * * the relocation is for a GOT entry
61 * * the relocation is relative to the load address
62 *
63 */
64 #define _RF_S 0x80000000 /* Resolve symbol */
65 #define _RF_A 0x40000000 /* Use addend */
66 #define _RF_P 0x20000000 /* Location relative */
67 #define _RF_G 0x10000000 /* GOT offset */
68 #define _RF_B 0x08000000 /* Load address relative */
69 #define _RF_U 0x04000000 /* Unaligned */
70 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
71 #define _RF_RS(s) ( (s) & 0xff) /* right shift */
72 static const int reloc_target_flags[] = {
73 0, /* NONE */
74 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
75 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
76 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
77 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
78 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
79 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
80 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
81 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
82 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
83 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
84 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
85 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
86 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
87 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
88 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
89 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
90 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
91 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
92 _RF_SZ(32) | _RF_RS(0), /* COPY */
93 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */
94 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
95 _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */
96 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
97
98 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PLT32 */
99 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* HIPLT22 */
100 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
101 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
102 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PCPLT22 */
103 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */
104 _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 10 */
105 _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 11 */
106 _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 64 */
107 _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */
108 _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* HH22 */
109 _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* HM10 */
110 _RF_S|_RF_A|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* LM22 */
111 _RF_S|_RF_A|_RF_P|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PC_HH22 */
112 _RF_S|_RF_A|_RF_P|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PC_HM10 */
113 _RF_S|_RF_A|_RF_P|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* PC_LM22 */
114 _RF_S|_RF_A|_RF_P|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* WDISP16 */
115 _RF_S|_RF_A|_RF_P|/*unknown*/ _RF_SZ(32) | _RF_RS(0), /* WDISP19 */
116 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */
117 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 7 */
118 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 5 */
119 /*unknown*/ _RF_SZ(32) | _RF_RS(0), /* 6 */
120 };
121
122 #ifdef RTLD_DEBUG_RELOC
123 static const char *reloc_names[] = {
124 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
125 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
126 "22", "13", "LO10", "GOT10", "GOT13",
127 "GOT22", "PC10", "PC22", "WPLT30", "COPY",
128 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
129 "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
130 "10", "11", "64", "OLO10", "HH22",
131 "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
132 "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6"
133 };
134 #endif
135
136 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
137 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
138 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
139 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0)
140 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
141 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
142 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
143
144 static const int reloc_target_bitmask[] = {
145 #define _BM(x) (~(-(1ULL << (x))))
146 0, /* NONE */
147 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
148 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
149 _BM(30), _BM(22), /* WDISP30, WDISP22 */
150 _BM(22), _BM(22), /* HI22, _22 */
151 _BM(13), _BM(10), /* RELOC_13, _LO10 */
152 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
153 _BM(10), _BM(22), /* _PC10, _PC22 */
154 _BM(30), 0, /* _WPLT30, _COPY */
155 -1, -1, -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
156 _BM(32), _BM(32), /* _UA32, PLT32 */
157 _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */
158 _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */
159 _BM(10), _BM(11), -1, /* _10, _11, _64 */
160 _BM(10), _BM(22), /* _OLO10, _HH22 */
161 _BM(10), _BM(22), /* _HM10, _LM22 */
162 _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */
163 _BM(16), _BM(19), /* _WDISP16, _WDISP19 */
164 -1, /* GLOB_JMP */
165 _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */
166 #undef _BM
167 };
168 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
169
170 void _rtld_bind_start(void);
171 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
172 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
173
174 void
175 _rtld_setup_pltgot(const Obj_Entry *obj)
176 {
177 /*
178 * PLTGOT is the PLT on the sparc.
179 * The first entry holds the call the dynamic linker.
180 * We construct a `call' sequence that transfers
181 * to `_rtld_bind_start()'.
182 * The second entry holds the object identification.
183 * Note: each PLT entry is three words long.
184 */
185 #define SAVE 0x9de3bfa0 /* i.e. `save %sp,-96,%sp' */
186 #define CALL 0x40000000
187 #define NOP 0x01000000
188 obj->pltgot[0] = SAVE;
189 obj->pltgot[1] = CALL |
190 ((Elf_Addr) &_rtld_bind_start - (Elf_Addr) &obj->pltgot[1]) >> 2;
191 obj->pltgot[2] = NOP;
192 obj->pltgot[3] = (Elf_Addr) obj;
193 }
194
195 void
196 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
197 {
198 const Elf_Rela *rela = 0, *relalim;
199 Elf_Addr relasz = 0;
200 Elf_Addr *where;
201
202 for (; dynp->d_tag != DT_NULL; dynp++) {
203 switch (dynp->d_tag) {
204 case DT_RELA:
205 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
206 break;
207 case DT_RELASZ:
208 relasz = dynp->d_un.d_val;
209 break;
210 }
211 }
212 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
213 for (; rela < relalim; rela++) {
214 where = (Elf_Addr *)(relocbase + rela->r_offset);
215 *where += (Elf_Addr)(relocbase + rela->r_addend);
216 }
217 }
218
219 int
220 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
221 {
222 const Elf_Rela *rela;
223
224 for (rela = obj->rela; rela < obj->relalim; rela++) {
225 Elf_Addr *where;
226 Elf_Word type, value, mask;
227 const Elf_Sym *def = NULL;
228 const Obj_Entry *defobj = NULL;
229 unsigned long symnum;
230
231 where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
232 symnum = ELF_R_SYM(rela->r_info);
233
234 type = ELF_R_TYPE(rela->r_info);
235 if (type == R_TYPE(NONE))
236 continue;
237
238 /* We do JMP_SLOTs in _rtld_bind() below */
239 if (type == R_TYPE(JMP_SLOT))
240 continue;
241
242 /* COPY relocs are also handled elsewhere */
243 if (type == R_TYPE(COPY))
244 continue;
245
246 /*
247 * We use the fact that relocation types are an `enum'
248 * Note: R_SPARC_6 is currently numerically largest.
249 */
250 if (type > R_TYPE(6))
251 return (-1);
252
253 value = rela->r_addend;
254
255 /*
256 * Handle relative relocs here, as an optimization.
257 */
258 if (type == R_TYPE(RELATIVE)) {
259 *where += (Elf_Addr)(obj->relocbase + value);
260 rdbg(("RELATIVE in %s --> %p", obj->path,
261 (void *)*where));
262 continue;
263 }
264
265 if (RELOC_RESOLVE_SYMBOL(type)) {
266
267 /* Find the symbol */
268 def = _rtld_find_symdef(symnum, obj, &defobj, false);
269 if (def == NULL)
270 return (-1);
271
272 /* Add in the symbol's absolute address */
273 value += (Elf_Word)(defobj->relocbase + def->st_value);
274 }
275
276 if (RELOC_PC_RELATIVE(type)) {
277 value -= (Elf_Word)where;
278 }
279
280 if (RELOC_BASE_RELATIVE(type)) {
281 /*
282 * Note that even though sparcs use `Elf_rela'
283 * exclusively we still need the implicit memory addend
284 * in relocations referring to GOT entries.
285 * Undoubtedly, someone f*cked this up in the distant
286 * past, and now we're stuck with it in the name of
287 * compatibility for all eternity..
288 *
289 * In any case, the implicit and explicit should be
290 * mutually exclusive. We provide a check for that
291 * here.
292 */
293 #define DIAGNOSTIC
294 #ifdef DIAGNOSTIC
295 if (value != 0 && *where != 0) {
296 xprintf("BASE_REL(%s): where=%p, *where 0x%x, "
297 "addend=0x%x, base %p\n",
298 obj->path, where, *where,
299 rela->r_addend, obj->relocbase);
300 }
301 #endif
302 value += (Elf_Word)(obj->relocbase + *where);
303 }
304
305 mask = RELOC_VALUE_BITMASK(type);
306 value >>= RELOC_VALUE_RIGHTSHIFT(type);
307 value &= mask;
308
309 if (RELOC_UNALIGNED(type)) {
310 /* Handle unaligned relocations. */
311 Elf_Addr tmp = 0;
312 char *ptr = (char *)where;
313 int i, size = RELOC_TARGET_SIZE(type)/8;
314
315 /* Read it in one byte at a time. */
316 for (i=0; i<size; i++)
317 tmp = (tmp << 8) | ptr[i];
318
319 tmp &= ~mask;
320 tmp |= value;
321
322 /* Write it back out. */
323 for (i=0; i<size; i++)
324 ptr[i] = ((tmp >> (8*i)) & 0xff);
325 #ifdef RTLD_DEBUG_RELOC
326 value = (Elf_Word)tmp;
327 #endif
328
329 } else {
330 *where &= ~mask;
331 *where |= value;
332 #ifdef RTLD_DEBUG_RELOC
333 value = (Elf_Word)*where;
334 #endif
335 }
336 #ifdef RTLD_DEBUG_RELOC
337 if (RELOC_RESOLVE_SYMBOL(type)) {
338 rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
339 obj->strtab + obj->symtab[symnum].st_name,
340 obj->path, (void *)value, defobj->path));
341 } else {
342 rdbg(("%s in %s --> %p", reloc_names[type],
343 obj->path, (void *)value));
344 }
345 #endif
346 }
347 return (0);
348 }
349
350 int
351 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
352 {
353 return (0);
354 }
355
356 caddr_t
357 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
358 {
359 const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
360 const Elf_Sym *def;
361 const Obj_Entry *defobj;
362 Elf_Word *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
363 Elf_Addr value;
364
365 /* Fully resolve procedure addresses now */
366
367 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
368
369 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
370 if (def == NULL)
371 _rtld_die();
372
373 value = (Elf_Addr)(defobj->relocbase + def->st_value);
374 rdbg(("bind now/fixup in %s --> new=%p",
375 defobj->strtab + def->st_name, (void *)value));
376
377 /*
378 * At the PLT entry pointed at by `where', we now construct
379 * a direct transfer to the now fully resolved function
380 * address. The resulting code in the jump slot is:
381 *
382 * sethi %hi(roffset), %g1
383 * sethi %hi(addr), %g1
384 * jmp %g1+%lo(addr)
385 *
386 * We write the third instruction first, since that leaves the
387 * previous `b,a' at the second word in place. Hence the whole
388 * PLT slot can be atomically change to the new sequence by
389 * writing the `sethi' instruction at word 2.
390 */
391 #define SETHI 0x03000000
392 #define JMP 0x81c06000
393 #define NOP 0x01000000
394 where[2] = JMP | (value & 0x000003ff);
395 where[1] = SETHI | ((value >> 10) & 0x003fffff);
396 __asm __volatile("iflush %0+8" : : "r" (where));
397 __asm __volatile("iflush %0+4" : : "r" (where));
398
399 return (caddr_t)value;
400 }
401