Skip to content

Commit ec5e6cb

Browse files
committed
Handle -Wtruncation for link-time jr values
Fixes #2028
1 parent 50678d0 commit ec5e6cb

7 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/link/patch.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,15 @@ static void applyFilePatches(Section &section, Section &dataSection) {
585585
rpnErrorAt(patch, "PC has no value outside of a section");
586586
dataSection.data[offset] = 0;
587587
} else {
588+
// A `jr` is *encoded* in ROM as a 1-byte (8-bit) offset, so here `typeSize == 8`,
589+
// but the object's *value* size is a 16-bit absolute address, so we pass 16 here.
590+
checkPatchSize(patch, value, 16);
588591
// Offset is relative to the byte *after* the operand
589592
// PC as operand to `jr` is lower than reference PC by 2
590593
uint16_t address = patch.pcSection->org + patch.pcOffset + 2;
591-
// The truncation of `value - address` is intentional, since
592-
// a low ROM0 address may `jr` to a high HRAM one.
593-
int16_t jumpOffset = value - address;
594+
// The 16-bit truncation of `value - address` is intentional, since
595+
// a low ROM0 address may `jr` backwards to a high HRAM one.
596+
int16_t jumpOffset = static_cast<int16_t>(value - address);
594597

595598
if (jumpOffset < -128 || jumpOffset > 127) {
596599
firstErrorAt(

test/link/jr-truncation/a.asm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SECTION "jr", ROM0
2+
jr CONSTANT

test/link/jr-truncation/b.asm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EXPORT DEF CONSTANT EQU $1234_0078

test/link/jr-truncation/out.err

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
warning: Value $12340078 is not 16-bit [-Wtruncation]
2+
at jr-truncation/a.asm(2)
16 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
; File generated by rgblink
2+
12340078 CONSTANT

test/link/test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ tryCmpRom "$test"/ref.out.bin
201201
tryDiff "$test"/ref.out.sym "$outtemp2"
202202
evaluateTest
203203

204+
test="jr-truncation"
205+
startTest
206+
"$RGBASM" -o "$otemp" "$test"/a.asm
207+
"$RGBASM" -o "$outtemp" "$test"/b.asm
208+
continueTest
209+
rgblinkQuiet -o "$gbtemp" -n "$outtemp2" "$otemp" "$outtemp" 2>"$outtemp3"
210+
tryDiff "$test"/out.err "$outtemp3"
211+
tryCmpRom "$test"/ref.out.bin
212+
tryDiff "$test"/ref.out.sym "$outtemp2"
213+
evaluateTest
214+
204215
test="high-low"
205216
startTest
206217
"$RGBASM" -o "$otemp" "$test"/a.asm

0 commit comments

Comments
 (0)