Skip to content

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

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

Merged
merged 2 commits into from
Jul 18, 2025
Merged
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
7 changes: 1 addition & 6 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2542,12 +2542,7 @@ 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);
}

cmd.set_output_kind(link_output_kind, crate_type, out_filename);
Expand Down
33 changes: 0 additions & 33 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ pub(crate) trait Linker {
link_or_cc_args(self, &[path]);
}
fn gc_sections(&mut self, keep_metadata: bool);
fn no_gc_sections(&mut self);
fn full_relro(&mut self);
fn partial_relro(&mut self);
fn no_relro(&mut self);
Expand Down Expand Up @@ -688,12 +687,6 @@ impl<'a> Linker for GccLinker<'a> {
}
}

fn no_gc_sections(&mut self) {
if self.is_gnu || self.sess.target.is_like_wasm {
self.link_arg("--no-gc-sections");
}
}

fn optimize(&mut self) {
if !self.is_gnu && !self.sess.target.is_like_wasm {
return;
Expand Down Expand Up @@ -1010,10 +1003,6 @@ impl<'a> Linker for MsvcLinker<'a> {
}
}

fn no_gc_sections(&mut self) {
self.link_arg("/OPT:NOREF,NOICF");
}

fn full_relro(&mut self) {
// noop
}
Expand Down Expand Up @@ -1243,10 +1232,6 @@ impl<'a> Linker for EmLinker<'a> {
// noop
}

fn no_gc_sections(&mut self) {
// noop
}

fn optimize(&mut self) {
// Emscripten performs own optimizations
self.cc_arg(match self.sess.opts.optimize {
Expand Down Expand Up @@ -1418,10 +1403,6 @@ impl<'a> Linker for WasmLd<'a> {
self.link_arg("--gc-sections");
}

fn no_gc_sections(&mut self) {
self.link_arg("--no-gc-sections");
}

fn optimize(&mut self) {
// The -O flag is, as of late 2023, only used for merging of strings and debuginfo, and
// only differentiates -O0 and -O1. It does not apply to LTO.
Expand Down Expand Up @@ -1567,10 +1548,6 @@ impl<'a> Linker for L4Bender<'a> {
}
}

fn no_gc_sections(&mut self) {
self.link_arg("--no-gc-sections");
}

fn optimize(&mut self) {
// GNU-style linkers support optimization with -O. GNU ld doesn't
// need a numeric argument, but other linkers do.
Expand Down Expand Up @@ -1734,10 +1711,6 @@ impl<'a> Linker for AixLinker<'a> {
self.link_arg("-bgc");
}

fn no_gc_sections(&mut self) {
self.link_arg("-bnogc");
}

fn optimize(&mut self) {}

fn pgo_gen(&mut self) {
Expand Down Expand Up @@ -1982,8 +1955,6 @@ impl<'a> Linker for PtxLinker<'a> {

fn gc_sections(&mut self, _keep_metadata: bool) {}

fn no_gc_sections(&mut self) {}

fn pgo_gen(&mut self) {}

fn no_crt_objects(&mut self) {}
Expand Down Expand Up @@ -2057,8 +2028,6 @@ impl<'a> Linker for LlbcLinker<'a> {

fn gc_sections(&mut self, _keep_metadata: bool) {}

fn no_gc_sections(&mut self) {}

fn pgo_gen(&mut self) {}

fn no_crt_objects(&mut self) {}
Expand Down Expand Up @@ -2139,8 +2108,6 @@ impl<'a> Linker for BpfLinker<'a> {

fn gc_sections(&mut self, _keep_metadata: bool) {}

fn no_gc_sections(&mut self) {}

fn pgo_gen(&mut self) {}

fn no_crt_objects(&mut self) {}
Expand Down
26 changes: 14 additions & 12 deletions tests/run-make/export-executable-symbols/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
// 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
//@ ignore-cross-compile

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);
}
30 changes: 25 additions & 5 deletions tests/ui/linking/export-executable-symbols.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//@ run-pass
//@ only-linux
//@ only-gnu
//@ compile-flags: -Zexport-executable-symbols
//@ compile-flags: -Ctarget-feature=-crt-static -Zexport-executable-symbols
//@ ignore-wasm
//@ ignore-cross-compile
//@ 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);
Expand All @@ -27,4 +27,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");
}
}
}
Loading