From ce5c8ce8ac94ae399579cca8d3659a7f4091a2d6 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Wed, 16 Sep 2026 23:30:51 -0400 Subject: [PATCH] Respect zero-size sections' align offsets Fixes #2095 This code pre-dates the introduction of alignment offsets, so it was never updated to take it into account. That said, some of the code that I have factored out is a little awkward because it will be used in an upcoming refactoring of the whole assignment loop, since that turns out to be more byzantine than it ought to be due to trying to handle a too disparate set of cases in a single loop. --- src/link/assign.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/link/assign.cpp b/src/link/assign.cpp index 9b8d47a2..2218b7fc 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -138,6 +138,14 @@ struct MemoryLocation { ++bank; return true; } + + void makeAddressAligned(uint16_t alignMask, uint16_t alignOfs) { + // By how much the address is past the target offset within the current alignment "page". + uint16_t offset = (address - alignOfs) & alignMask; + // Move by one page *minus* that "overshoot" offset. + // If it's 0, then this would move by a whole page, but `& alignMask` resets it back to 0. + address += ((alignMask + 1) - offset) & alignMask; + } }; // Checks whether a given location is suitable for placing a given section @@ -289,13 +297,10 @@ static void placeSection(Section §ion) { // Specially handle 0-byte SECTIONs, as they can't overlap anything if (section.size == 0) { - // Unless the SECTION has a fixed address or non-zero alignment offset, the starting - // address is fine for any alignment, as checked in `sect_DoSanityChecks`. - location.address = section.isAddressFixed ? section.org : section.typeInfo().startAddr; - if (section.isAlignFixed && !section.isAddressFixed) { - if (uint16_t offset = (location.address - section.alignOfs) & section.alignMask; - offset != 0) { - location.address += section.alignMask + 1 - offset; + if (!section.isAddressFixed) { + location.address = section.typeInfo().startAddr; + if (section.isAlignFixed) { + location.makeAddressAligned(section.alignMask, section.alignOfs); } } assignSection(section, location);