diff options
author | kib <kib@FreeBSD.org> | 2016-11-23 17:48:43 +0000 |
---|---|---|
committer | kib <kib@FreeBSD.org> | 2016-11-23 17:48:43 +0000 |
commit | 9ef73b375eb639d0ca5e6fe029a6aed4bf092ca6 (patch) | |
tree | a18aab4ed7ad8e0389d6779e328cfde735de1868 /libexec/rtld-elf | |
parent | 7cd55eaa0f37289e264d9325cb65d7b221badb56 (diff) | |
download | FreeBSD-src-9ef73b375eb639d0ca5e6fe029a6aed4bf092ca6.zip FreeBSD-src-9ef73b375eb639d0ca5e6fe029a6aed4bf092ca6.tar.gz |
MFC r308689:
Pass CPUID[1] %edx (cpu_feature), %ecx (cpu_feature2) and
CPUID[7].%ebx (cpu_stdext_feature), %ecx (cpu_stdext_feature2) to the
ifunc resolvers on x86.
MFC r308925:
Adjust r308689 to make rtld compilable with either in-tree or
(hopefully) stock gcc 4.2.1 on i386 and other arches.
Diffstat (limited to 'libexec/rtld-elf')
-rw-r--r-- | libexec/rtld-elf/amd64/reloc.c | 22 | ||||
-rw-r--r-- | libexec/rtld-elf/amd64/rtld_machdep.h | 8 | ||||
-rw-r--r-- | libexec/rtld-elf/arm/reloc.c | 5 | ||||
-rw-r--r-- | libexec/rtld-elf/arm/rtld_machdep.h | 3 | ||||
-rw-r--r-- | libexec/rtld-elf/i386/reloc.c | 54 | ||||
-rw-r--r-- | libexec/rtld-elf/i386/rtld_machdep.h | 8 | ||||
-rw-r--r-- | libexec/rtld-elf/ia64/reloc.c | 5 | ||||
-rw-r--r-- | libexec/rtld-elf/ia64/rtld_machdep.h | 3 | ||||
-rw-r--r-- | libexec/rtld-elf/mips/reloc.c | 5 | ||||
-rw-r--r-- | libexec/rtld-elf/mips/rtld_machdep.h | 3 | ||||
-rw-r--r-- | libexec/rtld-elf/powerpc/reloc.c | 5 | ||||
-rw-r--r-- | libexec/rtld-elf/powerpc/rtld_machdep.h | 3 | ||||
-rw-r--r-- | libexec/rtld-elf/powerpc64/reloc.c | 5 | ||||
-rw-r--r-- | libexec/rtld-elf/powerpc64/rtld_machdep.h | 3 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld.c | 3 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld.h | 1 | ||||
-rw-r--r-- | libexec/rtld-elf/sparc64/reloc.c | 5 | ||||
-rw-r--r-- | libexec/rtld-elf/sparc64/rtld_machdep.h | 5 |
18 files changed, 142 insertions, 4 deletions
diff --git a/libexec/rtld-elf/amd64/reloc.c b/libexec/rtld-elf/amd64/reloc.c index 96cf93c..f13789f 100644 --- a/libexec/rtld-elf/amd64/reloc.c +++ b/libexec/rtld-elf/amd64/reloc.c @@ -34,6 +34,7 @@ #include <sys/param.h> #include <sys/mman.h> #include <machine/sysarch.h> +#include <machine/cpufunc.h> #include <dlfcn.h> #include <err.h> @@ -406,7 +407,7 @@ reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate) ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend); where = (Elf_Addr *)(obj->relocbase + rela->r_offset); lock_release(rtld_bind_lock, lockstate); - target = ((Elf_Addr (*)(void))ptr)(); + target = call_ifunc_resolver(ptr); wlock_acquire(rtld_bind_lock, lockstate); *where = target; break; @@ -450,6 +451,25 @@ reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate) return (0); } +uint32_t cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2; + +void +ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) +{ + u_int p[4], cpu_high; + + do_cpuid(1, p); + cpu_feature = p[3]; + cpu_feature2 = p[2]; + do_cpuid(0, p); + cpu_high = p[0]; + if (cpu_high >= 7) { + cpuid_count(7, 0, p); + cpu_stdext_feature = p[1]; + cpu_stdext_feature2 = p[2]; + } +} + void allocate_initial_tls(Obj_Entry *objs) { diff --git a/libexec/rtld-elf/amd64/rtld_machdep.h b/libexec/rtld-elf/amd64/rtld_machdep.h index cb5e9a1..d6d7dff 100644 --- a/libexec/rtld-elf/amd64/rtld_machdep.h +++ b/libexec/rtld-elf/amd64/rtld_machdep.h @@ -61,6 +61,14 @@ reloc_jmpslot(Elf_Addr *where, Elf_Addr target, #define call_init_pointer(obj, target) \ (((InitArrFunc)(target))(main_argc, main_argv, environ)) +extern uint32_t cpu_feature; +extern uint32_t cpu_feature2; +extern uint32_t cpu_stdext_feature; +extern uint32_t cpu_stdext_feature2; +#define call_ifunc_resolver(ptr) \ + (((Elf_Addr (*)(uint32_t, uint32_t, uint32_t, uint32_t))ptr)( \ + cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2)) + #define round(size, align) \ (((size) + (align) - 1) & ~((align) - 1)) #define calculate_first_tls_offset(size, align) \ diff --git a/libexec/rtld-elf/arm/reloc.c b/libexec/rtld-elf/arm/reloc.c index 8b99514..40e6d7c 100644 --- a/libexec/rtld-elf/arm/reloc.c +++ b/libexec/rtld-elf/arm/reloc.c @@ -435,6 +435,11 @@ reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj, } void +ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) +{ +} + +void allocate_initial_tls(Obj_Entry *objs) { #ifdef ARM_TP_ADDRESS diff --git a/libexec/rtld-elf/arm/rtld_machdep.h b/libexec/rtld-elf/arm/rtld_machdep.h index f980de0..2c6eb31 100644 --- a/libexec/rtld-elf/arm/rtld_machdep.h +++ b/libexec/rtld-elf/arm/rtld_machdep.h @@ -51,6 +51,9 @@ Elf_Addr reloc_jmpslot(Elf_Addr *where, Elf_Addr target, #define call_init_pointer(obj, target) \ (((InitArrFunc)(target))(main_argc, main_argv, environ)) +#define call_ifunc_resolver(ptr) \ + (((Elf_Addr (*)(void))ptr)()) + #define TLS_TCB_SIZE 8 typedef struct { unsigned long ti_module; diff --git a/libexec/rtld-elf/i386/reloc.c b/libexec/rtld-elf/i386/reloc.c index 2d6021c..42efcf7 100644 --- a/libexec/rtld-elf/i386/reloc.c +++ b/libexec/rtld-elf/i386/reloc.c @@ -359,7 +359,7 @@ reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate) case R_386_IRELATIVE: where = (Elf_Addr *)(obj->relocbase + rel->r_offset); lock_release(rtld_bind_lock, lockstate); - target = ((Elf_Addr (*)(void))(obj->relocbase + *where))(); + target = call_ifunc_resolver(obj->relocbase + *where); wlock_acquire(rtld_bind_lock, lockstate); *where = target; break; @@ -404,6 +404,58 @@ reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate) return (0); } +uint32_t cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2; + +static void +rtld_cpuid_count(int idx, int cnt, u_int *p) +{ + + __asm __volatile( + " pushl %%ebx\n" + " cpuid\n" + " movl %%ebx,%1\n" + " popl %%ebx\n" + : "=a" (p[0]), "=r" (p[1]), "=c" (p[2]), "=d" (p[3]) + : "0" (idx), "2" (cnt)); +} + +void +ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) +{ + u_int p[4], cpu_high; + int cpuid_supported; + + __asm __volatile( + " pushfl\n" + " popl %%eax\n" + " movl %%eax,%%ecx\n" + " xorl $0x200000,%%eax\n" + " pushl %%eax\n" + " popfl\n" + " pushfl\n" + " popl %%eax\n" + " xorl %%eax,%%ecx\n" + " je 1f\n" + " movl $1,%0\n" + " jmp 2f\n" + "1: movl $0,%0\n" + "2:\n" + : "=r" (cpuid_supported) : : "eax", "ecx"); + if (!cpuid_supported) + return; + + rtld_cpuid_count(1, 0, p); + cpu_feature = p[3]; + cpu_feature2 = p[2]; + rtld_cpuid_count(0, 0, p); + cpu_high = p[0]; + if (cpu_high >= 7) { + rtld_cpuid_count(7, 0, p); + cpu_stdext_feature = p[1]; + cpu_stdext_feature2 = p[2]; + } +} + void allocate_initial_tls(Obj_Entry *objs) { diff --git a/libexec/rtld-elf/i386/rtld_machdep.h b/libexec/rtld-elf/i386/rtld_machdep.h index 5c328da..850b170 100644 --- a/libexec/rtld-elf/i386/rtld_machdep.h +++ b/libexec/rtld-elf/i386/rtld_machdep.h @@ -61,6 +61,14 @@ reloc_jmpslot(Elf_Addr *where, Elf_Addr target, #define call_init_pointer(obj, target) \ (((InitArrFunc)(target))(main_argc, main_argv, environ)) +extern uint32_t cpu_feature; +extern uint32_t cpu_feature2; +extern uint32_t cpu_stdext_feature; +extern uint32_t cpu_stdext_feature2; +#define call_ifunc_resolver(ptr) \ + (((Elf_Addr (*)(uint32_t, uint32_t, uint32_t, uint32_t))ptr)( \ + cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2)) + #define round(size, align) \ (((size) + (align) - 1) & ~((align) - 1)) #define calculate_first_tls_offset(size, align) \ diff --git a/libexec/rtld-elf/ia64/reloc.c b/libexec/rtld-elf/ia64/reloc.c index 539b7ef..76f06f5 100644 --- a/libexec/rtld-elf/ia64/reloc.c +++ b/libexec/rtld-elf/ia64/reloc.c @@ -603,6 +603,11 @@ call_init_pointer(const Obj_Entry *obj, Elf_Addr target) ((InitArrFunc) &fptr)(main_argc, main_argv, environ); } +void +ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) +{ +} + /* Initialize the special PLT entries. */ void init_pltgot(Obj_Entry *obj) diff --git a/libexec/rtld-elf/ia64/rtld_machdep.h b/libexec/rtld-elf/ia64/rtld_machdep.h index b00a90fc..f1e2554 100644 --- a/libexec/rtld-elf/ia64/rtld_machdep.h +++ b/libexec/rtld-elf/ia64/rtld_machdep.h @@ -54,6 +54,9 @@ void *make_function_pointer(const Elf_Sym *, const struct Struct_Obj_Entry *); void call_initfini_pointer(const struct Struct_Obj_Entry *, Elf_Addr); void call_init_pointer(const struct Struct_Obj_Entry *, Elf_Addr); +#define call_ifunc_resolver(ptr) \ + (((Elf_Addr (*)(void))ptr)()) + #define TLS_TCB_SIZE 16 #define round(size, align) \ diff --git a/libexec/rtld-elf/mips/reloc.c b/libexec/rtld-elf/mips/reloc.c index 809adb8..2ca915e 100644 --- a/libexec/rtld-elf/mips/reloc.c +++ b/libexec/rtld-elf/mips/reloc.c @@ -618,6 +618,11 @@ reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj, } void +ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) +{ +} + +void allocate_initial_tls(Obj_Entry *objs) { char *tls; diff --git a/libexec/rtld-elf/mips/rtld_machdep.h b/libexec/rtld-elf/mips/rtld_machdep.h index befbf13..4d2c9d9 100644 --- a/libexec/rtld-elf/mips/rtld_machdep.h +++ b/libexec/rtld-elf/mips/rtld_machdep.h @@ -52,6 +52,9 @@ Elf_Addr reloc_jmpslot(Elf_Addr *where, Elf_Addr target, #define call_init_pointer(obj, target) \ (((InitArrFunc)(target))(main_argc, main_argv, environ)) +#define call_ifunc_resolver(ptr) \ + (((Elf_Addr (*)(void))ptr)()) + typedef struct { unsigned long ti_module; unsigned long ti_offset; diff --git a/libexec/rtld-elf/powerpc/reloc.c b/libexec/rtld-elf/powerpc/reloc.c index a9a16de..f78c1d5 100644 --- a/libexec/rtld-elf/powerpc/reloc.c +++ b/libexec/rtld-elf/powerpc/reloc.c @@ -620,6 +620,11 @@ init_pltgot(Obj_Entry *obj) } void +ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) +{ +} + +void allocate_initial_tls(Obj_Entry *list) { register Elf_Addr **tp __asm__("r2"); diff --git a/libexec/rtld-elf/powerpc/rtld_machdep.h b/libexec/rtld-elf/powerpc/rtld_machdep.h index 1ddf1bc..9ab0e75 100644 --- a/libexec/rtld-elf/powerpc/rtld_machdep.h +++ b/libexec/rtld-elf/powerpc/rtld_machdep.h @@ -51,6 +51,9 @@ Elf_Addr reloc_jmpslot(Elf_Addr *where, Elf_Addr target, #define call_init_pointer(obj, target) \ (((InitArrFunc)(target))(main_argc, main_argv, environ)) +#define call_ifunc_resolver(ptr) \ + (((Elf_Addr (*)(void))ptr)()) + /* * Lazy binding entry point, called via PLT. */ diff --git a/libexec/rtld-elf/powerpc64/reloc.c b/libexec/rtld-elf/powerpc64/reloc.c index b33f6fe..79fbd2d 100644 --- a/libexec/rtld-elf/powerpc64/reloc.c +++ b/libexec/rtld-elf/powerpc64/reloc.c @@ -488,6 +488,11 @@ init_pltgot(Obj_Entry *obj) } void +ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) +{ +} + +void allocate_initial_tls(Obj_Entry *list) { Elf_Addr **tp; diff --git a/libexec/rtld-elf/powerpc64/rtld_machdep.h b/libexec/rtld-elf/powerpc64/rtld_machdep.h index b88ed9d..ce2139c 100644 --- a/libexec/rtld-elf/powerpc64/rtld_machdep.h +++ b/libexec/rtld-elf/powerpc64/rtld_machdep.h @@ -51,6 +51,9 @@ Elf_Addr reloc_jmpslot(Elf_Addr *where, Elf_Addr target, #define call_init_pointer(obj, target) \ (((InitArrFunc)(target))(main_argc, main_argv, environ)) +#define call_ifunc_resolver(ptr) \ + (((Elf_Addr (*)(void))ptr)()) + /* * Lazy binding entry point, called via PLT. */ diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index 0b72f85..75fda31 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -635,6 +635,7 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) r_debug_state(NULL, &obj_main->linkmap); /* say hello to gdb! */ map_stacks_exec(NULL); + ifunc_init(aux); dbg("resolving ifuncs"); if (resolve_objects_ifunc(obj_main, @@ -683,7 +684,7 @@ rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def) Elf_Addr target; ptr = (void *)make_function_pointer(def, obj); - target = ((Elf_Addr (*)(void))ptr)(); + target = call_ifunc_resolver(ptr); return ((void *)target); } diff --git a/libexec/rtld-elf/rtld.h b/libexec/rtld-elf/rtld.h index d63222d..cb0448d 100644 --- a/libexec/rtld-elf/rtld.h +++ b/libexec/rtld-elf/rtld.h @@ -380,6 +380,7 @@ void dump_Elf_Rela(Obj_Entry *, const Elf_Rela *, u_long); unsigned long elf_hash(const char *); const Elf_Sym *find_symdef(unsigned long, const Obj_Entry *, const Obj_Entry **, int, SymCache *, struct Struct_RtldLockState *); +void ifunc_init(Elf_Auxinfo *); void init_pltgot(Obj_Entry *); void lockdflt_init(void); void digest_notes(Obj_Entry *, Elf_Addr, Elf_Addr); diff --git a/libexec/rtld-elf/sparc64/reloc.c b/libexec/rtld-elf/sparc64/reloc.c index 242fb8e..ab16879 100644 --- a/libexec/rtld-elf/sparc64/reloc.c +++ b/libexec/rtld-elf/sparc64/reloc.c @@ -786,6 +786,11 @@ reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *obj, return (target); } +void +ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) +{ +} + /* * Install rtld function call into this PLT slot. */ diff --git a/libexec/rtld-elf/sparc64/rtld_machdep.h b/libexec/rtld-elf/sparc64/rtld_machdep.h index 44fe2cf..45e0202 100644 --- a/libexec/rtld-elf/sparc64/rtld_machdep.h +++ b/libexec/rtld-elf/sparc64/rtld_machdep.h @@ -53,7 +53,10 @@ Elf_Addr reloc_jmpslot(Elf_Addr *, Elf_Addr, #define call_init_pointer(obj, target) \ (((InitArrFunc)(target))(main_argc, main_argv, environ)) -#define round(size, align) \ +#define call_ifunc_resolver(ptr) \ + (((Elf_Addr (*)(void))ptr)()) + +#define round(size, align) \ (((size) + (align) - 1) & ~((align) - 1)) #define calculate_first_tls_offset(size, align) \ round(size, align) |