From fc0c8f5215c6e78fbd2c84d3ac99f9c3e100e7ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Marques?= Date: Tue, 19 Sep 2023 15:24:26 +0100 Subject: [PATCH] Warn on align directive with non-zero fill value in virtual sections This patch warns when an align directive with a non-zero fill value is used in a virtual section. The fill value is also set to zero, preventing an assertion in MCAssembler::writeSectionData for the case of MCFragment::FT_Align from tripping. --- llvm/lib/MC/MCParser/AsmParser.cpp | 14 +++++++++++++- llvm/test/MC/ELF/nobits-non-zero-value.s | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index f70620c15a7eb..15ba96b84fa47 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -3392,6 +3392,7 @@ bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { bool HasFillExpr = false; int64_t FillExpr = 0; int64_t MaxBytesToFill = 0; + SMLoc FillExprLoc; auto parseAlign = [&]() -> bool { if (parseAbsoluteExpression(Alignment)) @@ -3402,7 +3403,7 @@ bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { // .align 3,,4 if (getTok().isNot(AsmToken::Comma)) { HasFillExpr = true; - if (parseAbsoluteExpression(FillExpr)) + if (parseTokenLoc(FillExprLoc) || parseAbsoluteExpression(FillExpr)) return true; } if (parseOptionalToken(AsmToken::Comma)) @@ -3451,6 +3452,17 @@ bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { } } + if (HasFillExpr) { + MCSection *Sec = getStreamer().getCurrentSectionOnly(); + if (Sec && Sec->isVirtualSection()) { + ReturnVal |= + Warning(FillExprLoc, "ignoring non-zero fill value in " + + Sec->getVirtualSectionKind() + " section '" + + Sec->getName() + "'"); + FillExpr = 0; + } + } + // Diagnose non-sensical max bytes to align. if (MaxBytesLoc.isValid()) { if (MaxBytesToFill < 1) { diff --git a/llvm/test/MC/ELF/nobits-non-zero-value.s b/llvm/test/MC/ELF/nobits-non-zero-value.s index 16d386dc62ad5..8f37a957b6b56 100644 --- a/llvm/test/MC/ELF/nobits-non-zero-value.s +++ b/llvm/test/MC/ELF/nobits-non-zero-value.s @@ -12,5 +12,8 @@ # CHECK: {{.*}}.s:[[#@LINE+1]]:3: error: SHT_NOBITS section '.bss' cannot have instructions addb %al,(%rax) +# CHECK: {{.*}}.s:[[#@LINE+1]]:11: warning: ignoring non-zero fill value in SHT_NOBITS section '.bss' +.align 4, 42 + # CHECK: :0: error: SHT_NOBITS section '.bss' cannot have non-zero initializers .long 1