[lld] 1a59023 - [ELF] Optimize "Strip sections"

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 15 23:15:49 PDT 2022


Author: Fangrui Song
Date: 2022-03-15T23:15:43-07:00
New Revision: 1a590232f42a129b1d209a2f9ac2915f1e8d13d0

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

LOG: [ELF] Optimize "Strip sections"

If SHT_LLVM_SYMPART is unused, don't iterate over inputSections.
If neither --strip-debug/--strip-all, don't iterate over inputSections.

Added: 
    

Modified: 
    lld/ELF/Config.h
    lld/ELF/Driver.cpp
    lld/ELF/InputFiles.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 82aa217af089a..c05adb5d178c5 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -363,6 +363,8 @@ struct Ctx {
   // Symbols in a non-prevailing COMDAT group which should be changed to an
   // Undefined.
   SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms;
+  // True if SHT_LLVM_SYMPART is used.
+  std::atomic<bool> hasSympart{false};
 };
 
 // The only instance of Ctx struct.

diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 15434cc4c7f67..c51349d4a53a0 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2601,26 +2601,28 @@ void LinkerDriver::link(opt::InputArgList &args) {
 
   {
     llvm::TimeTraceScope timeScope("Strip sections");
-    llvm::erase_if(inputSections, [](InputSectionBase *s) {
-      if (s->type == SHT_LLVM_SYMPART) {
+    if (ctx->hasSympart.load(std::memory_order_relaxed)) {
+      llvm::erase_if(inputSections, [](InputSectionBase *s) {
+        if (s->type != SHT_LLVM_SYMPART)
+          return false;
         invokeELFT(readSymbolPartitionSection, s);
         return true;
-      }
+      });
+    }
+    // We do not want to emit debug sections if --strip-all
+    // or --strip-debug are given.
+    if (config->strip != StripPolicy::None) {
+      llvm::erase_if(inputSections, [](InputSectionBase *s) {
+        if (isDebugSection(*s))
+          return true;
+        if (auto *isec = dyn_cast<InputSection>(s))
+          if (InputSectionBase *rel = isec->getRelocatedSection())
+            if (isDebugSection(*rel))
+              return true;
 
-      // We do not want to emit debug sections if --strip-all
-      // or --strip-debug are given.
-      if (config->strip == StripPolicy::None)
         return false;
-
-      if (isDebugSection(*s))
-        return true;
-      if (auto *isec = dyn_cast<InputSection>(s))
-        if (InputSectionBase *rel = isec->getRelocatedSection())
-          if (isDebugSection(*rel))
-            return true;
-
-      return false;
-    });
+      });
+    }
   }
 
   // Since we now have a complete set of input files, we can create

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index d7ccbd9c6d5ac..362263e890062 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "InputFiles.h"
+#include "Config.h"
 #include "DWARF.h"
 #include "Driver.h"
 #include "InputSection.h"
@@ -600,6 +601,9 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
     case SHT_RELA:
     case SHT_NULL:
       break;
+    case SHT_LLVM_SYMPART:
+      ctx->hasSympart.store(true, std::memory_order_relaxed);
+      LLVM_FALLTHROUGH;
     default:
       this->sections[i] =
           createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));


        


More information about the llvm-commits mailing list