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 533bcbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 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
15 changes: 13 additions & 2 deletions elf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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 All @@ -43,6 +42,14 @@ pub fn load_elf_object_file(file: Vec<u8>) -> Program {
&& 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();
std::println!(
"sh_type = {:?}, sh_flags = {:?}, sh_name = {:?}, sh_size = {:?}, is_rodata = {:?}",
section_header.sh_type,
section_header.sh_flags,
section_header.sh_name,
section_header.sh_size,
is_rodata
);
if is_data || is_rodata || is_text {
let section_data = file.section_data(&section_header).unwrap();
match section_data {
Expand Down Expand Up @@ -76,7 +83,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 533bcbd

Please sign in to comment.