Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a symbol from a Rust executable?

Tags:

rust

I'm trying to export a symbol from a Rust executable:

#[allow(non_upper_case_globals)]
#[no_mangle]
pub static exported_symbol: [u8; 1] = *b"\0";

fn main() {
    println!("Hello, world!");
}

exported_symbol doesn't appear to be exported by the resulting binary:

$ cargo build
$ nm ./target/debug/test_export| grep exported_symbol

On the other hand, if I build a library with the same source, the symbol does get exported:

$ rustc --crate-type cdylib src/main.rs
$ nm libmain.so| grep exported_symbol
0000000000016c10 R exported_symbol

I'm using Rust 1.18.0 on Linux x86-64.

like image 345
Frederik Deweerdt Avatar asked May 01 '17 01:05

Frederik Deweerdt


Video Answer


1 Answers

You can pass linker options to rustc, like:

$ rustc src/main.rs --crate-type bin -C link-args=-Wl,-export-dynamic
$ nm main|grep export
00000000000d79c4 R exported_symbol

You'll probably want to put this in your rustflags in your .cargo/config something like:

[target.x86_64-unknown-linux-gnu]
rustflags = [ "-C", "link-args=-Wl,-export-dynamic" ]
like image 142
Evin Robertson Avatar answered Sep 21 '22 08:09

Evin Robertson