Skip to content

Commit a0d4d99

Browse files
committed
run_make_support nm implementation + bin-emit-no-symbols rmake rewrite
1 parent 7feb191 commit a0d4d99

File tree

6 files changed

+71
-10
lines changed

6 files changed

+71
-10
lines changed

src/tools/run-make-support/nm/mod.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use run_make_support::object;
2+
use std::fs::File as StdFile;
3+
4+
#[derive(Debug)]
5+
struct Nm {
6+
file: Option<object::File>,
7+
}
8+
9+
pub fn nm() -> Nm {
10+
Nm::new()
11+
}
12+
13+
impl Nm {
14+
/// Construct a bare `nm` invocation.
15+
pub fn new() -> Self {
16+
Self { file: None }
17+
}
18+
19+
/// Specify the file to analyze the symbols of.
20+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
21+
Self {
22+
file: Some(
23+
object::File::parse(
24+
fs::read(path)
25+
.expect(format!("Failed to open the ELF file at {:?}", &path.display())),
26+
)
27+
.expect(format!("Failed to parse ELF file at {:?}", &path.display())),
28+
),
29+
}
30+
}
31+
32+
/// Collect all symbols of an object file into a String.
33+
pub fn collect_symbols(&self) -> String {
34+
let object_file = self.file;
35+
let mut symbols_str = String::new();
36+
for section in object_file.sections() {
37+
if let Ok(ObjectSection::SymbolTable(st)) = section.parse::<object::SymbolTable>() {
38+
for symbol in st.symbols() {
39+
symbols_str.push_str(&format!(
40+
"{:016x} {:?} {}\n",
41+
symbol.address(),
42+
symbol.kind(),
43+
symbol.name()
44+
));
45+
}
46+
}
47+
}
48+
}
49+
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod cc;
77
pub mod clang;
88
pub mod diff;
99
pub mod llvm_readobj;
10+
pub mod nm;
1011
pub mod run;
1112
pub mod rustc;
1213
pub mod rustdoc;
@@ -18,13 +19,15 @@ use std::path::{Path, PathBuf};
1819
use std::process::{Command, Output};
1920

2021
pub use gimli;
22+
pub use goblin;
2123
pub use object;
2224
pub use regex;
2325
pub use wasmparser;
2426

2527
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
2628
pub use clang::{clang, Clang};
2729
pub use diff::{diff, Diff};
30+
pub use nm::{nm, Nm};
2831
pub use llvm_readobj::{llvm_readobj, LlvmReadobj};
2932
pub use run::{run, run_fail};
3033
pub use rustc::{aux_build, rustc, Rustc};

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ run-make/issue-37839/Makefile
103103
run-make/issue-40535/Makefile
104104
run-make/issue-47384/Makefile
105105
run-make/issue-47551/Makefile
106-
run-make/issue-51671/Makefile
107106
run-make/issue-64153/Makefile
108107
run-make/issue-68794-textrel-on-minimal-lib/Makefile
109108
run-make/issue-69368/Makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// When setting the crate type as a "bin" (in app.rs),
2+
// this could cause a bug where some symbols would not be
3+
// emitted in the object files. This has been fixed, and
4+
// this test checks that the correct symbols have been successfully
5+
// emitted inside the object files.
6+
// See https://github.com/rust-lang/rust/issues/51671
7+
8+
use run_make_support::{nm, rustc, tmp_dir};
9+
10+
fn main() {
11+
rustc().emit("obj").input("app.rs").run();
12+
//FIXME(Oneirical): This should eventually be tmp_path
13+
let nm = nm(tmp_dir().join("app.o"));
14+
assert!(
15+
nm.contains("rust_begin_unwind")
16+
&& nm.contains("rust_eh_personality")
17+
&& nm.contains("__rg_oom")
18+
);
19+
}

tests/run-make/issue-51671/Makefile

-9
This file was deleted.

0 commit comments

Comments
 (0)