Crate dlopen[][src]

Library for opening and working with dynamic link libraries (also known as shared object).

Overview

This library is an 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

Compare with other libraries

Featuredlopenlibloadingsharedlib
Basic functionalityYesYesYes
MultiplatformYesYesYes
Dangling symbol preventionYesYesYes
Thread safetyYesPotential problem with SetErrorMode() on older Windows platformsNo support for SetErrorMode (library may block the application on Windows)
Loading of symbols into structuresYesNoNo
OverheadMinimalMinimalSome overhead
Low-level, unsafe APIYesYesYes
Object-oriented friendlyYesNoYes
Load from the program itselfYesNoNo
Obtaining address information (dladdr)YesUnix onlyNo

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

Examples

Changelog

License

This code is licensed under MIT license.

Acknowledgement

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

Comparison of APIs:

Additionally both high-level APIs provide a way to automatically load symbols into a structure using Rust reflection mechanism. Decision which API should be used is a matter of taste - please check documentation of both of them and use the one that you prefer. At the moment none seems to have any reasonable advantage over the other.

Modules

raw

Low-level API for opening and getting raw symbols from dynamic link libraries.

symbor

High-level and safe API for opening and getting symbols from dynamic libraries. It is based on symbol borrowing mechanism and supports automatic loading of symbols into structures.

utils

Utilities for working with dynamic link libraries.

wrapper

High-level and safe API for opening and getting symbols from dynamic link libraries. It is based on wrapping private symbols with public functions to prevent direct access and supports automatic loading of symbols into structures.

Enums

Error

This is a library-specific error that is returned by all calls to all APIs.