[lld] r304700 - [ELF] SHF_LINK_ORDER should sort based on InputSectionDescriptions

Peter Smith via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 5 01:51:15 PDT 2017


Author: psmith
Date: Mon Jun  5 03:51:15 2017
New Revision: 304700

URL: http://llvm.org/viewvc/llvm-project?rev=304700&view=rev
Log:
[ELF] SHF_LINK_ORDER should sort based on InputSectionDescriptions
    
This change alters the sorting for OutputSections with the SHF_LINK_ORDER
flag in OutputSection::finalize() to use the InputSectionDescription
representation and not the OutputSection::Sections representation.

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


Modified:
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=304700&r1=304699&r2=304700&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Mon Jun  5 03:51:15 2017
@@ -99,8 +99,20 @@ template <class ELFT> static void finali
 
 template <class ELFT> void OutputSection::finalize() {
   if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
+    OutputSectionCommand *Cmd = Script->getCmd(this);
+    // Link order may be distributed across several InputSectionDescriptions
+    // but sort must consider them all at once.
+    std::vector<InputSection **> ScriptSections;
+    std::vector<InputSection *> Sections;
+    for (BaseCommand *Base : Cmd->Commands)
+      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
+        for (InputSection *&IS : ISD->Sections) {
+          ScriptSections.push_back(&IS);
+          Sections.push_back(IS);
+        }
     std::sort(Sections.begin(), Sections.end(), compareByFilePosition);
-    assignOffsets();
+    for (int I = 0, N = Sections.size(); I < N; ++I)
+      *ScriptSections[I] = Sections[I];
 
     // We must preserve the link order dependency of sections with the
     // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=304700&r1=304699&r2=304700&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Jun  5 03:51:15 2017
@@ -257,11 +257,6 @@ template <class ELFT> void Writer<ELFT>:
   if (ErrorCount)
     return;
 
-  if (!Script->Opt.HasSections)
-    Script->fabricateDefaultCommands();
-  else
-    Script->synchronize();
-
   for (BaseCommand *Base : Script->Opt.Commands)
     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
       OutputSectionCommands.push_back(Cmd);
@@ -1261,6 +1256,12 @@ template <class ELFT> void Writer<ELFT>:
       applySynthetic({InX::MipsGot},
                      [](SyntheticSection *SS) { SS->updateAllocSize(); });
   }
+
+  if (!Script->Opt.HasSections)
+    Script->fabricateDefaultCommands();
+  else
+    Script->synchronize();
+
   // Fill other section headers. The dynamic table is finalized
   // at the end because some tags like RELSZ depend on result
   // of finalizing other sections.




More information about the llvm-commits mailing list