Crate object

source ·
Expand description

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 relocatable object files and some executable files.

Raw struct definitions

Raw structs are defined for: ELF, Mach-O, PE/COFF, XCOFF, archive. Types and traits for zerocopy support are defined in pod and endian.

Unified read API

The read::Object trait defines the unified interface. This trait is implemented by read::File, which allows reading any file format, as well as implementations for each file format: ELF, Mach-O, COFF, PE, Wasm, XCOFF.

Low level read API

In addition to the unified read API, the various read modules define helpers that operate on the raw structs. These also provide traits that abstract over the differences between 32-bit and 64-bit versions of the file format.

Unified write API

write::Object allows building a COFF/ELF/Mach-O/XCOFF relocatable object file and then writing it out.

Low level executable writers

write::elf::Writer and write::pe::Writer allow writing executable files.

Example for unified read API

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

/// Reads a file and displays the content of the ".boot" section.
fn main() -> Result<(), Box<dyn Error>> {
 let bin_data = fs::read("./multiboot2-binary.elf")?;
 let obj_file = object::File::parse(&*bin_data)?;
 if let Some(section) = obj_file.section_by_name(".boot") {
   println!("{:#x?}", section.data()?);
 } else {
   eprintln!("section not available");
 }
 Ok(())
}

Re-exports

Modules

  • Archive definitions.
  • ELF definitions.
  • Types for compile-time and run-time endianness.
  • Mach-O definitions.
  • PE/COFF definitions.
  • Tools for converting file format structures to and from bytes.
  • Interface for reading object files.
  • Interface for writing object files.
  • XCOFF definitions

Enums