Skip to content

Commit 2850388

Browse files
committed
add codegen flag export symbols from executable
1 parent e371eeb commit 2850388

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
@@ -644,9 +644,12 @@ impl<'a> Linker for GccLinker<'a> {
644644

645645
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
646646
// Symbol visibility in object files typically takes care of this.
647-
if crate_type == CrateType::Executable && self.sess.target.override_export_symbols.is_none()
648-
{
649-
return;
647+
if crate_type == CrateType::Executable {
648+
if self.sess.target.override_export_symbols.is_none()
649+
&& !self.sess.opts.cg.export_executable_symbols
650+
{
651+
return;
652+
}
650653
}
651654

652655
// 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
@@ -573,6 +573,7 @@ fn test_codegen_options_tracking_hash() {
573573
tracked!(debug_assertions, Some(true));
574574
tracked!(debuginfo, 0xdeadbeef);
575575
tracked!(embed_bitcode, false);
576+
tracked!(export_executable_symbols, true);
576577
tracked!(force_frame_pointers, Some(false));
577578
tracked!(force_unwind_tables, Some(true));
578579
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
@@ -1054,6 +1054,8 @@ options! {
10541054
"allow the linker to link its default libraries (default: no)"),
10551055
embed_bitcode: bool = (true, parse_bool, [TRACKED],
10561056
"emit bitcode in rlibs (default: yes)"),
1057+
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
1058+
"export symbols from executables, as if they were dynamic libraries"),
10571059
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
10581060
"extra data to put in each output filename"),
10591061
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)