1 /* $NetBSD: mips_reloc.c,v 1.58 2010/01/14 11:57:06 skrll Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright 1997 Michael L. Hitch <mhitch@montana.edu>
7 * Portions copyright 2002 Charles M. Hannum <root@ihack.net>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
38 #include <sys/endian.h>
44 #include <machine/sysarch.h>
45 #include <machine/tls.h>
51 #define GOT1_MASK 0x8000000000000000UL
53 #define GOT1_MASK 0x80000000UL
57 * Determine if the second GOT entry is reserved for rtld or if it is
58 * the first "real" GOT entry.
60 * This must be a macro rather than a function so that
61 * _rtld_relocate_nonplt_self doesn't trigger a GOT invocation trying
62 * to use it before the local GOT entries in rtld are adjusted.
65 /* Old binutils uses the 32-bit GOT1 mask value for N64. */
66 #define GOT1_RESERVED_FOR_RTLD(got) \
67 (((got)[1] == 0x80000000) || (got)[1] & GOT1_MASK)
69 #define GOT1_RESERVED_FOR_RTLD(got) ((got)[1] & GOT1_MASK)
74 * ELF64 MIPS encodes the relocs uniquely. The first 32-bits of info contain
75 * the symbol index. The top 32-bits contain three relocation types encoded
76 * in big-endian integer with first relocation in LSB. This means for little
77 * endian we have to byte swap that integer (r_type).
79 #define Elf_Sxword Elf64_Sxword
80 #define ELF_R_NXTTYPE_64_P(r_type) ((((r_type) >> 8) & 0xff) == R_TYPE(64))
81 #if BYTE_ORDER == LITTLE_ENDIAN
84 #define ELF_R_SYM(r_info) ((r_info) & 0xffffffff)
85 #define ELF_R_TYPE(r_info) bswap32((r_info) >> 32)
88 #define ELF_R_NXTTYPE_64_P(r_type) (0)
89 #define Elf_Sxword Elf32_Sword
92 void _rtld_pltbind_start(void);
95 init_pltgot(Obj_Entry *obj)
98 if (obj->pltgot != NULL) {
99 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
100 if (GOT1_RESERVED_FOR_RTLD(obj->pltgot))
101 obj->pltgot[1] = (Elf_Addr) obj | GOT1_MASK;
103 if (obj->mips_pltgot != NULL) {
104 obj->mips_pltgot[0] = (Elf_Addr) &_rtld_pltbind_start;
105 obj->mips_pltgot[1] = (Elf_Addr) obj;
110 do_copy_relocations(Obj_Entry *dstobj)
112 const Obj_Entry *srcobj, *defobj;
113 const Elf_Rel *rellim;
115 const Elf_Sym *srcsym;
116 const Elf_Sym *dstsym;
125 * COPY relocs are invalid outside of the main program
127 assert(dstobj->mainprog);
129 rellim = (const Elf_Rel *)((caddr_t)dstobj->rel + dstobj->relsize);
130 for (rel = dstobj->rel; rel < rellim; rel++) {
131 if (ELF_R_TYPE(rel->r_info) != R_MIPS_COPY)
134 dstaddr = (void *)(dstobj->relocbase + rel->r_offset);
135 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
136 name = dstobj->strtab + dstsym->st_name;
137 size = dstsym->st_size;
139 symlook_init(&req, name);
140 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
141 req.flags = SYMLOOK_EARLY;
143 for (srcobj = globallist_next(dstobj); srcobj != NULL;
144 srcobj = globallist_next(srcobj)) {
145 res = symlook_obj(&req, srcobj);
147 srcsym = req.sym_out;
148 defobj = req.defobj_out;
152 if (srcobj == NULL) {
154 "Undefined symbol \"%s\" referenced from COPY relocation in %s",
159 srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
160 memcpy(dstaddr, srcaddr, size);
166 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
169 * It is possible for the compiler to emit relocations for unaligned data.
170 * We handle this situation with these inlines.
172 static __inline Elf_Sxword
173 load_ptr(void *where, size_t len)
177 if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
179 if (len == sizeof(Elf_Sxword))
180 return *(Elf_Sxword *)where;
182 return *(Elf_Sword *)where;
186 #if BYTE_ORDER == LITTLE_ENDIAN
187 (void)memcpy(&val, where, len);
189 #if BYTE_ORDER == BIG_ENDIAN
190 (void)memcpy((uint8_t *)((&val)+1) - len, where, len);
192 return (len == sizeof(Elf_Sxword)) ? val : (Elf_Sword)val;
196 store_ptr(void *where, Elf_Sxword val, size_t len)
198 if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
200 if (len == sizeof(Elf_Sxword)) {
201 *(Elf_Sxword *)where = val;
205 *(Elf_Sword *)where = val;
208 #if BYTE_ORDER == LITTLE_ENDIAN
209 (void)memcpy(where, &val, len);
211 #if BYTE_ORDER == BIG_ENDIAN
212 (void)memcpy(where, (const uint8_t *)((&val)+1) - len, len);
217 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
219 const Elf_Rel *rel = NULL, *rellim;
221 const Elf_Sym *symtab = NULL, *sym;
223 Elf_Addr *got = NULL;
224 Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0;
227 for (; dynp->d_tag != DT_NULL; dynp++) {
228 switch (dynp->d_tag) {
230 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
233 relsz = dynp->d_un.d_val;
236 symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
239 got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
241 case DT_MIPS_LOCAL_GOTNO:
242 local_gotno = dynp->d_un.d_val;
244 case DT_MIPS_SYMTABNO:
245 symtabno = dynp->d_un.d_val;
248 gotsym = dynp->d_un.d_val;
253 i = GOT1_RESERVED_FOR_RTLD(got) ? 2 : 1;
254 /* Relocate the local GOT entries */
256 for (; i < local_gotno; i++) {
260 sym = symtab + gotsym;
261 /* Now do the global GOT entries */
262 for (i = gotsym; i < symtabno; i++) {
263 *got = sym->st_value + relocbase;
268 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
269 for (; rel < rellim; rel++) {
270 Elf_Word r_symndx, r_type;
272 where = (void *)(relocbase + rel->r_offset);
274 r_symndx = ELF_R_SYM(rel->r_info);
275 r_type = ELF_R_TYPE(rel->r_info);
277 switch (r_type & 0xff) {
278 case R_TYPE(REL32): {
280 ELF_R_NXTTYPE_64_P(r_type)
283 Elf_Sxword old = load_ptr(where, rlen);
284 Elf_Sxword val = old;
286 assert(r_type == R_TYPE(REL32)
287 || r_type == (R_TYPE(REL32)|(R_TYPE(64) << 8)));
289 assert(r_symndx < gotsym);
290 sym = symtab + r_symndx;
291 assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
293 dbg("REL32/L(%p) %p -> %p in <self>",
294 where, (void *)old, (void *)val);
295 store_ptr(where, val, rlen);
299 case R_TYPE(GPREL32):
312 _mips_rtld_bind(Obj_Entry *obj, Elf_Size reloff)
314 Elf_Addr *got = obj->pltgot;
316 const Obj_Entry *defobj;
319 RtldLockState lockstate;
321 rlock_acquire(rtld_bind_lock, &lockstate);
322 if (sigsetjmp(lockstate.env, 0) != 0)
323 lock_upgrade(rtld_bind_lock, &lockstate);
325 where = &got[obj->local_gotno + reloff - obj->gotsym];
326 def = find_symdef(reloff, obj, &defobj, SYMLOOK_IN_PLT, NULL,
331 target = (Elf_Addr)(defobj->relocbase + def->st_value);
332 dbg("bind now/fixup at %s sym # %jd in %s --> was=%p new=%p",
334 (intmax_t)reloff, defobj->strtab + def->st_name,
335 (void *)*where, (void *)target);
338 lock_release(rtld_bind_lock, &lockstate);
339 return (Elf_Addr)target;
343 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
344 RtldLockState *lockstate)
347 const Elf_Rel *rellim;
348 Elf_Addr *got = obj->pltgot;
349 const Elf_Sym *sym, *def;
350 const Obj_Entry *defobj;
352 #ifdef SUPPORT_OLD_BROKEN_LD
356 /* The relocation for the dynamic loader has already been done. */
360 if ((flags & SYMLOOK_IFUNC) != 0)
361 /* XXX not implemented */
364 #ifdef SUPPORT_OLD_BROKEN_LD
367 for (i = 1; i < 12; i++)
368 if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
370 dbg("%s: broken=%d", obj->path, broken);
373 i = GOT1_RESERVED_FOR_RTLD(got) ? 2 : 1;
375 /* Relocate the local GOT entries */
377 dbg("got:%p for %d entries adding %p",
378 got, obj->local_gotno, obj->relocbase);
379 for (; i < obj->local_gotno; i++) {
380 *got += (Elf_Addr)obj->relocbase;
383 sym = obj->symtab + obj->gotsym;
385 dbg("got:%p for %d entries",
387 /* Now do the global GOT entries */
388 for (i = obj->gotsym; i < obj->symtabno; i++) {
389 dbg(" doing got %d sym %p (%s, %lx)", i - obj->gotsym, sym,
390 sym->st_name + obj->strtab, (u_long) *got);
392 #ifdef SUPPORT_OLD_BROKEN_LD
393 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
394 broken && sym->st_shndx == SHN_UNDEF) {
396 * XXX DANGER WILL ROBINSON!
397 * You might think this is stupid, as it intentionally
398 * defeats lazy binding -- and you'd be right.
399 * Unfortunately, for lazy binding to work right, we
400 * need to a way to force the GOT slots used for
401 * function pointers to be resolved immediately. This
402 * is supposed to be done automatically by the linker,
403 * by not outputting a PLT slot and setting st_value
404 * to 0 if there are non-PLT references, but older
405 * versions of GNU ld do not do this.
407 def = find_symdef(i, obj, &defobj, flags, NULL,
411 *got = def->st_value + (Elf_Addr)defobj->relocbase;
414 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
415 sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
417 * If there are non-PLT references to the function,
418 * st_value should be 0, forcing us to resolve the
419 * address immediately.
421 * XXX DANGER WILL ROBINSON!
422 * The linker is not outputting PLT slots for calls to
423 * functions that are defined in the same shared
424 * library. This is a bug, because it can screw up
425 * link ordering rules if the symbol is defined in
426 * more than one module. For now, if there is a
427 * definition, we fail the test above and force a full
428 * symbol lookup. This means that all intra-module
429 * calls are bound immediately. - mycroft, 2003/09/24
431 *got = sym->st_value + (Elf_Addr)obj->relocbase;
432 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
433 dbg("Warning2, i:%d maps to relocbase address:%p",
437 } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
438 /* Symbols with index SHN_ABS are not relocated. */
439 if (sym->st_shndx != SHN_ABS) {
440 *got = sym->st_value +
441 (Elf_Addr)obj->relocbase;
442 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
443 dbg("Warning3, i:%d maps to relocbase address:%p",
448 /* TODO: add cache here */
449 def = find_symdef(i, obj, &defobj, flags, NULL,
452 dbg("Warning4, can't find symbole %d", i);
455 *got = def->st_value + (Elf_Addr)defobj->relocbase;
456 if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
457 dbg("Warning4, i:%d maps to relocbase address:%p",
459 dbg("via first obj symbol %s",
460 obj->strtab + obj->symtab[i].st_name);
461 dbg("found in obj %p:%s",
462 defobj, defobj->path);
466 dbg(" --> now %lx", (u_long) *got);
472 rellim = (const Elf_Rel *)((caddr_t)obj->rel + obj->relsize);
473 for (rel = obj->rel; rel < rellim; rel++) {
474 Elf_Word r_symndx, r_type;
477 where = obj->relocbase + rel->r_offset;
478 r_symndx = ELF_R_SYM(rel->r_info);
479 r_type = ELF_R_TYPE(rel->r_info);
481 switch (r_type & 0xff) {
485 case R_TYPE(REL32): {
486 /* 32-bit PC-relative reference */
488 ELF_R_NXTTYPE_64_P(r_type)
491 Elf_Sxword old = load_ptr(where, rlen);
492 Elf_Sxword val = old;
494 def = obj->symtab + r_symndx;
496 if (r_symndx >= obj->gotsym) {
497 val += got[obj->local_gotno + r_symndx - obj->gotsym];
498 dbg("REL32/G(%p) %p --> %p (%s) in %s",
499 where, (void *)old, (void *)val,
500 obj->strtab + def->st_name,
504 * XXX: ABI DIFFERENCE!
506 * Old NetBSD binutils would generate shared
507 * libs with section-relative relocations being
508 * already adjusted for the start address of
511 * New binutils, OTOH, generate shared libs
512 * with the same relocations being based at
513 * zero, so we need to add in the start address
520 ELF_ST_INFO(STB_LOCAL, STT_SECTION)
521 #ifdef SUPPORT_OLD_BROKEN_LD
525 val += (Elf_Addr)def->st_value;
527 val += (Elf_Addr)obj->relocbase;
529 dbg("REL32/L(%p) %p -> %p (%s) in %s",
530 where, (void *)old, (void *)val,
531 obj->strtab + def->st_name, obj->path);
533 store_ptr(where, val, rlen);
539 * These are deferred until all other relocations have
540 * been done. All we do here is make sure that the
541 * COPY relocation is not in a shared library. They
542 * are allowed only in executable files.
544 if (!obj->mainprog) {
545 _rtld_error("%s: Unexpected R_MIPS_COPY "
546 "relocation in shared library", obj->path);
552 case R_TYPE(TLS_DTPMOD64):
554 case R_TYPE(TLS_DTPMOD32):
558 const size_t rlen = sizeof(Elf_Addr);
559 Elf_Addr old = load_ptr(where, rlen);
562 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
567 val += (Elf_Addr)defobj->tlsindex;
569 store_ptr(where, val, rlen);
570 dbg("DTPMOD %s in %s %p --> %p in %s",
571 obj->strtab + obj->symtab[r_symndx].st_name,
572 obj->path, (void *)old, (void*)val, defobj->path);
577 case R_TYPE(TLS_DTPREL64):
579 case R_TYPE(TLS_DTPREL32):
582 const size_t rlen = sizeof(Elf_Addr);
583 Elf_Addr old = load_ptr(where, rlen);
586 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
591 if (!defobj->tls_done && allocate_tls_offset(obj))
594 val += (Elf_Addr)def->st_value - TLS_DTP_OFFSET;
595 store_ptr(where, val, rlen);
597 dbg("DTPREL %s in %s %p --> %p in %s",
598 obj->strtab + obj->symtab[r_symndx].st_name,
599 obj->path, (void*)old, (void *)val, defobj->path);
604 case R_TYPE(TLS_TPREL64):
606 case R_TYPE(TLS_TPREL32):
609 const size_t rlen = sizeof(Elf_Addr);
610 Elf_Addr old = load_ptr(where, rlen);
613 def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
619 if (!defobj->tls_done && allocate_tls_offset(obj))
622 val += (Elf_Addr)(def->st_value + defobj->tlsoffset
623 - TLS_TP_OFFSET - TLS_TCB_SIZE);
624 store_ptr(where, val, rlen);
626 dbg("TPREL %s in %s %p --> %p in %s",
627 obj->strtab + obj->symtab[r_symndx].st_name,
628 obj->path, (void*)old, (void *)val, defobj->path);
635 dbg("sym = %lu, type = %lu, offset = %p, "
636 "contents = %p, symbol = %s",
637 (u_long)r_symndx, (u_long)ELF_R_TYPE(rel->r_info),
638 (void *)rel->r_offset,
639 (void *)load_ptr(where, sizeof(Elf_Sword)),
640 obj->strtab + obj->symtab[r_symndx].st_name);
641 _rtld_error("%s: Unsupported relocation type %ld "
642 "in non-PLT relocations",
643 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
652 * Process the PLT relocations.
655 reloc_plt(Obj_Entry *obj)
657 const Elf_Rel *rellim;
660 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
661 for (rel = obj->pltrel; rel < rellim; rel++) {
664 switch (ELF_R_TYPE(rel->r_info)) {
665 case R_MIPS_JUMP_SLOT:
666 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
667 *where += (Elf_Addr )obj->relocbase;
670 _rtld_error("Unknown relocation type %u in PLT",
671 (unsigned int)ELF_R_TYPE(rel->r_info));
680 * LD_BIND_NOW was set - force relocation for all jump slots
683 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
685 const Obj_Entry *defobj;
686 const Elf_Rel *rellim;
690 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
691 for (rel = obj->pltrel; rel < rellim; rel++) {
694 switch (ELF_R_TYPE(rel->r_info)) {
695 case R_MIPS_JUMP_SLOT:
696 def = find_symdef(ELF_R_SYM(rel->r_info), obj,
697 &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
699 dbg("reloc_jmpslots: sym not found");
703 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
704 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
707 _rtld_error("Unknown relocation type %u in PLT",
708 (unsigned int)ELF_R_TYPE(rel->r_info));
717 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
720 /* XXX not implemented */
725 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
726 struct Struct_RtldLockState *lockstate)
729 /* XXX not implemented */
734 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
735 const Obj_Entry *obj, const Elf_Rel *rel)
738 assert(ELF_R_TYPE(rel->r_info) == R_MIPS_JUMP_SLOT);
740 if (*where != target && !ld_bind_not)
746 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
751 allocate_initial_tls(Obj_Entry *objs)
756 * Fix the size of the static TLS block by using the maximum
757 * offset allocated so far and adding a bit for dynamic modules to
760 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
762 tls = (char *) allocate_tls(objs, NULL, TLS_TCB_SIZE, 8);
764 sysarch(MIPS_SET_TLS, tls);
773 __asm__ __volatile__ (
780 * XXXSS See 'git show c6be4f4d2d1b71c04de5d3bbb6933ce2dbcdb317'
782 * Remove the offset since this really a request to get the TLS
783 * pointer via sysarch() (in theory). Of course, this may go away
784 * once the TLS code is rewritten.
786 _rv = _rv - TLS_TP_OFFSET - TLS_TCB_SIZE;
798 __asm__ __volatile__ (
805 * XXXSS See 'git show c6be4f4d2d1b71c04de5d3bbb6933ce2dbcdb317'
807 * Remove the offset since this really a request to get the TLS
808 * pointer via sysarch() (in theory). Of course, this may go away
809 * once the TLS code is rewritten.
811 _rv = _rv - TLS_TP_OFFSET - TLS_TCB_SIZE;
815 #endif /* ! __mips_n64 */
818 __tls_get_addr(tls_index* ti)
823 #ifdef TLS_USE_SYSARCH
824 sysarch(MIPS_GET_TLS, &tls);
826 tls = _mips_get_tls();
829 p = tls_get_addr_common(tls, ti->ti_module, ti->ti_offset + TLS_DTP_OFFSET);