From 85f96d671f731d112148d57048d3e73cbef08a77 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:58:30 -0400 Subject: [PATCH] Allow empty one-past-the-end sections (#2221) --- include/linkdefs.hpp | 3 +++ src/link/assign.cpp | 11 +++++------ src/link/layout.cpp | 6 +++--- src/link/output.cpp | 3 +-- src/link/section.cpp | 8 ++++---- test/link/empty-fixed-section-no-t.out | 2 +- test/link/empty-one-past-end-section.asm | 2 ++ test/link/empty-one-past-end-section.link | 3 +++ test/link/empty-one-past-end-section.out | 0 test/link/fixed-address-overflow-no-t.out | 2 ++ test/link/fixed-address-overflow-t.out | 0 test/link/fixed-address-overflow.asm | 2 ++ 12 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 test/link/empty-one-past-end-section.asm create mode 100644 test/link/empty-one-past-end-section.link create mode 100644 test/link/empty-one-past-end-section.out create mode 100644 test/link/fixed-address-overflow-no-t.out create mode 100644 test/link/fixed-address-overflow-t.out create mode 100644 test/link/fixed-address-overflow.asm diff --git a/include/linkdefs.hpp b/include/linkdefs.hpp index f88cdf36..2b2ccecd 100644 --- a/include/linkdefs.hpp +++ b/include/linkdefs.hpp @@ -107,6 +107,9 @@ struct SectionTypeInfo { // Returns a memory region's end address (last byte), e.g. 0x7FFF uint16_t endAddr() const { return startAddr + size - 1; } + // Returns a memory region's one-past-the-end address, e.g. 0x8000 + uint16_t onePastEndAddr() const { return startAddr + size; } + // Returns a memory region's number of banks, or 1 for regions without banking uint32_t nbBanks() const { return lastBank - firstBank + 1; } diff --git a/src/link/assign.cpp b/src/link/assign.cpp index f01a6480..ba6f4e06 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -261,10 +261,10 @@ static void static void assignSection(Section §ion, MemoryLocation const &location) { assume(location.address >= section.typeInfo().startAddr); // Zero-sized sections can start one past the end of their region. - assume(location.address <= section.typeInfo().endAddr() + 1); + assume(location.address <= section.typeInfo().onePastEndAddr()); // This one is not redundant, it guards against overflow! assume(location.address + section.size >= section.typeInfo().startAddr); - assume(location.address + section.size <= section.typeInfo().endAddr() + 1); + assume(location.address + section.size <= section.typeInfo().onePastEndAddr()); if (section.isAddressFixed) { assume(location.address == section.org); @@ -345,7 +345,7 @@ static void placeSection(Section §ion) { // range for its `section`, which would violate an assumption in `assignSection`. // Note that sections with fixed addresses are handled earlier by `sect_DoSanityChecks`, // but this check would safely handle them too if they ever reached it. - if (location.address <= section.typeInfo().endAddr() + 1) { + if (location.address <= section.typeInfo().onePastEndAddr()) { assignSection(section, location); return; } @@ -363,14 +363,13 @@ static void placeSection(Section §ion) { if (!section.isBankFixed || !section.isAddressFixed) { // If a section failed to go to several places, nothing we can report fatal("Unable to place %s", describeConstraintsOf(section).c_str()); - } else if (uint16_t onePastEnd = section.typeInfo().endAddr() + 1; - section.org + section.size > onePastEnd) { + } else if (section.org + section.size > section.typeInfo().onePastEndAddr()) { // If the section just can't fit the bank, report that fatal( "Unable to place %s: section runs past end of region ($%04x > $%04x)", describeConstraintsOf(section).c_str(), section.org + section.size, - onePastEnd + section.typeInfo().onePastEndAddr() ); } else { // Otherwise there is overlap with another section diff --git a/src/link/layout.cpp b/src/link/layout.cpp index d2ded1c4..e0f5d5e3 100644 --- a/src/link/layout.cpp +++ b/src/link/layout.cpp @@ -92,7 +92,7 @@ void layout_SetAddr(uint32_t addr) { if (uint16_t &pc = curAddr[activeType][activeBankIdx]; addr < pc) { scriptError("Cannot decrease the current address (from $%04x to $%04x)", pc, addr); - } else if (addr > typeInfo.endAddr() + 1u) { // Allow "one past the end" zero-sized sections. + } else if (addr > typeInfo.onePastEndAddr()) { // Allow "one past the end" zero-sized sections. scriptError( "Cannot set the current address to $%04" PRIx32 ": %s ends at $%04" PRIx16, addr, @@ -182,7 +182,7 @@ void layout_AlignTo(uint32_t alignment, uint32_t alignOfs) { ", past $%04" PRIx16, pc, static_cast(pc + length), - static_cast(typeInfo.endAddr() + 1) + typeInfo.onePastEndAddr() ); return; } @@ -212,7 +212,7 @@ void layout_Pad(uint32_t length) { "Cannot increase the current address by %u bytes: only %u bytes to $%04" PRIx16, length, typeInfo.size - offset, - static_cast(typeInfo.endAddr() + 1) + typeInfo.onePastEndAddr() ); } else { pc += length; diff --git a/src/link/output.cpp b/src/link/output.cpp index 837e52b2..f5462649 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -493,8 +493,7 @@ static void writeMapBank(SortedSections const §List, SectionType type, uint3 if (used == 0) { fputs("\tEMPTY\n", mapFile); } else { - uint16_t bankEndAddr = sectionTypeInfo[type].endAddr() + 1; - writeEmptySpace(prevEndAddr, bankEndAddr); + writeEmptySpace(prevEndAddr, sectionTypeInfo[type].onePastEndAddr()); uint16_t slack = sectionTypeInfo[type].size - used; fprintf(mapFile, "\tTOTAL EMPTY: $%04" PRIx16 " byte%s\n", slack, slack == 1 ? "" : "s"); diff --git a/src/link/section.cpp b/src/link/section.cpp index a36aca61..9fa0f595 100644 --- a/src/link/section.cpp +++ b/src/link/section.cpp @@ -325,21 +325,21 @@ static void doSanityChecks(Section §ion) { } // Ensure the target address is valid - if (section.org < typeInfo.startAddr || section.org > typeInfo.endAddr()) { + if (section.org < typeInfo.startAddr || section.org > typeInfo.onePastEndAddr()) { error( "Section \"%s\"'s fixed address $%04" PRIx16 " is outside of range [$%04" PRIx16 "; $%04" PRIx16 "]", section.name.c_str(), section.org, typeInfo.startAddr, - typeInfo.endAddr() + typeInfo.onePastEndAddr() ); - } else if (section.org + section.size > typeInfo.endAddr() + 1) { + } else if (section.org + section.size > typeInfo.onePastEndAddr()) { error( "Section \"%s\"'s end address $%04x is greater than last address $%04x", section.name.c_str(), section.org + section.size, - typeInfo.endAddr() + 1 + typeInfo.onePastEndAddr() ); } } diff --git a/test/link/empty-fixed-section-no-t.out b/test/link/empty-fixed-section-no-t.out index 7fb6d81a..e0817fe2 100644 --- a/test/link/empty-fixed-section-no-t.out +++ b/test/link/empty-fixed-section-no-t.out @@ -1,2 +1,2 @@ -error: Section "z"'s fixed address $4321 is outside of range [$0000; $3fff] +error: Section "z"'s fixed address $4321 is outside of range [$0000; $4000] Linking failed with 1 error diff --git a/test/link/empty-one-past-end-section.asm b/test/link/empty-one-past-end-section.asm new file mode 100644 index 00000000..bd2b9f15 --- /dev/null +++ b/test/link/empty-one-past-end-section.asm @@ -0,0 +1,2 @@ +section "test1", rom0[$4000] +section "test2", rom0 diff --git a/test/link/empty-one-past-end-section.link b/test/link/empty-one-past-end-section.link new file mode 100644 index 00000000..95a2700b --- /dev/null +++ b/test/link/empty-one-past-end-section.link @@ -0,0 +1,3 @@ +rom0 + org $4000 + "test2" diff --git a/test/link/empty-one-past-end-section.out b/test/link/empty-one-past-end-section.out new file mode 100644 index 00000000..e69de29b diff --git a/test/link/fixed-address-overflow-no-t.out b/test/link/fixed-address-overflow-no-t.out new file mode 100644 index 00000000..8251134f --- /dev/null +++ b/test/link/fixed-address-overflow-no-t.out @@ -0,0 +1,2 @@ +error: Section "test"'s end address $4001 is greater than last address $4000 +Linking failed with 1 error diff --git a/test/link/fixed-address-overflow-t.out b/test/link/fixed-address-overflow-t.out new file mode 100644 index 00000000..e69de29b diff --git a/test/link/fixed-address-overflow.asm b/test/link/fixed-address-overflow.asm new file mode 100644 index 00000000..1ac65d05 --- /dev/null +++ b/test/link/fixed-address-overflow.asm @@ -0,0 +1,2 @@ +section "test", rom0[$3ffe] +db 1, 2, 3