diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp --- a/llvm/lib/MC/MCAssembler.cpp +++ b/llvm/lib/MC/MCAssembler.cpp @@ -687,10 +687,12 @@ for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i) if (DF.getContents()[i]) { if (auto *ELFSec = dyn_cast(Sec)) - report_fatal_error("non-zero initializer found in section '" + - ELFSec->getSectionName() + "'"); + getContext().reportError( + SMLoc(), "non-zero initializer found in section '" + + ELFSec->getSectionName() + "'"); else - report_fatal_error("non-zero initializer found in virtual section"); + getContext().reportError( + SMLoc(), "non-zero initializer found in virtual section"); } break; } diff --git a/llvm/lib/MC/MCELFStreamer.cpp b/llvm/lib/MC/MCELFStreamer.cpp --- a/llvm/lib/MC/MCELFStreamer.cpp +++ b/llvm/lib/MC/MCELFStreamer.cpp @@ -511,6 +511,12 @@ void MCELFStreamer::EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) { + MCSection &Sec = *getCurrentSectionOnly(); + if (Sec.isVirtualSection()) { + getContext().reportError(Inst.getLoc(), + "SHT_NOBITS section cannot have instructions"); + return; + } MCAssembler &Assembler = getAssembler(); SmallVector Fixups; SmallString<256> Code; @@ -538,7 +544,6 @@ MCDataFragment *DF; if (Assembler.isBundlingEnabled()) { - MCSection &Sec = *getCurrentSectionOnly(); if (Assembler.getRelaxAll() && isBundleLocked()) { // If the -mc-relax-all flag is used and we are bundle-locked, we re-use // the current bundle group. diff --git a/llvm/test/MC/ELF/ARM/bss-non-zero-value.s b/llvm/test/MC/ELF/ARM/bss-non-zero-value.s deleted file mode 100644 --- a/llvm/test/MC/ELF/ARM/bss-non-zero-value.s +++ /dev/null @@ -1,9 +0,0 @@ -// RUN: not --crash llvm-mc -filetype=obj -triple arm-linux-gnu %s -o %t 2>%t.out -// RUN: FileCheck --input-file=%t.out %s -// CHECK: non-zero initializer found in section '.bss' - .bss - .globl a - .align 2 -a: - .long 1 - .size a, 4 diff --git a/llvm/test/MC/ELF/nobits-non-zero-value.s b/llvm/test/MC/ELF/nobits-non-zero-value.s new file mode 100644 --- /dev/null +++ b/llvm/test/MC/ELF/nobits-non-zero-value.s @@ -0,0 +1,15 @@ +# RUN: not llvm-mc -filetype=obj -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s + +## -filetype=asm does not check the error. +# RUN: llvm-mc -triple=x86_64 %s + +.section .tbss,"aw",@nobits +# CHECK: {{.*}}.s:[[#@LINE+1]]:3: error: SHT_NOBITS section cannot have instructions + nop + +.bss +# CHECK: {{.*}}.s:[[#@LINE+1]]:3: error: SHT_NOBITS section cannot have instructions + addb %al,(%rax) + +# CHECK: :0: error: non-zero initializer found in section '.bss' + .long 1 diff --git a/llvm/test/MC/X86/reloc-bss.s b/llvm/test/MC/X86/reloc-bss.s deleted file mode 100644 --- a/llvm/test/MC/X86/reloc-bss.s +++ /dev/null @@ -1,9 +0,0 @@ -# RUN: not --crash llvm-mc -filetype=obj -triple=x86_64-linux-gnu %s 2>&1 | FileCheck %s -# CHECK: LLVM ERROR: cannot have fixups in virtual section! - -.section .init_array,"awT",@nobits - -.hidden patatino -.globl patatino -patatino: - movl __init_array_start, %eax