From 296e5489c973cbef5ce92b3c32122f0605126f9b Mon Sep 17 00:00:00 2001 From: Rangi Date: Sun, 2 May 2021 14:57:54 -0400 Subject: [PATCH] rgblink adjusts patches' PC offsets when merging FRAGMENTs Fixes #869 --- src/link/section.c | 3 +++ .../section-fragment/jr-offset-load/a.asm | 4 ++++ .../section-fragment/jr-offset-load/b.asm | 6 ++++++ .../jr-offset-load/ref.out.bin | Bin 0 -> 130 bytes test/link/section-fragment/jr-offset/a.asm | 2 ++ test/link/section-fragment/jr-offset/b.asm | 4 ++++ .../section-fragment/jr-offset/ref.out.bin | Bin 0 -> 130 bytes test/link/test.sh | 18 ++++++++++++++++++ 8 files changed, 37 insertions(+) create mode 100644 test/link/section-fragment/jr-offset-load/a.asm create mode 100644 test/link/section-fragment/jr-offset-load/b.asm create mode 100644 test/link/section-fragment/jr-offset-load/ref.out.bin create mode 100644 test/link/section-fragment/jr-offset/a.asm create mode 100644 test/link/section-fragment/jr-offset/b.asm create mode 100644 test/link/section-fragment/jr-offset/ref.out.bin diff --git a/src/link/section.c b/src/link/section.c index 4f3a6823..6c87094d 100644 --- a/src/link/section.c +++ b/src/link/section.c @@ -165,6 +165,9 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se 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; diff --git a/test/link/section-fragment/jr-offset-load/a.asm b/test/link/section-fragment/jr-offset-load/a.asm new file mode 100644 index 00000000..825a41fe --- /dev/null +++ b/test/link/section-fragment/jr-offset-load/a.asm @@ -0,0 +1,4 @@ +SECTION FRAGMENT "output", ROM0 +LOAD FRAGMENT "loaded", SRAM + ds 128 +ENDL diff --git a/test/link/section-fragment/jr-offset-load/b.asm b/test/link/section-fragment/jr-offset-load/b.asm new file mode 100644 index 00000000..2b1af8f9 --- /dev/null +++ b/test/link/section-fragment/jr-offset-load/b.asm @@ -0,0 +1,6 @@ +SECTION FRAGMENT "output", ROM0 +LOAD FRAGMENT "loaded", SRAM +label: + jr nz, label2 +label2: +ENDL diff --git a/test/link/section-fragment/jr-offset-load/ref.out.bin b/test/link/section-fragment/jr-offset-load/ref.out.bin new file mode 100644 index 0000000000000000000000000000000000000000..55edf813038ab50f12602a11f62e8e672f78dcad GIT binary patch literal 130 NcmZQz7+9dd0002O03iSX literal 0 HcmV?d00001 diff --git a/test/link/section-fragment/jr-offset/a.asm b/test/link/section-fragment/jr-offset/a.asm new file mode 100644 index 00000000..f3db7ecd --- /dev/null +++ b/test/link/section-fragment/jr-offset/a.asm @@ -0,0 +1,2 @@ +SECTION FRAGMENT "output", ROM0 + ds 128 diff --git a/test/link/section-fragment/jr-offset/b.asm b/test/link/section-fragment/jr-offset/b.asm new file mode 100644 index 00000000..a28c42bd --- /dev/null +++ b/test/link/section-fragment/jr-offset/b.asm @@ -0,0 +1,4 @@ +SECTION FRAGMENT "output", ROM0 +label: + jr nz, label2 +label2: diff --git a/test/link/section-fragment/jr-offset/ref.out.bin b/test/link/section-fragment/jr-offset/ref.out.bin new file mode 100644 index 0000000000000000000000000000000000000000..55edf813038ab50f12602a11f62e8e672f78dcad GIT binary patch literal 130 NcmZQz7+9dd0002O03iSX literal 0 HcmV?d00001 diff --git a/test/link/test.sh b/test/link/test.sh index e47d3c51..f90e1059 100755 --- a/test/link/test.sh +++ b/test/link/test.sh @@ -151,6 +151,24 @@ rc=$(($? || $rc)) 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