Loading...
src/ImageLoaderMachOClassic.cpp dyld-640.2 dyld-353.2.3
--- dyld/dyld-640.2/src/ImageLoaderMachOClassic.cpp
+++ dyld/dyld-353.2.3/src/ImageLoaderMachOClassic.cpp
@@ -97,8 +97,7 @@
 	// for PIE record end of program, to know where to start loading dylibs
 	if ( slide != 0 )
 		fgNextPIEDylibAddress = (uintptr_t)image->getEnd();
-
-	image->disableCoverageCheck();
+	
 	image->instantiateFinish(context);
 	image->setMapped(context);
 
@@ -127,7 +126,7 @@
 }
 
 // create image by mapping in a mach-o file
-ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateFromFile(const char* path, int fd, const uint8_t* fileData, size_t lenFileData,
+ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateFromFile(const char* path, int fd, const uint8_t* fileData, 
 															uint64_t offsetInFat, uint64_t lenInFat, const struct stat& info, 
 															unsigned int segCount, unsigned int libCount, 
 															const struct linkedit_data_command* codeSigCmd, const LinkContext& context)
@@ -140,9 +139,6 @@
 		// if this image is code signed, let kernel validate signature before mapping any pages from image
 		image->loadCodeSignature(codeSigCmd, fd, offsetInFat, context);
 		
-		// Validate that first data we read with pread actually matches with code signature
-		image->validateFirstPages(codeSigCmd, fd, fileData, lenFileData, offsetInFat, context);
-
 		// mmap segments
 		image->mapSegmentsClassic(fd, offsetInFat, lenInFat, info.st_size, context);
 
@@ -196,7 +192,6 @@
 		// remember this is from shared cache and cannot be unloaded
 		image->fInSharedCache = true;
 		image->setNeverUnload();
-		image->disableCoverageCheck();
 
 		// segments already mapped in cache
 		if ( context.verboseMapping ) {
@@ -235,8 +230,6 @@
 		// for compatibility, never unload dylibs loaded from memory
 		image->setNeverUnload();
 
-		image->disableCoverageCheck();
-
 		// bundle loads need path copied
 		if ( moduleName != NULL ) 
 			image->setPath(moduleName);
@@ -280,7 +273,7 @@
 void ImageLoaderMachOClassic::instantiateFinish(const LinkContext& context)
 {
 	// now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
-	this->parseLoadCmds(context);
+	this->parseLoadCmds();
 }
 
 ImageLoaderMachOClassic::~ImageLoaderMachOClassic()
@@ -436,14 +429,14 @@
 	while ( ! foundRoom ) {
 		foundRoom = true;
 		for(unsigned int i=0; i < regionCount; ++i) {
-			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 ;
+			vm_address_t addr = nextAltLoadAddress + regions[i].sfm_address - regions[0].sfm_address;
+			vm_size_t size = 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) {
-					addr = (vm_address_t)(nextAltLoadAddress + regions[j].sfm_address - regions[0].sfm_address);
-					size = (vm_size_t)(regions[j].sfm_size);
+					vm_address_t addr = nextAltLoadAddress + regions[j].sfm_address - regions[0].sfm_address;
+					vm_size_t size = regions[j].sfm_size ;
 					(void)vm_deallocate(mach_task_self(), addr, size);
 				}
 				nextAltLoadAddress += 0x00100000;  // skip ahead 1MB and try again
@@ -459,7 +452,7 @@
 	}
 	
 	// map in each region
-	uintptr_t slide = (uintptr_t)(nextAltLoadAddress - regions[0].sfm_address);
+	uintptr_t slide = 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 +460,7 @@
 		}
 		else {
 			void* mmapAddress = (void*)(uintptr_t)(regions[i].sfm_address + slide);
-			size_t size = (size_t)regions[i].sfm_size;
+			size_t size = regions[i].sfm_size;
 			int protection = 0;
 			if ( regions[i].sfm_init_prot & VM_PROT_EXECUTE )
 				protection   |= PROT_EXEC;
@@ -601,13 +594,11 @@
 							return true;
 						if ( context.imageSuffix != NULL ) {
 							// when DYLD_IMAGE_SUFFIX is used, lastSlash string needs imageSuffix removed from end
-							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;
-							}
+							char reexportAndSuffix[strlen(context.imageSuffix)+strlen(exportThruName)+1];
+							strcpy(reexportAndSuffix, exportThruName);
+							strcat(reexportAndSuffix, context.imageSuffix);
+							if ( strcmp(&lastSlash[1], reexportAndSuffix) == 0 )
+								return true;
 						}
 					}
 				}
@@ -649,13 +640,11 @@
 									return true;
 								if ( context.imageSuffix != NULL ) {
 									// when DYLD_IMAGE_SUFFIX is used, childLeafName string needs imageSuffix removed from end
-									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;
-									}
+									char aSubLibNameAndSuffix[strlen(context.imageSuffix)+strlen(aSubLibName)+1];
+									strcpy(aSubLibNameAndSuffix, aSubLibName);
+									strcat(aSubLibNameAndSuffix, context.imageSuffix);
+									if ( strcmp(aSubLibNameAndSuffix, childLeafName) == 0 )
+										return true;
 								}
 							}
 							break;
@@ -684,13 +673,11 @@
 									return true;
 								if ( context.imageSuffix != NULL ) {
 									// when DYLD_IMAGE_SUFFIX is used, lastSlash string needs imageSuffix removed from end
-									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;
-									}
+									char umbrellaAndSuffix[strlen(context.imageSuffix)+strlen(aSubUmbrellaName)+1];
+									strcpy(umbrellaAndSuffix, aSubUmbrellaName);
+									strcat(umbrellaAndSuffix, context.imageSuffix);
+									if ( strcmp(umbrellaAndSuffix, &lastSlash[1]) == 0 )
+										return true;
 								}
 							}
 							break;
@@ -734,7 +721,7 @@
 {
 	// loop through all local (internal) relocation records looking for pre-bound-lazy-pointer values
 	const uintptr_t relocBase = this->getRelocBase();
-    const uintptr_t slide = this->fSlide;
+	register 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) {
@@ -763,9 +750,10 @@
 
 
 
-void ImageLoaderMachOClassic::rebase(const LinkContext& context, uintptr_t slide)
+void ImageLoaderMachOClassic::rebase(const LinkContext& context)
 {
 	CRSetCrashLogMessage2(this->getPath());
+	register const uintptr_t slide = this->fSlide;
 	const uintptr_t relocBase = this->getRelocBase();
 	
 	// prefetch any LINKEDIT pages needed
@@ -938,7 +926,7 @@
 }
 
 
-const ImageLoader::Symbol* ImageLoaderMachOClassic::findShallowExportedSymbol(const char* name, const ImageLoader** foundIn) const
+const ImageLoader::Symbol* ImageLoaderMachOClassic::findExportedSymbol(const char* name, const ImageLoader** foundIn) const
 {
 	const struct macho_nlist* sym = NULL;
 	if ( fDynamicInfo->tocoff == 0 )
@@ -1048,7 +1036,7 @@
 }
 
 uintptr_t ImageLoaderMachOClassic::resolveUndefined(const LinkContext& context, const struct macho_nlist* undefinedSymbol, 
-										bool twoLevel, bool dontCoalesce, bool runResolver, const ImageLoader** foundIn)
+										bool twoLevel, bool dontCoalesce, const ImageLoader** foundIn)
 {
 	++fgTotalBindSymbolsResolved;
 	const char* symbolName = &fStrings[undefinedSymbol->n_un.n_strx];
@@ -1074,7 +1062,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->findShallowExportedSymbol(symbolName, foundIn);
+			sym = this->findExportedSymbol(symbolName, foundIn);
 			if ( sym != NULL )
 				return (*foundIn)->getExportedSymbolAddress(sym, context, this);
 		}
@@ -1089,7 +1077,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, nullptr) ) {
+			if ( context.coalescedExportFinder(symbolName, &sym, foundIn) ) {
 				if ( *foundIn != this )
 					context.addDynamicReference(this, const_cast<ImageLoader*>(*foundIn));
 				return (*foundIn)->getExportedSymbolAddress(sym, context, this);
@@ -1146,12 +1134,12 @@
 			//dyld::log("resolveUndefined(%s) in %s\n", symbolName, this->getPath());
 			throw "symbol not found";
 		}
-
-		uintptr_t address;
-		if ( target->findExportedSymbolAddress(context, symbolName, this, ord, runResolver, foundIn, &address) )
-			return address;
-
-		if ( (undefinedSymbol->n_type & N_PEXT) != 0 ) {
+				
+		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 ) {
 			// 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);
@@ -1274,7 +1262,7 @@
 								// range of global symbols.  To handle that case we do the coalesing now.
 								dontCoalesce = false;
 							}
-							symbolAddr = this->resolveUndefined(context, undefinedSymbol, twoLevel, dontCoalesce, false, &image);
+							symbolAddr = this->resolveUndefined(context, undefinedSymbol, twoLevel, dontCoalesce, &image);
 							lastUndefinedSymbol = undefinedSymbol;
 							symbolAddrCached = false;
 						}
@@ -1440,7 +1428,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, true, &image);
+							uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], twoLevel, false, &image);
 							symbolAddr = this->bindIndirectSymbol(lazyPointer, sect, symbolName, symbolAddr, image,  context);
 							++fgTotalLazyBindFixups;
 							return symbolAddr;
@@ -1456,7 +1444,7 @@
 
 
 
-void ImageLoaderMachOClassic::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder, unsigned)
+void ImageLoaderMachOClassic::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder)
 {
 	it.image = this;
 	it.symbolName = " ";
@@ -1547,7 +1535,7 @@
 }
 
 
-void ImageLoaderMachOClassic::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, unsigned targetIndex, const LinkContext& context)
+void ImageLoaderMachOClassic::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, const LinkContext& context)
 {
 	// flat_namespace images with classic LINKEDIT do not need late coalescing.
 	// They still need to be iterated becuase they may implement
@@ -1649,7 +1637,7 @@
 			if ( reloc->r_pcrel ) 
 				type = BIND_TYPE_TEXT_PCREL32;
 		#endif
-			this->bindLocation(context, this->imageBaseAddress(), (uintptr_t)location, value, type, symbolName, addend, this->getPath(), targetImage ? targetImage->getPath() : NULL, "weak ", NULL, fSlide);
+			this->bindLocation(context, (uintptr_t)location, value, targetImage, type, symbolName, addend, "weak ");
 			boundSomething = true;
 		}
 	}
@@ -1803,7 +1791,7 @@
 									// range of global symbols.  To handle that case we do the coalesing now.
 									dontCoalesce = false;
 								}
-								uintptr_t symbolAddr = resolveUndefined(context, sym, twoLevel, dontCoalesce, false, &image);
+								uintptr_t symbolAddr = resolveUndefined(context, sym, twoLevel, dontCoalesce, &image);
 								// update pointer
 								symbolAddr = this->bindIndirectSymbol((uintptr_t*)ptrToBind, sect, &fStrings[sym->n_un.n_strx], symbolAddr, image,  context);
 								// update stats
@@ -1858,7 +1846,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, false, &image);
+											uintptr_t symbolAddr = this->resolveUndefined(context, &fSymbolTable[symbolIndex], this->usesTwoLevelNameSpace(), false, &image);
 											symbolAddr = this->bindIndirectSymbol((uintptr_t*)entry, sect, symbolName, symbolAddr, image, context);
 											++fgTotalBindFixups;
 											uint32_t rel32 = symbolAddr - (((uint32_t)entry)+5);
@@ -2039,14 +2027,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 j=0; j < context.dynamicInterposeCount; ++j) {
+								for(size_t i=0; i < context.dynamicInterposeCount; ++i) {
 									// replace all references to 'replacee' with 'replacement'
-									if ( symbolPointers[pointerIndex] == (uintptr_t)context.dynamicInterposeArray[j].replacee ) {
+									if ( symbolPointers[pointerIndex] == (uintptr_t)context.dynamicInterposeArray[i].replacee ) {
 										if ( context.verboseInterposing ) {
 											dyld::log("dyld: dynamic interposing: at %p replace %p with %p in %s\n", 
-												&symbolPointers[pointerIndex], context.dynamicInterposeArray[j].replacee, context.dynamicInterposeArray[j].replacement, this->getPath());
+												&symbolPointers[pointerIndex], context.dynamicInterposeArray[i].replacee, context.dynamicInterposeArray[i].replacement, this->getPath());
 										}
-										symbolPointers[pointerIndex] = (uintptr_t)context.dynamicInterposeArray[j].replacement;
+										symbolPointers[pointerIndex] = (uintptr_t)context.dynamicInterposeArray[i].replacement;
 									}
 								}
 							}