shithub: rgbds

Download patch

ref: 296e5489c973cbef5ce92b3c32122f0605126f9b
parent: 9ab9d0f39c3bb9c263a1b117e020edb95c45c5cb
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Sun May 2 10:57:54 EDT 2021

rgblink adjusts patches' PC offsets when merging FRAGMENTs

Fixes #869

diff: cannot open b/test/link/section-fragment/jr-offset-load//null: file does not exist: 'b/test/link/section-fragment/jr-offset-load//null' diff: cannot open b/test/link/section-fragment/jr-offset//null: file does not exist: 'b/test/link/section-fragment/jr-offset//null' diff: cannot open b/test/link/section-fragment//null: file does not exist: 'b/test/link/section-fragment//null'
--- a/src/link/section.c
+++ b/src/link/section.c
@@ -165,6 +165,9 @@
 			if (!target->data)
 				errx(1, "Failed to concatenate \"%s\"'s fragments", target->name);
 			memcpy(target->data + target->size - other->size, other->data, other->size);
+			/* Adjust patches' PC offsets */
+			for (uint32_t patchID = 0; patchID < other->nbPatches; patchID++)
+				other->patches[patchID].pcOffset += other->offset;
 		}
 		break;
 
--- /dev/null
+++ b/test/link/section-fragment/jr-offset-load/a.asm
@@ -1,0 +1,4 @@
+SECTION FRAGMENT "output", ROM0
+LOAD FRAGMENT "loaded", SRAM
+    ds 128
+ENDL
--- /dev/null
+++ b/test/link/section-fragment/jr-offset-load/b.asm
@@ -1,0 +1,6 @@
+SECTION FRAGMENT "output", ROM0
+LOAD FRAGMENT "loaded", SRAM
+label:
+	jr nz, label2
+label2:
+ENDL
binary files /dev/null b/test/link/section-fragment/jr-offset-load/ref.out.bin differ
--- /dev/null
+++ b/test/link/section-fragment/jr-offset/a.asm
@@ -1,0 +1,2 @@
+SECTION FRAGMENT "output", ROM0
+    ds 128
--- /dev/null
+++ b/test/link/section-fragment/jr-offset/b.asm
@@ -1,0 +1,4 @@
+SECTION FRAGMENT "output", ROM0
+label:
+	jr nz, label2
+label2:
binary files /dev/null b/test/link/section-fragment/jr-offset/ref.out.bin differ
--- a/test/link/test.sh
+++ b/test/link/test.sh
@@ -151,6 +151,24 @@
 tryCmp overlay/out.gb $gbtemp
 rc=$(($? || $rc))
 
+i="section-fragment/jr-offset.asm"
+startTest
+$RGBASM -o $otemp section-fragment/jr-offset/a.asm
+$RGBASM -o $gbtemp2 section-fragment/jr-offset/b.asm
+rgblinkQuiet -o $gbtemp $otemp $gbtemp2
+dd if=$gbtemp count=1 bs=$(printf %s $(wc -c < section-fragment/jr-offset/ref.out.bin)) > $otemp 2>/dev/null
+tryCmp section-fragment/jr-offset/ref.out.bin $otemp
+rc=$(($? || $rc))
+
+i="section-fragment/jr-offset-load.asm"
+startTest
+$RGBASM -o $otemp section-fragment/jr-offset-load/a.asm
+$RGBASM -o $gbtemp2 section-fragment/jr-offset-load/b.asm
+rgblinkQuiet -o $gbtemp $otemp $gbtemp2
+dd if=$gbtemp count=1 bs=$(printf %s $(wc -c < section-fragment/jr-offset-load/ref.out.bin)) > $otemp 2>/dev/null
+tryCmp section-fragment/jr-offset-load/ref.out.bin $otemp
+rc=$(($? || $rc))
+
 i="section-union/good.asm"
 startTest
 $RGBASM -o $otemp section-union/good/a.asm