Skip to content

Commit 4913d26

Browse files
committed
Auto merge of #128607 - ChrisDenton:visibility, r=<try>
Use `object` in `run-make/symbols-visibility` This is another case where we can simply use a rust library instead of wrangling nm. try-job: x86_64-msvc try-job: i686-msvc try-job: test-various
2 parents ad0a2b7 + c2523c9 commit 4913d26

File tree

1 file changed

+32
-44
lines changed
  • tests/run-make/symbol-visibility

1 file changed

+32
-44
lines changed

Diff for: tests/run-make/symbol-visibility/rmake.rs

+32-44
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
// are exported, and that generics are only shown if explicitely requested.
55
// See https://github.com/rust-lang/rust/issues/37530
66

7-
//@ ignore-windows-msvc
8-
9-
//FIXME(Oneirical): This currently uses llvm-nm for symbol detection. However,
10-
// the custom Rust-based solution of #128314 may prove to be an interesting alternative.
11-
12-
use run_make_support::{bin_name, dynamic_lib_name, is_darwin, is_windows, llvm_nm, regex, rustc};
7+
use run_make_support::object::read::Object;
8+
use run_make_support::{bin_name, dynamic_lib_name, is_msvc, object, regex, rfs, rustc};
139

1410
fn main() {
1511
let cdylib_name = dynamic_lib_name("a_cdylib");
@@ -64,16 +60,15 @@ fn main() {
6460
);
6561

6662
// FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032
67-
// if is_windows() {
68-
// // Check that an executable does not export any dynamic symbols
69-
// symbols_check(&exe_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib")
70-
//, false);
71-
// symbols_check(
72-
// &exe_name,
73-
// SymbolCheckType::StrSymbol("public_rust_function_from_exe"),
74-
// false,
75-
// );
76-
// }
63+
if is_msvc() {
64+
// Check that an executable does not export any dynamic symbols
65+
symbols_check(&exe_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), false);
66+
symbols_check(
67+
&exe_name,
68+
SymbolCheckType::StrSymbol("public_rust_function_from_exe"),
69+
false,
70+
);
71+
}
7772

7873
// Check the combined case, where we generate a cdylib and an rlib in the same
7974
// compilation session:
@@ -131,44 +126,37 @@ fn main() {
131126
);
132127

133128
// FIXME(nbdd0121): This is broken in MinGW, see https://github.com/rust-lang/rust/pull/95604#issuecomment-1101564032
134-
// if is_windows() {
135-
// // Check that an executable does not export any dynamic symbols
136-
// symbols_check(&exe_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib")
137-
//, false);
138-
// symbols_check(
139-
// &exe_name,
140-
// SymbolCheckType::StrSymbol("public_rust_function_from_exe"),
141-
// false,
142-
// );
143-
// }
129+
if is_msvc() {
130+
// Check that an executable does not export any dynamic symbols
131+
symbols_check(&exe_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), false);
132+
symbols_check(
133+
&exe_name,
134+
SymbolCheckType::StrSymbol("public_rust_function_from_exe"),
135+
false,
136+
);
137+
}
144138
}
145139

146140
#[track_caller]
147141
fn symbols_check(path: &str, symbol_check_type: SymbolCheckType, exists_once: bool) {
148-
let mut nm = llvm_nm();
149-
if is_windows() {
150-
nm.arg("--extern-only");
151-
} else if is_darwin() {
152-
nm.arg("--extern-only").arg("--defined-only");
153-
} else {
154-
nm.arg("--dynamic");
142+
let binary_data = rfs::read(path);
143+
let file = object::File::parse(&*binary_data).unwrap();
144+
let mut found: u64 = 0;
145+
for export in file.exports().unwrap() {
146+
let name = std::str::from_utf8(export.name()).unwrap();
147+
if has_symbol(name, symbol_check_type) {
148+
found += 1;
149+
}
155150
}
156-
let out = nm.input(path).run().stdout_utf8();
157-
assert_eq!(
158-
out.lines()
159-
.filter(|&line| !line.contains("__imp_") && has_symbol(line, symbol_check_type))
160-
.count()
161-
== 1,
162-
exists_once
163-
);
151+
assert_eq!(found, exists_once as u64);
164152
}
165153

166-
fn has_symbol(line: &str, symbol_check_type: SymbolCheckType) -> bool {
154+
fn has_symbol(name: &str, symbol_check_type: SymbolCheckType) -> bool {
167155
if let SymbolCheckType::StrSymbol(expected) = symbol_check_type {
168-
line.contains(expected)
156+
!name.contains("__imp_") && name.contains(expected)
169157
} else {
170158
let regex = regex::Regex::new(r#"_ZN.*h.*E\|_R[a-zA-Z0-9_]+"#).unwrap();
171-
regex.is_match(line)
159+
regex.is_match(name)
172160
}
173161
}
174162

0 commit comments

Comments
 (0)