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 * Determine if the second GOT entry is reserved for rtld or if it is
56 * the first "real" GOT entry.
58 * This must be a macro rather than a function so that
59 * _rtld_relocate_nonplt_self doesn't trigger a GOT invocation trying
60 * to use it before the local GOT entries in rtld are adjusted.
63 /* Old binutils uses the 32-bit GOT1 mask value for N64. */
64 #define GOT1_RESERVED_FOR_RTLD(got) \
65 (((got)[1] == 0x80000000) || (got)[1] & GOT1_MASK)
67 #define GOT1_RESERVED_FOR_RTLD(got) ((got)[1] & GOT1_MASK)
72 * ELF64 MIPS encodes the relocs uniquely. The first 32-bits of info contain
73 * the symbol index. The top 32-bits contain three relocation types encoded
74 * in big-endian integer with first relocation in LSB. This means for little
75 * endian we have to byte swap that integer (r_type).
77 #define Elf_Sxword Elf64_Sxword
78 #define ELF_R_NXTTYPE_64_P(r_type) ((((r_type) >> 8) & 0xff) == R_TYPE(64))
79 #if BYTE_ORDER == LITTLE_ENDIAN
82 #define ELF_R_SYM(r_info) ((r_info) & 0xffffffff)
83 #define ELF_R_TYPE(r_info) bswap32((r_info) >> 32)
86 #define ELF_R_NXTTYPE_64_P(r_type) (0)
87 #define Elf_Sxword Elf32_Sword
90 void _rtld_pltbind_start(void);
93 init_pltgot(Obj_Entry *obj)
96 if (obj->pltgot != NULL) {
97 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
98 if (GOT1_RESERVED_FOR_RTLD(obj->pltgot))
99 obj->pltgot[1] = (Elf_Addr) obj | GOT1_MASK;
101 if (obj->mips_pltgot != NULL) {
102 obj->mips_pltgot[0] = (Elf_Addr) &_rtld_pltbind_start;
103 obj->mips_pltgot[1] = (Elf_Addr) obj;
108 do_copy_relocations(Obj_Entry *dstobj)
110 const Obj_Entry *srcobj, *defobj;
111 const Elf_Rel *rellim;
113 const Elf_Sym *srcsym;
114 const Elf_Sym *dstsym;
123 * COPY relocs are invalid outside of the main program
125 assert(dstobj->mainprog);
127 rellim = (const Elf_Rel *)((caddr_t)dstobj->rel + dstobj->relsize);
128 for (rel = dstobj->rel; rel < rellim; rel++) {
129 if (ELF_R_TYPE(rel->r_info) != R_MIPS_COPY)
132 dstaddr = (void *)(dstobj->relocbase + rel->r_offset);
133 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
134 name = dstobj->strtab + dstsym->st_name;
135 size = dstsym->st_size;
137 symlook_init(&req, name);
138 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
139 req.flags = SYMLOOK_EARLY;
141 for (srcobj = globallist_next(dstobj); srcobj != NULL;
142 srcobj = globallist_next(srcobj)) {
143 res = symlook_obj(&req, srcobj);
145 srcsym = req.sym_out;
146 defobj = req.defobj_out;
150 if (srcobj == NULL) {
152 "Undefined symbol \"%s\" referenced from COPY relocation in %s",
157 srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
158 memcpy(dstaddr, srcaddr, size);
164 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
167 * It is possible for the compiler to emit relocations for unaligned data.
168 * We handle this situation with these inlines.
170 static __inline Elf_Sxword
171 load_ptr(void *where, size_t len)
175 if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
177 if (len == sizeof(Elf_Sxword))
178 return *(Elf_Sxword *)where;
180 return *(Elf_Sword *)where;
184 #if BYTE_ORDER == LITTLE_ENDIAN
185 (void)memcpy(&val, where, len);
187 #if BYTE_ORDER == BIG_ENDIAN
188 (void)memcpy((uint8_t *)((&val)+1) - len, where, len);
190 return (len == sizeof(Elf_Sxword)) ? val : (Elf_Sword)val;
194 store_ptr(void *where, Elf_Sxword val, size_t len)
196 if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
198 if (len == sizeof(Elf_Sxword)) {
199 *(Elf_Sxword *)where = val;
203 *(Elf_Sword *)where = val;
206 #if BYTE_ORDER == LITTLE_ENDIAN
207 (void)memcpy(where, &val, len);
209 #if BYTE_ORDER == BIG_ENDIAN
210 (void)memcpy(where, (const uint8_t *)((&val)+1) - len, len);
215 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
217 const Elf_Rel *rel = NULL, *rellim;
219 const Elf_Sym *symtab = NULL, *sym;
221 Elf_Addr *got = NULL;
222 Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0;
225 for (; dynp->d_tag != DT_NULL; dynp++) {
226 switch (dynp->d_tag) {
228 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
231 relsz = dynp->d_un.d_val;
234 symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
237 got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
239 case DT_MIPS_LOCAL_GOTNO:
240 local_gotno = dynp->d_un.d_val;
242 case DT_MIPS_SYMTABNO:
243 symtabno = dynp->d_un.d_val;
246 gotsym = dynp->d_un.d_val;
251 i = GOT1_RESERVED_FOR_RTLD(got) ? 2 : 1;
252 /* Relocate the local GOT entries */
254 for (; i < local_gotno; i++) {
258 sym = symtab + gotsym;
259 /* Now do the global GOT entries */
260 for (i = gotsym; i < symtabno; i++) {
261 *got = sym->st_value + relocbase;
266 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
267 for (; rel < rellim; rel++) {
268 Elf_Word r_symndx, r_type;
270 where = (void *)(relocbase + rel->r_offset);
272 r_symndx = ELF_R_SYM(rel->r_info);
273 r_type = ELF_R_TYPE(rel->r_info);
275 switch (r_type & 0xff) {
276 case R_TYPE(REL32): {
278 ELF_R_NXTTYPE_64_P(r_type)
281 Elf_Sxword old = load_ptr(where, rlen);
282 Elf_Sxword val = old;
284 assert(r_type == R_TYPE(REL32)
285 || r_type == (R_TYPE(REL32)|(R_TYPE(64) << 8)));
287 assert(r_symndx < gotsym);
288 sym = symtab + r_symndx;
289 assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
291 dbg("REL32/L(%p) %p -> %p in <self>",
292 where, (void *)old, (void *)val);
293 store_ptr(where, val, rlen);
297 case R_TYPE(GPREL32):
310 _mips_rtld_bind(Obj_Entry *obj, Elf_Size reloff)
312 Elf_Addr *got = obj->pltgot;
314 const Obj_Entry *defobj;
317 RtldLockState lockstate;
319 rlock_acquire(rtld_bind_lock, &lockstate);
320 if (sigsetjmp(lockstate.env, 0) != 0)
321 lock_upgrade(rtld_bind_lock, &lockstate);
323 where = &got[obj->local_gotno + reloff - obj->gotsym];
324 def = find_symdef(reloff, obj, &defobj, SYMLOOK_IN_PLT, NULL,
329 target = (Elf_Addr)(defobj->relocbase + def->st_value);
330 dbg("bind now/fixup at %s sym # %jd in %s --> was=%p new=%p",
332 (intmax_t)reloff, defobj->strtab + def->st_name,
333 (void *)*where, (void *)target);
336 lock_release(rtld_bind_lock, &lockstate);
337 return (Elf_Addr)target;
341 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
342 RtldLockState *lockstate)
345 const Elf_Rel *rellim;
346 Elf_Addr *got = obj->pltgot;
347 const Elf_Sym *sym, *def;
348 const Obj_Entry *defobj;
350 #ifdef SUPPORT_OLD_BROKEN_LD
354 /* The relocation for the dynamic loader has already been done. */
358 if ((flags & SYMLOOK_IFUNC) != 0)
359 /* XXX not implemented */
362 #ifdef SUPPORT_OLD_BROKEN_LD
365 for (i = 1; i < 12; i++)
366 if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
368 dbg("%s: broken=%d", obj->path, broken);
371 i = GOT1_RESERVED_FOR_RTLD(got) ? 2 : 1;
373 /* Relocate the local GOT entries */
375 dbg("got:%p for %d entries adding %p",
376 got, obj->local_gotno, obj->relocbase);
377 for (; i < obj->local_gotno; i++) {
378 *got += (Elf_Addr)obj->relocbase;
381 sym = obj->symtab + obj->gotsym;
383 dbg("got:%p for %d entries",
385 /* Now do the global GOT entries */
386 for (i = obj->gotsym; i < obj->symtabno; i++) {
387 dbg(" doing got %d sym %p (%s, %lx)", i - obj->gotsym, sym,
388 sym->st_name + obj->strtab, (u_long) *got);
390 #ifdef SUPPORT_OLD_BROKEN_LD
391 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
392 broken && sym->st_shndx == SHN_UNDEF) {
394 * XXX DANGER WILL ROBINSON!
395 * You might think this is stupid, as it intentionally
396 * defeats lazy binding -- and you'd be right.
397 * Unfortunately, for lazy binding to work right, we
398 * need to a way to force the GOT slots used for
399 * function pointers to be resolved immediately. This
400 * is supposed to be done automatically by the linker,
401 * by not outputting a PLT slot and setting st_value
402 * to 0 if there are non-PLT references, but older
403 * versions of GNU ld do not do this.
405 def = find_symdef(i, obj, &defobj, flags, NULL,
409 *got = def->st_value + (Elf_Addr)defobj->relocbase;
412 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
413 sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
415 * If there are non-PLT references to the function,
416 * st_value should be 0, forcing us to resolve the
417 * address immediately.
419 * XXX DANGER WILL ROBINSON!
420 * The linker is not outputting PLT slots for calls to
421 * functions that are defined in the same shared
422 * library. This is a bug, because it can screw up
423 * link ordering rules if the symbol is defined in
424 * more than one module. For now, if there is a
425 * definition, we fail the test above and force a full
426 * symbol lookup. This means that all intra-module
427 * calls are bound immediately. - mycroft, 2003/09/24
429 *got = sym->st_value + (Elf_Addr)obj->relocbase;
430 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
431 dbg("Warning2, i:%d maps to relocbase address:%p",
435 } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
436 /* Symbols with index SHN_ABS are not relocated. */
437 if (sym->st_shndx != SHN_ABS) {
438 *got = sym->st_value +
439 (Elf_Addr)obj->relocbase;
440 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
441 dbg("Warning3, i:%d maps to relocbase address:%p",
446 /* TODO: add cache here */
447 def = find_symdef(i, obj, &defobj, flags, NULL,
450 dbg("Warning4, can't find symbole %d", i);
453 *got = def->st_value + (Elf_Addr)defobj->relocbase;
454 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
455 dbg("Warning4, i:%d maps to relocbase address:%p",
457 dbg("via first obj symbol %s",
458 obj->strtab + obj->symtab[i].st_name);
459 dbg("found in obj %p:%s",
460 defobj, defobj->path);
464 dbg(" --> now %lx", (u_long) *got);
470 rellim = (const Elf_Rel *)((caddr_t)obj->rel + obj->relsize);
471 for (rel = obj->rel; rel < rellim; rel++) {
472 Elf_Word r_symndx, r_type;
475 where = obj->relocbase + rel->r_offset;
476 r_symndx = ELF_R_SYM(rel->r_info);
477 r_type = ELF_R_TYPE(rel->r_info);
479 switch (r_type & 0xff) {
483 case R_TYPE(REL32): {
484 /* 32-bit PC-relative reference */
486 ELF_R_NXTTYPE_64_P(r_type)
489 Elf_Sxword old = load_ptr(where, rlen);
490 Elf_Sxword val = old;
492 def = obj->symtab + r_symndx;
494 if (r_symndx >= obj->gotsym) {
495 val += got[obj->local_gotno + r_symndx - obj->gotsym];
496 dbg("REL32/G(%p) %p --> %p (%s) in %s",
497 where, (void *)old, (void *)val,
498 obj->strtab + def->st_name,
502 * XXX: ABI DIFFERENCE!
504 * Old NetBSD binutils would generate shared
505 * libs with section-relative relocations being
506 * already adjusted for the start address of
509 * New binutils, OTOH, generate shared libs
510 * with the same relocations being based at
511 * zero, so we need to add in the start address
518 ELF_ST_INFO(STB_LOCAL, STT_SECTION)
519 #ifdef SUPPORT_OLD_BROKEN_LD
523 val += (Elf_Addr)def->st_value;
525 val += (Elf_Addr)obj->relocbase;
527 dbg("REL32/L(%p) %p -> %p (%s) in %s",
528 where, (void *)old, (void *)val,
529 obj->strtab + def->st_name, obj->path);
531 store_ptr(where, val, rlen);
537 * These are deferred until all other relocations have
538 * been done. All we do here is make sure that the
539 * COPY relocation is not in a shared library. They
540 * are allowed only in executable files.
542 if (!obj->mainprog) {
543 _rtld_error("%s: Unexpected R_MIPS_COPY "
544 "relocation in shared library", obj->path);
550 case R_TYPE(TLS_DTPMOD64):
552 case R_TYPE(TLS_DTPMOD32):
556 const size_t rlen = sizeof(Elf_Addr);
557 Elf_Addr old = load_ptr(where, rlen);
560 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
565 val += (Elf_Addr)defobj->tlsindex;
567 store_ptr(where, val, rlen);
568 dbg("DTPMOD %s in %s %p --> %p in %s",
569 obj->strtab + obj->symtab[r_symndx].st_name,
570 obj->path, (void *)old, (void*)val, defobj->path);
575 case R_TYPE(TLS_DTPREL64):
577 case R_TYPE(TLS_DTPREL32):
580 const size_t rlen = sizeof(Elf_Addr);
581 Elf_Addr old = load_ptr(where, rlen);
584 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
589 if (!defobj->tls_done && allocate_tls_offset(obj))
592 val += (Elf_Addr)def->st_value - TLS_DTP_OFFSET;
593 store_ptr(where, val, rlen);
595 dbg("DTPREL %s in %s %p --> %p in %s",
596 obj->strtab + obj->symtab[r_symndx].st_name,
597 obj->path, (void*)old, (void *)val, defobj->path);
602 case R_TYPE(TLS_TPREL64):
604 case R_TYPE(TLS_TPREL32):
607 const size_t rlen = sizeof(Elf_Addr);
608 Elf_Addr old = load_ptr(where, rlen);
611 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
617 if (!defobj->tls_done && allocate_tls_offset(obj))
620 val += (Elf_Addr)(def->st_value + defobj->tlsoffset
621 - TLS_TP_OFFSET - TLS_TCB_SIZE);
622 store_ptr(where, val, rlen);
624 dbg("TPREL %s in %s %p --> %p in %s",
625 obj->strtab + obj->symtab[r_symndx].st_name,
626 obj->path, (void*)old, (void *)val, defobj->path);
633 dbg("sym = %lu, type = %lu, offset = %p, "
634 "contents = %p, symbol = %s",
635 (u_long)r_symndx, (u_long)ELF_R_TYPE(rel->r_info),
636 (void *)rel->r_offset,
637 (void *)load_ptr(where, sizeof(Elf_Sword)),
638 obj->strtab + obj->symtab[r_symndx].st_name);
639 _rtld_error("%s: Unsupported relocation type %ld "
640 "in non-PLT relocations",
641 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
650 * Process the PLT relocations.
653 reloc_plt(Obj_Entry *obj)
655 const Elf_Rel *rellim;
658 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
659 for (rel = obj->pltrel; rel < rellim; rel++) {
662 switch (ELF_R_TYPE(rel->r_info)) {
663 case R_MIPS_JUMP_SLOT:
664 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
665 *where += (Elf_Addr )obj->relocbase;
668 _rtld_error("Unknown relocation type %u in PLT",
669 (unsigned int)ELF_R_TYPE(rel->r_info));
678 * LD_BIND_NOW was set - force relocation for all jump slots
681 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
683 const Obj_Entry *defobj;
684 const Elf_Rel *rellim;
688 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
689 for (rel = obj->pltrel; rel < rellim; rel++) {
692 switch (ELF_R_TYPE(rel->r_info)) {
693 case R_MIPS_JUMP_SLOT:
694 def = find_symdef(ELF_R_SYM(rel->r_info), obj,
695 &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
697 dbg("reloc_jmpslots: sym not found");
701 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
702 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
705 _rtld_error("Unknown relocation type %u in PLT",
706 (unsigned int)ELF_R_TYPE(rel->r_info));
715 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
718 /* XXX not implemented */
723 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
724 struct Struct_RtldLockState *lockstate)
727 /* XXX not implemented */
732 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
733 const Obj_Entry *obj, const Elf_Rel *rel)
736 assert(ELF_R_TYPE(rel->r_info) == R_MIPS_JUMP_SLOT);
738 if (*where != target && !ld_bind_not)
744 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
749 allocate_initial_tls(Obj_Entry *objs)
754 * Fix the size of the static TLS block by using the maximum
755 * offset allocated so far and adding a bit for dynamic modules to
758 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
760 tls = (char *) allocate_tls(objs, NULL, TLS_TCB_SIZE, 8);
762 sysarch(MIPS_SET_TLS, tls);
771 __asm__ __volatile__ (
778 * XXXSS See 'git show c6be4f4d2d1b71c04de5d3bbb6933ce2dbcdb317'
780 * Remove the offset since this really a request to get the TLS
781 * pointer via sysarch() (in theory). Of course, this may go away
782 * once the TLS code is rewritten.
784 _rv = _rv - TLS_TP_OFFSET - TLS_TCB_SIZE;
796 __asm__ __volatile__ (
803 * XXXSS See 'git show c6be4f4d2d1b71c04de5d3bbb6933ce2dbcdb317'
805 * Remove the offset since this really a request to get the TLS
806 * pointer via sysarch() (in theory). Of course, this may go away
807 * once the TLS code is rewritten.
809 _rv = _rv - TLS_TP_OFFSET - TLS_TCB_SIZE;
813 #endif /* ! __mips_n64 */
816 __tls_get_addr(tls_index* ti)
821 #ifdef TLS_USE_SYSARCH
822 sysarch(MIPS_GET_TLS, &tls);
824 tls = _mips_get_tls();
827 p = tls_get_addr_common(tls, ti->ti_module, ti->ti_offset + TLS_DTP_OFFSET);