diff --git a/empty-fixed-section.asm b/empty-fixed-section.asm new file mode 100644 index 00000000..fc1a45cf --- /dev/null +++ b/empty-fixed-section.asm @@ -0,0 +1,2 @@ +SECTION "fixed", ROM0[$4321] + assert @ == $4321 diff --git a/src/link/assign.cpp b/src/link/assign.cpp index 63c6d758..e3c7c47b 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -338,8 +338,16 @@ static void placeSection(Section §ion) { } } - assignSection(section, location); - return; + // This check safely handles sections with impossible alignment and no fixed address. + // The above `location.address = section.typeInfo().startAddr` would be valid on its own, + // but `location.makeAddressAligned(...)` can increase `location.address` above the valid + // 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) { + assignSection(section, location); + return; + } } FreeSpaceIter iter = tryPlacing(section, location); diff --git a/test/link/empty-aligned-section-no-t.out b/test/link/empty-aligned-section-no-t.out new file mode 100644 index 00000000..62b8f430 --- /dev/null +++ b/test/link/empty-aligned-section-no-t.out @@ -0,0 +1,2 @@ +FATAL: Unable to place "aligned" (ROM0 section) with align mask $8000 and offset $6420 +Linking aborted with 1 error diff --git a/test/link/empty-aligned-section-t.out b/test/link/empty-aligned-section-t.out new file mode 100644 index 00000000..e69de29b diff --git a/test/link/empty-aligned-section.asm b/test/link/empty-aligned-section.asm new file mode 100644 index 00000000..672add58 --- /dev/null +++ b/test/link/empty-aligned-section.asm @@ -0,0 +1,2 @@ +SECTION "aligned", ROM0, ALIGN[15, $6420] + assert @ == $6420 diff --git a/test/link/empty-fixed-section-no-t.out b/test/link/empty-fixed-section-no-t.out new file mode 100644 index 00000000..7fb6d81a --- /dev/null +++ b/test/link/empty-fixed-section-no-t.out @@ -0,0 +1,2 @@ +error: Section "z"'s fixed address $4321 is outside of range [$0000; $3fff] +Linking failed with 1 error diff --git a/test/link/empty-fixed-section-t.out b/test/link/empty-fixed-section-t.out new file mode 100644 index 00000000..e69de29b diff --git a/test/link/empty-fixed-section.asm b/test/link/empty-fixed-section.asm new file mode 100644 index 00000000..88e0f026 --- /dev/null +++ b/test/link/empty-fixed-section.asm @@ -0,0 +1,2 @@ +SECTION "z", ROM0[$4321] + assert @ == $4321