[lld] r323779 - Sort orphan section if --symbol-ordering-file is given.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 30 08:20:09 PST 2018


Author: rafael
Date: Tue Jan 30 08:20:08 2018
New Revision: 323779

URL: http://llvm.org/viewvc/llvm-project?rev=323779&view=rev
Log:
Sort orphan section if --symbol-ordering-file is given.

Before this patch orphan sections were not sorted.

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/linkerscript/symbol-ordering-file.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=323779&r1=323778&r2=323779&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Tue Jan 30 08:20:08 2018
@@ -285,23 +285,11 @@ static void sortSections(MutableArrayRef
 //    --sort-section is handled as an inner SORT command.
 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
 // 4. If no SORT command is given, sort according to --sort-section.
-// 5. If no SORT commands are given and --sort-section is not specified,
-//    apply sorting provided by --symbol-ordering-file if any exist.
-static void sortInputSections(
-    MutableArrayRef<InputSection *> Vec, const SectionPattern &Pat,
-    const DenseMap<SectionBase *, int> &Order) {
+static void sortInputSections(MutableArrayRef<InputSection *> Vec,
+                              const SectionPattern &Pat) {
   if (Pat.SortOuter == SortSectionPolicy::None)
     return;
 
-  if (Pat.SortOuter == SortSectionPolicy::Default &&
-      Config->SortSection == SortSectionPolicy::Default) {
-    // If -symbol-ordering-file was given, sort accordingly.
-    // Usually, Order is empty.
-    if (!Order.empty())
-      sortByOrder(Vec, [&](InputSectionBase *S) { return Order.lookup(S); });
-    return;
-  }
-
   if (Pat.SortInner == SortSectionPolicy::Default)
     sortSections(Vec, Config->SortSection);
   else
@@ -311,8 +299,7 @@ static void sortInputSections(
 
 // Compute and remember which sections the InputSectionDescription matches.
 std::vector<InputSection *>
-LinkerScript::computeInputSections(const InputSectionDescription *Cmd,
-                                   const DenseMap<SectionBase *, int> &Order) {
+LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
   std::vector<InputSection *> Ret;
 
   // Collects all sections that satisfy constraints of Cmd.
@@ -343,7 +330,7 @@ LinkerScript::computeInputSections(const
     }
 
     sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore),
-                      Pat, Order);
+                      Pat);
   }
   return Ret;
 }
@@ -360,13 +347,13 @@ void LinkerScript::discard(ArrayRef<Inpu
   }
 }
 
-std::vector<InputSection *> LinkerScript::createInputSectionList(
-    OutputSection &OutCmd, const DenseMap<SectionBase *, int> &Order) {
+std::vector<InputSection *>
+LinkerScript::createInputSectionList(OutputSection &OutCmd) {
   std::vector<InputSection *> Ret;
 
   for (BaseCommand *Base : OutCmd.SectionCommands) {
     if (auto *Cmd = dyn_cast<InputSectionDescription>(Base)) {
-      Cmd->Sections = computeInputSections(Cmd, Order);
+      Cmd->Sections = computeInputSections(Cmd);
       Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
     }
   }
@@ -395,7 +382,6 @@ void LinkerScript::processSectionCommand
   Ctx->OutSec = Aether;
 
   size_t I = 0;
-  DenseMap<SectionBase *, int> Order = buildSectionOrder();
   // Add input sections to output sections.
   for (BaseCommand *Base : SectionCommands) {
     // Handle symbol assignments outside of any output section.
@@ -405,7 +391,7 @@ void LinkerScript::processSectionCommand
     }
 
     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
-      std::vector<InputSection *> V = createInputSectionList(*Sec, Order);
+      std::vector<InputSection *> V = createInputSectionList(*Sec);
 
       // The output section name `/DISCARD/' is special.
       // Any input section assigned to it is discarded.

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=323779&r1=323778&r2=323779&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Tue Jan 30 08:20:08 2018
@@ -217,12 +217,9 @@ class LinkerScript final {
   void setDot(Expr E, const Twine &Loc, bool InSec);
 
   std::vector<InputSection *>
-  computeInputSections(const InputSectionDescription *,
-                       const llvm::DenseMap<SectionBase *, int> &Order);
+  computeInputSections(const InputSectionDescription *);
 
-  std::vector<InputSection *>
-  createInputSectionList(OutputSection &Cmd,
-                         const llvm::DenseMap<SectionBase *, int> &Order);
+  std::vector<InputSection *> createInputSectionList(OutputSection &Cmd);
 
   std::vector<size_t> getPhdrIndices(OutputSection *Sec);
 

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=323779&r1=323778&r2=323779&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Tue Jan 30 08:20:08 2018
@@ -134,8 +134,8 @@ void OutputSection::addSection(InputSect
   }
 }
 
-void elf::sortByOrder(MutableArrayRef<InputSection *> In,
-                      std::function<int(InputSectionBase *S)> Order) {
+static void sortByOrder(MutableArrayRef<InputSection *> In,
+                        std::function<int(InputSectionBase *S)> Order) {
   typedef std::pair<int, InputSection *> Pair;
   auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
 

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=323779&r1=323778&r2=323779&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Tue Jan 30 08:20:08 2018
@@ -143,8 +143,6 @@ namespace lld {
 namespace elf {
 
 uint64_t getHeaderSize();
-void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
-                 std::function<int(InputSectionBase *S)> Order);
 
 extern std::vector<OutputSection *> OutputSections;
 } // namespace elf

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=323779&r1=323778&r2=323779&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Jan 30 08:20:08 2018
@@ -1018,10 +1018,8 @@ findOrphanPos(std::vector<BaseCommand *>
 }
 
 // If no layout was provided by linker script, we want to apply default
-// sorting for special input sections and handle --symbol-ordering-file.
+// sorting for special input sections. This also handles --symbol-ordering-file.
 template <class ELFT> void Writer<ELFT>::sortInputSections() {
-  assert(!Script->HasSectionsCommand);
-
   // Sort input sections by priority using the list provided
   // by --symbol-ordering-file.
   DenseMap<SectionBase *, int> Order = buildSectionOrder();
@@ -1031,6 +1029,9 @@ template <class ELFT> void Writer<ELFT>:
         if (Sec->Live)
           Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); });
 
+  if (Script->HasSectionsCommand)
+    return;
+
   // Sort input sections by section name suffixes for
   // __attribute__((init_priority(N))).
   if (OutputSection *Sec = findSection(".init_array"))
@@ -1057,9 +1058,9 @@ template <class ELFT> void Writer<ELFT>:
     if (auto *Sec = dyn_cast<OutputSection>(Base))
       Sec->SortRank = getSectionRank(Sec);
 
-  if (!Script->HasSectionsCommand) {
-    sortInputSections();
+  sortInputSections();
 
+  if (!Script->HasSectionsCommand) {
     // We know that all the OutputSections are contiguous in this case.
     auto E = Script->SectionCommands.end();
     auto I = Script->SectionCommands.begin();

Modified: lld/trunk/test/ELF/linkerscript/symbol-ordering-file.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/symbol-ordering-file.s?rev=323779&r1=323778&r2=323779&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/symbol-ordering-file.s (original)
+++ lld/trunk/test/ELF/linkerscript/symbol-ordering-file.s Tue Jan 30 08:20:08 2018
@@ -14,6 +14,10 @@
 # AFTER:      Contents of section .foo:
 # AFTER-NEXT: 2211
 
+# RUN: echo "SECTIONS { .text : { *(.text) } }" > %t2.script
+# RUN: ld.lld --symbol-ordering-file %t.ord %t.o --script %t2.script -o %t3.out
+# RUN: llvm-objdump -s %t3.out| FileCheck %s --check-prefix=AFTER
+
 .section .foo,"ax", at progbits,unique,1
 _foo1:
  .byte 0x11




More information about the llvm-commits mailing list