diff --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h --- a/lld/MachO/Driver.h +++ b/lld/MachO/Driver.h @@ -56,6 +56,7 @@ 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/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1068,7 +1068,25 @@ 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 @@ 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 @@ if (canExitEarly) exitLld(errorCount() ? 1 : 0); - return !errorCount(); + bool ret = errorCount() == 0; + errorHandler().reset(); + return ret; } diff --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp --- a/lld/MachO/DriverUtils.cpp +++ b/lld/MachO/DriverUtils.cpp @@ -246,6 +246,8 @@ return newFile; } +void macho::resetLoadedDylibs() { loadedDylibs.clear(); } + Optional macho::findPathCombination(const Twine &name, const std::vector &roots, diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -69,6 +69,7 @@ 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.h b/lld/MachO/OutputSegment.h --- a/lld/MachO/OutputSegment.h +++ b/lld/MachO/OutputSegment.h @@ -68,6 +68,7 @@ extern std::vector outputSegments; void sortOutputSegments(); +void resetOutputSegments(); OutputSegment *getOrCreateOutputSegment(StringRef name); diff --git a/lld/MachO/OutputSegment.cpp b/lld/MachO/OutputSegment.cpp --- a/lld/MachO/OutputSegment.cpp +++ b/lld/MachO/OutputSegment.cpp @@ -161,6 +161,11 @@ static DenseMap nameToOutputSegment; std::vector 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/Writer.h b/lld/MachO/Writer.h --- a/lld/MachO/Writer.h +++ b/lld/MachO/Writer.h @@ -26,6 +26,7 @@ }; template void writeResult(); +void resetWriter(); void createSyntheticSections(); diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -343,6 +343,7 @@ } static uint32_t getInstanceCount() { return instanceCount; } + static void resetInstanceCount() { instanceCount = 0; } private: LoadCommandType type; @@ -1153,6 +1154,8 @@ template void macho::writeResult() { Writer().run(); } +void macho::resetWriter() { LCDylib::resetInstanceCount(); } + void macho::createSyntheticSections() { in.header = make(); if (config->dedupLiterals) {