Segmentation fault after linking Rust staticlib with C -
i'm trying link statically against library written in rust:
#![crate_type = "staticlib"] #[no_mangle] pub extern "c" fn foo() { println!("bork!"); }
using following code in c:
void foo(); int main() { foo(); return 0; }
compile lib rustc:
rustc foo.rs
compile binary , link library:
gcc -g bar.c libfoo.a -ldl -lpthread -lrt -lgcc_s -lpthread -lc -lm -o bar
run inside debugger:
(gdb) run starting program: /home/kykc/rusttest/bar [thread debugging using libthread_db enabled] using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". program received signal sigsegv, segmentation fault. 0x00007ffff72117df in __cxa_thread_atexit_impl (func=<optimized out>, obj=<optimized out>, dso_symbol=0x0) @ cxa_thread_atexit_impl.c:67 67 cxa_thread_atexit_impl.c: no such file or directory.
gcc:
gcc-4.8.real (ubuntu 4.8.2-19ubuntu1) 4.8.2
rustc:
rustc 1.0.0-beta (9854143cb 2015-04-02) (built 2015-04-02)
it works fine dylib. doing wrong?
the problem here thread-locals destructors can used in position-independent executables (due a bug) . fix: pass -pie
flag gcc, or wait day or two.
this caused std::io::stdio::_print
, std::io::stdio::local_stdout
in standard library:
/// stdout used print! , println! macros thread_local! { static local_stdout: refcell<option<box<write + send>>> = { refcell::new(none) } } // ... pub fn _print(args: fmt::arguments) { let result = local_stdout.with(|s| { if s.borrow_state() == borrowstate::unused { if let some(w) = s.borrow_mut().as_mut() { return w.write_fmt(args); } } stdout().write_fmt(args) }); if let err(e) = result { panic!("failed printing stdout: {}", e); } }
the box
in local_stdout
has destructor in case, executable crashes when thread-local variable touched. i've filed #24445, , alex crichton has diagnosed , fixed underlying cause already.
printing doesn't require initialising threads or anything; there's fallback stdout()
in case local_stdout
contains none
(which default). passing -pie
enough convince executable print bork!
instead of crashing.
Comments
Post a Comment