Skip to content

Commit ce6c148

Browse files
authored
Rollup merge of #86374 - bossmc:enable-static-pie-for-gnu, r=nagisa
Enable combining `+crt-static` and `relocation-model=pic` on `x86_64-unknown-linux-gnu` Modern `gcc` versions support `-static-pie`, and `rustc` will already fall-back to `-static` if the local `gcc` is too old (and hence this change is optimistic rather than absolute). This brings the `-musl` and `-gnu` targets to feature compatibility (albeit with different default settings). Of note a `-static` or `-static-pie` binary based on glibc that uses NSS-backed functions (`gethostbyname` or `getpwuid` etc.) need to have access to the `libnss_X.so.2` libraries and any of their dynamic dependencies. I wasn't sure about the `# only`/`# ignore` changes (I've not got a `gnux32` toolchain to test with hence not also enabling `-static-pie` there).
2 parents 547f2ba + 6a88311 commit ce6c148

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

Diff for: compiler/rustc_target/src/spec/x86_64_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub fn target() -> Target {
77
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m64".to_string());
88
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
99
base.stack_probes = StackProbeType::Call;
10+
base.static_position_independent_executables = true;
1011
base.supported_sanitizers = SanitizerSet::ADDRESS
1112
| SanitizerSet::CFI
1213
| SanitizerSet::LEAK

Diff for: src/test/run-make/static-pie/Makefile

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
-include ../../run-make-fulldeps/tools.mk
22

3-
# only-x86_64-unknown-linux-musl
3+
# only-x86_64
4+
# only-linux
5+
# ignore-gnux32
46

57
# How to manually run this
6-
# $ ./x.py test --target x86_64-unknown-linux-musl src/test/run-make/static-pie
7-
8-
all:
9-
$(RUSTC) --target $(TARGET) -C target-feature=+crt-static test-aslr.rs
10-
# Check that no dynamic interpreter is set
11-
! readelf -l $(call RUN_BINFILE,test-aslr) | $(CGREP) INTERP
12-
# Check that we have a dynamic executable
13-
readelf -l $(call RUN_BINFILE,test-aslr) | $(CGREP) DYNAMIC
14-
# Check for address space layout randomization
15-
$(call RUN,test-aslr) --test-aslr
8+
# $ ./x.py test --target x86_64-unknown-linux-[musl,gnu] src/test/run-make/static-pie
9+
10+
all: test-clang test-gcc
11+
12+
test-%:
13+
if ./check_$*_version.sh; then\
14+
${RUSTC} -Clinker=$* -Clinker-flavor=gcc --target ${TARGET} -C target-feature=+crt-static test-aslr.rs; \
15+
! readelf -l $(call RUN_BINFILE,test-aslr) | $(CGREP) INTERP; \
16+
readelf -l $(call RUN_BINFILE,test-aslr) | $(CGREP) DYNAMIC; \
17+
$(call RUN,test-aslr) --test-aslr; \
18+
fi

Diff for: src/test/run-make/static-pie/check_clang_version.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
if command -v clang > /dev/null
6+
then
7+
CLANG_VERSION=$(echo __clang_major__ | clang -E -x c - | grep -v -e '^#' )
8+
echo "clang version $CLANG_VERSION detected"
9+
if (( $CLANG_VERSION >= 9 ))
10+
then
11+
echo "clang supports -static-pie"
12+
exit 0
13+
else
14+
echo "clang too old to support -static-pie, skipping test"
15+
exit 1
16+
fi
17+
else
18+
echo "No clang version detected"
19+
exit 2
20+
fi

Diff for: src/test/run-make/static-pie/check_gcc_version.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
if command -v gcc > /dev/null
6+
then
7+
GCC_VERSION=$(echo __GNUC__ | gcc -E -x c - | grep -v -e '^#' )
8+
echo "gcc version $GCC_VERSION detected"
9+
if (( $GCC_VERSION >= 8 ))
10+
then
11+
echo "gcc supports -static-pie"
12+
exit 0
13+
else
14+
echo "gcc too old to support -static-pie, skipping test"
15+
exit 1
16+
fi
17+
else
18+
echo "No gcc version detected"
19+
exit 2
20+
fi

0 commit comments

Comments
 (0)