#elf #dlopen #loader #shared-memory #unix

no-std dlopen-rs

A library for loading elf dynamic libraries from memory and files

10 unstable releases (3 breaking)

new 0.4.0 Nov 17, 2024
0.3.1 Oct 12, 2024
0.3.0 Sep 30, 2024
0.2.3 Sep 14, 2024
0.1.0 Jul 25, 2024

#331 in Operating systems

Download history 227/week @ 2024-07-30 44/week @ 2024-08-06 116/week @ 2024-08-13 134/week @ 2024-08-20 3/week @ 2024-08-27 128/week @ 2024-09-03 158/week @ 2024-09-10 28/week @ 2024-09-17 134/week @ 2024-09-24 74/week @ 2024-10-01 143/week @ 2024-10-08 21/week @ 2024-10-15 2/week @ 2024-10-29 6/week @ 2024-11-05 68/week @ 2024-11-12

76 downloads per month

Apache-2.0

51KB
1K SLoC

license

dlopen-rs

English | 中文

A pure-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/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 No 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 default implementation on platforms with mmap
version No Activate specific versions of symbols for dynamic library loading
tls Yes Enable this to use thread local storage.
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.

Examples

Example 1

Fine-grained control over the dynamic library loading process, and the ability to replace certain functions in the dynamic library that need relocation with your own implementations. In this example, malloc is replaced with mymalloc.

use dlopen_rs::{ELFLibrary};
use 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 { 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(&[libc, libgcc], |name| {
            if name == "malloc" {
                return Some(mymalloc as _);
            } else {
                return None;
            }
        })
        .finish()
        .unwrap();

    let add = unsafe { libexample.get::<fn(i32, i32) -> i32>("add").unwrap() };
    println!("{}", add(1, 1));

    let print = unsafe { libexample.get::<fn(&str)>("print").unwrap() };
    print("dlopen-rs: hello world");
}

Example 2

Set the path for loading dynamic libraries:

export RUST_LD_LIBRARY_PATH=/lib32

Use the dlopen interface to load dynamic libraries:

use dlopen_rs::ELFLibrary;
use std::path::Path;

fn main() {
    let path = Path::new("./target/release/libexample.so");
    let libexample = ELFLibrary::dlopen(path).unwrap();
    let add = unsafe { libexample.get::<fn(i32, i32) -> i32>("add").unwrap() };
    println!("{}", add(1, 1));

    let print = unsafe { libexample.get::<fn(&str)>("print").unwrap() };
    print("dlopen-rs: hello world");
}

NOTE

If you encounter any issues while using the library, feel free to raise an issue on GitHub. If dlopen-rs has been helpful to you, don't hesitate to give it a star . ^V^

Dependencies

~2.5–3.5MB
~65K SLoC