Skip to content

Commit

Permalink
Makefiles: Add noexecstack Options to Compilation and Linking
Browse files Browse the repository at this point in the history
Hopefully this marks the binary artifacts `noexecstack` even on platforms
where binaries default to true.
  • Loading branch information
felixhandte committed Jan 5, 2022
1 parent 35208f7 commit 4620ce6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 28 additions & 0 deletions lib/libzstd.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 := \#

Expand Down Expand Up @@ -96,6 +98,32 @@ 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)
$(info Supports noexecstack linker flag!)
$(info $(LDFLAGS))
LDFLAGS += -z noexecstack
$(info $(LDFLAGS))
else
$(info Doesn't support noexecstack linker flag!)
endif
ifeq ($(shell echo | $(CC) $(FLAGS) -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
$(info Supports noexecstack assembler flag!)
$(info $(CFLAGS))
CFLAGS += -Wa,--noexecstack
$(info $(CFLAGS))
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
$(info Supports noexecstack assembler flag with unused arg suppression!)
$(info $(CFLAGS))
CFLAGS += -Qunused-arguments -Wa,--noexecstack
$(info $(CFLAGS))
else
$(info Doesn't support noexecstack assembler flag!)
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)
Expand Down
2 changes: 0 additions & 2 deletions programs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pthread.h>\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)
Expand Down

0 comments on commit 4620ce6

Please sign in to comment.