Skip to content

Commit

Permalink
issue/135: correctly load elf file sections
Browse files Browse the repository at this point in the history
  • Loading branch information
morganthomas committed Apr 23, 2024
1 parent c5de9fb commit d50f64a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
12 changes: 10 additions & 2 deletions basic/src/bin/valida.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
);
Expand Down Expand Up @@ -323,7 +327,11 @@ fn main() {
}

let mut machine = BasicMachine::<BabyBear>::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()),
);
Expand Down
2 changes: 0 additions & 2 deletions basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,8 +1070,6 @@ impl<F: PrimeField32 + TwoAdicField> Machine<F> for BasicMachine<F> {
let opcode = instruction.opcode;
let ops = instruction.operands;

println!("pc = {:?}, instruction = {:?}", pc, instruction);

// Execute
match opcode {
<Load32Instruction as Instruction<Self, F>>::OPCODE => {
Expand Down
9 changes: 7 additions & 2 deletions elf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

extern crate alloc;

use alloc::collections::BTreeMap;
Expand Down Expand Up @@ -33,7 +35,6 @@ pub fn load_elf_object_file(file: Vec<u8>) -> Program {
let mut bss_sections: Vec<SectionHeader> = 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
Expand Down Expand Up @@ -76,7 +77,11 @@ pub fn load_elf_object_file(file: Vec<u8>) -> Program {
}
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 {
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(
<u64 as TryInto<u32>>::try_into(section_header.sh_addr).unwrap()
+ <usize as TryInto<u32>>::try_into(i * 4).unwrap(),
Expand Down

0 comments on commit d50f64a

Please sign in to comment.