Skip to content

Commit 6674c94

Browse files
committed
feat: impl export-executable-symbols
1 parent babff22 commit 6674c94

File tree

7 files changed

+42
-5
lines changed

7 files changed

+42
-5
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,12 @@ fn add_order_independent_options(
20822082
// sections to ensure we have all the data for PGO.
20832083
let keep_metadata =
20842084
crate_type == CrateType::Dylib || sess.opts.cg.profile_generate.enabled();
2085-
cmd.gc_sections(keep_metadata);
2085+
if crate_type != CrateType::Executable || !sess.opts.unstable_opts.export_executable_symbols
2086+
{
2087+
cmd.gc_sections(keep_metadata);
2088+
} else {
2089+
cmd.no_gc_sections();
2090+
}
20862091
}
20872092

20882093
cmd.set_output_kind(link_output_kind, out_filename);

compiler/rustc_codegen_ssa/src/back/linker.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,14 @@ impl<'a> Linker for GccLinker<'a> {
640640

641641
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
642642
// Symbol visibility in object files typically takes care of this.
643-
if crate_type == CrateType::Executable && self.sess.target.override_export_symbols.is_none()
644-
{
645-
return;
643+
if crate_type == CrateType::Executable {
644+
let should_export_executable_symbols =
645+
self.sess.opts.unstable_opts.export_executable_symbols;
646+
if self.sess.target.override_export_symbols.is_none()
647+
&& !should_export_executable_symbols
648+
{
649+
return;
650+
}
646651
}
647652

648653
// We manually create a list of exported symbols to ensure we don't expose any more.
@@ -969,7 +974,11 @@ impl<'a> Linker for MsvcLinker<'a> {
969974
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
970975
// Symbol visibility takes care of this typically
971976
if crate_type == CrateType::Executable {
972-
return;
977+
let should_export_executable_symbols =
978+
self.sess.opts.unstable_opts.export_executable_symbols;
979+
if !should_export_executable_symbols {
980+
return;
981+
}
973982
}
974983

975984
let path = tmpdir.join("lib.def");

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ fn test_unstable_options_tracking_hash() {
733733
tracked!(debug_macros, true);
734734
tracked!(dep_info_omit_d_target, true);
735735
tracked!(drop_tracking, true);
736+
tracked!(export_executable_symbols, true);
736737
tracked!(dual_proc_macros, true);
737738
tracked!(dwarf_version, Some(5));
738739
tracked!(emit_thin_lto, false);

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,8 @@ options! {
12821282
"emit a section containing stack size metadata (default: no)"),
12831283
emit_thin_lto: bool = (true, parse_bool, [TRACKED],
12841284
"emit the bc module with thin LTO info (default: yes)"),
1285+
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
1286+
"export symbols from executables, as if they were dynamic libraries"),
12851287
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],
12861288
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \
12871289
(default: no)"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
# ignore-wasm32
4+
# ignore-wasm64
5+
# ignore-none no-std is not supported
6+
# only-linux
7+
8+
all:
9+
$(RUSTC) -Zexport-executable-symbols main.rs --target $(TARGET) --crate-type=bin
10+
nm $(TMPDIR)/main | $(CGREP) exported_symbol
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// edition:2018
2+
3+
fn main() {}
4+
5+
#[no_mangle]
6+
pub fn exported_symbol() -> i8 {
7+
42
8+
}

src/test/rustdoc-ui/z-help.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
-Z dwarf-version=val -- version of DWARF debug information to emit (default: 2 or 4, depending on platform)
3838
-Z emit-stack-sizes=val -- emit a section containing stack size metadata (default: no)
3939
-Z emit-thin-lto=val -- emit the bc module with thin LTO info (default: yes)
40+
-Z export-executable-symbols=val -- export symbols from executables, as if they were dynamic libraries
4041
-Z fewer-names=val -- reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) (default: no)
4142
-Z force-unstable-if-unmarked=val -- force all crates to be `rustc_private` unstable (default: no)
4243
-Z fuel=val -- set the optimization fuel quota for a crate

0 commit comments

Comments
 (0)