forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#85673 - csmoe:export-exe-sym, r=bjorn3
RFC-2841: add codegen flag export symbols from executable Closes rust-lang#84161 r? ``@nikomatsakis`` ``@Mark-Simulacrum``
- Loading branch information
Showing
6 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-include ../../run-make-fulldeps/tools.mk | ||
|
||
all: | ||
$(RUSTC) --crate-type=cdylib foo.rs | ||
$(RUSTC) -Zexport-executable-symbols -lfoo -L "$(TMPDIR)" main.rs | ||
$(call $(TMPDIR)/main) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// edition:2018 | ||
|
||
#![feature(rustc_private)] | ||
|
||
extern crate libc; | ||
use std::ffi::*; | ||
use std::os::unix::ffi::*; | ||
|
||
fn main() { | ||
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 fn exported_symbol() -> i8 { | ||
42 | ||
} |