[llvm] 75af9da - [MC][ELF] Error for sh_type, sh_flags or sh_entsize change

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 21 15:46:55 PST 2020


Author: Fangrui Song
Date: 2020-02-21T15:44:14-08:00
New Revision: 75af9da755721123e62b45cd0bc0c5e688a9722a

URL: https://github.com/llvm/llvm-project/commit/75af9da755721123e62b45cd0bc0c5e688a9722a
DIFF: https://github.com/llvm/llvm-project/commit/75af9da755721123e62b45cd0bc0c5e688a9722a.diff

LOG: [MC][ELF] Error for sh_type, sh_flags or sh_entsize change

Heads-up message: https://lists.llvm.org/pipermail/llvm-dev/2020-February/139390.html

GNU as started to emit warnings for changed sh_type or sh_flags in 2000.
GNU as>=2.35 will emit errors for most sh_type/sh_flags change, and error for entsize change.

Some cases remain warnings for legacy reasons:

   .section .init_array,"ax", @progbits
   .section .init_array,"ax", @init_array
   # And some obscure sh_flags changes (OS/Processor specific flags)

The rationale of a diagnostic (warning or error) is that sh_type,
sh_flags or sh_entsize changes usually indicate user errors. The values
are taken from the first .section directive. Successive directives are ignored.

We just try to be rigid and emit errors for all sh_type/sh_flags/sh_entsize change.

A possible improvement in the future is to reuse
llvm-readobj/ELFDumper.cpp:getSectionTypeString so that we can name the
type in the diagnostics.

Reviewed By: psmith

Differential Revision: https://reviews.llvm.org/D73999

Added: 
    llvm/test/MC/ELF/section-entsize-changed.s
    llvm/test/MC/ELF/section-flags-changed.s
    llvm/test/MC/ELF/section-type-changed.s

Modified: 
    llvm/lib/MC/MCParser/ELFAsmParser.cpp
    llvm/test/MC/ELF/exclude-debug-dwo.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
index a3d9ef0e3c36..7e1c0734f9d5 100644
--- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp
+++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
@@ -634,20 +634,29 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
       }
   }
 
-  MCSection *ELFSection = getContext().getELFSection(
+  MCSectionELF *Section = getContext().getELFSection(
       SectionName, Type, Flags, Size, GroupName, UniqueID, LinkedToSym);
-  getStreamer().SwitchSection(ELFSection, Subsection);
+  getStreamer().SwitchSection(Section, Subsection);
+  if (Section->getType() != Type)
+    Error(loc, "changed section type for " + SectionName + ", expected: 0x" +
+                   utohexstr(Section->getType()));
+  if (Section->getFlags() != Flags)
+    Error(loc, "changed section flags for " + SectionName + ", expected: 0x" +
+                   utohexstr(Section->getFlags()));
+  if (Section->getEntrySize() != Size)
+    Error(loc, "changed section entsize for " + SectionName +
+                   ", expected: " + Twine(Section->getEntrySize()));
 
   if (getContext().getGenDwarfForAssembly()) {
-    bool InsertResult = getContext().addGenDwarfSection(ELFSection);
+    bool InsertResult = getContext().addGenDwarfSection(Section);
     if (InsertResult) {
       if (getContext().getDwarfVersion() <= 2)
         Warning(loc, "DWARF2 only supports one section per compilation unit");
 
-      if (!ELFSection->getBeginSymbol()) {
+      if (!Section->getBeginSymbol()) {
         MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
         getStreamer().emitLabel(SectionStartSymbol);
-        ELFSection->setBeginSymbol(SectionStartSymbol);
+        Section->setBeginSymbol(SectionStartSymbol);
       }
     }
   }

diff  --git a/llvm/test/MC/ELF/exclude-debug-dwo.s b/llvm/test/MC/ELF/exclude-debug-dwo.s
index 5288b37f470b..be0b1c1b50e5 100644
--- a/llvm/test/MC/ELF/exclude-debug-dwo.s
+++ b/llvm/test/MC/ELF/exclude-debug-dwo.s
@@ -10,23 +10,23 @@
 # CHECK: .debug_loc.dwo         {{.*}} E
 # CHECK: .debug_str_offsets.dwo {{.*}} E
 
-.section .debug_info.dwo
+.section .debug_info.dwo,"e"
 nop
 
-.section .debug_types.dwo
+.section .debug_types.dwo,"e"
 nop
 
-.section .debug_abbrev.dwo
+.section .debug_abbrev.dwo,"e"
 nop
 
-.section .debug_str.dwo
+.section .debug_str.dwo,"MSe", at progbits,1
 nop
 
-.section .debug_line.dwo
+.section .debug_line.dwo,"e"
 nop
 
-.section .debug_loc.dwo
+.section .debug_loc.dwo,"e"
 nop
 
-.section .debug_str_offsets.dwo
+.section .debug_str_offsets.dwo,"e"
 nop

diff  --git a/llvm/test/MC/ELF/section-entsize-changed.s b/llvm/test/MC/ELF/section-entsize-changed.s
new file mode 100644
index 000000000000..f7c997ce4ca0
--- /dev/null
+++ b/llvm/test/MC/ELF/section-entsize-changed.s
@@ -0,0 +1,12 @@
+# RUN: not llvm-mc -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
+
+foo:
+.section .foo,"aM", at progbits,1
+
+# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section entsize for .foo, expected: 1
+.section .foo,"aM", at progbits,4
+
+# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section entsize for .foo, expected: 1
+.pushsection .foo,"aM", at progbits,4
+
+.pushsection .foo,"aM", at progbits,1

diff  --git a/llvm/test/MC/ELF/section-flags-changed.s b/llvm/test/MC/ELF/section-flags-changed.s
new file mode 100644
index 000000000000..65f52cc29a6d
--- /dev/null
+++ b/llvm/test/MC/ELF/section-flags-changed.s
@@ -0,0 +1,12 @@
+# RUN: not llvm-mc -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
+
+foo:
+.section .foo,"ax", at progbits
+
+# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section flags for .foo, expected: 0x6
+.section .foo,"awx", at progbits
+
+# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section flags for .foo, expected: 0x6
+.pushsection .foo,"a", at progbits
+
+.pushsection .foo,"ax", at progbits

diff  --git a/llvm/test/MC/ELF/section-type-changed.s b/llvm/test/MC/ELF/section-type-changed.s
new file mode 100644
index 000000000000..cc871462e2db
--- /dev/null
+++ b/llvm/test/MC/ELF/section-type-changed.s
@@ -0,0 +1,11 @@
+# RUN: not llvm-mc -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
+
+.section .foo,"a", at progbits
+
+# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section type for .foo, expected: 0x1
+.section .foo,"a", at init_array
+
+# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section type for .foo, expected: 0x1
+.pushsection .foo,"a", at nobits
+
+.pushsection .foo,"a", at progbits


        


More information about the llvm-commits mailing list