shithub: rgbds

Download patch

ref: 5be1c0da622d4cf7a294b7a8011f08394d045931
parent: b598911e96e0ec77c7e5f92303efa1df985bfe9f
author: ISSOtm <eldredhabert0@gmail.com>
date: Sat Jan 9 18:29:08 EST 2021

Fix intra-section ALIGN not computing offset correctly

--- a/src/asm/section.c
+++ b/src/asm/section.c
@@ -383,24 +383,24 @@
 {
 	checksection();
 	struct Section *sect = sect_GetSymbolSection();
+	uint16_t alignSize = 1 << alignment; // Size of an aligned "block"
 
 	if (sect->org != -1) {
-		if ((sym_GetPCValue() - offset) % (1 << alignment))
-			error("Section's fixed address fails required alignment (PC = $%04"
-				PRIx32 ")\n", sym_GetPCValue());
+		if ((sym_GetPCValue() - offset) % alignSize)
+			error("Section's fixed address fails required alignment (PC = $%04" PRIx32
+			      ")\n", sym_GetPCValue());
 	} else if (sect->align != 0) {
-		if ((((sect->alignOfs + curOffset) % (1 << sect->align))
-						- offset) % (1 << alignment)) {
+		if ((((sect->alignOfs + curOffset) % (1 << sect->align)) - offset) % alignSize) {
 			error("Section's alignment fails required alignment (offset from section start = $%04"
 				PRIx32 ")\n", curOffset);
 		} else if (alignment > sect->align) {
 			sect->align = alignment;
-			sect->alignOfs =
-					(offset - curOffset) % (1 << alignment);
+			sect->alignOfs = (offset - curOffset) % alignSize;
 		}
 	} else {
 		sect->align = alignment;
-		sect->alignOfs = offset;
+		// We need `(sect->alignOfs + curOffset) % alignSize == offset
+		sect->alignOfs = (offset - curOffset) % alignSize;
 	}
 }