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


#include <stdint.h>
#include <assert.h>
#include <uuid/uuid.h>
#include <unistd.h>
#include <limits.h>
#include <mach/vm_page_size.h>

#include "ClosureWriter.h"
#include "MachOFile.h"


namespace dyld3 {
namespace closure {


////////////////////////////  ContainerTypedBytesWriter ////////////////////////////////////////


void ContainerTypedBytesWriter::setContainerType(TypedBytes::Type containerType)
{
    assert(_vmAllocationStart == 0);
    _vmAllocationSize = 1024 * 1024;
    vm_address_t allocationAddr;
    ::vm_allocate(mach_task_self(), &allocationAddr, _vmAllocationSize, VM_FLAGS_ANYWHERE);
    assert(allocationAddr != 0);
    _vmAllocationStart = (void*)allocationAddr;
    _containerTypedBytes =  (TypedBytes*)_vmAllocationStart;
    _containerTypedBytes->type = containerType;
    _containerTypedBytes->payloadLength = 0;
    _end = (uint8_t*)_vmAllocationStart + sizeof(TypedBytes);
}

void* ContainerTypedBytesWriter::append(TypedBytes::Type t, const void* payload, uint32_t payloadSize)
{
    assert((payloadSize & 0x3) == 0);
    if ( (uint8_t*)_end + payloadSize >= (uint8_t*)_vmAllocationStart + _vmAllocationSize ) {
        // if current buffer too small, grow it
        size_t growth = _vmAllocationSize;
        if ( growth < payloadSize )
            growth = _vmAllocationSize*((payloadSize/_vmAllocationSize)+1);
        vm_address_t newAllocationAddr;
        size_t newAllocationSize = _vmAllocationSize+growth;
        ::vm_allocate(mach_task_self(), &newAllocationAddr, newAllocationSize, VM_FLAGS_ANYWHERE);
	    assert(newAllocationAddr != 0);
        size_t currentInUse = (char*)_end - (char*)_vmAllocationStart;
        memcpy((void*)newAllocationAddr, _vmAllocationStart, currentInUse);
        ::vm_deallocate(mach_task_self(), (vm_address_t)_vmAllocationStart, _vmAllocationSize);
        _end                 = (void*)(newAllocationAddr + currentInUse);
        _vmAllocationStart   = (void*)newAllocationAddr;
        _containerTypedBytes = (TypedBytes*)_vmAllocationStart;
        _vmAllocationSize    = newAllocationSize;
    }
    assert( (uint8_t*)_end + payloadSize < (uint8_t*)_vmAllocationStart + _vmAllocationSize);
    TypedBytes* tb = (TypedBytes*)_end;
    tb->type   = t;
    tb->payloadLength = payloadSize;
    if ( payload != nullptr )
        ::memcpy(tb->payload(), payload, payloadSize);
    _end = (uint8_t*)_end + sizeof(TypedBytes) + payloadSize;
    assert((_containerTypedBytes->payloadLength + sizeof(TypedBytes) + payloadSize) < (16 * 1024 * 1024));
    _containerTypedBytes->payloadLength += sizeof(TypedBytes) + payloadSize;
    return tb->payload();
}

const void* ContainerTypedBytesWriter::finalizeContainer()
{
    // trim vm allocation down to just what is needed
    uintptr_t bufferStart = (uintptr_t)_vmAllocationStart;
    uintptr_t used = round_page((uintptr_t)_end - bufferStart);
    if ( used < _vmAllocationSize ) {
        uintptr_t deallocStart = bufferStart + used;
        ::vm_deallocate(mach_task_self(), deallocStart, _vmAllocationSize - used);
        _end = nullptr;
        _vmAllocationSize = used;
    }
    // mark vm region read-only
    ::vm_protect(mach_task_self(), bufferStart, used, false, VM_PROT_READ);
    return (void*)_vmAllocationStart;
}

const void* ContainerTypedBytesWriter::currentTypedBytes()
{
    return (void*)_vmAllocationStart;
}

void ContainerTypedBytesWriter::deallocate()
{
    ::vm_deallocate(mach_task_self(), (long)_vmAllocationStart, _vmAllocationSize);
}

////////////////////////////  ImageWriter ////////////////////////////////////////


const Image* ImageWriter::finalize()
{
    return (Image*)finalizeContainer();
}

const Image* ImageWriter::currentImage()
{
    return (Image*)currentTypedBytes();
}

void ImageWriter::addPath(const char* path)
{
    uint32_t roundedPathLen = ((uint32_t)strlen(path) + 1 + 3) & (-4);
    Image::PathAndHash* ph = (Image::PathAndHash*)append(TypedBytes::Type::pathWithHash, nullptr, sizeof(Image::PathAndHash)+roundedPathLen);
    ph->hash = Image::hashFunction(path);
    strcpy(ph->path, path);
}

Image::Flags& ImageWriter::getFlags()
{
    if ( _flagsOffset == -1 ) {
        setContainerType(TypedBytes::Type::image);
        Image::Flags flags;
        ::bzero(&flags, sizeof(flags));
        uint8_t* p = (uint8_t*)append(TypedBytes::Type::imageFlags, &flags, sizeof(flags));
        _flagsOffset = (int)(p - (uint8_t*)currentTypedBytes());
    }
    return *((Image::Flags*)((uint8_t*)currentTypedBytes() + _flagsOffset));
}

void ImageWriter::setImageNum(ImageNum num)
{
   getFlags().imageNum = num;
}

void ImageWriter::setHasObjC(bool value)
{
    getFlags().hasObjC = value;
}

void ImageWriter::setIs64(bool value)
{
    getFlags().is64 = value;
}

void ImageWriter::setHasPlusLoads(bool value)
{
    getFlags().mayHavePlusLoads = value;
}

void ImageWriter::setIsBundle(bool value)
{
    getFlags().isBundle = value;
}

void ImageWriter::setIsDylib(bool value)
{
    getFlags().isDylib = value;
}

void ImageWriter::setIsExecutable(bool value)
{
    getFlags().isExecutable = value;
}

void ImageWriter::setHasWeakDefs(bool value)
{
    getFlags().hasWeakDefs = value;
}

void ImageWriter::setUses16KPages(bool value)
{
    getFlags().has16KBpages = value;
}

void ImageWriter::setOverridableDylib(bool value)
{
    getFlags().overridableDylib = value;
}

void ImageWriter::setInvalid()
{
    getFlags().isInvalid = true;
}

void ImageWriter::setInDyldCache(bool value)
{
    getFlags().inDyldCache = value;
}

void ImageWriter::setHasPrecomputedObjC(bool value)
{
    getFlags().hasPrecomputedObjC = value;
}

void ImageWriter::setNeverUnload(bool value)
{
    getFlags().neverUnload = value;
}

void ImageWriter::setUUID(const uuid_t uuid)
{
    append(TypedBytes::Type::uuid, uuid, sizeof(uuid_t));
}

void ImageWriter::addCDHash(const uint8_t cdHash[20])
{
    append(TypedBytes::Type::cdHash, cdHash, 20);
}

void ImageWriter::setDependents(const Array<Image::LinkedImage>& deps)
{
    append(TypedBytes::Type::dependents, deps.begin(), (uint32_t)deps.count()*sizeof(Image::LinkedImage));
}

void ImageWriter::setDofOffsets(const Array<uint32_t>& dofSectionOffsets)
{
    append(TypedBytes::Type::dofOffsets, &dofSectionOffsets[0], (uint32_t)dofSectionOffsets.count()*sizeof(uint32_t));
}

void ImageWriter::setInitOffsets(const uint32_t initOffsets[], uint32_t count)
{
    append(TypedBytes::Type::initOffsets, initOffsets, count*sizeof(uint32_t));
}

void ImageWriter::setTermOffsets(const uint32_t termOffsets[], uint32_t count)
{
    getFlags().hasTerminators = true;
    append(TypedBytes::Type::termOffsets, termOffsets, count*sizeof(uint32_t));
}

void ImageWriter::setInitSectRange(uint32_t sectionOffset, uint32_t sectionSize)
{
    Image::InitializerSectionRange range = { sectionOffset, sectionSize };
    append(TypedBytes::Type::initsSection, &range, sizeof(Image::InitializerSectionRange));
}

void ImageWriter::setDiskSegments(const Image::DiskSegment segs[], uint32_t count)
{
    append(TypedBytes::Type::diskSegment, segs, count*sizeof(Image::DiskSegment));
    for (uint32_t i=0; i < count; ++i) {
        if ( segs[i].permissions == Image::DiskSegment::kReadOnlyDataPermissions )
            getFlags().hasReadOnlyData = true;
    }
}

void ImageWriter::setCachedSegments(const Image::DyldCacheSegment segs[], uint32_t count)
{
    append(TypedBytes::Type::cacheSegment, segs, count*sizeof(Image::DyldCacheSegment));
}

void ImageWriter::setCodeSignatureLocation(uint32_t fileOffset, uint32_t size)
{
    Image::CodeSignatureLocation loc;
    loc.fileOffset = fileOffset;
    loc.fileSize   = size;
    append(TypedBytes::Type::codeSignLoc, &loc, sizeof(loc));
}

void ImageWriter::setFairPlayEncryptionRange(uint32_t fileOffset, uint32_t size)
{
    Image::FairPlayRange loc;
    loc.rangeStart = fileOffset;
    loc.rangeLength = size;
    append(TypedBytes::Type::fairPlayLoc, &loc, sizeof(loc));
}

void ImageWriter::setMappingInfo(uint64_t sliceOffset, uint64_t vmSize)
{
    const uint32_t pageSize = getFlags().has16KBpages ? 0x4000 : 0x1000;
    Image::MappingInfo info;
    info.sliceOffsetIn4K = (uint32_t)(sliceOffset / 0x1000);
    info.totalVmPages    = (uint32_t)(vmSize / pageSize);
    append(TypedBytes::Type::mappingInfo, &info, sizeof(info));
}

void ImageWriter::setFileInfo(uint64_t inode, uint64_t mTime)
{
    Image::FileInfo info = { inode, mTime };
    append(TypedBytes::Type::fileInodeAndTime, &info, sizeof(info));
}

void ImageWriter::setRebaseInfo(const Array<Image::RebasePattern>& fixups)
{
    append(TypedBytes::Type::rebaseFixups, fixups.begin(), (uint32_t)fixups.count()*sizeof(Image::RebasePattern));
}

void ImageWriter::setTextRebaseInfo(const Array<Image::TextFixupPattern>& fixups)
{
    append(TypedBytes::Type::textFixups, fixups.begin(), (uint32_t)fixups.count()*sizeof(Image::TextFixupPattern));
}

void ImageWriter::setBindInfo(const Array<Image::BindPattern>& fixups)
{
    append(TypedBytes::Type::bindFixups, fixups.begin(), (uint32_t)fixups.count()*sizeof(Image::BindPattern));
}

void ImageWriter::setFixupsNotEncoded()
{
    getFlags().fixupsNotEncoded = true;
}

void ImageWriter::setRebasesNotEncoded()
{
    getFlags().rebasesNotEncoded = true;
}

void ImageWriter::setChainedFixups(uint64_t runtimeStartsStructOffset, const Array<Image::ResolvedSymbolTarget>& targets)
{
    getFlags().hasChainedFixups = true;
    append(TypedBytes::Type::chainedStartsOffset, &runtimeStartsStructOffset, sizeof(uint64_t));
    append(TypedBytes::Type::chainedFixupsTargets, targets.begin(), (uint32_t)targets.count()*sizeof(Image::ResolvedSymbolTarget));
}

void ImageWriter::setObjCFixupInfo(const Image::ResolvedSymbolTarget& objcProtocolClassTarget,
                                   uint64_t objcImageInfoVMOffset,
                                   const Array<Image::ProtocolISAFixup>& protocolISAFixups,
                                   const Array<Image::SelectorReferenceFixup>& selRefFixups,
                                   const Array<Image::ClassStableSwiftFixup>& classStableSwiftFixups,
                                   const Array<Image::MethodListFixup>& methodListFixups)
{
    // The layout here is:
    //   ResolvedSymbolTarget
    //   uint64_t vmOffset to objc_imageinfo
    //   uint32_t protocol count
    //   uint32_t selector reference count
    //   array of ProtocolISAFixup
    //   array of SelectorReferenceFixup
    //   optional uint32_t stable swift fixup count
    //   optional uint32_t method list fixup count
    //   optional array of ClassStableSwiftFixup
    //   optional array of MethodListFixup

    uint64_t headerSize = sizeof(Image::ResolvedSymbolTarget) + sizeof(uint64_t) + (sizeof(uint32_t) * 4);
    uint64_t protocolsSize = (sizeof(Image::ProtocolISAFixup) * protocolISAFixups.count());
    uint64_t selRefsSize = (sizeof(Image::SelectorReferenceFixup) * selRefFixups.count());
    uint64_t stableSwiftSize = (sizeof(Image::ClassStableSwiftFixup) * classStableSwiftFixups.count());
    uint64_t methodListSize = (sizeof(Image::MethodListFixup) * methodListFixups.count());

    uint64_t totalSize = headerSize + protocolsSize + selRefsSize + stableSwiftSize + methodListSize;
    assert( (totalSize & 3) == 0);
    uint8_t* buffer = (uint8_t*)append(TypedBytes::Type::objcFixups, nullptr, (uint32_t)totalSize);

    // Set the statically sized data
    uint32_t protocolFixupCount = (uint32_t)protocolISAFixups.count();
    uint32_t selRefFixupCount = (uint32_t)selRefFixups.count();
    memcpy(buffer, &objcProtocolClassTarget, sizeof(Image::ResolvedSymbolTarget));
    buffer += sizeof(Image::ResolvedSymbolTarget);
    memcpy(buffer, &objcImageInfoVMOffset, sizeof(uint64_t));
    buffer += sizeof(uint64_t);
    memcpy(buffer, &protocolFixupCount, sizeof(uint32_t));
    buffer += sizeof(uint32_t);
    memcpy(buffer, &selRefFixupCount, sizeof(uint32_t));
    buffer += sizeof(uint32_t);

    // Set the protocol fixups
    if ( protocolFixupCount != 0 ) {
        memcpy(buffer, protocolISAFixups.begin(), (size_t)protocolsSize);
        buffer += protocolsSize;
    }

    // Set the selector reference fixups
    if ( selRefFixupCount != 0 ) {
        memcpy(buffer, selRefFixups.begin(), (size_t)selRefsSize);
        buffer += selRefsSize;
    }

    // New closures get additional fixups.  These are ignored by old dyld's
    uint32_t stableSwiftFixupCount = (uint32_t)classStableSwiftFixups.count();
    uint32_t methodListFixupCount = (uint32_t)methodListFixups.count();
    memcpy(buffer, &stableSwiftFixupCount, sizeof(uint32_t));
    buffer += sizeof(uint32_t);
    memcpy(buffer, &methodListFixupCount, sizeof(uint32_t));
    buffer += sizeof(uint32_t);

    // Set the stable swift fixups
    if ( stableSwiftFixupCount != 0 ) {
        memcpy(buffer, classStableSwiftFixups.begin(), (size_t)stableSwiftSize);
        buffer += stableSwiftSize;
    }

    // Set the method list fixups
    if ( methodListFixupCount != 0 ) {
        memcpy(buffer, methodListFixups.begin(), (size_t)methodListSize);
        buffer += methodListSize;
    }
}

void ImageWriter::setAsOverrideOf(ImageNum imageNum)
{
    uint32_t temp = imageNum;
    append(TypedBytes::Type::imageOverride, &temp, sizeof(temp));
    getFlags().hasOverrideImageNum = true;
}

void ImageWriter::setInitsOrder(const ImageNum images[], uint32_t count)
{
    append(TypedBytes::Type::initBefores, images, count*sizeof(ImageNum));
}


////////////////////////////  ImageArrayWriter ////////////////////////////////////////


ImageArrayWriter::ImageArrayWriter(ImageNum startImageNum, unsigned count, bool hasRoots) : _index(0)
{
    setContainerType(TypedBytes::Type::imageArray);
    _end = (void*)((uint8_t*)_end + sizeof(ImageArray) - sizeof(TypedBytes) + sizeof(uint32_t)*count);
    _containerTypedBytes->payloadLength = sizeof(ImageArray) - sizeof(TypedBytes) + sizeof(uint32_t)*count;
    ImageArray* ia = (ImageArray*)_containerTypedBytes;
    ia->firstImageNum   = startImageNum;
    ia->count           = count;
    ia->hasRoots        = hasRoots;
}

void ImageArrayWriter::appendImage(const Image* image)
{
    ImageArray* ia = (ImageArray*)_containerTypedBytes;
    ia->offsets[_index++] = _containerTypedBytes->payloadLength;
    append(TypedBytes::Type::image, image->payload(), image->payloadLength);
}

const ImageArray* ImageArrayWriter::finalize()
{
    return (ImageArray*)finalizeContainer();
}


////////////////////////////  ClosureWriter ////////////////////////////////////////

void ClosureWriter::setTopImageNum(ImageNum imageNum)
{
    append(TypedBytes::Type::topImage, &imageNum, sizeof(ImageNum));
}

void ClosureWriter::addCachePatches(const Array<Closure::PatchEntry>& patches)
{
    append(TypedBytes::Type::cacheOverrides, patches.begin(), (uint32_t)patches.count()*sizeof(Closure::PatchEntry));
}

void ClosureWriter::applyInterposing(const LaunchClosure* launchClosure)
{
    const Closure*       currentClosure = (Closure*)currentTypedBytes();
	const ImageArray*    images         = currentClosure->images();
	launchClosure->forEachInterposingTuple(^(const InterposingTuple& tuple, bool&) {
        images->forEachImage(^(const dyld3::closure::Image* image, bool&) {
            for (const Image::BindPattern& bindPat : image->bindFixups()) {
                if ( (bindPat.target == tuple.stockImplementation) && (tuple.newImplementation.image.imageNum != image->imageNum()) ) {
                    Image::BindPattern* writePat = const_cast<Image::BindPattern*>(&bindPat);
                    writePat->target = tuple.newImplementation;
                }
            }

            // Chained fixups may also be interposed.  We can't change elements in the chain, but we can change
            // the target list.
            for (const Image::ResolvedSymbolTarget& symbolTarget : image->chainedTargets()) {
                if ( (symbolTarget == tuple.stockImplementation) && (tuple.newImplementation.image.imageNum != image->imageNum()) ) {
                    Image::ResolvedSymbolTarget* writeTarget = const_cast<Image::ResolvedSymbolTarget*>(&symbolTarget);
                    *writeTarget = tuple.newImplementation;
                }
            }
        });
    });
}

void ClosureWriter::addWarning(Closure::Warning::Type warningType, const char* warning)
{
    uint32_t roundedMessageLen = ((uint32_t)strlen(warning) + 1 + 3) & (-4);
    Closure::Warning* ph = (Closure::Warning*)append(TypedBytes::Type::warning, nullptr, sizeof(Closure::Warning)+roundedMessageLen);
    ph->type = warningType;
    strcpy(ph->message, warning);
}


////////////////////////////  LaunchClosureWriter ////////////////////////////////////////

LaunchClosureWriter::LaunchClosureWriter(const ImageArray* images)
{
    setContainerType(TypedBytes::Type::launchClosure);
    append(TypedBytes::Type::imageArray, images->payload(), images->payloadLength);
}

const LaunchClosure* LaunchClosureWriter::finalize()
{
    return (LaunchClosure*)finalizeContainer();
}

void LaunchClosureWriter::setLibSystemImageNum(ImageNum imageNum)
{
    append(TypedBytes::Type::libSystemNum, &imageNum, sizeof(ImageNum));
}

void LaunchClosureWriter::setLibDyldEntry(Image::ResolvedSymbolTarget entry)
{
    append(TypedBytes::Type::libDyldEntry, &entry, sizeof(entry));
}

void LaunchClosureWriter::setMainEntry(Image::ResolvedSymbolTarget main)
{
    append(TypedBytes::Type::mainEntry, &main, sizeof(main));
}

void LaunchClosureWriter::setStartEntry(Image::ResolvedSymbolTarget start)
{
    append(TypedBytes::Type::startEntry, &start, sizeof(start));
}

void LaunchClosureWriter::setUsedFallbackPaths(bool value)
{
    getFlags().usedFallbackPaths = value;
}

void LaunchClosureWriter::setUsedAtPaths(bool value)
{
    getFlags().usedAtPaths = value;
}

void LaunchClosureWriter::setUsedInterposing(bool value)
{
    getFlags().usedInterposing = value;
}

void LaunchClosureWriter::setHasInsertedLibraries(bool value)
{
    getFlags().hasInsertedLibraries = value;
}

void LaunchClosureWriter::setInitImageCount(uint32_t count)
{
    getFlags().initImageCount = count;
}

LaunchClosure::Flags& LaunchClosureWriter::getFlags()
{
    if ( _flagsOffset == -1 ) {
        LaunchClosure::Flags flags;
        ::bzero(&flags, sizeof(flags));
        uint8_t* p = (uint8_t*)append(TypedBytes::Type::closureFlags, &flags, sizeof(flags));
        _flagsOffset = (int)(p - (uint8_t*)currentTypedBytes());
    }
    return *((LaunchClosure::Flags*)((uint8_t*)currentTypedBytes() + _flagsOffset));
}

void LaunchClosureWriter::setMustBeMissingFiles(const Array<const char*>& paths)
{
    uint32_t totalSize = 0;
    for (const char* s : paths)
        totalSize += (strlen(s) +1);
    totalSize = (totalSize + 3) & (-4); // align

    char* buffer = (char*)append(TypedBytes::Type::missingFiles, nullptr, totalSize);
    char* t = buffer;
    for (const char* path : paths) {
        for (const char* s=path; *s != '\0'; ++s)
            *t++ = *s;
        *t++ = '\0';
    }
    while (t < &buffer[totalSize])
        *t++ = '\0';
}

void LaunchClosureWriter::setMustExistFiles(const Array<LaunchClosure::SkippedFile>& files)
{
    // Start the structure with a count
    uint32_t totalSize = sizeof(uint64_t);

    // Then make space for the array of mod times and inode numbers
    totalSize += files.count() * sizeof(uint64_t) * 2;

    // Then the array of paths on the end
    for (const LaunchClosure::SkippedFile& file : files)
        totalSize += (strlen(file.path) + 1);
    totalSize = (totalSize + 3) & (-4); // align

    char* buffer = (char*)append(TypedBytes::Type::existingFiles, nullptr, totalSize);

    // Set the size
    uint64_t* bufferPtr = (uint64_t*)buffer;
    *bufferPtr++ = (uint64_t)files.count();

    // And the array of mod times and inode numbers
    for (const LaunchClosure::SkippedFile& file : files) {
        *bufferPtr++ = file.inode;
        *bufferPtr++ = file.mtime;
    }

    char* t = (char*)bufferPtr;
    for (const LaunchClosure::SkippedFile& file : files) {
        for (const char* s=file.path; *s != '\0'; ++s)
            *t++ = *s;
        *t++ = '\0';
    }
    while (t < &buffer[totalSize])
        *t++ = '\0';
}

void LaunchClosureWriter::addEnvVar(const char* envVar)
{
    unsigned len = (unsigned)strlen(envVar);
    char temp[len+8];
    strcpy(temp, envVar);
    unsigned paddedSize = len+1;
    while ( (paddedSize % 4) != 0 )
        temp[paddedSize++] = '\0';
    append(TypedBytes::Type::envVar, temp, paddedSize);
}

void LaunchClosureWriter::addInterposingTuples(const Array<InterposingTuple>& tuples)
{
    append(TypedBytes::Type::interposeTuples, tuples.begin(), (uint32_t)tuples.count()*sizeof(InterposingTuple));
}

void LaunchClosureWriter::setDyldCacheUUID(const uuid_t uuid)
{
    append(TypedBytes::Type::dyldCacheUUID, uuid, sizeof(uuid_t));
}

void LaunchClosureWriter::setHasProgramVars(uint32_t offset)
{
    getFlags().hasProgVars = true;
    append(TypedBytes::Type::progVars, &offset, sizeof(uint32_t));
}

void LaunchClosureWriter::setObjCSelectorInfo(const Array<uint8_t>& hashTable, const Array<Image::ObjCSelectorImage>& hashTableImages) {
    uint32_t count = (uint32_t)hashTableImages.count();
    uint32_t totalSize = (uint32_t)(sizeof(count) + (sizeof(Image::ObjCSelectorImage) * count) + hashTable.count());
    totalSize = (totalSize + 3) & (-4); // align
    uint8_t* buffer = (uint8_t*)append(TypedBytes::Type::selectorTable, nullptr, totalSize);

    // Write out out the image count
    memcpy(buffer, &count, sizeof(count));
    buffer += sizeof(count);

    // Write out out the image nums
    memcpy(buffer, hashTableImages.begin(), sizeof(Image::ObjCSelectorImage) * count);
    buffer += sizeof(Image::ObjCSelectorImage) * count;

    // Write out out the image count
    memcpy(buffer, hashTable.begin(), hashTable.count());
}

void LaunchClosureWriter::setObjCClassAndProtocolInfo(const Array<uint8_t>& classHashTable, const Array<uint8_t>& protocolHashTable,
                                                      const Array<Image::ObjCClassImage>& hashTableImages) {
    // The layout here is:
    //   uint32_t offset to class table (note this is 0 if there are no classes)
    //   uint32_t offset to protocol table (note this is 0 if there are no protocols)
    //   uint32_t num images
    //   ObjCClassImage[num images]
    //   class hash table
    //   [ padding to 4-byte alignment if needed
    //   protocol hash table
    //   [ padding to 4-byte alignment if needed

    uint32_t numImages = (uint32_t)hashTableImages.count();

    uint32_t headerSize = sizeof(uint32_t) * 3;
    uint32_t imagesSize = (sizeof(Image::ObjCClassImage) * numImages);
    uint32_t classTableSize = ((uint32_t)classHashTable.count() + 3) & (-4); // pad to 4-byte multiple
    uint32_t protocolTableSize = ((uint32_t)protocolHashTable.count() + 3) & (-4); // pad to 4-byte multiple
    uint32_t offsetToClassTable = (classTableSize == 0) ? 0 : (headerSize + imagesSize);
    uint32_t offsetToProtocolTable = (protocolTableSize == 0) ? 0 : (headerSize + imagesSize + classTableSize);

    uint32_t totalSize = headerSize + imagesSize + classTableSize + protocolTableSize;
    assert( (totalSize & 3) == 0);
    uint8_t* buffer = (uint8_t*)append(TypedBytes::Type::classTable, nullptr, totalSize);

    // Write out out the header
    memcpy(buffer + 0, &offsetToClassTable, sizeof(uint32_t));
    memcpy(buffer + 4, &offsetToProtocolTable, sizeof(uint32_t));
    memcpy(buffer + 8, &numImages, sizeof(uint32_t));

    // Write out out the image nums
    memcpy(buffer + headerSize, hashTableImages.begin(), imagesSize);

    // Write out out the class hash table
    if ( offsetToClassTable != 0 )
        memcpy(buffer + offsetToClassTable, classHashTable.begin(), classHashTable.count());

    // Write out out the protocol hash table
    if ( offsetToProtocolTable != 0 )
        memcpy(buffer + offsetToProtocolTable, protocolHashTable.begin(), protocolHashTable.count());
}

void LaunchClosureWriter::setObjCDuplicateClassesInfo(const Array<uint8_t>& hashTable) {
    uint32_t totalSize = (uint32_t)hashTable.count();
    totalSize = (totalSize + 3) & (-4); // align
    uint8_t* buffer = (uint8_t*)append(TypedBytes::Type::duplicateClassesTable, nullptr, totalSize);

    // Write out out the hash table
    memcpy(buffer, hashTable.begin(), hashTable.count());
}

////////////////////////////  DlopenClosureWriter ////////////////////////////////////////

DlopenClosureWriter::DlopenClosureWriter(const ImageArray* images)
{
    setContainerType(TypedBytes::Type::dlopenClosure);
    append(TypedBytes::Type::imageArray, images->payload(), images->payloadLength);
}

const DlopenClosure* DlopenClosureWriter::finalize()
{
    return (DlopenClosure*)finalizeContainer();
}


} // namespace closure
} // namespace dyld3