Skip to content

Commit

Permalink
Add test that all symbols we expect to be mangled are actually mangled
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Feb 19, 2025
1 parent b19a278 commit 275f3fa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/run-make/symbols-all-mangled/a_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn public_rust_function() {}
3 changes: 3 additions & 0 deletions tests/run-make/symbols-all-mangled/an_executable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn public_rust_function_from_exe() {}

fn main() {}
84 changes: 84 additions & 0 deletions tests/run-make/symbols-all-mangled/rmake.rs
Original file line number Diff line number Diff line change
@@ -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 }
}

0 comments on commit 275f3fa

Please sign in to comment.