diff --git a/libexec/rtld-elf/powerpc64/reloc.c b/libexec/rtld-elf/powerpc64/reloc.c index 026872e62d5..534e0245f8b 100644 --- a/libexec/rtld-elf/powerpc64/reloc.c +++ b/libexec/rtld-elf/powerpc64/reloc.c @@ -33,6 +33,7 @@ #include #include +#include #include #include @@ -160,83 +161,84 @@ static int reloc_nonplt_object(Obj_Entry *obj_rtld __unused, Obj_Entry *obj, const Elf_Rela *rela, SymCache *cache, int flags, RtldLockState *lockstate) { - Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); - const Elf_Sym *def; - const Obj_Entry *defobj; - Elf_Addr tmp; + const Elf_Sym *def = NULL; + const Obj_Entry *defobj; + Elf_Addr *where, symval = 0; + /* + * First, resolve symbol for relocations which + * reference symbols. + */ switch (ELF_R_TYPE(rela->r_info)) { - case R_PPC_NONE: - break; - - case R_PPC64_UADDR64: /* doubleword64 S + A */ - case R_PPC64_ADDR64: - case R_PPC_GLOB_DAT: + case R_PPC64_UADDR64: /* doubleword64 S + A */ + case R_PPC64_ADDR64: + case R_PPC_GLOB_DAT: + case R_PPC64_DTPMOD64: + case R_PPC64_TPREL64: + case R_PPC64_DTPREL64: def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, flags, cache, lockstate); if (def == NULL) { return (-1); } - - tmp = (Elf_Addr)(defobj->relocbase + def->st_value + - rela->r_addend); - - /* Don't issue write if unnecessary; avoid COW page fault */ - if (*where != tmp) { - *where = tmp; - } - break; - - case R_PPC_RELATIVE: /* doubleword64 B + A */ - tmp = (Elf_Addr)(obj->relocbase + rela->r_addend); - - /* As above, don't issue write unnecessarily */ - if (*where != tmp) { - *where = tmp; - } - break; - - case R_PPC_COPY: /* - * These are deferred until all other relocations - * have been done. All we do here is make sure - * that the COPY relocation is not in a shared - * library. They are allowed only in executable - * files. + * If symbol is IFUNC, only perform relocation + * when caller allowed it by passing + * SYMLOOK_IFUNC flag. Skip the relocations + * otherwise. + * + * Also error out in case IFUNC relocations + * are specified for TLS, which cannot be + * usefully interpreted. */ - if (!obj->mainprog) { - _rtld_error("%s: Unexpected R_COPY " - " relocation in shared library", - obj->path); - return (-1); + if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { + switch (ELF_R_TYPE(rela->r_info)) { + case R_PPC64_UADDR64: + case R_PPC64_ADDR64: + case R_PPC_GLOB_DAT: + if ((flags & SYMLOOK_IFUNC) == 0) { + dbg("Non-PLT reference to IFUNC found!"); + obj->non_plt_gnu_ifunc = true; + return (0); + } + symval = (Elf_Addr)rtld_resolve_ifunc( + defobj, def); + break; + default: + _rtld_error("%s: IFUNC for TLS reloc", + obj->path); + return (-1); + } + } else { + if ((flags & SYMLOOK_IFUNC) != 0) + return (0); + symval = (Elf_Addr)defobj->relocbase + + def->st_value; } break; + default: + if ((flags & SYMLOOK_IFUNC) != 0) + return (0); + } - case R_PPC_JMP_SLOT: - /* - * These will be handled by the plt/jmpslot routines - */ - break; + where = (Elf_Addr *)(obj->relocbase + rela->r_offset); + switch (ELF_R_TYPE(rela->r_info)) { + case R_PPC_NONE: + break; + case R_PPC64_UADDR64: + case R_PPC64_ADDR64: + case R_PPC_GLOB_DAT: + /* Don't issue write if unnecessary; avoid COW page fault */ + if (*where != symval + rela->r_addend) { + *where = symval + rela->r_addend; + } + break; case R_PPC64_DTPMOD64: - def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, - flags, cache, lockstate); - - if (def == NULL) - return (-1); - *where = (Elf_Addr) defobj->tlsindex; - break; - case R_PPC64_TPREL64: - def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, - flags, cache, lockstate); - - if (def == NULL) - return (-1); - /* * We lazily allocate offsets for static TLS as we * see the first relocation that references the @@ -257,19 +259,44 @@ reloc_nonplt_object(Obj_Entry *obj_rtld __unused, Obj_Entry *obj, *(Elf_Addr **)where = *where * sizeof(Elf_Addr) + (Elf_Addr *)(def->st_value + rela->r_addend + defobj->tlsoffset - TLS_TP_OFFSET - TLS_TCB_SIZE); - break; - case R_PPC64_DTPREL64: - def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, - flags, cache, lockstate); - - if (def == NULL) - return (-1); - *where += (Elf_Addr)(def->st_value + rela->r_addend - TLS_DTV_OFFSET); + break; + case R_PPC_RELATIVE: /* doubleword64 B + A */ + symval = (Elf_Addr)(obj->relocbase + rela->r_addend); + /* As above, don't issue write unnecessarily */ + if (*where != symval) { + *where = symval; + } + break; + case R_PPC_COPY: + /* + * These are deferred until all other relocations + * have been done. All we do here is make sure + * that the COPY relocation is not in a shared + * library. They are allowed only in executable + * files. + */ + if (!obj->mainprog) { + _rtld_error("%s: Unexpected R_COPY " + " relocation in shared library", + obj->path); + return (-1); + } + break; + case R_PPC64_IRELATIVE: + /* + * These will be handled by reloc_iresolve(). + */ + obj->irelative = true; + break; + case R_PPC_JMP_SLOT: + /* + * These will be handled by the plt/jmpslot routines + */ break; default: @@ -277,7 +304,7 @@ reloc_nonplt_object(Obj_Entry *obj_rtld __unused, Obj_Entry *obj, " in non-PLT relocations\n", obj->path, ELF_R_TYPE(rela->r_info)); return (-1); - } + } return (0); } @@ -296,10 +323,6 @@ reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags, int bytes = obj->dynsymcount * sizeof(SymCache); int r = -1; - if ((flags & SYMLOOK_IFUNC) != 0) - /* XXX not implemented */ - return (0); - /* * The dynamic loader may be called from a thread, we have * limited amounts of stack available so we cannot use alloca(). @@ -365,13 +388,13 @@ reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela) 8*((reloff < 0x8000) ? reloff : 0x8000) + 12*((reloff < 0x8000) ? 0 : (reloff - 0x8000)); #else + /* 64-Bit ELF V2 ABI Specification, sec. 4.2.5.3. */ *where = (Elf_Addr)obj->glink + 4*reloff + 32; #endif return (0); } - /* * Process the PLT relocations. */ @@ -385,6 +408,11 @@ reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused) relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize); for (rela = obj->pltrela; rela < relalim; rela++) { + /* + * PowerPC(64) .rela.plt is composed of an array of + * R_PPC_JMP_SLOT relocations. Unlike other platforms, + * this is the ONLY relocation type that is valid here. + */ assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT); if (reloc_plt_object(obj, rela) < 0) { @@ -396,7 +424,6 @@ reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused) return (0); } - /* * LD_BIND_NOW was set - force relocation for all jump slots */ @@ -432,6 +459,11 @@ reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate) *where = 0; #endif } else { + if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { + /* LD_BIND_NOW, ifunc in shared lib.*/ + obj->gnu_ifunc = true; + continue; + } reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *) rela); } @@ -494,34 +526,101 @@ reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj __unuse ((struct funcdesc *)(wherep))->toc += (Elf_Addr)defobj->relocbase; } -out: #else dbg(" reloc_jmpslot: where=%p, target=%p", (void *)wherep, (void *)target); - if (!ld_bind_not) + assert(target >= (Elf_Addr)defobj->relocbase); + + if (ld_bind_not) + goto out; + + if (*wherep != target) *wherep = target; + #endif +out: return (target); } int -reloc_iresolve(Obj_Entry *obj __unused, - struct Struct_RtldLockState *lockstate __unused) +reloc_iresolve(Obj_Entry *obj, + struct Struct_RtldLockState *lockstate) { - + /* + * Since PLT slots on PowerPC64 are always R_PPC64_JMP_SLOT, + * R_PPC64_IRELATIVE is in RELA. + */ +#if !defined(_CALL_ELF) || _CALL_ELF == 1 + (void)(obj); + (void)(lockstate); /* XXX not implemented */ return (0); +#else + const Elf_Rela *relalim; + const Elf_Rela *rela; + Elf_Addr *where, target, *ptr; + + if (!obj->irelative) + return (0); + + relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize); + for (rela = obj->rela; rela < relalim; rela++) { + if (ELF_R_TYPE(rela->r_info) == R_PPC64_IRELATIVE) { + ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend); + where = (Elf_Addr *)(obj->relocbase + rela->r_offset); + + lock_release(rtld_bind_lock, lockstate); + target = call_ifunc_resolver(ptr); + wlock_acquire(rtld_bind_lock, lockstate); + + *where = target; + } + } + obj->irelative = false; + return (0); +#endif } int reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused, struct Struct_RtldLockState *lockstate __unused) { - +#if !defined(_CALL_ELF) || _CALL_ELF == 1 + _rtld_error("reloc_gnu_ifunc(): Not implemented!"); /* XXX not implemented */ + return (-1); +#else + + const Elf_Rela *relalim; + const Elf_Rela *rela; + Elf_Addr *where, target; + const Elf_Sym *def; + const Obj_Entry *defobj; + + if (!obj->gnu_ifunc) + return (0); + relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize); + for (rela = obj->pltrela; rela < relalim; rela++) { + if (ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT) { + where = (Elf_Addr *)(obj->relocbase + rela->r_offset); + def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, + SYMLOOK_IN_PLT | flags, NULL, lockstate); + if (def == NULL) + return (-1); + if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC) + continue; + lock_release(rtld_bind_lock, lockstate); + target = (Elf_Addr)rtld_resolve_ifunc(defobj, def); + wlock_acquire(rtld_bind_lock, lockstate); + reloc_jmpslot(where, target, defobj, obj, + (const Elf_Rel *)rela); + } + } + obj->gnu_ifunc = false; return (0); +#endif } void @@ -544,10 +643,67 @@ init_pltgot(Obj_Entry *obj) #endif } +/* + * Actual values are 32 bit. + */ +u_long cpu_features; +u_long cpu_features2; + +void +powerpc64_abi_variant_hook(Elf_Auxinfo** aux_info) +{ + /* + * Since aux_info[] is easier to work with than aux, go ahead and + * initialize cpu_features / cpu_features2. + */ + cpu_features = -1UL; + cpu_features2 = -1UL; + if (aux_info[AT_HWCAP] != NULL) + cpu_features = (uint32_t)aux_info[AT_HWCAP]->a_un.a_val; + if (aux_info[AT_HWCAP2] != NULL) + cpu_features2 = (uint32_t)aux_info[AT_HWCAP2]->a_un.a_val; +} + void ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) { + int mib[2]; + size_t len, size; + uint64_t temp; + /* + * If the cpu features were not passed in auxv, we need to process + * them manually. + * XXX Remove after ELFv2 flag day. + */ + + if (cpu_features == -1UL) { + size = sizeof(temp); + len = 2; + if (sysctlnametomib("hw.cpu_features", mib, &len)) + goto error; + + if (sysctl(mib, len, &temp, &size, NULL, 0)) + goto error; + cpu_features = (uint32_t)(temp); + } + + if (cpu_features2 == -1UL) { + size = sizeof(temp); + len = 2; + if (sysctlnametomib("hw.cpu_features2", mib, &len)) + goto error; + + if (sysctl(mib, len, &temp, &size, NULL, 0)) + goto error; + cpu_features2 = (uint32_t)(temp); + } + + return; + +error: + _rtld_error("Unable to determine cpu info!"); + rtld_die(); } void diff --git a/libexec/rtld-elf/powerpc64/rtld_machdep.h b/libexec/rtld-elf/powerpc64/rtld_machdep.h index f95f7f3f374..40a2328aba0 100644 --- a/libexec/rtld-elf/powerpc64/rtld_machdep.h +++ b/libexec/rtld-elf/powerpc64/rtld_machdep.h @@ -53,8 +53,13 @@ void reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase); #define call_init_pointer(obj, target) \ (((InitArrFunc)(target))(main_argc, main_argv, environ)) +extern u_long cpu_features; /* r3 */ +extern u_long cpu_features2; /* r4 */ +/* r5-r10: ifunc resolver parameters reserved for future assignment. */ #define call_ifunc_resolver(ptr) \ - (((Elf_Addr (*)(void))ptr)()) + (((Elf_Addr (*)(uint32_t, uint32_t, uint64_t, uint64_t, uint64_t, \ + uint64_t, uint64_t, uint64_t))ptr)((uint32_t)cpu_features, \ + (uint32_t)cpu_features2, 0, 0, 0, 0, 0, 0)) /* * TLS @@ -83,6 +88,7 @@ extern void *__tls_get_addr(tls_index* ti); #define RTLD_DEFAULT_STACK_PF_EXEC PF_X #define RTLD_DEFAULT_STACK_EXEC PROT_EXEC -#define md_abi_variant_hook(x) +extern void powerpc64_abi_variant_hook(Elf_Auxinfo **); +#define md_abi_variant_hook(x) powerpc64_abi_variant_hook(x) #endif diff --git a/sys/sys/elf_common.h b/sys/sys/elf_common.h index f7484bd41c1..1ea5089d607 100644 --- a/sys/sys/elf_common.h +++ b/sys/sys/elf_common.h @@ -1220,6 +1220,7 @@ typedef struct { #define R_PPC64_DTPMOD64 68 #define R_PPC64_TPREL64 73 #define R_PPC64_DTPREL64 78 +#define R_PPC64_IRELATIVE 248 /* * TLS relocations