Skip to content

Commit 83c0398

Browse files
committed
Add test that all symbols we expect to be mangled are actually mangled
1 parent 0b0ccd4 commit 83c0398

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Diff for: tests/run-make/symbols-all-mangled/a_lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn public_rust_function() {}

Diff for: tests/run-make/symbols-all-mangled/an_executable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn public_rust_function_from_exe() {}
2+
3+
fn main() {}

Diff for: tests/run-make/symbols-all-mangled/rmake.rs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Check that all symbols in cdylibs, staticlibs and bins are mangled
2+
3+
use run_make_support::object::read::{Object, ObjectSymbol};
4+
use run_make_support::{bin_name, dynamic_lib_name, object, rfs, rustc, static_lib_name};
5+
6+
fn main() {
7+
let staticlib_name = static_lib_name("a_lib");
8+
let cdylib_name = dynamic_lib_name("a_lib");
9+
let exe_name = bin_name("an_executable");
10+
rustc().crate_type("cdylib").input("a_lib.rs").run();
11+
rustc().crate_type("staticlib").input("a_lib.rs").run();
12+
rustc().crate_type("bin").input("an_executable.rs").run();
13+
14+
symbols_check_archive(&staticlib_name);
15+
symbols_check(&cdylib_name);
16+
symbols_check(&exe_name);
17+
}
18+
19+
fn symbols_check_archive(path: &str) {
20+
let binary_data = rfs::read(path);
21+
let file = object::read::archive::ArchiveFile::parse(&*binary_data).unwrap();
22+
for symbol in file.symbols().unwrap().unwrap() {
23+
let symbol = symbol.unwrap();
24+
let name = strip_underscore_if_apple(std::str::from_utf8(symbol.name()).unwrap());
25+
if name.starts_with("_ZN") || name.starts_with("_R") {
26+
continue; // Correctly mangled
27+
}
28+
29+
let member_name =
30+
std::str::from_utf8(file.member(symbol.offset()).unwrap().name()).unwrap();
31+
if !member_name.ends_with(".rcgu.o") || member_name.contains("compiler_builtins") {
32+
continue; // All compiler-builtins symbols must remain unmangled
33+
}
34+
35+
if name == "__rust_no_alloc_shim_is_unstable" {
36+
continue; // FIXME remove exception once we mangle this symbol
37+
}
38+
39+
if name.contains("rust_eh_personality") {
40+
continue; // Unfortunately LLVM doesn't allow us to mangle this symbol
41+
}
42+
43+
panic!("Unmangled symbol found: {name}");
44+
}
45+
}
46+
47+
fn symbols_check(path: &str) {
48+
let binary_data = rfs::read(path);
49+
let file = object::File::parse(&*binary_data).unwrap();
50+
for symbol in file.symbols() {
51+
if !symbol.is_definition() || !symbol.is_global() {
52+
continue;
53+
}
54+
if symbol.is_weak() {
55+
continue; // Likely an intrinsic from compiler-builtins
56+
}
57+
let name = strip_underscore_if_apple(symbol.name().unwrap());
58+
if name.starts_with("_ZN") || name.starts_with("_R") {
59+
continue; // Correctly mangled
60+
}
61+
62+
if name == "__rust_no_alloc_shim_is_unstable" {
63+
continue; // FIXME remove exception once we mangle this symbol
64+
}
65+
66+
if name.contains("rust_eh_personality") {
67+
continue; // Unfortunately LLVM doesn't allow us to mangle this symbol
68+
}
69+
70+
if ["_start", "__dso_handle", "_init", "_fini", "__TMC_END__"].contains(&name) {
71+
continue; // Part of the libc crt object
72+
}
73+
74+
if name == "main" {
75+
continue; // The main symbol has to be unmangled for the crt object to find it
76+
}
77+
78+
panic!("Unmangled symbol found: {name}");
79+
}
80+
}
81+
82+
fn strip_underscore_if_apple(symbol: &str) -> &str {
83+
if cfg!(target_vendor = "apple") { symbol.strip_prefix("_").unwrap() } else { symbol }
84+
}

0 commit comments

Comments
 (0)