Skip to content

Commit

Permalink
Merge pull request #33 from tock/padding-optional
Browse files Browse the repository at this point in the history
Make binary padding to power of two optional, only use for cortex-m
  • Loading branch information
ppannuto authored Aug 10, 2021
2 parents ea5dd5c + 98cb064 commit c980f3c
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ fn main() {
.open(tbf_path.clone())
.unwrap();

// Adding padding to the end of cortex-m apps. Check for a cortex-m app
// by inspecting the "machine" value in the elf header. 0x28 is ARM (see
// https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header
// for a list).
//
// RISC-V apps do not need to be sized to power of two.
let add_trailing_padding = elffile.ehdr.machine.0 == 0x28;

// Do the conversion to a tock binary.
if opt.verbose {
println!("Creating {:?}", tbf_path);
Expand All @@ -106,6 +114,7 @@ fn main() {
opt.protected_region_size,
opt.permissions.to_vec(),
minimum_tock_kernel_version,
add_trailing_padding,
)
.unwrap();
if opt.verbose {
Expand Down Expand Up @@ -142,6 +151,7 @@ fn elf_to_tbf<W: Write>(
protected_region_size_arg: Option<u32>,
permissions: Vec<(u32, u32)>,
kernel_version: Option<(u16, u16)>,
trailing_padding: bool,
) -> io::Result<()> {
let package_name = package_name.unwrap_or_default();

Expand Down Expand Up @@ -547,16 +557,25 @@ fn elf_to_tbf<W: Write>(
// the relocation data length.
binary_index += relocation_binary.len() + mem::size_of::<u32>();

// That is everything that we are going to include in our app binary. Now
// we need to pad the binary to a power of 2 in size, and make sure it is
// at least 512 bytes in size.
let post_content_pad = if binary_index.count_ones() > 1 {
let power2len = cmp::max(1 << (32 - (binary_index as u32).leading_zeros()), 512);
power2len - binary_index
// That is everything that we are going to include in our app binary.

let post_content_pad = if trailing_padding {
// If trailing padding is requested, we need to pad the binary to a
// power of 2 in size, and make sure it is at least 512 bytes in size.
let pad = if binary_index.count_ones() > 1 {
let power2len = cmp::max(1 << (32 - (binary_index as u32).leading_zeros()), 512);
power2len - binary_index
} else {
0
};
// Increment to include the padding.
binary_index += pad;
pad
} else {
// No padding.
0
};
binary_index += post_content_pad;

let total_size = binary_index;

// Now set the total size of the app in the header.
Expand All @@ -574,7 +593,7 @@ fn elf_to_tbf<W: Write>(
output.write_all(&rel_data_len)?;
output.write_all(relocation_binary.as_ref())?;

// Pad to get a power of 2 sized flash app.
// Pad to get a power of 2 sized flash app, if requested.
util::do_pad(output, post_content_pad as usize)?;

Ok(())
Expand Down

0 comments on commit c980f3c

Please sign in to comment.