riscv_asm/
lib.rs

1use std::fs;
2use std::path::Path;
3
4use goblin::{elf::Elf, Object};
5
6mod decode;
7mod instr;
8
9pub use decode::*;
10pub use instr::*;
11
12// TODO: Add an error type
13type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
14
15/// Parse an elf file from disk and return the executable code as `u32` words
16pub fn parse_elf_from_path<P: AsRef<Path>>(path: P) -> Result<Vec<u32>> {
17    let path: &Path = path.as_ref();
18    let buffer: Vec<u8> = fs::read(&path)?;
19    let elf: Elf = match Object::parse(&buffer)? {
20        Object::Elf(elf) => elf,
21        Object::PE(_pe) => {
22            // TODO: Return an error
23            panic!("{}: Expected ELF, found PE", path.to_string_lossy());
24        }
25        Object::Mach(_mach) => {
26            // TODO: Return an error
27            panic!("{}: Expected ELF, found MACH", path.to_string_lossy());
28        }
29        Object::Archive(_archive) => {
30            // TODO: Return an error
31            panic!("{}: Expected ELF, found ARCHIVE", path.to_string_lossy());
32        }
33        Object::Unknown(magic) => {
34            // TODO: Return an error
35            panic!(
36                "{}: Expected ELF, found unknown format (magic: {:#x}",
37                path.to_string_lossy(),
38                magic
39            );
40        }
41    };
42
43    parse_elf(&elf, &buffer)
44}
45
46/// Parse an elf file and return the executable code as `u32` words
47pub fn parse_elf<'a>(elf: &'a Elf, buffer: &'a [u8]) -> Result<Vec<u32>> {
48    for section in &elf.section_headers {
49        let name = &elf.shdr_strtab[section.sh_name];
50
51        if name == ".text" {
52            let start = section.sh_offset as usize;
53            let end = start + section.sh_size as usize;
54            let bytes = &buffer[start..end];
55
56            println!("Found {} bytes of code in \".text\"", bytes.len());
57
58            return Ok(bytes
59                .chunks_exact(4)
60                .map(|w| u32::from_le_bytes([w[0], w[1], w[2], w[3]]))
61                .collect());
62        }
63    }
64
65    // TODO: Error "no .tet section found - does this elf contain code?"
66    panic!("No '.text' section in elf")
67}