use crate::Machine; use std::f32::consts::E; use std::fs; use std::io; use std::io::BufRead; use std::io::Read; /// load a 32-bits binary file into the machine /// /// ### Parameters /// /// - **path** path of the file to load /// - **machine** the machine where the bin file will be loaded /// - **start_index** at which index of machine memory you want to start to load the program /// /// Returns in a Result any io error pub fn load(path: &str, machine: &mut Machine, start_index: usize) -> Result<(), std::io::Error> { let mut file = fs::File::open(path)?; let mut instructions: Vec = Default::default(); loop { let mut buf: [u8; 4] = [0; 4]; let res = file.read(&mut buf)?; if res == 0 { break; // eof } else { instructions.push(u32::from_le_bytes(buf)); } } for i in 0..instructions.len() { machine.write_memory(4, 4 * i + start_index, instructions[i] as u64); } // #[cfg(debug_assertions)] // println!("{:04x?}", instructions); // only print loaded program in debug build Ok(()) } struct Loader { bytes: Vec } impl Loader { pub fn load(path: &str, machine: &mut Machine, start_index: usize) -> Result { let mut file = fs::File::open(path)?; let mut instructions: Vec = Default::default(); loop { let mut buf: [u8; 1] = [0; 1]; let res = file.read(&mut buf)?; if res == 0 { break; // eof } else { instructions.push(buf[0]); } } // #[cfg(debug_assertions)] // println!("{:04x?}", instructions); // only print loaded program in debug build Ok(Self { bytes: instructions }) } fn parse(&mut self) -> Result<(), ()> { todo!(); Ok(()) } /// return true if the 4 first bytes constitude the elf magic number fn is_elf(&self) -> bool { self.bytes.get(0..4) == Option::Some(&[0x7f, 0x45, 0x4c, 0x46]) } /// return true if big endian, false otherwise fn check_endianess(&self) -> bool { self.bytes.get(5) == Option::Some(&2) } /// return true if file is 32 bits, false if 64 bits fn is_32bits(&self) -> bool { self.bytes.get(4) == Option::Some(&1) } /// return the version of the elf file (should be 1) /// Can be None if the file is smaller than 7 bytes -> the file is invalid fn get_version(&self) -> Option { self.bytes.get(6).copied() // work as primitives implements Copy } /// return true if target abi of the binary file is System V, false otherwise fn is_system_v_elf(&self) -> bool{ self.bytes.get(7) == Option::Some(&0) } /// return true if specified target instruction set architecture is RISCV fn is_riscv_isa(&self) -> bool { self.get_u16_value(0x12) == Option::Some(0xf3) } /// memory address of the entry point from where the process starts its execution fn get_entrypoint(&self, is_32bits: bool) -> Option { if is_32bits { self.get_address_point(0x18, true) } else { self.get_address_point(0x18, false) } } /// Memory address of the start of the program header table fn get_program_header_table_location(&self, is_32bits: bool) -> Option { if is_32bits { self.get_address_point(0x1c, true) } else { self.get_address_point(0x20, false) } } /// Memory address of the start of the section header table fn get_section_header_table_location(&self, is_32bits: bool) -> Option { if is_32bits { self.get_address_point(0x20, true) } else { self.get_address_point(0x28, false) } } /// Return the size of the header, normally, 0x40 for 64 bits bin and 0x34 for 32 bits fn get_elf_header_size(&self, is_32bits: bool) -> Option { let address = if is_32bits { 0x28 } else { 0x34 }; self.get_u16_value(address) } /// return the size of the program header fn get_program_header_size(&self, is_32bits: bool) -> Option { let address = if is_32bits { 0x2a } else { 0x34 }; self.get_u16_value(address) } /// return the number of entries in the program header fn get_number_entries_program_header(&self, is_32bits: bool) -> Option { let address = if is_32bits { 0x2c } else { 0x38 }; self.get_u16_value(address) } /// Return the size of the section header fn get_section_header_size(&self, is_32bits: bool) -> Option { let address = if is_32bits { 0x2e } else { 0x3a }; self.get_u16_value(address) } /// Return the number of entries in the section header fn get_section_header_num_entries(&self, is_32bits: bool) -> Option { let address = if is_32bits { 0x30 } else { 0x3c }; self.get_u16_value(address) } /// Return a u16 value, usually for the size or the number of entries inside a header fn get_u16_value(&self, address: usize) -> Option { let mut bytes: [u8; 2] = [0; 2]; bytes[0] = self.bytes.get(address).copied()?; bytes[1] = self.bytes.get(address + 1).copied()?; Option::Some(u16::from_le_bytes(bytes)) } /// return the memory address of something stored at address /// Can return None if the file is smaller than adress + 3 (or 7 if 64 bits), in this case, the elf header is incorrect fn get_address_point(&self, address: usize, is_32bits: bool) -> Option { if is_32bits { let mut bytes: [u8; 4] = [0; 4]; bytes[0] = self.bytes.get(address).copied()?; bytes[1] = self.bytes.get(address + 1).copied()?; bytes[2] = self.bytes.get(address + 2).copied()?; bytes[3] = self.bytes.get(address + 3).copied()?; Option::Some(u32::from_le_bytes(bytes) as u64) } else { let mut bytes: [u8; 8] = [0; 8]; bytes[0] = self.bytes.get(address).copied()?; bytes[1] = self.bytes.get(address + 1).copied()?; bytes[2] = self.bytes.get(address + 2).copied()?; bytes[3] = self.bytes.get(address + 3).copied()?; bytes[4] = self.bytes.get(address + 4).copied()?; bytes[5] = self.bytes.get(address + 5).copied()?; bytes[6] = self.bytes.get(address + 6).copied()?; bytes[7] = self.bytes.get(address + 7).copied()?; Option::Some(u64::from_le_bytes(bytes)) } } } #[cfg(test)] mod test { use crate::simulator::{loader::Loader, machine::Machine}; #[test] fn test_parse() { let mut machine = Machine::init_machine(); let loader = Loader::load("./test/riscv_instructions/simple_arithmetics/unsigned_addition", &mut machine, 0).expect("IO Error"); assert_eq!(true, loader.is_elf()); assert_eq!(false, loader.is_32bits()); assert_eq!(false, loader.check_endianess()); assert_eq!(true, loader.is_system_v_elf()); assert_eq!(true, loader.is_riscv_isa()); assert_eq!(Option::Some(1), loader.get_version()); assert_eq!(Option::Some(0x4000), loader.get_entrypoint(false)); assert_eq!(Option::Some(64), loader.get_program_header_table_location(false)); assert_eq!(Option::Some(18984), loader.get_section_header_table_location(false)); } }