Skip to content

Commit 6aa0984

Browse files
authored
add export executable symbols make run
1 parent d07db09 commit 6aa0984

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'a> Linker for GccLinker<'a> {
643643
// Symbol visibility in object files typically takes care of this.
644644
if crate_type == CrateType::Executable {
645645
if self.sess.target.override_export_symbols.is_none()
646-
&& !self.sess.opts.cg.export_executable_symbols
646+
&& !self.sess.opts.debugging_opts.export_executable_symbols
647647
{
648648
return;
649649
}
@@ -966,7 +966,7 @@ impl<'a> Linker for MsvcLinker<'a> {
966966
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
967967
// Symbol visibility takes care of this typically
968968
if crate_type == CrateType::Executable {
969-
if !self.sess.opts.cg.export_executable_symbols {
969+
if !self.sess.opts.debugging_opts.export_executable_symbols {
970970
return;
971971
}
972972
}

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,6 @@ 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);
576575
tracked!(force_frame_pointers, Some(false));
577576
tracked!(force_unwind_tables, Some(true));
578577
tracked!(inline_threshold, Some(0xf007ba11));
@@ -734,6 +733,7 @@ fn test_debugging_options_tracking_hash() {
734733
tracked!(debug_macros, true);
735734
tracked!(dep_info_omit_d_target, true);
736735
tracked!(drop_tracking, true);
736+
tracked!(export_executable_symbols, true);
737737
tracked!(dual_proc_macros, true);
738738
tracked!(fewer_names, Some(true));
739739
tracked!(force_unstable_if_unmarked, true);

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,6 @@ 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"),
10471045
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
10481046
"extra data to put in each output filename"),
10491047
force_frame_pointers: Option<bool> = (None, parse_opt_bool, [TRACKED],
@@ -1232,6 +1230,8 @@ options! {
12321230
an additional `.html` file showing the computed coverage spans."),
12331231
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
12341232
"emit a section containing stack size metadata (default: no)"),
1233+
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
1234+
"export symbols from executables, as if they were dynamic libraries"),
12351235
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],
12361236
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \
12371237
(default: no)"),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
all:
4+
$(RUSTC) --edition=2018 --crate-type=cdylib foo.rs -o $(TMPDIR)/libfoo.so
5+
$(RUSTC) --edition=2018 -Zexport-executable-symbols -lfoo -L $(TMPDIR) main.rs -o $(TMPDIR)/main
6+
$(call $(TMPDIR)/main)
7+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extern "C" {
2+
fn exported_symbol() -> i8;
3+
}
4+
5+
#[no_mangle]
6+
pub extern "C" fn call_exported_symbol() -> i8 {
7+
unsafe { exported_symbol() }
8+
}
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
// edition:2018
22

3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
use std::ffi::*;
7+
use std::os::unix::ffi::*;
8+
39
fn main() {
4-
foo();
10+
let path = std::env::var("TMPDIR").unwrap();
11+
let path = std::path::PathBuf::from(path).join("libfoo.so");
12+
13+
let s = CString::new(path.as_os_str().as_bytes()).unwrap();
14+
let handle = unsafe { libc::dlopen(s.as_ptr(), libc::RTLD_LAZY | libc::RTLD_GLOBAL) };
15+
if handle.is_null() {
16+
let msg = unsafe { CStr::from_ptr(libc::dlerror() as *const _) };
17+
panic!("failed to dlopen lib {:?}", msg);
18+
}
19+
20+
unsafe {
21+
libc::dlerror();
22+
}
23+
24+
let raw_string = CString::new("call_exported_symbol").unwrap();
25+
let symbol = unsafe { libc::dlsym(handle as *mut libc::c_void, raw_string.as_ptr()) };
26+
if symbol.is_null() {
27+
let msg = unsafe { CStr::from_ptr(libc::dlerror() as *const _) };
28+
panic!("failed to load symbol {:?}", msg);
29+
}
30+
let func: extern "C" fn() -> i8 = unsafe { std::mem::transmute(symbol) };
31+
assert_eq!(func(), 42);
532
}
633

734
#[no_mangle]
8-
pub extern "C" fn foo() -> i32 {
9-
1 + 1
35+
pub fn exported_symbol() -> i8 {
36+
42
1037
}

0 commit comments

Comments
 (0)