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.
add export executable symbols make run
- Loading branch information
Showing
6 changed files
with
50 additions
and
8 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) --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) | ||
|
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 |
---|---|---|
@@ -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 | ||
} |