Skip to content

Commit

Permalink
add export executable symbols make run
Browse files Browse the repository at this point in the history
  • Loading branch information
csmoe authored Mar 21, 2022
1 parent d07db09 commit 6aa0984
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ impl<'a> Linker for GccLinker<'a> {
// Symbol visibility in object files typically takes care of this.
if crate_type == CrateType::Executable {
if self.sess.target.override_export_symbols.is_none()
&& !self.sess.opts.cg.export_executable_symbols
&& !self.sess.opts.debugging_opts.export_executable_symbols
{
return;
}
Expand Down Expand Up @@ -966,7 +966,7 @@ impl<'a> Linker for MsvcLinker<'a> {
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
// Symbol visibility takes care of this typically
if crate_type == CrateType::Executable {
if !self.sess.opts.cg.export_executable_symbols {
if !self.sess.opts.debugging_opts.export_executable_symbols {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@ fn test_codegen_options_tracking_hash() {
tracked!(debug_assertions, Some(true));
tracked!(debuginfo, 0xdeadbeef);
tracked!(embed_bitcode, false);
tracked!(export_executable_symbols, true);
tracked!(force_frame_pointers, Some(false));
tracked!(force_unwind_tables, Some(true));
tracked!(inline_threshold, Some(0xf007ba11));
Expand Down Expand Up @@ -734,6 +733,7 @@ fn test_debugging_options_tracking_hash() {
tracked!(debug_macros, true);
tracked!(dep_info_omit_d_target, true);
tracked!(drop_tracking, true);
tracked!(export_executable_symbols, true);
tracked!(dual_proc_macros, true);
tracked!(fewer_names, Some(true));
tracked!(force_unstable_if_unmarked, true);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,6 @@ options! {
"allow the linker to link its default libraries (default: no)"),
embed_bitcode: bool = (true, parse_bool, [TRACKED],
"emit bitcode in rlibs (default: yes)"),
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
"export symbols from executables, as if they were dynamic libraries"),
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
"extra data to put in each output filename"),
force_frame_pointers: Option<bool> = (None, parse_opt_bool, [TRACKED],
Expand Down Expand Up @@ -1232,6 +1230,8 @@ options! {
an additional `.html` file showing the computed coverage spans."),
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
"emit a section containing stack size metadata (default: no)"),
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
"export symbols from executables, as if they were dynamic libraries"),
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \
(default: no)"),
Expand Down
7 changes: 7 additions & 0 deletions src/test/run-make/export-executable-symbols/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-include ../../run-make-fulldeps/tools.mk

all:
$(RUSTC) --edition=2018 --crate-type=cdylib foo.rs -o $(TMPDIR)/libfoo.so
$(RUSTC) --edition=2018 -Zexport-executable-symbols -lfoo -L $(TMPDIR) main.rs -o $(TMPDIR)/main
$(call $(TMPDIR)/main)

8 changes: 8 additions & 0 deletions src/test/run-make/export-executable-symbols/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern "C" {
fn exported_symbol() -> i8;
}

#[no_mangle]
pub extern "C" fn call_exported_symbol() -> i8 {
unsafe { exported_symbol() }
}
33 changes: 30 additions & 3 deletions src/test/run-make/export-executable-symbols/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
// edition:2018

#![feature(rustc_private)]

extern crate libc;
use std::ffi::*;
use std::os::unix::ffi::*;

fn main() {
foo();
let path = std::env::var("TMPDIR").unwrap();
let path = std::path::PathBuf::from(path).join("libfoo.so");

let s = CString::new(path.as_os_str().as_bytes()).unwrap();
let handle = unsafe { libc::dlopen(s.as_ptr(), libc::RTLD_LAZY | libc::RTLD_GLOBAL) };
if handle.is_null() {
let msg = unsafe { CStr::from_ptr(libc::dlerror() as *const _) };
panic!("failed to dlopen lib {:?}", msg);
}

unsafe {
libc::dlerror();
}

let raw_string = CString::new("call_exported_symbol").unwrap();
let symbol = unsafe { libc::dlsym(handle as *mut libc::c_void, raw_string.as_ptr()) };
if symbol.is_null() {
let msg = unsafe { CStr::from_ptr(libc::dlerror() as *const _) };
panic!("failed to load symbol {:?}", msg);
}
let func: extern "C" fn() -> i8 = unsafe { std::mem::transmute(symbol) };
assert_eq!(func(), 42);
}

#[no_mangle]
pub extern "C" fn foo() -> i32 {
1 + 1
pub fn exported_symbol() -> i8 {
42
}

0 comments on commit 6aa0984

Please sign in to comment.