Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
 *
 * Copyright (c) 2016 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */

#include <TargetConditionals.h>

#if !TARGET_OS_EXCLAVEKIT

#include <System/sys/csr.h> // For csr_check()

#include "dyld_introspection.h"
#include "dyld_cache_format.h"
#include "ProcessAtlas.h"
#include "MachOLoaded.h"
#include "DyldProcessConfig.h"
#include "DyldAPIs.h"
#include "FileManager.h"

#define NO_ULEB
#include "FileAbstraction.hpp"
#include "MachOFileAbstraction.hpp"

using lsl::Allocator;
using lsl::UniquePtr;
using dyld4::FileManager;
using dyld4::Allocator;
using dyld4::Atlas::SharedCache;
using dyld4::Atlas::Image;
using dyld4::Atlas::ProcessSnapshot;
#if BUILDING_LIBDYLD
using dyld4::Atlas::Process;
#include "DyldLegacyInterfaceGlue.h"
namespace dyld4 {
    extern class APIs* gAPIs;
}
using dyld4::gAPIs;
#endif


#if BUILDING_DYLD
FileManager& defaultFileManager();
#else
static
FileManager& defaultFileManager() {
    static FileManager* sFileManager = nullptr;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sFileManager =  dyld4::MemoryManager::memoryManager().defaultAllocator().makeUnique<FileManager>(dyld4::MemoryManager::memoryManager().defaultAllocator(), nullptr).release();
    });
    return *sFileManager;
}
#endif /* !BUILDING_DYLD */

// This file is essentially glue to bind the public API/SPI to the internal object representations.
// No significant implementation code should be present in this file

#if BUILDING_LIBDYLD
#include <libgen.h>
extern int _NSGetExecutablePath(char* buf, uint32_t* bufsize);
IntrospectionVtable* dyldFrameworkIntrospectionVtable() {
    static IntrospectionVtable* vtable = nullptr;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        bool useAtlas = true;
        char pathBuffer[4096];
        uint32_t legnth = 4096;
        _NSGetExecutablePath(&pathBuffer[0], &legnth);
        const char* baseName = basename(pathBuffer);
        if (strcmp(baseName, "ReportCrash") == 0) {
            bool internalInstall = false;
#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
            uint32_t devFlags = *((uint32_t*)_COMM_PAGE_DEV_FIRM);
            if ((devFlags & 1) == 1) {
                internalInstall = true;
            }
#elif TARGET_OS_OSX
            if (::csr_check(CSR_ALLOW_APPLE_INTERNAL) == 0) {
                internalInstall = true;
            }
#endif
            // Get a value 0..<100
            uint32_t randValue = arc4random_uniform(100);
            // Increase this up to 99 for all runs
            if (!internalInstall || (randValue > 20)) {
                useAtlas = false;
            }
        }
        if ((strcmp(baseName, "com.apple.dt.instruments.dtsecurity") == 0)
            || (strcmp(baseName, "DTServiceHub") == 0)
            || (strcmp(baseName, "trace") == 0)
            || (strcmp(baseName, "trace_internal") == 0)
            || (strcmp(baseName, "apple-hwtrace") == 0)
            || (strcmp(baseName, "Xcode") == 0)) {
            useAtlas = false;
        }
#if !TARGET_OS_EXCLAVEKIT
        // Check boot-args after builtin overrides so they take precedence
        if (gAPIs->_dyld_commpage().disableAtlasUsage) {
            useAtlas = false;
        }
        // CHheck enablement secons so that if both  disable and enable bits are set it defaults to enabled
        if (gAPIs->_dyld_commpage().enableAtlasUsage) {
            useAtlas = true;
        }
#endif
        if (!useAtlas) {
            return;
        }
        // We want to use the atlas, so try to open up Dyld.framework
        void* handle = dlopen("/System/Library/PrivateFrameworks/Dyld.framework/Dyld", RTLD_LOCAL);
#if TARGET_OS_OSX
        // One OS X with versioned paths if the library is not n the cache the symlink will be in
        // base system and the dylib will be in the cryptex which will result in the above dlopen()
        // failing. Once this gets through a single build and is in the cache that will be never be an issue
        //FIXME: We can remove this after the first submission, once the paths have been added
        if (!handle) {
            handle = dlopen("/System/Library/PrivateFrameworks/Dyld.framework/Versions/A/Dyld", RTLD_LOCAL);
        }
#endif
        vtable = (IntrospectionVtable*)dlsym(handle, "_dyld_legacy_introspection_vtable");
    });
    return vtable;
}

#pragma mark -
#pragma mark Process

dyld_process_t dyld_process_create_for_task(task_t task, kern_return_t *kr) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_create_for_task(task, kr);
    }
    return (dyld_process_t)dyld4::MemoryManager::memoryManager().defaultAllocator().makeUnique<Process>(dyld4::MemoryManager::memoryManager().defaultAllocator(), defaultFileManager(), task, kr).release();
}

dyld_process_t dyld_process_create_for_current_task() {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_create_for_current_task();
    }
    return dyld_process_create_for_task(mach_task_self(), nullptr);
}

void dyld_process_dispose(dyld_process_t process) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_dispose(process);
    }
    UniquePtr<Process> temp((Process*)process);
}

uint32_t dyld_process_register_for_image_notifications(dyld_process_t process, kern_return_t *kr,
                                                       dispatch_queue_t queue, void (^block)(dyld_image_t image, bool load)) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_register_for_image_notifications(process, kr, queue, block);
    }
    kern_return_t krSink = KERN_SUCCESS;
    if (kr == nullptr) {
        kr = &krSink;
    }
    return ((dyld4::Atlas::Process*)process)->registerAtlasChangedEventHandler(kr, queue, (void (^)(dyld4::Atlas::Image*, bool))block);
}


uint32_t dyld_process_register_for_event_notification(dyld_process_t process, kern_return_t *kr, uint32_t event,
                                                      dispatch_queue_t queue, void (^block)()) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_register_for_event_notification(process, kr, event, queue, block);
    }
    kern_return_t krSink = KERN_SUCCESS;
    if (kr == nullptr) {
        kr = &krSink;
    }
    return ((dyld4::Atlas::Process*)process)->registerEventHandler(kr, event, queue, block);
}

void dyld_process_unregister_for_notification(dyld_process_t process, uint32_t handle) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_unregister_for_notification(process, handle);
    }
    ((dyld4::Atlas::Process*)process)->unregisterEventHandler(handle);
}

#pragma mark -
#pragma mark Process Snaphsot

dyld_process_snapshot_t dyld_process_snapshot_create_for_process(dyld_process_t process, kern_return_t *kr) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_snapshot_create_for_process(process, kr);
    }
    return (dyld_process_snapshot_t)((Process*)process)->getSnapshot(kr).release();
}

dyld_process_snapshot_t dyld_process_snapshot_create_from_data(void* buffer, size_t size, void* reserved1, size_t reserved2) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_snapshot_create_from_data(buffer, size, reserved1, reserved2);
    }
    // Make sure no one uses the reserved parameters
    assert(reserved1 == nullptr);
    assert(reserved2 == 0);
    STACK_ALLOCATOR(ephemeralAllocator, 0);
    auto bytes = std::span((std::byte*)buffer, size);
    return (dyld_process_snapshot_t)dyld4::MemoryManager::memoryManager().defaultAllocator()
                                .makeUnique<ProcessSnapshot>(ephemeralAllocator, defaultFileManager(), false, bytes).release();
}


void dyld_process_snapshot_dispose(dyld_process_snapshot_t snapshot) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_snapshot_dispose(snapshot);
    }
    ProcessSnapshot* processSnapshot = (ProcessSnapshot*)snapshot;
    if ( !processSnapshot->valid() )
        return;
    UniquePtr<ProcessSnapshot> temp(processSnapshot);
}

void dyld_process_snapshot_for_each_image(dyld_process_snapshot_t snapshot, void (^block)(dyld_image_t image)) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_snapshot_for_each_image(snapshot, block);
    }
    ProcessSnapshot* processSnapshot = (ProcessSnapshot*)snapshot;
    if ( !processSnapshot->valid() )
        return;
    processSnapshot->forEachImage(^(Image* image) {
        block((dyld_image_t)image);
    });
}

dyld_shared_cache_t dyld_process_snapshot_get_shared_cache(dyld_process_snapshot_t snapshot) {
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_process_snapshot_get_shared_cache(snapshot);
    }
    ProcessSnapshot* processSnapshot = (ProcessSnapshot*)snapshot;
    if ( !processSnapshot->valid() )
        return nullptr;
    return processSnapshot->sharedCache().withUnsafe([](auto cachePtr) {
        return (dyld_shared_cache_t)cachePtr;
    });
}

#endif /* BUILDING_LIBDYLD */

#pragma mark -
#pragma mark SharedCache

bool dyld_shared_cache_pin_mapping(dyld_shared_cache_t cache) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_pin_mapping(cache);
    }
#endif /* BUILDING_LIBDYLD */
    return ((dyld4::Atlas::SharedCache*)cache)->pin();
}

void dyld_shared_cache_unpin_mapping(dyld_shared_cache_t cache) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_unpin_mapping(cache);
    }
#endif /* BUILDING_LIBDYLD */
    ((dyld4::Atlas::SharedCache*)cache)->unpin();
}

uint64_t dyld_shared_cache_get_base_address(dyld_shared_cache_t cache_atlas) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_get_base_address(cache_atlas);
    }
#endif /* BUILDING_LIBDYLD */
    auto cache = (dyld4::Atlas::SharedCache*)cache_atlas;
    return (uint64_t)cache->rebasedAddress();

}

uint64_t dyld_shared_cache_get_mapped_size(dyld_shared_cache_t cache_atlas) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_get_mapped_size(cache_atlas);
    }
#endif /* BUILDING_LIBDYLD */
    auto cache = (dyld4::Atlas::SharedCache*)cache_atlas;
    return cache->size();
}

bool dyld_shared_cache_is_mapped_private(dyld_shared_cache_t cache_atlas) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_is_mapped_private(cache_atlas);
    }
#endif /* BUILDING_LIBDYLD */
    auto cache = (dyld4::Atlas::SharedCache*)cache_atlas;
    return cache->isPrivateMapped();
}

void dyld_shared_cache_copy_uuid(dyld_shared_cache_t cache_atlas, uuid_t *uuid) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_copy_uuid(cache_atlas, uuid);
    }
#endif /* BUILDING_LIBDYLD */
    auto cache = (dyld4::Atlas::SharedCache*)cache_atlas;
    memcpy((void*)&uuid[0], cache->uuid().begin(), 16);
}

void dyld_shared_cache_for_each_file(dyld_shared_cache_t cache_atlas, void (^block)(const char* file_path)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_for_each_file(cache_atlas, block);
    }
#endif /* BUILDING_LIBDYLD */
    auto cache = (dyld4::Atlas::SharedCache*)cache_atlas;
    cache->forEachFilePath(block);
}

void dyld_shared_cache_for_each_image(dyld_shared_cache_t cache, void (^block)(dyld_image_t image)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_for_each_image(cache, block);
    }
#endif /* BUILDING_LIBDYLD */
    ((dyld4::Atlas::SharedCache*)cache)->forEachImage(^(dyld4::Atlas::Image* image) {
        block((dyld_image_t)image);
    });
}

void dyld_for_each_installed_shared_cache_with_system_path(const char* root_path, void (^block)(dyld_shared_cache_t atlas)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_for_each_installed_shared_cache_with_system_path(root_path, block);
    }
#endif /* BUILDING_LIBDYLD */
    STACK_ALLOCATOR(ephemeralAllocator, 0);
    dyld4::Atlas::SharedCache::forEachInstalledCacheWithSystemPath(ephemeralAllocator, defaultFileManager(), root_path, ^(dyld4::Atlas::SharedCache* cache){
        block((dyld_shared_cache_t)cache);
    });
}

void dyld_for_each_installed_shared_cache(void (^block)(dyld_shared_cache_t cache)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_for_each_installed_shared_cache(block);
    }
#endif /* BUILDING_LIBDYLD */
    STACK_ALLOCATOR(ephemeralAllocator, 0);
    dyld4::Atlas::SharedCache::forEachInstalledCacheWithSystemPath(ephemeralAllocator, defaultFileManager(), "/", ^(dyld4::Atlas::SharedCache* cache){
        block((dyld_shared_cache_t)cache);
    });
}

bool dyld_shared_cache_for_file(const char* filePath, void (^block)(dyld_shared_cache_t cache)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_shared_cache_for_file(filePath, block);
    }
#endif /* BUILDING_LIBDYLD */
    STACK_ALLOCATOR(ephemeralAllocator, 0);
    auto cacheFile = defaultFileManager().fileRecordForPath(ephemeralAllocator, filePath);
    auto cache = SharedCache::createForFileRecord(ephemeralAllocator, std::move(cacheFile));
    if (cache) {
        cache.withUnsafe([&](auto cachePtr) {
            block((dyld_shared_cache_t)cachePtr);
        });
        return true;
    }
    return false;
}

bool dyld_image_content_for_segment(dyld_image_t image, const char* segmentName,
                                    void (^contentReader)(const void* content, uint64_t vmAddr, uint64_t vmSize)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_image_content_for_segment(image, segmentName, contentReader);
    }
#endif /* BUILDING_LIBDYLD */
    return ((Image*)image)->contentForSegment(segmentName, contentReader);
}

bool dyld_image_content_for_section(dyld_image_t image, const char* segmentName, const char* sectionName,
                                    void (^contentReader)(const void* content, uint64_t vmAddr, uint64_t vmSize)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_image_content_for_section(image, segmentName, sectionName, contentReader);
    }
#endif /* BUILDING_LIBDYLD */
    return ((Image*)image)->contentForSection(segmentName, sectionName, contentReader);
}

bool dyld_image_copy_uuid(dyld_image_t image, uuid_t* uuid) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_image_copy_uuid(image, uuid);
    }
#endif /* BUILDING_LIBDYLD */
    using lsl::UUID;

    UUID imageUUID = ((dyld4::Atlas::Image*)image)->uuid();
    if (imageUUID.empty()) {
        return false;
    }
    std::copy((uint8_t*)imageUUID.begin(), (uint8_t*)imageUUID.end(), (uint8_t*)&uuid[0]);
    return true;
}

bool dyld_image_for_each_segment_info(dyld_image_t image, void (^block)(const char* segmentName, uint64_t vmAddr, uint64_t vmSize, int perm)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_image_for_each_segment_info(image, block);
    }
#endif /* BUILDING_LIBDYLD */
    // FIXME: Make a temporary null terminated buffer for the segment name
    return ((dyld4::Atlas::Image*)image)->forEachSegment(block);
}

bool dyld_image_for_each_section_info(dyld_image_t image,
                                 void (^block)(const char* segmentName, const char* sectionName, uint64_t vmAddr, uint64_t vmSize)) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_image_for_each_section_info(image, block);
    }
#endif /* BUILDING_LIBDYLD */
    // FIXME: Make a temporary null terminated buffer for the segment and section names
    return ((dyld4::Atlas::Image*)image)->forEachSection(block);
}

const char* dyld_image_get_installname(dyld_image_t image) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_image_get_installname(image);
    }
#endif /* BUILDING_LIBDYLD */
    return ((dyld4::Atlas::Image*)image)->installname();
}

#if !BUILDING_CACHE_BUILDER
const char* dyld_image_get_file_path(dyld_image_t image) {
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_image_get_file_path(image);
    }
#endif /* BUILDING_LIBDYLD */
    return ((Image*)image)->filename();
}
#endif


// FIXME: These functions are part of DyldSharedCache.cpp and we should use that, but we can't until we factor out libdyld_introspection
static
const void* getLocalNlistEntries(const dyld_cache_local_symbols_info* localInfo) {
    return (uint8_t*)localInfo + localInfo->nlistOffset;
}

static
const char* getLocalStrings(const dyld_cache_local_symbols_info* localInfo)
{
    return (char*)localInfo + localInfo->stringsOffset;
}

static
void forEachLocalSymbolEntry(const dyld_cache_local_symbols_info* localInfo,
                             bool use64BitDylibOffsets,
                             void (^handler)(uint64_t dylibCacheVMOffset, uint32_t nlistStartIndex, uint32_t nlistCount, bool& stop))
{
    if ( use64BitDylibOffsets ) {
        // On new caches, the dylibOffset is 64-bits, and is a VM offset
        const auto* localEntries = (dyld_cache_local_symbols_entry_64*)((uint8_t*)localInfo + localInfo->entriesOffset);
        bool stop = false;
        for (uint32_t i = 0; i < localInfo->entriesCount; i++) {
            const dyld_cache_local_symbols_entry_64& localEntry = localEntries[i];
            handler(localEntry.dylibOffset, localEntry.nlistStartIndex, localEntry.nlistCount, stop);
            if ( stop )
                break;
        }
    } else {
        // On old caches, the dylibOffset is 64-bits, and is a file offset
        // Note, as we are only looking for mach_header's, a file offset is a VM offset in this case
        const auto* localEntries = (dyld_cache_local_symbols_entry*)((uint8_t*)localInfo + localInfo->entriesOffset);
        bool stop = false;
        for (uint32_t i = 0; i < localInfo->entriesCount; i++) {
            const dyld_cache_local_symbols_entry& localEntry = localEntries[i];
            handler(localEntry.dylibOffset, localEntry.nlistStartIndex, localEntry.nlistCount, stop);
            if ( stop )
                break;
        }
    }
}


bool dyld_image_local_nlist_content_4Symbolication(dyld_image_t image,
                                                   void (^contentReader)(const void* nListStart, uint64_t nListCount,
                                                                         const char* stringTable))
{
#if BUILDING_LIBDYLD
    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
        return vtable->dyld_image_local_nlist_content_4Symbolication(image, contentReader);
    }
#endif /* BUILDING_LIBDYLD */
    Image* atlasImage = (dyld4::Atlas::Image*)image;
    const SharedCache* sharedCache = atlasImage->sharedCache();
    if ( !sharedCache )
        return false;

    __block bool result = true;
    if ( auto localsFileData = sharedCache->localSymbols() ) {
        uint64_t textOffsetInCache = atlasImage->sharedCacheVMOffset();

        const dyld_cache_local_symbols_info* localInfo = localsFileData->localInfo();
        forEachLocalSymbolEntry(localInfo, localsFileData->use64BitDylibOffsets(),
                                ^(uint64_t dylibCacheVMOffset, uint32_t nlistStartIndex, uint32_t nlistCount, bool& stop){
            if ( dylibCacheVMOffset == textOffsetInCache ) {
                if ( atlasImage->pointerSize() == 8 ) {
                    typedef Pointer64<LittleEndian> P;
                    const macho_nlist<P>* allLocalNlists = (macho_nlist<P>*)getLocalNlistEntries(localInfo);
                    const macho_nlist<P>* dylibNListsStart = &allLocalNlists[nlistStartIndex];
                    contentReader(dylibNListsStart, nlistCount, getLocalStrings(localInfo));
                } else if ( atlasImage->pointerSize() == 4 ) {
                    typedef Pointer32<LittleEndian> P;
                    const macho_nlist<P>* allLocalNlists = (macho_nlist<P>*)getLocalNlistEntries(localInfo);
                    const macho_nlist<P>* dylibNListsStart = &allLocalNlists[nlistStartIndex];
                    contentReader(dylibNListsStart, nlistCount, getLocalStrings(localInfo));
                } else {
                    result = false;
                }
                stop = true;
            }
        });
        return result;
    }
    return result;
}


#endif // !TARGET_OS_EXCLAVEKIT