From 9d62b4b9bbff03398bfb44a05195e09aa246392b Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Thu, 3 Sep 2020 12:06:13 +0200 Subject: [PATCH] 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 --- src/asm/section.c | 24 +++++++++++++++--------- test/asm/load-begin.asm | 5 +++++ test/asm/load-begin.err | 0 test/asm/load-begin.out | 0 test/asm/load-begin.out.bin | 1 + test/asm/load-overflow.asm | 5 +++++ test/asm/load-overflow.err | 2 ++ test/asm/load-overflow.out | 0 test/asm/load-trail.asm | 5 +++++ test/asm/load-trail.err | 0 test/asm/load-trail.out | 0 test/asm/load-trail.out.bin | 1 + 12 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 test/asm/load-begin.asm create mode 100644 test/asm/load-begin.err create mode 100644 test/asm/load-begin.out create mode 100644 test/asm/load-begin.out.bin create mode 100644 test/asm/load-overflow.asm create mode 100644 test/asm/load-overflow.err create mode 100644 test/asm/load-overflow.out create mode 100644 test/asm/load-trail.asm create mode 100644 test/asm/load-trail.err create mode 100644 test/asm/load-trail.out create mode 100644 test/asm/load-trail.out.bin diff --git a/src/asm/section.c b/src/asm/section.c index 2f2969db..f26e1c11 100644 --- a/src/asm/section.c +++ b/src/asm/section.c @@ -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) @@ -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; } diff --git a/test/asm/load-begin.asm b/test/asm/load-begin.asm new file mode 100644 index 00000000..dc239f02 --- /dev/null +++ b/test/asm/load-begin.asm @@ -0,0 +1,5 @@ +SECTION "test", ROM0 +LOAD "RAM", WRAM0 + ld a, 5 +ENDL + db 1 diff --git a/test/asm/load-begin.err b/test/asm/load-begin.err new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/load-begin.out b/test/asm/load-begin.out new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/load-begin.out.bin b/test/asm/load-begin.out.bin new file mode 100644 index 00000000..5e69d04f --- /dev/null +++ b/test/asm/load-begin.out.bin @@ -0,0 +1 @@ +> \ No newline at end of file diff --git a/test/asm/load-overflow.asm b/test/asm/load-overflow.asm new file mode 100644 index 00000000..10a75adc --- /dev/null +++ b/test/asm/load-overflow.asm @@ -0,0 +1,5 @@ +SECTION "Overflow", ROM0 + ds $6000 +LOAD "oops",WRAM0 + ds $2001 +ENDL diff --git a/test/asm/load-overflow.err b/test/asm/load-overflow.err new file mode 100644 index 00000000..be3e50b9 --- /dev/null +++ b/test/asm/load-overflow.err @@ -0,0 +1,2 @@ +ERROR: load-overflow.asm(4): + Section 'Overflow' grew too big (max size = 0x8000 bytes, reached 0x8001). diff --git a/test/asm/load-overflow.out b/test/asm/load-overflow.out new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/load-trail.asm b/test/asm/load-trail.asm new file mode 100644 index 00000000..2cc4e262 --- /dev/null +++ b/test/asm/load-trail.asm @@ -0,0 +1,5 @@ +SECTION "test", ROM0 + db 1 +LOAD "RAM", WRAM0 + ld a, 5 +ENDL diff --git a/test/asm/load-trail.err b/test/asm/load-trail.err new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/load-trail.out b/test/asm/load-trail.out new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/load-trail.out.bin b/test/asm/load-trail.out.bin new file mode 100644 index 00000000..4baf9b11 --- /dev/null +++ b/test/asm/load-trail.out.bin @@ -0,0 +1 @@ +> \ No newline at end of file