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