| 
 | 1 | +// Ensure that crates compiled with different rustc versions cannot  | 
 | 2 | +// be dynamically linked.  | 
 | 3 | + | 
 | 4 | +//@ ignore-cross-compile  | 
 | 5 | +//@ only-unix  | 
 | 6 | + | 
 | 7 | +use run_make_support::llvm;  | 
 | 8 | +use run_make_support::{diff, dynamic_lib_name, is_darwin, run, run_fail, rustc};  | 
 | 9 | + | 
 | 10 | +fn llvm_readobj() -> llvm::LlvmReadobj {  | 
 | 11 | +    let mut cmd = llvm::llvm_readobj();  | 
 | 12 | +    if is_darwin() {  | 
 | 13 | +        cmd.symbols();  | 
 | 14 | +    } else {  | 
 | 15 | +        cmd.dynamic_table();  | 
 | 16 | +    }  | 
 | 17 | +    cmd  | 
 | 18 | +}  | 
 | 19 | + | 
 | 20 | +fn main() {  | 
 | 21 | +    let flags = ["-Cprefer-dynamic", "-Csymbol-mangling-version=v0"];  | 
 | 22 | + | 
 | 23 | +    // a.rs is compiled to a dylib  | 
 | 24 | +    rustc().input("a.rs").crate_type("dylib").args(&flags).run();  | 
 | 25 | + | 
 | 26 | +    // Store symbols  | 
 | 27 | +    let symbols_before = llvm_readobj().arg(dynamic_lib_name("a")).run().stdout_utf8();  | 
 | 28 | + | 
 | 29 | +    // b.rs is compiled to a binary  | 
 | 30 | +    rustc()  | 
 | 31 | +        .input("b.rs")  | 
 | 32 | +        .extern_("a", dynamic_lib_name("a"))  | 
 | 33 | +        .crate_type("bin")  | 
 | 34 | +        .arg("-Crpath")  | 
 | 35 | +        .args(&flags)  | 
 | 36 | +        .run();  | 
 | 37 | +    run("b");  | 
 | 38 | + | 
 | 39 | +    // Now re-compile a.rs with another rustc version  | 
 | 40 | +    rustc()  | 
 | 41 | +        .env("RUSTC_FORCE_RUSTC_VERSION", "deadfeed")  | 
 | 42 | +        .input("a.rs")  | 
 | 43 | +        .crate_type("dylib")  | 
 | 44 | +        .args(&flags)  | 
 | 45 | +        .run();  | 
 | 46 | + | 
 | 47 | +    // After compiling with a different rustc version, store symbols again.  | 
 | 48 | +    let symbols_after = llvm_readobj().arg(dynamic_lib_name("a")).run().stdout_utf8();  | 
 | 49 | + | 
 | 50 | +    // As a sanity check, test if the symbols changed:  | 
 | 51 | +    // If the symbols are identical, there's been an error.  | 
 | 52 | +    diff()  | 
 | 53 | +        .expected_text("symbols_before", symbols_before)  | 
 | 54 | +        .actual_text("symbols_after", symbols_after)  | 
 | 55 | +        .run_fail();  | 
 | 56 | +    run_fail("b");  | 
 | 57 | +}  | 
0 commit comments