From 1a1601e4d50510c9359b9ff4d101e0afd4f9bfb4 Mon Sep 17 00:00:00 2001 From: jaby Date: Thu, 1 Dec 2022 04:02:38 +0100 Subject: [PATCH] Write LD file --- src/Tools/mkoverlay/src/creator/ldscript.rs | 55 ++++++++++++++++++--- src/Tools/mkoverlay/src/main.rs | 2 +- src/Tools/mkoverlay/src/types/mod.rs | 12 ++--- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/Tools/mkoverlay/src/creator/ldscript.rs b/src/Tools/mkoverlay/src/creator/ldscript.rs index bb27c2d5..4a2592fa 100644 --- a/src/Tools/mkoverlay/src/creator/ldscript.rs +++ b/src/Tools/mkoverlay/src/creator/ldscript.rs @@ -2,20 +2,59 @@ use super::super::types::OverlaySlot; use tool_helper::{Error, format_if_error, Output}; use std::io::Write; -const DEFAULT_LD_SCRIPT:&'static str = r#" -__heap_base = __bss_end; -"#; +const OVERLAY_DEFAULT_START:&'static str = "__boot_loader_end"; // < will probably be "__boot_loader_start" later pub fn write(output: &mut Output, overlay_desc: &Vec) -> Result<(), Error> { - if overlay_desc.is_empty() { - return write_default(output); + fn create_heap_base(overlay_desc: &Vec) -> String { + let mut heap_start = format!("MAX({}, {}_end)", OVERLAY_DEFAULT_START, overlay_desc[0].name); + + for slot in overlay_desc.iter().skip(1) { + heap_start = format!("MAX({}_end, {})", slot.name, heap_start); + } + heap_start } - format_if_error!(write!(output, "Dino\n"), "Writing test output failed with: {error_text}")?; + fn get_slot_start_adr(slot: &OverlaySlot) -> &str { + if let Some(slot_adr) = &slot.start_adr { + slot_adr.as_ref() + } + + else { + OVERLAY_DEFAULT_START + } + } + + if overlay_desc.is_empty() { + return write_heap_base(output, OVERLAY_DEFAULT_START); + } + + write_heap_base(output, &create_heap_base(overlay_desc))?; + writeln!(output, "")?; + writeln!(output, "SECTION {{")?; + for slot in overlay_desc { + writeln!(output, "\t// {}", slot.name)?; + writeln!(output, "\tOVERLAY {} : NOCROSSREFS SUBALIGN(4) {{", get_slot_start_adr(slot))?; + for section in &slot.sections { + writeln!(output, "\t\t.{} {{", section.name)?; + writeln!(output, "\t\t\t__{}_start = .;", section.name)?; + section.file_pattern.iter().try_for_each(|patr| writeln!(output, "\t\t\tKEEP({}(.header)", patr))?; + writeln!(output, "\t\t\t__main_area_ctor = .;")?; + + for section_type in [".text.startup._GLOBAL__*", ".ctors", ".text.*", ".rodata*", ".sdata*", ".data*", ".sbss*", ".bss*"] { + section.file_pattern.iter().try_for_each(|patr| writeln!(output, "\t\t\t{}({})", patr, section_type))?; + } + writeln!(output, "\t\t}}")?; + } + writeln!(output, "\t}}")?; + writeln!(output, "\t{}_end = .;", slot.name)?; + writeln!(output, "")?; + } + writeln!(output, "\t/DISCARD/ : {{ *(.comment*) *(.gnu.*) *(.mdebug*) *(.debug_*) *(.pdr)}}")?; + writeln!(output, "}}")?; return Ok(()); } -fn write_default(output: &mut Output) -> Result<(), Error> { - format_if_error!(output.write(DEFAULT_LD_SCRIPT.as_bytes()), "Writing default LD Script failed with: {error_text}")?; +fn write_heap_base(output: &mut Output, heap_base: &str) -> Result<(), Error> { + format_if_error!(writeln!(output, "__heap_base = {};", heap_base), "Writing default LD Script failed with: {error_text}")?; Ok(()) } \ No newline at end of file diff --git a/src/Tools/mkoverlay/src/main.rs b/src/Tools/mkoverlay/src/main.rs index d33d9630..3f91e7e0 100644 --- a/src/Tools/mkoverlay/src/main.rs +++ b/src/Tools/mkoverlay/src/main.rs @@ -3,7 +3,7 @@ use tool_helper::{Error, open_output}; fn generate_file() -> Vec { let mut slots = Vec::new(); - let mut slot = OverlaySlot::new("slot_0".to_owned(), String::new()); + let mut slot = OverlaySlot::new("slot_0".to_owned(), None); let mut section = OverlaySection::new("main_area".to_owned()); section.add_file_pattern("bin/PSX-release/src/*.o".to_owned()); diff --git a/src/Tools/mkoverlay/src/types/mod.rs b/src/Tools/mkoverlay/src/types/mod.rs index ed3471bc..d72cf54f 100644 --- a/src/Tools/mkoverlay/src/types/mod.rs +++ b/src/Tools/mkoverlay/src/types/mod.rs @@ -1,7 +1,7 @@ #[derive(Debug)] pub struct OverlaySection { - name: String, - file_pattern: Vec + pub(super) name: String, + pub(super) file_pattern: Vec } impl OverlaySection { @@ -16,13 +16,13 @@ impl OverlaySection { #[derive(Debug)] pub struct OverlaySlot { - name: String, - start_adr: String, - sections: Vec + pub(super) name: String, + pub(super) start_adr: Option, + pub(super) sections: Vec } impl OverlaySlot { - pub fn new(name: String, start_adr: String) -> OverlaySlot { + pub fn new(name: String, start_adr: Option) -> OverlaySlot { OverlaySlot{name, start_adr, sections: Vec::new()} }