pass --gc-sections if -Zexport-executable-symbols is enabled and improve tests

This commit is contained in:
usamoi 2025-07-13 00:12:33 +08:00
parent 915e535244
commit f58accb8f3
3 changed files with 40 additions and 23 deletions

View file

@ -2542,12 +2542,9 @@ fn add_order_independent_options(
// sections to ensure we have all the data for PGO.
let keep_metadata =
crate_type == CrateType::Dylib || sess.opts.cg.profile_generate.enabled();
if crate_type != CrateType::Executable || !sess.opts.unstable_opts.export_executable_symbols
{
cmd.gc_sections(keep_metadata);
} else {
cmd.no_gc_sections();
}
cmd.gc_sections(keep_metadata);
} else {
cmd.no_gc_sections();
}
cmd.set_output_kind(link_output_kind, crate_type, out_filename);

View file

@ -4,20 +4,21 @@
// symbol.
// See https://github.com/rust-lang/rust/pull/85673
//@ only-unix
// Reason: the export-executable-symbols flag only works on Unix
// due to hardcoded platform-specific implementation
// (See #85673)
//@ ignore-cross-compile
//@ ignore-wasm
use run_make_support::{bin_name, llvm_readobj, rustc};
use run_make_support::object::Object;
use run_make_support::{bin_name, is_darwin, object, rustc};
fn main() {
rustc().arg("-Zexport-executable-symbols").input("main.rs").crate_type("bin").run();
llvm_readobj()
.symbols()
.input(bin_name("main"))
.run()
.assert_stdout_contains("exported_symbol");
rustc()
.arg("-Ctarget-feature=-crt-static")
.arg("-Zexport-executable-symbols")
.input("main.rs")
.crate_type("bin")
.run();
let name: &[u8] = if is_darwin() { b"_exported_symbol" } else { b"exported_symbol" };
let contents = std::fs::read(bin_name("main")).unwrap();
let object = object::File::parse(contents.as_slice()).unwrap();
let found = object.exports().unwrap().iter().any(|x| x.name() == name);
assert!(found);
}

View file

@ -1,22 +1,21 @@
//@ run-pass
//@ only-linux
//@ only-gnu
//@ compile-flags: -Zexport-executable-symbols
//@ compile-flags: -Ctarget-feature=-crt-static -Zexport-executable-symbols
//@ ignore-wasm
//@ edition: 2024
// Regression test for <https://github.com/rust-lang/rust/issues/101610>.
#![feature(rustc_private)]
extern crate libc;
#[unsafe(no_mangle)]
fn hack() -> u64 {
998244353
}
fn main() {
#[cfg(unix)]
unsafe {
extern crate libc;
let handle = libc::dlopen(std::ptr::null(), libc::RTLD_NOW);
let ptr = libc::dlsym(handle, c"hack".as_ptr());
let ptr: Option<unsafe fn() -> u64> = std::mem::transmute(ptr);
@ -27,4 +26,24 @@ fn main() {
panic!("symbol `hack` is not found");
}
}
#[cfg(windows)]
unsafe {
type PCSTR = *const u8;
type HMODULE = *mut core::ffi::c_void;
type FARPROC = Option<unsafe extern "system" fn() -> isize>;
#[link(name = "kernel32", kind = "raw-dylib")]
unsafe extern "system" {
fn GetModuleHandleA(lpmodulename: PCSTR) -> HMODULE;
fn GetProcAddress(hmodule: HMODULE, lpprocname: PCSTR) -> FARPROC;
}
let handle = GetModuleHandleA(std::ptr::null_mut());
let ptr = GetProcAddress(handle, b"hack\0".as_ptr());
let ptr: Option<unsafe fn() -> u64> = std::mem::transmute(ptr);
if let Some(f) = ptr {
assert!(f() == 998244353);
println!("symbol `hack` is found successfully");
} else {
panic!("symbol `hack` is not found");
}
}
}