diff --git a/tools/src/elf.rs b/tools/src/elf.rs index 05479a088..62a75ba67 100644 --- a/tools/src/elf.rs +++ b/tools/src/elf.rs @@ -1,3 +1,4 @@ +use std::convert::TryInto; use std::fmt; use std::fs::File; use std::io::{Cursor, Read, Seek, SeekFrom, Write}; @@ -178,6 +179,7 @@ pub fn process_program(b: &[u8], rom_only: bool) -> Result>"); @@ -251,6 +253,9 @@ pub fn process_program(b: &[u8], rom_only: bool) -> Result Result Result() == 0 { + const SPACING: &'static str = " "; + let mut non_zero_tuples = Vec::<(usize, u32)>::new(); + for (i, chunk) in data_copy.chunks_exact(4).enumerate() { + let word = u32::from_le_bytes(chunk.try_into().unwrap()); + if word != 0 { + non_zero_tuples.push((i, word)); + } + } + println!("Code for loader/baremetal integration:\n"); + println!("// Define the .data region - bootstrap baremetal using these hard-coded parameters."); + println!("const DATA_ORIGIN: usize = 0x{:x};", data_offset); + println!( + "const DATA_SIZE_BYTES: usize = 0x{:x};", + // round up to the nearest u32 word. Includes .data, .bss, .stack, .heap - regions to be zero'd. + ((data_size + bss_size) as usize + size_of::() - 1) & !(size_of::() - 1) + ); + let mut init_str = String::new(); + init_str.push_str(&format!("const DATA_INIT: [(usize, u32); {}] = [\n", non_zero_tuples.len())); + for (offset, data) in non_zero_tuples { + init_str.push_str(&format!("{}(0x{:x}, 0x{:x}),\n", SPACING, offset, data)); + } + init_str.push_str(&format!("];")); + print!("{}", init_str); + let boilerplate = r#" +// Clear .data, .bss, .stack, .heap regions & setup .data values +unsafe { + let data_ptr = DATA_ORIGIN as *mut u32; + for i in 0..DATA_SIZE_BYTES / size_of::() { + data_ptr.add(i).write_volatile(0); + } + for (offset, data) in DATA_INIT { + data_ptr.add(offset).write_volatile(data); + } +} + "#; + println!("\n{}", boilerplate); + } else { + println!( + "Data section is not word-aligned, check objdump in detail for how to initialize the section" + ); + } Ok(ProgramDescription { entry_point, program: program_data.into_inner(),