Index: llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp =================================================================== --- llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp +++ llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp @@ -354,20 +354,21 @@ bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const { - bool HasStdExtC = STI->getFeatureBits()[RISCV::FeatureStdExtC]; - unsigned MinNopLen = HasStdExtC ? 2 : 4; - - if ((Count % MinNopLen) != 0) - return false; - // The canonical nop on RISC-V is addi x0, x0, 0. for (; Count >= 4; Count -= 4) OS.write("\x13\0\0\0", 4); // The canonical nop on RVC is c.nop. - if (Count && HasStdExtC) - OS.write("\x01\0", 2); - + if (STI->getFeatureBits()[RISCV::FeatureStdExtC]) + for (; Count >= 2; Count -= 2) + OS.write("\x01\0", 2); + + // If the count is not 4-byte aligned (or 2-byte under the C extensions), we + // must be writing data into the text section (otherwise we have unaligned + // instructions, and thus have far bigger problems), so just write zeros + // instead. This also may occur for non-standard data sections which are not + // declared and thus get detected as text rather than data. + OS.write_zeros(Count); return true; } Index: llvm/test/MC/RISCV/nop-slide.s =================================================================== --- /dev/null +++ llvm/test/MC/RISCV/nop-slide.s @@ -0,0 +1,12 @@ +# RUN: llvm-mc -triple riscv64 -filetype obj -o - %s | llvm-objdump -d - | FileCheck %s + + .balign 4 + .byte 0 + + .balign 4 + auipc a0, 0 + +# CHECK: 0000000000000000 <.text>: +# CHECK-NEXT: 0: 00 00 +# CHECK-NEXT: 2: 00 00 +# CHECK-NEXT: 4: 17 05 00 00 auipc a0, 0