From b94da2bace66b4ba5485363ac7ba4fe85ad41578 Mon Sep 17 00:00:00 2001 From: Andy Caldwell Date: Wed, 16 Jun 2021 19:56:38 +0100 Subject: [PATCH 1/2] Enable combining +crt-static and relocation-model=pic on x86_64-unknown-linux-gnu --- compiler/rustc_target/src/spec/x86_64_unknown_linux_gnu.rs | 1 + src/test/run-make/static-pie/Makefile | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_target/src/spec/x86_64_unknown_linux_gnu.rs b/compiler/rustc_target/src/spec/x86_64_unknown_linux_gnu.rs index c2484f2d8f66d..aefbb398286ac 100644 --- a/compiler/rustc_target/src/spec/x86_64_unknown_linux_gnu.rs +++ b/compiler/rustc_target/src/spec/x86_64_unknown_linux_gnu.rs @@ -7,6 +7,7 @@ pub fn target() -> Target { base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m64".to_string()); // don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved base.stack_probes = StackProbeType::Call; + base.static_position_independent_executables = true; base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::CFI | SanitizerSet::LEAK diff --git a/src/test/run-make/static-pie/Makefile b/src/test/run-make/static-pie/Makefile index 1d3cc82138927..f5fe4bc5afee3 100644 --- a/src/test/run-make/static-pie/Makefile +++ b/src/test/run-make/static-pie/Makefile @@ -1,9 +1,11 @@ -include ../../run-make-fulldeps/tools.mk -# only-x86_64-unknown-linux-musl +# only-x86_64 +# only-linux +# ignore-gnux32 # How to manually run this -# $ ./x.py test --target x86_64-unknown-linux-musl src/test/run-make/static-pie +# $ ./x.py test --target x86_64-unknown-linux-[musl,gnu] src/test/run-make/static-pie all: $(RUSTC) --target $(TARGET) -C target-feature=+crt-static test-aslr.rs From 6a88311373bf18b272aed98d3032852cfcec4455 Mon Sep 17 00:00:00 2001 From: Andy Caldwell Date: Mon, 1 Nov 2021 18:38:11 +0000 Subject: [PATCH 2/2] Check for -static-pie support before testing support --- src/test/run-make/static-pie/Makefile | 17 ++++++++-------- .../static-pie/check_clang_version.sh | 20 +++++++++++++++++++ .../run-make/static-pie/check_gcc_version.sh | 20 +++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100755 src/test/run-make/static-pie/check_clang_version.sh create mode 100755 src/test/run-make/static-pie/check_gcc_version.sh diff --git a/src/test/run-make/static-pie/Makefile b/src/test/run-make/static-pie/Makefile index f5fe4bc5afee3..945ec1724ac04 100644 --- a/src/test/run-make/static-pie/Makefile +++ b/src/test/run-make/static-pie/Makefile @@ -7,11 +7,12 @@ # How to manually run this # $ ./x.py test --target x86_64-unknown-linux-[musl,gnu] src/test/run-make/static-pie -all: - $(RUSTC) --target $(TARGET) -C target-feature=+crt-static test-aslr.rs - # Check that no dynamic interpreter is set - ! readelf -l $(call RUN_BINFILE,test-aslr) | $(CGREP) INTERP - # Check that we have a dynamic executable - readelf -l $(call RUN_BINFILE,test-aslr) | $(CGREP) DYNAMIC - # Check for address space layout randomization - $(call RUN,test-aslr) --test-aslr +all: test-clang test-gcc + +test-%: + if ./check_$*_version.sh; then\ + ${RUSTC} -Clinker=$* -Clinker-flavor=gcc --target ${TARGET} -C target-feature=+crt-static test-aslr.rs; \ + ! readelf -l $(call RUN_BINFILE,test-aslr) | $(CGREP) INTERP; \ + readelf -l $(call RUN_BINFILE,test-aslr) | $(CGREP) DYNAMIC; \ + $(call RUN,test-aslr) --test-aslr; \ + fi diff --git a/src/test/run-make/static-pie/check_clang_version.sh b/src/test/run-make/static-pie/check_clang_version.sh new file mode 100755 index 0000000000000..b8e97c3da7d77 --- /dev/null +++ b/src/test/run-make/static-pie/check_clang_version.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -euo pipefail + +if command -v clang > /dev/null +then + CLANG_VERSION=$(echo __clang_major__ | clang -E -x c - | grep -v -e '^#' ) + echo "clang version $CLANG_VERSION detected" + if (( $CLANG_VERSION >= 9 )) + then + echo "clang supports -static-pie" + exit 0 + else + echo "clang too old to support -static-pie, skipping test" + exit 1 + fi +else + echo "No clang version detected" + exit 2 +fi diff --git a/src/test/run-make/static-pie/check_gcc_version.sh b/src/test/run-make/static-pie/check_gcc_version.sh new file mode 100755 index 0000000000000..d07e1d151dfb8 --- /dev/null +++ b/src/test/run-make/static-pie/check_gcc_version.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -euo pipefail + +if command -v gcc > /dev/null +then + GCC_VERSION=$(echo __GNUC__ | gcc -E -x c - | grep -v -e '^#' ) + echo "gcc version $GCC_VERSION detected" + if (( $GCC_VERSION >= 8 )) + then + echo "gcc supports -static-pie" + exit 0 + else + echo "gcc too old to support -static-pie, skipping test" + exit 1 + fi +else + echo "No gcc version detected" + exit 2 +fi