mdreloc.c revision 1.54 1 /* $NetBSD: mdreloc.c,v 1.54 2018/03/29 13:23:39 joerg 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 *
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.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: mdreloc.c,v 1.54 2018/03/29 13:23:39 joerg Exp $");
35 #endif /* not lint */
36
37 #include <machine/elf_support.h>
38
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include "rtldenv.h"
46 #include "debug.h"
47 #include "rtld.h"
48
49 /*
50 * The following table holds for each relocation type:
51 * - the width in bits of the memory location the relocation
52 * applies to (not currently used)
53 * - the number of bits the relocation value must be shifted to the
54 * right (i.e. discard least significant bits) to fit into
55 * the appropriate field in the instruction word.
56 * - flags indicating whether
57 * * the relocation involves a symbol
58 * * the relocation is relative to the current position
59 * * the relocation is for a GOT entry
60 * * the relocation is relative to the load address
61 *
62 */
63 #define _RF_S 0x80000000 /* Resolve symbol */
64 #define _RF_A 0x40000000 /* Use addend */
65 #define _RF_P 0x20000000 /* Location relative */
66 #define _RF_G 0x10000000 /* GOT offset */
67 #define _RF_B 0x08000000 /* Load address relative */
68 #define _RF_U 0x04000000 /* Unaligned */
69 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
70 #define _RF_RS(s) ( (s) & 0xff) /* right shift */
71 static const int reloc_target_flags[R_TYPE(TLS_TPOFF64)+1] = {
72 0, /* NONE */
73 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
74 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
75 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
76 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
77 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
78 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
79 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
80 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
81 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
82 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
83 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
84 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
85 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
86 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
87 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
88 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
89 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
90 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
91 _RF_SZ(32) | _RF_RS(0), /* COPY */
92 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */
93 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
94 _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */
95 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
96
97 /* TLS and 64 bit relocs not listed here... */
98 };
99
100 #ifdef RTLD_DEBUG_RELOC
101 static const char *reloc_names[] = {
102 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
103 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
104 "22", "13", "LO10", "GOT10", "GOT13",
105 "GOT22", "PC10", "PC22", "WPLT30", "COPY",
106 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32",
107
108 /* not used with 32bit userland, besides a few of the TLS ones */
109 "PLT32",
110 "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
111 "10", "11", "64", "OLO10", "HH22",
112 "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
113 "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
114 "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
115 "L44", "REGISTER", "UA64", "UA16",
116 "TLS_GD_HI22", "TLS_GD_LO10", "TLS_GD_ADD", "TLS_GD_CALL",
117 "TLS_LDM_HI22", "TLS_LDM_LO10", "TLS_LDM_ADD", "TLS_LDM_CALL",
118 "TLS_LDO_HIX22", "TLS_LDO_LOX10", "TLS_LDO_ADD", "TLS_IE_HI22",
119 "TLS_IE_LO10", "TLS_IE_LD", "TLS_IE_LDX", "TLS_IE_ADD", "TLS_LE_HIX22",
120 "TLS_LE_LOX10", "TLS_DTPMOD32", "TLS_DTPMOD64", "TLS_DTPOFF32",
121 "TLS_DTPOFF64", "TLS_TPOFF32", "TLS_TPOFF64",
122 };
123 #endif
124
125 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
126 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
127 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
128 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0)
129 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
130 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
131 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
132 #define RELOC_TLS(t) (t >= R_TYPE(TLS_GD_HI22))
133
134 static const int reloc_target_bitmask[] = {
135 #define _BM(x) (~(-(1ULL << (x))))
136 0, /* NONE */
137 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
138 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
139 _BM(30), _BM(22), /* WDISP30, WDISP22 */
140 _BM(22), _BM(22), /* HI22, _22 */
141 _BM(13), _BM(10), /* RELOC_13, _LO10 */
142 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
143 _BM(10), _BM(22), /* _PC10, _PC22 */
144 _BM(30), 0, /* _WPLT30, _COPY */
145 -1, -1, -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
146 _BM(32) /* _UA32 */
147 #undef _BM
148 };
149 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
150
151 void _rtld_bind_start(void);
152 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
153 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
154 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
155 const Elf_Rela *, Elf_Addr *);
156
157 void
158 _rtld_setup_pltgot(const Obj_Entry *obj)
159 {
160 /*
161 * PLTGOT is the PLT on the sparc.
162 * The first entry holds the call the dynamic linker.
163 * We construct a `call' sequence that transfers
164 * to `_rtld_bind_start()'.
165 * The second entry holds the object identification.
166 * Note: each PLT entry is three words long.
167 */
168 #define SAVE 0x9de3bfa0 /* i.e. `save %sp,-96,%sp' */
169 #define CALL 0x40000000
170 #define NOP 0x01000000
171 obj->pltgot[0] = SAVE;
172 obj->pltgot[1] = CALL |
173 ((Elf_Addr) &_rtld_bind_start - (Elf_Addr) &obj->pltgot[1]) >> 2;
174 obj->pltgot[2] = NOP;
175 obj->pltgot[3] = (Elf_Addr) obj;
176 }
177
178 void
179 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
180 {
181 const Elf_Rela *rela = 0, *relalim;
182 Elf_Addr relasz = 0;
183 Elf_Addr *where;
184
185 for (; dynp->d_tag != DT_NULL; dynp++) {
186 switch (dynp->d_tag) {
187 case DT_RELA:
188 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
189 break;
190 case DT_RELASZ:
191 relasz = dynp->d_un.d_val;
192 break;
193 }
194 }
195 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
196 for (; rela < relalim; rela++) {
197 where = (Elf_Addr *)(relocbase + rela->r_offset);
198 *where += (Elf_Addr)(relocbase + rela->r_addend);
199 }
200 }
201
202 int
203 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
204 {
205 const Elf_Rela *rela;
206 const Elf_Sym *def = NULL;
207 const Obj_Entry *defobj = NULL;
208 unsigned long last_symnum = ULONG_MAX;
209
210 for (rela = obj->rela; rela < obj->relalim; rela++) {
211 Elf_Addr *where;
212 Elf_Word type, value, mask;
213 unsigned long symnum;
214
215 where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
216
217 type = ELF_R_TYPE(rela->r_info);
218 if (type == R_TYPE(NONE))
219 continue;
220
221 /* We do JMP_SLOTs in _rtld_bind() below */
222 if (type == R_TYPE(JMP_SLOT))
223 continue;
224
225 /* IFUNC relocations are handled in _rtld_call_ifunc */
226 if (type == R_TYPE(IRELATIVE)) {
227 if (obj->ifunc_remaining_nonplt == 0)
228 obj->ifunc_remaining_nonplt = rela - obj->rela + 1;
229 continue;
230 }
231
232 /* COPY relocs are also handled elsewhere */
233 if (type == R_TYPE(COPY))
234 continue;
235
236 /*
237 * We use the fact that relocation types are an `enum'
238 * Note: R_SPARC_TLS_TPOFF64 is currently numerically largest.
239 */
240 if (type > R_TYPE(TLS_TPOFF64))
241 return (-1);
242
243 value = rela->r_addend;
244
245 if (RELOC_RESOLVE_SYMBOL(type) || RELOC_TLS(type)) {
246 symnum = ELF_R_SYM(rela->r_info);
247 if (last_symnum != symnum) {
248 last_symnum = symnum;
249 def = _rtld_find_symdef(symnum, obj, &defobj,
250 false);
251 if (def == NULL)
252 return -1;
253 }
254 }
255
256 /*
257 * Handle TLS relocations here, they are different.
258 */
259 if (RELOC_TLS(type)) {
260 switch (type) {
261 case R_TYPE(TLS_DTPMOD32):
262 *where = (Elf_Addr)defobj->tlsindex;
263
264 rdbg(("TLS_DTPMOD32 %s in %s --> %p",
265 obj->strtab +
266 obj->symtab[symnum].st_name,
267 obj->path, (void *)*where));
268
269 break;
270
271 case R_TYPE(TLS_DTPOFF32):
272 *where = (Elf_Addr)(def->st_value
273 + rela->r_addend);
274
275 rdbg(("TLS_DTPOFF32 %s in %s --> %p",
276 obj->strtab +
277 obj->symtab[symnum].st_name,
278 obj->path, (void *)*where));
279
280 break;
281
282 case R_TYPE(TLS_TPOFF32):
283 if (!defobj->tls_done &&
284 _rtld_tls_offset_allocate(obj))
285 return -1;
286
287 *where = (Elf_Addr)(def->st_value -
288 defobj->tlsoffset + rela->r_addend);
289
290 rdbg(("TLS_TPOFF32 %s in %s --> %p",
291 obj->strtab +
292 obj->symtab[symnum].st_name,
293 obj->path, (void *)*where));
294
295 break;
296 }
297 continue;
298 }
299
300 /*
301 * If it is no TLS relocation (handled above), we can not
302 * deal with it if it is beyound R_SPARC_6.
303 */
304 if (type > R_TYPE(6))
305 return (-1);
306
307 /*
308 * Handle relative relocs here, as an optimization.
309 */
310 if (type == R_TYPE(RELATIVE)) {
311 *where += (Elf_Addr)(obj->relocbase + value);
312 rdbg(("RELATIVE in %s --> %p", obj->path,
313 (void *)*where));
314 continue;
315 }
316
317 if (RELOC_RESOLVE_SYMBOL(type)) {
318 /* Add in the symbol's absolute address */
319 value += (Elf_Word)(defobj->relocbase + def->st_value);
320 }
321
322 if (RELOC_PC_RELATIVE(type)) {
323 value -= (Elf_Word)where;
324 }
325
326 if (RELOC_BASE_RELATIVE(type)) {
327 /*
328 * Note that even though sparcs use `Elf_rela'
329 * exclusively we still need the implicit memory addend
330 * in relocations referring to GOT entries.
331 * Undoubtedly, someone f*cked this up in the distant
332 * past, and now we're stuck with it in the name of
333 * compatibility for all eternity..
334 *
335 * In any case, the implicit and explicit should be
336 * mutually exclusive. We provide a check for that
337 * here.
338 */
339 #define DIAGNOSTIC
340 #ifdef DIAGNOSTIC
341 if (value != 0 && *where != 0) {
342 xprintf("BASE_REL(%s): where=%p, *where 0x%x, "
343 "addend=0x%x, base %p\n",
344 obj->path, where, *where,
345 rela->r_addend, obj->relocbase);
346 }
347 #endif
348 value += (Elf_Word)(obj->relocbase + *where);
349 }
350
351 mask = RELOC_VALUE_BITMASK(type);
352 value >>= RELOC_VALUE_RIGHTSHIFT(type);
353 value &= mask;
354
355 if (RELOC_UNALIGNED(type)) {
356 /* Handle unaligned relocations. */
357 Elf_Addr tmp = 0;
358 char *ptr = (char *)where;
359 int i, size = RELOC_TARGET_SIZE(type)/8;
360
361 /* Read it in one byte at a time. */
362 for (i=0; i<size; i++)
363 tmp = (tmp << 8) | ptr[i];
364
365 tmp &= ~mask;
366 tmp |= value;
367
368 /* Write it back out. */
369 for (i=0; i<size; i++)
370 ptr[i] = ((tmp >> (8*i)) & 0xff);
371 #ifdef RTLD_DEBUG_RELOC
372 value = (Elf_Word)tmp;
373 #endif
374
375 } else {
376 *where &= ~mask;
377 *where |= value;
378 #ifdef RTLD_DEBUG_RELOC
379 value = (Elf_Word)*where;
380 #endif
381 }
382 #ifdef RTLD_DEBUG_RELOC
383 if (RELOC_RESOLVE_SYMBOL(type)) {
384 rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
385 obj->strtab + obj->symtab[symnum].st_name,
386 obj->path, (void *)value, defobj->path));
387 } else {
388 rdbg(("%s in %s --> %p", reloc_names[type],
389 obj->path, (void *)value));
390 }
391 #endif
392 }
393 return (0);
394 }
395
396 int
397 _rtld_relocate_plt_lazy(Obj_Entry *obj)
398 {
399 const Elf_Rela *rela;
400
401 for (rela = obj->pltrelalim; rela-- > obj->pltrela; ) {
402 if (ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_IREL))
403 obj->ifunc_remaining = obj->pltrelalim - rela + 1;
404 }
405
406 return 0;
407 }
408
409 void
410 _rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
411 {
412 const Elf_Rela *rela;
413 Elf_Addr *where, target;
414
415 while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
416 rela = obj->pltrelalim - --obj->ifunc_remaining;
417 if (ELF_R_TYPE(rela->r_info) != R_TYPE(JMP_IREL))
418 continue;
419 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
420 target = (Elf_Addr)(obj->relocbase + rela->r_addend);
421 _rtld_exclusive_exit(mask);
422 target = _rtld_resolve_ifunc2(obj, target);
423 _rtld_exclusive_enter(mask);
424 sparc_write_branch(where + 1, (void *)target);
425 }
426
427 while (obj->ifunc_remaining_nonplt > 0 && _rtld_objgen == cur_objgen) {
428 rela = obj->relalim - --obj->ifunc_remaining_nonplt;
429 if (ELF_R_TYPE(rela->r_info) != R_TYPE(IRELATIVE))
430 continue;
431 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
432 target = (Elf_Addr)(obj->relocbase + rela->r_addend);
433 _rtld_exclusive_exit(mask);
434 target = _rtld_resolve_ifunc2(obj, target);
435 _rtld_exclusive_enter(mask);
436 if (*where != target)
437 *where = target;
438 }
439 }
440
441 caddr_t
442 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
443 {
444 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
445 Elf_Addr value;
446 int err;
447
448 value = 0; /* XXX gcc */
449
450 _rtld_shared_enter();
451 err = _rtld_relocate_plt_object(obj, rela, &value);
452 if (err)
453 _rtld_die();
454 _rtld_shared_exit();
455
456 return (caddr_t)value;
457 }
458
459 int
460 _rtld_relocate_plt_objects(const Obj_Entry *obj)
461 {
462 const Elf_Rela *rela = obj->pltrela;
463
464 for (; rela < obj->pltrelalim; rela++)
465 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
466 return -1;
467
468 return 0;
469 }
470
471 static inline int
472 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
473 {
474 const Elf_Sym *def;
475 const Obj_Entry *defobj;
476 Elf_Word *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
477 Elf_Addr value;
478 unsigned long info = rela->r_info;
479
480 if (ELF_R_TYPE(info) == R_TYPE(JMP_IREL))
481 return 0;
482
483 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
484
485 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
486 if (__predict_false(def == NULL))
487 return -1;
488 if (__predict_false(def == &_rtld_sym_zero))
489 return 0;
490
491 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
492 if (tp == NULL)
493 return 0;
494 value = _rtld_resolve_ifunc(defobj, def);
495 } else {
496 value = (Elf_Addr)(defobj->relocbase + def->st_value);
497 }
498 rdbg(("bind now/fixup in %s --> new=%p",
499 defobj->strtab + def->st_name, (void *)value));
500
501 sparc_write_branch(where + 1, (void *)value);
502
503 if (tp)
504 *tp = value;
505
506 return 0;
507 }
508