Skip to content

Commit 72accbf

Browse files
authored
[lld][LoongArch] Support the R_LARCH_{ADD,SUB}6 relocation type (llvm#72190)
The R_LARCH_{ADD,SUB}6 relocation type are usually used by DwarfCFA to calculate a tiny offset. They appear after binutils 2.41, with GAS enabling relaxation by default.
1 parent f2c5355 commit 72accbf

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lld/ELF/Arch/LoongArch.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,12 @@ RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
444444
case R_LARCH_TLS_LE64_LO20:
445445
case R_LARCH_TLS_LE64_HI12:
446446
return R_TPREL;
447+
case R_LARCH_ADD6:
447448
case R_LARCH_ADD8:
448449
case R_LARCH_ADD16:
449450
case R_LARCH_ADD32:
450451
case R_LARCH_ADD64:
452+
case R_LARCH_SUB6:
451453
case R_LARCH_SUB8:
452454
case R_LARCH_SUB16:
453455
case R_LARCH_SUB32:
@@ -650,6 +652,9 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
650652
write32le(loc, setK12(read32le(loc), extractBits(val, 63, 52)));
651653
return;
652654

655+
case R_LARCH_ADD6:
656+
*loc = (*loc & 0xc0) | ((*loc + val) & 0x3f);
657+
return;
653658
case R_LARCH_ADD8:
654659
*loc += val;
655660
return;
@@ -662,6 +667,9 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
662667
case R_LARCH_ADD64:
663668
write64le(loc, read64le(loc) + val);
664669
return;
670+
case R_LARCH_SUB6:
671+
*loc = (*loc & 0xc0) | ((*loc - val) & 0x3f);
672+
return;
665673
case R_LARCH_SUB8:
666674
*loc -= val;
667675
return;

lld/test/ELF/loongarch-add-sub.s

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# RUN: llvm-readelf -x .rodata %t.la64 | FileCheck --check-prefix=CHECK %s
77
# CHECK: section '.rodata':
88
# CHECK-NEXT: 0x9876543210 10325476 98badcfe 804602be 79ffffff
9-
# CHECK-NEXT: 0x9876543220 804602be 804680
9+
# CHECK-NEXT: 0x9876543220 804602be 80468097
1010

1111
.text
1212
.global _start
@@ -34,3 +34,7 @@ quux:
3434
.byte 0
3535
.reloc quux, R_LARCH_ADD8, 1b
3636
.reloc quux, R_LARCH_SUB8, 2b
37+
qux:
38+
.byte 0b10000000
39+
.reloc qux, R_LARCH_ADD6, qux
40+
.reloc qux, R_LARCH_SUB6, 2b

0 commit comments

Comments
 (0)