Skip to content

Commit d4cc01c

Browse files
committed
Auto merge of rust-lang#126715 - Rejyr:migrate-readelf-rmake, r=jieyouxu
Migrate `relro-levels`, `static-pie` to `rmake` Part of rust-lang#121876. r? `@jieyouxu` try-job: aarch64-gnu try-job: arm-android try-job: armhf-gnu try-job: dist-i586-gnu-i586-i686-musl try-job: dist-various-1 try-job: test-various
2 parents acb6273 + a19077d commit d4cc01c

File tree

11 files changed

+126
-87
lines changed

11 files changed

+126
-87
lines changed

src/tools/run-make-support/src/command.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ impl Command {
3636
Self { cmd: StdCommand::new(program), stdin: None, drop_bomb: DropBomb::arm(program) }
3737
}
3838

39-
pub fn set_stdin(&mut self, stdin: Box<[u8]>) {
40-
self.stdin = Some(stdin);
39+
/// Specify a stdin input
40+
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
41+
self.stdin = Some(input.as_ref().to_vec().into_boxed_slice());
42+
self
4143
}
4244

4345
/// Specify an environment variable.

src/tools/run-make-support/src/llvm.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ impl LlvmReadobj {
102102
self
103103
}
104104

105+
/// Pass `--program-headers` to display program headers.
106+
pub fn program_headers(&mut self) -> &mut Self {
107+
self.cmd.arg("--program-headers");
108+
self
109+
}
110+
111+
/// Pass `--symbols` to display the symbol.
112+
pub fn symbols(&mut self) -> &mut Self {
113+
self.cmd.arg("--symbols");
114+
self
115+
}
116+
117+
/// Pass `--dynamic-table` to display the dynamic symbol table.
118+
pub fn dynamic_table(&mut self) -> &mut Self {
119+
self.cmd.arg("--dynamic-table");
120+
self
121+
}
122+
105123
/// Specify the section to display.
106124
pub fn section(&mut self, section: &str) -> &mut Self {
107125
self.cmd.arg("--string-dump");
@@ -153,7 +171,7 @@ impl LlvmFilecheck {
153171

154172
/// Pipe a read file into standard input containing patterns that will be matched against the .patterns(path) call.
155173
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
156-
self.cmd.set_stdin(input.as_ref().to_vec().into_boxed_slice());
174+
self.cmd.stdin(input);
157175
self
158176
}
159177

src/tools/run-make-support/src/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl Rustc {
244244

245245
/// Specify a stdin input
246246
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
247-
self.cmd.set_stdin(input.as_ref().to_vec().into_boxed_slice());
247+
self.cmd.stdin(input);
248248
self
249249
}
250250

src/tools/run-make-support/src/rustdoc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl Rustdoc {
9292

9393
/// Specify a stdin input
9494
pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
95-
self.cmd.set_stdin(input.as_ref().to_vec().into_boxed_slice());
95+
self.cmd.stdin(input);
9696
self
9797
}
9898

src/tools/tidy/src/allowed_run_make_makefiles.txt

-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ run-make/raw-dylib-inline-cross-dylib/Makefile
151151
run-make/raw-dylib-link-ordinal/Makefile
152152
run-make/raw-dylib-stdcall-ordinal/Makefile
153153
run-make/redundant-libs/Makefile
154-
run-make/relro-levels/Makefile
155154
run-make/remap-path-prefix-dwarf/Makefile
156155
run-make/remap-path-prefix/Makefile
157156
run-make/reproducible-build-2/Makefile
@@ -177,7 +176,6 @@ run-make/split-debuginfo/Makefile
177176
run-make/stable-symbol-names/Makefile
178177
run-make/static-dylib-by-default/Makefile
179178
run-make/static-extern-type/Makefile
180-
run-make/static-pie/Makefile
181179
run-make/staticlib-blank-lib/Makefile
182180
run-make/staticlib-dylib-linkage/Makefile
183181
run-make/std-core-cycle/Makefile

tests/run-make/relro-levels/Makefile

-22
This file was deleted.

tests/run-make/relro-levels/rmake.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This tests the different -Crelro-level values, and makes sure that they work properly.
2+
3+
//@ only-linux
4+
5+
use run_make_support::llvm_readobj;
6+
use run_make_support::rustc;
7+
8+
fn compile(relro_level: &str) {
9+
rustc().arg(format!("-Crelro-level={relro_level}")).input("hello.rs").run();
10+
}
11+
12+
fn main() {
13+
// Ensure that binaries built with the full relro level links them with both
14+
// RELRO and BIND_NOW for doing eager symbol resolving.
15+
16+
compile("full");
17+
llvm_readobj().program_headers().input("hello").run().assert_stdout_contains("GNU_RELRO");
18+
llvm_readobj().dynamic_table().input("hello").run().assert_stdout_contains("BIND_NOW");
19+
20+
compile("partial");
21+
llvm_readobj().program_headers().input("hello").run().assert_stdout_contains("GNU_RELRO");
22+
23+
// Ensure that we're *not* built with RELRO when setting it to off. We do
24+
// not want to check for BIND_NOW however, as the linker might have that
25+
// enabled by default.
26+
compile("off");
27+
llvm_readobj().program_headers().input("hello").run().assert_stdout_not_contains("GNU_RELRO");
28+
}

tests/run-make/static-pie/Makefile

-18
This file was deleted.

tests/run-make/static-pie/check_clang_version.sh

-20
This file was deleted.

tests/run-make/static-pie/check_gcc_version.sh

-20
This file was deleted.

tests/run-make/static-pie/rmake.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

0 commit comments

Comments
 (0)