Loading...
src/ImageLoader.cpp dyld-852 dyld-733.6
--- dyld/dyld-852/src/ImageLoader.cpp
+++ dyld/dyld-733.6/src/ImageLoader.cpp
@@ -84,9 +84,6 @@
 	fPathOwnedByImage(false), fIsReferencedDownward(false), 
 	fWeakSymbolsBound(false)
 {
-#if __x86_64__
-	fAotPath = NULL;
-#endif
 	if ( fPath != NULL )
 		fPathHash = hash(fPath);
 	if ( libCount > 512 )
@@ -106,10 +103,6 @@
 		delete [] fRealPath;
 	if ( fPathOwnedByImage && (fPath != NULL) ) 
 		delete [] fPath;
-#if __x86_64__
-	if ( fAotPath != NULL )
-		delete [] fAotPath;
-#endif
 }
 
 void ImageLoader::setFileInfo(dev_t device, ino_t inode, time_t modDate)
@@ -396,7 +389,7 @@
 		//dyld::log("    interposedAddress: replacee=0x%08llX, replacement=0x%08llX, neverImage=%p, onlyImage=%p, inImage=%p\n", 
 		//				(uint64_t)it->replacee, (uint64_t)it->replacement,  it->neverImage, it->onlyImage, inImage);
 		// replace all references to 'replacee' with 'replacement'
-		if ( (address == it->replacee) && (it->neverImage != inImage) && ((it->onlyImage == NULL) || (it->onlyImage == inImage)) ) {
+		if ( (address == it->replacee) && (inImage != it->neverImage) && ((it->onlyImage == NULL) || (inImage == it->onlyImage)) ) {
 			if ( context.verboseInterposing ) {
 				dyld::log("dyld interposing: replace 0x%lX with 0x%lX\n", it->replacee, it->replacement);
 			}
@@ -409,14 +402,12 @@
 void ImageLoader::applyInterposingToDyldCache(const LinkContext& context) {
 	if (!context.dyldCache)
 		return;
+#if !__arm64e__ // until arm64e cache builder sets builtFromChainedFixups
 	if (!context.dyldCache->header.builtFromChainedFixups)
 		return;
+#endif
 	if (fgInterposingTuples.empty())
 		return;
-
-	// make the cache writable for this block
-	DyldSharedCache::DataConstScopedWriter patcher(context.dyldCache, mach_task_self(), (context.verboseMapping ? &dyld::log : nullptr));
-
 	// For each of the interposed addresses, see if any of them are in the shared cache.  If so, find
 	// that image and apply its patch table to all uses.
 	uintptr_t cacheStart = (uintptr_t)context.dyldCache;
@@ -484,7 +475,7 @@
 // <rdar://problem/29099600> dyld should tell the kernel when it is doing root fix-ups
 void ImageLoader::vmAccountingSetSuspended(const LinkContext& context, bool suspend)
 {
-#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
+#if __arm__ || __arm64__
 	static bool sVmAccountingSuspended = false;
 	if ( suspend == sVmAccountingSuspended )
 		return;
@@ -519,7 +510,7 @@
 
 	uint64_t t1 = mach_absolute_time();
 	context.clearAllDepths();
-	this->updateDepth(context.imageCount());
+	this->recursiveUpdateDepth(context.imageCount());
 
 	__block uint64_t t2, t3, t4, t5;
 	{
@@ -639,8 +630,7 @@
 			}
 		}
 		// bind lazies in this image
-		DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
-		this->doBindJustLazies(context, patcher);
+		this->doBindJustLazies(context);
 	}
 }
 
@@ -674,19 +664,7 @@
 	
 }
 
-unsigned int ImageLoader::updateDepth(unsigned int maxDepth)
-{
-	STACK_ALLOC_ARRAY(ImageLoader*, danglingUpwards, maxDepth);
-	unsigned int depth = this->recursiveUpdateDepth(maxDepth, danglingUpwards);
-	for (auto& danglingUpward : danglingUpwards) {
-		if ( danglingUpward->fDepth != 0)
-			continue;
-		danglingUpward->recursiveUpdateDepth(maxDepth, danglingUpwards);
-	}
-	return depth;
-}
-
-unsigned int ImageLoader::recursiveUpdateDepth(unsigned int maxDepth, dyld3::Array<ImageLoader*>& danglingUpwards)
+unsigned int ImageLoader::recursiveUpdateDepth(unsigned int maxDepth)
 {
 	// the purpose of this phase is to make the images sortable such that 
 	// in a sort list of images, every image that an image depends on
@@ -699,29 +677,17 @@
 		unsigned int minDependentDepth = maxDepth;
 		for(unsigned int i=0; i < libraryCount(); ++i) {
 			ImageLoader* dependentImage = libImage(i);
-			if ( dependentImage != NULL ) {
-				if ( libIsUpward(i) ) {
-					if ( dependentImage->fDepth == 0) {
-						if ( !danglingUpwards.contains(dependentImage) )
-							danglingUpwards.push_back(dependentImage);
-					}
-				} else {
-					unsigned int d = dependentImage->recursiveUpdateDepth(maxDepth, danglingUpwards);
-					if ( d < minDependentDepth )
-						minDependentDepth = d;
-				}
-			}
-			// <rdar://problem/60878811> make sure need to re-bind propagates up
-			if ( dependentImage != NULL ) {
-				if ( fAllLibraryChecksumsAndLoadAddressesMatch && !dependentImage->fAllLibraryChecksumsAndLoadAddressesMatch ) {
-					fAllLibraryChecksumsAndLoadAddressesMatch = false;
-				}
-			}
-		}
+			if ( (dependentImage != NULL) && !libIsUpward(i) ) {
+				unsigned int d = dependentImage->recursiveUpdateDepth(maxDepth);
+				if ( d < minDependentDepth )
+					minDependentDepth = d;
+			}
+		}
+	
 		// make me less deep then all my dependents
 		fDepth = minDependentDepth - 1;
-
-	}
+	}
+	
 	return fDepth;
 }
 
@@ -833,21 +799,22 @@
 		// tell each to load its dependents
 		for(unsigned int i=0; i < libraryCount(); ++i) {
 			ImageLoader* dependentImage = libImage(i);
-			if ( dependentImage != NULL ) {
+			if ( dependentImage != NULL ) {	
 				dependentImage->recursiveLoadLibraries(context, preflightOnly, thisRPaths, libraryInfos[i].name);
 			}
 		}
+		
 		// do deep prebind check
 		if ( fAllLibraryChecksumsAndLoadAddressesMatch ) {
 			for(unsigned int i=0; i < libraryCount(); ++i){
 				ImageLoader* dependentImage = libImage(i);
-				if ( dependentImage != NULL ) {
+				if ( dependentImage != NULL ) {	
 					if ( !dependentImage->allDependentLibrariesAsWhenPreBound() )
 						fAllLibraryChecksumsAndLoadAddressesMatch = false;
 				}
 			}
 		}
-
+		
 		// free rpaths (getRPaths() malloc'ed each string)
 		for(std::vector<const char*>::iterator it=rpathsFromThisImage.begin(); it != rpathsFromThisImage.end(); ++it) {
 			const char* str = *it;
@@ -945,11 +912,11 @@
 
 void ImageLoader::recursiveBindWithAccounting(const LinkContext& context, bool forceLazysBound, bool neverUnload)
 {
-	this->recursiveBind(context, forceLazysBound, neverUnload, nullptr);
+	this->recursiveBind(context, forceLazysBound, neverUnload);
 	vmAccountingSetSuspended(context, false);
 }
 
-void ImageLoader::recursiveBind(const LinkContext& context, bool forceLazysBound, bool neverUnload, const ImageLoader* parent)
+void ImageLoader::recursiveBind(const LinkContext& context, bool forceLazysBound, bool neverUnload)
 {
 	// Normally just non-lazy pointers are bound immediately.
 	// The exceptions are:
@@ -963,15 +930,11 @@
 			// bind lower level libraries first
 			for(unsigned int i=0; i < libraryCount(); ++i) {
 				ImageLoader* dependentImage = libImage(i);
-				if ( dependentImage != NULL )  {
-					const ImageLoader* reExportParent = nullptr;
-					if ( libReExported(i) )
-						reExportParent = this;
-					dependentImage->recursiveBind(context, forceLazysBound, neverUnload, reExportParent);
-				}
+				if ( dependentImage != NULL )
+					dependentImage->recursiveBind(context, forceLazysBound, neverUnload);
 			}
 			// bind this image
-			this->doBind(context, forceLazysBound, parent);
+			this->doBind(context, forceLazysBound);	
 			// mark if lazys are also bound
 			if ( forceLazysBound || this->usablePrebinding(context) )
 				fAllLazyPointersBound = true;
@@ -1049,12 +1012,7 @@
 			new (&context.weakDefMap) dyld3::Map<const char*, std::pair<const ImageLoader*, uintptr_t>, ImageLoader::HashCString, ImageLoader::EqualCString>();
 			context.weakDefMapInitialized = true;
 		}
-
-		// 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
+#if __MAC_OS_X_VERSION_MIN_REQUIRED
 	  // 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
@@ -1074,8 +1032,8 @@
 
 				  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;
+				  ma->forEachWeakDef(diag, ^(const char *symbolName, uintptr_t imageOffset, bool isFromExportTrie) {
+					  uintptr_t targetAddr = (uintptr_t)ma + imageOffset;
 					  if ( isFromExportTrie ) {
 						  // Avoid duplicating the string if we already have the symbol name
 						  if ( context.weakDefMap.find(symbolName) != context.weakDefMap.end() )
@@ -1097,8 +1055,8 @@
 				  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;
+			  ma->forEachWeakDef(diag, ^(const char *symbolName, uintptr_t imageOffset, bool isFromExportTrie) {
+				  uintptr_t targetAddr = (uintptr_t)ma + imageOffset;
 				  if ( isFromExportTrie ) {
 					  // Avoid duplicating the string if we already have the symbol name
 					  if ( context.weakDefMap.find(symbolName) != context.weakDefMap.end() )
@@ -1153,8 +1111,6 @@
 					}
 				}
 				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()) {
@@ -1179,7 +1135,7 @@
 		}
 	  }
 	  else
-#endif // TARGET_OS_OSX
+#endif // __MAC_OS_X_VERSION_MIN_REQUIRED
 	  {
 		// make symbol iterators for each
 		ImageLoader::CoalIterator iterators[count];
@@ -1259,11 +1215,8 @@
 											nameToCoalesce, iterators[i].image->getIndexedShortName((unsigned)iterators[i].imageIndex),
 											targetAddr, targetImage->getIndexedShortName(targetImageIndex));
 							}
-							if ( ! iterators[i].image->weakSymbolsBound(imageIndexes[i]) ) {
-								if ( iterators[i].image->inSharedCache() )
-									patcher.makeWriteable();
+							if ( ! iterators[i].image->weakSymbolsBound(imageIndexes[i]) )
 								iterators[i].image->updateUsesCoalIterator(iterators[i], targetAddr, targetImage, targetImageIndex, context);
-							}
 							iterators[i].symbolMatches = false; 
 						}
 					}
@@ -1289,7 +1242,7 @@
 				// but if main executable has non-weak override of operator new or delete it needs is handled here
 				for (const char* weakSymbolName : sTreatAsWeak) {
 					const ImageLoader* dummy;
-					imagesNeedingCoalescing[i]->resolveWeak(context, weakSymbolName, true, false, &dummy, patcher);
+					imagesNeedingCoalescing[i]->resolveWeak(context, weakSymbolName, true, false, &dummy);
 				}
 			}
 #if __arm64e__
@@ -1302,7 +1255,7 @@
 				while ( !coaler.done ) {
 					const ImageLoader* dummy;
 					// a side effect of resolveWeak() is to patch cache
-					imagesNeedingCoalescing[i]->resolveWeak(context, coaler.symbolName, true, false, &dummy, patcher);
+					imagesNeedingCoalescing[i]->resolveWeak(context, coaler.symbolName, true, false, &dummy);
 					imagesNeedingCoalescing[i]->incrementCoalIterator(coaler);
 				}
 			}
@@ -1346,11 +1299,7 @@
 
 	// don't need to do any coalescing if only one image has overrides, or all have already been done
 	if ( (countOfImagesWithWeakDefinitionsNotInSharedCache > 0) && (countNotYetWeakBound > 0) ) {
-		// 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
+#if __MAC_OS_X_VERSION_MIN_REQUIRED
 	  // only do alternate algorithm for dlopen(). Use traditional algorithm for launch
 	  if ( !context.linkingMainExecutable ) {
 		// for all images that need weak binding
@@ -1402,8 +1351,6 @@
 					}
 				}
 				if ( (targetAddr != 0) && (coalIterator.image != targetImage) ) {
-					if ( coalIterator.image->inSharedCache() )
-						patcher.makeWriteable();
 					coalIterator.image->updateUsesCoalIterator(coalIterator, targetAddr, (ImageLoader*)targetImage, 0, context);
 					if ( context.verboseWeakBind )
 						dyld::log("dyld:     adjusting uses of %s in %s to use definition from %s\n", nameToCoalesce, coalIterator.image->getPath(), targetImage->getPath());
@@ -1413,7 +1360,7 @@
 		}
 	  }
 	  else
-#endif // TARGET_OS_OSX
+#endif // __MAC_OS_X_VERSION_MIN_REQUIRED
 	  {
 		// make symbol iterators for each
 		ImageLoader::CoalIterator iterators[count];
@@ -1493,11 +1440,8 @@
 											nameToCoalesce, iterators[i].image->getIndexedShortName((unsigned)iterators[i].imageIndex),
 											targetAddr, targetImage->getIndexedShortName(targetImageIndex));
 							}
-							if ( ! iterators[i].image->weakSymbolsBound(imageIndexes[i]) ) {
-								if ( iterators[i].image->inSharedCache() )
-									patcher.makeWriteable();
+							if ( ! iterators[i].image->weakSymbolsBound(imageIndexes[i]) )
 								iterators[i].image->updateUsesCoalIterator(iterators[i], targetAddr, targetImage, targetImageIndex, context);
-							}
 							iterators[i].symbolMatches = false;
 						}
 					}
@@ -1515,21 +1459,20 @@
 				// but if main executable has non-weak override of operator new or delete it needs is handled here
 				for (const char* weakSymbolName : sTreatAsWeak) {
 					const ImageLoader* dummy;
-					imagesNeedingCoalescing[i]->resolveWeak(context, weakSymbolName, true, false, &dummy, patcher);
+					imagesNeedingCoalescing[i]->resolveWeak(context, weakSymbolName, true, false, &dummy);
 				}
 			}
 #if __arm64e__
 			else {
 				// support traditional arm64 app on an arm64e device
 				// look for weak def symbols in this image which may override the cache
-				patcher.makeWriteable();
 				ImageLoader::CoalIterator coaler;
 				imagesNeedingCoalescing[i]->initializeCoalIterator(coaler, i, 0);
 				imagesNeedingCoalescing[i]->incrementCoalIterator(coaler);
 				while ( !coaler.done ) {
 					const ImageLoader* dummy;
 					// a side effect of resolveWeak() is to patch cache
-					imagesNeedingCoalescing[i]->resolveWeak(context, coaler.symbolName, true, false, &dummy, patcher);
+					imagesNeedingCoalescing[i]->resolveWeak(context, coaler.symbolName, true, false, &dummy);
 					imagesNeedingCoalescing[i]->incrementCoalIterator(coaler);
 				}
 			}
@@ -1940,25 +1883,11 @@
 		bit += 7;
 	} while (byte & 0x80);
 	// sign extend negative numbers
-	if ( ((byte & 0x40) != 0) && (bit < 64) )
+	if ( (byte & 0x40) != 0 )
 		result |= (~0ULL) << bit;
 	return (intptr_t)result;
 }
 
-void ImageLoader::forEachReExportDependent( void (^callback)(const ImageLoader*, bool& stop)) const
-{
-	bool stop = false;
-	for (unsigned int i=0; i < libraryCount(); ++i) {
-		if ( libReExported(i) ) {
-			if ( ImageLoader* dependentImage = libImage(i) ) {
-				callback(dependentImage, stop);
-			}
-		}
-		if (stop)
-			break;
-	}
-}
-
 
 VECTOR_NEVER_DESTRUCTED_IMPL(ImageLoader::InterposeTuple);
 VECTOR_NEVER_DESTRUCTED_IMPL(ImagePair);