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.
This commit is contained in:
ISSOtm
2026-09-18 21:19:37 +02:00
committed by Eldred Habert
parent c49828f7c9
commit ce5c8ce8ac
+12 -7
View File
@@ -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 &section) {
// 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);