From 50270a820c85bbd548725af2329f97a3f8773fd5 Mon Sep 17 00:00:00 2001 From: Wolfvak Date: Sat, 15 Mar 2025 10:25:23 -0300 Subject: [PATCH] Split up the ARM9 code and data sections (#883) * Split up the ARM9 code (.text, .vectors) and data (.rodata, .data, .bss) sections into their own ELFs. This allows us to use more ARM9 WRAM while leaving the 128k BootROM mirror intact. * use the makefile definition * Also dump section headers on .dis file --- .gitignore | 1 + Makefile | 14 +++++++++----- Makefile.build | 3 ++- Makefile.common | 1 + arm9/Makefile | 6 ++++++ arm9/link.ld | 20 +++++++++++++------- 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index a9f5fc8..5ff6ec3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.obj *.elf *.map +*.dis # Precompiled Headers *.gch diff --git a/Makefile b/Makefile index da57d2e..5cda78c 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSIO -fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \ -Wno-unused-function -Wno-format-truncation -Wno-format-nonliteral $(INCLUDE) -ffunction-sections -fdata-sections export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=4096 -ELF := arm9/arm9.elf arm11/arm11.elf +ELF := arm9/arm9_code.elf arm9/arm9_data.elf arm11/arm11.elf .PHONY: all firm $(VRAM_TAR) elf release clean all: firm @@ -104,9 +104,13 @@ $(TRF_FOLDER)/%.trf: $(JSON_FOLDER)/%.json %.elf: .FORCE @echo "Building $@" - @$(MAKE) --no-print-directory -C $(@D) + @$(MAKE) --no-print-directory -C $(@D) $(@F) -arm9/arm9.elf: $(VRAM_TAR) $(LANGUAGE_INL) +# Indicate a few explicit dependencies: +# The ARM9 data section depends on the VRAM drive +arm9/arm9_data.elf: $(VRAM_TAR) $(LANGUAGE_INL) +# And the code section depends on the data section being built already +arm9/arm9_code.elf: arm9/arm9_data.elf firm: $(ELF) $(TRF_FILES) @mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)") @@ -114,9 +118,9 @@ firm: $(ELF) $(TRF_FILES) @echo "[VERSION] $(VERSION)" @echo "[BUILD] $(DBUILTL)" @echo "[FIRM] $(FIRM)" - @$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -D $(ELF) -C NDMA XDMA + @$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA @echo "[FIRM] $(FIRMD)" - @$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -D $(ELF) -C NDMA XDMA + @$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA vram0: $(VRAM_TAR) .FORCE # legacy target name diff --git a/Makefile.build b/Makefile.build index 0a36f6f..6df0778 100755 --- a/Makefile.build +++ b/Makefile.build @@ -11,11 +11,12 @@ all: $(TARGET).elf .PHONY: clean clean: - @rm -rf $(BUILD) $(TARGET).elf $(TARGET).map + @rm -rf $(BUILD) $(TARGET).elf $(TARGET).dis $(TARGET).map $(TARGET).elf: $(OBJECTS) $(OBJECTS_COMMON) @mkdir -p "$(@D)" @$(CC) $(LDFLAGS) $^ -o $@ + @$(OBJDUMP) -S -h $@ > $@.dis $(BUILD)/%.cmn.o: $(COMMON_DIR)/%.c @mkdir -p "$(@D)" diff --git a/Makefile.common b/Makefile.common index d8c8258..24036db 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1,3 +1,4 @@ +export OBJDUMP := arm-none-eabi-objdump dirname = $(shell dirname $(1)) diff --git a/arm9/Makefile b/arm9/Makefile index 0c5b852..b31133b 100644 --- a/arm9/Makefile +++ b/arm9/Makefile @@ -15,3 +15,9 @@ LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto include ../Makefile.common include ../Makefile.build + +arm9_data.elf: arm9.elf + $(OBJCOPY) -O elf32-littlearm -j .rodata* -j .data* -j .bss* $< $@ + +arm9_code.elf: arm9.elf + $(OBJCOPY) -O elf32-littlearm -j .text* -j .vectors* $< $@ diff --git a/arm9/link.ld b/arm9/link.ld index 5ff9a63..b8beedf 100644 --- a/arm9/link.ld +++ b/arm9/link.ld @@ -5,7 +5,9 @@ ENTRY(_start) MEMORY { VECTORS (RX) : ORIGIN = 0x08000000, LENGTH = 64 - AHBWRAM (RWX) : ORIGIN = 0x08000040, LENGTH = 512K - 64 + CODEMEM (RX) : ORIGIN = 0x08000040, LENGTH = 512K - 64 + BOOTROM (R) : ORIGIN = 0x08080000, LENGTH = 128K /* BootROM mirrors, don't touch! */ + DATAMEM (RW) : ORIGIN = 0x080A0000, LENGTH = 384K } SECTIONS @@ -16,7 +18,7 @@ SECTIONS KEEP(*(.vectors)); . = ALIGN(4); __vectors_len = ABSOLUTE(.) - __vectors_vma; - } >VECTORS AT>AHBWRAM + } >VECTORS AT>CODEMEM .text : ALIGN(4) { __text_s = ABSOLUTE(.); @@ -24,24 +26,28 @@ SECTIONS *(.text*); . = ALIGN(4); __text_e = ABSOLUTE(.); - } >AHBWRAM + } >CODEMEM .rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); - } >AHBWRAM + __exidx_start = .; + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + __exidx_end = .; + . = ALIGN(4); + } >DATAMEM .data : ALIGN(4) { *(.data*); . = ALIGN(4); - } >AHBWRAM + } >DATAMEM - .bss : ALIGN(4) { + .bss (NOLOAD) : ALIGN(4) { __bss_start = .; *(.bss*); . = ALIGN(4); __bss_end = .; - } >AHBWRAM + } >DATAMEM __end__ = ABSOLUTE(.); }