[lld] r247037 - Bug fix: Assign output section indexes *after* sorting them.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 11:08:57 PDT 2015


Author: rafael
Date: Tue Sep  8 13:08:57 2015
New Revision: 247037

URL: http://llvm.org/viewvc/llvm-project?rev=247037&view=rev
Log:
Bug fix: Assign output section indexes *after* sorting them.

Added:
    lld/trunk/test/elf2/output-section.s
Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=247037&r1=247036&r2=247037&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Sep  8 13:08:57 2015
@@ -184,11 +184,6 @@ private:
   StringTableSection<ELFT::Is64Bits> StringTable;
 
   SymbolTableSection<ELFT> SymTable;
-
-  void addOutputSection(OutputSectionBase<ELFT::Is64Bits> *Sec) {
-    OutputSections.push_back(Sec);
-    Sec->setSectionIndex(OutputSections.size());
-  }
 };
 } // anonymous namespace
 
@@ -428,6 +423,13 @@ static bool cmpAlign(const DefinedCommon
   return A->MaxAlignment > B->MaxAlignment;
 }
 
+template <bool Is64Bits>
+static bool compSec(OutputSectionBase<Is64Bits> *A,
+                    OutputSectionBase<Is64Bits> *B) {
+  // Place SHF_ALLOC sections first.
+  return (A->getFlags() & SHF_ALLOC) && !(B->getFlags() & SHF_ALLOC);
+}
+
 // Create output section objects and add them to OutputSections.
 template <class ELFT> void Writer<ELFT>::createSections() {
   SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSection<ELFT> *> Map;
@@ -438,7 +440,7 @@ template <class ELFT> void Writer<ELFT>:
     if (!Sec) {
       Sec = new (CAlloc.Allocate())
           OutputSection<ELFT>(Key.Name, Key.sh_type, Key.sh_flags);
-      addOutputSection(Sec);
+      OutputSections.push_back(Sec);
     }
     return Sec;
   };
@@ -485,13 +487,14 @@ template <class ELFT> void Writer<ELFT>:
   }
 
   BSSSec->setSize(Off);
-}
 
-template <bool Is64Bits>
-static bool compSec(OutputSectionBase<Is64Bits> *A,
-                    OutputSectionBase<Is64Bits> *B) {
-  // Place SHF_ALLOC sections first.
-  return (A->getFlags() & SHF_ALLOC) && !(B->getFlags() & SHF_ALLOC);
+  OutputSections.push_back(&SymTable);
+  OutputSections.push_back(&StringTable);
+
+  std::stable_sort(OutputSections.begin(), OutputSections.end(),
+                   compSec<ELFT::Is64Bits>);
+  for (unsigned I = 0, N = OutputSections.size(); I < N; ++I)
+    OutputSections[I]->setSectionIndex(I + 1);
 }
 
 // Visits all sections to assign incremental, non-overlapping RVAs and
@@ -501,11 +504,6 @@ template <class ELFT> void Writer<ELFT>:
   uintX_t VA = 0x1000; // The first page is kept unmapped.
   uintX_t FileOff = SizeOfHeaders;
 
-  std::stable_sort(OutputSections.begin(), OutputSections.end(),
-                   compSec<ELFT::Is64Bits>);
-
-  addOutputSection(&SymTable);
-  addOutputSection(&StringTable);
   StringTableIndex = OutputSections.size();
   SymTable.setStringTableIndex(StringTableIndex);
 

Added: lld/trunk/test/elf2/output-section.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/output-section.s?rev=247037&view=auto
==============================================================================
--- lld/trunk/test/elf2/output-section.s (added)
+++ lld/trunk/test/elf2/output-section.s Tue Sep  8 13:08:57 2015
@@ -0,0 +1,34 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+// RUN: lld -flavor gnu2 %t -o %t2
+// RUN: llvm-readobj -t %t2 | FileCheck %s
+// REQUIRES: x86
+
+// CHECK:      Symbol {
+// CHECK:        Name: bar_sym
+// CHECK-NEXT:   Value:
+// CHECK-NEXT:   Size:
+// CHECK-NEXT:   Binding:
+// CHECK-NEXT:   Type:
+// CHECK-NEXT:   Other:
+// CHECK-NEXT:   Section: bar
+// CHECK-NEXT: }
+// CHECK-NEXT: Symbol {
+// CHECK-NEXT:   Name: foo_sym
+// CHECK-NEXT:   Value:
+// CHECK-NEXT:   Size:
+// CHECK-NEXT:   Binding:
+// CHECK-NEXT:   Type:
+// CHECK-NEXT:   Other:
+// CHECK-NEXT:   Section: foo
+// CHECK-NEXT: }
+
+.section foo
+.global foo_sym
+foo_sym:
+
+.section bar, "a"
+.global bar_sym
+bar_sym:
+
+.global _start
+_start:




More information about the llvm-commits mailing list