Loading...
src/ImageLoaderMachOCompressed.cpp dyld-239.3 dyld-852.2
--- dyld/dyld-239.3/src/ImageLoaderMachOCompressed.cpp
+++ dyld/dyld-852.2/src/ImageLoaderMachOCompressed.cpp
@@ -23,23 +23,30 @@
  */
 
 
+#if __arm__ || __arm64__
+  #include <System/sys/mman.h>
+#else
+  #include <sys/mman.h>
+#endif
 #include <string.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/fcntl.h>
 #include <sys/stat.h> 
-#include <sys/mman.h>
 #include <sys/param.h>
 #include <mach/mach.h>
 #include <mach/thread_status.h>
 #include <mach-o/loader.h> 
-
+#include <mach-o/dyld_images.h>
+
+#include "dyld2.h"
 #include "ImageLoaderMachOCompressed.h"
-#include "mach-o/dyld_images.h"
-
-#ifndef EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
-	#define EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE			0x02
+#include "Closure.h"
+#include "Array.h"
+
+#ifndef BIND_SUBOPCODE_THREADED_SET_JOP
+   #define BIND_SUBOPCODE_THREADED_SET_JOP								0x0F
 #endif
 
 // relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables
@@ -59,45 +66,6 @@
 	struct macho_routines_command	: public routines_command  {};	
 #endif
 
-	
-static uintptr_t read_uleb128(const uint8_t*& p, const uint8_t* end)
-{
-	uint64_t result = 0;
-	int		 bit = 0;
-	do {
-		if (p == end)
-			dyld::throwf("malformed uleb128");
-
-		uint64_t slice = *p & 0x7f;
-
-		if (bit > 63)
-			dyld::throwf("uleb128 too big for uint64, bit=%d, result=0x%0llX", bit, result);
-		else {
-			result |= (slice << bit);
-			bit += 7;
-		}
-	} while (*p++ & 0x80);
-	return result;
-}
-
-
-static intptr_t read_sleb128(const uint8_t*& p, const uint8_t* end)
-{
-	int64_t result = 0;
-	int bit = 0;
-	uint8_t byte;
-	do {
-		if (p == end)
-			throw "malformed sleb128";
-		byte = *p++;
-		result |= (((int64_t)(byte & 0x7f)) << bit);
-		bit += 7;
-	} while (byte & 0x80);
-	// sign extend negative numbers
-	if ( (byte & 0x40) != 0 )
-		result |= (-1LL) << bit;
-	return result;
-}
 
 
 // create image for main executable
@@ -112,10 +80,11 @@
 	// 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);
-	
+
 	if ( context.verboseMapping ) {
 		dyld::log("dyld: Main executable mapped %s\n", path);
 		for(unsigned int i=0, e=image->segmentCount(); i < e; ++i) {
@@ -131,10 +100,12 @@
 }
 
 // create image by mapping in a mach-o file
-ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::instantiateFromFile(const char* path, int fd, const uint8_t* fileData, 
+ImageLoaderMachOCompressed* ImageLoaderMachOCompressed::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)
+															const struct linkedit_data_command* codeSigCmd, 
+															const struct encryption_info_command* encryptCmd, 
+															const LinkContext& context)
 {
 	ImageLoaderMachOCompressed* image = ImageLoaderMachOCompressed::instantiateStart((macho_header*)fileData, path, segCount, libCount);
 
@@ -145,9 +116,15 @@
 		// 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->mapSegments(fd, offsetInFat, lenInFat, info.st_size, context);
 
+		// if framework is FairPlay encrypted, register with kernel
+		image->registerEncryption(encryptCmd, context);
+		
 		// probe to see if code signed correctly
 		image->crashIfInvalidCodeSignature();
 
@@ -158,7 +135,7 @@
 		const char* installName = image->getInstallPath();
 		if ( (installName != NULL) && (strcmp(installName, path) == 0) && (path[0] == '/') )
 			image->setPathUnowned(installName);
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
+#if TARGET_OS_OSX
 		// <rdar://problem/6563887> app crashes when libSystem cannot be found
 		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");
@@ -172,18 +149,23 @@
 			else
 				image->setPath(path);
 		}
-		else 
-			image->setPath(path);
+		else {
+			// <rdar://problem/46682306> always try to realpath dylibs since they may have been dlopen()ed using a symlink path
+			if ( installName != NULL ) {
+				char realPath[MAXPATHLEN];
+				if ( (fcntl(fd, F_GETPATH, realPath) == 0) && (strcmp(path, realPath) != 0) )
+					image->setPaths(path, realPath);
+				else
+					image->setPath(path);
+			}
+			else {
+				image->setPath(path);
+			}
+		}
 
 		// make sure path is stable before recording in dyld_all_image_infos
 		image->setMapped(context);
 
-		// pre-fetch content of __DATA and __LINKEDIT 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);
-			image->markSequentialLINKEDIT(context);
-		}
 	}
 	catch (...) {
 		// ImageLoader::setMapped() can throw an exception to block loading of image
@@ -209,6 +191,7 @@
 		image->fInSharedCache = true;
 		image->setNeverUnload();
 		image->setSlide(slide);
+		image->disableCoverageCheck();
 
 		// segments already mapped in cache
 		if ( context.verboseMapping ) {
@@ -219,6 +202,15 @@
 		}
 
 		image->instantiateFinish(context);
+		
+#if TARGET_OS_SIMULATOR
+		char realPath[MAXPATHLEN] = { 0 };
+		if ( dyld::gLinkContext.rootPaths == NULL )
+			throw "root path is not set";
+		strlcpy(realPath, dyld::gLinkContext.rootPaths[0], MAXPATHLEN);
+		strlcat(realPath, path, MAXPATHLEN);
+		image->setPaths(path, realPath);
+#endif
 		image->setMapped(context);
 	}
 	catch (...) {
@@ -247,6 +239,8 @@
 		// for compatibility, never unload dylibs loaded from memory
 		image->setNeverUnload();
 
+		image->disableCoverageCheck();
+
 		// bundle loads need path copied
 		if ( moduleName != NULL ) 
 			image->setPath(moduleName);
@@ -267,7 +261,7 @@
 
 ImageLoaderMachOCompressed::ImageLoaderMachOCompressed(const macho_header* mh, const char* path, unsigned int segCount, 
 																		uint32_t segOffsets[], unsigned int libCount)
- : ImageLoaderMachO(mh, path, segCount, segOffsets, libCount), fDyldInfo(NULL)
+ : ImageLoaderMachO(mh, path, segCount, segOffsets, libCount), fDyldInfo(NULL), fChainedFixups(NULL), fExportsTrie(NULL)
 {
 }
 
@@ -297,7 +291,7 @@
 void ImageLoaderMachOCompressed::instantiateFinish(const LinkContext& context)
 {
 	// now that segments are mapped in, get real fMachOData, fLinkEditBase, and fSlide
-	this->parseLoadCmds();
+	this->parseLoadCmds(context);
 }
 
 uint32_t* ImageLoaderMachOCompressed::segmentCommandOffsets() const
@@ -323,7 +317,7 @@
 bool ImageLoaderMachOCompressed::libIsUpward(unsigned int libIndex) const
 {
 	const uintptr_t* images = ((uintptr_t*)(((uint8_t*)this) + sizeof(ImageLoaderMachOCompressed) + fSegmentsCount*sizeof(uint32_t)));
-	// re-export flag is second bit
+	// upward flag is second bit
 	return ((images[libIndex] & 2) != 0);
 }	
 
@@ -338,59 +332,6 @@
 		value |= 2;
 	images[libIndex] = value;
 }
-
-
-void ImageLoaderMachOCompressed::markFreeLINKEDIT(const LinkContext& context)
-{
-	// mark that we are done with rebase and bind info 
-	markLINKEDIT(context, MADV_FREE);
-}
-
-void ImageLoaderMachOCompressed::markSequentialLINKEDIT(const LinkContext& context)
-{
-	// mark the rebase and bind info and using sequential access
-	markLINKEDIT(context, MADV_SEQUENTIAL);
-}
-
-void ImageLoaderMachOCompressed::markLINKEDIT(const LinkContext& context, int advise)
-{
-	// if not loaded at preferred address, mark rebase info 
-	uintptr_t start = 0;
-	if ( (fSlide != 0) && (fDyldInfo->rebase_size != 0) ) 
-		start = (uintptr_t)fLinkEditBase + fDyldInfo->rebase_off;
-	else if ( fDyldInfo->bind_off != 0 )
-		start = (uintptr_t)fLinkEditBase + fDyldInfo->bind_off;
-	else
-		return; // no binding info to prefetch
-		
-	// end is at end of bind info
-	uintptr_t end = 0;
-	if ( fDyldInfo->bind_off != 0 )
-		end = (uintptr_t)fLinkEditBase + fDyldInfo->bind_off + fDyldInfo->bind_size;
-	else if ( fDyldInfo->rebase_off != 0 )
-		end = (uintptr_t)fLinkEditBase + fDyldInfo->rebase_off + fDyldInfo->rebase_size;
-	else
-		return;
-		
-			
-	// round to whole pages
-	start = start & (-4096);
-	end = (end + 4095) & (-4096);
-
-	// do nothing if only one page of rebase/bind info
-	if ( (end-start) <= 4096 )
-		return;
-	
-	// tell kernel about our access to these pages
-	madvise((void*)start, end-start, advise);
-	if ( context.verboseMapping ) {
-		const char* adstr = "sequential";
-		if ( advise == MADV_FREE )
-			adstr = "free";
-		dyld::log("%18s %s 0x%0lX -> 0x%0lX for %s\n", "__LINKEDIT", adstr, start, end-1, this->getPath());
-	}
-}
-
 
 
 void ImageLoaderMachOCompressed::rebaseAt(const LinkContext& context, uintptr_t addr, uintptr_t slide, uint8_t type)
@@ -415,26 +356,37 @@
 void ImageLoaderMachOCompressed::throwBadRebaseAddress(uintptr_t address, uintptr_t segmentEndAddress, int segmentIndex, 
 										const uint8_t* startOpcodes, const uint8_t* endOpcodes, const uint8_t* pos)
 {
-	dyld::throwf("malformed rebase opcodes (%ld/%ld): address 0x%08lX is beyond end of segment %s (0x%08lX -> 0x%08lX)",
+	dyld::throwf("malformed rebase opcodes (%ld/%ld): address 0x%08lX is outside of segment %s (0x%08lX -> 0x%08lX)",
 		(intptr_t)(pos-startOpcodes), (intptr_t)(endOpcodes-startOpcodes), address, segName(segmentIndex), 
 		segActualLoadAddress(segmentIndex), segmentEndAddress); 
 }
 
-void ImageLoaderMachOCompressed::rebase(const LinkContext& context)
-{
+void ImageLoaderMachOCompressed::rebase(const LinkContext& context, uintptr_t slide)
+{
+	// binary uses chained fixups where are applied during binding
+	if ( fDyldInfo == NULL )
+		return;
+
 	CRSetCrashLogMessage2(this->getPath());
-	const uintptr_t slide = this->fSlide;
 	const uint8_t* const start = fLinkEditBase + fDyldInfo->rebase_off;
 	const uint8_t* const end = &start[fDyldInfo->rebase_size];
 	const uint8_t* p = start;
+
+	if ( start == end )
+		return;
+
+	uint32_t ignore;
+	bool bindingBecauseOfRoot = this->overridesCachedDylib(ignore);
+	vmAccountingSetSuspended(context, bindingBecauseOfRoot);
 
 	try {
 		uint8_t type = 0;
 		int segmentIndex = 0;
 		uintptr_t address = segActualLoadAddress(0);
+		uintptr_t segmentStartAddress = segActualLoadAddress(0);
 		uintptr_t segmentEndAddress = segActualEndAddress(0);
-		uint32_t count;
-		uint32_t skip;
+		uintptr_t count;
+		uintptr_t skip;
 		bool done = false;
 		while ( !done && (p < end) ) {
 			uint8_t immediate = *p & REBASE_IMMEDIATE_MASK;
@@ -449,11 +401,19 @@
 					break;
 				case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
 					segmentIndex = immediate;
-					if ( segmentIndex > fSegmentsCount )
-						dyld::throwf("REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n", 
-								segmentIndex, fSegmentsCount);
-					address = segActualLoadAddress(segmentIndex) + read_uleb128(p, end);
+					if ( segmentIndex >= fSegmentsCount )
+						dyld::throwf("REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (0..%d)",
+								segmentIndex, fSegmentsCount-1);
+			#if TEXT_RELOC_SUPPORT
+					if ( !segWriteable(segmentIndex) && !segHasRebaseFixUps(segmentIndex) && !segHasBindFixUps(segmentIndex) )
+			#else
+					if ( !segWriteable(segmentIndex) )
+			#endif
+						dyld::throwf("REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is not a writable segment (%s)",
+								segmentIndex, segName(segmentIndex));
+					segmentStartAddress = segActualLoadAddress(segmentIndex);
 					segmentEndAddress = segActualEndAddress(segmentIndex);
+					address = segmentStartAddress + read_uleb128(p, end);
 					break;
 				case REBASE_OPCODE_ADD_ADDR_ULEB:
 					address += read_uleb128(p, end);
@@ -463,7 +423,7 @@
 					break;
 				case REBASE_OPCODE_DO_REBASE_IMM_TIMES:
 					for (int i=0; i < immediate; ++i) {
-						if ( address >= segmentEndAddress ) 
+						if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
 							throwBadRebaseAddress(address, segmentEndAddress, segmentIndex, start, end, p);
 						rebaseAt(context, address, slide, type);
 						address += sizeof(uintptr_t);
@@ -473,7 +433,7 @@
 				case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
 					count = read_uleb128(p, end);
 					for (uint32_t i=0; i < count; ++i) {
-						if ( address >= segmentEndAddress ) 
+						if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
 							throwBadRebaseAddress(address, segmentEndAddress, segmentIndex, start, end, p);
 						rebaseAt(context, address, slide, type);
 						address += sizeof(uintptr_t);
@@ -481,7 +441,7 @@
 					fgTotalRebaseFixups += count;
 					break;
 				case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
-					if ( address >= segmentEndAddress ) 
+					if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
 						throwBadRebaseAddress(address, segmentEndAddress, segmentIndex, start, end, p);
 					rebaseAt(context, address, slide, type);
 					address += read_uleb128(p, end) + sizeof(uintptr_t);
@@ -491,7 +451,7 @@
 					count = read_uleb128(p, end);
 					skip = read_uleb128(p, end);
 					for (uint32_t i=0; i < count; ++i) {
-						if ( address >= segmentEndAddress ) 
+						if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
 							throwBadRebaseAddress(address, segmentEndAddress, segmentIndex, start, end, p);
 						rebaseAt(context, address, slide, type);
 						address += skip + sizeof(uintptr_t);
@@ -499,7 +459,7 @@
 					fgTotalRebaseFixups += count;
 					break;
 				default:
-					dyld::throwf("bad rebase opcode %d", *p);
+					dyld::throwf("bad rebase opcode %d", *(p-1));
 			}
 		}
 	}
@@ -511,99 +471,38 @@
 	CRSetCrashLogMessage2(NULL);
 }
 
-//
-// This function is the hotspot of symbol lookup.  It was pulled out of findExportedSymbol()
-// to enable it to be re-written in assembler if needed.
-//
-const uint8_t* ImageLoaderMachOCompressed::trieWalk(const uint8_t* start, const uint8_t* end, const char* s)
-{
-	const uint8_t* p = start;
-	while ( p != NULL ) {
-		uint32_t terminalSize = *p++;
-		if ( terminalSize > 127 ) {
-			// except for re-export-with-rename, all terminal sizes fit in one byte
-			--p;
-			terminalSize = read_uleb128(p, end);
-		}
-		if ( (*s == '\0') && (terminalSize != 0) ) {
-			//dyld::log("trieWalk(%p) returning %p\n", start, p);
-			return p;
-		}
-		const uint8_t* children = p + terminalSize;
-		//dyld::log("trieWalk(%p) sym=%s, terminalSize=%d, children=%p\n", start, s, terminalSize, children);
-		uint8_t childrenRemaining = *children++;
-		p = children;
-		uint32_t nodeOffset = 0;
-		for (; childrenRemaining > 0; --childrenRemaining) {
-			const char* ss = s;
-			//dyld::log("trieWalk(%p) child str=%s\n", start, (char*)p);
-			bool wrongEdge = false;
-			// scan whole edge to get to next edge
-			// if edge is longer than target symbol name, don't read past end of symbol name
-			char c = *p;
-			while ( c != '\0' ) {
-				if ( !wrongEdge ) {
-					if ( c != *ss )
-						wrongEdge = true;
-					++ss;
-				}
-				++p;
-				c = *p;
-			}
-			if ( wrongEdge ) {
-				// advance to next child
-				++p; // skip over zero terminator
-				// skip over uleb128 until last byte is found
-				while ( (*p & 0x80) != 0 )
-					++p;
-				++p; // skil over last byte of uleb128
-			}
-			else {
- 				// the symbol so far matches this edge (child)
-				// so advance to the child's node
-				++p;
-				nodeOffset = read_uleb128(p, end);
-				s = ss;
-				//dyld::log("trieWalk() found matching edge advancing to node 0x%x\n", nodeOffset);
-				break;
-			}
-		}
-		if ( nodeOffset != 0 )
-			p = &start[nodeOffset];
-		else
-			p = NULL;
-	}
-	//dyld::log("trieWalk(%p) return NULL\n", start);
-	return NULL;
-}
-
-
-const ImageLoader::Symbol* ImageLoaderMachOCompressed::findExportedSymbol(const char* symbol, const ImageLoader** foundIn) const
+const ImageLoader::Symbol* ImageLoaderMachOCompressed::findShallowExportedSymbol(const char* symbol, const ImageLoader** foundIn) const
 {
 	//dyld::log("Compressed::findExportedSymbol(%s) in %s\n", symbol, this->getShortName());
-	if ( fDyldInfo->export_size == 0 )
+	uint32_t trieFileOffset = fDyldInfo ? fDyldInfo->export_off  : fExportsTrie->dataoff;
+	uint32_t trieFileSize   = fDyldInfo ? fDyldInfo->export_size : fExportsTrie->datasize;
+	if ( trieFileSize == 0 )
 		return NULL;
 #if LOG_BINDINGS
 	dyld::logBindings("%s: %s\n", this->getShortName(), symbol);
 #endif
 	++ImageLoaderMachO::fgSymbolTrieSearchs;
-	const uint8_t* start = &fLinkEditBase[fDyldInfo->export_off];
-	const uint8_t* end = &start[fDyldInfo->export_size];
+	const uint8_t* start = &fLinkEditBase[trieFileOffset];
+	const uint8_t* end = &start[trieFileSize];
 	const uint8_t* foundNodeStart = this->trieWalk(start, end, symbol); 
 	if ( foundNodeStart != NULL ) {
 		const uint8_t* p = foundNodeStart;
-		const uint32_t flags = read_uleb128(p, end);
+		const uintptr_t flags = read_uleb128(p, end);
 		// found match, return pointer to terminal part of node
 		if ( flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
 			// re-export from another dylib, lookup there
-			const uint32_t ordinal = read_uleb128(p, end);
+			const uintptr_t ordinal = read_uleb128(p, end);
 			const char* importedName = (char*)p;
 			if ( importedName[0] == '\0' )
 				importedName = symbol;
 			if ( (ordinal > 0) && (ordinal <= libraryCount()) ) {
-				const ImageLoader* reexportedFrom = libImage(ordinal-1);
+				const ImageLoader* reexportedFrom = libImage((unsigned int)ordinal-1);
+				// Missing weak-dylib
+				if ( reexportedFrom == NULL )
+					return NULL;
 				//dyld::log("Compressed::findExportedSymbol(), %s -> %s/%s\n", symbol, reexportedFrom->getShortName(), importedName);
-				return reexportedFrom->findExportedSymbol(importedName, true, foundIn);
+				const char* reExportLibPath = libPath((unsigned int)ordinal-1);
+				return reexportedFrom->findExportedSymbol(importedName, true, reExportLibPath, foundIn);
 			}
 			else {
 				//dyld::throwf("bad mach-o binary, library ordinal (%u) invalid (max %u) for re-exported symbol %s in %s",
@@ -624,66 +523,72 @@
 
 bool ImageLoaderMachOCompressed::containsSymbol(const void* addr) const
 {
-	const uint8_t* start = &fLinkEditBase[fDyldInfo->export_off];
-	const uint8_t* end = &start[fDyldInfo->export_size];
+	uint32_t trieFileOffset = fDyldInfo ? fDyldInfo->export_off  : fExportsTrie->dataoff;
+	uint32_t trieFileSize   = fDyldInfo ? fDyldInfo->export_size : fExportsTrie->datasize;
+	const uint8_t* start = &fLinkEditBase[trieFileOffset];
+	const uint8_t* end = &start[trieFileSize];
 	return ( (start <= addr) && (addr < end) );
 }
 
 
 uintptr_t ImageLoaderMachOCompressed::exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, const ImageLoader* requestor, bool runResolver) const
 {
+	uint32_t trieFileOffset = fDyldInfo ? fDyldInfo->export_off  : fExportsTrie->dataoff;
+	uint32_t trieFileSize   = fDyldInfo ? fDyldInfo->export_size : fExportsTrie->datasize;
 	const uint8_t* exportNode = (uint8_t*)symbol;
-	const uint8_t* exportTrieStart = fLinkEditBase + fDyldInfo->export_off;
-	const uint8_t* exportTrieEnd = exportTrieStart + fDyldInfo->export_size;
+	const uint8_t* exportTrieStart = fLinkEditBase + trieFileOffset;
+	const uint8_t* exportTrieEnd = exportTrieStart + trieFileSize;
 	if ( (exportNode < exportTrieStart) || (exportNode > exportTrieEnd) )
 		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);
+	uintptr_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;
-					}
-				}
+				uintptr_t interposedStub = interposedAddress(context, stub, requestor);
+				if ( interposedStub != stub )
+					return interposedStub;
+				// stub was not interposed, so run resolver
 				typedef uintptr_t (*ResolverProc)(void);
 				ResolverProc resolver = (ResolverProc)(read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)fMachOData);
+#if __has_feature(ptrauth_calls)
+				resolver = (ResolverProc)__builtin_ptrauth_sign_unauthenticated(resolver, ptrauth_key_asia, 0);
+#endif
 				uintptr_t result = (*resolver)();
 				if ( context.verboseBind )
 					dyld::log("dyld: resolver at %p returned 0x%08lX\n", resolver, result);
+#if __has_feature(ptrauth_calls)
+    			result = (uintptr_t)__builtin_ptrauth_strip((void*)result, ptrauth_key_asia);
+#endif
 				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);
+				dyld::throwf("unsupported exported symbol kind. flags=%lu 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);
+				dyld::throwf("unsupported exported symbol kind. flags=%lu at node=%p", flags, symbol);
 			return read_uleb128(exportNode, exportTrieEnd);
 		default:
-			dyld::throwf("unsupported exported symbol kind. flags=%d at node=%p", flags, symbol);
+			dyld::throwf("unsupported exported symbol kind. flags=%lu at node=%p", flags, symbol);
 	}
 }
 
 bool ImageLoaderMachOCompressed::exportedSymbolIsWeakDefintion(const Symbol* symbol) const
 {
+	uint32_t trieFileOffset = fDyldInfo ? fDyldInfo->export_off  : fExportsTrie->dataoff;
+	uint32_t trieFileSize   = fDyldInfo ? fDyldInfo->export_size : fExportsTrie->datasize;
 	const uint8_t* exportNode = (uint8_t*)symbol;
-	const uint8_t* exportTrieStart = fLinkEditBase + fDyldInfo->export_off;
-	const uint8_t* exportTrieEnd = exportTrieStart + fDyldInfo->export_size;
+	const uint8_t* exportTrieStart = fLinkEditBase + trieFileOffset;
+	const uint8_t* exportTrieEnd = exportTrieStart + trieFileSize;
 	if ( (exportNode < exportTrieStart) || (exportNode > exportTrieEnd) )
 		throw "symbol is not in trie";
-	uint32_t flags = read_uleb128(exportNode, exportTrieEnd);
+	uintptr_t flags = read_uleb128(exportNode, exportTrieEnd);
 	return ( flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION );
 }
 
@@ -732,7 +637,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->ImageLoaderMachO::findExportedSymbol(symbolName, false, foundIn);
+		sym = this->findShallowExportedSymbol(symbolName, foundIn);
 		if ( sym != NULL )
 			return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
 	}
@@ -740,31 +645,146 @@
 		// definition can't be found anywhere, ok because it is weak, just return 0
 		return 0;
 	}
-	throwSymbolNotFound(context, symbolName, this->getPath(), "flat namespace");
-}
-
-
-uintptr_t ImageLoaderMachOCompressed::resolveTwolevel(const LinkContext& context, const ImageLoader* targetImage, bool weak_import, 
-												const char* symbolName, bool runResolver, const ImageLoader** foundIn)
-{
-	// two level lookup
-	const Symbol* sym = targetImage->findExportedSymbol(symbolName, true, foundIn);
-	if ( sym != NULL ) {
+	throwSymbolNotFound(context, symbolName, this->getPath(), "", "flat namespace");
+}
+
+
+static void patchCacheUsesOf(const ImageLoader::LinkContext& context, const dyld3::closure::Image* overriddenImage,
+							 uint32_t cacheOffsetOfImpl, const char* symbolName, uintptr_t newImpl,
+							 DyldSharedCache::DataConstLazyScopedWriter& patcher)
+{
+	patcher.makeWriteable();
+
+	uintptr_t cacheStart = (uintptr_t)context.dyldCache;
+	uint32_t imageIndex = overriddenImage->imageNum() - (uint32_t)context.dyldCache->cachedDylibsImageArray()->startImageNum();
+	context.dyldCache->forEachPatchableUseOfExport(imageIndex, cacheOffsetOfImpl, ^(dyld_cache_patchable_location patchLocation) {
+		uintptr_t* loc = (uintptr_t*)(cacheStart+patchLocation.cacheOffset);
+#if __has_feature(ptrauth_calls)
+		if ( patchLocation.authenticated ) {
+			 dyld3::MachOLoaded::ChainedFixupPointerOnDisk fixupInfo;
+			fixupInfo.arm64e.authRebase.auth      = true;
+			fixupInfo.arm64e.authRebase.addrDiv   = patchLocation.usesAddressDiversity;
+			fixupInfo.arm64e.authRebase.diversity = patchLocation.discriminator;
+			fixupInfo.arm64e.authRebase.key       = patchLocation.key;
+			uintptr_t newValue = fixupInfo.arm64e.signPointer(loc, newImpl + DyldSharedCache::getAddend(patchLocation));
+			if ( *loc != newValue ) {
+				*loc = newValue;
+				if ( context.verboseBind )
+					dyld::log("dyld: cache fixup: *%p = %p (JOP: diversity 0x%04X, addr-div=%d, key=%s) to %s\n",
+						  	loc, (void*)newValue, patchLocation.discriminator, patchLocation.usesAddressDiversity, DyldSharedCache::keyName(patchLocation), symbolName);
+			}
+			return;
+		}
+#endif
+		uintptr_t newValue = newImpl + (uintptr_t)DyldSharedCache::getAddend(patchLocation);
+		if ( *loc != newValue ) {
+			*loc = newValue;
+			if ( context.verboseBind )
+				dyld::log("dyld: cache fixup: *%p = %p to %s\n", loc, (void*)newValue, symbolName);
+		}
+	});
+}
+
+
+
+uintptr_t ImageLoaderMachOCompressed::resolveWeak(const LinkContext& context, const char* symbolName, bool weak_import,
+												  bool runResolver, const ImageLoader** foundIn,
+												  DyldSharedCache::DataConstLazyScopedWriter& patcher)
+{
+	const Symbol* sym;
+	CoalesceNotifier notifier = nullptr;
+	__block uintptr_t   foundOutsideCache     = 0;
+	__block const char* foundOutsideCachePath = nullptr;
+	__block uintptr_t   lastFoundInCache      = 0;
+
+	if ( this->usesChainedFixups() ) {
+		notifier = ^(const Symbol* implSym, const ImageLoader* implIn, const mach_header* implMh) {
+			// This block is only called in dyld2 mode when a non-cached image is search for which weak-def implementation to use
+			// As a side effect of that search we notice any implementations outside and inside the cache,
+			// and use that to trigger patching the cache to use the implementation outside the cache.
+			uintptr_t implAddr = implIn->getExportedSymbolAddress(implSym, context, nullptr, false, symbolName);
+			if ( ((dyld3::MachOLoaded*)implMh)->inDyldCache() ) {
+				if ( foundOutsideCache != 0 ) {
+					// have an implementation in cache and and earlier one not in the cache, patch cache to use earlier one
+					lastFoundInCache = implAddr;
+					uint32_t imageIndex;
+					if ( context.dyldCache->findMachHeaderImageIndex(implMh, imageIndex) ) {
+						const dyld3::closure::Image* overriddenImage = context.dyldCache->cachedDylibsImageArray()->imageForNum(imageIndex+1);
+						uint32_t cacheOffsetOfImpl = (uint32_t)((uintptr_t)implAddr - (uintptr_t)context.dyldCache);
+						if ( context.verboseWeakBind )
+							dyld::log("dyld: weak bind, patching dyld cache uses of %s to use 0x%lX in %s\n", symbolName, foundOutsideCache, foundOutsideCachePath);
+						patchCacheUsesOf(context, overriddenImage, cacheOffsetOfImpl, symbolName, foundOutsideCache, patcher);
+					}
+				}
+			}
+			else {
+				// record first non-cache implementation
+				if ( foundOutsideCache == 0 ) {
+					foundOutsideCache     = implAddr;
+					foundOutsideCachePath = implIn->getPath();
+				}
+			}
+		};
+	}
+
+	if ( context.coalescedExportFinder(symbolName, &sym, foundIn, notifier) ) {
+		if ( *foundIn != this )
+			context.addDynamicReference(this, const_cast<ImageLoader*>(*foundIn));
 		return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
 	}
-	
+	// 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);
+		if ( sym != NULL )
+			return (*foundIn)->getExportedSymbolAddress(sym, context, this, runResolver);
+	}
 	if ( weak_import ) {
 		// definition can't be found anywhere, ok because it is weak, just return 0
 		return 0;
 	}
-
-	// nowhere to be found
-	throwSymbolNotFound(context, symbolName, this->getPath(), targetImage->getPath());
+	throwSymbolNotFound(context, symbolName, this->getPath(), "", "weak");
+}
+
+
+uintptr_t ImageLoaderMachOCompressed::resolveTwolevel(const LinkContext& context, const char* symbolName, const ImageLoader* definedInImage,
+													  const ImageLoader* requestorImage, unsigned requestorOrdinalOfDef, bool weak_import, bool runResolver,
+													  const ImageLoader** foundIn)
+{
+	// two level lookup
+	uintptr_t address;
+	if ( definedInImage->findExportedSymbolAddress(context, symbolName, requestorImage, requestorOrdinalOfDef, runResolver, foundIn, &address) )
+		return address;
+
+	if ( weak_import ) {
+		// definition can't be found anywhere, ok because it is weak, just return 0
+		return 0;
+	}
+
+	// nowhere to be found, check if maybe this image is too new for this OS
+	char versMismatch[256];
+	versMismatch[0] = '\0';
+	uint32_t imageMinOS = this->minOSVersion();
+	// dyld is always built for the current OS, so we can get the current OS version
+	// from the load command in dyld itself.
+	extern const mach_header __dso_handle;
+	uint32_t dyldMinOS = ImageLoaderMachO::minOSVersion(&__dso_handle);
+	if ( imageMinOS > dyldMinOS ) {
+#if TARGET_OS_OSX
+		const char* msg = dyld::mkstringf(" (which was built for Mac OS X %d.%d)", imageMinOS >> 16, (imageMinOS >> 8) & 0xFF);
+#else
+		const char* msg = dyld::mkstringf(" (which was built for iOS %d.%d)", imageMinOS >> 16, (imageMinOS >> 8) & 0xFF);
+#endif
+		strcpy(versMismatch, msg);
+		::free((void*)msg);
+	}
+	throwSymbolNotFound(context, symbolName, this->getPath(), versMismatch, definedInImage->getPath());
 }
 
 
 uintptr_t ImageLoaderMachOCompressed::resolve(const LinkContext& context, const char* symbolName, 
-													uint8_t symboFlags, int libraryOrdinal, const ImageLoader** targetImage,
+													uint8_t symboFlags, long libraryOrdinal, const ImageLoader** targetImage,
+													DyldSharedCache::DataConstLazyScopedWriter& patcher,
 													LastLookup* last, bool runResolver)
 {
 	*targetImage = NULL;
@@ -784,6 +804,9 @@
 	if ( context.bindFlat || (libraryOrdinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP) ) {
 		symbolAddress = this->resolveFlat(context, symbolName, weak_import, runResolver, targetImage);
 	}
+	else if ( libraryOrdinal == BIND_SPECIAL_DYLIB_WEAK_LOOKUP ) {
+		symbolAddress = this->resolveWeak(context, symbolName, weak_import, runResolver, targetImage, patcher);
+	}
 	else {
 		if ( libraryOrdinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE ) {
 			*targetImage = context.mainExecutable;
@@ -792,14 +815,14 @@
 			*targetImage = this;
 		}
 		else if ( libraryOrdinal <= 0 ) {
-			dyld::throwf("bad mach-o binary, unknown special library ordinal (%u) too big for symbol %s in %s",
+			dyld::throwf("bad mach-o binary, unknown special library ordinal (%ld) too big for symbol %s in %s",
 				libraryOrdinal, symbolName, this->getPath());
 		}
 		else if ( (unsigned)libraryOrdinal <= libraryCount() ) {
-			*targetImage = libImage(libraryOrdinal-1);
+			*targetImage = libImage((unsigned int)libraryOrdinal-1);
 		}
 		else {
-			dyld::throwf("bad mach-o binary, library ordinal (%u) too big (max %u) for symbol %s in %s",
+			dyld::throwf("bad mach-o binary, library ordinal (%ld) too big (max %u) for symbol %s in %s",
 				libraryOrdinal, libraryCount(), symbolName, this->getPath());
 		}
 		if ( *targetImage == NULL ) {
@@ -808,15 +831,21 @@
 				symbolAddress = 0;
 			}
 			else {
-				dyld::throwf("can't resolve symbol %s in %s because dependent dylib #%d could not be loaded",
-					symbolName, this->getPath(), libraryOrdinal);
+				// Try get the path from the load commands
+				if ( const char* depPath = libPath((unsigned int)libraryOrdinal-1) ) {
+					dyld::throwf("can't resolve symbol %s in %s because dependent dylib %s could not be loaded",
+								 symbolName, this->getPath(), depPath);
+				} else {
+					dyld::throwf("can't resolve symbol %s in %s because dependent dylib #%ld could not be loaded",
+								 symbolName, this->getPath(), libraryOrdinal);
+				}
 			}
 		}
 		else {
-			symbolAddress = resolveTwolevel(context, *targetImage, weak_import, symbolName, runResolver, targetImage);
-		}
-	}
-	
+			symbolAddress = resolveTwolevel(context, symbolName, *targetImage, this, (unsigned)libraryOrdinal, weak_import, runResolver, targetImage);
+		}
+	}
+
 	// save off lookup results if client wants 
 	if ( last != NULL ) {
 		last->ordinal	= libraryOrdinal;
@@ -829,30 +858,37 @@
 	return symbolAddress;
 }
 
-uintptr_t ImageLoaderMachOCompressed::bindAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char* symbolName, 
-								uint8_t symboFlags, intptr_t addend, int libraryOrdinal, const char* msg, 
-								LastLookup* last, bool runResolver)
+uintptr_t ImageLoaderMachOCompressed::bindAt(const LinkContext& context, ImageLoaderMachOCompressed* image,
+											 uintptr_t addr, uint8_t type, const char* symbolName,
+											 uint8_t symbolFlags, intptr_t addend, long libraryOrdinal,
+											 ExtraBindData *extraBindData,
+											 const char* msg, DyldSharedCache::DataConstLazyScopedWriter& patcher,
+											 LastLookup* last, bool runResolver)
 {
 	const ImageLoader*	targetImage;
 	uintptr_t			symbolAddress;
 	
 	// resolve symbol
-	symbolAddress = this->resolve(context, symbolName, symboFlags, libraryOrdinal, &targetImage, last, runResolver);
+    if (type == BIND_TYPE_THREADED_REBASE) {
+        symbolAddress = 0;
+        targetImage = nullptr;
+    } else
+        symbolAddress = image->resolve(context, symbolName, symbolFlags, libraryOrdinal, &targetImage, patcher, last, runResolver);
 
 	// do actual update
-	return this->bindLocation(context, addr, symbolAddress, targetImage, type, symbolName, addend, msg);
-}
+	return image->bindLocation(context, image->imageBaseAddress(), addr, symbolAddress, type, symbolName, addend, image->getPath(), targetImage ? targetImage->getPath() : NULL, msg, extraBindData, image->fSlide);
+}
+
 
 void ImageLoaderMachOCompressed::throwBadBindingAddress(uintptr_t address, uintptr_t segmentEndAddress, int segmentIndex, 
 										const uint8_t* startOpcodes, const uint8_t* endOpcodes, const uint8_t* pos)
 {
-	dyld::throwf("malformed binding opcodes (%ld/%ld): address 0x%08lX is beyond end of segment %s (0x%08lX -> 0x%08lX)",
+	dyld::throwf("malformed binding opcodes (%ld/%ld): address 0x%08lX is outside segment %s (0x%08lX -> 0x%08lX)",
 		(intptr_t)(pos-startOpcodes), (intptr_t)(endOpcodes-startOpcodes), address, segName(segmentIndex), 
 		segActualLoadAddress(segmentIndex), segmentEndAddress); 
 }
 
-
-void ImageLoaderMachOCompressed::doBind(const LinkContext& context, bool forceLazysBound)
+void ImageLoaderMachOCompressed::doBind(const LinkContext& context, bool forceLazysBound, const ImageLoader* reExportParent)
 {
 	CRSetCrashLogMessage2(this->getPath());
 
@@ -860,40 +896,118 @@
 	// note: flat-namespace binaries need to have imports rebound (even if correctly prebound)
 	if ( this->usablePrebinding(context) ) {
 		// don't need to bind
+		// except weak which may now be inline with the regular binds
+		if ( this->participatesInCoalescing() && (fDyldInfo != nullptr) ) {
+			// run through all binding opcodes
+			DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
+			auto* patcherPtr = &patcher;
+			eachBind(context, ^(const LinkContext& ctx, ImageLoaderMachOCompressed* image,
+								uintptr_t addr, uint8_t type, const char* symbolName,
+								uint8_t symbolFlags, intptr_t addend, long libraryOrdinal,
+								ExtraBindData *extraBindData,
+								const char* msg, LastLookup* last, bool runResolver) {
+				if ( libraryOrdinal != BIND_SPECIAL_DYLIB_WEAK_LOOKUP )
+					return (uintptr_t)0;
+				return ImageLoaderMachOCompressed::bindAt(ctx, image, addr, type, symbolName, symbolFlags,
+														  addend, libraryOrdinal, extraBindData,
+														  msg, *patcherPtr, last, runResolver);
+			});
+		}
 	}
 	else {
-	
-	#if TEXT_RELOC_SUPPORT
-		// if there are __TEXT fixups, temporarily make __TEXT writable
-		if ( fTextSegmentBinds ) 
-			this->makeTextSegmentWritable(context, true);
-	#endif
-	
-		// run through all binding opcodes
-		eachBind(context, &ImageLoaderMachOCompressed::bindAt);
-			
-	#if TEXT_RELOC_SUPPORT
-		// if there were __TEXT fixups, restore write protection
-		if ( fTextSegmentBinds ) 
-			this->makeTextSegmentWritable(context, false);
-	#endif	
-	
-		// if this image is in the shared cache, but depends on something no longer in the shared cache,
-		// there is no way to reset the lazy pointers, so force bind them now
-		if ( forceLazysBound || fInSharedCache ) 
-			this->doBindJustLazies(context);
-            
-		// this image is in cache, but something below it is not.  If
-        // this image has lazy pointer to a resolver function, then
-        // the stub may have been altered to point to a shared lazy pointer.
-		if ( fInSharedCache ) 
-			this->updateOptimizedLazyPointers(context);
-	
-		// tell kernel we are done with chunks of LINKEDIT
-		if ( !context.preFetchDisabled ) 
-			this->markFreeLINKEDIT(context);
-	}
-	
+		uint64_t t0 = mach_absolute_time();
+
+		uint32_t ignore;
+		bool bindingBecauseOfRoot = ( this->overridesCachedDylib(ignore) || this->inSharedCache() );
+		vmAccountingSetSuspended(context, bindingBecauseOfRoot);
+
+		if ( fChainedFixups != NULL ) {
+			DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
+			const dyld_chained_fixups_header* fixupsHeader = (dyld_chained_fixups_header*)(fLinkEditBase + fChainedFixups->dataoff);
+			doApplyFixups(context, fixupsHeader, patcher);
+		}
+		else if ( fDyldInfo != nullptr ) {
+		#if TEXT_RELOC_SUPPORT
+			// if there are __TEXT fixups, temporarily make __TEXT writable
+			if ( fTextSegmentBinds )
+				this->makeTextSegmentWritable(context, true);
+		#endif
+
+			// make the cache writable for this block
+			DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
+			auto* patcherPtr = &patcher;
+			if ( this->inSharedCache() )
+				patcher.makeWriteable();
+
+			// run through all binding opcodes
+			eachBind(context, ^(const LinkContext& ctx, ImageLoaderMachOCompressed* image,
+								uintptr_t addr, uint8_t type, const char* symbolName,
+								uint8_t symbolFlags, intptr_t addend, long libraryOrdinal,
+								ExtraBindData *extraBindData,
+								const char* msg, LastLookup* last, bool runResolver) {
+				return ImageLoaderMachOCompressed::bindAt(ctx, image, addr, type, symbolName, symbolFlags,
+														  addend, libraryOrdinal, extraBindData,
+														  msg, *patcherPtr, last, runResolver);
+			});
+
+		#if TEXT_RELOC_SUPPORT
+			// if there were __TEXT fixups, restore write protection
+			if ( fTextSegmentBinds )
+				this->makeTextSegmentWritable(context, false);
+		#endif
+
+			// if this image is in the shared cache, but depends on something no longer in the shared cache,
+			// there is no way to reset the lazy pointers, so force bind them now
+			if ( forceLazysBound || fInSharedCache )
+				this->doBindJustLazies(context, patcher);
+
+			// this image is in cache, but something below it is not.  If
+			// this image has lazy pointer to a resolver function, then
+			// the stub may have been altered to point to a shared lazy pointer.
+			if ( fInSharedCache )
+				this->updateOptimizedLazyPointers(context);
+		}
+
+		uint64_t t1 = mach_absolute_time();
+		ImageLoader::fgTotalRebindCacheTime += (t1-t0);
+	}
+
+	// See if this dylib overrides something in the dyld cache
+	uint32_t dyldCacheOverrideImageNum;
+	if ( context.dyldCache && context.dyldCache->header.builtFromChainedFixups && overridesCachedDylib(dyldCacheOverrideImageNum) ) {
+
+		// make the cache writable for this block
+		DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
+		auto* patcherPtr = &patcher;
+
+		// need to patch all other places in cache that point to the overridden dylib, to point to this dylib instead
+		const dyld3::closure::Image* overriddenImage = context.dyldCache->cachedDylibsImageArray()->imageForNum(dyldCacheOverrideImageNum);
+		uint32_t imageIndex = dyldCacheOverrideImageNum - (uint32_t)context.dyldCache->cachedDylibsImageArray()->startImageNum();
+		//dyld::log("doBind() found override of %s\n", this->getPath());
+		context.dyldCache->forEachPatchableExport(imageIndex, ^(uint32_t cacheOffsetOfImpl, const char* exportName) {
+			uintptr_t newImpl = 0;
+			const ImageLoader* foundIn = nullptr;
+			if ( this->findExportedSymbolAddress(context, exportName, NULL, 0, false, &foundIn, &newImpl) ) {
+				//dyld::log("   patchCacheUsesOf(%s) found in %s\n", exportName, foundIn->getPath());
+				patchCacheUsesOf(context, overriddenImage, cacheOffsetOfImpl, exportName, newImpl, *patcherPtr);
+			}
+			else {
+				// <rdar://problem/59196856> allow patched impls to move between re-export sibling dylibs
+				if ( reExportParent != nullptr ) {
+					reExportParent->forEachReExportDependent(^(const ImageLoader* reExportedDep, bool& stop) {
+						uintptr_t siblingImpl = 0;
+						const ImageLoader* foundInSibling = nullptr;
+						if ( reExportedDep->findExportedSymbolAddress(context, exportName, NULL, 0, false, &foundInSibling, &siblingImpl) ) {
+							stop = true;
+							//dyld::log("   patchCacheUsesOf(%s) found in sibling %s\n", exportName, foundInSibling->getPath());
+							patchCacheUsesOf(context, overriddenImage, cacheOffsetOfImpl, exportName, siblingImpl, *patcherPtr);
+						}
+					});
+				}
+			}
+		});
+	}
+
 	// set up dyld entry points in image
 	// do last so flat main executables will have __dyld or __program_vars set up
 	this->setupLazyPointerHandler(context);
@@ -901,24 +1015,244 @@
 }
 
 
-void ImageLoaderMachOCompressed::doBindJustLazies(const LinkContext& context)
-{
-	eachLazyBind(context, &ImageLoaderMachOCompressed::bindAt);
-}
+void ImageLoaderMachOCompressed::doBindJustLazies(const LinkContext& context, DyldSharedCache::DataConstLazyScopedWriter& patcher)
+{
+	eachLazyBind(context, ^(const LinkContext& ctx, ImageLoaderMachOCompressed* image,
+							uintptr_t addr, uint8_t type, const char* symbolName,
+							uint8_t symbolFlags, intptr_t addend, long libraryOrdinal,
+							ExtraBindData *extraBindData,
+							const char* msg, LastLookup* last, bool runResolver) {
+		return ImageLoaderMachOCompressed::bindAt(ctx, image, addr, type, symbolName, symbolFlags,
+												  addend, libraryOrdinal, extraBindData,
+												  msg, patcher, last, runResolver);
+	});
+}
+
+void ImageLoaderMachOCompressed::doApplyFixups(const LinkContext& context, const dyld_chained_fixups_header* fixupsHeader,
+											   DyldSharedCache::DataConstLazyScopedWriter& patcher)
+{
+	const dyld3::MachOLoaded* ml = (dyld3::MachOLoaded*)machHeader();
+	const dyld_chained_starts_in_image* starts = (dyld_chained_starts_in_image*)((uint8_t*)fixupsHeader + fixupsHeader->starts_offset);
+
+	// build table of resolved targets for each symbol ordinal
+	STACK_ALLOC_OVERFLOW_SAFE_ARRAY(const void*, targetAddrs, 128);
+	targetAddrs.reserve(fixupsHeader->imports_count);
+	__block Diagnostics diag;
+	const dyld3::MachOAnalyzer* ma = (dyld3::MachOAnalyzer*)ml;
+	ma->forEachChainedFixupTarget(diag, ^(int libOrdinal, const char* symbolName, uint64_t addend, bool weakImport, bool& stop) {
+		const ImageLoader*	targetImage;
+		uint8_t symbolFlags = weakImport ? BIND_SYMBOL_FLAGS_WEAK_IMPORT : 0;
+		try {
+			uintptr_t symbolAddress = this->resolve(context, symbolName, symbolFlags, libOrdinal, &targetImage, patcher, NULL, true);
+			targetAddrs.push_back((void*)(symbolAddress + addend));
+		}
+		catch (const char* msg) {
+			stop = true;
+			diag.error("%s", msg);
+		}
+	});
+	if ( diag.hasError() )
+		throw strdup(diag.errorMessage());
+
+	auto logFixups = ^(void* loc, void* newValue) {
+		dyld::log("dyld: fixup: %s:%p = %p\n", this->getShortName(), loc, newValue);
+	};
+	if ( !context.verboseBind )
+		logFixups = nullptr;
+
+	ml->fixupAllChainedFixups(diag, starts, fSlide, targetAddrs, logFixups);
+	if ( diag.hasError() )
+		throw strdup(diag.errorMessage());
+}
+
+void ImageLoaderMachOCompressed::registerInterposing(const LinkContext& context)
+{
+	// mach-o files advertise interposing by having a __DATA __interpose section
+	struct InterposeData { uintptr_t replacement; uintptr_t replacee; };
+
+	// FIDME: It seems wrong to need a patcher here, but resolve may call resolveWeak and patch the cache.
+	// That would require weak symbols in the interposing section though, which may not be supported.
+	DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
+	auto* patcherPtr = &patcher;
+
+	__block Diagnostics diag;
+	const dyld3::MachOAnalyzer* ma = (dyld3::MachOAnalyzer*)fMachOData;
+	ma->forEachInterposingSection(diag, ^(uint64_t vmOffset, uint64_t vmSize, bool& stopSections) {
+		if ( ma->hasChainedFixups() ) {
+            const uint16_t pointerFormat = ma->chainedPointerFormat();
+			const uint8_t* sectionStart = fMachOData+vmOffset;
+			const uint8_t* sectionEnd   = fMachOData+vmOffset+vmSize;
+        	ma->withChainStarts(diag, ma->chainStartsOffset(), ^(const dyld_chained_starts_in_image* startsInfo) {
+        		__block uintptr_t lastRebaseTarget = 0;
+				ma->forEachFixupInAllChains(diag, startsInfo, false, ^(dyld3::MachOLoaded::ChainedFixupPointerOnDisk* fixupLoc, const dyld_chained_starts_in_segment* segInfo, bool& stopFixups) {
+					if ( ((uint8_t*)fixupLoc < sectionStart) || ((uint8_t*)fixupLoc >= sectionEnd) )
+						return;
+					uint64_t rebaseTargetRuntimeOffset;
+					uint32_t bindOrdinal;
+					int64_t  ptrAddend;
+       				if ( fixupLoc->isRebase(pointerFormat, 0, rebaseTargetRuntimeOffset) ) {
+						//dyld::log("interpose rebase at fixup at %p to 0x%0llX\n", fixupLoc, rebaseTargetRuntimeOffset);
+						lastRebaseTarget = (uintptr_t)(fMachOData+rebaseTargetRuntimeOffset);
+       				}
+     				else if ( fixupLoc->isBind(pointerFormat, bindOrdinal, ptrAddend) ) {
+						//dyld::log("interpose bind fixup at %p to bind ordinal %d\n", fixupLoc, bindOrdinal);
+						__block uint32_t targetBindIndex = 0;
+						ma->forEachChainedFixupTarget(diag, ^(int libraryOrdinal, const char* symbolName, uint64_t addend, bool weakImport, bool& stop) {
+							if ( targetBindIndex == bindOrdinal ) {
+								//dyld::log("interpose bind fixup at %p is to %s libOrdinal=%d\n", fixupLoc, symbolName, libraryOrdinal);
+								LastLookup* last = NULL;
+								const ImageLoader* targetImage;
+								uintptr_t targetBindAddress = 0;
+								try {
+									targetBindAddress = this->resolve(context, symbolName, 0, libraryOrdinal, &targetImage, *patcherPtr, last, false);
+								}
+								catch (const char* msg) {
+									if ( !weakImport )
+										throw msg;
+									targetBindAddress = 0;
+								}
+								//dyld::log("interpose bind fixup at %p is bound to 0x%lX\n", fixupLoc, targetBindAddress);
+								// <rdar://problem/25686570> ignore interposing on a weak function that does not exist
+								if ( targetBindAddress == 0 )
+									return;
+								ImageLoader::InterposeTuple tuple;
+								tuple.replacement   = lastRebaseTarget;
+								tuple.neverImage	= this;
+								tuple.onlyImage 	= NULL;
+								tuple.replacee      = targetBindAddress;
+								// <rdar://problem/7937695> verify that replacement is in this image
+								if ( this->containsAddress((void*)tuple.replacement) ) {
+									if ( context.verboseInterposing )
+										dyld::log("dyld: interposing 0x%lx with 0x%lx\n", tuple.replacee, tuple.replacement);
+									// chain to any existing interpositions
+									for (std::vector<InterposeTuple>::iterator it=fgInterposingTuples.begin(); it != fgInterposingTuples.end(); it++) {
+										if ( it->replacee == tuple.replacee ) {
+											tuple.replacee = it->replacement;
+										}
+									}
+									ImageLoader::fgInterposingTuples.push_back(tuple);
+								}
+							}
+							++targetBindIndex;
+						});
+					}
+				});
+			});
+		}
+		else {
+			// traditional (non-chained) fixups
+			const size_t         count          = (size_t)(vmSize / sizeof(InterposeData));
+			const InterposeData* interposeArray = (InterposeData*)(fMachOData+vmOffset);
+			if ( context.verboseInterposing )
+				dyld::log("dyld: found %lu interposing tuples in %s\n", count, getPath());
+			for (size_t j=0; j < count; ++j) {
+				uint64_t bindOffset = ((uint8_t*)&(interposeArray[j].replacee)) - fMachOData;
+				ma->forEachBind(diag, ^(uint64_t runtimeOffset, int libOrdinal, const char* symbolName, bool weakImport, bool lazyBind, uint64_t addend, bool& stopBinds) {
+					if ( bindOffset != runtimeOffset )
+						return;
+					stopBinds = true;
+					LastLookup* last = NULL;
+					const ImageLoader* targetImage;
+					uintptr_t targetBindAddress = 0;
+					try {
+						targetBindAddress = this->resolve(context, symbolName, 0, libOrdinal, &targetImage, *patcherPtr, last, false);
+					}
+					catch (const char* msg) {
+						if ( !weakImport )
+							throw msg;
+						targetBindAddress = 0;
+					}
+					ImageLoader::InterposeTuple tuple;
+					tuple.replacement     = interposeArray[j].replacement;
+					tuple.neverImage      = this;
+					tuple.onlyImage       = NULL;
+					tuple.replacee        = targetBindAddress;
+					// <rdar://problem/25686570> ignore interposing on a weak function that does not exist
+					if ( tuple.replacee == 0 )
+						return;
+					// <rdar://problem/7937695> verify that replacement is in this image
+					if ( this->containsAddress((void*)tuple.replacement) ) {
+						if ( context.verboseInterposing )
+							dyld::log("dyld:   interposing 0x%lx with 0x%lx\n", tuple.replacee, tuple.replacement);
+						// chain to any existing interpositions
+						for (std::vector<InterposeTuple>::iterator it=fgInterposingTuples.begin(); it != fgInterposingTuples.end(); it++) {
+							if ( it->replacee == tuple.replacee ) {
+								tuple.replacee = it->replacement;
+							}
+						}
+						ImageLoader::fgInterposingTuples.push_back(tuple);
+					}
+				}, ^(const char* symbolName){
+				});
+			}
+		}
+	});
+}
+
+bool ImageLoaderMachOCompressed::usesChainedFixups() const
+{
+	return ((dyld3::MachOLoaded*)machHeader())->hasChainedFixups();
+}
+
+struct ThreadedBindData {
+    ThreadedBindData(const char* symbolName, int64_t addend, long libraryOrdinal, uint8_t symbolFlags, uint8_t type)
+    : symbolName(symbolName), addend(addend), libraryOrdinal(libraryOrdinal), symbolFlags(symbolFlags), type(type) { }
+
+    std::tuple<const char*, int64_t, long, bool, uint8_t> pack() const {
+        return std::make_tuple(symbolName, addend, libraryOrdinal, symbolFlags, type);
+    }
+
+    const char* symbolName     = nullptr;
+    int64_t addend             = 0;
+    long libraryOrdinal        = 0;
+    uint8_t symbolFlags        = 0;
+    uint8_t type               = 0;
+};
+
+void ImageLoaderMachOCompressed::makeDataReadOnly() const
+{
+#if !TEXT_RELOC_SUPPORT
+	if ( fReadOnlyDataSegment && !this->ImageLoader::inSharedCache() ) {
+		for (unsigned int i=0; i < fSegmentsCount; ++i) {
+			if ( segIsReadOnlyData(i) ) {
+				uintptr_t start = segActualLoadAddress(i);
+				uintptr_t size = segSize(i);
+	#if defined(__x86_64__) && !TARGET_OS_SIMULATOR
+				if ( dyld::isTranslated() ) {
+					// <rdar://problem/48325338> can't mprotect non-16KB segments
+					if ( ((size & 0x3FFF) != 0) || ((start & 0x3FFF) != 0) )
+						continue;
+				}
+	#endif
+				::mprotect((void*)start, size, PROT_READ);
+				//dyld::log("make read-only 0x%09lX -> 0x%09lX\n", (long)start, (long)(start+size));
+			}
+		}
+	}
+#endif
+}
+
 
 void ImageLoaderMachOCompressed::eachBind(const LinkContext& context, bind_handler handler)
 {
-	try {
+    try {
 		uint8_t type = 0;
-		int segmentIndex = 0;
+		int segmentIndex = -1;
 		uintptr_t address = segActualLoadAddress(0);
+		uintptr_t segmentStartAddress = segActualLoadAddress(0);
 		uintptr_t segmentEndAddress = segActualEndAddress(0);
 		const char* symbolName = NULL;
 		uint8_t symboFlags = 0;
-		int libraryOrdinal = 0;
+		bool libraryOrdinalSet = false;
+		long libraryOrdinal = 0;
 		intptr_t addend = 0;
-		uint32_t count;
-		uint32_t skip;
+		uintptr_t count;
+		uintptr_t skip;
+        uintptr_t segOffset = 0;
+
+		dyld3::OverflowSafeArray<ThreadedBindData> ordinalTable;
+        bool useThreadedRebaseBind = false;
+        ExtraBindData extraBindData;
 		LastLookup last = { 0, 0, NULL, 0, NULL };
 		const uint8_t* const start = fLinkEditBase + fDyldInfo->bind_off;
 		const uint8_t* const end = &start[fDyldInfo->bind_size];
@@ -934,9 +1268,11 @@
 					break;
 				case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
 					libraryOrdinal = immediate;
+					libraryOrdinalSet = true;
 					break;
 				case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
 					libraryOrdinal = read_uleb128(p, end);
+					libraryOrdinalSet = true;
 					break;
 				case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
 					// the special ordinals are negative numbers
@@ -946,6 +1282,7 @@
 						int8_t signExtended = BIND_OPCODE_MASK | immediate;
 						libraryOrdinal = signExtended;
 					}
+					libraryOrdinalSet = true;
 					break;
 				case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
 					symbolName = (char*)p;
@@ -962,43 +1299,144 @@
 					break;
 				case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
 					segmentIndex = immediate;
-					if ( segmentIndex > fSegmentsCount )
-						dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n", 
-								segmentIndex, fSegmentsCount);
-					address = segActualLoadAddress(segmentIndex) + read_uleb128(p, end);
+					if ( (segmentIndex >= fSegmentsCount) || (segmentIndex < 0) )
+						dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is out of range (0..%d)",
+								segmentIndex, fSegmentsCount-1);
+			#if TEXT_RELOC_SUPPORT
+					if ( !segWriteable(segmentIndex) && !segHasRebaseFixUps(segmentIndex) && !segHasBindFixUps(segmentIndex) )
+			#else
+					if ( !segWriteable(segmentIndex) )
+			#endif
+						dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is not writable", segmentIndex);
+					segOffset = read_uleb128(p, end);
+					if ( segOffset > segSize(segmentIndex) )
+						dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has offset 0x%08lX beyond segment size (0x%08lX)", segOffset, segSize(segmentIndex));
+					segmentStartAddress = segActualLoadAddress(segmentIndex);
+					address = segmentStartAddress + segOffset;
 					segmentEndAddress = segActualEndAddress(segmentIndex);
 					break;
 				case BIND_OPCODE_ADD_ADDR_ULEB:
 					address += read_uleb128(p, end);
 					break;
 				case BIND_OPCODE_DO_BIND:
-					if ( address >= segmentEndAddress ) 
+                    if (!useThreadedRebaseBind) {
+                        if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
+                            throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
+                        if ( symbolName  == NULL )
+                            dyld::throwf("BIND_OPCODE_DO_BIND missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM");
+                        if ( segmentIndex == -1 )
+                            dyld::throwf("BIND_OPCODE_DO_BIND missing preceding BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB");
+                        if ( !libraryOrdinalSet )
+                            dyld::throwf("BIND_OPCODE_DO_BIND missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL*");
+                        handler(context, this, address, type, symbolName, symboFlags, addend, libraryOrdinal,
+								&extraBindData, "", &last, false);
+                        address += sizeof(intptr_t);
+                    } else {
+                        ordinalTable.push_back(ThreadedBindData(symbolName, addend, libraryOrdinal, symboFlags, type));
+                    }
+					break;
+				case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
+					if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
 						throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
-					(this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
-					address += sizeof(intptr_t);
-					break;
-				case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
-					if ( address >= segmentEndAddress ) 
+					if ( symbolName  == NULL )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM");
+					if ( segmentIndex == -1 )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing preceding BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB");
+					if ( !libraryOrdinalSet )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL*");
+                    handler(context, this, address, type, symbolName, symboFlags, addend, libraryOrdinal,
+                                     &extraBindData, "", &last, false);
+					address += read_uleb128(p, end) + sizeof(intptr_t);
+					break;
+				case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
+					if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
 						throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
-					(this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
-					address += read_uleb128(p, end) + sizeof(intptr_t);
-					break;
-				case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
-					if ( address >= segmentEndAddress ) 
-						throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
-					(this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
+					if ( symbolName  == NULL )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM");
+					if ( segmentIndex == -1 )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED missing preceding BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB");
+					if ( !libraryOrdinalSet )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL*");
+                    handler(context, this, address, type, symbolName, symboFlags, addend, libraryOrdinal,
+                                     &extraBindData, "", &last, false);
 					address += immediate*sizeof(intptr_t) + sizeof(intptr_t);
 					break;
 				case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
+					if ( symbolName  == NULL )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM");
+					if ( segmentIndex == -1 )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB missing preceding BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB");
 					count = read_uleb128(p, end);
+					if ( !libraryOrdinalSet )
+						dyld::throwf("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL*");
 					skip = read_uleb128(p, end);
 					for (uint32_t i=0; i < count; ++i) {
-						if ( address >= segmentEndAddress ) 
+						if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
 							throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
-						(this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "", &last, false);
+                        handler(context, this, address, type, symbolName, symboFlags, addend, libraryOrdinal,
+                                         &extraBindData, "", &last, false);
 						address += skip + sizeof(intptr_t);
 					}
-					break;
+                    break;
+                case BIND_OPCODE_THREADED:
+                    if (sizeof(intptr_t) != 8) {
+                        dyld::throwf("BIND_OPCODE_THREADED require 64-bit");
+                        return;
+                    }
+                    // Note the immediate is a sub opcode
+                    switch (immediate) {
+                        case BIND_SUBOPCODE_THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB:
+                            count = read_uleb128(p, end);
+                            ordinalTable.clear();
+                            // FIXME: ld64 wrote the wrong value here and we need to offset by 1 for now.
+                            ordinalTable.reserve(count + 1);
+                            useThreadedRebaseBind = true;
+                            break;
+                        case BIND_SUBOPCODE_THREADED_APPLY: {
+                            uint64_t delta = 0;
+                            do {
+                                address = segmentStartAddress + segOffset;
+                                uint64_t value = *(uint64_t*)address;
+
+
+                                bool isRebase = (value & (1ULL << 62)) == 0;
+                                if (isRebase) {
+                                    {
+                                        // Call the bind handler which knows about our bind type being set to rebase
+                                        handler(context, this, address, BIND_TYPE_THREADED_REBASE, nullptr, 0, 0, 0,
+                                                         nullptr, "", &last, false);
+                                    }
+                                } else {
+                                    // the ordinal is bits [0..15]
+                                    uint16_t ordinal = value & 0xFFFF;
+                                    if (ordinal >= ordinalTable.count()) {
+                                        dyld::throwf("bind ordinal (%d) is out of range (max=%lu) for disk pointer 0x%16llX at segIndex=%d, segOffset=0x%0lX in %s",
+                                                    ordinal, ordinalTable.count(),value, segmentIndex, segOffset, this->getPath());
+                                        return;
+                                    }
+                                    std::tie(symbolName, addend, libraryOrdinal, symboFlags, type) = ordinalTable[ordinal].pack();
+                                    if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
+                                        throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
+                                    {
+                                        handler(context, this, address, BIND_TYPE_THREADED_BIND,
+                                                         symbolName, symboFlags, addend, libraryOrdinal,
+                                                         nullptr, "", &last, false);
+                                    }
+                                }
+
+                                // The delta is bits [51..61]
+                                // And bit 62 is to tell us if we are a rebase (0) or bind (1)
+                                value &= ~(1ULL << 62);
+                                delta = ( value & 0x3FF8000000000000 ) >> 51;
+                                segOffset += delta * sizeof(intptr_t);
+                            } while ( delta != 0 );
+                            break;
+                        }
+
+                        default:
+                            dyld::throwf("bad threaded bind subopcode 0x%02X", *p);
+                    }
+                    break;
 				default:
 					dyld::throwf("bad bind opcode %d in bind info", *p);
 			}
@@ -1015,12 +1453,14 @@
 {
 	try {
 		uint8_t type = BIND_TYPE_POINTER;
-		int segmentIndex = 0;
+		int segmentIndex = -1;
 		uintptr_t address = segActualLoadAddress(0);
+		uintptr_t segmentStartAddress = segActualLoadAddress(0);
 		uintptr_t segmentEndAddress = segActualEndAddress(0);
+		uintptr_t segOffset;
 		const char* symbolName = NULL;
 		uint8_t symboFlags = 0;
-		int libraryOrdinal = 0;
+		long libraryOrdinal = 0;
 		intptr_t addend = 0;
 		const uint8_t* const start = fLinkEditBase + fDyldInfo->lazy_bind_off;
 		const uint8_t* const end = &start[fDyldInfo->lazy_bind_size];
@@ -1064,19 +1504,30 @@
 					break;
 				case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
 					segmentIndex = immediate;
-					if ( segmentIndex > fSegmentsCount )
-						dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n", 
-								segmentIndex, fSegmentsCount);
-					address = segActualLoadAddress(segmentIndex) + read_uleb128(p, end);
+					if ( (segmentIndex >= fSegmentsCount) || (segmentIndex < 0) )
+						dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is out of range (0..%d)",
+								segmentIndex, fSegmentsCount-1);
+					if ( !segWriteable(segmentIndex) )
+						dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is not writable", segmentIndex);
+					segOffset = read_uleb128(p, end);
+					if ( segOffset > segSize(segmentIndex) )
+						dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has offset 0x%08lX beyond segment size (0x%08lX)", segOffset, segSize(segmentIndex));
+					segmentStartAddress = segActualLoadAddress(segmentIndex);
 					segmentEndAddress = segActualEndAddress(segmentIndex);
+					address = segmentStartAddress + segOffset;
 					break;
 				case BIND_OPCODE_ADD_ADDR_ULEB:
 					address += read_uleb128(p, end);
 					break;
 				case BIND_OPCODE_DO_BIND:
-					if ( address >= segmentEndAddress ) 
+					if ( segmentIndex == -1 )
+						dyld::throwf("BIND_OPCODE_DO_BIND missing preceding BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB");
+					if ( (address < segmentStartAddress) || (address >= segmentEndAddress) )
 						throwBadBindingAddress(address, segmentEndAddress, segmentIndex, start, end, p);
-					(this->*handler)(context, address, type, symbolName, symboFlags, addend, libraryOrdinal, "forced lazy ", NULL, false);
+					if ( symbolName  == NULL )
+						dyld::throwf("BIND_OPCODE_DO_BIND missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM");
+                    handler(context, this, address, type, symbolName, symboFlags, addend, libraryOrdinal,
+                                     NULL, "forced lazy ", NULL, false);
 					address += sizeof(intptr_t);
 					break;
 				case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
@@ -1097,7 +1548,8 @@
 
 // A program built targeting 10.5 will have hybrid stubs.  When used with weak symbols
 // the classic lazy loader is used even when running on 10.6
-uintptr_t ImageLoaderMachOCompressed::doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context)
+uintptr_t ImageLoaderMachOCompressed::doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context,
+													   DyldSharedCache::DataConstLazyScopedWriter& patcher)
 {
 	// only works with compressed LINKEDIT if classic symbol table is also present
 	const macho_nlist* symbolTable = NULL;
@@ -1140,11 +1592,11 @@
 						const uint8_t type = sect->flags & SECTION_TYPE;
 						uint32_t symbolIndex = INDIRECT_SYMBOL_LOCAL;
 						if ( type == S_LAZY_SYMBOL_POINTERS ) {
-							const uint32_t pointerCount = sect->size / sizeof(uintptr_t);
+							const size_t pointerCount = sect->size / sizeof(uintptr_t);
 							uintptr_t* const symbolPointers = (uintptr_t*)(sect->addr + fSlide);
 							if ( (lazyPointer >= symbolPointers) && (lazyPointer < &symbolPointers[pointerCount]) ) {
 								const uint32_t indirectTableOffset = sect->reserved1;
-								const uint32_t lazyIndex = lazyPointer - symbolPointers;
+								const size_t lazyIndex = lazyPointer - symbolPointers;
 								symbolIndex = indirectTable[indirectTableOffset + lazyIndex];
 							}
 						}
@@ -1155,7 +1607,8 @@
 							if ( !twoLevel || context.bindFlat ) 
 								libraryOrdinal = BIND_SPECIAL_DYLIB_FLAT_LOOKUP;
 							uintptr_t ptrToBind = (uintptr_t)lazyPointer;
-							uintptr_t symbolAddr = bindAt(context, ptrToBind, BIND_TYPE_POINTER, symbolName, 0, 0, libraryOrdinal, "lazy ", NULL);
+                            uintptr_t symbolAddr = bindAt(context, this, ptrToBind, BIND_TYPE_POINTER, symbolName, 0, 0, libraryOrdinal,
+                                                          NULL, "lazy ", patcher, NULL);
 							++fgTotalLazyBindFixups;
 							return symbolAddr;
 						}
@@ -1169,6 +1622,7 @@
 }
 
 
+
 uintptr_t ImageLoaderMachOCompressed::doBindFastLazySymbol(uint32_t lazyBindingInfoOffset, const LinkContext& context,
 															void (*lock)(), void (*unlock)())
 {
@@ -1181,76 +1635,32 @@
 		if ( lock != NULL )
 			lock();
 	}
+
+	DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
 	
 	const uint8_t* const start = fLinkEditBase + fDyldInfo->lazy_bind_off;
 	const uint8_t* const end = &start[fDyldInfo->lazy_bind_size];
-	if ( lazyBindingInfoOffset > fDyldInfo->lazy_bind_size ) {
-		dyld::throwf("fast lazy bind offset out of range (%u, max=%u) in image %s", 
-			lazyBindingInfoOffset, fDyldInfo->lazy_bind_size, this->getPath());
-	}
-
-	uint8_t type = BIND_TYPE_POINTER;
-	uintptr_t address = 0;
-	const char* symbolName = NULL;
-	uint8_t symboFlags = 0;
-	int libraryOrdinal = 0;
-	bool done = false;
-	uintptr_t result = 0;
-	const uint8_t* p = &start[lazyBindingInfoOffset];
-	while ( !done && (p < end) ) {
-		uint8_t immediate = *p & BIND_IMMEDIATE_MASK;
-		uint8_t opcode = *p & BIND_OPCODE_MASK;
-		++p;
-		switch (opcode) {
-			case BIND_OPCODE_DONE:
-				done = true;
-				break;
-			case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
-				libraryOrdinal = immediate;
-				break;
-			case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
-				libraryOrdinal = read_uleb128(p, end);
-				break;
-			case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
-				// the special ordinals are negative numbers
-				if ( immediate == 0 )
-					libraryOrdinal = 0;
-				else {
-					int8_t signExtended = BIND_OPCODE_MASK | immediate;
-					libraryOrdinal = signExtended;
-				}
-				break;
-			case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
-				symbolName = (char*)p;
-				symboFlags = immediate;
-				while (*p != '\0')
-					++p;
-				++p;
-				break;
-			case BIND_OPCODE_SET_TYPE_IMM:
-				type = immediate;
-				break;
-			case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
-				if ( immediate > fSegmentsCount )
-					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n", 
-							immediate, fSegmentsCount);
-				address = segActualLoadAddress(immediate) + read_uleb128(p, end);
-				break;
-			case BIND_OPCODE_DO_BIND:
-				
-			
-				result = this->bindAt(context, address, type, symbolName, 0, 0, libraryOrdinal, "lazy ", NULL, true);
-				break;
-			case BIND_OPCODE_SET_ADDEND_SLEB:
-			case BIND_OPCODE_ADD_ADDR_ULEB:
-			case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
-			case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
-			case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
-			default:
-				dyld::throwf("bad lazy bind opcode %d", *p);
-		}
-	}	
-	
+	uint8_t segIndex;
+	uintptr_t segOffset;
+	int libraryOrdinal;
+	const char* symbolName;
+	bool doneAfterBind;
+	uintptr_t result;
+	do {
+		if ( ! getLazyBindingInfo(lazyBindingInfoOffset, start, end, &segIndex, &segOffset, &libraryOrdinal, &symbolName, &doneAfterBind) )
+			dyld::throwf("bad lazy bind info");
+
+		if ( segIndex >= fSegmentsCount )
+			dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (0..%d)", 
+							segIndex, fSegmentsCount-1);
+		if ( segOffset > segSize(segIndex) )
+			dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has offset 0x%08lX beyond segment size (0x%08lX)", segOffset, segSize(segIndex));
+		uintptr_t address = segActualLoadAddress(segIndex) + segOffset;
+        result = bindAt(context, this, address, BIND_TYPE_POINTER, symbolName, 0, 0, libraryOrdinal,
+                              NULL, "lazy ", patcher, NULL, true);
+		// <rdar://problem/24140465> Some old apps had multiple lazy symbols bound at once
+	} while (!doneAfterBind && !context.strictMachORequired);
+
 	if ( !this->usesTwoLevelNameSpace() ) {
 		// release dyld global lock
 		if ( unlock != NULL )
@@ -1259,7 +1669,7 @@
 	return result;
 }
 
-void ImageLoaderMachOCompressed::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder)
+void ImageLoaderMachOCompressed::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder, unsigned)
 {
 	it.image = this;
 	it.symbolName = " ";
@@ -1268,7 +1678,7 @@
 	it.symbolMatches = false;
 	it.done = false;
 	it.curIndex = 0;
-	it.endIndex = this->fDyldInfo->weak_bind_size;
+	it.endIndex = (this->fDyldInfo ? this->fDyldInfo->weak_bind_size : 0);
 	it.address = 0;
 	it.type = 0;
 	it.addend = 0;
@@ -1280,7 +1690,7 @@
 	if ( it.done )
 		return false;
 		
-	if ( this->fDyldInfo->weak_bind_size == 0 ) {
+	if ( (this->fDyldInfo == nullptr) || (this->fDyldInfo->weak_bind_size == 0) ) {
 		/// hmmm, ld set MH_WEAK_DEFINES or MH_BINDS_TO_WEAK, but there is no weak binding info
 		it.done = true;
 		it.symbolName = "~~~";
@@ -1289,8 +1699,9 @@
 	const uint8_t* start = fLinkEditBase + fDyldInfo->weak_bind_off;
 	const uint8_t* p = start + it.curIndex;
 	const uint8_t* end = fLinkEditBase + fDyldInfo->weak_bind_off + this->fDyldInfo->weak_bind_size;
-	uint32_t count;
-	uint32_t skip;
+	uintptr_t count;
+	uintptr_t skip;
+	uintptr_t segOffset;
 	while ( p < end ) {
 		uint8_t immediate = *p & BIND_IMMEDIATE_MASK;
 		uint8_t opcode = *p & BIND_OPCODE_MASK;
@@ -1317,10 +1728,24 @@
 				it.addend = read_sleb128(p, end);
 				break;
 			case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
-				if ( immediate > fSegmentsCount )
-					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n", 
-							immediate, fSegmentsCount);
-				it.address = segActualLoadAddress(immediate) + read_uleb128(p, end);
+				if ( immediate >= fSegmentsCount )
+					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (0..%d)",
+							immediate, fSegmentsCount-1);
+		#if __arm__
+				// <rdar://problem/23138428> iOS app compatibility
+				if ( !segWriteable(immediate) && it.image->isPositionIndependentExecutable() )
+		#elif TEXT_RELOC_SUPPORT
+				// <rdar://problem/23479396&23590867> i386 OS X app compatibility
+				if ( !segWriteable(immediate) && !segHasRebaseFixUps(immediate) && !segHasBindFixUps(immediate)
+					&& (!it.image->isExecutable() || it.image->isPositionIndependentExecutable()) )
+		#else
+				if ( !segWriteable(immediate) )
+        #endif
+					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB targets segment %s which is not writable", segName(immediate));
+				segOffset = read_uleb128(p, end);
+				if ( segOffset > segSize(immediate) )
+					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has offset 0x%08lX beyond segment size (0x%08lX)", segOffset, segSize(immediate));
+				it.address = segActualLoadAddress(immediate) + segOffset;
 				break;
 			case BIND_OPCODE_ADD_ADDR_ULEB:
 				it.address += read_uleb128(p, end);
@@ -1356,7 +1781,7 @@
 {
 	//dyld::log("looking for %s in %s\n", it.symbolName, this->getPath());
 	const ImageLoader* foundIn = NULL;
-	const ImageLoader::Symbol* sym = this->findExportedSymbol(it.symbolName, &foundIn);
+	const ImageLoader::Symbol* sym = this->findShallowExportedSymbol(it.symbolName, &foundIn);
 	if ( sym != NULL ) {
 		//dyld::log("sym=%p, foundIn=%p\n", sym, foundIn);
 		return foundIn->getExportedSymbolAddress(sym, context, this);
@@ -1365,12 +1790,15 @@
 }
 
 
-void ImageLoaderMachOCompressed::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, const LinkContext& context)
+void ImageLoaderMachOCompressed::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, unsigned targetIndex, const LinkContext& context)
 {
 	// <rdar://problem/6570879> weak binding done too early with inserted libraries
 	if ( this->getState() < dyld_image_state_bound  )
 		return;
 
+	if ( fDyldInfo == nullptr )
+		return;
+		
 	const uint8_t* start = fLinkEditBase + fDyldInfo->weak_bind_off;
 	const uint8_t* p = start + it.curIndex;
 	const uint8_t* end = fLinkEditBase + fDyldInfo->weak_bind_off + this->fDyldInfo->weak_bind_size;
@@ -1379,8 +1807,9 @@
 	uintptr_t address = it.address;
 	const char* symbolName = it.symbolName;
 	intptr_t addend = it.addend;
-	uint32_t count;
-	uint32_t skip;
+	uintptr_t count;
+	uintptr_t skip;
+	uintptr_t segOffset;
 	bool done = false;
 	bool boundSomething = false;
 	while ( !done && (p < end) ) {
@@ -1401,26 +1830,40 @@
 				addend = read_sleb128(p, end);
 				break;
 			case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
-				if ( immediate > fSegmentsCount )
-					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (%d)\n", 
-							immediate, fSegmentsCount);
-				address = segActualLoadAddress(immediate) + read_uleb128(p, end);
+				if ( immediate >= fSegmentsCount )
+					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large (0..%d)",
+							immediate, fSegmentsCount-1);
+		#if __arm__
+				// <rdar://problem/23138428> iOS app compatibility
+				if ( !segWriteable(immediate) && it.image->isPositionIndependentExecutable() )
+		#elif TEXT_RELOC_SUPPORT
+				// <rdar://problem/23479396&23590867> i386 OS X app compatibility
+				if ( !segWriteable(immediate) && !segHasRebaseFixUps(immediate) && !segHasBindFixUps(immediate)
+					&& (!it.image->isExecutable() || it.image->isPositionIndependentExecutable()) )
+		#else
+				if ( !segWriteable(immediate) )
+        #endif
+					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB targets segment %s which is not writable", segName(immediate));
+				segOffset = read_uleb128(p, end);
+				if ( segOffset > segSize(immediate) )
+					dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has offset 0x%08lX beyond segment size (0x%08lX)", segOffset, segSize(immediate));
+				address = segActualLoadAddress(immediate) + segOffset;
 				break;
 			case BIND_OPCODE_ADD_ADDR_ULEB:
 				address += read_uleb128(p, end);
 				break;
 			case BIND_OPCODE_DO_BIND:
-				bindLocation(context, address, value, targetImage, type, symbolName, addend, "weak ");
+				bindLocation(context, this->imageBaseAddress(), address, value, type, symbolName, addend, this->getPath(), targetImage ? targetImage->getPath() : NULL, "weak ", NULL, fSlide);
 				boundSomething = true;
 				address += sizeof(intptr_t);
 				break;
 			case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
-				bindLocation(context, address, value, targetImage, type, symbolName, addend, "weak ");
+				bindLocation(context, this->imageBaseAddress(), address, value, type, symbolName, addend, this->getPath(), targetImage ? targetImage->getPath() : NULL, "weak ", NULL, fSlide);
 				boundSomething = true;
 				address += read_uleb128(p, end) + sizeof(intptr_t);
 				break;
 			case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
-				bindLocation(context, address, value, targetImage, type, symbolName, addend, "weak ");
+				bindLocation(context, this->imageBaseAddress(), address, value, type, symbolName, addend, this->getPath(), targetImage ? targetImage->getPath() : NULL, "weak ", NULL, fSlide);
 				boundSomething = true;
 				address += immediate*sizeof(intptr_t) + sizeof(intptr_t);
 				break;
@@ -1428,7 +1871,7 @@
 				count = read_uleb128(p, end);
 				skip = read_uleb128(p, end);
 				for (uint32_t i=0; i < count; ++i) {
-					bindLocation(context, address, value, targetImage, type, symbolName, addend, "weak ");
+					bindLocation(context, this->imageBaseAddress(), address, value, type, symbolName, addend, this->getPath(), targetImage ? targetImage->getPath() : NULL, "weak ", NULL, fSlide);
 					boundSomething = true;
 					address += skip + sizeof(intptr_t);
 				}
@@ -1442,110 +1885,120 @@
 		context.addDynamicReference(this, targetImage);
 }
 
-uintptr_t ImageLoaderMachOCompressed::interposeAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char*, 
-												uint8_t, intptr_t, int, const char*, LastLookup*, bool runResolver)
+uintptr_t ImageLoaderMachOCompressed::interposeAt(const LinkContext& context, ImageLoaderMachOCompressed* image,
+												  uintptr_t addr, uint8_t type, const char*,
+                                                  uint8_t, intptr_t, long,
+                                                  ExtraBindData *extraBindData,
+                                                  const char*, LastLookup*, bool runResolver)
+{
+	if ( type == BIND_TYPE_POINTER ) {
+		uintptr_t* fixupLocation = (uintptr_t*)addr;
+		uintptr_t curValue = *fixupLocation;
+		uintptr_t newValue = interposedAddress(context, curValue, image);
+		if ( newValue != curValue) {
+			*fixupLocation = newValue;
+		}
+	}
+	return 0;
+}
+
+void ImageLoaderMachOCompressed::doInterpose(const LinkContext& context)
+{
+	if ( context.verboseInterposing )
+		dyld::log("dyld: interposing %lu tuples onto image: %s\n", fgInterposingTuples.size(), this->getPath());
+
+	const dyld3::MachOAnalyzer* ma = (dyld3::MachOAnalyzer*)fMachOData;
+	if ( !ma->hasChainedFixups() && (fDyldInfo != nullptr) ) {
+		// Note: all binds that happen as part of normal loading and fixups will have interposing applied.
+		// There is only two cases where we need to parse bind opcodes and apply interposing:
+
+		// make the cache writable for this block
+		DyldSharedCache::DataConstLazyScopedWriter patcher(context.dyldCache, mach_task_self(), context.verboseMapping ? &dyld::log : nullptr);
+		if ( ma->inDyldCache() )
+			patcher.makeWriteable();
+
+		// 1) Lazy pointers are either not bound yet, or in dyld cache they are prebound (to uninterposed target) 
+		eachLazyBind(context, ^(const LinkContext& ctx, ImageLoaderMachOCompressed* image,
+								uintptr_t addr, uint8_t type, const char* symbolName,
+								uint8_t symbolFlags, intptr_t addend, long libraryOrdinal,
+								ExtraBindData *extraBindData,
+								const char* msg, LastLookup* last, bool runResolver) {
+			return ImageLoaderMachOCompressed::interposeAt(ctx, image, addr, type, symbolName, symbolFlags,
+														   addend, libraryOrdinal, extraBindData,
+														   msg, last, runResolver);
+		});
+
+	  	// 2) non-lazy pointers in the dyld cache need to be interposed
+		if ( ma->inDyldCache() ) {
+			eachBind(context, ^(const LinkContext& ctx, ImageLoaderMachOCompressed* image,
+								uintptr_t addr, uint8_t type, const char* symbolName,
+								uint8_t symbolFlags, intptr_t addend, long libraryOrdinal,
+								ExtraBindData *extraBindData,
+								const char* msg, LastLookup* last, bool runResolver) {
+				return ImageLoaderMachOCompressed::interposeAt(ctx, image, addr, type, symbolName, symbolFlags,
+															   addend, libraryOrdinal, extraBindData,
+															   msg, last, runResolver);
+			});
+		}
+
+	}
+}
+
+
+uintptr_t ImageLoaderMachOCompressed::dynamicInterposeAt(const LinkContext& context, ImageLoaderMachOCompressed* image,
+														 uintptr_t addr, uint8_t type, const char* symbolName,
+                                                         uint8_t, intptr_t, long,
+                                                         ExtraBindData *extraBindData,
+                                                         const char*, LastLookup*, bool runResolver)
 {
 	if ( type == BIND_TYPE_POINTER ) {
 		uintptr_t* fixupLocation = (uintptr_t*)addr;
 		uintptr_t value = *fixupLocation;
-		for (std::vector<InterposeTuple>::iterator it=fgInterposingTuples.begin(); it != fgInterposingTuples.end(); it++) {
-			// replace all references to 'replacee' with 'replacement'
-			if ( (value == it->replacee) && (this != it->replacementImage) ) {
+		// don't apply interposing to table entries.
+		if ( (context.dynamicInterposeArray <= (void*)addr) && ((void*)addr < &context.dynamicInterposeArray[context.dynamicInterposeCount]) )
+			return 0;
+		for(size_t i=0; i < context.dynamicInterposeCount; ++i) {
+			if ( value == (uintptr_t)context.dynamicInterposeArray[i].replacee ) {
 				if ( context.verboseInterposing ) {
-					dyld::log("dyld: interposing: at %p replace 0x%lX with 0x%lX in %s\n", 
-						fixupLocation, it->replacee, it->replacement, this->getPath());
+					dyld::log("dyld: dynamic interposing: at %p replace %p with %p in %s\n", 
+						fixupLocation, context.dynamicInterposeArray[i].replacee, context.dynamicInterposeArray[i].replacement, image->getPath());
 				}
-				*fixupLocation = it->replacement;
+				*fixupLocation = (uintptr_t)context.dynamicInterposeArray[i].replacement;
 			}
 		}
 	}
 	return 0;
 }
 
-void ImageLoaderMachOCompressed::doInterpose(const LinkContext& context)
+void ImageLoaderMachOCompressed::dynamicInterpose(const LinkContext& context)
 {
 	if ( context.verboseInterposing )
-		dyld::log("dyld: interposing %lu tuples onto image: %s\n", fgInterposingTuples.size(), this->getPath());
-
-	// update prebound symbols
-	eachBind(context, &ImageLoaderMachOCompressed::interposeAt);
-	eachLazyBind(context, &ImageLoaderMachOCompressed::interposeAt);
-}
-
-
-
+		dyld::log("dyld: dynamic interposing %lu tuples onto image: %s\n", context.dynamicInterposeCount, this->getPath());
+
+	// update already bound references to symbols
+	eachBind(context, ^(const LinkContext& ctx, ImageLoaderMachOCompressed* image,
+						uintptr_t addr, uint8_t type, const char* symbolName,
+						uint8_t symbolFlags, intptr_t addend, long libraryOrdinal,
+						ExtraBindData *extraBindData,
+						const char* msg, LastLookup* last, bool runResolver) {
+		return ImageLoaderMachOCompressed::dynamicInterposeAt(ctx, image, addr, type, symbolName, symbolFlags,
+															  addend, libraryOrdinal, extraBindData,
+															  msg, last, runResolver);
+	});
+	eachLazyBind(context, ^(const LinkContext& ctx, ImageLoaderMachOCompressed* image,
+							uintptr_t addr, uint8_t type, const char* symbolName,
+							uint8_t symbolFlags, intptr_t addend, long libraryOrdinal,
+							ExtraBindData *extraBindData,
+							const char* msg, LastLookup* last, bool runResolver) {
+		return ImageLoaderMachOCompressed::dynamicInterposeAt(ctx, image, addr, type, symbolName, symbolFlags,
+															  addend, libraryOrdinal, extraBindData,
+															  msg, last, runResolver);
+	});
+}
 
 const char* ImageLoaderMachOCompressed::findClosestSymbol(const void* addr, const void** closestAddr) const
 {
-	// called by dladdr()
-	// only works with compressed LINKEDIT if classic symbol table is also present
-	const macho_nlist* symbolTable = NULL;
-	const char* symbolTableStrings = NULL;
-	const dysymtab_command* dynSymbolTable = NULL;
-	const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
-	const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
-	const struct load_command* cmd = cmds;
-	for (uint32_t i = 0; i < cmd_count; ++i) {
-		switch (cmd->cmd) {
-			case LC_SYMTAB:
-				{
-					const struct symtab_command* symtab = (struct symtab_command*)cmd;
-					symbolTableStrings = (const char*)&fLinkEditBase[symtab->stroff];
-					symbolTable = (macho_nlist*)(&fLinkEditBase[symtab->symoff]);
-				}
-				break;
-			case LC_DYSYMTAB:
-				dynSymbolTable = (struct dysymtab_command*)cmd;
-				break;
-		}
-		cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
-	}
-	// no symbol table => no lookup by address
-	if ( (symbolTable == NULL) || (dynSymbolTable == NULL) )
-		return NULL;
-
-	uintptr_t targetAddress = (uintptr_t)addr - fSlide;
-	const struct macho_nlist* bestSymbol = NULL;
-	// first walk all global symbols
-	const struct macho_nlist* const globalsStart = &symbolTable[dynSymbolTable->iextdefsym];
-	const struct macho_nlist* const globalsEnd= &globalsStart[dynSymbolTable->nextdefsym];
-	for (const struct macho_nlist* s = globalsStart; s < globalsEnd; ++s) {
- 		if ( (s->n_type & N_TYPE) == N_SECT ) {
-			if ( bestSymbol == NULL ) {
-				if ( s->n_value <= targetAddress )
-					bestSymbol = s;
-			}
-			else if ( (s->n_value <= targetAddress) && (bestSymbol->n_value < s->n_value) ) {
-				bestSymbol = s;
-			}
-		}
-	}
-	// next walk all local symbols
-	const struct macho_nlist* const localsStart = &symbolTable[dynSymbolTable->ilocalsym];
-	const struct macho_nlist* const localsEnd= &localsStart[dynSymbolTable->nlocalsym];
-	for (const struct macho_nlist* s = localsStart; s < localsEnd; ++s) {
- 		if ( ((s->n_type & N_TYPE) == N_SECT) && ((s->n_type & N_STAB) == 0) ) {
-			if ( bestSymbol == NULL ) {
-				if ( s->n_value <= targetAddress )
-					bestSymbol = s;
-			}
-			else if ( (s->n_value <= targetAddress) && (bestSymbol->n_value < s->n_value) ) {
-				bestSymbol = s;
-			}
-		}
-	}
-	if ( bestSymbol != NULL ) {
-#if __arm__
-		if (bestSymbol->n_desc & N_ARM_THUMB_DEF)
-			*closestAddr = (void*)((bestSymbol->n_value | 1) + fSlide);
-		else
-			*closestAddr = (void*)(bestSymbol->n_value + fSlide);
-#else
-		*closestAddr = (void*)(bestSymbol->n_value + fSlide);
-#endif
-		return &symbolTableStrings[bestSymbol->n_un.n_strx];
-	}
-	return NULL;
+	return ImageLoaderMachO::findClosestSymbol((mach_header*)fMachOData, addr, closestAddr);
 }
 
 
@@ -1598,9 +2051,8 @@
 void ImageLoaderMachOCompressed::updateOptimizedLazyPointers(const LinkContext& context)
 {
 #if __arm__ || __x86_64__
-	// find stubs and lazy pointer sections
+	// find stubs and indirect symbol table
 	const struct macho_section* stubsSection = NULL;
-	const struct macho_section* lazyPointerSection = NULL;
 	const dysymtab_command* dynSymbolTable = NULL;
 	const macho_header* mh = (macho_header*)fMachOData;
 	const uint32_t cmd_count = mh->ncmds;
@@ -1615,8 +2067,6 @@
 				const uint8_t type = sect->flags & SECTION_TYPE;
 				if ( type == S_SYMBOL_STUBS ) 
 					stubsSection = sect;
-				else if ( type == S_LAZY_SYMBOL_POINTERS ) 
-					lazyPointerSection = sect;
 			}
 		}
 		else if ( cmd->cmd == LC_DYSYMTAB ) {
@@ -1624,37 +2074,82 @@
 		}
 		cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
 	}
-	
-	// sanity check
 	if ( dynSymbolTable == NULL )
 		return;
-	if ( (stubsSection == NULL) || (lazyPointerSection == NULL) )
+	const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[dynSymbolTable->indirectsymoff];
+	if ( stubsSection == NULL )
 		return;
-	const uint32_t stubsCount = stubsSection->size / stubsSection->reserved2;
-	const uint32_t lazyPointersCount = lazyPointerSection->size / sizeof(void*);
-	if ( stubsCount != lazyPointersCount )
-		return;
+	const uint32_t stubsSize = stubsSection->reserved2;
+	const uint32_t stubsCount = (uint32_t)(stubsSection->size / stubsSize);
 	const uint32_t stubsIndirectTableOffset = stubsSection->reserved1;
-	const uint32_t lazyPointersIndirectTableOffset = lazyPointerSection->reserved1;
 	if ( (stubsIndirectTableOffset+stubsCount) > dynSymbolTable->nindirectsyms )
 		return;
-	if ( (lazyPointersIndirectTableOffset+lazyPointersCount) > dynSymbolTable->nindirectsyms )
+	uint8_t* const stubsAddr = (uint8_t*)(stubsSection->addr + this->fSlide);
+
+	// for each lazy pointer section
+	cmd = cmds;
+	for (uint32_t i = 0; i < cmd_count; ++i) {
+		if (cmd->cmd == LC_SEGMENT_COMMAND) {
+			const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
+			const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
+			const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
+			for (const struct macho_section* lazyPointerSection=sectionsStart; lazyPointerSection < sectionsEnd; ++lazyPointerSection) {
+				const uint8_t type = lazyPointerSection->flags & SECTION_TYPE;
+				if ( type != S_LAZY_SYMBOL_POINTERS )
+					continue;
+				const uint32_t lazyPointersCount = (uint32_t)(lazyPointerSection->size / sizeof(void*));
+				const uint32_t lazyPointersIndirectTableOffset = lazyPointerSection->reserved1;
+				if ( (lazyPointersIndirectTableOffset+lazyPointersCount) > dynSymbolTable->nindirectsyms )
+					continue;
+				void** const lazyPointersAddr = (void**)(lazyPointerSection->addr + this->fSlide);
+				// for each lazy pointer
+				for(uint32_t lpIndex=0; lpIndex < lazyPointersCount; ++lpIndex) {
+					const uint32_t lpSymbolIndex = indirectTable[lazyPointersIndirectTableOffset+lpIndex];
+					// find matching stub and validate it uses this lazy pointer
+					for(uint32_t stubIndex=0; stubIndex < stubsCount; ++stubIndex) {
+						if ( indirectTable[stubsIndirectTableOffset+stubIndex] == lpSymbolIndex ) {
+							this->updateAlternateLazyPointer(stubsAddr+stubIndex*stubsSize, &lazyPointersAddr[lpIndex], context);
+							break;
+						}
+					}
+				}
+
+			}
+		}
+		cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
+	}
+
+#endif
+}
+
+
+void ImageLoaderMachOCompressed::registerEncryption(const encryption_info_command* encryptCmd, const LinkContext& context)
+{
+#if (__arm__ || __arm64__) && !TARGET_OS_SIMULATOR
+	if ( encryptCmd == NULL )
 		return;
-	
-	// walk stubs and lazy pointers
-	const uint32_t* const indirectTable = (uint32_t*)&fLinkEditBase[dynSymbolTable->indirectsymoff];
-	void** const lazyPointersStartAddr = (void**)(lazyPointerSection->addr + this->fSlide);
-	uint8_t* const stubsStartAddr = (uint8_t*)(stubsSection->addr + this->fSlide);
-	uint8_t* stub = stubsStartAddr;
-	void** lpa = lazyPointersStartAddr;
-	for(uint32_t i=0; i < stubsCount; ++i, stub += stubsSection->reserved2, ++lpa) {
-        // sanity check symbol index of stub and lazy pointer match
-		if ( indirectTable[stubsIndirectTableOffset+i] != indirectTable[lazyPointersIndirectTableOffset+i] ) 
-			continue;
-		this->updateAlternateLazyPointer(stub, lpa, context);
-	}
-	
+	// fMachOData not set up yet, need to manually find mach_header
+	const mach_header* mh = NULL;
+	for(unsigned int i=0; i < fSegmentsCount; ++i) {
+		if ( (segFileOffset(i) == 0) && (segFileSize(i) != 0) ) {
+			mh = (mach_header*)segActualLoadAddress(i);
+			void* start = ((uint8_t*)mh) + encryptCmd->cryptoff;
+			size_t len = encryptCmd->cryptsize;
+			uint32_t cputype = mh->cputype;
+			uint32_t cpusubtype = mh->cpusubtype;
+			uint32_t cryptid = encryptCmd->cryptid;
+			if (context.verboseMapping) {
+				 dyld::log("                      0x%08lX->0x%08lX configured for FairPlay decryption\n", (long)start, (long)start+len);
+			}
+			int result = mremap_encrypted(start, len, cryptid, cputype, cpusubtype);
+			if ( result != 0 ) {
+				dyld::throwf("mremap_encrypted() => %d, errno=%d for %s\n", result, errno, this->getPath());
+			}
+			return;
+		}
+	}
 #endif
 }
 
 
+