Skip to content

Commit

Permalink
[RISCV] Fix crash for section alignment with .option norvc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
luismarques committed Apr 7, 2022
1 parent c226a5c commit d09d297
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
Expand Up @@ -583,10 +583,11 @@ void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
bool RISCVAsmBackend::shouldInsertExtraNopBytesForCodeAlign(
const MCAlignFragment &AF, unsigned &Size) {
// Calculate Nops Size only when linker relaxation enabled.
if (!STI.getFeatureBits()[RISCV::FeatureRelax])
const MCSubtargetInfo *STI = AF.getSubtargetInfo();
if (!STI->getFeatureBits()[RISCV::FeatureRelax])
return false;

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

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

// Calculate total Nops we need to insert. If there are none to insert
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/MC/RISCV/align-option-relax.s
@@ -0,0 +1,8 @@
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=-relax < %s \
# RUN: | llvm-readobj -r - | FileCheck %s

# Check that .option relax overrides -mno-relax and enables R_RISCV_ALIGN
# relocations.
# CHECK: R_RISCV_ALIGN
.option relax
.align 4
8 changes: 8 additions & 0 deletions llvm/test/MC/RISCV/align.s
Expand Up @@ -112,3 +112,11 @@ data1:
# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN
data2:
.word 9
# Check that the initial alignment is properly handled when using .option to
# disable the C extension. This used to crash.
# C-EXT-RELAX-INST: <.text2>:
# C-EXT-RELAX-INST-NEXT: add a0, a0, a1
.section .text2, "x"
.option norvc
.balign 4
add a0, a0, a1

0 comments on commit d09d297

Please sign in to comment.