Skip to content

Commit

Permalink
Merge pull request rh-hideout#1952 from Icedude907/build-linkerscript
Browse files Browse the repository at this point in the history
[Build System Rewrite] Linkerscript now tracks RAM/ROM usage
  • Loading branch information
GriffinRichards authored Nov 23, 2023
2 parents b302291 + e5ac2a1 commit 15e1cd8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 58 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ sound/**/*.bin
sound/songs/midi/*.s
tools/agbcc
*.map
*.ld
*.bat
*.dump
*.sa*
Expand Down
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ ifneq ($(MODERN),1)
CPPFLAGS += -I tools/agbcc/include -I tools/agbcc -nostdinc -undef
endif

LDFLAGS = -Map ../../$(MAP)

SHA1 := $(shell { command -v sha1sum || command -v shasum; } 2>/dev/null) -c
GFX := tools/gbagfx/gbagfx$(EXE)
AIF := tools/aif2pcm/aif2pcm$(EXE)
Expand Down Expand Up @@ -406,19 +404,20 @@ $(OBJ_DIR)/sym_ewram.ld: sym_ewram.txt
$(RAMSCRGEN) ewram_data $< ENGLISH > $@

ifeq ($(MODERN),0)
LD_SCRIPT := ld_script.txt
LD_SCRIPT := ld_script.ld
LD_SCRIPT_DEPS := $(OBJ_DIR)/sym_bss.ld $(OBJ_DIR)/sym_common.ld $(OBJ_DIR)/sym_ewram.ld
else
LD_SCRIPT := ld_script_modern.txt
LD_SCRIPT := ld_script_modern.ld
LD_SCRIPT_DEPS :=
endif

$(OBJ_DIR)/ld_script.ld: $(LD_SCRIPT) $(LD_SCRIPT_DEPS)
cd $(OBJ_DIR) && sed "s#tools/#../../tools/#g" ../../$(LD_SCRIPT) > ld_script.ld

LDFLAGS = -Map ../../$(MAP)
$(ELF): $(OBJ_DIR)/ld_script.ld $(OBJS) libagbsyscall
@echo "cd $(OBJ_DIR) && $(LD) $(LDFLAGS) -T ld_script.ld -o ../../$@ <objects> <lib>"
@cd $(OBJ_DIR) && $(LD) $(LDFLAGS) -T ld_script.ld -o ../../$@ $(OBJS_REL) $(LIB)
@cd $(OBJ_DIR) && $(LD) $(LDFLAGS) -T ld_script.ld --print-memory-usage -o ../../$@ $(OBJS_REL) $(LIB) | cat
$(FIX) $@ -t"$(TITLE)" -c$(GAME_CODE) -m$(MAKER_CODE) -r$(REVISION) --silent

$(ROM): $(ELF)
Expand Down
3 changes: 3 additions & 0 deletions gflib/malloc.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "global.h"
#include "malloc.h"

static void *sHeapStart;
static u32 sHeapSize;

EWRAM_DATA u8 gHeap[HEAP_SIZE] = {0};

#define MALLOC_SYSTEM_ID 0xA3A3

struct MemBlock {
Expand Down
4 changes: 2 additions & 2 deletions gflib/malloc.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef GUARD_ALLOC_H
#define GUARD_ALLOC_H

#define HEAP_SIZE 0x1C000

#define FREE_AND_SET_NULL(ptr) \
{ \
Expand All @@ -11,7 +10,8 @@

#define TRY_FREE_AND_SET_NULL(ptr) if (ptr != NULL) FREE_AND_SET_NULL(ptr)

extern u8 gHeap[];
#define HEAP_SIZE 0x1C000
extern u8 gHeap[HEAP_SIZE];

void *Alloc(u32 size);
void *AllocZeroed(u32 size);
Expand Down
46 changes: 22 additions & 24 deletions ld_script.txt → ld_script.ld
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@ ENTRY(Start)
gNumMusicPlayers = 4;
gMaxLines = 0;

MEMORY
{
EWRAM (rwx) : ORIGIN = 0x2000000, LENGTH = 256K
IWRAM (rwx) : ORIGIN = 0x3000000, LENGTH = 32K
ROM (rx) : ORIGIN = 0x8000000, LENGTH = 32M
}

/* Modify the following load addresses as needed to make more room. Alternately, delete both the
declarations below and their references further down to get rid of the gaps. */

__anim_mon_load_address = 0x8b00000;
__gfx_load_address = 0x8c00000;

SECTIONS {
. = 0x2000000;

ewram (NOLOAD) :
ewram 0x2000000 (NOLOAD) :
ALIGN(4)
{
gHeap = .;

. = 0x1C000;

INCLUDE "sym_ewram.ld"
src/*.o(ewram_data);
gflib/*.o(ewram_data);

*libc.a:impure.o(.data);
*libc.a:locale.o(.data);
*libc.a:mallocr.o(.data);
. = 0x40000;
}

. = 0x3000000;
} > EWRAM

iwram (NOLOAD) :
iwram 0x3000000 (NOLOAD) :
ALIGN(4)
{
/* .bss starts at 0x3000000 */
Expand All @@ -46,10 +45,9 @@ SECTIONS {
/* COMMON starts at 0x30022A8 */
INCLUDE "sym_common.ld"
*libc.a:sbrkr.o(COMMON);
end = .;
. = 0x8000;
}
} > IWRAM

/* BEGIN ROM DATA */
. = 0x8000000;

.text :
Expand Down Expand Up @@ -343,7 +341,7 @@ SECTIONS {
src/gym_leader_rematch.o(.text);
src/battle_transition_frontier.o(.text);
src/international_string_util.o(.text);
} =0
} > ROM =0

script_data :
ALIGN(4)
Expand All @@ -356,7 +354,7 @@ SECTIONS {
data/battle_ai_scripts.o(script_data);
data/contest_ai_scripts.o(script_data);
data/mystery_event_script_cmd_table.o(script_data);
} =0
} > ROM =0

lib_text :
ALIGN(4)
Expand Down Expand Up @@ -440,7 +438,7 @@ SECTIONS {
*libc.a:libcfunc.o(.text);
*libc.a:lseekr.o(.text);
*libc.a:readr.o(.text);
} =0
} > ROM =0

.rodata :
ALIGN(4)
Expand Down Expand Up @@ -705,7 +703,7 @@ SECTIONS {
data/mystery_gift.o(.rodata);
src/m4a_tables.o(.rodata);
data/sound_data.o(.rodata);
} =0
} > ROM =0

song_data :
ALIGN(4)
Expand Down Expand Up @@ -1240,7 +1238,7 @@ SECTIONS {
sound/songs/midi/ph_nurse_blend.o(.rodata);
sound/songs/midi/ph_nurse_held.o(.rodata);
sound/songs/midi/ph_nurse_solo.o(.rodata);
} =0
} > ROM =0

lib_rodata :
SUBALIGN(4)
Expand Down Expand Up @@ -1293,27 +1291,27 @@ SECTIONS {
*libc.a:lseekr.o(.rodata);
*libc.a:readr.o(.rodata);
src/libisagbprn.o(.rodata);
} =0
} > ROM =0

multiboot_data :
ALIGN(4)
{
data/multiboot_ereader.o(.rodata);
data/multiboot_berry_glitch_fix.o(.rodata);
data/multiboot_pokemon_colosseum.o(.rodata);
} =0
} > ROM =0

anim_mon_front_pic_data __anim_mon_load_address :
ALIGN(4)
{
src/anim_mon_front_pics.o(.rodata);
} =0
} > ROM =0

gfx_data __gfx_load_address :
ALIGN(4)
{
src/graphics.o(.rodata);
} =0
} > ROM =0

extra :
ALIGN(4)
Expand All @@ -1323,7 +1321,7 @@ SECTIONS {
src/*.o(.rodata);
gflib/*.o(.rodata);
data/*.o(.rodata);
} = 0
} > ROM = 0

/* DWARF debug sections.
Symbols in the DWARF debugging sections are relative to the beginning
Expand Down
46 changes: 20 additions & 26 deletions ld_script_modern.txt → ld_script_modern.ld
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,40 @@ ENTRY(Start)
gNumMusicPlayers = 4;
gMaxLines = 0;

MEMORY
{
EWRAM (rwx) : ORIGIN = 0x2000000, LENGTH = 256K
IWRAM (rwx) : ORIGIN = 0x3000000, LENGTH = 32K
ROM (rx) : ORIGIN = 0x8000000, LENGTH = 32M
}

SECTIONS {
. = 0x2000000;

ewram (NOLOAD) :
ewram 0x2000000 (NOLOAD) :
ALIGN(4)
{
gHeap = .;

. = 0x1C000;

src/*.o(ewram_data);
gflib/*.o(ewram_data);
} > EWRAM

. = 0x40000;
}

. = 0x3000000;

iwram (NOLOAD) :
iwram 0x3000000 (NOLOAD) :
ALIGN(4)
{
/* .bss starts at 0x3000000 */
src/*.o(.bss);
gflib/*.o(.bss);
data/*.o(.bss);
*libc.a:*.o(.bss*);
*libnosys.a:*.o(.bss*);

/* .bss.code starts at 0x3001AA8 */
src/m4a.o(.bss.code);

/* COMMON starts at 0x30022A8 */
src/*.o(COMMON);
gflib/*.o(COMMON);
*libc.a:*.o(COMMON);
*libnosys.a:*.o(COMMON);
end = .;
. = 0x8000;
}
} > IWRAM

/* BEGIN ROM DATA */
. = 0x8000000;

.text :
Expand All @@ -55,13 +49,13 @@ SECTIONS {
gflib/*.o(.text*);
src/*.o(.text*);
asm/*.o(.text*);
} =0
} > ROM =0

script_data :
ALIGN(4)
{
data/*.o(script_data);
} =0
} > ROM =0

lib_text :
ALIGN(4)
Expand All @@ -82,21 +76,21 @@ SECTIONS {
*libc.a:*.o(.text*);
*libnosys.a:*.o(.text*);
src/libisagbprn.o(.text);
} =0
} > ROM =0

.rodata :
ALIGN(4)
{
src/*.o(.rodata*);
gflib/*.o(.rodata*);
data/*.o(.rodata*);
} =0
} > ROM =0

song_data :
ALIGN(4)
{
sound/songs/*.o(.rodata);
} =0
} > ROM =0

lib_rodata :
SUBALIGN(4)
Expand All @@ -121,19 +115,19 @@ SECTIONS {
data/multiboot_ereader.o(.rodata);
data/multiboot_berry_glitch_fix.o(.rodata);
data/multiboot_pokemon_colosseum.o(.rodata);
} =0
} > ROM =0

anim_mon_front_pic_data :
ALIGN(4)
{
src/anim_mon_front_pics.o(.rodata);
} =0
} > ROM =0

gfx_data :
ALIGN(4)
{
src/graphics.o(.rodata);
} =0
} > ROM =0

/* DWARF debug sections.
Symbols in the DWARF debugging sections are relative to the beginning
Expand Down
1 change: 1 addition & 0 deletions sym_ewram.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.include "gflib/malloc.o"
.include "src/decompress.o"
.include "src/main.o"
.include "gflib/window.o"
Expand Down

0 comments on commit 15e1cd8

Please sign in to comment.