From d50f64acc3483264690dd1e9005f43b7763c1956 Mon Sep 17 00:00:00 2001 From: Morgan Thomas Date: Tue, 23 Apr 2024 12:57:15 -0400 Subject: [PATCH] issue/135: correctly load elf file sections --- basic/src/bin/valida.rs | 12 ++++++++++-- basic/src/lib.rs | 2 -- elf/src/lib.rs | 9 +++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/basic/src/bin/valida.rs b/basic/src/bin/valida.rs index 2ea1dae..817521e 100644 --- a/basic/src/bin/valida.rs +++ b/basic/src/bin/valida.rs @@ -75,7 +75,11 @@ impl Context { last_fp_size_: 0, }; - let Program { code, data, initial_program_counter } = load_executable_file( + let Program { + code, + data, + initial_program_counter, + } = load_executable_file( fs::read(&args.program) .expect(format!("Failed to read executable file: {}", &args.program).as_str()), ); @@ -323,7 +327,11 @@ fn main() { } let mut machine = BasicMachine::::default(); - let Program { code, data, initial_program_counter } = load_executable_file( + let Program { + code, + data, + initial_program_counter, + } = load_executable_file( fs::read(&args.program) .expect(format!("Failed to read executable file: {}", &args.program).as_str()), ); diff --git a/basic/src/lib.rs b/basic/src/lib.rs index fe22692..d1df172 100644 --- a/basic/src/lib.rs +++ b/basic/src/lib.rs @@ -1070,8 +1070,6 @@ impl Machine for BasicMachine { let opcode = instruction.opcode; let ops = instruction.operands; - println!("pc = {:?}, instruction = {:?}", pc, instruction); - // Execute match opcode { >::OPCODE => { diff --git a/elf/src/lib.rs b/elf/src/lib.rs index e6cea2c..352ca54 100644 --- a/elf/src/lib.rs +++ b/elf/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + extern crate alloc; use alloc::collections::BTreeMap; @@ -33,7 +35,6 @@ pub fn load_elf_object_file(file: Vec) -> Program { let mut bss_sections: Vec = vec![]; let mut text_sections: Vec<(SectionHeader, &[u8])> = vec![]; for section_header in file.section_headers().unwrap().iter() { - std::println!("sh_type = {:?}, sh_flags = {:?}, sh_name = {:?}, sh_size = {:?}", section_header.sh_type, section_header.sh_flags, section_header.sh_name, section_header.sh_size); let is_data: bool = section_header.sh_type == abi::SHT_PROGBITS && section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_WRITE).into(); let is_rodata: bool = section_header.sh_type == abi::SHT_PROGBITS @@ -76,7 +77,11 @@ pub fn load_elf_object_file(file: Vec) -> Program { } 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 { + let mut section_data = Vec::from(section_data); + while section_data.len() % 4 != 0 { + section_data.push(0); + } + for i in 0..(section_data.len() / 4) as usize { data.insert( >::try_into(section_header.sh_addr).unwrap() + >::try_into(i * 4).unwrap(),