Loading...
src/ImageLoaderMachOCompressed.cpp dyld-239.3 dyld-195.5
--- dyld/dyld-239.3/src/ImageLoaderMachOCompressed.cpp
+++ dyld/dyld-195.5/src/ImageLoaderMachOCompressed.cpp
@@ -38,9 +38,6 @@
 #include "ImageLoaderMachOCompressed.h"
 #include "mach-o/dyld_images.h"
 
-#ifndef EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
-	#define EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE			0x02
-#endif
 
 // relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables
 #if __LP64__
@@ -90,7 +87,7 @@
 		if (p == end)
 			throw "malformed sleb128";
 		byte = *p++;
-		result |= (((int64_t)(byte & 0x7f)) << bit);
+		result |= ((byte & 0x7f) << bit);
 		bit += 7;
 	} while (byte & 0x80);
 	// sign extend negative numbers
@@ -113,6 +110,7 @@
 	if ( slide != 0 )
 		fgNextPIEDylibAddress = (uintptr_t)image->getEnd();
 	
+	image->setNeverUnload();
 	image->instantiateFinish(context);
 	image->setMapped(context);
 	
@@ -142,14 +140,14 @@
 		// record info about file  
 		image->setFileInfo(info.st_dev, info.st_ino, info.st_mtime);
 
+	#if CODESIGNING_SUPPORT
 		// if this image is code signed, let kernel validate signature before mapping any pages from image
-		image->loadCodeSignature(codeSigCmd, fd, offsetInFat, context);
+		if ( codeSigCmd != NULL )
+			image->loadCodeSignature(codeSigCmd, fd, offsetInFat);
+	#endif
 		
 		// mmap segments
 		image->mapSegments(fd, offsetInFat, lenInFat, info.st_size, context);
-
-		// probe to see if code signed correctly
-		image->crashIfInvalidCodeSignature();
 
 		// finish construction
 		image->instantiateFinish(context);
@@ -163,12 +161,11 @@
 		else if ( (installName != NULL) && (strcmp(path, "/usr/lib/libgcc_s.1.dylib") == 0) && (strcmp(installName, "/usr/lib/libSystem.B.dylib") == 0) )
 			image->setPathUnowned("/usr/lib/libSystem.B.dylib");
 #endif
-		else if ( (path[0] != '/') || (strstr(path, "../") != NULL) ) {
-			// rdar://problem/10733082 Fix up @rpath based paths during introspection
+		else if ( path[0] != '/' ) {
 			// rdar://problem/5135363 turn relative paths into absolute paths so gdb, Symbolication can later find them
 			char realPath[MAXPATHLEN];
-			if ( fcntl(fd, F_GETPATH, realPath) == 0 ) 
-				image->setPaths(path, realPath);
+			if ( realpath(path, realPath) != NULL )
+				image->setPath(realPath);
 			else
 				image->setPath(path);
 		}
@@ -207,6 +204,7 @@
 
 		// remember this is from shared cache and cannot be unloaded
 		image->fInSharedCache = true;
+		image->fGoodFirstSegment = true;
 		image->setNeverUnload();
 		image->setSlide(slide);
 
@@ -395,9 +393,6 @@
 
 void ImageLoaderMachOCompressed::rebaseAt(const LinkContext& context, uintptr_t addr, uintptr_t slide, uint8_t type)
 {
-	if ( context.verboseRebase ) {
-		dyld::log("dyld: rebase: %s:*0x%08lX += 0x%08lX\n", this->getShortName(), (uintptr_t)addr, slide);
-	}
 	//dyld::log("0x%08lX type=%d\n", addr, type);
 	uintptr_t* locationToFix = (uintptr_t*)addr;
 	switch (type) {
@@ -630,7 +625,7 @@
 }
 
 
-uintptr_t ImageLoaderMachOCompressed::exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, const ImageLoader* requestor, bool runResolver) const
+uintptr_t ImageLoaderMachOCompressed::exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, bool runResolver) const
 {
 	const uint8_t* exportNode = (uint8_t*)symbol;
 	const uint8_t* exportTrieStart = fLinkEditBase + fDyldInfo->export_off;
@@ -639,41 +634,26 @@
 		throw "symbol is not in trie";
 	//dyld::log("exportedSymbolAddress(): node=%p, nodeOffset=0x%04X in %s\n", symbol, (int)((uint8_t*)symbol - exportTrieStart), this->getShortName());
 	uint32_t flags = read_uleb128(exportNode, exportTrieEnd);
-	switch ( flags & EXPORT_SYMBOL_FLAGS_KIND_MASK ) {
-		case EXPORT_SYMBOL_FLAGS_KIND_REGULAR:
-			if ( runResolver && (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) ) {
-				// this node has a stub and resolver, run the resolver to get target address
-				uintptr_t stub = read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData; // skip over stub
-				// <rdar://problem/10657737> interposing dylibs have the stub address as their replacee
-				for (std::vector<InterposeTuple>::iterator it=fgInterposingTuples.begin(); it != fgInterposingTuples.end(); it++) {
-					// replace all references to 'replacee' with 'replacement'
-					if ( (stub == it->replacee) && (requestor != it->replacementImage) ) {
-						if ( context.verboseInterposing ) {
-							dyld::log("dyld interposing: lazy replace 0x%lX with 0x%lX from %s\n",
-									  it->replacee, it->replacement, this->getPath());
-						}
-						return it->replacement;
-					}
-				}
-				typedef uintptr_t (*ResolverProc)(void);
-				ResolverProc resolver = (ResolverProc)(read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData);
-				uintptr_t result = (*resolver)();
-				if ( context.verboseBind )
-					dyld::log("dyld: resolver at %p returned 0x%08lX\n", resolver, result);
-				return result;
-			}
-			return read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData;
-		case EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL:
-			if ( flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
-				dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
-			return read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData;
-		case EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE:
-			if ( flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
-				dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
-			return read_uleb128(exportNode, exportTrieEnd);
-		default:
+	if ( (flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_REGULAR ) {
+		if ( runResolver && (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) ) {
+			// this node has a stub and resolver, run the resolver to get target address
+			read_uleb128(exportNode, exportTrieEnd); // skip over stub
+			typedef uintptr_t (*ResolverProc)(void);
+			ResolverProc resolver = (ResolverProc)(read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData);
+			uintptr_t result = (*resolver)();
+			if ( context.verboseBind )
+				dyld::log("dyld: resolver at %p returned 0x%08lX\n", resolver, result);
+			return result;
+		}
+		return read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData;
+	}
+	else if ( (flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL ) {
+		if ( flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
 			dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
-	}
+		return read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData;
+	}
+	else
+		dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
 }
 
 bool ImageLoaderMachOCompressed::exportedSymbolIsWeakDefintion(const Symbol* symbol) const
@@ -725,8 +705,8 @@
 {
 	const Symbol* sym;
 	if ( context.flatExportFinder(symbolName, &sym, foundIn) ) {
-		if ( *foundIn != this )
-			context.addDynamicReference(this, const_cast<ImageLoader*>(*foundIn));
+		if ( (*foundIn != this) && !(*foundIn)->neverUnload() )
+				this->addDynamicReference(*foundIn);
 		return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
 	}
 	// if a bundle is loaded privately the above will not find its exports
@@ -1076,7 +1056,7 @@
 				case BIND_OPCODE_DO_BIND:
 					if ( address >= segmentEndAddress ) 
 						throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
-					(this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "forced lazy ", NULL, false);
+					(this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "lazy forced", NULL, true);
 					address += sizeof(intptr_t);
 					break;
 				case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
@@ -1342,7 +1322,7 @@
 				}
 				break;
 			default:
-				dyld::throwf("bad weak bind opcode '%d' found after processing %d bytes in '%s'", *p, (int)(p-start), this->getPath());
+				dyld::throwf("bad weak bind opcode %d", *p);
 		}
 	}
 	/// hmmm, BIND_OPCODE_DONE is missing...
@@ -1436,10 +1416,9 @@
 			default:
 				dyld::throwf("bad bind opcode %d in weak binding info", *p);
 		}
-	}
-	// C++ weak coalescing cannot be tracked by reference counting.  Error on side of never unloading.
-	if ( boundSomething && (targetImage != this) )
-		context.addDynamicReference(this, targetImage);
+	}	
+	if ( boundSomething && (targetImage != this) && !targetImage->neverUnload() )
+		this->addDynamicReference(targetImage);
 }
 
 uintptr_t ImageLoaderMachOCompressed::interposeAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char*, 
@@ -1558,7 +1537,7 @@
 
 
 #if __arm__ || __x86_64__
-void ImageLoaderMachOCompressed::updateAlternateLazyPointer(uint8_t* stub, void** originalLazyPointerAddr, const LinkContext& context)
+void ImageLoaderMachOCompressed::updateAlternateLazyPointer(uint8_t* stub, void** originalLazyPointerAddr)
 {
 #if __arm__ 
 	uint32_t* instructions = (uint32_t*)stub;
@@ -1578,17 +1557,8 @@
 
    // if stub does not use original lazy pointer (meaning it was optimized by update_dyld_shared_cache)
     if ( lazyPointerAddr != originalLazyPointerAddr ) {
-		// <rdar://problem/12928448> only de-optimization lazy pointers if they are part of shared cache not loaded (because overridden)
-		const ImageLoader* lazyPointerImage = context.findImageContainingAddress(lazyPointerAddr);
-		if ( lazyPointerImage != NULL )
-			return;
-		
         // copy newly re-bound lazy pointer value to shared lazy pointer
         *lazyPointerAddr = *originalLazyPointerAddr;
-		
-		if ( context.verboseBind )
-			dyld::log("dyld: alter bind: %s: *0x%08lX = 0x%08lX \n",
-					  this->getShortName(), (long)lazyPointerAddr, (long)*originalLazyPointerAddr);
     }
 }
 #endif
@@ -1651,7 +1621,7 @@
         // sanity check symbol index of stub and lazy pointer match
 		if ( indirectTable[stubsIndirectTableOffset+i] != indirectTable[lazyPointersIndirectTableOffset+i] ) 
 			continue;
-		this->updateAlternateLazyPointer(stub, lpa, context);
+		this->updateAlternateLazyPointer(stub, lpa);
 	}
 	
 #endif