[lld] r362444 - ELF: Introduce a separate bit for tracking whether an output section has ever had an input section added to it. NFCI.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 3 13:14:25 PDT 2019


Author: pcc
Date: Mon Jun  3 13:14:25 2019
New Revision: 362444

URL: http://llvm.org/viewvc/llvm-project?rev=362444&view=rev
Log:
ELF: Introduce a separate bit for tracking whether an output section has ever had an input section added to it. NFCI.

We currently (ab)use the Live bit on output sections to track whether
the section has ever had an input section added to it, and then later
use it during orphan placement. This will conflict with one of my upcoming
partition-related changes that will assign all output sections to a partition
(thus marking them as live) so that they can be added to the correct segment
by the code that creates program headers.

Instead of using the Live bit for this purpose, create a new flag and
start using it to track the property explicitly.

Differential Revision: https://reviews.llvm.org/D62348

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

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=362444&r1=362443&r2=362444&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Mon Jun  3 13:14:25 2019
@@ -889,10 +889,9 @@ void LinkerScript::adjustSectionsBeforeS
       Sec->Alignment =
           std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue());
 
-    // A live output section means that some input section was added to it. It
-    // might have been removed (if it was empty synthetic section), but we at
-    // least know the flags.
-    if (Sec->isLive())
+    // The input section might have been removed (if it was an empty synthetic
+    // section), but we at least know the flags.
+    if (Sec->HasInputSections)
       Flags = Sec->Flags;
 
     // We do not want to keep any special flags for output section

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=362444&r1=362443&r2=362444&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Mon Jun  3 13:14:25 2019
@@ -84,9 +84,10 @@ static bool canMergeToProgbits(unsigned
 }
 
 void OutputSection::addSection(InputSection *IS) {
-  if (!isLive()) {
+  if (!HasInputSections) {
     // If IS is the first section to be added to this section,
     // initialize Partition, Type, Entsize and flags from IS.
+    HasInputSections = true;
     Partition = IS->Partition;
     Type = IS->Type;
     Entsize = IS->Entsize;

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=362444&r1=362443&r2=362444&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Mon Jun  3 13:14:25 2019
@@ -93,6 +93,11 @@ public:
   bool UsedInExpression = false;
   bool InOverlay = false;
 
+  // Tracks whether the section has ever had an input section added to it, even
+  // if the section was later removed (e.g. because it is a synthetic section
+  // that wasn't needed). This is needed for orphan placement.
+  bool HasInputSections = false;
+
   void finalize();
   template <class ELFT> void writeTo(uint8_t *Buf);
   template <class ELFT> void maybeCompress();

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=362444&r1=362443&r2=362444&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Jun  3 13:14:25 2019
@@ -1052,7 +1052,7 @@ static int getRankProximityAux(OutputSec
 
 static int getRankProximity(OutputSection *A, BaseCommand *B) {
   auto *Sec = dyn_cast<OutputSection>(B);
-  return (Sec && Sec->isLive()) ? getRankProximityAux(A, Sec) : -1;
+  return (Sec && Sec->HasInputSections) ? getRankProximityAux(A, Sec) : -1;
 }
 
 // When placing orphan sections, we want to place them after symbol assignments
@@ -1094,19 +1094,20 @@ findOrphanPos(std::vector<BaseCommand *>
   int Proximity = getRankProximity(Sec, *I);
   for (; I != E; ++I) {
     auto *CurSec = dyn_cast<OutputSection>(*I);
-    if (!CurSec || !CurSec->isLive())
+    if (!CurSec || !CurSec->HasInputSections)
       continue;
     if (getRankProximity(Sec, CurSec) != Proximity ||
         Sec->SortRank < CurSec->SortRank)
       break;
   }
 
-  auto IsLiveOutputSec = [](BaseCommand *Cmd) {
+  auto IsOutputSecWithInputSections = [](BaseCommand *Cmd) {
     auto *OS = dyn_cast<OutputSection>(Cmd);
-    return OS && OS->isLive();
+    return OS && OS->HasInputSections;
   };
   auto J = std::find_if(llvm::make_reverse_iterator(I),
-                        llvm::make_reverse_iterator(B), IsLiveOutputSec);
+                        llvm::make_reverse_iterator(B),
+                        IsOutputSecWithInputSections);
   I = J.base();
 
   // As a special case, if the orphan section is the last section, put
@@ -1114,7 +1115,7 @@ findOrphanPos(std::vector<BaseCommand *>
   // This matches bfd's behavior and is convenient when the linker script fully
   // specifies the start of the file, but doesn't care about the end (the non
   // alloc sections for example).
-  auto NextSec = std::find_if(I, E, IsLiveOutputSec);
+  auto NextSec = std::find_if(I, E, IsOutputSecWithInputSections);
   if (NextSec == E)
     return E;
 




More information about the llvm-commits mailing list