+ // We might have to patch the shared cache __DATA_CONST. In that case, we'll create just a single
+ // patcher when needed.
+ DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
+
+#if TARGET_OS_OSX
+ // only do alternate algorithm for dlopen(). Use traditional algorithm for launch
+ if ( !context.linkingMainExecutable ) {
+ // Don't take the memory hit of weak defs on the launch path until we hit a dlopen with more weak symbols to bind
+ if (!context.weakDefMapProcessedLaunchDefs) {
+ context.weakDefMapProcessedLaunchDefs = true;
+
+ // Walk the nlist for all binaries from launch and fill in the map with any other weak defs
+ for (int i=0; i < count; ++i) {
+ const ImageLoader* image = imagesNeedingCoalescing[i];
+ // skip images without defs. We've processed launch time refs already
+ if ( !image->hasCoalescedExports() )
+ continue;
+ // Only process binaries which have had their weak symbols bound, ie, not the new ones we are processing now
+ // from this dlopen
+ if ( !image->weakSymbolsBound(imageIndexes[i]) )
+ continue;
+
+ Diagnostics diag;
+ const dyld3::MachOAnalyzer* ma = (const dyld3::MachOAnalyzer*)image->machHeader();
+ ma->forEachWeakDef(diag, ^(const char *symbolName, uint64_t imageOffset, bool isFromExportTrie) {
+ uintptr_t targetAddr = (uintptr_t)ma + (uintptr_t)imageOffset;
+ if ( isFromExportTrie ) {
+ // Avoid duplicating the string if we already have the symbol name
+ if ( context.weakDefMap.find(symbolName) != context.weakDefMap.end() )
+ return;
+ symbolName = strdup(symbolName);
+ }
+ context.weakDefMap.insert({ symbolName, { image, targetAddr } });
+ });
+ }
+ }
+
+ // Walk the nlist for all binaries in dlopen and fill in the map with any other weak defs
+ for (int i=0; i < count; ++i) {
+ const ImageLoader* image = imagesNeedingCoalescing[i];
+ if ( image->weakSymbolsBound(imageIndexes[i]) )
+ continue;
+ // skip images without defs. We'll process refs later
+ if ( !image->hasCoalescedExports() )
+ continue;
+ Diagnostics diag;
+ const dyld3::MachOAnalyzer* ma = (const dyld3::MachOAnalyzer*)image->machHeader();
+ ma->forEachWeakDef(diag, ^(const char *symbolName, uint64_t imageOffset, bool isFromExportTrie) {
+ uintptr_t targetAddr = (uintptr_t)ma + (uintptr_t)imageOffset;
+ if ( isFromExportTrie ) {
+ // Avoid duplicating the string if we already have the symbol name
+ if ( context.weakDefMap.find(symbolName) != context.weakDefMap.end() )
+ return;
+ symbolName = strdup(symbolName);
+ }
+ context.weakDefMap.insert({ symbolName, { image, targetAddr } });
+ });
+ }
+ // for all images that need weak binding
+ for (int i=0; i < count; ++i) {
+ ImageLoader* imageBeingFixedUp = imagesNeedingCoalescing[i];
+ if ( imageBeingFixedUp->weakSymbolsBound(imageIndexes[i]) )
+ continue; // weak binding already completed
+ bool imageBeingFixedUpInCache = imageBeingFixedUp->inSharedCache();
+
+ if ( context.verboseWeakBind )
+ dyld::log("dyld: checking for weak symbols in %s\n", imageBeingFixedUp->getPath());
+ // for all symbols that need weak binding in this image
+ ImageLoader::CoalIterator coalIterator;
+ imageBeingFixedUp->initializeCoalIterator(coalIterator, i, imageIndexes[i]);
+ while ( !imageBeingFixedUp->incrementCoalIterator(coalIterator) ) {
+ const char* nameToCoalesce = coalIterator.symbolName;
+ uintptr_t targetAddr = 0;
+ const ImageLoader* targetImage;
+ // Seatch the map for a previous definition to use
+ auto weakDefIt = context.weakDefMap.find(nameToCoalesce);
+ if ( (weakDefIt != context.weakDefMap.end()) && (weakDefIt->second.first != nullptr) ) {
+ // Found a previous defition
+ targetImage = weakDefIt->second.first;
+ targetAddr = weakDefIt->second.second;
+ } else {
+ // scan all images looking for definition to use
+ for (int j=0; j < count; ++j) {
+ const ImageLoader* anImage = imagesNeedingCoalescing[j];
+ bool anImageInCache = anImage->inSharedCache();
+ // <rdar://problem/47986398> Don't look at images in dyld cache because cache is
+ // already coalesced. Only images outside cache can potentially override something in cache.
+ if ( anImageInCache && imageBeingFixedUpInCache )
+ continue;
+
+ //dyld::log("looking for %s in %s\n", nameToCoalesce, anImage->getPath());
+ const ImageLoader* foundIn;
+ const Symbol* sym = anImage->findExportedSymbol(nameToCoalesce, false, &foundIn);
+ if ( sym != NULL ) {
+ targetAddr = foundIn->getExportedSymbolAddress(sym, context);
+ targetImage = foundIn;
+ if ( context.verboseWeakBind )
+ dyld::log("dyld: found weak %s at 0x%lX in %s\n", nameToCoalesce, targetAddr, foundIn->getPath());
+ break;
+ }
+ }
+ }
+ if ( (targetAddr != 0) && (coalIterator.image != targetImage) ) {
+ if ( coalIterator.image->inSharedCache() )
+ patcher.makeWriteable();
+ coalIterator.image->updateUsesCoalIterator(coalIterator, targetAddr, (ImageLoader*)targetImage, 0, context);
+ if (weakDefIt == context.weakDefMap.end()) {
+ if (targetImage->neverUnload()) {
+ // Add never unload defs to the map for next time
+ context.weakDefMap.insert({ nameToCoalesce, { targetImage, targetAddr } });
+ if ( context.verboseWeakBind ) {
+ dyld::log("dyld: weak binding adding %s to map\n", nameToCoalesce);
+ }
+ } else {
+ // Add a placeholder for unloadable symbols which makes us fall back to the regular search
+ context.weakDefMap.insert({ nameToCoalesce, { targetImage, targetAddr } });
+ if ( context.verboseWeakBind ) {
+ dyld::log("dyld: weak binding adding unloadable placeholder %s to map\n", nameToCoalesce);
+ }
+ }
+ }
+ if ( context.verboseWeakBind )
+ dyld::log("dyld: adjusting uses of %s in %s to use definition from %s\n", nameToCoalesce, coalIterator.image->getPath(), targetImage->getPath());
+ }
+ }
+ imageBeingFixedUp->setWeakSymbolsBound(imageIndexes[i]);
+ }
+ }
+ else
+#endif // TARGET_OS_OSX
+ {
+ // make symbol iterators for each
+ ImageLoader::CoalIterator iterators[count];
+ ImageLoader::CoalIterator* sortedIts[count];
+ for(int i=0; i < count; ++i) {
+ imagesNeedingCoalescing[i]->initializeCoalIterator(iterators[i], i, imageIndexes[i]);
+ sortedIts[i] = &iterators[i];
+ if ( context.verboseWeakBind )
+ dyld::log("dyld: weak bind load order %d/%d for %s\n", i, count, imagesNeedingCoalescing[i]->getIndexedPath(imageIndexes[i]));