AppleOpenSource/dyld-750.6/launch-cache/dyld_shared_cache_util.cpp
// mmap() an shared cache file read/only but laid out like it would be at runtime
mmap() an shared cache file read/only but laid out like it would be at runtime
mmap () an shared cache file read / only but laid out like it would be at runtime
static const DyldSharedCache* mapCacheFile(const char* path)
{
    struct stat statbuf;
    if ( ::stat(path, &statbuf) ) {
        fprintf(stderr, "Error: stat failed for dyld shared cache at %s\n", path);
        return nullptr;
    }
    int cache_fd = ::open(path, O_RDONLY);
    if (cache_fd < 0) {
        fprintf(stderr, "Error: failed to open shared cache file at %s\n", path);
        return nullptr;
    }
    uint8_t  firstPage[4096];
    if ( ::pread(cache_fd, firstPage, 4096, 0) != 4096 ) {
        fprintf(stderr, "Error: failed to read shared cache file at %s\n", path);
        return nullptr;
    }
    const dyld_cache_header*       header   = (dyld_cache_header*)firstPage;
    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)(firstPage + header->mappingOffset);
    size_t vmSize = (size_t)(mappings[2].address + mappings[2].size - mappings[0].address);
    vm_address_t result;
    kern_return_t r = ::vm_allocate(mach_task_self(), &result, vmSize, VM_FLAGS_ANYWHERE);
    if ( r != KERN_SUCCESS ) {
        fprintf(stderr, "Error: failed to allocate space to load shared cache file at %s\n", path);
        return nullptr;
    }
    for (int i=0; i < 3; ++i) {
        void* mapped_cache = ::mmap((void*)(result + mappings[i].address - mappings[0].address), (size_t)mappings[i].size,
                                    PROT_READ, MAP_FIXED | MAP_PRIVATE, cache_fd, mappings[i].fileOffset);
        if (mapped_cache == MAP_FAILED) {
            fprintf(stderr, "Error: mmap() for shared cache at %s failed, errno=%d\n", path, errno);
            return nullptr;
        }
    }
    ::close(cache_fd);
    return (DyldSharedCache*)result;
}
static const DyldSharedCache * mapCacheFile ( const char * path ) { struct stat statbuf ; if ( :: stat ( path , & statbuf ) ) { fprintf ( stderr , " \n " , path ) ; return nullptr ; } int cache_fd = :: open ( path , O_RDONLY ) ; if ( cache_fd < 0 ) { fprintf ( stderr , " \n " , path ) ; return nullptr ; } uint8_t firstPage [ 4096 ] ; if ( :: pread ( cache_fd , firstPage , 4096 , 0 ) != 4096 ) { fprintf ( stderr , " \n " , path ) ; return nullptr ; } const dyld_cache_header * header = ( dyld_cache_header * ) firstPage ; const dyld_cache_mapping_info * mappings = ( dyld_cache_mapping_info * ) ( firstPage + header -> mappingOffset ) ; size_t vmSize = ( size_t ) ( mappings [ 2 ] . address + mappings [ 2 ] . size - mappings [ 0 ] . address ) ; vm_address_t result ; kern_return_t r = :: vm_allocate ( mach_task_self ( ) , & result , vmSize , VM_FLAGS_ANYWHERE ) ; if ( r != KERN_SUCCESS ) { fprintf ( stderr , " \n " , path ) ; return nullptr ; } for ( int i = 0 ; i < 3 ; ++ i ) { void * mapped_cache = :: mmap ( ( void * ) ( result + mappings [ i ] . address - mappings [ 0 ] . address ) , ( size_t ) mappings [ i ] . size , PROT_READ , MAP_FIXED | MAP_PRIVATE , cache_fd , mappings [ i ] . fileOffset ) ; if ( mapped_cache == MAP_FAILED ) { fprintf ( stderr , " \n " , path , errno ) ; return nullptr ; } } :: close ( cache_fd ) ; return ( DyldSharedCache * ) result ; }
mmap() an shared cache file read/only but laid out like it would be at runtime
mmap () an shared cache file read / only but laid out like it would be at runtime