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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
*
* Copyright (c) 2017 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 Chunk_hpp
#define Chunk_hpp

#include "ASLRTracker.h"
#include "Types.h"
#include "fixup-chains.h"

#include <mach-o/nlist.h>
#include <unordered_set>
#include <string>
#include <vector>

namespace dyld3
{
struct MachOFile;
}

namespace cache_builder
{

struct BuilderConfig;
struct InputFile;

struct DylibSegmentChunk;
struct LinkeditDataChunk;
struct CodeSignatureChunk;
struct DynamicConfigChunk;
struct SlidChunk;
struct AlignChunk;
struct StubsChunk;
struct UniquedGOTsChunk;

namespace impl
{
    enum class Alignment;
};

// The smallest atom of data within a SubCache.  A Chunk is a contiguous region of memory
// which may point to data from dylibs, cache header, optimization results, etc
struct Chunk
{
    enum class Kind
    {
        // Contains the dyld_cache_header value
        // This is the HeaderChunk below
        cacheHeader,

        // Contains the slide info for a single one of the RW regions.
        // We may have multiple of these in a subCache
        slideInfo,

        // Contains the code signature
        codeSignature,

        // The SwiftOptimizationHeader value
        swiftOptsHeader,

        // A buffer to hold a swift hash table (type, metadata, foreign)
        swiftConformanceHashTable,

        // A buffer to hold a pointer hash table
        pointerHashTable,

        // A buffer to hold the trie for the cache dylib names
        cacheDylibsTrie,

        // A buffer to hold the patch table for the cache dylibs
        cachePatchTable,

        // A buffer to hold the list of function variants to patch at launch
        cacheFunctionVariantsPatchTable,

         // A buffer to hold the PrebuiltLoaderSet for the cache dylibs
        dylibPrebuiltLoaders,

        // A buffer to hold the PrebuiltLoaderSet for the executables
        executablePrebuiltLoaders,

        // A buffer to hold the trie for the cache dylib names
        cacheExecutablesTrie,

        // A buffer to hold the prewarming pages
        prewarmingData,

        // In the .symbols file, this is the payload
        unmappedSymbols,

        // Uniqued GOTs
        uniquedGOTs,

        // In a universal cache, we adds stubs every N MB.  This is the stubs for a given dylib
        stubs,

        // __TEXT copied from the source dylib
        dylibText,

        // __DATA_CONST copied from the source dylib, and the dylib is eligible for RO __DATA_CONST
        dylibDataConst,

        // __TPRO_CONST segments.  These are placed next to the very start of DATA/AUTH as that puts them
        // next to the dirty data inside the data/auth regions
        tproDataConst,

        // The objc HeaderInfoRW array.  It is before __DATA_DIRTY so that we sort it near
        // the __DATA_DIRTY from libobjc
        objcHeaderInfoRW,

        // A buffer to hold the canonical protocols. This is adjacent to HeaderInfoRW so that the
        // __OBJC_RW segment can cover them
        objcCanonicalProtocols,

        // __DATA_DIRTY copied from the source dylib
        dylibDataDirty,

        // __DATA copied from the source dylib
        dylibData,

        // __AUTH copied from the source dylib
        dylibAuth,

        // __AUTH_CONST copied from the source dylib, and the dylib is eligible for RO __DATA_CONST
        dylibAuthConst,

        // Read-only segment copied from the source dylib
        dylibReadOnly,

        // __LINKEDIT copied from the source dylib
        dylibLinkedit,

        // Individual pieces of LINKEDIT copied from input files
        linkeditSymbolNList,
        linkeditSymbolStrings,
        linkeditIndirectSymbols,
        linkeditFunctionStarts,
        linkeditDataInCode,
        linkeditExportTrie,
        linkeditFunctionVariants,

        // Optimized symbols nlist/strings.
        // Note this must be sorted after the above LINKEDIT entries so that
        // offsets from the dylib LINKEDIT work
        optimizedSymbolNList,
        optimizedSymbolStrings,

        // ObjC Optimizations.  These must be after dylibText so that offsets from the libobjc __TEXT
        // are positive if pointing to OBJC_RO

        // The ObjCOptimizationHeader value
        objcOptsHeader,

        // The objc HeaderInfoRO array
        objcHeaderInfoRO,

        // A copy of all the objc_imageInfo for all the dylibs
        objcImageInfo,

        // A contiguous buffer of objc strings.  There may be multiple of these, eg, selectors, class names, etc.
        objcStrings,

        // A buffer to hold the selectors hash table
        objcSelectorsHashTable,

        // A buffer to hold the classes hash table
        objcClassesHashTable,

        // A buffer to hold the protocols hash table
        objcProtocolsHashTable,

        // A buffer to hold the imp caches
        objcIMPCaches,

        // A buffer to hold the pre-attached categories metadata. This is adjacent to objcIMPCaches so that the
        // __OBJC_RO segment can cover them
        objcPreAttachedCategories,

        // This is a placeholder for empty address space that can be used at runtime
        dynamicConfig,

        // just align the current address to some value. This is only used to align before/after x86_64 TPRO right now,
        // to ensure that we have whole pages to mprotect at runtime
        align,
    };

protected:
    Kind          kind;

public:
    // Where are we in the subCache buffer (set by computeSubCacheFileLayout() and allocateSubCacheBuffers())
    CacheFileOffset     subCacheFileOffset;
    CacheFileSize       subCacheFileSize;
    uint8_t*            subCacheBuffer      = nullptr;

    // Where are we in the cache, ie, in memory layout.  Set by computeSubCacheFileLayout().
    CacheVMAddress      cacheVMAddress;
    CacheVMSize         cacheVMSize;

private:
    uint64_t            minAlignment = 1;

public:
    Chunk(Kind kind, uint64_t minAlignment);
    Chunk(Kind kind, impl::Alignment minAlignment);
    virtual ~Chunk();
    Chunk(const Chunk&) = delete;
    Chunk(Chunk&&) = default;
    Chunk& operator=(const Chunk&) = delete;
    Chunk& operator=(Chunk&&) = default;

    uint32_t sortOrder() const;
    uint64_t alignment() const;

    // Abstract methods
    virtual const char* name() const = 0;

    virtual bool isZeroFill() const;
    virtual SlidChunk* isSlidChunk();
    virtual const AlignChunk* isAlignChunk() const;
    virtual const DylibSegmentChunk* isDylibSegmentChunk() const;
    virtual const LinkeditDataChunk* isLinkeditDataChunk() const;
    virtual StubsChunk* isStubsChunk();
    virtual UniquedGOTsChunk* isUniquedGOTsChunk();
    virtual const DylibSegmentChunk* isTPROChunk() const;

private:
    __attribute__((used))
    virtual void dump() const;
};

struct CacheHeaderChunk : Chunk
{
public:
    CacheHeaderChunk();
    virtual ~CacheHeaderChunk();
    CacheHeaderChunk(const CacheHeaderChunk&) = delete;
    CacheHeaderChunk(CacheHeaderChunk&&) = delete;
    CacheHeaderChunk& operator=(const CacheHeaderChunk&) = delete;
    CacheHeaderChunk& operator=(CacheHeaderChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct SlideInfoChunk : Chunk
{
public:
    SlideInfoChunk();
    virtual ~SlideInfoChunk();
    SlideInfoChunk(const SlideInfoChunk&) = delete;
    SlideInfoChunk(SlideInfoChunk&&) = delete;
    SlideInfoChunk& operator=(const SlideInfoChunk&) = delete;
    SlideInfoChunk& operator=(SlideInfoChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

    // We allocate space for N-bytes per page, but for the V1 format, we may use less
    // This tracks the size we actually use, which is what we'll then wire to the kernel
    CacheFileSize usedFileSize;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct CodeSignatureChunk : Chunk
{

public:
    CodeSignatureChunk();
    virtual ~CodeSignatureChunk();
    CodeSignatureChunk(const CodeSignatureChunk&) = delete;
    CodeSignatureChunk(CodeSignatureChunk&&) = delete;
    CodeSignatureChunk& operator=(const CodeSignatureChunk&) = delete;
    CodeSignatureChunk& operator=(CodeSignatureChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

// A Chunk which may contain slid values, ie, rebases/binds
struct SlidChunk : Chunk
{
    ASLR_Tracker tracker;

public:
    SlidChunk(Kind kind, uint64_t minAlignment);
    virtual ~SlidChunk();
    SlidChunk(const SlidChunk&) = delete;
    SlidChunk(SlidChunk&&) = default;
    SlidChunk& operator=(const SlidChunk&) = delete;
    SlidChunk& operator=(SlidChunk&&) = default;

    // Virtual methods (will be overridden by subclasses)
    //const char* name() const override final;
    SlidChunk* isSlidChunk() override final;

private:
    // Note, not final as SlidChunk's are subclassesed, eg, by SegmentInfo
    __attribute__((used))
    virtual void dump() const override;
};

struct ObjCOptsHeaderChunk : Chunk
{
public:
    ObjCOptsHeaderChunk();
    virtual ~ObjCOptsHeaderChunk();
    ObjCOptsHeaderChunk(const ObjCOptsHeaderChunk&) = delete;
    ObjCOptsHeaderChunk(ObjCOptsHeaderChunk&&) = delete;
    ObjCOptsHeaderChunk& operator=(const ObjCOptsHeaderChunk&) = delete;
    ObjCOptsHeaderChunk& operator=(ObjCOptsHeaderChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct ObjCImageInfoChunk : Chunk
{
public:
    ObjCImageInfoChunk();
    virtual ~ObjCImageInfoChunk();
    ObjCImageInfoChunk(const ObjCImageInfoChunk&) = delete;
    ObjCImageInfoChunk(ObjCImageInfoChunk&&) = delete;
    ObjCImageInfoChunk& operator=(const ObjCImageInfoChunk&) = delete;
    ObjCImageInfoChunk& operator=(ObjCImageInfoChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct ObjCHeaderInfoReadOnlyChunk : Chunk
{
public:
    ObjCHeaderInfoReadOnlyChunk();
    virtual ~ObjCHeaderInfoReadOnlyChunk();
    ObjCHeaderInfoReadOnlyChunk(const ObjCHeaderInfoReadOnlyChunk&) = delete;
    ObjCHeaderInfoReadOnlyChunk(ObjCHeaderInfoReadOnlyChunk&&) = delete;
    ObjCHeaderInfoReadOnlyChunk& operator=(const ObjCHeaderInfoReadOnlyChunk&) = delete;
    ObjCHeaderInfoReadOnlyChunk& operator=(ObjCHeaderInfoReadOnlyChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct ObjCHeaderInfoReadWriteChunk : SlidChunk
{
public:
    ObjCHeaderInfoReadWriteChunk();
    virtual ~ObjCHeaderInfoReadWriteChunk();
    ObjCHeaderInfoReadWriteChunk(const ObjCHeaderInfoReadWriteChunk&) = delete;
    ObjCHeaderInfoReadWriteChunk(ObjCHeaderInfoReadWriteChunk&&) = delete;
    ObjCHeaderInfoReadWriteChunk& operator=(const ObjCHeaderInfoReadWriteChunk&) = delete;
    ObjCHeaderInfoReadWriteChunk& operator=(ObjCHeaderInfoReadWriteChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct ObjCStringsChunk : Chunk
{
public:
    ObjCStringsChunk();
    virtual ~ObjCStringsChunk();
    ObjCStringsChunk(const ObjCStringsChunk&) = delete;
    ObjCStringsChunk(ObjCStringsChunk&&) = delete;
    ObjCStringsChunk& operator=(const ObjCStringsChunk&) = delete;
    ObjCStringsChunk& operator=(ObjCStringsChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct ObjCSelectorHashTableChunk : Chunk
{
public:
    ObjCSelectorHashTableChunk();
    virtual ~ObjCSelectorHashTableChunk();
    ObjCSelectorHashTableChunk(const ObjCSelectorHashTableChunk&) = delete;
    ObjCSelectorHashTableChunk(ObjCSelectorHashTableChunk&&) = delete;
    ObjCSelectorHashTableChunk& operator=(const ObjCSelectorHashTableChunk&) = delete;
    ObjCSelectorHashTableChunk& operator=(ObjCSelectorHashTableChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct ObjCClassHashTableChunk : Chunk
{
public:
    ObjCClassHashTableChunk();
    virtual ~ObjCClassHashTableChunk();
    ObjCClassHashTableChunk(const ObjCClassHashTableChunk&) = delete;
    ObjCClassHashTableChunk(ObjCClassHashTableChunk&&) = delete;
    ObjCClassHashTableChunk& operator=(const ObjCClassHashTableChunk&) = delete;
    ObjCClassHashTableChunk& operator=(ObjCClassHashTableChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct ObjCProtocolHashTableChunk : Chunk
{
public:
    ObjCProtocolHashTableChunk();
    virtual ~ObjCProtocolHashTableChunk();
    ObjCProtocolHashTableChunk(const ObjCProtocolHashTableChunk&) = delete;
    ObjCProtocolHashTableChunk(ObjCProtocolHashTableChunk&&) = delete;
    ObjCProtocolHashTableChunk& operator=(const ObjCProtocolHashTableChunk&) = delete;
    ObjCProtocolHashTableChunk& operator=(ObjCProtocolHashTableChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct ObjCCanonicalProtocolsChunk : SlidChunk
{
public:
    ObjCCanonicalProtocolsChunk();
    virtual ~ObjCCanonicalProtocolsChunk();
    ObjCCanonicalProtocolsChunk(const ObjCCanonicalProtocolsChunk&) = delete;
    ObjCCanonicalProtocolsChunk(ObjCCanonicalProtocolsChunk&&) = delete;
    ObjCCanonicalProtocolsChunk& operator=(const ObjCCanonicalProtocolsChunk&) = delete;
    ObjCCanonicalProtocolsChunk& operator=(ObjCCanonicalProtocolsChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};


struct ObjCPreAttachedCategoriesChunk : SlidChunk
{
public:
    ObjCPreAttachedCategoriesChunk();
    virtual ~ObjCPreAttachedCategoriesChunk();
    ObjCPreAttachedCategoriesChunk(const ObjCPreAttachedCategoriesChunk&) = delete;
    ObjCPreAttachedCategoriesChunk(ObjCPreAttachedCategoriesChunk&&) = delete;
    ObjCPreAttachedCategoriesChunk& operator=(const ObjCPreAttachedCategoriesChunk&) = delete;
    ObjCPreAttachedCategoriesChunk& operator=(ObjCPreAttachedCategoriesChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};


struct ObjCIMPCachesChunk : Chunk
{
public:
    ObjCIMPCachesChunk();
    virtual ~ObjCIMPCachesChunk();
    ObjCIMPCachesChunk(const ObjCIMPCachesChunk&) = delete;
    ObjCIMPCachesChunk(ObjCIMPCachesChunk&&) = delete;
    ObjCIMPCachesChunk& operator=(const ObjCIMPCachesChunk&) = delete;
    ObjCIMPCachesChunk& operator=(ObjCIMPCachesChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct SwiftOptsHeaderChunk : Chunk
{
public:
    SwiftOptsHeaderChunk();
    virtual ~SwiftOptsHeaderChunk();
    SwiftOptsHeaderChunk(const SwiftOptsHeaderChunk&) = delete;
    SwiftOptsHeaderChunk(SwiftOptsHeaderChunk&&) = delete;
    SwiftOptsHeaderChunk& operator=(const SwiftOptsHeaderChunk&) = delete;
    SwiftOptsHeaderChunk& operator=(SwiftOptsHeaderChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct SwiftProtocolConformancesHashTableChunk : Chunk
{
public:
    SwiftProtocolConformancesHashTableChunk();
    virtual ~SwiftProtocolConformancesHashTableChunk();
    SwiftProtocolConformancesHashTableChunk(const SwiftProtocolConformancesHashTableChunk&) = delete;
    SwiftProtocolConformancesHashTableChunk(SwiftProtocolConformancesHashTableChunk&&) = delete;
    SwiftProtocolConformancesHashTableChunk& operator=(const SwiftProtocolConformancesHashTableChunk&) = delete;
    SwiftProtocolConformancesHashTableChunk& operator=(SwiftProtocolConformancesHashTableChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct PointerHashTableChunk : Chunk
{
public:
    PointerHashTableChunk();
    virtual ~PointerHashTableChunk();
    PointerHashTableChunk(const PointerHashTableChunk&) = delete;
    PointerHashTableChunk(PointerHashTableChunk&&) = delete;
    PointerHashTableChunk& operator=(const PointerHashTableChunk&) = delete;
    PointerHashTableChunk& operator=(PointerHashTableChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct CacheTrieChunk : Chunk
{
public:
    CacheTrieChunk(Kind kind);
    virtual ~CacheTrieChunk();
    CacheTrieChunk(const CacheTrieChunk&) = delete;
    CacheTrieChunk(CacheTrieChunk&&) = delete;
    CacheTrieChunk& operator=(const CacheTrieChunk&) = delete;
    CacheTrieChunk& operator=(CacheTrieChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct PatchTableChunk : Chunk
{
public:
    PatchTableChunk();
    virtual ~PatchTableChunk();
    PatchTableChunk(const PatchTableChunk&) = delete;
    PatchTableChunk(PatchTableChunk&&) = delete;
    PatchTableChunk& operator=(const PatchTableChunk&) = delete;
    PatchTableChunk& operator=(PatchTableChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};


struct FunctionVariantsPatchTableChunk : Chunk
{
public:
    FunctionVariantsPatchTableChunk();
    virtual ~FunctionVariantsPatchTableChunk();
    FunctionVariantsPatchTableChunk(const FunctionVariantsPatchTableChunk&) = delete;
    FunctionVariantsPatchTableChunk(FunctionVariantsPatchTableChunk&&) = delete;
    FunctionVariantsPatchTableChunk& operator=(const FunctionVariantsPatchTableChunk&) = delete;
    FunctionVariantsPatchTableChunk& operator=(FunctionVariantsPatchTableChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};


struct PrebuiltLoaderChunk : Chunk
{
public:
    PrebuiltLoaderChunk(Kind kind);
    virtual ~PrebuiltLoaderChunk();
    PrebuiltLoaderChunk(const PrebuiltLoaderChunk&) = delete;
    PrebuiltLoaderChunk(PrebuiltLoaderChunk&&) = delete;
    PrebuiltLoaderChunk& operator=(const PrebuiltLoaderChunk&) = delete;
    PrebuiltLoaderChunk& operator=(PrebuiltLoaderChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

// In the .symbols file, this dyld_cache_local_symbols_info and dyld_cache_local_symbols_entry_64
// The rest is the nlist and symbol strings chunks
struct UnmappedSymbolsChunk : Chunk
{
public:
    UnmappedSymbolsChunk();
    virtual ~UnmappedSymbolsChunk();
    UnmappedSymbolsChunk(const UnmappedSymbolsChunk&) = delete;
    UnmappedSymbolsChunk(UnmappedSymbolsChunk&&) = default;
    UnmappedSymbolsChunk& operator=(const UnmappedSymbolsChunk&) = delete;
    UnmappedSymbolsChunk& operator=(UnmappedSymbolsChunk&&) = default;

    // Virtual methods
    const char* name() const override final;

    // FIXME: We really don't want to make kind public
    using Chunk::kind;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct DylibSegmentChunk : SlidChunk
{
    std::string_view        segmentName; // Points to the LC_SEGMENT in the input dylib
    const InputFile*        inputFile;

    // Where are we in the input file (set by categorizeDylibSegments())
    InputDylibFileOffset    inputFileOffset;
    InputDylibFileSize      inputFileSize;
    InputDylibVMAddress     inputVMAddress;
    InputDylibVMSize        inputVMSize;

public:
    DylibSegmentChunk(Kind kind, uint64_t minAlignment);
    virtual ~DylibSegmentChunk();
    DylibSegmentChunk(const DylibSegmentChunk&) = delete;
    DylibSegmentChunk(DylibSegmentChunk&&) = default;
    DylibSegmentChunk& operator=(const DylibSegmentChunk&) = delete;
    DylibSegmentChunk& operator=(DylibSegmentChunk&&) = default;

    // Virtual methods
    const char* name() const override final;
    const DylibSegmentChunk* isDylibSegmentChunk() const override final;
    const DylibSegmentChunk* isTPROChunk() const override final;

    // FIXME: We really don't want to make kind public
    using Chunk::kind;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

// An individual piece of LINKEDIT, eg, an export trie, or function starts
struct LinkeditDataChunk : Chunk
{
    const InputFile*        inputFile;

    // Where are we in the input file (set by categorizeDylibLinkedit())
    InputDylibFileOffset    inputFileOffset;
    InputDylibFileSize      inputFileSize;

public:
    LinkeditDataChunk(Kind kind, uint64_t minAlignment);
    virtual ~LinkeditDataChunk();
    LinkeditDataChunk(const LinkeditDataChunk&) = delete;
    LinkeditDataChunk(LinkeditDataChunk&&) = default;
    LinkeditDataChunk& operator=(const LinkeditDataChunk&) = delete;
    LinkeditDataChunk& operator=(LinkeditDataChunk&&) = default;

    // Virtual methods
    const char* name() const override final;
    const LinkeditDataChunk* isLinkeditDataChunk() const override final;

    bool isIndirectSymbols() const;
    bool isNList() const;
    bool isNSymbolStrings() const;
    bool isFunctionVariantsTable() const;

    // FIXME: We really don't want to make kind public
    using Chunk::kind;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

// The optimizd nlists (local, global, undef) for a given dylib
struct NListChunk : Chunk
{
public:
    NListChunk();
    virtual ~NListChunk();
    NListChunk(const NListChunk&) = delete;
    NListChunk(NListChunk&&) = default;
    NListChunk& operator=(const NListChunk&) = delete;
    NListChunk& operator=(NListChunk&&) = default;

    // Virtual methods
    const char* name() const override final;

    std::vector<struct nlist>       nlist32;
    std::vector<struct nlist_64>    nlist64;

    uint32_t localsStartIndex   = 0;
    uint32_t localsCount        = 0;
    uint32_t globalsStartIndex  = 0;
    uint32_t globalsCount       = 0;
    uint32_t undefsStartIndex   = 0;
    uint32_t undefsCount        = 0;

    // FIXME: We really don't want to make kind public
    using Chunk::kind;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

// The optimized symbols strings for a given subCache
struct SymbolStringsChunk : Chunk
{
public:
    SymbolStringsChunk();
    virtual ~SymbolStringsChunk();
    SymbolStringsChunk(const SymbolStringsChunk&) = delete;
    SymbolStringsChunk(SymbolStringsChunk&&) = default;
    SymbolStringsChunk& operator=(const SymbolStringsChunk&) = delete;
    SymbolStringsChunk& operator=(SymbolStringsChunk&&) = default;

    // Virtual methods
    const char* name() const override final;

    // FIXME: We really don't want to make kind public
    using Chunk::kind;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

// The uniqued GOTs for a given subCache
struct UniquedGOTsChunk : SlidChunk
{
public:
    UniquedGOTsChunk();
    virtual ~UniquedGOTsChunk();
    UniquedGOTsChunk(const UniquedGOTsChunk&) = delete;
    UniquedGOTsChunk(UniquedGOTsChunk&&) = default;
    UniquedGOTsChunk& operator=(const UniquedGOTsChunk&) = delete;
    UniquedGOTsChunk& operator=(UniquedGOTsChunk&&) = default;

    // Virtual methods
    const char* name() const override final;
    UniquedGOTsChunk* isUniquedGOTsChunk() override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

// The stubs for a universal cache.  One StubsChunk per source dylib stubs section
struct StubsChunk : Chunk
{
public:
    StubsChunk();
    virtual ~StubsChunk();
    StubsChunk(const StubsChunk&) = delete;
    StubsChunk(StubsChunk&&) = default;
    StubsChunk& operator=(const StubsChunk&) = delete;
    StubsChunk& operator=(StubsChunk&&) = default;

    // Virtual methods
    const char* name() const override final;
    StubsChunk* isStubsChunk() override final;

    // A dylib might have multiple segment/section's with stubs.  This is to track which one
    // this stubs chunk corresponds to
    // Note we use strings, because forEachSegment/Section might return pointers to temporary strings
    std::string segmentName;
    std::string sectionName;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

// Space reserved for dynamic content generated at runtime
struct DynamicConfigChunk : Chunk
{
public:
    DynamicConfigChunk();
    virtual ~DynamicConfigChunk();
    DynamicConfigChunk(const DynamicConfigChunk&) = delete;
    DynamicConfigChunk(DynamicConfigChunk&&) = default;
    DynamicConfigChunk& operator=(const DynamicConfigChunk&) = delete;
    DynamicConfigChunk& operator=(DynamicConfigChunk&&) = default;

    // Virtual methods
    const char* name() const override final;
    bool isZeroFill() const override final;
private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct AlignChunk : Chunk
{
public:
    AlignChunk();
    virtual ~AlignChunk();
    AlignChunk(const AlignChunk&) = delete;
    AlignChunk(AlignChunk&&) = delete;
    AlignChunk& operator=(const AlignChunk&) = delete;
    AlignChunk& operator=(AlignChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

    const AlignChunk* isAlignChunk() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

struct PrewarmingChunk : Chunk
{
public:
    PrewarmingChunk(Kind kind);
    virtual ~PrewarmingChunk();
    PrewarmingChunk(const PrewarmingChunk&) = delete;
    PrewarmingChunk(PrewarmingChunk&&) = delete;
    PrewarmingChunk& operator=(const PrewarmingChunk&) = delete;
    PrewarmingChunk& operator=(PrewarmingChunk&&) = delete;

    // Virtual methods
    const char* name() const override final;

private:
    __attribute__((used))
    virtual void dump() const override final;
};

} // namespace cache_builder

#endif /* Chunk_hpp */