Auto merge of #126715 - Rejyr:migrate-readelf-rmake, r=jieyouxu · rust-lang/rust@d4cc01c (original) (raw)
``
1
`+
// How to manually run this
`
``
2
`+
// $ ./x.py test --target x86_64-unknown-linux-[musl,gnu] tests/run-make/static-pie
`
``
3
+
``
4
`+
//@ only-x86_64
`
``
5
`+
//@ only-linux
`
``
6
`+
//@ ignore-32bit
`
``
7
+
``
8
`+
use std::process::Command;
`
``
9
+
``
10
`+
use run_make_support::llvm_readobj;
`
``
11
`+
use run_make_support::regex::Regex;
`
``
12
`+
use run_make_support::rustc;
`
``
13
`+
use run_make_support::{cmd, run_with_args, target};
`
``
14
+
``
15
`+
// Minimum major versions supporting -static-pie
`
``
16
`+
const GCC_VERSION: u32 = 8;
`
``
17
`+
const CLANG_VERSION: u32 = 9;
`
``
18
+
``
19
`` +
// Return true
if the compiler
version supports -static-pie
.
``
``
20
`+
fn ok_compiler_version(compiler: &str) -> bool {
`
``
21
`+
let (trigger, version_threshold) = match compiler {
`
``
22
`+
"clang" => ("clang_major", CLANG_VERSION),
`
``
23
`+
"gcc" => ("GNUC", GCC_VERSION),
`
``
24
`+
other => panic!("unexpected compiler '{other}', expected 'clang' or 'gcc'"),
`
``
25
`+
};
`
``
26
+
``
27
`+
if Command::new(compiler).spawn().is_err() {
`
``
28
`+
eprintln!("No {compiler} version detected");
`
``
29
`+
return false;
`
``
30
`+
}
`
``
31
+
``
32
`+
let compiler_output =
`
``
33
`+
cmd(compiler).stdin(trigger).arg("-").arg("-E").arg("-x").arg("c").run().stdout_utf8();
`
``
34
`+
let re = Regex::new(r"(?m)^(\d+)").unwrap();
`
``
35
`+
let version: u32 =
`
``
36
`+
re.captures(&compiler_output).unwrap().get(1).unwrap().as_str().parse().unwrap();
`
``
37
+
``
38
`+
if version >= version_threshold {
`
``
39
`+
eprintln!("{compiler} supports -static-pie");
`
``
40
`+
true
`
``
41
`+
} else {
`
``
42
`+
eprintln!("{compiler} too old to support -static-pie, skipping test");
`
``
43
`+
false
`
``
44
`+
}
`
``
45
`+
}
`
``
46
+
``
47
`+
fn test(compiler: &str) {
`
``
48
`+
if !ok_compiler_version(compiler) {
`
``
49
`+
return;
`
``
50
`+
}
`
``
51
+
``
52
`+
rustc()
`
``
53
`+
.input("test-aslr.rs")
`
``
54
`+
.target(&target())
`
``
55
`+
.linker(compiler)
`
``
56
`+
.arg("-Clinker-flavor=gcc")
`
``
57
`+
.arg("-Ctarget-feature=+crt-static")
`
``
58
`+
.run();
`
``
59
+
``
60
`+
llvm_readobj()
`
``
61
`+
.symbols()
`
``
62
`+
.input("test-aslr")
`
``
63
`+
.run()
`
``
64
`+
.assert_stdout_not_contains("INTERP")
`
``
65
`+
.assert_stdout_contains("DYNAMIC");
`
``
66
+
``
67
`+
run_with_args("test-aslr", &["--test-aslr"]);
`
``
68
`+
}
`
``
69
+
``
70
`+
fn main() {
`
``
71
`+
test("clang");
`
``
72
`+
test("gcc");
`
``
73
`+
}
`