Loading...
src/ImageLoaderMegaDylib.cpp dyld-519.2.2 dyld-421.2
--- dyld/dyld-519.2.2/src/ImageLoaderMegaDylib.cpp
+++ dyld/dyld-421.2/src/ImageLoaderMegaDylib.cpp
@@ -45,13 +45,10 @@
 #include "ImageLoaderMachO.h"
 #include "mach-o/dyld_images.h"
 #include "dyldLibSystemInterface.h"
-#include "Tracing.h"
 #include "dyld.h"
 
 // from dyld_gdb.cpp 
 extern void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[]);
-
-extern "C" int _dyld_func_lookup(const char* name, void** address);
 
 
 #ifndef EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
@@ -80,9 +77,9 @@
 #if SUPPORT_ACCELERATE_TABLES
  
 
-ImageLoaderMegaDylib* ImageLoaderMegaDylib::makeImageLoaderMegaDylib(const dyld_cache_header* header, long slide, const macho_header* mainMH, const LinkContext& context)
-{
-	return new ImageLoaderMegaDylib(header, slide, mainMH, context);
+ImageLoaderMegaDylib* ImageLoaderMegaDylib::makeImageLoaderMegaDylib(const dyld_cache_header* header, long slide, const LinkContext& context)
+{
+	return new ImageLoaderMegaDylib(header, slide, context);
 }
 
 struct DATAdyld {
@@ -94,7 +91,7 @@
 
 
 
-ImageLoaderMegaDylib::ImageLoaderMegaDylib(const dyld_cache_header* header, long slide, const macho_header* mainMH, const LinkContext& context)
+ImageLoaderMegaDylib::ImageLoaderMegaDylib(const dyld_cache_header* header, long slide, const LinkContext& context)
 	: ImageLoader("dyld shared cache", 0), _header(header), _linkEditBias(NULL), _slide(slide),
 	 _lockArray(NULL), _lockArrayInUseCount(0)
 {
@@ -126,7 +123,7 @@
 	DATAdyld* dyldSection = (DATAdyld*)(accHeader->dyldSectionAddr + slide);
 	dyldSection->dyldLazyBinder = NULL; // not used by libdyld.dylib
 	dyldSection->dyldFuncLookup = (void*)&_dyld_func_lookup;
-	dyldSection->vars.mh = mainMH;
+	dyldSection->vars.mh = context.mainExecutable->machHeader();
 	context.setNewProgramVars(dyldSection->vars);
 }
 
@@ -272,7 +269,6 @@
 	if ( strncmp(path, "@rpath/", 7) == 0 ) {
 		std::vector<const char*> rpathsFromMainExecutable;
 		context.mainExecutable->getRPaths(context, rpathsFromMainExecutable);
-		rpathsFromMainExecutable.push_back("/System/Library/Frameworks/");
 		const char* trailingPath = &path[7];
 		for (const char* anRPath : rpathsFromMainExecutable) {
 			if ( anRPath[0] != '/' )
@@ -287,18 +283,6 @@
 			}
 		}
 	}
-    else {
-        // handle symlinks embedded in load commands
-        char resolvedPath[PATH_MAX];
-        realpath(path, resolvedPath);
-        int realpathErrno = errno;
-        // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
-        if ( (realpathErrno == ENOENT) || (realpathErrno == 0) ) {
-            if ( strcmp(resolvedPath, path) != 0 )
-                return findImageIndex(context, resolvedPath);
-        }
-    }
-
 	dyld::throwf("no cache image with name (%s)", path);
 }
 
@@ -843,7 +827,7 @@
 
 
 void ImageLoaderMegaDylib::recursiveInitialization(const LinkContext& context, mach_port_t thisThread, unsigned int imageIndex,
-													InitializerTimingList& timingInfo, UpwardIndexes& upInits)
+													InitializerTimingList& timingInfo)
 {
 	// Don't do any locking until libSystem.dylib is initialized, so we can malloc() the lock array
 	bool useLock = dyld::gProcessInfo->libSystemInitialized;
@@ -862,9 +846,7 @@
 			unsigned subDepIndex = _dependenciesArray[i];
 			// ignore upward links
 			if ( (subDepIndex & 0x8000) == 0 )
-				recursiveInitialization(context, thisThread, subDepIndex, timingInfo, upInits);
-            else
-                upInits.images[upInits.count++] = (subDepIndex & 0x7FFF);
+				recursiveInitialization(context, thisThread, subDepIndex, timingInfo);
 		}
 
 		// notify objc about this image
@@ -881,9 +863,7 @@
 				if ( context.verboseInit )
 					dyld::log("dyld: calling initializer function %p in %s\n", func, getIndexedPath(imageIndex));
 				bool haveLibSystemHelpersBefore = (dyld::gLibSystemHelpers != NULL);
-				dyld3::kdebug_trace_dyld_duration(DBG_DYLD_TIMING_STATIC_INITIALIZER, (uint64_t)func, 0, ^{
-					func(context.argc, context.argv, context.envp, context.apple, &context.programVars);
-				});
+				func(context.argc, context.argv, context.envp, context.apple, &context.programVars);
 				bool haveLibSystemHelpersAfter = (dyld::gLibSystemHelpers != NULL);
 				ranSomeInitializers = true;
 				if ( !haveLibSystemHelpersBefore && haveLibSystemHelpersAfter ) {
@@ -912,20 +892,11 @@
 
 
 void ImageLoaderMegaDylib::recursiveInitialization(const LinkContext& context, mach_port_t thisThread, const char* pathToInitialize,
-													InitializerTimingList& timingInfo, UninitedUpwards&)
+													InitializerTimingList& timingInfo, UninitedUpwards& uninitUps)
 {
 	unsigned imageIndex;
 	if ( hasDylib(pathToInitialize, &imageIndex) ) {
-		UpwardIndexes upsBuffer[256];
-		UpwardIndexes& ups = upsBuffer[0];
-		ups.count = 0;
-		this->recursiveInitialization(context, thisThread, imageIndex, timingInfo, ups);
-		for (int i=0; i < ups.count; ++i) {
-			UpwardIndexes upsBuffer2[256];
-			UpwardIndexes& ignoreUp = upsBuffer2[0];
-			ignoreUp.count = 0;
-			this->recursiveInitialization(context, thisThread, ups.images[i], timingInfo, ignoreUp);
-		}
+		this->recursiveInitialization(context, thisThread, imageIndex, timingInfo);
 	}
 }
 
@@ -964,8 +935,9 @@
 	uint8_t targetCacheState = dyldStateToCacheState(state);
 	if ( targetCacheState == kStateUnused )
 		return 0;
+
 	unsigned usedCount = 0;
-	for (int i=_imageCount-1; i >= 0; --i) {
+	for (int i=_imageCount-1; i > 0; --i) {
 		uint16_t index = _bottomUpArray[i];
 		uint8_t imageState = _stateFlags[index];
 		if ( imageState == kStateFlagWeakBound )
@@ -1010,17 +982,7 @@
 		InitializerTimingList timingInfo[_initializerCount];
 		timingInfo[0].count = 0;
 		mach_port_t thisThread  = mach_thread_self();
-		UpwardIndexes upsBuffer[256];  // room for 511 dangling upward links
-		UpwardIndexes& ups = upsBuffer[0];
-		ups.count = 0;
-		this->recursiveInitialization(context, thisThread, imageIndex, timingInfo[0], ups);
-		// make sure any upward linked dylibs were initialized
-		for (int i=0; i < ups.count; ++i) {
-			UpwardIndexes upsBuffer2[256];
-			UpwardIndexes& ignoreUp = upsBuffer2[0];
-			ignoreUp.count = 0;
-			this->recursiveInitialization(context, thisThread, ups.images[i], timingInfo[0], ignoreUp);
-		}
+		this->recursiveInitialization(context, thisThread, imageIndex, timingInfo[0]);
 		mach_port_deallocate(mach_task_self(), thisThread);
 		context.notifyBatch(dyld_image_state_initialized, false);
 	}