Strange behavior on Dynamic Lib loading

#1

Hi @ all,
I got a very strange behavior on loading a dynamic library (libGL (gl4es)) with libdl or libc on an arm system.

extern crate libc;
use libc::{c_void, c_char, c_int, dlopen, RTLD_NOW};
fn main() {
    println!("now loading libGL.so.1");
      let mut libglx = unsafe {
        dlopen(b"/usr/lib/arm-linux-gnueabihf/libGL.so\0".as_ptr() as *const _, RTLD_NOW)
      };
      if libglx.is_null() {
	    println!("error loading libglx returns with an empty object");
      }
}
  • this program does not detect the egl display.

  • if i run the compiled debug binary with gdb it works.

  • the same code in a simple cpp program works normally.

  • also the release version of the rust code does only works within gdb.

  • I also try to simple use linked libdl with dlopen to open the library with the same result.

  • I checked the cpp compiled binary with ldd and the release and debug binary from rust code. all looks the same. (expect the use of librt and libpthread in rust)

  • i also tested the nightly and stable build of rust toolchain.

it looks like rust called the unsafe block inside an sandboxed thread or process without full access to the system. but this does not explain why it then works inside gdb, or does gdb replaced the thread calls for better debugging?

hope anybody can help me :wink:

thanks
nico

#2

Have you by any chance seen the libloading crate?
It can be used to (reasonably safely) load a dynamic library.

The example they give:

extern crate libloading as lib;

fn call_dynamic() -> lib::Result<u32> {
    let lib = lib::Library::new("/path/to/liblibrary.so")?;
    unsafe {
        let func: lib::Symbol<unsafe extern fn() -> u32> = lib.get(b"my_func")?;
        Ok(func())
    }
}