Skip to content

Commit

Permalink
Fix bugs with LOAD section size
Browse files Browse the repository at this point in the history
LOAD blocks did not properly update their parent's size until after closed
Additionally, section size wasn't correctly sanitized inside LOAD blocks
  • Loading branch information
ISSOtm committed Sep 3, 2020
1 parent e053213 commit 9d62b4b
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/asm/section.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,29 @@ static inline void checkcodesection(void)
fatalerror("UNIONs cannot contain code or data");
}

static inline void checkSectionSize(struct Section const *sect, uint32_t size)
{
uint32_t maxSize = maxsize[sect->nType];

if (size > maxSize)
fatalerror("Section '%s' grew too big (max size = 0x%" PRIX32 " bytes, reached 0x%" PRIX32 ").",
sect->pzName, maxSize, size);
}

/*
* Check if the section has grown too much.
*/
static void reserveSpace(uint32_t delta_size)
static inline void reserveSpace(uint32_t delta_size)
{
uint32_t maxSize = maxsize[pCurrentSection->nType];
uint32_t newSize = curOffset + delta_size;

/*
* This check is here to trap broken code that generates sections that
* are too big and to prevent the assembler from generating huge object
* files or trying to allocate too much memory.
* A check at the linking stage is still necessary.
*/
if (newSize > maxSize)
fatalerror("Section '%s' is too big (max size = 0x%" PRIX32 " bytes, reached 0x%" PRIX32 ").",
pCurrentSection->pzName, maxSize, newSize);
checkSectionSize(pCurrentSection, curOffset + loadOffset + delta_size);
if (currentLoadSection)
checkSectionSize(currentLoadSection, curOffset + delta_size);
}

struct Section *out_FindSectionByName(const char *pzName)
Expand Down Expand Up @@ -384,8 +390,8 @@ void sect_AlignPC(uint8_t alignment, uint16_t offset)
static inline void growSection(uint32_t growth)
{
curOffset += growth;
if (curOffset > pCurrentSection->size)
pCurrentSection->size = curOffset;
if (curOffset + loadOffset > pCurrentSection->size)
pCurrentSection->size = curOffset + loadOffset;
if (currentLoadSection && curOffset > currentLoadSection->size)
currentLoadSection->size = curOffset;
}
Expand Down
5 changes: 5 additions & 0 deletions test/asm/load-begin.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SECTION "test", ROM0
LOAD "RAM", WRAM0
ld a, 5
ENDL
db 1
Empty file added test/asm/load-begin.err
Empty file.
Empty file added test/asm/load-begin.out
Empty file.
1 change: 1 addition & 0 deletions test/asm/load-begin.out.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
>
5 changes: 5 additions & 0 deletions test/asm/load-overflow.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SECTION "Overflow", ROM0
ds $6000
LOAD "oops",WRAM0
ds $2001
ENDL
2 changes: 2 additions & 0 deletions test/asm/load-overflow.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR: load-overflow.asm(4):
Section 'Overflow' grew too big (max size = 0x8000 bytes, reached 0x8001).
Empty file added test/asm/load-overflow.out
Empty file.
5 changes: 5 additions & 0 deletions test/asm/load-trail.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SECTION "test", ROM0
db 1
LOAD "RAM", WRAM0
ld a, 5
ENDL
Empty file added test/asm/load-trail.err
Empty file.
Empty file added test/asm/load-trail.out
Empty file.
1 change: 1 addition & 0 deletions test/asm/load-trail.out.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
>

0 comments on commit 9d62b4b

Please sign in to comment.