#elf #dlopen #shared-memory #so #loader #ldso

no-std dlopen-rs

A library for loading elf dynamic libraries from memory and files

8 releases

new 0.3.0 Sep 30, 2024
0.2.3 Sep 14, 2024
0.2.1 Aug 20, 2024
0.1.2 Aug 4, 2024
0.1.0 Jul 25, 2024

#218 in Operating systems

Download history 184/week @ 2024-07-25 205/week @ 2024-08-01 19/week @ 2024-08-08 229/week @ 2024-08-15 22/week @ 2024-08-22 2/week @ 2024-08-29 130/week @ 2024-09-05 167/week @ 2024-09-12 27/week @ 2024-09-19 163/week @ 2024-09-26

487 downloads per month

Apache-2.0

120KB
3K SLoC

license

dlopen-rs

A versatile Rust library designed for loading ELF dynamic libraries from memory or from files.

This library serves three purposes:

  1. Provide a pure Rust alternative to musl ld.so or glibc ld.so.
  2. Provide loading ELF dynamic libraries support for #![no_std] targets.
  3. Easily swap out symbols in shared libraries with your own custom symbols at runtime

Additional, it integrates seamlessly with the system’s dynamic linker in std environments when the ldso feature is enabled. Currently, it supports x86_64, x86, RV64, and AArch64 architectures.

Feature

Feature Default Description
ldso Yes Allows dynamic libraries to be loaded using system dynamic loaders (ld.so).
std Yes Enable std
debug Yes Enable this to use gdb/lldb for debugging loaded dynamic libraries. Note that only dynamic libraries loaded using dlopen-rs can be debugged with gdb.
mmap Yes Enable this on platforms that support mmap
version No Activate specific versions of symbols for dynamic library loading
tls Yes Enable this to use thread local storage.
nightly No Enable this for faster loading, but you’ll need to use the nightly compiler.
unwinding No Enable this to use the exception handling mechanism provided by dlopen-rs.
libgcc Yes Enable this if the program uses libgcc to handle exceptions.
libunwind No Enable this if the program uses libunwind to handle exceptions.

Example

use dlopen_rs::ELFLibrary;
use nix::libc::size_t;
use std::{ffi::c_void, path::Path};

extern "C" fn mymalloc(size: size_t) -> *mut c_void {
    println!("malloc:{}bytes", size);
    unsafe { nix::libc::malloc(size) }
}

fn main() {
    let path = Path::new("./target/release/libexample.so");
    let libc = ELFLibrary::sys_load("libc.so.6").unwrap();
    let libgcc = ELFLibrary::sys_load("libgcc_s.so.1").unwrap();
    let libexample = ELFLibrary::from_file(path)
        .unwrap()
        .relocate_with_func(&[libc, libgcc], |name| {
            if name == "malloc" {
                return Some(mymalloc as _);
            } else {
                return None;
            }
        })
        .unwrap();
    let f = unsafe {
        libexample
            .get::<extern "C" fn(i32) -> i32>("c_fun_add_two")
            .unwrap()
    };
    println!("{}", f(2));

    let f = unsafe {
        libexample
            .get::<extern "C" fn()>("c_fun_print_something_else")
            .unwrap()
    };
    f();
}

NOTE

If you encounter any issues while using it or need any new features, feel free to raise an issue on GitHub. https://github.com/weizhiao/dlopen-rs

Dependencies

~2.4–4MB
~73K SLoC