Skip to content

Commit 6c207ee

Browse files
authored
[RISCV] Force relocations if initial MCSubtargetInfo contains FeatureRelax (#77436)
Regarding ``` .option norelax j label .option relax // relaxable instructions // For assembly input, RISCVAsmParser::ParseInstruction will set ForceRelocs (https://reviews.llvm.org/D46423). // For direct object emission, ForceRelocs is not set after #73721 label: ``` The J instruction needs a relocation to ensure the target is correct after linker relaxation. This is related a limitation in the assembler: RISCVAsmBackend::shouldForceRelocation decides upfront whether a relocation is needed, instead of checking more information (whether there are relaxable fragments in between). Despite the limitation, `j label` produces a relocation in direct object emission mode, but was broken by #73721 due to the shouldForceRelocation limitation. Add a workaround to RISCVTargetELFStreamer to emulate the previous behavior. Link: ClangBuiltLinux/linux#1965
1 parent a43e0f9 commit 6c207ee

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ RISCVTargetELFStreamer::RISCVTargetELFStreamer(MCStreamer &S,
3737
auto &MAB = static_cast<RISCVAsmBackend &>(MCA.getBackend());
3838
setTargetABI(RISCVABI::computeTargetABI(STI.getTargetTriple(), Features,
3939
MAB.getTargetOptions().getABIName()));
40+
// `j label` in `.option norelax; j label; .option relax; ...; label:` needs a
41+
// relocation to ensure the jump target is correct after linking. This is due
42+
// to a limitation that shouldForceRelocation has to make the decision upfront
43+
// without knowing a possibly future .option relax. When RISCVAsmParser is used,
44+
// its ParseInstruction may call setForceRelocs as well.
45+
if (STI.hasFeature(RISCV::FeatureRelax))
46+
static_cast<RISCVAsmBackend &>(MAB).setForceRelocs();
4047
}
4148

4249
RISCVELFStreamer &RISCVTargetELFStreamer::getStreamer() {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
;; With +relax, J below needs a relocation to ensure the target is correct
2+
;; after linker relaxation. See https://github.com/ClangBuiltLinux/linux/issues/1965
3+
4+
; RUN: llc -mtriple=riscv64 -mattr=-relax -filetype=obj < %s \
5+
; RUN: | llvm-objdump -d -r - | FileCheck %s
6+
; RUN: llc -mtriple=riscv64 -mattr=+relax -filetype=obj < %s \
7+
; RUN: | llvm-objdump -d -r - | FileCheck %s --check-prefixes=CHECK,RELAX
8+
9+
; CHECK: j {{.*}}
10+
; RELAX-NEXT: R_RISCV_JAL {{.*}}
11+
; CHECK-NEXT: auipc ra, 0x0
12+
; CHECK-NEXT: R_RISCV_CALL_PLT f
13+
; RELAX-NEXT: R_RISCV_RELAX *ABS*
14+
; CHECK-NEXT: jalr ra
15+
16+
define dso_local noundef signext i32 @main() local_unnamed_addr #0 {
17+
entry:
18+
callbr void asm sideeffect ".option push\0A.option norvc\0A.option norelax\0Aj $0\0A.option pop\0A", "!i"() #2
19+
to label %asm.fallthrough [label %label]
20+
21+
asm.fallthrough: ; preds = %entry
22+
tail call void @f()
23+
br label %label
24+
25+
label: ; preds = %asm.fallthrough, %entry
26+
ret i32 0
27+
}
28+
29+
declare void @f()
30+
31+
attributes #0 = { nounwind "target-features"="-c,+relax" }

0 commit comments

Comments
 (0)