[lld] [LLD][RISCV] Error on PCREL_LO referencing other Section (PR #107558)
Sam Elliott via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 26 02:04:10 PDT 2024
https://github.com/lenary updated https://github.com/llvm/llvm-project/pull/107558
>From d35afe9753e35f1a600b831e4c320a0caf23a263 Mon Sep 17 00:00:00 2001
From: Sam Elliott <quic_aelliott at quicinc.com>
Date: Fri, 6 Sep 2024 03:10:57 -0700
Subject: [PATCH 1/2] [LLD][RISCV] Error on PCREL_LO referencing other Section
The RISC-V psABI states that "The `R_RISCV_PCREL_LO12_I` or
`R_RISCV_PCREL_LO12_S` relocations contain a label pointing to an
instruction in the same section with an `R_RISCV_PCREL_HI20` relocation
entry that points to the target symbol."
In this case, GNU ld errors, but LLD does not -- I think because LLD is
doing the right thing, certainly in the testcase provided.
Nonetheless, I think an error is good here to bring LLD in line with
what GNU ld is doing in showing that the object they are using is not
following the psABI as written.
Fixes #107304
---
lld/ELF/Arch/RISCV.cpp | 9 +++++
.../ELF/riscv-pcrel-hilo-error-sections.s | 35 +++++++++++++++++++
2 files changed, 44 insertions(+)
create mode 100644 lld/test/ELF/riscv-pcrel-hilo-error-sections.s
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 2435864ce5a7f0..2351e24f1eea8c 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -671,6 +671,15 @@ void RISCV::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
errorOrWarn(sec.getLocation(rel.offset) +
": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128");
return;
+ case R_RISCV_PC_INDIRECT:
+ if (Defined *def = dyn_cast<Defined>(rel.sym);
+ def->section && def->section != &sec) {
+ errorOrWarn(sec.getLocation(rel.offset) +
+ ": R_RISCV_PCREL_LO12 relocation points to a symbol '" +
+ rel.sym->getName() + "' in a different section '" +
+ def->section->name + "'");
+ }
+ break;
default:
break;
}
diff --git a/lld/test/ELF/riscv-pcrel-hilo-error-sections.s b/lld/test/ELF/riscv-pcrel-hilo-error-sections.s
new file mode 100644
index 00000000000000..e06bfa1495b833
--- /dev/null
+++ b/lld/test/ELF/riscv-pcrel-hilo-error-sections.s
@@ -0,0 +1,35 @@
+# REQUIRES: riscv
+
+# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.o
+# RUN: not ld.lld %t.o 2>&1 | FileCheck %s
+
+# CHECK: error: {{.*}}:(.text.sec_one+0x0): R_RISCV_PCREL_LO12 relocation points to a symbol '.Lpcrel_hi0' in a different section '.text.sec_two'
+# CHECK: error: {{.*}}:(.text.sec_one+0x4): R_RISCV_PCREL_LO12 relocation points to a symbol '.Lpcrel_hi1' in a different section '.text.sec_two'
+# CHECK-NOT: R_RISCV_PCREL_LO12 relocation points to a symbol '.Lpcrel_hi2'
+
+## This test is checking that we warn the user when the relocations in their
+## object don't follow the RISC-V psABI. In particular, the psABI requires
+## that PCREL_LO12 relocations are in the same section as the pcrel_hi
+## instruction they point to.
+
+ .section .text.sec_one,"ax"
+ .globl sec_one
+sec_one:
+ addi a0, a0, %pcrel_lo(.Lpcrel_hi0)
+ sw a0, %pcrel_lo(.Lpcrel_hi1)(a1)
+
+ .section .text.sec_two,"ax"
+sec_two:
+.Lpcrel_hi0:
+ auipc a0, %pcrel_hi(a)
+.Lpcrel_hi1:
+ auipc a1, %pcrel_hi(a)
+
+.Lpcrel_hi2:
+ auipc a2, %pcrel_hi(a)
+ addi a2, a2, %pcrel_lo(.Lpcrel_hi2)
+
+ .data
+ .global a
+a:
+ .word 50
>From faab323b91954a5fd20393f0abbbc44ea2ea5eab Mon Sep 17 00:00:00 2001
From: Sam Elliott <quic_aelliott at quicinc.com>
Date: Thu, 26 Sep 2024 10:04:01 +0100
Subject: [PATCH 2/2] fixup! Remove unused labels
---
lld/test/ELF/riscv-pcrel-hilo-error-sections.s | 3 ---
1 file changed, 3 deletions(-)
diff --git a/lld/test/ELF/riscv-pcrel-hilo-error-sections.s b/lld/test/ELF/riscv-pcrel-hilo-error-sections.s
index e06bfa1495b833..ebdefd357d722d 100644
--- a/lld/test/ELF/riscv-pcrel-hilo-error-sections.s
+++ b/lld/test/ELF/riscv-pcrel-hilo-error-sections.s
@@ -13,13 +13,10 @@
## instruction they point to.
.section .text.sec_one,"ax"
- .globl sec_one
-sec_one:
addi a0, a0, %pcrel_lo(.Lpcrel_hi0)
sw a0, %pcrel_lo(.Lpcrel_hi1)(a1)
.section .text.sec_two,"ax"
-sec_two:
.Lpcrel_hi0:
auipc a0, %pcrel_hi(a)
.Lpcrel_hi1:
More information about the llvm-commits
mailing list