diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp index 826e5e816adc..2bcea9ad505d 100644 --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -80,16 +80,30 @@ static HeaderFileType getOutputType(const InputArgList &args) { } } +static DenseMap resolvedLibraries; static Optional findLibrary(StringRef name) { - if (config->searchDylibsFirst) { - if (Optional path = findPathCombination( - "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"})) - return path; + CachedHashStringRef key(name); + auto entry = resolvedLibraries.find(key); + if (entry != resolvedLibraries.end()) + return entry->second; + + auto doFind = [&] { + if (config->searchDylibsFirst) { + if (Optional path = findPathCombination( + "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"})) + return path; + return findPathCombination("lib" + name, config->librarySearchPaths, + {".a"}); + } return findPathCombination("lib" + name, config->librarySearchPaths, - {".a"}); - } - return findPathCombination("lib" + name, config->librarySearchPaths, - {".tbd", ".dylib", ".a"}); + {".tbd", ".dylib", ".a"}); + }; + + Optional path = doFind(); + if (path) + resolvedLibraries[key] = *path; + + return path; } static Optional findFramework(StringRef name) { @@ -1076,6 +1090,7 @@ bool macho::link(ArrayRef argsArr, bool canExitEarly, errorHandler().cleanupCallback = []() { freeArena(); + resolvedLibraries.clear(); concatOutputSections.clear(); inputFiles.clear(); inputSections.clear();