[PATCH] [MC] report error instead of assertion for non-zero initializer in .bss section

Weiming Zhao weimingz at codeaurora.org
Wed Jun 18 15:03:46 PDT 2014


User may initialize a variable with non-zero value and put it into .bss section by mistake.
   E.g. : int a __attribute__((section(".bss"))) = 2; // mistakenly initialized to 2
    
This patch converts an assertion to error report for better user experience.

http://reviews.llvm.org/D4199

Files:
  lib/MC/MCAssembler.cpp
  test/MC/ELF/ARM/bss-non-zero-value.s

Index: lib/MC/MCAssembler.cpp
===================================================================
--- lib/MC/MCAssembler.cpp
+++ lib/MC/MCAssembler.cpp
@@ -27,6 +27,7 @@
 #include "llvm/Support/LEB128.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/MC/MCSectionELF.h"
 #include <tuple>
 using namespace llvm;
 
@@ -782,8 +783,14 @@
         assert(DF.fixup_begin() == DF.fixup_end() &&
                "Cannot have fixups in virtual section!");
         for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
-          assert(DF.getContents()[i] == 0 &&
-                 "Invalid data value for virtual section!");
+          if (DF.getContents()[i]) {
+            StringRef SecName = "virtual section";
+            if (SD->getSection().getVariant() == MCSection::SV_ELF)
+              SecName = "section '" + static_cast<const MCSectionELF&>
+                (SD->getSection()).getSectionName().str()  + "'";
+            report_fatal_error("non-zero initializer found in " + SecName);
+            return;
+          }
         break;
       }
       case MCFragment::FT_Align:
Index: test/MC/ELF/ARM/bss-non-zero-value.s
===================================================================
--- /dev/null
+++ test/MC/ELF/ARM/bss-non-zero-value.s
@@ -0,0 +1,9 @@
+// RUN: not 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4199.10591.patch
Type: text/x-patch
Size: 1557 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140618/d3e008db/attachment.bin>


More information about the llvm-commits mailing list