[lld] [lld][RISC-V] Implement Qualcomm Relocations (PR #178584)
Sam Elliott via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 28 21:59:13 PST 2026
https://github.com/lenary updated https://github.com/llvm/llvm-project/pull/178584
>From b5612fd417cf3cf798777b465ac446989dfc3f70 Mon Sep 17 00:00:00 2001
From: Sam Elliott <aelliott at qti.qualcomm.com>
Date: Wed, 28 Jan 2026 19:48:31 -0800
Subject: [PATCH 1/2] [lld][RISC-V] Implement Qualcomm Relocations
This is an initial implementation of the QUALCOMM vendor relocations as
described in https://github.com/quic/riscv-elf-psabi-quic-extensions
v0.3.
Tests are still needed for:
- PLT Behaviour
- Error messages
---
lld/ELF/Arch/RISCV.cpp | 71 ++++++++++++++
lld/test/ELF/riscv-vendor-qc.s | 118 ++++++++++++++++++++++++
lld/test/ELF/riscv-vendor-relocations.s | 3 -
3 files changed, 189 insertions(+), 3 deletions(-)
create mode 100644 lld/test/ELF/riscv-vendor-qc.s
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 6e069c7c9d456..905b3d66577a0 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -290,6 +290,8 @@ RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s,
case R_RISCV_HI20:
case R_RISCV_LO12_I:
case R_RISCV_LO12_S:
+ case INTERNAL_RISCV_QC_ABS20_U:
+ case INTERNAL_RISCV_QC_E_32:
return R_ABS;
case R_RISCV_ADD8:
case R_RISCV_ADD16:
@@ -311,10 +313,12 @@ RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s,
case R_RISCV_RVC_BRANCH:
case R_RISCV_RVC_JUMP:
case R_RISCV_32_PCREL:
+ case INTERNAL_RISCV_QC_E_BRANCH:
return R_PC;
case R_RISCV_CALL:
case R_RISCV_CALL_PLT:
case R_RISCV_PLT32:
+ case INTERNAL_RISCV_QC_E_CALL_PLT:
return R_PLT_PC;
case R_RISCV_GOT_HI20:
case R_RISCV_GOT32_PCREL:
@@ -563,6 +567,73 @@ void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
else
write32le(loc + 4, val);
break;
+
+ case INTERNAL_RISCV_QC_ABS20_U: {
+ checkInt(ctx, loc, val, 20, rel);
+ uint32_t insn = read32le(loc) & 0xFFF;
+ uint32_t imm20 = extractBits(val, 19, 19) << 31;
+ uint32_t imm18_15 = extractBits(val, 18, 15) << 12;
+ uint32_t imm14_0 = extractBits(val, 14, 0) << 16;
+ insn |= imm20 | imm18_15 | imm14_0;
+ write32le(loc, insn);
+ break;
+ }
+
+ case INTERNAL_RISCV_QC_E_32: {
+ checkInt(ctx, loc, val, 32, rel);
+ uint64_t insn = static_cast<uint64_t>(read32le(loc))
+ | (static_cast<uint64_t>(read16le(loc + 4)) << 32);
+ insn &= 0xFFFFull;
+
+ uint64_t imm32_0 = static_cast<uint64_t>(extractBits(val, 32, 0)) << 16;
+ insn |= imm32_0;
+
+ write32le(loc, insn);
+ write16le(loc+4, (insn >> 32) & 0xFFFF);
+
+ break;
+ }
+
+ case INTERNAL_RISCV_QC_E_BRANCH: {
+ checkInt(ctx, loc, val, 13, rel);
+ checkAlignment(ctx, loc, val, 2, rel);
+
+ uint64_t insn = static_cast<uint64_t>(read32le(loc))
+ | (static_cast<uint64_t>(read16le(loc + 4)) << 32);
+ insn &= 0xFFFF01FFF07Full;
+
+ uint64_t imm12 = static_cast<uint64_t>(extractBits(val, 12, 12)) << 31;
+ uint64_t imm10_5 = static_cast<uint64_t>(extractBits(val, 10, 5)) << 25;
+ uint64_t imm4_1 = static_cast<uint64_t>(extractBits(val, 4, 1)) << 8;
+ uint64_t imm11 = static_cast<uint64_t>(extractBits(val, 11, 11)) << 7;
+ insn |= imm12 | imm10_5 | imm4_1 | imm11;
+
+ write32le(loc, insn);
+ write16le(loc+4, (insn >> 32) & 0xFFFF);
+ return;
+ }
+
+ case INTERNAL_RISCV_QC_E_CALL_PLT: {
+ checkInt(ctx, loc, val, 32, rel);
+ checkAlignment(ctx, loc, val, 2, rel);
+
+ uint64_t insn = static_cast<uint64_t>(read32le(loc))
+ | (static_cast<uint64_t>(read16le(loc + 4)) << 32);
+ insn &= 0xFFFF01FFF07Full;
+
+ uint64_t imm31_16 = static_cast<uint64_t>(extractBits(val, 31, 16)) << 32;
+ uint64_t imm12 = static_cast<uint64_t>(extractBits(val, 12, 12)) << 31;
+ uint64_t imm10_5 = static_cast<uint64_t>(extractBits(val, 10, 5)) << 25;
+ uint64_t imm15_13 = static_cast<uint64_t>(extractBits(val, 15, 13)) << 17;
+ uint64_t imm4_1 = static_cast<uint64_t>(extractBits(val, 4, 1)) << 8;
+ uint64_t imm11 = static_cast<uint64_t>(extractBits(val, 11, 11)) << 7;
+ insn |= imm31_16 | imm12 | imm10_5 | imm15_13 | imm4_1 | imm11;
+
+ write32le(loc, insn);
+ write16le(loc+4, (insn >> 32) & 0xFFFF);
+ return;
+ }
+
default:
llvm_unreachable("unknown relocation");
}
diff --git a/lld/test/ELF/riscv-vendor-qc.s b/lld/test/ELF/riscv-vendor-qc.s
new file mode 100644
index 0000000000000..b3c082d6489bf
--- /dev/null
+++ b/lld/test/ELF/riscv-vendor-qc.s
@@ -0,0 +1,118 @@
+# REQUIRES: riscv
+# RUN: rm -rf %t && split-file %s %t && cd %t
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=+xqcili,+xqcibi,+xqcilb a.s -o rv32.o -riscv-add-build-attributes
+
+# RUN: ld.lld rv32.o lds -pie -o rv32
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn %t/rv32 | FileCheck %s
+
+#--- a.s
+
+ .text
+ .p2align 1
+
+ .option exact
+
+ .global _start
+_start:
+
+ # CHECK: qc.li t0, 0x0
+ qc.li x5, %qc.abs20(test_zero)
+
+ # CHECK: qc.li t0, -0x1
+ qc.li x5, %qc.abs20(test_minus_one)
+
+ # CHECK: qc.li t0, 0x7ffff
+ qc.li x5, %qc.abs20(test_abs20_high)
+
+ # CHECK: qc.li t0, -0x80000
+ qc.li x5, %qc.abs20(test_abs20_low)
+
+ # CHECK: qc.li t0, 0x12345
+ qc.li x5, %qc.abs20(test_abs20_distinct)
+
+
+ # CHECK: qc.e.li t1, 0x0
+ qc.e.li x6, test_zero
+
+ # CHECK: qc.e.li t1, -0x1
+ qc.e.li x6, test_minus_one
+
+ # CHECK: qc.e.li t1, 0x7fffffff
+ qc.e.li x6, test_e32_high
+
+ # CHECK: qc.e.li t1, -0x80000000
+ qc.e.li x6, test_e32_low
+
+ # CHECK: qc.e.li t1, 0x6789abcd
+ qc.e.li x6, test_e32_distinct
+
+ .global branch_case1
+branch_case1:
+ # CHECK: 11a0: qc.e.bgei t2, -0x8000, 0x11a0 <test_branch_here>
+ qc.e.bgei x7, -0x8000, test_branch_here
+
+ .global branch_case2
+branch_case2:
+ # CHECK: 11a6: qc.e.bltui t2, 0xffff, 0x12a6 <test_branch_high>
+ qc.e.bltui x7, 0xffff, test_branch_high
+
+ .global branch_case3
+branch_case3:
+ # CHECK: 11ac: qc.e.beqi t2, 0x7fff, 0x10ac <test_zero+0x10ac>
+ qc.e.beqi x7, 0x7fff, test_branch_low
+
+ .global branch_case4
+branch_case4:
+ # CHECK: 11b2: qc.e.bgeui t2, 0xffff, 0x126c <test_branch_distinct>
+ qc.e.bgeui x7, 0xffff, test_branch_distinct
+
+
+ .global call_case1
+call_case1:
+ # CHECK: 11b8: qc.e.j 0x11b8 <test_call_here>
+ qc.e.j test_call_here
+
+ .global call_case2
+call_case2:
+ # CHECK: 11be: qc.e.j 0x800011bc <test_e32_low+0x11bc>
+ qc.e.j test_call_high
+
+ .global call_case3
+call_case3:
+ # CHECK: 11c4: qc.e.j 0x800011c4 <test_minus_one+0xfffffffe800011c5>
+ qc.e.j test_call_low
+
+ .global call_case4
+call_case4:
+ # CHECK: 11ca: qc.e.j 0x23457954 <test_abs20_high+0x233d7955>
+ qc.e.j test_call_distinct
+
+ # CHECK: 11d0: qc.e.j 0x0 <test_zero>
+ qc.e.j test_zero
+
+ # CHECK: 11d6: qc.e.j 0xfffffffe <test_minus_one+0xfffffffeffffffff>
+ qc.e.j test_call_abs_high
+
+#--- lds
+
+test_zero = 0;
+test_minus_one = -1;
+
+test_abs20_high = 0x7ffff;
+test_abs20_low = -0x80000;
+test_abs20_distinct = 0x12345;
+
+test_e32_high = 0x7fffffff;
+test_e32_low = -0x80000000;
+test_e32_distinct = 0x6789abcd;
+
+test_branch_here = branch_case1;
+test_branch_high = branch_case2 + 0x100;
+test_branch_low = branch_case3 - 0x100;
+test_branch_distinct = branch_case4 + 0xba;
+
+test_call_here = call_case1;
+test_call_high = (call_case2 + 0x7ffffffe) & 0xffffffff;
+test_call_low = (call_case3 - 0x80000000) | (0xffffffff << 32);
+test_call_distinct = (call_case4 + 0x12345678a) & 0xffffffff;
+test_call_abs_high = 0xfffffffffffffffe;
diff --git a/lld/test/ELF/riscv-vendor-relocations.s b/lld/test/ELF/riscv-vendor-relocations.s
index f121adec95cd0..33f3043a58594 100644
--- a/lld/test/ELF/riscv-vendor-relocations.s
+++ b/lld/test/ELF/riscv-vendor-relocations.s
@@ -16,9 +16,6 @@ TARGET:
.reloc 1f, R_RISCV_CUSTOM255, TARGET
# CHECK: error: {{.*}}:(.text+0x4): malformed consecutive R_RISCV_VENDOR relocations
# CHECK: error: {{.*}}:(.text+0x4): unknown vendor-specific relocation (255) in namespace 'INVALID_VENDOR' against symbol 'TARGET'
-.reloc 1f, R_RISCV_VENDOR, QUALCOMM+0
-.reloc 1f, R_RISCV_CUSTOM192, TARGET
-# CHECK: error: {{.*}}:(.text+0x4): unsupported vendor-specific relocation R_RISCV_QC_ABS20_U against symbol TARGET
.reloc 1f, R_RISCV_VENDOR, ANDES+0
.reloc 1f, R_RISCV_CUSTOM241, TARGET
# CHECK: error: {{.*}}:(.text+0x4): unsupported vendor-specific relocation R_RISCV_NDS_BRANCH_10 against symbol TARGET
>From 7e75650145c10212d36fa4d60c2311de89ea34e9 Mon Sep 17 00:00:00 2001
From: Sam Elliott <aelliott at qti.qualcomm.com>
Date: Wed, 28 Jan 2026 21:57:09 -0800
Subject: [PATCH 2/2] clang-format
---
lld/ELF/Arch/RISCV.cpp | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 905b3d66577a0..f49be32e83895 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -581,15 +581,15 @@ void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
case INTERNAL_RISCV_QC_E_32: {
checkInt(ctx, loc, val, 32, rel);
- uint64_t insn = static_cast<uint64_t>(read32le(loc))
- | (static_cast<uint64_t>(read16le(loc + 4)) << 32);
+ uint64_t insn = static_cast<uint64_t>(read32le(loc)) |
+ (static_cast<uint64_t>(read16le(loc + 4)) << 32);
insn &= 0xFFFFull;
uint64_t imm32_0 = static_cast<uint64_t>(extractBits(val, 32, 0)) << 16;
insn |= imm32_0;
write32le(loc, insn);
- write16le(loc+4, (insn >> 32) & 0xFFFF);
+ write16le(loc + 4, (insn >> 32) & 0xFFFF);
break;
}
@@ -598,8 +598,8 @@ void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
checkInt(ctx, loc, val, 13, rel);
checkAlignment(ctx, loc, val, 2, rel);
- uint64_t insn = static_cast<uint64_t>(read32le(loc))
- | (static_cast<uint64_t>(read16le(loc + 4)) << 32);
+ uint64_t insn = static_cast<uint64_t>(read32le(loc)) |
+ (static_cast<uint64_t>(read16le(loc + 4)) << 32);
insn &= 0xFFFF01FFF07Full;
uint64_t imm12 = static_cast<uint64_t>(extractBits(val, 12, 12)) << 31;
@@ -609,7 +609,7 @@ void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
insn |= imm12 | imm10_5 | imm4_1 | imm11;
write32le(loc, insn);
- write16le(loc+4, (insn >> 32) & 0xFFFF);
+ write16le(loc + 4, (insn >> 32) & 0xFFFF);
return;
}
@@ -617,8 +617,8 @@ void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
checkInt(ctx, loc, val, 32, rel);
checkAlignment(ctx, loc, val, 2, rel);
- uint64_t insn = static_cast<uint64_t>(read32le(loc))
- | (static_cast<uint64_t>(read16le(loc + 4)) << 32);
+ uint64_t insn = static_cast<uint64_t>(read32le(loc)) |
+ (static_cast<uint64_t>(read16le(loc + 4)) << 32);
insn &= 0xFFFF01FFF07Full;
uint64_t imm31_16 = static_cast<uint64_t>(extractBits(val, 31, 16)) << 32;
@@ -630,7 +630,7 @@ void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
insn |= imm31_16 | imm12 | imm10_5 | imm15_13 | imm4_1 | imm11;
write32le(loc, insn);
- write16le(loc+4, (insn >> 32) & 0xFFFF);
+ write16le(loc + 4, (insn >> 32) & 0xFFFF);
return;
}
More information about the llvm-commits
mailing list