From f1b781d0d357322c2abf99b14442b962cdb6befa Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Sat, 3 Oct 2026 04:48:23 +0800 Subject: [PATCH] Limit SHA-NI compiler flags to implementation --- configure | 11 +++++++---- librhash/Makefile | 2 +- librhash/algorithms.c | 2 +- librhash/sha_ni.h | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/configure b/configure index 46072c3c..e12aa3ea 100755 --- a/configure +++ b/configure @@ -782,9 +782,11 @@ if test "$OPT_SHANI" = "auto"; then "-msse4 -msha"; then HAS_X86_SSE4_SHANI=yes - # Add all SHANI options to the LIBRHASH_OPTFLAGS, so it can be overriden by - # distro-specific OPTFLAGS, but only all together - LIBRHASH_OPTFLAGS=$(join_params $LIBRHASH_OPTFLAGS -DRHASH_SSE4_SHANI -msse4 -msha) + # Enable runtime dispatch globally, but compile only the SHA-NI + # implementation with ISA-specific flags. Other objects must remain + # runnable on CPUs without SSE4.1 and SHA-NI. + LIBRHASH_OPTFLAGS=$(join_params $LIBRHASH_OPTFLAGS -DRHASH_SSE4_SHANI) + LIBRHASH_SHANI_CFLAGS=$(join_params $LIBRHASH_SHANI_CFLAGS -msse4 -msha) fi fi finish_check "$HAS_X86_SSE4_SHANI" @@ -1013,6 +1015,7 @@ EXTRA_INSTALL_LIBSHARED = $EXTRA_INSTALL_LIBSHARED EXTRA_UNINSTALL_LIBSHARED = $EXTRA_UNINSTALL_LIBSHARED OPTFLAGS = $LIBRHASH_OPTFLAGS +SHANI_CFLAGS = $LIBRHASH_SHANI_CFLAGS OPTLDFLAGS = $WIN_LDFLAGS WARN_CFLAGS = $WARN_CFLAGS ADDCFLAGS = $BUILD_EXTRA_CFLAGS @@ -1060,4 +1063,4 @@ if test -f "${MAN_PATH}.in"; then SED_SYSCONFDIR=$(echo $INSTALL_SYSCONFDIR | sed -e 's/\([|\\&]\)/\\\1/g') echo "Writing ${MAN_PATH}" sed -e "s|@SYSCONFDIR@|$SED_SYSCONFDIR|" ${MAN_PATH}.in > ${MAN_PATH} -fi \ No newline at end of file +fi diff --git a/librhash/Makefile b/librhash/Makefile index 8fb52137..69f41922 100644 --- a/librhash/Makefile +++ b/librhash/Makefile @@ -134,7 +134,7 @@ sha1.o: sha1.c byte_order.h ustd.h sha1.h $(CC) -c $(CFLAGS) $< -o $@ sha_ni.o: sha_ni.c sha_ni.h sha1.h ustd.h sha256.h byte_order.h - $(CC) -c $(CFLAGS) $< -o $@ + $(CC) -c $(CFLAGS) $(SHANI_CFLAGS) $< -o $@ sha256.o: sha256.c byte_order.h ustd.h sha256.h $(CC) -c $(CFLAGS) $< -o $@ diff --git a/librhash/algorithms.c b/librhash/algorithms.c index 22f87f0d..12bbf56b 100644 --- a/librhash/algorithms.c +++ b/librhash/algorithms.c @@ -140,7 +140,7 @@ rhash_hash_info rhash_hash_info_default[] = { &info_blake3, sizeof(blake3_ctx), dgshft2(blake3, root.hash), iuf(rhash_blake3), 0 } /* 256 bit */ }; -#if defined(RHASH_SSE4_SHANI) && !defined(RHASH_DISABLE_SHANI) +#if defined(RHASH_SSE4_SHANI) static void table_init_sha_ext(void) { /* SHA-NI Implementation uses SHANI, SSE2, SSSE3, SSE4.1 instructions. diff --git a/librhash/sha_ni.h b/librhash/sha_ni.h index 316f6b05..fb4330ff 100644 --- a/librhash/sha_ni.h +++ b/librhash/sha_ni.h @@ -13,7 +13,7 @@ # endif #endif -#if defined(RHASH_SSE4_SHANI) && !defined(RHASH_DISABLE_SHANI) +#if defined(RHASH_SSE4_SHANI) #include "sha1.h" #include "sha256.h"