Loading...
src/dyldAPIs.cpp dyld-852.2 dyld-640.2
--- dyld/dyld-852.2/src/dyldAPIs.cpp
+++ dyld/dyld-640.2/src/dyldAPIs.cpp
@@ -45,24 +45,28 @@
 #include <sys/sysctl.h>
 #include <mach/mach_traps.h> // for task_self_trap()
 
-#include <mach-o/dyld_images.h>
-#include <mach-o/dyld.h>
-#include <mach-o/dyld_priv.h>
+
+#include "mach-o/dyld_images.h"
+#include "mach-o/dyld.h"
+#include "mach-o/dyld_priv.h"
 
 #include "ImageLoader.h"
 #include "ImageLoaderMachO.h"
-#include "dyld2.h"
+#include "dyld.h"
 #include "dyldLibSystemInterface.h"
 #include "DyldSharedCache.h"
 #include "MachOFile.h"
 
 #undef _POSIX_C_SOURCE
-#include <dlfcn.h>
+#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__
@@ -109,10 +113,10 @@
 
 
 // deprecated APIs are still availble on Mac OS X, but not on iPhone OS
-#if TARGET_OS_OSX
+#if __IPHONE_OS_VERSION_MIN_REQUIRED	
+	#define DEPRECATED_APIS_SUPPORTED 0
+#else
 	#define DEPRECATED_APIS_SUPPORTED 1
-#else
-	#define DEPRECATED_APIS_SUPPORTED 0
 #endif
 
 static bool sDynamicInterposing = false;
@@ -162,7 +166,7 @@
     void*		implementation;
 };
 
-static const struct dyld_func dyld_funcs[] = {
+static struct dyld_func dyld_funcs[] = {
     {"__dyld_register_func_for_add_image",				(void*)_dyld_register_func_for_add_image },
     {"__dyld_register_func_for_remove_image",			(void*)_dyld_register_func_for_remove_image },
     {"__dyld_dladdr",									(void*)dladdr },
@@ -179,7 +183,6 @@
     {"__dyld_get_image_vmaddr_slide",					(void*)_dyld_get_image_vmaddr_slide },
     {"__dyld_get_image_name",							(void*)_dyld_get_image_name },
     {"__dyld_get_image_slide",							(void*)_dyld_get_image_slide },
-    {"__dyld_get_prog_image_header",					(void*)_dyld_get_prog_image_header },
     {"__dyld__NSGetExecutablePath",						(void*)_NSGetExecutablePath },
 
 	// SPIs
@@ -198,7 +201,6 @@
 	{"__dyld_process_is_restricted",					(void*)dyld::processIsRestricted },
 	{"__dyld_dynamic_interpose",						(void*)dyld_dynamic_interpose },
 	{"__dyld_shared_cache_file_path",					(void*)dyld::getStandardSharedCacheFilePath },
-	{"__dyld_has_inserted_or_interposing_libraries",	(void*)dyld::hasInsertedOrInterposingLibraries },
     {"__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 },
@@ -206,13 +208,9 @@
     {"__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 },
-    {"__dyld_register_for_bulk_image_loads",			(void*)_dyld_register_for_bulk_image_loads },
-    {"__dyld_register_driverkit_main",					(void*)_dyld_register_driverkit_main },
-    {"__dyld_halt",										(void*)dyld::halt },
-
+
+	// deprecated
 #if DEPRECATED_APIS_SUPPORTED
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
     {"__dyld_lookup_and_bind",						(void*)client_dyld_lookup_and_bind },
     {"__dyld_lookup_and_bind_with_hint",			(void*)_dyld_lookup_and_bind_with_hint },
     {"__dyld_lookup_and_bind_fully",				(void*)_dyld_lookup_and_bind_fully },
@@ -254,7 +252,6 @@
 #if OLD_LIBSYSTEM_SUPPORT
     {"__dyld_link_module",							(void*)_dyld_link_module },
 #endif
-#pragma clang diagnostic pop
 #endif //DEPRECATED_APIS_SUPPORTED
 
     {NULL, 0}
@@ -391,14 +388,6 @@
 	return allImagesIndexedPath(image_index);
 }
 
-const struct mach_header* _dyld_get_prog_image_header()
-{
-	if ( dyld::gLogAPIs )
-		dyld::log("%s()\n", __func__);
-	
-	return dyld::mainExecutable()->machHeader();
-}
-
 static const void *stripPointer(const void *ptr) {
 #if __has_feature(ptrauth_calls)
 	return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
@@ -427,7 +416,7 @@
 		return mh;
 #endif
 	ImageLoader* image = dyld::findImageContainingAddress(address);
-	if ( image != NULL )
+	if ( image != NULL ) 
 		return image->machHeader();
 	return NULL;
 }
@@ -619,6 +608,7 @@
 		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
 
@@ -784,19 +774,6 @@
 	ImageLoader* image = dyld::findImageContainingSymbol(symbol);
 	if ( image != NULL ) 
 		result = (void*)image->getExportedSymbolAddress(NSSymbolToSymbol(symbol), dyld::gLinkContext);
-
-#if __has_feature(ptrauth_calls)
-	// Sign the pointer if it points to a function
-	if ( result ) {
-		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
 	return result;
 }
 
@@ -876,6 +853,7 @@
 		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
 
@@ -1393,7 +1371,7 @@
 
 	const bool leafName = (strchr(path, '/') == NULL);
 	const bool absolutePath = (path[0] == '/');
-#if TARGET_OS_IPHONE
+#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 ) {
@@ -1423,18 +1401,7 @@
 	// if requested path is to something in the dyld shared cache, always succeed
 	if ( dyld::inSharedCache(path) )
 		return true;
-
-#if TARGET_OS_OSX
-	// <rdar://problem/47464387> dlopen_preflight() on symlink to image in shared cache leaves it half loaded
-	if ( strncmp(path, "/System/Library/", 16) == 0 ) {
-		char canonicalPath[PATH_MAX];
-		if ( realpath(path, canonicalPath) ) {
-			if ( dyld::inSharedCache(canonicalPath) )
-				return true;
-		}
-	}
-#endif
-
+	
 	bool result = false;
 	std::vector<const char*> rpathsFromCallerImage;
 	try {
@@ -1458,6 +1425,7 @@
 		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
 
@@ -1539,7 +1507,7 @@
 	void* result = NULL;
 	const bool leafName = (strchr(path, '/') == NULL);
 	const bool absolutePath = (path[0] == '/');
-#if TARGET_OS_IPHONE
+#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 ) {
@@ -1592,6 +1560,7 @@
 		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
 
@@ -1852,7 +1821,7 @@
 			// 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_MASK) == CPU_SUBTYPE_ARM64E) ) {
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
 				const ImageLoader* symbolImage = image;
 				if (!symbolImage->containsAddress(result)) {
 					symbolImage = dyld::findImageContainingAddress(result);
@@ -1886,7 +1855,7 @@
 			// 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_MASK) == CPU_SUBTYPE_ARM64E) ) {
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
 				const ImageLoader* symbolImage = image;
 				if (!symbolImage->containsAddress(result)) {
 					symbolImage = dyld::findImageContainingAddress(result);
@@ -1932,7 +1901,7 @@
 			// 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_MASK) == CPU_SUBTYPE_ARM64E) ) {
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
 				const ImageLoader* symbolImage = image;
 				if (!symbolImage->containsAddress(result)) {
 					symbolImage = dyld::findImageContainingAddress(result);
@@ -1977,7 +1946,7 @@
 			// 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_MASK) == CPU_SUBTYPE_ARM64E) ) {
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
 				const ImageLoader* symbolImage = image;
 				if (!symbolImage->containsAddress(result)) {
 					symbolImage = dyld::findImageContainingAddress(result);
@@ -2028,7 +1997,7 @@
 			// 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_MASK) == CPU_SUBTYPE_ARM64E) ) {
+			if ( result && (dyld::gLinkContext.mainExecutable->machHeader()->cpusubtype == CPU_SUBTYPE_ARM64_E) ) {
 				const ImageLoader* symbolImage = image;
 				if (!symbolImage->containsAddress(result)) {
 					symbolImage = dyld::findImageContainingAddress(result);
@@ -2121,9 +2090,8 @@
 #endif
 
 	ImageLoader* image = dyld::findImageContainingAddress(address);
-	if ( image != NULL ) {
+	if ( image != NULL )
 		return image->getRealPath();
-	}
 	return NULL;
 }
 
@@ -2146,9 +2114,6 @@
 	ImageLoader* image = dyld::findImageByMachHeader(mh);
 	if ( image == NULL )
 		return;
-
-	// make the cache writable for this block
-	DyldSharedCache::DataConstScopedWriter patcher(dyld::gLinkContext.dyldCache, mach_task_self(), (dyld::gLinkContext.verboseMapping ? &dyld::log : nullptr));
 	
 	// make pass at bound references in this image and update them
 	dyld::gLinkContext.dynamicInterposeArray = array;
@@ -2219,8 +2184,7 @@
     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);
-		const dyld_cache_mapping_info* lastMapping = &mappings[cache->header.mappingCount - 1];
-        *length = (size_t)((lastMapping->address + lastMapping->size) - cache->unslidLoadAddress());
+        *length = (size_t)((mappings[2].address + mappings[2].size) - mappings[0].address);
         return cache;
     }
 	return nullptr;
@@ -2239,7 +2203,7 @@
 			infos[i].image         = mh;
 			infos[i].offsetInImage = (uintptr_t)addr - (uintptr_t)mh;
 			((dyld3::MachOFile*)mh)->getUuid(infos[i].uuid);
-			continue;
+			break;
 		}
 #endif
 		ImageLoader* image = dyld::findImageContainingAddress(addr);
@@ -2258,16 +2222,6 @@
 	dyld::registerLoadCallback(func);
 }
 
-void _dyld_register_for_bulk_image_loads(void (*func)(unsigned imageCount, const struct mach_header* mhs[], const char* paths[]))
-{
-	if ( dyld::gLogAPIs )
-		dyld::log("%s(%p)\n", __func__, (void *)func);
-	dyld::registerBulkLoadCallback(func);
-}
-
-void _dyld_register_driverkit_main(void (*mainFunc)())
-{
-	dyld::setMainEntry(mainFunc);
-}
-
-
+
+
+