Loading...
launch-cache/update_dyld_shared_cache.cpp dyld-360.14 dyld-195.6
--- dyld/dyld-360.14/launch-cache/update_dyld_shared_cache.cpp
+++ dyld/dyld-195.6/launch-cache/update_dyld_shared_cache.cpp
@@ -31,9 +31,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <math.h>
 #include <fcntl.h>
-#include <dlfcn.h>
 #include <signal.h>
 #include <errno.h>
 #include <sys/uio.h>
@@ -46,29 +44,25 @@
 #include <servers/bootstrap.h>
 #include <mach-o/loader.h>
 #include <mach-o/fat.h>
-#include <CoreFoundation/CoreFoundation.h>
-#include <Security/Security.h>
-#include <Security/SecCodeSigner.h>
-#include <CommonCrypto/CommonDigest.h>
 
 #include "dyld_cache_format.h"
 
 #include <vector>
 #include <set>
 #include <map>
-#include <unordered_map>
+#include <ext/hash_map>
 
 #include "Architectures.hpp"
 #include "MachOLayout.hpp"
 #include "MachORebaser.hpp"
 #include "MachOBinder.hpp"
 #include "CacheFileAbstraction.hpp"
-#include "dyld_cache_config.h"
 
 #define SELOPT_WRITE
-#include "objc-shared-cache.h"
-
-#define FIRST_DYLIB_TEXT_OFFSET 0x10000
+#include <objc/objc-selopt.h>
+
+#define FIRST_DYLIB_TEXT_OFFSET 0x5000
+#define FIRST_DYLIB_DATA_OFFSET 0x1000
 
 #ifndef LC_FUNCTION_STARTS
     #define LC_FUNCTION_STARTS 0x26
@@ -77,7 +71,6 @@
 static bool							verbose = false;
 static bool							progress = false;
 static bool							iPhoneOS = false;
-static bool							rootless = true;
 static std::vector<const char*>		warnings;
 
 
@@ -102,35 +95,21 @@
 }
 
 
-class CStringHash {
+static uint64_t pageAlign(uint64_t addr) { return ( (addr + 4095) & (-4096) ); }
+
+class ArchGraph
+{
 public:
-	size_t operator()(const char* __s) const {
-		size_t __h = 0;
-		for ( ; *__s; ++__s)
-			__h = 5 * __h + *__s;
-		return __h;
+	struct CStringEquals {
+		bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
 	};
-};
-class CStringEquals
-{
-public:
-	bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
-};
-
-
-
-class ArchGraph
-{
-public:
-	typedef std::unordered_map<const char*, const char*, CStringHash, CStringEquals> StringToString;
+	typedef __gnu_cxx::hash_map<const char*, const char*, __gnu_cxx::hash<const char*>, CStringEquals> StringToString;
 
 	static void			addArchPair(ArchPair ap);
 	static void			addRoot(const char* vpath, const std::set<ArchPair>& archs);
-	static uint64_t		maxCacheSizeForArchPair(ArchPair ap);
 	static void			findSharedDylibs(ArchPair ap);
 	static ArchGraph*	graphForArchPair(ArchPair ap) { return fgPerArchGraph[ap]; }
-	static void			setFileSystemRoot(const char* root) { fgFileSystemRoot = root; }
-	static void			setFileSystemOverlay(const std::vector<const char*>& overlays);
+	static void			setFileSystemRoot(const char* root, bool usesOverlay) { fgFileSystemRoot = root; fgUsesOverlay = usesOverlay; }
 	static const char*	archName(ArchPair ap);
 	
 	ArchPair											getArchPair() { return fArchPair; }
@@ -150,8 +129,6 @@
 		const MachOLayoutAbstraction*	getLayout() const { return fLayout; }
 		size_t							useCount() const { return fRootsDependentOnThis.size(); }
 		bool							allDependentsFound() const { return !fDependentMissing; }
-		bool							dependsOnDylibList() const { return fRootsDependentOnThis.count(const_cast<DependencyNode*>(this)); }
-
 	private:
 		ArchGraph*									fGraph;
 		const char*									fPath;
@@ -162,7 +139,7 @@
 		std::set<DependencyNode*>					fRootsDependentOnThis;
 	};
 
-	typedef std::unordered_map<const char*, class DependencyNode*, CStringHash, CStringEquals> PathToNode;
+	typedef __gnu_cxx::hash_map<const char*, class DependencyNode*, __gnu_cxx::hash<const char*>, CStringEquals> PathToNode;
 
 
 								ArchGraph(ArchPair ap) : fArchPair(ap) {}
@@ -174,7 +151,7 @@
 
 	static std::map<ArchPair, ArchGraph*>	fgPerArchGraph;
 	static const char*						fgFileSystemRoot;
-	static std::vector<const char*> 		fgFileSystemOverlays;
+	static bool								fgUsesOverlay;
 	
 	ArchPair									fArchPair;
 	std::set<DependencyNode*>					fRoots;
@@ -184,7 +161,7 @@
 };
 std::map<ArchPair, ArchGraph*>		ArchGraph::fgPerArchGraph;
 const char*							ArchGraph::fgFileSystemRoot = "";
-std::vector<const char*> 			ArchGraph::fgFileSystemOverlays;
+bool								ArchGraph::fgUsesOverlay = false;
 
 void ArchGraph::addArchPair(ArchPair ap)
 {
@@ -192,40 +169,30 @@
 	fgPerArchGraph[ap] = new ArchGraph(ap);
 }
 
-void ArchGraph::setFileSystemOverlay(const std::vector<const char*>& overlays) 
-{ 
-	for (std::vector<const char*>::const_iterator it=overlays.begin(); it != overlays.end(); ++it) 
-		fgFileSystemOverlays.push_back(*it);
-}
-
 void ArchGraph::addRoot(const char* vpath, const std::set<ArchPair>& onlyArchs)
 {
-	//fprintf(stderr, "addRoot(%s)\n", vpath);
-	char completePath[MAXPATHLEN];
+	char completePath[strlen(fgFileSystemRoot)+strlen(vpath)+2];
 	const char* path = NULL;
-	// check -overlay path first
-	for (std::vector<const char*>::const_iterator it=fgFileSystemOverlays.begin(); it != fgFileSystemOverlays.end(); ++it) {
-		strcpy(completePath, *it);
-		strcat(completePath, vpath);	// assumes vpath starts with '/'
-		struct stat stat_buf;
-		if ( stat(completePath, &stat_buf) == 0 ) {
-			path = completePath;
-			break;
-		}
-	}
-	// if not found in overlay, check for -root
-	if ( (path == NULL) && (fgFileSystemRoot[0] != '\0') ) {
+	if ( strlen(fgFileSystemRoot) == 0 ) {
+		path = vpath;
+	}
+	else {
 		strcpy(completePath, fgFileSystemRoot);
 		strcat(completePath, vpath);	// assumes vpath starts with '/'
-		struct stat stat_buf;
-		if ( stat(completePath, &stat_buf) == 0 )
+		if ( fgUsesOverlay ) {
+			// using -overlay means if /overlay/usr/lib exists use it, otherwise use original path
+			struct stat stat_buf;
+			if ( stat(completePath, &stat_buf) == 0 )
+				path = completePath;
+			else
+				path = vpath;
+		}
+		else {
+			// using -root means alway redirect /usr/lib to /rootpath/usr/lib
 			path = completePath;
-	}
-	if ( path == NULL ) 
-		path = vpath;
-	
+		}
+	}
 	try {
-		//fprintf(stderr, "    UniversalMachOLayout::find(%s)\n", path);
 		const UniversalMachOLayout& uni = UniversalMachOLayout::find(path, &onlyArchs);
 		for(std::set<ArchPair>::iterator ait = onlyArchs.begin(); ait != onlyArchs.end(); ++ait) {
 			try {
@@ -263,59 +230,53 @@
 }
 
 // a virtual path does not have the fgFileSystemRoot prefix
+// a virtual path does not have the fgFileSystemRoot prefix
 ArchGraph::DependencyNode* ArchGraph::getNodeForVirtualPath(const char* vpath)
 {
-	//fprintf(stderr, "getNodeForVirtualPath(%s)\n", vpath);
-	char completePath[MAXPATHLEN];
-	for (std::vector<const char*>::const_iterator it=fgFileSystemOverlays.begin(); it != fgFileSystemOverlays.end(); ++it) {
-		const char* overlayPath = *it;
-		// using -overlay means if /overlay/path/dylib exists use it, otherwise use /path/dylib
-		strcpy(completePath, overlayPath);
-		strcat(completePath, vpath);	// assumes vpath starts with '/'
-		struct stat stat_buf;
-		if ( stat(completePath, &stat_buf) == 0 ) {
-			return this->getNode(completePath);
-		}
-		// <rdar://problem/9279770> support when install name is a symlink
-		const char* pathToSymlink = vpath;
-		if ( fgFileSystemRoot[0] != '\0' ) {
-			strcpy(completePath, fgFileSystemRoot);
-			strcat(completePath, vpath);
-			pathToSymlink = completePath;
-		}
-		if ( (lstat(pathToSymlink, &stat_buf) == 0) && S_ISLNK(stat_buf.st_mode) ) {
-			// requested path did not exist in /overlay, but leaf of path is a symlink in /
-			char pathInSymLink[MAXPATHLEN];
-			size_t res = readlink(pathToSymlink, pathInSymLink, sizeof(pathInSymLink));
-			if ( res != -1 ) {
-				pathInSymLink[res] = '\0';
-				if ( pathInSymLink[0] != '/' ) {
-					char symFullPath[MAXPATHLEN];
-					strcpy(symFullPath, vpath);
-					char* lastSlash = strrchr(symFullPath, '/');
-					if ( lastSlash != NULL ) {
-						strcpy(lastSlash+1, pathInSymLink);
-						// (re)try looking for what symlink points to, but in /overlay
-						return this->getNodeForVirtualPath(symFullPath);
-					}
-				} 
-			}
-		}
-	}
-
-	if ( fgFileSystemRoot[0] != '\0' ) {
-		// using -root means always use /rootpath/usr/lib
+	if ( fgFileSystemRoot == NULL ) {
+		return this->getNode(vpath);
+	}
+	else {
+		char completePath[strlen(fgFileSystemRoot)+strlen(vpath)+2];
 		strcpy(completePath, fgFileSystemRoot);
 		strcat(completePath, vpath);	// assumes vpath starts with '/'
-		return this->getNode(completePath);
-	}
-	// not found in -overlay or -root not used
-	return this->getNode(vpath);
+		if ( fgUsesOverlay ) {
+			// using -overlay means if /overlay/path/dylib exists use it, otherwise use /path/dylib
+			struct stat stat_buf;
+			if ( stat(completePath, &stat_buf) == 0 )
+				return this->getNode(completePath);
+			else {
+				// <rdar://problem/9279770> support when install name is a symlink
+				if ( (lstat(vpath, &stat_buf) == 0) && S_ISLNK(stat_buf.st_mode) ) {
+					// requested path did not exist in /overlay, but leaf of path is a symlink in /
+					char pathInSymLink[MAXPATHLEN];
+					size_t res = readlink(vpath, pathInSymLink, sizeof(pathInSymLink));
+					if ( res != -1 ) {
+						pathInSymLink[res] = '\0';
+						if ( pathInSymLink[0] != '/' ) {
+							char symFullPath[MAXPATHLEN];
+							strcpy(symFullPath, vpath);
+							char* lastSlash = strrchr(symFullPath, '/');
+							if ( lastSlash != NULL ) {
+								strcpy(lastSlash+1, pathInSymLink);
+								// (re)try looking for what symlink points to, but in /overlay
+								return this->getNodeForVirtualPath(symFullPath);
+							}
+						} 
+					}
+				}
+				return this->getNode(vpath);
+			}
+		}
+		else {
+			// using -root means always use /rootpath/usr/lib
+			return this->getNode(completePath);
+		}
+	}
 }
 
 ArchGraph::DependencyNode* ArchGraph::getNode(const char* path)
 {
-	//fprintf(stderr, "getNode(%s)\n", path);
 	// look up supplied path to see if node already exists
 	PathToNode::iterator pos = fNodes.find(path);
 	if ( pos != fNodes.end() )
@@ -356,40 +317,19 @@
 		//fprintf(stderr, "adding %s node alias %s for %s\n", archName(fArchPair), node->getLayout()->getID().name, realPath);
 		pos = fNodes.find(node->getLayout()->getID().name);
 		if ( pos != fNodes.end() ) {
-			// get uuids of two dylibs to see if this is accidental copy of a dylib or two differnent dylibs with same -install_name
-			uuid_t uuid1;
-			uuid_t uuid2;
-			node->getLayout()->uuid(uuid1);
-			pos->second->getLayout()->uuid(uuid2);
-			if ( memcmp(&uuid1, &uuid2, 16) == 0 ) {
-				// <rdar://problem/8305479> warn if two dylib in cache have same install_name
-				char* msg;
-				asprintf(&msg, "update_dyld_shared_cache: warning, found two copies of the same dylib with same install path: %s\n\t%s\n\t%s\n", 
-										node->getLayout()->getID().name, pos->second->getPath(), node->getPath());
-				fprintf(stderr, "%s", msg);
-				warnings.push_back(msg);
-			}
-			else {
-				// <rdar://problem/12763450> update_dyld_shared_cache should fail if two images have same install name
-				fprintf(stderr, "update_dyld_shared_cache: found two different dylibs with same install path: %s\n\t%s\n\t%s\n", 
-							node->getLayout()->getID().name, pos->second->getPath(), node->getPath());
-				exit(1);
-			}
+			// <rdar://problem/8305479> warn if two dylib in cache have same install_name
+			char* msg;
+			asprintf(&msg, "update_dyld_shared_cache: warning, found two dylibs with same install path: %s\n\t%s\n\t%s\n", 
+									node->getLayout()->getID().name, pos->second->getPath(), node->getPath());
+			fprintf(stderr, "%s", msg);
+			warnings.push_back(msg);
 		}
 		else
 			fNodes[node->getLayout()->getID().name] = node;
 		// update fAliasesMap with symlinks found
 		const char* aliasPath = realPath;
-		if ( (fgFileSystemRoot != NULL) && (fgFileSystemRoot[0] != '\0') && (strncmp(realPath, fgFileSystemRoot, strlen(fgFileSystemRoot)) == 0) ) {
+		if ( (fgFileSystemRoot != NULL) && (strncmp(realPath, fgFileSystemRoot, strlen(fgFileSystemRoot)) == 0) ) {
 			aliasPath = &realPath[strlen(fgFileSystemRoot)];
-		}
-		// <rdar://problem/11192810> Too many aliases in -overlay mode
-		for (std::vector<const char*>::const_iterator it=fgFileSystemOverlays.begin(); it != fgFileSystemOverlays.end(); ++it) {
-			const char* overlayPath = *it;
-			if ( strncmp(realPath, overlayPath, strlen(overlayPath)) == 0 ) {
-				aliasPath = &realPath[strlen(overlayPath)];
-				break;
-			}
 		}
 		if ( fAliasesMap.find(aliasPath) == fAliasesMap.end() ) {
 			if ( strcmp(aliasPath, node->getLayout()->getID().name) != 0 ) {
@@ -462,7 +402,7 @@
 					fDependsOn.insert(fGraph->getNodeForVirtualPath(dependentPath));
 			}
 			catch (const char* msg) {
-				if ( it->weakImport || ! fLayout->hasSplitSegInfo() ) {
+				if ( it->weakImport && ! fLayout->hasSplitSegInfo() ) {
 					// ok to ignore missing weak imported dylibs from things that are
 					// not going to be in the dyld shared cache
 				}
@@ -490,32 +430,16 @@
 }
 
 
-
 ArchGraph::DependencyNode::DependencyNode(ArchGraph* graph, const char* path, const MachOLayoutAbstraction* layout) 
  : fGraph(graph), fPath(strdup(path)), fLayout(layout), fDependenciesLoaded(false), fDependentMissing(false)
 {
 	//fprintf(stderr, "new DependencyNode(0x%08X, %s)\n", graph->fArch, path);
 }
 
-uint64_t ArchGraph::maxCacheSizeForArchPair(ArchPair ap) {
-	switch ( ap.arch ) {
-		case CPU_TYPE_I386:
-			return 0x20000000;
-		case CPU_TYPE_X86_64:
-			return 0x40000000;
-		case CPU_TYPE_ARM:
-			return ARM_SHARED_REGION_SIZE;
-		case CPU_TYPE_ARM64:
-			return ARM64_SHARED_REGION_SIZE;
-		default: return UINT64_MAX;
-	}
-}
-
 void ArchGraph::findSharedDylibs(ArchPair ap)
 {
 	const PathToNode& nodes = fgPerArchGraph[ap]->fNodes;
 	std::set<const MachOLayoutAbstraction*> possibleLibs;
-	std::map<const MachOLayoutAbstraction*, const DependencyNode *> layoutToNode;
 	//fprintf(stderr, "shared for arch %s\n", archName(ap));
 	for(PathToNode::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
 		DependencyNode* node = it->second;
@@ -526,10 +450,9 @@
 				char* msg;
 				if ( sharable(layout, ap, &msg) ) {
 					possibleLibs.insert(layout);
-					layoutToNode[layout] = node;
 				}
 				else {
-					if ( !iPhoneOS && (layout->getID().name[0] == '@') ) {
+					if ( layout->getID().name[0] == '@' ) {
 						// <rdar://problem/7770139> update_dyld_shared_cache should suppress warnings for embedded frameworks
 					}
 					else {
@@ -544,63 +467,21 @@
 	// prune so that all shareable libs depend only on other shareable libs
 	std::set<const MachOLayoutAbstraction*>& sharedLibs = fgPerArchGraph[ap]->fSharedDylibs;
 	std::map<const MachOLayoutAbstraction*,bool> shareableMap;
-	uint64_t totalLibSize = 0;
 	for (std::set<const MachOLayoutAbstraction*>::iterator lit = possibleLibs.begin(); lit != possibleLibs.end(); ++lit) {
-		if ( canBeShared(*lit, ap, possibleLibs, shareableMap) ) {
-			totalLibSize += (*lit)->getVMSize();
+		if ( canBeShared(*lit, ap, possibleLibs, shareableMap) )
 			sharedLibs.insert(*lit);
-		}
-	}
-
-#if 0  // disable auto-eviction because it happens before linkedit optimization which means it is overly conservative.
-
-	// Check to see if the unoptimized cache size is too large, if so trim out some libraries
-	uint64_t maxCacheSize = maxCacheSizeForArchPair(ap);
-	if (totalLibSize > maxCacheSize) {
-		fprintf(stderr, "update_dyld_shared_cache: unoptimized %s shared cache overflow, total VM space: %lldMB (max=%lldMB)\n", archName(ap), totalLibSize/(1024*1024), maxCacheSize/(1024*1024));
-		std::vector<const MachOLayoutAbstraction*> removableLibs;
-
-		for (const MachOLayoutAbstraction* layout : sharedLibs) {
-			// Every library uses itself, and every MH_DYLIB has an extra useCount, so we know useCount of 2 implies nothing else in the shared cache uses it
-			if (layoutToNode[layout]->useCount() == 2) {
-				if ( layoutToNode[layout]->dependsOnDylibList() ) {
-					removableLibs.push_back(layout);
-					//fprintf(stderr, "  possible to evict: %s\n", layout->getID().name);
-				}
-			}
-		}
-
-		std::sort(removableLibs.begin(), removableLibs.end(),  [](const MachOLayoutAbstraction* a, const MachOLayoutAbstraction* b){
-			return a->getVMSize() < b->getVMSize();
-		});
-
-		while ( (totalLibSize > maxCacheSize) && !removableLibs.empty() ) {
-			const MachOLayoutAbstraction* largestRemovableLib = removableLibs.back();
-			removableLibs.pop_back();
-			if ( largestRemovableLib->getVMSize() > 1024*1024 )
-				fprintf(stderr, "update_dyld_shared_cache: evicting % 3lldMB leaf dylib %s\n", largestRemovableLib->getVMSize()/(1024*1024), largestRemovableLib->getID().name);
-			else
-				fprintf(stderr, "update_dyld_shared_cache: evicting % 3lldKB leaf dylib %s\n", largestRemovableLib->getVMSize()/1024, largestRemovableLib->getID().name);
-			sharedLibs.erase(largestRemovableLib);
-			totalLibSize -= largestRemovableLib->getVMSize();
-		}
-		fprintf(stderr, "update_dyld_shared_cache: unoptimized %s shared cache reduced to total VM space: %lldMB\n", archName(ap), totalLibSize/1024/1024);
-	}
-#endif
+	}
 }
 
 const char*	ArchGraph::archName(ArchPair ap)
 {
 	switch ( ap.arch ) {
+		case CPU_TYPE_POWERPC:
+			return "ppc";
 		case CPU_TYPE_I386:
 			return "i386";
 		case CPU_TYPE_X86_64:
-			switch ( ap.subtype ) {
-				case CPU_SUBTYPE_X86_64_H:
-					return "x86_64h";
-				default:
-					return "x86_64";
-			}
+			return "x86_64";
 		case CPU_TYPE_ARM:
 			switch ( ap.subtype ) {
 				case CPU_SUBTYPE_ARM_V4T:
@@ -613,17 +494,9 @@
 					return "arm-xscale";
 				case CPU_SUBTYPE_ARM_V7:
 					return "armv7";
-				case CPU_SUBTYPE_ARM_V7F:
-					return "armv7f";
-				case CPU_SUBTYPE_ARM_V7K:
-					return "armv7k";
-				case CPU_SUBTYPE_ARM_V7S:
-					return "armv7s";
 				default:
 					return "arm";
 			}
-		case CPU_TYPE_ARM64:
-			return "arm64";
 		default:
 			return "unknown";
 	}
@@ -631,19 +504,20 @@
 
 bool ArchGraph::sharable(const MachOLayoutAbstraction* layout, ArchPair ap, char** msg)
 {
-	int trustErr = layout->notTrusted();
 	if ( ! layout->isTwoLevelNamespace() ) 
 		asprintf(msg, "can't put %s in shared cache because it was built -flat_namespace", layout->getID().name);
-	else if ( ! layout->inSharableLocation() )
-		asprintf(msg, "can't put %s in shared cache because its -install_name is not in /usr/lib or /System/Library", layout->getID().name);
 	else if ( ! layout->hasSplitSegInfo() ) 
 		asprintf(msg, "can't put %s in shared cache because it was not built for %s or later", layout->getID().name, (iPhoneOS ? "iPhoneOS 3.1" : "MacOSX 10.5"));
-	else if ( rootless == true && trustErr != 0 )
-		asprintf(msg, "can't put %s in shared cache because it is not trusted: %s", layout->getFilePath(), strerror(trustErr));
+	else if ( ! layout->isRootOwned() )
+		asprintf(msg, "can't put %s in shared cache because it is not owned by root", layout->getID().name);
+	else if ( ! layout->inSharableLocation() )
+		asprintf(msg, "can't put %s in shared cache because it is not in /usr/lib or /System/Library", layout->getID().name);
 	else if ( layout->hasDynamicLookupLinkage() )
 		asprintf(msg, "can't put %s in shared cache because it was built with '-undefined dynamic_lookup'", layout->getID().name);
 	else if ( layout->hasMainExecutableLookupLinkage() )
 		asprintf(msg, "can't put %s in shared cache because it was built with '-bundle_loader'", layout->getID().name);
+	//else if ( ! layout->hasDyldInfo() )
+	//	asprintf(msg, "can't put %s in shared cache because it was built for older OS", layout->getID().name);
 	else
 		return true;
 	return false;
@@ -680,9 +554,6 @@
 				if ( nodes.find(realPath) != nodes.end() )
 					continue;
 			}
-			// handle weak imported dylibs not found
-			if ( dit->weakImport )
-				continue;
 			shareableMap[layout] = false;
 			char* msg;
 			asprintf(&msg, "can't put %s in shared cache because it depends on %s which can't be found", layout->getID().name, dit->name);
@@ -707,92 +578,13 @@
 }
 
 
-
-class StringPool
-{
-public:
-				StringPool();
-	const char*	getBuffer();
-	uint32_t	size();
-	uint32_t	add(const char* str);
-	uint32_t	addUnique(const char* str);
-	const char* stringAtIndex(uint32_t) const;
-	
-private:
-	typedef std::unordered_map<const char*, uint32_t, CStringHash, CStringEquals> StringToOffset;
-
-	char*			fBuffer;
-	uint32_t		fBufferAllocated;
-	uint32_t		fBufferUsed;
-	StringToOffset	fUniqueStrings;
-};
-
-
-StringPool::StringPool() 
-	: fBufferUsed(0), fBufferAllocated(128*1024*1024)
-{
-	fBuffer = (char*)malloc(fBufferAllocated);
-}
-
-uint32_t StringPool::add(const char* str)
-{
-	uint32_t len = strlen(str);
-	if ( (fBufferUsed + len + 1) > fBufferAllocated ) {
-		// grow buffer
-		throw "string buffer exhausted";
-	}
-	strcpy(&fBuffer[fBufferUsed], str);
-	uint32_t result = fBufferUsed;
-	fUniqueStrings[&fBuffer[fBufferUsed]] = result;
-	fBufferUsed += len+1;
-	return result;
-}
-
-uint32_t StringPool::addUnique(const char* str)
-{
-	StringToOffset::iterator pos = fUniqueStrings.find(str);
-	if ( pos != fUniqueStrings.end() ) 
-		return pos->second;
-	else {
-		//fprintf(stderr, "StringPool::addUnique() new string: %s\n", str);
-		return this->add(str);
-	}
-}
-
-uint32_t StringPool::size()
-{
-	return fBufferUsed;
-}
-
-const char*	StringPool::getBuffer()
-{
-	return fBuffer;
-}
-
-const char* StringPool::stringAtIndex(uint32_t index) const
-{
-	return &fBuffer[index];
-}
-
-
-
-struct LocalSymbolInfo
-{
-	uint32_t	dylibOffset;
-	uint32_t	nlistStartIndex;
-	uint32_t	nlistCount;
-};
-
-
 template <typename A>
 class SharedCache
 {
 public:
-							SharedCache(ArchGraph* graph, const char* rootPath, const std::vector<const char*>& overlayPaths, const char* cacheDir, bool explicitCacheDir,
-											bool alphaSort, bool verify, bool optimize, uint64_t dyldBaseAddress);
-	bool					update(bool force, bool optimize, bool deleteExistingFirst, int archIndex, 
-										int archCount, bool keepSignatures, bool dontMapLocalSymbols);
-	void					writeCacheFile(const char *cacheFilePath, uint8_t *cacheFileBuffer, uint32_t cacheFileSize, bool deleteOldCache);
+							SharedCache(ArchGraph* graph, const char* rootPath, const char* cacheDir, bool alphaSort, bool verify, bool optimize, bool overlay, uint64_t dyldBaseAddress);
+	bool					update(bool usesOverlay, bool force, bool optimize, bool deleteExistingFirst, int archIndex, 
+										int archCount, bool keepSignatures);
 	static const char*		cacheFileSuffix(bool optimized, const char* archName);
 
     // vm address = address AS WRITTEN into the cache
@@ -803,8 +595,6 @@
 	uint64_t				cacheFileOffsetForVMAddress(uint64_t addr) const;
 	uint64_t				VMAddressForCacheFileOffset(uint64_t addr) const;
 
-	static const char*		archName();
-
 private:
 	typedef typename A::P			P;
     typedef typename A::P::E		E;
@@ -812,22 +602,19 @@
 
 	bool					notUpToDate(const char* path, unsigned int aliasCount);
 	bool					notUpToDate(const void* cache, unsigned int aliasCount);
-	uint8_t*				optimizeLINKEDIT(bool keepSignatures, bool dontMapLocalSymbols);
+	uint8_t*				optimizeLINKEDIT(bool keepSignatures);
 	void					optimizeObjC(std::vector<void*>& pointersInData);
 
 	static void				getSharedCacheBasAddresses(cpu_type_t arch, uint64_t* baseReadOnly, uint64_t* baseWritable);
 	static cpu_type_t		arch();
-	static uint64_t			sharedRegionStartAddress();
-	static uint64_t			sharedRegionSize();
-	static uint64_t			sharedRegionStartWritableAddress(uint64_t);
-	static uint64_t			sharedRegionStartReadOnlyAddress(uint64_t, uint64_t);
+	static const char*		archName();
+	static uint64_t			sharedRegionReadOnlyStartAddress();
+	static uint64_t			sharedRegionWritableStartAddress();
+	static uint64_t			sharedRegionReadOnlySize();
+	static uint64_t			sharedRegionWritableSize();
 	static uint64_t			getWritableSegmentNewAddress(uint64_t proposedNewAddress, uint64_t originalAddress, uint64_t executableSlide);
 	static bool				addCacheSlideInfo();
-	static uint64_t			pathHash(const char*);
-	
-	static uint64_t			pageAlign(uint64_t addr);
-	static uint64_t			regionAlign(uint64_t addr);
-	static uint64_t			pageAlign4KB(uint64_t addr);
+	
 	void					assignNewBaseAddresses(bool verify);
 
 	struct LayoutInfo {
@@ -839,12 +626,6 @@
 	struct ByNameSorter {
 		bool operator()(const LayoutInfo& left, const LayoutInfo& right) 
 				{ return (strcmp(left.layout->getID().name, right.layout->getID().name) < 0); }
-	};
-	
-	struct ByAddressSorter {
-		bool operator()(const LayoutInfo& left, const LayoutInfo& right) { 
-			return (left.layout->getSegments()[0].newAddress() < right.layout->getSegments()[0].newAddress()); 
-		}
 	};
 
     struct ByCStringSectionSizeSorter {
@@ -889,9 +670,6 @@
 	std::vector<LayoutInfo>				fDylibs;
 	std::vector<LayoutInfo>				fDylibAliases;
 	std::vector<shared_file_mapping_np>	fMappings;
-	std::vector<macho_nlist<P> >		fUnmappedLocalSymbols;
-	StringPool							fUnmappedLocalsStringPool;
-	std::vector<LocalSymbolInfo>		fLocalSymbolInfos;
 	uint32_t							fHeaderSize;
     uint8_t*							fInMemoryCache;
 	uint64_t							fDyldBaseAddress;
@@ -912,10 +690,7 @@
 	uint32_t							fSizeOfOldStringPoolInCombinedLinkedit;
 	uint32_t							fOffsetOfFunctionStartsInCombinedLinkedit;
 	uint32_t							fSizeOfFunctionStartsInCombinedLinkedit;
-	uint32_t							fOffsetOfDataInCodeInCombinedLinkedit;
-	uint32_t							fSizeOfDataInCodeInCombinedLinkedit;
 	uint32_t							fLinkEditsTotalOptimizedSize;
-	uint32_t							fUnmappedLocalSymbolsSize;
 };
 
 
@@ -929,7 +704,7 @@
     SharedCache<A>* const			fCache;
     const macho_section<P>* const	fSection;
     pint_t * const					fBase;
-    pint_t							fCount;
+    uint64_t						fCount;
 
 public:
     PointerSection(SharedCache<A>* cache, const macho_header<P>* header, 
@@ -941,25 +716,25 @@
     {
     }
 
-    pint_t count() const { return fCount; }
-
-    pint_t getVMAddress(pint_t index) const {
+    uint64_t count() const { return fCount; }
+
+    uint64_t getUnmapped(uint64_t index) const {
         if (index >= fCount) throwf("index out of range");
         return P::getP(fBase[index]);
     }
 
-    T get(pint_t index) const { 
-        return (T)fCache->mappedAddressForVMAddress(getVMAddress(index));
+    T get(uint64_t index) const { 
+        return (T)fCache->mappedAddressForVMAddress(getUnmapped(index));
     }
 
-    void setVMAddress(pint_t index, pint_t value) {
+    void set(uint64_t index, uint64_t value) {
         if (index >= fCount) throwf("index out of range");
         P::setP(fBase[index], value);
     }
 	
     void removeNulls() {
-        pint_t shift = 0;
-        for (pint_t i = 0; i < fCount; i++) {
+        uint64_t shift = 0;
+        for (uint64_t i = 0; i < fCount; i++) {
             pint_t value = fBase[i];
             if (value) {
                 fBase[i-shift] = value;
@@ -1008,57 +783,44 @@
 
 
 	
+template <>	 cpu_type_t	SharedCache<ppc>::arch()	{ return CPU_TYPE_POWERPC; }
 template <>	 cpu_type_t	SharedCache<x86>::arch()	{ return CPU_TYPE_I386; }
 template <>	 cpu_type_t	SharedCache<x86_64>::arch()	{ return CPU_TYPE_X86_64; }
 template <>	 cpu_type_t	SharedCache<arm>::arch()	{ return CPU_TYPE_ARM; }
-template <>	 cpu_type_t	SharedCache<arm64>::arch()	{ return CPU_TYPE_ARM64; }
-
-template <>	 uint64_t	SharedCache<x86>::sharedRegionStartAddress()			{ return 0x90000000; }
-template <>	 uint64_t	SharedCache<x86_64>::sharedRegionStartAddress()			{ return 0x7FFF80000000LL; }
-template <>	 uint64_t	SharedCache<arm>::sharedRegionStartAddress()			{ return ARM_SHARED_REGION_START; }
-template <>	 uint64_t	SharedCache<arm64>::sharedRegionStartAddress()			{ return ARM64_SHARED_REGION_START; }
-
-template <>	 uint64_t	SharedCache<x86>::sharedRegionSize()					{ return 0x20000000; }
-template <>	 uint64_t	SharedCache<x86_64>::sharedRegionSize()					{ return 0x40000000; }
-template <>	 uint64_t	SharedCache<arm>::sharedRegionSize()					{ return ARM_SHARED_REGION_SIZE; }
-template <>	 uint64_t	SharedCache<arm64>::sharedRegionSize()					{ return ARM64_SHARED_REGION_SIZE; }
-
-template <>	 uint64_t	SharedCache<x86>::sharedRegionStartWritableAddress(uint64_t exEnd)			{ return exEnd + 0x04000000; }
-template <>	 uint64_t	SharedCache<x86_64>::sharedRegionStartWritableAddress(uint64_t exEnd)		{ return 0x7FFF70000000LL; }
-template <>	 uint64_t	SharedCache<arm>::sharedRegionStartWritableAddress(uint64_t exEnd)			{ return (exEnd + 16383) & (-16384); }
-template <>	 uint64_t	SharedCache<arm64>::sharedRegionStartWritableAddress(uint64_t exEnd)		{ return exEnd; }
-
-template <>	 uint64_t	SharedCache<x86>::sharedRegionStartReadOnlyAddress(uint64_t wrEnd, uint64_t exEnd)	 { return wrEnd + 0x04000000; }
-template <>	 uint64_t	SharedCache<x86_64>::sharedRegionStartReadOnlyAddress(uint64_t wrEnd, uint64_t exEnd){ return exEnd; }
-template <>	 uint64_t	SharedCache<arm>::sharedRegionStartReadOnlyAddress(uint64_t wrEnd, uint64_t exEnd)	 { return (wrEnd + 16383) & (-16384); }
-template <>	 uint64_t	SharedCache<arm64>::sharedRegionStartReadOnlyAddress(uint64_t wrEnd, uint64_t exEnd) { return (wrEnd + 16383) & (-16384); }
-
+
+template <>	 uint64_t	SharedCache<ppc>::sharedRegionReadOnlyStartAddress()	{ return 0x90000000; }
+template <>	 uint64_t	SharedCache<x86>::sharedRegionReadOnlyStartAddress()	{ return 0x90000000; }
+template <>	 uint64_t	SharedCache<x86_64>::sharedRegionReadOnlyStartAddress()	{ return 0x7FFF80000000LL; }
+template <>	 uint64_t	SharedCache<arm>::sharedRegionReadOnlyStartAddress()	{ return 0x30000000; }
+
+template <>	 uint64_t	SharedCache<ppc>::sharedRegionWritableStartAddress()	{ return 0xA0000000; }
+template <>	 uint64_t	SharedCache<x86>::sharedRegionWritableStartAddress()	{ return 0xAC000000; }
+template <>	 uint64_t	SharedCache<x86_64>::sharedRegionWritableStartAddress()	{ return 0x7FFF70000000LL; }
+template <>	 uint64_t	SharedCache<arm>::sharedRegionWritableStartAddress()	{ return 0x3E000000; }
+
+template <>	 uint64_t	SharedCache<ppc>::sharedRegionReadOnlySize()			{ return 0x10000000; }
+template <>	 uint64_t	SharedCache<x86>::sharedRegionReadOnlySize()			{ return 0x1C000000; }
+template <>	 uint64_t	SharedCache<x86_64>::sharedRegionReadOnlySize()			{ return 0x40000000; }
+template <>	 uint64_t	SharedCache<arm>::sharedRegionReadOnlySize()			{ return 0x0E000000; }
+
+template <>	 uint64_t	SharedCache<ppc>::sharedRegionWritableSize()			{ return 0x10000000; }
+template <>	 uint64_t	SharedCache<x86>::sharedRegionWritableSize()			{ return 0x04000000; }
+template <>	 uint64_t	SharedCache<x86_64>::sharedRegionWritableSize()			{ return 0x10000000; }
+template <>	 uint64_t	SharedCache<arm>::sharedRegionWritableSize()			{ return 0x02000000; }
+
+
+template <>	 const char*	SharedCache<ppc>::archName()	{ return "ppc"; }
 template <>	 const char*	SharedCache<x86>::archName()	{ return "i386"; }
 template <>	 const char*	SharedCache<x86_64>::archName()	{ return "x86_64"; }
 template <>	 const char*	SharedCache<arm>::archName()	{ return "arm"; }
-template <>	 const char*	SharedCache<arm64>::archName()	{ return "arm64"; }
-
+
+template <>	 const char*	SharedCache<ppc>::cacheFileSuffix(bool optimized, const char*)	{ return optimized ? "ppc" : "rosetta"; }
 template <>	 const char*	SharedCache<x86>::cacheFileSuffix(bool, const char* archName)	{ return archName; }
 template <>	 const char*	SharedCache<x86_64>::cacheFileSuffix(bool, const char* archName){ return archName; }
 template <>	 const char*	SharedCache<arm>::cacheFileSuffix(bool, const char* archName)	{ return archName; }
-template <>	 const char*	SharedCache<arm64>::cacheFileSuffix(bool, const char* archName)	{ return archName; }
-
-template <>  uint64_t		SharedCache<x86>::pageAlign(uint64_t addr)    { return ( (addr + 4095) & (-4096) ); }
-template <>  uint64_t		SharedCache<x86_64>::pageAlign(uint64_t addr) { return ( (addr + 4095) & (-4096) ); }
-template <>  uint64_t		SharedCache<arm>::pageAlign(uint64_t addr)    { return ( (addr + 4095) & (-4096) ); }
-template <>  uint64_t		SharedCache<arm64>::pageAlign(uint64_t addr)  { return ( (addr + 16383) & (-16384) ); }
-
-template <>  uint64_t		SharedCache<x86>::regionAlign(uint64_t addr)    { return ( (addr + 4095) & (-4096) ); }
-template <>  uint64_t		SharedCache<x86_64>::regionAlign(uint64_t addr) { return ( (addr + 4095) & (-4096) ); }
-template <>  uint64_t		SharedCache<arm>::regionAlign(uint64_t addr)    { return ( (addr + 16383) & (-16384) ); }
-template <>  uint64_t		SharedCache<arm64>::regionAlign(uint64_t addr)  { return ( (addr + 16383) & (-16384) ); }
-
-
-template <typename A>  
-uint64_t SharedCache<A>::pageAlign4KB(uint64_t addr)    { return ( (addr + 4095) & (-4096) ); }
 
 template <typename A>
-SharedCache<A>::SharedCache(ArchGraph* graph, const char* rootPath, const std::vector<const char*>& overlayPaths, const char* cacheDir, bool explicitCacheDir, bool alphaSort, bool verify, bool optimize, uint64_t dyldBaseAddress) 
+SharedCache<A>::SharedCache(ArchGraph* graph, const char* rootPath, const char* cacheDir, bool alphaSort, bool verify, bool optimize, bool overlay, uint64_t dyldBaseAddress) 
   : fArchGraph(graph), fVerify(verify), fExistingIsNotUpToDate(true), 
 	fCacheFileInFinalLocation(rootPath[0] == '\0'), fCacheFilePath(NULL),
 	fExistingCacheForVerification(NULL), fDyldBaseAddress(dyldBaseAddress),
@@ -1068,9 +830,7 @@
 	fOffsetOfOldExternalRelocationsInCombinedLinkedit(0), fSizeOfOldExternalRelocationsInCombinedLinkedit(0),
 	fOffsetOfOldIndirectSymbolsInCombinedLinkedit(0), fSizeOfOldIndirectSymbolsInCombinedLinkedit(0),
 	fOffsetOfOldStringPoolInCombinedLinkedit(0), fSizeOfOldStringPoolInCombinedLinkedit(0),
-	fOffsetOfFunctionStartsInCombinedLinkedit(0), fSizeOfFunctionStartsInCombinedLinkedit(0),
-	fOffsetOfDataInCodeInCombinedLinkedit(0), fSizeOfDataInCodeInCombinedLinkedit(0),
-	fUnmappedLocalSymbolsSize(0)
+	fOffsetOfFunctionStartsInCombinedLinkedit(0), fSizeOfFunctionStartsInCombinedLinkedit(0)
 {
 	if ( fArchGraph->getArchPair().arch != arch() )
 		throwf("SharedCache object is wrong architecture: 0x%08X vs 0x%08X", fArchGraph->getArchPair().arch, arch());
@@ -1084,12 +844,8 @@
 		LayoutInfo temp;
 		temp.layout = lib;
 		temp.info.address = 0;
+		temp.info.modTime = lib->getLastModTime();
 		temp.info.inode = lib->getInode();
-		temp.info.modTime = lib->getLastModTime();
-		if ( iPhoneOS ) {
-			temp.info.inode = pathHash(lib->getID().name);
-			temp.info.modTime = 0;
-		}
 		temp.info.pathFileOffset = lib->getNameFileOffset();  // for now this is the offset within the dylib
 		for(ArchGraph::StringToString::iterator ait = aliases.begin(); ait != aliases.end(); ++ait) {
 			if ( strcmp(ait->second, lib->getID().name) == 0 ) {
@@ -1101,56 +857,29 @@
 	}
 
 	// create path to cache file
-	char cachePathCanonical[MAXPATHLEN];
-	strcpy(cachePathCanonical, cacheDir);
-	if ( cachePathCanonical[strlen(cachePathCanonical)-1] != '/' )
-		strcat(cachePathCanonical, "/");
-	strcat(cachePathCanonical, DYLD_SHARED_CACHE_BASE_NAME);
-	strcat(cachePathCanonical, cacheFileSuffix(optimize, fArchGraph->archName()));
-	char cachePath[MAXPATHLEN];
-	if ( explicitCacheDir ) {
-		fCacheFilePath = strdup(cachePathCanonical);
-	}
-	else if ( overlayPaths.size() == 1 ) {
-		// if no -cache_dir and exactly on -overlay, write cache file into that overlay dir
-		strcpy(cachePath, overlayPaths[0]);
-		strcat(cachePath, "/");
-		strcat(cachePath, cachePathCanonical);
+	char cachePathNonOverlay[1024];
+	strcpy(cachePathNonOverlay, cacheDir);
+	if ( cachePathNonOverlay[strlen(cachePathNonOverlay)-1] != '/' )
+		strcat(cachePathNonOverlay, "/");
+	strcat(cachePathNonOverlay, DYLD_SHARED_CACHE_BASE_NAME);
+	strcat(cachePathNonOverlay, cacheFileSuffix(optimize, fArchGraph->archName()));
+	char cachePath[1024];
+	strcpy(cachePath, rootPath);
+	strcat(cachePath, "/");
+	strcat(cachePath, cachePathNonOverlay);
+	if ( !overlay && (rootPath[0] != '\0') )
+		fCacheFilePath = strdup(cachePathNonOverlay);
+	else
 		fCacheFilePath = strdup(cachePath);
-	}
-	else if ( rootPath[0] != '\0' ) {
-		strcpy(cachePath, rootPath);
-		strcat(cachePath, "/");
-		strcat(cachePath, cachePathCanonical);
-		fCacheFilePath = strdup(cachePath);
-	}
-	else {
-		fCacheFilePath = strdup(cachePathCanonical);
-	}
-
-	// If the path we are writing to is trusted then our sources need to be trusted
-	// <rdar://problem/21166835> Can't update the update_dyld_shared_cache on a non-boot volume
-	rootless = rootless_check_trusted(fCacheFilePath);
-
-	if ( overlayPaths.size() == 1 ) {
-		// in overlay mode if there already is a cache file in the overlay,
-		// check if it is up to date.  
+	if ( overlay ) {
+		// in overlay mode if there already is a cache file in the overlay
+		// check if it is up to date.  If there is no file, check if
+		// the one in the boot volume is up to date.
 		struct stat stat_buf;
-		if ( stat(fCacheFilePath, &stat_buf) == 0 ) {
+		if ( stat(fCacheFilePath, &stat_buf) == 0 ) 
 			fExistingIsNotUpToDate = this->notUpToDate(fCacheFilePath, aliasCount);
-		}
-		else if ( rootPath[0] != '\0' ) {
-			// using -root and -overlay, but no cache file in overlay, check one in -root
-			char cachePathRoot[MAXPATHLEN];
-			strcpy(cachePathRoot, rootPath);
-			strcat(cachePathRoot, "/");
-			strcat(cachePathRoot, cachePathCanonical);
-			fExistingIsNotUpToDate = this->notUpToDate(cachePathRoot, aliasCount);
-		}
-		else {
-			// uisng -overlay, but no cache file in overlay, check one in boot volume
-			fExistingIsNotUpToDate = this->notUpToDate(cachePathCanonical, aliasCount);
-		}
+		else
+			fExistingIsNotUpToDate = this->notUpToDate(cachePathNonOverlay, aliasCount);
 	}
 	else {
 		fExistingIsNotUpToDate = this->notUpToDate(fCacheFilePath, aliasCount);
@@ -1179,7 +908,6 @@
 	fHeaderSize = sizeof(dyld_cache_header) 
 							+ fMappings.size()*sizeof(shared_file_mapping_np) 
 							+ (fDylibs.size()+aliasCount)*sizeof(dyld_cache_image_info);
-	const uint64_t baseHeaderSize = fHeaderSize;
 	//fprintf(stderr, "aliasCount=%d, fHeaderSize=0x%08X\n", aliasCount, fHeaderSize);
 	// build list of aliases and compute where each ones path string will go
 	for(typename std::vector<struct LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
@@ -1190,10 +918,6 @@
 			temp.aliases.clear();
 			temp.aliases.push_back(aliasPath);
 			temp.info.pathFileOffset = fHeaderSize; 
-			if ( iPhoneOS ) {
-				temp.info.inode = pathHash(aliasPath); 
-				temp.info.modTime = 0;
-			}
 			fDylibAliases.push_back(temp);
 			fHeaderSize += strlen(aliasPath)+1;
 		}
@@ -1207,21 +931,21 @@
 		// if no existing cache, say so
 		if ( fExistingCacheForVerification == NULL ) {
 			throwf("update_dyld_shared_cache[%u] for arch=%s, could not verify because cache file does not exist in /var/db/dyld/\n",
-			 getpid(), fArchGraph->archName());
+			 getpid(), archName());
 		}
 		const dyldCacheHeader<E>* header = (dyldCacheHeader<E>*)fExistingCacheForVerification;
 		const dyldCacheImageInfo<E>* cacheEntry = (dyldCacheImageInfo<E>*)(fExistingCacheForVerification + header->imagesOffset());
 		for(typename std::vector<LayoutInfo>::iterator it = fDylibs.begin(); it != fDylibs.end(); ++it, ++cacheEntry) {
 			if ( cacheEntry->address() != it->layout->getSegments()[0].newAddress() ) {
 				throwf("update_dyld_shared_cache[%u] warning: for arch=%s, could not verify cache because start address of %s is 0x%llX in cache, but should be 0x%llX\n",
-							getpid(), fArchGraph->archName(), it->layout->getID().name, cacheEntry->address(), it->layout->getSegments()[0].newAddress());
+							getpid(), archName(), it->layout->getID().name, cacheEntry->address(), it->layout->getSegments()[0].newAddress());
 			}
 		}
 	}
 	
 	
 	if ( fHeaderSize > FIRST_DYLIB_TEXT_OFFSET )
-		throwf("header size overflow: allowed=0x%08X, base=0x%08llX, aliases=0x%08llX", FIRST_DYLIB_TEXT_OFFSET, baseHeaderSize, fHeaderSize-baseHeaderSize);
+		throwf("header size miscalculation 0x%08X", fHeaderSize);
 }
 
 
@@ -1231,159 +955,143 @@
 	return proposedNewAddress;
 }
 
-template <typename A>
-uint64_t SharedCache<A>::pathHash(const char* path)
-{
-	uint64_t sum = 0;
-	for (const char* s=path; *s != '\0'; ++s)
-		sum += sum*4 + *s;
-	return sum;
-}
-	
+template <>
+uint64_t SharedCache<ppc>::getWritableSegmentNewAddress(uint64_t proposedNewAddress, uint64_t originalAddress, uint64_t executableSlide)
+{
+	// for ppc writable segments can only move in increments of 64K (so only hi16 instruction needs to be modified)
+	return (((executableSlide & 0x000000000000F000ULL) - ((proposedNewAddress - originalAddress) & 0x000000000000F000ULL)) & 0x000000000000F000ULL) + proposedNewAddress;
+}
+
 
 template <typename A>
 void SharedCache<A>::assignNewBaseAddresses(bool verify)
 {
-	// first layout TEXT for dylibs
-	const uint64_t startExecuteAddress = sharedRegionStartAddress();
-	uint64_t currentExecuteAddress = startExecuteAddress + FIRST_DYLIB_TEXT_OFFSET;	
+	uint64_t sharedCacheStartAddress = sharedRegionReadOnlyStartAddress();
+#if 0
+	if ( arch() == CPU_TYPE_X86_64 ) {
+		if ( verify ) {
+			if ( fExistingCacheForVerification == NULL ) {
+				throwf("update_dyld_shared_cache[%u] for arch=%s, could not verify because cache file does not exist in /var/db/dyld/\n",
+					   getpid(), archName());
+			}
+			const dyldCacheHeader<E>* header = (dyldCacheHeader<E>*)fExistingCacheForVerification;
+			const dyldCacheFileMapping<E>* mappings = (dyldCacheFileMapping<E>*)(fExistingCacheForVerification + header->mappingOffset());
+			sharedCacheStartAddress = mappings[0].address();
+		}
+		else {
+			// <rdar://problem/5274722> dyld shared cache can be more random
+			uint64_t readOnlySize = 0;
+			for(typename std::vector<LayoutInfo>::iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
+				if ( ! it->layout->hasSplitSegInfo() )
+					continue;
+				std::vector<MachOLayoutAbstraction::Segment>& segs = ((MachOLayoutAbstraction*)(it->layout))->getSegments();
+				for (int i=0; i < segs.size(); ++i) {
+					MachOLayoutAbstraction::Segment& seg = segs[i];
+					if ( ! seg.writable() )
+						readOnlySize += pageAlign(seg.size());
+				}
+			}
+			uint64_t maxSlide = sharedRegionReadOnlySize() - (readOnlySize + FIRST_DYLIB_TEXT_OFFSET);
+			sharedCacheStartAddress = sharedRegionReadOnlyStartAddress() + pageAlign(arc4random() % maxSlide);
+		}
+	}
+#endif
+	uint64_t currentExecuteAddress = sharedCacheStartAddress + FIRST_DYLIB_TEXT_OFFSET;	
+	uint64_t currentWritableAddress = sharedRegionWritableStartAddress() + FIRST_DYLIB_DATA_OFFSET;
+
+	// first layout TEXT and DATA for dylibs
 	for(typename std::vector<LayoutInfo>::iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
 		std::vector<MachOLayoutAbstraction::Segment>& segs = ((MachOLayoutAbstraction*)(it->layout))->getSegments();
+		MachOLayoutAbstraction::Segment* executableSegment = NULL;
 		for (int i=0; i < segs.size(); ++i) {
 			MachOLayoutAbstraction::Segment& seg = segs[i];
 			seg.reset();
-			if ( seg.executable() && !seg.writable() ) {
-				// <rdar://problem/15947734> Some dylib require extra alignment
-				currentExecuteAddress = (currentExecuteAddress + seg.alignment() - 1) & (-seg.alignment());
-				// __TEXT segment
-				if ( it->info.address == 0 )
-					it->info.address = currentExecuteAddress;
-				seg.setNewAddress(currentExecuteAddress);
-				currentExecuteAddress += pageAlign(seg.size());
-			}
-		}
-	}
-	// align __TEXT region
-	currentExecuteAddress = regionAlign(currentExecuteAddress);
-
-#define DENSE_PACK 0
-	// layout __DATA* segments
-	std::vector<MachOLayoutAbstraction::Segment*> dataSegs;
-	std::vector<MachOLayoutAbstraction::Segment*> dataConstSegs;
-	std::vector<MachOLayoutAbstraction::Segment*> dataDirtySegs;
-	const uint64_t startWritableAddress = sharedRegionStartWritableAddress(currentExecuteAddress);
-	uint64_t currentWritableAddress = startWritableAddress;
-	for (const LayoutInfo& info : fDylibs ) {
-		for (MachOLayoutAbstraction::Segment& seg : ((MachOLayoutAbstraction*)(info.layout))->getSegments()) {
 			if ( seg.writable() ) {
-				if ( seg.executable() ) 
-					throw "found writable and executable segment";
-				seg.reset();
-				if ( strcmp(seg.name(), "__DATA_CONST") == 0 )
-					dataConstSegs.push_back(&seg);
-				else if ( strcmp(seg.name(), "__DATA_DIRTY") == 0 )
-					dataDirtySegs.push_back(&seg);
-				else
-					dataSegs.push_back(&seg);
-			}
-		}
-	}
-	// coalesce all __DATA_CONST segments
-	for (MachOLayoutAbstraction::Segment* seg : dataConstSegs) {
-	#if DENSE_PACK
-		// start segment at needed alignment
-		currentWritableAddress = (currentWritableAddress + seg->sectionsAlignment() - 1) & (-seg->sectionsAlignment());
-		seg->setNewAddress(currentWritableAddress);
-		// pack together
-		uint64_t justSectionsSize = seg->sectionsSize();
-		currentWritableAddress = seg->newAddress() + justSectionsSize;
-		seg->setSize(justSectionsSize);
-		if ( seg->fileSize() > justSectionsSize )
-			seg->setFileSize(justSectionsSize);
-	#else
-		seg->setNewAddress(currentWritableAddress);
-		// pack to 4KB pages
-		currentWritableAddress = pageAlign4KB(seg->newAddress() + seg->size());
-	#endif
-	}
-	#if DENSE_PACK
-	currentWritableAddress = pageAlign4KB(currentWritableAddress);
-	#endif
-	// coalesce all __DATA segments
-	for (MachOLayoutAbstraction::Segment* seg : dataSegs) {
-	#if DENSE_PACK
-		// start segment at needed alignment
-		currentWritableAddress = (currentWritableAddress + seg->sectionsAlignment() - 1) & (-seg->sectionsAlignment());
-		seg->setNewAddress(currentWritableAddress);
-		// pack together
-		uint64_t justSectionsSize = seg->sectionsSize();
-		currentWritableAddress = seg->newAddress() + justSectionsSize;
-		seg->setSize(justSectionsSize);
-		if ( seg->fileSize() > justSectionsSize )
-			seg->setFileSize(justSectionsSize);
-	#else
-		seg->setNewAddress(currentWritableAddress);
-		// pack to 4KB pages
-		currentWritableAddress = pageAlign4KB(seg->newAddress() + seg->size());
-	#endif
-	}
-	#if DENSE_PACK
-	currentWritableAddress = pageAlign4KB(currentWritableAddress);
-	#endif
- 	// coalesce all __DATA_DIRTY segments
-	for (MachOLayoutAbstraction::Segment* seg : dataDirtySegs) {
-		// start segment at needed alignment
-		currentWritableAddress = (currentWritableAddress + seg->sectionsAlignment() - 1) & (-seg->sectionsAlignment());
-		seg->setNewAddress(currentWritableAddress);
-		// pack together
-		uint64_t justSectionsSize = seg->sectionsSize();
-		currentWritableAddress = seg->newAddress() + justSectionsSize;
-		seg->setSize(justSectionsSize);
-		if ( seg->fileSize() > justSectionsSize )
-			seg->setFileSize(justSectionsSize);
-	}
-	// align __DATA region
-	currentWritableAddress = regionAlign(currentWritableAddress);
-
-	// layout all read-only (but not LINKEDIT) segments
-	const uint64_t startReadOnlyAddress = sharedRegionStartReadOnlyAddress(currentWritableAddress, currentExecuteAddress);
-	uint64_t currentReadOnlyAddress = startReadOnlyAddress;
+				if ( seg.executable() && it->layout->hasSplitSegInfo() ) {
+					// skip __IMPORT segments in this pass
+				}
+				else {
+					// __DATA segment
+					// for ppc, writable segments have to move in 64K increments
+					if (  it->layout->hasSplitSegInfo() ) {
+						if ( executableSegment == NULL )
+							throwf("first segment in dylib is not executable for %s", it->layout->getID().name);
+						seg.setNewAddress(getWritableSegmentNewAddress(currentWritableAddress, seg.address(), executableSegment->newAddress() - executableSegment->address()));
+					}
+					else
+						seg.setNewAddress(currentWritableAddress);
+					currentWritableAddress = pageAlign(seg.newAddress() + seg.size());
+				}
+			}
+			else {
+				if ( seg.executable() ) {
+					// __TEXT segment
+					if ( it->info.address == 0 )
+						it->info.address = currentExecuteAddress;
+					executableSegment = &seg;
+					seg.setNewAddress(currentExecuteAddress);
+					currentExecuteAddress += pageAlign(seg.size());
+				}
+				else {
+					// skip read-only segments in this pass
+				}
+			}
+		}
+	}
+
+	// append all read-only (but not LINKEDIT) segments at end of all TEXT segments
+	// append all IMPORT segments at end of all DATA segments rounded to next 2MB 
+	uint64_t currentReadOnlyAddress = currentExecuteAddress;
+	uint64_t startWritableExecutableAddress = (currentWritableAddress + 0x200000 - 1) & (-0x200000);
+	uint64_t currentWritableExecutableAddress = startWritableExecutableAddress;
 	for(typename std::vector<LayoutInfo>::iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
 		std::vector<MachOLayoutAbstraction::Segment>& segs = ((MachOLayoutAbstraction*)(it->layout))->getSegments();
 		for(int i=0; i < segs.size(); ++i) {
 			MachOLayoutAbstraction::Segment& seg = segs[i];
-			if ( seg.readable() && !seg.writable() && !seg.executable() && (strcmp(seg.name(), "__LINKEDIT") != 0) ) {
-				// __UNICODE segment
+			if ( !seg.writable() && !seg.executable() && (strcmp(seg.name(), "__LINKEDIT") != 0) ) {
+				// allocate non-executable,read-only segments from end of read only shared region
 				seg.setNewAddress(currentReadOnlyAddress);
 				currentReadOnlyAddress += pageAlign(seg.size());
 			}
+			else if ( seg.writable() && seg.executable() && it->layout->hasSplitSegInfo() ) {
+				// allocate IMPORT segments to end of writable shared region
+				seg.setNewAddress(currentWritableExecutableAddress);
+				currentWritableExecutableAddress += pageAlign(seg.size());
+			}
 		}
 	}	
 
-	// layout all LINKEDIT segments at end of all read-only segments
-	currentReadOnlyAddress = regionAlign(currentReadOnlyAddress); // <rdar://problem/16491435>
+	// append all LINKEDIT segments at end of all read-only segments
 	fLinkEditsStartAddress = currentReadOnlyAddress;
 	fFirstLinkEditSegment = NULL;
 	for(typename std::vector<LayoutInfo>::iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
 		std::vector<MachOLayoutAbstraction::Segment>& segs = ((MachOLayoutAbstraction*)(it->layout))->getSegments();
 		for(int i=0; i < segs.size(); ++i) {
 			MachOLayoutAbstraction::Segment& seg = segs[i];
-			if ( seg.readable() && !seg.writable() && !seg.executable() && (strcmp(seg.name(), "__LINKEDIT") == 0) ) {
+			if ( !seg.writable() && !seg.executable() && (strcmp(seg.name(), "__LINKEDIT") == 0) ) {
 				if ( fFirstLinkEditSegment == NULL ) 
 					fFirstLinkEditSegment = &seg;
+				// allocate non-executable,read-only segments from end of read only shared region
 				seg.setNewAddress(currentReadOnlyAddress);
 				currentReadOnlyAddress += pageAlign(seg.size());
 			}
 		}
 	}
-	fLinkEditsTotalUnoptimizedSize = pageAlign(currentReadOnlyAddress - fLinkEditsStartAddress);
+	fLinkEditsTotalUnoptimizedSize = (currentReadOnlyAddress - fLinkEditsStartAddress + 4095) & (-4096);
+
+    // <rdar://problem/9361288> i386 dyld shared cache overflows after adding libclh.dylib
+	if ( (currentReadOnlyAddress -  sharedRegionReadOnlyStartAddress()) > sharedRegionReadOnlySize() )
+		throwf("read-only slice of cache too big: %lluMB (max %lluMB)", 
+				(currentReadOnlyAddress -  sharedRegionReadOnlyStartAddress())/(1024*1024),
+			   sharedRegionReadOnlySize()/(1024*1024));
+		
 
 	// populate large mappings
 	uint64_t cacheFileOffset = 0;
-	if ( currentExecuteAddress > startExecuteAddress ) {
+	if ( currentExecuteAddress > sharedCacheStartAddress + FIRST_DYLIB_TEXT_OFFSET ) {
 		shared_file_mapping_np  executeMapping;
-		executeMapping.sfm_address		= startExecuteAddress;
-		executeMapping.sfm_size			= currentExecuteAddress - startExecuteAddress;
+		executeMapping.sfm_address		= sharedCacheStartAddress;
+		executeMapping.sfm_size			= currentExecuteAddress - sharedCacheStartAddress;
 		executeMapping.sfm_file_offset	= cacheFileOffset;
 		executeMapping.sfm_max_prot		= VM_PROT_READ | VM_PROT_EXECUTE;
 		executeMapping.sfm_init_prot	= VM_PROT_READ | VM_PROT_EXECUTE;
@@ -1391,18 +1099,29 @@
 		cacheFileOffset += executeMapping.sfm_size;
 		
 		shared_file_mapping_np  writableMapping;
-		writableMapping.sfm_address		= startWritableAddress;
-		writableMapping.sfm_size		= currentWritableAddress - startWritableAddress;
+		writableMapping.sfm_address		= sharedRegionWritableStartAddress();
+		writableMapping.sfm_size		= currentWritableAddress - sharedRegionWritableStartAddress();
 		writableMapping.sfm_file_offset	= cacheFileOffset;
 		writableMapping.sfm_max_prot	= VM_PROT_READ | VM_PROT_WRITE;
 		writableMapping.sfm_init_prot	= VM_PROT_READ | VM_PROT_WRITE;
 		fMappings.push_back(writableMapping);
 		cacheFileOffset += writableMapping.sfm_size;
-				
+		
+		if ( currentWritableExecutableAddress > startWritableExecutableAddress ) {
+			shared_file_mapping_np  writableExecutableMapping;
+			writableExecutableMapping.sfm_address	= startWritableExecutableAddress;
+			writableExecutableMapping.sfm_size		= currentWritableExecutableAddress - startWritableExecutableAddress;
+			writableExecutableMapping.sfm_file_offset= cacheFileOffset;
+			writableExecutableMapping.sfm_max_prot	= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
+			writableExecutableMapping.sfm_init_prot	= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 
+			fMappings.push_back(writableExecutableMapping);
+			cacheFileOffset += writableExecutableMapping.sfm_size;
+		}
+		
 		// make read-only (contains LINKEDIT segments) last, so it can be cut back when optimized
 		shared_file_mapping_np  readOnlyMapping;
-		readOnlyMapping.sfm_address		= startReadOnlyAddress;
-		readOnlyMapping.sfm_size		= currentReadOnlyAddress - startReadOnlyAddress;
+		readOnlyMapping.sfm_address		= currentExecuteAddress;
+		readOnlyMapping.sfm_size		= currentReadOnlyAddress - currentExecuteAddress;
 		readOnlyMapping.sfm_file_offset	= cacheFileOffset;
 		readOnlyMapping.sfm_max_prot	= VM_PROT_READ;
 		readOnlyMapping.sfm_init_prot	= VM_PROT_READ;
@@ -1412,7 +1131,7 @@
 	else {
 		// empty cache
 		shared_file_mapping_np  cacheHeaderMapping;
-		cacheHeaderMapping.sfm_address		= startExecuteAddress;
+		cacheHeaderMapping.sfm_address		= sharedRegionWritableStartAddress();
 		cacheHeaderMapping.sfm_size			= FIRST_DYLIB_TEXT_OFFSET;
 		cacheHeaderMapping.sfm_file_offset	= cacheFileOffset;
 		cacheHeaderMapping.sfm_max_prot		= VM_PROT_READ;
@@ -1470,7 +1189,7 @@
 	strcpy(&temp[15-strlen(archPairName)], archPairName);
 	if ( strcmp(header->magic(), temp) != 0 ) {
 		if ( fVerify ) {
-			fprintf(stderr, "update_dyld_shared_cache[%u] cannot verify %s because current cache file has invalid header\n", getpid(), archPairName);
+			fprintf(stderr, "update_dyld_shared_cache[%u] cannot verify %s because current cache file has invalid header\n", getpid(), archName());
 			return false;
 		}
 		else {
@@ -1481,11 +1200,11 @@
 	// not valid if count of images does not match current images needed
 	if ( header->imagesCount() != (fDylibs.size()+aliasCount) ) {
 		if ( fVerify ) {
-			fprintf(stderr, "update_dyld_shared_cache[%u] cannot verify %s because current cache file contains a different set of dylibs\n", getpid(), archPairName);
+			fprintf(stderr, "update_dyld_shared_cache[%u] cannot verify %s because current cache file contains a different set of dylibs\n", getpid(), archName());
 			return false;
 		}
 		else {
-			fprintf(stderr, "update_dyld_shared_cache[%u] updating %s cache because current cache file contains a different set of dylibs\n", getpid(), archPairName);
+			fprintf(stderr, "update_dyld_shared_cache[%u] updating %s cache because current cache file contains a different set of dylibs\n", getpid(), archName());
 			return true;
 		}
 	}
@@ -1504,7 +1223,7 @@
 			if ( fVerify ) {
 				if ( cacheEntry->pathFileOffset() > textSize ) {
 					throwf("update_dyld_shared_cache[%u]: for arch=%s, image entries corrupt, bad path offset in %s\n", 
-								getpid(), archPairName, it->layout->getID().name);
+								getpid(), archName(), it->layout->getID().name);
 				}
 				// in -verify mode, just match by path and warn if file looks different
 				if ( strcmp((char*)cache+cacheEntry->pathFileOffset(), it->layout->getID().name) == 0 ) {
@@ -1512,7 +1231,7 @@
 					sortingMap[it->layout] = cacheEntry-imagesStart;
 					if ( (cacheEntry->inode() != it->info.inode) || (cacheEntry->modTime() != it->info.modTime) ) {
 						fprintf(stderr, "update_dyld_shared_cache[%u] warning: for arch=%s, %s has changed since cache was built\n", 
-								getpid(), archPairName, it->layout->getID().name);
+								getpid(), archName(), it->layout->getID().name);
 					}
 					break;
 				}
@@ -1533,10 +1252,10 @@
 		}
 		if ( !found ) {
 			if ( fVerify ) {
-				throwf("update_dyld_shared_cache[%u] can't verify %s cache because %s is not in existing cache\n", getpid(), archPairName, it->layout->getID().name);
+				throwf("update_dyld_shared_cache[%u] can't verify %s cache because %s is not in existing cache\n", getpid(), archName(), it->layout->getID().name);
 			}
 			else {
-				fprintf(stderr, "update_dyld_shared_cache[%u] updating %s cache because dylib at %s has changed\n", getpid(), archPairName, it->layout->getID().name);
+				fprintf(stderr, "update_dyld_shared_cache[%u] updating %s cache because dylib at %s has changed\n", getpid(), archName(), it->layout->getID().name);
 				return true;
 			}
 		}
@@ -1569,7 +1288,7 @@
 	struct stat stat_buf;
 	::fstat(fd, &stat_buf);
     uint32_t cacheFileSize = stat_buf.st_size;
-    uint32_t cacheAllocatedSize = pageAlign(cacheFileSize);
+    uint32_t cacheAllocatedSize = (cacheFileSize + 4095) & (-4096);
     uint8_t* mappingAddr = NULL;
 	if ( vm_allocate(mach_task_self(), (vm_address_t*)(&mappingAddr), cacheAllocatedSize, VM_FLAGS_ANYWHERE) != KERN_SUCCESS )
         throwf("can't vm_allocate cache of size %u", cacheFileSize);
@@ -1577,7 +1296,7 @@
  	(void)fcntl(fd, F_NOCACHE, 1);
     ssize_t readResult = pread(fd, mappingAddr, cacheFileSize, 0);
     if ( readResult != cacheFileSize )
-        throwf("can't read all of existing cache file (%lu of %u): %s", readResult, cacheFileSize, path);
+        throw "can't read existing cache file";
 	::close(fd);
 
 	// validate it
@@ -1595,6 +1314,76 @@
 	return result;
 }
 
+class CStringEquals
+{
+public:
+	bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
+};
+
+class StringPool
+{
+public:
+				StringPool();
+	const char*	getBuffer();
+	uint32_t	size();
+	uint32_t	add(const char* str);
+	uint32_t	addUnique(const char* str);
+	const char* stringAtIndex(uint32_t) const;
+private:
+	typedef __gnu_cxx::hash_map<const char*, uint32_t, __gnu_cxx::hash<const char*>, CStringEquals> StringToOffset;
+
+	char*			fBuffer;
+	uint32_t		fBufferAllocated;
+	uint32_t		fBufferUsed;
+	StringToOffset	fUniqueStrings;
+};
+
+
+StringPool::StringPool() 
+	: fBufferUsed(0), fBufferAllocated(32*1024*1024)
+{
+	fBuffer = (char*)malloc(fBufferAllocated);
+}
+
+uint32_t StringPool::add(const char* str)
+{
+	uint32_t len = strlen(str);
+	if ( (fBufferUsed + len + 1) > fBufferAllocated ) {
+		// grow buffer
+		throw "string buffer exhausted";
+	}
+	strcpy(&fBuffer[fBufferUsed], str);
+	uint32_t result = fBufferUsed;
+	fUniqueStrings[&fBuffer[fBufferUsed]] = result;
+	fBufferUsed += len+1;
+	return result;
+}
+
+uint32_t StringPool::addUnique(const char* str)
+{
+	StringToOffset::iterator pos = fUniqueStrings.find(str);
+	if ( pos != fUniqueStrings.end() ) 
+		return pos->second;
+	else {
+		//fprintf(stderr, "StringPool::addUnique() new string: %s\n", str);
+		return this->add(str);
+	}
+}
+
+uint32_t StringPool::size()
+{
+	return fBufferUsed;
+}
+
+const char*	StringPool::getBuffer()
+{
+	return fBuffer;
+}
+
+const char* StringPool::stringAtIndex(uint32_t index) const
+{
+	return &fBuffer[index];
+}
 
 
 template <typename A>
@@ -1608,16 +1397,12 @@
 		void								copyWeakBindInfo(uint32_t&);
 		void								copyLazyBindInfo(uint32_t&);
 		void								copyExportInfo(uint32_t&);
-		void								copyLocalSymbols(uint32_t symbolTableOffset, uint32_t&, bool dontMapLocalSymbols,
-															uint8_t* cacheStart, StringPool& unmappedLocalsStringPool, 
-															std::vector<macho_nlist<typename A::P> >& unmappedSymbols,
-															std::vector<LocalSymbolInfo>& info);
+		void								copyLocalSymbols(uint32_t symbolTableOffset, uint32_t&);
 		void								copyExportedSymbols(uint32_t symbolTableOffset, uint32_t&);
 		void								copyImportedSymbols(uint32_t symbolTableOffset, uint32_t&);
 		void								copyExternalRelocations(uint32_t& offset);
 		void								copyIndirectSymbolTable(uint32_t& offset);
 		void								copyFunctionStarts(uint32_t& offset);
-		void								copyDataInCode(uint32_t& offset);
 		void								updateLoadCommands(uint64_t newVMAddress, uint64_t size, uint32_t stringPoolOffset, 
 																uint32_t linkEditsFileOffset, bool keepSignatures);
 	
@@ -1637,7 +1422,6 @@
 	macho_dyld_info_command<P>*					fDyldInfo;
 	macho_dysymtab_command<P>*					fDynamicSymbolTable;
 	macho_linkedit_data_command<P>*				fFunctionStarts;
-	macho_linkedit_data_command<P>*				fDataInCode;
 	macho_symtab_command<P>*					fSymbolTableLoadCommand;
 	const macho_nlist<P>*						fSymbolTable;
 	const char*									fStrings;
@@ -1661,9 +1445,6 @@
 	uint32_t									fExternalRelocationsOffsetIntoNewLinkEdit;
 	uint32_t									fIndirectSymbolTableOffsetInfoNewLinkEdit;
 	uint32_t									fFunctionStartsOffsetInNewLinkEdit;
-	uint32_t									fDataInCodeOffsetInNewLinkEdit;
-	uint32_t									fUnmappedLocalSymbolsStartIndexInNewLinkEdit;
-	uint32_t									fUnmappedLocalSymbolsCountInNewLinkEdit;
 };
 
 
@@ -1671,8 +1452,7 @@
 template <typename A>
 LinkEditOptimizer<A>::LinkEditOptimizer(const MachOLayoutAbstraction& layout, const SharedCache<A>& sharedCache, uint8_t* newLinkEdit, StringPool& stringPool)
  : 	fSharedCache(sharedCache), fLayout(layout), fLinkEditBase(NULL), fNewLinkEditStart(newLinkEdit), fDyldInfo(NULL),
-	fDynamicSymbolTable(NULL), fFunctionStarts(NULL), fDataInCode(NULL), 
-	fSymbolTableLoadCommand(NULL), fSymbolTable(NULL), fStrings(NULL), fNewStringPool(stringPool),
+	fDynamicSymbolTable(NULL), fFunctionStarts(NULL), fSymbolTableLoadCommand(NULL), fSymbolTable(NULL), fStrings(NULL), fNewStringPool(stringPool),
 	fBindInfoOffsetIntoNewLinkEdit(0), fBindInfoSizeInNewLinkEdit(0),
 	fWeakBindInfoOffsetIntoNewLinkEdit(0), fWeakBindInfoSizeInNewLinkEdit(0),
 	fLazyBindInfoOffsetIntoNewLinkEdit(0), fLazyBindInfoSizeInNewLinkEdit(0),
@@ -1682,8 +1462,7 @@
 	fExportedSymbolsStartIndexInNewLinkEdit(0), fExportedSymbolsCountInNewLinkEdit(0),
 	fImportSymbolsStartIndexInNewLinkEdit(0), fImportedSymbolsCountInNewLinkEdit(0),
 	fExternalRelocationsOffsetIntoNewLinkEdit(0), fIndirectSymbolTableOffsetInfoNewLinkEdit(0),
-	fFunctionStartsOffsetInNewLinkEdit(0), fDataInCodeOffsetInNewLinkEdit(0),
-	fUnmappedLocalSymbolsStartIndexInNewLinkEdit(0), fUnmappedLocalSymbolsCountInNewLinkEdit(0)
+	fFunctionStartsOffsetInNewLinkEdit(0)
 	
 {
 	fHeader = (const macho_header<P>*)fLayout.getSegments()[0].mappedAddress();
@@ -1718,8 +1497,6 @@
 				break;
 			case LC_FUNCTION_STARTS:
 				fFunctionStarts = (macho_linkedit_data_command<P>*)cmd;
-			case LC_DATA_IN_CODE:
-				fDataInCode = (macho_linkedit_data_command<P>*)cmd;
 				break;
 		}
 		cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
@@ -1792,46 +1569,25 @@
 }
 
 
+
 template <typename A>
-void LinkEditOptimizer<A>::copyLocalSymbols(uint32_t symbolTableOffset, uint32_t& symbolIndex, bool dontMapLocalSymbols, uint8_t* cacheStart, 
-											StringPool&	unmappedLocalsStringPool, std::vector<macho_nlist<P> >& unmappedSymbols,
-											std::vector<LocalSymbolInfo>& dylibInfos)
+void LinkEditOptimizer<A>::copyLocalSymbols(uint32_t symbolTableOffset, uint32_t& symbolIndex)
 {
 	fLocalSymbolsStartIndexInNewLinkEdit = symbolIndex;
-	LocalSymbolInfo localInfo;
-	localInfo.dylibOffset = ((uint8_t*)fHeader) - cacheStart;
-	localInfo.nlistStartIndex = unmappedSymbols.size();
-	localInfo.nlistCount = 0;
 	fSymbolTableStartOffsetInNewLinkEdit = symbolTableOffset + symbolIndex*sizeof(macho_nlist<P>);
 	macho_nlist<P>* const newSymbolTableStart = (macho_nlist<P>*)(fNewLinkEditStart+symbolTableOffset);
 	const macho_nlist<P>* const firstLocal = &fSymbolTable[fDynamicSymbolTable->ilocalsym()];
 	const macho_nlist<P>* const lastLocal  = &fSymbolTable[fDynamicSymbolTable->ilocalsym()+fDynamicSymbolTable->nlocalsym()];
 	uint32_t oldIndex = fDynamicSymbolTable->ilocalsym();
 	for (const macho_nlist<P>* entry = firstLocal; entry < lastLocal; ++entry, ++oldIndex) {
-		// <rdar://problem/12237639> don't copy stab symbols
-		if ( (entry->n_sect() != NO_SECT) && ((entry->n_type() & N_STAB) == 0) ) {
-			const char* name = &fStrings[entry->n_strx()];
+		if ( (entry->n_type() & N_TYPE) == N_SECT ) {
 			macho_nlist<P>* newSymbolEntry = &newSymbolTableStart[symbolIndex];
 			*newSymbolEntry = *entry;
-			if ( dontMapLocalSymbols ) {
-				// if local in __text, add <redacted> symbol name to shared cache so backtraces don't have bogus names
-				if ( entry->n_sect() == 1 ) {
-					newSymbolEntry->set_n_strx(fNewStringPool.addUnique("<redacted>"));
-					++symbolIndex;
-				}
-				// copy local symbol to unmmapped locals area
-				unmappedSymbols.push_back(*entry);			
-				unmappedSymbols.back().set_n_strx(unmappedLocalsStringPool.addUnique(name));
-			}
-			else {
-				newSymbolEntry->set_n_strx(fNewStringPool.addUnique(name));
-				++symbolIndex;
-			}
+			newSymbolEntry->set_n_strx(fNewStringPool.addUnique(&fStrings[entry->n_strx()]));
+			++symbolIndex;
 		}
 	}
 	fLocalSymbolsCountInNewLinkEdit = symbolIndex - fLocalSymbolsStartIndexInNewLinkEdit;
-	localInfo.nlistCount = unmappedSymbols.size() - localInfo.nlistStartIndex;
-	dylibInfos.push_back(localInfo);
 	//fprintf(stderr, "%u locals starting at %u for %s\n", fLocalSymbolsCountInNewLinkEdit, fLocalSymbolsStartIndexInNewLinkEdit, fLayout.getFilePath());
 }
 
@@ -1919,17 +1675,6 @@
 }
 
 template <typename A>
-void LinkEditOptimizer<A>::copyDataInCode(uint32_t& offset)
-{	
-	if ( fDataInCode != NULL ) {
-		fDataInCodeOffsetInNewLinkEdit = offset;
-		memcpy(&fNewLinkEditStart[offset], &fLinkEditBase[fDataInCode->dataoff()], fDataInCode->datasize());
-		offset += fDataInCode->datasize();
-	}
-}
-
-
-template <typename A>
 void LinkEditOptimizer<A>::copyIndirectSymbolTable(uint32_t& offset)
 {	
 	fIndirectSymbolTableOffsetInfoNewLinkEdit = offset;
@@ -1949,7 +1694,7 @@
 }
 
 template <typename A>
-void LinkEditOptimizer<A>::updateLoadCommands(uint64_t newVMAddress, uint64_t leSize, uint32_t stringPoolOffset, 
+void LinkEditOptimizer<A>::updateLoadCommands(uint64_t newVMAddress, uint64_t size, uint32_t stringPoolOffset, 
 												uint32_t linkEditsFileOffset, bool keepSignatures)
 {
 	// set LINKEDIT segment commmand to new merged LINKEDIT
@@ -1961,39 +1706,22 @@
 			macho_segment_command<P>* seg = (macho_segment_command<P>*)cmd;
 			if ( strcmp(seg->segname(), "__LINKEDIT") == 0 ) {
 				seg->set_vmaddr(newVMAddress);
-				seg->set_vmsize(leSize);
-				seg->set_filesize(leSize);
+				seg->set_vmsize(size);
+				seg->set_filesize(size);
 				seg->set_fileoff(linkEditsFileOffset);
 			}
-			else {
+			// don't alter __TEXT until <rdar://problem/7022345> is fixed
+			else if ( strcmp(seg->segname(), "__TEXT") != 0 ) {
+				// update all other segments fileoff to be offset from start of cache file
 				pint_t oldFileOff = seg->fileoff();
-				// don't alter __TEXT until <rdar://problem/7022345> is fixed
-				if ( strcmp(seg->segname(), "__TEXT") != 0 ) {
-					// update all other segments fileoff to be offset from start of cache file
-					seg->set_fileoff(fSharedCache.cacheFileOffsetForVMAddress(seg->vmaddr()));
-				}
+				seg->set_fileoff(fSharedCache.cacheFileOffsetForVMAddress(seg->vmaddr()));
 				pint_t fileOffsetDelta = seg->fileoff() - oldFileOff;
-				const MachOLayoutAbstraction::Segment* layoutSeg = fLayout.getSegment(seg->segname());
-				if ( layoutSeg != NULL ) {
-					//if ( seg->filesize() != layoutSeg->fileSize() ) {
-					//	fprintf(stderr, "LC filesize=0x%08llX, trimmed seg file size=0x%08llX, seg=%s, path=%s\n", 
-					//					seg->filesize(), layoutSeg->fileSize(), seg->segname(), fLayout.getFilePath());
-					//}
-					//if ( seg->vmsize() != layoutSeg->size() ) {
-					//	fprintf(stderr, "LC   vmsize=0x%08llX, trimmed seg      size=0x%08llX, seg=%s, path=%s\n", 
-					//					seg->vmsize(), layoutSeg->size(), seg->segname(), fLayout.getFilePath());
-					//}
-					seg->set_vmsize(layoutSeg->size());
-					seg->set_filesize(layoutSeg->fileSize());
-				}
 				// update all sections in this segment
 				macho_section<P>* const sectionsStart = (macho_section<P>*)((char*)seg + sizeof(macho_segment_command<P>));
 				macho_section<P>* const sectionsEnd = &sectionsStart[seg->nsects()];
 				for(macho_section<P>* sect = sectionsStart; sect < sectionsEnd; ++sect) {
 					if ( sect->offset() != 0 )
 						sect->set_offset(sect->offset()+fileOffsetDelta);
-						//if ( (sect->flags() & SECTION_TYPE) == S_MOD_INIT_FUNC_POINTERS )
-						//	fprintf(stderr, "found initializer(s) in %s\n", fLayout.getFilePath());
 				}
 			}
 		}
@@ -2044,10 +1772,6 @@
 	if ( fFunctionStarts != NULL ) {
 		fFunctionStarts->set_dataoff(linkEditsFileOffset+fFunctionStartsOffsetInNewLinkEdit);
 	}
-	// update data-in-code info
-	if ( fDataInCode != NULL ) {
-		fDataInCode->set_dataoff(linkEditsFileOffset+fDataInCodeOffsetInNewLinkEdit);
-	}
 	
 	// now remove load commands no longer needed
 	const macho_load_command<P>* srcCmd = cmds;
@@ -2057,8 +1781,6 @@
 		uint32_t cmdSize = srcCmd->cmdsize();
 		switch ( srcCmd->cmd() ) {
 			case LC_SEGMENT_SPLIT_INFO:
-			case LC_DYLIB_CODE_SIGN_DRS:
-			case LC_RPATH:
 				// don't copy
 				break;
 			case LC_CODE_SIGNATURE:
@@ -2092,7 +1814,7 @@
 
 
 template <typename A>
-uint8_t* SharedCache<A>::optimizeLINKEDIT(bool keepSignatures, bool dontMapLocalSymbols)
+uint8_t* SharedCache<A>::optimizeLINKEDIT(bool keepSignatures)
 {
 	// allocate space for optimized LINKEDIT area
 	uint8_t* newLinkEdit = new uint8_t[fLinkEditsTotalUnoptimizedSize];
@@ -2138,11 +1860,8 @@
 	fOffsetOfOldSymbolTableInfoInCombinedLinkedit = offset;
 	uint32_t symbolTableOffset = offset;
 	uint32_t symbolTableIndex = 0;
-	if ( dontMapLocalSymbols ) 
-		fUnmappedLocalSymbols.reserve(16384);
 	for(typename std::vector<LinkEditOptimizer<A>*>::iterator it = optimizers.begin(); it != optimizers.end(); ++it) {
-		(*it)->copyLocalSymbols(symbolTableOffset, symbolTableIndex, dontMapLocalSymbols, fInMemoryCache,
-								fUnmappedLocalsStringPool, fUnmappedLocalSymbols, fLocalSymbolInfos);
+		(*it)->copyLocalSymbols(symbolTableOffset, symbolTableIndex);
 		(*it)->copyExportedSymbols(symbolTableOffset, symbolTableIndex);
 		(*it)->copyImportedSymbols(symbolTableOffset, symbolTableIndex);
 	}
@@ -2162,13 +1881,6 @@
 		(*it)->copyFunctionStarts(offset);
 	}
 	fSizeOfFunctionStartsInCombinedLinkedit = offset - fOffsetOfFunctionStartsInCombinedLinkedit;
-
-	// copy data-in-code info
-	fOffsetOfDataInCodeInCombinedLinkedit = offset;
-	for(typename std::vector<LinkEditOptimizer<A>*>::iterator it = optimizers.begin(); it != optimizers.end(); ++it) {
-		(*it)->copyDataInCode(offset);
-	}
-	fSizeOfDataInCodeInCombinedLinkedit = offset - fOffsetOfDataInCodeInCombinedLinkedit;
 
 	// copy indirect symbol tables
 	fOffsetOfOldIndirectSymbolsInCombinedLinkedit = offset;
@@ -2183,15 +1895,15 @@
 	fSizeOfOldStringPoolInCombinedLinkedit = stringPool.size();
 	
 	// total new size round up to page size
-	fLinkEditsTotalOptimizedSize = pageAlign(fOffsetOfOldStringPoolInCombinedLinkedit + fSizeOfOldStringPoolInCombinedLinkedit);
+	fLinkEditsTotalOptimizedSize = (fOffsetOfOldStringPoolInCombinedLinkedit + fSizeOfOldStringPoolInCombinedLinkedit + 4095) & (-4096);
 	
 	// choose new linkedit file offset 
 	uint32_t linkEditsFileOffset = cacheFileOffsetForVMAddress(fLinkEditsStartAddress);
-//	uint32_t linkEditsFileOffset = fLinkEditsStartAddress - sharedRegionStartAddress();	
+//	uint32_t linkEditsFileOffset = fLinkEditsStartAddress - sharedRegionReadOnlyStartAddress();	
 
 	// update load commands so that all dylibs shared different areas of the same LINKEDIT segment
 	for(typename std::vector<LinkEditOptimizer<A>*>::iterator it = optimizers.begin(); it != optimizers.end(); ++it) {
-		(*it)->updateLoadCommands(fLinkEditsStartAddress, fLinkEditsTotalOptimizedSize, fOffsetOfOldStringPoolInCombinedLinkedit, linkEditsFileOffset, keepSignatures);
+		(*it)->updateLoadCommands(fLinkEditsStartAddress, fLinkEditsTotalUnoptimizedSize, fOffsetOfOldStringPoolInCombinedLinkedit, linkEditsFileOffset, keepSignatures);
 	}
 
 	//fprintf(stderr, "fLinkEditsTotalUnoptimizedSize=%llu, fLinkEditsTotalOptimizedSize=%u\n", fLinkEditsTotalUnoptimizedSize, fLinkEditsTotalOptimizedSize);
@@ -2213,11 +1925,12 @@
 				seg.setFileOffset(linkEditsFileOffset);
 			}
 		}
-	}	
-		
+	}
+	
 	// return new end of cache
-	return (uint8_t*)fFirstLinkEditSegment->mappedAddress() + regionAlign(fLinkEditsTotalOptimizedSize);
-}
+	return (uint8_t*)fFirstLinkEditSegment->mappedAddress() + fLinkEditsTotalOptimizedSize;
+}
+
 
 
 template <typename A>
@@ -2253,218 +1966,27 @@
     size_t count() const { return fCount; }
 };
 
-
-template <typename A>
-class ClassListBuilder
-{
-private:
-    typedef typename A::P P;
-
-    objc_opt::string_map fClassNames;
-    objc_opt::class_map fClasses;
-    size_t fCount;
-    HeaderInfoOptimizer<A>& fHinfos;
-
-public:
-
-    ClassListBuilder(HeaderInfoOptimizer<A>& hinfos)
-        : fClassNames()
-        , fClasses()
-        , fCount(0)
-        , fHinfos(hinfos)
-    { }
-
-    void visitClass(SharedCache<A>* cache, 
-                    const macho_header<P>* header,
-                    objc_class_t<A>* cls) 
-    {
-        if (cls->isMetaClass(cache)) return;
-
-        const char *name = cls->getName(cache);
-        uint64_t name_vmaddr = cache->VMAddressForMappedAddress(name);
-        uint64_t cls_vmaddr = cache->VMAddressForMappedAddress(cls);
-        uint64_t hinfo_vmaddr = cache->VMAddressForMappedAddress(fHinfos.hinfoForHeader(cache, header));
-        fClassNames.insert(objc_opt::string_map::value_type(name, name_vmaddr));
-        fClasses.insert(objc_opt::class_map::value_type(name, std::pair<uint64_t, uint64_t>(cls_vmaddr, hinfo_vmaddr)));
-        fCount++;
-    }
-
-    objc_opt::string_map& classNames() { 
-        return fClassNames;
-    }
-
-    objc_opt::class_map& classes() { 
-        return fClasses;
-    }
-
-    size_t count() const { return fCount; }
-};
-
-
-template <typename A>
-class ProtocolOptimizer
-{
-private:
-    typedef typename A::P P;
-    typedef typename A::P::uint_t pint_t;
-
-    objc_opt::string_map fProtocolNames;
-    objc_opt::protocol_map fProtocols;
-    size_t fProtocolCount;
-    size_t fProtocolReferenceCount;
-
-    friend class ProtocolReferenceWalker<A, ProtocolOptimizer<A>>;
-    pint_t visitProtocolReference(SharedCache<A>* cache, pint_t oldValue)
-    {
-        objc_protocol_t<A>* proto = (objc_protocol_t<A>*)
-            cache->mappedAddressForVMAddress(oldValue);
-        pint_t newValue = fProtocols[proto->getName(cache)];
-        if (oldValue != newValue) fProtocolReferenceCount++;
-        return newValue;
-    }
-
-public:
-
-    ProtocolOptimizer()
-        : fProtocolNames()
-        , fProtocols()
-        , fProtocolCount(0)
-        , fProtocolReferenceCount(0)
-    { }
-
-    void addProtocols(SharedCache<A>* cache, 
-                      const macho_header<P>* header)
-    {
-        PointerSection<A, objc_protocol_t<A> *> 
-            protocols(cache, header, "__DATA", "__objc_protolist");
-        
-        for (pint_t i = 0; i < protocols.count(); i++) {
-            objc_protocol_t<A> *proto = protocols.get(i);
-
-            const char *name = proto->getName(cache);
-            if (fProtocolNames.count(name) == 0) {
-                // Need a Swift demangler API in OS before we can handle this
-                if (0 == strncmp(name, "_TtP", 4)) {
-                    throw "objc protocol has Swift name";
-                }
-                if (proto->getSize() > sizeof(objc_protocol_t<A>)) {
-                    throw "objc protocol is too big";
-                }
-
-                uint64_t name_vmaddr = cache->VMAddressForMappedAddress(name);
-                uint64_t proto_vmaddr = cache->VMAddressForMappedAddress(proto);
-                fProtocolNames.insert(objc_opt::string_map::value_type(name, name_vmaddr));
-                fProtocols.insert(objc_opt::protocol_map::value_type(name, proto_vmaddr));
-                fProtocolCount++;
-            }
-        }
-    }
-
-    const char *writeProtocols(SharedCache<A>* cache, 
-                               uint8_t *& dest, size_t& remaining, 
-                               std::vector<void*>& pointersInData, 
-                               pint_t protocolClassVMAddr)
-    {
-        if (fProtocolCount == 0) return NULL;
-
-        if (protocolClassVMAddr == 0) {
-            return "libobjc's Protocol class symbol not found (metadata not optimized)";
-        }
-
-        size_t required = fProtocolCount * sizeof(objc_protocol_t<A>);
-        if (remaining < required) {
-            return "libobjc's read-write section is too small (metadata not optimized)";
-        }
-
-        for (objc_opt::protocol_map::iterator iter = fProtocols.begin();
-             iter != fProtocols.end();
-             ++iter)
-        {
-            objc_protocol_t<A>* oldProto = (objc_protocol_t<A>*)
-                cache->mappedAddressForVMAddress(iter->second);
-
-            // Create a new protocol object.
-            objc_protocol_t<A>* proto = (objc_protocol_t<A>*)dest;
-            dest += sizeof(*proto);
-            remaining -= sizeof(*proto);
-
-            // Initialize it.
-            uint32_t oldSize = oldProto->getSize();
-            memcpy(proto, oldProto, oldSize);
-            if (!proto->getIsaVMAddr()) {
-                proto->setIsaVMAddr(protocolClassVMAddr);
-            }
-            if (oldSize < sizeof(*proto)) {
-                // Protocol object is old. Populate new fields.
-                proto->setSize(sizeof(objc_protocol_t<A>));
-                // missing extendedMethodTypes is already nil
-            }
-            // Some protocol objects are big enough to have the 
-            // demangledName field but don't initialize it.
-            if (! proto->getDemangledName(cache)) {
-                proto->setDemangledName(cache, proto->getName(cache));
-            }
-            proto->setFixedUp();
-
-            // Redirect the protocol table at our new object.
-            iter->second = cache->VMAddressForMappedAddress(proto);
-
-            // Add new rebase entries.
-            proto->addPointers(pointersInData);
-        }
-        
-        return NULL;
-    }
-
-    void updateReferences(SharedCache<A>* cache, const macho_header<P>* header)
-    {
-        ProtocolReferenceWalker<A, ProtocolOptimizer<A>> refs(*this);
-        refs.walk(cache, header);
-    }
-
-    objc_opt::string_map& protocolNames() { 
-        return fProtocolNames;
-    }
-
-    objc_opt::protocol_map& protocols() { 
-        return fProtocols;
-    }
-
-    size_t protocolCount() const { return fProtocolCount; }
-    size_t protocolReferenceCount() const { return fProtocolReferenceCount; }
-};
-
-
-static int percent(size_t num, size_t denom) {
-    if (denom) return (int)(num / (double)denom * 100);
-    else return 100;
-}
-
 template <typename A>
 void SharedCache<A>::optimizeObjC(std::vector<void*>& pointersInData)
 {
     const char *err;
+    size_t headerSize = sizeof(objc_opt::objc_opt_t);
 
     if ( verbose ) {
         fprintf(stderr, "update_dyld_shared_cache: for %s, optimizing objc metadata\n", archName());
     }
 
-    size_t headerSize = P::round_up(sizeof(objc_opt::objc_opt_t));
-    if (headerSize != sizeof(objc_opt::objc_opt_t)) {
-		warn(archName(), "libobjc's optimization structure size is wrong (metadata not optimized)");
-    }
-
-    // Find libobjc's empty sections to fill in.
-    // Find libobjc's list of pointers for us to use.
+    // Find libobjc's empty sections to fill in
     const macho_section<P> *optROSection = NULL;
     const macho_section<P> *optRWSection = NULL;
-    const macho_section<P> *optPointerListSection = NULL;
 	for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
-        if ( strstr(it->layout->getFilePath(), "/libobjc.") != NULL ) {
+        if ( strstr(it->layout->getFilePath(), "libobjc") != NULL ) {
 			const macho_header<P>* mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
 			optROSection = mh->getSection("__TEXT", "__objc_opt_ro");
+			// __objc_selopt is old name for __objc_opt_ro
+			if ( optROSection == NULL )
+				optROSection = mh->getSection("__TEXT", "__objc_selopt");
 			optRWSection = mh->getSection("__DATA", "__objc_opt_rw");
-			optPointerListSection = mh->getSection("__DATA", "__objc_opt_ptrs");
 			break;
 		}
 	}
@@ -2474,73 +1996,17 @@
 		return;
 	}
 	
-	if ( optRWSection == NULL ) {
-		warn(archName(), "libobjc's read/write section missing (metadata not optimized)");
-		return;
-	}
-	
-	if ( optPointerListSection == NULL ) {
-		warn(archName(), "libobjc's pointer list section missing (metadata not optimized)");
-		return;
-	}
-
-	uint8_t* optROData = (uint8_t*)mappedAddressForVMAddress(optROSection->addr());
-    size_t optRORemaining = optROSection->size();
-
-	uint8_t* optRWData = (uint8_t*)mappedAddressForVMAddress(optRWSection->addr());
-    size_t optRWRemaining = optRWSection->size();
-	
-	if (optRORemaining < headerSize) {
+	objc_opt::objc_opt_t* optROHeader = (objc_opt::objc_opt_t*)mappedAddressForVMAddress(optROSection->addr());
+	if (optROSection->size() < headerSize) {
 		warn(archName(), "libobjc's read-only section is too small (metadata not optimized)");
 		return;
 	}
-	objc_opt::objc_opt_t* optROHeader = (objc_opt::objc_opt_t *)optROData;
-    optROData += headerSize;
-    optRORemaining -= headerSize;
 
 	if (E::get32(optROHeader->version) != objc_opt::VERSION) {
 		warn(archName(), "libobjc's read-only section version is unrecognized (metadata not optimized)");
 		return;
 	}
 
-    if (optPointerListSection->size() < sizeof(objc_opt::objc_opt_pointerlist_tt<pint_t>)) {
-        warn(archName(), "libobjc's pointer list section is too small (metadata not optimized)");
-		return;
-    }
-    const objc_opt::objc_opt_pointerlist_tt<pint_t> *optPointerList = (const objc_opt::objc_opt_pointerlist_tt<pint_t> *)mappedAddressForVMAddress(optPointerListSection->addr());
-
-    // Write nothing to optROHeader until everything else is written.
-    // If something fails below, libobjc will not use the section.
-
-    // Find objc-containing dylibs
-    std::vector<LayoutInfo> objcDylibs;
-    for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
-        macho_header<P> *mh = (macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        if (mh->getSection("__DATA", "__objc_imageinfo")  ||  mh->getSegment("__OBJC")) {
-            objcDylibs.push_back(*it);
-        }
-    }
-
-    // Build image list
-
-    // This is SAFE: the binaries themselves are unmodified.
-
-    std::vector<LayoutInfo> addressSortedDylibs = objcDylibs;
-    std::sort(addressSortedDylibs.begin(), addressSortedDylibs.end(), ByAddressSorter());
-
-    uint64_t hinfoVMAddr = optRWSection->addr() + optRWSection->size() - optRWRemaining;
-    HeaderInfoOptimizer<A> hinfoOptimizer;
-    err = hinfoOptimizer.init(objcDylibs.size(), optRWData, optRWRemaining);
-    if (err) {
-		warn(archName(), err);
-		return;
-    }
-    for(typename std::vector<LayoutInfo>::const_iterator it = addressSortedDylibs.begin(); it != addressSortedDylibs.end(); ++it) {
-        const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        hinfoOptimizer.update(this, mh, pointersInData);
-    }
-
-
     // Update selector references and build selector list
 
     // This is SAFE: if we run out of room for the selector table, 
@@ -2549,180 +2015,101 @@
     // Heuristic: choose selectors from libraries with more cstring data first.
     // This tries to localize selector cstring memory.
     ObjCSelectorUniquer<A> uniq(this);
-    std::vector<LayoutInfo> sizeSortedDylibs = objcDylibs;
-    std::sort(sizeSortedDylibs.begin(), sizeSortedDylibs.end(), ByCStringSectionSizeSorter());
+    std::vector<LayoutInfo> sortedDylibs = fDylibs;
+    std::sort(sortedDylibs.begin(), sortedDylibs.end(), ByCStringSectionSizeSorter());
 
     SelectorOptimizer<A, ObjCSelectorUniquer<A> > selOptimizer(uniq);
-	for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
+	for(typename std::vector<LayoutInfo>::const_iterator it = sortedDylibs.begin(); it != sortedDylibs.end(); ++it) {
         const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
         LegacySelectorUpdater<A, ObjCSelectorUniquer<A> >::update(this, mh, uniq);
         selOptimizer.optimize(this, mh);
 	}
 
-    uint64_t seloptVMAddr = optROSection->addr() + optROSection->size() - optRORemaining;
-    objc_opt::objc_selopt_t *selopt = new(optROData) objc_opt::objc_selopt_t;
-    err = selopt->write(seloptVMAddr, optRORemaining, uniq.strings());
+    if ( verbose ) {
+        fprintf(stderr, "update_dyld_shared_cache: for %s, found %zu unique objc selectors\n", archName(), uniq.strings().size());
+    }
+
+    // Write selector table in read-only data.
+    size_t selTableOffset = P::round_up(headerSize);
+    size_t selTableSize;
+    objc_opt::objc_selopt_t *seloptData = (objc_opt::objc_selopt_t *)
+        mappedAddressForVMAddress(optROSection->addr() + selTableOffset);
+    err = objc_opt::write_selopt(seloptData, 
+                                 optROSection->addr() + selTableOffset, 
+                                 optROSection->size() - selTableOffset, 
+                                 uniq.strings(), 
+                                 E::little_endian, &selTableSize);
     if (err) {
         warn(archName(), err);
         return;
     }
-    optROData += selopt->size();
-    optRORemaining -= selopt->size();
-    selopt->byteswap(E::little_endian), selopt = NULL;
-
-
-    // Build class table.
-
-    // This is SAFE: the binaries themselves are unmodified.
-
-    ClassListBuilder<A> classes(hinfoOptimizer);
-    ClassWalker< A, ClassListBuilder<A> > classWalker(classes);
-	for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
-        const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        classWalker.walk(this, mh);
-	}
-
-    uint64_t clsoptVMAddr = optROSection->addr() + optROSection->size() - optRORemaining;
-    objc_opt::objc_clsopt_t *clsopt = new(optROData) objc_opt::objc_clsopt_t;
-    err = clsopt->write(clsoptVMAddr, optRORemaining, 
-                        classes.classNames(), classes.classes(), verbose);
-    if (err) {
-        warn(archName(), err);
-        return;
-    }
-    optROData += clsopt->size();
-    optRORemaining -= clsopt->size();
-    size_t duplicateCount = clsopt->duplicateCount();
-    clsopt->byteswap(E::little_endian), clsopt = NULL;
-
-
-    // Sort method lists.
-
-    // This is SAFE: modified binaries are still usable as unsorted lists.
-    // This must be done AFTER uniquing selectors.
-
-    MethodListSorter<A> methodSorter;
-    for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
-        macho_header<P> *mh = (macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        methodSorter.optimize(this, mh);
-    }
-
-
-    // Unique protocols and build protocol table.
-
-    // This is SAFE: no protocol references are updated yet
-    // This must be done AFTER updating method lists.
-
-    ProtocolOptimizer<A> protocolOptimizer;
-	for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
-        const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        protocolOptimizer.addProtocols(this, mh);
-	}
-
-    pint_t protocolClassVMAddr = P::getP(optPointerList->protocolClass);
-    err = protocolOptimizer.writeProtocols(this, optRWData, optRWRemaining, 
-                                           pointersInData, protocolClassVMAddr);
-    if (err) {
-        warn(archName(), err);
-        return;
-    }
-
-    uint64_t protocoloptVMAddr = optROSection->addr() + optROSection->size() - optRORemaining;
-    objc_opt::objc_protocolopt_t *protocolopt = new(optROData) objc_opt::objc_protocolopt_t;
-    err = protocolopt->write(protocoloptVMAddr, optRORemaining, 
-                             protocolOptimizer.protocolNames(), 
-                             protocolOptimizer.protocols(), verbose);
-    if (err) {
-        warn(archName(), err);
-        return;
-    }
-    optROData += protocolopt->size();
-    optRORemaining -= protocolopt->size();
-    protocolopt->byteswap(E::little_endian), protocolopt = NULL;
-
-
-    // Redirect protocol references to the uniqued protocols.
-
-    // This is SAFE: the new protocol objects are still usable as-is.
-	for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
-        const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        protocolOptimizer.updateReferences(this, mh);
-	}
-
-
-    // Repair ivar offsets.
-
-    // This is SAFE: the runtime always validates ivar offsets at runtime.
-
-    IvarOffsetOptimizer<A> ivarOffsetOptimizer;
-	for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
-        const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        ivarOffsetOptimizer.findGCClasses(this, mh);
-	}
-	for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
-        const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        ivarOffsetOptimizer.optimize(this, mh);
-	}
-    
-
-    // Success. Mark dylibs as optimized.
-	for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
-        const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
-        const macho_section<P> *imageInfoSection;
-        imageInfoSection = mh->getSection("__DATA", "__objc_imageinfo");
-        if (!imageInfoSection) {
-            imageInfoSection = mh->getSection("__OBJC", "__image_info");
-        }
-        if (imageInfoSection) {
-            objc_image_info<A> *info = (objc_image_info<A> *)
-                mappedAddressForVMAddress(imageInfoSection->addr());
-            info->setOptimizedByDyld();
-        }
-    }
-
-
-    // Success. Update RO header last.
-    E::set32(optROHeader->selopt_offset, seloptVMAddr - optROSection->addr());
-    E::set32(optROHeader->clsopt_offset, clsoptVMAddr - optROSection->addr());
-    E::set32(optROHeader->protocolopt_offset, protocoloptVMAddr - optROSection->addr());
-    E::set32(optROHeader->headeropt_offset, hinfoVMAddr - optROSection->addr());
 
     if ( verbose ) {
-        size_t roSize = optROSection->size() - optRORemaining;
-        size_t rwSize = optRWSection->size() - optRWRemaining;
+        size_t totalSize = headerSize + selTableSize;
         fprintf(stderr, "update_dyld_shared_cache: for %s, %zu/%llu bytes "
                 "(%d%%) used in libobjc read-only optimization section\n", 
-                archName(), roSize, optROSection->size(), 
-                percent(roSize, optROSection->size()));
-        fprintf(stderr, "update_dyld_shared_cache: for %s, %zu/%llu bytes "
-                "(%d%%) used in libobjc read/write optimization section\n", 
-                archName(), rwSize, optRWSection->size(), 
-                percent(rwSize, optRWSection->size()));
-        fprintf(stderr, "update_dyld_shared_cache: for %s, "
-                "uniqued %zu selectors\n", 
-                archName(), uniq.strings().size());
+                archName(), totalSize, optROSection->size(), 
+                (int)(totalSize / (double)optROSection->size() * 100));
         fprintf(stderr, "update_dyld_shared_cache: for %s, "
                 "updated %zu selector references\n", 
                 archName(), uniq.count());
         fprintf(stderr, "update_dyld_shared_cache: for %s, "
-                "uniqued %zu protocols\n", 
-                archName(), protocolOptimizer.protocolCount());
-        fprintf(stderr, "update_dyld_shared_cache: for %s, "
-                "updated %zu protocol references\n", 
-                archName(), protocolOptimizer.protocolReferenceCount());
-        fprintf(stderr, "update_dyld_shared_cache: for %s, "
-                "updated %zu ivar offsets\n", 
-                archName(), ivarOffsetOptimizer.optimized());
-        fprintf(stderr, "update_dyld_shared_cache: for %s, "
-                "sorted %zu method lists\n", 
-                archName(), methodSorter.optimized());
-        fprintf(stderr, "update_dyld_shared_cache: for %s, "
-                "recorded %zu classes (%zu duplicates)\n", 
-                archName(), classes.classNames().size(), duplicateCount);
-        fprintf(stderr, "update_dyld_shared_cache: for %s, "
                 "wrote objc metadata optimization version %d\n", 
                 archName(), objc_opt::VERSION);
     }
+
+	// if r/w section exists in libojc attempt to optimize categories into classes
+	if ( optRWSection != NULL ) {
+		// Attach categories to classes in the same framework. 
+		// Build aggregated (but unsorted) method lists in read-write data.
+
+		// This is SAFE: if we run out of room while attaching categories in 
+		// a binary then previously-edited binaries are still valid. (This assumes 
+		// each binary is processed all-or-nothing, which CategoryAttacher does.)
+		// This must be done AFTER uniquing selectors.
+		// This must be done BEFORE sorting method lists.
+		
+		size_t categoryOffset = 0;
+		uint8_t *categoryData = (uint8_t*)mappedAddressForVMAddress(optRWSection->addr() + categoryOffset);
+		CategoryAttacher<A> categoryAttacher(categoryData, optRWSection->size() - categoryOffset);
+		for(typename std::vector<LayoutInfo>::const_iterator it = sortedDylibs.begin(); it != sortedDylibs.end(); ++it) {
+			macho_header<P> *mh = (macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+			err = categoryAttacher.optimize(this, mh, pointersInData);
+			if (err) {
+				warn(archName(), err);
+				return;
+			}
+		}    
+		size_t categorySize = categoryAttacher.bytesUsed();
+
+
+		// Sort method lists.
+		
+		// This is SAFE: modified binaries are still usable as unsorted lists.
+		// This must be done AFTER uniquing selectors.
+		// This must be done AFTER attaching categories.
+
+		MethodListSorter<A> methodSorter;
+		for(typename std::vector<LayoutInfo>::const_iterator it = sortedDylibs.begin(); it != sortedDylibs.end(); ++it) {
+			macho_header<P> *mh = (macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+			methodSorter.optimize(this, mh);
+		}
+
+		if ( verbose ) {
+			size_t totalRWSize = categorySize;
+			fprintf(stderr, "update_dyld_shared_cache: for %s, %zu/%llu bytes "
+                "(%d%%) used in libobjc read-write optimization section\n", 
+                archName(), totalRWSize, optRWSection->size(), 
+                (int)(totalRWSize / (double)optRWSection->size() * 100));
+			fprintf(stderr, "update_dyld_shared_cache: for %s, "
+                "attached %zu categories (%zd bytes used)\n", 
+                archName(), categoryAttacher.count(), 
+                categoryAttacher.bytesUsed());
+		}
+	}
+
+    // Success. Update RO header last
+    E::set32(optROHeader->selopt_offset, headerSize);
 
     return;
 }
@@ -2741,183 +2128,19 @@
 }
 
 
-// <rdar://problem/10730767> update_dyld_shared_cache should use sync_volume_np() instead of sync() 
-static void sync_volume(const char* volumePath)
-{
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
-	int error = sync_volume_np(volumePath, SYNC_VOLUME_FULLSYNC|SYNC_VOLUME_FULLSYNC);
-#else
-	int full_sync = 3; // SYNC_VOLUME_FULLSYNC | SYNC_VOLUME_FULLSYNC
-	int error = 0;
-	if ( fsctl(volumePath, 0x80004101 /*FSCTL_SYNC_VOLUME*/, &full_sync, 0) == -1) 
-		error = errno;
-#endif
-	if ( error )
-		::sync();
-}
-
-
-// <rdar://problem/12552226> update shared cache should sign the shared cache
-static bool adhoc_codesign_share_cache(const char* path)
-{
-	CFURLRef target = ::CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)path, strlen(path), FALSE);
-	if ( target == NULL )
-		return false;
-
-	SecStaticCodeRef code;
-	OSStatus status = ::SecStaticCodeCreateWithPath(target, kSecCSDefaultFlags, &code);
-	CFRelease(target);
-	if ( status ) {
-		::fprintf(stderr, "codesign: failed to create url to signed object\n");
-		return false;
-	}
-
-	const void * keys[1] = { (void *)kSecCodeSignerIdentity } ;
-	const void * values[1] = { (void *)kCFNull };
-	CFDictionaryRef params = ::CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-	if ( params == NULL ) {
-		CFRelease(code);
-		return false;
-	}
-	
-	SecCodeSignerRef signer;
-	status = ::SecCodeSignerCreate(params, kSecCSDefaultFlags, &signer);
-	CFRelease(params);
-	if ( status ) {
-		CFRelease(code);
-		::fprintf(stderr, "codesign: failed to create signer object\n");
-		return false;
-	}
-
-	status = ::SecCodeSignerAddSignatureWithErrors(signer, code, kSecCSDefaultFlags, NULL);
-	CFRelease(code);
-	CFRelease(signer);
-	if ( status ) {
-		::fprintf(stderr, "codesign: failed to sign object: %s\n", path);
-		return false;
-	}
-
-	if ( verbose )
-		::fprintf(stderr, "codesigning complete of %s\n", path);
-	
-	return true;
-}
-
-template <typename A>
-void SharedCache<A>::writeCacheFile(const char *cacheFilePath, uint8_t *cacheFileBuffer, uint32_t cacheFileSize, bool deleteOldCache) {
-	char tempCachePath[strlen(cacheFilePath)+16];
-	sprintf(tempCachePath, "%s.tmp%u", cacheFilePath, getpid());
-
-	try {
-		// install signal handlers to delete temp file if program is killed
-		sCleanupFile = tempCachePath;
-		::signal(SIGINT, cleanup);
-		::signal(SIGBUS, cleanup);
-		::signal(SIGSEGV, cleanup);
-
-		// compute UUID of whole cache
-		uint8_t digest[16];
-		CC_MD5(cacheFileBuffer, cacheFileSize, digest);
-		// <rdar://problem/6723729> uuids should conform to RFC 4122 UUID version 4 & UUID version 5 formats
-		digest[6] = ( digest[6] & 0x0F ) | ( 3 << 4 );
-		digest[8] = ( digest[8] & 0x3F ) | 0x80;
-		((dyldCacheHeader<E>*)cacheFileBuffer)->set_uuid(digest);
-
-		// create var/db/dyld dirs if needed
-		char dyldDirs[1024];
-		strcpy(dyldDirs, cacheFilePath);
-		char* lastSlash = strrchr(dyldDirs, '/');
-		if ( lastSlash != NULL )
-			lastSlash[1] = '\0';
-		struct stat stat_buf;
-		if ( stat(dyldDirs, &stat_buf) != 0 ) {
-			const char* afterSlash = &dyldDirs[1];
-			char* slash;
-			while ( (slash = strchr(afterSlash, '/')) != NULL ) {
-				*slash = '\0';
-				::mkdir(dyldDirs, S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
-				*slash = '/';
-				afterSlash = slash+1;
-			}
-		}
-
-		// create temp file for cache
-		int fd = ::open(tempCachePath, O_CREAT | O_RDWR | O_TRUNC, 0644);
-		if ( fd == -1 )
-			throwf("can't create temp file %s, errno=%d", tempCachePath, errno);
-
-		// try to allocate whole cache file contiguously
-		fstore_t fcntlSpec = { F_ALLOCATECONTIG|F_ALLOCATEALL, F_PEOFPOSMODE, 0, cacheFileSize, 0 };
-		::fcntl(fd, F_PREALLOCATE, &fcntlSpec);
-
-		// write out cache file
-		if ( verbose )
-			fprintf(stderr, "update_dyld_shared_cache: writing cache to disk: %s\n", tempCachePath);
-		if ( ::pwrite(fd, cacheFileBuffer, cacheFileSize, 0) != cacheFileSize )
-			throwf("write() failure creating cache file, errno=%d", errno);
-
-		// flush to disk and close
-		int result = ::fcntl(fd, F_FULLFSYNC, NULL);
-		if ( result == -1 )
-			fprintf(stderr, "update_dyld_shared_cache: warning, fcntl(F_FULLFSYNC) failed with errno=%d for %s\n", errno, tempCachePath);
-		result = ::close(fd);
-		if ( result != 0 )
-			fprintf(stderr, "update_dyld_shared_cache: warning, close() failed with errno=%d for %s\n", errno, tempCachePath);
-
-		if ( !iPhoneOS )
-			adhoc_codesign_share_cache(tempCachePath);
-
-		if ( deleteOldCache ) {
-			const char* pathLastSlash = strrchr(cacheFilePath, '/');
-			if ( pathLastSlash != NULL ) {
-				result = ::unlink(cacheFilePath);
-				if ( result != 0 ) {
-					if ( errno != ENOENT )
-						fprintf(stderr, "update_dyld_shared_cache: warning, unable to remove existing cache %s because errno=%d\n", cacheFilePath, errno);
-				}
-			}
-		}
-
-		// move new cache file to correct location for use after reboot
-		if ( verbose )
-			fprintf(stderr, "update_dyld_shared_cache: atomically moving cache file into place: %s\n", cacheFilePath);
-		result = ::rename(tempCachePath, cacheFilePath);
-		if ( result != 0 )
-			throwf("can't swap newly create dyld shared cache file: rename(%s,%s) returned errno=%d", tempCachePath, cacheFilePath, errno);
-
-		// flush everything to disk to assure rename() gets recorded
-		sync_volume(cacheFilePath);
-
-		// restore default signal handlers
-		::signal(SIGINT, SIG_DFL);
-		::signal(SIGBUS, SIG_DFL);
-		::signal(SIGSEGV, SIG_DFL);
-	}
-	catch (...){
-		// remove temp cache file
-		::unlink(tempCachePath);
-		throw;
-	}
-}
-
 
 template <>	 bool	SharedCache<x86_64>::addCacheSlideInfo(){ return true; }
 template <>	 bool	SharedCache<arm>::addCacheSlideInfo()	{ return true; }
 template <>	 bool	SharedCache<x86>::addCacheSlideInfo()	{ return false; }
-template <>	 bool	SharedCache<arm64>::addCacheSlideInfo()	{ return true; }
+template <>	 bool	SharedCache<ppc>::addCacheSlideInfo()	{ return false; }
+
 
 
 template <typename A>
-bool SharedCache<A>::update(bool force, bool optimize, bool deleteExistingFirst, int archIndex,
-								int archCount, bool keepSignatures, bool dontMapLocalSymbols)
+bool SharedCache<A>::update(bool usesOverlay, bool force, bool optimize, bool deleteExistingFirst, int archIndex,
+								int archCount, bool keepSignatures)
 {
 	bool didUpdate = false;
-	bool canEmitDevelopmentCache = true;
-	char devCacheFilePath[strlen(fCacheFilePath)+strlen(".development")];
-	char fileListFilePath[strlen(fCacheFilePath)+strlen(".list")];
-	sprintf(devCacheFilePath, "%s.development", fCacheFilePath);
-	sprintf(fileListFilePath, "%s.list", fCacheFilePath);
-	std::vector<const char *> paths;
 	
 	// already up to date?
 	if ( force || fExistingIsNotUpToDate ) {
@@ -2933,6 +2156,8 @@
 			::unlink(fCacheFilePath);
 		uint8_t* inMemoryCache = NULL;
 		uint32_t allocatedCacheSize = 0;
+		char tempCachePath[strlen(fCacheFilePath)+16];
+		sprintf(tempCachePath, "%s.tmp%u", fCacheFilePath, getpid());
 		try {
 			// allocate a memory block to hold cache
 			uint32_t cacheFileSize = 0;
@@ -2963,8 +2188,6 @@
 			header->set_codeSignatureSize(0);
 			header->set_slideInfoOffset(0);
 			header->set_slideInfoSize(0);
-			header->set_localSymbolsOffset(0);
-			header->set_localSymbolsSize(0);
 			
 			// fill in mappings
 			dyldCacheFileMapping<E>* mapping = (dyldCacheFileMapping<E>*)&inMemoryCache[sizeof(dyldCacheHeader<E>)];
@@ -3005,34 +2228,28 @@
 			const int dylibCount = fDylibs.size();
 			int dylibIndex = 0;
 			int progressIndex = 0;
-			bool foundLibSystem = false;
 			for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it, ++dylibIndex) {
 				const char* path = it->layout->getFilePath();
 				int src = ::open(path, O_RDONLY, 0);
 				if ( src == -1 )
-					throwf("can't open file %s, errno=%d", it->layout->getID().name, errno);
+					throwf("can't open file %s, errnor=%d", it->layout->getID().name, errno);
 				// mark source as "don't cache"
 				(void)fcntl(src, F_NOCACHE, 1);
 				// verify file has not changed since dependency analysis
 				struct stat stat_buf;
 				if ( fstat(src, &stat_buf) == -1)
 					throwf("can't stat open file %s, errno=%d", path, errno);
-				if ( (it->layout->getInode() != stat_buf.st_ino) )
-					throwf("file inode changed from %llu to %llu during cache creation: %s", it->layout->getInode(), stat_buf.st_ino, path);
-				else if ( it->layout->getLastModTime() != stat_buf.st_mtime )
-					throwf("file mtime changed from 0x%lX to 0x%lX during cache creation: %s", it->layout->getLastModTime(), stat_buf.st_mtime, path);
-				if ( strcmp(it->layout->getID().name, "/usr/lib/libSystem.B.dylib") == 0 )
-					foundLibSystem = true;
+				if ( (it->layout->getInode() != stat_buf.st_ino) || (it->layout->getLastModTime() != stat_buf.st_mtime) )
+					throwf("file modified during cache creation: %s", path);
+
 				if ( verbose )
-					fprintf(stderr, "update_dyld_shared_cache: copying %s to cache\n", it->layout->getFilePath());
+					fprintf(stderr, "update_dyld_shared_cache: copying %s to cache\n", it->layout->getID().name);
 				try {
 					const std::vector<MachOLayoutAbstraction::Segment>& segs = it->layout->getSegments();
 					for (int i=0; i < segs.size(); ++i) {
 						const MachOLayoutAbstraction::Segment& seg = segs[i];
-						if ( verbose ) {
-							fprintf(stderr, "\t\tsegment %s, size=0x%0llX, cache address=0x%0llX, buffer address=%p\n",
-								seg.name(), seg.size(), seg.newAddress(), &inMemoryCache[cacheFileOffsetForVMAddress(seg.newAddress())]);
-						}
+						if ( verbose )
+							fprintf(stderr, "\t\tsegment %s, size=0x%0llX, cache address=0x%0llX\n", seg.name(), seg.fileSize(), seg.newAddress());
 						if ( seg.size() > 0 ) {
 							const uint64_t segmentSrcStartOffset = it->layout->getOffsetInUniversalFile()+seg.fileOffset();
 							const uint64_t segmentSize = seg.fileSize();
@@ -3052,7 +2269,6 @@
 					throwf("%s while copying %s to shared cache", msg, it->layout->getID().name);
 				}
 				::close(src);
-				paths.push_back(it->layout->getID().name);
 				if ( progress ) {
 					// assuming read takes 40% of time
 					int nextProgressIndex = archIndex*100+(40*dylibIndex)/dylibCount;
@@ -3061,9 +2277,7 @@
 					progressIndex = nextProgressIndex;
 				}
 			}
-			if ( !foundLibSystem )
-				throw "cache would be missing required dylib /usr/lib/libSystem.B.dylib";
-
+						
 			// set mapped address for each segment
 			for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
 				std::vector<MachOLayoutAbstraction::Segment>& segs = ((MachOLayoutAbstraction*)(it->layout))->getSegments();
@@ -3079,14 +2293,15 @@
 			std::vector<void*> pointersInData;
 			pointersInData.reserve(1024);
 			
+			// add pointer in start of __DATA to start of __TEXT to remain compatible with previous dylds
+			pint_t* dataStartPtr = (pint_t*)(&inMemoryCache[fMappings[1].sfm_file_offset]);
+			P::setP(*dataStartPtr, fMappings[0].sfm_address);
+
 			// rebase each dylib in shared cache
 			for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
 				try {
 					Rebaser<A> r(*it->layout);
-					if (!r.rebase(pointersInData)) {
-						canEmitDevelopmentCache = false;
-						fprintf(stderr, "update_dyld_shared_cache: Omitting development cache for %s, cannot rebase dylib into place for %s\n", archName(), it->layout->getID().name);
-					}
+					r.rebase(pointersInData);
 					//if ( verbose )
 					//	fprintf(stderr, "update_dyld_shared_cache: for %s, rebasing dylib into cache for %s\n", archName(), it->layout->getID().name);
 				}
@@ -3102,12 +2317,13 @@
 			std::vector<Binder<A>*> binders;
 			for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
 				//fprintf(stderr, "binding %s\n", it->layout->getID().name);
-				Binder<A>* binder = new Binder<A>(*it->layout);
+				Binder<A>* binder = new Binder<A>(*it->layout, fDyldBaseAddress);
 				binders.push_back(binder);
 				// only add dylibs to map
 				if ( it->layout->getID().name != NULL )
 					map[it->layout->getID().name] = binder;
 			}
+  			
 			// tell each Binder about the others
 			for(typename std::vector<Binder<A>*>::iterator it = binders.begin(); it != binders.end(); ++it) {
 				(*it)->setDependentBinders(map);
@@ -3123,46 +2339,6 @@
 					throwf("%s in %s", msg, (*it)->getDylibID());
 				}
 			}
-
-			for(typename std::vector<LayoutInfo>::iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
-				const macho_header<P>* fHeader = (const macho_header<P>*)it->layout->getSegments()[0].mappedAddress();
-				const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>));
-				const uint32_t cmd_count = fHeader->ncmds();
-				const macho_load_command<P>* cmd = cmds;
-				macho_dyld_info_command<P>*					fDyldInfo;
-				uint64_t originalLinkEditVMAddr = 0;
-				for (uint32_t i = 0; i < cmd_count; ++i) {
-					if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
-						macho_segment_command<P>* seg = (macho_segment_command<P>*)cmd;
-						if ( strcmp(seg->segname(), "__LINKEDIT") != 0 ) {
-							pint_t oldFileOff = seg->fileoff();
-							originalLinkEditVMAddr += seg->vmsize();
-							// don't alter __TEXT until <rdar://problem/7022345> is fixed
-							if ( strcmp(seg->segname(), "__TEXT") != 0 ) {
-								// update all other segments fileoff to be offset from start of cache file
-								seg->set_fileoff(cacheFileOffsetForVMAddress(seg->vmaddr()));
-							}
-							pint_t fileOffsetDelta = seg->fileoff() - oldFileOff;
-							const MachOLayoutAbstraction::Segment* layoutSeg = it->layout->getSegment(seg->segname());
-							if ( layoutSeg != NULL ) {
-								seg->set_vmsize(layoutSeg->size());
-								seg->set_filesize(layoutSeg->fileSize());
-							}
-							// update all sections in this segment
-							macho_section<P>* const sectionsStart = (macho_section<P>*)((char*)seg + sizeof(macho_segment_command<P>));
-							macho_section<P>* const sectionsEnd = &sectionsStart[seg->nsects()];
-							for(macho_section<P>* sect = sectionsStart; sect < sectionsEnd; ++sect) {
-								if ( sect->offset() != 0 )
-									sect->set_offset(sect->offset()+fileOffsetDelta);
-							}
-						}
-					} else if (cmd->cmd() == LC_DYLD_INFO || cmd->cmd() == LC_DYLD_INFO_ONLY) {
-						fDyldInfo = (macho_dyld_info_command<P>*)cmd;
-					}
-					cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
-				}
-			}
-
 			// optimize binding
 			for(typename std::vector<Binder<A>*>::iterator it = binders.begin(); it != binders.end(); ++it) {
 				try {
@@ -3172,7 +2348,6 @@
 					throwf("%s in %s", msg, (*it)->getDylibID());
 				}
 			}
-
 			// delete binders
 			for(typename std::vector<Binder<A>*>::iterator it = binders.begin(); it != binders.end(); ++it) {
 				delete *it;
@@ -3180,11 +2355,9 @@
 	
 			// merge/optimize all LINKEDIT segments
 			if ( optimize ) {
-				if ( verbose )
-					fprintf(stderr, "update_dyld_shared_cache: original cache file size %uMB\n", cacheFileSize/(1024*1024));
-				cacheFileSize = (this->optimizeLINKEDIT(keepSignatures, dontMapLocalSymbols) - inMemoryCache);
-				if ( verbose )
-					fprintf(stderr, "update_dyld_shared_cache: optimized cache file size %uMB\n", cacheFileSize/(1024*1024));
+				//fprintf(stderr, "update_dyld_shared_cache: original cache file size %uMB\n", cacheFileSize/(1024*1024));
+				cacheFileSize = (this->optimizeLINKEDIT(keepSignatures) - inMemoryCache);
+				//fprintf(stderr, "update_dyld_shared_cache: optimized cache file size %uMB\n", cacheFileSize/(1024*1024));
 				// update header to reduce mapping size
 				dyldCacheHeader<E>* cacheHeader = (dyldCacheHeader<E>*)inMemoryCache;
 				dyldCacheFileMapping<E>* mappings = (dyldCacheFileMapping<E>*)&inMemoryCache[sizeof(dyldCacheHeader<E>)];
@@ -3197,22 +2370,7 @@
 				//		header->codeSignatureOffset(), fMappings.back().sfm_address + fMappings.back().sfm_size);
 				header->set_codeSignatureOffset(fMappings.back().sfm_file_offset + fMappings.back().sfm_size);
 			}
-
-			// dump dev cache with optimized linkedit, but not ObjC optimizations
-			if (iPhoneOS && canEmitDevelopmentCache) {
-				int fileListFD = ::open(fileListFilePath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
-				if ( fileListFD != -1 ) {
-					for (const char* path : paths) {
-						write(fileListFD, path, strlen(path)+1);
-						write(fileListFD, "\n", 1);
-					}
-					close(fileListFD);
-				}
-
-				((dyldCacheHeader<E>*)inMemoryCache)->set_cacheType(1);
-				writeCacheFile(devCacheFilePath, inMemoryCache, cacheFileSize, fCacheFileInFinalLocation);
-			}
-
+			
 			// unique objc selectors and update other objc metadata
             if ( optimize ) {
 				optimizeObjC(pointersInData);
@@ -3279,7 +2437,7 @@
 				}
 				slideInfo->set_entries_count(entry_count);
 	
-				int slideInfoPageSize = regionAlign(slideInfo->entries_offset() + entry_count*entry_size);
+				int slideInfoPageSize = (slideInfo->entries_offset() + entry_count*entry_size + 4095) & (-4096);
 				cacheFileSize += slideInfoPageSize;
 			
 				// update mappings to increase RO size
@@ -3300,64 +2458,6 @@
 				memcpy(&inMemoryCache[cacheHeader->slideInfoOffset()], slideInfo, slideInfoPageSize);	
 			}
 			
-			// append local symbol info in an unmapped region
-			if ( dontMapLocalSymbols ) {
-				uint32_t spaceAtEnd = allocatedCacheSize - cacheFileSize;
-				uint32_t localSymbolsOffset = pageAlign(cacheFileSize);
-				dyldCacheLocalSymbolsInfo<E>* infoHeader = (dyldCacheLocalSymbolsInfo<E>*)(&inMemoryCache[localSymbolsOffset]);
-				const uint32_t entriesOffset = sizeof(dyldCacheLocalSymbolsInfo<E>);
-				const uint32_t entriesCount = fLocalSymbolInfos.size();
-				const uint32_t nlistOffset = entriesOffset + entriesCount * sizeof(dyldCacheLocalSymbolEntry<E>);
-				const uint32_t nlistCount = fUnmappedLocalSymbols.size();
-				const uint32_t stringsOffset = nlistOffset + nlistCount * sizeof(macho_nlist<P>);
-				const uint32_t stringsSize = fUnmappedLocalsStringPool.size();
-				if ( stringsOffset+stringsSize > spaceAtEnd ) 
-					throwf("update_dyld_shared_cache[%u] for arch=%s, out of space for local symbols. Have 0x%X, Need 0x%X\n",
-							getpid(), fArchGraph->archName(), spaceAtEnd, stringsOffset+stringsSize);
-				// fill in local symbols info
-				infoHeader->set_nlistOffset(nlistOffset);
-				infoHeader->set_nlistCount(nlistCount);
-				infoHeader->set_stringsOffset(stringsOffset);
-				infoHeader->set_stringsSize(stringsSize);
-				infoHeader->set_entriesOffset(entriesOffset);
-				infoHeader->set_entriesCount(entriesCount);
-				// copy info for each dylib
-				dyldCacheLocalSymbolEntry<E>* entries = (dyldCacheLocalSymbolEntry<E>*)(&inMemoryCache[localSymbolsOffset+entriesOffset]);
-				for (int i=0; i < entriesCount; ++i) {
-					entries[i].set_dylibOffset(fLocalSymbolInfos[i].dylibOffset);
-					entries[i].set_nlistStartIndex(fLocalSymbolInfos[i].nlistStartIndex);
-					entries[i].set_nlistCount(fLocalSymbolInfos[i].nlistCount);
-				}
-				// copy nlists
-				memcpy(&inMemoryCache[localSymbolsOffset+nlistOffset], &fUnmappedLocalSymbols[0], nlistCount*sizeof(macho_nlist<P>));
-				// copy string pool
-				memcpy(&inMemoryCache[localSymbolsOffset+stringsOffset], fUnmappedLocalsStringPool.getBuffer(), stringsSize);
-				
-				// update state
-				fUnmappedLocalSymbolsSize = pageAlign(stringsOffset + stringsSize);
-				cacheFileSize = regionAlign(localSymbolsOffset + fUnmappedLocalSymbolsSize);
-				
-				// update header to show location of slidePointers
-				dyldCacheHeader<E>* cacheHeader = (dyldCacheHeader<E>*)inMemoryCache;
-				cacheHeader->set_localSymbolsOffset(localSymbolsOffset);
-				cacheHeader->set_localSymbolsSize(stringsOffset+stringsSize);
-				cacheHeader->set_codeSignatureOffset(cacheFileSize);
-			}
-			
-			// make sure after all optimizations, that whole cache file fits into shared region address range
-			{
-				dyldCacheHeader<E>* cacheHeader = (dyldCacheHeader<E>*)inMemoryCache;
-				dyldCacheFileMapping<E>* mappings = (dyldCacheFileMapping<E>*)&inMemoryCache[cacheHeader->mappingOffset()];
-				// <rdar://problem/16128830> incorporate code signature size into overflow check
-				uint32_t estCodeSigSize = regionAlign(cacheFileSize/200); // guess 0.5% for code signature
-				for (int i=0; i < cacheHeader->mappingCount(); ++i) {
-					uint64_t endAddr = mappings[i].address() + mappings[i].size() + estCodeSigSize;
-					if ( endAddr > (sharedRegionStartAddress() + sharedRegionSize()) ) {
-						throwf("update_dyld_shared_cache[%u] for arch=%s, shared cache will not fit in shared regions address space.  Overflow amount: %lluKB\n",
-							getpid(), fArchGraph->archName(), (endAddr-(sharedRegionStartAddress() + sharedRegionSize()))/1024);
-					}
-				}
-			}
 			
 			if ( fVerify ) {
 				// if no existing cache, say so
@@ -3421,9 +2521,93 @@
 				}
 			}
 			else {
-				((dyldCacheHeader<E>*)inMemoryCache)->set_cacheType(0);
-				writeCacheFile(fCacheFilePath, inMemoryCache, cacheFileSize, fCacheFileInFinalLocation);
+				// install signal handlers to delete temp file if program is killed 
+				sCleanupFile = tempCachePath;
+				::signal(SIGINT, cleanup);
+				::signal(SIGBUS, cleanup);
+				::signal(SIGSEGV, cleanup);
+				
+				// create var/db/dyld dirs if needed
+				char dyldDirs[1024];
+				strcpy(dyldDirs, fCacheFilePath);
+				char* lastSlash = strrchr(dyldDirs, '/');
+				if ( lastSlash != NULL )
+					lastSlash[1] = '\0';
+				struct stat stat_buf;
+				if ( stat(dyldDirs, &stat_buf) != 0 ) {
+					const char* afterSlash = &dyldDirs[1];
+					char* slash;
+					while ( (slash = strchr(afterSlash, '/')) != NULL ) {
+						*slash = '\0';
+						::mkdir(dyldDirs, S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
+						*slash = '/';
+						afterSlash = slash+1;
+					}
+				}
+				
+				// create temp file for cache
+				int fd = ::open(tempCachePath, O_CREAT | O_RDWR | O_TRUNC, 0644);	
+				if ( fd == -1 )
+					throwf("can't create temp file %s, errnor=%d", tempCachePath, errno);
+					
+				// try to allocate whole cache file contiguously
+				fstore_t fcntlSpec = { F_ALLOCATECONTIG|F_ALLOCATEALL, F_PEOFPOSMODE, 0, cacheFileSize, 0 };
+				::fcntl(fd, F_PREALLOCATE, &fcntlSpec);
+
+				// write out cache file
+				if ( verbose )
+					fprintf(stderr, "update_dyld_shared_cache: writing cache to disk\n");
+				if ( ::pwrite(fd, inMemoryCache, cacheFileSize, 0) != cacheFileSize )
+					throwf("write() failure creating cache file, errno=%d", errno);
+				if ( progress ) {
+					// assuming write takes 35% of time
+					fprintf(stdout, "%3u/%u\n", (archIndex+1)*90, archCount*100);
+				}
+				
+				// flush to disk and close
+				int result = ::fcntl(fd, F_FULLFSYNC, NULL);
+				if ( result == -1 ) 
+					fprintf(stderr, "update_dyld_shared_cache: warning, fcntl(F_FULLFSYNC) failed with errno=%d for %s\n", errno, tempCachePath);
+				result = ::close(fd);
+				if ( result != 0 ) 
+					fprintf(stderr, "update_dyld_shared_cache: warning, close() failed with errno=%d for %s\n", errno, tempCachePath);
+				
+				// <rdar://problem/7901042> Make life easier for the kernel at shutdown.
+				// If we just move the new cache file over the old, the old file
+				// may need to exist in the open-unlink state.  But because it
+				// may be mapped into the shared region, it cannot be deleted
+				// until all user processes are terminated.  That leaves are
+				// small to non-existent window for the kernel to delete the
+				// old cache file.
+				if ( fCacheFileInFinalLocation ) {
+					char tmpDirPath[64];
+					const char* pathLastSlash = strrchr(fCacheFilePath, '/');
+					if ( pathLastSlash != NULL ) {
+						sprintf(tmpDirPath, "/var/run%s.old.%u", pathLastSlash, getpid());
+						// move existing cache file to /var/run to be clean up next boot
+						result = ::rename(fCacheFilePath, tmpDirPath);
+						if ( result != 0 ) {
+							if ( errno != ENOENT )
+								fprintf(stderr, "update_dyld_shared_cache: warning, unable to move existing cache to %s errno=%d for %s\n", tmpDirPath, errno, fCacheFilePath);
+						}
+					}
+				}
+				
+				// move new cache file to correct location for use after reboot
+				result = ::rename(tempCachePath, fCacheFilePath);
+				if ( result != 0 ) 
+					throwf("can't swap newly create dyld shared cache file: rename(%s,%s) returned errno=%d", tempCachePath, fCacheFilePath, errno);
+				
+				
+				// flush everything to disk to assure rename() gets recorded
+				::sync();
 				didUpdate = true;
+				
+				// restore default signal handlers
+				::signal(SIGINT, SIG_DFL);
+				::signal(SIGBUS, SIG_DFL);
+				::signal(SIGSEGV, SIG_DFL);
+
 				// generate human readable "map" file that shows the layout of the cache file
 				if ( verbose )
 					fprintf(stderr, "update_dyld_shared_cache: writing .map file to disk\n");
@@ -3433,7 +2617,7 @@
 				sprintf(tempMapFilePath, "%s.map%u", fCacheFilePath, getpid());
 				FILE* fmap = ::fopen(tempMapFilePath, "w");	
 				if ( fmap == NULL ) {
-					fprintf(stderr, "can't create map file %s, errno=%d", tempMapFilePath, errno);
+					fprintf(stderr, "can't create map file %s, errnor=%d", tempCachePath, errno);
 				}
 				else {
 					for(std::vector<shared_file_mapping_np>::iterator it = fMappings.begin(); it != fMappings.end(); ++it) {
@@ -3445,75 +2629,51 @@
 						else if ( it->sfm_init_prot == (VM_PROT_EXECUTE|VM_PROT_WRITE|VM_PROT_READ) )
 							prot = "WX";
 						if ( it->sfm_size > 1024*1024 )
-							fprintf(fmap, "mapping  %s %4lluMB 0x%0llX -> 0x%0llX\n", prot, it->sfm_size/(1024*1024),
+							fprintf(fmap, "mapping %s %4lluMB 0x%0llX -> 0x%0llX\n", prot, it->sfm_size/(1024*1024),
 																it->sfm_address, it->sfm_address+it->sfm_size);
 						else
-							fprintf(fmap, "mapping  %s %4lluKB 0x%0llX -> 0x%0llX\n", prot, it->sfm_size/1024,
+							fprintf(fmap, "mapping %s %4lluKB 0x%0llX -> 0x%0llX\n", prot, it->sfm_size/1024,
 																it->sfm_address, it->sfm_address+it->sfm_size);
 					}
 
-					fprintf(fmap, " linkedit   %4uKB 0x%0llX -> 0x%0llX weak binding info\n",		
+					fprintf(fmap, "linkedit   %4uKB 0x%0llX -> 0x%0llX weak binding info\n",		
 								(fOffsetOfExportInfoInCombinedLinkedit-fOffsetOfWeakBindInfoInCombinedLinkedit)/1024,
 								fLinkEditsStartAddress+fOffsetOfWeakBindInfoInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfExportInfoInCombinedLinkedit);
-					fprintf(fmap, " linkedit   %4uKB 0x%0llX -> 0x%0llX export info\n",		
+					fprintf(fmap, "linkedit   %4uKB 0x%0llX -> 0x%0llX export info\n",		
 								(fOffsetOfBindInfoInCombinedLinkedit-fOffsetOfExportInfoInCombinedLinkedit)/1024,
 								fLinkEditsStartAddress+fOffsetOfExportInfoInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfBindInfoInCombinedLinkedit);
-					fprintf(fmap, " linkedit   %4uKB 0x%0llX -> 0x%0llX binding info\n",		
+					fprintf(fmap, "linkedit   %4uKB 0x%0llX -> 0x%0llX binding info\n",		
 								(fOffsetOfLazyBindInfoInCombinedLinkedit-fOffsetOfBindInfoInCombinedLinkedit)/1024,
 								fLinkEditsStartAddress+fOffsetOfBindInfoInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfLazyBindInfoInCombinedLinkedit);
-					fprintf(fmap, " linkedit   %4uKB 0x%0llX -> 0x%0llX lazy binding info\n",		
+					fprintf(fmap, "linkedit   %4uKB 0x%0llX -> 0x%0llX lazy binding info\n",		
 								(fOffsetOfOldSymbolTableInfoInCombinedLinkedit-fOffsetOfLazyBindInfoInCombinedLinkedit)/1024,
 								fLinkEditsStartAddress+fOffsetOfLazyBindInfoInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfOldSymbolTableInfoInCombinedLinkedit);
-					fprintf(fmap, " linkedit   %4uMB 0x%0llX -> 0x%0llX non-dyld symbol table size\n",		
+					fprintf(fmap, "linkedit   %4uMB 0x%0llX -> 0x%0llX non-dyld symbol table size\n",		
 								(fSizeOfOldSymbolTableInfoInCombinedLinkedit)/(1024*1024),
 								fLinkEditsStartAddress+fOffsetOfOldSymbolTableInfoInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfOldSymbolTableInfoInCombinedLinkedit+fSizeOfOldSymbolTableInfoInCombinedLinkedit);				
 					if ( fSizeOfFunctionStartsInCombinedLinkedit != 0 )
-						fprintf(fmap, " linkedit   %4uKB 0x%0llX -> 0x%0llX non-dyld functions starts size\n",		
+						fprintf(fmap, "linkedit   %4uKB 0x%0llX -> 0x%0llX non-dyld functions starts size\n",		
 								fSizeOfFunctionStartsInCombinedLinkedit/1024,
 								fLinkEditsStartAddress+fOffsetOfFunctionStartsInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfFunctionStartsInCombinedLinkedit+fSizeOfFunctionStartsInCombinedLinkedit);				
-					if ( fSizeOfDataInCodeInCombinedLinkedit != 0 )
-						fprintf(fmap, " linkedit   %4uKB 0x%0llX -> 0x%0llX non-dyld data-in-code info size\n",		
-								fSizeOfDataInCodeInCombinedLinkedit/1024,
-								fLinkEditsStartAddress+fOffsetOfDataInCodeInCombinedLinkedit,
-								fLinkEditsStartAddress+fOffsetOfDataInCodeInCombinedLinkedit+fSizeOfDataInCodeInCombinedLinkedit);				
 					if ( fSizeOfOldExternalRelocationsInCombinedLinkedit != 0 )
-						fprintf(fmap, " linkedit   %4uKB 0x%0llX -> 0x%0llX non-dyld external relocs size\n",		
+						fprintf(fmap, "linkedit   %4uKB 0x%0llX -> 0x%0llX non-dyld external relocs size\n",		
 								fSizeOfOldExternalRelocationsInCombinedLinkedit/1024,
 								fLinkEditsStartAddress+fOffsetOfOldExternalRelocationsInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfOldExternalRelocationsInCombinedLinkedit+fSizeOfOldExternalRelocationsInCombinedLinkedit);				
-					fprintf(fmap, " linkedit   %4uKB 0x%0llX -> 0x%0llX non-dyld indirect symbol table size\n",		
+					fprintf(fmap, "linkedit   %4uKB 0x%0llX -> 0x%0llX non-dyld indirect symbol table size\n",		
 								fSizeOfOldIndirectSymbolsInCombinedLinkedit/1024,
 								fLinkEditsStartAddress+fOffsetOfOldIndirectSymbolsInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfOldIndirectSymbolsInCombinedLinkedit+fSizeOfOldIndirectSymbolsInCombinedLinkedit);				
-					fprintf(fmap, " linkedit   %4uMB 0x%0llX -> 0x%0llX non-dyld string pool\n",		
+					fprintf(fmap, "linkedit   %4uMB 0x%0llX -> 0x%0llX non-dyld string pool\n",		
 								(fSizeOfOldStringPoolInCombinedLinkedit)/(1024*1024),
 								fLinkEditsStartAddress+fOffsetOfOldStringPoolInCombinedLinkedit,
 								fLinkEditsStartAddress+fOffsetOfOldStringPoolInCombinedLinkedit+fSizeOfOldStringPoolInCombinedLinkedit);				
-					
-					dyldCacheHeader<E>* cacheHeader = (dyldCacheHeader<E>*)inMemoryCache;
-					if ( cacheHeader->slideInfoSize() != 0 ) {
-						fprintf(fmap, " linkedit   %4lluKB kernel slide info\n", (cacheHeader->slideInfoSize())/1024);				
-					}
-
-					fprintf(fmap, "unmapped -- %4uMB local symbol info\n", fUnmappedLocalSymbolsSize/(1024*1024));					
-					
-					uint64_t endMappingAddr = fMappings[2].sfm_address + fMappings[2].sfm_size;
-					fprintf(fmap, "total map   %4lluMB\n", (endMappingAddr - sharedRegionStartAddress())/(1024*1024));	
-					if ( sharedRegionStartWritableAddress(0) == 0x7FFF70000000LL ) {
-						// x86_64 has different slide constraints
-						uint64_t freeSpace = 256*1024*1024 - fMappings[1].sfm_size;
-						fprintf(fmap, "r/w space   %4lluMB -> %d bits of entropy for ASLR\n\n", freeSpace/(1024*1024), (int)log2(freeSpace/4096));
-					}
-					else {
-						uint64_t freeSpace = sharedRegionStartAddress() + sharedRegionSize() - endMappingAddr;
-						fprintf(fmap, "free space  %4lluMB -> %d bits of entropy for ASLR\n\n", freeSpace/(1024*1024), (int)log2(freeSpace/4096));
-					}
 					
 					for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
 						fprintf(fmap, "%s\n", it->layout->getID().name);
@@ -3532,7 +2692,7 @@
 						}
 					}
 					fclose(fmap);
-					::rename(tempMapFilePath, mapFilePath);
+					result = ::rename(tempMapFilePath, mapFilePath);
 				}
 			}
 			
@@ -3545,6 +2705,8 @@
 			}
 		}
 		catch (...){
+			// remove temp cache file
+			::unlink(tempCachePath);
 			// remove in memory cache
 			if ( inMemoryCache != NULL ) 
 				vm_deallocate(mach_task_self(), (vm_address_t)inMemoryCache, allocatedCacheSize);
@@ -3609,7 +2771,15 @@
 						*last = '\0';
 						--last;
 					}
-					paths.push_back(symbolStart);
+					// <rdar://problem/8305479> images in shared cache are bound against different IOKit than found at runtime
+					// HACK:  Just ignore the known bad IOKit
+					if ( strcmp(symbolStart, "/System/Library/Frameworks/IOKit.framework/IOKit") == 0 ) {
+						fprintf(stderr, "update_dyld_shared_cache: warning, ignoring /System/Library/Frameworks/IOKit.framework/IOKit\n");
+						warnings.push_back("update_dyld_shared_cache: warning, ignoring /System/Library/Frameworks/IOKit.framework/IOKit\n");
+					}
+					else {
+						paths.push_back(symbolStart);
+					}
 					symbolStart = NULL;
 					state = lineStart;
 				}
@@ -3625,11 +2795,10 @@
 
 
 
-static void setSharedDylibs(const char* rootPath, const std::vector<const char*>& overlayPaths, const std::set<ArchPair>& onlyArchs, std::vector<const char*> rootsPaths)
+static void setSharedDylibs(const char* rootPath, bool usesOverlay, const std::set<ArchPair>& onlyArchs, std::vector<const char*> rootsPaths)
 {
 	// set file system root
-	ArchGraph::setFileSystemRoot(rootPath);
-	ArchGraph::setFileSystemOverlay(overlayPaths);
+	ArchGraph::setFileSystemRoot(rootPath, usesOverlay);
 
 	// initialize all architectures requested
 	for(std::set<ArchPair>::iterator a = onlyArchs.begin(); a != onlyArchs.end(); ++a)
@@ -3645,11 +2814,12 @@
 }
 
 
-static void scanForSharedDylibs(const char* rootPath, const std::vector<const char*>& overlayPaths, const char* dirOfPathFiles, const std::set<ArchPair>& onlyArchs)
+static void scanForSharedDylibs(const char* rootPath, bool usesOverlay, const char* dirOfPathFiles, const std::set<ArchPair>& onlyArchs)
 {
 	char rootDirOfPathFiles[strlen(rootPath)+strlen(dirOfPathFiles)+2];
+	// in -overlay mode, still look for roots in /var/db/dyld
 	// in -root mode, look for roots in /rootpath/var/db/dyld
-	if ( rootPath[0] != '\0' ) {
+	if ( !usesOverlay && (strlen(rootPath) != 0) ) {
 		strcpy(rootDirOfPathFiles, rootPath);
 		strcat(rootDirOfPathFiles, dirOfPathFiles);
 		dirOfPathFiles = rootDirOfPathFiles;
@@ -3690,14 +2860,14 @@
 	
 	if ( rootsPaths.size() == 0 )
 		fprintf(stderr, "update_dyld_shared_cache: warning, no entries found in shared_region_roots\n");
-	setSharedDylibs(rootPath, overlayPaths, onlyArchs, rootsPaths);
-}
-
-static void setSharedDylibs(const char* rootPath, const std::vector<const char*>& overlayPaths, const char* pathsFile, const std::set<ArchPair>& onlyArchs)
+	setSharedDylibs(rootPath, usesOverlay, onlyArchs, rootsPaths);
+}
+
+static void setSharedDylibs(const char* rootPath, bool usesOverlay, const char* pathsFile, const std::set<ArchPair>& onlyArchs)
 {
 	std::vector<const char*> rootsPaths;
 	parsePathsFile(pathsFile, rootsPaths);
-	setSharedDylibs(rootPath, overlayPaths, onlyArchs, rootsPaths);
+	setSharedDylibs(rootPath, usesOverlay, onlyArchs, rootsPaths);
 }
 
 
@@ -3744,8 +2914,8 @@
 
 
 
-static bool updateSharedeCacheFile(const char* rootPath, const std::vector<const char*>& overlayPaths, const char* cacheDir, bool explicitCacheDir, const std::set<ArchPair>& onlyArchs, 
-									bool force, bool alphaSort, bool optimize, bool deleteExistingFirst, bool verify, bool keepSignatures, bool dontMapLocalSymbols)
+static bool updateSharedeCacheFile(const char* rootPath, bool usesOverlay, const char* cacheDir, const std::set<ArchPair>& onlyArchs, 
+									bool force, bool alphaSort, bool optimize, bool deleteExistingFirst, bool verify, bool keepSignatures)
 {
 	bool didUpdate = false;
 	// get dyld load address info
@@ -3770,28 +2940,34 @@
 		else
 			fprintf(stderr, "update_dyld_shared_cache: warning, dyld not available for specified architectures\n");
 		switch ( a->arch ) {
+			case CPU_TYPE_POWERPC:
+				{
+		#if __i386__ || __x86_64__
+					// <rdar://problem/5217377> Rosetta does not work with optimized dyld shared cache
+					SharedCache<ppc> cache(ArchGraph::graphForArchPair(*a), rootPath, cacheDir, alphaSort, verify, false, usesOverlay, dyldBaseAddress);
+					didUpdate |= cache.update(usesOverlay, force, false, deleteExistingFirst, index, archCount, keepSignatures);
+		#else
+					SharedCache<ppc> cache(ArchGraph::graphForArchPair(*a), rootPath, cacheDir, alphaSort, verify, optimize, usesOverlay, dyldBaseAddress);
+					didUpdate |= cache.update(usesOverlay, force, optimize, deleteExistingFirst, index, archCount, keepSignatures);
+		#endif
+				}
+				break;
 			case CPU_TYPE_I386:
 				{
-					SharedCache<x86> cache(ArchGraph::graphForArchPair(*a), rootPath, overlayPaths, cacheDir, explicitCacheDir, alphaSort, verify, optimize, dyldBaseAddress);
-					didUpdate |= cache.update(force, optimize, deleteExistingFirst, index, archCount, keepSignatures, dontMapLocalSymbols);
+					SharedCache<x86> cache(ArchGraph::graphForArchPair(*a), rootPath, cacheDir, alphaSort, verify, optimize, usesOverlay, dyldBaseAddress);
+					didUpdate |= cache.update(usesOverlay, force, optimize, deleteExistingFirst, index, archCount, keepSignatures);
 				}
 				break;
 			case CPU_TYPE_X86_64:
 				{
-					SharedCache<x86_64> cache(ArchGraph::graphForArchPair(*a), rootPath, overlayPaths, cacheDir, explicitCacheDir, alphaSort, verify, optimize, dyldBaseAddress);
-					didUpdate |= cache.update(force, optimize, deleteExistingFirst, index, archCount, keepSignatures, dontMapLocalSymbols);
+					SharedCache<x86_64> cache(ArchGraph::graphForArchPair(*a), rootPath, cacheDir, alphaSort, verify, optimize, usesOverlay, dyldBaseAddress);
+					didUpdate |= cache.update(usesOverlay, force, optimize, deleteExistingFirst, index, archCount, keepSignatures);
 				}
 				break;
 			case CPU_TYPE_ARM:
 				{
-					SharedCache<arm> cache(ArchGraph::graphForArchPair(*a), rootPath, overlayPaths, cacheDir, explicitCacheDir, alphaSort, verify, optimize, dyldBaseAddress);
-					didUpdate |= cache.update(force, optimize, deleteExistingFirst, index, archCount, keepSignatures, dontMapLocalSymbols);
-				}
-				break;
-			case CPU_TYPE_ARM64:
-				{
-					SharedCache<arm64> cache(ArchGraph::graphForArchPair(*a), rootPath, overlayPaths, cacheDir, explicitCacheDir, alphaSort, verify, optimize, dyldBaseAddress);
-					didUpdate |= cache.update(force, optimize, deleteExistingFirst, index, archCount, keepSignatures, dontMapLocalSymbols);
+					SharedCache<arm> cache(ArchGraph::graphForArchPair(*a), rootPath, cacheDir, alphaSort, verify, optimize, usesOverlay, dyldBaseAddress);
+					didUpdate |= cache.update(usesOverlay, force, optimize, deleteExistingFirst, index, archCount, keepSignatures);
 				}
 				break;
 		}
@@ -3814,16 +2990,14 @@
 {
 	std::set<ArchPair> onlyArchs;
 	const char* rootPath = "";
-	std::vector<const char*> overlayPaths;
 	const char* dylibListFile = NULL;
 	bool force = false;
 	bool alphaSort = false;
 	bool optimize = true;
+	bool hasRoot = false;
+	bool hasOverlay = false;
 	bool verify = false;
 	bool keepSignatures = false;
-	bool explicitCacheDir = false;
-	bool dontMapLocalSymbols = false;
-	bool relaunchForHaswell = false;
 	const char* cacheDir = NULL;
 	
 	try {
@@ -3852,12 +3026,8 @@
 				else if ( strcmp(arg, "-no_opt") == 0 ) {
 					optimize = false;
 				}
-				else if ( strcmp(arg, "-dont_map_local_symbols") == 0 ) {
-					dontMapLocalSymbols = true;
-				}
 				else if ( strcmp(arg, "-iPhone") == 0 ) {
 					iPhoneOS = true;
-					alphaSort = true;
 				}
 				else if ( strcmp(arg, "-dylib_list") == 0 ) {
 					dylibListFile = argv[++i];
@@ -3865,30 +3035,34 @@
 						throw "-dylib_list missing path argument";
 				}
 				else if ( (strcmp(arg, "-root") == 0) || (strcmp(arg, "--root") == 0) ) {
+					if ( hasOverlay )
+						throw "cannot use both -root and -overlay";
 					rootPath = argv[++i];
 					if ( rootPath == NULL )
 						throw "-root missing path argument";
+					hasRoot = true;
 				}
 				else if ( strcmp(arg, "-overlay") == 0 ) {
-					const char* path = argv[++i];
-					if ( path == NULL )
-						throw "-overlay missing path argument";
-					overlayPaths.push_back(path);
+					if ( hasRoot )
+						throw "cannot use both -root and -overlay";
+					rootPath = argv[++i];
+					if ( rootPath == NULL )
+						throw "-root missing path argument";
+					hasOverlay = true;
 				}
 				else if ( strcmp(arg, "-cache_dir") == 0 ) {
 					cacheDir = argv[++i];
 					if ( cacheDir == NULL )
 						throw "-cache_dir missing path argument";
-					explicitCacheDir = true;
 				}
 				else if ( strcmp(arg, "-arch") == 0 ) {
 					const char* arch = argv[++i];
-					if ( strcmp(arch, "i386") == 0 )
+					if ( strcmp(arch, "ppc") == 0 ) 
+						onlyArchs.insert(ArchPair(CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL));
+					else if ( strcmp(arch, "i386") == 0 )
 						onlyArchs.insert(ArchPair(CPU_TYPE_I386, CPU_SUBTYPE_I386_ALL));
 					else if ( strcmp(arch, "x86_64") == 0 )
 						onlyArchs.insert(ArchPair(CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL));
-					else if ( strcmp(arch, "x86_64h") == 0 )
-						onlyArchs.insert(ArchPair(CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_H));
 					else if ( strcmp(arch, "armv4t") == 0 )
 						onlyArchs.insert(ArchPair(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V4T));
 					else if ( strcmp(arch, "armv5") == 0 )
@@ -3897,21 +3071,15 @@
 						onlyArchs.insert(ArchPair(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6));
 					else if ( strcmp(arch, "armv7") == 0 )
 						onlyArchs.insert(ArchPair(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7));
-					else if ( strcmp(arch, "armv7f") == 0 )
-						onlyArchs.insert(ArchPair(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7F));
-					else if ( strcmp(arch, "armv7k") == 0 )
-						onlyArchs.insert(ArchPair(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7K));
-					else if ( strcmp(arch, "armv7s") == 0 )
-						onlyArchs.insert(ArchPair(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S));
-					else if ( strcmp(arch, "arm64") == 0 )
-						onlyArchs.insert(ArchPair(CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL));
 					else 
 						throwf("unknown architecture %s", arch);
 				}
 				else if ( strcmp(arg, "-universal_boot") == 0 ) {
+			#if __ppc__
+					throwf("universal_boot option can only be used on Intel machines");
+			#endif
 					onlyArchs.insert(ArchPair(CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL));
 					onlyArchs.insert(ArchPair(CPU_TYPE_I386, CPU_SUBTYPE_I386_ALL));
-					relaunchForHaswell = true;
 				}
 				else {
 					usage();
@@ -3924,7 +3092,7 @@
 			}
 		}
 				
-		// strip tailing slashes on -root 
+		// strip tailing slashes on -root or -overlay
 		// make it a real path so as to not make all dylibs look like symlink aliases
 		if ( rootPath[0] != '\0' ) {
 			char realRootPath[MAXPATHLEN];
@@ -3933,17 +3101,15 @@
 			rootPath = strdup(realRootPath);
 		}
 		
-		// strip tailing slashes on -overlay
-		for (std::vector<const char*>::iterator it=overlayPaths.begin(); it != overlayPaths.end(); ++it) {
-			char realOverlayPath[MAXPATHLEN];
-			if ( realpath(*it, realOverlayPath) == NULL )
-				throwf("realpath() failed on %s\n", *it);
-			*it = strdup(realOverlayPath);
-		}
-
-		// set default location to write cache dir
-		if ( cacheDir == NULL ) 
-			cacheDir = (iPhoneOS ? IPHONE_DYLD_SHARED_CACHE_DIR : MACOSX_DYLD_SHARED_CACHE_DIR);
+		// set location to write cache dir
+		if ( cacheDir == NULL ) {
+			if ( (rootPath[0] == '\0') || hasOverlay ) {
+				cacheDir =  (iPhoneOS ? IPHONE_DYLD_SHARED_CACHE_DIR : MACOSX_DYLD_SHARED_CACHE_DIR);
+			}
+			else {
+				asprintf((char**)&cacheDir, "%s/%s", rootPath, (iPhoneOS ? IPHONE_DYLD_SHARED_CACHE_DIR : MACOSX_DYLD_SHARED_CACHE_DIR));
+			}
+		}
 
 		// if no restrictions specified, use architectures that work on this machine
 		if ( onlyArchs.size() == 0 ) {
@@ -3956,21 +3122,22 @@
 				size_t len = sizeof(int);
 			#if __i386__ || __x86_64__
 				onlyArchs.insert(ArchPair(CPU_TYPE_I386, CPU_SUBTYPE_I386_ALL));
+				// check rosetta is installed
+				char rosettaPath[1024];
+				strlcpy(rosettaPath, rootPath, 1024);
+				strlcat(rosettaPath, "/usr/libexec/oah/translate", 1024);
+				struct stat stat_buf;
+				if ( stat(rosettaPath, &stat_buf) == 0 ) {
+					onlyArchs.insert(ArchPair(CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL));
+				}
+				else if ( hasOverlay ) {
+					// in overlay mode, rosetta may be installed on base system, but is not in update root
+					if ( stat("/usr/libexec/oah/translate", &stat_buf) == 0 ) 
+						onlyArchs.insert(ArchPair(CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL));
+				}
 				// check system is capable of running 64-bit programs
-				if ( (sysctlbyname("hw.optional.x86_64", &available, &len, NULL, 0) == 0) && available ) {
-					// check system is capable of running x86_64h code
-					struct host_basic_info info;
-					mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
-					mach_port_t hostPort = mach_host_self();
-					kern_return_t result = host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&info, &count);
-					mach_port_deallocate(mach_task_self(), hostPort);
-					if ( result != KERN_SUCCESS )
-						throw "host_info() failed";
-					if ( info.cpu_subtype == CPU_SUBTYPE_X86_64_H )
-						onlyArchs.insert(ArchPair(CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_H));
-					else
-						onlyArchs.insert(ArchPair(CPU_TYPE_X86_64, CPU_SUBTYPE_X86_ALL));
-				}
+				if ( (sysctlbyname("hw.optional.x86_64", &available, &len, NULL, 0) == 0) && available )
+					onlyArchs.insert(ArchPair(CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL));
 			#else
 				#error unsupported architecture
 			#endif
@@ -3982,44 +3149,11 @@
 		
 		// build list of shared dylibs
 		if ( dylibListFile != NULL )
-			setSharedDylibs(rootPath, overlayPaths, dylibListFile, onlyArchs);
+			setSharedDylibs(rootPath, hasOverlay, dylibListFile, onlyArchs);
 		else
-			scanForSharedDylibs(rootPath, overlayPaths, "/var/db/dyld/shared_region_roots/", onlyArchs);
-		bool didUpdate = updateSharedeCacheFile(rootPath, overlayPaths, cacheDir, explicitCacheDir, onlyArchs, force, alphaSort, optimize,
-								false, verify, keepSignatures, dontMapLocalSymbols);
-								
-		if ( didUpdate && !iPhoneOS ) {
-			void* handle = dlopen("/usr/lib/libspindump.dylib", RTLD_LAZY);
-			if ( handle != NULL ) {
-				typedef bool (*dscsym_proc_t)(const char *root);
-				dscsym_proc_t proc = (dscsym_proc_t)dlsym(handle, "dscsym_save_nuggets_for_current_caches");
-				const char* nuggetRootPath = "/";
-				if ( !overlayPaths.empty() ) 
-					nuggetRootPath = overlayPaths[0];
-				else if ( rootPath[0] != '\0' )
-					nuggetRootPath = rootPath;
-				(*proc)(nuggetRootPath);
-			}
-			dlclose(handle);
-		}
-
-		if ( relaunchForHaswell ) {
-			char cmd[2048];
-			strlcpy(cmd, argv[0], 2048);
-			strlcat(cmd, " -arch x86_64h", 2048);
-			if ( force )
-				strlcat(cmd, " -force", 2048);
-			if ( verify )
-				strlcat(cmd, " -verify", 2048);
-			if ( alphaSort )
-				strlcat(cmd, " -sort_by_name", 2048);
-			if ( (rootPath != NULL) && (rootPath[0] != '\0') ) {
-				strlcat(cmd, " -root ", 2048);
-				strlcat(cmd, rootPath, 2048);
-			}
-			return system(cmd);
-		}
-
+			scanForSharedDylibs(rootPath, hasOverlay, "/var/db/dyld/shared_region_roots/", onlyArchs);
+		updateSharedeCacheFile(rootPath, hasOverlay, cacheDir, onlyArchs, force, alphaSort, optimize, 
+								false, verify, keepSignatures);
 	}
 	catch (const char* msg) {
 		fprintf(stderr, "update_dyld_shared_cache failed: %s\n", msg);