[PATCH] D44193: [ELF] - Fix crash relative to SHF_LINK_ORDER sections.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 7 02:31:02 PST 2018


grimar created this revision.
grimar added reviewers: ruiu, espindola.
Herald added subscribers: arichardson, emaste.

Our code assumes all input sections in an output SHF_LINK_ORDER section has SHF_LINK_ORDER flag. 
We do not check that and that can cause a crash.
That happens because we call  
`std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);`, 
where `compareByFilePosition` does not expect to see null when calls `getLinkOrderDep`. 
Given that we never faced it before, I suggest to error out that case.

The same might happen when sections refer to non-regular sections. 
Test cases demonstrate the issues.


https://reviews.llvm.org/D44193

Files:
  ELF/InputSection.cpp
  ELF/Writer.cpp
  test/ELF/section-metadata-err.s
  test/ELF/section-metadata-err2.s
  test/ELF/section-metadata-err3.s


Index: test/ELF/section-metadata-err3.s
===================================================================
--- test/ELF/section-metadata-err3.s
+++ test/ELF/section-metadata-err3.s
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+
+# CHECK: error: unable to place section {{.*}}section-metadata-err3.s.tmp.o:(.bar) into section '.bar': SHF_LINK_ORDER flags mismatch
+
+.section .foo,"a", at progbits
+.quad 0
+
+.section .bar,"ao", at progbits,.foo,unique,1
+.quad 0
+
+.section .bar,"a", at progbits,unique,2
+.quad 1
Index: test/ELF/section-metadata-err2.s
===================================================================
--- test/ELF/section-metadata-err2.s
+++ test/ELF/section-metadata-err2.s
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+
+## Check we do not crash and report proper errors.
+# CHECK: error: a section {{.*}}section-metadata-err2.s.tmp.o:(.bar) with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err2.s.tmp.o:(.foo)
+# CHECK: error: a section {{.*}}section-metadata-err2.s.tmp.o:(.bar) with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err2.s.tmp.o:(.foo)
+
+.section .foo,"aM", at progbits,8
+.quad 0
+
+.section .bar,"ao", at progbits,.foo,unique,1
+.quad 0
+
+.section .bar,"ao", at progbits,.foo,unique,2
+.quad 1
Index: test/ELF/section-metadata-err.s
===================================================================
--- test/ELF/section-metadata-err.s
+++ test/ELF/section-metadata-err.s
@@ -3,13 +3,13 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
 
-# CHECK: error: a section with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err.s.tmp.o:(.foo)
+# CHECK: error: a section {{.*}}section-metadata-err.s.tmp.o:(.bar) with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err.s.tmp.o:(.foo)
 
 .global _start
 _start:
 .quad .foo
 
 .section .foo,"aM", at progbits,8
 .quad 0
 
-.section bar,"ao", at progbits,.foo
+.section .bar,"ao", at progbits,.foo
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1307,6 +1307,15 @@
         }
       }
     }
+
+    // Check that all sections have link order dependency set. Ignore sentinels.
+    for (InputSection *IS : Sections)
+      if (!isa<SyntheticSection>(IS) && !IS->getLinkOrderDep())
+        error("unable to place section " + toString(IS) + " into section '" +
+              Sec->Name + "': SHF_LINK_ORDER flags mismatch");
+    if (errorCount())
+      return;
+
     std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
 
     if (!Config->Relocatable && Config->EMachine == EM_ARM &&
Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -210,7 +210,8 @@
     InputSectionBase *L = File->getSections()[Link];
     if (auto *IS = dyn_cast<InputSection>(L))
       return IS;
-    error("a section with SHF_LINK_ORDER should not refer a non-regular "
+    error("a section " + toString(this) +
+          " with SHF_LINK_ORDER should not refer a non-regular "
           "section: " +
           toString(L));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44193.137338.patch
Type: text/x-patch
Size: 3485 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180307/974008de/attachment.bin>


More information about the llvm-commits mailing list