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
/* -*- 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@
 */

#ifndef ObjCVisitor_h
#define ObjCVisitor_h

#include "OptimizerObjC.h"
#include "MetadataVisitor.h"
#include "MachOFile.h"
#include "Types.h"

#include <optional>

#if !BUILDING_DYLD
#include <vector>
#endif

namespace dyld3
{
#if BUILDING_DYLD
struct MachOAnalyzer;
#endif

struct MachOFile;
}

//
// MARK: --- ObjCVisitor ---
//

namespace objc_visitor
{

struct Category;
struct Class;
struct IVar;
struct IVarList;
struct Property;
struct PropertyList;
struct Method;
struct MethodList;
struct Protocol;
struct ProtocolList;
struct Visitor;

struct Visitor : metadata_visitor::Visitor
{
    using metadata_visitor::Visitor::Visitor;

    void forEachClass(bool visitMetaClasses, void (^callback)(Class& objcClass, bool isMetaClass, bool& stopClass));
    void forEachClass(void (^callback)(const Class& objcClass, bool& stopClass));
    void forEachClass(void (^callback)(Class& objcClass, bool& stopClass));
    void forEachClassAndMetaClass(void (^callback)(const Class& objcClass, bool& stopClass));
    void forEachClassAndMetaClass(void (^callback)(Class& objcClass, bool& stopClass));
    void forEachCategory(void (^callback)(const Category& objcCategory, bool& stopCategory));
    void forEachProtocol(void (^callback)(const Protocol& objcProtocol, bool& stopProtocol));
    void forEachProtocolReference(void (^callback)(metadata_visitor::ResolvedValue& value));
    void forEachSelectorReference(void (^callback)(metadata_visitor::ResolvedValue& value)) const;
    void forEachSelectorReference(void (^callback)(VMAddress selRefVMAddr, VMAddress selRefTargetVMAddr,
                                                   const char* selectorString)) const;

    void withImageInfo(void (^callback)(const uint32_t version, const uint32_t flags)) const;

#if BUILDING_CACHE_BUILDER_UNIT_TESTS
    // We need everything public to write tests
public:
#else
private:
#endif

    struct DataSection
    {
        DataSection(metadata_visitor::ResolvedValue baseAddress,
                    uint64_t sectSize)
            : sectionBase(baseAddress), sectSize(sectSize)
        {
        }

        metadata_visitor::ResolvedValue sectionBase;
        uint64_t                        sectSize;
    };

    std::optional<Visitor::DataSection> findObjCDataSection(const char *sectionName) const;

    void forEachClass(bool visitMetaClasses, const Visitor::DataSection& classListSection,
                      void (^callback)(Class& objcClass, bool isMetaClass, bool& stopClass));
};

// A wrapped around an individual objc Method in a Method list.
struct Method
{
    enum class Kind
    {
        // All fields are relative.  The name field is a relative offset to a
        // selector reference
        relativeIndirect,

        // All fields are relative.  The name field is an offset from the start
        // of the selector strings buffer.
        relativeDirect,

        // All fields are pointers
        pointer
    };

    Method(Kind kind, metadata_visitor::ResolvedValue methodPos) : kind(kind), methodPos(methodPos) { }

    metadata_visitor::ResolvedValue getNameField(const Visitor& objcVisitor) const;
    metadata_visitor::ResolvedValue getTypesField(const Visitor& objcVisitor) const;
    metadata_visitor::ResolvedValue getIMPField(const Visitor& objcVisitor) const;

    const char* getName(const Visitor& objcVisitor) const;
    const char* getTypes(const Visitor& objcVisitor) const;
    const void* getIMP(const Visitor& objcVisitor) const;

    VMAddress                   getNameVMAddr(const Visitor& objcVisitor) const;
    VMAddress                   getTypesVMAddr(const Visitor& objcVisitor) const;
    std::optional<VMAddress>    getIMPVMAddr(const Visitor& objcVisitor) const;

    // For indirect methods, the indirect offset points to a selector reference.  This returns
    // the vmAddr of that selector reference
    VMAddress getNameSelRefVMAddr(const Visitor& objcVisitor) const;

#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
    // Only used by the cache builder when sorting method lists
    void setName(const Visitor& objcVisitor, VMAddress nameVMAddr);
    void setTypes(const Visitor& objcVisitor, VMAddress typesVMAddr);
    void setIMP(const Visitor& objcVisitor, std::optional<VMAddress> impVMAddr);
#endif

    void convertNameToOffset(const Visitor& objcVisitor, uint32_t nameOffset);

    static uint32_t getSize(bool is64);

private:

    struct relative_method_t {
        int32_t nameOffset;   // SEL*
        int32_t typesOffset;  // const char *
        int32_t impOffset;    // IMP
    };

    struct method32_t {
        uint32_t nameVMAddr;   // SEL
        uint32_t typesVMAddr;  // const char *
        uint32_t impVMAddr;    // IMP
    };

    struct method64_t {
        uint64_t nameVMAddr;   // SEL
        uint64_t typesVMAddr;  // const char *
        uint64_t impVMAddr;    // IMP
    };

    enum class Field
    {
        name,
        types,
        imp
    };

    const void* getFieldPos(const Visitor& objcVisitor, Field field) const;

    const Kind                              kind;
    const metadata_visitor::ResolvedValue   methodPos;
};

// A wrapper around a Method list.  Points to the merhod list in the mach-o buffer, and can be used
// to find the other fields of the method list.
struct MethodList
{
    MethodList(std::optional<metadata_visitor::ResolvedValue> methodListPos) : methodListPos(methodListPos) { }

    // This matches the bits in the objc runtime
    enum : uint32_t {
        methodListIsUniqued             = 0x1,
        methodListIsSorted              = 0x2,

        methodListUsesSelectorOffsets   = 0x40000000,
        methodListIsRelative            = 0x80000000,

        // The size is bits 2 through 16 of the entsize field
        // The low 2 bits are uniqued/sorted as above.  The upper 16-bits
        // are reserved for other flags
        methodListSizeMask              = 0x0000FFFC
    };

    // Note a method list looks like this to libobjc:
    struct method_list_t {
        // The method list stores the size of each element:
        // - pointer based method lists: 3 * uintptr_t
        // - relative method lists: 3 * uint32_t
        uint32_t getMethodSize() const {
            return this->entsize & MethodList::methodListSizeMask;
        }

        uint32_t getMethodCount() const {
            return this->count;
        }

        const uint8_t* methodBase() const {
            return &this->methodArrayBase[0];
        }

        // The shared cache changes selectors to be offsets from a base pointer
        // This returns true if a method list has been converted to this form
        bool usesOffsetsFromSelectorBuffer() const {
            return (entsize & methodListUsesSelectorOffsets) != 0;
        }

        // Returns true if this is a relative method list.  False if its pointer based
        bool usesRelativeOffsets() const {
            return (entsize & methodListIsRelative) != 0;
        }

        void setIsUniqued()
        {
            this->entsize |= methodListIsUniqued;
        }

        void setIsSorted()
        {
            this->entsize |= methodListIsSorted;
        }

        void setUsesOffsetsFromSelectorBuffer()
        {
            this->entsize |= methodListUsesSelectorOffsets;
        }

    private:
        uint32_t    entsize;
        uint32_t    count;
        uint8_t     methodArrayBase[]; // Note this is the start the array method_t[0]
    };

    static_assert(sizeof(method_list_t) == 8, "makeEmptyMethodList expects a buffer without the method array");

    uint32_t numMethods() const;

    bool usesOffsetsFromSelectorBuffer() const;
    bool usesRelativeOffsets() const;

    Method getMethod(const Visitor& objcVisitor, uint32_t i) const;

    void setIsUniqued();
    void setIsSorted();
    void setUsesOffsetsFromSelectorBuffer();

    // Creates an empty shared cache method list (one that is fixed up), and returns
    // its size
    static size_t makeEmptyMethodList(void* buffer);

    // Returns true if this method list is a list of lists, ie, used for attaching categories to
    // classes in the cache builder
    bool isListOfLists() const;

    const void*                 getLocation() const;
    std::optional<VMAddress>    getVMAddress() const;
private:
    const std::optional<metadata_visitor::ResolvedValue> methodListPos;
};

// A wrapper around a Protocol list.  Points to the merhod list in the mach-o buffer, and can be used
// to find the other fields of the method list.
struct ProtocolList
{
    ProtocolList(std::optional<metadata_visitor::ResolvedValue> protocolListPos) : protocolListPos(protocolListPos) { }

    uint64_t        numProtocols(const Visitor& objcVisitor) const;
    Protocol        getProtocol(const Visitor& objcVisitor, uint64_t i) const;

#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
    void            setProtocol(const Visitor& objcVisitor, uint64_t i, VMAddress vmAddr);
#endif

    __attribute__((used))
    void dump(const Visitor& objcVisitor) const;

    // Returns true if this method list is a list of lists, ie, used for attaching categories to
    // classes in the cache builder
    bool isListOfLists() const;

    const void*                 getLocation() const;
    std::optional<VMAddress>    getVMAddress() const;

private:

    // Note a protocol list looks like this to libobjc:
    template<typename PtrTy>
    struct protocol_list_t {
        PtrTy       count;
        PtrTy       list[];
    };

    typedef protocol_list_t<uint32_t> protocol_list32_t;
    typedef protocol_list_t<uint64_t> protocol_list64_t;

    typedef ListOfListsEntry* protocol_list_list;

    metadata_visitor::ResolvedValue getProtocolField(const Visitor& objcVisitor, uint64_t i) const;

    const std::optional<metadata_visitor::ResolvedValue> protocolListPos;
};

// A wrapped around an individual objc IVar in a IVar list.
struct IVar
{
    IVar(metadata_visitor::ResolvedValue ivarPos) : ivarPos(ivarPos) { }

    std::optional<uint32_t> getOffset(const Visitor& objcVisitor) const;
    void                    setOffset(const Visitor& objcVisitor, uint32_t offset) const;
    const char*             getName(const Visitor& objcVisitor) const;
    uint32_t                getAlignment(const Visitor& objcVisitor) const;
    bool                    elided(const Visitor& objcVisitor) const;   // size == 0

private:

    template<typename PtrTy>
    struct ivar_t {
        PtrTy       offsetVMAddr;  // uint32_t*
        PtrTy       nameVMAddr;    // const char *
        PtrTy       typeVMAddr;    // const char *
        uint32_t    alignment;
        uint32_t    size;
    };

    typedef ivar_t<uint32_t> ivar32_t;
    typedef ivar_t<uint64_t> ivar64_t;

    enum class Field
    {
        offset,
        name,
        type,
        alignment,
        size
    };

    const void* getFieldPos(const Visitor& objcVisitor, Field field) const;

    const metadata_visitor::ResolvedValue ivarPos;
};

// A wrapper around an IVar list.  Points to the ivar list in the mach-o buffer, and can be used
// to find the other fields of the ivar list.
struct IVarList
{
    IVarList(std::optional<metadata_visitor::ResolvedValue> ivarListPos) : ivarListPos(ivarListPos) { }

    // Note a ivar list looks like this to libobjc:
    struct ivar_list_t {

        uint32_t getElementSize() const
        {
            return this->entsize;
        }

        uint32_t getCount() const
        {
            return this->count;
        }

        const uint8_t* ivarBase() const {
            return &this->ivarArrayBase[0];
        }

    private:
        uint32_t    entsize;
        uint32_t    count;
        uint8_t     ivarArrayBase[]; // Note this is the start the array ivar_t[0]
    };

    uint32_t numIVars() const;
    IVar    getIVar(const Visitor& objcVisitor, uint32_t i) const;

#if BUILDING_CACHE_BUILDER_UNIT_TESTS
    const void*                 getLocation() const;
    std::optional<VMAddress>    getVMAddress() const;
#endif

private:
    const std::optional<metadata_visitor::ResolvedValue> ivarListPos;
};

// A wrapped around an individual objc Property in a Property list
struct Property
{
    Property(metadata_visitor::ResolvedValue propertyPos) : propertyPos(propertyPos) { }

    const char*             getName(const Visitor& objcVisitor) const;
    const char*             getAttributes(const Visitor& objcVisitor) const;

private:

    template<typename PtrTy>
    struct property_t {
        PtrTy       nameVMAddr;       // const char *
        PtrTy       attributesVMAddr; // const char *
    };

    typedef property_t<uint32_t> property32_t;
    typedef property_t<uint64_t> property64_t;

    enum class Field
    {
        name,
        attributes
    };

    const void* getFieldPos(const Visitor& objcVisitor, Field field) const;

    const metadata_visitor::ResolvedValue propertyPos;
};

// A wrapper around an Property list.  Points to the property list in the mach-o buffer, and can be used
// to find the other fields of the property list.
struct PropertyList
{
    PropertyList(std::optional<metadata_visitor::ResolvedValue> propertyListPos) : propertyListPos(propertyListPos) { }

    // Note a property list looks like this to libobjc:
    struct property_list_t {

        uint32_t getElementSize() const
        {
            return this->entsize;
        }

        uint32_t getCount() const
        {
            return this->count;
        }

        const uint8_t* propertyBase() const {
            return &this->propertyArrayBase[0];
        }

    private:
        uint32_t    entsize;
        uint32_t    count;
        uint8_t     propertyArrayBase[]; // Note this is the start the array property_t[0]
    };

    uint32_t numProperties() const;
    Property getProperty(const Visitor& objcVisitor, uint32_t i) const;

    // Returns true if this method list is a list of lists, ie, used for attaching categories to
    // classes in the cache builder
    bool isListOfLists() const;

    const void*                 getLocation() const;
    std::optional<VMAddress>    getVMAddress() const;

private:
    const std::optional<metadata_visitor::ResolvedValue> propertyListPos;
};

struct ClassData
{
    ClassData(metadata_visitor::ResolvedValue classDataPos) : classDataPos(classDataPos) { }

    // Data is a pointer to the read only data for the class, ie, one of these according to objc:
    template<typename PtrTy>
    struct data_t {
        uint32_t flags;
        uint32_t instanceStart;
        // Note there is 4-bytes of alignment padding between instanceSize and ivarLayout
        // on 64-bit archs, but no padding on 32-bit archs.
        // This union is a way to model that.
        union {
            uint32_t    instanceSize;
            PtrTy       pad;
        } instanceSize;
        PtrTy ivarLayoutVMAddr;
        PtrTy nameVMAddr;
        PtrTy baseMethodsVMAddr;
        PtrTy baseProtocolsVMAddr;
        PtrTy ivarsVMAddr;
        PtrTy weakIvarLayoutVMAddr;
        PtrTy basePropertiesVMAddr;
    };

    typedef data_t<uint32_t> data32_t;
    typedef data_t<uint64_t> data64_t;

    enum class Field
    {
        flags,
        instanceStart,
        instanceSize,
        ivarLayout,
        name,
        baseMethods,
        baseProtocols,
        ivars,
        weakIvarLayout,
        baseProperties
    };

    const void*                         getFieldPos(const Visitor& objcVisitor, Field field) const;
    metadata_visitor::ResolvedValue     getField(const Visitor& objcVisitor, Field field) const;

    const void* getLocation() const;
    VMAddress   getVMAddress() const;

private:
    metadata_visitor::ResolvedValue classDataPos;
};

// A wrapper around a Class.  Points to the class in the mach-o buffer, and can be used
// to find the other fields of the class.
struct Class
{
    Class(metadata_visitor::ResolvedValue classPos, bool isMetaClass, bool isPatchable)
        : isMetaClass(isMetaClass), isPatchable(isPatchable), classPos(classPos) { }

    metadata_visitor::ResolvedValue                 getISA(const Visitor& objcVisitor, bool& isPatchableClass) const;
    std::optional<metadata_visitor::ResolvedValue>  getSuperclass(const Visitor& objcVisitor) const;
    metadata_visitor::ResolvedValue                 getMethodCache(const Visitor& objcVisitor) const;
    std::optional<metadata_visitor::ResolvedValue>  getMethodCacheProperties(const Visitor& objcVisitor) const;
    ClassData                                       getClassData(const Visitor& objcVisitor) const;
    VMAddress                                       getClassDataVMAddr(const Visitor& objcVisitor) const;
    std::optional<uint32_t>                         swiftClassFlags(const Visitor& objcVisitor) const;

    std::optional<VMAddress>                    getSuperclassVMAddr(const Visitor& objcVisitor) const;
    std::optional<VMAddress>                    getMethodCachePropertiesVMAddr(const Visitor& objcVisitor) const;
    void                                        setMethodCachePropertiesVMAddr(const Visitor& objcVisitor, VMAddress vmAddr);

    // Returns the superclass field itself, ie the value of &this->superclass
    metadata_visitor::ResolvedValue             getSuperclassField(const Visitor& objcVisitor) const;

    // Returns the vtable field itself, ie the value of &this->vtable
    metadata_visitor::ResolvedValue             getMethodCachePropertiesField(const Visitor& objcVisitor) const;

    // Returns the data field itself, ie the value of &this->data
    metadata_visitor::ResolvedValue             getDataField(const Visitor& objcVisitor) const;

    // Gets the raw fixup and chained pointer format for the super class fixup
    void withSuperclass(const Visitor& objcVisitor,
                        void (^handler)(const dyld3::MachOFile::ChainedFixupPointerOnDisk* fixup, uint16_t pointerFormat)) const;

    // Note this is taken from the objc runtime
    bool isSwiftLegacy(const Visitor& objcVisitor) const;
    bool isSwiftStable(const Visitor& objcVisitor) const;
    bool isSwift(const Visitor& objcVisitor) const;
    bool isUnfixedBackwardDeployingStableSwift(const Visitor& objcVisitor) const;

    bool                                  isRootClass(const Visitor& objcVisitor) const;
    uint32_t                              getInstanceStart(const Visitor& objcVisitor) const;
    void                                  setInstanceStart(const Visitor& objcVisitor, uint32_t value) const;
    uint32_t                              getInstanceSize(const Visitor& objcVisitor) const;
    void                                  setInstanceSize(const Visitor& objcVisitor, uint32_t value) const;
    const char*                           getName(const Visitor& objcVisitor) const;
    VMAddress                             getNameVMAddr(const Visitor& objcVisitor) const;
    MethodList                            getBaseMethods(const Visitor& objcVisitor) const;
    metadata_visitor::ResolvedValue       setBaseMethodsVMAddr(const Visitor& objcVisitor, VMAddress vmAddr,
                                             const dyld3::MachOFile::PointerMetaData& PMD);
    ProtocolList                          getBaseProtocols(const Visitor& objcVisitor) const;
    metadata_visitor::ResolvedValue       setBaseProtocolsVMAddr(const Visitor& objcVisitor, VMAddress vmAddr);
    IVarList                              getIVars(const Visitor& objcVisitor) const;
    PropertyList                          getBaseProperties(const Visitor& objcVisitor) const;
    metadata_visitor::ResolvedValue       setBasePropertiesVMAddr(const Visitor& objcVisitor, VMAddress vmAddr);

    const void* getLocation() const;
    VMAddress   getVMAddress() const;

    const bool          isMetaClass;
    const bool          isPatchable;

#if BUILDING_CACHE_BUILDER_UNIT_TESTS
    // We need everything public to write tests
public:
#else
private:
#endif

    // Note a class looks like this to libobjc:
    template<typename PtrTy>
    struct class_t {
        PtrTy isaVMAddr;
        PtrTy superclassVMAddr;
        PtrTy methodCacheBuckets;
        PtrTy methodCacheProperties; // aka vtable
        PtrTy dataVMAddrAndFastFlags;

        // This field is only present if this is a Swift object, ie, has the Swift
        // fast bits set
        uint32_t swiftClassFlags;
    };

    typedef class_t<uint32_t> class32_t;
    typedef class_t<uint64_t> class64_t;

    enum : uint32_t {
        FAST_DATA_MASK32 = 0xfffffffcUL
    };
    enum : uint64_t {
        FAST_DATA_MASK64 = 0x00007ffffffffff8ULL
    };

    // These are embedded in the Mach-O itself by the compiler
    enum FastDataBits {
        FAST_IS_SWIFT_LEGACY    = 0x1,
        FAST_IS_SWIFT_STABLE    = 0x2
    };

    // These are embedded by the Swift compiler in the swiftClassFlags field
    enum SwiftClassFlags {
        isSwiftPreStableABI     = 0x1
    };

    enum class Field
    {
        isa,
        superclass,
        methodCacheBuckets,
        methodCacheProperties,
        data,
        swiftClassFlags
    };

    const void* getFieldPos(const Visitor& objcVisitor, Field field) const;

    const metadata_visitor::ResolvedValue classPos;
};

// A wrapper around a Category.  Points to the category in the mach-o buffer, and can be used
// to find the other fields of the category.
struct Category
{
    Category(metadata_visitor::ResolvedValue categoryPos) : categoryPos(categoryPos) { }

    const char*         getName(const Visitor& objcVisitor) const;
    const void*         getLocation() const;
    VMAddress           getVMAddress() const;
    VMAddress           getNameVMAddr(const Visitor& objcVisitor) const;
    MethodList          getInstanceMethods(const Visitor& objcVisitor) const;
    MethodList          getClassMethods(const Visitor& objcVisitor) const;
    ProtocolList        getProtocols(const Visitor& objcVisitor) const;
    PropertyList        getInstanceProperties(const Visitor& objcVisitor) const;
    PropertyList        getClassProperties(const Visitor& objcVisitor) const;

    // Gets the raw fixup and chained pointer format for the class fixup
    void withClass(const Visitor& objcVisitor,
                   void (^handler)(const dyld3::MachOFile::ChainedFixupPointerOnDisk* fixup, uint16_t pointerFormat)) const;

    static uint32_t getSize(bool is64);

private:

    // Note a category looks like this to libobjc:
    template<typename PtrTy>
    struct category_t {
        PtrTy nameVMAddr;
        PtrTy clsVMAddr;
        PtrTy instanceMethodsVMAddr;
        PtrTy classMethodsVMAddr;
        PtrTy protocolsVMAddr;
        PtrTy instancePropertiesVMAddr;
        PtrTy classPropertiesVMAddr;
    };

    typedef category_t<uint32_t> category32_t;
    typedef category_t<uint64_t> category64_t;

    enum class Field
    {
        name,
        cls,
        instanceMethods,
        classMethods,
        protocols,
        instanceProperties,
        classProperties
    };

    const void* getFieldPos(const Visitor& objcVisitor, Field field) const;

    const metadata_visitor::ResolvedValue categoryPos;
};

// A wrapper around a Protocol.  Points to the protocol in the mach-o buffer, and can be used
// to find the other fields of the protocol.
struct Protocol
{
    Protocol(metadata_visitor::ResolvedValue protocolPos) : protocolPos(protocolPos) { }

    std::optional<VMAddress>                    getISAVMAddr(const Visitor& objcVisitor) const;
    const char*                                 getName(const Visitor& objcVisitor) const;
    VMAddress                                   getNameVMAddr(const Visitor& objcVisitor) const;
    ProtocolList                                getProtocols(const Visitor& objcVisitor) const;
    MethodList                                  getInstanceMethods(const Visitor& objcVisitor) const;
    MethodList                                  getClassMethods(const Visitor& objcVisitor) const;
    MethodList                                  getOptionalInstanceMethods(const Visitor& objcVisitor) const;
    MethodList                                  getOptionalClassMethods(const Visitor& objcVisitor) const;
    uint32_t                                        getSize(const Visitor& objcVisitor) const;
    void                                            setSize(const Visitor& objcVisitor, uint32_t size);
    void                                            setFixedUp(const Visitor& objcVisitor);
    void                                            setIsCanonical(const Visitor& objcVisitor);
    std::optional<metadata_visitor::ResolvedValue>  getExtendedMethodTypes(const Visitor& objcVisitor) const;
    std::optional<const char*>                  getDemangledName(const Visitor& objcVisitor) const;
    void                                        setDemangledName(const Visitor& objcVisitor, VMAddress nameVMAddr);

#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
    void                                        setISA(const Visitor& objcVisitor, VMAddress vmAddr,
                                                       const dyld3::MachOFile::PointerMetaData& PMD);

    // Used by the cache builder to construct fixup chains.  We need to find all fields of the protocol which have values
    void                                        addFixups(const Visitor& objcVisitor, std::vector<void*>& fixups) const;
    std::optional<uint16_t>                     chainedPointerFormat() const;
#endif

    const void* getLocation() const;
    VMAddress   getVMAddress() const;

    static uint32_t getSize(bool is64);

#if BUILDING_CACHE_BUILDER_UNIT_TESTS
    // We need everything public to write tests
public:
#else
private:
#endif

    // Note a protocol looks like this to libobjc:
    template<typename PtrTy>
    struct protocol_t {
        PtrTy       isaVMAddr;
        PtrTy       nameVMAddr;
        PtrTy       protocolsVMAddr;
        PtrTy       instanceMethodsVMAddr;
        PtrTy       classMethodsVMAddr;
        PtrTy       optionalInstanceMethodsVMAddr;
        PtrTy       optionalClassMethodsVMAddr;
        PtrTy       instancePropertiesVMAddr;
        uint32_t    size;
        uint32_t    flags;
        // Fields below this point are not always present on disk.
        PtrTy       extendedMethodTypesVMAddr;
        PtrTy       demangledNameVMAddr;
        PtrTy       classPropertiesVMAddr;
    };

    typedef protocol_t<uint32_t> protocol32_t;
    typedef protocol_t<uint64_t> protocol64_t;

    enum class Field
    {
        isa,
        name,
        protocols,
        instanceMethods,
        classMethods,
        optionalInstanceMethods,
        optionalClassMethods,
        instanceProperties,
        size,
        flags,
        // Fields below this point are not always present on disk.
        extendedMethodTypes,
        demangledName,
        classProperties,
    };

    const void* getFieldPos(const Visitor& objcVisitor, Field field) const;

    const metadata_visitor::ResolvedValue protocolPos;
};

} // namespace objc_visitor

#endif // ObjCVisitor_h