| 
 | 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::targets::is_windows;  | 
 | 5 | +use run_make_support::{dynamic_lib_name, env_var, rfs, rustc};  | 
 | 6 | + | 
 | 7 | +fn main() {  | 
 | 8 | +    let rdylib_name = dynamic_lib_name("a_rust_dylib");  | 
 | 9 | +    rustc().arg("-Zshare-generics=no").input("a_rust_dylib.rs").run();  | 
 | 10 | + | 
 | 11 | +    let binary_data = rfs::read(&rdylib_name);  | 
 | 12 | +    let rdylib = File::parse(&*binary_data).unwrap();  | 
 | 13 | + | 
 | 14 | +    // naked should mirror vanilla  | 
 | 15 | +    not_exported(&rdylib, "private_vanilla");  | 
 | 16 | +    not_exported(&rdylib, "private_naked");  | 
 | 17 | + | 
 | 18 | +    global_function(&rdylib, "public_vanilla");  | 
 | 19 | +    global_function(&rdylib, "public_naked");  | 
 | 20 | + | 
 | 21 | +    not_exported(&rdylib, "public_vanilla_generic");  | 
 | 22 | +    not_exported(&rdylib, "public_naked_generic");  | 
 | 23 | + | 
 | 24 | +    global_function(&rdylib, "vanilla_external_linkage");  | 
 | 25 | +    global_function(&rdylib, "naked_external_linkage");  | 
 | 26 | + | 
 | 27 | +    // #[linkage = "weak"] does not work well on windows, we get  | 
 | 28 | +    //  | 
 | 29 | +    // lib.def : error LNK2001: unresolved external symbol naked_weak_linkage␍  | 
 | 30 | +    // lib.def : error LNK2001: unresolved external symbol vanilla_weak_linkage  | 
 | 31 | +    //  | 
 | 32 | +    // so just skip weak symbols on windows (for now)  | 
 | 33 | +    if !is_windows() {  | 
 | 34 | +        weak_function(&rdylib, "vanilla_weak_linkage");  | 
 | 35 | +        weak_function(&rdylib, "naked_weak_linkage");  | 
 | 36 | +    }  | 
 | 37 | + | 
 | 38 | +    // functions that are declared in an `extern "C"` block are currently not exported  | 
 | 39 | +    // this maybe should change in the future, this is just tracking the current behavior  | 
 | 40 | +    // reported in https://github.com/rust-lang/rust/issues/128071  | 
 | 41 | +    not_exported(&rdylib, "function_defined_in_global_asm");  | 
 | 42 | + | 
 | 43 | +    // share generics should expose the generic functions  | 
 | 44 | +    rustc().arg("-Zshare-generics=yes").input("a_rust_dylib.rs").run();  | 
 | 45 | +    let binary_data = rfs::read(&rdylib_name);  | 
 | 46 | +    let rdylib = File::parse(&*binary_data).unwrap();  | 
 | 47 | + | 
 | 48 | +    global_function(&rdylib, "public_vanilla_generic");  | 
 | 49 | +    global_function(&rdylib, "public_naked_generic");  | 
 | 50 | +}  | 
 | 51 | + | 
 | 52 | +#[track_caller]  | 
 | 53 | +fn global_function(file: &File, symbol_name: &str) {  | 
 | 54 | +    let symbols = find_dynamic_symbol(file, symbol_name);  | 
 | 55 | +    let [symbol] = symbols.as_slice() else {  | 
 | 56 | +        panic!("symbol {symbol_name} occurs {} times", symbols.len())  | 
 | 57 | +    };  | 
 | 58 | + | 
 | 59 | +    assert!(symbol.is_definition(), "`{symbol_name}` is not a function");  | 
 | 60 | +    assert!(symbol.is_global(), "`{symbol_name}` is not marked as global");  | 
 | 61 | +}  | 
 | 62 | + | 
 | 63 | +#[track_caller]  | 
 | 64 | +fn weak_function(file: &File, symbol_name: &str) {  | 
 | 65 | +    let symbols = find_dynamic_symbol(file, symbol_name);  | 
 | 66 | +    let [symbol] = symbols.as_slice() else {  | 
 | 67 | +        panic!("symbol {symbol_name} occurs {} times", symbols.len())  | 
 | 68 | +    };  | 
 | 69 | + | 
 | 70 | +    assert!(symbol.is_definition(), "`{symbol_name}` is not a function");  | 
 | 71 | +    assert!(symbol.is_weak(), "`{symbol_name}` is not marked as weak");  | 
 | 72 | +}  | 
 | 73 | + | 
 | 74 | +#[track_caller]  | 
 | 75 | +fn not_exported(file: &File, symbol_name: &str) {  | 
 | 76 | +    assert_eq!(find_dynamic_symbol(file, symbol_name).len(), 0)  | 
 | 77 | +}  | 
 | 78 | + | 
 | 79 | +fn find_subsequence(haystack: &[u8], needle: &[u8]) -> bool {  | 
 | 80 | +    haystack.windows(needle.len()).any(|window| window == needle)  | 
 | 81 | +}  | 
 | 82 | + | 
 | 83 | +fn find_dynamic_symbol<'file, 'data>(  | 
 | 84 | +    file: &'file File<'data>,  | 
 | 85 | +    expected: &str,  | 
 | 86 | +) -> Vec<Symbol<'data, 'file>> {  | 
 | 87 | +    file.exports()  | 
 | 88 | +        .unwrap()  | 
 | 89 | +        .into_iter()  | 
 | 90 | +        .filter(|e| find_subsequence(e.name(), expected.as_bytes()))  | 
 | 91 | +        .filter_map(|e| file.symbol_by_name_bytes(e.name()))  | 
 | 92 | +        .collect()  | 
 | 93 | +}  | 
0 commit comments