Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an incorrect number of zero bytes being inserted between sections #44

Merged
merged 3 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions hf2/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ pub fn elf_to_bin(path: PathBuf) -> Result<(Vec<u8>, u32), UtilError> {
})
.enumerate()
{
data.extend_from_slice(&buffer[ph.p_offset as usize..][..ph.p_filesz as usize]);

// first time through grab the starting physical address
if i == 0 {
start_address = ph.p_paddr;
}
//if any of the rest of the sections are non contiguous, fill zeros
// on subsequent passes, if there's a gap between this section and the
// previous one, fill it with zeros
else {
let difference = (ph.p_paddr - last_address) as usize;
data.resize(data.len() + difference, 0x0);
}

last_address = start_address + ph.p_filesz;
data.extend_from_slice(&buffer[ph.p_offset as usize..][..ph.p_filesz as usize]);

last_address = ph.p_paddr + ph.p_filesz;
}

Ok((data, start_address as u32))
Expand Down Expand Up @@ -238,4 +240,19 @@ mod tests {
.unwrap();
assert_eq!(start_addr, 0x4000);
}

#[test]
fn elf_sections() {
let (data, start_addr) = super::elf_to_bin(
[env!("CARGO_MANIFEST_DIR"), "src/utils/testdata/sections"]
.iter()
.collect(),
)
.unwrap();
println!("{:?}", data);
assert_eq!(start_addr, 0);
assert_eq!(data[0], 1);
assert_eq!(data[12], 2);
assert_eq!(data[20], 3);
}
}
Binary file added hf2/src/utils/testdata/sections
Binary file not shown.