shithub: rgbds

Download patch

ref: 3564b3f9eae6caa1ae0504d6a76cfbf46e578efe
parent: 8a1e920e23f78e7e96307a45d80b463d34a30cd0
author: ISSOtm <[email protected]>
date: Thu Jan 2 09:03:54 EST 2020

Have `jr` offset wrap with 16 bits
Overflow with `int16_t` is defined to two's complement so it's OK
This could trigger when jumping from the top of ROM0 to HRAM

--- a/src/link/patch.c
+++ b/src/link/patch.c
@@ -299,11 +299,11 @@
 		struct Patch *patch = &section->patches[patchID];
 		int32_t value = computeRPNExpr(patch, section);
 
+		/* `jr` is quite unlike the others... */
 		if (patch->type == PATCHTYPE_JR) {
-			/* `jr` is quite unlike the others... */
-			uint16_t address = section->org + patch->offset;
 			/* Target is relative to the byte *after* the operand */
-			int32_t offset = value - (address + 1);
+			uint16_t address = section->org + patch->offset + 1;
+			int16_t offset = value - address;
 
 			if (offset < -128 || offset > 127)
 				errx(1, "%s(%d): jr target out of reach (%d)",