diff --git a/basic/src/bin/valida.rs b/basic/src/bin/valida.rs index c462f9a..98b3a9f 100644 --- a/basic/src/bin/valida.rs +++ b/basic/src/bin/valida.rs @@ -1,6 +1,6 @@ use clap::Parser; -use std::fs::File; use std::fs; +use std::fs::File; use std::io::{stdout, Write}; use valida_basic::BasicMachine; @@ -53,7 +53,8 @@ fn main() { let args = Args::parse(); let mut machine = BasicMachine::::default(); - let Program { code, data } = load_executable_file(fs::read(&args.program).expect("Failed to read executable file")); + let Program { code, data } = + load_executable_file(fs::read(&args.program).expect("Failed to read executable file")); machine.program_mut().set_program_rom(&code); machine.cpu_mut().fp = args.stack_height; machine.cpu_mut().save_register_state(); diff --git a/elf/src/lib.rs b/elf/src/lib.rs index 8bcb019..3cd5b4b 100644 --- a/elf/src/lib.rs +++ b/elf/src/lib.rs @@ -5,10 +5,10 @@ extern crate alloc; use alloc::collections::BTreeMap; use alloc::vec; use alloc::vec::Vec; -use elf::ElfBytes; use elf::abi; use elf::endian::AnyEndian; use elf::section::SectionHeader; +use elf::ElfBytes; use valida_machine::{ProgramROM, Word}; pub struct Program { @@ -17,8 +17,7 @@ pub struct Program { } pub fn load_executable_file(file: Vec) -> Program { - if file[0] == 0x7F && file[1] == 0x45 - && file[2] == 0x4C && file[3] == 0x46 { + if file[0] == 0x7F && file[1] == 0x45 && file[2] == 0x4C && file[3] == 0x46 { load_elf_object_file(file) } else { Program { @@ -30,16 +29,16 @@ pub fn load_executable_file(file: Vec) -> Program { pub fn load_elf_object_file(file: Vec) -> Program { let file = ElfBytes::::minimal_parse(file.as_slice()).unwrap(); - let mut data_sections: Vec::<(SectionHeader, &[u8])> = vec![]; - let mut bss_sections: Vec:: = vec![]; - let mut text_sections: Vec::<(SectionHeader, &[u8])> = vec![]; + let mut data_sections: Vec<(SectionHeader, &[u8])> = vec![]; + let mut bss_sections: Vec = vec![]; + let mut text_sections: Vec<(SectionHeader, &[u8])> = vec![]; for section_header in file.section_headers().unwrap().iter() { let is_data: bool = section_header.sh_type == abi::SHT_PROGBITS - && section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_WRITE).into(); + && section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_WRITE).into(); let is_bss: bool = section_header.sh_type == abi::SHT_NOBITS - && section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_WRITE).into(); + && section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_WRITE).into(); let is_text: bool = section_header.sh_type == abi::SHT_PROGBITS - && section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_EXECINSTR).into(); + && section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_EXECINSTR).into(); let is_useful: bool = is_data || is_bss || is_text; if is_useful { if is_data || is_text { @@ -51,7 +50,7 @@ pub fn load_elf_object_file(file: Vec) -> Program { } else if is_text { text_sections.push((section_header, section_data)); } - }, + } _ => panic!("unsupported: compressed ELF section data"), } } else if is_bss { @@ -59,25 +58,29 @@ pub fn load_elf_object_file(file: Vec) -> Program { } } } - let code_size = - text_sections - .iter() - .map(|(section_header, _)| { - section_header.sh_addr + section_header.sh_size - }) - .fold(0, |a, b| a.max(b)); + let code_size = text_sections + .iter() + .map(|(section_header, _)| section_header.sh_addr + section_header.sh_size) + .fold(0, |a, b| a.max(b)); let mut code: Vec = vec![0; code_size as usize]; for (section_header, section_data) in text_sections { - for i in 0 .. section_header.sh_size as usize { + for i in 0..section_header.sh_size as usize { code[i + section_header.sh_addr as usize] = section_data[i]; } } let mut data: BTreeMap> = BTreeMap::new(); for (section_header, section_data) in data_sections { - for i in 0 .. (section_header.sh_size / 4) as usize { - data.insert(>::try_into(section_header.sh_addr).unwrap() - + >::try_into(i*4).unwrap(), - Word([section_data[i*4], section_data[i*4+1], section_data[i*4+2], section_data[i*4+3]])); + for i in 0..(section_header.sh_size / 4) as usize { + data.insert( + >::try_into(section_header.sh_addr).unwrap() + + >::try_into(i * 4).unwrap(), + Word([ + section_data[i * 4], + section_data[i * 4 + 1], + section_data[i * 4 + 2], + section_data[i * 4 + 3], + ]), + ); } } Program {