From 29f050a8ad7dc44fd8f1e519ef0ba461335696b7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 8 Feb 2023 04:47:29 +0800 Subject: [PATCH] fix alignment for IniF region so page mappings are 1:1 Turns out, the pointing end of the stick for ELF sections don't start at a page boundary. For example: 5 IniF: entrypoint @ 0001e42e, loaded from 000440f4. Sections: Loaded from 000440f4 - Section .rodata 16924 bytes loading into 000100f4..00014310 flags: NONE Loaded from 00048310 - Section .eh_frame_hdr 1772 bytes loading into 00014310..000149fc flags: NONE Loaded from 000489fc - Section .eh_frame 6004 bytes loading into 000149fc..00016170 flags: NONE Loaded from 0004a170 - Section .text 59876 bytes loading into 00017170..00025b54 flags: EXECUTE Loaded from 00058b54 - Section .sdata 48 bytes loading into 00026b54..00026b84 flags: WRITE Loaded from 00058b84 - Section .sbss 48 bytes loading into 00026b84..00026bb4 flags: WRITE | NOCOPY Loaded from 00058bb4 - Section .bss 516 bytes loading into 00026bb8..00026dbc flags: WRITE | NOCOPY Here you can see, the data is "loading into" 0x100f4, not 0x10000. The original PR would align things to a page, which means you'd still have an offset mismatch if you tried to map these directly over. This patch causes IniF to insert extra padding to target the offset necessary for the MiniElf to map 1:1. An important assumption is the offset is constant across all programs being linked. This seems to be the case, but, if this assumption breaks, then the padding will also break. --- tools/src/bin/create-image.rs | 3 ++- tools/src/elf.rs | 8 ++++++++ tools/src/tags/inie.rs | 2 +- tools/src/tags/inif.rs | 14 +++++++++----- tools/src/tags/mod.rs | 16 ++++++++++------ tools/src/tags/xkrn.rs | 2 +- tools/src/xous_arguments.rs | 35 ++++++++++++++++++++++++++++------- 7 files changed, 59 insertions(+), 21 deletions(-) diff --git a/tools/src/bin/create-image.rs b/tools/src/bin/create-image.rs index f1f4667fd..7449c22f6 100644 --- a/tools/src/bin/create-image.rs +++ b/tools/src/bin/create-image.rs @@ -276,7 +276,8 @@ fn main() { ); pid += 1; let init = read_minielf(init_path).expect("couldn't parse init file"); - args.add(IniF::new(init.entry_point, init.sections, init.program)); + args.add(IniF::new(init.entry_point, init.sections, init.program, init.alignment_offset)); + args.set_alignment_offset(init.alignment_offset); } } diff --git a/tools/src/elf.rs b/tools/src/elf.rs index 000749394..e7f9a2bc2 100644 --- a/tools/src/elf.rs +++ b/tools/src/elf.rs @@ -75,6 +75,9 @@ pub struct MiniElf { /// Actual section data pub program: Vec, + + /// Alignment offset for page mapping + pub alignment_offset: usize, } #[derive(Debug)] @@ -291,6 +294,7 @@ pub fn process_minielf(b: &[u8]) -> Result { let elf = ElfFile::new(&b).map_err(|x| ElfReadError::ParseElfError(x))?; let entry_point = elf.header.pt2.entry_point() as u32; let mut program_data = Cursor::new(Vec::new()); + let mut alignment_offset = 0; let mut sections = vec![]; @@ -314,6 +318,9 @@ pub fn process_minielf(b: &[u8]) -> Result { debug!("(Skipping section {} -- invalid address)", name); continue; } + if alignment_offset == 0 { + alignment_offset = s.address() & 0xFFF; + } debug!("Section {}:", name); debug!("Official header:"); @@ -384,5 +391,6 @@ pub fn process_minielf(b: &[u8]) -> Result { entry_point, sections, program: program_data.into_inner(), + alignment_offset: alignment_offset as usize, }) } diff --git a/tools/src/tags/inie.rs b/tools/src/tags/inie.rs index 521ce2218..e9d7a0a3a 100644 --- a/tools/src/tags/inie.rs +++ b/tools/src/tags/inie.rs @@ -62,7 +62,7 @@ impl XousArgument for IniE { self.load_offset = offset as u32; assert!(offset % crate::tags::PAGE_SIZE == 0, "IniE load offset is not aligned"); - self.data = crate::tags::align_data_up(&self.data); + self.data = crate::tags::align_data_up(&self.data, 0); self.data.len() } diff --git a/tools/src/tags/inif.rs b/tools/src/tags/inif.rs index d10c79ace..7b10a15db 100644 --- a/tools/src/tags/inif.rs +++ b/tools/src/tags/inif.rs @@ -16,6 +16,9 @@ pub struct IniF { /// Actual program data data: Vec, + + /// Alignment offset + alignment_offset: usize, } impl fmt::Display for IniF { @@ -35,7 +38,7 @@ impl fmt::Display for IniF { } impl IniF { - pub fn new(entrypoint: u32, sections: Vec, mut data: Vec) -> IniF { + pub fn new(entrypoint: u32, sections: Vec, mut data: Vec, alignment_offset: usize) -> IniF { // pad the data to 4 bytes while data.len() & 3 != 0 { data.push(0); @@ -45,6 +48,7 @@ impl IniF { entrypoint, sections, data, + alignment_offset, } } } @@ -60,10 +64,10 @@ impl XousArgument for IniF { fn finalize(&mut self, offset: usize) -> usize { self.load_offset = offset as u32; - println!("{:x}", self.load_offset); - assert!(offset % crate::tags::PAGE_SIZE == 0, "IniF load offset is not aligned"); - self.data = crate::tags::align_data_up(&self.data); - crate::tags::align_size_up(self.data.len()) + + assert!(offset % crate::tags::PAGE_SIZE == self.alignment_offset, "IniF load offset is not aligned"); + self.data = crate::tags::align_data_up(&self.data, 0); + self.data.len() } fn last_data(&self) -> &[u8] { diff --git a/tools/src/tags/mod.rs b/tools/src/tags/mod.rs index 3028f8e3e..993bc5904 100644 --- a/tools/src/tags/mod.rs +++ b/tools/src/tags/mod.rs @@ -7,19 +7,23 @@ pub mod inif; pub (crate) const PAGE_SIZE: usize = 4096; -pub fn align_size_up(offset: usize) -> usize { - if offset % PAGE_SIZE == 0 { +pub fn align_size_up(offset: usize, alignment_offset: usize) -> usize { + if offset % PAGE_SIZE == alignment_offset { offset } else { - (offset + PAGE_SIZE) & !(PAGE_SIZE - 1) + if offset % PAGE_SIZE < alignment_offset { + offset + (alignment_offset - offset % PAGE_SIZE) + } else { + (offset & !(PAGE_SIZE - 1)) + PAGE_SIZE + alignment_offset + } } } -pub fn align_data_up(data: &Vec) -> Vec:: { - if data.len() % PAGE_SIZE == 0 { +pub fn align_data_up(data: &Vec, alignment_offset: usize) -> Vec:: { + if data.len() % PAGE_SIZE == alignment_offset { data.to_vec() } else { - let padding = align_size_up(data.len()) - data.len(); + let padding = align_size_up(data.len(), alignment_offset) - data.len(); let pad = vec![0u8; padding]; (&[&data[..], &pad[..]]).concat().to_vec() } diff --git a/tools/src/tags/xkrn.rs b/tools/src/tags/xkrn.rs index 1f4a550df..7f56c8629 100644 --- a/tools/src/tags/xkrn.rs +++ b/tools/src/tags/xkrn.rs @@ -79,7 +79,7 @@ impl XousArgument for XousKernel { "kernel text section is invalid: 0x{:08x} < 0xff000000 -- was it linked with a linker script?", self.text_offset); assert!(offset % crate::tags::PAGE_SIZE == 0, "XKrn load offset is not aligned"); - self.program = crate::tags::align_data_up(&self.program); + self.program = crate::tags::align_data_up(&self.program, 0); self.program.len() } diff --git a/tools/src/xous_arguments.rs b/tools/src/xous_arguments.rs index 72be3382d..c79fc9fe3 100644 --- a/tools/src/xous_arguments.rs +++ b/tools/src/xous_arguments.rs @@ -45,6 +45,7 @@ pub struct XousArguments { ram_start: XousSize, ram_length: XousSize, ram_name: u32, + alignment_offset: usize, pub arguments: Vec>, } @@ -77,16 +78,28 @@ impl XousArguments { ram_length, ram_name, arguments: vec![], + alignment_offset: 0, } } pub fn finalize(&mut self) { - let mut running_offset = crate::tags::align_size_up(self.len() as usize); + let mut running_offset = crate::tags::align_size_up(self.len() as usize, 0); + // println!("offset: {:x}, alignment_offset: {:x}", running_offset, 0); for arg in &mut self.arguments { + if arg.name() == "IniF" { + running_offset = crate::tags::align_size_up(running_offset, self.alignment_offset); + } else { + running_offset = crate::tags::align_size_up(running_offset, 0); + } + // println!("offset: {:x}", running_offset); running_offset += arg.finalize(running_offset); } } + pub fn set_alignment_offset(&mut self, alignment_offset: usize) { + self.alignment_offset = alignment_offset; + } + pub fn add(&mut self, arg: T) where T: XousArgument + Sized, @@ -156,14 +169,22 @@ impl XousArguments { w.write_all(tag_data.get_ref())?; } - // pad to the nearest page - let pos = w.stream_position()?; - let pad_len = crate::tags::align_size_up(pos as usize) - pos as usize; - let pad = vec![0u8; pad_len]; - w.write_all(&pad)?; - // Write any pending data, such as payloads for arg in &self.arguments { + if arg.name() == "IniF" { + // align for 1:1 FLASH mapping + let pos = w.stream_position()?; + let pad_len = crate::tags::align_size_up(pos as usize, self.alignment_offset) - pos as usize; + let pad = vec![0u8; pad_len]; + w.write_all(&pad)?; + } else { + // pad to the nearest page + let pos = w.stream_position()?; + let pad_len = crate::tags::align_size_up(pos as usize, 0) - pos as usize; + let pad = vec![0u8; pad_len]; + w.write_all(&pad)?; + } + // println!("position: {:x}", w.stream_position()?); w.write_all(arg.last_data()) .expect("couldn't write extra arg data"); }