Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
morganthomas committed Apr 5, 2024
1 parent 57d16fa commit 6874437
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
5 changes: 3 additions & 2 deletions basic/src/bin/valida.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -53,7 +53,8 @@ fn main() {
let args = Args::parse();

let mut machine = BasicMachine::<BabyBear>::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();
Expand Down
47 changes: 25 additions & 22 deletions elf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,8 +17,7 @@ pub struct Program {
}

pub fn load_executable_file(file: Vec<u8>) -> 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 {
Expand All @@ -30,16 +29,16 @@ pub fn load_executable_file(file: Vec<u8>) -> Program {

pub fn load_elf_object_file(file: Vec<u8>) -> Program {
let file = ElfBytes::<AnyEndian>::minimal_parse(file.as_slice()).unwrap();
let mut data_sections: Vec::<(SectionHeader, &[u8])> = vec![];
let mut bss_sections: Vec::<SectionHeader> = vec![];
let mut text_sections: Vec::<(SectionHeader, &[u8])> = vec![];
let mut data_sections: Vec<(SectionHeader, &[u8])> = vec![];
let mut bss_sections: Vec<SectionHeader> = 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 {
Expand All @@ -51,33 +50,37 @@ pub fn load_elf_object_file(file: Vec<u8>) -> Program {
} else if is_text {
text_sections.push((section_header, section_data));
}
},
}
_ => panic!("unsupported: compressed ELF section data"),
}
} else if is_bss {
bss_sections.push(section_header);
}
}
}
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<u8> = 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<u32, Word<u8>> = BTreeMap::new();
for (section_header, section_data) in data_sections {
for i in 0 .. (section_header.sh_size / 4) as usize {
data.insert(<u64 as TryInto<u32>>::try_into(section_header.sh_addr).unwrap()
+ <usize as TryInto<u32>>::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(
<u64 as TryInto<u32>>::try_into(section_header.sh_addr).unwrap()
+ <usize as TryInto<u32>>::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 {
Expand Down

0 comments on commit 6874437

Please sign in to comment.