Skip to content

Commit 51552e7

Browse files
committed
add codegen flag export symbols from executable
1 parent 1aabd8a commit 51552e7

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,12 @@ impl<'a> Linker for GccLinker<'a> {
641641

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

649652
// We manually create a list of exported symbols to ensure we don't expose any more.
@@ -970,7 +973,9 @@ impl<'a> Linker for MsvcLinker<'a> {
970973
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
971974
// Symbol visibility takes care of this typically
972975
if crate_type == CrateType::Executable {
973-
return;
976+
if !self.sess.opts.cg.export_executable_symbols {
977+
return;
978+
}
974979
}
975980

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

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ fn test_codegen_options_tracking_hash() {
574574
tracked!(debug_assertions, Some(true));
575575
tracked!(debuginfo, 0xdeadbeef);
576576
tracked!(embed_bitcode, false);
577+
tracked!(export_executable_symbols, true);
577578
tracked!(force_frame_pointers, Some(false));
578579
tracked!(force_unwind_tables, Some(true));
579580
tracked!(inline_threshold, Some(0xf007ba11));

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,8 @@ options! {
10891089
"allow the linker to link its default libraries (default: no)"),
10901090
embed_bitcode: bool = (true, parse_bool, [TRACKED],
10911091
"emit bitcode in rlibs (default: yes)"),
1092+
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
1093+
"export symbols from executables, as if they were dynamic libraries"),
10921094
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
10931095
"extra data to put in each output filename"),
10941096
force_frame_pointers: Option<bool> = (None, parse_opt_bool, [TRACKED],
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// edition:2018
2+
3+
fn main() {
4+
foo();
5+
}
6+
7+
#[no_mangle]
8+
pub extern "C" fn foo() -> i32 {
9+
1 + 1
10+
}

0 commit comments

Comments
 (0)