From 8ea3d57de4bcff2170296e0d1a5019f030630f3b Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Tue, 18 Jan 2022 14:28:43 -0800 Subject: [PATCH] [build][asm] Pass ASFLAGS to the assembler instead of CFLAGS * Add `-Wa,--noexecstack` to both `ASFLAGS` and `CFLAGS` * Pass `ASFLAGS` to `.S` compilation instead of `CFLAGS` Fixes #3006. --- contrib/linux-kernel/test/Makefile | 2 +- lib/Makefile | 4 ++-- lib/libzstd.mk | 9 ++++++--- programs/Makefile | 2 +- tests/Makefile | 4 ++-- tests/fuzz/Makefile | 6 ++++-- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/contrib/linux-kernel/test/Makefile b/contrib/linux-kernel/test/Makefile index dc76a5f401c..be82b3fbac8 100644 --- a/contrib/linux-kernel/test/Makefile +++ b/contrib/linux-kernel/test/Makefile @@ -24,7 +24,7 @@ LINUX_ZSTD_OBJECTS0 := $(LINUX_ZSTD_FILES:.c=.o) LINUX_ZSTD_OBJECTS := $(LINUX_ZSTD_OBJECTS0:.S=.o) %.o: %.S - $(CC) -c $(CPPFLAGS) $(CFLAGS) $^ -o $@ + $(COMPILE.S) $(OUTPUT_OPTION) $< liblinuxzstd.a: $(LINUX_ZSTD_OBJECTS) $(AR) $(ARFLAGS) $@ $^ diff --git a/lib/Makefile b/lib/Makefile index 00fc535b0e9..ef202183d84 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -212,11 +212,11 @@ $(ZSTD_STATLIB_DIR)/%.o : %.c $(ZSTD_STATLIB_DIR)/%.d | $(ZSTD_STATLIB_DIR) $(ZSTD_DYNLIB_DIR)/%.o : %.S | $(ZSTD_DYNLIB_DIR) @echo AS $@ - $(COMPILE.c) $(OUTPUT_OPTION) $< + $(COMPILE.S) $(OUTPUT_OPTION) $< $(ZSTD_STATLIB_DIR)/%.o : %.S | $(ZSTD_STATLIB_DIR) @echo AS $@ - $(COMPILE.c) $(OUTPUT_OPTION) $< + $(COMPILE.S) $(OUTPUT_OPTION) $< MKDIR ?= mkdir $(BUILD_DIR) $(ZSTD_DYNLIB_DIR) $(ZSTD_STATLIB_DIR): diff --git a/lib/libzstd.mk b/lib/libzstd.mk index 73a8cd5d860..6e9a643954b 100644 --- a/lib/libzstd.mk +++ b/lib/libzstd.mk @@ -95,8 +95,9 @@ DEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \ -Wredundant-decls -Wmissing-prototypes -Wc++-compat CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) +ASFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) $(CFLAGS) LDFLAGS += $(MOREFLAGS) -FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) +FLAGS = $(CPPFLAGS) $(CFLAGS) $(ASFLAGS) $(LDFLAGS) ifndef ALREADY_APPENDED_NOEXECSTACK export ALREADY_APPENDED_NOEXECSTACK := 1 @@ -104,10 +105,12 @@ ifeq ($(shell echo "int main(int argc, char* argv[]) { (void)argc; (void)argv; r 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 +CFLAGS += -Wa,--noexecstack +# CFLAGS are also added to ASFLAGS 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 +CFLAGS += -Qunused-arguments -Wa,--noexecstack +# CFLAGS are also added to ASFLAGS endif endif diff --git a/programs/Makefile b/programs/Makefile index da848eb66bc..f77e1b7f10f 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -320,7 +320,7 @@ $(BUILD_DIR)/%.o : %.c $(BUILD_DIR)/%.d | $(BUILD_DIR) $(BUILD_DIR)/%.o : %.S | $(BUILD_DIR) @echo AS $@ - $(COMPILE.c) $(OUTPUT_OPTION) $< + $(COMPILE.S) $(OUTPUT_OPTION) $< MKDIR ?= mkdir $(BUILD_DIR): ; $(MKDIR) -p $@ diff --git a/tests/Makefile b/tests/Makefile index efcbe9761e9..132fa7a0818 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -118,7 +118,7 @@ zstdd_%.o : $(ZSTDDIR)/decompress/%.c $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ zstdd_%.o : $(ZSTDDIR)/decompress/%.S - $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ + $(CC) -c $(CPPFLAGS) $(ASFLAGS) $< -o $@ zstdmt%.o : CPPFLAGS += $(MULTITHREAD_CPP) @@ -132,7 +132,7 @@ zstdmt_d_%.o : $(ZSTDDIR)/decompress/%.c $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ zstdmt_d_%.o : $(ZSTDDIR)/decompress/%.S - $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ + $(CC) -c $(CPPFLAGS) $(ASFLAGS) $< -o $@ fullbench32: CPPFLAGS += -m32 fullbench fullbench32 : CPPFLAGS += $(MULTITHREAD_CPP) -Wno-deprecated-declarations diff --git a/tests/fuzz/Makefile b/tests/fuzz/Makefile index b707bd95176..912348c3843 100644 --- a/tests/fuzz/Makefile +++ b/tests/fuzz/Makefile @@ -12,6 +12,7 @@ CFLAGS ?= -O3 CXXFLAGS ?= -O3 CPPFLAGS ?= +ASFLAGS ?= LDFLAGS ?= ARFLAGS ?= LIB_FUZZING_ENGINE ?= libregression.a @@ -43,6 +44,7 @@ FUZZ_EXTRA_FLAGS := -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ -Wredundant-decls -Wno-deprecated-declarations \ -g -fno-omit-frame-pointer FUZZ_CFLAGS := $(FUZZ_EXTRA_FLAGS) $(CFLAGS) +FUZZ_ASFLAGS := $(FUZZ_EXTRA_FLAGS) $(ASFLAGS) FUZZ_CXXFLAGS := $(FUZZ_EXTRA_FLAGS) -std=c++11 $(CXXFLAGS) FUZZ_LDFLAGS := -pthread $(LDFLAGS) FUZZ_ARFLAGS := $(ARFLAGS) @@ -127,7 +129,7 @@ rt_lib_decompress_%.o: $(ZSTDDIR)/decompress/%.c $(CC) $(FUZZ_CPPFLAGS) $(FUZZ_CFLAGS) $(FUZZ_ROUND_TRIP_FLAGS) $< -c -o $@ rt_lib_decompress_%.o: $(ZSTDDIR)/decompress/%.S - $(CC) $(FUZZ_CPPFLAGS) $(FUZZ_CFLAGS) $(FUZZ_ROUND_TRIP_FLAGS) $< -c -o $@ + $(CC) $(FUZZ_CPPFLAGS) $(FUZZ_ASFLAGS) $(FUZZ_ROUND_TRIP_FLAGS) $< -c -o $@ rt_lib_dictBuilder_%.o: $(ZSTDDIR)/dictBuilder/%.c $(CC) $(FUZZ_CPPFLAGS) $(FUZZ_CFLAGS) $(FUZZ_ROUND_TRIP_FLAGS) $< -c -o $@ @@ -151,7 +153,7 @@ d_lib_decompress_%.o: $(ZSTDDIR)/decompress/%.c $(CC) $(FUZZ_CPPFLAGS) $(FUZZ_CFLAGS) $< -c -o $@ d_lib_decompress_%.o: $(ZSTDDIR)/decompress/%.S - $(CC) $(FUZZ_CPPFLAGS) $(FUZZ_CFLAGS) $< -c -o $@ + $(CC) $(FUZZ_CPPFLAGS) $(FUZZ_ASFLAGS) $< -c -o $@ d_lib_dictBuilder_%.o: $(ZSTDDIR)/dictBuilder/%.c $(CC) $(FUZZ_CPPFLAGS) $(FUZZ_CFLAGS) $< -c -o $@