diff --git a/lib/decompress/huf_decompress_amd64.S b/lib/decompress/huf_decompress_amd64.S index 98173cce863..0b4cd38ee10 100644 --- a/lib/decompress/huf_decompress_amd64.S +++ b/lib/decompress/huf_decompress_amd64.S @@ -1,14 +1,14 @@ #include "../common/portability_macros.h" -#if ZSTD_ENABLE_ASM_X86_64_BMI2 - /* Stack marking * ref: https://wiki.gentoo.org/wiki/Hardened/GNU_stack_quickstart */ -#if defined(__linux__) && defined(__ELF__) +#if defined(__ELF__) && defined(__GNUC__) .section .note.GNU-stack,"",%progbits #endif +#if ZSTD_ENABLE_ASM_X86_64_BMI2 + /* Calling convention: * * %rdi contains the first argument: HUF_DecompressAsmArgs*. diff --git a/lib/libzstd.mk b/lib/libzstd.mk index c04957c6fae..73a8cd5d860 100644 --- a/lib/libzstd.mk +++ b/lib/libzstd.mk @@ -34,6 +34,8 @@ ZSTD_NO_ASM ?= 0 # libzstd helpers ################################################################## +VOID ?= /dev/null + # Make 4.3 doesn't support '\#' anymore (https://lwn.net/Articles/810071/) NUM_SYMBOL := \# @@ -96,6 +98,19 @@ CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) LDFLAGS += $(MOREFLAGS) FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) +ifndef ALREADY_APPENDED_NOEXECSTACK +export ALREADY_APPENDED_NOEXECSTACK := 1 +ifeq ($(shell echo "int main(int argc, char* argv[]) { (void)argc; (void)argv; return 0; }" | $(CC) $(FLAGS) -z noexecstack -x c -Werror - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1) +LDFLAGS += -z noexecstack +endif +ifeq ($(shell echo | $(CC) $(FLAGS) -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1) +CFLAGS += -Wa,--noexecstack +else ifeq ($(shell echo | $(CC) $(FLAGS) -Qunused-arguments -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1) +# See e.g.: https://github.com/android/ndk/issues/171 +CFLAGS += -Qunused-arguments -Wa,--noexecstack +endif +endif + HAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0) GREP_OPTIONS ?= ifeq ($HAVE_COLORNEVER, 1) diff --git a/programs/Makefile b/programs/Makefile index a54900cc1e9..da848eb66bc 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -62,8 +62,6 @@ else EXT = endif -VOID = /dev/null - # thread detection NO_THREAD_MSG := ==> no threads, building without multithreading support HAVE_PTHREAD := $(shell printf '$(NUM_SYMBOL)include \nint main(void) { return 0; }' > have_pthread.c && $(CC) $(FLAGS) -o have_pthread$(EXT) have_pthread.c -pthread 2> $(VOID) && rm have_pthread$(EXT) && echo 1 || echo 0; rm have_pthread.c) diff --git a/tests/playTests.sh b/tests/playTests.sh index a772b61ff7c..695d4333ba4 100755 --- a/tests/playTests.sh +++ b/tests/playTests.sh @@ -304,6 +304,13 @@ zstd -d -f tmp_corrupt.zst --no-check zstd -d -f tmp_corrupt.zst --check --no-check # final flag overrides zstd -d -f tmp.zst --no-check +if [ "$isWindows" = false ]; then + if [ -n "$(which readelf)" ]; then + println "test: check if binary has executable stack (#2963)" + readelf -lW "$ZSTD_BIN" | grep 'GNU_STACK .* RW ' || die "zstd binary has executable stack!" + fi +fi + println "\n===> zstdgrep tests" ln -sf "$ZSTD_BIN" zstdcat rm -f tmp_grep