Skip to content

Commit 09b0c55

Browse files
committed
add export executable symbols make run
1 parent 2850388 commit 09b0c55

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
@@ -646,7 +646,7 @@ impl<'a> Linker for GccLinker<'a> {
646646
// Symbol visibility in object files typically takes care of this.
647647
if crate_type == CrateType::Executable {
648648
if self.sess.target.override_export_symbols.is_none()
649-
&& !self.sess.opts.cg.export_executable_symbols
649+
&& !self.sess.opts.debugging_opts.export_executable_symbols
650650
{
651651
return;
652652
}
@@ -973,7 +973,7 @@ impl<'a> Linker for MsvcLinker<'a> {
973973
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
974974
// Symbol visibility takes care of this typically
975975
if crate_type == CrateType::Executable {
976-
if !self.sess.opts.cg.export_executable_symbols {
976+
if !self.sess.opts.debugging_opts.export_executable_symbols {
977977
return;
978978
}
979979
}

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,6 @@ 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);
577576
tracked!(force_frame_pointers, Some(false));
578577
tracked!(force_unwind_tables, Some(true));
579578
tracked!(inline_threshold, Some(0xf007ba11));
@@ -735,6 +734,7 @@ fn test_debugging_options_tracking_hash() {
735734
tracked!(debug_macros, true);
736735
tracked!(dep_info_omit_d_target, true);
737736
tracked!(drop_tracking, true);
737+
tracked!(export_executable_symbols, true);
738738
tracked!(dual_proc_macros, true);
739739
tracked!(fewer_names, Some(true));
740740
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
@@ -1054,8 +1054,6 @@ 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"),
10591057
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
10601058
"extra data to put in each output filename"),
10611059
force_frame_pointers: Option<bool> = (None, parse_opt_bool, [TRACKED],
@@ -1244,6 +1242,8 @@ options! {
12441242
an additional `.html` file showing the computed coverage spans."),
12451243
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
12461244
"emit a section containing stack size metadata (default: no)"),
1245+
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
1246+
"export symbols from executables, as if they were dynamic libraries"),
12471247
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],
12481248
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \
12491249
(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) --crate-type=cdylib foo.rs
5+
$(RUSTC) -Zexport-executable-symbols -lfoo -L "$(TMPDIR)" main.rs
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)