Skip to content

Commit

Permalink
Use reserved space footer for padding
Browse files Browse the repository at this point in the history
elf2tab supports two types of padding: 1) creating a reserved space
footer or 2) just adding bytes to the binary. The need to do both is
because a reserved space footer can only pad 8 or more bytes.

This patch fixes an issue where the padding needed is less than 8, but,
there is already going to be a reserved footer. In that case, we were
adding the padding twice (once in the reserved footer and again as just
extra bytes).

This issue only arises if you ask for a certain amount of footer space
when running elf2tab.
  • Loading branch information
bradjc committed May 7, 2024
1 parent 2f0e2f0 commit 45a5880
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,9 @@ pub fn elf_to_tbf(

let footers_initial_len = binary_index - tbfheader.binary_end_offset() as usize;

// Flag to track if we are guaranteed to have a reserved space footer.
let mut ensured_footer_reserved_space: bool = false;

// Make sure the footer is at least the minimum requested size.
if (minimum_footer_size as usize) > footers_initial_len {
let mut needed_footer_reserved_space = (minimum_footer_size as usize) - footers_initial_len;
Expand All @@ -885,6 +888,10 @@ pub fn elf_to_tbf(

// Add reserved space to the footer.
binary_index += needed_footer_reserved_space;

// Since we ensured there is room for the reserved space footer, we mark
// that that footer will be created.
ensured_footer_reserved_space = true;
}

// Optionally calculate the additional padding needed to ensure the app size
Expand Down Expand Up @@ -917,9 +924,10 @@ pub fn elf_to_tbf(
binary_index += pad;

// If there is room for a TbfFooterCredentials we will use that.
if pad
>= (mem::size_of::<header::TbfHeaderTlv>()
+ mem::size_of::<header::TbfFooterCredentialsType>())
if ensured_footer_reserved_space
|| pad
>= (mem::size_of::<header::TbfHeaderTlv>()
+ mem::size_of::<header::TbfFooterCredentialsType>())
{
0
} else {
Expand Down

0 comments on commit 45a5880

Please sign in to comment.