Consistently do & alignMask, not % alignSize

Also add more unrelated tests for coverage
This commit is contained in:
Rangi42
2025-09-23 11:53:05 -04:00
parent 96a75500d3
commit ca4b890273
10 changed files with 98 additions and 38 deletions

View File

@@ -128,6 +128,7 @@ void layout_AlignTo(uint32_t alignment, uint32_t alignOfs) {
layout_SetAddr(floatingAlignOffset);
} else {
uint32_t alignSize = 1u << alignment;
uint32_t alignMask = alignSize - 1;
if (alignOfs >= alignSize) {
scriptError(
@@ -139,8 +140,8 @@ void layout_AlignTo(uint32_t alignment, uint32_t alignOfs) {
return;
}
floatingAlignMask = alignSize - 1;
floatingAlignOffset = alignOfs % alignSize;
floatingAlignMask = alignMask;
floatingAlignOffset = alignOfs & alignMask;
}
return;
}
@@ -158,6 +159,7 @@ void layout_AlignTo(uint32_t alignment, uint32_t alignOfs) {
if (alignment < 16) {
uint32_t alignSize = 1u << alignment;
uint32_t alignMask = alignSize - 1;
if (alignOfs >= alignSize) {
scriptError(
@@ -170,7 +172,7 @@ void layout_AlignTo(uint32_t alignment, uint32_t alignOfs) {
}
assume(pc >= typeInfo.startAddr);
length %= alignSize;
length &= alignMask;
}
if (uint16_t offset = pc - typeInfo.startAddr; length > typeInfo.size - offset) {

View File

@@ -54,7 +54,7 @@ static void checkAgainstFixedAddress(Section const &target, Section const &other
}
}
static bool checkAgainstFixedAlign(Section const &target, Section const &other, int32_t ofs) {
static bool checkAgainstFixedAlign(Section const &target, Section const &other, uint32_t ofs) {
if (target.isAddressFixed) {
if ((target.org - ofs) & other.alignMask) {
fatalTwoAt(
@@ -107,10 +107,7 @@ static void checkFragmentCompat(Section &target, Section &other) {
target.isAddressFixed = true;
target.org = org;
} else if (other.isAlignFixed) {
int32_t ofs = (other.alignOfs - target.size) % (other.alignMask + 1);
if (ofs < 0) {
ofs += other.alignMask + 1;
}
uint32_t ofs = (other.alignOfs - target.size) & other.alignMask;
if (checkAgainstFixedAlign(target, other, ofs)) {
target.isAlignFixed = true;
target.alignMask = other.alignMask;