Skip to content
Merged
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
41 changes: 27 additions & 14 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,32 @@ pub fn elf_to_tbf(
if let Some(last_segment_address_end) = last_segment_address_end {
// We have a previous segment. Now, check if there is any padding
// between the segments in the .elf.
let padding = (segment.p_offset as usize)
.checked_sub(last_segment_address_end)
.expect("Invariant violated: expect to iterate over ELF sections in order");
let chk_padding = (segment.p_paddr as usize).checked_sub(last_segment_address_end);

if padding > 0 {
if verbose {
println!(" Including padding between segments size={}", padding);
}
if let Some(padding) = chk_padding {
if padding > 0 {
if verbose {
println!(" Including padding between segments size={}", padding);
}

// Insert the padding into the generated binary.
binary.extend(vec![0; padding]);
binary_index += padding;
if padding >= 4096 {
// Warn the user that we're inserting a large amount of
// padding (>= 4096, which is the ELF file segment padding)
// into the binary. This can be a sign of an incorrect /
// broken ELF file (where not all LOADed non-zero sized
// sections are marked to be loaded from flash).
println!(" Warning! Inserting a large amount of padding.");
}

// Insert the padding into the generated binary.
binary.extend(vec![0; padding]);
binary_index += padding;
}
} else {
println!(
" Warning! Expecting ELF sections to be in physical (load) address order."
);
println!(" Not inserting padding, the resulting TBF may be broken.");
}
}

Expand All @@ -582,7 +596,6 @@ pub fn elf_to_tbf(

let start_segment = segment.p_paddr;
let end_segment = segment.p_paddr + segment.p_filesz;
let end_offset = segment.p_offset + segment.p_filesz;

// Check if this segment contains the entry point, and calculate the
// offset we need to store in the TBF header if so.
Expand Down Expand Up @@ -684,9 +697,9 @@ pub fn elf_to_tbf(
}
}

// Save the end-offset of this segment within the ELF file, so we can
// check if padding is required between segments.
last_segment_address_end = Some(end_offset as usize);
// Save the end of this segment so we can check if padding is required
// between segments.
last_segment_address_end = Some(end_segment as usize);

binary.extend(content);
binary_index += segment.p_filesz as usize;
Expand Down