From 5c40ef119ea5211ab6f99bf808632dbd0ebfec0c Mon Sep 17 00:00:00 2001 From: Jaby Blubb Date: Sun, 27 Aug 2023 02:43:59 +0200 Subject: [PATCH] Share bss section with planschi section --- .../PSX/File/Processor/cd_file_processor.hpp | 4 +- lib/Makefile | 2 +- lib/PSEXETarget.mk | 2 +- lib/psexe.ld | 110 ++++++++++-------- src/Library/Makefile | 1 + src/Library/src/BootLoader/start_boot.cpp | 10 ++ src/Tools/mkoverlay/Cargo.toml | 2 +- src/Tools/mkoverlay/src/creator/ldscript.rs | 40 ++----- 8 files changed, 87 insertions(+), 84 deletions(-) diff --git a/include/PSX/File/Processor/cd_file_processor.hpp b/include/PSX/File/Processor/cd_file_processor.hpp index 42b408c5..19e42136 100644 --- a/include/PSX/File/Processor/cd_file_processor.hpp +++ b/include/PSX/File/Processor/cd_file_processor.hpp @@ -7,7 +7,7 @@ #include "../cd_file_types.hpp" #include "file_processor.hpp" -extern "C" uint32_t __heap_base; +extern "C" uint32_t __heap_start; namespace JabyEngine { class CDFileProcessor { @@ -20,7 +20,7 @@ namespace JabyEngine { size_t sector_count = 0; static constexpr BufferConfiguration new_default() { - return {&__heap_base, BufferConfiguration::MediumSectorCount}; + return {&__heap_start, BufferConfiguration::MediumSectorCount}; } }; diff --git a/lib/Makefile b/lib/Makefile index 4b9d388f..01616051 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -58,7 +58,7 @@ CCFLAGS += -DJABYENGINE_$(TV_FORMAT) #Linker flags LDFLAGS_release += -Os -LDFLAGS_all += -Wl,-Map=$(TARGET).map -nostdlib -T$(AUTO_OVERLAY_DIR)/Overlays.ld -T$(JABY_ENGINE_DIR)/lib/psexe.ld -static -Wl,--gc-sections -Wl,--build-id=none -Wl,--no-check-sections +LDFLAGS_all += -Wl,-Map=$(TARGET).map -nostdlib -T$(JABY_ENGINE_DIR)/lib/psexe.ld -static -Wl,--gc-sections -Wl,--build-id=none -Wl,--no-check-sections LDFLAGS_all += $(ARCHFLAGS) -Wl,--oformat=$(FORMAT) LDFLAGS_all += $(LDFLAGS_$(BUILD_PROFILE)) diff --git a/lib/PSEXETarget.mk b/lib/PSEXETarget.mk index 6f16adbd..ceef5f28 100644 --- a/lib/PSEXETarget.mk +++ b/lib/PSEXETarget.mk @@ -11,7 +11,7 @@ OVERLAY_TARGET = $(foreach ovl, $(OVERLAYSECTION), $(OUTPUT_DIR)/Overlay$(ovl)) #Linking rule $(TARGET).elf: $(OBJS) $(JABY_ENGINE_LIB_DIR)/lib$(JABY_ENGINE_LIB_NAME).a $(AUTO_OVERLAY_DIR)/Overlays.ld - $(LD) -o $(TARGET).elf $(LDFLAGS_all) $(LDFLAGS) $(OBJS) -L$(JABY_ENGINE_LIB_DIR) -l$(JABY_ENGINE_LIB_NAME) $(LIBS) + $(LD) -o $(TARGET).elf $(LDFLAGS_all) $(LDFLAGS) $(OBJS) -L$(JABY_ENGINE_LIB_DIR) -L$(AUTO_OVERLAY_DIR) -l$(JABY_ENGINE_LIB_NAME) $(LIBS) #Strips the psexe $(TARGET).psexe: $(TARGET).elf diff --git a/lib/psexe.ld b/lib/psexe.ld index aa5c6e7d..bdec0f2f 100644 --- a/lib/psexe.ld +++ b/lib/psexe.ld @@ -25,7 +25,6 @@ SOFTWARE. */ OUTPUT_FORMAT("binary") - EXTERN(_ZN10JabyEngine5startEv) ENTRY(_ZN10JabyEngine5startEv) @@ -34,7 +33,6 @@ TLOAD_ADDR = DEFINED(TLOAD_ADDR) ? TLOAD_ADDR : 0x80010000; MEMORY { loader : ORIGIN = (TLOAD_ADDR - 0x800), LENGTH = 2048 ram (rwx) : ORIGIN = 0x80010000, LENGTH = 2M - 0x10000 - ram2 (rwx) : ORIGIN = 0x80010000, LENGTH = 2M - 0x10000 dcache : ORIGIN = 0x1f800000, LENGTH = 0x400 } @@ -62,38 +60,39 @@ SECTIONS { } /*Overlay sections created by mkoverlay*/ +INCLUDE Overlays.ld SECTIONS { - .planschi __bss_end : SUBALIGN(4) + .planschi __engine_bss_end : SUBALIGN(4) { __planschi_start = .; __boot_loader_start = .; - *libJabyEngine.a:*_boot.o(.text.startup._GLOBAL__*) + *libJabyEngine_*.a:*_boot.o(.text.startup._GLOBAL__*) *_boot.o(.text.startup._GLOBAL__*) - *libJabyEngine.a:*_boot.o(.ctors) + *libJabyEngine_*.a:*_boot.o(.ctors) *_boot.o(.ctors) - *libJabyEngine.a:*_boot.o(.text.*) + *libJabyEngine_*.a:*_boot.o(.text.*) *_boot.o(.text.*) - *libJabyEngine.a:*_boot.o(.rodata*) + *libJabyEngine_*.a:*_boot.o(.rodata*) *_boot.o(.rodata*) - *libJabyEngine.a:*_boot.o(.sdata*) + *libJabyEngine_*.a:*_boot.o(.sdata*) *_boot.o(.sdata*) - *libJabyEngine.a:*_boot.o(.data*) + *libJabyEngine_*.a:*_boot.o(.data*) *_boot.o(.data*) - *libJabyEngine.a:*_boot.o(.sbss*) + *libJabyEngine_*.a:*_boot.o(.sbss*) *_boot.o(.sbss*) - *libJabyEngine.a:*_boot.o(.bss*) + *libJabyEngine_*.a:*_boot.o(.bss*) *_boot.o(.bss*) - *libJabyEngine.a:*_boot.o(*) + *libJabyEngine_*.a:*_boot.o(*) *_boot.o(*) . = ALIGN(4); __boot_loader_end = .; /*Only needed for the PSX BIOS to load the entire game*/ . = ALIGN(2048); - __planschi_end = .; - } + __planschi_end = .; + } } SECTIONS { @@ -141,37 +140,36 @@ SECTIONS { . = . + 1992; } > loader - __persistent_lbas_start = ABSOLUTE(.); .persistent_lbas TLOAD_ADDR : { - __persistent_lbas = .; - KEEP(*(.header.lbas)) + __persistent_lbas_start = .; + __persistent_lbas = .; + KEEP(*(.header.lbas)) + . = ALIGN(4); + __persistent_lbas_end = .; } > ram - . = ALIGN(4); - __persistent_lbas_end = ABSOLUTE(.); __ftext_start = ABSOLUTE(.); .text : { - *(.start) - *(.init) - KEEP (*(SORT_NONE(.fini))) - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) + __text_start = .; + *(.start) + *(.init) + KEEP (*(SORT_NONE(.fini))) + *(.text.unlikely .text.*_unlikely .text.unlikely.*) + *(.text.exit .text.exit.*) + *(.text.startup .text.startup.*) + *(.text.hot .text.hot.*) + *(.text .stub .text.* .gnu.linkonce.t.*) + . = ALIGN(16); + KEEP(*(.init)) + . = ALIGN(16); + KEEP(*(.fini)) . = ALIGN(16); - KEEP(*(.init)) - . = ALIGN(16); - KEEP(*(.fini)) + __text_end = .; } > ram - - . = ALIGN(16); - __text_end = .; __ftext_end = ABSOLUTE(.); __fdata_start = ABSOLUTE(.); - .rodata : { *(.rodata .rodata.* .rdata .rdata.* .gnu.linkonce.r.*) . = ALIGN(16); @@ -210,33 +208,43 @@ SECTIONS { *(.rodata1) } > ram - __data_start = .; .data : { - *(.a0table) - *(.data .data.* .gnu.linkonce.d.*) - *(.data1) - *(.sdata .sdata.* .gnu.linkonce.s.*) - *(.sdata2 .sdata2.* .gnu.linkonce.s2.*) - *(.got.plt) - *(.got) + __data_start = .; + *(.a0table) + *(.data .data.* .gnu.linkonce.d.*) + *(.data1) + *(.sdata .sdata.* .gnu.linkonce.s.*) + *(.sdata2 .sdata2.* .gnu.linkonce.s2.*) + *(.got.plt) + *(.got) + __data_end = .; } > ram - __data_end = .; + .engine_bss : { + __engine_bss_start = .; + *libJabyEngine_*.a:*(.dynsbss) + *libJabyEngine_*.a:*(.sbss .sbss.* .gnu.linkonce.sb.*) + *libJabyEngine_*.a:*(.scommon) + *libJabyEngine_*.a:*(.dynbss) + *libJabyEngine_*.a:*(.bss .bss.* .gnu.linkonce.b.*) + *libJabyEngine_*.a:*(COMMON) + . = ALIGN(4); + __engine_bss_end = .; + } > ram __fdata_end = .; - __bss_start = .; - .bss : { + + .bss __persistent_overlay_end (NOLOAD) : { + __bss_start = .; *(.dynsbss) *(.sbss .sbss.* .gnu.linkonce.sb.*) *(.scommon) *(.dynbss) *(.bss .bss.* .gnu.linkonce.b.*) *(COMMON) + . = ALIGN(4); + __bss_end = .; } > ram - . = ALIGN(4); - __bss_end = .; - __heap_start = __heap_base; - - /*. = ADDR(.text) - 0x800;*/ + __heap_start = __bss_end; __end = .; -} +} \ No newline at end of file diff --git a/src/Library/Makefile b/src/Library/Makefile index d4563cd1..2b77b4c8 100644 --- a/src/Library/Makefile +++ b/src/Library/Makefile @@ -1,3 +1,4 @@ +include ../../lib/RebuildTarget.mk JABY_ENGINE_DIR = ../../ ARTIFACT = libJabyEngine_$(TV_FORMAT) diff --git a/src/Library/src/BootLoader/start_boot.cpp b/src/Library/src/BootLoader/start_boot.cpp index 11fb9a3a..f3e8ea9b 100644 --- a/src/Library/src/BootLoader/start_boot.cpp +++ b/src/Library/src/BootLoader/start_boot.cpp @@ -3,6 +3,13 @@ #include #include +extern "C" uint32_t __heap_start; +extern "C" uint32_t __bss_start; +extern "C" uint32_t __bss_end; +extern "C" uint32_t __bss_len; +extern "C" uint32_t __planschi_start; +extern "C" uint32_t __planschi_end; + namespace JabyEngine { namespace boot { namespace Start { @@ -29,6 +36,9 @@ namespace JabyEngine { void start() { printf("Starting Planschbecken\n"); + printf("Heap starts @0x%p\n", &__heap_start); + printf("BSS from 0x%p to 0x%p (%u)\n", &__bss_start, &__bss_end, __bss_len); + printf("PLANSCHI from 0x%p to 0x%p\n", &__planschi_start, &__planschi_end); boot::Start::setup(); printf("Running main...\n"); diff --git a/src/Tools/mkoverlay/Cargo.toml b/src/Tools/mkoverlay/Cargo.toml index b13fa1dc..569ece16 100644 --- a/src/Tools/mkoverlay/Cargo.toml +++ b/src/Tools/mkoverlay/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mkoverlay" -version = "1.1.1" +version = "1.5.0" edition = "2021" [profile.release] diff --git a/src/Tools/mkoverlay/src/creator/ldscript.rs b/src/Tools/mkoverlay/src/creator/ldscript.rs index 7e636375..a553987a 100644 --- a/src/Tools/mkoverlay/src/creator/ldscript.rs +++ b/src/Tools/mkoverlay/src/creator/ldscript.rs @@ -1,9 +1,7 @@ use super::super::types::{OverlayDesc, OverlaySection}; -use tool_helper::{Error, format_if_error, Output}; +use tool_helper::{Error, Output}; use std::io::Write; -const OVERLAY_DEFAULT_START:&'static str = "__planschi_start"; // < will probably be "__boot_loader_start" later - macro_rules! section_end_name_template { () => { "__{}_end" @@ -11,25 +9,8 @@ macro_rules! section_end_name_template { } pub fn write(output: &mut Output, overlay_desc: &OverlayDesc) -> Result<(), Error> { - fn create_heap_base(overlay_desc: &OverlayDesc) -> String { - let (first_entry, additional_slots_iter) = (&overlay_desc[0].name, overlay_desc.iter().skip(1)); - - let mut heap_start = format!(concat!("MAX({}, ", section_end_name_template!(), ")"), OVERLAY_DEFAULT_START, first_entry); - for slot in additional_slots_iter { - heap_start = format!(concat!("MAX(", section_end_name_template!(), ", {})"), slot.name, heap_start); - } - heap_start - } - - if overlay_desc.is_empty() { - write_heap_base(output, OVERLAY_DEFAULT_START)?; - } - - else { - write_heap_base(output, &create_heap_base(&overlay_desc))?; - } - - let mut slot_start_adr = format!(section_end_name_template!(), "bss"); + let has_overlays = !overlay_desc.is_empty(); + let mut slot_start_adr = format!(section_end_name_template!(), "engine_bss"); writeln!(output, "")?; writeln!(output, "SECTIONS {{")?; @@ -38,22 +19,25 @@ pub fn write(output: &mut Output, overlay_desc: &OverlayDesc) -> Result<(), Erro writeln!(output, "\t/*{}*/", slot.name)?; writeln!(output, "\tOVERLAY {} : NOCROSSREFS SUBALIGN(4) {{", slot_start_adr)?; write_section(output, &slot.sections, false)?; - writeln!(output, "\t}}")?; + writeln!(output, "\t}} > ram")?; writeln!(output, "\t. = ALIGN(4);")?; writeln!(output, concat!("\t", section_end_name_template!(), " = .;"), slot.name)?; writeln!(output, "")?; slot_start_adr = format!(section_end_name_template!(), slot.name); } + + if has_overlays { + writeln!(output, "\t__persistent_overlay_end = .;")?; + } + + else { + writeln!(output, "\t__persistent_overlay_end =__engine_bss_end;")?; + } writeln!(output, "}}")?; return Ok(()); } -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(()) -} - fn write_section(output: &mut Output, sections: &Vec, add_end_name: bool) -> Result<(), Error> { for section in sections { writeln!(output, "\t\t.{} {{", section.name)?;