mips_reloc.c revision 1.63 1 1.63 joerg /* $NetBSD: mips_reloc.c,v 1.63 2014/08/25 20:40:52 joerg Exp $ */
2 1.1 mhitch
3 1.1 mhitch /*
4 1.1 mhitch * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
5 1.37 mycroft * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net>
6 1.1 mhitch * All rights reserved.
7 1.1 mhitch *
8 1.1 mhitch * Redistribution and use in source and binary forms, with or without
9 1.1 mhitch * modification, are permitted provided that the following conditions
10 1.1 mhitch * are met:
11 1.1 mhitch * 1. Redistributions of source code must retain the above copyright
12 1.1 mhitch * notice, this list of conditions and the following disclaimer.
13 1.1 mhitch * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 mhitch * notice, this list of conditions and the following disclaimer in the
15 1.1 mhitch * documentation and/or other materials provided with the distribution.
16 1.1 mhitch * 3. The name of the author may not be used to endorse or promote products
17 1.1 mhitch * derived from this software without specific prior written permission.
18 1.1 mhitch *
19 1.1 mhitch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 mhitch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 mhitch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 mhitch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 mhitch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 mhitch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 mhitch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 mhitch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 mhitch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 mhitch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 mhitch */
30 1.1 mhitch
31 1.48 skrll #include <sys/cdefs.h>
32 1.48 skrll #ifndef lint
33 1.63 joerg __RCSID("$NetBSD: mips_reloc.c,v 1.63 2014/08/25 20:40:52 joerg Exp $");
34 1.48 skrll #endif /* not lint */
35 1.48 skrll
36 1.1 mhitch #include <sys/types.h>
37 1.56 matt #include <sys/endian.h>
38 1.61 matt #include <sys/tls.h>
39 1.40 mrg
40 1.40 mrg #include <stdlib.h>
41 1.33 thorpej #include <string.h>
42 1.1 mhitch
43 1.1 mhitch #include "debug.h"
44 1.1 mhitch #include "rtld.h"
45 1.1 mhitch
46 1.56 matt #ifdef __mips_o32
47 1.36 mycroft #define SUPPORT_OLD_BROKEN_LD
48 1.56 matt #endif
49 1.36 mycroft
50 1.22 mycroft void _rtld_bind_start(void);
51 1.17 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
52 1.36 mycroft caddr_t _rtld_bind(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
53 1.6 mycroft
54 1.45 simonb /*
55 1.45 simonb * It is possible for the compiler to emit relocations for unaligned data.
56 1.45 simonb * We handle this situation with these inlines.
57 1.45 simonb */
58 1.45 simonb
59 1.56 matt #if ELFSIZE == 64
60 1.56 matt /*
61 1.56 matt * ELF64 MIPS encodes the relocs uniquely. The first 32-bits of info contain
62 1.56 matt * the symbol index. The top 32-bits contain three relocation types encoded
63 1.56 matt * in big-endian integer with first relocation in LSB. This means for little
64 1.56 matt * endian we have to byte swap that interger (r_type).
65 1.56 matt */
66 1.56 matt #define Elf_Sxword Elf64_Sxword
67 1.56 matt #define ELF_R_NXTTYPE_64_P(r_type) ((((r_type) >> 8) & 0xff) == R_TYPE(64))
68 1.56 matt #if BYTE_ORDER == LITTLE_ENDIAN
69 1.56 matt #undef ELF_R_SYM
70 1.56 matt #undef ELF_R_TYPE
71 1.56 matt #define ELF_R_SYM(r_info) ((r_info) & 0xffffffff)
72 1.56 matt #define ELF_R_TYPE(r_info) bswap32((r_info) >> 32)
73 1.56 matt #endif
74 1.56 matt #else
75 1.56 matt #define ELF_R_NXTTYPE_64_P(r_type) (0)
76 1.56 matt #define Elf_Sxword Elf32_Sword
77 1.56 matt #endif
78 1.60 matt #define GOT1_MASK (~(Elf_Addr)0 >> 1)
79 1.56 matt
80 1.56 matt static inline Elf_Sxword
81 1.56 matt load_ptr(void *where, size_t len)
82 1.45 simonb {
83 1.56 matt Elf_Sxword val;
84 1.45 simonb
85 1.56 matt if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
86 1.56 matt #if ELFSIZE == 64
87 1.56 matt if (len == sizeof(Elf_Sxword))
88 1.56 matt return *(Elf_Sxword *)where;
89 1.56 matt #endif
90 1.56 matt return *(Elf_Sword *)where;
91 1.52 simonb }
92 1.56 matt
93 1.56 matt val = 0;
94 1.56 matt #if BYTE_ORDER == LITTLE_ENDIAN
95 1.56 matt (void)memcpy(&val, where, len);
96 1.56 matt #endif
97 1.56 matt #if BYTE_ORDER == BIG_ENDIAN
98 1.56 matt (void)memcpy((uint8_t *)((&val)+1) - len, where, len);
99 1.56 matt #endif
100 1.56 matt return (len == sizeof(Elf_Sxword)) ? val : (Elf_Sword)val;
101 1.45 simonb }
102 1.45 simonb
103 1.49 perry static inline void
104 1.56 matt store_ptr(void *where, Elf_Sxword val, size_t len)
105 1.45 simonb {
106 1.56 matt if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
107 1.56 matt #if ELFSIZE == 64
108 1.56 matt if (len == sizeof(Elf_Sxword)) {
109 1.56 matt *(Elf_Sxword *)where = val;
110 1.56 matt return;
111 1.56 matt }
112 1.56 matt #endif
113 1.56 matt *(Elf_Sword *)where = val;
114 1.56 matt return;
115 1.56 matt }
116 1.56 matt #if BYTE_ORDER == LITTLE_ENDIAN
117 1.56 matt (void)memcpy(where, &val, len);
118 1.56 matt #endif
119 1.56 matt #if BYTE_ORDER == BIG_ENDIAN
120 1.56 matt (void)memcpy(where, (const uint8_t *)((&val)+1) - len, len);
121 1.56 matt #endif
122 1.45 simonb }
123 1.45 simonb
124 1.45 simonb
125 1.1 mhitch void
126 1.39 skrll _rtld_setup_pltgot(const Obj_Entry *obj)
127 1.1 mhitch {
128 1.6 mycroft obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
129 1.6 mycroft /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
130 1.6 mycroft obj->pltgot[1] |= (Elf_Addr) obj;
131 1.8 mycroft }
132 1.8 mycroft
133 1.17 mycroft void
134 1.39 skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
135 1.17 mycroft {
136 1.17 mycroft const Elf_Rel *rel = 0, *rellim;
137 1.17 mycroft Elf_Addr relsz = 0;
138 1.52 simonb void *where;
139 1.47 he const Elf_Sym *symtab = NULL, *sym;
140 1.47 he Elf_Addr *got = NULL;
141 1.56 matt Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0;
142 1.56 matt size_t i;
143 1.17 mycroft
144 1.17 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
145 1.17 mycroft switch (dynp->d_tag) {
146 1.17 mycroft case DT_REL:
147 1.17 mycroft rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
148 1.17 mycroft break;
149 1.17 mycroft case DT_RELSZ:
150 1.17 mycroft relsz = dynp->d_un.d_val;
151 1.17 mycroft break;
152 1.17 mycroft case DT_SYMTAB:
153 1.17 mycroft symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
154 1.17 mycroft break;
155 1.18 mycroft case DT_PLTGOT:
156 1.18 mycroft got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
157 1.18 mycroft break;
158 1.18 mycroft case DT_MIPS_LOCAL_GOTNO:
159 1.18 mycroft local_gotno = dynp->d_un.d_val;
160 1.18 mycroft break;
161 1.18 mycroft case DT_MIPS_SYMTABNO:
162 1.18 mycroft symtabno = dynp->d_un.d_val;
163 1.18 mycroft break;
164 1.18 mycroft case DT_MIPS_GOTSYM:
165 1.18 mycroft gotsym = dynp->d_un.d_val;
166 1.18 mycroft break;
167 1.17 mycroft }
168 1.17 mycroft }
169 1.34 mycroft
170 1.34 mycroft i = (got[1] & 0x80000000) ? 2 : 1;
171 1.34 mycroft /* Relocate the local GOT entries */
172 1.34 mycroft got += i;
173 1.34 mycroft for (; i < local_gotno; i++)
174 1.34 mycroft *got++ += relocbase;
175 1.34 mycroft sym = symtab + gotsym;
176 1.34 mycroft /* Now do the global GOT entries */
177 1.34 mycroft for (i = gotsym; i < symtabno; i++) {
178 1.34 mycroft *got = sym->st_value + relocbase;
179 1.34 mycroft ++sym;
180 1.34 mycroft ++got;
181 1.34 mycroft }
182 1.34 mycroft
183 1.56 matt rellim = (const Elf_Rel *)((uintptr_t)rel + relsz);
184 1.17 mycroft for (; rel < rellim; rel++) {
185 1.56 matt Elf_Word r_symndx, r_type;
186 1.56 matt
187 1.52 simonb where = (void *)(relocbase + rel->r_offset);
188 1.17 mycroft
189 1.56 matt r_symndx = ELF_R_SYM(rel->r_info);
190 1.56 matt r_type = ELF_R_TYPE(rel->r_info);
191 1.56 matt
192 1.56 matt switch (r_type & 0xff) {
193 1.56 matt case R_TYPE(REL32): {
194 1.56 matt const size_t rlen =
195 1.56 matt ELF_R_NXTTYPE_64_P(r_type)
196 1.56 matt ? sizeof(Elf_Sxword)
197 1.56 matt : sizeof(Elf_Sword);
198 1.56 matt Elf_Sxword old = load_ptr(where, rlen);
199 1.56 matt Elf_Sxword val = old;
200 1.56 matt #if ELFSIZE == 64
201 1.56 matt assert(r_type == R_TYPE(REL32)
202 1.56 matt || r_type == (R_TYPE(REL32)|(R_TYPE(64) << 8)));
203 1.56 matt #endif
204 1.56 matt assert(r_symndx < gotsym);
205 1.56 matt sym = symtab + r_symndx;
206 1.56 matt assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
207 1.56 matt val += relocbase;
208 1.56 matt store_ptr(where, val, sizeof(Elf_Sword));
209 1.56 matt rdbg(("REL32/L(%p) %p -> %p in <self>",
210 1.56 matt where, (void *)old, (void *)val));
211 1.56 matt store_ptr(where, val, rlen);
212 1.56 matt break;
213 1.56 matt }
214 1.56 matt
215 1.56 matt case R_TYPE(GPREL32):
216 1.17 mycroft case R_TYPE(NONE):
217 1.17 mycroft break;
218 1.17 mycroft
219 1.17 mycroft
220 1.17 mycroft default:
221 1.17 mycroft abort();
222 1.17 mycroft }
223 1.17 mycroft }
224 1.17 mycroft }
225 1.17 mycroft
226 1.8 mycroft int
227 1.59 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
228 1.8 mycroft {
229 1.9 mycroft const Elf_Rel *rel;
230 1.18 mycroft Elf_Addr *got = obj->pltgot;
231 1.19 mycroft const Elf_Sym *sym, *def;
232 1.18 mycroft const Obj_Entry *defobj;
233 1.54 christos Elf_Word i;
234 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
235 1.36 mycroft int broken;
236 1.36 mycroft #endif
237 1.36 mycroft
238 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
239 1.36 mycroft broken = 0;
240 1.36 mycroft sym = obj->symtab;
241 1.36 mycroft for (i = 1; i < 12; i++)
242 1.36 mycroft if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
243 1.36 mycroft broken = 1;
244 1.36 mycroft dbg(("%s: broken=%d", obj->path, broken));
245 1.36 mycroft #endif
246 1.17 mycroft
247 1.34 mycroft i = (got[1] & 0x80000000) ? 2 : 1;
248 1.34 mycroft /* Relocate the local GOT entries */
249 1.34 mycroft got += i;
250 1.34 mycroft for (; i < obj->local_gotno; i++)
251 1.34 mycroft *got++ += (Elf_Addr)obj->relocbase;
252 1.34 mycroft sym = obj->symtab + obj->gotsym;
253 1.34 mycroft /* Now do the global GOT entries */
254 1.34 mycroft for (i = obj->gotsym; i < obj->symtabno; i++) {
255 1.56 matt rdbg((" doing got %d sym %p (%s, %lx)", i - obj->gotsym, sym,
256 1.56 matt sym->st_name + obj->strtab, (u_long) *got));
257 1.34 mycroft
258 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
259 1.34 mycroft if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
260 1.36 mycroft broken && sym->st_shndx == SHN_UNDEF) {
261 1.34 mycroft /*
262 1.34 mycroft * XXX DANGER WILL ROBINSON!
263 1.34 mycroft * You might think this is stupid, as it intentionally
264 1.34 mycroft * defeats lazy binding -- and you'd be right.
265 1.34 mycroft * Unfortunately, for lazy binding to work right, we
266 1.34 mycroft * need to a way to force the GOT slots used for
267 1.34 mycroft * function pointers to be resolved immediately. This
268 1.34 mycroft * is supposed to be done automatically by the linker,
269 1.34 mycroft * by not outputting a PLT slot and setting st_value
270 1.36 mycroft * to 0 if there are non-PLT references, but older
271 1.36 mycroft * versions of GNU ld do not do this.
272 1.34 mycroft */
273 1.38 skrll def = _rtld_find_symdef(i, obj, &defobj, false);
274 1.34 mycroft if (def == NULL)
275 1.34 mycroft return -1;
276 1.34 mycroft *got = def->st_value + (Elf_Addr)defobj->relocbase;
277 1.36 mycroft } else
278 1.36 mycroft #endif
279 1.36 mycroft if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
280 1.41 mycroft sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
281 1.34 mycroft /*
282 1.34 mycroft * If there are non-PLT references to the function,
283 1.34 mycroft * st_value should be 0, forcing us to resolve the
284 1.34 mycroft * address immediately.
285 1.41 mycroft *
286 1.41 mycroft * XXX DANGER WILL ROBINSON!
287 1.41 mycroft * The linker is not outputting PLT slots for calls to
288 1.41 mycroft * functions that are defined in the same shared
289 1.42 mycroft * library. This is a bug, because it can screw up
290 1.42 mycroft * link ordering rules if the symbol is defined in
291 1.42 mycroft * more than one module. For now, if there is a
292 1.42 mycroft * definition, we fail the test above and force a full
293 1.44 mycroft * symbol lookup. This means that all intra-module
294 1.44 mycroft * calls are bound immediately. - mycroft, 2003/09/24
295 1.34 mycroft */
296 1.34 mycroft *got = sym->st_value + (Elf_Addr)obj->relocbase;
297 1.34 mycroft } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
298 1.34 mycroft /* Symbols with index SHN_ABS are not relocated. */
299 1.34 mycroft if (sym->st_shndx != SHN_ABS)
300 1.34 mycroft *got = sym->st_value +
301 1.34 mycroft (Elf_Addr)obj->relocbase;
302 1.34 mycroft } else {
303 1.38 skrll def = _rtld_find_symdef(i, obj, &defobj, false);
304 1.34 mycroft if (def == NULL)
305 1.34 mycroft return -1;
306 1.34 mycroft *got = def->st_value + (Elf_Addr)defobj->relocbase;
307 1.34 mycroft }
308 1.34 mycroft
309 1.56 matt rdbg((" --> now %lx", (u_long) *got));
310 1.34 mycroft ++sym;
311 1.34 mycroft ++got;
312 1.34 mycroft }
313 1.34 mycroft
314 1.34 mycroft got = obj->pltgot;
315 1.9 mycroft for (rel = obj->rel; rel < obj->rellim; rel++) {
316 1.56 matt Elf_Word r_symndx, r_type;
317 1.52 simonb void *where;
318 1.9 mycroft
319 1.52 simonb where = obj->relocbase + rel->r_offset;
320 1.56 matt r_symndx = ELF_R_SYM(rel->r_info);
321 1.56 matt r_type = ELF_R_TYPE(rel->r_info);
322 1.9 mycroft
323 1.56 matt switch (r_type & 0xff) {
324 1.9 mycroft case R_TYPE(NONE):
325 1.9 mycroft break;
326 1.9 mycroft
327 1.56 matt case R_TYPE(REL32): {
328 1.9 mycroft /* 32-bit PC-relative reference */
329 1.56 matt const size_t rlen =
330 1.56 matt ELF_R_NXTTYPE_64_P(r_type)
331 1.56 matt ? sizeof(Elf_Sxword)
332 1.56 matt : sizeof(Elf_Sword);
333 1.56 matt Elf_Sxword old = load_ptr(where, rlen);
334 1.56 matt Elf_Sxword val = old;
335 1.56 matt
336 1.56 matt def = obj->symtab + r_symndx;
337 1.56 matt
338 1.56 matt if (r_symndx >= obj->gotsym) {
339 1.56 matt val += got[obj->local_gotno + r_symndx - obj->gotsym];
340 1.56 matt rdbg(("REL32/G(%p) %p --> %p (%s) in %s",
341 1.56 matt where, (void *)old, (void *)val,
342 1.56 matt obj->strtab + def->st_name,
343 1.56 matt obj->path));
344 1.34 mycroft } else {
345 1.9 mycroft /*
346 1.9 mycroft * XXX: ABI DIFFERENCE!
347 1.9 mycroft *
348 1.9 mycroft * Old NetBSD binutils would generate shared
349 1.9 mycroft * libs with section-relative relocations being
350 1.9 mycroft * already adjusted for the start address of
351 1.9 mycroft * the section.
352 1.9 mycroft *
353 1.9 mycroft * New binutils, OTOH, generate shared libs
354 1.9 mycroft * with the same relocations being based at
355 1.9 mycroft * zero, so we need to add in the start address
356 1.9 mycroft * of the section.
357 1.9 mycroft *
358 1.9 mycroft * --rkb, Oct 6, 2001
359 1.9 mycroft */
360 1.34 mycroft
361 1.35 mycroft if (def->st_info ==
362 1.36 mycroft ELF_ST_INFO(STB_LOCAL, STT_SECTION)
363 1.36 mycroft #ifdef SUPPORT_OLD_BROKEN_LD
364 1.36 mycroft && !broken
365 1.36 mycroft #endif
366 1.36 mycroft )
367 1.56 matt val += (Elf_Addr)def->st_value;
368 1.9 mycroft
369 1.56 matt val += (Elf_Addr)obj->relocbase;
370 1.9 mycroft
371 1.56 matt rdbg(("REL32/L(%p) %p -> %p (%s) in %s",
372 1.56 matt where, (void *)old, (void *)val,
373 1.56 matt obj->strtab + def->st_name, obj->path));
374 1.9 mycroft }
375 1.56 matt store_ptr(where, val, rlen);
376 1.9 mycroft break;
377 1.56 matt }
378 1.9 mycroft
379 1.61 matt #if ELFSIZE == 64
380 1.61 matt case R_TYPE(TLS_DTPMOD64):
381 1.61 matt #else
382 1.61 matt case R_TYPE(TLS_DTPMOD32):
383 1.61 matt #endif
384 1.61 matt {
385 1.61 matt Elf_Addr old = load_ptr(where, ELFSIZE / 8);
386 1.61 matt Elf_Addr val = old;
387 1.61 matt
388 1.61 matt def = _rtld_find_symdef(r_symndx, obj, &defobj, false);
389 1.61 matt if (def == NULL)
390 1.61 matt return -1;
391 1.61 matt
392 1.61 matt val += (Elf_Addr)defobj->tlsindex;
393 1.61 matt
394 1.61 matt store_ptr(where, val, ELFSIZE / 8);
395 1.61 matt rdbg(("DTPMOD %s in %s --> %p in %s",
396 1.61 matt obj->strtab + obj->symtab[r_symndx].st_name,
397 1.61 matt obj->path, (void *)old, defobj->path));
398 1.61 matt break;
399 1.61 matt }
400 1.61 matt
401 1.61 matt #if ELFSIZE == 64
402 1.61 matt case R_TYPE(TLS_DTPREL64):
403 1.61 matt #else
404 1.61 matt case R_TYPE(TLS_DTPREL32):
405 1.61 matt #endif
406 1.61 matt {
407 1.61 matt Elf_Addr old = load_ptr(where, ELFSIZE / 8);
408 1.61 matt Elf_Addr val = old;
409 1.61 matt
410 1.61 matt def = _rtld_find_symdef(r_symndx, obj, &defobj, false);
411 1.61 matt if (def == NULL)
412 1.61 matt return -1;
413 1.61 matt
414 1.61 matt if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
415 1.61 matt return -1;
416 1.61 matt
417 1.61 matt val += (Elf_Addr)def->st_value - TLS_DTV_OFFSET;
418 1.61 matt store_ptr(where, val, ELFSIZE / 8);
419 1.61 matt
420 1.61 matt rdbg(("DTPREL %s in %s --> %p in %s",
421 1.61 matt obj->strtab + obj->symtab[r_symndx].st_name,
422 1.61 matt obj->path, (void *)old, defobj->path));
423 1.61 matt break;
424 1.61 matt }
425 1.61 matt
426 1.61 matt #if ELFSIZE == 64
427 1.61 matt case R_TYPE(TLS_TPREL64):
428 1.61 matt #else
429 1.61 matt case R_TYPE(TLS_TPREL32):
430 1.61 matt #endif
431 1.61 matt {
432 1.61 matt Elf_Addr old = load_ptr(where, ELFSIZE / 8);
433 1.61 matt Elf_Addr val = old;
434 1.61 matt
435 1.61 matt def = _rtld_find_symdef(r_symndx, obj, &defobj, false);
436 1.61 matt if (def == NULL)
437 1.61 matt return -1;
438 1.61 matt
439 1.61 matt if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
440 1.61 matt return -1;
441 1.61 matt
442 1.61 matt val += (Elf_Addr)(def->st_value + defobj->tlsoffset
443 1.61 matt - TLS_TP_OFFSET);
444 1.61 matt store_ptr(where, val, ELFSIZE / 8);
445 1.61 matt
446 1.61 matt rdbg(("TPREL %s in %s --> %p in %s",
447 1.61 matt obj->strtab + obj->symtab[r_symndx].st_name,
448 1.61 matt obj->path, (void *)*where, defobj->path));
449 1.61 matt break;
450 1.61 matt }
451 1.61 matt
452 1.9 mycroft default:
453 1.23 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
454 1.9 mycroft "contents = %p, symbol = %s",
455 1.56 matt (u_long)r_symndx, (u_long)ELF_R_TYPE(rel->r_info),
456 1.56 matt (void *)rel->r_offset,
457 1.56 matt (void *)load_ptr(where, sizeof(Elf_Sword)),
458 1.56 matt obj->strtab + obj->symtab[r_symndx].st_name));
459 1.9 mycroft _rtld_error("%s: Unsupported relocation type %ld "
460 1.55 jmmv "in non-PLT relocations",
461 1.9 mycroft obj->path, (u_long) ELF_R_TYPE(rel->r_info));
462 1.9 mycroft return -1;
463 1.8 mycroft }
464 1.18 mycroft }
465 1.18 mycroft
466 1.8 mycroft return 0;
467 1.8 mycroft }
468 1.8 mycroft
469 1.8 mycroft int
470 1.39 skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
471 1.13 mycroft {
472 1.31 mycroft /* PLT fixups were done above in the GOT relocation. */
473 1.28 mycroft return 0;
474 1.36 mycroft }
475 1.36 mycroft
476 1.50 skrll static inline int
477 1.50 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, Elf_Word sym, Elf_Addr *tp)
478 1.36 mycroft {
479 1.50 skrll Elf_Addr *got = obj->pltgot;
480 1.36 mycroft const Elf_Sym *def;
481 1.36 mycroft const Obj_Entry *defobj;
482 1.36 mycroft Elf_Addr new_value;
483 1.36 mycroft
484 1.57 christos def = _rtld_find_plt_symdef(sym, obj, &defobj, tp != NULL);
485 1.57 christos if (__predict_false(def == NULL))
486 1.50 skrll return -1;
487 1.57 christos if (__predict_false(def == &_rtld_sym_zero))
488 1.57 christos return 0;
489 1.36 mycroft
490 1.63 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
491 1.63 joerg if (tp == NULL)
492 1.63 joerg return 0;
493 1.63 joerg new_value = _rtld_resolve_ifunc(defobj, def);
494 1.63 joerg } else {
495 1.63 joerg new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
496 1.63 joerg }
497 1.36 mycroft rdbg(("bind now/fixup in %s --> new=%p",
498 1.36 mycroft defobj->strtab + def->st_name, (void *)new_value));
499 1.50 skrll got[obj->local_gotno + sym - obj->gotsym] = new_value;
500 1.50 skrll
501 1.50 skrll if (tp)
502 1.50 skrll *tp = new_value;
503 1.50 skrll return 0;
504 1.50 skrll }
505 1.50 skrll
506 1.50 skrll caddr_t
507 1.50 skrll _rtld_bind(Elf_Word a0, Elf_Addr a1, Elf_Addr a2, Elf_Addr a3)
508 1.50 skrll {
509 1.50 skrll Elf_Addr *got = (Elf_Addr *)(a2 - 0x7ff0);
510 1.60 matt const Obj_Entry *obj = (Obj_Entry *)(got[1] & GOT1_MASK);
511 1.58 skrll Elf_Addr new_value = 0; /* XXX gcc */
512 1.50 skrll int err;
513 1.50 skrll
514 1.62 joerg _rtld_shared_enter();
515 1.50 skrll err = _rtld_relocate_plt_object(obj, a0, &new_value);
516 1.57 christos if (err)
517 1.50 skrll _rtld_die();
518 1.62 joerg _rtld_shared_exit();
519 1.50 skrll
520 1.50 skrll return (caddr_t)new_value;
521 1.50 skrll }
522 1.50 skrll
523 1.50 skrll int
524 1.50 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
525 1.50 skrll {
526 1.50 skrll const Elf_Sym *sym = obj->symtab + obj->gotsym;
527 1.54 christos Elf_Word i;
528 1.50 skrll
529 1.50 skrll for (i = obj->gotsym; i < obj->symtabno; i++, sym++) {
530 1.50 skrll if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
531 1.50 skrll if (_rtld_relocate_plt_object(obj, i, NULL) < 0)
532 1.50 skrll return -1;
533 1.50 skrll }
534 1.50 skrll
535 1.50 skrll return 0;
536 1.1 mhitch }
537