[lld] c09d323 - [ELF] Move EhInputSection out of inputSections. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 31 11:58:14 PDT 2022


Author: Fangrui Song
Date: 2022-07-31T11:58:08-07:00
New Revision: c09d3235997a0d1a816e0210e0611be31a0e9415

URL: https://github.com/llvm/llvm-project/commit/c09d3235997a0d1a816e0210e0611be31a0e9415
DIFF: https://github.com/llvm/llvm-project/commit/c09d3235997a0d1a816e0210e0611be31a0e9415.diff

LOG: [ELF] Move EhInputSection out of inputSections. NFC

inputSections temporarily contains EhInputSection objects mainly for
combineEhSections. Place EhInputSection objects into a new vector
ehInputSections instead of inputSections.

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/ELF/InputSection.cpp
    lld/ELF/InputSection.h
    lld/ELF/LinkerScript.cpp
    lld/ELF/MarkLive.cpp
    lld/ELF/SyntheticSections.cpp
    lld/ELF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 8d2a718510877..826cbaeeed707 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -96,6 +96,7 @@ bool elf::link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
   ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
   ctx->e.cleanupCallback = []() {
     inputSections.clear();
+    ehInputSections.clear();
     outputSections.clear();
     symAux.clear();
 
@@ -2655,10 +2656,16 @@ void LinkerDriver::link(opt::InputArgList &args) {
     // Now that we have a complete list of input files.
     // Beyond this point, no new files are added.
     // Aggregate all input sections into one place.
-    for (InputFile *f : ctx->objectFiles)
-      for (InputSectionBase *s : f->getSections())
-        if (s && s != &InputSection::discarded)
+    for (InputFile *f : ctx->objectFiles) {
+      for (InputSectionBase *s : f->getSections()) {
+        if (!s || s == &InputSection::discarded)
+          continue;
+        if (LLVM_UNLIKELY(isa<EhInputSection>(s)))
+          ehInputSections.push_back(cast<EhInputSection>(s));
+        else
           inputSections.push_back(s);
+      }
+    }
     for (BinaryFile *f : ctx->binaryFiles)
       for (InputSectionBase *s : f->getSections())
         inputSections.push_back(cast<InputSection>(s));

diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index e8fbab0666ec1..bfab457edfbd4 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -34,6 +34,7 @@ using namespace lld;
 using namespace lld::elf;
 
 SmallVector<InputSectionBase *, 0> elf::inputSections;
+SmallVector<EhInputSection *, 0> elf::ehInputSections;
 DenseSet<std::pair<const Symbol *, uint64_t>> elf::ppc64noTocRelax;
 
 // Returns a string to construct an error message.

diff  --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h
index 73b2bdaf58be0..b53119fcda027 100644
--- a/lld/ELF/InputSection.h
+++ b/lld/ELF/InputSection.h
@@ -424,6 +424,7 @@ inline bool isDebugSection(const InputSectionBase &sec) {
 
 // The list of all input sections.
 extern SmallVector<InputSectionBase *, 0> inputSections;
+extern SmallVector<EhInputSection *, 0> ehInputSections;
 
 // The set of TOC entries (.toc + addend) for which we should not apply
 // toc-indirect to toc-relative relaxation. const Symbol * refers to the

diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 7e41b622beb56..5a8c196b9633a 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -848,11 +848,9 @@ void LinkerScript::addOrphanSections() {
   // for synthetic sections because them are special.
   size_t n = 0;
   for (InputSectionBase *isec : inputSections) {
-    // Process InputSection and MergeInputSection. Discard EhInputSection.
+    // Process InputSection and MergeInputSection.
     if (LLVM_LIKELY(isa<InputSection>(isec)))
       inputSections[n++] = isec;
-    else if (isa<EhInputSection>(isec))
-      continue;
 
     // In -r links, SHF_LINK_ORDER sections are added while adding their parent
     // sections because we need to know the parent's output section before we

diff  --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp
index 12a23390e663b..c7596d1293bbf 100644
--- a/lld/ELF/MarkLive.cpp
+++ b/lld/ELF/MarkLive.cpp
@@ -244,22 +244,18 @@ template <class ELFT> void MarkLive<ELFT>::run() {
   for (StringRef s : script->referencedSymbols)
     markSymbol(symtab->find(s));
 
+  // Mark .eh_frame sections as live because there are usually no relocations
+  // that point to .eh_frames. Otherwise, the garbage collector would drop
+  // all of them. We also want to preserve personality routines and LSDA
+  // referenced by .eh_frame sections, so we scan them for that here.
+  for (EhInputSection *eh : ehInputSections) {
+    const RelsOrRelas<ELFT> rels = eh->template relsOrRelas<ELFT>();
+    if (rels.areRelocsRel())
+      scanEhFrameSection(*eh, rels.rels);
+    else if (rels.relas.size())
+      scanEhFrameSection(*eh, rels.relas);
+  }
   for (InputSectionBase *sec : inputSections) {
-    // Mark .eh_frame sections as live because there are usually no relocations
-    // that point to .eh_frames. Otherwise, the garbage collector would drop
-    // all of them. We also want to preserve personality routines and LSDA
-    // referenced by .eh_frame sections, so we scan them for that here.
-    if (auto *eh = dyn_cast<EhInputSection>(sec)) {
-      eh->markLive();
-
-      const RelsOrRelas<ELFT> rels = eh->template relsOrRelas<ELFT>();
-      if (rels.areRelocsRel())
-        scanEhFrameSection(*eh, rels.rels);
-      else if (rels.relas.size())
-        scanEhFrameSection(*eh, rels.relas);
-      continue;
-    }
-
     if (sec->flags & SHF_GNU_RETAIN) {
       enqueue(sec, 0);
       continue;

diff  --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 2d96bc6351a84..f251bbcfcba00 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -3364,23 +3364,20 @@ template <class ELFT> void elf::splitSections() {
 
 void elf::combineEhSections() {
   llvm::TimeTraceScope timeScope("Combine EH sections");
-  for (InputSectionBase *&s : inputSections) {
+  for (EhInputSection *sec : ehInputSections)
+    sec->getPartition().ehFrame->addSection(sec);
+
+  if (!mainPart->armExidx)
+    return;
+  llvm::erase_if(inputSections, [](InputSectionBase *s) {
     // Ignore dead sections and the partition end marker (.part.end),
     // whose partition number is out of bounds.
     if (!s->isLive() || s->partition == 255)
-      continue;
-
+      return false;
     Partition &part = s->getPartition();
-    if (auto *es = dyn_cast<EhInputSection>(s)) {
-      part.ehFrame->addSection(es);
-    } else if (s->kind() == SectionBase::Regular && part.armExidx &&
-               part.armExidx->addSection(cast<InputSection>(s))) {
-      s = nullptr;
-    }
-  }
-
-  if (mainPart->armExidx)
-    llvm::erase_value(inputSections, nullptr);
+    return s->kind() == SectionBase::Regular && part.armExidx &&
+           part.armExidx->addSection(cast<InputSection>(s));
+  });
 }
 
 MipsRldMapSection::MipsRldMapSection()

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index e4e6cb242436d..858a219ab1439 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -121,20 +121,21 @@ static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
 
 void elf::copySectionsIntoPartitions() {
   SmallVector<InputSectionBase *, 0> newSections;
+  const size_t ehSize = ehInputSections.size();
   for (unsigned part = 2; part != partitions.size() + 1; ++part) {
     for (InputSectionBase *s : inputSections) {
-      if (!(s->flags & SHF_ALLOC) || !s->isLive())
-        continue;
-      InputSectionBase *copy;
-      if (s->type == SHT_NOTE)
-        copy = make<InputSection>(cast<InputSection>(*s));
-      else if (auto *es = dyn_cast<EhInputSection>(s))
-        copy = make<EhInputSection>(*es);
-      else
+      if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)
         continue;
+      auto *copy = make<InputSection>(cast<InputSection>(*s));
       copy->partition = part;
       newSections.push_back(copy);
     }
+    for (size_t i = 0; i != ehSize; ++i) {
+      assert(ehInputSections[i]->isLive());
+      auto *copy = make<EhInputSection>(*ehInputSections[i]);
+      copy->partition = part;
+      ehInputSections.push_back(copy);
+    }
   }
 
   inputSections.insert(inputSections.end(), newSections.begin(),


        


More information about the llvm-commits mailing list