diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 1b6f05df0..05918f452 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -18,7 +18,7 @@ env_logger = "0.7.1" log = "0.4.14" pem = "0.8.3" svd2utra = "0.1.11" -xmas-elf = "0.7.0" +xmas-elf = "0.9.0" xous-semver = "0.1.2" [[bin]] diff --git a/tools/src/elf.rs b/tools/src/elf.rs index e7f9a2bc2..f8637d0f3 100644 --- a/tools/src/elf.rs +++ b/tools/src/elf.rs @@ -310,7 +310,8 @@ pub fn process_minielf(b: &[u8]) -> Result { // This keeps a running offset of where data is getting copied. let mut program_offset = 0; - for s in elf.section_iter() { + let mut section_iter = elf.section_iter().peekable(); + while let Some(s) = section_iter.next() { let mut flags = MiniElfFlags::NONE; let name = s.get_name(&elf).unwrap_or("<>"); @@ -332,7 +333,7 @@ pub fn process_minielf(b: &[u8]) -> Result { debug!(" offset: {:08x}", s.offset()); debug!(" size: {:?}", s.size()); debug!(" link: {:?}", s.link()); - let size = s.size(); + let mut size = s.size(); let no_copy = s.get_type() == Ok(ShType::NoBits); @@ -363,6 +364,16 @@ pub fn process_minielf(b: &[u8]) -> Result { // If this section gets copied, add it to the program stream. 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 { + next_section.align() as usize - (section_data.len() % next_section.align() as usize) + } else { + 0 + } + } else { + 0 + }; + debug!( "Section start: {:02x} {:02x} {:02x} {:02x} going into offset 0x{:08x}", section_data[0], section_data[1], section_data[2], section_data[3], program_offset @@ -374,6 +385,18 @@ pub fn process_minielf(b: &[u8]) -> Result { .write(section_data) .map_err(ElfReadError::WriteSectionError)?; program_offset += section_data.len() as u64; + + if pad_amount != 0 { + let pad = vec![0u8; pad_amount]; + program_data.write(&pad) + .map_err(ElfReadError::WriteSectionError)?; + program_offset += pad_amount as u64; + size += pad_amount as u64; + } + } else { + // we leave the nocopy sections mis-aligned. + // They don't exist in the file, they are just zero'd on spec. + // This works so long as the nocopy sections are at the end of the MiniElf. } sections.push(MiniElfSection { virt: s.address() as u32,