|
| 1 | +//@ only-x86_64 |
| 2 | +use run_make_support::object::read::{File, Object, Symbol}; |
| 3 | +use run_make_support::object::ObjectSymbol; |
| 4 | +use run_make_support::{dynamic_lib_name, rfs, rustc}; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + let rdylib_name = dynamic_lib_name("a_rust_dylib"); |
| 8 | + rustc().arg("-Zshare-generics=no").input("a_rust_dylib.rs").run(); |
| 9 | + |
| 10 | + let binary_data = rfs::read(&rdylib_name); |
| 11 | + let rdylib = File::parse(&*binary_data).unwrap(); |
| 12 | + |
| 13 | + // check vanilla symbols |
| 14 | + not_exported(&rdylib, "private_vanilla_rust_function_from_rust_dylib"); |
| 15 | + global_function(&rdylib, "public_vanilla_rust_function_from_rust_dylib"); |
| 16 | + not_exported(&rdylib, "public_vanilla_generic_function_from_rust_dylib"); |
| 17 | + |
| 18 | + weak_function(&rdylib, "vanilla_weak_linkage"); |
| 19 | + global_function(&rdylib, "vanilla_external_linkage"); |
| 20 | + |
| 21 | + // naked should mirror vanilla |
| 22 | + not_exported(&rdylib, "private_naked_rust_function_from_rust_dylib"); |
| 23 | + global_function(&rdylib, "public_naked_rust_function_from_rust_dylib"); |
| 24 | + not_exported(&rdylib, "public_naked_generic_function_from_rust_dylib"); |
| 25 | + |
| 26 | + weak_function(&rdylib, "naked_weak_linkage"); |
| 27 | + global_function(&rdylib, "naked_external_linkage"); |
| 28 | + |
| 29 | + // functions that are declared in an `extern "C"` block are currently not exported |
| 30 | + // this maybe should change in the future, this is just tracking the current behavior |
| 31 | + // reported in https://github.com/rust-lang/rust/issues/128071 |
| 32 | + not_exported(&rdylib, "function_defined_in_global_asm"); |
| 33 | + |
| 34 | + // share generics should expose the generic functions |
| 35 | + rustc().arg("-Zshare-generics=yes").input("a_rust_dylib.rs").run(); |
| 36 | + let binary_data = rfs::read(&rdylib_name); |
| 37 | + let rdylib = File::parse(&*binary_data).unwrap(); |
| 38 | + |
| 39 | + global_function(&rdylib, "public_vanilla_generic_function_from_rust_dylib"); |
| 40 | + global_function(&rdylib, "public_naked_generic_function_from_rust_dylib"); |
| 41 | +} |
| 42 | + |
| 43 | +#[track_caller] |
| 44 | +fn global_function(file: &File, symbol_name: &str) { |
| 45 | + let symbols = find_dynamic_symbol(file, symbol_name); |
| 46 | + let [symbol] = symbols.as_slice() else { |
| 47 | + panic!("symbol {symbol_name} occurs {} times", symbols.len()) |
| 48 | + }; |
| 49 | + |
| 50 | + assert!(symbol.is_definition(), "`{symbol_name}` is not a function"); |
| 51 | + assert!(symbol.is_global(), "`{symbol_name}` is not marked as global"); |
| 52 | +} |
| 53 | + |
| 54 | +#[track_caller] |
| 55 | +fn weak_function(file: &File, symbol_name: &str) { |
| 56 | + let symbols = find_dynamic_symbol(file, symbol_name); |
| 57 | + let [symbol] = symbols.as_slice() else { |
| 58 | + panic!("symbol {symbol_name} occurs {} times", symbols.len()) |
| 59 | + }; |
| 60 | + |
| 61 | + assert!(symbol.is_definition(), "`{symbol_name}` is not a function"); |
| 62 | + assert!(symbol.is_weak(), "`{symbol_name}` is not marked as weak"); |
| 63 | +} |
| 64 | + |
| 65 | +#[track_caller] |
| 66 | +fn not_exported(file: &File, symbol_name: &str) { |
| 67 | + assert_eq!(find_dynamic_symbol(file, symbol_name).len(), 0) |
| 68 | +} |
| 69 | + |
| 70 | +fn find_dynamic_symbol<'file, 'data>( |
| 71 | + file: &'file File<'data>, |
| 72 | + expected: &str, |
| 73 | +) -> Vec<Symbol<'data, 'file>> { |
| 74 | + file.dynamic_symbols() |
| 75 | + .filter(|symbol| { |
| 76 | + let name = symbol.name().unwrap(); |
| 77 | + !name.contains("__imp_") && name.contains(expected) |
| 78 | + }) |
| 79 | + .collect() |
| 80 | +} |
0 commit comments