gProcessInfo的声明:
struct dyld_all_image_infos* gProcessInfo = &dyld_all_image_infos;
struct dyld_all_image_infos { uint32_t version; /* 1 in Mac OS X 10.4 and 10.5 */ uint32_t infoArrayCount;#if defined(__cplusplus) && (BUILDING_LIBDYLD || BUILDING_DYLD) std::atomic<const struct dyld_image_info*> infoArray;#else const struct dyld_image_info* infoArray;#endif dyld_image_notifier notification; bool processDetachedFromSharedRegion; /* the following fields are only in version 2 (Mac OS X 10.6, iPhoneOS 2.0) and later */ bool libSystemInitialized; const struct mach_header* dyldImageLoadAddress; /* the following field is only in version 3 (Mac OS X 10.6, iPhoneOS 3.0) and later */ void* jitInfo; /* the following fields are only in version 5 (Mac OS X 10.6, iPhoneOS 3.0) and later */ const char* dyldVersion; const char* errorMessage; uintptr_t terminationFlags; /* the following field is only in version 6 (Mac OS X 10.6, iPhoneOS 3.1) and later */ void* coreSymbolicationShmPage; /* the following field is only in version 7 (Mac OS X 10.6, iPhoneOS 3.1) and later */ uintptr_t systemOrderFlag; /* the following field is only in version 8 (Mac OS X 10.7, iPhoneOS 3.1) and later */ uintptr_t uuidArrayCount; const struct dyld_uuid_info* uuidArray; /* only images not in dyld shared cache */ /* the following field is only in version 9 (Mac OS X 10.7, iOS 4.0) and later */ struct dyld_all_image_infos* dyldAllImageInfosAddress; /* the following field is only in version 10 (Mac OS X 10.7, iOS 4.2) and later */ uintptr_t initialImageCount; /* the following field is only in version 11 (Mac OS X 10.7, iOS 4.2) and later */ uintptr_t errorKind; const char* errorClientOfDylibPath; const char* errorTargetDylibPath; const char* errorSymbol; /* the following field is only in version 12 (Mac OS X 10.7, iOS 4.3) and later */ uintptr_t sharedCacheSlide; /* the following field is only in version 13 (Mac OS X 10.9, iOS 7.0) and later */ uint8_t sharedCacheUUID[16]; /* the following field is only in version 15 (macOS 10.12, iOS 10.0) and later */ uintptr_t sharedCacheBaseAddress;#if defined(__cplusplus) && (BUILDING_LIBDYLD || BUILDING_DYLD) // We want this to be atomic in libdyld so that we can see updates when we map it shared std::atomic<uint64_t> infoArrayChangeTimestamp;#else uint64_t infoArrayChangeTimestamp;#endif const char* dyldPath; mach_port_t notifyPorts[DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT];#if __LP64__ uintptr_t reserved[11-(DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT/2)];#else uintptr_t reserved[9-DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT];#endif // The following fields were added in version 18 (previously they were reserved padding fields) uint64_t sharedCacheFSID; uint64_t sharedCacheFSObjID; /* the following field is only in version 16 (macOS 10.13, iOS 11.0) and later */ uintptr_t compact_dyld_image_info_addr; size_t compact_dyld_image_info_size; uint32_t platform; // FIXME: really a dyld_platform_t, but those aren't exposed here. /* the following field is only in version 17 (macOS 10.16) and later */ uint32_t aotInfoCount; const struct dyld_aot_image_info* aotInfoArray; uint64_t aotInfoArrayChangeTimestamp; uintptr_t aotSharedCacheBaseAddress; uint8_t aotSharedCacheUUID[16];};
// 这是从dyldMain.cpp中提取出来的,以支持利用crt1.o的旧macOS应用步伐void APIs::runAllInitializersForMain(){ // 起首运行libSystem的初始化器 const_cast<Loader*>(this->libSystemLoader)->beginInitializers(*this); this->libSystemLoader->runInitializers(*this); gProcessInfo->libSystemInitialized = true; // 在运行libSystem的初始化器后,告诉objc在libSystem的子dylibs上运行任何+load方法 this->notifyObjCInit(this->libSystemLoader); // <rdar://problem/32209809> 调用'init'函数对全部已经init'ed的图像 (below libSystem) // 利用下标举行迭代,以便在+加载dloopen时数组不会在我们下面增长 for ( uint32_t i = 0; i != this->loaded.size(); ++i ) { const Loader* ldr = this->loaded; if ( ldr->analyzer(*this)->isDylib() && (strncmp(ldr->analyzer(*this)->installName(), "/usr/lib/system/lib", 19) == 0) ) { // 查抄安装名称而不是路径,以处置惩罚libsystem子dylibs的DYLD_LIBRARY_PATH覆盖 const_cast<Loader*>(ldr)->beginInitializers(*this); this->notifyObjCInit(ldr); } } // 自底向上运行全部其他初始化器,起首运行插入的dylib初始化器 // 利用下标举行迭代,以便在初始化式dloopen时数组不会在下面增长 for ( uint32_t i = 0; i != this->loaded.size(); ++i ) { const Loader* ldr = this->loaded; ldr->runInitializersBottomUpPlusUpwardLinks(*this); // stop as soon as we did main executable // normally this is first image, but if there are inserted N dylibs, it is Nth in the list if ( ldr->analyzer(*this)->isMainExecutable() ) break; }}