-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[LLD][RISCV] Error on PCREL_LO referencing other Section #107558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-lld-elf @llvm/pr-subscribers-lld Author: Sam Elliott (lenary) ChangesThe RISC-V psABI states that "The In this case, GNU ld errors, but LLD does not -- I think because it is doing the right thing, certainly in the testcase provided. Nonetheless, it would be good for LLD to at least warn the user, so they know the objects they are using are not following the psABI as written. Fixes #107304 Full diff: https://github.com/llvm/llvm-project/pull/107558.diff 2 Files Affected:
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 2435864ce5a7f0..80a7909c42f2e5 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..deffbd91b0ff61
--- /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
+ .globl sec_one
+sec_one:
+ addi a0, a0, %pcrel_lo(.Lpcrel_hi0)
+ sw a0, %pcrel_lo(.Lpcrel_hi1)(a1)
+
+ .section .text.sec_two
+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
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
e6346f6
to
4e77a57
Compare
Some notes on how I fixed this:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I am on vacation and is likely slow to respond.)
4e77a57
to
d35afe9
Compare
ping |
Thanks for the refactor in |
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 llvm#107304
faab323
to
a781311
Compare
I also took the opportunity to improve the other errors/warnings emitted by the same function to point to exactly where the issue is. |
The RISC-V psABI states that "The
R_RISCV_PCREL_LO12_I
orR_RISCV_PCREL_LO12_S
relocations contain a label pointing to an instruction in the same section with anR_RISCV_PCREL_HI20
relocation entry that points to the target symbol."In this case, GNU ld errors, but LLD does not -- I think because it 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