pelf

Crates.iopelf
lib.rspelf
version0.1.5
sourcesrc
created_at2023-04-18 07:34:11.510876
updated_at2023-04-24 16:23:09.723386
descriptionA library for parsing/generating/analyzing ELF
homepagehttps://github.com/Drumato/pelf
repositoryhttps://github.com/Drumato/pelf
max_upload_size
id842257
size96,463
Drumato (Drumato)

documentation

https://docs.rs/pelf

README

pelf

pelf at crates.io pelf at docs.rs

A Rust-native library for parsing/generating ELF.

Getting Started

Here's an example that parses elf file and show the attributes of its header.

fn main() {
    let args: Vec<String> = args().collect();
    if args.len() != 2 {
        panic!("usage: demo <elf-filepath>")
    }

    let mut f = File::open(&args[1]).expect("failed to open elf-file");

    let mut buf = Vec::with_capacity(4096);
    let _ = f.read_to_end(&mut buf).expect("failed to read elf-file");

    let parser = pelf::parser::ElfParserConfig::new().build();
    let elf = parser
        .parse_elf_file(&buf)
        .expect("failed to parse elf-file");

    match elf {
        pelf::file::ElfFile::Elf64(elf64) => {
            dump_elf64_file(elf64);
        }
        _ => unimplemented!(),
    }
}

fn dump_elf64_file(elf: pelf::file::Elf64File) {
    dump_elf64_header(&elf.header);
}

fn dump_elf64_header(header: &pelf::header::Elf64Header) {
    println!("ELF Header:");
    println!("    Class: {}", header.ident.class);
    println!("    Data: {}", header.ident.data);
    println!("    File Version: {}(Current)", header.ident.version);
    println!("    Type: {}", header.elf_type);
    println!("    Machine: {}", header.machine);
    println!("    Version: 0x{:x}", header.version);
    println!("    Entry: 0x{:x}", header.entry);
    println!("    PHT Offset: 0x{:x}", header.phoff);
    println!("    SHT Offset: 0x{:x}", header.shoff);
    // println!("    Flags: 0b{:b}", header.e_flags);
    println!("    PHT Entry Size: 0x{:x}", header.phentsize);
    println!("    PHT Entries: {}", header.phnum);
    println!("    SHT Entry Size: 0x{:x}", header.shentsize);
    println!("    SHT Entries: {}", header.shnum);
    println!("    Section Header String Table Index: {}", header.shstrndx);
}

features

  • 64bit ELF object files.
  • parser
    • ELF header
    • section header table
    • section(data)
    • program header table
    • symbol table
    • dynamic information table
    • relocation information table

Run examples

the other examples are placed in examples.

# parser example
$ cargo run --example demo /usr/bin/ls
Commit count: 0

cargo fmt