Loading...
src/ImageLoaderMachOClassic.cpp dyld-360.21 dyld-852
--- dyld/dyld-360.21/src/ImageLoaderMachOClassic.cpp
+++ dyld/dyld-852/src/ImageLoaderMachOClassic.cpp
@@ -43,6 +43,7 @@
 #include <sys/sysctl.h>
 #include <libkern/OSAtomic.h>
 #include <libkern/OSCacheControl.h>
+#include <mach-o/dyld_images.h>
 
 #if __x86_64__
 	#include <mach-o/x86_64/reloc.h>
@@ -52,7 +53,6 @@
 #endif
 
 #include "ImageLoaderMachOClassic.h"
-#include "mach-o/dyld_images.h"
 
 // in dyldStartup.s
 extern "C" void stub_binding_helper_i386_old();
@@ -168,11 +168,6 @@
 		// make sure path is stable before recording in dyld_all_image_infos
 		image->setMapped(context);
 
-		// pre-fetch content of __DATA segment for faster launches
-		// don't do this on prebound images or if prefetching is disabled
-        if ( !context.preFetchDisabled && !image->isPrebindable())
-			image->preFetchDATA(fd, offsetInFat, context);
-
 	}
 	catch (...) {
 		// ImageLoader::setMapped() can throw an exception to block loading of image
@@ -336,41 +331,6 @@
 	fDynamicInfo = dynSym;
 }
 
-void ImageLoaderMachOClassic::prefetchLINKEDIT(const LinkContext& context)
-{
-	// always prefetch a subrange of __LINKEDIT pages
-	uintptr_t symbolTableStart = (uintptr_t)fSymbolTable;
-	uintptr_t stringTableStart = (uintptr_t)fStrings;
-	uintptr_t start;
-	// if image did not load at preferred address
-	if ( segPreferredLoadAddress(0) != (uintptr_t)fMachOData ) {
-		// local relocations will be processed, so start pre-fetch at local symbols
-		start = (uintptr_t)fMachOData + fDynamicInfo->locreloff;
-	}
-	else {
-		// otherwise start pre-fetch at global symbols section of symbol table
-		start = symbolTableStart + fDynamicInfo->iextdefsym * sizeof(macho_nlist);
-	}
-	// prefetch ends at end of last undefined string in string pool
-	uintptr_t end = stringTableStart;
-	if ( fDynamicInfo->nundefsym != 0 )
-		end += fSymbolTable[fDynamicInfo->iundefsym+fDynamicInfo->nundefsym-1].n_un.n_strx;
-	else if ( fDynamicInfo->nextdefsym != 0 )
-		end += fSymbolTable[fDynamicInfo->iextdefsym+fDynamicInfo->nextdefsym-1].n_un.n_strx;
-		
-	// round to whole pages
-	start = dyld_page_trunc(start);
-	end = dyld_page_round(end);
-	
-	// skip if there is only one page
-	if ( (end-start) > dyld_page_size ) {
-		madvise((void*)start, end-start, MADV_WILLNEED);
-		fgTotalBytesPreFetched += (end-start);
-		if ( context.verboseMapping ) {
-			dyld::log("%18s prefetching 0x%0lX -> 0x%0lX\n", "__LINKEDIT", start, end-1);
-		}
-	}
-}
 
 
 #if SPLIT_SEG_DYLIB_SUPPORT	
@@ -436,14 +396,14 @@
 	while ( ! foundRoom ) {
 		foundRoom = true;
 		for(unsigned int i=0; i < regionCount; ++i) {
-			vm_address_t addr = nextAltLoadAddress + regions[i].sfm_address - regions[0].sfm_address;
-			vm_size_t size = regions[i].sfm_size ;
+			vm_address_t addr = (vm_address_t)(nextAltLoadAddress + regions[i].sfm_address - regions[0].sfm_address);
+			vm_size_t size = (vm_size_t)regions[i].sfm_size ;
 			r = vm_allocate(mach_task_self(), &addr, size, false /*only this range*/);
 			if ( 0 != r ) {
 				// no room here, deallocate what has succeeded so far
 				for(unsigned int j=0; j < i; ++j) {
-					vm_address_t addr = nextAltLoadAddress + regions[j].sfm_address - regions[0].sfm_address;
-					vm_size_t size = regions[j].sfm_size ;
+					addr = (vm_address_t)(nextAltLoadAddress + regions[j].sfm_address - regions[0].sfm_address);
+					size = (vm_size_t)(regions[j].sfm_size);
 					(void)vm_deallocate(mach_task_self(), addr, size);
 				}
 				nextAltLoadAddress += 0x00100000;  // skip ahead 1MB and try again
@@ -459,7 +419,7 @@
 	}
 	
 	// map in each region
-	uintptr_t slide = nextAltLoadAddress - regions[0].sfm_address;
+	uintptr_t slide = (uintptr_t)(nextAltLoadAddress - regions[0].sfm_address);
 	this->setSlide(slide);
 	for(unsigned int i=0; i < regionCount; ++i) {
 		if ( ((regions[i].sfm_init_prot & VM_PROT_ZF) != 0) || (regions[i].sfm_size == 0) ) {
@@ -467,7 +427,7 @@
 		}
 		else {
 			void* mmapAddress = (void*)(uintptr_t)(regions[i].sfm_address + slide);
-			size_t size = regions[i].sfm_size;
+			size_t size = (size_t)regions[i].sfm_size;
 			int protection = 0;
 			if ( regions[i].sfm_init_prot & VM_PROT_EXECUTE )
 				protection   |= PROT_EXEC;
@@ -601,11 +561,13 @@
 							return true;
 						if ( context.imageSuffix != NULL ) {
 							// when DYLD_IMAGE_SUFFIX is used, lastSlash string needs imageSuffix removed from end
-							char reexportAndSuffix[strlen(context.imageSuffix)+strlen(exportThruName)+1];
-							strcpy(reexportAndSuffix, exportThruName);
-							strcat(reexportAndSuffix, context.imageSuffix);
-							if ( strcmp(&lastSlash[1], reexportAndSuffix) == 0 )
-								return true;
+							for(const char* const* suffix = context.imageSuffix; *suffix != NULL; ++suffix) {
+								char reexportAndSuffix[strlen(*suffix)+strlen(exportThruName)+1];
+								strcpy(reexportAndSuffix, exportThruName);
+								strcat(reexportAndSuffix, *suffix);
+								if ( strcmp(&lastSlash[1], reexportAndSuffix) == 0 )
+									return true;
+							}
 						}
 					}
 				}
@@ -647,11 +609,13 @@
 									return true;
 								if ( context.imageSuffix != NULL ) {
 									// when DYLD_IMAGE_SUFFIX is used, childLeafName string needs imageSuffix removed from end
-									char aSubLibNameAndSuffix[strlen(context.imageSuffix)+strlen(aSubLibName)+1];
-									strcpy(aSubLibNameAndSuffix, aSubLibName);
-									strcat(aSubLibNameAndSuffix, context.imageSuffix);
-									if ( strcmp(aSubLibNameAndSuffix, childLeafName) == 0 )
-										return true;
+									for(const char* const* suffix = context.imageSuffix; *suffix != NULL; ++suffix) {
+										char aSubLibNameAndSuffix[strlen(*suffix)+strlen(aSubLibName)+1];
+										strcpy(aSubLibNameAndSuffix, aSubLibName);
+										strcat(aSubLibNameAndSuffix, *suffix);
+										if ( strcmp(aSubLibNameAndSuffix, childLeafName) == 0 )
+											return true;
+									}
 								}
 							}
 							break;
@@ -680,11 +644,13 @@
 									return true;
 								if ( context.imageSuffix != NULL ) {
 									// when DYLD_IMAGE_SUFFIX is used, lastSlash string needs imageSuffix removed from end
-									char umbrellaAndSuffix[strlen(context.imageSuffix)+strlen(aSubUmbrellaName)+1];
-									strcpy(umbrellaAndSuffix, aSubUmbrellaName);
-									strcat(umbrellaAndSuffix, context.imageSuffix);
-									if ( strcmp(umbrellaAndSuffix, &lastSlash[1]) == 0 )
-										return true;
+									for(const char* const* suffix = context.imageSuffix; *suffix != NULL; ++suffix) {
+										char umbrellaAndSuffix[strlen(*suffix)+strlen(aSubUmbrellaName)+1];
+										strcpy(umbrellaAndSuffix, aSubUmbrellaName);
+										strcat(umbrellaAndSuffix, *suffix);
+										if ( strcmp(umbrellaAndSuffix, &lastSlash[1]) == 0 )
+											return true;
+									}
 								}
 							}
 							break;
@@ -728,7 +694,7 @@
 {
 	// loop through all local (internal) relocation records looking for pre-bound-lazy-pointer values
 	const uintptr_t relocBase = this->getRelocBase();
-	register const uintptr_t slide = this->fSlide;
+    const uintptr_t slide = this->fSlide;
 	const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->locreloff]);
 	const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nlocrel];
 	for (const relocation_info* reloc=relocsStart; reloc < relocsEnd; ++reloc) {
@@ -757,16 +723,11 @@
 
 
 
-void ImageLoaderMachOClassic::rebase(const LinkContext& context)
+void ImageLoaderMachOClassic::rebase(const LinkContext& context, uintptr_t slide)
 {
 	CRSetCrashLogMessage2(this->getPath());
-	register const uintptr_t slide = this->fSlide;
 	const uintptr_t relocBase = this->getRelocBase();
-	
-	// prefetch any LINKEDIT pages needed
-	if ( !context.preFetchDisabled && !this->isPrebindable())
-		this->prefetchLINKEDIT(context);
-	
+
 	// loop through all local (internal) relocation records
 	const relocation_info* const relocsStart = (struct relocation_info*)(&fLinkEditBase[fDynamicInfo->locreloff]);
 	const relocation_info* const relocsEnd = &relocsStart[fDynamicInfo->nlocrel];
@@ -933,7 +894,7 @@
 }
 
 
-const ImageLoader::Symbol* ImageLoaderMachOClassic::findExportedSymbol(const char* name, const ImageLoader** foundIn) const
+const ImageLoader::Symbol* ImageLoaderMachOClassic::findShallowExportedSymbol(const char* name, const ImageLoader** foundIn) const
 {
 	const struct macho_nlist* sym = NULL;
 	if ( fDynamicInfo->tocoff == 0 )
@@ -1043,7 +1004,7 @@
 }
 
 uintptr_t ImageLoaderMachOClassic::resolveUndefined(const LinkContext& context, const struct macho_nlist* undefinedSymbol, 
-										bool twoLevel, bool dontCoalesce, const ImageLoader** foundIn)
+										bool twoLevel, bool dontCoalesce, bool runResolver, const ImageLoader** foundIn)
 {
 	++fgTotalBindSymbolsResolved;
 	const char* symbolName = &fStrings[undefinedSymbol->n_un.n_strx];
@@ -1069,7 +1030,7 @@
 		// if a bundle is loaded privately the above will not find its exports
 		if ( this->isBundle() && this->hasHiddenExports() ) {
 			// look in self for needed symbol
-			sym = this->findExportedSymbol(symbolName, foundIn);
+			sym = this->findShallowExportedSymbol(symbolName, foundIn);
 			if ( sym != NULL )
 				return (*foundIn)->getExportedSymbolAddress(sym, context, this);
 		}
@@ -1084,7 +1045,7 @@
 		// symbol requires searching images with coalesced symbols (not done during prebinding)
 		if ( !context.prebinding && !dontCoalesce && (symbolIsWeakReference(undefinedSymbol) || symbolIsWeakDefinition(undefinedSymbol)) ) {
 			const Symbol* sym;
-			if ( context.coalescedExportFinder(symbolName, &sym, foundIn) ) {
+			if ( context.coalescedExportFinder(symbolName, &sym, foundIn, nullptr) ) {
 				if ( *foundIn != this )
 					context.addDynamicReference(this, const_cast<ImageLoader*>(*foundIn));
 				return (*foundIn)->getExportedSymbolAddress(sym, context, this);
@@ -1141,12 +1102,12 @@
 			//dyld::log("resolveUndefined(%s) in %s\n", symbolName, this->getPath());
 			throw "symbol not found";
 		}
-				
-		const Symbol* sym = target->findExportedSymbol(symbolName, true, foundIn);
-		if ( sym!= NULL ) {
-			return (*foundIn)->getExportedSymbolAddress(sym, context, this);
-		}
-		else if ( (undefinedSymbol->n_type & N_PEXT) != 0 ) {
+
+		uintptr_t address;
+		if ( target->findExportedSymbolAddress(context, symbolName, this, ord, runResolver, foundIn, &address) )
+			return address;
+
+		if ( (undefinedSymbol->n_type & N_PEXT) != 0 ) {
 			// don't know why the static linker did not eliminate the internal reference to a private extern definition
 			*foundIn = this;
 			return this->getSymbolAddress(undefinedSymbol, context, false);
@@ -1269,11 +1230,11 @@
 								// range of global symbols.  To handle that case we do the coalesing now.
 								dontCoalesce = false;
 							}
-							symbolAddr = this->resolveUndefined(context, undefinedSymbol, twoLevel, dontCoalesce, &image);
+							symbolAddr = this->resolveUndefined(context, undefinedSymbol, twoLevel, dontCoalesce, false, &image);
 							lastUndefinedSymbol = undefinedSymbol;
 							symbolAddrCached = false;
 						}
-						if ( context.verboseBind ) {
+						if ( context.verboseBind && (undefinedSymbol != NULL) ) {
 							const char *path = NULL;
 							if ( image != NULL ) {
 								path = image->getShortName();
@@ -1390,7 +1351,8 @@
 	throw "compressed LINKEDIT lazy binder called with classic LINKEDIT";
 }
 
-uintptr_t ImageLoaderMachOClassic::doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context)
+uintptr_t ImageLoaderMachOClassic::doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context,
+													DyldSharedCache::DataConstLazyScopedWriter& patcher)
 {
 	// scan for all lazy-pointer sections
 	const bool twoLevel = this->usesTwoLevelNameSpace();
@@ -1435,7 +1397,7 @@
 						if ( symbolIndex != INDIRECT_SYMBOL_ABS && symbolIndex != INDIRECT_SYMBOL_LOCAL ) {
 							const char* symbolName = &fStrings[fSymbolTable[symbolIndex].n_un.n_strx];
 							const ImageLoader* image = NULL;
-							uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], twoLevel, false, &image);
+							uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], twoLevel, false, true, &image);
 							symbolAddr = this->bindIndirectSymbol(lazyPointer, sect, symbolName, symbolAddr, image,  context);
 							++fgTotalLazyBindFixups;
 							return symbolAddr;
@@ -1451,7 +1413,7 @@
 
 
 
-void ImageLoaderMachOClassic::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder)
+void ImageLoaderMachOClassic::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder, unsigned)
 {
 	it.image = this;
 	it.symbolName = " ";
@@ -1542,7 +1504,7 @@
 }
 
 
-void ImageLoaderMachOClassic::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, const LinkContext& context)
+void ImageLoaderMachOClassic::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, unsigned targetIndex, const LinkContext& context)
 {
 	// flat_namespace images with classic LINKEDIT do not need late coalescing.
 	// They still need to be iterated becuase they may implement
@@ -1644,7 +1606,7 @@
 			if ( reloc->r_pcrel ) 
 				type = BIND_TYPE_TEXT_PCREL32;
 		#endif
-			this->bindLocation(context, (uintptr_t)location, value, targetImage, type, symbolName, addend, "weak ");
+			this->bindLocation(context, this->imageBaseAddress(), (uintptr_t)location, value, type, symbolName, addend, this->getPath(), targetImage ? targetImage->getPath() : NULL, "weak ", NULL, fSlide);
 			boundSomething = true;
 		}
 	}
@@ -1667,6 +1629,7 @@
 						if ( ((sect->flags & S_ATTR_SELF_MODIFYING_CODE) ==0) || (sect->reserved2 != 5) )
 							continue;
 						elementSize = 5;
+                        [[clang::fallthrough]];
 				#endif
 					case S_NON_LAZY_SYMBOL_POINTERS:
 					case S_LAZY_SYMBOL_POINTERS:
@@ -1798,9 +1761,9 @@
 									// range of global symbols.  To handle that case we do the coalesing now.
 									dontCoalesce = false;
 								}
-								uintptr_t symbolAddr = resolveUndefined(context, sym, twoLevel, dontCoalesce, &image);
+								uintptr_t symbolAddr = resolveUndefined(context, sym, twoLevel, dontCoalesce, false, &image);
 								// update pointer
-								symbolAddr = this->bindIndirectSymbol((uintptr_t*)ptrToBind, sect, &fStrings[sym->n_un.n_strx], symbolAddr, image,  context);
+								this->bindIndirectSymbol((uintptr_t*)ptrToBind, sect, &fStrings[sym->n_un.n_strx], symbolAddr, image,  context);
 								// update stats
 								++fgTotalBindFixups;
 							}
@@ -1853,7 +1816,7 @@
 										const char* symbolName = &fStrings[fSymbolTable[symbolIndex].n_un.n_strx];
 										const ImageLoader* image = NULL;
 										try {
-											uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], this->usesTwoLevelNameSpace(), false, &image);
+											uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], this->usesTwoLevelNameSpace(), false, false, &image);
 											symbolAddr = this->bindIndirectSymbol((uintptr_t*)entry, sect, symbolName, symbolAddr, image, context);
 											++fgTotalBindFixups;
 											uint32_t rel32 = symbolAddr - (((uint32_t)entry)+5);
@@ -1890,7 +1853,7 @@
 #endif // __i386__
 
 
-void ImageLoaderMachOClassic::doBind(const LinkContext& context, bool forceLazysBound)
+void ImageLoaderMachOClassic::doBind(const LinkContext& context, bool forceLazysBound, const ImageLoader* reExportParent)
 {
 	CRSetCrashLogMessage2(this->getPath());
 #if __i386__
@@ -1932,7 +1895,7 @@
 	CRSetCrashLogMessage2(NULL);
 }
 
-void ImageLoaderMachOClassic::doBindJustLazies(const LinkContext& context)
+void ImageLoaderMachOClassic::doBindJustLazies(const LinkContext& context, DyldSharedCache::DataConstLazyScopedWriter& patcher)
 {
 	// some API called requested that all lazy pointers in this image be force bound
 	this->bindIndirectSymbolPointers(context, false, true);
@@ -2034,14 +1997,14 @@
 							const size_t pointerCount = sect->size / sizeof(uintptr_t);
 							uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + fSlide);
 							for (size_t pointerIndex=0; pointerIndex < pointerCount; ++pointerIndex) {
-								for(size_t i=0; i < context.dynamicInterposeCount; ++i) {
+								for(size_t j=0; j < context.dynamicInterposeCount; ++j) {
 									// replace all references to 'replacee' with 'replacement'
-									if ( symbolPointers[pointerIndex] == (uintptr_t)context.dynamicInterposeArray[i].replacee ) {
+									if ( symbolPointers[pointerIndex] == (uintptr_t)context.dynamicInterposeArray[j].replacee ) {
 										if ( context.verboseInterposing ) {
 											dyld::log("dyld: dynamic interposing: at %p replace %p with %p in %s\n", 
-												&symbolPointers[pointerIndex], context.dynamicInterposeArray[i].replacee, context.dynamicInterposeArray[i].replacement, this->getPath());
+												&symbolPointers[pointerIndex], context.dynamicInterposeArray[j].replacee, context.dynamicInterposeArray[j].replacement, this->getPath());
 										}
-										symbolPointers[pointerIndex] = (uintptr_t)context.dynamicInterposeArray[i].replacement;
+										symbolPointers[pointerIndex] = (uintptr_t)context.dynamicInterposeArray[j].replacement;
 									}
 								}
 							}