Chapter 04無料公開

applicationとして動かす

Drumato
Drumato
2021.08.29に更新
このチャプターの目次

applicationとして動かす

多分unused functionのwarningがいっぱい出ちゃっていると思うので,
適当にsrc/main.rsを編集します.

use io::prelude::*;
use std::io;

mod elf;
mod parser;

fn main() -> io::Result<()> {
    use std::env;
    use std::fs;
    use std::process;

    let file_path = env::args().nth(1);
    if file_path.is_none() {
        eprintln!("usage: ./main <elf-file-path>");
        process::exit(1);
    }

    let mut file = fs::File::open(file_path.unwrap())?;
    let mut raw_elf = Vec::new();
    file.read_to_end(&mut raw_elf)?;

    let elf_repr = parser::parse_64bit_elf(&raw_elf);
    if elf_repr.is_err() {
        eprintln!("failed to parse elf; err => {}", elf_repr.err().unwrap());
        process::exit(1);
    }

    let elf_file = elf_repr.unwrap().1;
    {
        println!("ELF Header:");
        println!("\tClass: {:?}", elf_file.header.id.class);
        println!("\tData: {:?}", elf_file.header.id.data);
        println!("\tEntry: 0x{:x}", elf_file.header.entry);
        println!("\tPHT Info:");
        println!("\t\tOffset: {:?}", elf_file.header.pht_offset);
        println!("\t\tEntries: {:?}", elf_file.header.pht_entries);
        println!("\t\tEntry Size: 0x{:x}", elf_file.header.pht_entry_size);
        println!("\tSHT Info:");
        println!("\t\tOffset: 0x{:x}", elf_file.header.sht_offset);
        println!("\t\tEntries: {}", elf_file.header.sht_entries);
        println!("\t\tEntry Size: 0x{:x}", elf_file.header.sht_entry_size);
        println!(
            "\t\tName Table Index: {:?}",
            elf_file.header.sht_string_table_index
        );
    }

    Ok(())
}

実行結果

$ cargo run target/debug/elf-parser-in-rust-book
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/elf-parser-in-rust-book target/debug/elf-parser-in-rust-book`
ELF Header:
        Class: Bit64
        Data: Lsb
        Entry: 0x9720
        PHT Info:
                Offset: 64
                Entries: 12
                Entry Size: 0x38
        SHT Info:
                Offset: 0x464758
                Entries: 43
                Entry Size: 0x40
                Name Table Index: 42