94084c
commit 990c953bce06d77360d2e933faa9a008e2c55405
94084c
Author: Florian Weimer <fweimer@redhat.com>
94084c
Date:   Fri Jan 14 20:16:05 2022 +0100
94084c
94084c
    x86: Add x86-64-vN check to early startup
94084c
    
94084c
    This ISA level covers the glibc build itself.  <dl-hwcap-check.h>
94084c
    cannot be used because this check (by design) happens before
94084c
    DL_PLATFORM_INIT and the x86 CPU flags initialization.
94084c
    
94084c
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
94084c
94084c
diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile
94084c
index 5ee06f94735e5189..36ca1a7126047b86 100644
94084c
--- a/sysdeps/x86/Makefile
94084c
+++ b/sysdeps/x86/Makefile
94084c
@@ -7,6 +7,7 @@ sysdep_routines += get-cpuid-feature-leaf
94084c
 sysdep-dl-routines += dl-get-cpu-features
94084c
 sysdep_headers += sys/platform/x86.h bits/platform/x86.h
94084c
 
94084c
+CFLAGS-dl-get-cpu-features.os += $(rtld-early-cflags)
94084c
 CFLAGS-get-cpuid-feature-leaf.o += $(no-stack-protector)
94084c
 
94084c
 tests += tst-get-cpu-features tst-get-cpu-features-static \
94084c
diff --git a/sysdeps/x86/dl-get-cpu-features.c b/sysdeps/x86/dl-get-cpu-features.c
94084c
index 839803c746f408ed..6ccde4404b13a725 100644
94084c
--- a/sysdeps/x86/dl-get-cpu-features.c
94084c
+++ b/sysdeps/x86/dl-get-cpu-features.c
94084c
@@ -20,6 +20,7 @@
94084c
 
94084c
 #ifdef SHARED
94084c
 # include <cpu-features.c>
94084c
+# include <gcc-macros.h>
94084c
 
94084c
 /* NB: Normally, DL_PLATFORM_INIT calls init_cpu_features to initialize
94084c
    CPU features in dynamic executable.  But when loading ld.so inside of
94084c
@@ -36,7 +37,35 @@ _dl_x86_init_cpu_features (void)
94084c
 {
94084c
   struct cpu_features *cpu_features = __get_cpu_features ();
94084c
   if (cpu_features->basic.kind == arch_kind_unknown)
94084c
-    init_cpu_features (cpu_features);
94084c
+    {
94084c
+      init_cpu_features (cpu_features);
94084c
+
94084c
+# if IS_IN (rtld)
94084c
+      /* See isa-level.c.  */
94084c
+#  if defined GCCMACRO__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16		\
94084c
+  && defined HAVE_X86_LAHF_SAHF && defined GCCMACRO__POPCNT__		\
94084c
+  && defined GCCMACRO__SSE3__ && defined GCCMACRO__SSSE3__		\
94084c
+  && defined GCCMACRO__SSE4_1__ && defined GCCMACRO__SSE4_2__
94084c
+      if (!(cpu_features->isa_1 & GNU_PROPERTY_X86_ISA_1_V2))
94084c
+	_dl_fatal_printf ("\
94084c
+Fatal glibc error: CPU does not support x86-64-v%d\n", 2);
94084c
+#   if defined GCCMACRO__AVX__ && defined GCCMACRO__AVX2__ \
94084c
+  && defined GCCMACRO__F16C__ && defined GCCMACRO__FMA__   \
94084c
+  && defined GCCMACRO__LZCNT__ && defined HAVE_X86_MOVBE
94084c
+      if (!(cpu_features->isa_1 & GNU_PROPERTY_X86_ISA_1_V3))
94084c
+	_dl_fatal_printf ("\
94084c
+Fatal glibc error: CPU does not support x86-64-v%d\n", 3);
94084c
+#    if defined GCCMACRO__AVX512F__ && defined GCCMACRO__AVX512BW__ \
94084c
+     && defined GCCMACRO__AVX512CD__ && defined GCCMACRO__AVX512DQ__ \
94084c
+     && defined GCCMACRO__AVX512VL__
94084c
+      if (!(cpu_features->isa_1 & GNU_PROPERTY_X86_ISA_1_V4))
94084c
+	_dl_fatal_printf ("\
94084c
+Fatal glibc error: CPU does not support x86-64-v%d\n", 4);
94084c
+#    endif /* ISA level 4 */
94084c
+#   endif /* ISA level 3 */
94084c
+#  endif /* ISA level 2 */
94084c
+# endif /* IS_IN (rtld) */
94084c
+    }
94084c
 }
94084c
 
94084c
 __ifunc (__x86_cpu_features, __x86_cpu_features, NULL, void,