Skip to content
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

RFC-2841: add codegen flag export symbols from executable #85673

Merged
merged 1 commit into from
Jul 25, 2022
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: 6 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,12 @@ 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();
cmd.gc_sections(keep_metadata);
if crate_type != CrateType::Executable || !sess.opts.unstable_opts.export_executable_symbols
{
cmd.gc_sections(keep_metadata);
} else {
cmd.no_gc_sections();
}
csmoe marked this conversation as resolved.
Show resolved Hide resolved
}

cmd.set_output_kind(link_output_kind, out_filename);
Expand Down
17 changes: 13 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,14 @@ impl<'a> Linker for GccLinker<'a> {

fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
// Symbol visibility in object files typically takes care of this.
if crate_type == CrateType::Executable && self.sess.target.override_export_symbols.is_none()
{
return;
if crate_type == CrateType::Executable {
let should_export_executable_symbols =
self.sess.opts.unstable_opts.export_executable_symbols;
if self.sess.target.override_export_symbols.is_none()
&& !should_export_executable_symbols
{
return;
}
}

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

let path = tmpdir.join("lib.def");
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ fn test_unstable_options_tracking_hash() {
tracked!(debug_macros, true);
tracked!(dep_info_omit_d_target, true);
tracked!(drop_tracking, true);
tracked!(export_executable_symbols, true);
tracked!(dual_proc_macros, true);
tracked!(dwarf_version, Some(5));
tracked!(emit_thin_lto, false);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,8 @@ options! {
"emit a section containing stack size metadata (default: no)"),
emit_thin_lto: bool = (true, parse_bool, [TRACKED],
"emit the bc module with thin LTO info (default: yes)"),
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
"export symbols from executables, as if they were dynamic libraries"),
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \
(default: no)"),
Expand Down
11 changes: 11 additions & 0 deletions src/test/run-make/export-executable-symbols/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-include ../../run-make-fulldeps/tools.mk

# ignore-wasm32
# ignore-wasm64
# ignore-none no-std is not supported
# only-linux

all:
$(RUSTC) -Zexport-executable-symbols main.rs --target $(TARGET) --crate-type=bin
nm $(TMPDIR)/main | $(CGREP) exported_symbol

8 changes: 8 additions & 0 deletions src/test/run-make/export-executable-symbols/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// edition:2018

fn main() {}

#[no_mangle]
pub fn exported_symbol() -> i8 {
42
}
1 change: 1 addition & 0 deletions src/test/rustdoc-ui/z-help.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
-Z dwarf-version=val -- version of DWARF debug information to emit (default: 2 or 4, depending on platform)
-Z emit-stack-sizes=val -- emit a section containing stack size metadata (default: no)
-Z emit-thin-lto=val -- emit the bc module with thin LTO info (default: yes)
-Z export-executable-symbols=val -- export symbols from executables, as if they were dynamic libraries
-Z fewer-names=val -- reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) (default: no)
-Z force-unstable-if-unmarked=val -- force all crates to be `rustc_private` unstable (default: no)
-Z fuel=val -- set the optimization fuel quota for a crate
Expand Down