1 /* $NetBSD: mips_reloc.c,v 1.58 2010/01/14 11:57:06 skrll Exp $ */
4 * Copyright 1997 Michael L. Hitch <mhitch@montana.edu>
5 * Portions copyright 2002 Charles M. Hannum <root@ihack.net>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
36 #include <sys/endian.h>
42 #include <machine/sysarch.h>
43 #include <machine/tls.h>
49 #define GOT1_MASK 0x8000000000000000UL
51 #define GOT1_MASK 0x80000000UL
55 init_pltgot(Obj_Entry *obj)
57 if (obj->pltgot != NULL) {
58 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
59 if (obj->pltgot[1] & 0x80000000)
60 obj->pltgot[1] = (Elf_Addr) obj | GOT1_MASK;
65 do_copy_relocations(Obj_Entry *dstobj)
71 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
74 * It is possible for the compiler to emit relocations for unaligned data.
75 * We handle this situation with these inlines.
79 * ELF64 MIPS encodes the relocs uniquely. The first 32-bits of info contain
80 * the symbol index. The top 32-bits contain three relocation types encoded
81 * in big-endian integer with first relocation in LSB. This means for little
82 * endian we have to byte swap that integer (r_type).
84 #define Elf_Sxword Elf64_Sxword
85 #define ELF_R_NXTTYPE_64_P(r_type) ((((r_type) >> 8) & 0xff) == R_TYPE(64))
86 #if BYTE_ORDER == LITTLE_ENDIAN
89 #define ELF_R_SYM(r_info) ((r_info) & 0xffffffff)
90 #define ELF_R_TYPE(r_info) bswap32((r_info) >> 32)
93 #define ELF_R_NXTTYPE_64_P(r_type) (0)
94 #define Elf_Sxword Elf32_Sword
97 static __inline Elf_Sxword
98 load_ptr(void *where, size_t len)
102 if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
104 if (len == sizeof(Elf_Sxword))
105 return *(Elf_Sxword *)where;
107 return *(Elf_Sword *)where;
111 #if BYTE_ORDER == LITTLE_ENDIAN
112 (void)memcpy(&val, where, len);
114 #if BYTE_ORDER == BIG_ENDIAN
115 (void)memcpy((uint8_t *)((&val)+1) - len, where, len);
117 return (len == sizeof(Elf_Sxword)) ? val : (Elf_Sword)val;
121 store_ptr(void *where, Elf_Sxword val, size_t len)
123 if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
125 if (len == sizeof(Elf_Sxword)) {
126 *(Elf_Sxword *)where = val;
130 *(Elf_Sword *)where = val;
133 #if BYTE_ORDER == LITTLE_ENDIAN
134 (void)memcpy(where, &val, len);
136 #if BYTE_ORDER == BIG_ENDIAN
137 (void)memcpy(where, (const uint8_t *)((&val)+1) - len, len);
142 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
144 const Elf_Rel *rel = 0, *rellim;
146 const Elf_Sym *symtab = NULL, *sym;
148 Elf_Addr *got = NULL;
149 Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0;
152 for (; dynp->d_tag != DT_NULL; dynp++) {
153 switch (dynp->d_tag) {
155 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
158 relsz = dynp->d_un.d_val;
161 symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
164 got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
166 case DT_MIPS_LOCAL_GOTNO:
167 local_gotno = dynp->d_un.d_val;
169 case DT_MIPS_SYMTABNO:
170 symtabno = dynp->d_un.d_val;
173 gotsym = dynp->d_un.d_val;
178 i = (got[1] & GOT1_MASK) ? 2 : 1;
179 /* Relocate the local GOT entries */
181 for (; i < local_gotno; i++) {
185 sym = symtab + gotsym;
186 /* Now do the global GOT entries */
187 for (i = gotsym; i < symtabno; i++) {
188 *got = sym->st_value + relocbase;
193 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
194 for (; rel < rellim; rel++) {
195 Elf_Word r_symndx, r_type;
197 where = (void *)(relocbase + rel->r_offset);
199 r_symndx = ELF_R_SYM(rel->r_info);
200 r_type = ELF_R_TYPE(rel->r_info);
202 switch (r_type & 0xff) {
203 case R_TYPE(REL32): {
205 ELF_R_NXTTYPE_64_P(r_type)
208 Elf_Sxword old = load_ptr(where, rlen);
209 Elf_Sxword val = old;
211 assert(r_type == R_TYPE(REL32)
212 || r_type == (R_TYPE(REL32)|(R_TYPE(64) << 8)));
214 assert(r_symndx < gotsym);
215 sym = symtab + r_symndx;
216 assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
218 store_ptr(where, val, sizeof(Elf_Sword));
219 dbg("REL32/L(%p) %p -> %p in <self>",
220 where, (void *)old, (void *)val);
221 store_ptr(where, val, rlen);
225 case R_TYPE(GPREL32):
238 _mips_rtld_bind(Obj_Entry *obj, Elf_Size reloff)
240 Elf_Addr *got = obj->pltgot;
242 const Obj_Entry *defobj;
245 def = find_symdef(reloff, obj, &defobj, SYMLOOK_IN_PLT, NULL,
250 target = (Elf_Addr)(defobj->relocbase + def->st_value);
251 dbg("bind now/fixup at %s sym # %jd in %s --> was=%p new=%p",
253 (intmax_t)reloff, defobj->strtab + def->st_name,
254 (void *)got[obj->local_gotno + reloff - obj->gotsym],
256 got[obj->local_gotno + reloff - obj->gotsym] = target;
257 return (Elf_Addr)target;
261 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
262 RtldLockState *lockstate)
265 const Elf_Rel *rellim;
266 Elf_Addr *got = obj->pltgot;
267 const Elf_Sym *sym, *def;
268 const Obj_Entry *defobj;
270 #ifdef SUPPORT_OLD_BROKEN_LD
274 /* The relocation for the dynamic loader has already been done. */
278 if ((flags & SYMLOOK_IFUNC) != 0)
279 /* XXX not implemented */
282 #ifdef SUPPORT_OLD_BROKEN_LD
285 for (i = 1; i < 12; i++)
286 if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
288 dbg("%s: broken=%d", obj->path, broken);
291 i = (got[1] & GOT1_MASK) ? 2 : 1;
293 /* Relocate the local GOT entries */
295 dbg("got:%p for %d entries adding %p",
296 got, obj->local_gotno, obj->relocbase);
297 for (; i < obj->local_gotno; i++) {
298 *got += (Elf_Addr)obj->relocbase;
301 sym = obj->symtab + obj->gotsym;
303 dbg("got:%p for %d entries",
305 /* Now do the global GOT entries */
306 for (i = obj->gotsym; i < obj->symtabno; i++) {
307 dbg(" doing got %d sym %p (%s, %lx)", i - obj->gotsym, sym,
308 sym->st_name + obj->strtab, (u_long) *got);
310 #ifdef SUPPORT_OLD_BROKEN_LD
311 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
312 broken && sym->st_shndx == SHN_UNDEF) {
314 * XXX DANGER WILL ROBINSON!
315 * You might think this is stupid, as it intentionally
316 * defeats lazy binding -- and you'd be right.
317 * Unfortunately, for lazy binding to work right, we
318 * need to a way to force the GOT slots used for
319 * function pointers to be resolved immediately. This
320 * is supposed to be done automatically by the linker,
321 * by not outputting a PLT slot and setting st_value
322 * to 0 if there are non-PLT references, but older
323 * versions of GNU ld do not do this.
325 def = find_symdef(i, obj, &defobj, flags, NULL,
329 *got = def->st_value + (Elf_Addr)defobj->relocbase;
332 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
333 sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
335 * If there are non-PLT references to the function,
336 * st_value should be 0, forcing us to resolve the
337 * address immediately.
339 * XXX DANGER WILL ROBINSON!
340 * The linker is not outputting PLT slots for calls to
341 * functions that are defined in the same shared
342 * library. This is a bug, because it can screw up
343 * link ordering rules if the symbol is defined in
344 * more than one module. For now, if there is a
345 * definition, we fail the test above and force a full
346 * symbol lookup. This means that all intra-module
347 * calls are bound immediately. - mycroft, 2003/09/24
349 *got = sym->st_value + (Elf_Addr)obj->relocbase;
350 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
351 dbg("Warning2, i:%d maps to relocbase address:%p",
355 } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
356 /* Symbols with index SHN_ABS are not relocated. */
357 if (sym->st_shndx != SHN_ABS) {
358 *got = sym->st_value +
359 (Elf_Addr)obj->relocbase;
360 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
361 dbg("Warning3, i:%d maps to relocbase address:%p",
366 /* TODO: add cache here */
367 def = find_symdef(i, obj, &defobj, flags, NULL,
370 dbg("Warning4, can't find symbole %d", i);
373 *got = def->st_value + (Elf_Addr)defobj->relocbase;
374 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
375 dbg("Warning4, i:%d maps to relocbase address:%p",
377 dbg("via first obj symbol %s",
378 obj->strtab + obj->symtab[i].st_name);
379 dbg("found in obj %p:%s",
380 defobj, defobj->path);
384 dbg(" --> now %lx", (u_long) *got);
390 rellim = (const Elf_Rel *)((caddr_t)obj->rel + obj->relsize);
391 for (rel = obj->rel; rel < rellim; rel++) {
392 Elf_Word r_symndx, r_type;
395 where = obj->relocbase + rel->r_offset;
396 r_symndx = ELF_R_SYM(rel->r_info);
397 r_type = ELF_R_TYPE(rel->r_info);
399 switch (r_type & 0xff) {
403 case R_TYPE(REL32): {
404 /* 32-bit PC-relative reference */
406 ELF_R_NXTTYPE_64_P(r_type)
409 Elf_Sxword old = load_ptr(where, rlen);
410 Elf_Sxword val = old;
412 def = obj->symtab + r_symndx;
414 if (r_symndx >= obj->gotsym) {
415 val += got[obj->local_gotno + r_symndx - obj->gotsym];
416 dbg("REL32/G(%p) %p --> %p (%s) in %s",
417 where, (void *)old, (void *)val,
418 obj->strtab + def->st_name,
422 * XXX: ABI DIFFERENCE!
424 * Old NetBSD binutils would generate shared
425 * libs with section-relative relocations being
426 * already adjusted for the start address of
429 * New binutils, OTOH, generate shared libs
430 * with the same relocations being based at
431 * zero, so we need to add in the start address
438 ELF_ST_INFO(STB_LOCAL, STT_SECTION)
439 #ifdef SUPPORT_OLD_BROKEN_LD
443 val += (Elf_Addr)def->st_value;
445 val += (Elf_Addr)obj->relocbase;
447 dbg("REL32/L(%p) %p -> %p (%s) in %s",
448 where, (void *)old, (void *)val,
449 obj->strtab + def->st_name, obj->path);
451 store_ptr(where, val, rlen);
456 case R_TYPE(TLS_DTPMOD64):
458 case R_TYPE(TLS_DTPMOD32):
462 const size_t rlen = sizeof(Elf_Addr);
463 Elf_Addr old = load_ptr(where, rlen);
466 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
471 val += (Elf_Addr)defobj->tlsindex;
473 store_ptr(where, val, rlen);
474 dbg("DTPMOD %s in %s %p --> %p in %s",
475 obj->strtab + obj->symtab[r_symndx].st_name,
476 obj->path, (void *)old, (void*)val, defobj->path);
481 case R_TYPE(TLS_DTPREL64):
483 case R_TYPE(TLS_DTPREL32):
486 const size_t rlen = sizeof(Elf_Addr);
487 Elf_Addr old = load_ptr(where, rlen);
490 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
495 if (!defobj->tls_done && allocate_tls_offset(obj))
498 val += (Elf_Addr)def->st_value - TLS_DTP_OFFSET;
499 store_ptr(where, val, rlen);
501 dbg("DTPREL %s in %s %p --> %p in %s",
502 obj->strtab + obj->symtab[r_symndx].st_name,
503 obj->path, (void*)old, (void *)val, defobj->path);
508 case R_TYPE(TLS_TPREL64):
510 case R_TYPE(TLS_TPREL32):
513 const size_t rlen = sizeof(Elf_Addr);
514 Elf_Addr old = load_ptr(where, rlen);
517 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
523 if (!defobj->tls_done && allocate_tls_offset(obj))
526 val += (Elf_Addr)(def->st_value + defobj->tlsoffset
527 - TLS_TP_OFFSET - TLS_TCB_SIZE);
528 store_ptr(where, val, rlen);
530 dbg("TPREL %s in %s %p --> %p in %s",
531 obj->strtab + obj->symtab[r_symndx].st_name,
532 obj->path, (void*)old, (void *)val, defobj->path);
539 dbg("sym = %lu, type = %lu, offset = %p, "
540 "contents = %p, symbol = %s",
541 (u_long)r_symndx, (u_long)ELF_R_TYPE(rel->r_info),
542 (void *)rel->r_offset,
543 (void *)load_ptr(where, sizeof(Elf_Sword)),
544 obj->strtab + obj->symtab[r_symndx].st_name);
545 _rtld_error("%s: Unsupported relocation type %ld "
546 "in non-PLT relocations",
547 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
556 * Process the PLT relocations.
559 reloc_plt(Obj_Entry *obj)
562 const Elf_Rel *rellim;
565 dbg("reloc_plt obj:%p pltrel:%p sz:%s", obj, obj->pltrel, (int)obj->pltrelsize);
566 dbg("gottable %p num syms:%s", obj->pltgot, obj->symtabno );
567 dbg("*****************************************************");
568 rellim = (const Elf_Rel *)((char *)obj->pltrel +
570 for (rel = obj->pltrel; rel < rellim; rel++) {
572 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
573 *where += (Elf_Addr )obj->relocbase;
577 /* PLT fixups were done above in the GOT relocation. */
582 * LD_BIND_NOW was set - force relocation for all jump slots
585 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
588 obj->jmpslots_done = true;
594 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
597 /* XXX not implemented */
602 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
603 struct Struct_RtldLockState *lockstate)
606 /* XXX not implemented */
611 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
612 const Obj_Entry *obj, const Elf_Rel *rel)
621 allocate_initial_tls(Obj_Entry *objs)
626 * Fix the size of the static TLS block by using the maximum
627 * offset allocated so far and adding a bit for dynamic modules to
630 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
632 tls = (char *) allocate_tls(objs, NULL, TLS_TCB_SIZE, 8);
634 sysarch(MIPS_SET_TLS, tls);
638 __tls_get_addr(tls_index* ti)
643 sysarch(MIPS_GET_TLS, &tls);
645 p = tls_get_addr_common(tls, ti->ti_module, ti->ti_offset + TLS_DTP_OFFSET);