+static void* mapStartOfCache(const char* path, size_t length)
+{
+ struct stat statbuf;
+ if ( ::stat(path, &statbuf) == -1 )
+ return NULL;
+
+ if ( (size_t)statbuf.st_size < length )
+ return NULL;
+
+ int cache_fd = ::open(path, O_RDONLY);
+ if ( cache_fd < 0 )
+ return NULL;
+
+ void* result = ::mmap(NULL, length, PROT_READ, MAP_PRIVATE, cache_fd, 0);
+ close(cache_fd);
+
+ if ( result == MAP_FAILED )
+ return NULL;
+
+ return result;
+}
+
+
+static const dyld_cache_header* findCacheInDirAndMap(const uuid_t cacheUuid, const char* dirPath)
+{
+ DIR* dirp = ::opendir(dirPath);
+ if ( dirp != NULL) {
+ dirent entry;
+ dirent* entp = NULL;
+ char cachePath[PATH_MAX];
+ while ( ::readdir_r(dirp, &entry, &entp) == 0 ) {
+ if ( entp == NULL )
+ break;
+ if ( entp->d_type != DT_REG )
+ continue;
+ if ( strlcpy(cachePath, dirPath, PATH_MAX) >= PATH_MAX )
+ continue;
+ if ( strlcat(cachePath, "/", PATH_MAX) >= PATH_MAX )
+ continue;
+ if ( strlcat(cachePath, entp->d_name, PATH_MAX) >= PATH_MAX )
+ continue;
+ if ( const dyld_cache_header* cacheHeader = (dyld_cache_header*)mapStartOfCache(cachePath, 0x00100000) ) {
+ if ( ::memcmp(cacheHeader->uuid, cacheUuid, 16) != 0 ) {
+ // wrong uuid, unmap and keep looking
+ ::munmap((void*)cacheHeader, 0x00100000);
+ }
+ else {
+ // found cache
+ closedir(dirp);
+ return cacheHeader;
+ }
+ }
+ }
+ closedir(dirp);
+ }
+ return NULL;
+}
+
+int dyld_shared_cache_find_iterate_text(const uuid_t cacheUuid, const char* extraSearchDirs[], void (^callback)(const dyld_shared_cache_dylib_text_info* info))
+{
+ if ( gUseDyld3 )
+ return dyld3::dyld_shared_cache_find_iterate_text(cacheUuid, extraSearchDirs, callback);
+
+ const dyld_cache_header* cacheHeader = NULL;
+ bool needToUnmap = true;
+
+ // get info from dyld about this process, to see if requested cache is already mapped into this process
+ const dyld_all_image_infos* allInfo = _dyld_get_all_image_infos();
+ if ( (allInfo != NULL) && (allInfo->sharedCacheBaseAddress != 0) && (memcmp(allInfo->sharedCacheUUID, cacheUuid, 16) == 0) ) {
+ // requested cache is already mapped, just re-use it
+ cacheHeader = (dyld_cache_header*)(allInfo->sharedCacheBaseAddress);
+ needToUnmap = false;
+ }
+ else {
+ // look first is default location for cache files
+ #if __IPHONE_OS_VERSION_MIN_REQUIRED
+ const char* defaultSearchDir = IPHONE_DYLD_SHARED_CACHE_DIR;
+ #else
+ const char* defaultSearchDir = MACOSX_DYLD_SHARED_CACHE_DIR;
+ #endif
+ cacheHeader = findCacheInDirAndMap(cacheUuid, defaultSearchDir);
+ // if not there, look in extra search locations
+ if ( cacheHeader == NULL ) {
+ for (const char** p = extraSearchDirs; *p != NULL; ++p) {
+ cacheHeader = findCacheInDirAndMap(cacheUuid, *p);
+ if ( cacheHeader != NULL )
+ break;
+ }
+ }
+ }
+
+ if ( cacheHeader == NULL )
+ return -1;
+
+ if ( cacheHeader->mappingOffset < sizeof(dyld_cache_header) ) {
+ // old cache without imagesText array
+ if ( needToUnmap )
+ ::munmap((void*)cacheHeader, 0x00100000);
+ return -1;
+ }
+
+ // walk imageText table and call callback for each entry
+ const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)cacheHeader + cacheHeader->mappingOffset);
+ const uint64_t cacheUnslidBaseAddress = mappings[0].address;
+ const dyld_cache_image_text_info* imagesText = (dyld_cache_image_text_info*)((char*)cacheHeader + cacheHeader->imagesTextOffset);
+ const dyld_cache_image_text_info* imagesTextEnd = &imagesText[cacheHeader->imagesTextCount];
+ for (const dyld_cache_image_text_info* p=imagesText; p < imagesTextEnd; ++p) {
+ dyld_shared_cache_dylib_text_info dylibTextInfo;
+ dylibTextInfo.version = 2;
+ dylibTextInfo.loadAddressUnslid = p->loadAddress;
+ dylibTextInfo.textSegmentSize = p->textSegmentSize;
+ dylibTextInfo.path = (char*)cacheHeader + p->pathOffset;
+ ::memcpy(dylibTextInfo.dylibUuid, p->uuid, 16);
+ dylibTextInfo.textSegmentOffset = p->loadAddress - cacheUnslidBaseAddress;
+ callback(&dylibTextInfo);
+ }
+
+ if ( needToUnmap )
+ ::munmap((void*)cacheHeader, 0x00100000);
+
+ return 0;
+}
+
+int dyld_shared_cache_iterate_text(const uuid_t cacheUuid, void (^callback)(const dyld_shared_cache_dylib_text_info* info))
+{
+ if ( gUseDyld3 )
+ return dyld3::dyld_shared_cache_iterate_text(cacheUuid, callback);
+
+ const char* extraSearchDirs[] = { NULL };
+ return dyld_shared_cache_find_iterate_text(cacheUuid, extraSearchDirs, callback);
+}
+
+
+bool _dyld_is_memory_immutable(const void* addr, size_t length)
+{
+ if ( gUseDyld3 )
+ return dyld3::_dyld_is_memory_immutable(addr, length);
+
+ DYLD_NO_LOCK_THIS_BLOCK;
+ static bool (*p)(const void*, size_t) = NULL;
+
+ if(p == NULL)
+ _dyld_func_lookup("__dyld_is_memory_immutable", (void**)&p);
+ return p(addr, length);
+}
+
+
+void _dyld_objc_notify_register(_dyld_objc_notify_mapped mapped,
+ _dyld_objc_notify_init init,
+ _dyld_objc_notify_unmapped unmapped)
+{
+ if ( gUseDyld3 )
+ return dyld3::_dyld_objc_notify_register(mapped, init, unmapped);
+
+ DYLD_LOCK_THIS_BLOCK;
+ static bool (*p)(_dyld_objc_notify_mapped, _dyld_objc_notify_init, _dyld_objc_notify_unmapped) = NULL;
+
+ if(p == NULL)
+ _dyld_func_lookup("__dyld_objc_notify_register", (void**)&p);
+ p(mapped, init, unmapped);
+}
+
+