mirror of
https://github.com/Noratrieb/rustv32i.git
synced 2026-01-14 21:35:02 +01:00
init
This commit is contained in:
commit
7183421b8f
10 changed files with 378 additions and 0 deletions
145
src/elf.rs
Normal file
145
src/elf.rs
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
use eyre::{Result, bail};
|
||||
|
||||
pub struct Elf {
|
||||
pub content: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Header {
|
||||
pub e_entry: u32,
|
||||
pub e_phoff: u32,
|
||||
pub e_shoff: u32,
|
||||
pub e_flags: u32,
|
||||
pub e_ehsize: u16,
|
||||
pub e_phentsize: u16,
|
||||
pub e_phnum: u16,
|
||||
pub e_shentsize: u16,
|
||||
pub e_shnum: u16,
|
||||
pub e_shstrndx: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Phdr {
|
||||
pub p_type: u32,
|
||||
pub p_offset: u32,
|
||||
pub p_vaddr: u32,
|
||||
pub p_paddr: u32,
|
||||
pub p_filesz: u32,
|
||||
pub p_memsz: u32,
|
||||
pub p_flags: u32,
|
||||
pub p_align: u32,
|
||||
}
|
||||
|
||||
impl Elf {
|
||||
pub fn header(&self) -> Result<Header> {
|
||||
let (ident, rest) = self.content.split_bytes(16)?;
|
||||
if ident[..4] != *b"\x7fELF" {
|
||||
bail!("not an elf file (invalid magic)");
|
||||
}
|
||||
// ELFCLASS32
|
||||
if ident[5] != 1 {
|
||||
bail!("not a ELF32 file (EI_CLASS={})", ident[5]);
|
||||
}
|
||||
|
||||
let (e_type, rest) = rest.split_u16()?;
|
||||
// ET_EXEC
|
||||
if e_type != 2 {
|
||||
bail!("not a static executable: {e_type}");
|
||||
}
|
||||
|
||||
let (e_machine, rest) = rest.split_u16()?;
|
||||
// EM_RISCV
|
||||
if e_machine != 243 {
|
||||
bail!("not a RISC-V executable");
|
||||
}
|
||||
|
||||
let (_e_version, rest) = rest.split_u32()?;
|
||||
|
||||
let (e_entry, rest) = rest.split_u32()?;
|
||||
let (e_phoff, rest) = rest.split_u32()?;
|
||||
let (e_shoff, rest) = rest.split_u32()?;
|
||||
let (e_flags, rest) = rest.split_u32()?;
|
||||
let (e_ehsize, rest) = rest.split_u16()?;
|
||||
let (e_phentsize, rest) = rest.split_u16()?;
|
||||
let (e_phnum, rest) = rest.split_u16()?;
|
||||
let (e_shentsize, rest) = rest.split_u16()?;
|
||||
let (e_shnum, rest) = rest.split_u16()?;
|
||||
let (e_shstrndx, _) = rest.split_u16()?;
|
||||
|
||||
Ok(Header {
|
||||
e_entry,
|
||||
e_phoff,
|
||||
e_shoff,
|
||||
e_flags,
|
||||
e_ehsize,
|
||||
e_phentsize,
|
||||
e_phnum,
|
||||
e_shentsize,
|
||||
e_shnum,
|
||||
e_shstrndx,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn segments(&self) -> Result<Vec<Phdr>> {
|
||||
let header = self.header()?;
|
||||
|
||||
let (_, phdrs) = self.content.split_bytes(header.e_phoff as usize)?;
|
||||
let (mut phdrs, _) = phdrs.split_bytes((header.e_phentsize * header.e_phnum) as usize)?;
|
||||
|
||||
let mut parsed_phdrs = vec![];
|
||||
|
||||
while !phdrs.is_empty() {
|
||||
let phdr;
|
||||
(phdr, phdrs) = phdrs.split_bytes(header.e_phentsize as usize)?;
|
||||
|
||||
let (p_type, phdr) = phdr.split_u32()?;
|
||||
let (p_offset, phdr) = phdr.split_u32()?;
|
||||
let (p_vaddr, phdr) = phdr.split_u32()?;
|
||||
let (p_paddr, phdr) = phdr.split_u32()?;
|
||||
let (p_filesz, phdr) = phdr.split_u32()?;
|
||||
let (p_memsz, phdr) = phdr.split_u32()?;
|
||||
let (p_flags, phdr) = phdr.split_u32()?;
|
||||
let (p_align, _) = phdr.split_u32()?;
|
||||
|
||||
parsed_phdrs.push(Phdr {
|
||||
p_type,
|
||||
p_offset,
|
||||
p_vaddr,
|
||||
p_paddr,
|
||||
p_filesz,
|
||||
p_memsz,
|
||||
p_flags,
|
||||
p_align,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(parsed_phdrs)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SplitAtCheckedErr {
|
||||
fn split_bytes(&self, split: usize) -> Result<(&[u8], &[u8])>;
|
||||
fn split_u16(&self) -> Result<(u16, &[u8])>;
|
||||
fn split_u32(&self) -> Result<(u32, &[u8])>;
|
||||
fn split_u64(&self) -> Result<(u64, &[u8])>;
|
||||
}
|
||||
impl SplitAtCheckedErr for [u8] {
|
||||
fn split_bytes(&self, mid: usize) -> Result<(&[u8], &[u8])> {
|
||||
if self.len() < mid {
|
||||
bail!("invalid file: too short");
|
||||
}
|
||||
Ok(self.split_at(mid))
|
||||
}
|
||||
fn split_u16(&self) -> Result<(u16, &[u8])> {
|
||||
let (bytes, rest) = self.split_bytes(2)?;
|
||||
Ok((u16::from_le_bytes(bytes.try_into().unwrap()), rest))
|
||||
}
|
||||
fn split_u32(&self) -> Result<(u32, &[u8])> {
|
||||
let (bytes, rest) = self.split_bytes(4)?;
|
||||
Ok((u32::from_le_bytes(bytes.try_into().unwrap()), rest))
|
||||
}
|
||||
fn split_u64(&self) -> Result<(u64, &[u8])> {
|
||||
let (bytes, rest) = self.split_bytes(8)?;
|
||||
Ok((u64::from_le_bytes(bytes.try_into().unwrap()), rest))
|
||||
}
|
||||
}
|
||||
41
src/emu.rs
Normal file
41
src/emu.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use aligned_vec::{AVec, ConstAlign};
|
||||
|
||||
use crate::PAGE_SIZE;
|
||||
|
||||
pub struct Memory {
|
||||
pub mem: AVec<u8, ConstAlign<PAGE_SIZE>>,
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
fn load_u32(&self, addr: u32) -> Result<u32, ()> {
|
||||
Ok(u32::from_le_bytes(
|
||||
self.mem
|
||||
.get((addr as usize)..)
|
||||
.ok_or(())?
|
||||
.get(..4)
|
||||
.ok_or(())?
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Emulator {
|
||||
pub mem: Memory,
|
||||
pub xreg: [u32; 32],
|
||||
pub pc: u32,
|
||||
}
|
||||
|
||||
impl Emulator {
|
||||
pub fn execute(&mut self) -> Result<(), ()> {
|
||||
loop {
|
||||
self.step();
|
||||
}
|
||||
}
|
||||
|
||||
fn step(&mut self) -> Result<(), ()> {
|
||||
let instruction = self.mem.load_u32(self.pc)?;
|
||||
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
71
src/main.rs
Normal file
71
src/main.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
use aligned_vec::avec;
|
||||
use eyre::{OptionExt, bail};
|
||||
|
||||
mod elf;
|
||||
mod emu;
|
||||
|
||||
const PAGE_SIZE: usize = 4096;
|
||||
|
||||
// 2 MiB
|
||||
const MEMORY_SIZE: usize = 2 << 21;
|
||||
|
||||
fn main() -> eyre::Result<()> {
|
||||
let content = std::fs::read(std::env::args().nth(1).unwrap()).unwrap();
|
||||
|
||||
let elf = elf::Elf { content };
|
||||
let header = elf.header()?;
|
||||
|
||||
dbg!(&header);
|
||||
|
||||
let segments = elf.segments()?;
|
||||
|
||||
let mut mem = emu::Memory {
|
||||
mem: avec![[PAGE_SIZE]| 0; MEMORY_SIZE],
|
||||
};
|
||||
|
||||
for phdr in segments {
|
||||
dbg!(&phdr);
|
||||
match phdr.p_type {
|
||||
// PT_NULL
|
||||
0 => {}
|
||||
// PT_LOAD
|
||||
1 => {
|
||||
if phdr.p_filesz > 0 {
|
||||
let contents = &elf
|
||||
.content
|
||||
.get((phdr.p_offset as usize)..)
|
||||
.ok_or_eyre("invalid offset")?
|
||||
.get(..(phdr.p_filesz as usize))
|
||||
.ok_or_eyre("invalid offset")?;
|
||||
|
||||
mem.mem
|
||||
.get_mut((phdr.p_vaddr as usize)..)
|
||||
.ok_or_eyre("invalid offset")?
|
||||
.get_mut(..(phdr.p_filesz as usize))
|
||||
.ok_or_eyre("invalid offset")?
|
||||
.copy_from_slice(contents);
|
||||
}
|
||||
}
|
||||
// PT_PHDR
|
||||
6 => {}
|
||||
// PT_GNU_EH_FRAME
|
||||
1685382480 => {}
|
||||
// PT_GNU_STACK
|
||||
1685382481 => {}
|
||||
// PT_RISCV_ATTRIBUTES
|
||||
0x70000003 => {}
|
||||
_ => bail!("unknown program header type: {}", phdr.p_type),
|
||||
}
|
||||
}
|
||||
|
||||
let start = header.e_entry;
|
||||
|
||||
let mut emu = emu::Emulator {
|
||||
mem,
|
||||
xreg: [0; 32],
|
||||
pc: start,
|
||||
};
|
||||
emu.execute().unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue