[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
Mon Jun 5 01:51:44 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL304700: [ELF] SHF_LINK_ORDER should sort based on InputSectionDescriptions (authored by psmith).

Changed prior to commit:
  https://reviews.llvm.org/D33772?vs=101177&id=101377#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D33772

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


Index: lld/trunk/ELF/OutputSections.cpp
===================================================================
--- lld/trunk/ELF/OutputSections.cpp
+++ lld/trunk/ELF/OutputSections.cpp
@@ -99,8 +99,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: lld/trunk/ELF/Writer.cpp
===================================================================
--- lld/trunk/ELF/Writer.cpp
+++ lld/trunk/ELF/Writer.cpp
@@ -257,11 +257,6 @@
   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 @@
       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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33772.101377.patch
Type: text/x-patch
Size: 2095 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170605/dbe5920e/attachment.bin>


More information about the llvm-commits mailing list