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