Skip to content

Commit 0e27d08

Browse files
luismarqueststellar
authored andcommitted
[RISCV] Fix crash for section alignment with .option norvc
The existing code wasn't getting the subtarget info from the fragment, so the current status of RVC would be ignored. This would cause a crash for the new test case when the target then reported it couldn't write the requested number of code alignment bytes. Differential Revision: https://reviews.llvm.org/D122236 (cherry picked from commit d09d297)
1 parent a368017 commit 0e27d08

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,11 @@ void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
583583
bool RISCVAsmBackend::shouldInsertExtraNopBytesForCodeAlign(
584584
const MCAlignFragment &AF, unsigned &Size) {
585585
// Calculate Nops Size only when linker relaxation enabled.
586-
if (!STI.getFeatureBits()[RISCV::FeatureRelax])
586+
const MCSubtargetInfo *STI = AF.getSubtargetInfo();
587+
if (!STI->getFeatureBits()[RISCV::FeatureRelax])
587588
return false;
588589

589-
bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
590+
bool HasStdExtC = STI->getFeatureBits()[RISCV::FeatureStdExtC];
590591
unsigned MinNopLen = HasStdExtC ? 2 : 4;
591592

592593
if (AF.getAlignment() <= MinNopLen) {
@@ -606,7 +607,8 @@ bool RISCVAsmBackend::shouldInsertFixupForCodeAlign(MCAssembler &Asm,
606607
const MCAsmLayout &Layout,
607608
MCAlignFragment &AF) {
608609
// Insert the fixup only when linker relaxation enabled.
609-
if (!STI.getFeatureBits()[RISCV::FeatureRelax])
610+
const MCSubtargetInfo *STI = AF.getSubtargetInfo();
611+
if (!STI->getFeatureBits()[RISCV::FeatureRelax])
610612
return false;
611613

612614
// Calculate total Nops we need to insert. If there are none to insert
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=-relax < %s \
2+
# RUN: | llvm-readobj -r - | FileCheck %s
3+
4+
# Check that .option relax overrides -mno-relax and enables R_RISCV_ALIGN
5+
# relocations.
6+
# CHECK: R_RISCV_ALIGN
7+
.option relax
8+
.align 4

llvm/test/MC/RISCV/align.s

+8
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,11 @@ data1:
112112
# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN
113113
data2:
114114
.word 9
115+
# Check that the initial alignment is properly handled when using .option to
116+
# disable the C extension. This used to crash.
117+
# C-EXT-RELAX-INST: <.text2>:
118+
# C-EXT-RELAX-INST-NEXT: add a0, a0, a1
119+
.section .text2, "x"
120+
.option norvc
121+
.balign 4
122+
add a0, a0, a1

0 commit comments

Comments
 (0)