Last active
August 29, 2015 14:21
-
-
Save geofft/e22e9e4b72eb460359a5 to your computer and use it in GitHub Desktop.
Rust-ABI calls across dynamic libraries
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
#![crate_type="dylib"] | |
#[no_mangle] | |
pub fn foo(x: std::fmt::Arguments) -> i32 { | |
println!("{:?}", x); | |
3 | |
} |
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
#[link(name="dl")] | |
extern { | |
fn dlopen(filename: *const u8, flags: isize) -> *const u8; | |
fn dlsym(handle: *const u8, symbol: *const u8) -> *const u8; | |
} | |
fn main() { | |
let eggman = unsafe { dlopen(b"${ORIGIN}/libeggman.so\0" as *const _, 2) }; | |
let foo: fn(x: std::fmt::Arguments) -> i32 = unsafe { | |
std::mem::transmute(dlsym(eggman, b"foo\0" as *const _)) | |
}; | |
println!("{}", foo(format_args!("{}", 17))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment