Skip to content

Commit 1c1ab11

Browse files
MaskRaytru
authored andcommitted
[RISCV][MC] Adjust conditions to emit R_RISCV_ADD*/R_RISCV_SUB* pairs
D132262 tried to simplify `IsMetadataOrEHFrameSection` originally introduced in D127549 but caused a regression as `.quad` directives in ``` .section .note,"a",@note; note: .quad extern-note # extern is undefined .section .rodata,"a",@progbits; rodata: .quad extern-rodata # extern is undefined .section .nonalloc,"",@progbits; nw: .quad extern-nw ``` are incorrectly rejected: these differences may be link-time constants and are allowed in GNU assembler and LLVM MC's non-RISC-V ports. Relax the conditions to allow these cases. For A-B, A may be defined later, but this requiresFixups call has to eagerly make a decision. For now, emit ADD/SUB unless A is `.L*`. This euristic handles many temporary label differences for .debug_* and .apple_types sections. Ideally we should delay the decision of PC-relative vs ADD/SUB until A is defined. Reviewed By: compnerd Differential Revision: https://reviews.llvm.org/D145474 (cherry picked from commit 2f5fe16)
1 parent 34194d8 commit 1c1ab11

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ bool RISCVELFStreamer::requiresFixups(MCContext &C, const MCExpr *Value,
239239
if (B.isInSection() && B.getSection().getKind().isText())
240240
return true;
241241

242+
// If A is undefined and B is defined, we should emit ADD/SUB for A-B.
243+
// Unfortunately, A may be defined later, but this requiresFixups call has to
244+
// eagerly make a decision. For now, emit ADD/SUB unless A is .L*. This
245+
// heuristic handles many temporary label differences for .debug_* and
246+
// .apple_types sections.
247+
//
248+
// TODO Implement delayed relocation decision.
249+
if (!A.isInSection() && !A.isTemporary() && B.isInSection())
250+
return true;
251+
242252
// Support cross-section symbolic differences ...
243253
return A.isInSection() && B.isInSection() &&
244254
A.getSection().getName() != B.getSection().getName();

llvm/test/MC/RISCV/riscv64-64b-pcrel.s

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
# RUN: | FileCheck %s --check-prefix=ERROR
55

66
# CHECK: Relocations [
7+
# CHECK-NEXT: Section ({{.*}}) .rela.note {
8+
# CHECK-NEXT: 0x0 R_RISCV_ADD64 extern 0x0
9+
# CHECK-NEXT: 0x0 R_RISCV_SUB64 note 0x0
10+
# CHECK-NEXT: }
11+
# CHECK-NEXT: Section ({{.*}}) .rela.rodata {
12+
# CHECK-NEXT: 0x0 R_RISCV_ADD64 extern 0x0
13+
# CHECK-NEXT: 0x0 R_RISCV_SUB64 rodata 0x0
14+
# CHECK-NEXT: }
715
# CHECK-NEXT: Section ({{.*}}) .rela.alloc_w {
816
# CHECK-NEXT: 0x0 R_RISCV_ADD64 extern 0x0
917
# CHECK-NEXT: 0x0 R_RISCV_SUB64 w 0x0
@@ -24,20 +32,24 @@
2432
# CHECK-NEXT: 0x0 R_RISCV_ADD64 x 0x0
2533
# CHECK-NEXT: 0x0 R_RISCV_SUB64 y 0x0
2634
# CHECK-NEXT: }
35+
# CHECK-NEXT: Section ({{.*}}) .rela.nonalloc_w {
36+
# CHECK-NEXT: 0x0 R_RISCV_ADD64 extern 0x0
37+
# CHECK-NEXT: 0x0 R_RISCV_SUB64 nw 0x0
38+
# CHECK-NEXT: }
39+
# CHECK-NEXT: Section ({{.*}}) .rela.nonalloc_x {
40+
# CHECK-NEXT: 0x0 R_RISCV_ADD64 ny 0x0
41+
# CHECK-NEXT: 0x0 R_RISCV_SUB64 nx 0x0
42+
# CHECK-NEXT: }
2743
# CHECK-NEXT: Section ({{.*}}) .rela.nonalloc_y {
2844
# CHECK-NEXT: 0x0 R_RISCV_ADD64 nx 0x0
2945
# CHECK-NEXT: 0x0 R_RISCV_SUB64 ny 0x0
3046
# CHECK-NEXT: }
3147
# CHECK-NEXT: ]
3248

33-
.ifdef ERR
3449
.section .note,"a",@note; note:
35-
# ERROR: :[[#@LINE+1]]:7: error: unsupported relocation type
3650
.quad extern-note
3751
.section .rodata,"a",@progbits; rodata:
38-
# ERROR: :[[#@LINE+1]]:7: error: unsupported relocation type
3952
.quad extern-rodata
40-
.endif
4153

4254
.section .alloc_w,"aw",@progbits; w:
4355
.quad extern-w # extern is undefined
@@ -53,17 +65,13 @@ w1:
5365
.quad x-y
5466

5567
.section .nonalloc_w; nw:
56-
.ifdef ERR
57-
# ERROR: :[[#@LINE+1]]:7: error: unsupported relocation type
5868
.quad extern-nw
69+
.ifdef ERR
5970
# ERROR: :[[#@LINE+1]]:7: error: symbol 'extern' can not be undefined in a subtraction expression
6071
.quad nw-extern
6172
.endif
6273
.section .nonalloc_x; nx:
63-
.ifdef ERR
64-
# ERROR: :[[#@LINE+1]]:7: error: unsupported relocation type
6574
.quad ny-nx
66-
.endif
6775
.section .nonalloc_y; ny:
6876
.quad nx-ny
6977

0 commit comments

Comments
 (0)