|
| 1 | +//! Smoke test to check that that symbols of `extern "C"` functions and `#[no_mangle]` rust |
| 2 | +//! functions: |
| 3 | +//! |
| 4 | +//! 1. Are externally visible in the dylib produced. |
| 5 | +//! 2. That the symbol visibility is orthogonal to the Rust nameres visibility of the functions |
| 6 | +//! involved. |
| 7 | +
|
| 8 | +//@ ignore-cross-compile |
| 9 | + |
| 10 | +use std::collections::BTreeSet; |
| 11 | + |
| 12 | +use run_make_support::object::{self, Object}; |
| 13 | +use run_make_support::{dynamic_lib_name, is_darwin, path, rfs, rustc}; |
| 14 | + |
| 15 | +fn main() { |
| 16 | + let dylib = dynamic_lib_name("dylib"); |
| 17 | + rustc().input("dylib.rs").output(&dylib).arg("-Cprefer-dynamic").run(); |
| 18 | + |
| 19 | + let expected_symbols = if is_darwin() { |
| 20 | + // Mach-O states that all exported symbols should have an underscore as prefix. At the |
| 21 | + // same time dlsym will implicitly add it, so outside of compilers, linkers and people |
| 22 | + // writing assembly, nobody needs to be aware of this. |
| 23 | + BTreeSet::from(["_fun1", "_fun2", "_fun3", "_fun4", "_fun5", "_fun6"]) |
| 24 | + } else { |
| 25 | + BTreeSet::from(["fun1", "fun2", "fun3", "fun4", "fun5", "fun6"]) |
| 26 | + }; |
| 27 | + |
| 28 | + let mut found_symbols = BTreeSet::new(); |
| 29 | + |
| 30 | + let blob = rfs::read(path(dylib)); |
| 31 | + let file = object::File::parse(&*blob).unwrap(); |
| 32 | + for export in file.exports().unwrap() { |
| 33 | + let sym_name = export.name(); |
| 34 | + let sym_name = std::str::from_utf8(sym_name).unwrap(); |
| 35 | + found_symbols.insert(sym_name); |
| 36 | + } |
| 37 | + |
| 38 | + println!("expected_symbols = {:?}", expected_symbols); |
| 39 | + println!("found_symbols = {:?}", found_symbols); |
| 40 | + if !found_symbols.is_superset(&expected_symbols) { |
| 41 | + for diff in expected_symbols.difference(&found_symbols) { |
| 42 | + eprintln!("missing symbol: {}", diff); |
| 43 | + } |
| 44 | + panic!("missing expected symbols"); |
| 45 | + } |
| 46 | +} |
0 commit comments