From 9ca720f1887286bd324df3a4cf97bb81b5e95309 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 1 May 2024 02:18:29 +0800 Subject: [PATCH 1/2] add an assert to sanity-check that IniF sections are aligned There is no hope for success if the alignment of an IniF is not aligned, since we directly map the data into memory. --- tools/src/tags/inif.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tools/src/tags/inif.rs b/tools/src/tags/inif.rs index 3def02f65..a97053d53 100644 --- a/tools/src/tags/inif.rs +++ b/tools/src/tags/inif.rs @@ -1,6 +1,7 @@ use std::fmt; use std::io; +use crate::elf::MiniElfFlags; use crate::elf::MiniElfSection; use crate::xous_arguments::{XousArgument, XousArgumentCode, XousSize}; @@ -74,11 +75,29 @@ impl XousArgument for IniF { let mut written = 0; written += output.write(&self.load_offset.to_le_bytes())?; written += output.write(&self.entrypoint.to_le_bytes())?; + let mut flash_offset = self.load_offset; for section in &self.sections { + log::debug!("section.virt: {:x} <> flash_offset: {:x}", section.virt, flash_offset); + // Check that the padding fix worked + if section.flags & MiniElfFlags::NOCOPY == MiniElfFlags::NONE { + // Only modestly informative panic, because we're deep in a serialize + // routine and we don't have metadata about the process or anything: + // But if these offsets don't line up, it's definitely a problem. + assert!( + section.virt & 0xFFF == flash_offset & 0xFFF, + "IniF page offsets must align: ({:x}){:x} <> ({:x}){:x} ({:x?})", + section.virt >> 12, + section.virt & 0xFFF, + flash_offset >> 12, + flash_offset & 0xFFF, + section + ); + } written += output.write(§ion.virt.to_le_bytes())?; let mut word2 = section.size.to_le_bytes(); word2[3] = section.flags.bits(); written += output.write(&word2)?; + flash_offset += section.size; } Ok(written) } From 9499a41739c9aee6825e00b233555be420777cb7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 1 May 2024 02:19:10 +0800 Subject: [PATCH 2/2] fix padding computations in ELF file read-in Not sure how this even worked in the first place....I think we always just got lucky with the size of .gcc_except_table. However, something changed in the last rustc update that causes this section to align to a 256-byte boundary instead of a 4-byte boundary, which exposes downstream alignment issues. This patch now captures the actual offset alignment + padding, which is somewhat complicated by the fact that we throw away the first section (not sure why??? it doesn't seem to have a valid name or header, but the first 0x114 bytes are non-existent) so we have to factor this into all the downstream padding efforts. --- tools/src/elf.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tools/src/elf.rs b/tools/src/elf.rs index cda91216b..ed0ea8d89 100644 --- a/tools/src/elf.rs +++ b/tools/src/elf.rs @@ -295,12 +295,17 @@ pub fn process_minielf(b: &[u8]) -> Result { // This keeps a running offset of where data is getting copied. let mut program_offset = 0; let mut section_iter = elf.section_iter().peekable(); + let mut init_offset = 0; while let Some(s) = section_iter.next() { let mut flags = MiniElfFlags::NONE; let name = s.get_name(&elf).unwrap_or("<>"); if s.address() == 0 { debug!("(Skipping section {} -- invalid address)", name); + // only extract the initial offset once per ELF file + if init_offset == 0 { + init_offset = if let Some(s) = section_iter.peek() { s.offset() } else { 0 }; + } continue; } if alignment_offset == 0 { @@ -308,8 +313,7 @@ pub fn process_minielf(b: &[u8]) -> Result { } debug!("Section {}:", name); - debug!("Official header:"); - debug!("{:?}", s); + debug!("{} official header: {:x?}", name, s); debug!("Interpreted:"); debug!(" flags: {:?}", s.flags()); debug!(" type: {:?}", s.get_type()); @@ -354,9 +358,13 @@ pub fn process_minielf(b: &[u8]) -> Result { if s.get_type() != Ok(ShType::NoBits) { let section_data = s.raw_data(&elf); let pad_amount = if let Some(next_section) = section_iter.peek() { - if section_data.len() % next_section.align() as usize != 0 { - let pad_amount = - next_section.align() as usize - (section_data.len() % next_section.align() as usize); + if (section_data.len() + program_offset as usize + init_offset as usize) + % next_section.align() as usize + != 0 + { + let pad_amount = next_section.align() as usize + - ((section_data.len() + program_offset as usize + init_offset as usize) + % next_section.align() as usize); if s.address() + size + pad_amount as u64 > next_section.address() { (next_section.address() - (s.address() + size)) as usize } else {