diff --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp --- a/lld/MachO/DriverUtils.cpp +++ b/lld/MachO/DriverUtils.cpp @@ -188,8 +188,8 @@ Optional macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella, bool isBundleLoader) { - StringRef path = mbref.getBufferIdentifier(); - DylibFile *&file = loadedDylibs[CachedHashStringRef(path)]; + CachedHashStringRef path(mbref.getBufferIdentifier()); + DylibFile *file = loadedDylibs[path]; if (file) return file; @@ -209,6 +209,11 @@ magic == file_magic::macho_bundle); file = make(mbref, umbrella, isBundleLoader); } + // Note that DylibFile's ctor may recursively invoke loadDylib(), which can + // cause loadedDylibs to get resized and its iterators invalidated. As such, + // we redo the key lookup here instead of caching an iterator from our earlier + // lookup at the start of the function. + loadedDylibs[path] = file; return file; } diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -160,7 +160,7 @@ bool isBundleLoader; private: - template void parse(DylibFile *umbrella = nullptr); + template void parse(DylibFile *umbrella); }; // .a file