From 83c0398856762562e3398c565fe2010c1d13e0aa Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 6 Feb 2025 18:16:45 +0000 Subject: [PATCH] Add test that all symbols we expect to be mangled are actually mangled --- tests/run-make/symbols-all-mangled/a_lib.rs | 1 + .../symbols-all-mangled/an_executable.rs | 3 + tests/run-make/symbols-all-mangled/rmake.rs | 84 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 tests/run-make/symbols-all-mangled/a_lib.rs create mode 100644 tests/run-make/symbols-all-mangled/an_executable.rs create mode 100644 tests/run-make/symbols-all-mangled/rmake.rs diff --git a/tests/run-make/symbols-all-mangled/a_lib.rs b/tests/run-make/symbols-all-mangled/a_lib.rs new file mode 100644 index 000000000000..716dd3263c5b --- /dev/null +++ b/tests/run-make/symbols-all-mangled/a_lib.rs @@ -0,0 +1 @@ +pub fn public_rust_function() {} diff --git a/tests/run-make/symbols-all-mangled/an_executable.rs b/tests/run-make/symbols-all-mangled/an_executable.rs new file mode 100644 index 000000000000..db8df13b9a75 --- /dev/null +++ b/tests/run-make/symbols-all-mangled/an_executable.rs @@ -0,0 +1,3 @@ +pub fn public_rust_function_from_exe() {} + +fn main() {} diff --git a/tests/run-make/symbols-all-mangled/rmake.rs b/tests/run-make/symbols-all-mangled/rmake.rs new file mode 100644 index 000000000000..a4c656722a7a --- /dev/null +++ b/tests/run-make/symbols-all-mangled/rmake.rs @@ -0,0 +1,84 @@ +// Check that all symbols in cdylibs, staticlibs and bins are mangled + +use run_make_support::object::read::{Object, ObjectSymbol}; +use run_make_support::{bin_name, dynamic_lib_name, object, rfs, rustc, static_lib_name}; + +fn main() { + let staticlib_name = static_lib_name("a_lib"); + let cdylib_name = dynamic_lib_name("a_lib"); + let exe_name = bin_name("an_executable"); + rustc().crate_type("cdylib").input("a_lib.rs").run(); + rustc().crate_type("staticlib").input("a_lib.rs").run(); + rustc().crate_type("bin").input("an_executable.rs").run(); + + symbols_check_archive(&staticlib_name); + symbols_check(&cdylib_name); + symbols_check(&exe_name); +} + +fn symbols_check_archive(path: &str) { + let binary_data = rfs::read(path); + let file = object::read::archive::ArchiveFile::parse(&*binary_data).unwrap(); + for symbol in file.symbols().unwrap().unwrap() { + let symbol = symbol.unwrap(); + let name = strip_underscore_if_apple(std::str::from_utf8(symbol.name()).unwrap()); + if name.starts_with("_ZN") || name.starts_with("_R") { + continue; // Correctly mangled + } + + let member_name = + std::str::from_utf8(file.member(symbol.offset()).unwrap().name()).unwrap(); + if !member_name.ends_with(".rcgu.o") || member_name.contains("compiler_builtins") { + continue; // All compiler-builtins symbols must remain unmangled + } + + if name == "__rust_no_alloc_shim_is_unstable" { + continue; // FIXME remove exception once we mangle this symbol + } + + if name.contains("rust_eh_personality") { + continue; // Unfortunately LLVM doesn't allow us to mangle this symbol + } + + panic!("Unmangled symbol found: {name}"); + } +} + +fn symbols_check(path: &str) { + let binary_data = rfs::read(path); + let file = object::File::parse(&*binary_data).unwrap(); + for symbol in file.symbols() { + if !symbol.is_definition() || !symbol.is_global() { + continue; + } + if symbol.is_weak() { + continue; // Likely an intrinsic from compiler-builtins + } + let name = strip_underscore_if_apple(symbol.name().unwrap()); + if name.starts_with("_ZN") || name.starts_with("_R") { + continue; // Correctly mangled + } + + if name == "__rust_no_alloc_shim_is_unstable" { + continue; // FIXME remove exception once we mangle this symbol + } + + if name.contains("rust_eh_personality") { + continue; // Unfortunately LLVM doesn't allow us to mangle this symbol + } + + if ["_start", "__dso_handle", "_init", "_fini", "__TMC_END__"].contains(&name) { + continue; // Part of the libc crt object + } + + if name == "main" { + continue; // The main symbol has to be unmangled for the crt object to find it + } + + panic!("Unmangled symbol found: {name}"); + } +} + +fn strip_underscore_if_apple(symbol: &str) -> &str { + if cfg!(target_vendor = "apple") { symbol.strip_prefix("_").unwrap() } else { symbol } +}