Skip to content

Commit d07db09

Browse files
authored
add codegen flag export symbols from executable
1 parent 051d117 commit d07db09

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.
@@ -963,7 +966,9 @@ impl<'a> Linker for MsvcLinker<'a> {
963966
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
964967
// Symbol visibility takes care of this typically
965968
if crate_type == CrateType::Executable {
966-
return;
969+
if !self.sess.opts.cg.export_executable_symbols {
970+
return;
971+
}
967972
}
968973

969974
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
@@ -572,6 +572,7 @@ fn test_codegen_options_tracking_hash() {
572572
tracked!(debug_assertions, Some(true));
573573
tracked!(debuginfo, 0xdeadbeef);
574574
tracked!(embed_bitcode, false);
575+
tracked!(export_executable_symbols, true);
575576
tracked!(force_frame_pointers, Some(false));
576577
tracked!(force_unwind_tables, Some(true));
577578
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
@@ -1042,6 +1042,8 @@ options! {
10421042
"allow the linker to link its default libraries (default: no)"),
10431043
embed_bitcode: bool = (true, parse_bool, [TRACKED],
10441044
"emit bitcode in rlibs (default: yes)"),
1045+
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
1046+
"export symbols from executables, as if they were dynamic libraries"),
10451047
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
10461048
"extra data to put in each output filename"),
10471049
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)