Skip to content

Commit ef36f4f

Browse files
Add new power-of-two masking logic for >64KB apps
Takes advantage of subregions to lower the wasted space.
1 parent 06b05fd commit ef36f4f

1 file changed

Lines changed: 47 additions & 24 deletions

File tree

src/convert.rs

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,30 +179,6 @@ pub fn elf_to_tbf(
179179
.iter()
180180
.collect();
181181

182-
/// Specify how elf2tab should add trailing padding to the end of the TBF
183-
/// file.
184-
enum TrailingPadding {
185-
/// Make sure the entire TBF is a power of 2 in size, so add any
186-
/// necessary padding to make that happen.
187-
TotalSizePowerOfTwo,
188-
/// Make sure the entire TBF is a multiple of a specific value.
189-
TotalSizeMultiple(usize),
190-
}
191-
192-
// Add trailing padding for certain architectures.
193-
//
194-
// - ARM: make sure the entire TBF is a power of 2 to make configuring the
195-
// MPU easy.
196-
// - RISC_V: make sure the entire TBF is a multiple of 4 to meet TBF
197-
// alignment requirements.
198-
// - x86: use 4k padding to match page size.
199-
let trailing_padding = match elf_file.ehdr.e_machine {
200-
elf::abi::EM_ARM => Some(TrailingPadding::TotalSizePowerOfTwo),
201-
elf::abi::EM_RISCV => Some(TrailingPadding::TotalSizeMultiple(4)),
202-
elf::abi::EM_386 => Some(TrailingPadding::TotalSizeMultiple(4096)),
203-
_ => None,
204-
};
205-
206182
////////////////////////////////////////////////////////////////////////////
207183
// Determine the amount of RAM this app needs.
208184
////////////////////////////////////////////////////////////////////////////
@@ -897,6 +873,39 @@ pub fn elf_to_tbf(
897873
ensured_footer_reserved_space = true;
898874
}
899875

876+
/// Specify how elf2tab should add trailing padding to the end of the TBF
877+
/// file.
878+
enum TrailingPadding {
879+
/// Make sure the entire TBF is a power of 2 in size, so add any
880+
/// necessary padding to make that happen.
881+
TotalSizePowerOfTwo,
882+
/// Make sure the entire TBF is a multiple of a specific value.
883+
TotalSizeMultiple(usize),
884+
/// To prevent wasted space, only pad to:
885+
/// (X - 1) * (TotalSizePowerOfTwo / 8) < size < X * (TotalSizePowerOfTwo)
886+
MaskedPowerOfTwo,
887+
}
888+
889+
// Add trailing padding for certain architectures.
890+
// - ARM: make sure the entire TBF is a power of 2 to make configuring the
891+
// MPU easy (for apps <64kB). Otherwise (for apps >64kB) round up to the
892+
// nearest power of 2 and then use subregions to limit wasted flash.
893+
// - RISC_V: make sure the entire TBF is a multiple of 4 to meet TBF
894+
// alignment requirements.
895+
// - x86: use 4k padding to match page size.
896+
let trailing_padding = match elf_file.ehdr.e_machine {
897+
elf::abi::EM_ARM => {
898+
// Do power of two padding if less than 64KB, otherwise do masked.
899+
if binary_index < 65536 {
900+
Some(TrailingPadding::TotalSizePowerOfTwo)
901+
} else {
902+
Some(TrailingPadding::MaskedPowerOfTwo)
903+
}
904+
}
905+
elf::abi::EM_RISCV => Some(TrailingPadding::TotalSizeMultiple(4)),
906+
elf::abi::EM_386 => Some(TrailingPadding::TotalSizeMultiple(4096)),
907+
_ => None,
908+
};
900909
// Optionally calculate the additional padding needed to ensure the app size
901910
// meets the padding requirements.
902911
//
@@ -921,6 +930,20 @@ pub fn elf_to_tbf(
921930
TrailingPadding::TotalSizeMultiple(multiple) => {
922931
(multiple - (binary_index % multiple)) % multiple
923932
}
933+
TrailingPadding::MaskedPowerOfTwo => {
934+
// Find the next power of two.
935+
let next_power2 = 1 << (32 - (binary_index as u32).leading_zeros());
936+
let multiple = next_power2 / 8;
937+
let number_of_unmasked_subregions = binary_index.div_ceil(multiple);
938+
let target_size = number_of_unmasked_subregions * multiple;
939+
940+
// Pad to the nearest multiple of (power of two / 8).
941+
if target_size > binary_index {
942+
target_size - binary_index
943+
} else {
944+
0
945+
}
946+
}
924947
};
925948

926949
// Increment to include the padding.

0 commit comments

Comments
 (0)