Loading...
src/ImageLoaderMachOClassic.cpp dyld-353.2.1 dyld-421.2
--- dyld/dyld-353.2.1/src/ImageLoaderMachOClassic.cpp
+++ dyld/dyld-421.2/src/ImageLoaderMachOClassic.cpp
@@ -97,7 +97,8 @@
 	// 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);
 
@@ -126,7 +127,7 @@
 }
 
 // create image by mapping in a mach-o file
-ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateFromFile(const char* path, int fd, const uint8_t* fileData, 
+ImageLoaderMachOClassic* ImageLoaderMachOClassic::instantiateFromFile(const char* path, int fd, const uint8_t* fileData, size_t lenFileData,
 															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)
@@ -139,6 +140,9 @@
 		// 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);
 
@@ -192,6 +196,7 @@
 		// 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 ) {
@@ -230,6 +235,8 @@
 		// for compatibility, never unload dylibs loaded from memory
 		image->setNeverUnload();
 
+		image->disableCoverageCheck();
+
 		// bundle loads need path copied
 		if ( moduleName != NULL ) 
 			image->setPath(moduleName);
@@ -273,7 +280,7 @@
 void ImageLoaderMachOClassic::instantiateFinish(const LinkContext& context)
 {
 	// now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
-	this->parseLoadCmds();
+	this->parseLoadCmds(context);
 }
 
 ImageLoaderMachOClassic::~ImageLoaderMachOClassic()
@@ -435,8 +442,8 @@
 			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 = nextAltLoadAddress + regions[j].sfm_address - regions[0].sfm_address;
+					size = regions[j].sfm_size ;
 					(void)vm_deallocate(mach_task_self(), addr, size);
 				}
 				nextAltLoadAddress += 0x00100000;  // skip ahead 1MB and try again
@@ -750,10 +757,9 @@
 
 
 
-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
@@ -926,7 +932,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 )
@@ -1036,7 +1042,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];
@@ -1062,7 +1068,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);
 		}
@@ -1134,12 +1140,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);
@@ -1262,7 +1268,7 @@
 								// 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;
 						}
@@ -1428,7 +1434,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;
@@ -1444,7 +1450,7 @@
 
 
 
-void ImageLoaderMachOClassic::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder)
+void ImageLoaderMachOClassic::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder, unsigned)
 {
 	it.image = this;
 	it.symbolName = " ";
@@ -1535,7 +1541,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
@@ -1637,7 +1643,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, (uintptr_t)location, value, type, symbolName, addend, this->getPath(), targetImage ? targetImage->getPath() : NULL, "weak ");
 			boundSomething = true;
 		}
 	}
@@ -1791,7 +1797,7 @@
 									// 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);
 								// update stats
@@ -1846,7 +1852,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);
@@ -2027,14 +2033,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;
 									}
 								}
 							}