Skip to content

symcheck: Add a wrapper around an archive #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 80 additions & 24 deletions crates/symbol-check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use object::read::archive::{ArchiveFile, ArchiveMember};
use object::{Object, ObjectSymbol, Symbol, SymbolKind, SymbolScope, SymbolSection};
use object::{
File as ObjFile, Object, ObjectSymbol, Symbol, SymbolKind, SymbolScope, SymbolSection,
};
use serde_json::Value;

const CHECK_LIBRARIES: &[&str] = &["compiler_builtins", "builtins_test_intrinsics"];
Expand All @@ -28,13 +30,11 @@ fn main() {
let args_ref = args.iter().map(String::as_str).collect::<Vec<_>>();

match &args_ref[1..] {
["build-and-check", rest @ ..] if !rest.is_empty() => {
let paths = exec_cargo_with_args(rest);
for path in paths {
println!("Checking {}", path.display());
verify_no_duplicates(&path);
verify_core_symbols(&path);
}
["build-and-check", "--target", target, args @ ..] if !args.is_empty() => {
run_build_and_check(Some(target), args);
}
["build-and-check", args @ ..] if !args.is_empty() => {
run_build_and_check(None, args);
}
_ => {
println!("{USAGE}");
Expand All @@ -43,12 +43,42 @@ fn main() {
}
}

fn run_build_and_check(target: Option<&str>, args: &[&str]) {
let paths = exec_cargo_with_args(target, args);
for path in paths {
println!("Checking {}", path.display());
let archive = Archive::from_path(&path);

verify_no_duplicates(&archive);
verify_core_symbols(&archive);
}
}

fn host_target() -> String {
let out = Command::new("rustc")
.arg("--version")
.arg("--verbose")
.output()
.unwrap();
assert!(out.status.success());
let out = String::from_utf8(out.stdout).unwrap();
out.lines()
.find_map(|s| s.strip_prefix("host: "))
.unwrap()
.to_owned()
}

/// Run `cargo build` with the provided additional arguments, collecting the list of created
/// libraries.
fn exec_cargo_with_args(args: &[&str]) -> Vec<PathBuf> {
fn exec_cargo_with_args(target: Option<&str>, args: &[&str]) -> Vec<PathBuf> {
let mut host = String::new();
let target = target.unwrap_or_else(|| {
host = host_target();
host.as_str()
});

let mut cmd = Command::new("cargo");
cmd.arg("build")
.arg("--message-format=json")
cmd.args(["build", "--target", target, "--message-format=json"])
.args(args)
.stdout(Stdio::piped());

Expand Down Expand Up @@ -133,12 +163,12 @@ impl SymInfo {
/// Note that this will also locate cases where a symbol is weakly defined in more than one place.
/// Technically there are no linker errors that will come from this, but it keeps our binary more
/// straightforward and saves some distribution size.
fn verify_no_duplicates(path: &Path) {
fn verify_no_duplicates(archive: &Archive) {
let mut syms = BTreeMap::<String, SymInfo>::new();
let mut dups = Vec::new();
let mut found_any = false;

for_each_symbol(path, |symbol, member| {
archive.for_each_symbol(|symbol, member| {
// Only check defined globals
if !symbol.is_global() || symbol.is_undefined() {
return;
Expand Down Expand Up @@ -185,12 +215,12 @@ fn verify_no_duplicates(path: &Path) {
}

/// Ensure that there are no references to symbols from `core` that aren't also (somehow) defined.
fn verify_core_symbols(path: &Path) {
fn verify_core_symbols(archive: &Archive) {
let mut defined = BTreeSet::new();
let mut undefined = Vec::new();
let mut has_symbols = false;

for_each_symbol(path, |symbol, member| {
archive.for_each_symbol(|symbol, member| {
has_symbols = true;

// Find only symbols from `core`
Expand Down Expand Up @@ -219,14 +249,40 @@ fn verify_core_symbols(path: &Path) {
println!(" success: no undefined references to core found");
}

/// For a given archive path, do something with each symbol.
fn for_each_symbol(path: &Path, mut f: impl FnMut(Symbol, &ArchiveMember)) {
let data = fs::read(path).expect("reading file failed");
let archive = ArchiveFile::parse(data.as_slice()).expect("archive parse failed");
for member in archive.members() {
let member = member.expect("failed to access member");
let obj_data = member.data(&*data).expect("failed to access object");
let obj = object::File::parse(obj_data).expect("failed to parse object");
obj.symbols().for_each(|sym| f(sym, &member));
/// Thin wrapper for owning data used by `object`.
struct Archive {
data: Vec<u8>,
}

impl Archive {
fn from_path(path: &Path) -> Self {
Self {
data: fs::read(path).expect("reading file failed"),
}
}

fn file(&self) -> ArchiveFile<'_> {
ArchiveFile::parse(self.data.as_slice()).expect("archive parse failed")
}

/// For a given archive, do something with each object file.
fn for_each_object(&self, mut f: impl FnMut(ObjFile, &ArchiveMember)) {
let archive = self.file();

for member in archive.members() {
let member = member.expect("failed to access member");
let obj_data = member
.data(self.data.as_slice())
.expect("failed to access object");
let obj = ObjFile::parse(obj_data).expect("failed to parse object");
f(obj, &member);
}
}

/// For a given archive, do something with each symbol.
fn for_each_symbol(&self, mut f: impl FnMut(Symbol, &ArchiveMember)) {
self.for_each_object(|obj, member| {
obj.symbols().for_each(|sym| f(sym, member));
});
}
}