1 /* $NetBSD: mdreloc.c,v 1.70 2023/06/04 01:24:58 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Eduardo Horvath.
5 * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Paul Kranenburg and by Charles M. Hannum.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __RCSID("$NetBSD: mdreloc.c,v 1.70 2023/06/04 01:24:58 joerg Exp $");
36 #endif /* not lint */
37
38 #include <machine/elf_support.h>
39
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.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[R_TYPE(TLS_TPOFF64)+1] = {
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(64) | _RF_RS(0), /* GLOB_DAT */
94 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
95 _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */
96 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
97
98 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */
99 _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */
100 _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
101 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */
102 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */
103 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */
104 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */
105 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */
106 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */
107 _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */
108 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */
109 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */
110 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */
111 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */
112 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */
113 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */
114 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */
115 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */
116 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */
117 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */
118 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */
119 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */
120 _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */
121 _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */
122 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */
123 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */
124 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */
125 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */
126 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */
127 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */
128 _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */
129 _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */
130 /* TLS relocs not represented here! */
131 };
132
133 #ifdef RTLD_DEBUG_RELOC
134 static const char *reloc_names[] = {
135 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
136 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
137 "22", "13", "LO10", "GOT10", "GOT13",
138 "GOT22", "PC10", "PC22", "WPLT30", "COPY",
139 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
140 "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
141 "10", "11", "64", "OLO10", "HH22",
142 "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
143 "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
144 "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
145 "L44", "REGISTER", "UA64", "UA16",
146 "TLS_GD_HI22", "TLS_GD_LO10", "TLS_GD_ADD", "TLS_GD_CALL",
147 "TLS_LDM_HI22", "TLS_LDM_LO10", "TLS_LDM_ADD", "TLS_LDM_CALL",
148 "TLS_LDO_HIX22", "TLS_LDO_LOX10", "TLS_LDO_ADD", "TLS_IE_HI22",
149 "TLS_IE_LO10", "TLS_IE_LD", "TLS_IE_LDX", "TLS_IE_ADD", "TLS_LE_HIX22",
150 "TLS_LE_LOX10", "TLS_DTPMOD32", "TLS_DTPMOD64", "TLS_DTPOFF32",
151 "TLS_DTPOFF64", "TLS_TPOFF32", "TLS_TPOFF64",
152 };
153 #endif
154
155 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
156 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
157 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
158 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0)
159 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
160 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
161 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
162 #define RELOC_TLS(t) (t >= R_TYPE(TLS_GD_HI22))
163
164 static const long reloc_target_bitmask[] = {
165 #define _BM(x) (~(-(1ULL << (x))))
166 0, /* NONE */
167 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
168 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
169 _BM(30), _BM(22), /* WDISP30, WDISP22 */
170 _BM(22), _BM(22), /* HI22, _22 */
171 _BM(13), _BM(10), /* RELOC_13, _LO10 */
172 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
173 _BM(10), _BM(22), /* _PC10, _PC22 */
174 _BM(30), 0, /* _WPLT30, _COPY */
175 -1, _BM(32), -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
176 _BM(32), _BM(32), /* _UA32, PLT32 */
177 _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */
178 _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */
179 _BM(10), _BM(11), -1, /* _10, _11, _64 */
180 _BM(13), _BM(22), /* _OLO10, _HH22 */
181 _BM(10), _BM(22), /* _HM10, _LM22 */
182 _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */
183 _BM(16), _BM(19), /* _WDISP16, _WDISP19 */
184 -1, /* GLOB_JMP */
185 _BM(7), _BM(5), _BM(6), /* _7, _5, _6 */
186 -1, -1, /* DISP64, PLT64 */
187 _BM(22), _BM(13), /* HIX22, LOX10 */
188 _BM(22), _BM(10), _BM(12), /* H44, M44, L44 */
189 -1, -1, _BM(16), /* REGISTER, UA64, UA16 */
190 #undef _BM
191 };
192 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
193
194 /*
195 * Instruction templates:
196 */
197
198
199 /* %hi(v)/%lo(v) with variable shift */
200 #define HIVAL(v, s) (((v) >> (s)) & 0x003fffff)
201 #define LOVAL(v, s) (((v) >> (s)) & 0x000003ff)
202
203 void _rtld_bind_start_0(long, long);
204 void _rtld_bind_start_1(long, long);
205 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
206 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
207
208 /*
209 * Install rtld function call into this PLT slot.
210 */
211 #define SAVE 0x9de3bf50 /* i.e. `save %sp,-176,%sp' */
212 #define SETHI_l0 0x21000000
213 #define SETHI_l1 0x23000000
214 #define OR_l0_l0 0xa0142000
215 #define SLLX_l0_32_l0 0xa12c3020
216 #define OR_l0_l1_l0 0xa0140011
217 #define JMPL_l0_o0 0x91c42000
218 #define MOV_g1_o1 0x92100001
219
220 void _rtld_install_plt(Elf_Word *, Elf_Addr);
221 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
222 const Elf_Rela *, Elf_Addr *);
223
224 void
_rtld_install_plt(Elf_Word * pltgot,Elf_Addr proc)225 _rtld_install_plt(Elf_Word *pltgot, Elf_Addr proc)
226 {
227 pltgot[0] = SAVE;
228 pltgot[1] = SETHI_l0 | HIVAL(proc, 42);
229 pltgot[2] = SETHI_l1 | HIVAL(proc, 10);
230 pltgot[3] = OR_l0_l0 | LOVAL(proc, 32);
231 pltgot[4] = SLLX_l0_32_l0;
232 pltgot[5] = OR_l0_l1_l0;
233 pltgot[6] = JMPL_l0_o0 | LOVAL(proc, 0);
234 pltgot[7] = MOV_g1_o1;
235 }
236
237 void
_rtld_setup_pltgot(const Obj_Entry * obj)238 _rtld_setup_pltgot(const Obj_Entry *obj)
239 {
240 /*
241 * On sparc64 we got troubles.
242 *
243 * Instructions are 4 bytes long.
244 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
245 * array entries.
246 * Each PLT entry jumps to PLT0 to enter the dynamic
247 * linker.
248 * Loading an arbitrary 64-bit pointer takes 6
249 * instructions and 2 registers.
250 *
251 * Somehow we need to issue a save to get a new stack
252 * frame, load the address of the dynamic linker, and
253 * jump there, in 8 instructions or less.
254 *
255 * Oh, we need to fill out both PLT0 and PLT1.
256 */
257 {
258 Elf_Word *entry = (Elf_Word *)obj->pltgot;
259
260 /* Install in entries 0 and 1 */
261 _rtld_install_plt(&entry[0], (Elf_Addr) &_rtld_bind_start_0);
262 _rtld_install_plt(&entry[8], (Elf_Addr) &_rtld_bind_start_1);
263
264 /*
265 * Install the object reference in first slot
266 * of entry 2.
267 */
268 obj->pltgot[8] = (Elf_Addr) obj;
269 }
270 }
271
272 void
_rtld_relocate_nonplt_self(Elf_Dyn * dynp,Elf_Addr relocbase)273 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
274 {
275 const Elf_Rela *rela = 0, *relalim;
276 Elf_Addr relasz = 0;
277 Elf_Addr *where;
278
279 for (; dynp->d_tag != DT_NULL; dynp++) {
280 switch (dynp->d_tag) {
281 case DT_RELA:
282 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
283 break;
284 case DT_RELASZ:
285 relasz = dynp->d_un.d_val;
286 break;
287 }
288 }
289 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
290 for (; rela < relalim; rela++) {
291 where = (Elf_Addr *)(relocbase + rela->r_offset);
292 *where = (Elf_Addr)(relocbase + rela->r_addend);
293 }
294 }
295
296 int
_rtld_relocate_nonplt_objects(Obj_Entry * obj)297 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
298 {
299 const Elf_Rela *rela;
300 const Elf_Sym *def = NULL;
301 const Obj_Entry *defobj = NULL;
302 unsigned long last_symnum = ULONG_MAX;
303
304 for (rela = obj->rela; rela < obj->relalim; rela++) {
305 Elf_Addr *where;
306 Elf_Word type;
307 Elf_Addr value = 0, mask;
308 unsigned long symnum;
309
310 where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
311
312 type = ELF_R_TYPE(rela->r_info);
313 if (type == R_TYPE(NONE))
314 continue;
315
316 /* OLO10 relocations have extra info */
317 if ((type & 0x00ff) == R_SPARC_OLO10)
318 type = R_SPARC_OLO10;
319
320 /* We do JMP_SLOTs in _rtld_bind() below */
321 if (type == R_TYPE(JMP_SLOT))
322 continue;
323
324 /* IFUNC relocations are handled in _rtld_call_ifunc */
325 if (type == R_TYPE(IRELATIVE)) {
326 if (obj->ifunc_remaining_nonplt == 0) {
327 obj->ifunc_remaining_nonplt =
328 obj->relalim - rela;
329 }
330 continue;
331 }
332
333 /* COPY relocs are also handled elsewhere */
334 if (type == R_TYPE(COPY))
335 continue;
336
337 /*
338 * We use the fact that relocation types are an `enum'
339 * Note: R_SPARC_TLS_TPOFF64 is currently numerically largest.
340 */
341 if (type > R_TYPE(TLS_TPOFF64)) {
342 dbg(("unknown relocation type %x at %p", type, rela));
343 return -1;
344 }
345
346 value = rela->r_addend;
347
348 if (RELOC_RESOLVE_SYMBOL(type) || RELOC_TLS(type)) {
349 symnum = ELF_R_SYM(rela->r_info);
350 if (last_symnum != symnum) {
351 last_symnum = symnum;
352 def = _rtld_find_symdef(symnum, obj, &defobj,
353 false);
354 if (def == NULL)
355 return -1;
356 }
357 }
358
359 /*
360 * Handle TLS relocations here, they are different.
361 */
362 if (RELOC_TLS(type)) {
363 switch (type) {
364 case R_TYPE(TLS_DTPMOD64):
365 *where = (Elf64_Addr)defobj->tlsindex;
366
367 rdbg(("TLS_DTPMOD64 %s in %s --> %p",
368 obj->strtab +
369 obj->symtab[symnum].st_name,
370 obj->path, (void *)*where));
371
372 break;
373
374 case R_TYPE(TLS_DTPOFF64):
375 *where = (Elf64_Addr)(def->st_value
376 + rela->r_addend);
377
378 rdbg(("DTPOFF64 %s in %s --> %p",
379 obj->strtab +
380 obj->symtab[symnum].st_name,
381 obj->path, (void *)*where));
382
383 break;
384
385 case R_TYPE(TLS_TPOFF64):
386 if (!defobj->tls_static &&
387 _rtld_tls_offset_allocate(__UNCONST(defobj)))
388 return -1;
389
390 *where = (Elf64_Addr)(def->st_value -
391 defobj->tlsoffset + rela->r_addend);
392
393 rdbg(("TLS_TPOFF64 %s in %s --> %p",
394 obj->strtab + obj->symtab[symnum].st_name,
395 obj->path, (void *)*where));
396
397 break;
398 }
399 continue;
400 }
401
402 /*
403 * Handle relative relocs here, as an optimization.
404 */
405 if (type == R_TYPE(RELATIVE)) {
406 *where = (Elf_Addr)(obj->relocbase + value);
407 rdbg(("RELATIVE in %s --> %p", obj->path,
408 (void *)*where));
409 continue;
410 }
411
412 if (RELOC_RESOLVE_SYMBOL(type)) {
413 /* Add in the symbol's absolute address */
414 value += (Elf_Addr)(defobj->relocbase + def->st_value);
415 }
416
417 if (type == R_SPARC_OLO10) {
418 value = (value & 0x3ff)
419 + (((Elf64_Xword)rela->r_info<<32)>>40);
420 }
421
422 if (RELOC_PC_RELATIVE(type)) {
423 value -= (Elf_Addr)where;
424 }
425
426 if (RELOC_BASE_RELATIVE(type)) {
427 /*
428 * Note that even though sparcs use `Elf_rela'
429 * exclusively we still need the implicit memory addend
430 * in relocations referring to GOT entries.
431 * Undoubtedly, someone f*cked this up in the distant
432 * past, and now we're stuck with it in the name of
433 * compatibility for all eternity..
434 *
435 * In any case, the implicit and explicit should be
436 * mutually exclusive. We provide a check for that
437 * here.
438 */
439 #ifdef DIAGNOSTIC
440 if (value != 0 && *where != 0) {
441 xprintf("BASE_REL(%s): where=%p, *where 0x%lx, "
442 "addend=0x%lx, base %p\n",
443 obj->path, where, *where,
444 rela->r_addend, obj->relocbase);
445 }
446 #endif
447 /* XXXX -- apparently we ignore the preexisting value */
448 value += (Elf_Addr)(obj->relocbase);
449 }
450
451 mask = RELOC_VALUE_BITMASK(type);
452 value >>= RELOC_VALUE_RIGHTSHIFT(type);
453 value &= mask;
454
455 if (RELOC_UNALIGNED(type)) {
456 /* Handle unaligned relocations. */
457 Elf_Addr tmp = 0;
458 char *ptr = (char *)where;
459 int i, size = RELOC_TARGET_SIZE(type)/8;
460
461 /* Read it in one byte at a time. */
462 for (i=0; i<size; i++)
463 tmp = (tmp << 8) | ptr[i];
464
465 tmp &= ~mask;
466 tmp |= value;
467
468 /* Write it back out. */
469 for (i=0; i<size; i++)
470 ptr[i] = ((tmp >> (8*i)) & 0xff);
471 #ifdef RTLD_DEBUG_RELOC
472 value = (Elf_Addr)tmp;
473 #endif
474
475 } else if (RELOC_TARGET_SIZE(type) > 32) {
476 *where &= ~mask;
477 *where |= value;
478 #ifdef RTLD_DEBUG_RELOC
479 value = (Elf_Addr)*where;
480 #endif
481 } else {
482 Elf32_Addr *where32 = (Elf32_Addr *)where;
483
484 *where32 &= ~mask;
485 *where32 |= value;
486 #ifdef RTLD_DEBUG_RELOC
487 value = (Elf_Addr)*where32;
488 #endif
489 }
490
491 #ifdef RTLD_DEBUG_RELOC
492 if (RELOC_RESOLVE_SYMBOL(type)) {
493 rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
494 obj->strtab + obj->symtab[symnum].st_name,
495 obj->path, (void *)value, defobj->path));
496 } else {
497 rdbg(("%s in %s --> %p", reloc_names[type],
498 obj->path, (void *)value));
499 }
500 #endif
501 }
502 return (0);
503 }
504
505 int
_rtld_relocate_plt_lazy(Obj_Entry * obj)506 _rtld_relocate_plt_lazy(Obj_Entry *obj)
507 {
508 const Elf_Rela *rela;
509
510 for (rela = obj->pltrelalim; rela-- > obj->pltrela; ) {
511 if (ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_IREL))
512 obj->ifunc_remaining = obj->pltrelalim - rela + 1;
513 }
514
515 return 0;
516 }
517
518 caddr_t
_rtld_bind(const Obj_Entry * obj,Elf_Word reloff)519 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
520 {
521 const Elf_Rela *rela = obj->pltrela + reloff;
522 Elf_Addr result;
523 int err;
524
525 result = 0; /* XXX gcc */
526
527 if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT) ||
528 ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_IREL)) {
529 /*
530 * XXXX
531 *
532 * The first four PLT entries are reserved. There is some
533 * disagreement whether they should have associated relocation
534 * entries. Both the SPARC 32-bit and 64-bit ELF
535 * specifications say that they should have relocation entries,
536 * but the 32-bit SPARC binutils do not generate them, and now
537 * the 64-bit SPARC binutils have stopped generating them too.
538 *
539 * So, to provide binary compatibility, we will check the first
540 * entry, if it is reserved it should not be of the type
541 * JMP_SLOT or JMP_REL. If it is either of those, then
542 * the 4 reserved entries were not generated and our index
543 * is 4 entries too far.
544 */
545 rela -= 4;
546 }
547
548 _rtld_shared_enter();
549 err = _rtld_relocate_plt_object(obj, rela, &result);
550 if (err)
551 _rtld_die();
552 _rtld_shared_exit();
553
554 return (caddr_t)result;
555 }
556
557 int
_rtld_relocate_plt_objects(const Obj_Entry * obj)558 _rtld_relocate_plt_objects(const Obj_Entry *obj)
559 {
560 const Elf_Rela *rela;
561
562 rela = obj->pltrela;
563
564 /*
565 * Check for first four reserved entries - and skip them.
566 * See above for details.
567 */
568 if (ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_SLOT) &&
569 ELF_R_TYPE(obj->pltrela->r_info) != R_TYPE(JMP_IREL))
570 rela += 4;
571
572 for (; rela < obj->pltrelalim; rela++)
573 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
574 return -1;
575
576 return 0;
577 }
578
579 static inline void
_rtld_write_plt(Elf_Word * where,Elf_Addr value,const Elf_Rela * rela,const Obj_Entry * obj)580 _rtld_write_plt(Elf_Word *where, Elf_Addr value, const Elf_Rela *rela,
581 const Obj_Entry *obj)
582 {
583 if (rela && rela->r_addend) {
584 Elf_Addr *ptr = (Elf_Addr *)where;
585 /*
586 * This entry is >= 32768. The relocations points to a
587 * PC-relative pointer to the bind_0 stub at the top of the
588 * PLT section. Update it to point to the target function.
589 */
590 ptr[0] += value - (Elf_Addr)obj->pltgot;
591 } else {
592 sparc_write_branch(where + 1, (void *)value);
593 }
594 }
595
596 /*
597 * New inline function that is called by _rtld_relocate_plt_object and
598 * _rtld_bind
599 */
600 static inline int
_rtld_relocate_plt_object(const Obj_Entry * obj,const Elf_Rela * rela,Elf_Addr * tp)601 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
602 Elf_Addr *tp)
603 {
604 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
605 const Elf_Sym *def;
606 const Obj_Entry *defobj;
607 Elf_Addr value;
608 unsigned long info = rela->r_info;
609
610 if (ELF_R_TYPE(info) == R_TYPE(JMP_IREL))
611 return 0;
612
613 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
614
615 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
616 if (__predict_false(def == NULL))
617 return -1;
618 if (__predict_false(def == &_rtld_sym_zero))
619 return 0;
620
621 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
622 if (tp == NULL)
623 return 0;
624 value = _rtld_resolve_ifunc(defobj, def);
625 } else {
626 value = (Elf_Addr)(defobj->relocbase + def->st_value);
627 }
628 rdbg(("bind now/fixup in %s at %p --> new=%p",
629 defobj->strtab + def->st_name, (void*)where, (void *)value));
630
631 _rtld_write_plt(where, value, rela, obj);
632
633 if (tp)
634 *tp = value;
635
636 return 0;
637 }
638