Skip to content

Commit 9709af7

Browse files
committed
Auto merge of rust-lang#135768 - jieyouxu:migrate-symbol-mangling-hashed, r=Noratrieb
tests: Port `symbol-mangling-hashed` to rmake.rs Part of rust-lang#121876. This PR supersedes rust-lang#128567 and is co-authored with `@lolbinarycat.` ### Summary This PR ports `tests/run-make/symbol-mangling-hashed` to rmake.rs. Notable differences when compared to the Makefile version includes: - It's no longer limited to linux + x86_64 only. In particular, this now is exercised on darwin and windows (esp. msvc) too. - The test uses `object` crate to be more precise in the filtering, and avoids relying on parsing the human-readable `nm` output for *some* `nm` in the given environment (which isn't really a thing on msvc anyway, and `llvm-nm` doesn't handle msvc dylibs AFAICT). - Dump the symbols satisfying various criteria on test failure to make it hopefully less of a pain to debug if it ever fails in CI. ### Review advice - Best reviewed commit-by-commit. - I'm not *super* sure about the msvc logic, would benefit from a MSVC (PE/COFF) expert taking a look. --- try-job: x86_64-msvc-1 try-job: i686-msvc-1 try-job: i686-mingw try-job: x86_64-mingw-1 try-job: x86_64-apple-1 try-job: aarch64-apple try-job: test-various
2 parents 854f225 + 9734ebb commit 9709af7

File tree

13 files changed

+178
-81
lines changed

13 files changed

+178
-81
lines changed

src/tools/run-make-support/src/external_deps/rustc.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ impl Rustc {
216216
self
217217
}
218218

219+
/// Specify option of `-C symbol-mangling-version`.
220+
pub fn symbol_mangling_version(&mut self, option: &str) -> &mut Self {
221+
self.cmd.arg(format!("-Csymbol-mangling-version={option}"));
222+
self
223+
}
224+
225+
/// Specify `-C prefer-dynamic`.
226+
pub fn prefer_dynamic(&mut self) -> &mut Self {
227+
self.cmd.arg(format!("-Cprefer-dynamic"));
228+
self
229+
}
230+
219231
/// Specify error format to use
220232
pub fn error_format(&mut self, format: &str) -> &mut Self {
221233
self.cmd.arg(format!("--error-format={format}"));

src/tools/run-make-support/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ pub use wasmparser;
4747
// tidy-alphabetical-end
4848

4949
// Re-exports of external dependencies.
50-
pub use external_deps::{c_build, c_cxx_compiler, clang, htmldocck, llvm, python, rustc, rustdoc};
50+
pub use external_deps::{
51+
cargo, c_build, c_cxx_compiler, clang, htmldocck, llvm, python, rustc, rustdoc
52+
};
5153

5254
// These rely on external dependencies.
5355
pub use c_cxx_compiler::{Cc, Gcc, cc, cxx, extra_c_flags, extra_cxx_flags, gcc};
@@ -79,7 +81,10 @@ pub use env::{env_var, env_var_os, set_current_dir};
7981
pub use run::{cmd, run, run_fail, run_with_args};
8082

8183
/// Helpers for checking target information.
82-
pub use targets::{is_aix, is_darwin, is_msvc, is_windows, llvm_components_contain, target, uname, apple_os};
84+
pub use targets::{
85+
apple_os, is_aix, is_darwin, is_msvc, is_windows, is_windows_gnu, llvm_components_contain,
86+
target, uname,
87+
};
8388

8489
/// Helpers for building names of output artifacts that are potentially target-specific.
8590
pub use artifact_names::{
@@ -104,4 +109,3 @@ pub use assertion_helpers::{
104109
pub use string::{
105110
count_regex_matches_in_files_with_extension, invalid_utf8_contains, invalid_utf8_not_contains,
106111
};
107-
use crate::external_deps::cargo;

src/tools/run-make-support/src/symbols.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,44 @@ use std::path::Path;
22

33
use object::{self, Object, ObjectSymbol, SymbolIterator};
44

5-
/// Iterate through the symbols in an object file.
6-
///
7-
/// Uses a callback because `SymbolIterator` does not own its data.
5+
/// Given an [`object::File`], find the exported dynamic symbol names via
6+
/// [`object::Object::exports`]. This does not distinguish between which section the symbols appear
7+
/// in.
8+
#[track_caller]
9+
pub fn exported_dynamic_symbol_names<'file>(file: &'file object::File<'file>) -> Vec<&'file str> {
10+
file.exports()
11+
.unwrap()
12+
.into_iter()
13+
.filter_map(|sym| std::str::from_utf8(sym.name()).ok())
14+
.collect()
15+
}
16+
17+
/// Iterate through the symbols in an object file. See [`object::Object::symbols`].
818
///
9-
/// Panics if `path` is not a valid object file readable by the current user.
19+
/// Panics if `path` is not a valid object file readable by the current user or if `path` cannot be
20+
/// parsed as a recognized object file.
21+
#[track_caller]
1022
pub fn with_symbol_iter<P, F, R>(path: P, func: F) -> R
1123
where
1224
P: AsRef<Path>,
1325
F: FnOnce(&mut SymbolIterator<'_, '_>) -> R,
1426
{
15-
let raw_bytes = crate::fs::read(path);
16-
let f = object::File::parse(raw_bytes.as_slice()).expect("unable to parse file");
27+
let path = path.as_ref();
28+
let blob = crate::fs::read(path);
29+
let f = object::File::parse(&*blob)
30+
.unwrap_or_else(|e| panic!("failed to parse `{}`: {e}", path.display()));
1731
let mut iter = f.symbols();
1832
func(&mut iter)
1933
}
2034

2135
/// Check an object file's symbols for substrings.
2236
///
23-
/// Returns `true` if any of the symbols found in the object file at
24-
/// `path` contain a substring listed in `substrings`.
37+
/// Returns `true` if any of the symbols found in the object file at `path` contain a substring
38+
/// listed in `substrings`.
2539
///
26-
/// Panics if `path` is not a valid object file readable by the current user.
40+
/// Panics if `path` is not a valid object file readable by the current user or if `path` cannot be
41+
/// parsed as a recognized object file.
42+
#[track_caller]
2743
pub fn any_symbol_contains(path: impl AsRef<Path>, substrings: &[&str]) -> bool {
2844
with_symbol_iter(path, |syms| {
2945
for sym in syms {

src/tools/run-make-support/src/targets.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ pub fn is_msvc() -> bool {
2222
target().contains("msvc")
2323
}
2424

25+
/// Check if target is windows-gnu.
26+
#[must_use]
27+
pub fn is_windows_gnu() -> bool {
28+
target().ends_with("windows-gnu")
29+
}
30+
2531
/// Check if target uses macOS.
2632
#[must_use]
2733
pub fn is_darwin() -> bool {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
run-make/split-debuginfo/Makefile
2-
run-make/symbol-mangling-hashed/Makefile

tests/run-make/symbol-mangling-hashed/Makefile

Lines changed: 0 additions & 48 deletions
This file was deleted.

tests/run-make/symbol-mangling-hashed/b_bin.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/run-make/symbol-mangling-hashed/b_dylib.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern crate default_dylib;
2+
extern crate hashed_dylib;
3+
extern crate hashed_rlib;
4+
5+
fn main() {
6+
hashed_rlib::hrhello();
7+
hashed_dylib::hdhello();
8+
default_dylib::ddhello();
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![crate_type = "dylib"]
2+
3+
extern crate hashed_dylib;
4+
extern crate hashed_rlib;
5+
6+
pub fn ddhello() {
7+
hashed_rlib::hrhello();
8+
hashed_dylib::hdhello();
9+
}

0 commit comments

Comments
 (0)