[PATCH] D38582: [ELF] - Get rid of LinkerScript::adjustSectionsBeforeSorting().

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 5 06:19:41 PDT 2017


grimar created this revision.
Herald added a subscriber: emaste.

I suggest to get rid of LinkerScript::adjustSectionsBeforeSorting() method.
It is used to force output sections that does not have any input sections
to be live when they have data bytes commands or assignments inside.

Probably there is no reason to delay that and do separatelly, I believe it
is simpler to set this flag when we do linker script commands proccessing,
as at that place we already know everything we need to do that.


https://reviews.llvm.org/D38582

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


Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1048,8 +1048,6 @@
 }
 
 template <class ELFT> void Writer<ELFT>::sortSections() {
-  Script->adjustSectionsBeforeSorting();
-
   // Don't sort if using -r. It is not necessary and we want to preserve the
   // relative order for SHF_LINK_ORDER sections.
   if (Config->Relocatable)
Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -246,7 +246,6 @@
   void fabricateDefaultCommands();
   void addOrphanSections(OutputSectionFactory &Factory);
   void removeEmptyCommands();
-  void adjustSectionsBeforeSorting();
   void adjustSectionsAfterSorting();
 
   std::vector<PhdrEntry *> createPhdrs();
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -367,6 +367,7 @@
   CurAddressState->OutSec = Aether;
   Dot = 0;
 
+  uint64_t PrevFlags = SHF_ALLOC;
   for (size_t I = 0; I < Opt.Commands.size(); ++I) {
     // Handle symbol assignments outside of any output section.
     if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
@@ -421,6 +422,20 @@
       Sec->SectionIndex = I;
       if (Sec->Noload)
         Sec->Type = SHT_NOBITS;
+
+      if (Sec->Live)
+        PrevFlags = Sec->Flags;
+      if (Sec->Live || llvm::all_of(Sec->Commands, [](BaseCommand *Cmd) {
+            return isa<InputSectionDescription>(Cmd);
+          }))
+        continue;
+      // If the output section contains any other commands except input section
+      // descriptions, we create a corresponding output section. The bfd linker
+      // seems to only create them if  '.' is assigned to. But creating these
+      // section should not have any bad consequeces and gives us a section to
+      // put in the content of commands like BYTE() or attach symbols assigned.
+      Sec->Live = true;
+      Sec->Flags = PrevFlags;
     }
   }
   CurAddressState = nullptr;
@@ -642,37 +657,6 @@
   });
 }
 
-static bool isAllSectionDescription(const OutputSection &Cmd) {
-  for (BaseCommand *Base : Cmd.Commands)
-    if (!isa<InputSectionDescription>(*Base))
-      return false;
-  return true;
-}
-
-void LinkerScript::adjustSectionsBeforeSorting() {
-  // If the output section contains only symbol assignments, create a
-  // corresponding output section. The bfd linker seems to only create them if
-  // '.' is assigned to, but creating these section should not have any bad
-  // consequeces and gives us a section to put the symbol in.
-  uint64_t Flags = SHF_ALLOC;
-
-  for (BaseCommand * Cmd : Opt.Commands) {
-    auto *Sec = dyn_cast<OutputSection>(Cmd);
-    if (!Sec)
-      continue;
-    if (Sec->Live) {
-      Flags = Sec->Flags;
-      continue;
-    }
-
-    if (isAllSectionDescription(*Sec))
-      continue;
-
-    Sec->Live = true;
-    Sec->Flags = Flags;
-  }
-}
-
 void LinkerScript::adjustSectionsAfterSorting() {
   // Try and find an appropriate memory region to assign offsets in.
   for (BaseCommand *Base : Opt.Commands) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38582.117805.patch
Type: text/x-patch
Size: 3174 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171005/84afd1b1/attachment.bin>


More information about the llvm-commits mailing list