11个版本

0.2.2 2019年12月6日
0.2.1 2019年12月6日
0.1.8 2019年11月22日
0.1.1 2019年10月31日

#1973解析器实现

38 每月下载次数

MIT 许可证

54KB
1K SLoC

包含 (ELF 可执行文件/库, 9KB) examples/example-binary

Elfy

Build Status Docs

文档

Crates.io

描述

Elfy是一个用于加载和解析ELF (2) 文件的Rust包。该项目最初是一个用于ARMv7-M虚拟机的简单二进制加载器,但很快演变成了独立的包。Elfy的目标是提供一个简单、直观的接口来处理所有类型的ELF文件。

Elfy目前专注于读取静态编译的ARM可执行文件的重要数据,未来将支持更多架构和ELF功能。

使用方法

要使用Elfy,首先将其添加到项目的Cargo.toml依赖中

[dependencies]
elfy = "0.2.2"

要加载ELF文件,将Elfy作为一个外部包包含在内。现在从磁盘加载ELF文件并进行解析就像调用Elf::load(path) -> ParseElfResult<Elf>一样简单,其中path是ELF文件的任何有效std::path::Path。如果文件不存在、文件不是有效的ELF文件或解析文件时出现问题,则返回一个包含错误描述的Err(ParseElfError)

extern crate elfy;
use elfy::Elf;

fn main() {
    let elf = Elf::load("examples/example-binary").expect("Something went wrong!");

    // ...
}

可以使用Elf::try_get_section(&self, section_name) -> Option<&Section>访问加载的ELF文件中的数据。如果存在该部分,则返回Some(&Section),否则返回None

可以使用Section::data(&self) -> &SectionData来访问节内的解析数据。类型SectionData是一个枚举,表示ELF文件中可能包含的不同数据格式。

use elfy::{ Section, SectionData, SectionType, SectionFlags };

fn main() {
    // ...
    
    let text_section = elf.try_get_section(".text").expect("The section doesn't exist!");
    let header = text_section.header();


    // The .text section usually contains executable machine code and as such will be
    // parsed as raw binary data. Here we retrieve a vector of that data in `bytes` 
    if let SectionData::Bytes(bytes) = text_section.data() {
        // ...
    }

    // Sections containing executable code are of type `ProgramData`, and
    // are flagged as Alloc and Execute, meaning they take up space in a program
    // image and have execution permissions
    assert_eq!(SectionType::ProgramData, header.ty);
    assert_eq!(SectionFlags::AllocExecute, header.flags);

    // ...
}

也可以通过提供的SectionSegment迭代器来访问数据

fn main() {
    // ...

    for section in elf.sections() {
        // do something with each section
    }

    for segment in elf.segments() {
        // do something with each segment
    }

    // ...
}

无运行时依赖