[lld] 0f6d720 - [MachO] Properly reset global state

Shoaib Meenai via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 31 16:15:39 PDT 2021


Author: Shoaib Meenai
Date: 2021-10-31T16:14:29-07:00
New Revision: 0f6d720f1f485b7a1842170b571e6b9af7bcaa0e

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

LOG: [MachO] Properly reset global state

We need to reset global state between runs, similar to the other ports.
There's some file-static state which needs to be reset as well and we
need to add some new helpers for that.

With this change, most LLD Mach-O tests pass with `LLD_IN_TEST=2` (which
runs the linker twice on each test). Some tests will be fixed by the
remainder of this stack, and the rest are fundamentally incompatible
with that mode (e.g. they intentionally throw fatal errors).

Fixes PR52070.

Reviewed By: #lld-macho, int3

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

Added: 
    

Modified: 
    lld/MachO/Driver.cpp
    lld/MachO/Driver.h
    lld/MachO/DriverUtils.cpp
    lld/MachO/InputFiles.h
    lld/MachO/OutputSegment.cpp
    lld/MachO/OutputSegment.h
    lld/MachO/Writer.cpp
    lld/MachO/Writer.h

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index c5dccb307db7a..a06b5682d722d 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -1068,7 +1068,25 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
   lld::stdoutOS = &stdoutOS;
   lld::stderrOS = &stderrOS;
 
-  errorHandler().cleanupCallback = []() { freeArena(); };
+  errorHandler().cleanupCallback = []() {
+    freeArena();
+
+    concatOutputSections.clear();
+    inputFiles.clear();
+    inputSections.clear();
+    loadedArchives.clear();
+    syntheticSections.clear();
+    thunkMap.clear();
+
+    firstTLVDataSection = nullptr;
+    tar = nullptr;
+    memset(&in, 0, sizeof(in));
+
+    resetLoadedDylibs();
+    resetOutputSegments();
+    resetWriter();
+    InputFile::resetIdCount();
+  };
 
   errorHandler().logName = args::getFilenameWithoutExe(argsArr[0]);
   stderrOS.enable_colors(stderrOS.has_colors());
@@ -1392,6 +1410,8 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
         reexportHandler(arg, extensions);
     }
 
+    cl::ResetAllOptionOccurrences();
+
     // Parse LTO options.
     if (const Arg *arg = args.getLastArg(OPT_mcpu))
       parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
@@ -1476,5 +1496,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
   if (canExitEarly)
     exitLld(errorCount() ? 1 : 0);
 
-  return !errorCount();
+  bool ret = errorCount() == 0;
+  errorHandler().reset();
+  return ret;
 }

diff  --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h
index af0b1d3e1f440..4a970ac8a0845 100644
--- a/lld/MachO/Driver.h
+++ b/lld/MachO/Driver.h
@@ -56,6 +56,7 @@ llvm::Optional<StringRef> resolveDylibPath(llvm::StringRef path);
 
 DylibFile *loadDylib(llvm::MemoryBufferRef mbref, DylibFile *umbrella = nullptr,
                      bool isBundleLoader = false);
+void resetLoadedDylibs();
 
 // Search for all possible combinations of `{root}/{name}.{extension}`.
 // If \p extensions are not specified, then just search for `{root}/{name}`.

diff  --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp
index a38d4ab075209..3c5440544614c 100644
--- a/lld/MachO/DriverUtils.cpp
+++ b/lld/MachO/DriverUtils.cpp
@@ -246,6 +246,8 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
   return newFile;
 }
 
+void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
+
 Optional<StringRef>
 macho::findPathCombination(const Twine &name,
                            const std::vector<StringRef> &roots,

diff  --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index 3ab3f8e3a0b44..e51e5d557d3b3 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -69,6 +69,7 @@ class InputFile {
   virtual ~InputFile() = default;
   Kind kind() const { return fileKind; }
   StringRef getName() const { return name; }
+  static void resetIdCount() { idCount = 0; }
 
   MemoryBufferRef mb;
 

diff  --git a/lld/MachO/OutputSegment.cpp b/lld/MachO/OutputSegment.cpp
index 3bbaf7f0304eb..da1394c088314 100644
--- a/lld/MachO/OutputSegment.cpp
+++ b/lld/MachO/OutputSegment.cpp
@@ -161,6 +161,11 @@ void macho::sortOutputSegments() {
 static DenseMap<StringRef, OutputSegment *> nameToOutputSegment;
 std::vector<OutputSegment *> macho::outputSegments;
 
+void macho::resetOutputSegments() {
+  outputSegments.clear();
+  nameToOutputSegment.clear();
+}
+
 static StringRef maybeRenameSegment(StringRef name) {
   auto newName = config->segmentRenameMap.find(name);
   if (newName != config->segmentRenameMap.end())

diff  --git a/lld/MachO/OutputSegment.h b/lld/MachO/OutputSegment.h
index b3863f4148d9e..bff99e28a88f1 100644
--- a/lld/MachO/OutputSegment.h
+++ b/lld/MachO/OutputSegment.h
@@ -68,6 +68,7 @@ class OutputSegment {
 extern std::vector<OutputSegment *> outputSegments;
 
 void sortOutputSegments();
+void resetOutputSegments();
 
 OutputSegment *getOrCreateOutputSegment(StringRef name);
 

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 4c01398a6d57b..203a04f888c16 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -343,6 +343,7 @@ class LCDylib final : public LoadCommand {
   }
 
   static uint32_t getInstanceCount() { return instanceCount; }
+  static void resetInstanceCount() { instanceCount = 0; }
 
 private:
   LoadCommandType type;
@@ -1153,6 +1154,8 @@ template <class LP> void Writer::run() {
 
 template <class LP> void macho::writeResult() { Writer().run<LP>(); }
 
+void macho::resetWriter() { LCDylib::resetInstanceCount(); }
+
 void macho::createSyntheticSections() {
   in.header = make<MachHeaderSection>();
   if (config->dedupLiterals) {

diff  --git a/lld/MachO/Writer.h b/lld/MachO/Writer.h
index 56f6f7ae6fe76..5ab40cabe64b4 100644
--- a/lld/MachO/Writer.h
+++ b/lld/MachO/Writer.h
@@ -26,6 +26,7 @@ class LoadCommand {
 };
 
 template <class LP> void writeResult();
+void resetWriter();
 
 void createSyntheticSections();
 


        


More information about the llvm-commits mailing list