Skip to content

Commit 99e55c0

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

File tree

8 files changed

+79
-10
lines changed

8 files changed

+79
-10
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,7 @@ name = "run_make_support"
32553255
version = "0.0.0"
32563256
dependencies = [
32573257
"gimli 0.28.1",
3258+
"goblin 0.8.2",
32583259
"object 0.34.0",
32593260
"regex",
32603261
"similar",

src/tools/run-make-support/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ similar = "2.5.0"
99
wasmparser = "0.118.2"
1010
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
1111
gimli = "0.28.1"
12+
goblin = "0.8.2"

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

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use goblin::elf::Elf32;
2+
use goblin::elf::Elf64;
3+
use goblin::elf::SectionHeader;
4+
use std::fs::File as StdFile;
5+
6+
#[derive(Debug)]
7+
struct Nm {
8+
file: Option<File>,
9+
}
10+
11+
pub fn nm() -> Nm {
12+
Nm::new()
13+
}
14+
15+
impl Nm {
16+
/// Construct a bare `nm` invocation.
17+
pub fn new() -> Self {
18+
Self {
19+
file: None,
20+
}
21+
}
22+
23+
/// Specify the file to analyze the symbols of.
24+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
25+
Self {
26+
file: File::open(&path).expect(format!("Failed to open the ELF file at {:?}", &path.display()))
27+
}
28+
}
29+
30+
pub fn collect_symbols(&self) -> String {
31+
// Determine whether the file is 32-bit or 64-bit
32+
// by fetching the target's configuration
33+
let mut elf = if cfg!(target_pointer_width = 64) {
34+
Elf64::new(self.file)
35+
} else if cfg!(target_pointer_width = 32) {
36+
Elf32::new(self.file)
37+
} else {
38+
panic!("Unsupported architecture detected, should be 32 or 64 bits.")
39+
};
40+
41+
let mut all_symbol_names = String::new();
42+
43+
let symtab_section_index = elf.section_headers()
44+
.iter()
45+
.position(|sh| sh.sh_type == SectionHeader::SHT_SYMTAB)
46+
.expect("Symbol table section not found");
47+
48+
for symbol in elf.symbol_table(symtab_section_index).symbols() {
49+
all_symbol_names.push_str(&symbol.name.to_string_lossy().into_owned());
50+
all_symbol_names.push('\n');
51+
}
52+
53+
all_symbol_names
54+
}
55+
}

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
File renamed without changes.
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)