[llvm] r335558 - Add a warning if someone attempts to add extra section flags to sections

Eric Christopher via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 25 16:53:54 PDT 2018


Author: echristo
Date: Mon Jun 25 16:53:54 2018
New Revision: 335558

URL: http://llvm.org/viewvc/llvm-project?rev=335558&view=rev
Log:
Add a warning if someone attempts to add extra section flags to sections
with well defined semantics like .rodata.

Added:
    llvm/trunk/test/MC/ELF/extra-section-flags.s
Modified:
    llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp

Modified: llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp?rev=335558&r1=335557&r2=335558&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp Mon Jun 25 16:53:54 2018
@@ -481,6 +481,31 @@ static bool hasPrefix(StringRef SectionN
   return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
 }
 
+// Return a set of section flags based on the section name that can then
+// be augmented later, otherwise return 0 if we don't have any reasonable
+// defaults.
+static unsigned defaultSectionFlags(StringRef SectionName) {
+
+  if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
+    return ELF::SHF_ALLOC;
+
+  if (SectionName == ".fini" || SectionName == ".init" ||
+      hasPrefix(SectionName, ".text."))
+    return ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
+
+  if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
+      hasPrefix(SectionName, ".bss.") ||
+      hasPrefix(SectionName, ".init_array.") ||
+      hasPrefix(SectionName, ".fini_array.") ||
+      hasPrefix(SectionName, ".preinit_array."))
+    return ELF::SHF_ALLOC | ELF::SHF_WRITE;
+
+  if (hasPrefix(SectionName, ".tdata.") || hasPrefix(SectionName, ".tbss."))
+    return ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
+
+  return 0;
+}
+
 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
   StringRef SectionName;
 
@@ -490,27 +515,13 @@ bool ELFAsmParser::ParseSectionArguments
   StringRef TypeName;
   int64_t Size = 0;
   StringRef GroupName;
-  unsigned Flags = 0;
   const MCExpr *Subsection = nullptr;
   bool UseLastGroup = false;
   MCSymbolELF *Associated = nullptr;
   int64_t UniqueID = ~0;
 
-  // Set the defaults first.
-  if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
-    Flags |= ELF::SHF_ALLOC;
-  else if (SectionName == ".fini" || SectionName == ".init" ||
-           hasPrefix(SectionName, ".text."))
-    Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
-  else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
-           hasPrefix(SectionName, ".bss.") ||
-           hasPrefix(SectionName, ".init_array.") ||
-           hasPrefix(SectionName, ".fini_array.") ||
-           hasPrefix(SectionName, ".preinit_array."))
-    Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
-  else if (hasPrefix(SectionName, ".tdata.") ||
-           hasPrefix(SectionName, ".tbss."))
-    Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
+  // Set the default section flags first in case no others are given.
+  unsigned Flags = defaultSectionFlags(SectionName);
 
   if (getLexer().is(AsmToken::Comma)) {
     Lex();
@@ -538,6 +549,12 @@ bool ELFAsmParser::ParseSectionArguments
 
     if (extraFlags == -1U)
       return TokError("unknown flag");
+
+    // If we found additional section flags on a known section then give a
+    // warning.
+    if (Flags && Flags != extraFlags)
+      Warning(loc, "setting incorrect section attributes for " + SectionName);
+
     Flags |= extraFlags;
 
     bool Mergeable = Flags & ELF::SHF_MERGE;

Added: llvm/trunk/test/MC/ELF/extra-section-flags.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/extra-section-flags.s?rev=335558&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/extra-section-flags.s (added)
+++ llvm/trunk/test/MC/ELF/extra-section-flags.s Mon Jun 25 16:53:54 2018
@@ -0,0 +1,10 @@
+# RUN: llvm-mc -triple x86_64-unknown-unknown -filetype=obj %s -o /dev/null 2>&1 | FileCheck %s
+	
+.section .rodata, "ax"
+# CHECK: warning: setting incorrect section attributes for .rodata
+nop
+
+.section .rodata, "a"
+# CHECK-NOT: warning:
+nop
+




More information about the llvm-commits mailing list