[PATCH] D33772: [LLD][ELF] SHF_LINK_ORDER should sort based on InputSectionDescriptions

Peter Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 1 05:06:32 PDT 2017


peter.smith created this revision.
Herald added a subscriber: emaste.

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.

      

With this change the OutputSections::Sections is no longer operated on for SHF_LINK_ORDER. The synchronize function has been changed to skip sections with SHF_LINK_ORDER as this will undo the ordering done in finalize().


https://reviews.llvm.org/D33772

Files:
  ELF/LinkerScript.cpp
  ELF/OutputSections.cpp
  ELF/Writer.cpp


Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -257,12 +257,6 @@
   if (ErrorCount)
     return;
 
-  if (!Script->Opt.HasSections) {
-    if (!Config->Relocatable)
-      fixSectionAlignments();
-    Script->fabricateDefaultCommands();
-  }
-
   for (BaseCommand *Base : Script->Opt.Commands)
     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
       OutputSectionCommands.push_back(Cmd);
@@ -1230,6 +1224,12 @@
     Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
   }
 
+  if (!Script->Opt.HasSections) {
+    if (!Config->Relocatable)
+      fixSectionAlignments();
+    Script->fabricateDefaultCommands();
+  }
+
   // Dynamic section must be the last one in this list and dynamic
   // symbol table section (DynSymTab) must be the first one.
   applySynthetic({InX::DynSymTab,    InX::Bss,           InX::BssRelRo,
Index: ELF/OutputSections.cpp
===================================================================
--- ELF/OutputSections.cpp
+++ ELF/OutputSections.cpp
@@ -127,8 +127,20 @@
 
 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
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -885,7 +885,7 @@
 void LinkerScript::synchronize() {
   for (BaseCommand *Base : Opt.Commands) {
     auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
-    if (!Cmd)
+    if (!Cmd || (Cmd->Sec->Flags & SHF_LINK_ORDER))
       continue;
     ArrayRef<InputSection *> Sections = Cmd->Sec->Sections;
     std::vector<InputSection **> ScriptSections;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33772.101013.patch
Type: text/x-patch
Size: 2591 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170601/5007d2cb/attachment.bin>


More information about the llvm-commits mailing list