[llvm] Warn on align directive with non-zero fill value in virtual sections (PR #66792)

Luís Marques via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 20 05:54:17 PDT 2023


https://github.com/luismarques updated https://github.com/llvm/llvm-project/pull/66792

>From fc0c8f5215c6e78fbd2c84d3ac99f9c3e100e7ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lu=C3=ADs=20Marques?= <luismarques at lowrisc.org>
Date: Tue, 19 Sep 2023 15:24:26 +0100
Subject: [PATCH 1/2] 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 f70620c15a7ebb7..15ba96b84fa4701 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 16d386dc62ad549..8f37a957b6b56c0 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: <unknown>:0: error: SHT_NOBITS section '.bss' cannot have non-zero initializers
   .long 1

>From 9d53a0e795f2981a7b036fac1f02c4f9d225b49a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lu=C3=ADs=20Marques?= <luismarques at lowrisc.org>
Date: Wed, 20 Sep 2023 13:53:49 +0100
Subject: [PATCH 2/2] fixup! Warn on align directive with non-zero fill value
 in virtual sections

---
 llvm/test/MC/ELF/nobits-non-zero-value.s | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/llvm/test/MC/ELF/nobits-non-zero-value.s b/llvm/test/MC/ELF/nobits-non-zero-value.s
index 8f37a957b6b56c0..4e251f93eb26829 100644
--- a/llvm/test/MC/ELF/nobits-non-zero-value.s
+++ b/llvm/test/MC/ELF/nobits-non-zero-value.s
@@ -1,4 +1,8 @@
-# RUN: not llvm-mc -filetype=obj -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s
+# RUN: not llvm-mc -filetype=obj -triple=x86_64 %s -o /dev/null 2>&1 | \
+# RUN: FileCheck --check-prefixes=CHECK,NON-FATAL %s
+
+# RUN: not llvm-mc --fatal-warnings -filetype=obj -triple=x86_64 %s \
+# RUN:   -o /dev/null 2>&1 | FileCheck %s
 
 ## -filetype=asm does not check the error.
 # RUN: llvm-mc -triple=x86_64 %s
@@ -12,8 +16,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'
+# CHECK: {{.*}}.s:[[#@LINE+1]]:11: {{warning|error}}: ignoring non-zero fill value in SHT_NOBITS section '.bss'
 .align 4, 42
 
-# CHECK: <unknown>:0: error: SHT_NOBITS section '.bss' cannot have non-zero initializers
+# NON-FATAL: <unknown>:0: error: SHT_NOBITS section '.bss' cannot have non-zero initializers
   .long 1



More information about the llvm-commits mailing list