2 releases

0.1.1 Apr 10, 2024
0.1.0 Mar 5, 2024

#1496 in Parser implementations

Download history 129/week @ 2024-03-04 18/week @ 2024-03-11 21/week @ 2024-04-01 140/week @ 2024-04-08

161 downloads per month

Apache-2.0 OR MIT

1.5MB
35K SLoC

object

The object crate provides a unified interface to working with object files across platforms. It supports reading relocatable object files and executable files, and writing COFF/ELF/Mach-O/XCOFF relocatable object files and ELF/PE executable files.

For reading files, it provides multiple levels of support:

  • raw struct definitions suitable for zero copy access
  • low level APIs for accessing the raw structs (example)
  • a higher level unified API for accessing common features of object files, such as sections and symbols (example)

Supported file formats for reading: ELF, Mach-O, Windows PE/COFF, Wasm, XCOFF, and Unix archive.

For writing files, it provides:

  • low level writers for ELF, PE, and COFF
  • higher level builder for ELF (example)
  • a unified API for writing relocatable object files (ELF, Mach-O, COFF, XCOFF) (example)

Example for unified read API

use object::{Object, ObjectSection};
use std::error::Error;
use std::fs;

/// Reads a file and displays the name of each section.
fn main() -> Result<(), Box<dyn Error>> {
    let binary_data = fs::read("path/to/binary")?;
    let file = object::File::parse(&*binary_data)?;
    for section in file.sections() {
        println!("{}", section.name()?);
    }
    Ok(())
}

See crates/examples for more examples.

Minimum Supported Rust Version (MSRV)

Changes to MSRV are considered breaking changes. We are conservative about changing the MSRV, but sometimes are required to due to dependencies. The MSRV is 1.65.0.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

A library for rewriting object and executable files.

Use the Rewriter struct to read a file, modify it, and write it back. Modifications can be performed using methods on the Rewriter struct, or by passing an Options struct to the Rewriter::modify method.

Currently, only ELF files are supported, and not many modifications are possible yet.

Example

use object_rewrite::{Options, Rewriter};

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let mut options = Options::default();
  options.delete_symbols.insert(b"main".to_vec());

  let input = std::fs::read("path/to/input")?;
  let mut rewriter = Rewriter::read(&input)?;
  rewriter.modify(options)?;
  let mut output = std::fs::File::create("path/to/output")?;
  rewriter.write(&mut output)?;
  Ok(())
}

Dependencies