#interpreter #patch #elf #api-bindings #rpath #runpath

no-std elb

A library that supports reading ELF files and patching RPATH, RUNPATH and interpreter

5 unstable releases

new 0.3.2 Apr 19, 2025
0.3.1 Apr 16, 2025
0.3.0 Apr 11, 2025
0.2.0 Mar 31, 2025
0.1.0 Mar 30, 2025

#1059 in Encoding

Download history 253/week @ 2025-03-29 118/week @ 2025-04-05 186/week @ 2025-04-12

557 downloads per month
Used in 2 crates

MIT/Apache

175KB
4.5K SLoC

elb

Crates.io Version Docs dependency status

ELF reader/patcher library that features

  • reading and writing ELF files,
  • patching RPATH, RUNPATH and interpreter via high-level API,
  • verifying correctness of ELF files,
  • custom patching via low-level API.

To resolve dependencies without loading and executing files, you can use elb-dl that is based on this crate.

There is also an accompanying command-line utility.

Usage

use elb::{DynamicTag, Elf, ElfPatcher, Error};
use std::fs::{File, OpenOptions};

fn read_elf() -> Result<(), Error> {
    let mut file = File::open("/bin/ls")?;
    let page_size = 4096;
    let elf = Elf::read(&mut file, page_size)?;
    eprintln!("{:#?}", elf.header);
    Ok(())
}

fn patch_elf() -> Result<(), Error> {
    let mut file = OpenOptions::new().read(true).write(true).open("/chroot/bin/ls")?;
    let page_size = 4096;
    let elf = Elf::read(&mut file, page_size)?;
    let mut patcher = ElfPatcher::new(elf, file);
    patcher.set_interpreter(c"/chroot/lib64/ld-linux-x86-64.so.2")?;
    patcher.set_library_search_path(DynamicTag::Runpath, c"/chroot/lib64:/chroot/usr/lib64")?;
    patcher.finish()?;
    Ok(())
}

References

Other ELF readers/patchers:

Dynamic linkers:

Man pages:

Linters:

Dependencies

~0.4–0.9MB
~20K SLoC