Loading...
src/dyldAPIs.cpp dyld-132.13 dyld-640.2
--- dyld/dyld-132.13/src/dyldAPIs.cpp
+++ dyld/dyld-640.2/src/dyldAPIs.cpp
@@ -51,11 +51,65 @@
 #include "mach-o/dyld_priv.h"
 
 #include "ImageLoader.h"
+#include "ImageLoaderMachO.h"
 #include "dyld.h"
 #include "dyldLibSystemInterface.h"
+#include "DyldSharedCache.h"
+#include "MachOFile.h"
 
 #undef _POSIX_C_SOURCE
 #include "dlfcn.h"
+
+#if __has_feature(ptrauth_calls)
+  #include <ptrauth.h>
+#endif
+
+#ifndef CPU_SUBTYPE_ARM64_E
+	#define CPU_SUBTYPE_ARM64_E    2
+#endif
+
+// relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables
+#if __LP64__
+#define RELOC_SIZE 3
+#define LC_SEGMENT_COMMAND		LC_SEGMENT_64
+#define LC_ROUTINES_COMMAND		LC_ROUTINES_64
+struct macho_segment_command	: public segment_command_64  {};
+struct macho_section			: public section_64  {};
+struct macho_routines_command	: public routines_command_64  {};
+#else
+#define RELOC_SIZE 2
+#define LC_SEGMENT_COMMAND		LC_SEGMENT
+#define LC_ROUTINES_COMMAND		LC_ROUTINES
+struct macho_segment_command	: public segment_command {};
+struct macho_section			: public section  {};
+struct macho_routines_command	: public routines_command  {};
+#endif
+
+
+// this was in dyld_priv.h but it is no longer exported
+extern "C" {
+    const struct dyld_all_image_infos* _dyld_get_all_image_infos();
+}
+
+// from dyldExceptions.c
+extern "C" void __Unwind_SjLj_SetThreadKey(pthread_key_t key);
+
+// from dyld_gdb.cpp 
+extern void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[]);
+extern uint32_t allImagesCount();
+extern const mach_header* allImagesIndexedMachHeader(uint32_t index);
+extern const char* allImagesIndexedPath(uint32_t index);
+
+extern "C" int _dyld_func_lookup(const char* name, void** address);
+
+extern "C" void* dlopen_internal(const char* path, int mode, void* callerAddress);
+extern "C" bool  dlopen_preflight_internal(const char* path, void* callerAddress);
+extern "C" void* dlsym_internal(void* handle, const char* symbolName, void* callerAddress);
+
+extern "C" void* dlopen_compat(const char* path, int mode);
+extern "C" bool  dlopen_preflight_compat(const char* path);
+extern "C" void* dlsym_compat(void* handle, const char* symbolName);
+
 
 
 // deprecated APIs are still availble on Mac OS X, but not on iPhone OS
@@ -65,6 +119,7 @@
 	#define DEPRECATED_APIS_SUPPORTED 1
 #endif
 
+static bool sDynamicInterposing = false;
 
 #if DEPRECATED_APIS_SUPPORTED
 static char sLastErrorFilePath[1024];
@@ -75,12 +130,10 @@
 // In 10.3.x and earlier all the NSObjectFileImage API's were implemeneted in libSystem.dylib
 // Beginning in 10.4 the NSObjectFileImage API's are implemented in dyld and libSystem just forwards
 // This conditional keeps support for old libSystem's which needed some help implementing the API's
-#define OLD_LIBSYSTEM_SUPPORT (__ppc__ || __i386__)
-
+#define OLD_LIBSYSTEM_SUPPORT (__i386__)
 
 // The following functions have no prototype in any header.  They are special cases
 // where _dyld_func_lookup() is used directly.
-static void _dyld_fork_child();
 static void _dyld_make_delayed_module_initializer_calls();
 static void registerThreadHelpers(const dyld::LibSystemHelpers*);
 #if DEPRECATED_APIS_SUPPORTED
@@ -97,7 +150,11 @@
 static void		client_dyld_lookup_and_bind(const char* symbolName, void** address, NSModule* module);
 static bool		client_NSIsSymbolNameDefined(const char* symbolName);
 #endif // DEPRECATED_APIS_SUPPORTED
+#if SUPPORT_ZERO_COST_EXCEPTIONS
 static bool client_dyld_find_unwind_sections(void* addr, dyld_unwind_sections* info);
+#endif
+#if DEPRECATED_APIS_SUPPORTED
+#endif
 
 static void unimplemented()
 {
@@ -115,10 +172,12 @@
     {"__dyld_dladdr",									(void*)dladdr },
     {"__dyld_dlclose",									(void*)dlclose },
     {"__dyld_dlerror",									(void*)dlerror },
-    {"__dyld_dlopen",									(void*)dlopen },
-    {"__dyld_dlsym",									(void*)dlsym },
-    {"__dyld_dlopen_preflight",							(void*)dlopen_preflight },
-    {"__dyld_get_image_header_containing_address",		(void*)_dyld_get_image_header_containing_address },
+    {"__dyld_dlopen_internal",							(void*)dlopen_internal },
+    {"__dyld_dlsym_internal",							(void*)dlsym_internal },
+    {"__dyld_dlopen_preflight_internal",				(void*)dlopen_preflight_internal },
+    {"__dyld_dlopen",									(void*)dlopen_compat },
+    {"__dyld_dlsym",									(void*)dlsym_compat },
+    {"__dyld_dlopen_preflight",							(void*)dlopen_preflight_compat },
 	{"__dyld_image_count",								(void*)_dyld_image_count },
     {"__dyld_get_image_header",							(void*)_dyld_get_image_header },
     {"__dyld_get_image_vmaddr_slide",					(void*)_dyld_get_image_vmaddr_slide },
@@ -127,19 +186,28 @@
     {"__dyld__NSGetExecutablePath",						(void*)_NSGetExecutablePath },
 
 	// SPIs
-	{"__dyld_dyld_register_image_state_change_handler",	(void*)dyld_register_image_state_change_handler },
   	{"__dyld_register_thread_helpers",					(void*)registerThreadHelpers },
 	{"__dyld_fork_child",								(void*)_dyld_fork_child },
-    {"__dyld_moninit",									(void*)_dyld_moninit },
     {"__dyld_make_delayed_module_initializer_calls",	(void*)_dyld_make_delayed_module_initializer_calls },
 	{"__dyld_get_all_image_infos",						(void*)_dyld_get_all_image_infos },
-#if !__arm__
+#if SUPPORT_ZERO_COST_EXCEPTIONS
 	{"__dyld_find_unwind_sections",						(void*)client_dyld_find_unwind_sections },
 #endif
-#if __i386__ || __x86_64__
+#if __i386__ || __x86_64__ || __arm__ || __arm64__
 	{"__dyld_fast_stub_entry",							(void*)dyld::fastBindLazySymbol },
 #endif
 	{"__dyld_image_path_containing_address",			(void*)dyld_image_path_containing_address },
+	{"__dyld_shared_cache_some_image_overridden",		(void*)dyld_shared_cache_some_image_overridden },
+	{"__dyld_process_is_restricted",					(void*)dyld::processIsRestricted },
+	{"__dyld_dynamic_interpose",						(void*)dyld_dynamic_interpose },
+	{"__dyld_shared_cache_file_path",					(void*)dyld::getStandardSharedCacheFilePath },
+    {"__dyld_get_image_header_containing_address",		(void*)dyld_image_header_containing_address },
+    {"__dyld_is_memory_immutable",						(void*)_dyld_is_memory_immutable },
+    {"__dyld_objc_notify_register",						(void*)_dyld_objc_notify_register },
+    {"__dyld_get_shared_cache_uuid",					(void*)_dyld_get_shared_cache_uuid },
+    {"__dyld_get_shared_cache_range",					(void*)_dyld_get_shared_cache_range },
+    {"__dyld_images_for_addresses",						(void*)_dyld_images_for_addresses },
+    {"__dyld_register_for_image_loads",					(void*)_dyld_register_for_image_loads },
 
 	// deprecated
 #if DEPRECATED_APIS_SUPPORTED
@@ -149,7 +217,6 @@
     {"__dyld_install_handlers",						(void*)_dyld_install_handlers },
     {"__dyld_link_edit_error",						(void*)NSLinkEditError },
     {"__dyld_unlink_module",						(void*)NSUnLinkModule },
-    {"__dyld_bind_objc_module",						(void*)_dyld_bind_objc_module },
     {"__dyld_bind_fully_image_containing_address",  (void*)_dyld_bind_fully_image_containing_address },
     {"__dyld_image_containing_address",				(void*)_dyld_image_containing_address },
     {"__dyld_register_binding_handler",				(void*)_dyld_register_binding_handler },
@@ -176,7 +243,6 @@
     {"__dyld_NSCreateObjectFileImageFromMemory",		(void*)NSCreateObjectFileImageFromMemory },
     {"__dyld_NSDestroyObjectFileImage",					(void*)NSDestroyObjectFileImage },
     {"__dyld_NSLinkModule",								(void*)NSLinkModule },
-    {"__dyld_NSHasModInitObjectFileImage",				(void*)NSHasModInitObjectFileImage },
     {"__dyld_NSSymbolDefinitionCountInObjectFileImage",	(void*)NSSymbolDefinitionCountInObjectFileImage },
     {"__dyld_NSSymbolDefinitionNameInObjectFileImage",	(void*)NSSymbolDefinitionNameInObjectFileImage },
     {"__dyld_NSIsSymbolDefinedInObjectFileImage",		(void*)NSIsSymbolDefinedInObjectFileImage },
@@ -231,6 +297,10 @@
 	const void*		imageBaseAddress;	// not used with OFI created from files
 	size_t			imageLength;		// not used with OFI created from files
 };
+typedef __NSObjectFileImage*  NSObjectFileImage;
+
+
+VECTOR_NEVER_DESTRUCTED(NSObjectFileImage);
 static std::vector<NSObjectFileImage> sObjectFileImages;
 
 
@@ -271,7 +341,7 @@
 		dyld::log("%s(...)\n", __func__);
 	const char* exePath = dyld::getExecutablePath();
 	if(*bufsize < strlen(exePath) + 1){
-	    *bufsize = strlen(exePath) + 1;
+	    *bufsize = (uint32_t)(strlen(exePath) + 1);
 	    return -1;
 	}
 	strcpy(buf, exePath);
@@ -282,27 +352,23 @@
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s()\n", __func__);
-	return dyld::getImageCount();
+	return allImagesCount();
 }
 
 const struct mach_header* _dyld_get_image_header(uint32_t image_index)
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%u)\n", __func__, image_index);
-	ImageLoader* image = dyld::getIndexedImage(image_index);
-	if ( image != NULL )
-		return (struct mach_header*)image->machHeader();
-	else
-		return NULL;
+	return allImagesIndexedMachHeader(image_index);
 }
 
 intptr_t _dyld_get_image_vmaddr_slide(uint32_t image_index)
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%u)\n", __func__, image_index);
-	ImageLoader* image = dyld::getIndexedImage(image_index);
-	if ( image != NULL )
-		return image->getSlide();
+	const struct mach_header* mh = allImagesIndexedMachHeader(image_index);
+	if ( mh != NULL )
+		return ImageLoaderMachO::computeSlide(mh);
 	else
 		return 0;
 }
@@ -311,11 +377,7 @@
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%p)\n", __func__, mh);
-	ImageLoader* image = dyld::findImageByMachHeader(mh);
-	if ( image != NULL )
-		return image->getSlide();
-	else
-		return 0;
+	return ImageLoaderMachO::computeSlide(mh);
 }
 
 
@@ -323,17 +385,36 @@
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%u)\n", __func__, image_index);
-	ImageLoader* image = dyld::getIndexedImage(image_index);
-	if ( image != NULL )
-		return image->getPath();
-	else
-		return NULL;
-}
-
-const struct mach_header * _dyld_get_image_header_containing_address(const void* address)
+	return allImagesIndexedPath(image_index);
+}
+
+static const void *stripPointer(const void *ptr) {
+#if __has_feature(ptrauth_calls)
+	return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
+#else
+	return ptr;
+#endif
+}
+
+static void *stripPointer(void *ptr) {
+#if __has_feature(ptrauth_calls)
+	return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
+#else
+	return ptr;
+#endif
+}
+
+const struct mach_header * dyld_image_header_containing_address(const void* address)
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%p)\n", __func__, address);
+	address = stripPointer(address);
+#if SUPPORT_ACCELERATE_TABLES
+	const mach_header* mh;
+	const char* path;
+	if ( dyld::addressInCache(address, &mh, &path) )
+		return mh;
+#endif
 	ImageLoader* image = dyld::findImageContainingAddress(address);
 	if ( image != NULL ) 
 		return image->machHeader();
@@ -527,14 +608,16 @@
 		context.mustBeBundle		= false;
 		context.mustBeDylib			= true;
 		context.canBePIE			= false;
+		context.enforceIOSMac		= false;
 		context.origin				= callerImage != NULL ? callerImage->getPath() : NULL; // caller's image's path
 		context.rpath				= &callersRPaths; 	// rpaths from caller and main executable
-				
-		image = load(path, context);
+
+		unsigned cacheIndex;
+		image = load(path, context, cacheIndex);
 		if ( image != NULL ) {
 			if ( context.matchByInstallName )
 				image->setMatchInstallPath(true);
-			dyld::link(image, false, callersRPaths);
+			dyld::link(image, false, false, callersRPaths, cacheIndex);
 			dyld::runInitializers(image);
 			// images added with NSAddImage() can never be unloaded
 			image->setNeverUnload(); 
@@ -573,7 +656,7 @@
 	const bool dontLoad = ( (options & NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED) != 0 );
 	const bool search = ( (options & NSADDIMAGE_OPTION_WITH_SEARCHING) != 0 );
 	const bool matchInstallName = ( (options & NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME) != 0 );
-	const bool abortOnError = ( (options & NSADDIMAGE_OPTION_RETURN_ON_ERROR|NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED) == 0 );
+	const bool abortOnError = ( (options & (NSADDIMAGE_OPTION_RETURN_ON_ERROR|NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED)) == 0 );
 	void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
 	return addImage(callerAddress, path, search, dontLoad, matchInstallName, abortOnError);
 }
@@ -621,6 +704,7 @@
 	dyld::clearErrorMessage();
 	ImageLoader* image = dyld::findImageByMachHeader(mh);
 	if ( image != NULL ) {
+		const char* symbolToFind = symbolName;
 		try {
 			if ( options & NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_FULLY ) {
 				image->bindAllLazyPointers(dyld::gLinkContext, true);
@@ -634,7 +718,7 @@
 				dyldAPIhalt(__func__, msg);
 			}
 		}
-		symbol = image->findExportedSymbol(symbolName, true, NULL);
+		symbol = image->findExportedSymbol(symbolToFind, true, NULL);
 	}
 	if ( dyld::gLogAPIs && (symbol == NULL) )
 		dyld::log("%s(%p, \"%s\", 0x%08X) ==> NULL\n", __func__, mh, symbolName, options);
@@ -714,18 +798,11 @@
 	return FALSE; 
 }
 
-void _dyld_bind_objc_module(const void *objc_module)
-{
-	if ( dyld::gLogAPIs )
-		dyld::log("%s(%p)\n", __func__, objc_module);
-	// do nothing, with new dyld everything already bound
-}
-
-
 bool _dyld_bind_fully_image_containing_address(const void* address)
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%p)\n", __func__, address);
+	address = stripPointer(address);
 	dyld::clearErrorMessage();
 	ImageLoader* image = dyld::findImageContainingAddress(address);
 	if ( image != NULL ) {
@@ -776,10 +853,12 @@
 		context.mustBeBundle		= true;
 		context.mustBeDylib			= false;
 		context.canBePIE			= false;
+		context.enforceIOSMac		= false;
 		context.origin				= callerImage != NULL ? callerImage->getPath() : NULL; // caller's image's path
 		context.rpath				= NULL; // support not yet implemented
 
-		ImageLoader* image = dyld::load(pathName, context);
+		unsigned cacheIndex;
+		ImageLoader* image = dyld::load(pathName, context, cacheIndex);
 		// Note:  We DO NOT link the image!  NSLinkModule will do that
 		if ( image != NULL ) {
 			if ( !image->isBundle() ) {
@@ -828,8 +907,8 @@
 
 static bool validOFI(NSObjectFileImage objectFileImage)
 {
-	const int ofiCount = sObjectFileImages.size();
-	for (int i=0; i < ofiCount; ++i) {
+	const size_t ofiCount = sObjectFileImages.size();
+	for (size_t i=0; i < ofiCount; ++i) {
 		if ( sObjectFileImages[i] == objectFileImage )
 			return true;
 	}
@@ -884,13 +963,6 @@
 	return false;
 }
 
-bool NSHasModInitObjectFileImage(NSObjectFileImage objectFileImage)
-{
-	if ( dyld::gLogAPIs )
-		dyld::log("%s(%p)\n", __func__, objectFileImage);
-	return objectFileImage->image->needsInitialization();
-}
-
 uint32_t NSSymbolDefinitionCountInObjectFileImage(NSObjectFileImage objectFileImage)
 {
 	if ( dyld::gLogAPIs )
@@ -964,16 +1036,26 @@
 	
 	dyld::clearErrorMessage();
 	try {
+		if ( (options & NSLINKMODULE_OPTION_CAN_UNLOAD) != 0 )
+			objectFileImage->image->setCanUnload();
+
 		// NSLinkModule allows a bundle to be link multpile times
 		// each link causes the bundle to be copied to a new address
 		if ( objectFileImage->image->isLinked() ) {
 			// already linked, so clone a new one and link it
 			objectFileImage->image = dyld::cloneImage(objectFileImage->image);
 		}
-			
+		
 		// for memory based images, set moduleName as the name anyone calling _dyld_get_image_name() will see
-		if ( objectFileImage->image->getPath() == NULL )
+		if ( objectFileImage->image->getPath() == NULL ) {
 			objectFileImage->image->setPath(moduleName);
+			// <rdar://problem/8812589> dyld has NULL paths in image info array
+			dyld_image_info info;
+			info.imageLoadAddress = objectFileImage->image->machHeader();
+			info.imageFilePath = moduleName;
+			info.imageFileModDate = 0;
+			addImagesToAllImages(1, &info);
+		}
 
 		// support private bundles
 		if ( (options & NSLINKMODULE_OPTION_PRIVATE) != 0 )
@@ -983,7 +1065,7 @@
 		bool forceLazysBound = ( (options & NSLINKMODULE_OPTION_BINDNOW) != 0 );
 		
 		// load libraries, rebase, bind, to make this image usable
-		dyld::link(objectFileImage->image, forceLazysBound, ImageLoader::RPathChain(NULL,NULL));
+		dyld::link(objectFileImage->image, forceLazysBound, false, ImageLoader::RPathChain(NULL,NULL), UINT32_MAX);
 		
 		// bump reference count to keep this bundle from being garbage collected
 		objectFileImage->image->incrementDlopenReferenceCount();
@@ -1029,7 +1111,7 @@
 			bool forceLazysBound = ( (options & NSLINKMODULE_OPTION_BINDNOW) != 0 );
 			
 			// load libraries, rebase, bind, to make this image usable
-			dyld::link(image, forceLazysBound, ImageLoader::RPathChain(NULL,NULL));
+			dyld::link(image, forceLazysBound, false, ImageLoader::RPathChain(NULL,NULL), UINT32_MAX);
 			
 			// run initializers unless magic flag says not to
 			if ( (options & NSLINKMODULE_OPTION_DONT_CALL_MOD_INIT_ROUTINES) == 0 )
@@ -1092,6 +1174,19 @@
 	ImageLoader* image = NSModuleToImageLoader(module);
 	if ( image == NULL ) 
 		return false;
+	dyld::runImageStaticTerminators(image);
+	if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 13) ) {
+		__cxa_range_t ranges[image->segmentCount()];
+		int rangeCount = 0;
+		for (unsigned int j=0; j < image->segmentCount(); ++j) {
+			if ( !image->segExecutable(j) )
+				continue;
+			ranges[rangeCount].addr = (const void*)image->segActualLoadAddress(j);
+			ranges[rangeCount].length = image->segSize(j);
+			++rangeCount;
+		}
+		(*dyld::gLibSystemHelpers->cxa_finalize_ranges)(ranges, rangeCount);
+	}
 	dyld::removeImage(image);
 	
 	if ( (options & NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED) != 0 )
@@ -1101,9 +1196,9 @@
 
 	// Only delete image if there is no ofi referencing it
 	// That means the ofi was destroyed after linking, so no one is left to delete this image	
-	const int ofiCount = sObjectFileImages.size();
+	const size_t ofiCount = sObjectFileImages.size();
 	bool found = false;
-	for (int i=0; i < ofiCount; ++i) {
+	for (size_t i=0; i < ofiCount; ++i) {
 		NSObjectFileImage ofi = sObjectFileImages[i];
 		if ( ofi->image == image )
 			found = true;
@@ -1150,7 +1245,7 @@
 
 
 // Call by fork() in libSystem after the kernel trap is done on the child side
-static void _dyld_fork_child()
+void _dyld_fork_child()
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s()\n", __func__);
@@ -1167,34 +1262,11 @@
 	// If dyld is sending load/unload notices to CoreSymbolication, the shared memory
 	// page is not copied on fork. <rdar://problem/6797342>
  	// NULL the CoreSymbolication shared memory pointer to prevent a crash.
- 	dyld_all_image_infos.coreSymbolicationShmPage = NULL;
+ 	dyld::gProcessInfo->coreSymbolicationShmPage = NULL;
 	// for safety, make sure child starts with clean systemOrderFlag
-	dyld_all_image_infos.systemOrderFlag = 0;
-}
-
-
-
-typedef void (*MonitorProc)(char *lowpc, char *highpc);
-
-static void monInitCallback(ImageLoader* image, void* userData)
-{
-	MonitorProc proc = (MonitorProc)userData;
-	void* start;
-	size_t length;
-	if ( image->getSectionContent("__TEXT", "__text", &start, &length) ) {
-		proc((char*)start, (char*)start+length);
-	}
-}
-
-//
-// _dyld_moninit is called from profiling runtime routine moninit().
-// dyld calls back with the range of each __TEXT/__text section in every
-// linked image.
-//
-void _dyld_moninit(MonitorProc proc)
-{
-	dyld::forEachImageDo(&monInitCallback, (void*)proc);
-}
+	dyld::gProcessInfo->systemOrderFlag = 0;
+}
+
 
 #if DEPRECATED_APIS_SUPPORTED
 // returns true if prebinding was used in main executable
@@ -1229,13 +1301,13 @@
 
 #endif // DEPRECATED_APIS_SUPPORTED
 
-bool lookupDyldFunction(const char* name, uintptr_t* address)
+int _dyld_func_lookup(const char* name, void** address)
 {
 	for (const dyld_func* p = dyld_funcs; p->name != NULL; ++p) {
 	    if ( strcmp(p->name, name) == 0 ) {
 			if( p->implementation == unimplemented )
 				dyld::log("unimplemented dyld function: %s\n", p->name);
-			*address = (uintptr_t)p->implementation;
+			*address = p->implementation;
 			return true;
 	    }
 	}
@@ -1243,18 +1315,33 @@
 	return false;
 }
 
+
 static void registerThreadHelpers(const dyld::LibSystemHelpers* helpers)
 {
 	dyld::gLibSystemHelpers = helpers;
 	
-	// let gdb know it is safe to run code in inferior that might call malloc()
-	dyld_all_image_infos.libSystemInitialized = true;	
+#if !SUPPORT_ZERO_COST_EXCEPTIONS
+	if ( helpers->version >= 5 )  {
+		// create key use by dyld exception handling
+		pthread_key_t key;
+		int result = helpers->pthread_key_create(&key, NULL);
+		if ( result == 0 )
+			__Unwind_SjLj_SetThreadKey(key);
+	}
+#endif
 }
 
 
 static void dlerrorClear()
 {
 	if ( dyld::gLibSystemHelpers != NULL ) {
+		// <rdar://problem/10595338> dlerror buffer leak
+		// dlerrorClear() should not force allocation, but zero it if already allocated
+		if ( dyld::gLibSystemHelpers->version >= 10 ) {
+			if ( ! (*dyld::gLibSystemHelpers->hasPerThreadBufferFor_dlerror)() )
+				return;
+		}
+
 		// first char of buffer is flag whether string (starting at second char) is valid
 		char* buffer = (*dyld::gLibSystemHelpers->getThreadBufferFor_dlerror)(2);
 		buffer[0] = '\0';
@@ -1273,24 +1360,51 @@
 }
 
 
-bool dlopen_preflight(const char* path)
+bool dlopen_preflight_internal(const char* path, void* callerAddress)
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%s)\n", __func__, path);
 
 	dlerrorClear();
 	
-#if DYLD_SHARED_CACHE_SUPPORT
+	CRSetCrashLogMessage("dyld: in dlopen_preflight()");
+
+	const bool leafName = (strchr(path, '/') == NULL);
+	const bool absolutePath = (path[0] == '/');
+#if __IPHONE_OS_VERSION_MIN_REQUIRED
+	char canonicalPath[PATH_MAX]; 
+	// <rdar://problem/7017050> dlopen() not opening frameworks from shared cache with // or ./ in path
+	if ( !leafName ) {
+		// make path canonical if it contains a // or ./
+		if ( (strstr(path, "//") != NULL) || (strstr(path, "./") != NULL) ) {
+			const char* lastSlash = strrchr(path, '/');
+			char dirPath[PATH_MAX]; 
+			if ( strlcpy(dirPath, path, sizeof(dirPath)) < sizeof(dirPath) ) {
+				dirPath[lastSlash-path] = '\0';
+				if ( realpath(dirPath, canonicalPath) ) {
+					strlcat(canonicalPath, "/", sizeof(canonicalPath));
+					if ( strlcat(canonicalPath, lastSlash+1, sizeof(canonicalPath)) < sizeof(canonicalPath) ) {
+						// if all fit in buffer, use new canonical path
+						path = canonicalPath;
+					}
+				}
+			}
+		}
+	}
+#endif
+#if SUPPORT_ACCELERATE_TABLES
+	if  ( dyld::isPathInCache(path) )
+		return true;
+#endif
+
 	// <rdar://problem/5910137> dlopen_preflight() on image in shared cache leaves it loaded but not objc initialized
 	// if requested path is to something in the dyld shared cache, always succeed
 	if ( dyld::inSharedCache(path) )
 		return true;
-#endif
 	
 	bool result = false;
 	std::vector<const char*> rpathsFromCallerImage;
 	try {
-		void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
 		ImageLoader* callerImage = dyld::findImageContainingAddress(callerAddress);
 		// for dlopen, use rpath from caller image and from main executable
 		if ( callerImage != NULL )
@@ -1301,8 +1415,6 @@
 		}
 
 		ImageLoader*	image = NULL;
-		const bool leafName = (strchr(path, '/') == NULL);
-		const bool absolutePath = (path[0] == '/');
 		dyld::LoadContext context;
 		context.useSearchPaths	= true;
 		context.useFallbackPaths= leafName;					// a partial path implies don't use fallback paths
@@ -1313,12 +1425,14 @@
 		context.mustBeBundle	= false;
 		context.mustBeDylib		= false;
 		context.canBePIE		= true;
+		context.enforceIOSMac	= false;
 		context.origin			= callerImage != NULL ? callerImage->getPath() : NULL; // caller's image's path
 		context.rpath			= &callersRPaths;	// rpaths from caller and main executable
-		
-		image = load(path, context);
+
+		unsigned cacheIndex;
+		image = load(path, context, cacheIndex);
 		if ( image != NULL ) {
-			dyld::preflight(image, callersRPaths);	// image object deleted by dyld::preflight()
+			dyld::preflight(image, callersRPaths, cacheIndex);	// image object deleted by dyld::preflight()
 			result = true;
 		}
 	}
@@ -1333,17 +1447,46 @@
 		const char* str = *it;
 		free((void*)str);
 	}
+	CRSetCrashLogMessage(NULL);
 	return result;
 }
 
-
-void* dlopen(const char* path, int mode)
+#if SUPPORT_ACCELERATE_TABLES
+bool static callerIsNonOSApp(void* callerAddress, const char** shortName)
+{
+	*shortName = NULL;
+	const mach_header* unusedMh;
+	const char* unusedPath;
+	unsigned unusedIndex;
+	// any address in shared cache is not from app
+	if ( dyld::addressInCache(callerAddress, &unusedMh, &unusedPath, &unusedIndex) )
+		return false;
+
+	ImageLoader* callerImage = dyld::findImageContainingAddress(callerAddress);
+	if ( callerImage == NULL )
+		return false;
+
+	*shortName = callerImage->getShortName();
+	return ( strncmp(callerImage->getPath(), "/var/containers/", 16) == 0 );
+}
+#endif
+
+void* dlopen_internal(const char* path, int mode, void* callerAddress)
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%s, 0x%08X)\n", __func__, ((path==NULL) ? "NULL" : path), mode);
 
+#if SUPPORT_ACCELERATE_TABLES
+	if ( dyld::gLogAppAPIs ) {
+		const char* shortName;
+		if ( callerIsNonOSApp(callerAddress, &shortName) ) {
+			dyld::log("%s: %s(%s, 0x%08X)\n", shortName, __func__, ((path==NULL) ? "NULL" : path), mode);
+		}
+	}
+#endif
+
 	dlerrorClear();
-	
+
 	// passing NULL for path means return magic object
 	if ( path == NULL ) {
 		// RTLD_FIRST means any dlsym() calls on the handle should only search that handle and not subsequent images
@@ -1357,25 +1500,56 @@
 	bool lockHeld = false;
 	if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 4) ) {
 		dyld::gLibSystemHelpers->acquireGlobalDyldLock();
+		CRSetCrashLogMessage("dyld: in dlopen()");
 		lockHeld = true;
 	}
 		
 	void* result = NULL;
+	const bool leafName = (strchr(path, '/') == NULL);
+	const bool absolutePath = (path[0] == '/');
+#if __IPHONE_OS_VERSION_MIN_REQUIRED
+	char canonicalPath[PATH_MAX]; 
+	// <rdar://problem/7017050> dlopen() not opening frameworks from shared cache with // or ./ in path
+	if ( !leafName ) {
+		// make path canonical if it contains a // or ./
+		if ( (strstr(path, "//") != NULL) || (strstr(path, "./") != NULL) ) {
+			const char* lastSlash = strrchr(path, '/');
+			char dirPath[PATH_MAX]; 
+			if ( strlcpy(dirPath, path, sizeof(dirPath)) < sizeof(dirPath) ) {
+				dirPath[lastSlash-path] = '\0';
+				if ( realpath(dirPath, canonicalPath) ) {
+					strlcat(canonicalPath, "/", sizeof(canonicalPath));
+					if ( strlcat(canonicalPath, lastSlash+1, sizeof(canonicalPath)) < sizeof(canonicalPath) ) {
+						// if all fit in buffer, use new canonical path
+						path = canonicalPath;
+					}
+				}
+			}
+		}
+	}
+#endif
+#if SUPPORT_ACCELERATE_TABLES
+	if ( dyld::dlopenFromCache(path, mode, &result) ) {
+		// Note: dlopenFromCache() releases the lock
+		if ( dyld::gLogAPIs )
+			dyld::log("  %s(%s) ==> %p\n", __func__, path, result);
+		return result;
+	}
+#endif
+
 	ImageLoader* image = NULL;
 	std::vector<const char*> rpathsFromCallerImage;
+	ImageLoader::RPathChain callersRPaths(NULL, &rpathsFromCallerImage);
 	try {
-		void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
 		ImageLoader* callerImage = dyld::findImageContainingAddress(callerAddress);
-		// for dlopen, use rpath from caller image and from main executable
-		if ( callerImage != NULL )
-			callerImage->getRPaths(dyld::gLinkContext, rpathsFromCallerImage);
-		ImageLoader::RPathChain callersRPaths(NULL, &rpathsFromCallerImage);
-		if ( callerImage != dyld::mainExecutable() ) {
-			dyld::mainExecutable()->getRPaths(dyld::gLinkContext, rpathsFromCallerImage);
+		if ( (mode & RTLD_NOLOAD) == 0 ) {
+			// for dlopen, use rpath from caller image and from main executable
+			if ( callerImage != NULL )
+				callerImage->getRPaths(dyld::gLinkContext, rpathsFromCallerImage);
+			if ( callerImage != dyld::mainExecutable() )
+				dyld::mainExecutable()->getRPaths(dyld::gLinkContext, rpathsFromCallerImage);
 		}
  
-		const bool leafName = (strchr(path, '/') == NULL);
-		const bool absolutePath = (path[0] == '/');
 		dyld::LoadContext context;
 		context.useSearchPaths	= true;
 		context.useFallbackPaths= leafName;				// a partial path means no fallback paths
@@ -1386,10 +1560,24 @@
 		context.mustBeBundle	= false;
 		context.mustBeDylib		= false;
 		context.canBePIE		= true;
+		context.enforceIOSMac	= false;
 		context.origin			= callerImage != NULL ? callerImage->getPath() : NULL; // caller's image's path
 		context.rpath			= &callersRPaths;				// rpaths from caller and main executable
-		
-		image = load(path, context);
+
+		unsigned cacheIndex;
+		image = load(path, context, cacheIndex);
+#if SUPPORT_ACCELERATE_TABLES
+		if ( (image != NULL) && (cacheIndex != UINT32_MAX) ) {
+            // found in cache, but under a different path
+            const char* betterPath = dyld::getPathFromIndex(cacheIndex);
+            if ( (betterPath != NULL) && dyld::dlopenFromCache(betterPath, mode, &result) ) {
+                // Note: dlopenFromCache() releases the lock
+                if ( dyld::gLogAPIs )
+                    dyld::log("  %s(%s) ==> %p\n", __func__, path, result);
+                return result;
+			}
+		}
+#endif
 		if ( image != NULL ) {
 			// bump reference count.  Do this before link() so that if an initializer calls dlopen and fails
 			// this image is not garbage collected
@@ -1398,8 +1586,13 @@
 			if ( (mode & RTLD_NOLOAD) == 0 ) {
 				bool alreadyLinked = image->isLinked();
 				bool forceLazysBound = ( (mode & RTLD_NOW) != 0 );
-				dyld::link(image, forceLazysBound, callersRPaths);
-				if ( ! alreadyLinked ) {
+				dyld::link(image, forceLazysBound, false, callersRPaths, cacheIndex);
+				if ( alreadyLinked ) {
+					// upgrade
+					if ( ((mode & RTLD_LOCAL) == 0) && image->hasHiddenExports() )
+						image->setHideExports(false);
+				}
+				else {
 					// only hide exports if image is not already in use
 					if ( (mode & RTLD_LOCAL) != 0 )
 						image->setHideExports(true);
@@ -1416,6 +1609,7 @@
 			
 			// release global dyld lock early, this enables initializers to do threaded operations
 			if ( lockHeld ) {
+				CRSetCrashLogMessage(NULL);
 				dyld::gLibSystemHelpers->releaseGlobalDyldLock();
 				lockHeld = false;
 			}
@@ -1443,9 +1637,12 @@
 			// load() succeeded but, link() failed
 			// back down reference count and do GC
 			image->decrementDlopenReferenceCount();
-			dyld::garbageCollectImages();
+			if ( image->dlopenCount() == 0 )
+				dyld::garbageCollectImages();
 		}
 		const char* str = dyld::mkstringf("dlopen(%s, %d): %s", path, mode, msg);
+		if ( dyld::gLogAPIs )
+			dyld::log("  %s() failed, error: '%s'\n", __func__, str);
 		dlerrorSet(str);
 		free((void*)str);
 		free((void*)msg); 	// our free() will do nothing if msg is a string literal
@@ -1462,12 +1659,14 @@
 		dlerrorSet("image not already loaded");
 	}
 	
-	if ( lockHeld ) 
+	if ( lockHeld ) {
+		CRSetCrashLogMessage(NULL);
 		dyld::gLibSystemHelpers->releaseGlobalDyldLock();
+	}
+	if ( dyld::gLogAPIs && (result != NULL) )
+		dyld::log("  %s(%s) ==> %p\n", __func__, path, result);
 	return result;
 }
-
-
 
 int dlclose(void* handle)
 {
@@ -1479,7 +1678,14 @@
 		return 0;
 	if ( handle == RTLD_DEFAULT )
 		return 0;
-	
+
+#if SUPPORT_ACCELERATE_TABLES
+	if ( dyld::isCacheHandle(handle) ) {
+		dlerrorClear();
+		return 0;
+	}
+#endif
+
 	ImageLoader* image = (ImageLoader*)(((uintptr_t)handle) & (-4));	// clear mode bits
 	if ( dyld::validImage(image) ) {
 		dlerrorClear();
@@ -1489,7 +1695,8 @@
 			return -1;
 		}
 		// remove image if reference count went to zero
-		dyld::garbageCollectImages();
+		if ( image->dlopenCount() == 0 )
+			dyld::garbageCollectImages();
 		return 0;
 	}
 	else {
@@ -1505,28 +1712,53 @@
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%p, %p)\n", __func__, address, info);
 
+	// <rdar://problem/42171466> calling dladdr(xx,NULL) crashes
+	if ( info == NULL )
+		return 0; // failure
+
+	address = stripPointer(address);
+
+	CRSetCrashLogMessage("dyld: in dladdr()");
+#if SUPPORT_ACCELERATE_TABLES
+	if ( dyld::dladdrFromCache(address, info) ) {
+		CRSetCrashLogMessage(NULL);
+		return 1; // success
+	}
+#endif
+
 	ImageLoader* image = dyld::findImageContainingAddress(address);
 	if ( image != NULL ) {
-		info->dli_fname = image->getPath();
+		info->dli_fname = image->getRealPath();
 		info->dli_fbase = (void*)image->machHeader();
 		if ( address == info->dli_fbase ) {
 			// special case lookup of header
 			info->dli_sname = "__dso_handle";
 			info->dli_saddr = info->dli_fbase;
+			CRSetCrashLogMessage(NULL);
 			return 1; // success
 		}
 		// find closest symbol in the image
 		info->dli_sname = image->findClosestSymbol(address, (const void**)&info->dli_saddr);
+		// never return the mach_header symbol
+		if ( info->dli_saddr == info->dli_fbase ) {
+			info->dli_sname = NULL;
+			info->dli_saddr = NULL;
+			CRSetCrashLogMessage(NULL);
+			return 1; // success
+		}
 		if ( info->dli_sname != NULL ) {
 			if ( info->dli_sname[0] == '_' )
 				info->dli_sname = info->dli_sname +1; // strip off leading underscore
 			//dyld::log("dladdr(%p) => %p %s\n", address, info->dli_saddr, info->dli_sname);
+			CRSetCrashLogMessage(NULL);
 			return 1; // success
 		}
 		info->dli_sname = NULL;
 		info->dli_saddr = NULL;
+		CRSetCrashLogMessage(NULL);
 		return 1; // success
 	}
+	CRSetCrashLogMessage(NULL);
 	return 0;  // failure
 }
 
@@ -1537,6 +1769,12 @@
 		dyld::log("%s()\n", __func__);
 
 	if ( dyld::gLibSystemHelpers != NULL ) {
+		// if using newer libdyld.dylib and buffer if buffer not yet allocated, return NULL
+		if ( dyld::gLibSystemHelpers->version >= 10 ) {
+			if ( ! (*dyld::gLibSystemHelpers->hasPerThreadBufferFor_dlerror)() )
+				return NULL;
+		}
+
 		// first char of buffer is flag whether string (starting at second char) is valid
 		char* buffer = (*dyld::gLibSystemHelpers->getThreadBufferFor_dlerror)(2);
 		if ( buffer[0] != '\0' ) {	// if valid buffer
@@ -1547,15 +1785,26 @@
 	return NULL;
 }
 
-void* dlsym(void* handle, const char* symbolName)
+void* dlsym_internal(void* handle, const char* symbolName, void* callerAddress)
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%p, %s)\n", __func__, handle, symbolName);
 
+#if SUPPORT_ACCELERATE_TABLES
+	if ( dyld::gLogAppAPIs ) {
+		const char* shortName;
+		if ( callerIsNonOSApp(callerAddress, &shortName) ) {
+			dyld::log("%s: %s(%p, %s)\n", shortName, __func__, handle, symbolName);
+		}
+	}
+#endif
+
+	CRSetCrashLogMessage("dyld: in dlsym()");
 	dlerrorClear();
 
 	const ImageLoader* image;
 	const ImageLoader::Symbol* sym;
+	void* result;
 
 	// dlsym() assumes symbolName passed in is same as in C source code
 	// dyld assumes all symbol names have an underscore prefix
@@ -1566,53 +1815,168 @@
 	// magic "search all" handle
 	if ( handle == RTLD_DEFAULT ) {
 		if ( dyld::flatFindExportedSymbol(underscoredName, &sym, &image) ) {
-			return (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext);
+			CRSetCrashLogMessage(NULL);
+			result = (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext, NULL, false, underscoredName);
+#if __has_feature(ptrauth_calls)
+			// Sign the pointer if it points to a function
+			// Note we only do this if the main executable is arm64e as otherwise we
+			// may end up calling containsAddress on the accelerator tables.
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
+				const ImageLoader* symbolImage = image;
+				if (!symbolImage->containsAddress(result)) {
+					symbolImage = dyld::findImageContainingAddress(result);
+				}
+				const macho_section *sect = symbolImage ? symbolImage->findSection(result) : NULL;
+				if ( sect && ((sect->flags & S_ATTR_PURE_INSTRUCTIONS) || (sect->flags & S_ATTR_SOME_INSTRUCTIONS)) )
+					result = __builtin_ptrauth_sign_unauthenticated(result, ptrauth_key_asia, 0);
+			}
+#endif
+			if ( dyld::gLogAPIs )
+				dyld::log("  %s(RTLD_DEFAULT, %s) ==> %p\n", __func__, symbolName, result);
+			return result;
 		}
 		const char* str = dyld::mkstringf("dlsym(RTLD_DEFAULT, %s): symbol not found", symbolName);
 		dlerrorSet(str);
 		free((void*)str);
+		CRSetCrashLogMessage(NULL);
+		if ( dyld::gLogAPIs )
+			dyld::log("  %s(RTLD_DEFAULT, %s) ==> NULL\n", __func__, symbolName);
 		return NULL;
 	}
 	
 	// magic "search only main executable" handle
-	if ( handle == RTLD_MAIN_ONLY ) {
+	else if ( handle == RTLD_MAIN_ONLY ) {
 		image = dyld::mainExecutable();
 		sym = image->findExportedSymbol(underscoredName, true, &image); // search RTLD_FIRST way
 		if ( sym != NULL ) {
-			return (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext);
+			CRSetCrashLogMessage(NULL);
+			result = (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext, NULL, false, underscoredName);
+#if __has_feature(ptrauth_calls)
+			// Sign the pointer if it points to a function
+			// Note we only do this if the main executable is arm64e as otherwise we
+			// may end up calling containsAddress on the accelerator tables.
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
+				const ImageLoader* symbolImage = image;
+				if (!symbolImage->containsAddress(result)) {
+					symbolImage = dyld::findImageContainingAddress(result);
+				}
+				const macho_section *sect = symbolImage ? symbolImage->findSection(result) : NULL;
+				if ( sect && ((sect->flags & S_ATTR_PURE_INSTRUCTIONS) || (sect->flags & S_ATTR_SOME_INSTRUCTIONS)) )
+					result = __builtin_ptrauth_sign_unauthenticated(result, ptrauth_key_asia, 0);
+			}
+#endif
+			if ( dyld::gLogAPIs )
+				dyld::log("  %s(RTLD_MAIN_ONLY, %s) ==> %p\n", __func__, symbolName, result);
+			return result;
 		}
 		const char* str = dyld::mkstringf("dlsym(RTLD_MAIN_ONLY, %s): symbol not found", symbolName);
 		dlerrorSet(str);
 		free((void*)str);
+		CRSetCrashLogMessage(NULL);
+		if ( dyld::gLogAPIs )
+			dyld::log("  %s(RTLD_MAIN_ONLY, %s) ==> NULL\n", __func__, symbolName);
 		return NULL;
 	}
 	
 	// magic "search what I would see" handle
-	if ( handle == RTLD_NEXT ) {
-		void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
+	else if ( handle == RTLD_NEXT ) {
+#if SUPPORT_ACCELERATE_TABLES
+		const mach_header* mh;
+		const char* path;
+		unsigned index;
+		if ( dyld::addressInCache(callerAddress, &mh, &path, &index) ) {
+			// if dylib in cache is calling dlsym(RTLD_NEXT,xxx) handle search differently
+			result = dyld::dlsymFromCache(RTLD_NEXT, underscoredName, index);
+			if ( dyld::gLogAPIs )
+				dyld::log("  %s(RTLD_NEXT, %s) ==> %p\n", __func__, symbolName, result);
+			return result;
+		}
+#endif
 		ImageLoader* callerImage = dyld::findImageContainingAddress(callerAddress);
 		sym = callerImage->findExportedSymbolInDependentImages(underscoredName, dyld::gLinkContext, &image); // don't search image, but do search what it links against
 		if ( sym != NULL ) {
-			return (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext);
+			CRSetCrashLogMessage(NULL);
+			result = (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext , callerImage, false, underscoredName);
+#if __has_feature(ptrauth_calls)
+			// Sign the pointer if it points to a function
+			// Note we only do this if the main executable is arm64e as otherwise we
+			// may end up calling containsAddress on the accelerator tables.
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
+				const ImageLoader* symbolImage = image;
+				if (!symbolImage->containsAddress(result)) {
+					symbolImage = dyld::findImageContainingAddress(result);
+				}
+				const macho_section *sect = symbolImage ? symbolImage->findSection(result) : NULL;
+				if ( sect && ((sect->flags & S_ATTR_PURE_INSTRUCTIONS) || (sect->flags & S_ATTR_SOME_INSTRUCTIONS)) )
+					result = __builtin_ptrauth_sign_unauthenticated(result, ptrauth_key_asia, 0);
+			}
+#endif
+			if ( dyld::gLogAPIs )
+				dyld::log("  %s(RTLD_NEXT, %s) ==> %p\n", __func__, symbolName, result);
+			return result;
 		}
 		const char* str = dyld::mkstringf("dlsym(RTLD_NEXT, %s): symbol not found", symbolName);
 		dlerrorSet(str);
 		free((void*)str);
+		CRSetCrashLogMessage(NULL);
+		if ( dyld::gLogAPIs )
+			dyld::log("  %s(RTLD_NEXT, %s) ==> NULL\n", __func__, symbolName);
 		return NULL;
 	}
 	// magic "search me, then what I would see" handle
-	if ( handle == RTLD_SELF ) {
-		void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
+	else if ( handle == RTLD_SELF ) {
+#if SUPPORT_ACCELERATE_TABLES
+		const mach_header* mh;
+		const char* path;
+		unsigned index;
+		if ( dyld::addressInCache(callerAddress, &mh, &path, &index) ) {
+			// if dylib in cache is calling dlsym(RTLD_SELF,xxx) handle search differently
+			result = dyld::dlsymFromCache(RTLD_SELF, underscoredName, index);
+			if ( dyld::gLogAPIs )
+				dyld::log("  %s(RTLD_SELF, %s) ==> %p\n", __func__, symbolName, result);
+			return result;
+		}
+#endif
 		ImageLoader* callerImage = dyld::findImageContainingAddress(callerAddress);
 		sym = callerImage->findExportedSymbolInImageOrDependentImages(underscoredName, dyld::gLinkContext, &image); // search image and what it links against
 		if ( sym != NULL ) {
-			return (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext);
+			CRSetCrashLogMessage(NULL);
+			result = (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext, callerImage, false, underscoredName);
+#if __has_feature(ptrauth_calls)
+			// Sign the pointer if it points to a function
+			// Note we only do this if the main executable is arm64e as otherwise we
+			// may end up calling containsAddress on the accelerator tables.
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
+				const ImageLoader* symbolImage = image;
+				if (!symbolImage->containsAddress(result)) {
+					symbolImage = dyld::findImageContainingAddress(result);
+				}
+				const macho_section *sect = symbolImage ? symbolImage->findSection(result) : NULL;
+				if ( sect && ((sect->flags & S_ATTR_PURE_INSTRUCTIONS) || (sect->flags & S_ATTR_SOME_INSTRUCTIONS)) )
+					result = __builtin_ptrauth_sign_unauthenticated(result, ptrauth_key_asia, 0);
+			}
+#endif
+			if ( dyld::gLogAPIs )
+				dyld::log("  %s(RTLD_SELF, %s) ==> %p\n", __func__, symbolName, result);
+			return result;
 		}
 		const char* str = dyld::mkstringf("dlsym(RTLD_SELF, %s): symbol not found", symbolName);
 		dlerrorSet(str);
 		free((void*)str);
+		CRSetCrashLogMessage(NULL);
+		if ( dyld::gLogAPIs )
+			dyld::log("  %s(RTLD_SELF, %s) ==> NULL\n", __func__, symbolName);
 		return NULL;
 	}
+#if SUPPORT_ACCELERATE_TABLES
+	// check for mega dylib handle
+	else if ( dyld::isCacheHandle(handle) ) {
+		result = dyld::dlsymFromCache(handle, underscoredName, 0);
+		if ( dyld::gLogAPIs )
+			dyld::log("  %s(%p, %s) ==> %p\n", __func__, handle, symbolName, result);
+		return result;
+	}
+#endif
 	// real handle
 	image = (ImageLoader*)(((uintptr_t)handle) & (-4));	// clear mode bits
 	if ( dyld::validImage(image) ) {
@@ -1622,7 +1986,30 @@
 			sym = image->findExportedSymbolInImageOrDependentImages(underscoredName, dyld::gLinkContext, &image); // search image and what it links against
 		
 		if ( sym != NULL ) {
-			return (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext);
+			CRSetCrashLogMessage(NULL);
+			ImageLoader* callerImage = NULL;
+			if ( sDynamicInterposing ) {
+				// only take time to look up caller, if dynamic interposing in use
+				callerImage = dyld::findImageContainingAddress(callerAddress);
+			}
+			result = (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext, callerImage, false, underscoredName);
+#if __has_feature(ptrauth_calls)
+			// Sign the pointer if it points to a function
+			// Note we only do this if the main executable is arm64e as otherwise we
+			// may end up calling containsAddress on the accelerator tables.
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
+				const ImageLoader* symbolImage = image;
+				if (!symbolImage->containsAddress(result)) {
+					symbolImage = dyld::findImageContainingAddress(result);
+				}
+				const macho_section *sect = symbolImage ? symbolImage->findSection(result) : NULL;
+				if ( sect && ((sect->flags & S_ATTR_PURE_INSTRUCTIONS) || (sect->flags & S_ATTR_SOME_INSTRUCTIONS)) )
+					result = __builtin_ptrauth_sign_unauthenticated(result, ptrauth_key_asia, 0);
+			}
+#endif
+			if ( dyld::gLogAPIs )
+				dyld::log("  %s(%p, %s) ==> %p\n", __func__, handle, symbolName, result);
+			return result;
 		}
 		const char* str = dyld::mkstringf("dlsym(%p, %s): symbol not found", handle, symbolName);
 		dlerrorSet(str);
@@ -1631,29 +2018,53 @@
 	else {
 		dlerrorSet("invalid handle passed to dlsym()");
 	}
+	CRSetCrashLogMessage(NULL);
+	if ( dyld::gLogAPIs )
+		dyld::log("  %s(%p, %s) ==> NULL\n", __func__, handle, symbolName);
 	return NULL;
 }
 
-
-
-
-
-
+// Note this is only here to support ___pthread_abort in libpthread.a
+void* dlsym(void* handle, const char* symbolName) {
+	return dlsym_internal(handle, symbolName, __builtin_return_address(1));
+}
+
+
+// <rdar://problem/40352925> *_compat functions are for old binaries that have __dyld section and use it to bypass libdyld.dylib
+void* dlopen_compat(const char* path, int mode)
+{
+	return dlopen_internal(path, mode, (void*)dyld::mainExecutable()->machHeader());
+}
+bool  dlopen_preflight_compat(const char* path)
+{
+	return dlopen_preflight_internal(path, (void*)dyld::mainExecutable()->machHeader());
+}
+void* dlsym_compat(void* handle, const char* symbolName)
+{
+	return dlsym_internal(handle, symbolName, (void*)dyld::mainExecutable()->machHeader());
+}
 
 
 
 
 const struct dyld_all_image_infos* _dyld_get_all_image_infos()
 {
-	return &dyld_all_image_infos;
-}
-
-#if !__arm__
+	return dyld::gProcessInfo;
+}
+
+
+#if SUPPORT_ZERO_COST_EXCEPTIONS
 static bool client_dyld_find_unwind_sections(void* addr, dyld_unwind_sections* info)
 {
 	//if ( dyld::gLogAPIs )
 	//	dyld::log("%s(%p, %p)\n", __func__, addr, info);
+
+	addr = stripPointer(addr);
 	
+#if SUPPORT_ACCELERATE_TABLES
+	if ( dyld::findUnwindSections(addr, info) )
+		return true;
+#endif
 	ImageLoader* image = dyld::findImageContainingAddress(addr);
 	if ( image != NULL ) {
 		image->getUnwindInfo(info);
@@ -1664,27 +2075,153 @@
 #endif
 
 
-void dyld_register_image_state_change_handler(dyld_image_states state, bool batch, 
-											dyld_image_state_change_handler handler)
-{
-	if ( dyld::gLogAPIs )
-		dyld::log("%s(%d, %d, %p)\n", __func__, state, batch, handler);
-	if ( batch )
-		dyld::registerImageStateBatchChangeHandler(state, handler);
-	else
-		dyld::registerImageStateSingleChangeHandler(state, handler);
-}
-
-
 const char* dyld_image_path_containing_address(const void* address)
 {
 	if ( dyld::gLogAPIs )
 		dyld::log("%s(%p)\n", __func__, address);
+
+    address = (void*)stripPointer(address);
+    
+#if SUPPORT_ACCELERATE_TABLES
+	const mach_header* mh;
+	const char* path;
+	if ( dyld::addressInCache(address, &mh, &path) )
+		return path;
+#endif
 
 	ImageLoader* image = dyld::findImageContainingAddress(address);
 	if ( image != NULL )
-		return image->getPath();
+		return image->getRealPath();
 	return NULL;
 }
 
 
+
+bool dyld_shared_cache_some_image_overridden()
+{
+	return dyld::gSharedCacheOverridden;
+}
+
+
+void dyld_dynamic_interpose(const struct mach_header* mh, const struct dyld_interpose_tuple array[], size_t count)
+{
+	if ( mh == NULL )
+		return;
+	if ( array == NULL )
+		return;
+	if ( count == 0 )
+		return;
+	ImageLoader* image = dyld::findImageByMachHeader(mh);
+	if ( image == NULL )
+		return;
+	
+	// make pass at bound references in this image and update them
+	dyld::gLinkContext.dynamicInterposeArray = array;
+	dyld::gLinkContext.dynamicInterposeCount = count;
+		image->dynamicInterpose(dyld::gLinkContext);
+	dyld::gLinkContext.dynamicInterposeArray = NULL;
+	dyld::gLinkContext.dynamicInterposeCount = 0;
+	
+	// leave interposing info so any future (lazy) binding will get it too
+	image->addDynamicInterposingTuples(array, count);
+	
+	sDynamicInterposing = true;
+}
+
+
+bool _dyld_is_memory_immutable(const void* addr, size_t length)
+{
+	if ( dyld::gLogAPIs )
+		dyld::log("%s(%p, %ld)\n", __func__, addr, length);
+
+	uintptr_t checkStart = (uintptr_t)addr;
+	uintptr_t checkEnd   = checkStart + length;
+
+	// quick check to see if in r/o region of shared cache.  If so return true.
+    const DyldSharedCache* cache = (DyldSharedCache*)dyld::imMemorySharedCacheHeader();
+    if ( cache != nullptr ) {
+        const dyld_cache_mapping_info* const mappings = (dyld_cache_mapping_info*)((char*)cache + cache->header.mappingOffset);
+		uintptr_t roStart    = (uintptr_t)cache;
+		uintptr_t roEnd      = roStart + (uintptr_t)mappings[0].size;
+		if ( (roStart < checkStart) && (checkEnd < roEnd) )
+			return true;
+    }
+
+	// Otherwise find if addr is in a dyld loaded image
+	ImageLoader* image = dyld::findImageContainingAddress(addr);
+	if ( image != NULL ) {
+		// <rdar://problem/24091154> already checked for r/o portion of cache
+		if ( image->inSharedCache() )
+			return false;
+		if ( !image->neverUnload() )
+			return false;
+		for (unsigned i=0, e=image->segmentCount(); i < e; ++i) {
+			if ( (image->segActualLoadAddress(i) < checkStart) && (checkEnd < image->segActualEndAddress(i)) ) {
+				return !image->segWriteable(i);
+			}
+		}
+	}
+	return false;
+}
+
+
+
+void _dyld_objc_notify_register(_dyld_objc_notify_mapped    mapped,
+                                _dyld_objc_notify_init      init,
+                                _dyld_objc_notify_unmapped  unmapped)
+{
+	dyld::registerObjCNotifiers(mapped, init, unmapped);
+}
+
+
+bool _dyld_get_shared_cache_uuid(uuid_t uuid)
+{
+	return dyld::sharedCacheUUID(uuid);
+}
+
+const void* _dyld_get_shared_cache_range(size_t* length)
+{
+    const DyldSharedCache* cache = (DyldSharedCache*)dyld::imMemorySharedCacheHeader();
+    if ( cache != nullptr ) {
+        const dyld_cache_mapping_info* const mappings = (dyld_cache_mapping_info*)((char*)cache + cache->header.mappingOffset);
+        *length = (size_t)((mappings[2].address + mappings[2].size) - mappings[0].address);
+        return cache;
+    }
+	return nullptr;
+}
+
+void _dyld_images_for_addresses(unsigned count, const void* addresses[], struct dyld_image_uuid_offset infos[])
+{
+	for (unsigned i=0; i < count; ++i) {
+        const void* addr = addresses[i];
+		addr = stripPointer(addr);
+        bzero(&infos[i], sizeof(dyld_image_uuid_offset));
+#if SUPPORT_ACCELERATE_TABLES
+		const mach_header* 	mh;
+		const char* 		path;
+		if ( dyld::addressInCache(addr, &mh, &path) ) {
+			infos[i].image         = mh;
+			infos[i].offsetInImage = (uintptr_t)addr - (uintptr_t)mh;
+			((dyld3::MachOFile*)mh)->getUuid(infos[i].uuid);
+			break;
+		}
+#endif
+		ImageLoader* image = dyld::findImageContainingAddress(addr);
+        if ( image != nullptr ) {
+            infos[i].image         = image->machHeader();
+            infos[i].offsetInImage = (uintptr_t)addr - (uintptr_t)(image->machHeader());
+            image->getUUID(infos[i].uuid);
+        }
+    }
+}
+
+void _dyld_register_for_image_loads(void (*func)(const mach_header* mh, const char* path, bool unloadable))
+{
+	if ( dyld::gLogAPIs )
+		dyld::log("%s(%p)\n", __func__, (void *)func);
+	dyld::registerLoadCallback(func);
+}
+
+
+
+