Skip to content

Commit d3ca6fd

Browse files
committed
Enable static-pie for the x86_64-unknown-linux-musl target
Fixes: rust-lang#70693
1 parent 4fb54ed commit d3ca6fd

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)