[lld] r244898 - Place SHF_ALLOC sections first in the output.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 08:23:46 PDT 2015


Author: rafael
Date: Thu Aug 13 10:23:46 2015
New Revision: 244898

URL: http://llvm.org/viewvc/llvm-project?rev=244898&view=rev
Log:
Place SHF_ALLOC sections first in the output.

Having them in the middle of the file complicates the creation of segments.

Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/elf2/string-table.s

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=244898&r1=244897&r2=244898&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Aug 13 10:23:46 2015
@@ -51,6 +51,8 @@ public:
   // Returns the size of the section in the output file.
   uintX_t getSize() { return Header.sh_size; }
 
+  uintX_t getFlags() { return Header.sh_flags; }
+
 private:
   StringRef Name;
   Elf_Shdr Header;
@@ -156,12 +158,21 @@ template <class ELFT> void Writer<ELFT>:
   }
 }
 
+template <class ELFT>
+static bool compSec(OutputSection<ELFT> *A, OutputSection<ELFT> *B) {
+  // Place SHF_ALLOC sections first.
+  return (A->getFlags() & SHF_ALLOC) && !(B->getFlags() & SHF_ALLOC);
+}
+
 // Visits all sections to assign incremental, non-overlapping RVAs and
 // file offsets.
 template <class ELFT> void Writer<ELFT>::assignAddresses() {
   SizeOfHeaders = RoundUpToAlignment(sizeof(Elf_Ehdr_Impl<ELFT>), PageSize);
   uintX_t VA = 0x1000; // The first page is kept unmapped.
   uintX_t FileOff = SizeOfHeaders;
+
+  std::stable_sort(OutputSections.begin(), OutputSections.end(), compSec<ELFT>);
+
   for (OutputSection<ELFT> *Sec : OutputSections) {
     Sec->setVA(VA);
     Sec->setFileOffset(FileOff);

Modified: lld/trunk/test/elf2/string-table.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/string-table.s?rev=244898&r1=244897&r2=244898&view=diff
==============================================================================
--- lld/trunk/test/elf2/string-table.s (original)
+++ lld/trunk/test/elf2/string-table.s Thu Aug 13 10:23:46 2015
@@ -6,12 +6,20 @@
 .global _start
 _start:
 
-.section bar
 .section foobar
+.section bar, "a"
 
-// Both sections are in the output:
-// CHECK: Name: bar
-// CHECK: Name: foobar
+// Both sections are in the output and that the alloc section is first:
+// CHECK:      Name: bar
+// CHECK-NEXT:   Type: SHT_PROGBITS
+// CHECK-NEXT:   Flags [
+// CHECK-NEXT:     SHF_ALLOC
+// CHECK-NEXT:   ]
+
+// CHECK:      Name: foobar
+// CHECK-NEXT:   Type: SHT_PROGBITS
+// CHECK-NEXT:   Flags [
+// CHECK-NEXT:   ]
 
 // Test that the sting "bar" is merged into "foobar"
 




More information about the llvm-commits mailing list