From 4e817b9a26d538808101b57f99b7edbe590769db Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Mon, 9 Aug 2021 17:59:51 -0400 Subject: [PATCH 1/3] make padding an option for `elf_to_tbf()` --- src/main.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 527fbae..e90cb39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,6 +106,7 @@ fn main() { opt.protected_region_size, opt.permissions.to_vec(), minimum_tock_kernel_version, + true, ) .unwrap(); if opt.verbose { @@ -148,6 +149,7 @@ fn elf_to_tbf( protected_region_size_arg: Option, permissions: Vec<(u32, u32)>, kernel_version: Option<(u16, u16)>, + trailing_padding: bool, ) -> io::Result<()> { let package_name = package_name.unwrap_or_default(); @@ -553,16 +555,25 @@ fn elf_to_tbf( // the relocation data length. binary_index += relocation_binary.len() + mem::size_of::(); - // 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. @@ -580,7 +591,7 @@ fn elf_to_tbf( 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(()) From 68ecad60b2bb34a89dceca9603539e5f32667626 Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Mon, 9 Aug 2021 18:41:08 -0400 Subject: [PATCH 2/3] only add trailing padding to cortex-m apps --- src/main.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e90cb39..91843dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,17 @@ fn main() { .open(tbf_path.clone()) .unwrap(); + // Adding padding to the end of cortex-m apps. Check for a cortex-m app + // because the start of the elf filename will start with "cortex". + // + // RISC-V apps do not need to be sized to power of two. + let add_trailing_padding = elf_path + .file_name() + .unwrap_or(std::ffi::OsStr::new("")) + .to_str() + .unwrap_or("") + .starts_with("cortex"); + // Do the conversion to a tock binary. if opt.verbose { println!("Creating {:?}", tbf_path); @@ -106,7 +117,7 @@ fn main() { opt.protected_region_size, opt.permissions.to_vec(), minimum_tock_kernel_version, - true, + add_trailing_padding, ) .unwrap(); if opt.verbose { From 98cb06449ffd2e9dc44e55caf7ad0d40038e8155 Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Tue, 10 Aug 2021 12:34:39 -0400 Subject: [PATCH 3/3] use elf header info to detect arm apps --- src/main.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 91843dd..13f2aad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,15 +92,12 @@ fn main() { .unwrap(); // Adding padding to the end of cortex-m apps. Check for a cortex-m app - // because the start of the elf filename will start with "cortex". + // 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 = elf_path - .file_name() - .unwrap_or(std::ffi::OsStr::new("")) - .to_str() - .unwrap_or("") - .starts_with("cortex"); + let add_trailing_padding = elffile.ehdr.machine.0 == 0x28; // Do the conversion to a tock binary. if opt.verbose {