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