Skip to content

Commit 46616f3

Browse files
committed
pass --gc-sections if -Zexport-executable-symbols is enabled and improve tests
1 parent 915e535 commit 46616f3

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,12 +2542,9 @@ fn add_order_independent_options(
25422542
// sections to ensure we have all the data for PGO.
25432543
let keep_metadata =
25442544
crate_type == CrateType::Dylib || sess.opts.cg.profile_generate.enabled();
2545-
if crate_type != CrateType::Executable || !sess.opts.unstable_opts.export_executable_symbols
2546-
{
2547-
cmd.gc_sections(keep_metadata);
2548-
} else {
2549-
cmd.no_gc_sections();
2550-
}
2545+
cmd.gc_sections(keep_metadata);
2546+
} else {
2547+
cmd.no_gc_sections();
25512548
}
25522549

25532550
cmd.set_output_kind(link_output_kind, crate_type, out_filename);

tests/run-make/export-executable-symbols/rmake.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@
44
// symbol.
55
// See https://github.com/rust-lang/rust/pull/85673
66

7-
//@ only-unix
8-
// Reason: the export-executable-symbols flag only works on Unix
9-
// due to hardcoded platform-specific implementation
10-
// (See #85673)
11-
//@ ignore-cross-compile
127
//@ ignore-wasm
138

14-
use run_make_support::{bin_name, llvm_readobj, rustc};
9+
use run_make_support::{assert_contains, bin_name, llvm_nm, llvm_readobj, rustc, target};
1510

1611
fn main() {
17-
rustc().arg("-Zexport-executable-symbols").input("main.rs").crate_type("bin").run();
18-
llvm_readobj()
19-
.symbols()
20-
.input(bin_name("main"))
21-
.run()
22-
.assert_stdout_contains("exported_symbol");
12+
let target = target();
13+
rustc()
14+
.arg("-Ctarget-feature=-crt-static")
15+
.arg("-Zexport-executable-symbols")
16+
.input("main.rs")
17+
.crate_type("bin")
18+
.run();
19+
if target.contains("linux") {
20+
let output = llvm_nm()
21+
.arg("--dynamic")
22+
.arg("--defined-only")
23+
.input(bin_name("main"))
24+
.run()
25+
.invalid_stdout_utf8();
26+
assert_contains(&output, "exported_symbol");
27+
} else {
28+
let output = llvm_readobj().symbols().input(bin_name("main")).run().invalid_stdout_utf8();
29+
assert_contains(&output, "exported_symbol");
30+
}
2331
}
Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
//@ run-pass
2-
//@ only-linux
3-
//@ only-gnu
4-
//@ compile-flags: -Zexport-executable-symbols
2+
//@ compile-flags: -Ctarget-feature=-crt-static -Zexport-executable-symbols
3+
//@ ignore-wasm
54
//@ edition: 2024
65

76
// Regression test for <https://github.com/rust-lang/rust/issues/101610>.
87

98
#![feature(rustc_private)]
109

11-
extern crate libc;
12-
1310
#[unsafe(no_mangle)]
1411
fn hack() -> u64 {
1512
998244353
1613
}
1714

1815
fn main() {
16+
#[cfg(unix)]
1917
unsafe {
18+
extern crate libc;
2019
let handle = libc::dlopen(std::ptr::null(), libc::RTLD_NOW);
2120
let ptr = libc::dlsym(handle, c"hack".as_ptr());
2221
let ptr: Option<unsafe fn() -> u64> = std::mem::transmute(ptr);
@@ -27,4 +26,24 @@ fn main() {
2726
panic!("symbol `hack` is not found");
2827
}
2928
}
29+
#[cfg(windows)]
30+
unsafe {
31+
type PCSTR = *const u8;
32+
type HMODULE = *mut core::ffi::c_void;
33+
type FARPROC = Option<unsafe extern "system" fn() -> isize>;
34+
#[link(name = "kernel32", kind = "raw-dylib")]
35+
unsafe extern "system" {
36+
fn GetModuleHandleA(lpmodulename: PCSTR) -> HMODULE;
37+
fn GetProcAddress(hmodule: HMODULE, lpprocname: PCSTR) -> FARPROC;
38+
}
39+
let handle = GetModuleHandleA(std::ptr::null_mut());
40+
let ptr = GetProcAddress(handle, b"hack\0".as_ptr());
41+
let ptr: Option<unsafe fn() -> u64> = std::mem::transmute(ptr);
42+
if let Some(f) = ptr {
43+
assert!(f() == 998244353);
44+
println!("symbol `hack` is found successfully");
45+
} else {
46+
panic!("symbol `hack` is not found");
47+
}
48+
}
3049
}

0 commit comments

Comments
 (0)