* V5 [PATCH 0/2] x86: Support GNU_PROPERTY_X86_ISA_1_NEEDED marker [BZ #26717] @ 2020-12-06 14:49 H.J. Lu 2020-12-06 14:49 ` V5 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] " H.J. Lu 2020-12-06 14:49 ` V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps H.J. Lu 0 siblings, 2 replies; 27+ messages in thread From: H.J. Lu @ 2020-12-06 14:49 UTC (permalink / raw) To: libc-alpha; +Cc: Florian Weimer GCC 11 supports -march=x86-64-v[234] to enable x86 micro-architecture ISA levels and -mneeded to emit GNU_PROPERTY_X86_ISA_1_NEEDED property with GNU_PROPERTY_X86_ISA_1_V[234] marker. GNU_PROPERTY_X86_ISA_1_NEEDED property in x86 ELF binaries indicate the micro-architecture ISA level required to execute the binary. The marker must be added by programmers explicitly in one of 3 ways: 1. Pass -mneeded to GCC. 2. Add the marker in the linker inputs as this patch does. 3. Pass -z x86-64-v[234] to the linker. Marked elf/tst-isa-level-1 with x86-64-v4, ran it on x86-64-v3 machine and got: [hjl@gnu-cfl-2 build-x86_64-linux]$ ./elf/tst-isa-level-1 ./elf/tst-isa-level-1: CPU ISA level is lower than required [hjl@gnu-cfl-2 build-x86_64-linux]$ H.J. Lu (2): x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] ldconfig/x86: Add ISA level check to glibc-hwcaps config.h.in | 3 + elf/elf.h | 35 +++--- elf/ldconfig.c | 9 +- elf/readelflib.c | 83 ++++++++++++- elf/readlib.c | 7 +- elf/tst-glibc-hwcaps-2-cache.c | 45 +++++++ .../etc/ld.so.conf | 2 + .../postclean.req | 0 elf/tst-glibc-hwcaps-2-cache.script | 6 + sysdeps/generic/ldconfig.h | 9 +- sysdeps/generic/read-prop.h | 32 +++++ sysdeps/unix/sysv/linux/x86/read-prop.h | 57 +++++++++ sysdeps/unix/sysv/linux/x86/readelflib.c | 67 +++++++++-- sysdeps/unix/sysv/linux/x86_64/Makefile | 26 ++++ .../sysv/linux/x86_64/tst-glibc-hwcaps-2.c | 50 ++++++++ .../linux/x86_64/x86-64-isa-level-VALUE.c | 4 + sysdeps/x86/Makefile | 10 ++ sysdeps/x86/abi-note.c | 29 +++++ sysdeps/x86/configure | 101 ++++++++++++++++ sysdeps/x86/configure.ac | 26 ++++ sysdeps/x86/cpu-features.c | 3 + sysdeps/x86/dl-cet.c | 12 +- sysdeps/x86/dl-prop.h | 113 +++++++++++++----- sysdeps/x86/get-isa-level.h | 66 ++++++++++ sysdeps/x86/include/cpu-features.h | 2 + sysdeps/x86/isa-level.c | 95 +++++++++++++++ sysdeps/x86/link_map.h | 18 +-- sysdeps/x86/tst-isa-level-1.c | 74 ++++++++++++ sysdeps/x86/tst-isa-level-mod-1.c | 23 ++++ sysdeps/x86/tst-isa-level-mod-1a.c | 2 + sysdeps/x86/tst-isa-level-mod-1b.c | 1 + sysdeps/x86_64/dl-hwcaps-subdirs.c | 30 ++--- sysdeps/x86_64/tst-glibc-hwcaps.c | 41 ++----- 33 files changed, 950 insertions(+), 131 deletions(-) create mode 100644 elf/tst-glibc-hwcaps-2-cache.c create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/postclean.req create mode 100644 elf/tst-glibc-hwcaps-2-cache.script create mode 100644 sysdeps/generic/read-prop.h create mode 100644 sysdeps/unix/sysv/linux/x86/read-prop.h create mode 100644 sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c create mode 100644 sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c create mode 100644 sysdeps/x86/abi-note.c create mode 100644 sysdeps/x86/get-isa-level.h create mode 100644 sysdeps/x86/isa-level.c create mode 100644 sysdeps/x86/tst-isa-level-1.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1a.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1b.c -- 2.28.0 ^ permalink raw reply [flat|nested] 27+ messages in thread
* V5 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] 2020-12-06 14:49 V5 [PATCH 0/2] x86: Support GNU_PROPERTY_X86_ISA_1_NEEDED marker [BZ #26717] H.J. Lu @ 2020-12-06 14:49 ` H.J. Lu 2021-01-06 12:00 ` Adhemerval Zanella 2020-12-06 14:49 ` V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps H.J. Lu 1 sibling, 1 reply; 27+ messages in thread From: H.J. Lu @ 2020-12-06 14:49 UTC (permalink / raw) To: libc-alpha; +Cc: Florian Weimer GCC 11 supports -march=x86-64-v[234] to enable x86 micro-architecture ISA levels: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97250 and -mneeded to emit GNU_PROPERTY_X86_ISA_1_NEEDED property with GNU_PROPERTY_X86_ISA_1_V[234] marker: https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/13 Binutils support for GNU_PROPERTY_X86_ISA_1_V[234] marker were added by commit b0ab06937385e0ae25cebf1991787d64f439bf12 Author: H.J. Lu <hjl.tools@gmail.com> Date: Fri Oct 30 06:49:57 2020 -0700 x86: Support GNU_PROPERTY_X86_ISA_1_BASELINE marker and commit 32930e4edbc06bc6f10c435dbcc63131715df678 Author: H.J. Lu <hjl.tools@gmail.com> Date: Fri Oct 9 05:05:57 2020 -0700 x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker GNU_PROPERTY_X86_ISA_1_NEEDED property in x86 ELF binaries indicate the micro-architecture ISA level required to execute the binary. The marker must be added by programmers explicitly in one of 3 ways: 1. Pass -mneeded to GCC. 2. Add the marker in the linker inputs as this patch does. 3. Pass -z x86-64-v[234] to the linker. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] marker support to ld.so if binutils 2.32 or newer is used to build glibc: 1. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] markers to elf.h. 2. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] marker to abi-note.o based on the ISA level used to compile abi-note.o, assuming that the same ISA level is used to compile the whole glibc. 3. Add isa_1 to cpu_features to record the supported x86 ISA level. 4. Rename _dl_process_cet_property_note to _dl_process_property_note and add GNU_PROPERTY_X86_ISA_1_V[234] marker detection. 5. Update _rtld_main_check and _dl_open_check to check loaded objects with the incompatible ISA level. 6. Add a testcase to verify that dlopen an x86-64-v4 shared object fails on lesser platforms. 7. Use <get-isa-level.h> in dl-hwcaps-subdirs.c and tst-glibc-hwcaps.c. Marked elf/tst-isa-level-1 with x86-64-v4, ran it on x86-64-v3 machine and got: [hjl@gnu-cfl-2 build-x86_64-linux]$ ./elf/tst-isa-level-1 ./elf/tst-isa-level-1: CPU ISA level is lower than required [hjl@gnu-cfl-2 build-x86_64-linux]$ --- config.h.in | 3 + elf/elf.h | 35 ++++----- sysdeps/x86/Makefile | 10 +++ sysdeps/x86/abi-note.c | 29 ++++++++ sysdeps/x86/configure | 101 ++++++++++++++++++++++++++ sysdeps/x86/configure.ac | 26 +++++++ sysdeps/x86/cpu-features.c | 3 + sysdeps/x86/dl-cet.c | 12 ++- sysdeps/x86/dl-prop.h | 113 +++++++++++++++++++++-------- sysdeps/x86/get-isa-level.h | 66 +++++++++++++++++ sysdeps/x86/include/cpu-features.h | 2 + sysdeps/x86/isa-level.c | 95 ++++++++++++++++++++++++ sysdeps/x86/link_map.h | 18 +++-- sysdeps/x86/tst-isa-level-1.c | 74 +++++++++++++++++++ sysdeps/x86/tst-isa-level-mod-1.c | 23 ++++++ sysdeps/x86/tst-isa-level-mod-1a.c | 2 + sysdeps/x86/tst-isa-level-mod-1b.c | 1 + sysdeps/x86_64/dl-hwcaps-subdirs.c | 30 ++------ sysdeps/x86_64/tst-glibc-hwcaps.c | 41 +++-------- 19 files changed, 571 insertions(+), 113 deletions(-) create mode 100644 sysdeps/x86/abi-note.c create mode 100644 sysdeps/x86/get-isa-level.h create mode 100644 sysdeps/x86/isa-level.c create mode 100644 sysdeps/x86/tst-isa-level-1.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1a.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1b.c diff --git a/config.h.in b/config.h.in index b823c8e080..dc3854f4eb 100644 --- a/config.h.in +++ b/config.h.in @@ -266,4 +266,7 @@ /* The default value of x86 CET control. */ #define DEFAULT_DL_X86_CET_CONTROL cet_elf_property +/* Define if x86 ISA level should be included in shared libraries. */ +#undef INCLUDE_X86_ISA_LEVEL + #endif diff --git a/elf/elf.h b/elf/elf.h index bd7af7cca4..42fd697129 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -1325,31 +1325,26 @@ typedef struct /* The x86 instruction sets indicated by the corresponding bits are used in program. Their support in the hardware is optional. */ -#define GNU_PROPERTY_X86_ISA_1_USED 0xc0000000 +#define GNU_PROPERTY_X86_ISA_1_USED 0xc0010002 /* The x86 instruction sets indicated by the corresponding bits are used in program and they must be supported by the hardware. */ -#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0000001 +#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0008002 /* X86 processor-specific features used in program. */ #define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 -#define GNU_PROPERTY_X86_ISA_1_486 (1U << 0) -#define GNU_PROPERTY_X86_ISA_1_586 (1U << 1) -#define GNU_PROPERTY_X86_ISA_1_686 (1U << 2) -#define GNU_PROPERTY_X86_ISA_1_SSE (1U << 3) -#define GNU_PROPERTY_X86_ISA_1_SSE2 (1U << 4) -#define GNU_PROPERTY_X86_ISA_1_SSE3 (1U << 5) -#define GNU_PROPERTY_X86_ISA_1_SSSE3 (1U << 6) -#define GNU_PROPERTY_X86_ISA_1_SSE4_1 (1U << 7) -#define GNU_PROPERTY_X86_ISA_1_SSE4_2 (1U << 8) -#define GNU_PROPERTY_X86_ISA_1_AVX (1U << 9) -#define GNU_PROPERTY_X86_ISA_1_AVX2 (1U << 10) -#define GNU_PROPERTY_X86_ISA_1_AVX512F (1U << 11) -#define GNU_PROPERTY_X86_ISA_1_AVX512CD (1U << 12) -#define GNU_PROPERTY_X86_ISA_1_AVX512ER (1U << 13) -#define GNU_PROPERTY_X86_ISA_1_AVX512PF (1U << 14) -#define GNU_PROPERTY_X86_ISA_1_AVX512VL (1U << 15) -#define GNU_PROPERTY_X86_ISA_1_AVX512DQ (1U << 16) -#define GNU_PROPERTY_X86_ISA_1_AVX512BW (1U << 17) +/* GNU_PROPERTY_X86_ISA_1_BASELINE: CMOV, CX8 (cmpxchg8b), FPU (fld), + MMX, OSFXSR (fxsave), SCE (syscall), SSE and SSE2. */ +#define GNU_PROPERTY_X86_ISA_1_BASELINE (1U << 0) +/* GNU_PROPERTY_X86_ISA_1_V2: GNU_PROPERTY_X86_ISA_1_BASELINE, + CMPXCHG16B (cmpxchg16b), LAHF-SAHF (lahf), POPCNT (popcnt), SSE3, + SSSE3, SSE4.1 and SSE4.2. */ +#define GNU_PROPERTY_X86_ISA_1_V2 (1U << 1) +/* GNU_PROPERTY_X86_ISA_1_V3: GNU_PROPERTY_X86_ISA_1_V2, AVX, AVX2, BMI1, + BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE. */ +#define GNU_PROPERTY_X86_ISA_1_V3 (1U << 2) +/* GNU_PROPERTY_X86_ISA_1_V4: GNU_PROPERTY_X86_ISA_1_V3, AVX512F, + AVX512BW, AVX512CD, AVX512DQ and AVX512VL. */ +#define GNU_PROPERTY_X86_ISA_1_V4 (1U << 3) /* This indicates that all executable sections are compatible with IBT. */ diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index 081cc72e93..109f03e9b9 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -9,6 +9,16 @@ sysdep_headers += sys/platform/x86.h tests += tst-get-cpu-features tst-get-cpu-features-static \ tst-cpu-features-cpuinfo tst-cpu-features-supports tests-static += tst-get-cpu-features-static +ifeq (yesyes,$(enable-x86-isa-level)$(config-cflags-skylake-avx512)) +tests += tst-isa-level-1 +modules-names += tst-isa-level-mod-1a tst-isa-level-mod-1b + +CFLAGS-tst-isa-level-mod-1a.c += -march=skylake-avx512 \ + -DINCLUDE_X86_ISA_LEVEL +$(objpfx)tst-isa-level-1: $(libdl) +$(objpfx)tst-isa-level-1.out: $(objpfx)tst-isa-level-mod-1a.so \ + $(objpfx)tst-isa-level-mod-1b.so +endif endif ifeq ($(subdir),math) diff --git a/sysdeps/x86/abi-note.c b/sysdeps/x86/abi-note.c new file mode 100644 index 0000000000..5140e6ab47 --- /dev/null +++ b/sysdeps/x86/abi-note.c @@ -0,0 +1,29 @@ +/* Special .init and .fini section support. x86-64 version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + In addition to the permissions in the GNU Lesser General Public + License, the Free Software Foundation gives you unlimited + permission to link the compiled version of this file with other + programs, and to distribute those programs without any restriction + coming from the use of this file. (The Lesser General Public + License restrictions do apply in other respects; for example, they + cover modification of the file, and distribution when not linked + into another program.) + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <isa-level.c> +#include <csu/abi-note.c> diff --git a/sysdeps/x86/configure b/sysdeps/x86/configure index 81cc4e80d6..2738fe47b2 100644 --- a/sysdeps/x86/configure +++ b/sysdeps/x86/configure @@ -68,3 +68,104 @@ elif test $enable_cet = permissive; then fi config_vars="$config_vars enable-cet = $enable_cet" + +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -march=skylake-avx512 support" >&5 +$as_echo_n "checking for -march=skylake-avx512 support... " >&6; } +if ${libc_cv_cc_skylake_avx512+:} false; then : + $as_echo_n "(cached) " >&6 +else + if { ac_try='${CC-cc} -march=skylake-avx512 -xc /dev/null -S -o /dev/null' + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 + (eval $ac_try) 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; }; then : + libc_cv_cc_skylake_avx512=yes +else + libc_cv_cc_skylake_avx512=no +fi + +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_cc_skylake_avx512" >&5 +$as_echo "$libc_cv_cc_skylake_avx512" >&6; } +if test $libc_cv_cc_skylake_avx512 = yes; then + $as_echo "#define HAVE_SKYLAKE_AVX512_SUPPORT 1" >>confdefs.h + +fi +config_vars="$config_vars +config-cflags-skylake-avx512 = $libc_cv_cc_skylake_avx512" + +libc_cv_include_x86_isa_level=no +if test -z "`$LD --version | sed -n 's/^GNU \(gold\).*$/\1/p'`"; then + # Check for ld 2.32 or higher. + libc_cv_include_x86_isa_level=yes + for ac_prog in $LD +do + # Extract the first word of "$ac_prog", so it can be a program name with args. +set dummy $ac_prog; ac_word=$2 +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 +$as_echo_n "checking for $ac_word... " >&6; } +if ${ac_cv_prog_LD+:} false; then : + $as_echo_n "(cached) " >&6 +else + if test -n "$LD"; then + ac_cv_prog_LD="$LD" # Let the user override the test. +else +as_save_IFS=$IFS; IFS=$PATH_SEPARATOR +for as_dir in $PATH +do + IFS=$as_save_IFS + test -z "$as_dir" && as_dir=. + for ac_exec_ext in '' $ac_executable_extensions; do + if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then + ac_cv_prog_LD="$ac_prog" + $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 + break 2 + fi +done + done +IFS=$as_save_IFS + +fi +fi +LD=$ac_cv_prog_LD +if test -n "$LD"; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $LD" >&5 +$as_echo "$LD" >&6; } +else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } +fi + + + test -n "$LD" && break +done + +if test -z "$LD"; then + ac_verc_fail=yes +else + # Found it, now check the version. + { $as_echo "$as_me:${as_lineno-$LINENO}: checking version of $LD" >&5 +$as_echo_n "checking version of $LD... " >&6; } + ac_prog_version=`$LD --version 2>&1 | sed -n 's/^.*GNU ld.* \([0-9][0-9]*\.[0-9.]*\).*$/\1/p'` + case $ac_prog_version in + '') ac_prog_version="v. ?.??, bad"; ac_verc_fail=yes;; + 2.3[2-9]*|2.[4-9][0-9]*|[3-9].*|[1-9][0-9]*) + ac_prog_version="$ac_prog_version, ok"; ac_verc_fail=no;; + *) ac_prog_version="$ac_prog_version, bad"; ac_verc_fail=yes;; + + esac + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_prog_version" >&5 +$as_echo "$ac_prog_version" >&6; } +fi +if test $ac_verc_fail = yes; then + libc_cv_include_x86_isa_level=no +fi + + if test $libc_cv_include_x86_isa_level = yes; then + $as_echo "#define INCLUDE_X86_ISA_LEVEL 1" >>confdefs.h + + fi +fi +config_vars="$config_vars +enable-x86-isa-level = $libc_cv_include_x86_isa_level" diff --git a/sysdeps/x86/configure.ac b/sysdeps/x86/configure.ac index 8f3e1191f6..8f00b9663d 100644 --- a/sysdeps/x86/configure.ac +++ b/sysdeps/x86/configure.ac @@ -43,3 +43,29 @@ elif test $enable_cet = permissive; then AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_permissive) fi LIBC_CONFIG_VAR([enable-cet], [$enable_cet]) + +dnl Check if -march=skylake-avx512 works. +AC_CACHE_CHECK(for -march=skylake-avx512 support, + libc_cv_cc_skylake_avx512, [dnl +LIBC_TRY_CC_OPTION([-march=skylake-avx512], + [libc_cv_cc_skylake_avx512=yes], + [libc_cv_cc_skylake_avx512=no]) +]) +if test $libc_cv_cc_skylake_avx512 = yes; then + AC_DEFINE(HAVE_SKYLAKE_AVX512_SUPPORT) +fi +LIBC_CONFIG_VAR([config-cflags-skylake-avx512], [$libc_cv_cc_skylake_avx512]) + +libc_cv_include_x86_isa_level=no +if test -z "`$LD --version | sed -n 's/^GNU \(gold\).*$/\1/p'`"; then + # Check for ld 2.32 or higher. + libc_cv_include_x86_isa_level=yes + AC_CHECK_PROG_VER(LD, $LD, --version, + [GNU ld.* \([0-9][0-9]*\.[0-9.]*\)], + [2.3[2-9]*|2.[4-9][0-9]*|[3-9].*|[1-9][0-9]*], + libc_cv_include_x86_isa_level=no) + if test $libc_cv_include_x86_isa_level = yes; then + AC_DEFINE(INCLUDE_X86_ISA_LEVEL) + fi +fi +LIBC_CONFIG_VAR([enable-x86-isa-level], [$libc_cv_include_x86_isa_level]) diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c index fe080b63b2..ec5cdafe81 100644 --- a/sysdeps/x86/cpu-features.c +++ b/sysdeps/x86/cpu-features.c @@ -19,6 +19,7 @@ #include <cpuid.h> #include <dl-hwcap.h> #include <libc-pointer-arith.h> +#include <get-isa-level.h> #if IS_IN (libc) && !defined SHARED # include <assert.h> # include <unistd.h> @@ -290,6 +291,8 @@ update_usable (struct cpu_features *cpu_features) CPU_FEATURE_SET_USABLE (cpu_features, KL); CPU_FEATURE_SET_USABLE (cpu_features, WIDE_KL); } + + cpu_features->isa_1 = get_isa_level (cpu_features); } static void diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c index 5524b66038..8986c776e5 100644 --- a/sysdeps/x86/dl-cet.c +++ b/sysdeps/x86/dl-cet.c @@ -76,10 +76,12 @@ dl_cet_check (struct link_map *m, const char *program) */ enable_ibt &= (HAS_CPU_FEATURE (IBT) && (enable_ibt_type == cet_always_on - || (m->l_cet & lc_ibt) != 0)); + || (m->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0)); enable_shstk &= (HAS_CPU_FEATURE (SHSTK) && (enable_shstk_type == cet_always_on - || (m->l_cet & lc_shstk) != 0)); + || (m->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)); } /* ld.so is CET-enabled by kernel. But shared objects may not @@ -111,7 +113,8 @@ dl_cet_check (struct link_map *m, const char *program) /* IBT is enabled only if it is enabled in executable as well as all shared objects. */ enable_ibt &= (enable_ibt_type == cet_always_on - || (l->l_cet & lc_ibt) != 0); + || (l->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0); if (!found_ibt_legacy && enable_ibt != ibt_enabled) { found_ibt_legacy = true; @@ -121,7 +124,8 @@ dl_cet_check (struct link_map *m, const char *program) /* SHSTK is enabled only if it is enabled in executable as well as all shared objects. */ enable_shstk &= (enable_shstk_type == cet_always_on - || (l->l_cet & lc_shstk) != 0); + || (l->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0); if (enable_shstk != shstk_enabled) { found_shstk_legacy = true; diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h index 89911e19e2..61c0624c09 100644 --- a/sysdeps/x86/dl-prop.h +++ b/sysdeps/x86/dl-prop.h @@ -19,14 +19,54 @@ #ifndef _DL_PROP_H #define _DL_PROP_H +#include <libintl.h> + extern void _dl_cet_check (struct link_map *, const char *) attribute_hidden; extern void _dl_cet_open_check (struct link_map *) attribute_hidden; +static void +dl_isa_level_check (struct link_map *m, const char *program) +{ + const struct cpu_features *cpu_features = __get_cpu_features (); + unsigned int i; + struct link_map *l; + + i = m->l_searchlist.r_nlist; + while (i-- > 0) + { + /* Check each shared object to see if ISA level is compatible. */ + l = m->l_initfini[i]; + + /* Skip ISA level check if functions have been executed. */ + if (l->l_init_called) + continue; + +#ifdef SHARED + /* Skip ISA level check for ld.so since ld.so won't run if its ISA + level is higher than CPU. */ + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) + continue; +#endif + + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) + != l->l_x86_isa_1_needed) + { + if (program) + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", + *l->l_name != '\0' ? l->l_name : program); + else + _dl_signal_error (0, l->l_name, "dlopen", + N_("CPU ISA level is lower than required")); + } + } +} + static inline void __attribute__ ((always_inline)) _rtld_main_check (struct link_map *m, const char *program) { + dl_isa_level_check (m, program); #if CET_ENABLED _dl_cet_check (m, program); #endif @@ -35,20 +75,18 @@ _rtld_main_check (struct link_map *m, const char *program) static inline void __attribute__ ((always_inline)) _dl_open_check (struct link_map *m) { + dl_isa_level_check (m, NULL); #if CET_ENABLED _dl_cet_open_check (m); #endif } static inline void __attribute__ ((unused)) -_dl_process_cet_property_note (struct link_map *l, - const ElfW(Nhdr) *note, - const ElfW(Addr) size, - const ElfW(Addr) align) +_dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, + const ElfW(Addr) size, const ElfW(Addr) align) { -#if CET_ENABLED /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ - if (l->l_cet != lc_unknown) + if (l->l_property != lc_property_unknown) return; /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in @@ -59,7 +97,8 @@ _dl_process_cet_property_note (struct link_map *l, const ElfW(Addr) start = (ElfW(Addr)) note; - unsigned int feature_1 = 0; + unsigned int feature_1_and = 0; + unsigned int isa_1_needed = 0; unsigned int last_type = 0; while ((ElfW(Addr)) (note + 1) - start < size) @@ -71,11 +110,11 @@ _dl_process_cet_property_note (struct link_map *l, { /* Stop if we see more than one GNU property note which may be generated by the older linker. */ - if (l->l_cet != lc_unknown) + if (l->l_property != lc_property_unknown) return; - /* Check CET status now. */ - l->l_cet = lc_none; + /* Check CET status and ISA levels now. */ + l->l_property = lc_property_none; /* Check for invalid property. */ if (note->n_descsz < 8 @@ -101,26 +140,37 @@ _dl_process_cet_property_note (struct link_map *l, last_type = type; - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) + if (type == GNU_PROPERTY_X86_FEATURE_1_AND + || type == GNU_PROPERTY_X86_ISA_1_NEEDED) { - /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4 - bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND, - we stop the search regardless if its size is correct - or not. There is no point to continue if this note - is ill-formed. */ + /* The sizes of types which we are searching for are + 4 bytes. There is no point to continue if this + note is ill-formed. */ if (datasz != 4) return; - feature_1 = *(unsigned int *) ptr; - - /* Keep searching for the next GNU property note - generated by the older linker. */ - break; + /* NB: Stop the scan only after seeing all types which + we are searching for. */ + _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > + GNU_PROPERTY_X86_FEATURE_1_AND), + "GNU_PROPERTY_X86_ISA_1_NEEDED > " + "GNU_PROPERTY_X86_FEATURE_1_AND"); + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) + feature_1_and = *(unsigned int *) ptr; + else + { + isa_1_needed = *(unsigned int *) ptr; + + /* Keep searching for the next GNU property note + generated by the older linker. */ + break; + } } - else if (type > GNU_PROPERTY_X86_FEATURE_1_AND) + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) { - /* Stop since property type is in ascending order. */ - return; + /* Stop the scan since property type is in ascending + order. */ + break; } /* Check the next property item. */ @@ -137,18 +187,21 @@ _dl_process_cet_property_note (struct link_map *l, } /* We get here only if there is one or no GNU property note. */ - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT)) - l->l_cet |= lc_ibt; - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) - l->l_cet |= lc_shstk; -#endif + if (isa_1_needed || feature_1_and) + { + l->l_property = lc_property_valid; + l->l_x86_isa_1_needed = isa_1_needed; + l->l_x86_feature_1_and = feature_1_and; + } + else + l->l_property = lc_property_none; } static inline void __attribute__ ((unused)) _dl_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph) { const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); - _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align); + _dl_process_property_note (l, note, ph->p_memsz, ph->p_align); } static inline int __attribute__ ((always_inline)) diff --git a/sysdeps/x86/get-isa-level.h b/sysdeps/x86/get-isa-level.h new file mode 100644 index 0000000000..166dd5df01 --- /dev/null +++ b/sysdeps/x86/get-isa-level.h @@ -0,0 +1,66 @@ +/* Get x86 ISA level. + This file is part of the GNU C Library. + Copyright (C) 2020 Free Software Foundation, Inc. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <sys/platform/x86.h> + +/* Get GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] + ISA level. */ + +static unsigned int +get_isa_level (const struct cpu_features *cpu_features) +{ + unsigned isa_level = 0; + + if (CPU_FEATURE_USABLE_P (cpu_features, CMOV) + && CPU_FEATURE_USABLE_P (cpu_features, CX8) + && CPU_FEATURE_CPU_P (cpu_features, FPU) + && CPU_FEATURE_USABLE_P (cpu_features, FXSR) + && CPU_FEATURE_USABLE_P (cpu_features, MMX) + && CPU_FEATURE_USABLE_P (cpu_features, SSE) + && CPU_FEATURE_USABLE_P (cpu_features, SSE2)) + { + isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE; + if (CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) + && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) + && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) + && CPU_FEATURE_USABLE_P (cpu_features, SSE3) + && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2)) + { + isa_level |= GNU_PROPERTY_X86_ISA_1_V2; + if (CPU_FEATURE_USABLE_P (cpu_features, AVX) + && CPU_FEATURE_USABLE_P (cpu_features, AVX2) + && CPU_FEATURE_USABLE_P (cpu_features, F16C) + && CPU_FEATURE_USABLE_P (cpu_features, FMA) + && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) + && CPU_FEATURE_USABLE_P (cpu_features, MOVBE)) + { + isa_level |= GNU_PROPERTY_X86_ISA_1_V3; + if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)) + isa_level |= GNU_PROPERTY_X86_ISA_1_V4; + } + } + } + + return isa_level; +} diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h index f62be0b9b3..ef0c6b4fbc 100644 --- a/sysdeps/x86/include/cpu-features.h +++ b/sysdeps/x86/include/cpu-features.h @@ -129,6 +129,8 @@ struct cpu_features struct cpu_features_basic basic; struct cpuid_features features[COMMON_CPUID_INDEX_MAX]; unsigned int preferred[PREFERRED_FEATURE_INDEX_MAX]; + /* X86 micro-architecture ISA levels. */ + unsigned int isa_1; /* The state size for XSAVEC or XSAVE. The type must be unsigned long int so that we use diff --git a/sysdeps/x86/isa-level.c b/sysdeps/x86/isa-level.c new file mode 100644 index 0000000000..0751e86f27 --- /dev/null +++ b/sysdeps/x86/isa-level.c @@ -0,0 +1,95 @@ +/* ELF program property for x86 ISA level. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + In addition to the permissions in the GNU Lesser General Public + License, the Free Software Foundation gives you unlimited + permission to link the compiled version of this file with other + programs, and to distribute those programs without any restriction + coming from the use of this file. (The Lesser General Public + License restrictions do apply in other respects; for example, they + cover modification of the file, and distribution when not linked + into another program.) + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <elf.h> + +/* ELF program property for x86 ISA level. */ +#ifdef INCLUDE_X86_ISA_LEVEL +# if defined __x86_64__ || defined __FXSR__ || !defined _SOFT_FLOAT \ + || defined __MMX__ || defined __SSE__ || defined __SSE2__ +# define ISA_BASELINE GNU_PROPERTY_X86_ISA_1_BASELINE +# else +# define ISA_BASELINE 0 +# endif + +# if defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 \ + || (defined __x86_64__ && defined __LAHF_SAHF__) \ + || defined __POPCNT__ || defined __SSE3__ \ + || defined __SSSE3__ || defined __SSE4_1__ || defined __SSE4_2__ +# define ISA_V2 GNU_PROPERTY_X86_ISA_1_V2 +# else +# define ISA_V2 0 +# endif + +# if defined __AVX__ || defined __AVX2__ || defined __F16C__ \ + || defined __FMA__ || defined __LZCNT__ || defined __MOVBE__ \ + || defined __XSAVE__ +# define ISA_V3 GNU_PROPERTY_X86_ISA_1_V3 +# else +# define ISA_V3 0 +# endif + +# if defined __AVX512F__ || defined __AVX512BW__ || defined __AVX512CD__ \ + || defined __AVX512DQ__ || defined __AVX512VL__ +# define ISA_V4 GNU_PROPERTY_X86_ISA_1_V4 +# else +# define ISA_V4 0 +# endif + +# define ISA_LEVEL (ISA_BASELINE | ISA_V2 | ISA_V3 | ISA_V4) + +# if ISA_LEVEL +# ifdef __LP64__ +# define PROPERTY_ALIGN 3 +# else +# define PROPERTY_ALIGN 2 +# endif + +# define note_stringify(arg) note_stringify_1(arg) +# define note_stringify_1(arg) #arg + +asm(".pushsection \".note.gnu.property\",\"a\",@note\n" +" .p2align " note_stringify (PROPERTY_ALIGN) + /* name length. */ +"\n .long 1f - 0f\n" + /* data length. */ +" .long 4f - 1f\n" + /* note type: NT_GNU_PROPERTY_TYPE_0. */ +" .long " note_stringify (NT_GNU_PROPERTY_TYPE_0) + /* vendor name. */ +"\n0: .asciz \"GNU\"\n" +"1: .p2align " note_stringify (PROPERTY_ALIGN) + /* pr_type: GNU_PROPERTY_X86_ISA_1_NEEDED. */ +"\n .long " note_stringify (GNU_PROPERTY_X86_ISA_1_NEEDED) + /* pr_datasz. */ +"\n .long 3f - 2f\n" + /* GNU_PROPERTY_X86_ISA_1_V[234]. */ +"2:\n .long " note_stringify (ISA_LEVEL) +"\n3:\n .p2align " note_stringify (PROPERTY_ALIGN) +"\n4:\n .popsection"); +# endif /* ISA_LEVEL */ +#endif /* INCLUDE_X86_ISA_LEVEL */ diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h index 2e4f8850f9..3c550e529a 100644 --- a/sysdeps/x86/link_map.h +++ b/sysdeps/x86/link_map.h @@ -16,12 +16,16 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ -/* If this object is enabled with CET. */ +/* if this object has GNU property. */ enum { - lc_unknown = 0, /* Unknown CET status. */ - lc_none = 1 << 0, /* Not enabled with CET. */ - lc_ibt = 1 << 1, /* Enabled with IBT. */ - lc_shstk = 1 << 2, /* Enabled with STSHK. */ - lc_ibt_and_shstk = lc_ibt | lc_shstk /* Enabled with both. */ - } l_cet:3; + lc_property_unknown = 0, /* Unknown property status. */ + lc_property_none = 1 << 0, /* No property. */ + lc_property_valid = 1 << 1 /* Has valid property. */ + } l_property:2; + +/* GNU_PROPERTY_X86_FEATURE_1_AND of this object. */ +unsigned int l_x86_feature_1_and; + +/* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */ +unsigned int l_x86_isa_1_needed; diff --git a/sysdeps/x86/tst-isa-level-1.c b/sysdeps/x86/tst-isa-level-1.c new file mode 100644 index 0000000000..c263157c8c --- /dev/null +++ b/sysdeps/x86/tst-isa-level-1.c @@ -0,0 +1,74 @@ +/* Check ISA level on dlopened shared object. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <elf.h> +#include <get-isa-level.h> +#include <support/xdlfcn.h> +#include <support/check.h> +#include <support/test-driver.h> + +static void +do_test_1 (const char *modname, bool fail) +{ + int (*fp) (void); + void *h; + + h = dlopen (modname, RTLD_LAZY); + if (h == NULL) + { + const char *err = dlerror (); + if (fail) + { + if (strstr (err, "CPU ISA level is lower than required") == NULL) + FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err); + + return; + } + + FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err); + } + + if (fail) + FAIL_EXIT1 ("dlopen '%s' should have failed\n", modname); + + fp = xdlsym (h, "test"); + + if (fp () != 0) + FAIL_EXIT1 ("test () != 0\n"); + + dlclose (h); +} + +static int +do_test (void) +{ + const struct cpu_features *cpu_features + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); + unsigned int isa_level = get_isa_level (cpu_features); + /* Skip on x86-64-v4 platforms since dlopen always works. */ + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) + return EXIT_UNSUPPORTED; + do_test_1 ("tst-isa-level-mod-1a.so", true); + do_test_1 ("tst-isa-level-mod-1b.so", false); + return EXIT_SUCCESS; +} + +#include <support/test-driver.c> diff --git a/sysdeps/x86/tst-isa-level-mod-1.c b/sysdeps/x86/tst-isa-level-mod-1.c new file mode 100644 index 0000000000..3816536428 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1.c @@ -0,0 +1,23 @@ +/* Check ISA level on dlopened shared object. + Copyright (C) 2019-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +int +test (void) +{ + return 0; +} diff --git a/sysdeps/x86/tst-isa-level-mod-1a.c b/sysdeps/x86/tst-isa-level-mod-1a.c new file mode 100644 index 0000000000..4f026dd915 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1a.c @@ -0,0 +1,2 @@ +#include <isa-level.c> +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1b.c b/sysdeps/x86/tst-isa-level-mod-1b.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1b.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c index 8810a822ef..e5640303f9 100644 --- a/sysdeps/x86_64/dl-hwcaps-subdirs.c +++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c @@ -18,6 +18,7 @@ #include <dl-hwcaps.h> #include <cpu-features.h> +#include <get-isa-level.h> const char _dl_hwcaps_subdirs[] = "x86-64-v4:x86-64-v3:x86-64-v2"; enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ @@ -25,40 +26,25 @@ enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ uint32_t _dl_hwcaps_subdirs_active (void) { + const struct cpu_features *cpu_features + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); + unsigned int isa_level = get_isa_level (cpu_features); int active = 0; /* Test in reverse preference order. */ /* x86-64-v2. */ - if (!(CPU_FEATURE_USABLE (CMPXCHG16B) - && CPU_FEATURE_USABLE (LAHF64_SAHF64) - && CPU_FEATURE_USABLE (POPCNT) - && CPU_FEATURE_USABLE (SSE3) - && CPU_FEATURE_USABLE (SSE4_1) - && CPU_FEATURE_USABLE (SSE4_2) - && CPU_FEATURE_USABLE (SSSE3))) + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; /* x86-64-v3. */ - if (!(CPU_FEATURE_USABLE (AVX) - && CPU_FEATURE_USABLE (AVX2) - && CPU_FEATURE_USABLE (BMI1) - && CPU_FEATURE_USABLE (BMI2) - && CPU_FEATURE_USABLE (F16C) - && CPU_FEATURE_USABLE (FMA) - && CPU_FEATURE_USABLE (LZCNT) - && CPU_FEATURE_USABLE (MOVBE) - && CPU_FEATURE_USABLE (OSXSAVE))) + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; - /* x86-64-v4. */ - if (!(CPU_FEATURE_USABLE (AVX512F) - && CPU_FEATURE_USABLE (AVX512BW) - && CPU_FEATURE_USABLE (AVX512CD) - && CPU_FEATURE_USABLE (AVX512DQ) - && CPU_FEATURE_USABLE (AVX512VL))) + /* x86-64-v4. */ + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; diff --git a/sysdeps/x86_64/tst-glibc-hwcaps.c b/sysdeps/x86_64/tst-glibc-hwcaps.c index 3075a8286d..a041a9047d 100644 --- a/sysdeps/x86_64/tst-glibc-hwcaps.c +++ b/sysdeps/x86_64/tst-glibc-hwcaps.c @@ -19,7 +19,8 @@ #include <stdio.h> #include <support/check.h> #include <sys/param.h> -#include <sys/platform/x86.h> +#include <elf.h> +#include <get-isa-level.h> extern int marker2 (void); extern int marker3 (void); @@ -31,35 +32,15 @@ compute_level (void) { const struct cpu_features *cpu_features = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); - - if (!(CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) - && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) - && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) - && CPU_FEATURE_USABLE_P (cpu_features, MMX) - && CPU_FEATURE_USABLE_P (cpu_features, SSE) - && CPU_FEATURE_USABLE_P (cpu_features, SSE2) - && CPU_FEATURE_USABLE_P (cpu_features, SSE3) - && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2))) - return 1; - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX) - && CPU_FEATURE_USABLE_P (cpu_features, AVX2) - && CPU_FEATURE_USABLE_P (cpu_features, BMI1) - && CPU_FEATURE_USABLE_P (cpu_features, BMI2) - && CPU_FEATURE_USABLE_P (cpu_features, F16C) - && CPU_FEATURE_USABLE_P (cpu_features, FMA) - && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) - && CPU_FEATURE_USABLE_P (cpu_features, MOVBE) - && CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE))) - return 2; - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX512F) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL))) - return 3; - return 4; + unsigned int isa_level = get_isa_level (cpu_features); + + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) + return 1; + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) + return 2; + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) + return 3; + return 4; } static int -- 2.28.0 ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] 2020-12-06 14:49 ` V5 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] " H.J. Lu @ 2021-01-06 12:00 ` Adhemerval Zanella 2021-01-06 17:11 ` V6 " H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Adhemerval Zanella @ 2021-01-06 12:00 UTC (permalink / raw) To: libc-alpha, H.J. Lu, Florian Weimer On 06/12/2020 11:49, H.J. Lu via Libc-alpha wrote: > GCC 11 supports -march=x86-64-v[234] to enable x86 micro-architecture ISA > levels: > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97250 > > and -mneeded to emit GNU_PROPERTY_X86_ISA_1_NEEDED property with > GNU_PROPERTY_X86_ISA_1_V[234] marker: > > https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/13 > > Binutils support for GNU_PROPERTY_X86_ISA_1_V[234] marker were added by > > commit b0ab06937385e0ae25cebf1991787d64f439bf12 > Author: H.J. Lu <hjl.tools@gmail.com> > Date: Fri Oct 30 06:49:57 2020 -0700 > > x86: Support GNU_PROPERTY_X86_ISA_1_BASELINE marker > > and > > commit 32930e4edbc06bc6f10c435dbcc63131715df678 > Author: H.J. Lu <hjl.tools@gmail.com> > Date: Fri Oct 9 05:05:57 2020 -0700 > > x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker > > GNU_PROPERTY_X86_ISA_1_NEEDED property in x86 ELF binaries indicate the > micro-architecture ISA level required to execute the binary. The marker > must be added by programmers explicitly in one of 3 ways: > > 1. Pass -mneeded to GCC. > 2. Add the marker in the linker inputs as this patch does. > 3. Pass -z x86-64-v[234] to the linker. > > Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > marker support to ld.so if binutils 2.32 or newer is used to build glibc: > > 1. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > markers to elf.h. > 2. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > marker to abi-note.o based on the ISA level used to compile abi-note.o, > assuming that the same ISA level is used to compile the whole glibc. > 3. Add isa_1 to cpu_features to record the supported x86 ISA level. > 4. Rename _dl_process_cet_property_note to _dl_process_property_note and > add GNU_PROPERTY_X86_ISA_1_V[234] marker detection. > 5. Update _rtld_main_check and _dl_open_check to check loaded objects > with the incompatible ISA level. > 6. Add a testcase to verify that dlopen an x86-64-v4 shared object fails > on lesser platforms. > 7. Use <get-isa-level.h> in dl-hwcaps-subdirs.c and tst-glibc-hwcaps.c. > > Marked elf/tst-isa-level-1 with x86-64-v4, ran it on x86-64-v3 machine > and got: > > [hjl@gnu-cfl-2 build-x86_64-linux]$ ./elf/tst-isa-level-1 > ./elf/tst-isa-level-1: CPU ISA level is lower than required > [hjl@gnu-cfl-2 build-x86_64-linux]$ > --- > config.h.in | 3 + > elf/elf.h | 35 ++++----- > sysdeps/x86/Makefile | 10 +++ > sysdeps/x86/abi-note.c | 29 ++++++++ > sysdeps/x86/configure | 101 ++++++++++++++++++++++++++ > sysdeps/x86/configure.ac | 26 +++++++ > sysdeps/x86/cpu-features.c | 3 + > sysdeps/x86/dl-cet.c | 12 ++- > sysdeps/x86/dl-prop.h | 113 +++++++++++++++++++++-------- > sysdeps/x86/get-isa-level.h | 66 +++++++++++++++++ > sysdeps/x86/include/cpu-features.h | 2 + > sysdeps/x86/isa-level.c | 95 ++++++++++++++++++++++++ > sysdeps/x86/link_map.h | 18 +++-- > sysdeps/x86/tst-isa-level-1.c | 74 +++++++++++++++++++ > sysdeps/x86/tst-isa-level-mod-1.c | 23 ++++++ > sysdeps/x86/tst-isa-level-mod-1a.c | 2 + > sysdeps/x86/tst-isa-level-mod-1b.c | 1 + > sysdeps/x86_64/dl-hwcaps-subdirs.c | 30 ++------ > sysdeps/x86_64/tst-glibc-hwcaps.c | 41 +++-------- > 19 files changed, 571 insertions(+), 113 deletions(-) > create mode 100644 sysdeps/x86/abi-note.c > create mode 100644 sysdeps/x86/get-isa-level.h > create mode 100644 sysdeps/x86/isa-level.c > create mode 100644 sysdeps/x86/tst-isa-level-1.c > create mode 100644 sysdeps/x86/tst-isa-level-mod-1.c > create mode 100644 sysdeps/x86/tst-isa-level-mod-1a.c > create mode 100644 sysdeps/x86/tst-isa-level-mod-1b.c > > diff --git a/config.h.in b/config.h.in > index b823c8e080..dc3854f4eb 100644 > --- a/config.h.in > +++ b/config.h.in > @@ -266,4 +266,7 @@ > /* The default value of x86 CET control. */ > #define DEFAULT_DL_X86_CET_CONTROL cet_elf_property > > +/* Define if x86 ISA level should be included in shared libraries. */ > +#undef INCLUDE_X86_ISA_LEVEL > + > #endif > diff --git a/elf/elf.h b/elf/elf.h > index bd7af7cca4..42fd697129 100644 > --- a/elf/elf.h > +++ b/elf/elf.h > @@ -1325,31 +1325,26 @@ typedef struct > > /* The x86 instruction sets indicated by the corresponding bits are > used in program. Their support in the hardware is optional. */ > -#define GNU_PROPERTY_X86_ISA_1_USED 0xc0000000 > +#define GNU_PROPERTY_X86_ISA_1_USED 0xc0010002 > /* The x86 instruction sets indicated by the corresponding bits are > used in program and they must be supported by the hardware. */ > -#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0000001 > +#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0008002 > /* X86 processor-specific features used in program. */ > #define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 > > -#define GNU_PROPERTY_X86_ISA_1_486 (1U << 0) > -#define GNU_PROPERTY_X86_ISA_1_586 (1U << 1) > -#define GNU_PROPERTY_X86_ISA_1_686 (1U << 2) > -#define GNU_PROPERTY_X86_ISA_1_SSE (1U << 3) > -#define GNU_PROPERTY_X86_ISA_1_SSE2 (1U << 4) > -#define GNU_PROPERTY_X86_ISA_1_SSE3 (1U << 5) > -#define GNU_PROPERTY_X86_ISA_1_SSSE3 (1U << 6) > -#define GNU_PROPERTY_X86_ISA_1_SSE4_1 (1U << 7) > -#define GNU_PROPERTY_X86_ISA_1_SSE4_2 (1U << 8) > -#define GNU_PROPERTY_X86_ISA_1_AVX (1U << 9) > -#define GNU_PROPERTY_X86_ISA_1_AVX2 (1U << 10) > -#define GNU_PROPERTY_X86_ISA_1_AVX512F (1U << 11) > -#define GNU_PROPERTY_X86_ISA_1_AVX512CD (1U << 12) > -#define GNU_PROPERTY_X86_ISA_1_AVX512ER (1U << 13) > -#define GNU_PROPERTY_X86_ISA_1_AVX512PF (1U << 14) > -#define GNU_PROPERTY_X86_ISA_1_AVX512VL (1U << 15) > -#define GNU_PROPERTY_X86_ISA_1_AVX512DQ (1U << 16) > -#define GNU_PROPERTY_X86_ISA_1_AVX512BW (1U << 17) > +/* GNU_PROPERTY_X86_ISA_1_BASELINE: CMOV, CX8 (cmpxchg8b), FPU (fld), > + MMX, OSFXSR (fxsave), SCE (syscall), SSE and SSE2. */ > +#define GNU_PROPERTY_X86_ISA_1_BASELINE (1U << 0) > +/* GNU_PROPERTY_X86_ISA_1_V2: GNU_PROPERTY_X86_ISA_1_BASELINE, > + CMPXCHG16B (cmpxchg16b), LAHF-SAHF (lahf), POPCNT (popcnt), SSE3, > + SSSE3, SSE4.1 and SSE4.2. */ > +#define GNU_PROPERTY_X86_ISA_1_V2 (1U << 1) > +/* GNU_PROPERTY_X86_ISA_1_V3: GNU_PROPERTY_X86_ISA_1_V2, AVX, AVX2, BMI1, > + BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE. */ > +#define GNU_PROPERTY_X86_ISA_1_V3 (1U << 2) > +/* GNU_PROPERTY_X86_ISA_1_V4: GNU_PROPERTY_X86_ISA_1_V3, AVX512F, > + AVX512BW, AVX512CD, AVX512DQ and AVX512VL. */ > +#define GNU_PROPERTY_X86_ISA_1_V4 (1U << 3) > > /* This indicates that all executable sections are compatible with > IBT. */ Shouldn't it follow bintuils 32930e4edbc06 change and move current GNU_PROPERTY_X86_ISA_1_ definitions to GNU_PROPERTY_X86_COMPAT_2_? Not sure if this charecterize any exported ABI change, since the most possible usual consumer (bintuils) does not use glibc version. Also, maybe move this change to an specific patch. > diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile > index 081cc72e93..109f03e9b9 100644 > --- a/sysdeps/x86/Makefile > +++ b/sysdeps/x86/Makefile > @@ -9,6 +9,16 @@ sysdep_headers += sys/platform/x86.h > tests += tst-get-cpu-features tst-get-cpu-features-static \ > tst-cpu-features-cpuinfo tst-cpu-features-supports > tests-static += tst-get-cpu-features-static > +ifeq (yesyes,$(enable-x86-isa-level)$(config-cflags-skylake-avx512)) > +tests += tst-isa-level-1 > +modules-names += tst-isa-level-mod-1a tst-isa-level-mod-1b > + > +CFLAGS-tst-isa-level-mod-1a.c += -march=skylake-avx512 \ > + -DINCLUDE_X86_ISA_LEVEL > +$(objpfx)tst-isa-level-1: $(libdl) > +$(objpfx)tst-isa-level-1.out: $(objpfx)tst-isa-level-mod-1a.so \ > + $(objpfx)tst-isa-level-mod-1b.so > +endif > endif > > ifeq ($(subdir),math) This will limit the test to a very narrow set of machines, couldn't we check only for enable-x86-isa-level and make the conditional check for ISA inside the test itself? What I have in mind would be something like: 1. Create onde module object for each ISA variant, by varying either -march or using the specific gcc option (not sure it would clash with the ABI notes isa-level.c already include). 2. On tst-isa-level-1.c check current ISA level with get_isa_level and check against the ISA_BASELINE and load the modules with a ISA number lower than baseline. Something like: const struct cpu_features *cpu_features = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); unsigned int isa_level = get_isa_level (cpu_features); bool check_isa_baseline = (isa_level & ISA_BASELINE) == ISA_BASELINE; bool check_isa_v2 = (isa_level & ISA_V2) == ISA_V2; bool check_isa_v3 = (isa_level & ISA_V3) == ISA_V3; bool check_isa_v4 = (isa_level & ISA_V4) == ISA_V4; do_test_1 ("tst-isa-level-mod-1-baseline.so", true); do_test_1 ("tst-isa-level-mod-1-isa_v2.so", true); do_test_1 ("tst-isa-level-mod-1-isa_v3.so", true); do_test_1 ("tst-isa-level-mod-1-isa_v4.so", true); You might require to just build the isa_v4 only for config-cflags-skylake-avx512 and conditionalize with HAVE_SKYLAKE_AVX512_SUPPORT (which seems to be ununsed in this patch). > diff --git a/sysdeps/x86/abi-note.c b/sysdeps/x86/abi-note.c > new file mode 100644 > index 0000000000..5140e6ab47 > --- /dev/null > +++ b/sysdeps/x86/abi-note.c > @@ -0,0 +1,29 @@ > +/* Special .init and .fini section support. x86-64 version. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + In addition to the permissions in the GNU Lesser General Public > + License, the Free Software Foundation gives you unlimited > + permission to link the compiled version of this file with other > + programs, and to distribute those programs without any restriction > + coming from the use of this file. (The Lesser General Public > + License restrictions do apply in other respects; for example, they > + cover modification of the file, and distribution when not linked > + into another program.) > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <isa-level.c> > +#include <csu/abi-note.c> Ok, > diff --git a/sysdeps/x86/configure b/sysdeps/x86/configure > index 81cc4e80d6..2738fe47b2 100644 > --- a/sysdeps/x86/configure > +++ b/sysdeps/x86/configure > @@ -68,3 +68,104 @@ elif test $enable_cet = permissive; then > fi > config_vars="$config_vars > enable-cet = $enable_cet" > + > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -march=skylake-avx512 support" >&5 > +$as_echo_n "checking for -march=skylake-avx512 support... " >&6; } > +if ${libc_cv_cc_skylake_avx512+:} false; then : > + $as_echo_n "(cached) " >&6 > +else > + if { ac_try='${CC-cc} -march=skylake-avx512 -xc /dev/null -S -o /dev/null' > + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 > + (eval $ac_try) 2>&5 > + ac_status=$? > + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 > + test $ac_status = 0; }; }; then : > + libc_cv_cc_skylake_avx512=yes > +else > + libc_cv_cc_skylake_avx512=no > +fi > + > +fi > +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_cc_skylake_avx512" >&5 > +$as_echo "$libc_cv_cc_skylake_avx512" >&6; } > +if test $libc_cv_cc_skylake_avx512 = yes; then > + $as_echo "#define HAVE_SKYLAKE_AVX512_SUPPORT 1" >>confdefs.h > + > +fi > +config_vars="$config_vars > +config-cflags-skylake-avx512 = $libc_cv_cc_skylake_avx512" > + > +libc_cv_include_x86_isa_level=no > +if test -z "`$LD --version | sed -n 's/^GNU \(gold\).*$/\1/p'`"; then > + # Check for ld 2.32 or higher. > + libc_cv_include_x86_isa_level=yes > + for ac_prog in $LD > +do > + # Extract the first word of "$ac_prog", so it can be a program name with args. > +set dummy $ac_prog; ac_word=$2 > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 > +$as_echo_n "checking for $ac_word... " >&6; } > +if ${ac_cv_prog_LD+:} false; then : > + $as_echo_n "(cached) " >&6 > +else > + if test -n "$LD"; then > + ac_cv_prog_LD="$LD" # Let the user override the test. > +else > +as_save_IFS=$IFS; IFS=$PATH_SEPARATOR > +for as_dir in $PATH > +do > + IFS=$as_save_IFS > + test -z "$as_dir" && as_dir=. > + for ac_exec_ext in '' $ac_executable_extensions; do > + if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then > + ac_cv_prog_LD="$ac_prog" > + $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 > + break 2 > + fi > +done > + done > +IFS=$as_save_IFS > + > +fi > +fi > +LD=$ac_cv_prog_LD > +if test -n "$LD"; then > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $LD" >&5 > +$as_echo "$LD" >&6; } > +else > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 > +$as_echo "no" >&6; } > +fi > + > + > + test -n "$LD" && break > +done > + > +if test -z "$LD"; then > + ac_verc_fail=yes > +else > + # Found it, now check the version. > + { $as_echo "$as_me:${as_lineno-$LINENO}: checking version of $LD" >&5 > +$as_echo_n "checking version of $LD... " >&6; } > + ac_prog_version=`$LD --version 2>&1 | sed -n 's/^.*GNU ld.* \([0-9][0-9]*\.[0-9.]*\).*$/\1/p'` > + case $ac_prog_version in > + '') ac_prog_version="v. ?.??, bad"; ac_verc_fail=yes;; > + 2.3[2-9]*|2.[4-9][0-9]*|[3-9].*|[1-9][0-9]*) > + ac_prog_version="$ac_prog_version, ok"; ac_verc_fail=no;; > + *) ac_prog_version="$ac_prog_version, bad"; ac_verc_fail=yes;; > + > + esac > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_prog_version" >&5 > +$as_echo "$ac_prog_version" >&6; } > +fi > +if test $ac_verc_fail = yes; then > + libc_cv_include_x86_isa_level=no > +fi > + > + if test $libc_cv_include_x86_isa_level = yes; then > + $as_echo "#define INCLUDE_X86_ISA_LEVEL 1" >>confdefs.h > + > + fi > +fi > +config_vars="$config_vars > +enable-x86-isa-level = $libc_cv_include_x86_isa_level" > diff --git a/sysdeps/x86/configure.ac b/sysdeps/x86/configure.ac > index 8f3e1191f6..8f00b9663d 100644 > --- a/sysdeps/x86/configure.ac > +++ b/sysdeps/x86/configure.ac > @@ -43,3 +43,29 @@ elif test $enable_cet = permissive; then > AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_permissive) > fi > LIBC_CONFIG_VAR([enable-cet], [$enable_cet]) > + > +dnl Check if -march=skylake-avx512 works. > +AC_CACHE_CHECK(for -march=skylake-avx512 support, > + libc_cv_cc_skylake_avx512, [dnl > +LIBC_TRY_CC_OPTION([-march=skylake-avx512], > + [libc_cv_cc_skylake_avx512=yes], > + [libc_cv_cc_skylake_avx512=no]) > +]) > +if test $libc_cv_cc_skylake_avx512 = yes; then > + AC_DEFINE(HAVE_SKYLAKE_AVX512_SUPPORT) > +fi Where is it used specifically? There is no config.h.in tag either. > +LIBC_CONFIG_VAR([config-cflags-skylake-avx512], [$libc_cv_cc_skylake_avx512]) > + > +libc_cv_include_x86_isa_level=no > +if test -z "`$LD --version | sed -n 's/^GNU \(gold\).*$/\1/p'`"; then > + # Check for ld 2.32 or higher. > + libc_cv_include_x86_isa_level=yes > + AC_CHECK_PROG_VER(LD, $LD, --version, > + [GNU ld.* \([0-9][0-9]*\.[0-9.]*\)], > + [2.3[2-9]*|2.[4-9][0-9]*|[3-9].*|[1-9][0-9]*], > + libc_cv_include_x86_isa_level=no) > + if test $libc_cv_include_x86_isa_level = yes; then > + AC_DEFINE(INCLUDE_X86_ISA_LEVEL) > + fi > +fi > +LIBC_CONFIG_VAR([enable-x86-isa-level], [$libc_cv_include_x86_isa_level]) Instead of version check, would it better to check if the linker actually build a snippet with the expected notes? > diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c > index fe080b63b2..ec5cdafe81 100644 > --- a/sysdeps/x86/cpu-features.c > +++ b/sysdeps/x86/cpu-features.c > @@ -19,6 +19,7 @@ > #include <cpuid.h> > #include <dl-hwcap.h> > #include <libc-pointer-arith.h> > +#include <get-isa-level.h> > #if IS_IN (libc) && !defined SHARED > # include <assert.h> > # include <unistd.h> > @@ -290,6 +291,8 @@ update_usable (struct cpu_features *cpu_features) > CPU_FEATURE_SET_USABLE (cpu_features, KL); > CPU_FEATURE_SET_USABLE (cpu_features, WIDE_KL); > } > + > + cpu_features->isa_1 = get_isa_level (cpu_features); > } > > static void Ok. > diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c > index 5524b66038..8986c776e5 100644 > --- a/sysdeps/x86/dl-cet.c > +++ b/sysdeps/x86/dl-cet.c > @@ -76,10 +76,12 @@ dl_cet_check (struct link_map *m, const char *program) > */ > enable_ibt &= (HAS_CPU_FEATURE (IBT) > && (enable_ibt_type == cet_always_on > - || (m->l_cet & lc_ibt) != 0)); > + || (m->l_x86_feature_1_and > + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0)); > enable_shstk &= (HAS_CPU_FEATURE (SHSTK) > && (enable_shstk_type == cet_always_on > - || (m->l_cet & lc_shstk) != 0)); > + || (m->l_x86_feature_1_and > + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)); > } > Ok. > /* ld.so is CET-enabled by kernel. But shared objects may not > @@ -111,7 +113,8 @@ dl_cet_check (struct link_map *m, const char *program) > /* IBT is enabled only if it is enabled in executable as > well as all shared objects. */ > enable_ibt &= (enable_ibt_type == cet_always_on > - || (l->l_cet & lc_ibt) != 0); > + || (l->l_x86_feature_1_and > + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0); > if (!found_ibt_legacy && enable_ibt != ibt_enabled) > { > found_ibt_legacy = true; > @@ -121,7 +124,8 @@ dl_cet_check (struct link_map *m, const char *program) > /* SHSTK is enabled only if it is enabled in executable as > well as all shared objects. */ > enable_shstk &= (enable_shstk_type == cet_always_on > - || (l->l_cet & lc_shstk) != 0); > + || (l->l_x86_feature_1_and > + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0); > if (enable_shstk != shstk_enabled) > { > found_shstk_legacy = true; Ok. > diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h > index 89911e19e2..61c0624c09 100644 > --- a/sysdeps/x86/dl-prop.h > +++ b/sysdeps/x86/dl-prop.h > @@ -19,14 +19,54 @@ > #ifndef _DL_PROP_H > #define _DL_PROP_H > > +#include <libintl.h> > + > extern void _dl_cet_check (struct link_map *, const char *) > attribute_hidden; > extern void _dl_cet_open_check (struct link_map *) > attribute_hidden; > > +static void > +dl_isa_level_check (struct link_map *m, const char *program) > +{ > + const struct cpu_features *cpu_features = __get_cpu_features (); > + unsigned int i; > + struct link_map *l; > + > + i = m->l_searchlist.r_nlist; > + while (i-- > 0) > + { > + /* Check each shared object to see if ISA level is compatible. */ > + l = m->l_initfini[i]; > + > + /* Skip ISA level check if functions have been executed. */ > + if (l->l_init_called) > + continue; > + > +#ifdef SHARED > + /* Skip ISA level check for ld.so since ld.so won't run if its ISA > + level is higher than CPU. */ > + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) > + continue; > +#endif > + > + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) > + != l->l_x86_isa_1_needed) > + { > + if (program) > + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", > + *l->l_name != '\0' ? l->l_name : program); > + else > + _dl_signal_error (0, l->l_name, "dlopen", > + N_("CPU ISA level is lower than required")); > + } > + } > +} > + Ok. Not sure if it is worth to translate the _dl_fatal_printf (it seems that at least one use at elf/dl-open.c does it). > static inline void __attribute__ ((always_inline)) > _rtld_main_check (struct link_map *m, const char *program) > { > + dl_isa_level_check (m, program); > #if CET_ENABLED > _dl_cet_check (m, program); > #endif > @@ -35,20 +75,18 @@ _rtld_main_check (struct link_map *m, const char *program) > static inline void __attribute__ ((always_inline)) > _dl_open_check (struct link_map *m) > { > + dl_isa_level_check (m, NULL); > #if CET_ENABLED > _dl_cet_open_check (m); > #endif > } > Ok. > static inline void __attribute__ ((unused)) > -_dl_process_cet_property_note (struct link_map *l, > - const ElfW(Nhdr) *note, > - const ElfW(Addr) size, > - const ElfW(Addr) align) > +_dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, > + const ElfW(Addr) size, const ElfW(Addr) align) > { > -#if CET_ENABLED > /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ > - if (l->l_cet != lc_unknown) > + if (l->l_property != lc_property_unknown) > return; > > /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in Ok. > @@ -59,7 +97,8 @@ _dl_process_cet_property_note (struct link_map *l, > > const ElfW(Addr) start = (ElfW(Addr)) note; > > - unsigned int feature_1 = 0; > + unsigned int feature_1_and = 0; > + unsigned int isa_1_needed = 0; > unsigned int last_type = 0; > > while ((ElfW(Addr)) (note + 1) - start < size) > @@ -71,11 +110,11 @@ _dl_process_cet_property_note (struct link_map *l, > { > /* Stop if we see more than one GNU property note which may > be generated by the older linker. */ > - if (l->l_cet != lc_unknown) > + if (l->l_property != lc_property_unknown) > return; > > - /* Check CET status now. */ > - l->l_cet = lc_none; > + /* Check CET status and ISA levels now. */ > + l->l_property = lc_property_none; > > /* Check for invalid property. */ > if (note->n_descsz < 8 Ok. > @@ -101,26 +140,37 @@ _dl_process_cet_property_note (struct link_map *l, > > last_type = type; > > - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND > + || type == GNU_PROPERTY_X86_ISA_1_NEEDED) > { > - /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4 > - bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND, > - we stop the search regardless if its size is correct > - or not. There is no point to continue if this note > - is ill-formed. */ > + /* The sizes of types which we are searching for are > + 4 bytes. There is no point to continue if this > + note is ill-formed. */ > if (datasz != 4) > return; > Ok. > - feature_1 = *(unsigned int *) ptr; > - > - /* Keep searching for the next GNU property note > - generated by the older linker. */ > - break; > + /* NB: Stop the scan only after seeing all types which > + we are searching for. */ > + _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > > + GNU_PROPERTY_X86_FEATURE_1_AND), > + "GNU_PROPERTY_X86_ISA_1_NEEDED > " > + "GNU_PROPERTY_X86_FEATURE_1_AND"); Ok. > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) > + feature_1_and = *(unsigned int *) ptr; > + else > + { > + isa_1_needed = *(unsigned int *) ptr; > + > + /* Keep searching for the next GNU property note > + generated by the older linker. */ > + break; > + } > } > - else if (type > GNU_PROPERTY_X86_FEATURE_1_AND) > + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) > { > - /* Stop since property type is in ascending order. */ > - return; > + /* Stop the scan since property type is in ascending > + order. */ > + break; > } > > /* Check the next property item. */ Ok. > @@ -137,18 +187,21 @@ _dl_process_cet_property_note (struct link_map *l, > } > > /* We get here only if there is one or no GNU property note. */ > - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT)) > - l->l_cet |= lc_ibt; > - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) > - l->l_cet |= lc_shstk; > -#endif > + if (isa_1_needed || feature_1_and) No implicit checks. > + { > + l->l_property = lc_property_valid; > + l->l_x86_isa_1_needed = isa_1_needed; > + l->l_x86_feature_1_and = feature_1_and; > + } > + else > + l->l_property = lc_property_none; > } > > static inline void __attribute__ ((unused)) > _dl_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph) > { > const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); > - _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align); > + _dl_process_property_note (l, note, ph->p_memsz, ph->p_align); > } > > static inline int __attribute__ ((always_inline)) Ok. > diff --git a/sysdeps/x86/get-isa-level.h b/sysdeps/x86/get-isa-level.h > new file mode 100644 > index 0000000000..166dd5df01 > --- /dev/null > +++ b/sysdeps/x86/get-isa-level.h > @@ -0,0 +1,66 @@ > +/* Get x86 ISA level. > + This file is part of the GNU C Library. > + Copyright (C) 2020 Free Software Foundation, Inc. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <sys/platform/x86.h> > + > +/* Get GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > + ISA level. */ > + > +static unsigned int > +get_isa_level (const struct cpu_features *cpu_features) > +{ > + unsigned isa_level = 0; s/unsigned/unsigned int/ > + > + if (CPU_FEATURE_USABLE_P (cpu_features, CMOV) > + && CPU_FEATURE_USABLE_P (cpu_features, CX8) > + && CPU_FEATURE_CPU_P (cpu_features, FPU) > + && CPU_FEATURE_USABLE_P (cpu_features, FXSR) > + && CPU_FEATURE_USABLE_P (cpu_features, MMX) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE2)) > + { > + isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE; > + if (CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) > + && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) > + && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE3) > + && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2)) > + { > + isa_level |= GNU_PROPERTY_X86_ISA_1_V2; > + if (CPU_FEATURE_USABLE_P (cpu_features, AVX) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX2) > + && CPU_FEATURE_USABLE_P (cpu_features, F16C) > + && CPU_FEATURE_USABLE_P (cpu_features, FMA) > + && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) > + && CPU_FEATURE_USABLE_P (cpu_features, MOVBE)) > + { > + isa_level |= GNU_PROPERTY_X86_ISA_1_V3; > + if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)) > + isa_level |= GNU_PROPERTY_X86_ISA_1_V4; > + } > + } > + } > + > + return isa_level; > +} Do we actually support a isa level lower than baseline? > diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h > index f62be0b9b3..ef0c6b4fbc 100644 > --- a/sysdeps/x86/include/cpu-features.h > +++ b/sysdeps/x86/include/cpu-features.h > @@ -129,6 +129,8 @@ struct cpu_features > struct cpu_features_basic basic; > struct cpuid_features features[COMMON_CPUID_INDEX_MAX]; > unsigned int preferred[PREFERRED_FEATURE_INDEX_MAX]; > + /* X86 micro-architecture ISA levels. */ > + unsigned int isa_1; > /* The state size for XSAVEC or XSAVE. The type must be unsigned long > int so that we use > Ok. > diff --git a/sysdeps/x86/isa-level.c b/sysdeps/x86/isa-level.c > new file mode 100644 > index 0000000000..0751e86f27 > --- /dev/null > +++ b/sysdeps/x86/isa-level.c > @@ -0,0 +1,95 @@ > +/* ELF program property for x86 ISA level. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + In addition to the permissions in the GNU Lesser General Public > + License, the Free Software Foundation gives you unlimited > + permission to link the compiled version of this file with other > + programs, and to distribute those programs without any restriction > + coming from the use of this file. (The Lesser General Public > + License restrictions do apply in other respects; for example, they > + cover modification of the file, and distribution when not linked > + into another program.) > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <elf.h> > + > +/* ELF program property for x86 ISA level. */ > +#ifdef INCLUDE_X86_ISA_LEVEL > +# if defined __x86_64__ || defined __FXSR__ || !defined _SOFT_FLOAT \ > + || defined __MMX__ || defined __SSE__ || defined __SSE2__ > +# define ISA_BASELINE GNU_PROPERTY_X86_ISA_1_BASELINE > +# else > +# define ISA_BASELINE 0 > +# endif > + > +# if defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 \ > + || (defined __x86_64__ && defined __LAHF_SAHF__) \ > + || defined __POPCNT__ || defined __SSE3__ \ > + || defined __SSSE3__ || defined __SSE4_1__ || defined __SSE4_2__ > +# define ISA_V2 GNU_PROPERTY_X86_ISA_1_V2 > +# else > +# define ISA_V2 0 > +# endif > + > +# if defined __AVX__ || defined __AVX2__ || defined __F16C__ \ > + || defined __FMA__ || defined __LZCNT__ || defined __MOVBE__ \ > + || defined __XSAVE__ > +# define ISA_V3 GNU_PROPERTY_X86_ISA_1_V3 > +# else > +# define ISA_V3 0 > +# endif > + > +# if defined __AVX512F__ || defined __AVX512BW__ || defined __AVX512CD__ \ > + || defined __AVX512DQ__ || defined __AVX512VL__ > +# define ISA_V4 GNU_PROPERTY_X86_ISA_1_V4 > +# else > +# define ISA_V4 0 > +# endif > + > +# define ISA_LEVEL (ISA_BASELINE | ISA_V2 | ISA_V3 | ISA_V4) > + > +# if ISA_LEVEL > +# ifdef __LP64__ > +# define PROPERTY_ALIGN 3 > +# else > +# define PROPERTY_ALIGN 2 > +# endif > + > +# define note_stringify(arg) note_stringify_1(arg) > +# define note_stringify_1(arg) #arg > + > +asm(".pushsection \".note.gnu.property\",\"a\",@note\n" > +" .p2align " note_stringify (PROPERTY_ALIGN) > + /* name length. */ > +"\n .long 1f - 0f\n" > + /* data length. */ > +" .long 4f - 1f\n" > + /* note type: NT_GNU_PROPERTY_TYPE_0. */ > +" .long " note_stringify (NT_GNU_PROPERTY_TYPE_0) > + /* vendor name. */ > +"\n0: .asciz \"GNU\"\n" > +"1: .p2align " note_stringify (PROPERTY_ALIGN) > + /* pr_type: GNU_PROPERTY_X86_ISA_1_NEEDED. */ > +"\n .long " note_stringify (GNU_PROPERTY_X86_ISA_1_NEEDED) > + /* pr_datasz. */ > +"\n .long 3f - 2f\n" > + /* GNU_PROPERTY_X86_ISA_1_V[234]. */ > +"2:\n .long " note_stringify (ISA_LEVEL) > +"\n3:\n .p2align " note_stringify (PROPERTY_ALIGN) > +"\n4:\n .popsection"); > +# endif /* ISA_LEVEL */ > +#endif /* INCLUDE_X86_ISA_LEVEL */ Ok. > diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h > index 2e4f8850f9..3c550e529a 100644 > --- a/sysdeps/x86/link_map.h > +++ b/sysdeps/x86/link_map.h > @@ -16,12 +16,16 @@ > License along with the GNU C Library; if not, see > <https://www.gnu.org/licenses/>. */ > > -/* If this object is enabled with CET. */ > +/* if this object has GNU property. */ > enum > { > - lc_unknown = 0, /* Unknown CET status. */ > - lc_none = 1 << 0, /* Not enabled with CET. */ > - lc_ibt = 1 << 1, /* Enabled with IBT. */ > - lc_shstk = 1 << 2, /* Enabled with STSHK. */ > - lc_ibt_and_shstk = lc_ibt | lc_shstk /* Enabled with both. */ > - } l_cet:3; > + lc_property_unknown = 0, /* Unknown property status. */ > + lc_property_none = 1 << 0, /* No property. */ > + lc_property_valid = 1 << 1 /* Has valid property. */ > + } l_property:2; > + Ok. > +/* GNU_PROPERTY_X86_FEATURE_1_AND of this object. */ > +unsigned int l_x86_feature_1_and; > + > +/* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */ > +unsigned int l_x86_isa_1_needed; Ok. > diff --git a/sysdeps/x86/tst-isa-level-1.c b/sysdeps/x86/tst-isa-level-1.c > new file mode 100644 > index 0000000000..c263157c8c > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-1.c > @@ -0,0 +1,74 @@ > +/* Check ISA level on dlopened shared object. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <stdlib.h> > +#include <stdbool.h> > +#include <string.h> > +#include <elf.h> > +#include <get-isa-level.h> > +#include <support/xdlfcn.h> > +#include <support/check.h> > +#include <support/test-driver.h> > + > +static void > +do_test_1 (const char *modname, bool fail) > +{ > + int (*fp) (void); > + void *h; > + > + h = dlopen (modname, RTLD_LAZY); > + if (h == NULL) > + { > + const char *err = dlerror (); > + if (fail) > + { > + if (strstr (err, "CPU ISA level is lower than required") == NULL) > + FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err); > + > + return; > + } > + > + FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err); > + } > + > + if (fail) > + FAIL_EXIT1 ("dlopen '%s' should have failed\n", modname); > + > + fp = xdlsym (h, "test"); > + > + if (fp () != 0) > + FAIL_EXIT1 ("test () != 0\n"); > + > + dlclose (h); > +} > + Ok. > +static int > +do_test (void) > +{ > + const struct cpu_features *cpu_features > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > + unsigned int isa_level = get_isa_level (cpu_features); > + /* Skip on x86-64-v4 platforms since dlopen always works. */ > + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) > + return EXIT_UNSUPPORTED; > + do_test_1 ("tst-isa-level-mod-1a.so", true); > + do_test_1 ("tst-isa-level-mod-1b.so", false); > + return EXIT_SUCCESS; > +} > + > +#include <support/test-driver.c> Maybe use my suggestion to improve test coverage. > diff --git a/sysdeps/x86/tst-isa-level-mod-1.c b/sysdeps/x86/tst-isa-level-mod-1.c > new file mode 100644 > index 0000000000..3816536428 > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-mod-1.c > @@ -0,0 +1,23 @@ > +/* Check ISA level on dlopened shared object. > + Copyright (C) 2019-2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +int > +test (void) > +{ > + return 0; > +} Ok. > diff --git a/sysdeps/x86/tst-isa-level-mod-1a.c b/sysdeps/x86/tst-isa-level-mod-1a.c > new file mode 100644 > index 0000000000..4f026dd915 > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-mod-1a.c > @@ -0,0 +1,2 @@ > +#include <isa-level.c> > +#include "tst-isa-level-mod-1.c" > diff --git a/sysdeps/x86/tst-isa-level-mod-1b.c b/sysdeps/x86/tst-isa-level-mod-1b.c > new file mode 100644 > index 0000000000..d6fe0685a4 > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-mod-1b.c > @@ -0,0 +1 @@ > +#include "tst-isa-level-mod-1.c" Ok. > diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c > index 8810a822ef..e5640303f9 100644 > --- a/sysdeps/x86_64/dl-hwcaps-subdirs.c > +++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c > @@ -18,6 +18,7 @@ > > #include <dl-hwcaps.h> > #include <cpu-features.h> > +#include <get-isa-level.h> > > const char _dl_hwcaps_subdirs[] = "x86-64-v4:x86-64-v3:x86-64-v2"; > enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ > @@ -25,40 +26,25 @@ enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ > uint32_t > _dl_hwcaps_subdirs_active (void) > { > + const struct cpu_features *cpu_features > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > + unsigned int isa_level = get_isa_level (cpu_features); > int active = 0; > > /* Test in reverse preference order. */ > > /* x86-64-v2. */ > - if (!(CPU_FEATURE_USABLE (CMPXCHG16B) > - && CPU_FEATURE_USABLE (LAHF64_SAHF64) > - && CPU_FEATURE_USABLE (POPCNT) > - && CPU_FEATURE_USABLE (SSE3) > - && CPU_FEATURE_USABLE (SSE4_1) > - && CPU_FEATURE_USABLE (SSE4_2) > - && CPU_FEATURE_USABLE (SSSE3))) > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > ++active; > > /* x86-64-v3. */ > - if (!(CPU_FEATURE_USABLE (AVX) > - && CPU_FEATURE_USABLE (AVX2) > - && CPU_FEATURE_USABLE (BMI1) > - && CPU_FEATURE_USABLE (BMI2) > - && CPU_FEATURE_USABLE (F16C) > - && CPU_FEATURE_USABLE (FMA) > - && CPU_FEATURE_USABLE (LZCNT) > - && CPU_FEATURE_USABLE (MOVBE) > - && CPU_FEATURE_USABLE (OSXSAVE))) > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > ++active; > > - /* x86-64-v4. */ > - if (!(CPU_FEATURE_USABLE (AVX512F) > - && CPU_FEATURE_USABLE (AVX512BW) > - && CPU_FEATURE_USABLE (AVX512CD) > - && CPU_FEATURE_USABLE (AVX512DQ) > - && CPU_FEATURE_USABLE (AVX512VL))) > + /* x86-64-v4. */ > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > ++active; > Ok. > diff --git a/sysdeps/x86_64/tst-glibc-hwcaps.c b/sysdeps/x86_64/tst-glibc-hwcaps.c > index 3075a8286d..a041a9047d 100644 > --- a/sysdeps/x86_64/tst-glibc-hwcaps.c > +++ b/sysdeps/x86_64/tst-glibc-hwcaps.c > @@ -19,7 +19,8 @@ > #include <stdio.h> > #include <support/check.h> > #include <sys/param.h> > -#include <sys/platform/x86.h> > +#include <elf.h> > +#include <get-isa-level.h> > > extern int marker2 (void); > extern int marker3 (void); > @@ -31,35 +32,15 @@ compute_level (void) > { > const struct cpu_features *cpu_features > = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > - > - if (!(CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) > - && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) > - && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) > - && CPU_FEATURE_USABLE_P (cpu_features, MMX) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE2) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE3) > - && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2))) > - return 1; > - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX2) > - && CPU_FEATURE_USABLE_P (cpu_features, BMI1) > - && CPU_FEATURE_USABLE_P (cpu_features, BMI2) > - && CPU_FEATURE_USABLE_P (cpu_features, F16C) > - && CPU_FEATURE_USABLE_P (cpu_features, FMA) > - && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) > - && CPU_FEATURE_USABLE_P (cpu_features, MOVBE) > - && CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE))) > - return 2; > - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX512F) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL))) > - return 3; > - return 4; > + unsigned int isa_level = get_isa_level (cpu_features); > + > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) > + return 1; > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) > + return 2; > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) > + return 3; > + return 4; > } > > static int > Ok. ^ permalink raw reply [flat|nested] 27+ messages in thread
* V6 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] 2021-01-06 12:00 ` Adhemerval Zanella @ 2021-01-06 17:11 ` H.J. Lu 2021-01-07 20:09 ` Adhemerval Zanella 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2021-01-06 17:11 UTC (permalink / raw) To: Adhemerval Zanella; +Cc: GNU C Library, Florian Weimer [-- Attachment #1: Type: text/plain, Size: 47275 bytes --] On Wed, Jan 6, 2021 at 4:01 AM Adhemerval Zanella <adhemerval.zanella@linaro.org> wrote: > > > > On 06/12/2020 11:49, H.J. Lu via Libc-alpha wrote: > > GCC 11 supports -march=x86-64-v[234] to enable x86 micro-architecture ISA > > levels: > > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97250 > > > > and -mneeded to emit GNU_PROPERTY_X86_ISA_1_NEEDED property with > > GNU_PROPERTY_X86_ISA_1_V[234] marker: > > > > https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/13 > > > > Binutils support for GNU_PROPERTY_X86_ISA_1_V[234] marker were added by > > > > commit b0ab06937385e0ae25cebf1991787d64f439bf12 > > Author: H.J. Lu <hjl.tools@gmail.com> > > Date: Fri Oct 30 06:49:57 2020 -0700 > > > > x86: Support GNU_PROPERTY_X86_ISA_1_BASELINE marker > > > > and > > > > commit 32930e4edbc06bc6f10c435dbcc63131715df678 > > Author: H.J. Lu <hjl.tools@gmail.com> > > Date: Fri Oct 9 05:05:57 2020 -0700 > > > > x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker > > > > GNU_PROPERTY_X86_ISA_1_NEEDED property in x86 ELF binaries indicate the > > micro-architecture ISA level required to execute the binary. The marker > > must be added by programmers explicitly in one of 3 ways: > > > > 1. Pass -mneeded to GCC. > > 2. Add the marker in the linker inputs as this patch does. > > 3. Pass -z x86-64-v[234] to the linker. > > > > Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > > marker support to ld.so if binutils 2.32 or newer is used to build glibc: > > > > 1. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > > markers to elf.h. > > 2. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > > marker to abi-note.o based on the ISA level used to compile abi-note.o, > > assuming that the same ISA level is used to compile the whole glibc. > > 3. Add isa_1 to cpu_features to record the supported x86 ISA level. > > 4. Rename _dl_process_cet_property_note to _dl_process_property_note and > > add GNU_PROPERTY_X86_ISA_1_V[234] marker detection. > > 5. Update _rtld_main_check and _dl_open_check to check loaded objects > > with the incompatible ISA level. > > 6. Add a testcase to verify that dlopen an x86-64-v4 shared object fails > > on lesser platforms. > > 7. Use <get-isa-level.h> in dl-hwcaps-subdirs.c and tst-glibc-hwcaps.c. > > > > Marked elf/tst-isa-level-1 with x86-64-v4, ran it on x86-64-v3 machine > > and got: > > > > [hjl@gnu-cfl-2 build-x86_64-linux]$ ./elf/tst-isa-level-1 > > ./elf/tst-isa-level-1: CPU ISA level is lower than required > > [hjl@gnu-cfl-2 build-x86_64-linux]$ > > --- > > config.h.in | 3 + > > elf/elf.h | 35 ++++----- > > sysdeps/x86/Makefile | 10 +++ > > sysdeps/x86/abi-note.c | 29 ++++++++ > > sysdeps/x86/configure | 101 ++++++++++++++++++++++++++ > > sysdeps/x86/configure.ac | 26 +++++++ > > sysdeps/x86/cpu-features.c | 3 + > > sysdeps/x86/dl-cet.c | 12 ++- > > sysdeps/x86/dl-prop.h | 113 +++++++++++++++++++++-------- > > sysdeps/x86/get-isa-level.h | 66 +++++++++++++++++ > > sysdeps/x86/include/cpu-features.h | 2 + > > sysdeps/x86/isa-level.c | 95 ++++++++++++++++++++++++ > > sysdeps/x86/link_map.h | 18 +++-- > > sysdeps/x86/tst-isa-level-1.c | 74 +++++++++++++++++++ > > sysdeps/x86/tst-isa-level-mod-1.c | 23 ++++++ > > sysdeps/x86/tst-isa-level-mod-1a.c | 2 + > > sysdeps/x86/tst-isa-level-mod-1b.c | 1 + > > sysdeps/x86_64/dl-hwcaps-subdirs.c | 30 ++------ > > sysdeps/x86_64/tst-glibc-hwcaps.c | 41 +++-------- > > 19 files changed, 571 insertions(+), 113 deletions(-) > > create mode 100644 sysdeps/x86/abi-note.c > > create mode 100644 sysdeps/x86/get-isa-level.h > > create mode 100644 sysdeps/x86/isa-level.c > > create mode 100644 sysdeps/x86/tst-isa-level-1.c > > create mode 100644 sysdeps/x86/tst-isa-level-mod-1.c > > create mode 100644 sysdeps/x86/tst-isa-level-mod-1a.c > > create mode 100644 sysdeps/x86/tst-isa-level-mod-1b.c > > > > diff --git a/config.h.in b/config.h.in > > index b823c8e080..dc3854f4eb 100644 > > --- a/config.h.in > > +++ b/config.h.in > > @@ -266,4 +266,7 @@ > > /* The default value of x86 CET control. */ > > #define DEFAULT_DL_X86_CET_CONTROL cet_elf_property > > > > +/* Define if x86 ISA level should be included in shared libraries. */ > > +#undef INCLUDE_X86_ISA_LEVEL > > + > > #endif > > diff --git a/elf/elf.h b/elf/elf.h > > index bd7af7cca4..42fd697129 100644 > > --- a/elf/elf.h > > +++ b/elf/elf.h > > @@ -1325,31 +1325,26 @@ typedef struct > > > > /* The x86 instruction sets indicated by the corresponding bits are > > used in program. Their support in the hardware is optional. */ > > -#define GNU_PROPERTY_X86_ISA_1_USED 0xc0000000 > > +#define GNU_PROPERTY_X86_ISA_1_USED 0xc0010002 > > /* The x86 instruction sets indicated by the corresponding bits are > > used in program and they must be supported by the hardware. */ > > -#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0000001 > > +#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0008002 > > /* X86 processor-specific features used in program. */ > > #define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 > > > > -#define GNU_PROPERTY_X86_ISA_1_486 (1U << 0) > > -#define GNU_PROPERTY_X86_ISA_1_586 (1U << 1) > > -#define GNU_PROPERTY_X86_ISA_1_686 (1U << 2) > > -#define GNU_PROPERTY_X86_ISA_1_SSE (1U << 3) > > -#define GNU_PROPERTY_X86_ISA_1_SSE2 (1U << 4) > > -#define GNU_PROPERTY_X86_ISA_1_SSE3 (1U << 5) > > -#define GNU_PROPERTY_X86_ISA_1_SSSE3 (1U << 6) > > -#define GNU_PROPERTY_X86_ISA_1_SSE4_1 (1U << 7) > > -#define GNU_PROPERTY_X86_ISA_1_SSE4_2 (1U << 8) > > -#define GNU_PROPERTY_X86_ISA_1_AVX (1U << 9) > > -#define GNU_PROPERTY_X86_ISA_1_AVX2 (1U << 10) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512F (1U << 11) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512CD (1U << 12) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512ER (1U << 13) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512PF (1U << 14) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512VL (1U << 15) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512DQ (1U << 16) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512BW (1U << 17) > > +/* GNU_PROPERTY_X86_ISA_1_BASELINE: CMOV, CX8 (cmpxchg8b), FPU (fld), > > + MMX, OSFXSR (fxsave), SCE (syscall), SSE and SSE2. */ > > +#define GNU_PROPERTY_X86_ISA_1_BASELINE (1U << 0) > > +/* GNU_PROPERTY_X86_ISA_1_V2: GNU_PROPERTY_X86_ISA_1_BASELINE, > > + CMPXCHG16B (cmpxchg16b), LAHF-SAHF (lahf), POPCNT (popcnt), SSE3, > > + SSSE3, SSE4.1 and SSE4.2. */ > > +#define GNU_PROPERTY_X86_ISA_1_V2 (1U << 1) > > +/* GNU_PROPERTY_X86_ISA_1_V3: GNU_PROPERTY_X86_ISA_1_V2, AVX, AVX2, BMI1, > > + BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE. */ > > +#define GNU_PROPERTY_X86_ISA_1_V3 (1U << 2) > > +/* GNU_PROPERTY_X86_ISA_1_V4: GNU_PROPERTY_X86_ISA_1_V3, AVX512F, > > + AVX512BW, AVX512CD, AVX512DQ and AVX512VL. */ > > +#define GNU_PROPERTY_X86_ISA_1_V4 (1U << 3) > > > > /* This indicates that all executable sections are compatible with > > IBT. */ > > Shouldn't it follow bintuils 32930e4edbc06 change and move current > GNU_PROPERTY_X86_ISA_1_ definitions to GNU_PROPERTY_X86_COMPAT_2_? > Not sure if this charecterize any exported ABI change, since the > most possible usual consumer (bintuils) does not use glibc version. > > Also, maybe move this change to an specific patch. GNU_PROPERTY_X86_COMPAT_XXX were added to binutils consumers to support existing old binaries. Since glibc never supports old properties, there is no need to add them to glibc. > > > diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile > > index 081cc72e93..109f03e9b9 100644 > > --- a/sysdeps/x86/Makefile > > +++ b/sysdeps/x86/Makefile > > @@ -9,6 +9,16 @@ sysdep_headers += sys/platform/x86.h > > tests += tst-get-cpu-features tst-get-cpu-features-static \ > > tst-cpu-features-cpuinfo tst-cpu-features-supports > > tests-static += tst-get-cpu-features-static > > +ifeq (yesyes,$(enable-x86-isa-level)$(config-cflags-skylake-avx512)) > > +tests += tst-isa-level-1 > > +modules-names += tst-isa-level-mod-1a tst-isa-level-mod-1b > > + > > +CFLAGS-tst-isa-level-mod-1a.c += -march=skylake-avx512 \ > > + -DINCLUDE_X86_ISA_LEVEL > > +$(objpfx)tst-isa-level-1: $(libdl) > > +$(objpfx)tst-isa-level-1.out: $(objpfx)tst-isa-level-mod-1a.so \ > > + $(objpfx)tst-isa-level-mod-1b.so > > +endif > > endif > > > > ifeq ($(subdir),math) > > This will limit the test to a very narrow set of machines, couldn't > we check only for enable-x86-isa-level and make the conditional check > for ISA inside the test itself? Fixed. > What I have in mind would be something like: > > 1. Create onde module object for each ISA variant, by varying either > -march or using the specific gcc option (not sure it would clash > with the ABI notes isa-level.c already include). > > 2. On tst-isa-level-1.c check current ISA level with get_isa_level > and check against the ISA_BASELINE and load the modules with a > ISA number lower than baseline. > > Something like: > > const struct cpu_features *cpu_features > = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > unsigned int isa_level = get_isa_level (cpu_features); > > bool check_isa_baseline = (isa_level & ISA_BASELINE) == ISA_BASELINE; > bool check_isa_v2 = (isa_level & ISA_V2) == ISA_V2; > bool check_isa_v3 = (isa_level & ISA_V3) == ISA_V3; > bool check_isa_v4 = (isa_level & ISA_V4) == ISA_V4; > > do_test_1 ("tst-isa-level-mod-1-baseline.so", true); > do_test_1 ("tst-isa-level-mod-1-isa_v2.so", true); > do_test_1 ("tst-isa-level-mod-1-isa_v3.so", true); > do_test_1 ("tst-isa-level-mod-1-isa_v4.so", true); Fixed. > You might require to just build the isa_v4 only for > config-cflags-skylake-avx512 and conditionalize with HAVE_SKYLAKE_AVX512_SUPPORT > (which seems to be ununsed in this patch). Fixed. > > diff --git a/sysdeps/x86/abi-note.c b/sysdeps/x86/abi-note.c > > new file mode 100644 > > index 0000000000..5140e6ab47 > > --- /dev/null > > +++ b/sysdeps/x86/abi-note.c > > @@ -0,0 +1,29 @@ > > +/* Special .init and .fini section support. x86-64 version. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + In addition to the permissions in the GNU Lesser General Public > > + License, the Free Software Foundation gives you unlimited > > + permission to link the compiled version of this file with other > > + programs, and to distribute those programs without any restriction > > + coming from the use of this file. (The Lesser General Public > > + License restrictions do apply in other respects; for example, they > > + cover modification of the file, and distribution when not linked > > + into another program.) > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <isa-level.c> > > +#include <csu/abi-note.c> > > Ok, > > > diff --git a/sysdeps/x86/configure b/sysdeps/x86/configure > > index 81cc4e80d6..2738fe47b2 100644 > > --- a/sysdeps/x86/configure > > +++ b/sysdeps/x86/configure > > @@ -68,3 +68,104 @@ elif test $enable_cet = permissive; then > > fi > > config_vars="$config_vars > > enable-cet = $enable_cet" > > + > > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -march=skylake-avx512 support" >&5 > > +$as_echo_n "checking for -march=skylake-avx512 support... " >&6; } > > +if ${libc_cv_cc_skylake_avx512+:} false; then : > > + $as_echo_n "(cached) " >&6 > > +else > > + if { ac_try='${CC-cc} -march=skylake-avx512 -xc /dev/null -S -o /dev/null' > > + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 > > + (eval $ac_try) 2>&5 > > + ac_status=$? > > + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 > > + test $ac_status = 0; }; }; then : > > + libc_cv_cc_skylake_avx512=yes > > +else > > + libc_cv_cc_skylake_avx512=no > > +fi > > + > > +fi > > +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_cc_skylake_avx512" >&5 > > +$as_echo "$libc_cv_cc_skylake_avx512" >&6; } > > +if test $libc_cv_cc_skylake_avx512 = yes; then > > + $as_echo "#define HAVE_SKYLAKE_AVX512_SUPPORT 1" >>confdefs.h > > + > > +fi > > +config_vars="$config_vars > > +config-cflags-skylake-avx512 = $libc_cv_cc_skylake_avx512" > > + > > +libc_cv_include_x86_isa_level=no > > +if test -z "`$LD --version | sed -n 's/^GNU \(gold\).*$/\1/p'`"; then > > + # Check for ld 2.32 or higher. > > + libc_cv_include_x86_isa_level=yes > > + for ac_prog in $LD > > +do > > + # Extract the first word of "$ac_prog", so it can be a program name with args. > > +set dummy $ac_prog; ac_word=$2 > > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 > > +$as_echo_n "checking for $ac_word... " >&6; } > > +if ${ac_cv_prog_LD+:} false; then : > > + $as_echo_n "(cached) " >&6 > > +else > > + if test -n "$LD"; then > > + ac_cv_prog_LD="$LD" # Let the user override the test. > > +else > > +as_save_IFS=$IFS; IFS=$PATH_SEPARATOR > > +for as_dir in $PATH > > +do > > + IFS=$as_save_IFS > > + test -z "$as_dir" && as_dir=. > > + for ac_exec_ext in '' $ac_executable_extensions; do > > + if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then > > + ac_cv_prog_LD="$ac_prog" > > + $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 > > + break 2 > > + fi > > +done > > + done > > +IFS=$as_save_IFS > > + > > +fi > > +fi > > +LD=$ac_cv_prog_LD > > +if test -n "$LD"; then > > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $LD" >&5 > > +$as_echo "$LD" >&6; } > > +else > > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 > > +$as_echo "no" >&6; } > > +fi > > + > > + > > + test -n "$LD" && break > > +done > > + > > +if test -z "$LD"; then > > + ac_verc_fail=yes > > +else > > + # Found it, now check the version. > > + { $as_echo "$as_me:${as_lineno-$LINENO}: checking version of $LD" >&5 > > +$as_echo_n "checking version of $LD... " >&6; } > > + ac_prog_version=`$LD --version 2>&1 | sed -n 's/^.*GNU ld.* \([0-9][0-9]*\.[0-9.]*\).*$/\1/p'` > > + case $ac_prog_version in > > + '') ac_prog_version="v. ?.??, bad"; ac_verc_fail=yes;; > > + 2.3[2-9]*|2.[4-9][0-9]*|[3-9].*|[1-9][0-9]*) > > + ac_prog_version="$ac_prog_version, ok"; ac_verc_fail=no;; > > + *) ac_prog_version="$ac_prog_version, bad"; ac_verc_fail=yes;; > > + > > + esac > > + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_prog_version" >&5 > > +$as_echo "$ac_prog_version" >&6; } > > +fi > > +if test $ac_verc_fail = yes; then > > + libc_cv_include_x86_isa_level=no > > +fi > > + > > + if test $libc_cv_include_x86_isa_level = yes; then > > + $as_echo "#define INCLUDE_X86_ISA_LEVEL 1" >>confdefs.h > > + > > + fi > > +fi > > +config_vars="$config_vars > > +enable-x86-isa-level = $libc_cv_include_x86_isa_level" > > diff --git a/sysdeps/x86/configure.ac b/sysdeps/x86/configure.ac > > index 8f3e1191f6..8f00b9663d 100644 > > --- a/sysdeps/x86/configure.ac > > +++ b/sysdeps/x86/configure.ac > > @@ -43,3 +43,29 @@ elif test $enable_cet = permissive; then > > AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_permissive) > > fi > > LIBC_CONFIG_VAR([enable-cet], [$enable_cet]) > > + > > +dnl Check if -march=skylake-avx512 works. > > +AC_CACHE_CHECK(for -march=skylake-avx512 support, > > + libc_cv_cc_skylake_avx512, [dnl > > +LIBC_TRY_CC_OPTION([-march=skylake-avx512], > > + [libc_cv_cc_skylake_avx512=yes], > > + [libc_cv_cc_skylake_avx512=no]) > > +]) > > +if test $libc_cv_cc_skylake_avx512 = yes; then > > + AC_DEFINE(HAVE_SKYLAKE_AVX512_SUPPORT) > > +fi > > Where is it used specifically? There is no config.h.in tag either. The whole check is removed. > > +LIBC_CONFIG_VAR([config-cflags-skylake-avx512], [$libc_cv_cc_skylake_avx512]) > > + > > +libc_cv_include_x86_isa_level=no > > +if test -z "`$LD --version | sed -n 's/^GNU \(gold\).*$/\1/p'`"; then > > + # Check for ld 2.32 or higher. > > + libc_cv_include_x86_isa_level=yes > > + AC_CHECK_PROG_VER(LD, $LD, --version, > > + [GNU ld.* \([0-9][0-9]*\.[0-9.]*\)], > > + [2.3[2-9]*|2.[4-9][0-9]*|[3-9].*|[1-9][0-9]*], > > + libc_cv_include_x86_isa_level=no) > > + if test $libc_cv_include_x86_isa_level = yes; then > > + AC_DEFINE(INCLUDE_X86_ISA_LEVEL) > > + fi > > +fi > > +LIBC_CONFIG_VAR([enable-x86-isa-level], [$libc_cv_include_x86_isa_level]) > > Instead of version check, would it better to check if the linker actually > build a snippet with the expected notes? Fixed. > > diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c > > index fe080b63b2..ec5cdafe81 100644 > > --- a/sysdeps/x86/cpu-features.c > > +++ b/sysdeps/x86/cpu-features.c > > @@ -19,6 +19,7 @@ > > #include <cpuid.h> > > #include <dl-hwcap.h> > > #include <libc-pointer-arith.h> > > +#include <get-isa-level.h> > > #if IS_IN (libc) && !defined SHARED > > # include <assert.h> > > # include <unistd.h> > > @@ -290,6 +291,8 @@ update_usable (struct cpu_features *cpu_features) > > CPU_FEATURE_SET_USABLE (cpu_features, KL); > > CPU_FEATURE_SET_USABLE (cpu_features, WIDE_KL); > > } > > + > > + cpu_features->isa_1 = get_isa_level (cpu_features); > > } > > > > static void > > Ok. > > > diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c > > index 5524b66038..8986c776e5 100644 > > --- a/sysdeps/x86/dl-cet.c > > +++ b/sysdeps/x86/dl-cet.c > > @@ -76,10 +76,12 @@ dl_cet_check (struct link_map *m, const char *program) > > */ > > enable_ibt &= (HAS_CPU_FEATURE (IBT) > > && (enable_ibt_type == cet_always_on > > - || (m->l_cet & lc_ibt) != 0)); > > + || (m->l_x86_feature_1_and > > + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0)); > > enable_shstk &= (HAS_CPU_FEATURE (SHSTK) > > && (enable_shstk_type == cet_always_on > > - || (m->l_cet & lc_shstk) != 0)); > > + || (m->l_x86_feature_1_and > > + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)); > > } > > > > Ok. > > > /* ld.so is CET-enabled by kernel. But shared objects may not > > @@ -111,7 +113,8 @@ dl_cet_check (struct link_map *m, const char *program) > > /* IBT is enabled only if it is enabled in executable as > > well as all shared objects. */ > > enable_ibt &= (enable_ibt_type == cet_always_on > > - || (l->l_cet & lc_ibt) != 0); > > + || (l->l_x86_feature_1_and > > + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0); > > if (!found_ibt_legacy && enable_ibt != ibt_enabled) > > { > > found_ibt_legacy = true; > > @@ -121,7 +124,8 @@ dl_cet_check (struct link_map *m, const char *program) > > /* SHSTK is enabled only if it is enabled in executable as > > well as all shared objects. */ > > enable_shstk &= (enable_shstk_type == cet_always_on > > - || (l->l_cet & lc_shstk) != 0); > > + || (l->l_x86_feature_1_and > > + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0); > > if (enable_shstk != shstk_enabled) > > { > > found_shstk_legacy = true; > > Ok. > > > diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h > > index 89911e19e2..61c0624c09 100644 > > --- a/sysdeps/x86/dl-prop.h > > +++ b/sysdeps/x86/dl-prop.h > > @@ -19,14 +19,54 @@ > > #ifndef _DL_PROP_H > > #define _DL_PROP_H > > > > +#include <libintl.h> > > + > > extern void _dl_cet_check (struct link_map *, const char *) > > attribute_hidden; > > extern void _dl_cet_open_check (struct link_map *) > > attribute_hidden; > > > > +static void > > +dl_isa_level_check (struct link_map *m, const char *program) > > +{ > > + const struct cpu_features *cpu_features = __get_cpu_features (); > > + unsigned int i; > > + struct link_map *l; > > + > > + i = m->l_searchlist.r_nlist; > > + while (i-- > 0) > > + { > > + /* Check each shared object to see if ISA level is compatible. */ > > + l = m->l_initfini[i]; > > + > > + /* Skip ISA level check if functions have been executed. */ > > + if (l->l_init_called) > > + continue; > > + > > +#ifdef SHARED > > + /* Skip ISA level check for ld.so since ld.so won't run if its ISA > > + level is higher than CPU. */ > > + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) > > + continue; > > +#endif > > + > > + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) > > + != l->l_x86_isa_1_needed) > > + { > > + if (program) > > + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", > > + *l->l_name != '\0' ? l->l_name : program); > > + else > > + _dl_signal_error (0, l->l_name, "dlopen", > > + N_("CPU ISA level is lower than required")); > > + } > > + } > > +} > > + > > Ok. Not sure if it is worth to translate the _dl_fatal_printf (it seems > that at least one use at elf/dl-open.c does it). I left it unchanged. > > > static inline void __attribute__ ((always_inline)) > > _rtld_main_check (struct link_map *m, const char *program) > > { > > + dl_isa_level_check (m, program); > > #if CET_ENABLED > > _dl_cet_check (m, program); > > #endif > > @@ -35,20 +75,18 @@ _rtld_main_check (struct link_map *m, const char *program) > > static inline void __attribute__ ((always_inline)) > > _dl_open_check (struct link_map *m) > > { > > + dl_isa_level_check (m, NULL); > > #if CET_ENABLED > > _dl_cet_open_check (m); > > #endif > > } > > > > Ok. > > > static inline void __attribute__ ((unused)) > > -_dl_process_cet_property_note (struct link_map *l, > > - const ElfW(Nhdr) *note, > > - const ElfW(Addr) size, > > - const ElfW(Addr) align) > > +_dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, > > + const ElfW(Addr) size, const ElfW(Addr) align) > > { > > -#if CET_ENABLED > > /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ > > - if (l->l_cet != lc_unknown) > > + if (l->l_property != lc_property_unknown) > > return; > > > > /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in > > Ok. > > > @@ -59,7 +97,8 @@ _dl_process_cet_property_note (struct link_map *l, > > > > const ElfW(Addr) start = (ElfW(Addr)) note; > > > > - unsigned int feature_1 = 0; > > + unsigned int feature_1_and = 0; > > + unsigned int isa_1_needed = 0; > > unsigned int last_type = 0; > > > > while ((ElfW(Addr)) (note + 1) - start < size) > > @@ -71,11 +110,11 @@ _dl_process_cet_property_note (struct link_map *l, > > { > > /* Stop if we see more than one GNU property note which may > > be generated by the older linker. */ > > - if (l->l_cet != lc_unknown) > > + if (l->l_property != lc_property_unknown) > > return; > > > > - /* Check CET status now. */ > > - l->l_cet = lc_none; > > + /* Check CET status and ISA levels now. */ > > + l->l_property = lc_property_none; > > > > /* Check for invalid property. */ > > if (note->n_descsz < 8 > > Ok. > > > @@ -101,26 +140,37 @@ _dl_process_cet_property_note (struct link_map *l, > > > > last_type = type; > > > > - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) > > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND > > + || type == GNU_PROPERTY_X86_ISA_1_NEEDED) > > { > > - /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4 > > - bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND, > > - we stop the search regardless if its size is correct > > - or not. There is no point to continue if this note > > - is ill-formed. */ > > + /* The sizes of types which we are searching for are > > + 4 bytes. There is no point to continue if this > > + note is ill-formed. */ > > if (datasz != 4) > > return; > > > > Ok. > > > - feature_1 = *(unsigned int *) ptr; > > - > > - /* Keep searching for the next GNU property note > > - generated by the older linker. */ > > - break; > > + /* NB: Stop the scan only after seeing all types which > > + we are searching for. */ > > + _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > > > + GNU_PROPERTY_X86_FEATURE_1_AND), > > + "GNU_PROPERTY_X86_ISA_1_NEEDED > " > > + "GNU_PROPERTY_X86_FEATURE_1_AND"); > > Ok. > > > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) > > + feature_1_and = *(unsigned int *) ptr; > > + else > > + { > > + isa_1_needed = *(unsigned int *) ptr; > > + > > + /* Keep searching for the next GNU property note > > + generated by the older linker. */ > > + break; > > + } > > } > > - else if (type > GNU_PROPERTY_X86_FEATURE_1_AND) > > + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) > > { > > - /* Stop since property type is in ascending order. */ > > - return; > > + /* Stop the scan since property type is in ascending > > + order. */ > > + break; > > } > > > > /* Check the next property item. */ > > Ok. > > > @@ -137,18 +187,21 @@ _dl_process_cet_property_note (struct link_map *l, > > } > > > > /* We get here only if there is one or no GNU property note. */ > > - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT)) > > - l->l_cet |= lc_ibt; > > - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) > > - l->l_cet |= lc_shstk; > > -#endif > > + if (isa_1_needed || feature_1_and) > > No implicit checks. Fixed. > > + { > > + l->l_property = lc_property_valid; > > + l->l_x86_isa_1_needed = isa_1_needed; > > + l->l_x86_feature_1_and = feature_1_and; > > + } > > + else > > + l->l_property = lc_property_none; > > } > > > > static inline void __attribute__ ((unused)) > > _dl_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph) > > { > > const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); > > - _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align); > > + _dl_process_property_note (l, note, ph->p_memsz, ph->p_align); > > } > > > > static inline int __attribute__ ((always_inline)) > > Ok. > > > diff --git a/sysdeps/x86/get-isa-level.h b/sysdeps/x86/get-isa-level.h > > new file mode 100644 > > index 0000000000..166dd5df01 > > --- /dev/null > > +++ b/sysdeps/x86/get-isa-level.h > > @@ -0,0 +1,66 @@ > > +/* Get x86 ISA level. > > + This file is part of the GNU C Library. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <sys/platform/x86.h> > > + > > +/* Get GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > > + ISA level. */ > > + > > +static unsigned int > > +get_isa_level (const struct cpu_features *cpu_features) > > +{ > > + unsigned isa_level = 0; > > s/unsigned/unsigned int/ Fixed. > > + > > + if (CPU_FEATURE_USABLE_P (cpu_features, CMOV) > > + && CPU_FEATURE_USABLE_P (cpu_features, CX8) > > + && CPU_FEATURE_CPU_P (cpu_features, FPU) > > + && CPU_FEATURE_USABLE_P (cpu_features, FXSR) > > + && CPU_FEATURE_USABLE_P (cpu_features, MMX) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE2)) > > + { > > + isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE; > > + if (CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) > > + && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) > > + && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE3) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2)) > > + { > > + isa_level |= GNU_PROPERTY_X86_ISA_1_V2; > > + if (CPU_FEATURE_USABLE_P (cpu_features, AVX) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX2) > > + && CPU_FEATURE_USABLE_P (cpu_features, F16C) > > + && CPU_FEATURE_USABLE_P (cpu_features, FMA) > > + && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) > > + && CPU_FEATURE_USABLE_P (cpu_features, MOVBE)) > > + { > > + isa_level |= GNU_PROPERTY_X86_ISA_1_V3; > > + if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)) > > + isa_level |= GNU_PROPERTY_X86_ISA_1_V4; > > + } > > + } > > + } > > + > > + return isa_level; > > +} > > Do we actually support a isa level lower than baseline? Yes. In 32-bit, we require i486 or above. > > diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h > > index f62be0b9b3..ef0c6b4fbc 100644 > > --- a/sysdeps/x86/include/cpu-features.h > > +++ b/sysdeps/x86/include/cpu-features.h > > @@ -129,6 +129,8 @@ struct cpu_features > > struct cpu_features_basic basic; > > struct cpuid_features features[COMMON_CPUID_INDEX_MAX]; > > unsigned int preferred[PREFERRED_FEATURE_INDEX_MAX]; > > + /* X86 micro-architecture ISA levels. */ > > + unsigned int isa_1; > > /* The state size for XSAVEC or XSAVE. The type must be unsigned long > > int so that we use > > > > Ok. > > > diff --git a/sysdeps/x86/isa-level.c b/sysdeps/x86/isa-level.c > > new file mode 100644 > > index 0000000000..0751e86f27 > > --- /dev/null > > +++ b/sysdeps/x86/isa-level.c > > @@ -0,0 +1,95 @@ > > +/* ELF program property for x86 ISA level. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + In addition to the permissions in the GNU Lesser General Public > > + License, the Free Software Foundation gives you unlimited > > + permission to link the compiled version of this file with other > > + programs, and to distribute those programs without any restriction > > + coming from the use of this file. (The Lesser General Public > > + License restrictions do apply in other respects; for example, they > > + cover modification of the file, and distribution when not linked > > + into another program.) > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <elf.h> > > + > > +/* ELF program property for x86 ISA level. */ > > +#ifdef INCLUDE_X86_ISA_LEVEL > > +# if defined __x86_64__ || defined __FXSR__ || !defined _SOFT_FLOAT \ > > + || defined __MMX__ || defined __SSE__ || defined __SSE2__ > > +# define ISA_BASELINE GNU_PROPERTY_X86_ISA_1_BASELINE > > +# else > > +# define ISA_BASELINE 0 > > +# endif > > + > > +# if defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 \ > > + || (defined __x86_64__ && defined __LAHF_SAHF__) \ > > + || defined __POPCNT__ || defined __SSE3__ \ > > + || defined __SSSE3__ || defined __SSE4_1__ || defined __SSE4_2__ > > +# define ISA_V2 GNU_PROPERTY_X86_ISA_1_V2 > > +# else > > +# define ISA_V2 0 > > +# endif > > + > > +# if defined __AVX__ || defined __AVX2__ || defined __F16C__ \ > > + || defined __FMA__ || defined __LZCNT__ || defined __MOVBE__ \ > > + || defined __XSAVE__ > > +# define ISA_V3 GNU_PROPERTY_X86_ISA_1_V3 > > +# else > > +# define ISA_V3 0 > > +# endif > > + > > +# if defined __AVX512F__ || defined __AVX512BW__ || defined __AVX512CD__ \ > > + || defined __AVX512DQ__ || defined __AVX512VL__ > > +# define ISA_V4 GNU_PROPERTY_X86_ISA_1_V4 > > +# else > > +# define ISA_V4 0 > > +# endif > > + > > +# define ISA_LEVEL (ISA_BASELINE | ISA_V2 | ISA_V3 | ISA_V4) > > + > > +# if ISA_LEVEL > > +# ifdef __LP64__ > > +# define PROPERTY_ALIGN 3 > > +# else > > +# define PROPERTY_ALIGN 2 > > +# endif > > + > > +# define note_stringify(arg) note_stringify_1(arg) > > +# define note_stringify_1(arg) #arg > > + > > +asm(".pushsection \".note.gnu.property\",\"a\",@note\n" > > +" .p2align " note_stringify (PROPERTY_ALIGN) > > + /* name length. */ > > +"\n .long 1f - 0f\n" > > + /* data length. */ > > +" .long 4f - 1f\n" > > + /* note type: NT_GNU_PROPERTY_TYPE_0. */ > > +" .long " note_stringify (NT_GNU_PROPERTY_TYPE_0) > > + /* vendor name. */ > > +"\n0: .asciz \"GNU\"\n" > > +"1: .p2align " note_stringify (PROPERTY_ALIGN) > > + /* pr_type: GNU_PROPERTY_X86_ISA_1_NEEDED. */ > > +"\n .long " note_stringify (GNU_PROPERTY_X86_ISA_1_NEEDED) > > + /* pr_datasz. */ > > +"\n .long 3f - 2f\n" > > + /* GNU_PROPERTY_X86_ISA_1_V[234]. */ > > +"2:\n .long " note_stringify (ISA_LEVEL) > > +"\n3:\n .p2align " note_stringify (PROPERTY_ALIGN) > > +"\n4:\n .popsection"); > > +# endif /* ISA_LEVEL */ > > +#endif /* INCLUDE_X86_ISA_LEVEL */ > > Ok. > > > diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h > > index 2e4f8850f9..3c550e529a 100644 > > --- a/sysdeps/x86/link_map.h > > +++ b/sysdeps/x86/link_map.h > > @@ -16,12 +16,16 @@ > > License along with the GNU C Library; if not, see > > <https://www.gnu.org/licenses/>. */ > > > > -/* If this object is enabled with CET. */ > > +/* if this object has GNU property. */ > > enum > > { > > - lc_unknown = 0, /* Unknown CET status. */ > > - lc_none = 1 << 0, /* Not enabled with CET. */ > > - lc_ibt = 1 << 1, /* Enabled with IBT. */ > > - lc_shstk = 1 << 2, /* Enabled with STSHK. */ > > - lc_ibt_and_shstk = lc_ibt | lc_shstk /* Enabled with both. */ > > - } l_cet:3; > > + lc_property_unknown = 0, /* Unknown property status. */ > > + lc_property_none = 1 << 0, /* No property. */ > > + lc_property_valid = 1 << 1 /* Has valid property. */ > > + } l_property:2; > > + > > Ok. > > > +/* GNU_PROPERTY_X86_FEATURE_1_AND of this object. */ > > +unsigned int l_x86_feature_1_and; > > + > > +/* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */ > > +unsigned int l_x86_isa_1_needed; > > Ok. > > > diff --git a/sysdeps/x86/tst-isa-level-1.c b/sysdeps/x86/tst-isa-level-1.c > > new file mode 100644 > > index 0000000000..c263157c8c > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-1.c > > @@ -0,0 +1,74 @@ > > +/* Check ISA level on dlopened shared object. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <stdlib.h> > > +#include <stdbool.h> > > +#include <string.h> > > +#include <elf.h> > > +#include <get-isa-level.h> > > +#include <support/xdlfcn.h> > > +#include <support/check.h> > > +#include <support/test-driver.h> > > + > > +static void > > +do_test_1 (const char *modname, bool fail) > > +{ > > + int (*fp) (void); > > + void *h; > > + > > + h = dlopen (modname, RTLD_LAZY); > > + if (h == NULL) > > + { > > + const char *err = dlerror (); > > + if (fail) > > + { > > + if (strstr (err, "CPU ISA level is lower than required") == NULL) > > + FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err); > > + > > + return; > > + } > > + > > + FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err); > > + } > > + > > + if (fail) > > + FAIL_EXIT1 ("dlopen '%s' should have failed\n", modname); > > + > > + fp = xdlsym (h, "test"); > > + > > + if (fp () != 0) > > + FAIL_EXIT1 ("test () != 0\n"); > > + > > + dlclose (h); > > +} > > + > > Ok. > > > +static int > > +do_test (void) > > +{ > > + const struct cpu_features *cpu_features > > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > > + unsigned int isa_level = get_isa_level (cpu_features); > > + /* Skip on x86-64-v4 platforms since dlopen always works. */ > > + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) > > + return EXIT_UNSUPPORTED; > > + do_test_1 ("tst-isa-level-mod-1a.so", true); > > + do_test_1 ("tst-isa-level-mod-1b.so", false); > > + return EXIT_SUCCESS; > > +} > > + > > +#include <support/test-driver.c> > > Maybe use my suggestion to improve test coverage. Fixed. > > diff --git a/sysdeps/x86/tst-isa-level-mod-1.c b/sysdeps/x86/tst-isa-level-mod-1.c > > new file mode 100644 > > index 0000000000..3816536428 > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-mod-1.c > > @@ -0,0 +1,23 @@ > > +/* Check ISA level on dlopened shared object. > > + Copyright (C) 2019-2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +int > > +test (void) > > +{ > > + return 0; > > +} > > Ok. > > > diff --git a/sysdeps/x86/tst-isa-level-mod-1a.c b/sysdeps/x86/tst-isa-level-mod-1a.c > > new file mode 100644 > > index 0000000000..4f026dd915 > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-mod-1a.c > > @@ -0,0 +1,2 @@ > > +#include <isa-level.c> > > +#include "tst-isa-level-mod-1.c" > > diff --git a/sysdeps/x86/tst-isa-level-mod-1b.c b/sysdeps/x86/tst-isa-level-mod-1b.c > > new file mode 100644 > > index 0000000000..d6fe0685a4 > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-mod-1b.c > > @@ -0,0 +1 @@ > > +#include "tst-isa-level-mod-1.c" > > Ok. > > > diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c > > index 8810a822ef..e5640303f9 100644 > > --- a/sysdeps/x86_64/dl-hwcaps-subdirs.c > > +++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c > > @@ -18,6 +18,7 @@ > > > > #include <dl-hwcaps.h> > > #include <cpu-features.h> > > +#include <get-isa-level.h> > > > > const char _dl_hwcaps_subdirs[] = "x86-64-v4:x86-64-v3:x86-64-v2"; > > enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ > > @@ -25,40 +26,25 @@ enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ > > uint32_t > > _dl_hwcaps_subdirs_active (void) > > { > > + const struct cpu_features *cpu_features > > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > > + unsigned int isa_level = get_isa_level (cpu_features); > > int active = 0; > > > > /* Test in reverse preference order. */ > > > > /* x86-64-v2. */ > > - if (!(CPU_FEATURE_USABLE (CMPXCHG16B) > > - && CPU_FEATURE_USABLE (LAHF64_SAHF64) > > - && CPU_FEATURE_USABLE (POPCNT) > > - && CPU_FEATURE_USABLE (SSE3) > > - && CPU_FEATURE_USABLE (SSE4_1) > > - && CPU_FEATURE_USABLE (SSE4_2) > > - && CPU_FEATURE_USABLE (SSSE3))) > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) > > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > > ++active; > > > > /* x86-64-v3. */ > > - if (!(CPU_FEATURE_USABLE (AVX) > > - && CPU_FEATURE_USABLE (AVX2) > > - && CPU_FEATURE_USABLE (BMI1) > > - && CPU_FEATURE_USABLE (BMI2) > > - && CPU_FEATURE_USABLE (F16C) > > - && CPU_FEATURE_USABLE (FMA) > > - && CPU_FEATURE_USABLE (LZCNT) > > - && CPU_FEATURE_USABLE (MOVBE) > > - && CPU_FEATURE_USABLE (OSXSAVE))) > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) > > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > > ++active; > > > > - /* x86-64-v4. */ > > - if (!(CPU_FEATURE_USABLE (AVX512F) > > - && CPU_FEATURE_USABLE (AVX512BW) > > - && CPU_FEATURE_USABLE (AVX512CD) > > - && CPU_FEATURE_USABLE (AVX512DQ) > > - && CPU_FEATURE_USABLE (AVX512VL))) > > + /* x86-64-v4. */ > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) > > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > > ++active; > > > > Ok. > > > diff --git a/sysdeps/x86_64/tst-glibc-hwcaps.c b/sysdeps/x86_64/tst-glibc-hwcaps.c > > index 3075a8286d..a041a9047d 100644 > > --- a/sysdeps/x86_64/tst-glibc-hwcaps.c > > +++ b/sysdeps/x86_64/tst-glibc-hwcaps.c > > @@ -19,7 +19,8 @@ > > #include <stdio.h> > > #include <support/check.h> > > #include <sys/param.h> > > -#include <sys/platform/x86.h> > > +#include <elf.h> > > +#include <get-isa-level.h> > > > > extern int marker2 (void); > > extern int marker3 (void); > > @@ -31,35 +32,15 @@ compute_level (void) > > { > > const struct cpu_features *cpu_features > > = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > > - > > - if (!(CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) > > - && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) > > - && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) > > - && CPU_FEATURE_USABLE_P (cpu_features, MMX) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE2) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE3) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2))) > > - return 1; > > - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX2) > > - && CPU_FEATURE_USABLE_P (cpu_features, BMI1) > > - && CPU_FEATURE_USABLE_P (cpu_features, BMI2) > > - && CPU_FEATURE_USABLE_P (cpu_features, F16C) > > - && CPU_FEATURE_USABLE_P (cpu_features, FMA) > > - && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) > > - && CPU_FEATURE_USABLE_P (cpu_features, MOVBE) > > - && CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE))) > > - return 2; > > - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX512F) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL))) > > - return 3; > > - return 4; > > + unsigned int isa_level = get_isa_level (cpu_features); > > + > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) > > + return 1; > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) > > + return 2; > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) > > + return 3; > > + return 4; > > } > > > > static int > > > > Ok. Here is the updated patch. Tested under i686, x32 and x86-64 modes on x86-64-v2, x86-64-v3 and x86-64-v4 machines. OK for master? Thanks. -- H.J. [-- Attachment #2: 0001-x86-Support-GNU_PROPERTY_X86_ISA_1_V-234-marker-BZ-2.patch --] [-- Type: text/x-patch, Size: 40539 bytes --] From 6630e06f9bcb361177d46c277cf8e67cbd722429 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.tools@gmail.com> Date: Fri, 9 Oct 2020 06:06:56 -0700 Subject: [PATCH] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] GCC 11 supports -march=x86-64-v[234] to enable x86 micro-architecture ISA levels: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97250 and -mneeded to emit GNU_PROPERTY_X86_ISA_1_NEEDED property with GNU_PROPERTY_X86_ISA_1_V[234] marker: https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/13 Binutils support for GNU_PROPERTY_X86_ISA_1_V[234] marker were added by commit b0ab06937385e0ae25cebf1991787d64f439bf12 Author: H.J. Lu <hjl.tools@gmail.com> Date: Fri Oct 30 06:49:57 2020 -0700 x86: Support GNU_PROPERTY_X86_ISA_1_BASELINE marker and commit 32930e4edbc06bc6f10c435dbcc63131715df678 Author: H.J. Lu <hjl.tools@gmail.com> Date: Fri Oct 9 05:05:57 2020 -0700 x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker GNU_PROPERTY_X86_ISA_1_NEEDED property in x86 ELF binaries indicate the micro-architecture ISA level required to execute the binary. The marker must be added by programmers explicitly in one of 3 ways: 1. Pass -mneeded to GCC. 2. Add the marker in the linker inputs as this patch does. 3. Pass -z x86-64-v[234] to the linker. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] marker support to ld.so if binutils 2.32 or newer is used to build glibc: 1. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] markers to elf.h. 2. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] marker to abi-note.o based on the ISA level used to compile abi-note.o, assuming that the same ISA level is used to compile the whole glibc. 3. Add isa_1 to cpu_features to record the supported x86 ISA level. 4. Rename _dl_process_cet_property_note to _dl_process_property_note and add GNU_PROPERTY_X86_ISA_1_V[234] marker detection. 5. Update _rtld_main_check and _dl_open_check to check loaded objects with the incompatible ISA level. 6. Add a testcase to verify that dlopen an x86-64-v4 shared object fails on lesser platforms. 7. Use <get-isa-level.h> in dl-hwcaps-subdirs.c and tst-glibc-hwcaps.c. Tested under i686, x32 and x86-64 modes on x86-64-v2, x86-64-v3 and x86-64-v4 machines. Marked elf/tst-isa-level-1 with x86-64-v4, ran it on x86-64-v3 machine and got: [hjl@gnu-cfl-2 build-x86_64-linux]$ ./elf/tst-isa-level-1 ./elf/tst-isa-level-1: CPU ISA level is lower than required [hjl@gnu-cfl-2 build-x86_64-linux]$ --- config.h.in | 3 + elf/elf.h | 35 +++---- sysdeps/x86/Makefile | 30 ++++++ sysdeps/x86/abi-note.c | 29 ++++++ sysdeps/x86/configure | 79 ++++++++++++++ sysdeps/x86/configure.ac | 66 ++++++++++++ sysdeps/x86/cpu-features.c | 3 + sysdeps/x86/dl-cet.c | 12 ++- sysdeps/x86/dl-prop.h | 113 +++++++++++++++------ sysdeps/x86/get-isa-level.h | 66 ++++++++++++ sysdeps/x86/include/cpu-features.h | 2 + sysdeps/x86/isa-level.c | 97 ++++++++++++++++++ sysdeps/x86/link_map.h | 18 ++-- sysdeps/x86/tst-isa-level-1.c | 104 +++++++++++++++++++ sysdeps/x86/tst-isa-level-mod-1-baseline.c | 1 + sysdeps/x86/tst-isa-level-mod-1-v2.c | 1 + sysdeps/x86/tst-isa-level-mod-1-v3.c | 1 + sysdeps/x86/tst-isa-level-mod-1-v4.c | 1 + sysdeps/x86/tst-isa-level-mod-1.c | 25 +++++ sysdeps/x86_64/dl-hwcaps-subdirs.c | 30 ++---- sysdeps/x86_64/tst-glibc-hwcaps.c | 41 ++------ 21 files changed, 644 insertions(+), 113 deletions(-) create mode 100644 sysdeps/x86/abi-note.c create mode 100644 sysdeps/x86/get-isa-level.h create mode 100644 sysdeps/x86/isa-level.c create mode 100644 sysdeps/x86/tst-isa-level-1.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1-baseline.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v2.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v3.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v4.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1.c diff --git a/config.h.in b/config.h.in index eca2d66a2c..947feeff36 100644 --- a/config.h.in +++ b/config.h.in @@ -269,4 +269,7 @@ /* The default value of x86 CET control. */ #define DEFAULT_DL_X86_CET_CONTROL cet_elf_property +/* Define if x86 ISA level should be included in shared libraries. */ +#undef INCLUDE_X86_ISA_LEVEL + #endif diff --git a/elf/elf.h b/elf/elf.h index 4a9ae97dfc..9ebd052c8a 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -1324,31 +1324,26 @@ typedef struct /* The x86 instruction sets indicated by the corresponding bits are used in program. Their support in the hardware is optional. */ -#define GNU_PROPERTY_X86_ISA_1_USED 0xc0000000 +#define GNU_PROPERTY_X86_ISA_1_USED 0xc0010002 /* The x86 instruction sets indicated by the corresponding bits are used in program and they must be supported by the hardware. */ -#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0000001 +#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0008002 /* X86 processor-specific features used in program. */ #define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 -#define GNU_PROPERTY_X86_ISA_1_486 (1U << 0) -#define GNU_PROPERTY_X86_ISA_1_586 (1U << 1) -#define GNU_PROPERTY_X86_ISA_1_686 (1U << 2) -#define GNU_PROPERTY_X86_ISA_1_SSE (1U << 3) -#define GNU_PROPERTY_X86_ISA_1_SSE2 (1U << 4) -#define GNU_PROPERTY_X86_ISA_1_SSE3 (1U << 5) -#define GNU_PROPERTY_X86_ISA_1_SSSE3 (1U << 6) -#define GNU_PROPERTY_X86_ISA_1_SSE4_1 (1U << 7) -#define GNU_PROPERTY_X86_ISA_1_SSE4_2 (1U << 8) -#define GNU_PROPERTY_X86_ISA_1_AVX (1U << 9) -#define GNU_PROPERTY_X86_ISA_1_AVX2 (1U << 10) -#define GNU_PROPERTY_X86_ISA_1_AVX512F (1U << 11) -#define GNU_PROPERTY_X86_ISA_1_AVX512CD (1U << 12) -#define GNU_PROPERTY_X86_ISA_1_AVX512ER (1U << 13) -#define GNU_PROPERTY_X86_ISA_1_AVX512PF (1U << 14) -#define GNU_PROPERTY_X86_ISA_1_AVX512VL (1U << 15) -#define GNU_PROPERTY_X86_ISA_1_AVX512DQ (1U << 16) -#define GNU_PROPERTY_X86_ISA_1_AVX512BW (1U << 17) +/* GNU_PROPERTY_X86_ISA_1_BASELINE: CMOV, CX8 (cmpxchg8b), FPU (fld), + MMX, OSFXSR (fxsave), SCE (syscall), SSE and SSE2. */ +#define GNU_PROPERTY_X86_ISA_1_BASELINE (1U << 0) +/* GNU_PROPERTY_X86_ISA_1_V2: GNU_PROPERTY_X86_ISA_1_BASELINE, + CMPXCHG16B (cmpxchg16b), LAHF-SAHF (lahf), POPCNT (popcnt), SSE3, + SSSE3, SSE4.1 and SSE4.2. */ +#define GNU_PROPERTY_X86_ISA_1_V2 (1U << 1) +/* GNU_PROPERTY_X86_ISA_1_V3: GNU_PROPERTY_X86_ISA_1_V2, AVX, AVX2, BMI1, + BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE. */ +#define GNU_PROPERTY_X86_ISA_1_V3 (1U << 2) +/* GNU_PROPERTY_X86_ISA_1_V4: GNU_PROPERTY_X86_ISA_1_V3, AVX512F, + AVX512BW, AVX512CD, AVX512DQ and AVX512VL. */ +#define GNU_PROPERTY_X86_ISA_1_V4 (1U << 3) /* This indicates that all executable sections are compatible with IBT. */ diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index 081cc72e93..c814d5a195 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -9,6 +9,36 @@ sysdep_headers += sys/platform/x86.h tests += tst-get-cpu-features tst-get-cpu-features-static \ tst-cpu-features-cpuinfo tst-cpu-features-supports tests-static += tst-get-cpu-features-static +ifeq (yes,$(enable-x86-isa-level)) +tests += tst-isa-level-1 +modules-names += tst-isa-level-mod-1-baseline \ + tst-isa-level-mod-1-v2 \ + tst-isa-level-mod-1-v3 \ + tst-isa-level-mod-1-v4 \ + +# X86 ISA level baseline +CFLAGS-tst-isa-level-mod-1-baseline.c += -DINCLUDE_X86_ISA_LEVEL \ + -DISA_LEVEL=0x1 \ + -march=x86-64 +# X86 ISA level v2 +CFLAGS-tst-isa-level-mod-1-v2.c += -DINCLUDE_X86_ISA_LEVEL \ + -DISA_LEVEL=0x3 \ + -march=x86-64 +# X86 ISA level v3 +CFLAGS-tst-isa-level-mod-1-v3.c += -DINCLUDE_X86_ISA_LEVEL \ + -DISA_LEVEL=0x7 \ + -march=x86-64 +# X86 ISA level v4 +CFLAGS-tst-isa-level-mod-1-v4.c += -DINCLUDE_X86_ISA_LEVEL \ + -DISA_LEVEL=0xf \ + -march=x86-64 + +$(objpfx)tst-isa-level-1: $(libdl) +$(objpfx)tst-isa-level-1.out: $(objpfx)tst-isa-level-mod-1-baseline.so \ + $(objpfx)tst-isa-level-mod-1-v2.so \ + $(objpfx)tst-isa-level-mod-1-v3.so \ + $(objpfx)tst-isa-level-mod-1-v4.so +endif endif ifeq ($(subdir),math) diff --git a/sysdeps/x86/abi-note.c b/sysdeps/x86/abi-note.c new file mode 100644 index 0000000000..5140e6ab47 --- /dev/null +++ b/sysdeps/x86/abi-note.c @@ -0,0 +1,29 @@ +/* Special .init and .fini section support. x86-64 version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + In addition to the permissions in the GNU Lesser General Public + License, the Free Software Foundation gives you unlimited + permission to link the compiled version of this file with other + programs, and to distribute those programs without any restriction + coming from the use of this file. (The Lesser General Public + License restrictions do apply in other respects; for example, they + cover modification of the file, and distribution when not linked + into another program.) + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <isa-level.c> +#include <csu/abi-note.c> diff --git a/sysdeps/x86/configure b/sysdeps/x86/configure index 81cc4e80d6..5e32dc62b3 100644 --- a/sysdeps/x86/configure +++ b/sysdeps/x86/configure @@ -68,3 +68,82 @@ elif test $enable_cet = permissive; then fi config_vars="$config_vars enable-cet = $enable_cet" + +# Check if linker supports x86 ISA level. +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker x86 ISA level support" >&5 +$as_echo_n "checking for linker x86 ISA level support... " >&6; } +if ${libc_cv_include_x86_isa_level+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat > conftest1.S <<EOF +#ifdef __LP64__ +# define P2ALIGN 3 +#else +# define P2ALIGN 2 +#endif + .section ".note.gnu.property", "a" + .p2align P2ALIGN + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + /* NT_GNU_PROPERTY_TYPE_0 */ + .long 5 /* note type. */ +0: + .asciz "GNU" /* vendor name. */ +1: + .p2align P2ALIGN + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ + .long 0xc0008002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x1 +3: + .p2align P2ALIGN +4: +EOF +cat > conftest2.S <<EOF +#ifdef __LP64__ +# define P2ALIGN 3 +#else +# define P2ALIGN 2 +#endif + .section ".note.gnu.property", "a" + .p2align P2ALIGN + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + /* NT_GNU_PROPERTY_TYPE_0 */ + .long 5 /* note type. */ +0: + .asciz "GNU" /* vendor name. */ +1: + .p2align P2ALIGN + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ + .long 0xc0008002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x2 +3: + .p2align P2ALIGN +4: +EOF +libc_cv_include_x86_isa_level=no +if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS -nostartfiles -nostdlib -r -o conftest conftest1.S conftest2.S' + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 + (eval $ac_try) 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; }; then + count=`LC_ALL=C $READELF -n conftest | grep NT_GNU_PROPERTY_TYPE_0 | wc -l` + if test "$count" = 1; then + libc_cv_include_x86_isa_level=yes + fi +fi +rm -f conftest* +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_include_x86_isa_level" >&5 +$as_echo "$libc_cv_include_x86_isa_level" >&6; } +if test $libc_cv_include_x86_isa_level = yes; then + $as_echo "#define INCLUDE_X86_ISA_LEVEL 1" >>confdefs.h + +fi +config_vars="$config_vars +enable-x86-isa-level = $libc_cv_include_x86_isa_level" diff --git a/sysdeps/x86/configure.ac b/sysdeps/x86/configure.ac index 8f3e1191f6..f94088f377 100644 --- a/sysdeps/x86/configure.ac +++ b/sysdeps/x86/configure.ac @@ -43,3 +43,69 @@ elif test $enable_cet = permissive; then AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_permissive) fi LIBC_CONFIG_VAR([enable-cet], [$enable_cet]) + +# Check if linker supports x86 ISA level. +AC_CACHE_CHECK([for linker x86 ISA level support], + libc_cv_include_x86_isa_level, [dnl +cat > conftest1.S <<EOF +#ifdef __LP64__ +# define P2ALIGN 3 +#else +# define P2ALIGN 2 +#endif + .section ".note.gnu.property", "a" + .p2align P2ALIGN + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + /* NT_GNU_PROPERTY_TYPE_0 */ + .long 5 /* note type. */ +0: + .asciz "GNU" /* vendor name. */ +1: + .p2align P2ALIGN + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ + .long 0xc0008002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x1 +3: + .p2align P2ALIGN +4: +EOF +cat > conftest2.S <<EOF +#ifdef __LP64__ +# define P2ALIGN 3 +#else +# define P2ALIGN 2 +#endif + .section ".note.gnu.property", "a" + .p2align P2ALIGN + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + /* NT_GNU_PROPERTY_TYPE_0 */ + .long 5 /* note type. */ +0: + .asciz "GNU" /* vendor name. */ +1: + .p2align P2ALIGN + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ + .long 0xc0008002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x2 +3: + .p2align P2ALIGN +4: +EOF +libc_cv_include_x86_isa_level=no +if AC_TRY_COMMAND(${CC-cc} $CFLAGS $CPPFLAGS -nostartfiles -nostdlib -r -o conftest conftest1.S conftest2.S); then + count=`LC_ALL=C $READELF -n conftest | grep NT_GNU_PROPERTY_TYPE_0 | wc -l` + if test "$count" = 1; then + libc_cv_include_x86_isa_level=yes + fi +fi +rm -f conftest*]) +if test $libc_cv_include_x86_isa_level = yes; then + AC_DEFINE(INCLUDE_X86_ISA_LEVEL) +fi +LIBC_CONFIG_VAR([enable-x86-isa-level], [$libc_cv_include_x86_isa_level]) diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c index 529c586193..e7da682a2e 100644 --- a/sysdeps/x86/cpu-features.c +++ b/sysdeps/x86/cpu-features.c @@ -19,6 +19,7 @@ #include <cpuid.h> #include <dl-hwcap.h> #include <libc-pointer-arith.h> +#include <get-isa-level.h> #if IS_IN (libc) && !defined SHARED # include <assert.h> # include <unistd.h> @@ -290,6 +291,8 @@ update_usable (struct cpu_features *cpu_features) CPU_FEATURE_SET_USABLE (cpu_features, KL); CPU_FEATURE_SET_USABLE (cpu_features, WIDE_KL); } + + cpu_features->isa_1 = get_isa_level (cpu_features); } static void diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c index b4910f7634..ed7fd6d10d 100644 --- a/sysdeps/x86/dl-cet.c +++ b/sysdeps/x86/dl-cet.c @@ -76,10 +76,12 @@ dl_cet_check (struct link_map *m, const char *program) */ enable_ibt &= (HAS_CPU_FEATURE (IBT) && (enable_ibt_type == cet_always_on - || (m->l_cet & lc_ibt) != 0)); + || (m->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0)); enable_shstk &= (HAS_CPU_FEATURE (SHSTK) && (enable_shstk_type == cet_always_on - || (m->l_cet & lc_shstk) != 0)); + || (m->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)); } /* ld.so is CET-enabled by kernel. But shared objects may not @@ -111,7 +113,8 @@ dl_cet_check (struct link_map *m, const char *program) /* IBT is enabled only if it is enabled in executable as well as all shared objects. */ enable_ibt &= (enable_ibt_type == cet_always_on - || (l->l_cet & lc_ibt) != 0); + || (l->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0); if (!found_ibt_legacy && enable_ibt != ibt_enabled) { found_ibt_legacy = true; @@ -121,7 +124,8 @@ dl_cet_check (struct link_map *m, const char *program) /* SHSTK is enabled only if it is enabled in executable as well as all shared objects. */ enable_shstk &= (enable_shstk_type == cet_always_on - || (l->l_cet & lc_shstk) != 0); + || (l->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0); if (enable_shstk != shstk_enabled) { found_shstk_legacy = true; diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h index ce1667dc7a..56bd020b3c 100644 --- a/sysdeps/x86/dl-prop.h +++ b/sysdeps/x86/dl-prop.h @@ -19,14 +19,54 @@ #ifndef _DL_PROP_H #define _DL_PROP_H +#include <libintl.h> + extern void _dl_cet_check (struct link_map *, const char *) attribute_hidden; extern void _dl_cet_open_check (struct link_map *) attribute_hidden; +static void +dl_isa_level_check (struct link_map *m, const char *program) +{ + const struct cpu_features *cpu_features = __get_cpu_features (); + unsigned int i; + struct link_map *l; + + i = m->l_searchlist.r_nlist; + while (i-- > 0) + { + /* Check each shared object to see if ISA level is compatible. */ + l = m->l_initfini[i]; + + /* Skip ISA level check if functions have been executed. */ + if (l->l_init_called) + continue; + +#ifdef SHARED + /* Skip ISA level check for ld.so since ld.so won't run if its ISA + level is higher than CPU. */ + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) + continue; +#endif + + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) + != l->l_x86_isa_1_needed) + { + if (program) + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", + *l->l_name != '\0' ? l->l_name : program); + else + _dl_signal_error (0, l->l_name, "dlopen", + N_("CPU ISA level is lower than required")); + } + } +} + static inline void __attribute__ ((always_inline)) _rtld_main_check (struct link_map *m, const char *program) { + dl_isa_level_check (m, program); #if CET_ENABLED _dl_cet_check (m, program); #endif @@ -35,20 +75,18 @@ _rtld_main_check (struct link_map *m, const char *program) static inline void __attribute__ ((always_inline)) _dl_open_check (struct link_map *m) { + dl_isa_level_check (m, NULL); #if CET_ENABLED _dl_cet_open_check (m); #endif } static inline void __attribute__ ((unused)) -_dl_process_cet_property_note (struct link_map *l, - const ElfW(Nhdr) *note, - const ElfW(Addr) size, - const ElfW(Addr) align) +_dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, + const ElfW(Addr) size, const ElfW(Addr) align) { -#if CET_ENABLED /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ - if (l->l_cet != lc_unknown) + if (l->l_property != lc_property_unknown) return; /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in @@ -59,7 +97,8 @@ _dl_process_cet_property_note (struct link_map *l, const ElfW(Addr) start = (ElfW(Addr)) note; - unsigned int feature_1 = 0; + unsigned int feature_1_and = 0; + unsigned int isa_1_needed = 0; unsigned int last_type = 0; while ((ElfW(Addr)) (note + 1) - start < size) @@ -71,11 +110,11 @@ _dl_process_cet_property_note (struct link_map *l, { /* Stop if we see more than one GNU property note which may be generated by the older linker. */ - if (l->l_cet != lc_unknown) + if (l->l_property != lc_property_unknown) return; - /* Check CET status now. */ - l->l_cet = lc_none; + /* Check CET status and ISA levels now. */ + l->l_property = lc_property_none; /* Check for invalid property. */ if (note->n_descsz < 8 @@ -101,26 +140,37 @@ _dl_process_cet_property_note (struct link_map *l, last_type = type; - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) + if (type == GNU_PROPERTY_X86_FEATURE_1_AND + || type == GNU_PROPERTY_X86_ISA_1_NEEDED) { - /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4 - bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND, - we stop the search regardless if its size is correct - or not. There is no point to continue if this note - is ill-formed. */ + /* The sizes of types which we are searching for are + 4 bytes. There is no point to continue if this + note is ill-formed. */ if (datasz != 4) return; - feature_1 = *(unsigned int *) ptr; - - /* Keep searching for the next GNU property note - generated by the older linker. */ - break; + /* NB: Stop the scan only after seeing all types which + we are searching for. */ + _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > + GNU_PROPERTY_X86_FEATURE_1_AND), + "GNU_PROPERTY_X86_ISA_1_NEEDED > " + "GNU_PROPERTY_X86_FEATURE_1_AND"); + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) + feature_1_and = *(unsigned int *) ptr; + else + { + isa_1_needed = *(unsigned int *) ptr; + + /* Keep searching for the next GNU property note + generated by the older linker. */ + break; + } } - else if (type > GNU_PROPERTY_X86_FEATURE_1_AND) + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) { - /* Stop since property type is in ascending order. */ - return; + /* Stop the scan since property type is in ascending + order. */ + break; } /* Check the next property item. */ @@ -137,18 +187,21 @@ _dl_process_cet_property_note (struct link_map *l, } /* We get here only if there is one or no GNU property note. */ - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT)) - l->l_cet |= lc_ibt; - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) - l->l_cet |= lc_shstk; -#endif + if (isa_1_needed != 0 || feature_1_and != 0) + { + l->l_property = lc_property_valid; + l->l_x86_isa_1_needed = isa_1_needed; + l->l_x86_feature_1_and = feature_1_and; + } + else + l->l_property = lc_property_none; } static inline void __attribute__ ((unused)) _dl_process_pt_note (struct link_map *l, int fd, const ElfW(Phdr) *ph) { const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); - _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align); + _dl_process_property_note (l, note, ph->p_memsz, ph->p_align); } static inline int __attribute__ ((always_inline)) diff --git a/sysdeps/x86/get-isa-level.h b/sysdeps/x86/get-isa-level.h new file mode 100644 index 0000000000..a86e1e8941 --- /dev/null +++ b/sysdeps/x86/get-isa-level.h @@ -0,0 +1,66 @@ +/* Get x86 ISA level. + This file is part of the GNU C Library. + Copyright (C) 2020 Free Software Foundation, Inc. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <sys/platform/x86.h> + +/* Get GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] + ISA level. */ + +static unsigned int +get_isa_level (const struct cpu_features *cpu_features) +{ + unsigned int isa_level = 0; + + if (CPU_FEATURE_USABLE_P (cpu_features, CMOV) + && CPU_FEATURE_USABLE_P (cpu_features, CX8) + && CPU_FEATURE_CPU_P (cpu_features, FPU) + && CPU_FEATURE_USABLE_P (cpu_features, FXSR) + && CPU_FEATURE_USABLE_P (cpu_features, MMX) + && CPU_FEATURE_USABLE_P (cpu_features, SSE) + && CPU_FEATURE_USABLE_P (cpu_features, SSE2)) + { + isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE; + if (CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) + && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) + && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) + && CPU_FEATURE_USABLE_P (cpu_features, SSE3) + && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2)) + { + isa_level |= GNU_PROPERTY_X86_ISA_1_V2; + if (CPU_FEATURE_USABLE_P (cpu_features, AVX) + && CPU_FEATURE_USABLE_P (cpu_features, AVX2) + && CPU_FEATURE_USABLE_P (cpu_features, F16C) + && CPU_FEATURE_USABLE_P (cpu_features, FMA) + && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) + && CPU_FEATURE_USABLE_P (cpu_features, MOVBE)) + { + isa_level |= GNU_PROPERTY_X86_ISA_1_V3; + if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)) + isa_level |= GNU_PROPERTY_X86_ISA_1_V4; + } + } + } + + return isa_level; +} diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h index ffe300c931..99e7ee08cf 100644 --- a/sysdeps/x86/include/cpu-features.h +++ b/sysdeps/x86/include/cpu-features.h @@ -123,6 +123,8 @@ struct cpu_features struct cpu_features_basic basic; struct cpuid_features features[COMMON_CPUID_INDEX_MAX]; unsigned int preferred[PREFERRED_FEATURE_INDEX_MAX]; + /* X86 micro-architecture ISA levels. */ + unsigned int isa_1; /* The state size for XSAVEC or XSAVE. The type must be unsigned long int so that we use diff --git a/sysdeps/x86/isa-level.c b/sysdeps/x86/isa-level.c new file mode 100644 index 0000000000..aaf524cb56 --- /dev/null +++ b/sysdeps/x86/isa-level.c @@ -0,0 +1,97 @@ +/* ELF program property for x86 ISA level. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + In addition to the permissions in the GNU Lesser General Public + License, the Free Software Foundation gives you unlimited + permission to link the compiled version of this file with other + programs, and to distribute those programs without any restriction + coming from the use of this file. (The Lesser General Public + License restrictions do apply in other respects; for example, they + cover modification of the file, and distribution when not linked + into another program.) + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <elf.h> + +/* ELF program property for x86 ISA level. */ +#ifdef INCLUDE_X86_ISA_LEVEL +# if defined __x86_64__ || defined __FXSR__ || !defined _SOFT_FLOAT \ + || defined __MMX__ || defined __SSE__ || defined __SSE2__ +# define ISA_BASELINE GNU_PROPERTY_X86_ISA_1_BASELINE +# else +# define ISA_BASELINE 0 +# endif + +# if defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 \ + || (defined __x86_64__ && defined __LAHF_SAHF__) \ + || defined __POPCNT__ || defined __SSE3__ \ + || defined __SSSE3__ || defined __SSE4_1__ || defined __SSE4_2__ +# define ISA_V2 GNU_PROPERTY_X86_ISA_1_V2 +# else +# define ISA_V2 0 +# endif + +# if defined __AVX__ || defined __AVX2__ || defined __F16C__ \ + || defined __FMA__ || defined __LZCNT__ || defined __MOVBE__ \ + || defined __XSAVE__ +# define ISA_V3 GNU_PROPERTY_X86_ISA_1_V3 +# else +# define ISA_V3 0 +# endif + +# if defined __AVX512F__ || defined __AVX512BW__ || defined __AVX512CD__ \ + || defined __AVX512DQ__ || defined __AVX512VL__ +# define ISA_V4 GNU_PROPERTY_X86_ISA_1_V4 +# else +# define ISA_V4 0 +# endif + +# ifndef ISA_LEVEL +# define ISA_LEVEL (ISA_BASELINE | ISA_V2 | ISA_V3 | ISA_V4) +# endif + +# if ISA_LEVEL +# ifdef __LP64__ +# define PROPERTY_ALIGN 3 +# else +# define PROPERTY_ALIGN 2 +# endif + +# define note_stringify(arg) note_stringify_1(arg) +# define note_stringify_1(arg) #arg + +asm(".pushsection \".note.gnu.property\",\"a\",@note\n" +" .p2align " note_stringify (PROPERTY_ALIGN) + /* name length. */ +"\n .long 1f - 0f\n" + /* data length. */ +" .long 4f - 1f\n" + /* note type: NT_GNU_PROPERTY_TYPE_0. */ +" .long " note_stringify (NT_GNU_PROPERTY_TYPE_0) + /* vendor name. */ +"\n0: .asciz \"GNU\"\n" +"1: .p2align " note_stringify (PROPERTY_ALIGN) + /* pr_type: GNU_PROPERTY_X86_ISA_1_NEEDED. */ +"\n .long " note_stringify (GNU_PROPERTY_X86_ISA_1_NEEDED) + /* pr_datasz. */ +"\n .long 3f - 2f\n" + /* GNU_PROPERTY_X86_ISA_1_V[234]. */ +"2:\n .long " note_stringify (ISA_LEVEL) +"\n3:\n .p2align " note_stringify (PROPERTY_ALIGN) +"\n4:\n .popsection"); +# endif /* ISA_LEVEL */ +#endif /* INCLUDE_X86_ISA_LEVEL */ diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h index 8074739b47..4c46a25f83 100644 --- a/sysdeps/x86/link_map.h +++ b/sysdeps/x86/link_map.h @@ -16,12 +16,16 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ -/* If this object is enabled with CET. */ +/* if this object has GNU property. */ enum { - lc_unknown = 0, /* Unknown CET status. */ - lc_none = 1 << 0, /* Not enabled with CET. */ - lc_ibt = 1 << 1, /* Enabled with IBT. */ - lc_shstk = 1 << 2, /* Enabled with STSHK. */ - lc_ibt_and_shstk = lc_ibt | lc_shstk /* Enabled with both. */ - } l_cet:3; + lc_property_unknown = 0, /* Unknown property status. */ + lc_property_none = 1 << 0, /* No property. */ + lc_property_valid = 1 << 1 /* Has valid property. */ + } l_property:2; + +/* GNU_PROPERTY_X86_FEATURE_1_AND of this object. */ +unsigned int l_x86_feature_1_and; + +/* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */ +unsigned int l_x86_isa_1_needed; diff --git a/sysdeps/x86/tst-isa-level-1.c b/sysdeps/x86/tst-isa-level-1.c new file mode 100644 index 0000000000..dc31d71ad2 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-1.c @@ -0,0 +1,104 @@ +/* Check ISA level on dlopened shared object. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <elf.h> +#include <get-isa-level.h> +#include <support/xdlfcn.h> +#include <support/check.h> +#include <support/test-driver.h> + +static void +do_test_1 (const char *modname, bool fail) +{ + int (*fp) (void); + void *h; + + h = dlopen (modname, RTLD_LAZY); + if (h == NULL) + { + const char *err = dlerror (); + if (fail) + { + if (strstr (err, "CPU ISA level is lower than required") == NULL) + FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err); + + return; + } + + FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err); + } + + if (fail) + FAIL_EXIT1 ("dlopen '%s' should have failed\n", modname); + + fp = xdlsym (h, "test"); + + if (fp () != 0) + FAIL_EXIT1 ("test () != 0\n"); + + dlclose (h); +} + +static int +do_test (void) +{ + const struct cpu_features *cpu_features + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); + unsigned int isa_level = get_isa_level (cpu_features); + bool has_isa_baseline = ((isa_level & GNU_PROPERTY_X86_ISA_1_BASELINE) + == GNU_PROPERTY_X86_ISA_1_BASELINE); + bool has_isa_v2 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V2) + == GNU_PROPERTY_X86_ISA_1_V2); + bool has_isa_v3 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V3) + == GNU_PROPERTY_X86_ISA_1_V3); + bool has_isa_v4 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) + == GNU_PROPERTY_X86_ISA_1_V4); + + if (!has_isa_baseline) + { + do_test_1 ("tst-isa-level-mod-1-baseline.so", true); + return EXIT_SUCCESS; + } + + do_test_1 ("tst-isa-level-mod-1-baseline.so", false); + + /* Skip on x86-64-v4 platforms since dlopen v4 module always works. */ + if (has_isa_v4) + return EXIT_UNSUPPORTED; + + do_test_1 ("tst-isa-level-mod-1-v4.so", true); + + /* Skip on x86-64-v3 platforms since dlopen v3 module always works. */ + if (has_isa_v3) + return EXIT_SUCCESS; + + do_test_1 ("tst-isa-level-mod-1-v3.so", true); + + /* Skip on x86-64-v2 platforms since dlopen v2 module always works. */ + if (has_isa_v2) + return EXIT_SUCCESS; + + do_test_1 ("tst-isa-level-mod-1-v2.so", true); + + return EXIT_SUCCESS; +} + +#include <support/test-driver.c> diff --git a/sysdeps/x86/tst-isa-level-mod-1-baseline.c b/sysdeps/x86/tst-isa-level-mod-1-baseline.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1-baseline.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1-v2.c b/sysdeps/x86/tst-isa-level-mod-1-v2.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1-v2.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1-v3.c b/sysdeps/x86/tst-isa-level-mod-1-v3.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1-v3.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1-v4.c b/sysdeps/x86/tst-isa-level-mod-1-v4.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1-v4.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1.c b/sysdeps/x86/tst-isa-level-mod-1.c new file mode 100644 index 0000000000..40a2e1d99f --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1.c @@ -0,0 +1,25 @@ +/* Check ISA level on dlopened shared object. + Copyright (C) 2019-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <isa-level.c> + +int +test (void) +{ + return 0; +} diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c index 0aaa635a00..e030534f1b 100644 --- a/sysdeps/x86_64/dl-hwcaps-subdirs.c +++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c @@ -18,6 +18,7 @@ #include <dl-hwcaps.h> #include <cpu-features.h> +#include <get-isa-level.h> const char _dl_hwcaps_subdirs[] = "x86-64-v4:x86-64-v3:x86-64-v2"; enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ @@ -25,40 +26,25 @@ enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ uint32_t _dl_hwcaps_subdirs_active (void) { + const struct cpu_features *cpu_features + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); + unsigned int isa_level = get_isa_level (cpu_features); int active = 0; /* Test in reverse preference order. */ /* x86-64-v2. */ - if (!(CPU_FEATURE_USABLE (CMPXCHG16B) - && CPU_FEATURE_USABLE (LAHF64_SAHF64) - && CPU_FEATURE_USABLE (POPCNT) - && CPU_FEATURE_USABLE (SSE3) - && CPU_FEATURE_USABLE (SSE4_1) - && CPU_FEATURE_USABLE (SSE4_2) - && CPU_FEATURE_USABLE (SSSE3))) + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; /* x86-64-v3. */ - if (!(CPU_FEATURE_USABLE (AVX) - && CPU_FEATURE_USABLE (AVX2) - && CPU_FEATURE_USABLE (BMI1) - && CPU_FEATURE_USABLE (BMI2) - && CPU_FEATURE_USABLE (F16C) - && CPU_FEATURE_USABLE (FMA) - && CPU_FEATURE_USABLE (LZCNT) - && CPU_FEATURE_USABLE (MOVBE) - && CPU_FEATURE_USABLE (OSXSAVE))) + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; - /* x86-64-v4. */ - if (!(CPU_FEATURE_USABLE (AVX512F) - && CPU_FEATURE_USABLE (AVX512BW) - && CPU_FEATURE_USABLE (AVX512CD) - && CPU_FEATURE_USABLE (AVX512DQ) - && CPU_FEATURE_USABLE (AVX512VL))) + /* x86-64-v4. */ + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; diff --git a/sysdeps/x86_64/tst-glibc-hwcaps.c b/sysdeps/x86_64/tst-glibc-hwcaps.c index 8bdcb91db0..37b61d3c84 100644 --- a/sysdeps/x86_64/tst-glibc-hwcaps.c +++ b/sysdeps/x86_64/tst-glibc-hwcaps.c @@ -19,7 +19,8 @@ #include <stdio.h> #include <support/check.h> #include <sys/param.h> -#include <sys/platform/x86.h> +#include <elf.h> +#include <get-isa-level.h> extern int marker2 (void); extern int marker3 (void); @@ -31,35 +32,15 @@ compute_level (void) { const struct cpu_features *cpu_features = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); - - if (!(CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) - && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) - && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) - && CPU_FEATURE_USABLE_P (cpu_features, MMX) - && CPU_FEATURE_USABLE_P (cpu_features, SSE) - && CPU_FEATURE_USABLE_P (cpu_features, SSE2) - && CPU_FEATURE_USABLE_P (cpu_features, SSE3) - && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2))) - return 1; - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX) - && CPU_FEATURE_USABLE_P (cpu_features, AVX2) - && CPU_FEATURE_USABLE_P (cpu_features, BMI1) - && CPU_FEATURE_USABLE_P (cpu_features, BMI2) - && CPU_FEATURE_USABLE_P (cpu_features, F16C) - && CPU_FEATURE_USABLE_P (cpu_features, FMA) - && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) - && CPU_FEATURE_USABLE_P (cpu_features, MOVBE) - && CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE))) - return 2; - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX512F) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL))) - return 3; - return 4; + unsigned int isa_level = get_isa_level (cpu_features); + + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) + return 1; + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) + return 2; + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) + return 3; + return 4; } static int -- 2.29.2 ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V6 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] 2021-01-06 17:11 ` V6 " H.J. Lu @ 2021-01-07 20:09 ` Adhemerval Zanella 2021-01-07 20:58 ` H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Adhemerval Zanella @ 2021-01-07 20:09 UTC (permalink / raw) To: H.J. Lu; +Cc: GNU C Library, Florian Weimer On 06/01/2021 14:11, H.J. Lu wrote: > Here is the updated patch. Tested under i686, x32 and x86-64 modes > on x86-64-v2, x86-64-v3 and x86-64-v4 machines. > > OK for master? > > Thanks. > LGTM with just two clarification below. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org> > From 6630e06f9bcb361177d46c277cf8e67cbd722429 Mon Sep 17 00:00:00 2001 > From: "H.J. Lu" <hjl.tools@gmail.com> > Date: Fri, 9 Oct 2020 06:06:56 -0700 > Subject: [PATCH] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] > > GCC 11 supports -march=x86-64-v[234] to enable x86 micro-architecture ISA > levels: > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97250 > > and -mneeded to emit GNU_PROPERTY_X86_ISA_1_NEEDED property with > GNU_PROPERTY_X86_ISA_1_V[234] marker: > > https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/13 > > Binutils support for GNU_PROPERTY_X86_ISA_1_V[234] marker were added by > > commit b0ab06937385e0ae25cebf1991787d64f439bf12 > Author: H.J. Lu <hjl.tools@gmail.com> > Date: Fri Oct 30 06:49:57 2020 -0700 > > x86: Support GNU_PROPERTY_X86_ISA_1_BASELINE marker > > and > > commit 32930e4edbc06bc6f10c435dbcc63131715df678 > Author: H.J. Lu <hjl.tools@gmail.com> > Date: Fri Oct 9 05:05:57 2020 -0700 > > x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker > > GNU_PROPERTY_X86_ISA_1_NEEDED property in x86 ELF binaries indicate the > micro-architecture ISA level required to execute the binary. The marker > must be added by programmers explicitly in one of 3 ways: > > 1. Pass -mneeded to GCC. > 2. Add the marker in the linker inputs as this patch does. > 3. Pass -z x86-64-v[234] to the linker. > > Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > marker support to ld.so if binutils 2.32 or newer is used to build glibc: > > 1. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > markers to elf.h. > 2. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > marker to abi-note.o based on the ISA level used to compile abi-note.o, > assuming that the same ISA level is used to compile the whole glibc. > 3. Add isa_1 to cpu_features to record the supported x86 ISA level. > 4. Rename _dl_process_cet_property_note to _dl_process_property_note and > add GNU_PROPERTY_X86_ISA_1_V[234] marker detection. > 5. Update _rtld_main_check and _dl_open_check to check loaded objects > with the incompatible ISA level. > 6. Add a testcase to verify that dlopen an x86-64-v4 shared object fails > on lesser platforms. > 7. Use <get-isa-level.h> in dl-hwcaps-subdirs.c and tst-glibc-hwcaps.c. > > Tested under i686, x32 and x86-64 modes on x86-64-v2, x86-64-v3 and > x86-64-v4 machines. > > Marked elf/tst-isa-level-1 with x86-64-v4, ran it on x86-64-v3 machine > and got: > > [hjl@gnu-cfl-2 build-x86_64-linux]$ ./elf/tst-isa-level-1 > ./elf/tst-isa-level-1: CPU ISA level is lower than required > [hjl@gnu-cfl-2 build-x86_64-linux]$ > --- > config.h.in | 3 + > elf/elf.h | 35 +++---- > sysdeps/x86/Makefile | 30 ++++++ > sysdeps/x86/abi-note.c | 29 ++++++ > sysdeps/x86/configure | 79 ++++++++++++++ > sysdeps/x86/configure.ac | 66 ++++++++++++ > sysdeps/x86/cpu-features.c | 3 + > sysdeps/x86/dl-cet.c | 12 ++- > sysdeps/x86/dl-prop.h | 113 +++++++++++++++------ > sysdeps/x86/get-isa-level.h | 66 ++++++++++++ > sysdeps/x86/include/cpu-features.h | 2 + > sysdeps/x86/isa-level.c | 97 ++++++++++++++++++ > sysdeps/x86/link_map.h | 18 ++-- > sysdeps/x86/tst-isa-level-1.c | 104 +++++++++++++++++++ > sysdeps/x86/tst-isa-level-mod-1-baseline.c | 1 + > sysdeps/x86/tst-isa-level-mod-1-v2.c | 1 + > sysdeps/x86/tst-isa-level-mod-1-v3.c | 1 + > sysdeps/x86/tst-isa-level-mod-1-v4.c | 1 + > sysdeps/x86/tst-isa-level-mod-1.c | 25 +++++ > sysdeps/x86_64/dl-hwcaps-subdirs.c | 30 ++---- > sysdeps/x86_64/tst-glibc-hwcaps.c | 41 ++------ > 21 files changed, 644 insertions(+), 113 deletions(-) > create mode 100644 sysdeps/x86/abi-note.c > create mode 100644 sysdeps/x86/get-isa-level.h > create mode 100644 sysdeps/x86/isa-level.c > create mode 100644 sysdeps/x86/tst-isa-level-1.c > create mode 100644 sysdeps/x86/tst-isa-level-mod-1-baseline.c > create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v2.c > create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v3.c > create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v4.c > create mode 100644 sysdeps/x86/tst-isa-level-mod-1.c > > diff --git a/config.h.in b/config.h.in > index eca2d66a2c..947feeff36 100644 > --- a/config.h.in > +++ b/config.h.in > @@ -269,4 +269,7 @@ > /* The default value of x86 CET control. */ > #define DEFAULT_DL_X86_CET_CONTROL cet_elf_property > > +/* Define if x86 ISA level should be included in shared libraries. */ > +#undef INCLUDE_X86_ISA_LEVEL > + > #endif > diff --git a/elf/elf.h b/elf/elf.h Ok. > index 4a9ae97dfc..9ebd052c8a 100644 > --- a/elf/elf.h > +++ b/elf/elf.h > @@ -1324,31 +1324,26 @@ typedef struct > > /* The x86 instruction sets indicated by the corresponding bits are > used in program. Their support in the hardware is optional. */ > -#define GNU_PROPERTY_X86_ISA_1_USED 0xc0000000 > +#define GNU_PROPERTY_X86_ISA_1_USED 0xc0010002 > /* The x86 instruction sets indicated by the corresponding bits are > used in program and they must be supported by the hardware. */ > -#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0000001 > +#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0008002 > /* X86 processor-specific features used in program. */ > #define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 > > -#define GNU_PROPERTY_X86_ISA_1_486 (1U << 0) > -#define GNU_PROPERTY_X86_ISA_1_586 (1U << 1) > -#define GNU_PROPERTY_X86_ISA_1_686 (1U << 2) > -#define GNU_PROPERTY_X86_ISA_1_SSE (1U << 3) > -#define GNU_PROPERTY_X86_ISA_1_SSE2 (1U << 4) > -#define GNU_PROPERTY_X86_ISA_1_SSE3 (1U << 5) > -#define GNU_PROPERTY_X86_ISA_1_SSSE3 (1U << 6) > -#define GNU_PROPERTY_X86_ISA_1_SSE4_1 (1U << 7) > -#define GNU_PROPERTY_X86_ISA_1_SSE4_2 (1U << 8) > -#define GNU_PROPERTY_X86_ISA_1_AVX (1U << 9) > -#define GNU_PROPERTY_X86_ISA_1_AVX2 (1U << 10) > -#define GNU_PROPERTY_X86_ISA_1_AVX512F (1U << 11) > -#define GNU_PROPERTY_X86_ISA_1_AVX512CD (1U << 12) > -#define GNU_PROPERTY_X86_ISA_1_AVX512ER (1U << 13) > -#define GNU_PROPERTY_X86_ISA_1_AVX512PF (1U << 14) > -#define GNU_PROPERTY_X86_ISA_1_AVX512VL (1U << 15) > -#define GNU_PROPERTY_X86_ISA_1_AVX512DQ (1U << 16) > -#define GNU_PROPERTY_X86_ISA_1_AVX512BW (1U << 17) > +/* GNU_PROPERTY_X86_ISA_1_BASELINE: CMOV, CX8 (cmpxchg8b), FPU (fld), > + MMX, OSFXSR (fxsave), SCE (syscall), SSE and SSE2. */ > +#define GNU_PROPERTY_X86_ISA_1_BASELINE (1U << 0) > +/* GNU_PROPERTY_X86_ISA_1_V2: GNU_PROPERTY_X86_ISA_1_BASELINE, > + CMPXCHG16B (cmpxchg16b), LAHF-SAHF (lahf), POPCNT (popcnt), SSE3, > + SSSE3, SSE4.1 and SSE4.2. */ > +#define GNU_PROPERTY_X86_ISA_1_V2 (1U << 1) > +/* GNU_PROPERTY_X86_ISA_1_V3: GNU_PROPERTY_X86_ISA_1_V2, AVX, AVX2, BMI1, > + BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE. */ > +#define GNU_PROPERTY_X86_ISA_1_V3 (1U << 2) > +/* GNU_PROPERTY_X86_ISA_1_V4: GNU_PROPERTY_X86_ISA_1_V3, AVX512F, > + AVX512BW, AVX512CD, AVX512DQ and AVX512VL. */ > +#define GNU_PROPERTY_X86_ISA_1_V4 (1U << 3) > > /* This indicates that all executable sections are compatible with > IBT. */ Ok. > diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile > index 081cc72e93..c814d5a195 100644 > --- a/sysdeps/x86/Makefile > +++ b/sysdeps/x86/Makefile > @@ -9,6 +9,36 @@ sysdep_headers += sys/platform/x86.h > tests += tst-get-cpu-features tst-get-cpu-features-static \ > tst-cpu-features-cpuinfo tst-cpu-features-supports > tests-static += tst-get-cpu-features-static > +ifeq (yes,$(enable-x86-isa-level)) > +tests += tst-isa-level-1 > +modules-names += tst-isa-level-mod-1-baseline \ > + tst-isa-level-mod-1-v2 \ > + tst-isa-level-mod-1-v3 \ > + tst-isa-level-mod-1-v4 \ > + > +# X86 ISA level baseline > +CFLAGS-tst-isa-level-mod-1-baseline.c += -DINCLUDE_X86_ISA_LEVEL \ > + -DISA_LEVEL=0x1 \ > + -march=x86-64 > +# X86 ISA level v2 > +CFLAGS-tst-isa-level-mod-1-v2.c += -DINCLUDE_X86_ISA_LEVEL \ > + -DISA_LEVEL=0x3 \ > + -march=x86-64 > +# X86 ISA level v3 > +CFLAGS-tst-isa-level-mod-1-v3.c += -DINCLUDE_X86_ISA_LEVEL \ > + -DISA_LEVEL=0x7 \ > + -march=x86-64 > +# X86 ISA level v4 > +CFLAGS-tst-isa-level-mod-1-v4.c += -DINCLUDE_X86_ISA_LEVEL \ > + -DISA_LEVEL=0xf \ > + -march=x86-64 > + > +$(objpfx)tst-isa-level-1: $(libdl) > +$(objpfx)tst-isa-level-1.out: $(objpfx)tst-isa-level-mod-1-baseline.so \ > + $(objpfx)tst-isa-level-mod-1-v2.so \ > + $(objpfx)tst-isa-level-mod-1-v3.so \ > + $(objpfx)tst-isa-level-mod-1-v4.so > +endif > endif > > ifeq ($(subdir),math) Ok. > diff --git a/sysdeps/x86/abi-note.c b/sysdeps/x86/abi-note.c > new file mode 100644 > index 0000000000..5140e6ab47 > --- /dev/null > +++ b/sysdeps/x86/abi-note.c > @@ -0,0 +1,29 @@ > +/* Special .init and .fini section support. x86-64 version. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + In addition to the permissions in the GNU Lesser General Public > + License, the Free Software Foundation gives you unlimited > + permission to link the compiled version of this file with other > + programs, and to distribute those programs without any restriction > + coming from the use of this file. (The Lesser General Public > + License restrictions do apply in other respects; for example, they > + cover modification of the file, and distribution when not linked > + into another program.) > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <isa-level.c> > +#include <csu/abi-note.c> Ok. > diff --git a/sysdeps/x86/configure b/sysdeps/x86/configure > index 81cc4e80d6..5e32dc62b3 100644 > --- a/sysdeps/x86/configure > +++ b/sysdeps/x86/configure > @@ -68,3 +68,82 @@ elif test $enable_cet = permissive; then > fi > config_vars="$config_vars > enable-cet = $enable_cet" > + > +# Check if linker supports x86 ISA level. > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker x86 ISA level support" >&5 > +$as_echo_n "checking for linker x86 ISA level support... " >&6; } > +if ${libc_cv_include_x86_isa_level+:} false; then : > + $as_echo_n "(cached) " >&6 > +else > + cat > conftest1.S <<EOF > +#ifdef __LP64__ > +# define P2ALIGN 3 > +#else > +# define P2ALIGN 2 > +#endif > + .section ".note.gnu.property", "a" > + .p2align P2ALIGN > + .long 1f - 0f /* name length. */ > + .long 4f - 1f /* data length. */ > + /* NT_GNU_PROPERTY_TYPE_0 */ > + .long 5 /* note type. */ > +0: > + .asciz "GNU" /* vendor name. */ > +1: > + .p2align P2ALIGN > + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ > + .long 0xc0008002 /* pr_type. */ > + .long 3f - 2f /* pr_datasz. */ > +2: > + .long 0x1 > +3: > + .p2align P2ALIGN > +4: > +EOF > +cat > conftest2.S <<EOF > +#ifdef __LP64__ > +# define P2ALIGN 3 > +#else > +# define P2ALIGN 2 > +#endif > + .section ".note.gnu.property", "a" > + .p2align P2ALIGN > + .long 1f - 0f /* name length. */ > + .long 4f - 1f /* data length. */ > + /* NT_GNU_PROPERTY_TYPE_0 */ > + .long 5 /* note type. */ > +0: > + .asciz "GNU" /* vendor name. */ > +1: > + .p2align P2ALIGN > + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ > + .long 0xc0008002 /* pr_type. */ > + .long 3f - 2f /* pr_datasz. */ > +2: > + .long 0x2 > +3: > + .p2align P2ALIGN > +4: > +EOF > +libc_cv_include_x86_isa_level=no > +if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS -nostartfiles -nostdlib -r -o conftest conftest1.S conftest2.S' > + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 > + (eval $ac_try) 2>&5 > + ac_status=$? > + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 > + test $ac_status = 0; }; }; then > + count=`LC_ALL=C $READELF -n conftest | grep NT_GNU_PROPERTY_TYPE_0 | wc -l` > + if test "$count" = 1; then > + libc_cv_include_x86_isa_level=yes > + fi > +fi > +rm -f conftest* > +fi > +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_include_x86_isa_level" >&5 > +$as_echo "$libc_cv_include_x86_isa_level" >&6; } > +if test $libc_cv_include_x86_isa_level = yes; then > + $as_echo "#define INCLUDE_X86_ISA_LEVEL 1" >>confdefs.h > + > +fi > +config_vars="$config_vars > +enable-x86-isa-level = $libc_cv_include_x86_isa_level" > diff --git a/sysdeps/x86/configure.ac b/sysdeps/x86/configure.ac > index 8f3e1191f6..f94088f377 100644 > --- a/sysdeps/x86/configure.ac > +++ b/sysdeps/x86/configure.ac > @@ -43,3 +43,69 @@ elif test $enable_cet = permissive; then > AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_permissive) > fi > LIBC_CONFIG_VAR([enable-cet], [$enable_cet]) > + > +# Check if linker supports x86 ISA level. > +AC_CACHE_CHECK([for linker x86 ISA level support], > + libc_cv_include_x86_isa_level, [dnl > +cat > conftest1.S <<EOF > +#ifdef __LP64__ > +# define P2ALIGN 3 > +#else > +# define P2ALIGN 2 > +#endif > + .section ".note.gnu.property", "a" > + .p2align P2ALIGN > + .long 1f - 0f /* name length. */ > + .long 4f - 1f /* data length. */ > + /* NT_GNU_PROPERTY_TYPE_0 */ > + .long 5 /* note type. */ > +0: > + .asciz "GNU" /* vendor name. */ > +1: > + .p2align P2ALIGN > + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ > + .long 0xc0008002 /* pr_type. */ > + .long 3f - 2f /* pr_datasz. */ > +2: > + .long 0x1 > +3: > + .p2align P2ALIGN > +4: > +EOF > +cat > conftest2.S <<EOF > +#ifdef __LP64__ > +# define P2ALIGN 3 > +#else > +# define P2ALIGN 2 > +#endif > + .section ".note.gnu.property", "a" > + .p2align P2ALIGN > + .long 1f - 0f /* name length. */ > + .long 4f - 1f /* data length. */ > + /* NT_GNU_PROPERTY_TYPE_0 */ > + .long 5 /* note type. */ > +0: > + .asciz "GNU" /* vendor name. */ > +1: > + .p2align P2ALIGN > + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ > + .long 0xc0008002 /* pr_type. */ > + .long 3f - 2f /* pr_datasz. */ > +2: > + .long 0x2 > +3: > + .p2align P2ALIGN > +4: > +EOF > +libc_cv_include_x86_isa_level=no > +if AC_TRY_COMMAND(${CC-cc} $CFLAGS $CPPFLAGS -nostartfiles -nostdlib -r -o conftest conftest1.S conftest2.S); then > + count=`LC_ALL=C $READELF -n conftest | grep NT_GNU_PROPERTY_TYPE_0 | wc -l` > + if test "$count" = 1; then > + libc_cv_include_x86_isa_level=yes > + fi > +fi > +rm -f conftest*]) > +if test $libc_cv_include_x86_isa_level = yes; then > + AC_DEFINE(INCLUDE_X86_ISA_LEVEL) > +fi > +LIBC_CONFIG_VAR([enable-x86-isa-level], [$libc_cv_include_x86_isa_level]) Skipping autogenerated file. > diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c > index 529c586193..e7da682a2e 100644 > --- a/sysdeps/x86/cpu-features.c > +++ b/sysdeps/x86/cpu-features.c > @@ -19,6 +19,7 @@ > #include <cpuid.h> > #include <dl-hwcap.h> > #include <libc-pointer-arith.h> > +#include <get-isa-level.h> > #if IS_IN (libc) && !defined SHARED > # include <assert.h> > # include <unistd.h> > @@ -290,6 +291,8 @@ update_usable (struct cpu_features *cpu_features) > CPU_FEATURE_SET_USABLE (cpu_features, KL); > CPU_FEATURE_SET_USABLE (cpu_features, WIDE_KL); > } > + > + cpu_features->isa_1 = get_isa_level (cpu_features); > } > > static void Ok. > diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c > index b4910f7634..ed7fd6d10d 100644 > --- a/sysdeps/x86/dl-cet.c > +++ b/sysdeps/x86/dl-cet.c > @@ -76,10 +76,12 @@ dl_cet_check (struct link_map *m, const char *program) > */ > enable_ibt &= (HAS_CPU_FEATURE (IBT) > && (enable_ibt_type == cet_always_on > - || (m->l_cet & lc_ibt) != 0)); > + || (m->l_x86_feature_1_and > + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0)); > enable_shstk &= (HAS_CPU_FEATURE (SHSTK) > && (enable_shstk_type == cet_always_on > - || (m->l_cet & lc_shstk) != 0)); > + || (m->l_x86_feature_1_and > + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)); > } > > /* ld.so is CET-enabled by kernel. But shared objects may not Ok. > @@ -111,7 +113,8 @@ dl_cet_check (struct link_map *m, const char *program) > /* IBT is enabled only if it is enabled in executable as > well as all shared objects. */ > enable_ibt &= (enable_ibt_type == cet_always_on > - || (l->l_cet & lc_ibt) != 0); > + || (l->l_x86_feature_1_and > + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0); > if (!found_ibt_legacy && enable_ibt != ibt_enabled) > { > found_ibt_legacy = true; > @@ -121,7 +124,8 @@ dl_cet_check (struct link_map *m, const char *program) > /* SHSTK is enabled only if it is enabled in executable as > well as all shared objects. */ > enable_shstk &= (enable_shstk_type == cet_always_on > - || (l->l_cet & lc_shstk) != 0); > + || (l->l_x86_feature_1_and > + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0); > if (enable_shstk != shstk_enabled) > { > found_shstk_legacy = true; Ok. > diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h > index ce1667dc7a..56bd020b3c 100644 > --- a/sysdeps/x86/dl-prop.h > +++ b/sysdeps/x86/dl-prop.h > @@ -19,14 +19,54 @@ > #ifndef _DL_PROP_H > #define _DL_PROP_H > > +#include <libintl.h> > + > extern void _dl_cet_check (struct link_map *, const char *) > attribute_hidden; > extern void _dl_cet_open_check (struct link_map *) > attribute_hidden; > > +static void > +dl_isa_level_check (struct link_map *m, const char *program) > +{ > + const struct cpu_features *cpu_features = __get_cpu_features (); > + unsigned int i; > + struct link_map *l; > + > + i = m->l_searchlist.r_nlist; > + while (i-- > 0) > + { > + /* Check each shared object to see if ISA level is compatible. */ > + l = m->l_initfini[i]; > + > + /* Skip ISA level check if functions have been executed. */ > + if (l->l_init_called) > + continue; > + > +#ifdef SHARED > + /* Skip ISA level check for ld.so since ld.so won't run if its ISA > + level is higher than CPU. */ > + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) > + continue; > +#endif > + > + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) > + != l->l_x86_isa_1_needed) > + { > + if (program) > + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", > + *l->l_name != '\0' ? l->l_name : program); > + else > + _dl_signal_error (0, l->l_name, "dlopen", > + N_("CPU ISA level is lower than required")); > + } > + } > +} > + Ok. > static inline void __attribute__ ((always_inline)) > _rtld_main_check (struct link_map *m, const char *program) > { > + dl_isa_level_check (m, program); > #if CET_ENABLED > _dl_cet_check (m, program); > #endif > @@ -35,20 +75,18 @@ _rtld_main_check (struct link_map *m, const char *program) > static inline void __attribute__ ((always_inline)) > _dl_open_check (struct link_map *m) > { > + dl_isa_level_check (m, NULL); > #if CET_ENABLED > _dl_cet_open_check (m); > #endif > } > > static inline void __attribute__ ((unused)) > -_dl_process_cet_property_note (struct link_map *l, > - const ElfW(Nhdr) *note, > - const ElfW(Addr) size, > - const ElfW(Addr) align) > +_dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, > + const ElfW(Addr) size, const ElfW(Addr) align) > { > -#if CET_ENABLED > /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ > - if (l->l_cet != lc_unknown) > + if (l->l_property != lc_property_unknown) > return; > > /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in Ok. > @@ -59,7 +97,8 @@ _dl_process_cet_property_note (struct link_map *l, > > const ElfW(Addr) start = (ElfW(Addr)) note; > > - unsigned int feature_1 = 0; > + unsigned int feature_1_and = 0; > + unsigned int isa_1_needed = 0; > unsigned int last_type = 0; > > while ((ElfW(Addr)) (note + 1) - start < size) > @@ -71,11 +110,11 @@ _dl_process_cet_property_note (struct link_map *l, > { > /* Stop if we see more than one GNU property note which may > be generated by the older linker. */ > - if (l->l_cet != lc_unknown) > + if (l->l_property != lc_property_unknown) > return; > > - /* Check CET status now. */ > - l->l_cet = lc_none; > + /* Check CET status and ISA levels now. */ > + l->l_property = lc_property_none; > > /* Check for invalid property. */ > if (note->n_descsz < 8 > @@ -101,26 +140,37 @@ _dl_process_cet_property_note (struct link_map *l, > > last_type = type; > > - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND > + || type == GNU_PROPERTY_X86_ISA_1_NEEDED) > { > - /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4 > - bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND, > - we stop the search regardless if its size is correct > - or not. There is no point to continue if this note > - is ill-formed. */ > + /* The sizes of types which we are searching for are > + 4 bytes. There is no point to continue if this > + note is ill-formed. */ > if (datasz != 4) > return; > > - feature_1 = *(unsigned int *) ptr; > - > - /* Keep searching for the next GNU property note > - generated by the older linker. */ > - break; > + /* NB: Stop the scan only after seeing all types which > + we are searching for. */ > + _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > > + GNU_PROPERTY_X86_FEATURE_1_AND), > + "GNU_PROPERTY_X86_ISA_1_NEEDED > " > + "GNU_PROPERTY_X86_FEATURE_1_AND"); > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) > + feature_1_and = *(unsigned int *) ptr; > + else > + { > + isa_1_needed = *(unsigned int *) ptr; > + > + /* Keep searching for the next GNU property note > + generated by the older linker. */ > + break; > + } > } > - else if (type > GNU_PROPERTY_X86_FEATURE_1_AND) > + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) > { > - /* Stop since property type is in ascending order. */ > - return; > + /* Stop the scan since property type is in ascending > + order. */ > + break; > } > > /* Check the next property item. */ Ok. > @@ -137,18 +187,21 @@ _dl_process_cet_property_note (struct link_map *l, > } > > /* We get here only if there is one or no GNU property note. */ > - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT)) > - l->l_cet |= lc_ibt; > - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) > - l->l_cet |= lc_shstk; > -#endif > + if (isa_1_needed != 0 || feature_1_and != 0) > + { > + l->l_property = lc_property_valid; > + l->l_x86_isa_1_needed = isa_1_needed; > + l->l_x86_feature_1_and = feature_1_and; > + } > + else > + l->l_property = lc_property_none; > } > > static inline void __attribute__ ((unused)) > _dl_process_pt_note (struct link_map *l, int fd, const ElfW(Phdr) *ph) > { > const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); > - _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align); > + _dl_process_property_note (l, note, ph->p_memsz, ph->p_align); > } > > static inline int __attribute__ ((always_inline)) Ok. > diff --git a/sysdeps/x86/get-isa-level.h b/sysdeps/x86/get-isa-level.h > new file mode 100644 > index 0000000000..a86e1e8941 > --- /dev/null > +++ b/sysdeps/x86/get-isa-level.h > @@ -0,0 +1,66 @@ > +/* Get x86 ISA level. > + This file is part of the GNU C Library. > + Copyright (C) 2020 Free Software Foundation, Inc. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <sys/platform/x86.h> > + > +/* Get GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > + ISA level. */ > + > +static unsigned int > +get_isa_level (const struct cpu_features *cpu_features) > +{ > + unsigned int isa_level = 0; > + > + if (CPU_FEATURE_USABLE_P (cpu_features, CMOV) > + && CPU_FEATURE_USABLE_P (cpu_features, CX8) > + && CPU_FEATURE_CPU_P (cpu_features, FPU) > + && CPU_FEATURE_USABLE_P (cpu_features, FXSR) > + && CPU_FEATURE_USABLE_P (cpu_features, MMX) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE2)) > + { > + isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE; > + if (CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) > + && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) > + && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE3) > + && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) > + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2)) > + { > + isa_level |= GNU_PROPERTY_X86_ISA_1_V2; > + if (CPU_FEATURE_USABLE_P (cpu_features, AVX) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX2) > + && CPU_FEATURE_USABLE_P (cpu_features, F16C) > + && CPU_FEATURE_USABLE_P (cpu_features, FMA) > + && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) > + && CPU_FEATURE_USABLE_P (cpu_features, MOVBE)) > + { > + isa_level |= GNU_PROPERTY_X86_ISA_1_V3; > + if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)) > + isa_level |= GNU_PROPERTY_X86_ISA_1_V4; > + } > + } > + } > + > + return isa_level; > +} Ok, is there any plan to add the gcc ISA_1_VX for 32-bit ABI as well? I am asking because if there is no upcoming plan maybe an option is just to return 0 for !__X86_64__. > diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h > index ffe300c931..99e7ee08cf 100644 > --- a/sysdeps/x86/include/cpu-features.h > +++ b/sysdeps/x86/include/cpu-features.h > @@ -123,6 +123,8 @@ struct cpu_features > struct cpu_features_basic basic; > struct cpuid_features features[COMMON_CPUID_INDEX_MAX]; > unsigned int preferred[PREFERRED_FEATURE_INDEX_MAX]; > + /* X86 micro-architecture ISA levels. */ > + unsigned int isa_1; > /* The state size for XSAVEC or XSAVE. The type must be unsigned long > int so that we use > Ok. > diff --git a/sysdeps/x86/isa-level.c b/sysdeps/x86/isa-level.c > new file mode 100644 > index 0000000000..aaf524cb56 > --- /dev/null > +++ b/sysdeps/x86/isa-level.c > @@ -0,0 +1,97 @@ > +/* ELF program property for x86 ISA level. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + In addition to the permissions in the GNU Lesser General Public > + License, the Free Software Foundation gives you unlimited > + permission to link the compiled version of this file with other > + programs, and to distribute those programs without any restriction > + coming from the use of this file. (The Lesser General Public > + License restrictions do apply in other respects; for example, they > + cover modification of the file, and distribution when not linked > + into another program.) > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <elf.h> > + > +/* ELF program property for x86 ISA level. */ > +#ifdef INCLUDE_X86_ISA_LEVEL > +# if defined __x86_64__ || defined __FXSR__ || !defined _SOFT_FLOAT \ > + || defined __MMX__ || defined __SSE__ || defined __SSE2__ > +# define ISA_BASELINE GNU_PROPERTY_X86_ISA_1_BASELINE > +# else > +# define ISA_BASELINE 0 > +# endif > + > +# if defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 \ > + || (defined __x86_64__ && defined __LAHF_SAHF__) \ > + || defined __POPCNT__ || defined __SSE3__ \ > + || defined __SSSE3__ || defined __SSE4_1__ || defined __SSE4_2__ > +# define ISA_V2 GNU_PROPERTY_X86_ISA_1_V2 > +# else > +# define ISA_V2 0 > +# endif > + > +# if defined __AVX__ || defined __AVX2__ || defined __F16C__ \ > + || defined __FMA__ || defined __LZCNT__ || defined __MOVBE__ \ > + || defined __XSAVE__ > +# define ISA_V3 GNU_PROPERTY_X86_ISA_1_V3 > +# else > +# define ISA_V3 0 > +# endif > + > +# if defined __AVX512F__ || defined __AVX512BW__ || defined __AVX512CD__ \ > + || defined __AVX512DQ__ || defined __AVX512VL__ > +# define ISA_V4 GNU_PROPERTY_X86_ISA_1_V4 > +# else > +# define ISA_V4 0 > +# endif > + > +# ifndef ISA_LEVEL > +# define ISA_LEVEL (ISA_BASELINE | ISA_V2 | ISA_V3 | ISA_V4) > +# endif > + > +# if ISA_LEVEL > +# ifdef __LP64__ > +# define PROPERTY_ALIGN 3 > +# else > +# define PROPERTY_ALIGN 2 > +# endif > + > +# define note_stringify(arg) note_stringify_1(arg) > +# define note_stringify_1(arg) #arg > + > +asm(".pushsection \".note.gnu.property\",\"a\",@note\n" > +" .p2align " note_stringify (PROPERTY_ALIGN) > + /* name length. */ > +"\n .long 1f - 0f\n" > + /* data length. */ > +" .long 4f - 1f\n" > + /* note type: NT_GNU_PROPERTY_TYPE_0. */ > +" .long " note_stringify (NT_GNU_PROPERTY_TYPE_0) > + /* vendor name. */ > +"\n0: .asciz \"GNU\"\n" > +"1: .p2align " note_stringify (PROPERTY_ALIGN) > + /* pr_type: GNU_PROPERTY_X86_ISA_1_NEEDED. */ > +"\n .long " note_stringify (GNU_PROPERTY_X86_ISA_1_NEEDED) > + /* pr_datasz. */ > +"\n .long 3f - 2f\n" > + /* GNU_PROPERTY_X86_ISA_1_V[234]. */ > +"2:\n .long " note_stringify (ISA_LEVEL) > +"\n3:\n .p2align " note_stringify (PROPERTY_ALIGN) > +"\n4:\n .popsection"); > +# endif /* ISA_LEVEL */ > +#endif /* INCLUDE_X86_ISA_LEVEL */ Ok. > diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h > index 8074739b47..4c46a25f83 100644 > --- a/sysdeps/x86/link_map.h > +++ b/sysdeps/x86/link_map.h > @@ -16,12 +16,16 @@ > License along with the GNU C Library; if not, see > <https://www.gnu.org/licenses/>. */ > > -/* If this object is enabled with CET. */ > +/* if this object has GNU property. */ > enum > { > - lc_unknown = 0, /* Unknown CET status. */ > - lc_none = 1 << 0, /* Not enabled with CET. */ > - lc_ibt = 1 << 1, /* Enabled with IBT. */ > - lc_shstk = 1 << 2, /* Enabled with STSHK. */ > - lc_ibt_and_shstk = lc_ibt | lc_shstk /* Enabled with both. */ > - } l_cet:3; > + lc_property_unknown = 0, /* Unknown property status. */ > + lc_property_none = 1 << 0, /* No property. */ > + lc_property_valid = 1 << 1 /* Has valid property. */ > + } l_property:2; > + > +/* GNU_PROPERTY_X86_FEATURE_1_AND of this object. */ > +unsigned int l_x86_feature_1_and; > + > +/* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */ > +unsigned int l_x86_isa_1_needed; Ok. > diff --git a/sysdeps/x86/tst-isa-level-1.c b/sysdeps/x86/tst-isa-level-1.c > new file mode 100644 > index 0000000000..dc31d71ad2 > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-1.c > @@ -0,0 +1,104 @@ > +/* Check ISA level on dlopened shared object. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <stdlib.h> > +#include <stdbool.h> > +#include <string.h> > +#include <elf.h> > +#include <get-isa-level.h> > +#include <support/xdlfcn.h> > +#include <support/check.h> > +#include <support/test-driver.h> > + > +static void > +do_test_1 (const char *modname, bool fail) > +{ > + int (*fp) (void); > + void *h; > + > + h = dlopen (modname, RTLD_LAZY); > + if (h == NULL) > + { > + const char *err = dlerror (); > + if (fail) > + { > + if (strstr (err, "CPU ISA level is lower than required") == NULL) > + FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err); > + > + return; > + } > + > + FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err); > + } > + > + if (fail) > + FAIL_EXIT1 ("dlopen '%s' should have failed\n", modname); > + > + fp = xdlsym (h, "test"); > + > + if (fp () != 0) > + FAIL_EXIT1 ("test () != 0\n"); > + > + dlclose (h); > +} > + > +static int > +do_test (void) > +{ > + const struct cpu_features *cpu_features > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > + unsigned int isa_level = get_isa_level (cpu_features); > + bool has_isa_baseline = ((isa_level & GNU_PROPERTY_X86_ISA_1_BASELINE) > + == GNU_PROPERTY_X86_ISA_1_BASELINE); > + bool has_isa_v2 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V2) > + == GNU_PROPERTY_X86_ISA_1_V2); > + bool has_isa_v3 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V3) > + == GNU_PROPERTY_X86_ISA_1_V3); > + bool has_isa_v4 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) > + == GNU_PROPERTY_X86_ISA_1_V4); > + > + if (!has_isa_baseline) > + { > + do_test_1 ("tst-isa-level-mod-1-baseline.so", true); > + return EXIT_SUCCESS; > + } > + > + do_test_1 ("tst-isa-level-mod-1-baseline.so", false); > + > + /* Skip on x86-64-v4 platforms since dlopen v4 module always works. */ > + if (has_isa_v4) > + return EXIT_UNSUPPORTED; Not sure if unsupported is the right error code here, since the isa selection is just ignored (not really not supported). > + > + do_test_1 ("tst-isa-level-mod-1-v4.so", true); > + > + /* Skip on x86-64-v3 platforms since dlopen v3 module always works. */ > + if (has_isa_v3) > + return EXIT_SUCCESS; > + > + do_test_1 ("tst-isa-level-mod-1-v3.so", true); > + > + /* Skip on x86-64-v2 platforms since dlopen v2 module always works. */ > + if (has_isa_v2) > + return EXIT_SUCCESS; > + > + do_test_1 ("tst-isa-level-mod-1-v2.so", true); > + > + return EXIT_SUCCESS; > +} > + > +#include <support/test-driver.c> > diff --git a/sysdeps/x86/tst-isa-level-mod-1-baseline.c b/sysdeps/x86/tst-isa-level-mod-1-baseline.c > new file mode 100644 > index 0000000000..d6fe0685a4 > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-mod-1-baseline.c > @@ -0,0 +1 @@ > +#include "tst-isa-level-mod-1.c" > diff --git a/sysdeps/x86/tst-isa-level-mod-1-v2.c b/sysdeps/x86/tst-isa-level-mod-1-v2.c > new file mode 100644 > index 0000000000..d6fe0685a4 > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-mod-1-v2.c > @@ -0,0 +1 @@ > +#include "tst-isa-level-mod-1.c" > diff --git a/sysdeps/x86/tst-isa-level-mod-1-v3.c b/sysdeps/x86/tst-isa-level-mod-1-v3.c > new file mode 100644 > index 0000000000..d6fe0685a4 > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-mod-1-v3.c > @@ -0,0 +1 @@ > +#include "tst-isa-level-mod-1.c" > diff --git a/sysdeps/x86/tst-isa-level-mod-1-v4.c b/sysdeps/x86/tst-isa-level-mod-1-v4.c > new file mode 100644 > index 0000000000..d6fe0685a4 > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-mod-1-v4.c > @@ -0,0 +1 @@ > +#include "tst-isa-level-mod-1.c" Ok. > diff --git a/sysdeps/x86/tst-isa-level-mod-1.c b/sysdeps/x86/tst-isa-level-mod-1.c > new file mode 100644 > index 0000000000..40a2e1d99f > --- /dev/null > +++ b/sysdeps/x86/tst-isa-level-mod-1.c > @@ -0,0 +1,25 @@ > +/* Check ISA level on dlopened shared object. > + Copyright (C) 2019-2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <isa-level.c> > + > +int > +test (void) > +{ > + return 0; > +} Ok. > diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c > index 0aaa635a00..e030534f1b 100644 > --- a/sysdeps/x86_64/dl-hwcaps-subdirs.c > +++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c > @@ -18,6 +18,7 @@ > > #include <dl-hwcaps.h> > #include <cpu-features.h> > +#include <get-isa-level.h> > > const char _dl_hwcaps_subdirs[] = "x86-64-v4:x86-64-v3:x86-64-v2"; > enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ > @@ -25,40 +26,25 @@ enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ > uint32_t > _dl_hwcaps_subdirs_active (void) > { > + const struct cpu_features *cpu_features > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > + unsigned int isa_level = get_isa_level (cpu_features); > int active = 0; > > /* Test in reverse preference order. */ > > /* x86-64-v2. */ > - if (!(CPU_FEATURE_USABLE (CMPXCHG16B) > - && CPU_FEATURE_USABLE (LAHF64_SAHF64) > - && CPU_FEATURE_USABLE (POPCNT) > - && CPU_FEATURE_USABLE (SSE3) > - && CPU_FEATURE_USABLE (SSE4_1) > - && CPU_FEATURE_USABLE (SSE4_2) > - && CPU_FEATURE_USABLE (SSSE3))) > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > ++active; > > /* x86-64-v3. */ > - if (!(CPU_FEATURE_USABLE (AVX) > - && CPU_FEATURE_USABLE (AVX2) > - && CPU_FEATURE_USABLE (BMI1) > - && CPU_FEATURE_USABLE (BMI2) > - && CPU_FEATURE_USABLE (F16C) > - && CPU_FEATURE_USABLE (FMA) > - && CPU_FEATURE_USABLE (LZCNT) > - && CPU_FEATURE_USABLE (MOVBE) > - && CPU_FEATURE_USABLE (OSXSAVE))) > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > ++active; > > - /* x86-64-v4. */ > - if (!(CPU_FEATURE_USABLE (AVX512F) > - && CPU_FEATURE_USABLE (AVX512BW) > - && CPU_FEATURE_USABLE (AVX512CD) > - && CPU_FEATURE_USABLE (AVX512DQ) > - && CPU_FEATURE_USABLE (AVX512VL))) > + /* x86-64-v4. */ > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > ++active; > > diff --git a/sysdeps/x86_64/tst-glibc-hwcaps.c b/sysdeps/x86_64/tst-glibc-hwcaps.c > index 8bdcb91db0..37b61d3c84 100644 > --- a/sysdeps/x86_64/tst-glibc-hwcaps.c > +++ b/sysdeps/x86_64/tst-glibc-hwcaps.c > @@ -19,7 +19,8 @@ > #include <stdio.h> > #include <support/check.h> > #include <sys/param.h> > -#include <sys/platform/x86.h> > +#include <elf.h> > +#include <get-isa-level.h> > > extern int marker2 (void); > extern int marker3 (void); > @@ -31,35 +32,15 @@ compute_level (void) > { > const struct cpu_features *cpu_features > = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > - > - if (!(CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) > - && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) > - && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) > - && CPU_FEATURE_USABLE_P (cpu_features, MMX) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE2) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE3) > - && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) > - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2))) > - return 1; > - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX2) > - && CPU_FEATURE_USABLE_P (cpu_features, BMI1) > - && CPU_FEATURE_USABLE_P (cpu_features, BMI2) > - && CPU_FEATURE_USABLE_P (cpu_features, F16C) > - && CPU_FEATURE_USABLE_P (cpu_features, FMA) > - && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) > - && CPU_FEATURE_USABLE_P (cpu_features, MOVBE) > - && CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE))) > - return 2; > - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX512F) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL))) > - return 3; > - return 4; > + unsigned int isa_level = get_isa_level (cpu_features); > + > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) > + return 1; > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) > + return 2; > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) > + return 3; > + return 4; > } > > static int > -- > 2.29.2 Ok. ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V6 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] 2021-01-07 20:09 ` Adhemerval Zanella @ 2021-01-07 20:58 ` H.J. Lu 2021-01-18 11:17 ` Florian Weimer 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2021-01-07 20:58 UTC (permalink / raw) To: Adhemerval Zanella; +Cc: GNU C Library, Florian Weimer [-- Attachment #1: Type: text/plain, Size: 49804 bytes --] On Thu, Jan 7, 2021 at 12:09 PM Adhemerval Zanella <adhemerval.zanella@linaro.org> wrote: > > > > On 06/01/2021 14:11, H.J. Lu wrote: > > Here is the updated patch. Tested under i686, x32 and x86-64 modes > > on x86-64-v2, x86-64-v3 and x86-64-v4 machines. > > > > OK for master? > > > > Thanks. > > > > LGTM with just two clarification below. > > Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org> > > > From 6630e06f9bcb361177d46c277cf8e67cbd722429 Mon Sep 17 00:00:00 2001 > > From: "H.J. Lu" <hjl.tools@gmail.com> > > Date: Fri, 9 Oct 2020 06:06:56 -0700 > > Subject: [PATCH] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] > > > > GCC 11 supports -march=x86-64-v[234] to enable x86 micro-architecture ISA > > levels: > > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97250 > > > > and -mneeded to emit GNU_PROPERTY_X86_ISA_1_NEEDED property with > > GNU_PROPERTY_X86_ISA_1_V[234] marker: > > > > https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/13 > > > > Binutils support for GNU_PROPERTY_X86_ISA_1_V[234] marker were added by > > > > commit b0ab06937385e0ae25cebf1991787d64f439bf12 > > Author: H.J. Lu <hjl.tools@gmail.com> > > Date: Fri Oct 30 06:49:57 2020 -0700 > > > > x86: Support GNU_PROPERTY_X86_ISA_1_BASELINE marker > > > > and > > > > commit 32930e4edbc06bc6f10c435dbcc63131715df678 > > Author: H.J. Lu <hjl.tools@gmail.com> > > Date: Fri Oct 9 05:05:57 2020 -0700 > > > > x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker > > > > GNU_PROPERTY_X86_ISA_1_NEEDED property in x86 ELF binaries indicate the > > micro-architecture ISA level required to execute the binary. The marker > > must be added by programmers explicitly in one of 3 ways: > > > > 1. Pass -mneeded to GCC. > > 2. Add the marker in the linker inputs as this patch does. > > 3. Pass -z x86-64-v[234] to the linker. > > > > Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > > marker support to ld.so if binutils 2.32 or newer is used to build glibc: > > > > 1. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > > markers to elf.h. > > 2. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > > marker to abi-note.o based on the ISA level used to compile abi-note.o, > > assuming that the same ISA level is used to compile the whole glibc. > > 3. Add isa_1 to cpu_features to record the supported x86 ISA level. > > 4. Rename _dl_process_cet_property_note to _dl_process_property_note and > > add GNU_PROPERTY_X86_ISA_1_V[234] marker detection. > > 5. Update _rtld_main_check and _dl_open_check to check loaded objects > > with the incompatible ISA level. > > 6. Add a testcase to verify that dlopen an x86-64-v4 shared object fails > > on lesser platforms. > > 7. Use <get-isa-level.h> in dl-hwcaps-subdirs.c and tst-glibc-hwcaps.c. > > > > Tested under i686, x32 and x86-64 modes on x86-64-v2, x86-64-v3 and > > x86-64-v4 machines. > > > > Marked elf/tst-isa-level-1 with x86-64-v4, ran it on x86-64-v3 machine > > and got: > > > > [hjl@gnu-cfl-2 build-x86_64-linux]$ ./elf/tst-isa-level-1 > > ./elf/tst-isa-level-1: CPU ISA level is lower than required > > [hjl@gnu-cfl-2 build-x86_64-linux]$ > > --- > > config.h.in | 3 + > > elf/elf.h | 35 +++---- > > sysdeps/x86/Makefile | 30 ++++++ > > sysdeps/x86/abi-note.c | 29 ++++++ > > sysdeps/x86/configure | 79 ++++++++++++++ > > sysdeps/x86/configure.ac | 66 ++++++++++++ > > sysdeps/x86/cpu-features.c | 3 + > > sysdeps/x86/dl-cet.c | 12 ++- > > sysdeps/x86/dl-prop.h | 113 +++++++++++++++------ > > sysdeps/x86/get-isa-level.h | 66 ++++++++++++ > > sysdeps/x86/include/cpu-features.h | 2 + > > sysdeps/x86/isa-level.c | 97 ++++++++++++++++++ > > sysdeps/x86/link_map.h | 18 ++-- > > sysdeps/x86/tst-isa-level-1.c | 104 +++++++++++++++++++ > > sysdeps/x86/tst-isa-level-mod-1-baseline.c | 1 + > > sysdeps/x86/tst-isa-level-mod-1-v2.c | 1 + > > sysdeps/x86/tst-isa-level-mod-1-v3.c | 1 + > > sysdeps/x86/tst-isa-level-mod-1-v4.c | 1 + > > sysdeps/x86/tst-isa-level-mod-1.c | 25 +++++ > > sysdeps/x86_64/dl-hwcaps-subdirs.c | 30 ++---- > > sysdeps/x86_64/tst-glibc-hwcaps.c | 41 ++------ > > 21 files changed, 644 insertions(+), 113 deletions(-) > > create mode 100644 sysdeps/x86/abi-note.c > > create mode 100644 sysdeps/x86/get-isa-level.h > > create mode 100644 sysdeps/x86/isa-level.c > > create mode 100644 sysdeps/x86/tst-isa-level-1.c > > create mode 100644 sysdeps/x86/tst-isa-level-mod-1-baseline.c > > create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v2.c > > create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v3.c > > create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v4.c > > create mode 100644 sysdeps/x86/tst-isa-level-mod-1.c > > > > diff --git a/config.h.in b/config.h.in > > index eca2d66a2c..947feeff36 100644 > > --- a/config.h.in > > +++ b/config.h.in > > @@ -269,4 +269,7 @@ > > /* The default value of x86 CET control. */ > > #define DEFAULT_DL_X86_CET_CONTROL cet_elf_property > > > > +/* Define if x86 ISA level should be included in shared libraries. */ > > +#undef INCLUDE_X86_ISA_LEVEL > > + > > #endif > > diff --git a/elf/elf.h b/elf/elf.h > > Ok. > > > index 4a9ae97dfc..9ebd052c8a 100644 > > --- a/elf/elf.h > > +++ b/elf/elf.h > > @@ -1324,31 +1324,26 @@ typedef struct > > > > /* The x86 instruction sets indicated by the corresponding bits are > > used in program. Their support in the hardware is optional. */ > > -#define GNU_PROPERTY_X86_ISA_1_USED 0xc0000000 > > +#define GNU_PROPERTY_X86_ISA_1_USED 0xc0010002 > > /* The x86 instruction sets indicated by the corresponding bits are > > used in program and they must be supported by the hardware. */ > > -#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0000001 > > +#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0008002 > > /* X86 processor-specific features used in program. */ > > #define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 > > > > -#define GNU_PROPERTY_X86_ISA_1_486 (1U << 0) > > -#define GNU_PROPERTY_X86_ISA_1_586 (1U << 1) > > -#define GNU_PROPERTY_X86_ISA_1_686 (1U << 2) > > -#define GNU_PROPERTY_X86_ISA_1_SSE (1U << 3) > > -#define GNU_PROPERTY_X86_ISA_1_SSE2 (1U << 4) > > -#define GNU_PROPERTY_X86_ISA_1_SSE3 (1U << 5) > > -#define GNU_PROPERTY_X86_ISA_1_SSSE3 (1U << 6) > > -#define GNU_PROPERTY_X86_ISA_1_SSE4_1 (1U << 7) > > -#define GNU_PROPERTY_X86_ISA_1_SSE4_2 (1U << 8) > > -#define GNU_PROPERTY_X86_ISA_1_AVX (1U << 9) > > -#define GNU_PROPERTY_X86_ISA_1_AVX2 (1U << 10) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512F (1U << 11) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512CD (1U << 12) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512ER (1U << 13) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512PF (1U << 14) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512VL (1U << 15) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512DQ (1U << 16) > > -#define GNU_PROPERTY_X86_ISA_1_AVX512BW (1U << 17) > > +/* GNU_PROPERTY_X86_ISA_1_BASELINE: CMOV, CX8 (cmpxchg8b), FPU (fld), > > + MMX, OSFXSR (fxsave), SCE (syscall), SSE and SSE2. */ > > +#define GNU_PROPERTY_X86_ISA_1_BASELINE (1U << 0) > > +/* GNU_PROPERTY_X86_ISA_1_V2: GNU_PROPERTY_X86_ISA_1_BASELINE, > > + CMPXCHG16B (cmpxchg16b), LAHF-SAHF (lahf), POPCNT (popcnt), SSE3, > > + SSSE3, SSE4.1 and SSE4.2. */ > > +#define GNU_PROPERTY_X86_ISA_1_V2 (1U << 1) > > +/* GNU_PROPERTY_X86_ISA_1_V3: GNU_PROPERTY_X86_ISA_1_V2, AVX, AVX2, BMI1, > > + BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE. */ > > +#define GNU_PROPERTY_X86_ISA_1_V3 (1U << 2) > > +/* GNU_PROPERTY_X86_ISA_1_V4: GNU_PROPERTY_X86_ISA_1_V3, AVX512F, > > + AVX512BW, AVX512CD, AVX512DQ and AVX512VL. */ > > +#define GNU_PROPERTY_X86_ISA_1_V4 (1U << 3) > > > > /* This indicates that all executable sections are compatible with > > IBT. */ > > Ok. > > > diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile > > index 081cc72e93..c814d5a195 100644 > > --- a/sysdeps/x86/Makefile > > +++ b/sysdeps/x86/Makefile > > @@ -9,6 +9,36 @@ sysdep_headers += sys/platform/x86.h > > tests += tst-get-cpu-features tst-get-cpu-features-static \ > > tst-cpu-features-cpuinfo tst-cpu-features-supports > > tests-static += tst-get-cpu-features-static > > +ifeq (yes,$(enable-x86-isa-level)) > > +tests += tst-isa-level-1 > > +modules-names += tst-isa-level-mod-1-baseline \ > > + tst-isa-level-mod-1-v2 \ > > + tst-isa-level-mod-1-v3 \ > > + tst-isa-level-mod-1-v4 \ > > + > > +# X86 ISA level baseline > > +CFLAGS-tst-isa-level-mod-1-baseline.c += -DINCLUDE_X86_ISA_LEVEL \ > > + -DISA_LEVEL=0x1 \ > > + -march=x86-64 > > +# X86 ISA level v2 > > +CFLAGS-tst-isa-level-mod-1-v2.c += -DINCLUDE_X86_ISA_LEVEL \ > > + -DISA_LEVEL=0x3 \ > > + -march=x86-64 > > +# X86 ISA level v3 > > +CFLAGS-tst-isa-level-mod-1-v3.c += -DINCLUDE_X86_ISA_LEVEL \ > > + -DISA_LEVEL=0x7 \ > > + -march=x86-64 > > +# X86 ISA level v4 > > +CFLAGS-tst-isa-level-mod-1-v4.c += -DINCLUDE_X86_ISA_LEVEL \ > > + -DISA_LEVEL=0xf \ > > + -march=x86-64 > > + > > +$(objpfx)tst-isa-level-1: $(libdl) > > +$(objpfx)tst-isa-level-1.out: $(objpfx)tst-isa-level-mod-1-baseline.so \ > > + $(objpfx)tst-isa-level-mod-1-v2.so \ > > + $(objpfx)tst-isa-level-mod-1-v3.so \ > > + $(objpfx)tst-isa-level-mod-1-v4.so > > +endif > > endif > > > > ifeq ($(subdir),math) > > Ok. > > > diff --git a/sysdeps/x86/abi-note.c b/sysdeps/x86/abi-note.c > > new file mode 100644 > > index 0000000000..5140e6ab47 > > --- /dev/null > > +++ b/sysdeps/x86/abi-note.c > > @@ -0,0 +1,29 @@ > > +/* Special .init and .fini section support. x86-64 version. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + In addition to the permissions in the GNU Lesser General Public > > + License, the Free Software Foundation gives you unlimited > > + permission to link the compiled version of this file with other > > + programs, and to distribute those programs without any restriction > > + coming from the use of this file. (The Lesser General Public > > + License restrictions do apply in other respects; for example, they > > + cover modification of the file, and distribution when not linked > > + into another program.) > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <isa-level.c> > > +#include <csu/abi-note.c> > > Ok. > > > diff --git a/sysdeps/x86/configure b/sysdeps/x86/configure > > index 81cc4e80d6..5e32dc62b3 100644 > > --- a/sysdeps/x86/configure > > +++ b/sysdeps/x86/configure > > @@ -68,3 +68,82 @@ elif test $enable_cet = permissive; then > > fi > > config_vars="$config_vars > > enable-cet = $enable_cet" > > + > > +# Check if linker supports x86 ISA level. > > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker x86 ISA level support" >&5 > > +$as_echo_n "checking for linker x86 ISA level support... " >&6; } > > +if ${libc_cv_include_x86_isa_level+:} false; then : > > + $as_echo_n "(cached) " >&6 > > +else > > + cat > conftest1.S <<EOF > > +#ifdef __LP64__ > > +# define P2ALIGN 3 > > +#else > > +# define P2ALIGN 2 > > +#endif > > + .section ".note.gnu.property", "a" > > + .p2align P2ALIGN > > + .long 1f - 0f /* name length. */ > > + .long 4f - 1f /* data length. */ > > + /* NT_GNU_PROPERTY_TYPE_0 */ > > + .long 5 /* note type. */ > > +0: > > + .asciz "GNU" /* vendor name. */ > > +1: > > + .p2align P2ALIGN > > + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ > > + .long 0xc0008002 /* pr_type. */ > > + .long 3f - 2f /* pr_datasz. */ > > +2: > > + .long 0x1 > > +3: > > + .p2align P2ALIGN > > +4: > > +EOF > > +cat > conftest2.S <<EOF > > +#ifdef __LP64__ > > +# define P2ALIGN 3 > > +#else > > +# define P2ALIGN 2 > > +#endif > > + .section ".note.gnu.property", "a" > > + .p2align P2ALIGN > > + .long 1f - 0f /* name length. */ > > + .long 4f - 1f /* data length. */ > > + /* NT_GNU_PROPERTY_TYPE_0 */ > > + .long 5 /* note type. */ > > +0: > > + .asciz "GNU" /* vendor name. */ > > +1: > > + .p2align P2ALIGN > > + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ > > + .long 0xc0008002 /* pr_type. */ > > + .long 3f - 2f /* pr_datasz. */ > > +2: > > + .long 0x2 > > +3: > > + .p2align P2ALIGN > > +4: > > +EOF > > +libc_cv_include_x86_isa_level=no > > +if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS -nostartfiles -nostdlib -r -o conftest conftest1.S conftest2.S' > > + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 > > + (eval $ac_try) 2>&5 > > + ac_status=$? > > + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 > > + test $ac_status = 0; }; }; then > > + count=`LC_ALL=C $READELF -n conftest | grep NT_GNU_PROPERTY_TYPE_0 | wc -l` > > + if test "$count" = 1; then > > + libc_cv_include_x86_isa_level=yes > > + fi > > +fi > > +rm -f conftest* > > +fi > > +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_include_x86_isa_level" >&5 > > +$as_echo "$libc_cv_include_x86_isa_level" >&6; } > > +if test $libc_cv_include_x86_isa_level = yes; then > > + $as_echo "#define INCLUDE_X86_ISA_LEVEL 1" >>confdefs.h > > + > > +fi > > +config_vars="$config_vars > > +enable-x86-isa-level = $libc_cv_include_x86_isa_level" > > diff --git a/sysdeps/x86/configure.ac b/sysdeps/x86/configure.ac > > index 8f3e1191f6..f94088f377 100644 > > --- a/sysdeps/x86/configure.ac > > +++ b/sysdeps/x86/configure.ac > > @@ -43,3 +43,69 @@ elif test $enable_cet = permissive; then > > AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_permissive) > > fi > > LIBC_CONFIG_VAR([enable-cet], [$enable_cet]) > > + > > +# Check if linker supports x86 ISA level. > > +AC_CACHE_CHECK([for linker x86 ISA level support], > > + libc_cv_include_x86_isa_level, [dnl > > +cat > conftest1.S <<EOF > > +#ifdef __LP64__ > > +# define P2ALIGN 3 > > +#else > > +# define P2ALIGN 2 > > +#endif > > + .section ".note.gnu.property", "a" > > + .p2align P2ALIGN > > + .long 1f - 0f /* name length. */ > > + .long 4f - 1f /* data length. */ > > + /* NT_GNU_PROPERTY_TYPE_0 */ > > + .long 5 /* note type. */ > > +0: > > + .asciz "GNU" /* vendor name. */ > > +1: > > + .p2align P2ALIGN > > + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ > > + .long 0xc0008002 /* pr_type. */ > > + .long 3f - 2f /* pr_datasz. */ > > +2: > > + .long 0x1 > > +3: > > + .p2align P2ALIGN > > +4: > > +EOF > > +cat > conftest2.S <<EOF > > +#ifdef __LP64__ > > +# define P2ALIGN 3 > > +#else > > +# define P2ALIGN 2 > > +#endif > > + .section ".note.gnu.property", "a" > > + .p2align P2ALIGN > > + .long 1f - 0f /* name length. */ > > + .long 4f - 1f /* data length. */ > > + /* NT_GNU_PROPERTY_TYPE_0 */ > > + .long 5 /* note type. */ > > +0: > > + .asciz "GNU" /* vendor name. */ > > +1: > > + .p2align P2ALIGN > > + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ > > + .long 0xc0008002 /* pr_type. */ > > + .long 3f - 2f /* pr_datasz. */ > > +2: > > + .long 0x2 > > +3: > > + .p2align P2ALIGN > > +4: > > +EOF > > +libc_cv_include_x86_isa_level=no > > +if AC_TRY_COMMAND(${CC-cc} $CFLAGS $CPPFLAGS -nostartfiles -nostdlib -r -o conftest conftest1.S conftest2.S); then > > + count=`LC_ALL=C $READELF -n conftest | grep NT_GNU_PROPERTY_TYPE_0 | wc -l` > > + if test "$count" = 1; then > > + libc_cv_include_x86_isa_level=yes > > + fi > > +fi > > +rm -f conftest*]) > > +if test $libc_cv_include_x86_isa_level = yes; then > > + AC_DEFINE(INCLUDE_X86_ISA_LEVEL) > > +fi > > +LIBC_CONFIG_VAR([enable-x86-isa-level], [$libc_cv_include_x86_isa_level]) > > Skipping autogenerated file. > > > diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c > > index 529c586193..e7da682a2e 100644 > > --- a/sysdeps/x86/cpu-features.c > > +++ b/sysdeps/x86/cpu-features.c > > @@ -19,6 +19,7 @@ > > #include <cpuid.h> > > #include <dl-hwcap.h> > > #include <libc-pointer-arith.h> > > +#include <get-isa-level.h> > > #if IS_IN (libc) && !defined SHARED > > # include <assert.h> > > # include <unistd.h> > > @@ -290,6 +291,8 @@ update_usable (struct cpu_features *cpu_features) > > CPU_FEATURE_SET_USABLE (cpu_features, KL); > > CPU_FEATURE_SET_USABLE (cpu_features, WIDE_KL); > > } > > + > > + cpu_features->isa_1 = get_isa_level (cpu_features); > > } > > > > static void > > Ok. > > > diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c > > index b4910f7634..ed7fd6d10d 100644 > > --- a/sysdeps/x86/dl-cet.c > > +++ b/sysdeps/x86/dl-cet.c > > @@ -76,10 +76,12 @@ dl_cet_check (struct link_map *m, const char *program) > > */ > > enable_ibt &= (HAS_CPU_FEATURE (IBT) > > && (enable_ibt_type == cet_always_on > > - || (m->l_cet & lc_ibt) != 0)); > > + || (m->l_x86_feature_1_and > > + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0)); > > enable_shstk &= (HAS_CPU_FEATURE (SHSTK) > > && (enable_shstk_type == cet_always_on > > - || (m->l_cet & lc_shstk) != 0)); > > + || (m->l_x86_feature_1_and > > + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)); > > } > > > > /* ld.so is CET-enabled by kernel. But shared objects may not > > Ok. > > > @@ -111,7 +113,8 @@ dl_cet_check (struct link_map *m, const char *program) > > /* IBT is enabled only if it is enabled in executable as > > well as all shared objects. */ > > enable_ibt &= (enable_ibt_type == cet_always_on > > - || (l->l_cet & lc_ibt) != 0); > > + || (l->l_x86_feature_1_and > > + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0); > > if (!found_ibt_legacy && enable_ibt != ibt_enabled) > > { > > found_ibt_legacy = true; > > @@ -121,7 +124,8 @@ dl_cet_check (struct link_map *m, const char *program) > > /* SHSTK is enabled only if it is enabled in executable as > > well as all shared objects. */ > > enable_shstk &= (enable_shstk_type == cet_always_on > > - || (l->l_cet & lc_shstk) != 0); > > + || (l->l_x86_feature_1_and > > + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0); > > if (enable_shstk != shstk_enabled) > > { > > found_shstk_legacy = true; > > Ok. > > > diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h > > index ce1667dc7a..56bd020b3c 100644 > > --- a/sysdeps/x86/dl-prop.h > > +++ b/sysdeps/x86/dl-prop.h > > @@ -19,14 +19,54 @@ > > #ifndef _DL_PROP_H > > #define _DL_PROP_H > > > > +#include <libintl.h> > > + > > extern void _dl_cet_check (struct link_map *, const char *) > > attribute_hidden; > > extern void _dl_cet_open_check (struct link_map *) > > attribute_hidden; > > > > +static void > > +dl_isa_level_check (struct link_map *m, const char *program) > > +{ > > + const struct cpu_features *cpu_features = __get_cpu_features (); > > + unsigned int i; > > + struct link_map *l; > > + > > + i = m->l_searchlist.r_nlist; > > + while (i-- > 0) > > + { > > + /* Check each shared object to see if ISA level is compatible. */ > > + l = m->l_initfini[i]; > > + > > + /* Skip ISA level check if functions have been executed. */ > > + if (l->l_init_called) > > + continue; > > + > > +#ifdef SHARED > > + /* Skip ISA level check for ld.so since ld.so won't run if its ISA > > + level is higher than CPU. */ > > + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) > > + continue; > > +#endif > > + > > + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) > > + != l->l_x86_isa_1_needed) > > + { > > + if (program) > > + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", > > + *l->l_name != '\0' ? l->l_name : program); > > + else > > + _dl_signal_error (0, l->l_name, "dlopen", > > + N_("CPU ISA level is lower than required")); > > + } > > + } > > +} > > + > > Ok. > > > static inline void __attribute__ ((always_inline)) > > _rtld_main_check (struct link_map *m, const char *program) > > { > > + dl_isa_level_check (m, program); > > #if CET_ENABLED > > _dl_cet_check (m, program); > > #endif > > @@ -35,20 +75,18 @@ _rtld_main_check (struct link_map *m, const char *program) > > static inline void __attribute__ ((always_inline)) > > _dl_open_check (struct link_map *m) > > { > > + dl_isa_level_check (m, NULL); > > #if CET_ENABLED > > _dl_cet_open_check (m); > > #endif > > } > > > > static inline void __attribute__ ((unused)) > > -_dl_process_cet_property_note (struct link_map *l, > > - const ElfW(Nhdr) *note, > > - const ElfW(Addr) size, > > - const ElfW(Addr) align) > > +_dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, > > + const ElfW(Addr) size, const ElfW(Addr) align) > > { > > -#if CET_ENABLED > > /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ > > - if (l->l_cet != lc_unknown) > > + if (l->l_property != lc_property_unknown) > > return; > > > > /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in > > Ok. > > > @@ -59,7 +97,8 @@ _dl_process_cet_property_note (struct link_map *l, > > > > const ElfW(Addr) start = (ElfW(Addr)) note; > > > > - unsigned int feature_1 = 0; > > + unsigned int feature_1_and = 0; > > + unsigned int isa_1_needed = 0; > > unsigned int last_type = 0; > > > > while ((ElfW(Addr)) (note + 1) - start < size) > > @@ -71,11 +110,11 @@ _dl_process_cet_property_note (struct link_map *l, > > { > > /* Stop if we see more than one GNU property note which may > > be generated by the older linker. */ > > - if (l->l_cet != lc_unknown) > > + if (l->l_property != lc_property_unknown) > > return; > > > > - /* Check CET status now. */ > > - l->l_cet = lc_none; > > + /* Check CET status and ISA levels now. */ > > + l->l_property = lc_property_none; > > > > /* Check for invalid property. */ > > if (note->n_descsz < 8 > > @@ -101,26 +140,37 @@ _dl_process_cet_property_note (struct link_map *l, > > > > last_type = type; > > > > - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) > > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND > > + || type == GNU_PROPERTY_X86_ISA_1_NEEDED) > > { > > - /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4 > > - bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND, > > - we stop the search regardless if its size is correct > > - or not. There is no point to continue if this note > > - is ill-formed. */ > > + /* The sizes of types which we are searching for are > > + 4 bytes. There is no point to continue if this > > + note is ill-formed. */ > > if (datasz != 4) > > return; > > > > - feature_1 = *(unsigned int *) ptr; > > - > > - /* Keep searching for the next GNU property note > > - generated by the older linker. */ > > - break; > > + /* NB: Stop the scan only after seeing all types which > > + we are searching for. */ > > + _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > > > + GNU_PROPERTY_X86_FEATURE_1_AND), > > + "GNU_PROPERTY_X86_ISA_1_NEEDED > " > > + "GNU_PROPERTY_X86_FEATURE_1_AND"); > > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) > > + feature_1_and = *(unsigned int *) ptr; > > + else > > + { > > + isa_1_needed = *(unsigned int *) ptr; > > + > > + /* Keep searching for the next GNU property note > > + generated by the older linker. */ > > + break; > > + } > > } > > - else if (type > GNU_PROPERTY_X86_FEATURE_1_AND) > > + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) > > { > > - /* Stop since property type is in ascending order. */ > > - return; > > + /* Stop the scan since property type is in ascending > > + order. */ > > + break; > > } > > > > /* Check the next property item. */ > > Ok. > > > @@ -137,18 +187,21 @@ _dl_process_cet_property_note (struct link_map *l, > > } > > > > /* We get here only if there is one or no GNU property note. */ > > - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT)) > > - l->l_cet |= lc_ibt; > > - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) > > - l->l_cet |= lc_shstk; > > -#endif > > + if (isa_1_needed != 0 || feature_1_and != 0) > > + { > > + l->l_property = lc_property_valid; > > + l->l_x86_isa_1_needed = isa_1_needed; > > + l->l_x86_feature_1_and = feature_1_and; > > + } > > + else > > + l->l_property = lc_property_none; > > } > > > > static inline void __attribute__ ((unused)) > > _dl_process_pt_note (struct link_map *l, int fd, const ElfW(Phdr) *ph) > > { > > const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); > > - _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align); > > + _dl_process_property_note (l, note, ph->p_memsz, ph->p_align); > > } > > > > static inline int __attribute__ ((always_inline)) > > Ok. > > > diff --git a/sysdeps/x86/get-isa-level.h b/sysdeps/x86/get-isa-level.h > > new file mode 100644 > > index 0000000000..a86e1e8941 > > --- /dev/null > > +++ b/sysdeps/x86/get-isa-level.h > > @@ -0,0 +1,66 @@ > > +/* Get x86 ISA level. > > + This file is part of the GNU C Library. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <sys/platform/x86.h> > > + > > +/* Get GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] > > + ISA level. */ > > + > > +static unsigned int > > +get_isa_level (const struct cpu_features *cpu_features) > > +{ > > + unsigned int isa_level = 0; > > + > > + if (CPU_FEATURE_USABLE_P (cpu_features, CMOV) > > + && CPU_FEATURE_USABLE_P (cpu_features, CX8) > > + && CPU_FEATURE_CPU_P (cpu_features, FPU) > > + && CPU_FEATURE_USABLE_P (cpu_features, FXSR) > > + && CPU_FEATURE_USABLE_P (cpu_features, MMX) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE2)) > > + { > > + isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE; > > + if (CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) > > + && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) > > + && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE3) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) > > + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2)) > > + { > > + isa_level |= GNU_PROPERTY_X86_ISA_1_V2; > > + if (CPU_FEATURE_USABLE_P (cpu_features, AVX) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX2) > > + && CPU_FEATURE_USABLE_P (cpu_features, F16C) > > + && CPU_FEATURE_USABLE_P (cpu_features, FMA) > > + && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) > > + && CPU_FEATURE_USABLE_P (cpu_features, MOVBE)) > > + { > > + isa_level |= GNU_PROPERTY_X86_ISA_1_V3; > > + if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) > > + && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)) > > + isa_level |= GNU_PROPERTY_X86_ISA_1_V4; > > + } > > + } > > + } > > + > > + return isa_level; > > +} > > Ok, is there any plan to add the gcc ISA_1_VX for 32-bit ABI as well? I am > asking because if there is no upcoming plan maybe an option is just to > return 0 for !__X86_64__. Although there is no official plan to support it in 32-bit, since the ISA level support implemented in GCC and binutils is independent of 32-bit or 64-bit, 32-bit also get the same feature: [hjl@gnu-cfl-2 tmp]$ touch x.c [hjl@gnu-cfl-2 tmp]$ /usr/gcc-11.0.0-x32/bin/gcc -c -mneeded x.c -mavx2 [hjl@gnu-cfl-2 tmp]$ readelf -n x.o Displaying notes found in: .note.gnu.property Owner Data size Description GNU 0x00000010 NT_GNU_PROPERTY_TYPE_0 Properties: x86 ISA needed: x86-64-baseline, x86-64-v2, x86-64-v3 GNU 0x00000020 NT_GNU_PROPERTY_TYPE_0 Properties: x86 ISA used: x86 feature used: x86 [hjl@gnu-cfl-2 tmp]$ /usr/gcc-11.0.0-x32/bin/gcc -c -mneeded x.c -mavx2 -m32 [hjl@gnu-cfl-2 tmp]$ readelf -n x.o Displaying notes found in: .note.gnu.property Owner Data size Description GNU 0x0000000c NT_GNU_PROPERTY_TYPE_0 Properties: x86 ISA needed: x86-64-baseline, x86-64-v2, x86-64-v3 GNU 0x00000018 NT_GNU_PROPERTY_TYPE_0 Properties: x86 ISA used: x86 feature used: x86 [hjl@gnu-cfl-2 tmp]$ The glibc implementation also works on 32-bit. > > diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h > > index ffe300c931..99e7ee08cf 100644 > > --- a/sysdeps/x86/include/cpu-features.h > > +++ b/sysdeps/x86/include/cpu-features.h > > @@ -123,6 +123,8 @@ struct cpu_features > > struct cpu_features_basic basic; > > struct cpuid_features features[COMMON_CPUID_INDEX_MAX]; > > unsigned int preferred[PREFERRED_FEATURE_INDEX_MAX]; > > + /* X86 micro-architecture ISA levels. */ > > + unsigned int isa_1; > > /* The state size for XSAVEC or XSAVE. The type must be unsigned long > > int so that we use > > > > Ok. > > > diff --git a/sysdeps/x86/isa-level.c b/sysdeps/x86/isa-level.c > > new file mode 100644 > > index 0000000000..aaf524cb56 > > --- /dev/null > > +++ b/sysdeps/x86/isa-level.c > > @@ -0,0 +1,97 @@ > > +/* ELF program property for x86 ISA level. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + In addition to the permissions in the GNU Lesser General Public > > + License, the Free Software Foundation gives you unlimited > > + permission to link the compiled version of this file with other > > + programs, and to distribute those programs without any restriction > > + coming from the use of this file. (The Lesser General Public > > + License restrictions do apply in other respects; for example, they > > + cover modification of the file, and distribution when not linked > > + into another program.) > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <elf.h> > > + > > +/* ELF program property for x86 ISA level. */ > > +#ifdef INCLUDE_X86_ISA_LEVEL > > +# if defined __x86_64__ || defined __FXSR__ || !defined _SOFT_FLOAT \ > > + || defined __MMX__ || defined __SSE__ || defined __SSE2__ > > +# define ISA_BASELINE GNU_PROPERTY_X86_ISA_1_BASELINE > > +# else > > +# define ISA_BASELINE 0 > > +# endif > > + > > +# if defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 \ > > + || (defined __x86_64__ && defined __LAHF_SAHF__) \ > > + || defined __POPCNT__ || defined __SSE3__ \ > > + || defined __SSSE3__ || defined __SSE4_1__ || defined __SSE4_2__ > > +# define ISA_V2 GNU_PROPERTY_X86_ISA_1_V2 > > +# else > > +# define ISA_V2 0 > > +# endif > > + > > +# if defined __AVX__ || defined __AVX2__ || defined __F16C__ \ > > + || defined __FMA__ || defined __LZCNT__ || defined __MOVBE__ \ > > + || defined __XSAVE__ > > +# define ISA_V3 GNU_PROPERTY_X86_ISA_1_V3 > > +# else > > +# define ISA_V3 0 > > +# endif > > + > > +# if defined __AVX512F__ || defined __AVX512BW__ || defined __AVX512CD__ \ > > + || defined __AVX512DQ__ || defined __AVX512VL__ > > +# define ISA_V4 GNU_PROPERTY_X86_ISA_1_V4 > > +# else > > +# define ISA_V4 0 > > +# endif > > + > > +# ifndef ISA_LEVEL > > +# define ISA_LEVEL (ISA_BASELINE | ISA_V2 | ISA_V3 | ISA_V4) > > +# endif > > + > > +# if ISA_LEVEL > > +# ifdef __LP64__ > > +# define PROPERTY_ALIGN 3 > > +# else > > +# define PROPERTY_ALIGN 2 > > +# endif > > + > > +# define note_stringify(arg) note_stringify_1(arg) > > +# define note_stringify_1(arg) #arg > > + > > +asm(".pushsection \".note.gnu.property\",\"a\",@note\n" > > +" .p2align " note_stringify (PROPERTY_ALIGN) > > + /* name length. */ > > +"\n .long 1f - 0f\n" > > + /* data length. */ > > +" .long 4f - 1f\n" > > + /* note type: NT_GNU_PROPERTY_TYPE_0. */ > > +" .long " note_stringify (NT_GNU_PROPERTY_TYPE_0) > > + /* vendor name. */ > > +"\n0: .asciz \"GNU\"\n" > > +"1: .p2align " note_stringify (PROPERTY_ALIGN) > > + /* pr_type: GNU_PROPERTY_X86_ISA_1_NEEDED. */ > > +"\n .long " note_stringify (GNU_PROPERTY_X86_ISA_1_NEEDED) > > + /* pr_datasz. */ > > +"\n .long 3f - 2f\n" > > + /* GNU_PROPERTY_X86_ISA_1_V[234]. */ > > +"2:\n .long " note_stringify (ISA_LEVEL) > > +"\n3:\n .p2align " note_stringify (PROPERTY_ALIGN) > > +"\n4:\n .popsection"); > > +# endif /* ISA_LEVEL */ > > +#endif /* INCLUDE_X86_ISA_LEVEL */ > > Ok. > > > diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h > > index 8074739b47..4c46a25f83 100644 > > --- a/sysdeps/x86/link_map.h > > +++ b/sysdeps/x86/link_map.h > > @@ -16,12 +16,16 @@ > > License along with the GNU C Library; if not, see > > <https://www.gnu.org/licenses/>. */ > > > > -/* If this object is enabled with CET. */ > > +/* if this object has GNU property. */ > > enum > > { > > - lc_unknown = 0, /* Unknown CET status. */ > > - lc_none = 1 << 0, /* Not enabled with CET. */ > > - lc_ibt = 1 << 1, /* Enabled with IBT. */ > > - lc_shstk = 1 << 2, /* Enabled with STSHK. */ > > - lc_ibt_and_shstk = lc_ibt | lc_shstk /* Enabled with both. */ > > - } l_cet:3; > > + lc_property_unknown = 0, /* Unknown property status. */ > > + lc_property_none = 1 << 0, /* No property. */ > > + lc_property_valid = 1 << 1 /* Has valid property. */ > > + } l_property:2; > > + > > +/* GNU_PROPERTY_X86_FEATURE_1_AND of this object. */ > > +unsigned int l_x86_feature_1_and; > > + > > +/* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */ > > +unsigned int l_x86_isa_1_needed; > > Ok. > > > diff --git a/sysdeps/x86/tst-isa-level-1.c b/sysdeps/x86/tst-isa-level-1.c > > new file mode 100644 > > index 0000000000..dc31d71ad2 > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-1.c > > @@ -0,0 +1,104 @@ > > +/* Check ISA level on dlopened shared object. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <stdlib.h> > > +#include <stdbool.h> > > +#include <string.h> > > +#include <elf.h> > > +#include <get-isa-level.h> > > +#include <support/xdlfcn.h> > > +#include <support/check.h> > > +#include <support/test-driver.h> > > + > > +static void > > +do_test_1 (const char *modname, bool fail) > > +{ > > + int (*fp) (void); > > + void *h; > > + > > + h = dlopen (modname, RTLD_LAZY); > > + if (h == NULL) > > + { > > + const char *err = dlerror (); > > + if (fail) > > + { > > + if (strstr (err, "CPU ISA level is lower than required") == NULL) > > + FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err); > > + > > + return; > > + } > > + > > + FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err); > > + } > > + > > + if (fail) > > + FAIL_EXIT1 ("dlopen '%s' should have failed\n", modname); > > + > > + fp = xdlsym (h, "test"); > > + > > + if (fp () != 0) > > + FAIL_EXIT1 ("test () != 0\n"); > > + > > + dlclose (h); > > +} > > + > > +static int > > +do_test (void) > > +{ > > + const struct cpu_features *cpu_features > > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > > + unsigned int isa_level = get_isa_level (cpu_features); > > + bool has_isa_baseline = ((isa_level & GNU_PROPERTY_X86_ISA_1_BASELINE) > > + == GNU_PROPERTY_X86_ISA_1_BASELINE); > > + bool has_isa_v2 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V2) > > + == GNU_PROPERTY_X86_ISA_1_V2); > > + bool has_isa_v3 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V3) > > + == GNU_PROPERTY_X86_ISA_1_V3); > > + bool has_isa_v4 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) > > + == GNU_PROPERTY_X86_ISA_1_V4); > > + > > + if (!has_isa_baseline) > > + { > > + do_test_1 ("tst-isa-level-mod-1-baseline.so", true); > > + return EXIT_SUCCESS; > > + } > > + > > + do_test_1 ("tst-isa-level-mod-1-baseline.so", false); > > + > > + /* Skip on x86-64-v4 platforms since dlopen v4 module always works. */ > > + if (has_isa_v4) > > + return EXIT_UNSUPPORTED; > > Not sure if unsupported is the right error code here, since the isa > selection is just ignored (not really not supported). I will change it to EXIT_SUCCESS. > > + > > + do_test_1 ("tst-isa-level-mod-1-v4.so", true); > > + > > + /* Skip on x86-64-v3 platforms since dlopen v3 module always works. */ > > + if (has_isa_v3) > > + return EXIT_SUCCESS; > > + > > + do_test_1 ("tst-isa-level-mod-1-v3.so", true); > > + > > + /* Skip on x86-64-v2 platforms since dlopen v2 module always works. */ > > + if (has_isa_v2) > > + return EXIT_SUCCESS; > > + > > + do_test_1 ("tst-isa-level-mod-1-v2.so", true); > > + > > + return EXIT_SUCCESS; > > +} > > + > > +#include <support/test-driver.c> > > diff --git a/sysdeps/x86/tst-isa-level-mod-1-baseline.c b/sysdeps/x86/tst-isa-level-mod-1-baseline.c > > new file mode 100644 > > index 0000000000..d6fe0685a4 > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-mod-1-baseline.c > > @@ -0,0 +1 @@ > > +#include "tst-isa-level-mod-1.c" > > diff --git a/sysdeps/x86/tst-isa-level-mod-1-v2.c b/sysdeps/x86/tst-isa-level-mod-1-v2.c > > new file mode 100644 > > index 0000000000..d6fe0685a4 > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-mod-1-v2.c > > @@ -0,0 +1 @@ > > +#include "tst-isa-level-mod-1.c" > > diff --git a/sysdeps/x86/tst-isa-level-mod-1-v3.c b/sysdeps/x86/tst-isa-level-mod-1-v3.c > > new file mode 100644 > > index 0000000000..d6fe0685a4 > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-mod-1-v3.c > > @@ -0,0 +1 @@ > > +#include "tst-isa-level-mod-1.c" > > diff --git a/sysdeps/x86/tst-isa-level-mod-1-v4.c b/sysdeps/x86/tst-isa-level-mod-1-v4.c > > new file mode 100644 > > index 0000000000..d6fe0685a4 > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-mod-1-v4.c > > @@ -0,0 +1 @@ > > +#include "tst-isa-level-mod-1.c" > > Ok. > > > diff --git a/sysdeps/x86/tst-isa-level-mod-1.c b/sysdeps/x86/tst-isa-level-mod-1.c > > new file mode 100644 > > index 0000000000..40a2e1d99f > > --- /dev/null > > +++ b/sysdeps/x86/tst-isa-level-mod-1.c > > @@ -0,0 +1,25 @@ > > +/* Check ISA level on dlopened shared object. > > + Copyright (C) 2019-2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <isa-level.c> > > + > > +int > > +test (void) > > +{ > > + return 0; > > +} > > Ok. > > > diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c > > index 0aaa635a00..e030534f1b 100644 > > --- a/sysdeps/x86_64/dl-hwcaps-subdirs.c > > +++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c > > @@ -18,6 +18,7 @@ > > > > #include <dl-hwcaps.h> > > #include <cpu-features.h> > > +#include <get-isa-level.h> > > > > const char _dl_hwcaps_subdirs[] = "x86-64-v4:x86-64-v3:x86-64-v2"; > > enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ > > @@ -25,40 +26,25 @@ enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ > > uint32_t > > _dl_hwcaps_subdirs_active (void) > > { > > + const struct cpu_features *cpu_features > > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > > + unsigned int isa_level = get_isa_level (cpu_features); > > int active = 0; > > > > /* Test in reverse preference order. */ > > > > /* x86-64-v2. */ > > - if (!(CPU_FEATURE_USABLE (CMPXCHG16B) > > - && CPU_FEATURE_USABLE (LAHF64_SAHF64) > > - && CPU_FEATURE_USABLE (POPCNT) > > - && CPU_FEATURE_USABLE (SSE3) > > - && CPU_FEATURE_USABLE (SSE4_1) > > - && CPU_FEATURE_USABLE (SSE4_2) > > - && CPU_FEATURE_USABLE (SSSE3))) > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) > > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > > ++active; > > > > /* x86-64-v3. */ > > - if (!(CPU_FEATURE_USABLE (AVX) > > - && CPU_FEATURE_USABLE (AVX2) > > - && CPU_FEATURE_USABLE (BMI1) > > - && CPU_FEATURE_USABLE (BMI2) > > - && CPU_FEATURE_USABLE (F16C) > > - && CPU_FEATURE_USABLE (FMA) > > - && CPU_FEATURE_USABLE (LZCNT) > > - && CPU_FEATURE_USABLE (MOVBE) > > - && CPU_FEATURE_USABLE (OSXSAVE))) > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) > > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > > ++active; > > > > - /* x86-64-v4. */ > > - if (!(CPU_FEATURE_USABLE (AVX512F) > > - && CPU_FEATURE_USABLE (AVX512BW) > > - && CPU_FEATURE_USABLE (AVX512CD) > > - && CPU_FEATURE_USABLE (AVX512DQ) > > - && CPU_FEATURE_USABLE (AVX512VL))) > > + /* x86-64-v4. */ > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) > > return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); > > ++active; > > > > diff --git a/sysdeps/x86_64/tst-glibc-hwcaps.c b/sysdeps/x86_64/tst-glibc-hwcaps.c > > index 8bdcb91db0..37b61d3c84 100644 > > --- a/sysdeps/x86_64/tst-glibc-hwcaps.c > > +++ b/sysdeps/x86_64/tst-glibc-hwcaps.c > > @@ -19,7 +19,8 @@ > > #include <stdio.h> > > #include <support/check.h> > > #include <sys/param.h> > > -#include <sys/platform/x86.h> > > +#include <elf.h> > > +#include <get-isa-level.h> > > > > extern int marker2 (void); > > extern int marker3 (void); > > @@ -31,35 +32,15 @@ compute_level (void) > > { > > const struct cpu_features *cpu_features > > = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > > - > > - if (!(CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) > > - && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) > > - && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) > > - && CPU_FEATURE_USABLE_P (cpu_features, MMX) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE2) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE3) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) > > - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2))) > > - return 1; > > - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX2) > > - && CPU_FEATURE_USABLE_P (cpu_features, BMI1) > > - && CPU_FEATURE_USABLE_P (cpu_features, BMI2) > > - && CPU_FEATURE_USABLE_P (cpu_features, F16C) > > - && CPU_FEATURE_USABLE_P (cpu_features, FMA) > > - && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) > > - && CPU_FEATURE_USABLE_P (cpu_features, MOVBE) > > - && CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE))) > > - return 2; > > - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX512F) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) > > - && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL))) > > - return 3; > > - return 4; > > + unsigned int isa_level = get_isa_level (cpu_features); > > + > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) > > + return 1; > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) > > + return 2; > > + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) > > + return 3; > > + return 4; > > } > > > > static int > > -- > > 2.29.2 > > Ok. This is the patch I am checking in. Thanks. -- H.J. [-- Attachment #2: 0001-x86-Support-GNU_PROPERTY_X86_ISA_1_V-234-marker-BZ-2.patch --] [-- Type: text/x-patch, Size: 40535 bytes --] From 6dfa1ac62635e05d13600bd1e6e0a770a44875b4 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.tools@gmail.com> Date: Fri, 9 Oct 2020 06:06:56 -0700 Subject: [PATCH] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] GCC 11 supports -march=x86-64-v[234] to enable x86 micro-architecture ISA levels: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97250 and -mneeded to emit GNU_PROPERTY_X86_ISA_1_NEEDED property with GNU_PROPERTY_X86_ISA_1_V[234] marker: https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/13 Binutils support for GNU_PROPERTY_X86_ISA_1_V[234] marker were added by commit b0ab06937385e0ae25cebf1991787d64f439bf12 Author: H.J. Lu <hjl.tools@gmail.com> Date: Fri Oct 30 06:49:57 2020 -0700 x86: Support GNU_PROPERTY_X86_ISA_1_BASELINE marker and commit 32930e4edbc06bc6f10c435dbcc63131715df678 Author: H.J. Lu <hjl.tools@gmail.com> Date: Fri Oct 9 05:05:57 2020 -0700 x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker GNU_PROPERTY_X86_ISA_1_NEEDED property in x86 ELF binaries indicate the micro-architecture ISA level required to execute the binary. The marker must be added by programmers explicitly in one of 3 ways: 1. Pass -mneeded to GCC. 2. Add the marker in the linker inputs as this patch does. 3. Pass -z x86-64-v[234] to the linker. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] marker support to ld.so if binutils 2.32 or newer is used to build glibc: 1. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] markers to elf.h. 2. Add GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] marker to abi-note.o based on the ISA level used to compile abi-note.o, assuming that the same ISA level is used to compile the whole glibc. 3. Add isa_1 to cpu_features to record the supported x86 ISA level. 4. Rename _dl_process_cet_property_note to _dl_process_property_note and add GNU_PROPERTY_X86_ISA_1_V[234] marker detection. 5. Update _rtld_main_check and _dl_open_check to check loaded objects with the incompatible ISA level. 6. Add a testcase to verify that dlopen an x86-64-v4 shared object fails on lesser platforms. 7. Use <get-isa-level.h> in dl-hwcaps-subdirs.c and tst-glibc-hwcaps.c. Tested under i686, x32 and x86-64 modes on x86-64-v2, x86-64-v3 and x86-64-v4 machines. Marked elf/tst-isa-level-1 with x86-64-v4, ran it on x86-64-v3 machine and got: [hjl@gnu-cfl-2 build-x86_64-linux]$ ./elf/tst-isa-level-1 ./elf/tst-isa-level-1: CPU ISA level is lower than required [hjl@gnu-cfl-2 build-x86_64-linux]$ --- config.h.in | 3 + elf/elf.h | 35 +++---- sysdeps/x86/Makefile | 30 ++++++ sysdeps/x86/abi-note.c | 29 ++++++ sysdeps/x86/configure | 79 ++++++++++++++ sysdeps/x86/configure.ac | 66 ++++++++++++ sysdeps/x86/cpu-features.c | 3 + sysdeps/x86/dl-cet.c | 12 ++- sysdeps/x86/dl-prop.h | 113 +++++++++++++++------ sysdeps/x86/get-isa-level.h | 66 ++++++++++++ sysdeps/x86/include/cpu-features.h | 2 + sysdeps/x86/isa-level.c | 97 ++++++++++++++++++ sysdeps/x86/link_map.h | 18 ++-- sysdeps/x86/tst-isa-level-1.c | 104 +++++++++++++++++++ sysdeps/x86/tst-isa-level-mod-1-baseline.c | 1 + sysdeps/x86/tst-isa-level-mod-1-v2.c | 1 + sysdeps/x86/tst-isa-level-mod-1-v3.c | 1 + sysdeps/x86/tst-isa-level-mod-1-v4.c | 1 + sysdeps/x86/tst-isa-level-mod-1.c | 25 +++++ sysdeps/x86_64/dl-hwcaps-subdirs.c | 30 ++---- sysdeps/x86_64/tst-glibc-hwcaps.c | 41 ++------ 21 files changed, 644 insertions(+), 113 deletions(-) create mode 100644 sysdeps/x86/abi-note.c create mode 100644 sysdeps/x86/get-isa-level.h create mode 100644 sysdeps/x86/isa-level.c create mode 100644 sysdeps/x86/tst-isa-level-1.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1-baseline.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v2.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v3.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1-v4.c create mode 100644 sysdeps/x86/tst-isa-level-mod-1.c diff --git a/config.h.in b/config.h.in index eca2d66a2c..947feeff36 100644 --- a/config.h.in +++ b/config.h.in @@ -269,4 +269,7 @@ /* The default value of x86 CET control. */ #define DEFAULT_DL_X86_CET_CONTROL cet_elf_property +/* Define if x86 ISA level should be included in shared libraries. */ +#undef INCLUDE_X86_ISA_LEVEL + #endif diff --git a/elf/elf.h b/elf/elf.h index 4a9ae97dfc..9ebd052c8a 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -1324,31 +1324,26 @@ typedef struct /* The x86 instruction sets indicated by the corresponding bits are used in program. Their support in the hardware is optional. */ -#define GNU_PROPERTY_X86_ISA_1_USED 0xc0000000 +#define GNU_PROPERTY_X86_ISA_1_USED 0xc0010002 /* The x86 instruction sets indicated by the corresponding bits are used in program and they must be supported by the hardware. */ -#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0000001 +#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0008002 /* X86 processor-specific features used in program. */ #define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 -#define GNU_PROPERTY_X86_ISA_1_486 (1U << 0) -#define GNU_PROPERTY_X86_ISA_1_586 (1U << 1) -#define GNU_PROPERTY_X86_ISA_1_686 (1U << 2) -#define GNU_PROPERTY_X86_ISA_1_SSE (1U << 3) -#define GNU_PROPERTY_X86_ISA_1_SSE2 (1U << 4) -#define GNU_PROPERTY_X86_ISA_1_SSE3 (1U << 5) -#define GNU_PROPERTY_X86_ISA_1_SSSE3 (1U << 6) -#define GNU_PROPERTY_X86_ISA_1_SSE4_1 (1U << 7) -#define GNU_PROPERTY_X86_ISA_1_SSE4_2 (1U << 8) -#define GNU_PROPERTY_X86_ISA_1_AVX (1U << 9) -#define GNU_PROPERTY_X86_ISA_1_AVX2 (1U << 10) -#define GNU_PROPERTY_X86_ISA_1_AVX512F (1U << 11) -#define GNU_PROPERTY_X86_ISA_1_AVX512CD (1U << 12) -#define GNU_PROPERTY_X86_ISA_1_AVX512ER (1U << 13) -#define GNU_PROPERTY_X86_ISA_1_AVX512PF (1U << 14) -#define GNU_PROPERTY_X86_ISA_1_AVX512VL (1U << 15) -#define GNU_PROPERTY_X86_ISA_1_AVX512DQ (1U << 16) -#define GNU_PROPERTY_X86_ISA_1_AVX512BW (1U << 17) +/* GNU_PROPERTY_X86_ISA_1_BASELINE: CMOV, CX8 (cmpxchg8b), FPU (fld), + MMX, OSFXSR (fxsave), SCE (syscall), SSE and SSE2. */ +#define GNU_PROPERTY_X86_ISA_1_BASELINE (1U << 0) +/* GNU_PROPERTY_X86_ISA_1_V2: GNU_PROPERTY_X86_ISA_1_BASELINE, + CMPXCHG16B (cmpxchg16b), LAHF-SAHF (lahf), POPCNT (popcnt), SSE3, + SSSE3, SSE4.1 and SSE4.2. */ +#define GNU_PROPERTY_X86_ISA_1_V2 (1U << 1) +/* GNU_PROPERTY_X86_ISA_1_V3: GNU_PROPERTY_X86_ISA_1_V2, AVX, AVX2, BMI1, + BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE. */ +#define GNU_PROPERTY_X86_ISA_1_V3 (1U << 2) +/* GNU_PROPERTY_X86_ISA_1_V4: GNU_PROPERTY_X86_ISA_1_V3, AVX512F, + AVX512BW, AVX512CD, AVX512DQ and AVX512VL. */ +#define GNU_PROPERTY_X86_ISA_1_V4 (1U << 3) /* This indicates that all executable sections are compatible with IBT. */ diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index 081cc72e93..c814d5a195 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -9,6 +9,36 @@ sysdep_headers += sys/platform/x86.h tests += tst-get-cpu-features tst-get-cpu-features-static \ tst-cpu-features-cpuinfo tst-cpu-features-supports tests-static += tst-get-cpu-features-static +ifeq (yes,$(enable-x86-isa-level)) +tests += tst-isa-level-1 +modules-names += tst-isa-level-mod-1-baseline \ + tst-isa-level-mod-1-v2 \ + tst-isa-level-mod-1-v3 \ + tst-isa-level-mod-1-v4 \ + +# X86 ISA level baseline +CFLAGS-tst-isa-level-mod-1-baseline.c += -DINCLUDE_X86_ISA_LEVEL \ + -DISA_LEVEL=0x1 \ + -march=x86-64 +# X86 ISA level v2 +CFLAGS-tst-isa-level-mod-1-v2.c += -DINCLUDE_X86_ISA_LEVEL \ + -DISA_LEVEL=0x3 \ + -march=x86-64 +# X86 ISA level v3 +CFLAGS-tst-isa-level-mod-1-v3.c += -DINCLUDE_X86_ISA_LEVEL \ + -DISA_LEVEL=0x7 \ + -march=x86-64 +# X86 ISA level v4 +CFLAGS-tst-isa-level-mod-1-v4.c += -DINCLUDE_X86_ISA_LEVEL \ + -DISA_LEVEL=0xf \ + -march=x86-64 + +$(objpfx)tst-isa-level-1: $(libdl) +$(objpfx)tst-isa-level-1.out: $(objpfx)tst-isa-level-mod-1-baseline.so \ + $(objpfx)tst-isa-level-mod-1-v2.so \ + $(objpfx)tst-isa-level-mod-1-v3.so \ + $(objpfx)tst-isa-level-mod-1-v4.so +endif endif ifeq ($(subdir),math) diff --git a/sysdeps/x86/abi-note.c b/sysdeps/x86/abi-note.c new file mode 100644 index 0000000000..5140e6ab47 --- /dev/null +++ b/sysdeps/x86/abi-note.c @@ -0,0 +1,29 @@ +/* Special .init and .fini section support. x86-64 version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + In addition to the permissions in the GNU Lesser General Public + License, the Free Software Foundation gives you unlimited + permission to link the compiled version of this file with other + programs, and to distribute those programs without any restriction + coming from the use of this file. (The Lesser General Public + License restrictions do apply in other respects; for example, they + cover modification of the file, and distribution when not linked + into another program.) + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <isa-level.c> +#include <csu/abi-note.c> diff --git a/sysdeps/x86/configure b/sysdeps/x86/configure index 81cc4e80d6..5e32dc62b3 100644 --- a/sysdeps/x86/configure +++ b/sysdeps/x86/configure @@ -68,3 +68,82 @@ elif test $enable_cet = permissive; then fi config_vars="$config_vars enable-cet = $enable_cet" + +# Check if linker supports x86 ISA level. +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker x86 ISA level support" >&5 +$as_echo_n "checking for linker x86 ISA level support... " >&6; } +if ${libc_cv_include_x86_isa_level+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat > conftest1.S <<EOF +#ifdef __LP64__ +# define P2ALIGN 3 +#else +# define P2ALIGN 2 +#endif + .section ".note.gnu.property", "a" + .p2align P2ALIGN + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + /* NT_GNU_PROPERTY_TYPE_0 */ + .long 5 /* note type. */ +0: + .asciz "GNU" /* vendor name. */ +1: + .p2align P2ALIGN + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ + .long 0xc0008002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x1 +3: + .p2align P2ALIGN +4: +EOF +cat > conftest2.S <<EOF +#ifdef __LP64__ +# define P2ALIGN 3 +#else +# define P2ALIGN 2 +#endif + .section ".note.gnu.property", "a" + .p2align P2ALIGN + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + /* NT_GNU_PROPERTY_TYPE_0 */ + .long 5 /* note type. */ +0: + .asciz "GNU" /* vendor name. */ +1: + .p2align P2ALIGN + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ + .long 0xc0008002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x2 +3: + .p2align P2ALIGN +4: +EOF +libc_cv_include_x86_isa_level=no +if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS -nostartfiles -nostdlib -r -o conftest conftest1.S conftest2.S' + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 + (eval $ac_try) 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; }; then + count=`LC_ALL=C $READELF -n conftest | grep NT_GNU_PROPERTY_TYPE_0 | wc -l` + if test "$count" = 1; then + libc_cv_include_x86_isa_level=yes + fi +fi +rm -f conftest* +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_include_x86_isa_level" >&5 +$as_echo "$libc_cv_include_x86_isa_level" >&6; } +if test $libc_cv_include_x86_isa_level = yes; then + $as_echo "#define INCLUDE_X86_ISA_LEVEL 1" >>confdefs.h + +fi +config_vars="$config_vars +enable-x86-isa-level = $libc_cv_include_x86_isa_level" diff --git a/sysdeps/x86/configure.ac b/sysdeps/x86/configure.ac index 8f3e1191f6..f94088f377 100644 --- a/sysdeps/x86/configure.ac +++ b/sysdeps/x86/configure.ac @@ -43,3 +43,69 @@ elif test $enable_cet = permissive; then AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_permissive) fi LIBC_CONFIG_VAR([enable-cet], [$enable_cet]) + +# Check if linker supports x86 ISA level. +AC_CACHE_CHECK([for linker x86 ISA level support], + libc_cv_include_x86_isa_level, [dnl +cat > conftest1.S <<EOF +#ifdef __LP64__ +# define P2ALIGN 3 +#else +# define P2ALIGN 2 +#endif + .section ".note.gnu.property", "a" + .p2align P2ALIGN + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + /* NT_GNU_PROPERTY_TYPE_0 */ + .long 5 /* note type. */ +0: + .asciz "GNU" /* vendor name. */ +1: + .p2align P2ALIGN + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ + .long 0xc0008002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x1 +3: + .p2align P2ALIGN +4: +EOF +cat > conftest2.S <<EOF +#ifdef __LP64__ +# define P2ALIGN 3 +#else +# define P2ALIGN 2 +#endif + .section ".note.gnu.property", "a" + .p2align P2ALIGN + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + /* NT_GNU_PROPERTY_TYPE_0 */ + .long 5 /* note type. */ +0: + .asciz "GNU" /* vendor name. */ +1: + .p2align P2ALIGN + /* GNU_PROPERTY_X86_ISA_1_NEEDED */ + .long 0xc0008002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x2 +3: + .p2align P2ALIGN +4: +EOF +libc_cv_include_x86_isa_level=no +if AC_TRY_COMMAND(${CC-cc} $CFLAGS $CPPFLAGS -nostartfiles -nostdlib -r -o conftest conftest1.S conftest2.S); then + count=`LC_ALL=C $READELF -n conftest | grep NT_GNU_PROPERTY_TYPE_0 | wc -l` + if test "$count" = 1; then + libc_cv_include_x86_isa_level=yes + fi +fi +rm -f conftest*]) +if test $libc_cv_include_x86_isa_level = yes; then + AC_DEFINE(INCLUDE_X86_ISA_LEVEL) +fi +LIBC_CONFIG_VAR([enable-x86-isa-level], [$libc_cv_include_x86_isa_level]) diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c index 529c586193..e7da682a2e 100644 --- a/sysdeps/x86/cpu-features.c +++ b/sysdeps/x86/cpu-features.c @@ -19,6 +19,7 @@ #include <cpuid.h> #include <dl-hwcap.h> #include <libc-pointer-arith.h> +#include <get-isa-level.h> #if IS_IN (libc) && !defined SHARED # include <assert.h> # include <unistd.h> @@ -290,6 +291,8 @@ update_usable (struct cpu_features *cpu_features) CPU_FEATURE_SET_USABLE (cpu_features, KL); CPU_FEATURE_SET_USABLE (cpu_features, WIDE_KL); } + + cpu_features->isa_1 = get_isa_level (cpu_features); } static void diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c index b4910f7634..ed7fd6d10d 100644 --- a/sysdeps/x86/dl-cet.c +++ b/sysdeps/x86/dl-cet.c @@ -76,10 +76,12 @@ dl_cet_check (struct link_map *m, const char *program) */ enable_ibt &= (HAS_CPU_FEATURE (IBT) && (enable_ibt_type == cet_always_on - || (m->l_cet & lc_ibt) != 0)); + || (m->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0)); enable_shstk &= (HAS_CPU_FEATURE (SHSTK) && (enable_shstk_type == cet_always_on - || (m->l_cet & lc_shstk) != 0)); + || (m->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0)); } /* ld.so is CET-enabled by kernel. But shared objects may not @@ -111,7 +113,8 @@ dl_cet_check (struct link_map *m, const char *program) /* IBT is enabled only if it is enabled in executable as well as all shared objects. */ enable_ibt &= (enable_ibt_type == cet_always_on - || (l->l_cet & lc_ibt) != 0); + || (l->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0); if (!found_ibt_legacy && enable_ibt != ibt_enabled) { found_ibt_legacy = true; @@ -121,7 +124,8 @@ dl_cet_check (struct link_map *m, const char *program) /* SHSTK is enabled only if it is enabled in executable as well as all shared objects. */ enable_shstk &= (enable_shstk_type == cet_always_on - || (l->l_cet & lc_shstk) != 0); + || (l->l_x86_feature_1_and + & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0); if (enable_shstk != shstk_enabled) { found_shstk_legacy = true; diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h index ce1667dc7a..56bd020b3c 100644 --- a/sysdeps/x86/dl-prop.h +++ b/sysdeps/x86/dl-prop.h @@ -19,14 +19,54 @@ #ifndef _DL_PROP_H #define _DL_PROP_H +#include <libintl.h> + extern void _dl_cet_check (struct link_map *, const char *) attribute_hidden; extern void _dl_cet_open_check (struct link_map *) attribute_hidden; +static void +dl_isa_level_check (struct link_map *m, const char *program) +{ + const struct cpu_features *cpu_features = __get_cpu_features (); + unsigned int i; + struct link_map *l; + + i = m->l_searchlist.r_nlist; + while (i-- > 0) + { + /* Check each shared object to see if ISA level is compatible. */ + l = m->l_initfini[i]; + + /* Skip ISA level check if functions have been executed. */ + if (l->l_init_called) + continue; + +#ifdef SHARED + /* Skip ISA level check for ld.so since ld.so won't run if its ISA + level is higher than CPU. */ + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) + continue; +#endif + + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) + != l->l_x86_isa_1_needed) + { + if (program) + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", + *l->l_name != '\0' ? l->l_name : program); + else + _dl_signal_error (0, l->l_name, "dlopen", + N_("CPU ISA level is lower than required")); + } + } +} + static inline void __attribute__ ((always_inline)) _rtld_main_check (struct link_map *m, const char *program) { + dl_isa_level_check (m, program); #if CET_ENABLED _dl_cet_check (m, program); #endif @@ -35,20 +75,18 @@ _rtld_main_check (struct link_map *m, const char *program) static inline void __attribute__ ((always_inline)) _dl_open_check (struct link_map *m) { + dl_isa_level_check (m, NULL); #if CET_ENABLED _dl_cet_open_check (m); #endif } static inline void __attribute__ ((unused)) -_dl_process_cet_property_note (struct link_map *l, - const ElfW(Nhdr) *note, - const ElfW(Addr) size, - const ElfW(Addr) align) +_dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, + const ElfW(Addr) size, const ElfW(Addr) align) { -#if CET_ENABLED /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before. */ - if (l->l_cet != lc_unknown) + if (l->l_property != lc_property_unknown) return; /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in @@ -59,7 +97,8 @@ _dl_process_cet_property_note (struct link_map *l, const ElfW(Addr) start = (ElfW(Addr)) note; - unsigned int feature_1 = 0; + unsigned int feature_1_and = 0; + unsigned int isa_1_needed = 0; unsigned int last_type = 0; while ((ElfW(Addr)) (note + 1) - start < size) @@ -71,11 +110,11 @@ _dl_process_cet_property_note (struct link_map *l, { /* Stop if we see more than one GNU property note which may be generated by the older linker. */ - if (l->l_cet != lc_unknown) + if (l->l_property != lc_property_unknown) return; - /* Check CET status now. */ - l->l_cet = lc_none; + /* Check CET status and ISA levels now. */ + l->l_property = lc_property_none; /* Check for invalid property. */ if (note->n_descsz < 8 @@ -101,26 +140,37 @@ _dl_process_cet_property_note (struct link_map *l, last_type = type; - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) + if (type == GNU_PROPERTY_X86_FEATURE_1_AND + || type == GNU_PROPERTY_X86_ISA_1_NEEDED) { - /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4 - bytes. When seeing GNU_PROPERTY_X86_FEATURE_1_AND, - we stop the search regardless if its size is correct - or not. There is no point to continue if this note - is ill-formed. */ + /* The sizes of types which we are searching for are + 4 bytes. There is no point to continue if this + note is ill-formed. */ if (datasz != 4) return; - feature_1 = *(unsigned int *) ptr; - - /* Keep searching for the next GNU property note - generated by the older linker. */ - break; + /* NB: Stop the scan only after seeing all types which + we are searching for. */ + _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED > + GNU_PROPERTY_X86_FEATURE_1_AND), + "GNU_PROPERTY_X86_ISA_1_NEEDED > " + "GNU_PROPERTY_X86_FEATURE_1_AND"); + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) + feature_1_and = *(unsigned int *) ptr; + else + { + isa_1_needed = *(unsigned int *) ptr; + + /* Keep searching for the next GNU property note + generated by the older linker. */ + break; + } } - else if (type > GNU_PROPERTY_X86_FEATURE_1_AND) + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) { - /* Stop since property type is in ascending order. */ - return; + /* Stop the scan since property type is in ascending + order. */ + break; } /* Check the next property item. */ @@ -137,18 +187,21 @@ _dl_process_cet_property_note (struct link_map *l, } /* We get here only if there is one or no GNU property note. */ - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT)) - l->l_cet |= lc_ibt; - if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK)) - l->l_cet |= lc_shstk; -#endif + if (isa_1_needed != 0 || feature_1_and != 0) + { + l->l_property = lc_property_valid; + l->l_x86_isa_1_needed = isa_1_needed; + l->l_x86_feature_1_and = feature_1_and; + } + else + l->l_property = lc_property_none; } static inline void __attribute__ ((unused)) _dl_process_pt_note (struct link_map *l, int fd, const ElfW(Phdr) *ph) { const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr); - _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align); + _dl_process_property_note (l, note, ph->p_memsz, ph->p_align); } static inline int __attribute__ ((always_inline)) diff --git a/sysdeps/x86/get-isa-level.h b/sysdeps/x86/get-isa-level.h new file mode 100644 index 0000000000..a86e1e8941 --- /dev/null +++ b/sysdeps/x86/get-isa-level.h @@ -0,0 +1,66 @@ +/* Get x86 ISA level. + This file is part of the GNU C Library. + Copyright (C) 2020 Free Software Foundation, Inc. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <sys/platform/x86.h> + +/* Get GNU_PROPERTY_X86_ISA_1_BASELINE and GNU_PROPERTY_X86_ISA_1_V[234] + ISA level. */ + +static unsigned int +get_isa_level (const struct cpu_features *cpu_features) +{ + unsigned int isa_level = 0; + + if (CPU_FEATURE_USABLE_P (cpu_features, CMOV) + && CPU_FEATURE_USABLE_P (cpu_features, CX8) + && CPU_FEATURE_CPU_P (cpu_features, FPU) + && CPU_FEATURE_USABLE_P (cpu_features, FXSR) + && CPU_FEATURE_USABLE_P (cpu_features, MMX) + && CPU_FEATURE_USABLE_P (cpu_features, SSE) + && CPU_FEATURE_USABLE_P (cpu_features, SSE2)) + { + isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE; + if (CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) + && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) + && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) + && CPU_FEATURE_USABLE_P (cpu_features, SSE3) + && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) + && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2)) + { + isa_level |= GNU_PROPERTY_X86_ISA_1_V2; + if (CPU_FEATURE_USABLE_P (cpu_features, AVX) + && CPU_FEATURE_USABLE_P (cpu_features, AVX2) + && CPU_FEATURE_USABLE_P (cpu_features, F16C) + && CPU_FEATURE_USABLE_P (cpu_features, FMA) + && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) + && CPU_FEATURE_USABLE_P (cpu_features, MOVBE)) + { + isa_level |= GNU_PROPERTY_X86_ISA_1_V3; + if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) + && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)) + isa_level |= GNU_PROPERTY_X86_ISA_1_V4; + } + } + } + + return isa_level; +} diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h index ffe300c931..99e7ee08cf 100644 --- a/sysdeps/x86/include/cpu-features.h +++ b/sysdeps/x86/include/cpu-features.h @@ -123,6 +123,8 @@ struct cpu_features struct cpu_features_basic basic; struct cpuid_features features[COMMON_CPUID_INDEX_MAX]; unsigned int preferred[PREFERRED_FEATURE_INDEX_MAX]; + /* X86 micro-architecture ISA levels. */ + unsigned int isa_1; /* The state size for XSAVEC or XSAVE. The type must be unsigned long int so that we use diff --git a/sysdeps/x86/isa-level.c b/sysdeps/x86/isa-level.c new file mode 100644 index 0000000000..aaf524cb56 --- /dev/null +++ b/sysdeps/x86/isa-level.c @@ -0,0 +1,97 @@ +/* ELF program property for x86 ISA level. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + In addition to the permissions in the GNU Lesser General Public + License, the Free Software Foundation gives you unlimited + permission to link the compiled version of this file with other + programs, and to distribute those programs without any restriction + coming from the use of this file. (The Lesser General Public + License restrictions do apply in other respects; for example, they + cover modification of the file, and distribution when not linked + into another program.) + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <elf.h> + +/* ELF program property for x86 ISA level. */ +#ifdef INCLUDE_X86_ISA_LEVEL +# if defined __x86_64__ || defined __FXSR__ || !defined _SOFT_FLOAT \ + || defined __MMX__ || defined __SSE__ || defined __SSE2__ +# define ISA_BASELINE GNU_PROPERTY_X86_ISA_1_BASELINE +# else +# define ISA_BASELINE 0 +# endif + +# if defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 \ + || (defined __x86_64__ && defined __LAHF_SAHF__) \ + || defined __POPCNT__ || defined __SSE3__ \ + || defined __SSSE3__ || defined __SSE4_1__ || defined __SSE4_2__ +# define ISA_V2 GNU_PROPERTY_X86_ISA_1_V2 +# else +# define ISA_V2 0 +# endif + +# if defined __AVX__ || defined __AVX2__ || defined __F16C__ \ + || defined __FMA__ || defined __LZCNT__ || defined __MOVBE__ \ + || defined __XSAVE__ +# define ISA_V3 GNU_PROPERTY_X86_ISA_1_V3 +# else +# define ISA_V3 0 +# endif + +# if defined __AVX512F__ || defined __AVX512BW__ || defined __AVX512CD__ \ + || defined __AVX512DQ__ || defined __AVX512VL__ +# define ISA_V4 GNU_PROPERTY_X86_ISA_1_V4 +# else +# define ISA_V4 0 +# endif + +# ifndef ISA_LEVEL +# define ISA_LEVEL (ISA_BASELINE | ISA_V2 | ISA_V3 | ISA_V4) +# endif + +# if ISA_LEVEL +# ifdef __LP64__ +# define PROPERTY_ALIGN 3 +# else +# define PROPERTY_ALIGN 2 +# endif + +# define note_stringify(arg) note_stringify_1(arg) +# define note_stringify_1(arg) #arg + +asm(".pushsection \".note.gnu.property\",\"a\",@note\n" +" .p2align " note_stringify (PROPERTY_ALIGN) + /* name length. */ +"\n .long 1f - 0f\n" + /* data length. */ +" .long 4f - 1f\n" + /* note type: NT_GNU_PROPERTY_TYPE_0. */ +" .long " note_stringify (NT_GNU_PROPERTY_TYPE_0) + /* vendor name. */ +"\n0: .asciz \"GNU\"\n" +"1: .p2align " note_stringify (PROPERTY_ALIGN) + /* pr_type: GNU_PROPERTY_X86_ISA_1_NEEDED. */ +"\n .long " note_stringify (GNU_PROPERTY_X86_ISA_1_NEEDED) + /* pr_datasz. */ +"\n .long 3f - 2f\n" + /* GNU_PROPERTY_X86_ISA_1_V[234]. */ +"2:\n .long " note_stringify (ISA_LEVEL) +"\n3:\n .p2align " note_stringify (PROPERTY_ALIGN) +"\n4:\n .popsection"); +# endif /* ISA_LEVEL */ +#endif /* INCLUDE_X86_ISA_LEVEL */ diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h index 8074739b47..4c46a25f83 100644 --- a/sysdeps/x86/link_map.h +++ b/sysdeps/x86/link_map.h @@ -16,12 +16,16 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ -/* If this object is enabled with CET. */ +/* if this object has GNU property. */ enum { - lc_unknown = 0, /* Unknown CET status. */ - lc_none = 1 << 0, /* Not enabled with CET. */ - lc_ibt = 1 << 1, /* Enabled with IBT. */ - lc_shstk = 1 << 2, /* Enabled with STSHK. */ - lc_ibt_and_shstk = lc_ibt | lc_shstk /* Enabled with both. */ - } l_cet:3; + lc_property_unknown = 0, /* Unknown property status. */ + lc_property_none = 1 << 0, /* No property. */ + lc_property_valid = 1 << 1 /* Has valid property. */ + } l_property:2; + +/* GNU_PROPERTY_X86_FEATURE_1_AND of this object. */ +unsigned int l_x86_feature_1_and; + +/* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */ +unsigned int l_x86_isa_1_needed; diff --git a/sysdeps/x86/tst-isa-level-1.c b/sysdeps/x86/tst-isa-level-1.c new file mode 100644 index 0000000000..cf611e8b78 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-1.c @@ -0,0 +1,104 @@ +/* Check ISA level on dlopened shared object. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <elf.h> +#include <get-isa-level.h> +#include <support/xdlfcn.h> +#include <support/check.h> +#include <support/test-driver.h> + +static void +do_test_1 (const char *modname, bool fail) +{ + int (*fp) (void); + void *h; + + h = dlopen (modname, RTLD_LAZY); + if (h == NULL) + { + const char *err = dlerror (); + if (fail) + { + if (strstr (err, "CPU ISA level is lower than required") == NULL) + FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err); + + return; + } + + FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err); + } + + if (fail) + FAIL_EXIT1 ("dlopen '%s' should have failed\n", modname); + + fp = xdlsym (h, "test"); + + if (fp () != 0) + FAIL_EXIT1 ("test () != 0\n"); + + dlclose (h); +} + +static int +do_test (void) +{ + const struct cpu_features *cpu_features + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); + unsigned int isa_level = get_isa_level (cpu_features); + bool has_isa_baseline = ((isa_level & GNU_PROPERTY_X86_ISA_1_BASELINE) + == GNU_PROPERTY_X86_ISA_1_BASELINE); + bool has_isa_v2 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V2) + == GNU_PROPERTY_X86_ISA_1_V2); + bool has_isa_v3 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V3) + == GNU_PROPERTY_X86_ISA_1_V3); + bool has_isa_v4 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) + == GNU_PROPERTY_X86_ISA_1_V4); + + if (!has_isa_baseline) + { + do_test_1 ("tst-isa-level-mod-1-baseline.so", true); + return EXIT_SUCCESS; + } + + do_test_1 ("tst-isa-level-mod-1-baseline.so", false); + + /* Skip on x86-64-v4 platforms since dlopen v4 module always works. */ + if (has_isa_v4) + return EXIT_SUCCESS; + + do_test_1 ("tst-isa-level-mod-1-v4.so", true); + + /* Skip on x86-64-v3 platforms since dlopen v3 module always works. */ + if (has_isa_v3) + return EXIT_SUCCESS; + + do_test_1 ("tst-isa-level-mod-1-v3.so", true); + + /* Skip on x86-64-v2 platforms since dlopen v2 module always works. */ + if (has_isa_v2) + return EXIT_SUCCESS; + + do_test_1 ("tst-isa-level-mod-1-v2.so", true); + + return EXIT_SUCCESS; +} + +#include <support/test-driver.c> diff --git a/sysdeps/x86/tst-isa-level-mod-1-baseline.c b/sysdeps/x86/tst-isa-level-mod-1-baseline.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1-baseline.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1-v2.c b/sysdeps/x86/tst-isa-level-mod-1-v2.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1-v2.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1-v3.c b/sysdeps/x86/tst-isa-level-mod-1-v3.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1-v3.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1-v4.c b/sysdeps/x86/tst-isa-level-mod-1-v4.c new file mode 100644 index 0000000000..d6fe0685a4 --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1-v4.c @@ -0,0 +1 @@ +#include "tst-isa-level-mod-1.c" diff --git a/sysdeps/x86/tst-isa-level-mod-1.c b/sysdeps/x86/tst-isa-level-mod-1.c new file mode 100644 index 0000000000..40a2e1d99f --- /dev/null +++ b/sysdeps/x86/tst-isa-level-mod-1.c @@ -0,0 +1,25 @@ +/* Check ISA level on dlopened shared object. + Copyright (C) 2019-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <isa-level.c> + +int +test (void) +{ + return 0; +} diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c index 0aaa635a00..e030534f1b 100644 --- a/sysdeps/x86_64/dl-hwcaps-subdirs.c +++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c @@ -18,6 +18,7 @@ #include <dl-hwcaps.h> #include <cpu-features.h> +#include <get-isa-level.h> const char _dl_hwcaps_subdirs[] = "x86-64-v4:x86-64-v3:x86-64-v2"; enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ @@ -25,40 +26,25 @@ enum { subdirs_count = 3 }; /* Number of components in _dl_hwcaps_subdirs. */ uint32_t _dl_hwcaps_subdirs_active (void) { + const struct cpu_features *cpu_features + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); + unsigned int isa_level = get_isa_level (cpu_features); int active = 0; /* Test in reverse preference order. */ /* x86-64-v2. */ - if (!(CPU_FEATURE_USABLE (CMPXCHG16B) - && CPU_FEATURE_USABLE (LAHF64_SAHF64) - && CPU_FEATURE_USABLE (POPCNT) - && CPU_FEATURE_USABLE (SSE3) - && CPU_FEATURE_USABLE (SSE4_1) - && CPU_FEATURE_USABLE (SSE4_2) - && CPU_FEATURE_USABLE (SSSE3))) + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; /* x86-64-v3. */ - if (!(CPU_FEATURE_USABLE (AVX) - && CPU_FEATURE_USABLE (AVX2) - && CPU_FEATURE_USABLE (BMI1) - && CPU_FEATURE_USABLE (BMI2) - && CPU_FEATURE_USABLE (F16C) - && CPU_FEATURE_USABLE (FMA) - && CPU_FEATURE_USABLE (LZCNT) - && CPU_FEATURE_USABLE (MOVBE) - && CPU_FEATURE_USABLE (OSXSAVE))) + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; - /* x86-64-v4. */ - if (!(CPU_FEATURE_USABLE (AVX512F) - && CPU_FEATURE_USABLE (AVX512BW) - && CPU_FEATURE_USABLE (AVX512CD) - && CPU_FEATURE_USABLE (AVX512DQ) - && CPU_FEATURE_USABLE (AVX512VL))) + /* x86-64-v4. */ + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); ++active; diff --git a/sysdeps/x86_64/tst-glibc-hwcaps.c b/sysdeps/x86_64/tst-glibc-hwcaps.c index 8bdcb91db0..37b61d3c84 100644 --- a/sysdeps/x86_64/tst-glibc-hwcaps.c +++ b/sysdeps/x86_64/tst-glibc-hwcaps.c @@ -19,7 +19,8 @@ #include <stdio.h> #include <support/check.h> #include <sys/param.h> -#include <sys/platform/x86.h> +#include <elf.h> +#include <get-isa-level.h> extern int marker2 (void); extern int marker3 (void); @@ -31,35 +32,15 @@ compute_level (void) { const struct cpu_features *cpu_features = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); - - if (!(CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B) - && CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64) - && CPU_FEATURE_USABLE_P (cpu_features, POPCNT) - && CPU_FEATURE_USABLE_P (cpu_features, MMX) - && CPU_FEATURE_USABLE_P (cpu_features, SSE) - && CPU_FEATURE_USABLE_P (cpu_features, SSE2) - && CPU_FEATURE_USABLE_P (cpu_features, SSE3) - && CPU_FEATURE_USABLE_P (cpu_features, SSSE3) - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_1) - && CPU_FEATURE_USABLE_P (cpu_features, SSE4_2))) - return 1; - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX) - && CPU_FEATURE_USABLE_P (cpu_features, AVX2) - && CPU_FEATURE_USABLE_P (cpu_features, BMI1) - && CPU_FEATURE_USABLE_P (cpu_features, BMI2) - && CPU_FEATURE_USABLE_P (cpu_features, F16C) - && CPU_FEATURE_USABLE_P (cpu_features, FMA) - && CPU_FEATURE_USABLE_P (cpu_features, LZCNT) - && CPU_FEATURE_USABLE_P (cpu_features, MOVBE) - && CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE))) - return 2; - if (!(CPU_FEATURE_USABLE_P (cpu_features, AVX512F) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512CD) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512DQ) - && CPU_FEATURE_USABLE_P (cpu_features, AVX512VL))) - return 3; - return 4; + unsigned int isa_level = get_isa_level (cpu_features); + + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V2)) + return 1; + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V3)) + return 2; + if (!(isa_level & GNU_PROPERTY_X86_ISA_1_V4)) + return 3; + return 4; } static int -- 2.29.2 ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V6 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] 2021-01-07 20:58 ` H.J. Lu @ 2021-01-18 11:17 ` Florian Weimer 2021-01-18 13:49 ` H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Florian Weimer @ 2021-01-18 11:17 UTC (permalink / raw) To: H.J. Lu via Libc-alpha * H. J. Lu via Libc-alpha: > +static void > +dl_isa_level_check (struct link_map *m, const char *program) > +{ > + const struct cpu_features *cpu_features = __get_cpu_features (); > + unsigned int i; > + struct link_map *l; > + > + i = m->l_searchlist.r_nlist; > + while (i-- > 0) > + { > + /* Check each shared object to see if ISA level is compatible. */ > + l = m->l_initfini[i]; > + > + /* Skip ISA level check if functions have been executed. */ > + if (l->l_init_called) > + continue; > + > +#ifdef SHARED > + /* Skip ISA level check for ld.so since ld.so won't run if its ISA > + level is higher than CPU. */ > + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) > + continue; > +#endif > + > + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) > + != l->l_x86_isa_1_needed) > + { > + if (program) > + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", > + *l->l_name != '\0' ? l->l_name : program); > + else > + _dl_signal_error (0, l->l_name, "dlopen", > + N_("CPU ISA level is lower than required")); > + } > + } > +} We may have hit an integration issue with this check. It turns out that libvirt defaults to the qemu64 model. It masks most of the feature bits required by x86-64-v2. Libvirt developers have previously told me that nothing should be using the qemu64 model, but it's still the default for API stability reasons (in the same way SSL 3 and DES used to be the default in crypto libraries, under the assumption that if you negotiatied a particular protocol and set of algorithms in 1998, you'd still want the exact same choice fifteen years later). But of course, despite no one should be using the qemu64 model, they still do. Here's an example: Guest: fix to always use host-passthrough CPU for all arches <https://github.com/clalancette/oz/pull/283> One pecularity about x86-64-v2 is that under KVM virtualization, the new structions added over the baseline do not actually trap (assuming the host supports them). So we could paper over this integration issue by doing the check for x86-64-v3 and later only. (Although it might not work there—I looked at printing a nice error message for the same situation when we transitioned to z13, but it it turned to be quite difficult.) Downstream, this would likely buy us about three years, in which we could fix qemu64-type problems. But maybe we should tackle this now. I also hope to have a conversation about the libvirt developers regarding defaults, but I'm still trying to figure out the appropriate forum. Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V6 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] 2021-01-18 11:17 ` Florian Weimer @ 2021-01-18 13:49 ` H.J. Lu 2021-01-18 15:15 ` Florian Weimer 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2021-01-18 13:49 UTC (permalink / raw) To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha, Adhemerval Zanella On Mon, Jan 18, 2021 at 3:17 AM Florian Weimer <fweimer@redhat.com> wrote: > > * H. J. Lu via Libc-alpha: > > > +static void > > +dl_isa_level_check (struct link_map *m, const char *program) > > +{ > > + const struct cpu_features *cpu_features = __get_cpu_features (); > > + unsigned int i; > > + struct link_map *l; > > + > > + i = m->l_searchlist.r_nlist; > > + while (i-- > 0) > > + { > > + /* Check each shared object to see if ISA level is compatible. */ > > + l = m->l_initfini[i]; > > + > > + /* Skip ISA level check if functions have been executed. */ > > + if (l->l_init_called) > > + continue; > > + > > +#ifdef SHARED > > + /* Skip ISA level check for ld.so since ld.so won't run if its ISA > > + level is higher than CPU. */ > > + if (l == &GL(dl_rtld_map) || l->l_real == &GL(dl_rtld_map)) > > + continue; > > +#endif > > + > > + if ((l->l_x86_isa_1_needed & cpu_features->isa_1) > > + != l->l_x86_isa_1_needed) > > + { > > + if (program) > > + _dl_fatal_printf ("%s: CPU ISA level is lower than required\n", > > + *l->l_name != '\0' ? l->l_name : program); > > + else > > + _dl_signal_error (0, l->l_name, "dlopen", > > + N_("CPU ISA level is lower than required")); > > + } > > + } > > +} > > We may have hit an integration issue with this check. > > It turns out that libvirt defaults to the qemu64 model. It masks most > of the feature bits required by x86-64-v2. Libvirt developers have > previously told me that nothing should be using the qemu64 model, but > it's still the default for API stability reasons (in the same way SSL 3 > and DES used to be the default in crypto libraries, under the assumption > that if you negotiatied a particular protocol and set of algorithms in > 1998, you'd still want the exact same choice fifteen years later). > > But of course, despite no one should be using the qemu64 model, they > still do. Here's an example: > > Guest: fix to always use host-passthrough CPU for all arches > <https://github.com/clalancette/oz/pull/283> > > One pecularity about x86-64-v2 is that under KVM virtualization, the new > structions added over the baseline do not actually trap (assuming the > host supports them). So we could paper over this integration issue by > doing the check for x86-64-v3 and later only. (Although it might not > work there—I looked at printing a nice error message for the same > situation when we transitioned to z13, but it it turned to be quite > difficult.) > > Downstream, this would likely buy us about three years, in which we > could fix qemu64-type problems. But maybe we should tackle this now. > > I also hope to have a conversation about the libvirt developers > regarding defaults, but I'm still trying to figure out the appropriate > forum. > If we are considering a possible glibc solution, can you open a glibc bug to track it? Thanks. -- H.J. ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V6 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] marker [BZ #26717] 2021-01-18 13:49 ` H.J. Lu @ 2021-01-18 15:15 ` Florian Weimer 0 siblings, 0 replies; 27+ messages in thread From: Florian Weimer @ 2021-01-18 15:15 UTC (permalink / raw) To: H.J. Lu; +Cc: H.J. Lu via Libc-alpha, Adhemerval Zanella * H. J. Lu: > If we are considering a possible glibc solution, can you open a glibc bug > to track it? Done: <https://sourceware.org/bugzilla/show_bug.cgi?id=27201> Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill ^ permalink raw reply [flat|nested] 27+ messages in thread
* V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-06 14:49 V5 [PATCH 0/2] x86: Support GNU_PROPERTY_X86_ISA_1_NEEDED marker [BZ #26717] H.J. Lu 2020-12-06 14:49 ` V5 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] " H.J. Lu @ 2020-12-06 14:49 ` H.J. Lu 2020-12-07 10:00 ` Florian Weimer 1 sibling, 1 reply; 27+ messages in thread From: H.J. Lu @ 2020-12-06 14:49 UTC (permalink / raw) To: libc-alpha; +Cc: Florian Weimer Add ISA level check to detect misplaced shared objects with incompatible ISA level requirement in glibc-hwcaps subdirectories: /sbin/ldconfig: /usr/lib64/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: skipped, ISA level mismatch (x86-64-v4 > x86-64-v2) --- elf/ldconfig.c | 9 +- elf/readelflib.c | 83 ++++++++++++++++++- elf/readlib.c | 7 +- elf/tst-glibc-hwcaps-2-cache.c | 45 ++++++++++ .../etc/ld.so.conf | 2 + .../postclean.req | 0 elf/tst-glibc-hwcaps-2-cache.script | 6 ++ sysdeps/generic/ldconfig.h | 9 +- sysdeps/generic/read-prop.h | 32 +++++++ sysdeps/unix/sysv/linux/x86/read-prop.h | 57 +++++++++++++ sysdeps/unix/sysv/linux/x86/readelflib.c | 67 +++++++++++++-- sysdeps/unix/sysv/linux/x86_64/Makefile | 26 ++++++ .../sysv/linux/x86_64/tst-glibc-hwcaps-2.c | 50 +++++++++++ .../linux/x86_64/x86-64-isa-level-VALUE.c | 4 + 14 files changed, 379 insertions(+), 18 deletions(-) create mode 100644 elf/tst-glibc-hwcaps-2-cache.c create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/postclean.req create mode 100644 elf/tst-glibc-hwcaps-2-cache.script create mode 100644 sysdeps/generic/read-prop.h create mode 100644 sysdeps/unix/sysv/linux/x86/read-prop.h create mode 100644 sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c create mode 100644 sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c diff --git a/elf/ldconfig.c b/elf/ldconfig.c index 10927a8c7f..f306d3df26 100644 --- a/elf/ldconfig.c +++ b/elf/ldconfig.c @@ -720,8 +720,8 @@ manual_link (char *library) return; } - if (process_file (real_library, library, libname, &flag, &osversion, - &soname, 0, &stat_buf)) + if (process_file (real_library, library, libname, NULL, &flag, + &osversion, &soname, 0, &stat_buf)) { error (0, 0, _("No link created since soname could not be found for %s"), library); @@ -982,8 +982,9 @@ search_dir (const struct dir_entry *entry) unsigned int osversion; if (!search_aux_cache (&lstat_buf, &flag, &osversion, &soname)) { - if (process_file (real_name, file_name, direntry->d_name, &flag, - &osversion, &soname, is_link, &lstat_buf)) + if (process_file (real_name, file_name, direntry->d_name, + entry->hwcaps, &flag, &osversion, + &soname, is_link, &lstat_buf)) { if (real_name != real_file_name) free (real_name); diff --git a/elf/readelflib.c b/elf/readelflib.c index 5905f6d344..0784bbcc1f 100644 --- a/elf/readelflib.c +++ b/elf/readelflib.c @@ -17,6 +17,8 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ +#include <read-prop.h> + /* This code is a heavily simplified version of the readelf program that's part of the current binutils development version. For architectures which need to handle both 32bit and 64bit ELF libraries, this file is @@ -39,7 +41,8 @@ do \ /* Returns 0 if everything is ok, != 0 in case of error. */ int -process_elf_file (const char *file_name, const char *lib, int *flag, +process_elf_file (const char *file_name, const char *lib, + struct glibc_hwcaps_subdirectory *hwcaps, int *flag, unsigned int *osversion, char **soname, void *file_contents, size_t file_length) { @@ -164,6 +167,84 @@ process_elf_file (const char *file_name, const char *lib, int *flag, } break; + case PT_GNU_PROPERTY: + /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes + in 32-bit objects and to 8 bytes in 64-bit objects. Skip + notes with incorrect alignment. */ + if (segment->p_align == (__ELF_NATIVE_CLASS / 8)) + { + const ElfW(Nhdr) *note = (const void *) (file_contents + + segment->p_offset); + const ElfW(Addr) size = segment->p_filesz; + const ElfW(Addr) align = segment->p_align; + + const ElfW(Addr) start = (ElfW(Addr)) (uintptr_t) note; + unsigned int last_type = 0; + + while ((ElfW(Addr)) (uintptr_t) (note + 1) - start < size) + { + /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ + if (note->n_namesz == 4 + && note->n_type == NT_GNU_PROPERTY_TYPE_0 + && memcmp (note + 1, "GNU", 4) == 0) + { + /* Check for invalid property. */ + if (note->n_descsz < 8 + || (note->n_descsz % sizeof (ElfW(Addr))) != 0) + goto done; + + /* Start and end of property array. */ + unsigned char *ptr = (unsigned char *) (note + 1) + 4; + unsigned char *ptr_end = ptr + note->n_descsz; + + do + { + unsigned int type = *(unsigned int *) ptr; + unsigned int datasz = *(unsigned int *) (ptr + 4); + + /* Property type must be in ascending order. */ + if (type < last_type) + goto done; + + ptr += 8; + if ((ptr + datasz) > ptr_end) + goto done; + + last_type = type; + + /* Target specific property processing. + Return value: + 0: Stop processing the properties. + > 0: Continue processing the properties. + < 0: Stop and skip the file. + */ + int read_prop + = ElfW(read_gnu_property) (hwcaps, file_name, + segment, type, + datasz, ptr); + if (read_prop == 0) + goto done; + else if (read_prop < 0) + return 1; + + /* Check the next property item. */ + ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); + } + while ((ptr_end - ptr) >= 8); + + /* Only handle one NT_GNU_PROPERTY_TYPE_0. */ + goto done; + } + + note = ((const void *) note + + ELF_NOTE_NEXT_OFFSET (note->n_namesz, + note->n_descsz, + align)); + } + } +done: + break; + default: break; } diff --git a/elf/readlib.c b/elf/readlib.c index 994a4426a1..400f64bafa 100644 --- a/elf/readlib.c +++ b/elf/readlib.c @@ -74,7 +74,8 @@ is_gdb_python_file (const char *name) /* Returns 0 if everything is ok, != 0 in case of error. */ int process_file (const char *real_file_name, const char *file_name, - const char *lib, int *flag, unsigned int *osversion, + const char *lib, struct glibc_hwcaps_subdirectory *hwcaps, + int *flag, unsigned int *osversion, char **soname, int is_link, struct stat64 *stat_buf) { FILE *file; @@ -173,8 +174,8 @@ process_file (const char *real_file_name, const char *file_name, /* Libraries have to be shared object files. */ else if (elf_header->e_type != ET_DYN) ret = 1; - else if (process_elf_file (file_name, lib, flag, osversion, soname, - file_contents, statbuf.st_size)) + else if (process_elf_file (file_name, lib, hwcaps, flag, osversion, + soname, file_contents, statbuf.st_size)) ret = 1; done: diff --git a/elf/tst-glibc-hwcaps-2-cache.c b/elf/tst-glibc-hwcaps-2-cache.c new file mode 100644 index 0000000000..2c30a2c911 --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.c @@ -0,0 +1,45 @@ +/* Wrapper to invoke tst-glibc-hwcaps-2 in a container to test ldconfig. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +/* This program is just a wrapper that runs ldconfig followed by + tst-glibc-hwcaps-2. The actual test is provided via an + implementation in a sysdeps subdirectory. */ + +#include <stdio.h> +#include <stdlib.h> +#include <support/support.h> +#include <unistd.h> + +int +main (int argc, char **argv) +{ + /* Run ldconfig to populate the cache. */ + { + char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir); + if (system (command) != 0) + return 1; + free (command); + } + + /* Reuse tst-glibc-hwcaps. Since this code is running in a + container, we can launch it directly. */ + char *path = xasprintf ("%s/elf/tst-glibc-hwcaps-2", support_objdir_root); + execv (path, argv); + printf ("error: execv of %s failed: %m\n", path); + return 1; +} diff --git a/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf new file mode 100644 index 0000000000..e1e74dbda2 --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf @@ -0,0 +1,2 @@ +# This file was created to suppress a warning from ldconfig: +# /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory diff --git a/elf/tst-glibc-hwcaps-2-cache.root/postclean.req b/elf/tst-glibc-hwcaps-2-cache.root/postclean.req new file mode 100644 index 0000000000..e69de29bb2 diff --git a/elf/tst-glibc-hwcaps-2-cache.script b/elf/tst-glibc-hwcaps-2-cache.script new file mode 100644 index 0000000000..29ccfc3b49 --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.script @@ -0,0 +1,6 @@ +# test-container does not support scripts in sysdeps directories, so +# collect everything in one file. + +mkdirp 0770 $L/glibc-hwcaps/x86-64-v2 +cp $B/elf/libx86-64-isa-level-1.so $L/libx86-64-isa-level.so +cp $B/elf/libx86-64-isa-level-4.so $L/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h index 1ad1528890..f85eaa48cb 100644 --- a/sysdeps/generic/ldconfig.h +++ b/sysdeps/generic/ldconfig.h @@ -88,13 +88,16 @@ extern void save_aux_cache (const char *aux_cache_name); /* Declared in readlib.c. */ extern int process_file (const char *real_file_name, const char *file_name, - const char *lib, int *flag, unsigned int *osversion, - char **soname, int is_link, struct stat64 *stat_buf); + const char *lib, struct glibc_hwcaps_subdirectory *, + int *flag, unsigned int *osversion, char **soname, + int is_link, struct stat64 *stat_buf); extern char *implicit_soname (const char *lib, int flag); /* Declared in readelflib.c. */ -extern int process_elf_file (const char *file_name, const char *lib, int *flag, +extern int process_elf_file (const char *file_name, const char *lib, + struct glibc_hwcaps_subdirectory *hwcaps, + int *flag, unsigned int *osversion, char **soname, void *file_contents, size_t file_length); diff --git a/sysdeps/generic/read-prop.h b/sysdeps/generic/read-prop.h new file mode 100644 index 0000000000..49092f83f1 --- /dev/null +++ b/sysdeps/generic/read-prop.h @@ -0,0 +1,32 @@ +/* Support for GNU properties in ldconfig. Generic version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. + Return value: + 0: Stop processing the properties. + > 0: Continue processing the properties. + < 0: Stop and skip the file. + */ + +static inline int __attribute__ ((always_inline)) +ElfW(read_gnu_property) (struct glibc_hwcaps_subdirectory *hwcaps, + const char *file_name, ElfW(Phdr) *segment, + uint32_t type, uint32_t datasz, void *data) +{ + return 0; +} diff --git a/sysdeps/unix/sysv/linux/x86/read-prop.h b/sysdeps/unix/sysv/linux/x86/read-prop.h new file mode 100644 index 0000000000..c22395f851 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/read-prop.h @@ -0,0 +1,57 @@ +/* Support for GNU properties in ldconfig. x86 version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. + Return value: + 0: Stop processing the properties. + > 0: Continue processing the properties. + < 0: Stop and skip the file for ISA level mismatch. + */ + +static inline int __attribute__ ((always_inline)) +ElfW(read_gnu_property) (struct glibc_hwcaps_subdirectory *hwcaps, + const char *file_name, ElfW(Phdr) *segment, + uint32_t type, uint32_t datasz, void *data) +{ + if (type == GNU_PROPERTY_X86_ISA_1_NEEDED) + { + /* The size of GNU_PROPERTY_X86_ISA_1_NEEDED must be 4 bytes. There + is no point to continue if this type is ill-formed. */ + if (hwcaps != NULL && datasz == 4) + { + const char *isa_string = glibc_hwcaps_subdirectory_name (hwcaps); + if (isa_string != NULL) + { + unsigned int isa_1_needed = *(unsigned int *) data; + unsigned int isa_level; + if (get_isa_level_from_string (isa_string, &isa_level) == 0 + && isa_1_needed > isa_level) + { + error (0, 0, + _("%s: skipped, ISA level mismatch (%s > %s)"), + file_name, + get_isa_string_from_level (isa_1_needed), + isa_string); + return -1; + } + } + } + return 0; + } + return 1; +} diff --git a/sysdeps/unix/sysv/linux/x86/readelflib.c b/sysdeps/unix/sysv/linux/x86/readelflib.c index 3e83419f5b..336ba57ae7 100644 --- a/sysdeps/unix/sysv/linux/x86/readelflib.c +++ b/sysdeps/unix/sysv/linux/x86/readelflib.c @@ -17,17 +17,70 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ +/* Get ISA level string from GNU_PROPERTY_X86_ISA_1_V[234] ISA level. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, +static const char * +get_isa_string_from_level (unsigned int isa_level) +{ + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4)) + return "x86-64-v4"; + else if ((isa_level & GNU_PROPERTY_X86_ISA_1_V3)) + return "x86-64-v3"; + else if ((isa_level & GNU_PROPERTY_X86_ISA_1_V2)) + return "x86-64-v2"; + abort (); + return NULL; +} + +/* Get GNU_PROPERTY_X86_ISA_1_V[234] ISA level from ISA level string. */ + +static int +get_isa_level_from_string (const char *level, unsigned int *isa_level_p) +{ + if (memcmp (level, "x86-64-v", 8) == 0) + { + unsigned int isa_level = 0; + if (level[9] == '\0') + switch (level[8]) + { + case '2': + isa_level = (GNU_PROPERTY_X86_ISA_1_BASELINE + | GNU_PROPERTY_X86_ISA_1_V2); + break; + case '3': + isa_level = (GNU_PROPERTY_X86_ISA_1_BASELINE + | GNU_PROPERTY_X86_ISA_1_V2 + | GNU_PROPERTY_X86_ISA_1_V3); + break; + case '4': + isa_level = (GNU_PROPERTY_X86_ISA_1_BASELINE + | GNU_PROPERTY_X86_ISA_1_V2 + | GNU_PROPERTY_X86_ISA_1_V3 + | GNU_PROPERTY_X86_ISA_1_V4); + break; + } + if (isa_level) + { + *isa_level_p = isa_level; + return 0; + } + } + return -1; +} + +int process_elf32_file (const char *file_name, const char *lib, + struct glibc_hwcaps_subdirectory *hwcaps, int *flag, unsigned int *osversion, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, +int process_elf64_file (const char *file_name, const char *lib, + struct glibc_hwcaps_subdirectory *hwcaps, int *flag, unsigned int *osversion, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int -process_elf_file (const char *file_name, const char *lib, int *flag, +process_elf_file (const char *file_name, const char *lib, + struct glibc_hwcaps_subdirectory *hwcaps, int *flag, unsigned int *osversion, char **soname, void *file_contents, size_t file_length) { @@ -68,11 +121,11 @@ failed: } if (elf_header->e_ident[EI_CLASS] == ELFCLASS32) - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, hwcaps, flag, osversion, + soname, file_contents, file_length); else - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, hwcaps, flag, osversion, + soname, file_contents, file_length); if (!ret && file_flag) *flag = file_flag; diff --git a/sysdeps/unix/sysv/linux/x86_64/Makefile b/sysdeps/unix/sysv/linux/x86_64/Makefile index 9b82155393..a22bb6321f 100644 --- a/sysdeps/unix/sysv/linux/x86_64/Makefile +++ b/sysdeps/unix/sysv/linux/x86_64/Makefile @@ -13,3 +13,29 @@ endif ifeq ($(subdir),misc) gen-as-const-headers += sigaltstack-offsets.sym endif + +ifeq ($(subdir),elf) +ifeq (yesyes,$(enable-x86-isa-level)$(config-cflags-skylake-avx512)) +tests += tst-glibc-hwcaps-2 +modules-names += libx86-64-isa-level-1 libx86-64-isa-level-4 + +ifeq (no,$(build-hardcoded-path-in-tests)) +# This is an ld.so.cache test, and RPATH/RUNPATH in the executable +# interferes with its test objectives. +tests-container += tst-glibc-hwcaps-2-cache +endif + +$(objpfx)tst-glibc-hwcaps-2: $(objpfx)libx86-64-isa-level.so + +CFLAGS-libx86-64-isa-level-1.os += -march=x86-64 +CFLAGS-libx86-64-isa-level-4.os += -march=skylake-avx512 + +# The test modules are parameterized by preprocessor macros. +LDFLAGS-libx86-64-isa-level-1.so += -Wl,-soname,libx86-64-isa-level.so +LDFLAGS-libx86-64-isa-level-4.so += -Wl,-soname,libx86-64-isa-level.so +$(objpfx)libx86-64-isa-level%.os: $(..)/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c + $(compile-command.c) -DVALUE=$(lastword $(subst -, ,$*)) +$(objpfx)libx86-64-isa-level.so: $(objpfx)libx86-64-isa-level-1.so + cp $< $@ +endif +endif # $(subdir) == elf diff --git a/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c new file mode 100644 index 0000000000..ce6b189415 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c @@ -0,0 +1,50 @@ +/* Check ISA level on shared object in glibc-hwcaps subdirectories. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <stdlib.h> +#include <stdio.h> +#include <support/check.h> +#include <support/test-driver.h> + +extern int isa_level (void); + +static int +do_test (void) +{ + int level = isa_level (); + int ret; + switch (level) + { + case 1: + /* The default libx86-64-isa-level.so should be used. */ + printf ("The default shared library is used.\n"); + ret = EXIT_SUCCESS; + break; + case 4: + /* libx86-64-isa-level.so marked as x86-64 ISA level 4 needed in + x86-64-v2 should be ignored. */ + printf ("x86-64 ISA level 4 shared library in x86-64-v2 is used.\n"); + ret = EXIT_FAILURE; + break; + default: + abort (); + } + return ret; +} + +#include <support/test-driver.c> diff --git a/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c new file mode 100644 index 0000000000..892e4c464b --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c @@ -0,0 +1,4 @@ +#define INCLUDE_X86_ISA_LEVEL +#define MARKER isa_level +#include <isa-level.c> +#include <markermodMARKER-VALUE.c> -- 2.28.0 ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-06 14:49 ` V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps H.J. Lu @ 2020-12-07 10:00 ` Florian Weimer 2020-12-08 13:25 ` H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Florian Weimer @ 2020-12-07 10:00 UTC (permalink / raw) To: H.J. Lu via Libc-alpha * H. J. Lu via Libc-alpha: > Add ISA level check to detect misplaced shared objects with incompatible > ISA level requirement in glibc-hwcaps subdirectories: > > /sbin/ldconfig: /usr/lib64/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: skipped, ISA level mismatch (x86-64-v4 > x86-64-v2) I think this is conceptually the wrong approach. ldconfig should copy the notes into the cache (and aux cache), and ld.so should skip cache contents that doesn't match the run-time requirements during load. Then directory place does not matter that much anymore. Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-07 10:00 ` Florian Weimer @ 2020-12-08 13:25 ` H.J. Lu 2020-12-08 13:31 ` Florian Weimer 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2020-12-08 13:25 UTC (permalink / raw) To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha On Mon, Dec 7, 2020 at 2:00 AM Florian Weimer <fweimer@redhat.com> wrote: > > * H. J. Lu via Libc-alpha: > > > Add ISA level check to detect misplaced shared objects with incompatible > > ISA level requirement in glibc-hwcaps subdirectories: > > > > /sbin/ldconfig: /usr/lib64/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: skipped, ISA level mismatch (x86-64-v4 > x86-64-v2) > > I think this is conceptually the wrong approach. ldconfig should copy > the notes into the cache (and aux cache), and ld.so should skip cache > contents that doesn't match the run-time requirements during load. Then > directory place does not matter that much anymore. > Do we have space in cache for it? -- H.J. ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-08 13:25 ` H.J. Lu @ 2020-12-08 13:31 ` Florian Weimer 2020-12-08 14:10 ` H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Florian Weimer @ 2020-12-08 13:31 UTC (permalink / raw) To: H.J. Lu; +Cc: H.J. Lu via Libc-alpha * H. J. Lu: > On Mon, Dec 7, 2020 at 2:00 AM Florian Weimer <fweimer@redhat.com> wrote: >> >> * H. J. Lu via Libc-alpha: >> >> > Add ISA level check to detect misplaced shared objects with incompatible >> > ISA level requirement in glibc-hwcaps subdirectories: >> > >> > /sbin/ldconfig: /usr/lib64/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: skipped, ISA level mismatch (x86-64-v4 > x86-64-v2) >> >> I think this is conceptually the wrong approach. ldconfig should copy >> the notes into the cache (and aux cache), and ld.so should skip cache >> contents that doesn't match the run-time requirements during load. Then >> directory place does not matter that much anymore. > > Do we have space in cache for it? We have an extension mechanism. The relevant struct looks like this: struct file_entry_new { int32_t flags; /* This is 1 for an ELF library. */ uint32_t key, value; /* String table indices. */ uint32_t osversion; /* Required OS version. */ uint64_t hwcap; /* Hwcap entry. */ }; We can set a previously-unused bit hwcap to hide entries from current ld.so. This way, we can repurpose other hwcap bits, osversion, even flags. We could put an index there into a array of (property) notes, and use deduplication to share identical notes from different shared objects. In essence, this is how the glibc-hwcaps subdirectory mechanism achieves backwards-compatibility. Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-08 13:31 ` Florian Weimer @ 2020-12-08 14:10 ` H.J. Lu 2020-12-08 14:18 ` Florian Weimer 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2020-12-08 14:10 UTC (permalink / raw) To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha On Tue, Dec 8, 2020 at 5:32 AM Florian Weimer <fweimer@redhat.com> wrote: > > * H. J. Lu: > > > On Mon, Dec 7, 2020 at 2:00 AM Florian Weimer <fweimer@redhat.com> wrote: > >> > >> * H. J. Lu via Libc-alpha: > >> > >> > Add ISA level check to detect misplaced shared objects with incompatible > >> > ISA level requirement in glibc-hwcaps subdirectories: > >> > > >> > /sbin/ldconfig: /usr/lib64/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: skipped, ISA level mismatch (x86-64-v4 > x86-64-v2) > >> > >> I think this is conceptually the wrong approach. ldconfig should copy > >> the notes into the cache (and aux cache), and ld.so should skip cache > >> contents that doesn't match the run-time requirements during load. Then > >> directory place does not matter that much anymore. > > > > Do we have space in cache for it? > > We have an extension mechanism. The relevant struct looks like this: > > struct file_entry_new > { > int32_t flags; /* This is 1 for an ELF library. */ > uint32_t key, value; /* String table indices. */ > uint32_t osversion; /* Required OS version. */ > uint64_t hwcap; /* Hwcap entry. */ > }; > > We can set a previously-unused bit hwcap to hide entries from current > ld.so. This way, we can repurpose other hwcap bits, osversion, even > flags. We could put an index there into a array of (property) notes, > and use deduplication to share identical notes from different shared > objects. > > In essence, this is how the glibc-hwcaps subdirectory mechanism achieves > backwards-compatibility. > For each file entry for a shared object, the hwcap field has been used by DL_CACHE_HWCAP_EXTENSION for glibc-hwcaps. Are you suggesting to add another file entry for the same shared object to store ISA level requirement? -- H.J. ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-08 14:10 ` H.J. Lu @ 2020-12-08 14:18 ` Florian Weimer 2020-12-08 14:23 ` H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Florian Weimer @ 2020-12-08 14:18 UTC (permalink / raw) To: H.J. Lu; +Cc: H.J. Lu via Libc-alpha * H. J. Lu: > On Tue, Dec 8, 2020 at 5:32 AM Florian Weimer <fweimer@redhat.com> wrote: >> >> * H. J. Lu: >> >> > On Mon, Dec 7, 2020 at 2:00 AM Florian Weimer <fweimer@redhat.com> wrote: >> >> >> >> * H. J. Lu via Libc-alpha: >> >> >> >> > Add ISA level check to detect misplaced shared objects with incompatible >> >> > ISA level requirement in glibc-hwcaps subdirectories: >> >> > >> >> > /sbin/ldconfig: /usr/lib64/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: skipped, ISA level mismatch (x86-64-v4 > x86-64-v2) >> >> >> >> I think this is conceptually the wrong approach. ldconfig should copy >> >> the notes into the cache (and aux cache), and ld.so should skip cache >> >> contents that doesn't match the run-time requirements during load. Then >> >> directory place does not matter that much anymore. >> > >> > Do we have space in cache for it? >> >> We have an extension mechanism. The relevant struct looks like this: >> >> struct file_entry_new >> { >> int32_t flags; /* This is 1 for an ELF library. */ >> uint32_t key, value; /* String table indices. */ >> uint32_t osversion; /* Required OS version. */ >> uint64_t hwcap; /* Hwcap entry. */ >> }; >> >> We can set a previously-unused bit hwcap to hide entries from current >> ld.so. This way, we can repurpose other hwcap bits, osversion, even >> flags. We could put an index there into a array of (property) notes, >> and use deduplication to share identical notes from different shared >> objects. >> >> In essence, this is how the glibc-hwcaps subdirectory mechanism achieves >> backwards-compatibility. >> > > For each file entry for a shared object, the hwcap field has been used by > DL_CACHE_HWCAP_EXTENSION for glibc-hwcaps. Are you suggesting > to add another file entry for the same shared object to store ISA level > requirement? It's a 64-bit field. We do this: /* This bit in the hwcap field of struct file_entry_new indicates that the lower 32 bits contain an index into the cache_extension_tag_glibc_hwcaps section. Older glibc versions do not know about this HWCAP bit, so they will ignore these entries. */ #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) /* Return true if the ENTRY->hwcap value indicates that DL_CACHE_HWCAP_EXTENSION is used. */ static inline bool dl_cache_hwcap_extension (struct file_entry_new *entry) { /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this is a different kind of extension. */ return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); } So we can use a different bit pattern involving DL_CACHE_HWCAP_EXTENSION to express something else. In particular, only the lower 32 bits are currently used as an index. Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-08 14:18 ` Florian Weimer @ 2020-12-08 14:23 ` H.J. Lu 2020-12-08 15:15 ` Florian Weimer 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2020-12-08 14:23 UTC (permalink / raw) To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha On Tue, Dec 8, 2020 at 6:18 AM Florian Weimer <fweimer@redhat.com> wrote: > > * H. J. Lu: > > > On Tue, Dec 8, 2020 at 5:32 AM Florian Weimer <fweimer@redhat.com> wrote: > >> > >> * H. J. Lu: > >> > >> > On Mon, Dec 7, 2020 at 2:00 AM Florian Weimer <fweimer@redhat.com> wrote: > >> >> > >> >> * H. J. Lu via Libc-alpha: > >> >> > >> >> > Add ISA level check to detect misplaced shared objects with incompatible > >> >> > ISA level requirement in glibc-hwcaps subdirectories: > >> >> > > >> >> > /sbin/ldconfig: /usr/lib64/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: skipped, ISA level mismatch (x86-64-v4 > x86-64-v2) > >> >> > >> >> I think this is conceptually the wrong approach. ldconfig should copy > >> >> the notes into the cache (and aux cache), and ld.so should skip cache > >> >> contents that doesn't match the run-time requirements during load. Then > >> >> directory place does not matter that much anymore. > >> > > >> > Do we have space in cache for it? > >> > >> We have an extension mechanism. The relevant struct looks like this: > >> > >> struct file_entry_new > >> { > >> int32_t flags; /* This is 1 for an ELF library. */ > >> uint32_t key, value; /* String table indices. */ > >> uint32_t osversion; /* Required OS version. */ > >> uint64_t hwcap; /* Hwcap entry. */ > >> }; > >> > >> We can set a previously-unused bit hwcap to hide entries from current > >> ld.so. This way, we can repurpose other hwcap bits, osversion, even > >> flags. We could put an index there into a array of (property) notes, > >> and use deduplication to share identical notes from different shared > >> objects. > >> > >> In essence, this is how the glibc-hwcaps subdirectory mechanism achieves > >> backwards-compatibility. > >> > > > > For each file entry for a shared object, the hwcap field has been used by > > DL_CACHE_HWCAP_EXTENSION for glibc-hwcaps. Are you suggesting > > to add another file entry for the same shared object to store ISA level > > requirement? > > It's a 64-bit field. We do this: > > /* This bit in the hwcap field of struct file_entry_new indicates that > the lower 32 bits contain an index into the > cache_extension_tag_glibc_hwcaps section. Older glibc versions do > not know about this HWCAP bit, so they will ignore these > entries. */ > #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) > > /* Return true if the ENTRY->hwcap value indicates that > DL_CACHE_HWCAP_EXTENSION is used. */ > static inline bool > dl_cache_hwcap_extension (struct file_entry_new *entry) > { > /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this > is a different kind of extension. */ > return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); > } > > So we can use a different bit pattern involving DL_CACHE_HWCAP_EXTENSION > to express something else. In particular, only the lower 32 bits are > currently used as an index. So bits 33-47 can be used for ISA level requirement. This should support 65536 ISA levels. -- H.J. ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-08 14:23 ` H.J. Lu @ 2020-12-08 15:15 ` Florian Weimer 2020-12-08 15:31 ` H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Florian Weimer @ 2020-12-08 15:15 UTC (permalink / raw) To: H.J. Lu; +Cc: H.J. Lu via Libc-alpha * H. J. Lu: >> > For each file entry for a shared object, the hwcap field has been used by >> > DL_CACHE_HWCAP_EXTENSION for glibc-hwcaps. Are you suggesting >> > to add another file entry for the same shared object to store ISA level >> > requirement? >> >> It's a 64-bit field. We do this: >> >> /* This bit in the hwcap field of struct file_entry_new indicates that >> the lower 32 bits contain an index into the >> cache_extension_tag_glibc_hwcaps section. Older glibc versions do >> not know about this HWCAP bit, so they will ignore these >> entries. */ >> #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) >> >> /* Return true if the ENTRY->hwcap value indicates that >> DL_CACHE_HWCAP_EXTENSION is used. */ >> static inline bool >> dl_cache_hwcap_extension (struct file_entry_new *entry) >> { >> /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this >> is a different kind of extension. */ >> return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); >> } >> >> So we can use a different bit pattern involving DL_CACHE_HWCAP_EXTENSION >> to express something else. In particular, only the lower 32 bits are >> currently used as an index. > > So bits 33-47 can be used for ISA level requirement. This should support > 65536 ISA levels. The question is whether the ISA levels and the glibc-hwcaps name are used in parallel. If not, we can use (1ULL << 62) | (1ULL << 32) as the marker bits and a 32-bit index in the lower half. (I do not think this is necessarily relevant for levels, but it could be used for locating objects according to other criteria.) Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps 2020-12-08 15:15 ` Florian Weimer @ 2020-12-08 15:31 ` H.J. Lu 2020-12-09 3:27 ` [PATCH] ldconfig/x86: Store ISA level in cache and aux cache H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2020-12-08 15:31 UTC (permalink / raw) To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha On Tue, Dec 8, 2020 at 7:16 AM Florian Weimer <fweimer@redhat.com> wrote: > > * H. J. Lu: > > >> > For each file entry for a shared object, the hwcap field has been used by > >> > DL_CACHE_HWCAP_EXTENSION for glibc-hwcaps. Are you suggesting > >> > to add another file entry for the same shared object to store ISA level > >> > requirement? > >> > >> It's a 64-bit field. We do this: > >> > >> /* This bit in the hwcap field of struct file_entry_new indicates that > >> the lower 32 bits contain an index into the > >> cache_extension_tag_glibc_hwcaps section. Older glibc versions do > >> not know about this HWCAP bit, so they will ignore these > >> entries. */ > >> #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) > >> > >> /* Return true if the ENTRY->hwcap value indicates that > >> DL_CACHE_HWCAP_EXTENSION is used. */ > >> static inline bool > >> dl_cache_hwcap_extension (struct file_entry_new *entry) > >> { > >> /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this > >> is a different kind of extension. */ > >> return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); > >> } > >> > >> So we can use a different bit pattern involving DL_CACHE_HWCAP_EXTENSION > >> to express something else. In particular, only the lower 32 bits are > >> currently used as an index. > > > > So bits 33-47 can be used for ISA level requirement. This should support > > 65536 ISA levels. > > The question is whether the ISA levels and the glibc-hwcaps name are > used in parallel. If not, we can use (1ULL << 62) | (1ULL << 32) as > the marker bits and a 32-bit index in the lower half. > > (I do not think this is necessarily relevant for levels, but it could be > used for locating objects according to other criteria.) > I think ISA level and glibc-hwcaps should go together. -- H.J. ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2020-12-08 15:31 ` H.J. Lu @ 2020-12-09 3:27 ` H.J. Lu 2021-01-12 16:25 ` Adhemerval Zanella 2021-01-28 20:20 ` Florian Weimer 0 siblings, 2 replies; 27+ messages in thread From: H.J. Lu @ 2020-12-09 3:27 UTC (permalink / raw) To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha [-- Attachment #1: Type: text/plain, Size: 2145 bytes --] On Tue, Dec 8, 2020 at 7:31 AM H.J. Lu <hjl.tools@gmail.com> wrote: > > On Tue, Dec 8, 2020 at 7:16 AM Florian Weimer <fweimer@redhat.com> wrote: > > > > * H. J. Lu: > > > > >> > For each file entry for a shared object, the hwcap field has been used by > > >> > DL_CACHE_HWCAP_EXTENSION for glibc-hwcaps. Are you suggesting > > >> > to add another file entry for the same shared object to store ISA level > > >> > requirement? > > >> > > >> It's a 64-bit field. We do this: > > >> > > >> /* This bit in the hwcap field of struct file_entry_new indicates that > > >> the lower 32 bits contain an index into the > > >> cache_extension_tag_glibc_hwcaps section. Older glibc versions do > > >> not know about this HWCAP bit, so they will ignore these > > >> entries. */ > > >> #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) > > >> > > >> /* Return true if the ENTRY->hwcap value indicates that > > >> DL_CACHE_HWCAP_EXTENSION is used. */ > > >> static inline bool > > >> dl_cache_hwcap_extension (struct file_entry_new *entry) > > >> { > > >> /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this > > >> is a different kind of extension. */ > > >> return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); > > >> } > > >> > > >> So we can use a different bit pattern involving DL_CACHE_HWCAP_EXTENSION > > >> to express something else. In particular, only the lower 32 bits are > > >> currently used as an index. > > > > > > So bits 33-47 can be used for ISA level requirement. This should support > > > 65536 ISA levels. > > > > The question is whether the ISA levels and the glibc-hwcaps name are > > used in parallel. If not, we can use (1ULL << 62) | (1ULL << 32) as > > the marker bits and a 32-bit index in the lower half. > > > > (I do not think this is necessarily relevant for levels, but it could be > > used for locating objects according to other criteria.) > > > > I think ISA level and glibc-hwcaps should go together. > Here is the patch to store ISA level in the portion of the unused upper 32 bits of the hwcaps field in cache and the unused pad field in aux cache. -- H.J. [-- Attachment #2: 0001-ldconfig-x86-Store-ISA-level-in-cache-and-aux-cache.patch --] [-- Type: text/x-patch, Size: 49240 bytes --] From f5053fabd1e8a5567b487ef4054793e043a7b817 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.tools@gmail.com> Date: Sat, 5 Dec 2020 07:01:58 -0800 Subject: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache Store ISA level in the portion of the unused upper 32 bits of the hwcaps field in cache and the unused pad field in aux cache. ISA level is stored and checked only for shared objects in glibc-hwcaps subdirectories. The shared objects in the default directories aren't checked since there are no fallbacks for these shared objects. --- elf/cache.c | 34 +++++--- elf/dl-cache.c | 4 + elf/ldconfig.c | 18 +++-- elf/readelflib.c | 81 ++++++++++++++++++- elf/readlib.c | 7 +- elf/tst-glibc-hwcaps-2-cache.c | 45 +++++++++++ .../etc/ld.so.conf | 2 + .../postclean.req | 0 elf/tst-glibc-hwcaps-2-cache.script | 6 ++ sysdeps/generic/dl-cache.h | 15 +++- sysdeps/generic/dl-isa-level.h | 26 ++++++ sysdeps/generic/ldconfig.h | 22 +++-- sysdeps/generic/read-prop.h | 35 ++++++++ sysdeps/unix/sysv/linux/arm/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/ia64/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/mips/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/powerpc/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/riscv/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/s390/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/sparc/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/x86/read-prop.h | 61 ++++++++++++++ sysdeps/unix/sysv/linux/x86/readelflib.c | 23 +++--- sysdeps/unix/sysv/linux/x86_64/Makefile | 31 +++++++ .../sysv/linux/x86_64/tst-glibc-hwcaps-2.c | 61 ++++++++++++++ .../linux/x86_64/x86-64-isa-level-VALUE.c | 4 + sysdeps/x86/dl-isa-level.h | 32 ++++++++ 26 files changed, 550 insertions(+), 111 deletions(-) create mode 100644 elf/tst-glibc-hwcaps-2-cache.c create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/postclean.req create mode 100644 elf/tst-glibc-hwcaps-2-cache.script create mode 100644 sysdeps/generic/dl-isa-level.h create mode 100644 sysdeps/generic/read-prop.h create mode 100644 sysdeps/unix/sysv/linux/x86/read-prop.h create mode 100644 sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c create mode 100644 sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c create mode 100644 sysdeps/x86/dl-isa-level.h diff --git a/elf/cache.c b/elf/cache.c index b03c5319f8..91a72381ae 100644 --- a/elf/cache.c +++ b/elf/cache.c @@ -145,6 +145,7 @@ struct cache_entry struct stringtable_entry *path; /* Path to find library. */ int flags; /* Flags to indicate kind of library. */ unsigned int osversion; /* Required OS version. */ + unsigned int isa_level; /* Required ISAL level. */ uint64_t hwcap; /* Important hardware capabilities. */ int bits_hwcap; /* Number of bits set in hwcap. */ @@ -660,8 +661,14 @@ save_cache (const char *cache_name) if (entry->hwcaps == NULL) file_entries_new->libs[idx_new].hwcap = entry->hwcap; else - file_entries_new->libs[idx_new].hwcap - = DL_CACHE_HWCAP_EXTENSION | entry->hwcaps->section_index; + { + if (entry->isa_level > (1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT)) + abort (); + file_entries_new->libs[idx_new].hwcap + = (DL_CACHE_HWCAP_EXTENSION + | (((uint64_t) entry->isa_level) << 32) + | entry->hwcaps->section_index); + } file_entries_new->libs[idx_new].key = str_offset + entry->lib->offset; file_entries_new->libs[idx_new].value @@ -776,7 +783,8 @@ save_cache (const char *cache_name) /* Add one library to the cache. */ void add_to_cache (const char *path, const char *filename, const char *soname, - int flags, unsigned int osversion, uint64_t hwcap, + int flags, unsigned int osversion, + unsigned int isa_level, uint64_t hwcap, struct glibc_hwcaps_subdirectory *hwcaps) { struct cache_entry *new_entry = xmalloc (sizeof (*new_entry)); @@ -794,6 +802,7 @@ add_to_cache (const char *path, const char *filename, const char *soname, new_entry->path = path_interned; new_entry->flags = flags; new_entry->osversion = osversion; + new_entry->isa_level = isa_level; new_entry->hwcap = hwcap; new_entry->hwcaps = hwcaps; new_entry->bits_hwcap = 0; @@ -850,6 +859,7 @@ struct aux_cache_entry struct aux_cache_entry_id id; int flags; unsigned int osversion; + unsigned int isa_level; int used; char *soname; struct aux_cache_entry *next; @@ -863,7 +873,7 @@ struct aux_cache_file_entry int32_t flags; /* This is 1 for an ELF library. */ uint32_t soname; /* String table indice. */ uint32_t osversion; /* Required OS version. */ - int32_t pad; + uint32_t isa_level; /* Required ISA level. */ }; /* ldconfig maintains an auxiliary cache file that allows @@ -914,7 +924,8 @@ init_aux_cache (void) int search_aux_cache (struct stat64 *stat_buf, int *flags, - unsigned int *osversion, char **soname) + unsigned int *osversion, unsigned int *isa_level, + char **soname) { struct aux_cache_entry_id id; id.ino = (uint64_t) stat_buf->st_ino; @@ -932,6 +943,7 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, { *flags = entry->flags; *osversion = entry->osversion; + *isa_level = entry->isa_level; if (entry->soname != NULL) *soname = xstrdup (entry->soname); else @@ -945,7 +957,8 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, static void insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, - unsigned int osversion, const char *soname, int used) + unsigned int osversion, unsigned int isa_level, + const char *soname, int used) { size_t hash = aux_cache_entry_id_hash (id) % aux_hash_size; struct aux_cache_entry *entry; @@ -961,6 +974,7 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, entry->id = *id; entry->flags = flags; entry->osversion = osversion; + entry->isa_level = isa_level; entry->used = used; if (soname != NULL) entry->soname = memcpy ((char *) (entry + 1), soname, len); @@ -972,14 +986,15 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, void add_to_aux_cache (struct stat64 *stat_buf, int flags, - unsigned int osversion, const char *soname) + unsigned int osversion, unsigned int isa_level, + const char *soname) { struct aux_cache_entry_id id; id.ino = (uint64_t) stat_buf->st_ino; id.ctime = (uint64_t) stat_buf->st_ctime; id.size = (uint64_t) stat_buf->st_size; id.dev = (uint64_t) stat_buf->st_dev; - insert_to_aux_cache (&id, flags, osversion, soname, 1); + insert_to_aux_cache (&id, flags, osversion, isa_level, soname, 1); } /* Load auxiliary cache to search for unchanged entries. */ @@ -1025,6 +1040,7 @@ load_aux_cache (const char *aux_cache_name) insert_to_aux_cache (&aux_cache->libs[i].id, aux_cache->libs[i].flags, aux_cache->libs[i].osversion, + aux_cache->libs[i].isa_level, aux_cache->libs[i].soname == 0 ? NULL : aux_cache_data + aux_cache->libs[i].soname, 0); @@ -1093,7 +1109,7 @@ save_aux_cache (const char *aux_cache_name) str_offset += len; } file_entries->libs[idx].osversion = entry->osversion; - file_entries->libs[idx++].pad = 0; + file_entries->libs[idx++].isa_level = entry->isa_level; } /* Write out auxiliary cache file. */ diff --git a/elf/dl-cache.c b/elf/dl-cache.c index 97093bb700..edeff7ccc1 100644 --- a/elf/dl-cache.c +++ b/elf/dl-cache.c @@ -25,6 +25,7 @@ #include <stdint.h> #include <_itoa.h> #include <dl-hwcaps.h> +#include <dl-isa-level.h> #ifndef _DL_PLATFORMS_COUNT # define _DL_PLATFORMS_COUNT 0 @@ -284,6 +285,9 @@ search_cache (const char *string_table, uint32_t string_table_size, #ifdef SHARED named_hwcap = dl_cache_hwcap_extension (libnew); + if (named_hwcap + && !dl_cache_hwcap_isa_level_compatible (libnew)) + continue; #endif /* The entries with named/extension hwcaps diff --git a/elf/ldconfig.c b/elf/ldconfig.c index 10927a8c7f..9dceac38aa 100644 --- a/elf/ldconfig.c +++ b/elf/ldconfig.c @@ -655,6 +655,7 @@ manual_link (char *library) struct stat64 stat_buf; int flag; unsigned int osversion; + unsigned int isa_level; /* Prepare arguments for create_links call. Split library name in directory and filename first. Since path is allocated, we've got @@ -721,7 +722,7 @@ manual_link (char *library) } if (process_file (real_library, library, libname, &flag, &osversion, - &soname, 0, &stat_buf)) + &isa_level, &soname, 0, &stat_buf)) { error (0, 0, _("No link created since soname could not be found for %s"), library); @@ -768,6 +769,7 @@ struct dlib_entry int flag; int is_link; unsigned int osversion; + unsigned int isa_level; struct dlib_entry *next; }; @@ -980,17 +982,21 @@ search_dir (const struct dir_entry *entry) library already and it's not changed. */ char *soname; unsigned int osversion; - if (!search_aux_cache (&lstat_buf, &flag, &osversion, &soname)) + unsigned int isa_level; + if (!search_aux_cache (&lstat_buf, &flag, &osversion, &isa_level, + &soname)) { if (process_file (real_name, file_name, direntry->d_name, &flag, - &osversion, &soname, is_link, &lstat_buf)) + &osversion, &isa_level, &soname, is_link, + &lstat_buf)) { if (real_name != real_file_name) free (real_name); continue; } else if (opt_build_cache) - add_to_aux_cache (&lstat_buf, flag, osversion, soname); + add_to_aux_cache (&lstat_buf, flag, osversion, isa_level, + soname); } if (soname == NULL) @@ -1096,6 +1102,7 @@ search_dir (const struct dir_entry *entry) dlib_ptr->name = xstrdup (direntry->d_name); dlib_ptr->is_link = is_link; dlib_ptr->osversion = osversion; + dlib_ptr->isa_level = isa_level; } /* Don't add this library, abort loop. */ /* Also free soname, since it's dynamically allocated. */ @@ -1112,6 +1119,7 @@ search_dir (const struct dir_entry *entry) dlib_ptr->flag = flag; dlib_ptr->is_link = is_link; dlib_ptr->osversion = osversion; + dlib_ptr->isa_level = isa_level; /* Add at head of list. */ dlib_ptr->next = dlibs; dlibs = dlib_ptr; @@ -1149,7 +1157,7 @@ search_dir (const struct dir_entry *entry) if (opt_build_cache) add_to_cache (entry->path, filename, dlib_ptr->soname, dlib_ptr->flag, dlib_ptr->osversion, - hwcap, entry->hwcaps); + dlib_ptr->isa_level, hwcap, entry->hwcaps); } /* Free all resources. */ diff --git a/elf/readelflib.c b/elf/readelflib.c index 5905f6d344..abb1ac61fa 100644 --- a/elf/readelflib.c +++ b/elf/readelflib.c @@ -17,6 +17,8 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ +#include <read-prop.h> + /* This code is a heavily simplified version of the readelf program that's part of the current binutils development version. For architectures which need to handle both 32bit and 64bit ELF libraries, this file is @@ -40,8 +42,8 @@ do \ /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { int i; unsigned int j; @@ -86,6 +88,9 @@ process_elf_file (const char *file_name, const char *lib, int *flag, libc5/libc6. */ *flag = FLAG_ELF; + /* The default ISA level is 0. */ + *isa_level = 0; + dynamic_addr = 0; dynamic_size = 0; program_interpreter = NULL; @@ -164,6 +169,78 @@ process_elf_file (const char *file_name, const char *lib, int *flag, } break; + case PT_GNU_PROPERTY: + /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes + in 32-bit objects and to 8 bytes in 64-bit objects. Skip + notes with incorrect alignment. */ + if (segment->p_align == (__ELF_NATIVE_CLASS / 8)) + { + const ElfW(Nhdr) *note = (const void *) (file_contents + + segment->p_offset); + const ElfW(Addr) size = segment->p_filesz; + const ElfW(Addr) align = segment->p_align; + + const ElfW(Addr) start = (ElfW(Addr)) (uintptr_t) note; + unsigned int last_type = 0; + + while ((ElfW(Addr)) (uintptr_t) (note + 1) - start < size) + { + /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ + if (note->n_namesz == 4 + && note->n_type == NT_GNU_PROPERTY_TYPE_0 + && memcmp (note + 1, "GNU", 4) == 0) + { + /* Check for invalid property. */ + if (note->n_descsz < 8 + || (note->n_descsz % sizeof (ElfW(Addr))) != 0) + goto done; + + /* Start and end of property array. */ + unsigned char *ptr = (unsigned char *) (note + 1) + 4; + unsigned char *ptr_end = ptr + note->n_descsz; + + do + { + unsigned int type = *(unsigned int *) ptr; + unsigned int datasz = *(unsigned int *) (ptr + 4); + + /* Property type must be in ascending order. */ + if (type < last_type) + goto done; + + ptr += 8; + if ((ptr + datasz) > ptr_end) + goto done; + + last_type = type; + + /* Target specific property processing. + Return value: + false: Continue processing the properties. + true : Stop processing the properties. + */ + if (read_gnu_property (isa_level, type, + datasz, ptr)) + goto done; + + /* Check the next property item. */ + ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); + } + while ((ptr_end - ptr) >= 8); + + /* Only handle one NT_GNU_PROPERTY_TYPE_0. */ + goto done; + } + + note = ((const void *) note + + ELF_NOTE_NEXT_OFFSET (note->n_namesz, + note->n_descsz, + align)); + } + } +done: + break; + default: break; } diff --git a/elf/readlib.c b/elf/readlib.c index 994a4426a1..def0eafbeb 100644 --- a/elf/readlib.c +++ b/elf/readlib.c @@ -75,7 +75,8 @@ is_gdb_python_file (const char *name) int process_file (const char *real_file_name, const char *file_name, const char *lib, int *flag, unsigned int *osversion, - char **soname, int is_link, struct stat64 *stat_buf) + unsigned int *isa_level, char **soname, int is_link, + struct stat64 *stat_buf) { FILE *file; struct stat64 statbuf; @@ -173,8 +174,8 @@ process_file (const char *real_file_name, const char *file_name, /* Libraries have to be shared object files. */ else if (elf_header->e_type != ET_DYN) ret = 1; - else if (process_elf_file (file_name, lib, flag, osversion, soname, - file_contents, statbuf.st_size)) + else if (process_elf_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, statbuf.st_size)) ret = 1; done: diff --git a/elf/tst-glibc-hwcaps-2-cache.c b/elf/tst-glibc-hwcaps-2-cache.c new file mode 100644 index 0000000000..2c30a2c911 --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.c @@ -0,0 +1,45 @@ +/* Wrapper to invoke tst-glibc-hwcaps-2 in a container to test ldconfig. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +/* This program is just a wrapper that runs ldconfig followed by + tst-glibc-hwcaps-2. The actual test is provided via an + implementation in a sysdeps subdirectory. */ + +#include <stdio.h> +#include <stdlib.h> +#include <support/support.h> +#include <unistd.h> + +int +main (int argc, char **argv) +{ + /* Run ldconfig to populate the cache. */ + { + char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir); + if (system (command) != 0) + return 1; + free (command); + } + + /* Reuse tst-glibc-hwcaps. Since this code is running in a + container, we can launch it directly. */ + char *path = xasprintf ("%s/elf/tst-glibc-hwcaps-2", support_objdir_root); + execv (path, argv); + printf ("error: execv of %s failed: %m\n", path); + return 1; +} diff --git a/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf new file mode 100644 index 0000000000..e1e74dbda2 --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf @@ -0,0 +1,2 @@ +# This file was created to suppress a warning from ldconfig: +# /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory diff --git a/elf/tst-glibc-hwcaps-2-cache.root/postclean.req b/elf/tst-glibc-hwcaps-2-cache.root/postclean.req new file mode 100644 index 0000000000..e69de29bb2 diff --git a/elf/tst-glibc-hwcaps-2-cache.script b/elf/tst-glibc-hwcaps-2-cache.script new file mode 100644 index 0000000000..29ccfc3b49 --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.script @@ -0,0 +1,6 @@ +# test-container does not support scripts in sysdeps directories, so +# collect everything in one file. + +mkdirp 0770 $L/glibc-hwcaps/x86-64-v2 +cp $B/elf/libx86-64-isa-level-1.so $L/libx86-64-isa-level.so +cp $B/elf/libx86-64-isa-level-4.so $L/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so diff --git a/sysdeps/generic/dl-cache.h b/sysdeps/generic/dl-cache.h index 72a66b45ee..1138228e5c 100644 --- a/sysdeps/generic/dl-cache.h +++ b/sysdeps/generic/dl-cache.h @@ -106,14 +106,23 @@ struct file_entry_new entries. */ #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) +/* The number of the ISA level bits in the upper 32 bits of the hwcap + field. */ +#define DL_CACHE_HWCAP_ISA_LEVEL_COUNT 10 + +/* The mask of the ISA level bits in the hwcap field. */ +#define DL_CACHE_HWCAP_ISA_LEVEL_MASK \ + ((1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT) -1) + /* Return true if the ENTRY->hwcap value indicates that DL_CACHE_HWCAP_EXTENSION is used. */ static inline bool dl_cache_hwcap_extension (struct file_entry_new *entry) { - /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this - is a different kind of extension. */ - return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); + /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits, except for the + ISA level bits, as well, this is a different kind of extension. */ + return (((entry->hwcap >> 32) & ~DL_CACHE_HWCAP_ISA_LEVEL_MASK) + == (DL_CACHE_HWCAP_EXTENSION >> 32)); } /* See flags member of struct cache_file_new below. */ diff --git a/sysdeps/generic/dl-isa-level.h b/sysdeps/generic/dl-isa-level.h new file mode 100644 index 0000000000..77735775b9 --- /dev/null +++ b/sysdeps/generic/dl-isa-level.h @@ -0,0 +1,26 @@ +/* Support for reading ISA level in /etc/ld.so.cache files written by + Linux ldconfig. Generic version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +/* Return true if the ISA level in ENTRY is compatible with CPU. */ + +static inline bool +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) +{ + return true; +} diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h index 1ad1528890..a5ed350237 100644 --- a/sysdeps/generic/ldconfig.h +++ b/sysdeps/generic/ldconfig.h @@ -70,8 +70,9 @@ const char *glibc_hwcaps_subdirectory_name (const struct glibc_hwcaps_subdirectory *); extern void add_to_cache (const char *path, const char *filename, - const char *soname, - int flags, unsigned int osversion, uint64_t hwcap, + const char *soname, int flags, + unsigned int osversion, unsigned int isa_level, + uint64_t hwcap, struct glibc_hwcaps_subdirectory *); extern void init_aux_cache (void); @@ -79,23 +80,28 @@ extern void init_aux_cache (void); extern void load_aux_cache (const char *aux_cache_name); extern int search_aux_cache (struct stat64 *stat_buf, int *flags, - unsigned int *osversion, char **soname); + unsigned int *osversion, + unsigned int *isa_level, char **soname); extern void add_to_aux_cache (struct stat64 *stat_buf, int flags, - unsigned int osversion, const char *soname); + unsigned int osversion, + unsigned int isa_level, const char *soname); extern void save_aux_cache (const char *aux_cache_name); /* Declared in readlib.c. */ extern int process_file (const char *real_file_name, const char *file_name, - const char *lib, int *flag, unsigned int *osversion, - char **soname, int is_link, struct stat64 *stat_buf); + const char *lib, int *flag, + unsigned int *osversion, unsigned int *isa_level, + char **soname, int is_link, + struct stat64 *stat_buf); extern char *implicit_soname (const char *lib, int flag); /* Declared in readelflib.c. */ -extern int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +extern int process_elf_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Declared in chroot_canon.c. */ diff --git a/sysdeps/generic/read-prop.h b/sysdeps/generic/read-prop.h new file mode 100644 index 0000000000..091bd2f6c1 --- /dev/null +++ b/sysdeps/generic/read-prop.h @@ -0,0 +1,35 @@ +/* Support for GNU properties in ldconfig. Generic version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#ifndef _READ_PROP_H +#define _READ_PROP_H + +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. + Return value: + false: Continue processing the properties. + true : Stop processing the properties. + */ + +static inline bool __attribute__ ((always_inline)) +read_gnu_property (unsigned int *isal_level, uint32_t type, uint32_t + datasz, void *data) +{ + return true; +} + +#endif diff --git a/sysdeps/unix/sysv/linux/arm/readelflib.c b/sysdeps/unix/sysv/linux/arm/readelflib.c index 9d03fa6405..7f444add14 100644 --- a/sysdeps/unix/sysv/linux/arm/readelflib.c +++ b/sysdeps/unix/sysv/linux/arm/readelflib.c @@ -18,18 +18,20 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; @@ -38,8 +40,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, { Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); if (!ret && EF_ARM_EABI_VERSION (elf32_header->e_flags) == EF_ARM_EABI_VER5) { @@ -57,8 +59,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, } else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* AArch64 libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_AARCH64_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/ia64/readelflib.c b/sysdeps/unix/sysv/linux/ia64/readelflib.c index d24a934351..19d37b438e 100644 --- a/sysdeps/unix/sysv/linux/ia64/readelflib.c +++ b/sysdeps/unix/sysv/linux/ia64/readelflib.c @@ -16,29 +16,31 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, - void *file_contents, size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) - return process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + return process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* Intel 64bit libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_IA64_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/mips/readelflib.c b/sysdeps/unix/sysv/linux/mips/readelflib.c index 8363b8afcc..a3399961c9 100644 --- a/sysdeps/unix/sysv/linux/mips/readelflib.c +++ b/sysdeps/unix/sysv/linux/mips/readelflib.c @@ -20,18 +20,20 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { union { @@ -45,8 +47,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, elf_header.eh = file_contents; if (elf_header.eh->e_ident [EI_CLASS] == ELFCLASS32) { - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); if (!ret) { Elf32_Word flags = elf_header.eh32->e_flags; @@ -62,8 +64,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, } else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* n64 libraries are always libc.so.6+. */ if (!ret) { diff --git a/sysdeps/unix/sysv/linux/powerpc/readelflib.c b/sysdeps/unix/sysv/linux/powerpc/readelflib.c index eb20680418..ce01658fb3 100644 --- a/sysdeps/unix/sysv/linux/powerpc/readelflib.c +++ b/sysdeps/unix/sysv/linux/powerpc/readelflib.c @@ -17,29 +17,31 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) - return process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + return process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* PowerPC 64bit libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_POWERPC_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/riscv/readelflib.c b/sysdeps/unix/sysv/linux/riscv/readelflib.c index 1e70735cc0..98ae3c0b2b 100644 --- a/sysdeps/unix/sysv/linux/riscv/readelflib.c +++ b/sysdeps/unix/sysv/linux/riscv/readelflib.c @@ -17,11 +17,13 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* The ELF flags supported by our current glibc port: @@ -38,8 +40,8 @@ int process_elf64_file (const char *file_name, const char *lib, int *flag, /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; @@ -52,14 +54,14 @@ process_elf_file (const char *file_name, const char *lib, int *flag, if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) { - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); flags = elf32_header->e_flags; } else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); flags = elf64_header->e_flags; } diff --git a/sysdeps/unix/sysv/linux/s390/readelflib.c b/sysdeps/unix/sysv/linux/s390/readelflib.c index 1718efc9f9..6d19e351ba 100644 --- a/sysdeps/unix/sysv/linux/s390/readelflib.c +++ b/sysdeps/unix/sysv/linux/s390/readelflib.c @@ -16,29 +16,31 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) - return process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + return process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* S/390 64bit libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_S390_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/sparc/readelflib.c b/sysdeps/unix/sysv/linux/sparc/readelflib.c index e027a11a37..3768003925 100644 --- a/sysdeps/unix/sysv/linux/sparc/readelflib.c +++ b/sysdeps/unix/sysv/linux/sparc/readelflib.c @@ -18,29 +18,31 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) - return process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + return process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* Sparc 64bit libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_SPARC_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/x86/read-prop.h b/sysdeps/unix/sysv/linux/x86/read-prop.h new file mode 100644 index 0000000000..ac37fbc449 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/read-prop.h @@ -0,0 +1,61 @@ +/* Support for GNU properties in ldconfig. x86 version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#ifndef _READ_PROP_H +#define _READ_PROP_H + +#include <dl-cache.h> + +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. + Return value: + false: Continue processing the properties. + true : Stop processing the properties. + */ + +static inline bool __attribute__ ((always_inline)) +read_gnu_property (unsigned int *isal_level, uint32_t type, + uint32_t datasz, void *data) +{ + /* Property type must be in ascending order. */ + if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) + return true; + + if (type == GNU_PROPERTY_X86_ISA_1_NEEDED) + { + if (datasz == 4) + { + /* The size of GNU_PROPERTY_X86_ISA_1_NEEDED must be 4 bytes. + There is no point to continue if this type is ill-formed. */ + unsigned int isa_1_needed = *(unsigned int *) data; + _Static_assert (((sizeof (isa_1_needed) * 8) + <= (1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT)), + "DL_CACHE_HWCAP_ISA_LEVEL_COUNT is too small"); + if (isa_1_needed) + { + unsigned int level; + asm ("bsr %1, %0" : "=r" (level) : "g" (isa_1_needed)); + *isal_level = level; + } + } + return true; + } + + return false; +} + +#endif diff --git a/sysdeps/unix/sysv/linux/x86/readelflib.c b/sysdeps/unix/sysv/linux/x86/readelflib.c index 3e83419f5b..716629bf10 100644 --- a/sysdeps/unix/sysv/linux/x86/readelflib.c +++ b/sysdeps/unix/sysv/linux/x86/readelflib.c @@ -17,19 +17,20 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ - -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret, file_flag = 0; @@ -68,11 +69,11 @@ failed: } if (elf_header->e_ident[EI_CLASS] == ELFCLASS32) - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); if (!ret && file_flag) *flag = file_flag; diff --git a/sysdeps/unix/sysv/linux/x86_64/Makefile b/sysdeps/unix/sysv/linux/x86_64/Makefile index 9b82155393..9a7b3cd8e5 100644 --- a/sysdeps/unix/sysv/linux/x86_64/Makefile +++ b/sysdeps/unix/sysv/linux/x86_64/Makefile @@ -13,3 +13,34 @@ endif ifeq ($(subdir),misc) gen-as-const-headers += sigaltstack-offsets.sym endif + +ifeq ($(subdir),elf) +ifeq (yesyes,$(enable-x86-isa-level)$(config-cflags-skylake-avx512)) +tests += tst-glibc-hwcaps-2 +ifeq (no,$(build-hardcoded-path-in-tests)) +# This is an ld.so.cache test, and RPATH/RUNPATH in the executable +# interferes with its test objectives. +tests-container += tst-glibc-hwcaps-2-cache +endif +modules-names += libx86-64-isa-level-1 libx86-64-isa-level-4 + +$(objpfx)tst-glibc-hwcaps-2: $(objpfx)libx86-64-isa-level.so + +$(objpfx)tst-glibc-hwcaps-2.out: \ + $(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so +$(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so: \ + $(objpfx)libx86-64-isa-level-4.so + cp $< $@ + +CFLAGS-libx86-64-isa-level-1.os += -march=x86-64 +CFLAGS-libx86-64-isa-level-4.os += -march=skylake-avx512 + +# The test modules are parameterized by preprocessor macros. +LDFLAGS-libx86-64-isa-level-1.so += -Wl,-soname,libx86-64-isa-level.so +LDFLAGS-libx86-64-isa-level-4.so += -Wl,-soname,libx86-64-isa-level.so +$(objpfx)libx86-64-isa-level%.os: $(..)/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c + $(compile-command.c) -DVALUE=$(lastword $(subst -, ,$*)) +$(objpfx)libx86-64-isa-level.so: $(objpfx)libx86-64-isa-level-1.so + cp $< $@ +endif +endif # $(subdir) == elf diff --git a/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c new file mode 100644 index 0000000000..8d6b17d658 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c @@ -0,0 +1,61 @@ +/* Check ISA level on shared object in glibc-hwcaps subdirectories. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <stdlib.h> +#include <stdio.h> +#include <elf.h> +#include <get-isa-level.h> +#include <support/check.h> +#include <support/test-driver.h> + +extern int dso_isa_level (void); + +static int +do_test (void) +{ + const struct cpu_features *cpu_features + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); + unsigned int isa_level = get_isa_level (cpu_features); + int level = dso_isa_level (); + int ret; + switch (level) + { + case 1: + /* The default libx86-64-isa-level.so should be used. */ + printf ("The default shared library is used.\n"); + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) + ret = EXIT_FAILURE; + else + ret = EXIT_SUCCESS; + break; + case 4: + /* libx86-64-isa-level.so marked as x86-64 ISA level 4 needed in + x86-64-v2 should be ignored on lesser CPU. */ + printf ("x86-64 ISA level 4 shared library in x86-64-v2 is used.\n"); + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) + ret = EXIT_SUCCESS; + else + ret = EXIT_FAILURE; + break; + default: + abort (); + } + return ret; +} + +#include <support/test-driver.c> diff --git a/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c new file mode 100644 index 0000000000..2813d627cc --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c @@ -0,0 +1,4 @@ +#define INCLUDE_X86_ISA_LEVEL +#define MARKER dso_isa_level +#include <isa-level.c> +#include <markermodMARKER-VALUE.c> diff --git a/sysdeps/x86/dl-isa-level.h b/sysdeps/x86/dl-isa-level.h new file mode 100644 index 0000000000..c6ed8fb9d2 --- /dev/null +++ b/sysdeps/x86/dl-isa-level.h @@ -0,0 +1,32 @@ +/* Support for reading ISA level in /etc/ld.so.cache files written by + Linux ldconfig. x86 version. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <sys/platform/x86.h> + +/* Return true if the ISA level in ENTRY is compatible with CPU. */ + +static inline bool +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) +{ + const struct cpu_features *cpu_features = __get_cpu_features (); + unsigned int isa_level + = 1 << ((entry->hwcap >> 32) & DL_CACHE_HWCAP_ISA_LEVEL_MASK); + + return (isa_level & cpu_features->isa_1) == isa_level; +} -- 2.29.2 ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2020-12-09 3:27 ` [PATCH] ldconfig/x86: Store ISA level in cache and aux cache H.J. Lu @ 2021-01-12 16:25 ` Adhemerval Zanella 2021-01-12 23:32 ` V2 " H.J. Lu 2021-01-28 20:20 ` Florian Weimer 1 sibling, 1 reply; 27+ messages in thread From: Adhemerval Zanella @ 2021-01-12 16:25 UTC (permalink / raw) To: H.J. Lu, Florian Weimer; +Cc: H.J. Lu via Libc-alpha On 09/12/2020 00:27, H.J. Lu via Libc-alpha wrote: > On Tue, Dec 8, 2020 at 7:31 AM H.J. Lu <hjl.tools@gmail.com> wrote: >> >> On Tue, Dec 8, 2020 at 7:16 AM Florian Weimer <fweimer@redhat.com> wrote: >>> >>> * H. J. Lu: >>> >>>>>> For each file entry for a shared object, the hwcap field has been used by >>>>>> DL_CACHE_HWCAP_EXTENSION for glibc-hwcaps. Are you suggesting >>>>>> to add another file entry for the same shared object to store ISA level >>>>>> requirement? >>>>> >>>>> It's a 64-bit field. We do this: >>>>> >>>>> /* This bit in the hwcap field of struct file_entry_new indicates that >>>>> the lower 32 bits contain an index into the >>>>> cache_extension_tag_glibc_hwcaps section. Older glibc versions do >>>>> not know about this HWCAP bit, so they will ignore these >>>>> entries. */ >>>>> #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) >>>>> >>>>> /* Return true if the ENTRY->hwcap value indicates that >>>>> DL_CACHE_HWCAP_EXTENSION is used. */ >>>>> static inline bool >>>>> dl_cache_hwcap_extension (struct file_entry_new *entry) >>>>> { >>>>> /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this >>>>> is a different kind of extension. */ >>>>> return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); >>>>> } >>>>> >>>>> So we can use a different bit pattern involving DL_CACHE_HWCAP_EXTENSION >>>>> to express something else. In particular, only the lower 32 bits are >>>>> currently used as an index. >>>> >>>> So bits 33-47 can be used for ISA level requirement. This should support >>>> 65536 ISA levels. >>> >>> The question is whether the ISA levels and the glibc-hwcaps name are >>> used in parallel. If not, we can use (1ULL << 62) | (1ULL << 32) as >>> the marker bits and a 32-bit index in the lower half. >>> >>> (I do not think this is necessarily relevant for levels, but it could be >>> used for locating objects according to other criteria.) >>> >> >> I think ISA level and glibc-hwcaps should go together. >> > > Here is the patch to store ISA level in the portion of the unused > upper 32 bits of the hwcaps field in cache and the unused pad > field in aux cache. > > > From f5053fabd1e8a5567b487ef4054793e043a7b817 Mon Sep 17 00:00:00 2001 > From: "H.J. Lu" <hjl.tools@gmail.com> > Date: Sat, 5 Dec 2020 07:01:58 -0800 > Subject: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache > > Store ISA level in the portion of the unused upper 32 bits of the hwcaps > field in cache and the unused pad field in aux cache. ISA level is stored > and checked only for shared objects in glibc-hwcaps subdirectories. The > shared objects in the default directories aren't checked since there are > no fallbacks for these shared objects. Some comments below, most minor changes and code organization. The main change I would like is to improve test coverage, as for the first patch. > --- > elf/cache.c | 34 +++++--- > elf/dl-cache.c | 4 + > elf/ldconfig.c | 18 +++-- > elf/readelflib.c | 81 ++++++++++++++++++- > elf/readlib.c | 7 +- > elf/tst-glibc-hwcaps-2-cache.c | 45 +++++++++++ > .../etc/ld.so.conf | 2 + > .../postclean.req | 0 > elf/tst-glibc-hwcaps-2-cache.script | 6 ++ > sysdeps/generic/dl-cache.h | 15 +++- > sysdeps/generic/dl-isa-level.h | 26 ++++++ > sysdeps/generic/ldconfig.h | 22 +++-- > sysdeps/generic/read-prop.h | 35 ++++++++ > sysdeps/unix/sysv/linux/arm/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/ia64/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/mips/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/powerpc/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/riscv/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/s390/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/sparc/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/x86/read-prop.h | 61 ++++++++++++++ > sysdeps/unix/sysv/linux/x86/readelflib.c | 23 +++--- > sysdeps/unix/sysv/linux/x86_64/Makefile | 31 +++++++ > .../sysv/linux/x86_64/tst-glibc-hwcaps-2.c | 61 ++++++++++++++ > .../linux/x86_64/x86-64-isa-level-VALUE.c | 4 + > sysdeps/x86/dl-isa-level.h | 32 ++++++++ > 26 files changed, 550 insertions(+), 111 deletions(-) > create mode 100644 elf/tst-glibc-hwcaps-2-cache.c > create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/postclean.req > create mode 100644 elf/tst-glibc-hwcaps-2-cache.script > create mode 100644 sysdeps/generic/dl-isa-level.h > create mode 100644 sysdeps/generic/read-prop.h > create mode 100644 sysdeps/unix/sysv/linux/x86/read-prop.h > create mode 100644 sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > create mode 100644 sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > create mode 100644 sysdeps/x86/dl-isa-level.h > > diff --git a/elf/cache.c b/elf/cache.c > index b03c5319f8..91a72381ae 100644 > --- a/elf/cache.c > +++ b/elf/cache.c > @@ -145,6 +145,7 @@ struct cache_entry > struct stringtable_entry *path; /* Path to find library. */ > int flags; /* Flags to indicate kind of library. */ > unsigned int osversion; /* Required OS version. */ > + unsigned int isa_level; /* Required ISAL level. */ Maybe a typo here 'ISAL'? > uint64_t hwcap; /* Important hardware capabilities. */ > int bits_hwcap; /* Number of bits set in hwcap. */ > > @@ -660,8 +661,14 @@ save_cache (const char *cache_name) > if (entry->hwcaps == NULL) > file_entries_new->libs[idx_new].hwcap = entry->hwcap; > else > - file_entries_new->libs[idx_new].hwcap > - = DL_CACHE_HWCAP_EXTENSION | entry->hwcaps->section_index; > + { > + if (entry->isa_level > (1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT)) Maybe wrap this on a macro at dl-cache.h? It already does it for DL_CACHE_HWCAP_ISA_LEVEL_MASK. > + abort (); Maybe use 'error (...)' to print a proper error message? > + file_entries_new->libs[idx_new].hwcap > + = (DL_CACHE_HWCAP_EXTENSION > + | (((uint64_t) entry->isa_level) << 32) > + | entry->hwcaps->section_index); > + } Would be better to wrap this on a inline function at dl-cache.h? static inline unsigned int dl_cache_hwcap_isa_level (struct file_entry_new *entry) { return DL_CACHE_HWCAP_EXTENSION | ((uint64_t) entry->isa_level << 32) | entry->hwcaps->sections_index; } > file_entries_new->libs[idx_new].key > = str_offset + entry->lib->offset; > file_entries_new->libs[idx_new].value > @@ -776,7 +783,8 @@ save_cache (const char *cache_name) > /* Add one library to the cache. */ > void > add_to_cache (const char *path, const char *filename, const char *soname, > - int flags, unsigned int osversion, uint64_t hwcap, > + int flags, unsigned int osversion, > + unsigned int isa_level, uint64_t hwcap, > struct glibc_hwcaps_subdirectory *hwcaps) > { > struct cache_entry *new_entry = xmalloc (sizeof (*new_entry)); > @@ -794,6 +802,7 @@ add_to_cache (const char *path, const char *filename, const char *soname, > new_entry->path = path_interned; > new_entry->flags = flags; > new_entry->osversion = osversion; > + new_entry->isa_level = isa_level; > new_entry->hwcap = hwcap; > new_entry->hwcaps = hwcaps; > new_entry->bits_hwcap = 0; Ok. > @@ -850,6 +859,7 @@ struct aux_cache_entry > struct aux_cache_entry_id id; > int flags; > unsigned int osversion; > + unsigned int isa_level; > int used; > char *soname; > struct aux_cache_entry *next; > @@ -863,7 +873,7 @@ struct aux_cache_file_entry > int32_t flags; /* This is 1 for an ELF library. */ > uint32_t soname; /* String table indice. */ > uint32_t osversion; /* Required OS version. */ > - int32_t pad; > + uint32_t isa_level; /* Required ISA level. */ > }; > I think it would be good to have a _Static_assert with the expected aux_cache_entry size. > /* ldconfig maintains an auxiliary cache file that allows > @@ -914,7 +924,8 @@ init_aux_cache (void) > > int > search_aux_cache (struct stat64 *stat_buf, int *flags, > - unsigned int *osversion, char **soname) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname) > { > struct aux_cache_entry_id id; > id.ino = (uint64_t) stat_buf->st_ino; > @@ -932,6 +943,7 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, > { > *flags = entry->flags; > *osversion = entry->osversion; > + *isa_level = entry->isa_level; > if (entry->soname != NULL) > *soname = xstrdup (entry->soname); > else Ok. > @@ -945,7 +957,8 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, > > static void > insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > - unsigned int osversion, const char *soname, int used) > + unsigned int osversion, unsigned int isa_level, > + const char *soname, int used) > { > size_t hash = aux_cache_entry_id_hash (id) % aux_hash_size; > struct aux_cache_entry *entry; > @@ -961,6 +974,7 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > entry->id = *id; > entry->flags = flags; > entry->osversion = osversion; > + entry->isa_level = isa_level; > entry->used = used; > if (soname != NULL) > entry->soname = memcpy ((char *) (entry + 1), soname, len); Ok. > @@ -972,14 +986,15 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > > void > add_to_aux_cache (struct stat64 *stat_buf, int flags, > - unsigned int osversion, const char *soname) > + unsigned int osversion, unsigned int isa_level, > + const char *soname) > { > struct aux_cache_entry_id id; > id.ino = (uint64_t) stat_buf->st_ino; > id.ctime = (uint64_t) stat_buf->st_ctime; > id.size = (uint64_t) stat_buf->st_size; > id.dev = (uint64_t) stat_buf->st_dev; > - insert_to_aux_cache (&id, flags, osversion, soname, 1); > + insert_to_aux_cache (&id, flags, osversion, isa_level, soname, 1); > } > > /* Load auxiliary cache to search for unchanged entries. */ > @@ -1025,6 +1040,7 @@ load_aux_cache (const char *aux_cache_name) > insert_to_aux_cache (&aux_cache->libs[i].id, > aux_cache->libs[i].flags, > aux_cache->libs[i].osversion, > + aux_cache->libs[i].isa_level, > aux_cache->libs[i].soname == 0 > ? NULL : aux_cache_data + aux_cache->libs[i].soname, > 0); > @@ -1093,7 +1109,7 @@ save_aux_cache (const char *aux_cache_name) > str_offset += len; > } > file_entries->libs[idx].osversion = entry->osversion; > - file_entries->libs[idx++].pad = 0; > + file_entries->libs[idx++].isa_level = entry->isa_level; > } > > /* Write out auxiliary cache file. */ Ok. > diff --git a/elf/dl-cache.c b/elf/dl-cache.c > index 97093bb700..edeff7ccc1 100644 > --- a/elf/dl-cache.c > +++ b/elf/dl-cache.c > @@ -25,6 +25,7 @@ > #include <stdint.h> > #include <_itoa.h> > #include <dl-hwcaps.h> > +#include <dl-isa-level.h> > > #ifndef _DL_PLATFORMS_COUNT > # define _DL_PLATFORMS_COUNT 0 > @@ -284,6 +285,9 @@ search_cache (const char *string_table, uint32_t string_table_size, > > #ifdef SHARED > named_hwcap = dl_cache_hwcap_extension (libnew); > + if (named_hwcap > + && !dl_cache_hwcap_isa_level_compatible (libnew)) > + continue; > #endif > > /* The entries with named/extension hwcaps Ok, so it skips non compatible ISA shared libraries. > diff --git a/elf/ldconfig.c b/elf/ldconfig.c > index 10927a8c7f..9dceac38aa 100644 > --- a/elf/ldconfig.c > +++ b/elf/ldconfig.c > @@ -655,6 +655,7 @@ manual_link (char *library) > struct stat64 stat_buf; > int flag; > unsigned int osversion; > + unsigned int isa_level; > > /* Prepare arguments for create_links call. Split library name in > directory and filename first. Since path is allocated, we've got > @@ -721,7 +722,7 @@ manual_link (char *library) > } > > if (process_file (real_library, library, libname, &flag, &osversion, > - &soname, 0, &stat_buf)) > + &isa_level, &soname, 0, &stat_buf)) > { > error (0, 0, _("No link created since soname could not be found for %s"), > library); > @@ -768,6 +769,7 @@ struct dlib_entry > int flag; > int is_link; > unsigned int osversion; > + unsigned int isa_level; > struct dlib_entry *next; > }; > Ok. > @@ -980,17 +982,21 @@ search_dir (const struct dir_entry *entry) > library already and it's not changed. */ > char *soname; > unsigned int osversion; > - if (!search_aux_cache (&lstat_buf, &flag, &osversion, &soname)) > + unsigned int isa_level; > + if (!search_aux_cache (&lstat_buf, &flag, &osversion, &isa_level, > + &soname)) > { > if (process_file (real_name, file_name, direntry->d_name, &flag, > - &osversion, &soname, is_link, &lstat_buf)) > + &osversion, &isa_level, &soname, is_link, > + &lstat_buf)) > { > if (real_name != real_file_name) > free (real_name); > continue; > } > else if (opt_build_cache) > - add_to_aux_cache (&lstat_buf, flag, osversion, soname); > + add_to_aux_cache (&lstat_buf, flag, osversion, isa_level, > + soname); > } > > if (soname == NULL) Ok. > @@ -1096,6 +1102,7 @@ search_dir (const struct dir_entry *entry) > dlib_ptr->name = xstrdup (direntry->d_name); > dlib_ptr->is_link = is_link; > dlib_ptr->osversion = osversion; > + dlib_ptr->isa_level = isa_level; > } > /* Don't add this library, abort loop. */ > /* Also free soname, since it's dynamically allocated. */ > @@ -1112,6 +1119,7 @@ search_dir (const struct dir_entry *entry) > dlib_ptr->flag = flag; > dlib_ptr->is_link = is_link; > dlib_ptr->osversion = osversion; > + dlib_ptr->isa_level = isa_level; > /* Add at head of list. */ > dlib_ptr->next = dlibs; > dlibs = dlib_ptr; > @@ -1149,7 +1157,7 @@ search_dir (const struct dir_entry *entry) > if (opt_build_cache) > add_to_cache (entry->path, filename, dlib_ptr->soname, > dlib_ptr->flag, dlib_ptr->osversion, > - hwcap, entry->hwcaps); > + dlib_ptr->isa_level, hwcap, entry->hwcaps); > } > > /* Free all resources. */ Ok. > diff --git a/elf/readelflib.c b/elf/readelflib.c > index 5905f6d344..abb1ac61fa 100644 > --- a/elf/readelflib.c > +++ b/elf/readelflib.c > @@ -17,6 +17,8 @@ > License along with the GNU C Library; if not, see > <https://www.gnu.org/licenses/>. */ > > +#include <read-prop.h> > + > /* This code is a heavily simplified version of the readelf program > that's part of the current binutils development version. For architectures > which need to handle both 32bit and 64bit ELF libraries, this file is > @@ -40,8 +42,8 @@ do \ > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > int i; > unsigned int j; > @@ -86,6 +88,9 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > libc5/libc6. */ > *flag = FLAG_ELF; > > + /* The default ISA level is 0. */ > + *isa_level = 0; > + > dynamic_addr = 0; > dynamic_size = 0; > program_interpreter = NULL; > @@ -164,6 +169,78 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > } > break; > > + case PT_GNU_PROPERTY: > + /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes > + in 32-bit objects and to 8 bytes in 64-bit objects. Skip > + notes with incorrect alignment. */ > + if (segment->p_align == (__ELF_NATIVE_CLASS / 8)) > + { > + const ElfW(Nhdr) *note = (const void *) (file_contents > + + segment->p_offset); > + const ElfW(Addr) size = segment->p_filesz; > + const ElfW(Addr) align = segment->p_align; > + > + const ElfW(Addr) start = (ElfW(Addr)) (uintptr_t) note; > + unsigned int last_type = 0; > + > + while ((ElfW(Addr)) (uintptr_t) (note + 1) - start < size) > + { > + /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ > + if (note->n_namesz == 4 > + && note->n_type == NT_GNU_PROPERTY_TYPE_0 > + && memcmp (note + 1, "GNU", 4) == 0) > + { > + /* Check for invalid property. */ > + if (note->n_descsz < 8 > + || (note->n_descsz % sizeof (ElfW(Addr))) != 0) > + goto done; > + > + /* Start and end of property array. */ > + unsigned char *ptr = (unsigned char *) (note + 1) + 4; > + unsigned char *ptr_end = ptr + note->n_descsz; > + > + do > + { > + unsigned int type = *(unsigned int *) ptr; > + unsigned int datasz = *(unsigned int *) (ptr + 4); > + > + /* Property type must be in ascending order. */ > + if (type < last_type) > + goto done; > + > + ptr += 8; > + if ((ptr + datasz) > ptr_end) > + goto done; > + > + last_type = type; > + > + /* Target specific property processing. > + Return value: > + false: Continue processing the properties. > + true : Stop processing the properties. > + */ > + if (read_gnu_property (isa_level, type, > + datasz, ptr)) > + goto done; > + > + /* Check the next property item. */ > + ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); > + } > + while ((ptr_end - ptr) >= 8); > + > + /* Only handle one NT_GNU_PROPERTY_TYPE_0. */ > + goto done; > + } > + > + note = ((const void *) note > + + ELF_NOTE_NEXT_OFFSET (note->n_namesz, > + note->n_descsz, > + align)); > + } > + } > +done: > + break; > + > default: > break; > } Ok, although this is quite similar to the _dl_process_pt_gnu_property (maybe a future work to try refactor both to use a common code). > diff --git a/elf/readlib.c b/elf/readlib.c > index 994a4426a1..def0eafbeb 100644 > --- a/elf/readlib.c > +++ b/elf/readlib.c > @@ -75,7 +75,8 @@ is_gdb_python_file (const char *name) > int > process_file (const char *real_file_name, const char *file_name, > const char *lib, int *flag, unsigned int *osversion, > - char **soname, int is_link, struct stat64 *stat_buf) > + unsigned int *isa_level, char **soname, int is_link, > + struct stat64 *stat_buf) > { > FILE *file; > struct stat64 statbuf; > @@ -173,8 +174,8 @@ process_file (const char *real_file_name, const char *file_name, > /* Libraries have to be shared object files. */ > else if (elf_header->e_type != ET_DYN) > ret = 1; > - else if (process_elf_file (file_name, lib, flag, osversion, soname, > - file_contents, statbuf.st_size)) > + else if (process_elf_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, statbuf.st_size)) > ret = 1; > > done: Ok. > diff --git a/elf/tst-glibc-hwcaps-2-cache.c b/elf/tst-glibc-hwcaps-2-cache.c > new file mode 100644 > index 0000000000..2c30a2c911 > --- /dev/null > +++ b/elf/tst-glibc-hwcaps-2-cache.c > @@ -0,0 +1,45 @@ > +/* Wrapper to invoke tst-glibc-hwcaps-2 in a container to test ldconfig. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +/* This program is just a wrapper that runs ldconfig followed by > + tst-glibc-hwcaps-2. The actual test is provided via an > + implementation in a sysdeps subdirectory. */ > + > +#include <stdio.h> > +#include <stdlib.h> > +#include <support/support.h> > +#include <unistd.h> > + > +int > +main (int argc, char **argv) > +{ > + /* Run ldconfig to populate the cache. */ > + { > + char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir); > + if (system (command) != 0) > + return 1; > + free (command); > + } Maybe use support_capture_subprogram here? char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir); struct support_capture_subprocess result = support_capture_subprogram (command, &((char *) { NULL })); support_capture_subprocess_check (&result, "ldconfig", 0, sc_allow_none); free (command); > + > + /* Reuse tst-glibc-hwcaps. Since this code is running in a > + container, we can launch it directly. */ > + char *path = xasprintf ("%s/elf/tst-glibc-hwcaps-2", support_objdir_root); > + execv (path, argv); > + printf ("error: execv of %s failed: %m\n", path); > + return 1; > +} Ok. > diff --git a/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > new file mode 100644 > index 0000000000..e1e74dbda2 > --- /dev/null > +++ b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > @@ -0,0 +1,2 @@ > +# This file was created to suppress a warning from ldconfig: > +# /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory > diff --git a/elf/tst-glibc-hwcaps-2-cache.root/postclean.req b/elf/tst-glibc-hwcaps-2-cache.root/postclean.req > new file mode 100644 > index 0000000000..e69de29bb2 Ok. > diff --git a/elf/tst-glibc-hwcaps-2-cache.script b/elf/tst-glibc-hwcaps-2-cache.script > new file mode 100644 > index 0000000000..29ccfc3b49 > --- /dev/null > +++ b/elf/tst-glibc-hwcaps-2-cache.script > @@ -0,0 +1,6 @@ > +# test-container does not support scripts in sysdeps directories, so > +# collect everything in one file. > + > +mkdirp 0770 $L/glibc-hwcaps/x86-64-v2 Ok. > +cp $B/elf/libx86-64-isa-level-1.so $L/libx86-64-isa-level.so > +cp $B/elf/libx86-64-isa-level-4.so $L/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so > diff --git a/sysdeps/generic/dl-cache.h b/sysdeps/generic/dl-cache.h > index 72a66b45ee..1138228e5c 100644 > --- a/sysdeps/generic/dl-cache.h > +++ b/sysdeps/generic/dl-cache.h > @@ -106,14 +106,23 @@ struct file_entry_new > entries. */ > #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) > > +/* The number of the ISA level bits in the upper 32 bits of the hwcap > + field. */ > +#define DL_CACHE_HWCAP_ISA_LEVEL_COUNT 10 > + > +/* The mask of the ISA level bits in the hwcap field. */ > +#define DL_CACHE_HWCAP_ISA_LEVEL_MASK \ > + ((1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT) -1) > + Space after '-'. > /* Return true if the ENTRY->hwcap value indicates that > DL_CACHE_HWCAP_EXTENSION is used. */ > static inline bool > dl_cache_hwcap_extension (struct file_entry_new *entry) > { > - /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this > - is a different kind of extension. */ > - return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); > + /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits, except for the > + ISA level bits, as well, this is a different kind of extension. */ > + return (((entry->hwcap >> 32) & ~DL_CACHE_HWCAP_ISA_LEVEL_MASK) > + == (DL_CACHE_HWCAP_EXTENSION >> 32)); > } > > /* See flags member of struct cache_file_new below. */ The comment sounds confusing. > diff --git a/sysdeps/generic/dl-isa-level.h b/sysdeps/generic/dl-isa-level.h > new file mode 100644 > index 0000000000..77735775b9 > --- /dev/null > +++ b/sysdeps/generic/dl-isa-level.h > @@ -0,0 +1,26 @@ > +/* Support for reading ISA level in /etc/ld.so.cache files written by > + Linux ldconfig. Generic version. > + Copyright (C) 2020 Free Software Foundation, Inc. s/2020/2021 > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +/* Return true if the ISA level in ENTRY is compatible with CPU. */ > + My understanding is for function comment there is no need to add an extra newline. > +static inline bool > +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) > +{ > + return true; > +} Ok. > diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h > index 1ad1528890..a5ed350237 100644 > --- a/sysdeps/generic/ldconfig.h > +++ b/sysdeps/generic/ldconfig.h > @@ -70,8 +70,9 @@ const char *glibc_hwcaps_subdirectory_name > (const struct glibc_hwcaps_subdirectory *); > > extern void add_to_cache (const char *path, const char *filename, > - const char *soname, > - int flags, unsigned int osversion, uint64_t hwcap, > + const char *soname, int flags, > + unsigned int osversion, unsigned int isa_level, > + uint64_t hwcap, > struct glibc_hwcaps_subdirectory *); > > extern void init_aux_cache (void); > @@ -79,23 +80,28 @@ extern void init_aux_cache (void); > extern void load_aux_cache (const char *aux_cache_name); > > extern int search_aux_cache (struct stat64 *stat_buf, int *flags, > - unsigned int *osversion, char **soname); > + unsigned int *osversion, > + unsigned int *isa_level, char **soname); > > extern void add_to_aux_cache (struct stat64 *stat_buf, int flags, > - unsigned int osversion, const char *soname); > + unsigned int osversion, > + unsigned int isa_level, const char *soname); > > extern void save_aux_cache (const char *aux_cache_name); > > /* Declared in readlib.c. */ > extern int process_file (const char *real_file_name, const char *file_name, > - const char *lib, int *flag, unsigned int *osversion, > - char **soname, int is_link, struct stat64 *stat_buf); > + const char *lib, int *flag, > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, int is_link, > + struct stat64 *stat_buf); > > extern char *implicit_soname (const char *lib, int flag); > > /* Declared in readelflib.c. */ > -extern int process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +extern int process_elf_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Declared in chroot_canon.c. */ Ok. > diff --git a/sysdeps/generic/read-prop.h b/sysdeps/generic/read-prop.h > new file mode 100644 > index 0000000000..091bd2f6c1 > --- /dev/null > +++ b/sysdeps/generic/read-prop.h Maybe elf-read-prop.h to add more information on exactly this file intends to? > @@ -0,0 +1,35 @@ > +/* Support for GNU properties in ldconfig. Generic version. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#ifndef _READ_PROP_H > +#define _READ_PROP_H > + > +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. > + Return value: > + false: Continue processing the properties. > + true : Stop processing the properties. > + */ > + > +static inline bool __attribute__ ((always_inline)) > +read_gnu_property (unsigned int *isal_level, uint32_t type, uint32_t > + datasz, void *data) > +{ > + return true; > +} > + > +#endif Ok. > diff --git a/sysdeps/unix/sysv/linux/arm/readelflib.c b/sysdeps/unix/sysv/linux/arm/readelflib.c > index 9d03fa6405..7f444add14 100644 > --- a/sysdeps/unix/sysv/linux/arm/readelflib.c > +++ b/sysdeps/unix/sysv/linux/arm/readelflib.c > @@ -18,18 +18,20 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > @@ -38,8 +40,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > { > Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; > > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > > if (!ret && EF_ARM_EABI_VERSION (elf32_header->e_flags) == EF_ARM_EABI_VER5) > { > @@ -57,8 +59,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > } > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* AArch64 libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_AARCH64_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/ia64/readelflib.c b/sysdeps/unix/sysv/linux/ia64/readelflib.c > index d24a934351..19d37b438e 100644 > --- a/sysdeps/unix/sysv/linux/ia64/readelflib.c > +++ b/sysdeps/unix/sysv/linux/ia64/readelflib.c > @@ -16,29 +16,31 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > - void *file_contents, size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > - return process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* Intel 64bit libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_IA64_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/mips/readelflib.c b/sysdeps/unix/sysv/linux/mips/readelflib.c > index 8363b8afcc..a3399961c9 100644 > --- a/sysdeps/unix/sysv/linux/mips/readelflib.c > +++ b/sysdeps/unix/sysv/linux/mips/readelflib.c > @@ -20,18 +20,20 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > union > { > @@ -45,8 +47,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > elf_header.eh = file_contents; > if (elf_header.eh->e_ident [EI_CLASS] == ELFCLASS32) > { > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > if (!ret) > { > Elf32_Word flags = elf_header.eh32->e_flags; > @@ -62,8 +64,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > } > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* n64 libraries are always libc.so.6+. */ > if (!ret) > { Ok. > diff --git a/sysdeps/unix/sysv/linux/powerpc/readelflib.c b/sysdeps/unix/sysv/linux/powerpc/readelflib.c > index eb20680418..ce01658fb3 100644 > --- a/sysdeps/unix/sysv/linux/powerpc/readelflib.c > +++ b/sysdeps/unix/sysv/linux/powerpc/readelflib.c > @@ -17,29 +17,31 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > - return process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* PowerPC 64bit libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_POWERPC_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/riscv/readelflib.c b/sysdeps/unix/sysv/linux/riscv/readelflib.c > index 1e70735cc0..98ae3c0b2b 100644 > --- a/sysdeps/unix/sysv/linux/riscv/readelflib.c > +++ b/sysdeps/unix/sysv/linux/riscv/readelflib.c > @@ -17,11 +17,13 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* The ELF flags supported by our current glibc port: > @@ -38,8 +40,8 @@ int process_elf64_file (const char *file_name, const char *lib, int *flag, > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; > @@ -52,14 +54,14 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > { > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > flags = elf32_header->e_flags; > } > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > flags = elf64_header->e_flags; > } > Ok. > diff --git a/sysdeps/unix/sysv/linux/s390/readelflib.c b/sysdeps/unix/sysv/linux/s390/readelflib.c > index 1718efc9f9..6d19e351ba 100644 > --- a/sysdeps/unix/sysv/linux/s390/readelflib.c > +++ b/sysdeps/unix/sysv/linux/s390/readelflib.c > @@ -16,29 +16,31 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > - return process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* S/390 64bit libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_S390_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/sparc/readelflib.c b/sysdeps/unix/sysv/linux/sparc/readelflib.c > index e027a11a37..3768003925 100644 > --- a/sysdeps/unix/sysv/linux/sparc/readelflib.c > +++ b/sysdeps/unix/sysv/linux/sparc/readelflib.c > @@ -18,29 +18,31 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > - return process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* Sparc 64bit libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_SPARC_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/x86/read-prop.h b/sysdeps/unix/sysv/linux/x86/read-prop.h > new file mode 100644 > index 0000000000..ac37fbc449 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/x86/read-prop.h > @@ -0,0 +1,61 @@ > +/* Support for GNU properties in ldconfig. x86 version. > + Copyright (C) 2020 Free Software Foundation, Inc. s/2020/2021. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#ifndef _READ_PROP_H > +#define _READ_PROP_H > + > +#include <dl-cache.h> > + > +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. > + Return value: > + false: Continue processing the properties. > + true : Stop processing the properties. > + */ > + > +static inline bool __attribute__ ((always_inline)) > +read_gnu_property (unsigned int *isal_level, uint32_t type, > + uint32_t datasz, void *data) > +{ > + /* Property type must be in ascending order. */ > + if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) > + return true; > + > + if (type == GNU_PROPERTY_X86_ISA_1_NEEDED) > + { > + if (datasz == 4) > + { > + /* The size of GNU_PROPERTY_X86_ISA_1_NEEDED must be 4 bytes. > + There is no point to continue if this type is ill-formed. */ > + unsigned int isa_1_needed = *(unsigned int *) data; > + _Static_assert (((sizeof (isa_1_needed) * 8) > + <= (1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT)), > + "DL_CACHE_HWCAP_ISA_LEVEL_COUNT is too small"); > + if (isa_1_needed) Implicit check. > + { > + unsigned int level; > + asm ("bsr %1, %0" : "=r" (level) : "g" (isa_1_needed)); > + *isal_level = level; > + } > + } > + return true; > + } > + > + return false; > +} > + > +#endif Ok. > diff --git a/sysdeps/unix/sysv/linux/x86/readelflib.c b/sysdeps/unix/sysv/linux/x86/readelflib.c > index 3e83419f5b..716629bf10 100644 > --- a/sysdeps/unix/sysv/linux/x86/readelflib.c > +++ b/sysdeps/unix/sysv/linux/x86/readelflib.c > @@ -17,19 +17,20 @@ > License along with the GNU C Library; if not, see > <https://www.gnu.org/licenses/>. */ > > - > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret, file_flag = 0; > @@ -68,11 +69,11 @@ failed: > } > > if (elf_header->e_ident[EI_CLASS] == ELFCLASS32) > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > > if (!ret && file_flag) > *flag = file_flag; Ok. > diff --git a/sysdeps/unix/sysv/linux/x86_64/Makefile b/sysdeps/unix/sysv/linux/x86_64/Makefile > index 9b82155393..9a7b3cd8e5 100644 > --- a/sysdeps/unix/sysv/linux/x86_64/Makefile > +++ b/sysdeps/unix/sysv/linux/x86_64/Makefile > @@ -13,3 +13,34 @@ endif > ifeq ($(subdir),misc) > gen-as-const-headers += sigaltstack-offsets.sym > endif > + > +ifeq ($(subdir),elf) > +ifeq (yesyes,$(enable-x86-isa-level)$(config-cflags-skylake-avx512)) As for first patch, I think we should extend the test coverage to check not only for config-cflags-skylake-avx512. It means to build at least one shared object with different ISA levels and run the ldconfig and check the output against the system supported ISA level. > +tests += tst-glibc-hwcaps-2 > +ifeq (no,$(build-hardcoded-path-in-tests)) > +# This is an ld.so.cache test, and RPATH/RUNPATH in the executable > +# interferes with its test objectives. > +tests-container += tst-glibc-hwcaps-2-cache > +endif > +modules-names += libx86-64-isa-level-1 libx86-64-isa-level-4 > + > +$(objpfx)tst-glibc-hwcaps-2: $(objpfx)libx86-64-isa-level.so > + > +$(objpfx)tst-glibc-hwcaps-2.out: \ > + $(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so > +$(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so: \ > + $(objpfx)libx86-64-isa-level-4.so > + cp $< $@ > + > +CFLAGS-libx86-64-isa-level-1.os += -march=x86-64 > +CFLAGS-libx86-64-isa-level-4.os += -march=skylake-avx512 > + > +# The test modules are parameterized by preprocessor macros. > +LDFLAGS-libx86-64-isa-level-1.so += -Wl,-soname,libx86-64-isa-level.so > +LDFLAGS-libx86-64-isa-level-4.so += -Wl,-soname,libx86-64-isa-level.so > +$(objpfx)libx86-64-isa-level%.os: $(..)/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > + $(compile-command.c) -DVALUE=$(lastword $(subst -, ,$*)) > +$(objpfx)libx86-64-isa-level.so: $(objpfx)libx86-64-isa-level-1.so > + cp $< $@ > +endif > +endif # $(subdir) == elf > diff --git a/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > new file mode 100644 > index 0000000000..8d6b17d658 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > @@ -0,0 +1,61 @@ > +/* Check ISA level on shared object in glibc-hwcaps subdirectories. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <stdlib.h> > +#include <stdio.h> > +#include <elf.h> > +#include <get-isa-level.h> > +#include <support/check.h> > +#include <support/test-driver.h> > + > +extern int dso_isa_level (void); > + > +static int > +do_test (void) > +{ > + const struct cpu_features *cpu_features > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > + unsigned int isa_level = get_isa_level (cpu_features); > + int level = dso_isa_level (); > + int ret; > + switch (level) > + { > + case 1: > + /* The default libx86-64-isa-level.so should be used. */ > + printf ("The default shared library is used.\n"); > + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) > + ret = EXIT_FAILURE; > + else > + ret = EXIT_SUCCESS; > + break; > + case 4: > + /* libx86-64-isa-level.so marked as x86-64 ISA level 4 needed in > + x86-64-v2 should be ignored on lesser CPU. */ > + printf ("x86-64 ISA level 4 shared library in x86-64-v2 is used.\n"); > + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) > + ret = EXIT_SUCCESS; > + else > + ret = EXIT_FAILURE; > + break; > + default: > + abort (); > + } > + return ret; > +} > + > +#include <support/test-driver.c> > diff --git a/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > new file mode 100644 > index 0000000000..2813d627cc > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > @@ -0,0 +1,4 @@ > +#define INCLUDE_X86_ISA_LEVEL > +#define MARKER dso_isa_level > +#include <isa-level.c> > +#include <markermodMARKER-VALUE.c> > diff --git a/sysdeps/x86/dl-isa-level.h b/sysdeps/x86/dl-isa-level.h > new file mode 100644 > index 0000000000..c6ed8fb9d2 > --- /dev/null > +++ b/sysdeps/x86/dl-isa-level.h > @@ -0,0 +1,32 @@ > +/* Support for reading ISA level in /etc/ld.so.cache files written by > + Linux ldconfig. x86 version. > + Copyright (C) 2020 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <sys/platform/x86.h> > + > +/* Return true if the ISA level in ENTRY is compatible with CPU. */ > + > +static inline bool > +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) > +{ > + const struct cpu_features *cpu_features = __get_cpu_features (); > + unsigned int isa_level > + = 1 << ((entry->hwcap >> 32) & DL_CACHE_HWCAP_ISA_LEVEL_MASK); > + > + return (isa_level & cpu_features->isa_1) == isa_level; > +} Ok. > -- > 2.29.2 ^ permalink raw reply [flat|nested] 27+ messages in thread
* V2 [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2021-01-12 16:25 ` Adhemerval Zanella @ 2021-01-12 23:32 ` H.J. Lu 2021-01-13 13:47 ` Adhemerval Zanella 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2021-01-12 23:32 UTC (permalink / raw) To: Adhemerval Zanella; +Cc: Florian Weimer, H.J. Lu via Libc-alpha [-- Attachment #1: Type: text/plain, Size: 64192 bytes --] On Tue, Jan 12, 2021 at 8:25 AM Adhemerval Zanella <adhemerval.zanella@linaro.org> wrote: > > > > On 09/12/2020 00:27, H.J. Lu via Libc-alpha wrote: > > On Tue, Dec 8, 2020 at 7:31 AM H.J. Lu <hjl.tools@gmail.com> wrote: > >> > >> On Tue, Dec 8, 2020 at 7:16 AM Florian Weimer <fweimer@redhat.com> wrote: > >>> > >>> * H. J. Lu: > >>> > >>>>>> For each file entry for a shared object, the hwcap field has been used by > >>>>>> DL_CACHE_HWCAP_EXTENSION for glibc-hwcaps. Are you suggesting > >>>>>> to add another file entry for the same shared object to store ISA level > >>>>>> requirement? > >>>>> > >>>>> It's a 64-bit field. We do this: > >>>>> > >>>>> /* This bit in the hwcap field of struct file_entry_new indicates that > >>>>> the lower 32 bits contain an index into the > >>>>> cache_extension_tag_glibc_hwcaps section. Older glibc versions do > >>>>> not know about this HWCAP bit, so they will ignore these > >>>>> entries. */ > >>>>> #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) > >>>>> > >>>>> /* Return true if the ENTRY->hwcap value indicates that > >>>>> DL_CACHE_HWCAP_EXTENSION is used. */ > >>>>> static inline bool > >>>>> dl_cache_hwcap_extension (struct file_entry_new *entry) > >>>>> { > >>>>> /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this > >>>>> is a different kind of extension. */ > >>>>> return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); > >>>>> } > >>>>> > >>>>> So we can use a different bit pattern involving DL_CACHE_HWCAP_EXTENSION > >>>>> to express something else. In particular, only the lower 32 bits are > >>>>> currently used as an index. > >>>> > >>>> So bits 33-47 can be used for ISA level requirement. This should support > >>>> 65536 ISA levels. > >>> > >>> The question is whether the ISA levels and the glibc-hwcaps name are > >>> used in parallel. If not, we can use (1ULL << 62) | (1ULL << 32) as > >>> the marker bits and a 32-bit index in the lower half. > >>> > >>> (I do not think this is necessarily relevant for levels, but it could be > >>> used for locating objects according to other criteria.) > >>> > >> > >> I think ISA level and glibc-hwcaps should go together. > >> > > > > Here is the patch to store ISA level in the portion of the unused > > upper 32 bits of the hwcaps field in cache and the unused pad > > field in aux cache. > > > > > > From f5053fabd1e8a5567b487ef4054793e043a7b817 Mon Sep 17 00:00:00 2001 > > From: "H.J. Lu" <hjl.tools@gmail.com> > > Date: Sat, 5 Dec 2020 07:01:58 -0800 > > Subject: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache > > > > Store ISA level in the portion of the unused upper 32 bits of the hwcaps > > field in cache and the unused pad field in aux cache. ISA level is stored > > and checked only for shared objects in glibc-hwcaps subdirectories. The > > shared objects in the default directories aren't checked since there are > > no fallbacks for these shared objects. > > Some comments below, most minor changes and code organization. The main > change I would like is to improve test coverage, as for the first patch. > > > --- > > elf/cache.c | 34 +++++--- > > elf/dl-cache.c | 4 + > > elf/ldconfig.c | 18 +++-- > > elf/readelflib.c | 81 ++++++++++++++++++- > > elf/readlib.c | 7 +- > > elf/tst-glibc-hwcaps-2-cache.c | 45 +++++++++++ > > .../etc/ld.so.conf | 2 + > > .../postclean.req | 0 > > elf/tst-glibc-hwcaps-2-cache.script | 6 ++ > > sysdeps/generic/dl-cache.h | 15 +++- > > sysdeps/generic/dl-isa-level.h | 26 ++++++ > > sysdeps/generic/ldconfig.h | 22 +++-- > > sysdeps/generic/read-prop.h | 35 ++++++++ > > sysdeps/unix/sysv/linux/arm/readelflib.c | 22 ++--- > > sysdeps/unix/sysv/linux/ia64/readelflib.c | 22 ++--- > > sysdeps/unix/sysv/linux/mips/readelflib.c | 22 ++--- > > sysdeps/unix/sysv/linux/powerpc/readelflib.c | 22 ++--- > > sysdeps/unix/sysv/linux/riscv/readelflib.c | 22 ++--- > > sysdeps/unix/sysv/linux/s390/readelflib.c | 22 ++--- > > sysdeps/unix/sysv/linux/sparc/readelflib.c | 22 ++--- > > sysdeps/unix/sysv/linux/x86/read-prop.h | 61 ++++++++++++++ > > sysdeps/unix/sysv/linux/x86/readelflib.c | 23 +++--- > > sysdeps/unix/sysv/linux/x86_64/Makefile | 31 +++++++ > > .../sysv/linux/x86_64/tst-glibc-hwcaps-2.c | 61 ++++++++++++++ > > .../linux/x86_64/x86-64-isa-level-VALUE.c | 4 + > > sysdeps/x86/dl-isa-level.h | 32 ++++++++ > > 26 files changed, 550 insertions(+), 111 deletions(-) > > create mode 100644 elf/tst-glibc-hwcaps-2-cache.c > > create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > > create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/postclean.req > > create mode 100644 elf/tst-glibc-hwcaps-2-cache.script > > create mode 100644 sysdeps/generic/dl-isa-level.h > > create mode 100644 sysdeps/generic/read-prop.h > > create mode 100644 sysdeps/unix/sysv/linux/x86/read-prop.h > > create mode 100644 sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > > create mode 100644 sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > > create mode 100644 sysdeps/x86/dl-isa-level.h > > > > diff --git a/elf/cache.c b/elf/cache.c > > index b03c5319f8..91a72381ae 100644 > > --- a/elf/cache.c > > +++ b/elf/cache.c > > @@ -145,6 +145,7 @@ struct cache_entry > > struct stringtable_entry *path; /* Path to find library. */ > > int flags; /* Flags to indicate kind of library. */ > > unsigned int osversion; /* Required OS version. */ > > + unsigned int isa_level; /* Required ISAL level. */ > > Maybe a typo here 'ISAL'? Fixed. > > uint64_t hwcap; /* Important hardware capabilities. */ > > int bits_hwcap; /* Number of bits set in hwcap. */ > > > > @@ -660,8 +661,14 @@ save_cache (const char *cache_name) > > if (entry->hwcaps == NULL) > > file_entries_new->libs[idx_new].hwcap = entry->hwcap; > > else > > - file_entries_new->libs[idx_new].hwcap > > - = DL_CACHE_HWCAP_EXTENSION | entry->hwcaps->section_index; > > + { > > + if (entry->isa_level > (1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT)) > > Maybe wrap this on a macro at dl-cache.h? It already does it for > DL_CACHE_HWCAP_ISA_LEVEL_MASK. > > + abort (); > > Maybe use 'error (...)' to print a proper error message? I changed it to + if (entry->isa_level > DL_CACHE_HWCAP_ISA_LEVEL_MASK) + error (EXIT_FAILURE, 0, _("%s: ISA level is too high (%d > %d)"), + entry->path->string, entry->isa_level, + DL_CACHE_HWCAP_ISA_LEVEL_MASK); since DL_CACHE_HWCAP_ISA_LEVEL_MASK is the maximum ISA level. > > + file_entries_new->libs[idx_new].hwcap > > + = (DL_CACHE_HWCAP_EXTENSION > > + | (((uint64_t) entry->isa_level) << 32) > > + | entry->hwcaps->section_index); > > + } > > Would be better to wrap this on a inline function at dl-cache.h? > > static inline unsigned int dl_cache_hwcap_isa_level (struct file_entry_new *entry) > { > return DL_CACHE_HWCAP_EXTENSION > | ((uint64_t) entry->isa_level << 32) > | entry->hwcaps->sections_index; > } This function takes "struct cache_entry *entry" which is private to cache.c I added compute_hwcap_value to cache.c instead. > > file_entries_new->libs[idx_new].key > > = str_offset + entry->lib->offset; > > file_entries_new->libs[idx_new].value > > @@ -776,7 +783,8 @@ save_cache (const char *cache_name) > > /* Add one library to the cache. */ > > void > > add_to_cache (const char *path, const char *filename, const char *soname, > > - int flags, unsigned int osversion, uint64_t hwcap, > > + int flags, unsigned int osversion, > > + unsigned int isa_level, uint64_t hwcap, > > struct glibc_hwcaps_subdirectory *hwcaps) > > { > > struct cache_entry *new_entry = xmalloc (sizeof (*new_entry)); > > @@ -794,6 +802,7 @@ add_to_cache (const char *path, const char *filename, const char *soname, > > new_entry->path = path_interned; > > new_entry->flags = flags; > > new_entry->osversion = osversion; > > + new_entry->isa_level = isa_level; > > new_entry->hwcap = hwcap; > > new_entry->hwcaps = hwcaps; > > new_entry->bits_hwcap = 0; > > Ok. > > > @@ -850,6 +859,7 @@ struct aux_cache_entry > > struct aux_cache_entry_id id; > > int flags; > > unsigned int osversion; > > + unsigned int isa_level; > > int used; > > char *soname; > > struct aux_cache_entry *next; > > @@ -863,7 +873,7 @@ struct aux_cache_file_entry > > int32_t flags; /* This is 1 for an ELF library. */ > > uint32_t soname; /* String table indice. */ > > uint32_t osversion; /* Required OS version. */ > > - int32_t pad; > > + uint32_t isa_level; /* Required ISA level. */ > > }; > > > > I think it would be good to have a _Static_assert with the > expected aux_cache_entry size. What would that be? aux_cache_entry_id size is increased by 4 bytes with my patch. > > /* ldconfig maintains an auxiliary cache file that allows > > @@ -914,7 +924,8 @@ init_aux_cache (void) > > > > int > > search_aux_cache (struct stat64 *stat_buf, int *flags, > > - unsigned int *osversion, char **soname) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname) > > { > > struct aux_cache_entry_id id; > > id.ino = (uint64_t) stat_buf->st_ino; > > @@ -932,6 +943,7 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, > > { > > *flags = entry->flags; > > *osversion = entry->osversion; > > + *isa_level = entry->isa_level; > > if (entry->soname != NULL) > > *soname = xstrdup (entry->soname); > > else > > Ok. > > > @@ -945,7 +957,8 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, > > > > static void > > insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > > - unsigned int osversion, const char *soname, int used) > > + unsigned int osversion, unsigned int isa_level, > > + const char *soname, int used) > > { > > size_t hash = aux_cache_entry_id_hash (id) % aux_hash_size; > > struct aux_cache_entry *entry; > > @@ -961,6 +974,7 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > > entry->id = *id; > > entry->flags = flags; > > entry->osversion = osversion; > > + entry->isa_level = isa_level; > > entry->used = used; > > if (soname != NULL) > > entry->soname = memcpy ((char *) (entry + 1), soname, len); > > Ok. > > > @@ -972,14 +986,15 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > > > > void > > add_to_aux_cache (struct stat64 *stat_buf, int flags, > > - unsigned int osversion, const char *soname) > > + unsigned int osversion, unsigned int isa_level, > > + const char *soname) > > { > > struct aux_cache_entry_id id; > > id.ino = (uint64_t) stat_buf->st_ino; > > id.ctime = (uint64_t) stat_buf->st_ctime; > > id.size = (uint64_t) stat_buf->st_size; > > id.dev = (uint64_t) stat_buf->st_dev; > > - insert_to_aux_cache (&id, flags, osversion, soname, 1); > > + insert_to_aux_cache (&id, flags, osversion, isa_level, soname, 1); > > } > > > > /* Load auxiliary cache to search for unchanged entries. */ > > @@ -1025,6 +1040,7 @@ load_aux_cache (const char *aux_cache_name) > > insert_to_aux_cache (&aux_cache->libs[i].id, > > aux_cache->libs[i].flags, > > aux_cache->libs[i].osversion, > > + aux_cache->libs[i].isa_level, > > aux_cache->libs[i].soname == 0 > > ? NULL : aux_cache_data + aux_cache->libs[i].soname, > > 0); > > @@ -1093,7 +1109,7 @@ save_aux_cache (const char *aux_cache_name) > > str_offset += len; > > } > > file_entries->libs[idx].osversion = entry->osversion; > > - file_entries->libs[idx++].pad = 0; > > + file_entries->libs[idx++].isa_level = entry->isa_level; > > } > > > > /* Write out auxiliary cache file. */ > > Ok. > > > diff --git a/elf/dl-cache.c b/elf/dl-cache.c > > index 97093bb700..edeff7ccc1 100644 > > --- a/elf/dl-cache.c > > +++ b/elf/dl-cache.c > > @@ -25,6 +25,7 @@ > > #include <stdint.h> > > #include <_itoa.h> > > #include <dl-hwcaps.h> > > +#include <dl-isa-level.h> > > > > #ifndef _DL_PLATFORMS_COUNT > > # define _DL_PLATFORMS_COUNT 0 > > @@ -284,6 +285,9 @@ search_cache (const char *string_table, uint32_t string_table_size, > > > > #ifdef SHARED > > named_hwcap = dl_cache_hwcap_extension (libnew); > > + if (named_hwcap > > + && !dl_cache_hwcap_isa_level_compatible (libnew)) > > + continue; > > #endif > > > > /* The entries with named/extension hwcaps > > Ok, so it skips non compatible ISA shared libraries. > > > diff --git a/elf/ldconfig.c b/elf/ldconfig.c > > index 10927a8c7f..9dceac38aa 100644 > > --- a/elf/ldconfig.c > > +++ b/elf/ldconfig.c > > @@ -655,6 +655,7 @@ manual_link (char *library) > > struct stat64 stat_buf; > > int flag; > > unsigned int osversion; > > + unsigned int isa_level; > > > > /* Prepare arguments for create_links call. Split library name in > > directory and filename first. Since path is allocated, we've got > > @@ -721,7 +722,7 @@ manual_link (char *library) > > } > > > > if (process_file (real_library, library, libname, &flag, &osversion, > > - &soname, 0, &stat_buf)) > > + &isa_level, &soname, 0, &stat_buf)) > > { > > error (0, 0, _("No link created since soname could not be found for %s"), > > library); > > @@ -768,6 +769,7 @@ struct dlib_entry > > int flag; > > int is_link; > > unsigned int osversion; > > + unsigned int isa_level; > > struct dlib_entry *next; > > }; > > > > Ok. > > > @@ -980,17 +982,21 @@ search_dir (const struct dir_entry *entry) > > library already and it's not changed. */ > > char *soname; > > unsigned int osversion; > > - if (!search_aux_cache (&lstat_buf, &flag, &osversion, &soname)) > > + unsigned int isa_level; > > + if (!search_aux_cache (&lstat_buf, &flag, &osversion, &isa_level, > > + &soname)) > > { > > if (process_file (real_name, file_name, direntry->d_name, &flag, > > - &osversion, &soname, is_link, &lstat_buf)) > > + &osversion, &isa_level, &soname, is_link, > > + &lstat_buf)) > > { > > if (real_name != real_file_name) > > free (real_name); > > continue; > > } > > else if (opt_build_cache) > > - add_to_aux_cache (&lstat_buf, flag, osversion, soname); > > + add_to_aux_cache (&lstat_buf, flag, osversion, isa_level, > > + soname); > > } > > > > if (soname == NULL) > > Ok. > > > @@ -1096,6 +1102,7 @@ search_dir (const struct dir_entry *entry) > > dlib_ptr->name = xstrdup (direntry->d_name); > > dlib_ptr->is_link = is_link; > > dlib_ptr->osversion = osversion; > > + dlib_ptr->isa_level = isa_level; > > } > > /* Don't add this library, abort loop. */ > > /* Also free soname, since it's dynamically allocated. */ > > @@ -1112,6 +1119,7 @@ search_dir (const struct dir_entry *entry) > > dlib_ptr->flag = flag; > > dlib_ptr->is_link = is_link; > > dlib_ptr->osversion = osversion; > > + dlib_ptr->isa_level = isa_level; > > /* Add at head of list. */ > > dlib_ptr->next = dlibs; > > dlibs = dlib_ptr; > > @@ -1149,7 +1157,7 @@ search_dir (const struct dir_entry *entry) > > if (opt_build_cache) > > add_to_cache (entry->path, filename, dlib_ptr->soname, > > dlib_ptr->flag, dlib_ptr->osversion, > > - hwcap, entry->hwcaps); > > + dlib_ptr->isa_level, hwcap, entry->hwcaps); > > } > > > > /* Free all resources. */ > > Ok. > > > diff --git a/elf/readelflib.c b/elf/readelflib.c > > index 5905f6d344..abb1ac61fa 100644 > > --- a/elf/readelflib.c > > +++ b/elf/readelflib.c > > @@ -17,6 +17,8 @@ > > License along with the GNU C Library; if not, see > > <https://www.gnu.org/licenses/>. */ > > > > +#include <read-prop.h> > > + > > /* This code is a heavily simplified version of the readelf program > > that's part of the current binutils development version. For architectures > > which need to handle both 32bit and 64bit ELF libraries, this file is > > @@ -40,8 +42,8 @@ do \ > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, void *file_contents, > > - size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > int i; > > unsigned int j; > > @@ -86,6 +88,9 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > libc5/libc6. */ > > *flag = FLAG_ELF; > > > > + /* The default ISA level is 0. */ > > + *isa_level = 0; > > + > > dynamic_addr = 0; > > dynamic_size = 0; > > program_interpreter = NULL; > > @@ -164,6 +169,78 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > } > > break; > > > > + case PT_GNU_PROPERTY: > > + /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes > > + in 32-bit objects and to 8 bytes in 64-bit objects. Skip > > + notes with incorrect alignment. */ > > + if (segment->p_align == (__ELF_NATIVE_CLASS / 8)) > > + { > > + const ElfW(Nhdr) *note = (const void *) (file_contents > > + + segment->p_offset); > > + const ElfW(Addr) size = segment->p_filesz; > > + const ElfW(Addr) align = segment->p_align; > > + > > + const ElfW(Addr) start = (ElfW(Addr)) (uintptr_t) note; > > + unsigned int last_type = 0; > > + > > + while ((ElfW(Addr)) (uintptr_t) (note + 1) - start < size) > > + { > > + /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ > > + if (note->n_namesz == 4 > > + && note->n_type == NT_GNU_PROPERTY_TYPE_0 > > + && memcmp (note + 1, "GNU", 4) == 0) > > + { > > + /* Check for invalid property. */ > > + if (note->n_descsz < 8 > > + || (note->n_descsz % sizeof (ElfW(Addr))) != 0) > > + goto done; > > + > > + /* Start and end of property array. */ > > + unsigned char *ptr = (unsigned char *) (note + 1) + 4; > > + unsigned char *ptr_end = ptr + note->n_descsz; > > + > > + do > > + { > > + unsigned int type = *(unsigned int *) ptr; > > + unsigned int datasz = *(unsigned int *) (ptr + 4); > > + > > + /* Property type must be in ascending order. */ > > + if (type < last_type) > > + goto done; > > + > > + ptr += 8; > > + if ((ptr + datasz) > ptr_end) > > + goto done; > > + > > + last_type = type; > > + > > + /* Target specific property processing. > > + Return value: > > + false: Continue processing the properties. > > + true : Stop processing the properties. > > + */ > > + if (read_gnu_property (isa_level, type, > > + datasz, ptr)) > > + goto done; > > + > > + /* Check the next property item. */ > > + ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); > > + } > > + while ((ptr_end - ptr) >= 8); > > + > > + /* Only handle one NT_GNU_PROPERTY_TYPE_0. */ > > + goto done; > > + } > > + > > + note = ((const void *) note > > + + ELF_NOTE_NEXT_OFFSET (note->n_namesz, > > + note->n_descsz, > > + align)); > > + } > > + } > > +done: > > + break; > > + > > default: > > break; > > } > > Ok, although this is quite similar to the _dl_process_pt_gnu_property > (maybe a future work to try refactor both to use a common code). That is a future possibility. > > diff --git a/elf/readlib.c b/elf/readlib.c > > index 994a4426a1..def0eafbeb 100644 > > --- a/elf/readlib.c > > +++ b/elf/readlib.c > > @@ -75,7 +75,8 @@ is_gdb_python_file (const char *name) > > int > > process_file (const char *real_file_name, const char *file_name, > > const char *lib, int *flag, unsigned int *osversion, > > - char **soname, int is_link, struct stat64 *stat_buf) > > + unsigned int *isa_level, char **soname, int is_link, > > + struct stat64 *stat_buf) > > { > > FILE *file; > > struct stat64 statbuf; > > @@ -173,8 +174,8 @@ process_file (const char *real_file_name, const char *file_name, > > /* Libraries have to be shared object files. */ > > else if (elf_header->e_type != ET_DYN) > > ret = 1; > > - else if (process_elf_file (file_name, lib, flag, osversion, soname, > > - file_contents, statbuf.st_size)) > > + else if (process_elf_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, statbuf.st_size)) > > ret = 1; > > > > done: > > Ok. > > > diff --git a/elf/tst-glibc-hwcaps-2-cache.c b/elf/tst-glibc-hwcaps-2-cache.c > > new file mode 100644 > > index 0000000000..2c30a2c911 > > --- /dev/null > > +++ b/elf/tst-glibc-hwcaps-2-cache.c > > @@ -0,0 +1,45 @@ > > +/* Wrapper to invoke tst-glibc-hwcaps-2 in a container to test ldconfig. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +/* This program is just a wrapper that runs ldconfig followed by > > + tst-glibc-hwcaps-2. The actual test is provided via an > > + implementation in a sysdeps subdirectory. */ > > + > > +#include <stdio.h> > > +#include <stdlib.h> > > +#include <support/support.h> > > +#include <unistd.h> > > + > > +int > > +main (int argc, char **argv) > > +{ > > + /* Run ldconfig to populate the cache. */ > > + { > > + char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir); > > + if (system (command) != 0) > > + return 1; > > + free (command); > > + } > > Maybe use support_capture_subprogram here? > > char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir); > struct support_capture_subprocess result = > support_capture_subprogram (command, &((char *) { NULL })); > support_capture_subprocess_check (&result, "ldconfig", 0, sc_allow_none); > free (command); Fixed. > > + > > + /* Reuse tst-glibc-hwcaps. Since this code is running in a > > + container, we can launch it directly. */ > > + char *path = xasprintf ("%s/elf/tst-glibc-hwcaps-2", support_objdir_root); > > + execv (path, argv); > > + printf ("error: execv of %s failed: %m\n", path); > > + return 1; > > +} > > Ok. > > > diff --git a/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > > new file mode 100644 > > index 0000000000..e1e74dbda2 > > --- /dev/null > > +++ b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > > @@ -0,0 +1,2 @@ > > +# This file was created to suppress a warning from ldconfig: > > +# /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory > > diff --git a/elf/tst-glibc-hwcaps-2-cache.root/postclean.req b/elf/tst-glibc-hwcaps-2-cache.root/postclean.req > > new file mode 100644 > > index 0000000000..e69de29bb2 > > Ok. > > > diff --git a/elf/tst-glibc-hwcaps-2-cache.script b/elf/tst-glibc-hwcaps-2-cache.script > > new file mode 100644 > > index 0000000000..29ccfc3b49 > > --- /dev/null > > +++ b/elf/tst-glibc-hwcaps-2-cache.script > > @@ -0,0 +1,6 @@ > > +# test-container does not support scripts in sysdeps directories, so > > +# collect everything in one file. > > + > > +mkdirp 0770 $L/glibc-hwcaps/x86-64-v2 > > Ok. > > > +cp $B/elf/libx86-64-isa-level-1.so $L/libx86-64-isa-level.so > > +cp $B/elf/libx86-64-isa-level-4.so $L/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so > > diff --git a/sysdeps/generic/dl-cache.h b/sysdeps/generic/dl-cache.h > > index 72a66b45ee..1138228e5c 100644 > > --- a/sysdeps/generic/dl-cache.h > > +++ b/sysdeps/generic/dl-cache.h > > @@ -106,14 +106,23 @@ struct file_entry_new > > entries. */ > > #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) > > > > +/* The number of the ISA level bits in the upper 32 bits of the hwcap > > + field. */ > > +#define DL_CACHE_HWCAP_ISA_LEVEL_COUNT 10 > > + > > +/* The mask of the ISA level bits in the hwcap field. */ > > +#define DL_CACHE_HWCAP_ISA_LEVEL_MASK \ > > + ((1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT) -1) > > + > > Space after '-'. Fixed. > > /* Return true if the ENTRY->hwcap value indicates that > > DL_CACHE_HWCAP_EXTENSION is used. */ > > static inline bool > > dl_cache_hwcap_extension (struct file_entry_new *entry) > > { > > - /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this > > - is a different kind of extension. */ > > - return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); > > + /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits, except for the > > + ISA level bits, as well, this is a different kind of extension. */ > > + return (((entry->hwcap >> 32) & ~DL_CACHE_HWCAP_ISA_LEVEL_MASK) > > + == (DL_CACHE_HWCAP_EXTENSION >> 32)); > > } > > > > /* See flags member of struct cache_file_new below. */ > > The comment sounds confusing. I changed it to /* This is an hwcap extension if only the DL_CACHE_HWCAP_EXTENSION bit is set, ignoring the lower 32 bits as well as the ISA level bits in the upper 32 bits. */ > > diff --git a/sysdeps/generic/dl-isa-level.h b/sysdeps/generic/dl-isa-level.h > > new file mode 100644 > > index 0000000000..77735775b9 > > --- /dev/null > > +++ b/sysdeps/generic/dl-isa-level.h > > @@ -0,0 +1,26 @@ > > +/* Support for reading ISA level in /etc/ld.so.cache files written by > > + Linux ldconfig. Generic version. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > s/2020/2021 Fixed. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +/* Return true if the ISA level in ENTRY is compatible with CPU. */ > > + > > My understanding is for function comment there is no need to add an > extra newline. Fixed. > > +static inline bool > > +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) > > +{ > > + return true; > > +} > > Ok. > > > diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h > > index 1ad1528890..a5ed350237 100644 > > --- a/sysdeps/generic/ldconfig.h > > +++ b/sysdeps/generic/ldconfig.h > > @@ -70,8 +70,9 @@ const char *glibc_hwcaps_subdirectory_name > > (const struct glibc_hwcaps_subdirectory *); > > > > extern void add_to_cache (const char *path, const char *filename, > > - const char *soname, > > - int flags, unsigned int osversion, uint64_t hwcap, > > + const char *soname, int flags, > > + unsigned int osversion, unsigned int isa_level, > > + uint64_t hwcap, > > struct glibc_hwcaps_subdirectory *); > > > > extern void init_aux_cache (void); > > @@ -79,23 +80,28 @@ extern void init_aux_cache (void); > > extern void load_aux_cache (const char *aux_cache_name); > > > > extern int search_aux_cache (struct stat64 *stat_buf, int *flags, > > - unsigned int *osversion, char **soname); > > + unsigned int *osversion, > > + unsigned int *isa_level, char **soname); > > > > extern void add_to_aux_cache (struct stat64 *stat_buf, int flags, > > - unsigned int osversion, const char *soname); > > + unsigned int osversion, > > + unsigned int isa_level, const char *soname); > > > > extern void save_aux_cache (const char *aux_cache_name); > > > > /* Declared in readlib.c. */ > > extern int process_file (const char *real_file_name, const char *file_name, > > - const char *lib, int *flag, unsigned int *osversion, > > - char **soname, int is_link, struct stat64 *stat_buf); > > + const char *lib, int *flag, > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, int is_link, > > + struct stat64 *stat_buf); > > > > extern char *implicit_soname (const char *lib, int flag); > > > > /* Declared in readelflib.c. */ > > -extern int process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +extern int process_elf_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* Declared in chroot_canon.c. */ > > > Ok. > > > diff --git a/sysdeps/generic/read-prop.h b/sysdeps/generic/read-prop.h > > new file mode 100644 > > index 0000000000..091bd2f6c1 > > --- /dev/null > > +++ b/sysdeps/generic/read-prop.h > > Maybe elf-read-prop.h to add more information on exactly this > file intends to? Renamed to elf-read-prop.h. > > @@ -0,0 +1,35 @@ > > +/* Support for GNU properties in ldconfig. Generic version. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#ifndef _READ_PROP_H > > +#define _READ_PROP_H > > + > > +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. > > + Return value: > > + false: Continue processing the properties. > > + true : Stop processing the properties. > > + */ > > + > > +static inline bool __attribute__ ((always_inline)) > > +read_gnu_property (unsigned int *isal_level, uint32_t type, uint32_t > > + datasz, void *data) > > +{ > > + return true; > > +} > > + > > +#endif > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/arm/readelflib.c b/sysdeps/unix/sysv/linux/arm/readelflib.c > > index 9d03fa6405..7f444add14 100644 > > --- a/sysdeps/unix/sysv/linux/arm/readelflib.c > > +++ b/sysdeps/unix/sysv/linux/arm/readelflib.c > > @@ -18,18 +18,20 @@ > > <https://www.gnu.org/licenses/>. */ > > > > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf32_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf64_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, void *file_contents, > > - size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > > int ret; > > @@ -38,8 +40,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > { > > Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; > > > > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > > > if (!ret && EF_ARM_EABI_VERSION (elf32_header->e_flags) == EF_ARM_EABI_VER5) > > { > > @@ -57,8 +59,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > } > > else > > { > > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > /* AArch64 libraries are always libc.so.6+. */ > > if (!ret) > > *flag = FLAG_AARCH64_LIB64|FLAG_ELF_LIBC6; > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/ia64/readelflib.c b/sysdeps/unix/sysv/linux/ia64/readelflib.c > > index d24a934351..19d37b438e 100644 > > --- a/sysdeps/unix/sysv/linux/ia64/readelflib.c > > +++ b/sysdeps/unix/sysv/linux/ia64/readelflib.c > > @@ -16,29 +16,31 @@ > > <https://www.gnu.org/licenses/>. */ > > > > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf32_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf64_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > - void *file_contents, size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > > int ret; > > > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > > - return process_elf32_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > else > > { > > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > /* Intel 64bit libraries are always libc.so.6+. */ > > if (!ret) > > *flag = FLAG_IA64_LIB64|FLAG_ELF_LIBC6; > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/mips/readelflib.c b/sysdeps/unix/sysv/linux/mips/readelflib.c > > index 8363b8afcc..a3399961c9 100644 > > --- a/sysdeps/unix/sysv/linux/mips/readelflib.c > > +++ b/sysdeps/unix/sysv/linux/mips/readelflib.c > > @@ -20,18 +20,20 @@ > > <https://www.gnu.org/licenses/>. */ > > > > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf32_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf64_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, void *file_contents, > > - size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > union > > { > > @@ -45,8 +47,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > elf_header.eh = file_contents; > > if (elf_header.eh->e_ident [EI_CLASS] == ELFCLASS32) > > { > > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > if (!ret) > > { > > Elf32_Word flags = elf_header.eh32->e_flags; > > @@ -62,8 +64,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > } > > else > > { > > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > /* n64 libraries are always libc.so.6+. */ > > if (!ret) > > { > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/powerpc/readelflib.c b/sysdeps/unix/sysv/linux/powerpc/readelflib.c > > index eb20680418..ce01658fb3 100644 > > --- a/sysdeps/unix/sysv/linux/powerpc/readelflib.c > > +++ b/sysdeps/unix/sysv/linux/powerpc/readelflib.c > > @@ -17,29 +17,31 @@ > > <https://www.gnu.org/licenses/>. */ > > > > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf32_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf64_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, void *file_contents, > > - size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > > int ret; > > > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > > - return process_elf32_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > else > > { > > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > /* PowerPC 64bit libraries are always libc.so.6+. */ > > if (!ret) > > *flag = FLAG_POWERPC_LIB64|FLAG_ELF_LIBC6; > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/riscv/readelflib.c b/sysdeps/unix/sysv/linux/riscv/readelflib.c > > index 1e70735cc0..98ae3c0b2b 100644 > > --- a/sysdeps/unix/sysv/linux/riscv/readelflib.c > > +++ b/sysdeps/unix/sysv/linux/riscv/readelflib.c > > @@ -17,11 +17,13 @@ > > <https://www.gnu.org/licenses/>. */ > > > > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf32_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf64_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* The ELF flags supported by our current glibc port: > > @@ -38,8 +40,8 @@ int process_elf64_file (const char *file_name, const char *lib, int *flag, > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, void *file_contents, > > - size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > > Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; > > @@ -52,14 +54,14 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > > { > > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > flags = elf32_header->e_flags; > > } > > else > > { > > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > flags = elf64_header->e_flags; > > } > > > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/s390/readelflib.c b/sysdeps/unix/sysv/linux/s390/readelflib.c > > index 1718efc9f9..6d19e351ba 100644 > > --- a/sysdeps/unix/sysv/linux/s390/readelflib.c > > +++ b/sysdeps/unix/sysv/linux/s390/readelflib.c > > @@ -16,29 +16,31 @@ > > <https://www.gnu.org/licenses/>. */ > > > > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf32_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf64_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, void *file_contents, > > - size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > > int ret; > > > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > > - return process_elf32_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > else > > { > > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > /* S/390 64bit libraries are always libc.so.6+. */ > > if (!ret) > > *flag = FLAG_S390_LIB64|FLAG_ELF_LIBC6; > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/sparc/readelflib.c b/sysdeps/unix/sysv/linux/sparc/readelflib.c > > index e027a11a37..3768003925 100644 > > --- a/sysdeps/unix/sysv/linux/sparc/readelflib.c > > +++ b/sysdeps/unix/sysv/linux/sparc/readelflib.c > > @@ -18,29 +18,31 @@ > > <https://www.gnu.org/licenses/>. */ > > > > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf32_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf64_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, void *file_contents, > > - size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > > int ret; > > > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > > - return process_elf32_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > else > > { > > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > /* Sparc 64bit libraries are always libc.so.6+. */ > > if (!ret) > > *flag = FLAG_SPARC_LIB64|FLAG_ELF_LIBC6; > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/x86/read-prop.h b/sysdeps/unix/sysv/linux/x86/read-prop.h > > new file mode 100644 > > index 0000000000..ac37fbc449 > > --- /dev/null > > +++ b/sysdeps/unix/sysv/linux/x86/read-prop.h > > @@ -0,0 +1,61 @@ > > +/* Support for GNU properties in ldconfig. x86 version. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > s/2020/2021. Fixed, > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#ifndef _READ_PROP_H > > +#define _READ_PROP_H > > + > > +#include <dl-cache.h> > > + > > +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. > > + Return value: > > + false: Continue processing the properties. > > + true : Stop processing the properties. > > + */ > > + > > +static inline bool __attribute__ ((always_inline)) > > +read_gnu_property (unsigned int *isal_level, uint32_t type, > > + uint32_t datasz, void *data) > > +{ > > + /* Property type must be in ascending order. */ > > + if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) > > + return true; > > + > > + if (type == GNU_PROPERTY_X86_ISA_1_NEEDED) > > + { > > + if (datasz == 4) > > + { > > + /* The size of GNU_PROPERTY_X86_ISA_1_NEEDED must be 4 bytes. > > + There is no point to continue if this type is ill-formed. */ > > + unsigned int isa_1_needed = *(unsigned int *) data; > > + _Static_assert (((sizeof (isa_1_needed) * 8) > > + <= (1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT)), > > + "DL_CACHE_HWCAP_ISA_LEVEL_COUNT is too small"); > > + if (isa_1_needed) > > Implicit check. Fixed. > > + { > > + unsigned int level; > > + asm ("bsr %1, %0" : "=r" (level) : "g" (isa_1_needed)); > > + *isal_level = level; > > + } > > + } > > + return true; > > + } > > + > > + return false; > > +} > > + > > +#endif > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/x86/readelflib.c b/sysdeps/unix/sysv/linux/x86/readelflib.c > > index 3e83419f5b..716629bf10 100644 > > --- a/sysdeps/unix/sysv/linux/x86/readelflib.c > > +++ b/sysdeps/unix/sysv/linux/x86/readelflib.c > > @@ -17,19 +17,20 @@ > > License along with the GNU C Library; if not, see > > <https://www.gnu.org/licenses/>. */ > > > > - > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf32_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, > > +int process_elf64_file (const char *file_name, const char *lib, > > + int *flag, unsigned int *osversion, > > + unsigned int *isa_level, char **soname, > > void *file_contents, size_t file_length); > > > > /* Returns 0 if everything is ok, != 0 in case of error. */ > > int > > process_elf_file (const char *file_name, const char *lib, int *flag, > > - unsigned int *osversion, char **soname, void *file_contents, > > - size_t file_length) > > + unsigned int *osversion, unsigned int *isa_level, > > + char **soname, void *file_contents, size_t file_length) > > { > > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > > int ret, file_flag = 0; > > @@ -68,11 +69,11 @@ failed: > > } > > > > if (elf_header->e_ident[EI_CLASS] == ELFCLASS32) > > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > else > > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > > - file_contents, file_length); > > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > > + soname, file_contents, file_length); > > > > if (!ret && file_flag) > > *flag = file_flag; > > Ok. > > > diff --git a/sysdeps/unix/sysv/linux/x86_64/Makefile b/sysdeps/unix/sysv/linux/x86_64/Makefile > > index 9b82155393..9a7b3cd8e5 100644 > > --- a/sysdeps/unix/sysv/linux/x86_64/Makefile > > +++ b/sysdeps/unix/sysv/linux/x86_64/Makefile > > @@ -13,3 +13,34 @@ endif > > ifeq ($(subdir),misc) > > gen-as-const-headers += sigaltstack-offsets.sym > > endif > > + > > +ifeq ($(subdir),elf) > > +ifeq (yesyes,$(enable-x86-isa-level)$(config-cflags-skylake-avx512)) > > As for first patch, I think we should extend the test coverage to check not > only for config-cflags-skylake-avx512. It means to build at least one > shared object with different ISA levels and run the ldconfig and check the > output against the system supported ISA level. I removed $(config-cflags-skylake-avx512) and added more tests. > > +tests += tst-glibc-hwcaps-2 > > +ifeq (no,$(build-hardcoded-path-in-tests)) > > +# This is an ld.so.cache test, and RPATH/RUNPATH in the executable > > +# interferes with its test objectives. > > +tests-container += tst-glibc-hwcaps-2-cache > > +endif > > +modules-names += libx86-64-isa-level-1 libx86-64-isa-level-4 > > + > > +$(objpfx)tst-glibc-hwcaps-2: $(objpfx)libx86-64-isa-level.so > > + > > +$(objpfx)tst-glibc-hwcaps-2.out: \ > > + $(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so > > +$(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so: \ > > + $(objpfx)libx86-64-isa-level-4.so > > + cp $< $@ > > + > > +CFLAGS-libx86-64-isa-level-1.os += -march=x86-64 > > +CFLAGS-libx86-64-isa-level-4.os += -march=skylake-avx512 > > + > > +# The test modules are parameterized by preprocessor macros. > > +LDFLAGS-libx86-64-isa-level-1.so += -Wl,-soname,libx86-64-isa-level.so > > +LDFLAGS-libx86-64-isa-level-4.so += -Wl,-soname,libx86-64-isa-level.so > > +$(objpfx)libx86-64-isa-level%.os: $(..)/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > > + $(compile-command.c) -DVALUE=$(lastword $(subst -, ,$*)) > > +$(objpfx)libx86-64-isa-level.so: $(objpfx)libx86-64-isa-level-1.so > > + cp $< $@ > > +endif > > +endif # $(subdir) == elf > > diff --git a/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > > new file mode 100644 > > index 0000000000..8d6b17d658 > > --- /dev/null > > +++ b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > > @@ -0,0 +1,61 @@ > > +/* Check ISA level on shared object in glibc-hwcaps subdirectories. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <stdlib.h> > > +#include <stdio.h> > > +#include <elf.h> > > +#include <get-isa-level.h> > > +#include <support/check.h> > > +#include <support/test-driver.h> > > + > > +extern int dso_isa_level (void); > > + > > +static int > > +do_test (void) > > +{ > > + const struct cpu_features *cpu_features > > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > > + unsigned int isa_level = get_isa_level (cpu_features); > > + int level = dso_isa_level (); > > + int ret; > > + switch (level) > > + { > > + case 1: > > + /* The default libx86-64-isa-level.so should be used. */ > > + printf ("The default shared library is used.\n"); > > + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) > > + ret = EXIT_FAILURE; > > + else > > + ret = EXIT_SUCCESS; > > + break; > > + case 4: > > + /* libx86-64-isa-level.so marked as x86-64 ISA level 4 needed in > > + x86-64-v2 should be ignored on lesser CPU. */ > > + printf ("x86-64 ISA level 4 shared library in x86-64-v2 is used.\n"); > > + if ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) != 0) > > + ret = EXIT_SUCCESS; > > + else > > + ret = EXIT_FAILURE; > > + break; > > + default: > > + abort (); > > + } > > + return ret; > > +} > > + > > +#include <support/test-driver.c> > > diff --git a/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > > new file mode 100644 > > index 0000000000..2813d627cc > > --- /dev/null > > +++ b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > > @@ -0,0 +1,4 @@ > > +#define INCLUDE_X86_ISA_LEVEL > > +#define MARKER dso_isa_level > > +#include <isa-level.c> > > +#include <markermodMARKER-VALUE.c> > > diff --git a/sysdeps/x86/dl-isa-level.h b/sysdeps/x86/dl-isa-level.h > > new file mode 100644 > > index 0000000000..c6ed8fb9d2 > > --- /dev/null > > +++ b/sysdeps/x86/dl-isa-level.h > > @@ -0,0 +1,32 @@ > > +/* Support for reading ISA level in /etc/ld.so.cache files written by > > + Linux ldconfig. x86 version. > > + Copyright (C) 2020 Free Software Foundation, Inc. > > + This file is part of the GNU C Library. > > + > > + The GNU C Library is free software; you can redistribute it and/or > > + modify it under the terms of the GNU Lesser General Public > > + License as published by the Free Software Foundation; either > > + version 2.1 of the License, or (at your option) any later version. > > + > > + The GNU C Library is distributed in the hope that it will be useful, > > + but WITHOUT ANY WARRANTY; without even the implied warranty of > > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > > + Lesser General Public License for more details. > > + > > + You should have received a copy of the GNU Lesser General Public > > + License along with the GNU C Library; if not, see > > + <https://www.gnu.org/licenses/>. */ > > + > > +#include <sys/platform/x86.h> > > + > > +/* Return true if the ISA level in ENTRY is compatible with CPU. */ > > + > > +static inline bool > > +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) > > +{ > > + const struct cpu_features *cpu_features = __get_cpu_features (); > > + unsigned int isa_level > > + = 1 << ((entry->hwcap >> 32) & DL_CACHE_HWCAP_ISA_LEVEL_MASK); > > + > > + return (isa_level & cpu_features->isa_1) == isa_level; > > +} > > Ok. Here is the updated patch. OK for master? Thanks. -- H.J. [-- Attachment #2: 0001-ldconfig-x86-Store-ISA-level-in-cache-and-aux-cache.patch --] [-- Type: text/x-patch, Size: 51478 bytes --] From 40bacb7470f0c8d8eabd4d3b24057566d45ca5dd Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.tools@gmail.com> Date: Sat, 5 Dec 2020 07:01:58 -0800 Subject: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache Store ISA level in the portion of the unused upper 32 bits of the hwcaps field in cache and the unused pad field in aux cache. ISA level is stored and checked only for shared objects in glibc-hwcaps subdirectories. The shared objects in the default directories aren't checked since there are no fallbacks for these shared objects. Tested on x86-64-v2, x86-64-v3 and x86-64-v4 machines with --disable-hardcoded-path-in-tests and --enable-hardcoded-path-in-tests. --- elf/cache.c | 39 +++++++-- elf/dl-cache.c | 4 + elf/ldconfig.c | 18 ++-- elf/readelflib.c | 81 +++++++++++++++++- elf/readlib.c | 7 +- elf/tst-glibc-hwcaps-2-cache.c | 45 ++++++++++ .../etc/ld.so.conf | 2 + .../postclean.req | 0 elf/tst-glibc-hwcaps-2-cache.script | 8 ++ sysdeps/generic/dl-cache.h | 16 +++- sysdeps/generic/dl-isa-level.h | 25 ++++++ sysdeps/generic/elf-read-prop.h | 34 ++++++++ sysdeps/generic/ldconfig.h | 22 +++-- sysdeps/unix/sysv/linux/arm/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/ia64/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/mips/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/powerpc/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/riscv/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/s390/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/sparc/readelflib.c | 22 ++--- sysdeps/unix/sysv/linux/x86/elf-read-prop.h | 60 +++++++++++++ sysdeps/unix/sysv/linux/x86/readelflib.c | 23 ++--- sysdeps/unix/sysv/linux/x86_64/Makefile | 51 +++++++++++ .../sysv/linux/x86_64/tst-glibc-hwcaps-2.c | 84 +++++++++++++++++++ .../linux/x86_64/x86-64-isa-level-VALUE.c | 4 + sysdeps/x86/dl-isa-level.h | 31 +++++++ 26 files changed, 598 insertions(+), 110 deletions(-) create mode 100644 elf/tst-glibc-hwcaps-2-cache.c create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/postclean.req create mode 100644 elf/tst-glibc-hwcaps-2-cache.script create mode 100644 sysdeps/generic/dl-isa-level.h create mode 100644 sysdeps/generic/elf-read-prop.h create mode 100644 sysdeps/unix/sysv/linux/x86/elf-read-prop.h create mode 100644 sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c create mode 100644 sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c create mode 100644 sysdeps/x86/dl-isa-level.h diff --git a/elf/cache.c b/elf/cache.c index 11ce4ade8a..c01d302072 100644 --- a/elf/cache.c +++ b/elf/cache.c @@ -146,6 +146,7 @@ struct cache_entry struct stringtable_entry *path; /* Path to find library. */ int flags; /* Flags to indicate kind of library. */ unsigned int osversion; /* Required OS version. */ + unsigned int isa_level; /* Required ISA level. */ uint64_t hwcap; /* Important hardware capabilities. */ int bits_hwcap; /* Number of bits set in hwcap. */ @@ -549,6 +550,19 @@ write_extensions (int fd, uint32_t str_offset, free (ext); } +/* Compute the hwcap value from ENTRY. */ +static inline uint64_t +compute_hwcap_value (struct cache_entry *entry) +{ + if (entry->isa_level > DL_CACHE_HWCAP_ISA_LEVEL_MASK) + error (EXIT_FAILURE, 0, _("%s: ISA level is too high (%d > %d)"), + entry->path->string, entry->isa_level, + DL_CACHE_HWCAP_ISA_LEVEL_MASK); + return (DL_CACHE_HWCAP_EXTENSION + | (((uint64_t) entry->isa_level) << 32) + | entry->hwcaps->section_index); +} + /* Save the contents of the cache. */ void save_cache (const char *cache_name) @@ -662,7 +676,7 @@ save_cache (const char *cache_name) file_entries_new->libs[idx_new].hwcap = entry->hwcap; else file_entries_new->libs[idx_new].hwcap - = DL_CACHE_HWCAP_EXTENSION | entry->hwcaps->section_index; + = compute_hwcap_value (entry); file_entries_new->libs[idx_new].key = str_offset + entry->lib->offset; file_entries_new->libs[idx_new].value @@ -777,7 +791,8 @@ save_cache (const char *cache_name) /* Add one library to the cache. */ void add_to_cache (const char *path, const char *filename, const char *soname, - int flags, unsigned int osversion, uint64_t hwcap, + int flags, unsigned int osversion, + unsigned int isa_level, uint64_t hwcap, struct glibc_hwcaps_subdirectory *hwcaps) { struct cache_entry *new_entry = xmalloc (sizeof (*new_entry)); @@ -795,6 +810,7 @@ add_to_cache (const char *path, const char *filename, const char *soname, new_entry->path = path_interned; new_entry->flags = flags; new_entry->osversion = osversion; + new_entry->isa_level = isa_level; new_entry->hwcap = hwcap; new_entry->hwcaps = hwcaps; new_entry->bits_hwcap = 0; @@ -851,6 +867,7 @@ struct aux_cache_entry struct aux_cache_entry_id id; int flags; unsigned int osversion; + unsigned int isa_level; int used; char *soname; struct aux_cache_entry *next; @@ -864,7 +881,7 @@ struct aux_cache_file_entry int32_t flags; /* This is 1 for an ELF library. */ uint32_t soname; /* String table indice. */ uint32_t osversion; /* Required OS version. */ - int32_t pad; + uint32_t isa_level; /* Required ISA level. */ }; /* ldconfig maintains an auxiliary cache file that allows @@ -915,7 +932,8 @@ init_aux_cache (void) int search_aux_cache (struct stat64 *stat_buf, int *flags, - unsigned int *osversion, char **soname) + unsigned int *osversion, unsigned int *isa_level, + char **soname) { struct aux_cache_entry_id id; id.ino = (uint64_t) stat_buf->st_ino; @@ -933,6 +951,7 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, { *flags = entry->flags; *osversion = entry->osversion; + *isa_level = entry->isa_level; if (entry->soname != NULL) *soname = xstrdup (entry->soname); else @@ -946,7 +965,8 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, static void insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, - unsigned int osversion, const char *soname, int used) + unsigned int osversion, unsigned int isa_level, + const char *soname, int used) { size_t hash = aux_cache_entry_id_hash (id) % aux_hash_size; struct aux_cache_entry *entry; @@ -962,6 +982,7 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, entry->id = *id; entry->flags = flags; entry->osversion = osversion; + entry->isa_level = isa_level; entry->used = used; if (soname != NULL) entry->soname = memcpy ((char *) (entry + 1), soname, len); @@ -973,14 +994,15 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, void add_to_aux_cache (struct stat64 *stat_buf, int flags, - unsigned int osversion, const char *soname) + unsigned int osversion, unsigned int isa_level, + const char *soname) { struct aux_cache_entry_id id; id.ino = (uint64_t) stat_buf->st_ino; id.ctime = (uint64_t) stat_buf->st_ctime; id.size = (uint64_t) stat_buf->st_size; id.dev = (uint64_t) stat_buf->st_dev; - insert_to_aux_cache (&id, flags, osversion, soname, 1); + insert_to_aux_cache (&id, flags, osversion, isa_level, soname, 1); } /* Load auxiliary cache to search for unchanged entries. */ @@ -1026,6 +1048,7 @@ load_aux_cache (const char *aux_cache_name) insert_to_aux_cache (&aux_cache->libs[i].id, aux_cache->libs[i].flags, aux_cache->libs[i].osversion, + aux_cache->libs[i].isa_level, aux_cache->libs[i].soname == 0 ? NULL : aux_cache_data + aux_cache->libs[i].soname, 0); @@ -1094,7 +1117,7 @@ save_aux_cache (const char *aux_cache_name) str_offset += len; } file_entries->libs[idx].osversion = entry->osversion; - file_entries->libs[idx++].pad = 0; + file_entries->libs[idx++].isa_level = entry->isa_level; } /* Write out auxiliary cache file. */ diff --git a/elf/dl-cache.c b/elf/dl-cache.c index 935e3a60b4..32f3bef5ea 100644 --- a/elf/dl-cache.c +++ b/elf/dl-cache.c @@ -25,6 +25,7 @@ #include <stdint.h> #include <_itoa.h> #include <dl-hwcaps.h> +#include <dl-isa-level.h> #ifndef _DL_PLATFORMS_COUNT # define _DL_PLATFORMS_COUNT 0 @@ -284,6 +285,9 @@ search_cache (const char *string_table, uint32_t string_table_size, #ifdef SHARED named_hwcap = dl_cache_hwcap_extension (libnew); + if (named_hwcap + && !dl_cache_hwcap_isa_level_compatible (libnew)) + continue; #endif /* The entries with named/extension hwcaps diff --git a/elf/ldconfig.c b/elf/ldconfig.c index bbcf8f5c5c..28ed637a29 100644 --- a/elf/ldconfig.c +++ b/elf/ldconfig.c @@ -655,6 +655,7 @@ manual_link (char *library) struct stat64 stat_buf; int flag; unsigned int osversion; + unsigned int isa_level; /* Prepare arguments for create_links call. Split library name in directory and filename first. Since path is allocated, we've got @@ -721,7 +722,7 @@ manual_link (char *library) } if (process_file (real_library, library, libname, &flag, &osversion, - &soname, 0, &stat_buf)) + &isa_level, &soname, 0, &stat_buf)) { error (0, 0, _("No link created since soname could not be found for %s"), library); @@ -768,6 +769,7 @@ struct dlib_entry int flag; int is_link; unsigned int osversion; + unsigned int isa_level; struct dlib_entry *next; }; @@ -980,17 +982,21 @@ search_dir (const struct dir_entry *entry) library already and it's not changed. */ char *soname; unsigned int osversion; - if (!search_aux_cache (&lstat_buf, &flag, &osversion, &soname)) + unsigned int isa_level; + if (!search_aux_cache (&lstat_buf, &flag, &osversion, &isa_level, + &soname)) { if (process_file (real_name, file_name, direntry->d_name, &flag, - &osversion, &soname, is_link, &lstat_buf)) + &osversion, &isa_level, &soname, is_link, + &lstat_buf)) { if (real_name != real_file_name) free (real_name); continue; } else if (opt_build_cache) - add_to_aux_cache (&lstat_buf, flag, osversion, soname); + add_to_aux_cache (&lstat_buf, flag, osversion, isa_level, + soname); } if (soname == NULL) @@ -1096,6 +1102,7 @@ search_dir (const struct dir_entry *entry) dlib_ptr->name = xstrdup (direntry->d_name); dlib_ptr->is_link = is_link; dlib_ptr->osversion = osversion; + dlib_ptr->isa_level = isa_level; } /* Don't add this library, abort loop. */ /* Also free soname, since it's dynamically allocated. */ @@ -1112,6 +1119,7 @@ search_dir (const struct dir_entry *entry) dlib_ptr->flag = flag; dlib_ptr->is_link = is_link; dlib_ptr->osversion = osversion; + dlib_ptr->isa_level = isa_level; /* Add at head of list. */ dlib_ptr->next = dlibs; dlibs = dlib_ptr; @@ -1149,7 +1157,7 @@ search_dir (const struct dir_entry *entry) if (opt_build_cache) add_to_cache (entry->path, filename, dlib_ptr->soname, dlib_ptr->flag, dlib_ptr->osversion, - hwcap, entry->hwcaps); + dlib_ptr->isa_level, hwcap, entry->hwcaps); } /* Free all resources. */ diff --git a/elf/readelflib.c b/elf/readelflib.c index cdea79d729..c09425a574 100644 --- a/elf/readelflib.c +++ b/elf/readelflib.c @@ -17,6 +17,8 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ +#include <elf-read-prop.h> + /* This code is a heavily simplified version of the readelf program that's part of the current binutils development version. For architectures which need to handle both 32bit and 64bit ELF libraries, this file is @@ -40,8 +42,8 @@ do \ /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { int i; unsigned int j; @@ -86,6 +88,9 @@ process_elf_file (const char *file_name, const char *lib, int *flag, libc5/libc6. */ *flag = FLAG_ELF; + /* The default ISA level is 0. */ + *isa_level = 0; + dynamic_addr = 0; dynamic_size = 0; program_interpreter = NULL; @@ -164,6 +169,78 @@ process_elf_file (const char *file_name, const char *lib, int *flag, } break; + case PT_GNU_PROPERTY: + /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes + in 32-bit objects and to 8 bytes in 64-bit objects. Skip + notes with incorrect alignment. */ + if (segment->p_align == (__ELF_NATIVE_CLASS / 8)) + { + const ElfW(Nhdr) *note = (const void *) (file_contents + + segment->p_offset); + const ElfW(Addr) size = segment->p_filesz; + const ElfW(Addr) align = segment->p_align; + + const ElfW(Addr) start = (ElfW(Addr)) (uintptr_t) note; + unsigned int last_type = 0; + + while ((ElfW(Addr)) (uintptr_t) (note + 1) - start < size) + { + /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ + if (note->n_namesz == 4 + && note->n_type == NT_GNU_PROPERTY_TYPE_0 + && memcmp (note + 1, "GNU", 4) == 0) + { + /* Check for invalid property. */ + if (note->n_descsz < 8 + || (note->n_descsz % sizeof (ElfW(Addr))) != 0) + goto done; + + /* Start and end of property array. */ + unsigned char *ptr = (unsigned char *) (note + 1) + 4; + unsigned char *ptr_end = ptr + note->n_descsz; + + do + { + unsigned int type = *(unsigned int *) ptr; + unsigned int datasz = *(unsigned int *) (ptr + 4); + + /* Property type must be in ascending order. */ + if (type < last_type) + goto done; + + ptr += 8; + if ((ptr + datasz) > ptr_end) + goto done; + + last_type = type; + + /* Target specific property processing. + Return value: + false: Continue processing the properties. + true : Stop processing the properties. + */ + if (read_gnu_property (isa_level, type, + datasz, ptr)) + goto done; + + /* Check the next property item. */ + ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); + } + while ((ptr_end - ptr) >= 8); + + /* Only handle one NT_GNU_PROPERTY_TYPE_0. */ + goto done; + } + + note = ((const void *) note + + ELF_NOTE_NEXT_OFFSET (note->n_namesz, + note->n_descsz, + align)); + } + } +done: + break; + default: break; } diff --git a/elf/readlib.c b/elf/readlib.c index 3d52c9a980..7383c23249 100644 --- a/elf/readlib.c +++ b/elf/readlib.c @@ -75,7 +75,8 @@ is_gdb_python_file (const char *name) int process_file (const char *real_file_name, const char *file_name, const char *lib, int *flag, unsigned int *osversion, - char **soname, int is_link, struct stat64 *stat_buf) + unsigned int *isa_level, char **soname, int is_link, + struct stat64 *stat_buf) { FILE *file; struct stat64 statbuf; @@ -173,8 +174,8 @@ process_file (const char *real_file_name, const char *file_name, /* Libraries have to be shared object files. */ else if (elf_header->e_type != ET_DYN) ret = 1; - else if (process_elf_file (file_name, lib, flag, osversion, soname, - file_contents, statbuf.st_size)) + else if (process_elf_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, statbuf.st_size)) ret = 1; done: diff --git a/elf/tst-glibc-hwcaps-2-cache.c b/elf/tst-glibc-hwcaps-2-cache.c new file mode 100644 index 0000000000..97d9835ddb --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.c @@ -0,0 +1,45 @@ +/* Wrapper to invoke tst-glibc-hwcaps-2 in a container to test ldconfig. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +/* This program is just a wrapper that runs ldconfig followed by + tst-glibc-hwcaps-2. The actual test is provided via an + implementation in a sysdeps subdirectory. */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <support/support.h> +#include <support/capture_subprocess.h> + +int +main (int argc, char **argv) +{ + /* Run ldconfig to populate the cache. */ + char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir); + struct support_capture_subprocess result = + support_capture_subprogram (command, &((char *) { NULL })); + support_capture_subprocess_check (&result, "ldconfig", 0, sc_allow_none); + free (command); + + /* Reuse tst-glibc-hwcaps. Since this code is running in a + container, we can launch it directly. */ + char *path = xasprintf ("%s/elf/tst-glibc-hwcaps-2", support_objdir_root); + execv (path, argv); + printf ("error: execv of %s failed: %m\n", path); + return 1; +} diff --git a/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf new file mode 100644 index 0000000000..e1e74dbda2 --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf @@ -0,0 +1,2 @@ +# This file was created to suppress a warning from ldconfig: +# /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory diff --git a/elf/tst-glibc-hwcaps-2-cache.root/postclean.req b/elf/tst-glibc-hwcaps-2-cache.root/postclean.req new file mode 100644 index 0000000000..e69de29bb2 diff --git a/elf/tst-glibc-hwcaps-2-cache.script b/elf/tst-glibc-hwcaps-2-cache.script new file mode 100644 index 0000000000..8e4e9896ee --- /dev/null +++ b/elf/tst-glibc-hwcaps-2-cache.script @@ -0,0 +1,8 @@ +# test-container does not support scripts in sysdeps directories, so +# collect everything in one file. + +mkdirp 0770 $L/glibc-hwcaps/x86-64-v2 +mkdirp 0770 $L/glibc-hwcaps/x86-64-v3 +cp $B/elf/libx86-64-isa-level-1.so $L/libx86-64-isa-level.so +cp $B/elf/libx86-64-isa-level-3.so $L/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so +cp $B/elf/libx86-64-isa-level-4.so $L/glibc-hwcaps/x86-64-v3/libx86-64-isa-level.so diff --git a/sysdeps/generic/dl-cache.h b/sysdeps/generic/dl-cache.h index c7eca70d0c..964d50a486 100644 --- a/sysdeps/generic/dl-cache.h +++ b/sysdeps/generic/dl-cache.h @@ -106,14 +106,24 @@ struct file_entry_new entries. */ #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) +/* The number of the ISA level bits in the upper 32 bits of the hwcap + field. */ +#define DL_CACHE_HWCAP_ISA_LEVEL_COUNT 10 + +/* The mask of the ISA level bits in the hwcap field. */ +#define DL_CACHE_HWCAP_ISA_LEVEL_MASK \ + ((1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT) -1) + /* Return true if the ENTRY->hwcap value indicates that DL_CACHE_HWCAP_EXTENSION is used. */ static inline bool dl_cache_hwcap_extension (struct file_entry_new *entry) { - /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this - is a different kind of extension. */ - return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); + /* This is an hwcap extension if only the DL_CACHE_HWCAP_EXTENSION bit + is set, ignoring the lower 32 bits as well as the ISA level bits in + the upper 32 bits. */ + return (((entry->hwcap >> 32) & ~DL_CACHE_HWCAP_ISA_LEVEL_MASK) + == (DL_CACHE_HWCAP_EXTENSION >> 32)); } /* See flags member of struct cache_file_new below. */ diff --git a/sysdeps/generic/dl-isa-level.h b/sysdeps/generic/dl-isa-level.h new file mode 100644 index 0000000000..6fcd319c49 --- /dev/null +++ b/sysdeps/generic/dl-isa-level.h @@ -0,0 +1,25 @@ +/* Support for reading ISA level in /etc/ld.so.cache files written by + Linux ldconfig. Generic version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +/* Return true if the ISA level in ENTRY is compatible with CPU. */ +static inline bool +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) +{ + return true; +} diff --git a/sysdeps/generic/elf-read-prop.h b/sysdeps/generic/elf-read-prop.h new file mode 100644 index 0000000000..98c3d2acb5 --- /dev/null +++ b/sysdeps/generic/elf-read-prop.h @@ -0,0 +1,34 @@ +/* Support for GNU properties in ldconfig. Generic version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#ifndef _ELF_READ_PROP_H +#define _ELF_READ_PROP_H + +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. + Return value: + false: Continue processing the properties. + true : Stop processing the properties. + */ +static inline bool __attribute__ ((always_inline)) +read_gnu_property (unsigned int *isal_level, uint32_t type, uint32_t + datasz, void *data) +{ + return true; +} + +#endif diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h index eb070fd259..3ab757077d 100644 --- a/sysdeps/generic/ldconfig.h +++ b/sysdeps/generic/ldconfig.h @@ -70,8 +70,9 @@ const char *glibc_hwcaps_subdirectory_name (const struct glibc_hwcaps_subdirectory *); extern void add_to_cache (const char *path, const char *filename, - const char *soname, - int flags, unsigned int osversion, uint64_t hwcap, + const char *soname, int flags, + unsigned int osversion, unsigned int isa_level, + uint64_t hwcap, struct glibc_hwcaps_subdirectory *); extern void init_aux_cache (void); @@ -79,23 +80,28 @@ extern void init_aux_cache (void); extern void load_aux_cache (const char *aux_cache_name); extern int search_aux_cache (struct stat64 *stat_buf, int *flags, - unsigned int *osversion, char **soname); + unsigned int *osversion, + unsigned int *isa_level, char **soname); extern void add_to_aux_cache (struct stat64 *stat_buf, int flags, - unsigned int osversion, const char *soname); + unsigned int osversion, + unsigned int isa_level, const char *soname); extern void save_aux_cache (const char *aux_cache_name); /* Declared in readlib.c. */ extern int process_file (const char *real_file_name, const char *file_name, - const char *lib, int *flag, unsigned int *osversion, - char **soname, int is_link, struct stat64 *stat_buf); + const char *lib, int *flag, + unsigned int *osversion, unsigned int *isa_level, + char **soname, int is_link, + struct stat64 *stat_buf); extern char *implicit_soname (const char *lib, int flag); /* Declared in readelflib.c. */ -extern int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +extern int process_elf_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Declared in chroot_canon.c. */ diff --git a/sysdeps/unix/sysv/linux/arm/readelflib.c b/sysdeps/unix/sysv/linux/arm/readelflib.c index 2d3ecb1eef..ade2f49aeb 100644 --- a/sysdeps/unix/sysv/linux/arm/readelflib.c +++ b/sysdeps/unix/sysv/linux/arm/readelflib.c @@ -18,18 +18,20 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; @@ -38,8 +40,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, { Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); if (!ret && EF_ARM_EABI_VERSION (elf32_header->e_flags) == EF_ARM_EABI_VER5) { @@ -57,8 +59,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, } else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* AArch64 libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_AARCH64_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/ia64/readelflib.c b/sysdeps/unix/sysv/linux/ia64/readelflib.c index 7fb3ace359..2aa32666aa 100644 --- a/sysdeps/unix/sysv/linux/ia64/readelflib.c +++ b/sysdeps/unix/sysv/linux/ia64/readelflib.c @@ -16,29 +16,31 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, - void *file_contents, size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) - return process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + return process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* Intel 64bit libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_IA64_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/mips/readelflib.c b/sysdeps/unix/sysv/linux/mips/readelflib.c index e50bc3ce23..d0e809d893 100644 --- a/sysdeps/unix/sysv/linux/mips/readelflib.c +++ b/sysdeps/unix/sysv/linux/mips/readelflib.c @@ -20,18 +20,20 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { union { @@ -45,8 +47,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, elf_header.eh = file_contents; if (elf_header.eh->e_ident [EI_CLASS] == ELFCLASS32) { - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); if (!ret) { Elf32_Word flags = elf_header.eh32->e_flags; @@ -62,8 +64,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, } else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* n64 libraries are always libc.so.6+. */ if (!ret) { diff --git a/sysdeps/unix/sysv/linux/powerpc/readelflib.c b/sysdeps/unix/sysv/linux/powerpc/readelflib.c index 4f9a9be254..51f8a9496a 100644 --- a/sysdeps/unix/sysv/linux/powerpc/readelflib.c +++ b/sysdeps/unix/sysv/linux/powerpc/readelflib.c @@ -17,29 +17,31 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) - return process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + return process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* PowerPC 64bit libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_POWERPC_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/riscv/readelflib.c b/sysdeps/unix/sysv/linux/riscv/readelflib.c index 2aca6670a8..3822d63a05 100644 --- a/sysdeps/unix/sysv/linux/riscv/readelflib.c +++ b/sysdeps/unix/sysv/linux/riscv/readelflib.c @@ -17,11 +17,13 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* The ELF flags supported by our current glibc port: @@ -38,8 +40,8 @@ int process_elf64_file (const char *file_name, const char *lib, int *flag, /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; @@ -52,14 +54,14 @@ process_elf_file (const char *file_name, const char *lib, int *flag, if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) { - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); flags = elf32_header->e_flags; } else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); flags = elf64_header->e_flags; } diff --git a/sysdeps/unix/sysv/linux/s390/readelflib.c b/sysdeps/unix/sysv/linux/s390/readelflib.c index fc21af34d8..e190109e3d 100644 --- a/sysdeps/unix/sysv/linux/s390/readelflib.c +++ b/sysdeps/unix/sysv/linux/s390/readelflib.c @@ -16,29 +16,31 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) - return process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + return process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* S/390 64bit libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_S390_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/sparc/readelflib.c b/sysdeps/unix/sysv/linux/sparc/readelflib.c index d54b8dfd28..bbfc81337a 100644 --- a/sysdeps/unix/sysv/linux/sparc/readelflib.c +++ b/sysdeps/unix/sysv/linux/sparc/readelflib.c @@ -18,29 +18,31 @@ <https://www.gnu.org/licenses/>. */ -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret; if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) - return process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + return process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else { - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); /* Sparc 64bit libraries are always libc.so.6+. */ if (!ret) *flag = FLAG_SPARC_LIB64|FLAG_ELF_LIBC6; diff --git a/sysdeps/unix/sysv/linux/x86/elf-read-prop.h b/sysdeps/unix/sysv/linux/x86/elf-read-prop.h new file mode 100644 index 0000000000..85affa2864 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/elf-read-prop.h @@ -0,0 +1,60 @@ +/* Support for GNU properties in ldconfig. x86 version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#ifndef _ELF_READ_PROP_H +#define _ELF_READ_PROP_H + +#include <dl-cache.h> + +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. + Return value: + false: Continue processing the properties. + true : Stop processing the properties. + */ +static inline bool __attribute__ ((always_inline)) +read_gnu_property (unsigned int *isal_level, uint32_t type, + uint32_t datasz, void *data) +{ + /* Property type must be in ascending order. */ + if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) + return true; + + if (type == GNU_PROPERTY_X86_ISA_1_NEEDED) + { + if (datasz == 4) + { + /* The size of GNU_PROPERTY_X86_ISA_1_NEEDED must be 4 bytes. + There is no point to continue if this type is ill-formed. */ + unsigned int isa_1_needed = *(unsigned int *) data; + _Static_assert (((sizeof (isa_1_needed) * 8) + <= (1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT)), + "DL_CACHE_HWCAP_ISA_LEVEL_COUNT is too small"); + if (isa_1_needed != 0) + { + unsigned int level; + asm ("bsr %1, %0" : "=r" (level) : "g" (isa_1_needed)); + *isal_level = level; + } + } + return true; + } + + return false; +} + +#endif diff --git a/sysdeps/unix/sysv/linux/x86/readelflib.c b/sysdeps/unix/sysv/linux/x86/readelflib.c index a4df70e7e6..4a9bcc82cb 100644 --- a/sysdeps/unix/sysv/linux/x86/readelflib.c +++ b/sysdeps/unix/sysv/linux/x86/readelflib.c @@ -17,19 +17,20 @@ License along with the GNU C Library; if not, see <https://www.gnu.org/licenses/>. */ - -int process_elf32_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf32_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); -int process_elf64_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, +int process_elf64_file (const char *file_name, const char *lib, + int *flag, unsigned int *osversion, + unsigned int *isa_level, char **soname, void *file_contents, size_t file_length); /* Returns 0 if everything is ok, != 0 in case of error. */ int process_elf_file (const char *file_name, const char *lib, int *flag, - unsigned int *osversion, char **soname, void *file_contents, - size_t file_length) + unsigned int *osversion, unsigned int *isa_level, + char **soname, void *file_contents, size_t file_length) { ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; int ret, file_flag = 0; @@ -68,11 +69,11 @@ failed: } if (elf_header->e_ident[EI_CLASS] == ELFCLASS32) - ret = process_elf32_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); else - ret = process_elf64_file (file_name, lib, flag, osversion, soname, - file_contents, file_length); + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, + soname, file_contents, file_length); if (!ret && file_flag) *flag = file_flag; diff --git a/sysdeps/unix/sysv/linux/x86_64/Makefile b/sysdeps/unix/sysv/linux/x86_64/Makefile index 9b82155393..5e19202ebf 100644 --- a/sysdeps/unix/sysv/linux/x86_64/Makefile +++ b/sysdeps/unix/sysv/linux/x86_64/Makefile @@ -13,3 +13,54 @@ endif ifeq ($(subdir),misc) gen-as-const-headers += sigaltstack-offsets.sym endif + +ifeq ($(subdir),elf) +ifeq (yes,$(enable-x86-isa-level)) +tests += \ + tst-glibc-hwcaps-2 +ifeq (no,$(build-hardcoded-path-in-tests)) +# This is an ld.so.cache test, and RPATH/RUNPATH in the executable +# interferes with its test objectives. +tests-container += \ + tst-glibc-hwcaps-2-cache +endif +modules-names += \ + libx86-64-isa-level-1 \ + libx86-64-isa-level-2 \ + libx86-64-isa-level-3 \ + libx86-64-isa-level-4 + +$(objpfx)tst-glibc-hwcaps-2: $(objpfx)libx86-64-isa-level.so + +$(objpfx)tst-glibc-hwcaps-2.out: \ + $(objpfx)glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so \ + $(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so \ + $(objpfx)glibc-hwcaps/x86-64-v3/libx86-64-isa-level.so +$(objpfx)glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: \ + $(objpfx)libx86-64-isa-level-2.so + $(make-target-directory) + cp $< $@ +$(objpfx)glibc-hwcaps/x86-64-v3/libx86-64-isa-level.so: \ + $(objpfx)libx86-64-isa-level-3.so + $(make-target-directory) + cp $< $@ +$(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so: \ + $(objpfx)libx86-64-isa-level-4.so + $(make-target-directory) + cp $< $@ + +CFLAGS-libx86-64-isa-level-1.os += -march=x86-64 +CFLAGS-libx86-64-isa-level-2.os += -march=x86-64 +CFLAGS-libx86-64-isa-level-3.os += -march=x86-64 +CFLAGS-libx86-64-isa-level-4.os += -march=x86-64 + +# The test modules are parameterized by preprocessor macros. +LDFLAGS-libx86-64-isa-level-1.so += -Wl,-soname,libx86-64-isa-level.so +LDFLAGS-libx86-64-isa-level-4.so += -Wl,-soname,libx86-64-isa-level.so +$(objpfx)libx86-64-isa-level%.os: $(..)/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c + $(compile-command.c) -DVALUE=$(lastword $(subst -, ,$*)) \ + -DISA_LEVEL="(1 << ($(lastword $(subst -, ,$*)) - 1))" +$(objpfx)libx86-64-isa-level.so: $(objpfx)libx86-64-isa-level-1.so + cp $< $@ +endif +endif # $(subdir) == elf diff --git a/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c new file mode 100644 index 0000000000..fe91bfd224 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c @@ -0,0 +1,84 @@ +/* Check ISA level on shared object in glibc-hwcaps subdirectories. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <stdbool.h> +#include <stdlib.h> +#include <stdio.h> +#include <elf.h> +#include <get-isa-level.h> +#include <support/check.h> +#include <support/test-driver.h> + +extern int dso_isa_level (void); + +static int +do_test (void) +{ + const struct cpu_features *cpu_features + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); + unsigned int isa_level = get_isa_level (cpu_features); + bool has_isa_baseline = ((isa_level & GNU_PROPERTY_X86_ISA_1_BASELINE) + == GNU_PROPERTY_X86_ISA_1_BASELINE); + bool has_isa_v2 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V2) + == GNU_PROPERTY_X86_ISA_1_V2); + bool has_isa_v3 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V3) + == GNU_PROPERTY_X86_ISA_1_V3); + bool has_isa_v4 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) + == GNU_PROPERTY_X86_ISA_1_V4); + + if (!has_isa_baseline) + return EXIT_FAILURE; + + int level = dso_isa_level (); + int ret; + switch (level) + { + case 1: + case 2: + /* The default libx86-64-isa-level.so is used. */ + printf ("The default shared library is used.\n"); + if (has_isa_v3 || has_isa_v4 || !has_isa_v2) + ret = EXIT_FAILURE; + else + ret = EXIT_SUCCESS; + break; + case 3: + /* libx86-64-isa-level.so marked as x86-64 ISA level 3 needed in + x86-64-v2 should be ignored on lesser CPU. */ + printf ("x86-64 ISA level 3 shared library is used.\n"); + if (has_isa_v4 || !has_isa_v3) + ret = EXIT_FAILURE; + else + ret = EXIT_SUCCESS; + break; + case 4: + /* libx86-64-isa-level.so marked as x86-64 ISA level 4 needed in + x86-64-v3 should be ignored on lesser CPU. */ + printf ("x86-64 ISA level 4 shared library is used.\n"); + if (has_isa_v4) + ret = EXIT_SUCCESS; + else + ret = EXIT_FAILURE; + break; + default: + abort (); + } + return ret; +} + +#include <support/test-driver.c> diff --git a/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c new file mode 100644 index 0000000000..2813d627cc --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c @@ -0,0 +1,4 @@ +#define INCLUDE_X86_ISA_LEVEL +#define MARKER dso_isa_level +#include <isa-level.c> +#include <markermodMARKER-VALUE.c> diff --git a/sysdeps/x86/dl-isa-level.h b/sysdeps/x86/dl-isa-level.h new file mode 100644 index 0000000000..afe390c365 --- /dev/null +++ b/sysdeps/x86/dl-isa-level.h @@ -0,0 +1,31 @@ +/* Support for reading ISA level in /etc/ld.so.cache files written by + Linux ldconfig. x86 version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <sys/platform/x86.h> + +/* Return true if the ISA level in ENTRY is compatible with CPU. */ +static inline bool +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) +{ + const struct cpu_features *cpu_features = __get_cpu_features (); + unsigned int isa_level + = 1 << ((entry->hwcap >> 32) & DL_CACHE_HWCAP_ISA_LEVEL_MASK); + + return (isa_level & cpu_features->isa_1) == isa_level; +} -- 2.29.2 ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V2 [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2021-01-12 23:32 ` V2 " H.J. Lu @ 2021-01-13 13:47 ` Adhemerval Zanella 2021-01-13 14:12 ` H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Adhemerval Zanella @ 2021-01-13 13:47 UTC (permalink / raw) To: H.J. Lu; +Cc: Florian Weimer, H.J. Lu via Libc-alpha On 12/01/2021 20:32, H.J. Lu wrote: > On Tue, Jan 12, 2021 at 8:25 AM Adhemerval Zanella > <adhemerval.zanella@linaro.org> wrote: >> >> >> >> On 09/12/2020 00:27, H.J. Lu via Libc-alpha wrote: >>> On Tue, Dec 8, 2020 at 7:31 AM H.J. Lu <hjl.tools@gmail.com> wrote: >>> @@ -850,6 +859,7 @@ struct aux_cache_entry >>> struct aux_cache_entry_id id; >>> int flags; >>> unsigned int osversion; >>> + unsigned int isa_level; >>> int used; >>> char *soname; >>> struct aux_cache_entry *next; >>> @@ -863,7 +873,7 @@ struct aux_cache_file_entry >>> int32_t flags; /* This is 1 for an ELF library. */ >>> uint32_t soname; /* String table indice. */ >>> uint32_t osversion; /* Required OS version. */ >>> - int32_t pad; >>> + uint32_t isa_level; /* Required ISA level. */ >>> }; >>> >> >> I think it would be good to have a _Static_assert with the >> expected aux_cache_entry size. > > What would that be? aux_cache_entry_id size is increased > by 4 bytes with my patch. My understanding is aux_cache_entry_id keeps the same and you are using an unused field on *aux_cache_file_entry* to add the isa_level. Their sizes should kept the same (since isa_level is the same size of the previous pad). But the _Static_assert might indeed unrequired, the load_aux_cache already check if the aux_cache_file_entry does match the file size and uses an empty auxiliary cache otherwise (init_aux_cache). elf/cache.c 1008 if (aux_cache == MAP_FAILED 1009 || aux_cache_size < sizeof (struct aux_cache_file) 1010 || memcmp (aux_cache->magic, AUX_CACHEMAGIC, sizeof AUX_CACHEMAGIC - 1) 1011 || aux_cache_size != (sizeof (struct aux_cache_file) 1012 + aux_cache->nlibs * sizeof (struct aux_cache_file_entry) 1013 + aux_cache->len_strings)) 1014 { 1015 close (fd); 1016 init_aux_cache (); 1017 return; 1018 } >>> /* Return true if the ENTRY->hwcap value indicates that >>> DL_CACHE_HWCAP_EXTENSION is used. */ >>> static inline bool >>> dl_cache_hwcap_extension (struct file_entry_new *entry) >>> { >>> - /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this >>> - is a different kind of extension. */ >>> - return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); >>> + /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits, except for the >>> + ISA level bits, as well, this is a different kind of extension. */ >>> + return (((entry->hwcap >> 32) & ~DL_CACHE_HWCAP_ISA_LEVEL_MASK) >>> + == (DL_CACHE_HWCAP_EXTENSION >> 32)); >>> } >>> >>> /* See flags member of struct cache_file_new below. */ >> >> The comment sounds confusing. > > I changed it to > > /* This is an hwcap extension if only the DL_CACHE_HWCAP_EXTENSION bit > is set, ignoring the lower 32 bits as well as the ISA level bits in > the upper 32 bits. */ Thanks. --- > From 40bacb7470f0c8d8eabd4d3b24057566d45ca5dd Mon Sep 17 00:00:00 2001 > From: "H.J. Lu" <hjl.tools@gmail.com> > Date: Sat, 5 Dec 2020 07:01:58 -0800 > Subject: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache > > Store ISA level in the portion of the unused upper 32 bits of the hwcaps > field in cache and the unused pad field in aux cache. ISA level is stored > and checked only for shared objects in glibc-hwcaps subdirectories. The > shared objects in the default directories aren't checked since there are > no fallbacks for these shared objects. > > Tested on x86-64-v2, x86-64-v3 and x86-64-v4 machines with > --disable-hardcoded-path-in-tests and --enable-hardcoded-path-in-tests. LGTM, thanks. Please update the release page once you commit it. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org> > --- > elf/cache.c | 39 +++++++-- > elf/dl-cache.c | 4 + > elf/ldconfig.c | 18 ++-- > elf/readelflib.c | 81 +++++++++++++++++- > elf/readlib.c | 7 +- > elf/tst-glibc-hwcaps-2-cache.c | 45 ++++++++++ > .../etc/ld.so.conf | 2 + > .../postclean.req | 0 > elf/tst-glibc-hwcaps-2-cache.script | 8 ++ > sysdeps/generic/dl-cache.h | 16 +++- > sysdeps/generic/dl-isa-level.h | 25 ++++++ > sysdeps/generic/elf-read-prop.h | 34 ++++++++ > sysdeps/generic/ldconfig.h | 22 +++-- > sysdeps/unix/sysv/linux/arm/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/ia64/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/mips/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/powerpc/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/riscv/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/s390/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/sparc/readelflib.c | 22 ++--- > sysdeps/unix/sysv/linux/x86/elf-read-prop.h | 60 +++++++++++++ > sysdeps/unix/sysv/linux/x86/readelflib.c | 23 ++--- > sysdeps/unix/sysv/linux/x86_64/Makefile | 51 +++++++++++ > .../sysv/linux/x86_64/tst-glibc-hwcaps-2.c | 84 +++++++++++++++++++ > .../linux/x86_64/x86-64-isa-level-VALUE.c | 4 + > sysdeps/x86/dl-isa-level.h | 31 +++++++ > 26 files changed, 598 insertions(+), 110 deletions(-) > create mode 100644 elf/tst-glibc-hwcaps-2-cache.c > create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > create mode 100644 elf/tst-glibc-hwcaps-2-cache.root/postclean.req > create mode 100644 elf/tst-glibc-hwcaps-2-cache.script > create mode 100644 sysdeps/generic/dl-isa-level.h > create mode 100644 sysdeps/generic/elf-read-prop.h > create mode 100644 sysdeps/unix/sysv/linux/x86/elf-read-prop.h > create mode 100644 sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > create mode 100644 sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > create mode 100644 sysdeps/x86/dl-isa-level.h > > diff --git a/elf/cache.c b/elf/cache.c > index 11ce4ade8a..c01d302072 100644 > --- a/elf/cache.c > +++ b/elf/cache.c > @@ -146,6 +146,7 @@ struct cache_entry > struct stringtable_entry *path; /* Path to find library. */ > int flags; /* Flags to indicate kind of library. */ > unsigned int osversion; /* Required OS version. */ > + unsigned int isa_level; /* Required ISA level. */ > uint64_t hwcap; /* Important hardware capabilities. */ > int bits_hwcap; /* Number of bits set in hwcap. */ > > @@ -549,6 +550,19 @@ write_extensions (int fd, uint32_t str_offset, > free (ext); > } > > +/* Compute the hwcap value from ENTRY. */ > +static inline uint64_t > +compute_hwcap_value (struct cache_entry *entry) > +{ > + if (entry->isa_level > DL_CACHE_HWCAP_ISA_LEVEL_MASK) > + error (EXIT_FAILURE, 0, _("%s: ISA level is too high (%d > %d)"), > + entry->path->string, entry->isa_level, > + DL_CACHE_HWCAP_ISA_LEVEL_MASK); > + return (DL_CACHE_HWCAP_EXTENSION > + | (((uint64_t) entry->isa_level) << 32) > + | entry->hwcaps->section_index); > +} > + > /* Save the contents of the cache. */ > void > save_cache (const char *cache_name) > @@ -662,7 +676,7 @@ save_cache (const char *cache_name) > file_entries_new->libs[idx_new].hwcap = entry->hwcap; > else > file_entries_new->libs[idx_new].hwcap > - = DL_CACHE_HWCAP_EXTENSION | entry->hwcaps->section_index; > + = compute_hwcap_value (entry); > file_entries_new->libs[idx_new].key > = str_offset + entry->lib->offset; > file_entries_new->libs[idx_new].value Ok. > @@ -777,7 +791,8 @@ save_cache (const char *cache_name) > /* Add one library to the cache. */ > void > add_to_cache (const char *path, const char *filename, const char *soname, > - int flags, unsigned int osversion, uint64_t hwcap, > + int flags, unsigned int osversion, > + unsigned int isa_level, uint64_t hwcap, > struct glibc_hwcaps_subdirectory *hwcaps) > { > struct cache_entry *new_entry = xmalloc (sizeof (*new_entry)); > @@ -795,6 +810,7 @@ add_to_cache (const char *path, const char *filename, const char *soname, > new_entry->path = path_interned; > new_entry->flags = flags; > new_entry->osversion = osversion; > + new_entry->isa_level = isa_level; > new_entry->hwcap = hwcap; > new_entry->hwcaps = hwcaps; > new_entry->bits_hwcap = 0; > @@ -851,6 +867,7 @@ struct aux_cache_entry > struct aux_cache_entry_id id; > int flags; > unsigned int osversion; > + unsigned int isa_level; > int used; > char *soname; > struct aux_cache_entry *next; > @@ -864,7 +881,7 @@ struct aux_cache_file_entry > int32_t flags; /* This is 1 for an ELF library. */ > uint32_t soname; /* String table indice. */ > uint32_t osversion; /* Required OS version. */ > - int32_t pad; > + uint32_t isa_level; /* Required ISA level. */ > }; > > /* ldconfig maintains an auxiliary cache file that allows > @@ -915,7 +932,8 @@ init_aux_cache (void) > > int > search_aux_cache (struct stat64 *stat_buf, int *flags, > - unsigned int *osversion, char **soname) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname) > { > struct aux_cache_entry_id id; > id.ino = (uint64_t) stat_buf->st_ino; > @@ -933,6 +951,7 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, > { > *flags = entry->flags; > *osversion = entry->osversion; > + *isa_level = entry->isa_level; > if (entry->soname != NULL) > *soname = xstrdup (entry->soname); > else > @@ -946,7 +965,8 @@ search_aux_cache (struct stat64 *stat_buf, int *flags, > > static void > insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > - unsigned int osversion, const char *soname, int used) > + unsigned int osversion, unsigned int isa_level, > + const char *soname, int used) > { > size_t hash = aux_cache_entry_id_hash (id) % aux_hash_size; > struct aux_cache_entry *entry; > @@ -962,6 +982,7 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > entry->id = *id; > entry->flags = flags; > entry->osversion = osversion; > + entry->isa_level = isa_level; > entry->used = used; > if (soname != NULL) > entry->soname = memcpy ((char *) (entry + 1), soname, len); > @@ -973,14 +994,15 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, > > void > add_to_aux_cache (struct stat64 *stat_buf, int flags, > - unsigned int osversion, const char *soname) > + unsigned int osversion, unsigned int isa_level, > + const char *soname) > { > struct aux_cache_entry_id id; > id.ino = (uint64_t) stat_buf->st_ino; > id.ctime = (uint64_t) stat_buf->st_ctime; > id.size = (uint64_t) stat_buf->st_size; > id.dev = (uint64_t) stat_buf->st_dev; > - insert_to_aux_cache (&id, flags, osversion, soname, 1); > + insert_to_aux_cache (&id, flags, osversion, isa_level, soname, 1); > } > > /* Load auxiliary cache to search for unchanged entries. */ > @@ -1026,6 +1048,7 @@ load_aux_cache (const char *aux_cache_name) > insert_to_aux_cache (&aux_cache->libs[i].id, > aux_cache->libs[i].flags, > aux_cache->libs[i].osversion, > + aux_cache->libs[i].isa_level, > aux_cache->libs[i].soname == 0 > ? NULL : aux_cache_data + aux_cache->libs[i].soname, > 0); > @@ -1094,7 +1117,7 @@ save_aux_cache (const char *aux_cache_name) > str_offset += len; > } > file_entries->libs[idx].osversion = entry->osversion; > - file_entries->libs[idx++].pad = 0; > + file_entries->libs[idx++].isa_level = entry->isa_level; > } > > /* Write out auxiliary cache file. */ Ok. > diff --git a/elf/dl-cache.c b/elf/dl-cache.c > index 935e3a60b4..32f3bef5ea 100644 > --- a/elf/dl-cache.c > +++ b/elf/dl-cache.c > @@ -25,6 +25,7 @@ > #include <stdint.h> > #include <_itoa.h> > #include <dl-hwcaps.h> > +#include <dl-isa-level.h> > > #ifndef _DL_PLATFORMS_COUNT > # define _DL_PLATFORMS_COUNT 0 > @@ -284,6 +285,9 @@ search_cache (const char *string_table, uint32_t string_table_size, > > #ifdef SHARED > named_hwcap = dl_cache_hwcap_extension (libnew); > + if (named_hwcap > + && !dl_cache_hwcap_isa_level_compatible (libnew)) > + continue; > #endif > > /* The entries with named/extension hwcaps Ok. > diff --git a/elf/ldconfig.c b/elf/ldconfig.c > index bbcf8f5c5c..28ed637a29 100644 > --- a/elf/ldconfig.c > +++ b/elf/ldconfig.c > @@ -655,6 +655,7 @@ manual_link (char *library) > struct stat64 stat_buf; > int flag; > unsigned int osversion; > + unsigned int isa_level; > > /* Prepare arguments for create_links call. Split library name in > directory and filename first. Since path is allocated, we've got > @@ -721,7 +722,7 @@ manual_link (char *library) > } > > if (process_file (real_library, library, libname, &flag, &osversion, > - &soname, 0, &stat_buf)) > + &isa_level, &soname, 0, &stat_buf)) > { > error (0, 0, _("No link created since soname could not be found for %s"), > library); > @@ -768,6 +769,7 @@ struct dlib_entry > int flag; > int is_link; > unsigned int osversion; > + unsigned int isa_level; > struct dlib_entry *next; > }; > > @@ -980,17 +982,21 @@ search_dir (const struct dir_entry *entry) > library already and it's not changed. */ > char *soname; > unsigned int osversion; > - if (!search_aux_cache (&lstat_buf, &flag, &osversion, &soname)) > + unsigned int isa_level; > + if (!search_aux_cache (&lstat_buf, &flag, &osversion, &isa_level, > + &soname)) > { > if (process_file (real_name, file_name, direntry->d_name, &flag, > - &osversion, &soname, is_link, &lstat_buf)) > + &osversion, &isa_level, &soname, is_link, > + &lstat_buf)) > { > if (real_name != real_file_name) > free (real_name); > continue; > } > else if (opt_build_cache) > - add_to_aux_cache (&lstat_buf, flag, osversion, soname); > + add_to_aux_cache (&lstat_buf, flag, osversion, isa_level, > + soname); > } > > if (soname == NULL) > @@ -1096,6 +1102,7 @@ search_dir (const struct dir_entry *entry) > dlib_ptr->name = xstrdup (direntry->d_name); > dlib_ptr->is_link = is_link; > dlib_ptr->osversion = osversion; > + dlib_ptr->isa_level = isa_level; > } > /* Don't add this library, abort loop. */ > /* Also free soname, since it's dynamically allocated. */ > @@ -1112,6 +1119,7 @@ search_dir (const struct dir_entry *entry) > dlib_ptr->flag = flag; > dlib_ptr->is_link = is_link; > dlib_ptr->osversion = osversion; > + dlib_ptr->isa_level = isa_level; > /* Add at head of list. */ > dlib_ptr->next = dlibs; > dlibs = dlib_ptr; > @@ -1149,7 +1157,7 @@ search_dir (const struct dir_entry *entry) > if (opt_build_cache) > add_to_cache (entry->path, filename, dlib_ptr->soname, > dlib_ptr->flag, dlib_ptr->osversion, > - hwcap, entry->hwcaps); > + dlib_ptr->isa_level, hwcap, entry->hwcaps); > } > > /* Free all resources. */ Ok. > diff --git a/elf/readelflib.c b/elf/readelflib.c > index cdea79d729..c09425a574 100644 > --- a/elf/readelflib.c > +++ b/elf/readelflib.c > @@ -17,6 +17,8 @@ > License along with the GNU C Library; if not, see > <https://www.gnu.org/licenses/>. */ > > +#include <elf-read-prop.h> > + > /* This code is a heavily simplified version of the readelf program > that's part of the current binutils development version. For architectures > which need to handle both 32bit and 64bit ELF libraries, this file is > @@ -40,8 +42,8 @@ do \ > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > int i; > unsigned int j; > @@ -86,6 +88,9 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > libc5/libc6. */ > *flag = FLAG_ELF; > > + /* The default ISA level is 0. */ > + *isa_level = 0; > + > dynamic_addr = 0; > dynamic_size = 0; > program_interpreter = NULL; > @@ -164,6 +169,78 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > } > break; > > + case PT_GNU_PROPERTY: > + /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes > + in 32-bit objects and to 8 bytes in 64-bit objects. Skip > + notes with incorrect alignment. */ > + if (segment->p_align == (__ELF_NATIVE_CLASS / 8)) > + { > + const ElfW(Nhdr) *note = (const void *) (file_contents > + + segment->p_offset); > + const ElfW(Addr) size = segment->p_filesz; > + const ElfW(Addr) align = segment->p_align; > + > + const ElfW(Addr) start = (ElfW(Addr)) (uintptr_t) note; > + unsigned int last_type = 0; > + > + while ((ElfW(Addr)) (uintptr_t) (note + 1) - start < size) > + { > + /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ > + if (note->n_namesz == 4 > + && note->n_type == NT_GNU_PROPERTY_TYPE_0 > + && memcmp (note + 1, "GNU", 4) == 0) > + { > + /* Check for invalid property. */ > + if (note->n_descsz < 8 > + || (note->n_descsz % sizeof (ElfW(Addr))) != 0) > + goto done; > + > + /* Start and end of property array. */ > + unsigned char *ptr = (unsigned char *) (note + 1) + 4; > + unsigned char *ptr_end = ptr + note->n_descsz; > + > + do > + { > + unsigned int type = *(unsigned int *) ptr; > + unsigned int datasz = *(unsigned int *) (ptr + 4); > + > + /* Property type must be in ascending order. */ > + if (type < last_type) > + goto done; > + > + ptr += 8; > + if ((ptr + datasz) > ptr_end) > + goto done; > + > + last_type = type; > + > + /* Target specific property processing. > + Return value: > + false: Continue processing the properties. > + true : Stop processing the properties. > + */ > + if (read_gnu_property (isa_level, type, > + datasz, ptr)) > + goto done; > + > + /* Check the next property item. */ > + ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); > + } > + while ((ptr_end - ptr) >= 8); > + > + /* Only handle one NT_GNU_PROPERTY_TYPE_0. */ > + goto done; > + } > + > + note = ((const void *) note > + + ELF_NOTE_NEXT_OFFSET (note->n_namesz, > + note->n_descsz, > + align)); > + } > + } > +done: > + break; > + > default: > break; > } > diff --git a/elf/readlib.c b/elf/readlib.c Ok. > index 3d52c9a980..7383c23249 100644 > --- a/elf/readlib.c > +++ b/elf/readlib.c > @@ -75,7 +75,8 @@ is_gdb_python_file (const char *name) > int > process_file (const char *real_file_name, const char *file_name, > const char *lib, int *flag, unsigned int *osversion, > - char **soname, int is_link, struct stat64 *stat_buf) > + unsigned int *isa_level, char **soname, int is_link, > + struct stat64 *stat_buf) > { > FILE *file; > struct stat64 statbuf; > @@ -173,8 +174,8 @@ process_file (const char *real_file_name, const char *file_name, > /* Libraries have to be shared object files. */ > else if (elf_header->e_type != ET_DYN) > ret = 1; > - else if (process_elf_file (file_name, lib, flag, osversion, soname, > - file_contents, statbuf.st_size)) > + else if (process_elf_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, statbuf.st_size)) > ret = 1; > > done: Ok. > diff --git a/elf/tst-glibc-hwcaps-2-cache.c b/elf/tst-glibc-hwcaps-2-cache.c > new file mode 100644 > index 0000000000..97d9835ddb > --- /dev/null > +++ b/elf/tst-glibc-hwcaps-2-cache.c > @@ -0,0 +1,45 @@ > +/* Wrapper to invoke tst-glibc-hwcaps-2 in a container to test ldconfig. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +/* This program is just a wrapper that runs ldconfig followed by > + tst-glibc-hwcaps-2. The actual test is provided via an > + implementation in a sysdeps subdirectory. */ > + > +#include <stdio.h> > +#include <stdlib.h> > +#include <unistd.h> > +#include <support/support.h> > +#include <support/capture_subprocess.h> > + > +int > +main (int argc, char **argv) > +{ > + /* Run ldconfig to populate the cache. */ > + char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir); > + struct support_capture_subprocess result = > + support_capture_subprogram (command, &((char *) { NULL })); > + support_capture_subprocess_check (&result, "ldconfig", 0, sc_allow_none); > + free (command); > + > + /* Reuse tst-glibc-hwcaps. Since this code is running in a > + container, we can launch it directly. */ > + char *path = xasprintf ("%s/elf/tst-glibc-hwcaps-2", support_objdir_root); > + execv (path, argv); > + printf ("error: execv of %s failed: %m\n", path); > + return 1; > +} Ok. > diff --git a/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > new file mode 100644 > index 0000000000..e1e74dbda2 > --- /dev/null > +++ b/elf/tst-glibc-hwcaps-2-cache.root/etc/ld.so.conf > @@ -0,0 +1,2 @@ > +# This file was created to suppress a warning from ldconfig: > +# /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory > diff --git a/elf/tst-glibc-hwcaps-2-cache.root/postclean.req b/elf/tst-glibc-hwcaps-2-cache.root/postclean.req > new file mode 100644 > index 0000000000..e69de29bb2 > diff --git a/elf/tst-glibc-hwcaps-2-cache.script b/elf/tst-glibc-hwcaps-2-cache.script > new file mode 100644 > index 0000000000..8e4e9896ee > --- /dev/null > +++ b/elf/tst-glibc-hwcaps-2-cache.script > @@ -0,0 +1,8 @@ > +# test-container does not support scripts in sysdeps directories, so > +# collect everything in one file. > + > +mkdirp 0770 $L/glibc-hwcaps/x86-64-v2 > +mkdirp 0770 $L/glibc-hwcaps/x86-64-v3 > +cp $B/elf/libx86-64-isa-level-1.so $L/libx86-64-isa-level.so > +cp $B/elf/libx86-64-isa-level-3.so $L/glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so > +cp $B/elf/libx86-64-isa-level-4.so $L/glibc-hwcaps/x86-64-v3/libx86-64-isa-level.so Ok. > diff --git a/sysdeps/generic/dl-cache.h b/sysdeps/generic/dl-cache.h > index c7eca70d0c..964d50a486 100644 > --- a/sysdeps/generic/dl-cache.h > +++ b/sysdeps/generic/dl-cache.h > @@ -106,14 +106,24 @@ struct file_entry_new > entries. */ > #define DL_CACHE_HWCAP_EXTENSION (1ULL << 62) > > +/* The number of the ISA level bits in the upper 32 bits of the hwcap > + field. */ > +#define DL_CACHE_HWCAP_ISA_LEVEL_COUNT 10 > + > +/* The mask of the ISA level bits in the hwcap field. */ > +#define DL_CACHE_HWCAP_ISA_LEVEL_MASK \ > + ((1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT) -1) > + > /* Return true if the ENTRY->hwcap value indicates that > DL_CACHE_HWCAP_EXTENSION is used. */ > static inline bool > dl_cache_hwcap_extension (struct file_entry_new *entry) > { > - /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this > - is a different kind of extension. */ > - return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); > + /* This is an hwcap extension if only the DL_CACHE_HWCAP_EXTENSION bit > + is set, ignoring the lower 32 bits as well as the ISA level bits in > + the upper 32 bits. */ > + return (((entry->hwcap >> 32) & ~DL_CACHE_HWCAP_ISA_LEVEL_MASK) > + == (DL_CACHE_HWCAP_EXTENSION >> 32)); > } > > /* See flags member of struct cache_file_new below. */ Ok. > diff --git a/sysdeps/generic/dl-isa-level.h b/sysdeps/generic/dl-isa-level.h > new file mode 100644 > index 0000000000..6fcd319c49 > --- /dev/null > +++ b/sysdeps/generic/dl-isa-level.h > @@ -0,0 +1,25 @@ > +/* Support for reading ISA level in /etc/ld.so.cache files written by > + Linux ldconfig. Generic version. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +/* Return true if the ISA level in ENTRY is compatible with CPU. */ > +static inline bool > +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) > +{ > + return true; > +} Ok. > diff --git a/sysdeps/generic/elf-read-prop.h b/sysdeps/generic/elf-read-prop.h > new file mode 100644 > index 0000000000..98c3d2acb5 > --- /dev/null > +++ b/sysdeps/generic/elf-read-prop.h > @@ -0,0 +1,34 @@ > +/* Support for GNU properties in ldconfig. Generic version. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#ifndef _ELF_READ_PROP_H > +#define _ELF_READ_PROP_H > + > +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. > + Return value: > + false: Continue processing the properties. > + true : Stop processing the properties. > + */ > +static inline bool __attribute__ ((always_inline)) > +read_gnu_property (unsigned int *isal_level, uint32_t type, uint32_t > + datasz, void *data) > +{ > + return true; > +} > + > +#endif Ok. > diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h > index eb070fd259..3ab757077d 100644 > --- a/sysdeps/generic/ldconfig.h > +++ b/sysdeps/generic/ldconfig.h > @@ -70,8 +70,9 @@ const char *glibc_hwcaps_subdirectory_name > (const struct glibc_hwcaps_subdirectory *); > > extern void add_to_cache (const char *path, const char *filename, > - const char *soname, > - int flags, unsigned int osversion, uint64_t hwcap, > + const char *soname, int flags, > + unsigned int osversion, unsigned int isa_level, > + uint64_t hwcap, > struct glibc_hwcaps_subdirectory *); > > extern void init_aux_cache (void); > @@ -79,23 +80,28 @@ extern void init_aux_cache (void); > extern void load_aux_cache (const char *aux_cache_name); > > extern int search_aux_cache (struct stat64 *stat_buf, int *flags, > - unsigned int *osversion, char **soname); > + unsigned int *osversion, > + unsigned int *isa_level, char **soname); > > extern void add_to_aux_cache (struct stat64 *stat_buf, int flags, > - unsigned int osversion, const char *soname); > + unsigned int osversion, > + unsigned int isa_level, const char *soname); > > extern void save_aux_cache (const char *aux_cache_name); > > /* Declared in readlib.c. */ > extern int process_file (const char *real_file_name, const char *file_name, > - const char *lib, int *flag, unsigned int *osversion, > - char **soname, int is_link, struct stat64 *stat_buf); > + const char *lib, int *flag, > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, int is_link, > + struct stat64 *stat_buf); > > extern char *implicit_soname (const char *lib, int flag); > > /* Declared in readelflib.c. */ > -extern int process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +extern int process_elf_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Declared in chroot_canon.c. */ Ok. > diff --git a/sysdeps/unix/sysv/linux/arm/readelflib.c b/sysdeps/unix/sysv/linux/arm/readelflib.c > index 2d3ecb1eef..ade2f49aeb 100644 > --- a/sysdeps/unix/sysv/linux/arm/readelflib.c > +++ b/sysdeps/unix/sysv/linux/arm/readelflib.c > @@ -18,18 +18,20 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > @@ -38,8 +40,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > { > Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; > > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > > if (!ret && EF_ARM_EABI_VERSION (elf32_header->e_flags) == EF_ARM_EABI_VER5) > { > @@ -57,8 +59,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > } > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* AArch64 libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_AARCH64_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/ia64/readelflib.c b/sysdeps/unix/sysv/linux/ia64/readelflib.c > index 7fb3ace359..2aa32666aa 100644 > --- a/sysdeps/unix/sysv/linux/ia64/readelflib.c > +++ b/sysdeps/unix/sysv/linux/ia64/readelflib.c > @@ -16,29 +16,31 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > - void *file_contents, size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > - return process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* Intel 64bit libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_IA64_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/mips/readelflib.c b/sysdeps/unix/sysv/linux/mips/readelflib.c > index e50bc3ce23..d0e809d893 100644 > --- a/sysdeps/unix/sysv/linux/mips/readelflib.c > +++ b/sysdeps/unix/sysv/linux/mips/readelflib.c > @@ -20,18 +20,20 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > union > { > @@ -45,8 +47,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > elf_header.eh = file_contents; > if (elf_header.eh->e_ident [EI_CLASS] == ELFCLASS32) > { > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > if (!ret) > { > Elf32_Word flags = elf_header.eh32->e_flags; > @@ -62,8 +64,8 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > } > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* n64 libraries are always libc.so.6+. */ > if (!ret) > { Ok. > diff --git a/sysdeps/unix/sysv/linux/powerpc/readelflib.c b/sysdeps/unix/sysv/linux/powerpc/readelflib.c > index 4f9a9be254..51f8a9496a 100644 > --- a/sysdeps/unix/sysv/linux/powerpc/readelflib.c > +++ b/sysdeps/unix/sysv/linux/powerpc/readelflib.c > @@ -17,29 +17,31 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > - return process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* PowerPC 64bit libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_POWERPC_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/riscv/readelflib.c b/sysdeps/unix/sysv/linux/riscv/readelflib.c > index 2aca6670a8..3822d63a05 100644 > --- a/sysdeps/unix/sysv/linux/riscv/readelflib.c > +++ b/sysdeps/unix/sysv/linux/riscv/readelflib.c > @@ -17,11 +17,13 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* The ELF flags supported by our current glibc port: > @@ -38,8 +40,8 @@ int process_elf64_file (const char *file_name, const char *lib, int *flag, > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > Elf32_Ehdr *elf32_header = (Elf32_Ehdr *) elf_header; > @@ -52,14 +54,14 @@ process_elf_file (const char *file_name, const char *lib, int *flag, > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > { > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > flags = elf32_header->e_flags; > } > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > flags = elf64_header->e_flags; > } > Ok. > diff --git a/sysdeps/unix/sysv/linux/s390/readelflib.c b/sysdeps/unix/sysv/linux/s390/readelflib.c > index fc21af34d8..e190109e3d 100644 > --- a/sysdeps/unix/sysv/linux/s390/readelflib.c > +++ b/sysdeps/unix/sysv/linux/s390/readelflib.c > @@ -16,29 +16,31 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > - return process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* S/390 64bit libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_S390_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/sparc/readelflib.c b/sysdeps/unix/sysv/linux/sparc/readelflib.c > index d54b8dfd28..bbfc81337a 100644 > --- a/sysdeps/unix/sysv/linux/sparc/readelflib.c > +++ b/sysdeps/unix/sysv/linux/sparc/readelflib.c > @@ -18,29 +18,31 @@ > <https://www.gnu.org/licenses/>. */ > > > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret; > > if (elf_header->e_ident [EI_CLASS] == ELFCLASS32) > - return process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + return process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > { > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > /* Sparc 64bit libraries are always libc.so.6+. */ > if (!ret) > *flag = FLAG_SPARC_LIB64|FLAG_ELF_LIBC6; Ok. > diff --git a/sysdeps/unix/sysv/linux/x86/elf-read-prop.h b/sysdeps/unix/sysv/linux/x86/elf-read-prop.h > new file mode 100644 > index 0000000000..85affa2864 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/x86/elf-read-prop.h > @@ -0,0 +1,60 @@ > +/* Support for GNU properties in ldconfig. x86 version. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#ifndef _ELF_READ_PROP_H > +#define _ELF_READ_PROP_H > + > +#include <dl-cache.h> > + > +/* Called for each property in the NT_GNU_PROPERTY_TYPE_0 note of SEGMENT. > + Return value: > + false: Continue processing the properties. > + true : Stop processing the properties. > + */ > +static inline bool __attribute__ ((always_inline)) > +read_gnu_property (unsigned int *isal_level, uint32_t type, > + uint32_t datasz, void *data) > +{ > + /* Property type must be in ascending order. */ > + if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) > + return true; > + > + if (type == GNU_PROPERTY_X86_ISA_1_NEEDED) > + { > + if (datasz == 4) > + { > + /* The size of GNU_PROPERTY_X86_ISA_1_NEEDED must be 4 bytes. > + There is no point to continue if this type is ill-formed. */ > + unsigned int isa_1_needed = *(unsigned int *) data; > + _Static_assert (((sizeof (isa_1_needed) * 8) > + <= (1 << DL_CACHE_HWCAP_ISA_LEVEL_COUNT)), > + "DL_CACHE_HWCAP_ISA_LEVEL_COUNT is too small"); > + if (isa_1_needed != 0) > + { > + unsigned int level; > + asm ("bsr %1, %0" : "=r" (level) : "g" (isa_1_needed)); > + *isal_level = level; > + } > + } > + return true; > + } > + > + return false; > +} > + > +#endif Ok. > diff --git a/sysdeps/unix/sysv/linux/x86/readelflib.c b/sysdeps/unix/sysv/linux/x86/readelflib.c > index a4df70e7e6..4a9bcc82cb 100644 > --- a/sysdeps/unix/sysv/linux/x86/readelflib.c > +++ b/sysdeps/unix/sysv/linux/x86/readelflib.c > @@ -17,19 +17,20 @@ > License along with the GNU C Library; if not, see > <https://www.gnu.org/licenses/>. */ > > - > -int process_elf32_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf32_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > -int process_elf64_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, > +int process_elf64_file (const char *file_name, const char *lib, > + int *flag, unsigned int *osversion, > + unsigned int *isa_level, char **soname, > void *file_contents, size_t file_length); > > /* Returns 0 if everything is ok, != 0 in case of error. */ > int > process_elf_file (const char *file_name, const char *lib, int *flag, > - unsigned int *osversion, char **soname, void *file_contents, > - size_t file_length) > + unsigned int *osversion, unsigned int *isa_level, > + char **soname, void *file_contents, size_t file_length) > { > ElfW(Ehdr) *elf_header = (ElfW(Ehdr) *) file_contents; > int ret, file_flag = 0; > @@ -68,11 +69,11 @@ failed: > } > > if (elf_header->e_ident[EI_CLASS] == ELFCLASS32) > - ret = process_elf32_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf32_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > else > - ret = process_elf64_file (file_name, lib, flag, osversion, soname, > - file_contents, file_length); > + ret = process_elf64_file (file_name, lib, flag, osversion, isa_level, > + soname, file_contents, file_length); > > if (!ret && file_flag) > *flag = file_flag; Ok. > diff --git a/sysdeps/unix/sysv/linux/x86_64/Makefile b/sysdeps/unix/sysv/linux/x86_64/Makefile > index 9b82155393..5e19202ebf 100644 > --- a/sysdeps/unix/sysv/linux/x86_64/Makefile > +++ b/sysdeps/unix/sysv/linux/x86_64/Makefile > @@ -13,3 +13,54 @@ endif > ifeq ($(subdir),misc) > gen-as-const-headers += sigaltstack-offsets.sym > endif > + > +ifeq ($(subdir),elf) > +ifeq (yes,$(enable-x86-isa-level)) > +tests += \ > + tst-glibc-hwcaps-2 > +ifeq (no,$(build-hardcoded-path-in-tests)) > +# This is an ld.so.cache test, and RPATH/RUNPATH in the executable > +# interferes with its test objectives. > +tests-container += \ > + tst-glibc-hwcaps-2-cache > +endif > +modules-names += \ > + libx86-64-isa-level-1 \ > + libx86-64-isa-level-2 \ > + libx86-64-isa-level-3 \ > + libx86-64-isa-level-4 > + > +$(objpfx)tst-glibc-hwcaps-2: $(objpfx)libx86-64-isa-level.so > + > +$(objpfx)tst-glibc-hwcaps-2.out: \ > + $(objpfx)glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so \ > + $(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so \ > + $(objpfx)glibc-hwcaps/x86-64-v3/libx86-64-isa-level.so > +$(objpfx)glibc-hwcaps/x86-64-v2/libx86-64-isa-level.so: \ > + $(objpfx)libx86-64-isa-level-2.so > + $(make-target-directory) > + cp $< $@ > +$(objpfx)glibc-hwcaps/x86-64-v3/libx86-64-isa-level.so: \ > + $(objpfx)libx86-64-isa-level-3.so > + $(make-target-directory) > + cp $< $@ > +$(objpfx)glibc-hwcaps/x86-64-v4/libx86-64-isa-level.so: \ > + $(objpfx)libx86-64-isa-level-4.so > + $(make-target-directory) > + cp $< $@ > + > +CFLAGS-libx86-64-isa-level-1.os += -march=x86-64 > +CFLAGS-libx86-64-isa-level-2.os += -march=x86-64 > +CFLAGS-libx86-64-isa-level-3.os += -march=x86-64 > +CFLAGS-libx86-64-isa-level-4.os += -march=x86-64 > + > +# The test modules are parameterized by preprocessor macros. > +LDFLAGS-libx86-64-isa-level-1.so += -Wl,-soname,libx86-64-isa-level.so > +LDFLAGS-libx86-64-isa-level-4.so += -Wl,-soname,libx86-64-isa-level.so > +$(objpfx)libx86-64-isa-level%.os: $(..)/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > + $(compile-command.c) -DVALUE=$(lastword $(subst -, ,$*)) \ > + -DISA_LEVEL="(1 << ($(lastword $(subst -, ,$*)) - 1))" > +$(objpfx)libx86-64-isa-level.so: $(objpfx)libx86-64-isa-level-1.so > + cp $< $@ > +endif > +endif # $(subdir) == elf Ok. > diff --git a/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > new file mode 100644 > index 0000000000..fe91bfd224 > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/x86_64/tst-glibc-hwcaps-2.c > @@ -0,0 +1,84 @@ > +/* Check ISA level on shared object in glibc-hwcaps subdirectories. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <stdbool.h> > +#include <stdlib.h> > +#include <stdio.h> > +#include <elf.h> > +#include <get-isa-level.h> > +#include <support/check.h> > +#include <support/test-driver.h> > + > +extern int dso_isa_level (void); > + > +static int > +do_test (void) > +{ > + const struct cpu_features *cpu_features > + = __x86_get_cpu_features (COMMON_CPUID_INDEX_MAX); > + unsigned int isa_level = get_isa_level (cpu_features); > + bool has_isa_baseline = ((isa_level & GNU_PROPERTY_X86_ISA_1_BASELINE) > + == GNU_PROPERTY_X86_ISA_1_BASELINE); > + bool has_isa_v2 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V2) > + == GNU_PROPERTY_X86_ISA_1_V2); > + bool has_isa_v3 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V3) > + == GNU_PROPERTY_X86_ISA_1_V3); > + bool has_isa_v4 = ((isa_level & GNU_PROPERTY_X86_ISA_1_V4) > + == GNU_PROPERTY_X86_ISA_1_V4); > + > + if (!has_isa_baseline) > + return EXIT_FAILURE; > + > + int level = dso_isa_level (); > + int ret; > + switch (level) > + { > + case 1: > + case 2: > + /* The default libx86-64-isa-level.so is used. */ > + printf ("The default shared library is used.\n"); > + if (has_isa_v3 || has_isa_v4 || !has_isa_v2) > + ret = EXIT_FAILURE; > + else > + ret = EXIT_SUCCESS; > + break; > + case 3: > + /* libx86-64-isa-level.so marked as x86-64 ISA level 3 needed in > + x86-64-v2 should be ignored on lesser CPU. */ > + printf ("x86-64 ISA level 3 shared library is used.\n"); > + if (has_isa_v4 || !has_isa_v3) > + ret = EXIT_FAILURE; > + else > + ret = EXIT_SUCCESS; > + break; > + case 4: > + /* libx86-64-isa-level.so marked as x86-64 ISA level 4 needed in > + x86-64-v3 should be ignored on lesser CPU. */ > + printf ("x86-64 ISA level 4 shared library is used.\n"); > + if (has_isa_v4) > + ret = EXIT_SUCCESS; > + else > + ret = EXIT_FAILURE; > + break; > + default: > + abort (); > + } > + return ret; > +} > + > +#include <support/test-driver.c> Ok. > diff --git a/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > new file mode 100644 > index 0000000000..2813d627cc > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/x86_64/x86-64-isa-level-VALUE.c > @@ -0,0 +1,4 @@ > +#define INCLUDE_X86_ISA_LEVEL > +#define MARKER dso_isa_level > +#include <isa-level.c> > +#include <markermodMARKER-VALUE.c> > diff --git a/sysdeps/x86/dl-isa-level.h b/sysdeps/x86/dl-isa-level.h > new file mode 100644 > index 0000000000..afe390c365 > --- /dev/null > +++ b/sysdeps/x86/dl-isa-level.h > @@ -0,0 +1,31 @@ > +/* Support for reading ISA level in /etc/ld.so.cache files written by > + Linux ldconfig. x86 version. > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <https://www.gnu.org/licenses/>. */ > + > +#include <sys/platform/x86.h> > + > +/* Return true if the ISA level in ENTRY is compatible with CPU. */ > +static inline bool > +dl_cache_hwcap_isa_level_compatible (struct file_entry_new *entry) > +{ > + const struct cpu_features *cpu_features = __get_cpu_features (); > + unsigned int isa_level > + = 1 << ((entry->hwcap >> 32) & DL_CACHE_HWCAP_ISA_LEVEL_MASK); > + > + return (isa_level & cpu_features->isa_1) == isa_level; > +} > -- > 2.29.2 Ok. ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: V2 [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2021-01-13 13:47 ` Adhemerval Zanella @ 2021-01-13 14:12 ` H.J. Lu 0 siblings, 0 replies; 27+ messages in thread From: H.J. Lu @ 2021-01-13 14:12 UTC (permalink / raw) To: Adhemerval Zanella; +Cc: Florian Weimer, H.J. Lu via Libc-alpha On Wed, Jan 13, 2021 at 5:47 AM Adhemerval Zanella <adhemerval.zanella@linaro.org> wrote: > > > > On 12/01/2021 20:32, H.J. Lu wrote: > > On Tue, Jan 12, 2021 at 8:25 AM Adhemerval Zanella > > <adhemerval.zanella@linaro.org> wrote: > >> > >> > >> > >> On 09/12/2020 00:27, H.J. Lu via Libc-alpha wrote: > >>> On Tue, Dec 8, 2020 at 7:31 AM H.J. Lu <hjl.tools@gmail.com> wrote: > >>> @@ -850,6 +859,7 @@ struct aux_cache_entry > >>> struct aux_cache_entry_id id; > >>> int flags; > >>> unsigned int osversion; > >>> + unsigned int isa_level; > >>> int used; > >>> char *soname; > >>> struct aux_cache_entry *next; > >>> @@ -863,7 +873,7 @@ struct aux_cache_file_entry > >>> int32_t flags; /* This is 1 for an ELF library. */ > >>> uint32_t soname; /* String table indice. */ > >>> uint32_t osversion; /* Required OS version. */ > >>> - int32_t pad; > >>> + uint32_t isa_level; /* Required ISA level. */ > >>> }; > >>> > >> > >> I think it would be good to have a _Static_assert with the > >> expected aux_cache_entry size. > > > > What would that be? aux_cache_entry_id size is increased > > by 4 bytes with my patch. > > My understanding is aux_cache_entry_id keeps the same and you are using > an unused field on *aux_cache_file_entry* to add the isa_level. Their > sizes should kept the same (since isa_level is the same size of the > previous pad). > > But the _Static_assert might indeed unrequired, the load_aux_cache already > check if the aux_cache_file_entry does match the file size and uses an > empty auxiliary cache otherwise (init_aux_cache). > > elf/cache.c > > 1008 if (aux_cache == MAP_FAILED > 1009 || aux_cache_size < sizeof (struct aux_cache_file) > 1010 || memcmp (aux_cache->magic, AUX_CACHEMAGIC, sizeof AUX_CACHEMAGIC - 1) > 1011 || aux_cache_size != (sizeof (struct aux_cache_file) > 1012 + aux_cache->nlibs * sizeof (struct aux_cache_file_entry) > 1013 + aux_cache->len_strings)) > 1014 { > 1015 close (fd); > 1016 init_aux_cache (); > 1017 return; > 1018 } > > > >>> /* Return true if the ENTRY->hwcap value indicates that > >>> DL_CACHE_HWCAP_EXTENSION is used. */ > >>> static inline bool > >>> dl_cache_hwcap_extension (struct file_entry_new *entry) > >>> { > >>> - /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits as well, this > >>> - is a different kind of extension. */ > >>> - return (entry->hwcap >> 32) == (DL_CACHE_HWCAP_EXTENSION >> 32); > >>> + /* If DL_CACHE_HWCAP_EXTENSION is set, but other bits, except for the > >>> + ISA level bits, as well, this is a different kind of extension. */ > >>> + return (((entry->hwcap >> 32) & ~DL_CACHE_HWCAP_ISA_LEVEL_MASK) > >>> + == (DL_CACHE_HWCAP_EXTENSION >> 32)); > >>> } > >>> > >>> /* See flags member of struct cache_file_new below. */ > >> > >> The comment sounds confusing. > > > > I changed it to > > > > /* This is an hwcap extension if only the DL_CACHE_HWCAP_EXTENSION bit > > is set, ignoring the lower 32 bits as well as the ISA level bits in > > the upper 32 bits. */ > > Thanks. > > --- > > From 40bacb7470f0c8d8eabd4d3b24057566d45ca5dd Mon Sep 17 00:00:00 2001 > > From: "H.J. Lu" <hjl.tools@gmail.com> > > Date: Sat, 5 Dec 2020 07:01:58 -0800 > > Subject: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache > > > > Store ISA level in the portion of the unused upper 32 bits of the hwcaps > > field in cache and the unused pad field in aux cache. ISA level is stored > > and checked only for shared objects in glibc-hwcaps subdirectories. The > > shared objects in the default directories aren't checked since there are > > no fallbacks for these shared objects. > > > > Tested on x86-64-v2, x86-64-v3 and x86-64-v4 machines with > > --disable-hardcoded-path-in-tests and --enable-hardcoded-path-in-tests. > > LGTM, thanks. > > Please update the release page once you commit it. Done. Thanks. > Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org> > > -- H.J. ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2020-12-09 3:27 ` [PATCH] ldconfig/x86: Store ISA level in cache and aux cache H.J. Lu 2021-01-12 16:25 ` Adhemerval Zanella @ 2021-01-28 20:20 ` Florian Weimer 2021-01-28 20:43 ` H.J. Lu 1 sibling, 1 reply; 27+ messages in thread From: Florian Weimer @ 2021-01-28 20:20 UTC (permalink / raw) To: H.J. Lu via Libc-alpha * H. J. Lu via Libc-alpha: > Subject: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache > > Store ISA level in the portion of the unused upper 32 bits of the hwcaps > field in cache and the unused pad field in aux cache. ISA level is stored > and checked only for shared objects in glibc-hwcaps subdirectories. The > shared objects in the default directories aren't checked since there are > no fallbacks for these shared objects. Why is it necessary to duplicate the ISA levels in the cache when they are implied by the subdirectory name? I would have expected to store the ISA levels in the cache for DSOs *not* in glibc-hwcaps subdirectories. The redundancy does not make sense to me. ldconfig checking for matches between ISA levels and subdirectory names makes sense too. Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2021-01-28 20:20 ` Florian Weimer @ 2021-01-28 20:43 ` H.J. Lu 2021-01-29 8:56 ` Florian Weimer 0 siblings, 1 reply; 27+ messages in thread From: H.J. Lu @ 2021-01-28 20:43 UTC (permalink / raw) To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha On Thu, Jan 28, 2021 at 12:20 PM Florian Weimer <fweimer@redhat.com> wrote: > > * H. J. Lu via Libc-alpha: > > > Subject: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache > > > > Store ISA level in the portion of the unused upper 32 bits of the hwcaps > > field in cache and the unused pad field in aux cache. ISA level is stored > > and checked only for shared objects in glibc-hwcaps subdirectories. The > > shared objects in the default directories aren't checked since there are > > no fallbacks for these shared objects. > > Why is it necessary to duplicate the ISA levels in the cache when they > are implied by the subdirectory name? > > I would have expected to store the ISA levels in the cache for DSOs > *not* in glibc-hwcaps subdirectories. The redundancy does not make > sense to me. > > ldconfig checking for matches between ISA levels and subdirectory names > makes sense too. > A v4 DSO may be placed in v2 subdirectory by mistake. -- H.J. ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2021-01-28 20:43 ` H.J. Lu @ 2021-01-29 8:56 ` Florian Weimer 2021-01-29 12:44 ` H.J. Lu 0 siblings, 1 reply; 27+ messages in thread From: Florian Weimer @ 2021-01-29 8:56 UTC (permalink / raw) To: H.J. Lu; +Cc: H.J. Lu via Libc-alpha * H. J. Lu: > A v4 DSO may be placed in v2 subdirectory by mistake. Right, and makes sense for ldconfig to warn about that. But I don't see why this data has to be stored in the cache separately, when it's already implied by the subdirectory name. Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH] ldconfig/x86: Store ISA level in cache and aux cache 2021-01-29 8:56 ` Florian Weimer @ 2021-01-29 12:44 ` H.J. Lu 0 siblings, 0 replies; 27+ messages in thread From: H.J. Lu @ 2021-01-29 12:44 UTC (permalink / raw) To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha On Fri, Jan 29, 2021 at 12:56 AM Florian Weimer <fweimer@redhat.com> wrote: > > * H. J. Lu: > > > A v4 DSO may be placed in v2 subdirectory by mistake. > > Right, and makes sense for ldconfig to warn about that. But I don't see > why this data has to be stored in the cache separately, when it's > already implied by the subdirectory name. > With this patch: diff --git a/elf/cache.c b/elf/cache.c index c01d302072..ad644dae58 100644 --- a/elf/cache.c +++ b/elf/cache.c @@ -810,7 +810,7 @@ add_to_cache (const char *path, const char *filename, const char *soname, new_entry->path = path_interned; new_entry->flags = flags; new_entry->osversion = osversion; - new_entry->isa_level = isa_level; + new_entry->isa_level = isa_level && 0; new_entry->hwcap = hwcap; new_entry->hwcaps = hwcaps; new_entry->bits_hwcap = 0; @@ -982,7 +982,7 @@ insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, entry->id = *id; entry->flags = flags; entry->osversion = osversion; - entry->isa_level = isa_level; + entry->isa_level = isa_level && 0; entry->used = used; if (soname != NULL) entry->soname = memcpy ((char *) (entry + 1), soname, len); I got FAIL: elf/tst-glibc-hwcaps-2-cache on Coffee Lake since the misplaced DSO is used. -- H.J. ^ permalink raw reply [flat|nested] 27+ messages in thread
end of thread, other threads:[~2021-01-29 12:45 UTC | newest] Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-12-06 14:49 V5 [PATCH 0/2] x86: Support GNU_PROPERTY_X86_ISA_1_NEEDED marker [BZ #26717] H.J. Lu 2020-12-06 14:49 ` V5 [PATCH 1/2] x86: Support GNU_PROPERTY_X86_ISA_1_V[234] " H.J. Lu 2021-01-06 12:00 ` Adhemerval Zanella 2021-01-06 17:11 ` V6 " H.J. Lu 2021-01-07 20:09 ` Adhemerval Zanella 2021-01-07 20:58 ` H.J. Lu 2021-01-18 11:17 ` Florian Weimer 2021-01-18 13:49 ` H.J. Lu 2021-01-18 15:15 ` Florian Weimer 2020-12-06 14:49 ` V5 [PATCH 2/2] ldconfig/x86: Add ISA level check to glibc-hwcaps H.J. Lu 2020-12-07 10:00 ` Florian Weimer 2020-12-08 13:25 ` H.J. Lu 2020-12-08 13:31 ` Florian Weimer 2020-12-08 14:10 ` H.J. Lu 2020-12-08 14:18 ` Florian Weimer 2020-12-08 14:23 ` H.J. Lu 2020-12-08 15:15 ` Florian Weimer 2020-12-08 15:31 ` H.J. Lu 2020-12-09 3:27 ` [PATCH] ldconfig/x86: Store ISA level in cache and aux cache H.J. Lu 2021-01-12 16:25 ` Adhemerval Zanella 2021-01-12 23:32 ` V2 " H.J. Lu 2021-01-13 13:47 ` Adhemerval Zanella 2021-01-13 14:12 ` H.J. Lu 2021-01-28 20:20 ` Florian Weimer 2021-01-28 20:43 ` H.J. Lu 2021-01-29 8:56 ` Florian Weimer 2021-01-29 12:44 ` H.J. Lu
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).