Skip to content

Commit 4e77a57

Browse files
committed
[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
1 parent a424b79 commit 4e77a57

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lld/ELF/Arch/RISCV.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,15 @@ void RISCV::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
671671
errorOrWarn(sec.getLocation(rel.offset) +
672672
": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128");
673673
return;
674+
case R_RISCV_PC_INDIRECT:
675+
if (Defined *def = dyn_cast<Defined>(rel.sym);
676+
def->section && def->section != &sec) {
677+
errorOrWarn(sec.getLocation(rel.offset) +
678+
": R_RISCV_PCREL_LO12 relocation points to a symbol '" +
679+
rel.sym->getName() + "' in a different section '" +
680+
def->section->name + "'");
681+
}
682+
break;
674683
default:
675684
break;
676685
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# REQUIRES: riscv
2+
3+
# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.o
4+
# RUN: not ld.lld %t.o 2>&1 | FileCheck %s
5+
6+
# CHECK: error: {{.*}}:(.text.sec_one+0x0): R_RISCV_PCREL_LO12 relocation points to a symbol '.Lpcrel_hi0' in a different section '.text.sec_two'
7+
# CHECK: error: {{.*}}:(.text.sec_one+0x4): R_RISCV_PCREL_LO12 relocation points to a symbol '.Lpcrel_hi1' in a different section '.text.sec_two'
8+
# CHECK-NOT: R_RISCV_PCREL_LO12 relocation points to a symbol '.Lpcrel_hi2'
9+
10+
## This test is checking that we warn the user when the relocations in their
11+
## object don't follow the RISC-V psABI. In particular, the psABI requires
12+
## that PCREL_LO12 relocations are in the same section as the pcrel_hi
13+
## instruction they point to.
14+
15+
.section .text.sec_one
16+
.globl sec_one
17+
sec_one:
18+
addi a0, a0, %pcrel_lo(.Lpcrel_hi0)
19+
sw a0, %pcrel_lo(.Lpcrel_hi1)(a1)
20+
21+
.section .text.sec_two
22+
sec_two:
23+
.Lpcrel_hi0:
24+
auipc a0, %pcrel_hi(a)
25+
.Lpcrel_hi1:
26+
auipc a1, %pcrel_hi(a)
27+
28+
.Lpcrel_hi2:
29+
auipc a2, %pcrel_hi(a)
30+
addi a2, a2, %pcrel_lo(.Lpcrel_hi2)
31+
32+
.data
33+
.global a
34+
a:
35+
.word 50

0 commit comments

Comments
 (0)