Skip to content

Commit 27d4737

Browse files
authored
Rollup merge of #70740 - haraldh:static-pie, r=petrochenkov
Enabling static-pie for musl and make it the default for the x86_64-unknown-linux-musl target This is a quick implementation for #70693 Opening it as a draft PR to gather some feedback, before I put more work in it. ```console ❯ cat hello.rs fn main() { println!("main = {:#x}", &main as *const _ as usize); } ❯ /tmp/rust-musl/bin/rustc --target x86_64-unknown-linux-musl ~/hello.rs ❯ ldd hello statically linked ❯ file hello hello: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=fec5cdc170f503a712a63a6958691ce5ce433654, with debug_info, not stripped ❯ ./hello main = 0x7f233ca30008 ❯ ./hello main = 0x7f9ddc529008 ❯ ./hello main = 0x7f1e5a224008 ❯ ./hello main = 0x7f4485c7c008 ❯ /tmp/rust-musl/bin/rustc --target x86_64-unknown-linux-musl -Z print-link-args ~/hello.rs "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-m64" "-nostdlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/rcrt1.o" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/crti.o" "-L" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "hello.hello.7rcbfp3g-cgu.0.rcgu.o" "hello.hello.7rcbfp3g-cgu.1.rcgu.o" "hello.hello.7rcbfp3g-cgu.2.rcgu.o" "hello.hello.7rcbfp3g-cgu.3.rcgu.o" "hello.hello.7rcbfp3g-cgu.4.rcgu.o" "hello.hello.7rcbfp3g-cgu.5.rcgu.o" "-o" "hello" "hello.1nxjf9so94czdgcz.rcgu.o" "-Wl,--gc-sections" "-static-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-0f9cb7646f9e2c34.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-ba857f2f2e4e7187.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-58ba5e25bbdf9d29.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-886bfe43afa847dc.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace-fbfb8fe99f19a67b.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace_sys-85fa859e7d364cc9.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-07ab026cd3ec0d82.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-a8ec5932d92ea864.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-0ba4cc2f38a198d5.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-c1bb2b3ce4f78b7c.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-0ff673c1cf0d451a.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-c8ff2001db856926.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-2ae14177140eeca2.rlib" "-Wl,--end-group" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-4fd81b5ce1b08a9c.rlib" "-static" "-Wl,-Bdynamic" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/crtn.o" ``` Closes #70693 Closes #53968
2 parents a39c778 + d3ca6fd commit 27d4737

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/librustc_target/spec/x86_64_unknown_linux_musl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub fn target() -> TargetResult {
66
base.max_atomic_width = Some(64);
77
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m64".to_string());
88
base.stack_probes = true;
9+
base.static_position_independent_executables = true;
910

1011
Ok(Target {
1112
llvm_target: "x86_64-unknown-linux-musl".to_string(),

src/test/run-make/static-pie/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
# only-x86_64-unknown-linux-musl
4+
5+
# 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
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const NUM_RUNS: usize = 10;
2+
3+
fn run_self(exe: &str) -> usize {
4+
use std::process::Command;
5+
let mut set = std::collections::HashSet::new();
6+
7+
let mut cmd = Command::new(exe);
8+
cmd.arg("--report");
9+
(0..NUM_RUNS).for_each(|_| {
10+
set.insert(cmd.output().expect("failed to execute process").stdout);
11+
});
12+
set.len()
13+
}
14+
15+
fn main() {
16+
let mut args = std::env::args();
17+
let arg0 = args.next().unwrap();
18+
match args.next() {
19+
Some(s) if s.eq("--report") => {
20+
println!("main = {:#?}", &main as *const _);
21+
}
22+
Some(s) if s.eq("--test-no-aslr") => {
23+
let cnt = run_self(&arg0);
24+
if cnt != 1 {
25+
eprintln!("FAIL: {} most likely ASLR", arg0);
26+
std::process::exit(1);
27+
}
28+
println!("PASS: {} does no ASLR", arg0);
29+
}
30+
Some(s) if s.eq("--test-aslr") => {
31+
let cnt = run_self(&arg0);
32+
if cnt != NUM_RUNS {
33+
eprintln!("FAIL: {} most likely no ASLR", arg0);
34+
std::process::exit(1);
35+
}
36+
println!("PASS: {} does ASLR", arg0);
37+
}
38+
Some(_) | None => {
39+
println!("Usage: {} --test-no-aslr | --test-aslr", arg0);
40+
std::process::exit(1);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)