Skip to content
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

Migrate symbol-mangling-hashed to rmake.rs #128567

Closed
Closed
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/tools/run-make-support/src/external_deps/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ impl Rustc {
self
}

/// Make `rustc` prefere dynamic linking
pub fn prefer_dynamic(&mut self) -> &mut Self {
self.arg("-Cprefer-dynamic")
}

/// Specify directory path used for profile generation
pub fn profile_generate<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
let mut arg = OsString::new();
Expand Down Expand Up @@ -264,6 +269,13 @@ impl Rustc {
self
}

/// Enable `-Z unstable-options` and set `-C symbol-mangling-version`.
pub fn symbol_mangling_version(&mut self, v: &str) -> &mut Self {
self.cmd.arg("-Zunstable-options");
self.cmd.arg(format!("-Csymbol-mangling-version={v}"));
self
}

/// Specify the edition year.
pub fn edition(&mut self, edition: &str) -> &mut Self {
self.cmd.arg("--edition");
Expand Down
37 changes: 36 additions & 1 deletion src/tools/run-make-support/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ where
///
/// Panics if `path` is not a valid object file readable by the current user.
pub fn any_symbol_contains(path: impl AsRef<Path>, substrings: &[&str]) -> bool {
let path = path.as_ref();
with_symbol_iter(path, |syms| {
for sym in syms {
for substring in substrings {
Expand All @@ -34,11 +35,45 @@ pub fn any_symbol_contains(path: impl AsRef<Path>, substrings: &[&str]) -> bool
.windows(substring.len())
.any(|x| x == substring.as_bytes())
{
eprintln!("{:?} contains {}", sym, substring);
eprintln!("{:?} contains {} in {}", sym, substring, path.display());
return true;
}
}
}
false
})
}

/// Check if an object file contains *all* of the given symbols.
///
/// The symbol names must match exactly.
///
/// Panics if `path` is not a valid object file readable by the current user.
pub fn contains_exact_symbols(path: impl AsRef<Path>, symbol_names: &[&str]) -> bool {
let mut found = vec![false; symbol_names.len()];
with_symbol_iter(path, |syms| {
for sym in syms {
for (i, symbol_name) in symbol_names.iter().enumerate() {
found[i] |= sym.name_bytes().unwrap() == symbol_name.as_bytes();
}
}
});
let result = found.iter().all(|x| *x);
if !result {
eprintln!("does not contain symbol(s): ");
for i in 0..found.len() {
if !found[i] {
eprintln!("* {}", symbol_names[i]);
}
}
}
result
}

pub fn print_symbols(path: impl AsRef<Path>) {
let path = path.as_ref();
println!("symbols in {}:", path.display());
with_symbol_iter(path, |syms| {
syms.for_each(|sym| println!(" {}", &sym.name().unwrap()));
});
}
2 changes: 0 additions & 2 deletions src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ run-make/dep-info-doesnt-run-much/Makefile
run-make/dep-info-spaces/Makefile
run-make/dep-info/Makefile
run-make/emit-to-stdout/Makefile
run-make/extern-fn-reachable/Makefile
run-make/foreign-double-unwind/Makefile
run-make/foreign-exceptions/Makefile
run-make/incr-add-rust-src-component/Makefile
Expand Down Expand Up @@ -43,7 +42,6 @@ run-make/simd-ffi/Makefile
run-make/split-debuginfo/Makefile
run-make/stable-symbol-names/Makefile
run-make/staticlib-dylib-linkage/Makefile
run-make/symbol-mangling-hashed/Makefile
run-make/sysroot-crates-are-unstable/Makefile
run-make/thumb-none-cortex-m/Makefile
run-make/thumb-none-qemu/Makefile
Expand Down
26 changes: 0 additions & 26 deletions tests/run-make/extern-fn-reachable/Makefile

This file was deleted.

8 changes: 8 additions & 0 deletions tests/run-make/extern-fn-reachable/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ ignore-cross-compile
use run_make_support::rustc;
use run_make_support::symbols::contains_exact_symbols;

fn main() {
rustc().input("dylib.rs").output("dylib.so").prefer_dynamic().run();
assert!(contains_exact_symbols("dylib.so", &["fun1", "fun2", "fun3", "fun4", "fun5"]));
}
48 changes: 0 additions & 48 deletions tests/run-make/symbol-mangling-hashed/Makefile

This file was deleted.

61 changes: 61 additions & 0 deletions tests/run-make/symbol-mangling-hashed/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//@ ignore-cross-compile
//@ only-linux
//@ only-x86_64

use run_make_support::object::ObjectSymbol;
use run_make_support::symbols::*;
use run_make_support::{bin_name, cwd, dynamic_lib_name, run, rust_lib_name, rustc};

fn main() {
rustc()
.prefer_dynamic()
.symbol_mangling_version("hashed")
.metadata("foo")
.input("a_dylib.rs")
.run();
rustc()
.prefer_dynamic()
.symbol_mangling_version("hashed")
.metadata("bar")
.input("a_rlib.rs")
.run();
/*println!("{}", cwd().display());
dbg!(cwd().display());
use std::io::Write;
std::io::stdout().flush().unwrap();
std::fs::write("/tmp/the_dir", cwd().display().to_string()).unwrap();
loop {}*/
//run("sh");
//run("pwd");
rustc().prefer_dynamic().library_search_path(cwd()).input("b_dylib.rs").run();
rustc().prefer_dynamic().library_search_path(cwd()).input("b_bin.rs").run();
let a_dylib = dynamic_lib_name("a_dylib");
assert!(!any_symbol_contains(&a_dylib, &["hello"]));
assert!(contains_exact_symbols(
&a_dylib,
&["_RNxC7a_dylib12H98WkzJ7B2nk", "_RNxC7a_dylib12HjermeVgSqiY",]
));
let b_dylib = dynamic_lib_name("b_dylib");
// b_dylib was compiled with regular symbol mangling.
assert!(any_symbol_contains(&b_dylib, &["hello"]));
// it depends on a_rlib, which was compiled with
// hashed symbol mangling.
assert!(contains_exact_symbols(
&b_dylib,
&[
"_RNxC6a_rlib12H85r05hDVgWS",
"_RNxC6a_rlib12HeiQWRC1rtuF",
"_RNxC7a_dylib12HjermeVgSqiY",
]
));
let b_bin = bin_name("b_bin");
assert!(contains_exact_symbols(
&b_bin,
&[
"_RNxC6a_rlib12HeiQWRC1rtuF",
"_RNxC7a_dylib12HjermeVgSqiY",
"_ZN7b_dylib5hello17h3a39df941aa66c40E",
]
));
print_symbols(b_bin);
}