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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
 *
 * Copyright (c) 2014 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 "MetadataVisitor.h"

#if SUPPORT_VM_LAYOUT
#include "MachOAnalyzer.h"
#endif

#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
#include "ASLRTracker.h"
#endif

#if POINTERS_ARE_UNSLID
#include "DyldSharedCache.h"
#endif

using namespace metadata_visitor;

#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
typedef cache_builder::Fixup::Cache32 Cache32;
typedef cache_builder::Fixup::Cache64 Cache64;
#endif

using mach_o::Header;

//
// MARK: --- ResolvedValue methods ---
//

#if SUPPORT_VM_LAYOUT

ResolvedValue::ResolvedValue(const void* targetValue, VMAddress vmAddr)
    : targetValue(targetValue), vmAddr(vmAddr)
{
}

#else

ResolvedValue::ResolvedValue(const Segment& cacheSegment, VMOffset segmentVMOffset)
    : cacheSegment(cacheSegment), segmentVMOffset(segmentVMOffset)
{
}
ResolvedValue::ResolvedValue(const ResolvedValue& parentValue, const void* childLocation)
    : cacheSegment(parentValue.cacheSegment)
{
    this->segmentVMOffset = VMOffset((uint64_t)((uint8_t*)childLocation - parentValue.cacheSegment.bufferStart));
}

std::optional<uint16_t> ResolvedValue::chainedPointerFormat() const
{
    return this->cacheSegment.onDiskDylibChainedPointerFormat;
}

uint32_t ResolvedValue::segmentIndex() const
{
    return this->cacheSegment.segIndex;
}

#endif // SUPPORT_VM_LAYOUT

void* ResolvedValue::value() const
{
#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
    return this->cacheSegment.bufferStart + this->segmentVMOffset.rawValue();
#else
    return (void*)this->targetValue;
#endif
}

VMAddress ResolvedValue::vmAddress() const
{
#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
    return this->cacheSegment.startVMAddr + this->segmentVMOffset;
#else
    return this->vmAddr;
#endif
}

//
// MARK: --- Visitor methods ---
//

#if POINTERS_ARE_UNSLID

Visitor::Visitor(const DyldSharedCache* dyldCache, const dyld3::MachOAnalyzer* dylibMA,
                 std::optional<VMAddress> selectorStringsBaseAddress)
    : dylibMA(dylibMA), dylibBaseAddress(((const Header*)dylibMA)->preferredLoadAddress()),
      selectorStringsBaseAddress(selectorStringsBaseAddress)
{
    pointerSize = dylibMA->pointerSize();

    this->onDiskDylibChainedPointerBaseAddress = dylibBaseAddress;

    if ( dylibMA->inDyldCache() ) {
        dyldCache->forEachCache(^(const DyldSharedCache *cache, bool& stopCache) {
            cache->forEachSlideInfo(^(uint64_t mappingStartAddress, uint64_t mappingSize, const uint8_t *mappingPagesStart, uint64_t slideInfoOffset, uint64_t slideInfoSize, const dyld_cache_slide_info *slideInfoHeader) {
                if ( slideInfoHeader->version == 1 ) {
                    this->sharedCacheChainedPointerFormat       = SharedCacheFormat::v1;
                    this->onDiskDylibChainedPointerBaseAddress  = VMAddress(dyldCache->unslidLoadAddress());
                } else if ( slideInfoHeader->version == 2 ) {
                    const dyld_cache_slide_info2* slideInfo = (dyld_cache_slide_info2*)(slideInfoHeader);
                    assert(slideInfo->delta_mask == 0x00FFFF0000000000);
                    this->sharedCacheChainedPointerFormat       = SharedCacheFormat::v2_x86_64_tbi;
                    this->onDiskDylibChainedPointerBaseAddress  = VMAddress(slideInfo->value_add);
                } else if ( slideInfoHeader->version == 3 ) {
                    this->sharedCacheChainedPointerFormat       = SharedCacheFormat::v3;
                    this->onDiskDylibChainedPointerBaseAddress  = VMAddress(dyldCache->unslidLoadAddress());
                } else if ( slideInfoHeader->version == 4 ) {
                    const dyld_cache_slide_info4* slideInfo = (dyld_cache_slide_info4*)(slideInfoHeader);
                    assert(slideInfo->delta_mask == 0x00000000C0000000);
                    this->sharedCacheChainedPointerFormat       = SharedCacheFormat::v4;
                    this->onDiskDylibChainedPointerBaseAddress  = VMAddress(slideInfo->value_add);
                } else if ( slideInfoHeader->version == 5 ) {
                    this->sharedCacheChainedPointerFormat       = SharedCacheFormat::v5;
                    this->onDiskDylibChainedPointerBaseAddress  = VMAddress(dyldCache->unslidLoadAddress());
                } else {
                    assert(false);
                }
            });
        });
    } else {
        if ( dylibMA->hasChainedFixups() )
            this->chainedPointerFormat = dylibMA->chainedPointerFormat();
    }
}

#elif SUPPORT_VM_LAYOUT

Visitor::Visitor(const dyld3::MachOAnalyzer* dylibMA)
    : dylibMA(dylibMA), dylibBaseAddress(((const Header*)dylibMA)->preferredLoadAddress())
{
    pointerSize = dylibMA->pointerSize();
}

#else

// Cache builder dylib
Visitor::Visitor(CacheVMAddress cacheBaseAddress, const dyld3::MachOFile* dylibMF,
                 std::vector<Segment>&& segments, std::optional<VMAddress> selectorStringsBaseAddress,
                 std::vector<uint64_t>&& bindTargets)
    : isOnDiskDylib(false), dylibMF(dylibMF), sharedCacheBaseAddress(cacheBaseAddress),
      segments(std::move(segments)), bindTargets(std::move(bindTargets)),
      selectorStringsBaseAddress(selectorStringsBaseAddress)
{
    pointerSize = dylibMF->pointerSize();

    // Cache dylibs should never have a chain value set, as they always use the in-cache builder
    // representation of values
    for ( const Segment& segment : this->segments ) {
        assert(!segment.onDiskDylibChainedPointerFormat.has_value());
    }
}

// On disk dylib/executable
Visitor::Visitor(VMAddress chainedPointerBaseAddress, const dyld3::MachOFile* dylibMF,
                 std::vector<Segment>&& segments, std::optional<VMAddress> selectorStringsBaseAddress,
                 std::vector<uint64_t>&& bindTargets)
    : isOnDiskDylib(true), dylibMF(dylibMF), onDiskDylibChainedPointerBaseAddress(chainedPointerBaseAddress),
      segments(std::move(segments)), bindTargets(std::move(bindTargets)),
      selectorStringsBaseAddress(selectorStringsBaseAddress)
{
    pointerSize = dylibMF->pointerSize();

    // On-disk dylibs should have a chain value set, even if its a 0 for opcode fixup dylibs
    for ( const Segment& segment : this->segments ) {
        assert(segment.onDiskDylibChainedPointerFormat.has_value());
    }
}

#endif

#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS || POINTERS_ARE_UNSLID
VMAddress Visitor::sharedCacheSelectorStringsBaseAddress() const
{
    return this->selectorStringsBaseAddress.value();
}
#endif

#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
VMAddress Visitor::getOnDiskDylibChainedPointerBaseAddress() const
{
    assert(this->isOnDiskDylib);
    return this->onDiskDylibChainedPointerBaseAddress;
}

const dyld3::MachOFile* Visitor::mf() const
{
    return this->dylibMF;
}

const Header* Visitor::hdr() const
{
    return (const Header*)this->dylibMF;
}


bool Visitor::isOnDiskBinary() const
{
    return this->isOnDiskDylib;
}
#endif

ResolvedValue Visitor::getField(const ResolvedValue& parent, const void* fieldPos) const
{
#if SUPPORT_VM_LAYOUT
    // In dyld, we just use raw pointers for everything, and don't need to indirect via segment+offset like
    // in the cache builder
    VMOffset offsetInDylib((uint64_t)fieldPos - (uint64_t)this->dylibMA);
    VMAddress fieldVMAddr(this->dylibBaseAddress + offsetInDylib);
    return ResolvedValue(fieldPos, fieldVMAddr);
#else
    // In the cache builder, everything is an offset in a segment, as we don't know where the segments
    // will be in memory when running
    return ResolvedValue(parent, fieldPos);
#endif
}

ResolvedValue Visitor::getValueFor(VMAddress vmAddr) const
{
#if SUPPORT_VM_LAYOUT
    // In dyld, we just use raw pointers for everything, and don't need to indirect via segment+offset like
    // in the cache builder
    VMOffset offsetInDylib = vmAddr - this->dylibBaseAddress;
    const void* valueInDylib = (const uint8_t*)this->dylibMA + offsetInDylib.rawValue();
    return ResolvedValue(valueInDylib, vmAddr);
#else
    // Find the segment containing the target address
    for ( const Segment& cacheSegment : segments ) {
        if ( (vmAddr >= cacheSegment.startVMAddr) && (vmAddr < cacheSegment.endVMAddr) ) {
            // Skip segments which don't contribute to the cache.  This is a hack to account
            // for LINKEDIT, which doesn't really get its own buffer.  We don't want to match
            // an address to LINKEDIT, when we actually wanted to find it in the selector strings
            // "segment" we also track here
            if ( cacheSegment.bufferStart == nullptr )
                continue;

            VMOffset segmentVMOffset = vmAddr - cacheSegment.startVMAddr;
            return ResolvedValue(cacheSegment, segmentVMOffset);
        }
    }

    assert(0);
#endif
}

// Dereferences the given value.  It must not resolve to nullptr
ResolvedValue Visitor::resolveRebase(const ResolvedValue& value) const
{
#if POINTERS_ARE_UNSLID
    uint64_t runtimeOffset = 0;

    if ( this->sharedCacheChainedPointerFormat != SharedCacheFormat::none ) {
        // Crack the shared cache slide format
        switch ( this->sharedCacheChainedPointerFormat ) {
            case SharedCacheFormat::none:
                assert(false);
            case SharedCacheFormat::v1: {
                runtimeOffset = *(uint32_t*)value.value() - onDiskDylibChainedPointerBaseAddress.rawValue();
                break;
            }
            case SharedCacheFormat::v2_x86_64_tbi: {
                const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                uint64_t rawValue = fixup->raw64;

                const uint64_t   deltaMask    = 0x00FFFF0000000000;
                const uint64_t   valueMask    = ~deltaMask;
                rawValue = (rawValue & valueMask);
                // Already a runtime offset, so no need to do anything with valueAdd
                runtimeOffset = rawValue;
                break;
            }
            case SharedCacheFormat::v3: {
                // Just use the chained pointer format for arm64e
                auto* chainedValue = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                chainedValue->isRebase(DYLD_CHAINED_PTR_ARM64E,
                                       onDiskDylibChainedPointerBaseAddress.rawValue(),
                                       runtimeOffset);
                break;
            }
            case SharedCacheFormat::v4: {
                const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                uint64_t rawValue = fixup->raw32;

                const uint64_t   deltaMask    = 0x00000000C0000000;
                const uint64_t   valueMask    = ~deltaMask;
                rawValue = (rawValue & valueMask);
                // Already a runtime offset, so no need to do anything with valueAdd
                runtimeOffset = rawValue;
                break;
            }
            case SharedCacheFormat::v5: {
                // Just use the chained pointer format for arm64/arm64e shared caches
                auto* chainedValue = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                chainedValue->isRebase(DYLD_CHAINED_PTR_ARM64E_SHARED_CACHE,
                                       onDiskDylibChainedPointerBaseAddress.rawValue(),
                                       runtimeOffset);
                break;
            }
        }
    } else {
        const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
        if ( this->chainedPointerFormat == 0 ) {
            // HACK: 32-bit cache dylibs don't have enough bits to have real chains, so we pretend they
            // have no chains, just raw VMAddr's
            assert(dylibMA->hasOpcodeFixups());

            // HACK: This is a binary without chained fixups.  Is it safe to assume this is a rebase?
            uint64_t rebaseVMAddr = (pointerSize == 8) ? fixup->raw64 : fixup->raw32;
            runtimeOffset = rebaseVMAddr - this->onDiskDylibChainedPointerBaseAddress.rawValue();
        } else {
            bool isRebase = fixup->isRebase(this->chainedPointerFormat,
                                            onDiskDylibChainedPointerBaseAddress.rawValue(),
                                            runtimeOffset);
            assert(isRebase);
        }
    }

    VMAddress targetVMAddress = onDiskDylibChainedPointerBaseAddress + VMOffset(runtimeOffset);
    return this->getValueFor(targetVMAddress);
#elif SUPPORT_VM_LAYOUT
    // In dyld, we just use raw pointers for everything, and don't need to indirect via segment+offset like
    // in the cache builder
    const void* targetValue = (const void*)*(uintptr_t*)value.value();

    // FIXME: We didn't expect a null here.  Should we find a way to error out, or just let the parser
    // crash with a nullptr dereference.
    if ( targetValue == nullptr )
        return ResolvedValue(nullptr, VMAddress());

    // The value may have been signed.  Strip the signature if that is the case
#if __has_feature(ptrauth_calls)
    targetValue = __builtin_ptrauth_strip(targetValue, ptrauth_key_asia);
#endif

    VMOffset offsetInDylib((uint64_t)targetValue - (uint64_t)this->dylibMA);
    return ResolvedValue(targetValue, this->dylibBaseAddress + offsetInDylib);
#else
    // In on-disk dylibs, we crack the chained fixups or other fixups
    if ( this->isOnDiskBinary() ) {
        uint64_t runtimeOffset = 0;
        const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
        uint16_t chainedPointerFormat = value.chainedPointerFormat().value();
        if ( chainedPointerFormat == 0 ) {
            // HACK: 32-bit cache dylibs don't have enough bits to have real chains, so we pretend they
            // have no chains, just raw VMAddr's
            assert(dylibMF->hasOpcodeFixups() || (dylibMF->inDyldCache() && !dylibMF->is64()));

            // HACK: This is a binary without chained fixups.  Is it safe to assume this is a rebase?
            uint64_t rebaseVMAddr = (pointerSize == 8) ? fixup->raw64 : fixup->raw32;
            runtimeOffset = rebaseVMAddr - this->onDiskDylibChainedPointerBaseAddress.rawValue();
        } else {
            bool isRebase = fixup->isRebase(chainedPointerFormat, onDiskDylibChainedPointerBaseAddress.rawValue(), runtimeOffset);
            assert(isRebase);
        }

        VMAddress targetVMAddress = onDiskDylibChainedPointerBaseAddress + VMOffset(runtimeOffset);
        return this->getValueFor(targetVMAddress);
    } else {
        // Cache builder dylib.  These use values in a packed format
        if ( this->pointerSize == 4 ) {
            const void* fixupLocation = value.value();
            assert(!Cache32::isNull(fixupLocation));

            CacheVMAddress targetCacheVMAddress = Cache32::getCacheVMAddressFromLocation(sharedCacheBaseAddress, fixupLocation);
            VMAddress targetVMAddress(targetCacheVMAddress.rawValue());
            return this->getValueFor(targetVMAddress);
        } else {
            const void* fixupLocation = value.value();
            assert(!Cache64::isNull(fixupLocation));

            CacheVMAddress targetCacheVMAddress = Cache64::getCacheVMAddressFromLocation(sharedCacheBaseAddress, fixupLocation);
            VMAddress targetVMAddress(targetCacheVMAddress.rawValue());
            return this->getValueFor(targetVMAddress);
        }
    }
#endif
}

// Dereferences the given value.  It may be either a bind or a rebase.  It must not resolve to nullptr
ResolvedValue Visitor::resolveBindOrRebase(const ResolvedValue& value, bool& wasBind) const
{
#if SUPPORT_VM_LAYOUT
    // dyld will never see a bind or a rebase, just live values.  Use resolveRebase for this as it already
    // handles this case
    wasBind = false;
    return this->resolveRebase(value);
#else
    // In on-disk dylibs, we crack the chained fixups or other fixups
    if ( this->isOnDiskBinary() ) {
        // Check if this is a bind
        {
            const auto* fixupLoc = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
            uint16_t chainedPointerFormat = value.chainedPointerFormat().value();

            // Follow the class reference to get to the actual class
            // With objc patching, this might be a bind to self
            uint32_t bindOrdinal;
            int64_t  bindAddend;
            if ( (chainedPointerFormat != 0) && fixupLoc->isBind(chainedPointerFormat, bindOrdinal, bindAddend) ) {
                wasBind = true;
                VMAddress targetVMAddress(this->bindTargets[bindOrdinal] + bindAddend);
                return this->getValueFor(targetVMAddress);
            }
        }

        // Fall back to resolveRebase() which can handle rebases
        wasBind = false;
        return this->resolveRebase(value);
    } else {
        // Cache builder dylibs don't have binds, so fall back to resolveRebase()
        wasBind = false;
        return this->resolveRebase(value);
    }
#endif
}

std::optional<ResolvedValue> Visitor::resolveOptionalRebase(const ResolvedValue& value) const
{
#if POINTERS_ARE_UNSLID
    uint64_t runtimeOffset = 0;

    if ( this->sharedCacheChainedPointerFormat != SharedCacheFormat::none ) {
        // Crack the shared cache slide format
        switch ( this->sharedCacheChainedPointerFormat ) {
            case SharedCacheFormat::none:
                assert(false);
            case SharedCacheFormat::v1: {
                uint64_t rawvalue = *(uint32_t*)value.value();
                if ( rawvalue == 0 )
                    return { };
                runtimeOffset = rawvalue - onDiskDylibChainedPointerBaseAddress.rawValue();
                break;
            }
            case SharedCacheFormat::v2_x86_64_tbi: {
                const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                uint64_t rawValue = fixup->raw64;
                if ( rawValue == 0 )
                    return { };

                const uint64_t   deltaMask    = 0x00FFFF0000000000;
                const uint64_t   valueMask    = ~deltaMask;
                rawValue = (rawValue & valueMask);
                // Already a runtime offset, so no need to do anything with valueAdd
                runtimeOffset = rawValue;
                break;
            }
            case SharedCacheFormat::v3: {
                // Just use the chained pointer format for arm64e
                auto* chainedValue = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                if ( chainedValue->raw64 == 0 )
                    return { };

                chainedValue->isRebase(DYLD_CHAINED_PTR_ARM64E,
                                       onDiskDylibChainedPointerBaseAddress.rawValue(),
                                       runtimeOffset);
                break;
            }
            case SharedCacheFormat::v4: {
                const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                uint64_t rawValue = fixup->raw32;
                if ( rawValue == 0 )
                    return { };

                const uint64_t   deltaMask    = 0x00000000C0000000;
                const uint64_t   valueMask    = ~deltaMask;
                rawValue = (rawValue & valueMask);
                // Already a runtime offset, so no need to do anything with valueAdd
                runtimeOffset = rawValue;
                break;
            }
            case SharedCacheFormat::v5: {
                // Just use the chained pointer format for arm64/arm64e shared caches
                auto* chainedValue = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                if ( chainedValue->raw64 == 0 )
                    return { };

                chainedValue->isRebase(DYLD_CHAINED_PTR_ARM64E_SHARED_CACHE,
                                       onDiskDylibChainedPointerBaseAddress.rawValue(),
                                       runtimeOffset);
                break;
            }
        }
    } else {
        const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
        if ( this->chainedPointerFormat == 0 ) {
            // HACK: 32-bit cache dylibs don't have enough bits to have real chains, so we pretend they
            // have no chains, just raw VMAddr's
            assert(dylibMA->hasOpcodeFixups());

            // HACK: This is a binary without chained fixups.  Is it safe to assume this is a rebase?
            uint64_t rebaseVMAddr = (pointerSize == 8) ? fixup->raw64 : fixup->raw32;
            if ( rebaseVMAddr == 0 )
                return { };

            runtimeOffset = rebaseVMAddr - this->onDiskDylibChainedPointerBaseAddress.rawValue();
        } else {
            if ( pointerSize == 8 ) {
                if ( fixup->raw64 == 0 )
                    return { };
            } else {
                if ( fixup->raw32 == 0 )
                    return { };
            }

            bool isRebase = fixup->isRebase(this->chainedPointerFormat,
                                            onDiskDylibChainedPointerBaseAddress.rawValue(),
                                            runtimeOffset);
            assert(isRebase);
        }
    }

    VMAddress targetVMAddress = onDiskDylibChainedPointerBaseAddress + VMOffset(runtimeOffset);
    return this->getValueFor(targetVMAddress);
#elif SUPPORT_VM_LAYOUT
    // In dyld, we just use raw pointers for everything, and don't need to indirect via segment+offset like
    // in the cache builder
    const void* targetValue = (const void*)*(uintptr_t*)value.value();

    // FIXME: We didn't expect a null here.  Should we find a way to error out, or just let the parser
    // crash with a nullptr dereference.
    if ( targetValue == nullptr )
        return std::nullopt;

    // The value may have been signed.  Strip the signature if that is the case
#if __has_feature(ptrauth_calls)
    targetValue = __builtin_ptrauth_strip(targetValue, ptrauth_key_asia);
#endif

    VMOffset offsetInDylib((uint64_t)targetValue - (uint64_t)this->dylibMA);
    return ResolvedValue(targetValue, this->dylibBaseAddress + offsetInDylib);
#else
    // In on-disk dylibs, we crack the chained fixups or other fixups
    if ( this->isOnDiskBinary() ) {
        uint64_t runtimeOffset = 0;
        const auto* fixupLoc = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
        uint16_t chainedPointerFormat = value.chainedPointerFormat().value();
        if ( chainedPointerFormat == 0 ) {
            assert(dylibMF->hasOpcodeFixups() || (dylibMF->inDyldCache() && !dylibMF->is64()));

            // HACK: This is a binary without chained fixups.  Is it safe to assume this is a rebase?
            uint64_t rebaseVMAddr = (pointerSize == 8) ? fixupLoc->raw64 : fixupLoc->raw32;

            // Assume null VMAddr's means there's no value here
            if ( rebaseVMAddr == 0 )
                return { };

            runtimeOffset = rebaseVMAddr - this->onDiskDylibChainedPointerBaseAddress.rawValue();
        } else {
            bool isRebase = fixupLoc->isRebase(chainedPointerFormat, onDiskDylibChainedPointerBaseAddress.rawValue(), runtimeOffset);
            assert(isRebase);

            if ( pointerSize == 8 ) {
                if ( fixupLoc->raw64 == 0 )
                    return { };
            } else {
                if ( fixupLoc->raw32 == 0 )
                    return { };
            }

            // Assume an offset of 0 means its null.  There's no good reason for an objc class to have an offset of 0 from the cache.
            if ( runtimeOffset == 0 )
                return { };
        }

        VMAddress targetVMAddress = this->onDiskDylibChainedPointerBaseAddress + VMOffset(runtimeOffset);
        return this->getValueFor(targetVMAddress);
    } else {
        // Cache builder dylib.  These use values in a packed format
        if ( this->pointerSize == 4 ) {
            const void* fixupLocation = value.value();
            if ( Cache32::isNull(fixupLocation) )
                return { };

            CacheVMAddress targetCacheVMAddress = Cache32::getCacheVMAddressFromLocation(sharedCacheBaseAddress, fixupLocation);
            VMAddress targetVMAddress(targetCacheVMAddress.rawValue());
            return this->getValueFor(targetVMAddress);
        } else {
            const void* fixupLocation = value.value();
            if ( Cache64::isNull(fixupLocation) )
                return { };

            CacheVMAddress targetCacheVMAddress = Cache64::getCacheVMAddressFromLocation(sharedCacheBaseAddress, fixupLocation);
            VMAddress targetVMAddress(targetCacheVMAddress.rawValue());
            return this->getValueFor(targetVMAddress);
        }
    }
#endif
}

std::optional<VMAddress> Visitor::resolveOptionalRebaseToVMAddress(const ResolvedValue& value) const
{
#if POINTERS_ARE_UNSLID
    const void* targetValue = (const void*)*(uintptr_t*)value.value();

    // FIXME: We didn't expect a null here.  Should we find a way to error out, or just let the parser
    // crash with a nullptr dereference.
    if ( targetValue == nullptr )
        return std::nullopt;

    uint64_t runtimeOffset = 0;

    if ( this->sharedCacheChainedPointerFormat != SharedCacheFormat::none ) {
        // Crack the shared cache slide format
        switch ( this->sharedCacheChainedPointerFormat ) {
            case SharedCacheFormat::none:
                assert(false);
            case SharedCacheFormat::v1: {
                uint64_t rawvalue = *(uint32_t*)value.value();
                if ( rawvalue == 0 )
                    return { };
                runtimeOffset = rawvalue - onDiskDylibChainedPointerBaseAddress.rawValue();
                break;
            }
            case SharedCacheFormat::v2_x86_64_tbi: {
                const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                uint64_t rawValue = fixup->raw64;
                if ( rawValue == 0 )
                    return { };

                const uint64_t   deltaMask    = 0x00FFFF0000000000;
                const uint64_t   valueMask    = ~deltaMask;
                rawValue = (rawValue & valueMask);
                // Already a runtime offset, so no need to do anything with valueAdd
                runtimeOffset = rawValue;
                break;
            }
            case SharedCacheFormat::v3: {
                // Just use the chained pointer format for arm64e
                auto* chainedValue = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                if ( chainedValue->raw64 == 0 )
                    return { };

                chainedValue->isRebase(DYLD_CHAINED_PTR_ARM64E,
                                       onDiskDylibChainedPointerBaseAddress.rawValue(),
                                       runtimeOffset);
                break;
            }
            case SharedCacheFormat::v4: {
                const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                uint64_t rawValue = fixup->raw32;
                if ( rawValue == 0 )
                    return { };

                const uint64_t   deltaMask    = 0x00000000C0000000;
                const uint64_t   valueMask    = ~deltaMask;
                rawValue = (rawValue & valueMask);
                // Already a runtime offset, so no need to do anything with valueAdd
                runtimeOffset = rawValue;
                break;
            }
            case SharedCacheFormat::v5: {
                // Just use the chained pointer format for arm64/arm64e shared caches
                auto* chainedValue = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
                if ( chainedValue->raw64 == 0 )
                    return { };

                chainedValue->isRebase(DYLD_CHAINED_PTR_ARM64E_SHARED_CACHE,
                                       onDiskDylibChainedPointerBaseAddress.rawValue(),
                                       runtimeOffset);
                break;
            }
        }
    } else {
        const auto* fixup = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
        if ( this->chainedPointerFormat == 0 ) {
            // HACK: 32-bit cache dylibs don't have enough bits to have real chains, so we pretend they
            // have no chains, just raw VMAddr's
            assert(dylibMA->hasOpcodeFixups());

            // HACK: This is a binary without chained fixups.  Is it safe to assume this is a rebase?
            uint64_t rebaseVMAddr = (pointerSize == 8) ? fixup->raw64 : fixup->raw32;
            if ( rebaseVMAddr == 0 )
                return { };

            runtimeOffset = rebaseVMAddr - this->onDiskDylibChainedPointerBaseAddress.rawValue();
        } else {
            if ( pointerSize == 8 ) {
                if ( fixup->raw64 == 0 )
                    return { };
            } else {
                if ( fixup->raw32 == 0 )
                    return { };
            }

            bool isRebase = fixup->isRebase(this->chainedPointerFormat,
                                            onDiskDylibChainedPointerBaseAddress.rawValue(),
                                            runtimeOffset);
            assert(isRebase);
        }
    }

    VMAddress targetVMAddress = onDiskDylibChainedPointerBaseAddress + VMOffset(runtimeOffset);
    return targetVMAddress;
#elif SUPPORT_VM_LAYOUT
    // In dyld, we just use raw pointers for everything, and don't need to indirect via segment+offset like
    // in the cache builder
    const void* targetValue = (const void*)*(uintptr_t*)value.value();

    // FIXME: We didn't expect a null here.  Should we find a way to error out, or just let the parser
    // crash with a nullptr dereference.
    if ( targetValue == nullptr )
        return std::nullopt;

    // The value may have been signed.  Strip the signature if that is the case
#if __has_feature(ptrauth_calls)
    targetValue = __builtin_ptrauth_strip(targetValue, ptrauth_key_asia);
#endif

    VMOffset offsetInDylib((uint64_t)targetValue - (uint64_t)this->dylibMA);
    return ResolvedValue(targetValue, this->dylibBaseAddress + offsetInDylib).vmAddress();
#else
    // In on-disk dylibs, we crack the chained fixups or other fixups
    if ( this->isOnDiskBinary() ) {
        uint64_t runtimeOffset = 0;
        const auto* fixupLoc = (dyld3::MachOFile::ChainedFixupPointerOnDisk*)value.value();
        uint16_t chainedPointerFormat = value.chainedPointerFormat().value();
        if ( chainedPointerFormat == 0 ) {
            assert(dylibMF->hasOpcodeFixups() || (dylibMF->inDyldCache() && !dylibMF->is64()));

            // HACK: This is a binary without chained fixups.  Is it safe to assume this is a rebase?
            uint64_t rebaseVMAddr = (pointerSize == 8) ? fixupLoc->raw64 : fixupLoc->raw32;

            // Assume null VMAddr's means there's no value here
            if ( rebaseVMAddr == 0 )
                return { };

            runtimeOffset = rebaseVMAddr - this->onDiskDylibChainedPointerBaseAddress.rawValue();
        } else {
            bool isRebase = fixupLoc->isRebase(chainedPointerFormat, onDiskDylibChainedPointerBaseAddress.rawValue(), runtimeOffset);
            assert(isRebase);

            if ( pointerSize == 8 ) {
                if ( fixupLoc->raw64 == 0 )
                    return { };
            } else {
                if ( fixupLoc->raw32 == 0 )
                    return { };
            }

            // Assume an offset of 0 means its null.  There's no good reason for an objc class to have an offset of 0 from the cache.
            if ( runtimeOffset == 0 )
                return { };
        }

        VMAddress targetVMAddress = this->onDiskDylibChainedPointerBaseAddress + VMOffset(runtimeOffset);
        return targetVMAddress;
    } else {
        // Cache builder dylib.  These use values in a packed format
        if ( this->pointerSize == 4 ) {
            const void* fixupLocation = value.value();
            if ( Cache32::isNull(fixupLocation) )
                return { };

            CacheVMAddress targetCacheVMAddress = Cache32::getCacheVMAddressFromLocation(sharedCacheBaseAddress, fixupLocation);
            VMAddress targetVMAddress(targetCacheVMAddress.rawValue());
            return targetVMAddress;
        } else {
            const void* fixupLocation = value.value();
            if ( Cache64::isNull(fixupLocation) )
                return { };

            CacheVMAddress targetCacheVMAddress = Cache64::getCacheVMAddressFromLocation(sharedCacheBaseAddress, fixupLocation);
            VMAddress targetVMAddress(targetCacheVMAddress.rawValue());
            return targetVMAddress;
        }
    }
#endif
}


#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
void Visitor::setTargetVMAddress(ResolvedValue& value, CacheVMAddress vmAddr, const dyld3::MachOFile::PointerMetaData& PMD) const
{
    assert(!isOnDiskDylib);

    void* fixupLocation = value.value();
    if ( this->pointerSize == 4 ) {
        Cache32::setLocation(this->sharedCacheBaseAddress, fixupLocation, vmAddr);
    } else {
        uint8_t high8 = 0;
        Cache64::setLocation(this->sharedCacheBaseAddress, fixupLocation, vmAddr, high8,
                             PMD.diversity, PMD.usesAddrDiversity, PMD.key,
                             PMD.authenticated);
    }
}

// Update just the target address in a given location.  Doesn't change any of the other fields
// such as high8 or PointerMetadata
void Visitor::updateTargetVMAddress(ResolvedValue& value, CacheVMAddress vmAddr) const
{
    assert(!isOnDiskDylib);

    void* fixupLocation = value.value();
    if ( this->pointerSize == 4 ) {
        Cache32::updateLocationToCacheVMAddress(this->sharedCacheBaseAddress, fixupLocation, vmAddr);
    } else {
        Cache64::updateLocationToCacheVMAddress(this->sharedCacheBaseAddress, fixupLocation, vmAddr);
    }
}
#endif

#endif // !TARGET_OS_EXCLAVEKIT