mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
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.
This commit is contained in:
parent
9889856ede
commit
29f050a8ad
7 changed files with 59 additions and 21 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -75,6 +75,9 @@ pub struct MiniElf {
|
|||
|
||||
/// Actual section data
|
||||
pub program: Vec<u8>,
|
||||
|
||||
/// Alignment offset for page mapping
|
||||
pub alignment_offset: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -291,6 +294,7 @@ pub fn process_minielf(b: &[u8]) -> Result<MiniElf, ElfReadError> {
|
|||
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<MiniElf, ElfReadError> {
|
|||
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<MiniElf, ElfReadError> {
|
|||
entry_point,
|
||||
sections,
|
||||
program: program_data.into_inner(),
|
||||
alignment_offset: alignment_offset as usize,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,9 @@ pub struct IniF {
|
|||
|
||||
/// Actual program data
|
||||
data: Vec<u8>,
|
||||
|
||||
/// 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<MiniElfSection>, mut data: Vec<u8>) -> IniF {
|
||||
pub fn new(entrypoint: u32, sections: Vec<MiniElfSection>, mut data: Vec<u8>, 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] {
|
||||
|
|
|
|||
|
|
@ -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<u8>) -> Vec::<u8> {
|
||||
if data.len() % PAGE_SIZE == 0 {
|
||||
pub fn align_data_up(data: &Vec<u8>, alignment_offset: usize) -> Vec::<u8> {
|
||||
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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ pub struct XousArguments {
|
|||
ram_start: XousSize,
|
||||
ram_length: XousSize,
|
||||
ram_name: u32,
|
||||
alignment_offset: usize,
|
||||
pub arguments: Vec<Box<dyn XousArgument>>,
|
||||
}
|
||||
|
||||
|
|
@ -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<T: 'static>(&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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue