[lld] r327006 - [ELF] - Fix crash relative to SHF_LINK_ORDER sections.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 8 07:06:59 PST 2018


Author: grimar
Date: Thu Mar  8 07:06:58 2018
New Revision: 327006

URL: http://llvm.org/viewvc/llvm-project?rev=327006&view=rev
Log:
[ELF] - Fix crash relative to SHF_LINK_ORDER sections.

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 predicate does not expect to see
null when calls getLinkOrderDep. 

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

Differential revision: https://reviews.llvm.org/D44193

Added:
    lld/trunk/test/ELF/section-metadata-err2.s
    lld/trunk/test/ELF/section-metadata-err3.s
Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/test/ELF/section-metadata-err.s

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=327006&r1=327005&r2=327006&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Thu Mar  8 07:06:58 2018
@@ -431,8 +431,15 @@ void ObjFile<ELFT>::initializeSections(
       if (Sec.sh_link >= this->Sections.size())
         fatal(toString(this) +
               ": invalid sh_link index: " + Twine(Sec.sh_link));
-      this->Sections[Sec.sh_link]->DependentSections.push_back(
-          cast<InputSection>(this->Sections[I]));
+
+      InputSectionBase *LinkSec = this->Sections[Sec.sh_link];
+      InputSection *IS = cast<InputSection>(this->Sections[I]);
+      LinkSec->DependentSections.push_back(IS);
+      if (!isa<InputSection>(LinkSec))
+        error("a section " + IS->Name +
+              " with SHF_LINK_ORDER should not refer a non-regular "
+              "section: " +
+              toString(LinkSec));
     }
   }
 }

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=327006&r1=327005&r2=327006&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Thu Mar  8 07:06:58 2018
@@ -206,15 +206,9 @@ void InputSectionBase::maybeDecompress()
 }
 
 InputSection *InputSectionBase::getLinkOrderDep() const {
-  if ((Flags & SHF_LINK_ORDER) && Link != 0) {
-    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 "
-          "section: " +
-          toString(L));
-  }
-  return nullptr;
+  assert(Link);
+  assert(Flags & SHF_LINK_ORDER);
+  return cast<InputSection>(File->getSections()[Link]);
 }
 
 // Returns a source location string. Used to construct an error message.

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=327006&r1=327005&r2=327006&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Thu Mar  8 07:06:58 2018
@@ -98,7 +98,8 @@ void OutputSection::addSection(InputSect
     Flags = IS->Flags;
   } else {
     // Otherwise, check if new type or flags are compatible with existing ones.
-    if ((Flags & (SHF_ALLOC | SHF_TLS)) != (IS->Flags & (SHF_ALLOC | SHF_TLS)))
+    unsigned Mask = SHF_ALLOC | SHF_TLS | SHF_LINK_ORDER;
+    if ((Flags & Mask) != (IS->Flags & Mask))
       error("incompatible section flags for " + Name + "\n>>> " + toString(IS) +
             ": 0x" + utohexstr(IS->Flags) + "\n>>> output section " + Name +
             ": 0x" + utohexstr(Flags));

Modified: lld/trunk/test/ELF/section-metadata-err.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/section-metadata-err.s?rev=327006&r1=327005&r2=327006&view=diff
==============================================================================
--- lld/trunk/test/ELF/section-metadata-err.s (original)
+++ lld/trunk/test/ELF/section-metadata-err.s Thu Mar  8 07:06:58 2018
@@ -3,7 +3,7 @@
 # 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 .bar with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err.s.tmp.o:(.foo)
 
 .global _start
 _start:
@@ -12,4 +12,4 @@ _start:
 .section .foo,"aM", at progbits,8
 .quad 0
 
-.section bar,"ao", at progbits,.foo
+.section .bar,"ao", at progbits,.foo

Added: lld/trunk/test/ELF/section-metadata-err2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/section-metadata-err2.s?rev=327006&view=auto
==============================================================================
--- lld/trunk/test/ELF/section-metadata-err2.s (added)
+++ lld/trunk/test/ELF/section-metadata-err2.s Thu Mar  8 07:06:58 2018
@@ -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 .bar with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err2.s.tmp.o:(.foo)
+# CHECK: error: a section .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

Added: lld/trunk/test/ELF/section-metadata-err3.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/section-metadata-err3.s?rev=327006&view=auto
==============================================================================
--- lld/trunk/test/ELF/section-metadata-err3.s (added)
+++ lld/trunk/test/ELF/section-metadata-err3.s Thu Mar  8 07:06:58 2018
@@ -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:      error: incompatible section flags for .bar
+# CHECK-NEXT: >>> {{.*}}section-metadata-err3.s.tmp.o:(.bar): 0x2
+# CHECK-NEXT: >>> output section .bar: 0x82
+
+.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




More information about the llvm-commits mailing list