#shared #dll #dylib #platform-independent #so

dlopen

Library for opening and operating on dynamic link libraries (also known as shared objects or shared libraries). This is a modern and more flexible alternative to the already existing libraries like libloading or sharedlib

9 releases

Uses old Rust 2015

0.1.8 Aug 16, 2019
0.1.7 Jun 9, 2019
0.1.6 Dec 28, 2018
0.1.5 Jul 17, 2018
0.1.2 Sep 6, 2017

#179 in Operating systems

Download history 12138/week @ 2024-06-16 11658/week @ 2024-06-23 14842/week @ 2024-06-30 11419/week @ 2024-07-07 11644/week @ 2024-07-14 10209/week @ 2024-07-21 13155/week @ 2024-07-28 15245/week @ 2024-08-04 12551/week @ 2024-08-11 14587/week @ 2024-08-18 15896/week @ 2024-08-25 17359/week @ 2024-09-01 15136/week @ 2024-09-08 12414/week @ 2024-09-15 13842/week @ 2024-09-22 14067/week @ 2024-09-29

57,327 downloads per month
Used in 221 crates (33 directly)

MIT license

79KB
1K SLoC

rust-dlopen

Travis CI Appveyor CI Crates CI Codedov CI Docs

Overview

This library is my effort to make use of dynamic link libraries in Rust simple. Previously existing solutions were either unsafe, provided huge overhead of required writing too much code to achieve simple things. I hope that this library will help you to quickly get what you need and avoid errors.

Quick example

extern crate dlopen;
#[macro_use]
extern crate dlopen_derive;
use dlopen::wrapper::{Container, WrapperApi};

#[derive(WrapperApi)]
struct Api<'a> {
    example_rust_fun: fn(arg: i32) -> u32,
    example_c_fun: unsafe extern "C" fn(),
    example_reference: &'a mut i32,
}

fn main(){
    let mut cont: Container<Api> =
        unsafe { Container::load("libexample.so") }.expect("Could not open library or load symbols");
    cont.example_rust_fun(5);
    unsafe{cont.example_c_fun()};
    *cont.example_reference_mut() = 5;
}

Features

Main features

  • Supports majority of platforms and is platform independent.
  • Is consistent with Rust error handling mechanism and makes making mistakes much more difficult.
  • Is very lightweight. It mostly uses zero cost wrappers to create safer abstractions over platform API.
  • Is thread safe.
  • Is object-oriented programming friendly.
  • Has a low-level API that provides full flexibility of using libraries.
  • Has two high-level APIs that protect against dangling symbols - each in its own way.
  • High level APIs support automatic loading of symbols into structures. You only need to define a structure that represents an API. The rest happens automatically and requires only minimal amount of code.
  • Automatic loading of symbols helps you to follow the DRY paradigm.

Compare with other libraries

Feature dlopen libloading sharedlib
Basic functionality Yes Yes Yes
Multiplatform Yes Yes Yes
Dangling symbol prevention Yes Yes Yes
Thread safety Yes Potential problem with SetErrorMode() on older Windows platforms No support for SetErrorMode (library may block the application on Windows)
Loading of symbols into structures Yes No No
Overhead Minimal Minimal Some overhead
Low-level, unsafe API Yes Yes Yes
Object-oriented friendly Yes No Yes
Load from the program itself Yes No No
Obtaining address information (dladdr) Yes Unix only No

Safety

Please note that while Rust aims at being 100% safe language, it does not yet provide mechanisms that would allow me to create a 100% safe library, so I had to settle on 99%. Also the nature of dynamic link libraries requires casting obtained pointers into types that are defined on the application side. And this cannot be safe. Having said that I still think that this library provides the best approach and greatest safety possible in Rust.

Usage:

Cargo.toml:

[dependencies]
dlopen = "0.1"

Documentation

Cargo documentation

License

This code is licensed under MIT license.

Changelog

GitHub changelog

Acknowledgement

Special thanks to Simonas Kazlauskas whose libloading became code base for my project.

Dependencies

~2MB
~48K SLoC