shithub: rgbds

Download patch

ref: 9d62b4b9bbff03398bfb44a05195e09aa246392b
parent: e05321356b68a1736d5d304bab0ee2bc4b39b7bf
author: ISSOtm <eldredhabert0@gmail.com>
date: Thu Sep 3 08:06:13 EDT 2020

Fix bugs with LOAD section size

LOAD blocks did not properly update their parent's size until after closed
Additionally, section size wasn't correctly sanitized inside LOAD blocks

--- a/src/asm/section.c
+++ b/src/asm/section.c
@@ -51,14 +51,20 @@
 		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
@@ -65,9 +71,9 @@
 	 * 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)
@@ -384,8 +390,8 @@
 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;
 }
--- /dev/null
+++ b/test/asm/load-begin.asm
@@ -1,0 +1,5 @@
+SECTION "test", ROM0
+LOAD "RAM", WRAM0
+	ld a, 5
+ENDL
+	db 1
--- /dev/null
+++ b/test/asm/load-begin.out.bin
@@ -1,0 +1,1 @@
+>
\ No newline at end of file
--- /dev/null
+++ b/test/asm/load-overflow.asm
@@ -1,0 +1,5 @@
+SECTION "Overflow", ROM0
+	ds $6000
+LOAD "oops",WRAM0
+	ds $2001
+ENDL
--- /dev/null
+++ b/test/asm/load-overflow.err
@@ -1,0 +1,2 @@
+ERROR: load-overflow.asm(4):
+    Section 'Overflow' grew too big (max size = 0x8000 bytes, reached 0x8001).
--- /dev/null
+++ b/test/asm/load-trail.asm
@@ -1,0 +1,5 @@
+SECTION "test", ROM0
+	db 1
+LOAD "RAM", WRAM0
+	ld a, 5
+ENDL
--- /dev/null
+++ b/test/asm/load-trail.out.bin
@@ -1,0 +1,1 @@
+>
\ No newline at end of file