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
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
/*
 * 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-o/dyld_priv.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <System/machine/cpu_capabilities.h>
#include <_simple.h>

extern "C" {
  #include <corecrypto/ccdigest.h>
  #include <corecrypto/ccsha2.h>
}

#include "Closure.h"
#include "MachOFile.h"
#include "MachOLoaded.h"
#include "StringUtils.h"

#include "objc-shared-cache.h"


namespace dyld3 {
namespace closure {


////////////////////////////  TypedBytes ////////////////////////////////////////

const void* TypedBytes::payload() const
{
    return (uint8_t*)this + sizeof(TypedBytes);
}

void* TypedBytes::payload()
{
    return (uint8_t*)this + sizeof(TypedBytes);
}


////////////////////////////  ContainerTypedBytes ////////////////////////////////////////

const TypedBytes* ContainerTypedBytes::first() const
{
    return (TypedBytes*)payload();
}

const TypedBytes* ContainerTypedBytes::next(const TypedBytes* p) const
{
    assert((p->payloadLength & 0x3) == 0);
    return (TypedBytes*)((uint8_t*)(p->payload()) + p->payloadLength);
}

void ContainerTypedBytes::forEachAttribute(void (^handler)(const TypedBytes* typedBytes, bool& stop)) const
{
    assert(((long)this & 0x3) == 0);
    const TypedBytes* end = next(this);
    bool stop = false;
    for (const TypedBytes* p = first(); p < end && !stop; p = next(p)) {
        handler(p, stop);
    }
}

void ContainerTypedBytes::forEachAttributePayload(Type requestedType, void (^handler)(const void* payload, uint32_t size, bool& stop)) const
{
    forEachAttribute(^(const TypedBytes* typedBytes, bool& stop) {
        if ( (Type)(typedBytes->type) != requestedType )
            return;
        handler(typedBytes->payload(), typedBytes->payloadLength, stop);
    });
}

const void* ContainerTypedBytes::findAttributePayload(Type requestedType, uint32_t* payloadSize) const
{
    assert(((long)this & 0x3) == 0);
    if ( payloadSize != nullptr )
        *payloadSize = 0;
    const TypedBytes* end = next(this);
    bool stop = false;
    for (const TypedBytes* p = first(); p < end && !stop; p = next(p)) {
        if ( (Type)(p->type) == requestedType ) {
            if ( payloadSize != nullptr )
                *payloadSize = p->payloadLength;
            return p->payload();
        }
    }
    return nullptr;
}


////////////////////////////  Image ////////////////////////////////////////

const Image::Flags& Image::getFlags() const
{
    return *(Flags*)((uint8_t*)this + 2*sizeof(TypedBytes));
}

bool Image::isInvalid() const
{
    return getFlags().isInvalid;
}

size_t Image::size() const
{
    return sizeof(TypedBytes) + this->payloadLength;
}

ImageNum Image::imageNum() const
{
    return getFlags().imageNum;
}

// returns true iff 'num' is this image's ImageNum, or this image overrides that imageNum (in dyld cache)
bool Image::representsImageNum(ImageNum num) const
{
    const Flags& flags = getFlags();
    if ( flags.imageNum == num )
        return true;
    if ( !flags.isDylib )
        return false;
    if ( !flags.hasOverrideImageNum )
        return false;
    ImageNum cacheImageNum;
    if ( isOverrideOfDyldCacheImage(cacheImageNum) )
        return (cacheImageNum == num);
    return false;
}

uint32_t Image::maxLoadCount() const
{
    return getFlags().maxLoadCount;
}

bool Image::isBundle() const
{
    return getFlags().isBundle;
}

bool Image::isDylib() const
{
    return getFlags().isDylib;
}

bool Image::isExecutable() const
{
    return getFlags().isExecutable;
}

bool Image::hasObjC() const
{
    return getFlags().hasObjC;
}

bool Image::is64() const
{
    return getFlags().is64;
}

bool Image::hasWeakDefs() const
{
    return getFlags().hasWeakDefs;
}

bool Image::mayHavePlusLoads() const
{
    return getFlags().mayHavePlusLoads;
}

bool Image::neverUnload() const
{
    return getFlags().neverUnload;
}

bool Image::overridableDylib() const
{
    return getFlags().overridableDylib;
}

bool Image::inDyldCache() const
{
    return getFlags().inDyldCache;
}

const char* Image::path() const
{
    // might be multiple pathWithHash enties, first is canonical name
    const PathAndHash* result = (PathAndHash*)findAttributePayload(Type::pathWithHash);
    assert(result && "Image missing pathWithHash");
    return result->path;
}

const char* Image::leafName() const
{
    uint32_t size;
    // might be multiple pathWithHash enties, first is canonical name
    const PathAndHash* result = (PathAndHash*)findAttributePayload(Type::pathWithHash, &size);
    assert(result && "Image missing pathWithHash");
    for (const char* p=(char*)result + size; p > result->path; --p) {
        if ( *p == '/' )
            return p+1;
    }
    return result->path;
}

bool Image::hasFileModTimeAndInode(uint64_t& inode, uint64_t& mTime) const
{
    uint32_t size;
    const FileInfo* info = (FileInfo*)(findAttributePayload(Type::fileInodeAndTime, &size));
    if ( info != nullptr ) {
        assert(size == sizeof(FileInfo));
        inode = info->inode;
        mTime = info->modTime;
        return true;
    }
    return false;
}

void Image::forEachCDHash(void (^handler)(const uint8_t cdHash[20], bool& stop)) const
{
    forEachAttribute(^(const TypedBytes* typedBytes, bool& stopLoop) {
        if ( (Type)(typedBytes->type) != Type::cdHash )
            return;
        assert(typedBytes->payloadLength == 20);
        const uint8_t* bytes = (const uint8_t*)typedBytes->payload();
        handler(bytes, stopLoop);
    });
}

bool Image::getUuid(uuid_t uuid) const
{
    uint32_t size;
    const uint8_t* bytes = (uint8_t*)(findAttributePayload(Type::uuid, &size));
    if ( bytes == nullptr )
        return false;
    assert(size == 16);
    memcpy(uuid, bytes, 16);
    return true;
}

bool Image::hasCodeSignature(uint32_t& sigFileOffset, uint32_t& sigSize) const
{
    uint32_t sz;
    const Image::CodeSignatureLocation* sigInfo = (Image::CodeSignatureLocation*)(findAttributePayload(Type::codeSignLoc, &sz));
    if ( sigInfo != nullptr ) {
        assert(sz == sizeof(Image::CodeSignatureLocation));
        sigFileOffset = sigInfo->fileOffset;
        sigSize       = sigInfo->fileSize;
        return true;
    }
    return false;
}

bool Image::isFairPlayEncrypted(uint32_t& textOffset, uint32_t& size) const
{
    uint32_t sz;
    const Image::FairPlayRange* fpInfo = (Image::FairPlayRange*)(findAttributePayload(Type::fairPlayLoc, &sz));
    if ( fpInfo != nullptr ) {
        assert(sz == sizeof(Image::FairPlayRange));
        textOffset = fpInfo->rangeStart;
        size       = fpInfo->rangeLength;
        return true;
    }
    return false;
}

const Array<Image::LinkedImage> Image::dependentsArray() const
{
    uint32_t size;
    LinkedImage* dependents = (LinkedImage*)findAttributePayload(Type::dependents, &size);
    assert((size % sizeof(LinkedImage)) == 0);
    uintptr_t count = size / sizeof(LinkedImage);
    return Array<Image::LinkedImage>(dependents, count, count);
}

void Image::forEachDependentImage(void (^handler)(uint32_t dependentIndex, LinkKind kind, ImageNum imageNum, bool& stop)) const
{
    uint32_t size;
    const LinkedImage* dependents = (LinkedImage*)findAttributePayload(Type::dependents, &size);
    assert((size % sizeof(LinkedImage)) == 0);
    const uint32_t count = size / sizeof(LinkedImage);
    bool stop = false;
    for (uint32_t i=0; (i < count) && !stop; ++i) {
        LinkKind kind     = dependents[i].kind();
        ImageNum imageNum = dependents[i].imageNum();
        // ignore missing weak links
        if ( (imageNum == kMissingWeakLinkedImage) && (kind == LinkKind::weak) )
            continue;
        handler(i, kind, imageNum, stop);
    }
}

ImageNum Image::dependentImageNum(uint32_t depIndex) const
{
    uint32_t size;
    const LinkedImage* dependents = (LinkedImage*)findAttributePayload(Type::dependents, &size);
    assert((size % sizeof(LinkedImage)) == 0);
    const uint32_t count = size / sizeof(LinkedImage);
    assert(depIndex < count);
    return dependents[depIndex].imageNum();
}


uint32_t Image::hashFunction(const char* str)
{
    uint32_t h = 0;
    for (const char* s=str; *s != '\0'; ++s)
        h = h*5 + *s;
    return h;
}

void Image::forEachAlias(void (^handler)(const char* aliasPath, bool& stop)) const
{
    __block bool foundFirst = false;
    forEachAttribute(^(const TypedBytes* typedBytes, bool& stopLoop) {
        if ( (Type)(typedBytes->type) != Type::pathWithHash )
            return;
        if ( foundFirst ) {
            const PathAndHash* aliasInfo = (PathAndHash*)typedBytes->payload();
            handler(aliasInfo->path, stopLoop);
        }
        else {
            foundFirst = true;
        }
    });
}

bool Image::hasPathWithHash(const char* path, uint32_t hash) const
{
    __block bool found = false;
    forEachAttribute(^(const TypedBytes* typedBytes, bool& stop) {
        if ( (Type)(typedBytes->type) != Type::pathWithHash )
            return;
        const PathAndHash* pathInfo = (PathAndHash*)typedBytes->payload();
        if ( (pathInfo->hash == hash) && (strcmp(path, pathInfo->path) == 0) ) {
            stop = true;
            found = true;
        }
    });
    return found;
}

void Image::forEachDiskSegment(void (^handler)(uint32_t segIndex, uint32_t fileOffset, uint32_t fileSize, int64_t vmOffset, uint64_t vmSize,
                                               uint8_t permissions, bool laterReadOnly, bool& stop)) const
{
    uint32_t size;
    const DiskSegment* segments = (DiskSegment*)findAttributePayload(Type::diskSegment, &size);
    assert(segments != nullptr);
    assert((size % sizeof(DiskSegment)) == 0);
    const uint32_t  count        = size / sizeof(DiskSegment);
    const uint32_t  pageSz       = pageSize();
    uint32_t        segIndex     = 0;
    uint32_t        fileOffset   = 0;
    int64_t         vmOffset     = 0;
    // decrement vmOffset by all segments before TEXT (e.g. PAGEZERO)
    for (uint32_t i=0; i < count; ++i) {
        const DiskSegment* seg = &segments[i];
        if ( seg->filePageCount != 0 ) {
            break;
        }
        vmOffset -= (uint64_t)seg->vmPageCount * pageSz;
    }
    // walk each segment and call handler
    bool stop = false;
    for (uint32_t i=0; i < count && !stop; ++i) {
        const DiskSegment* seg = &segments[i];
        uint64_t vmSize   = (uint64_t)seg->vmPageCount * pageSz;
        uint32_t fileSize = seg->filePageCount * pageSz;
        if ( !seg->paddingNotSeg ) {
            uint8_t perms   = seg->permissions;
            bool    laterRO = false;
            // read-only data segments are encoded as .w. , initially make them r/w
            if ( perms == Image::DiskSegment::kReadOnlyDataPermissions ) {
                perms   = VM_PROT_READ|VM_PROT_WRITE;
                laterRO = true;
            }
            handler(segIndex, ( fileSize == 0) ? 0 : fileOffset, fileSize, vmOffset, vmSize, perms, laterRO, stop);
            ++segIndex;
        }
        vmOffset   += vmSize;
        fileOffset += fileSize;
    }
}

uint32_t Image::pageSize() const
{
    if ( getFlags().has16KBpages )
        return 0x4000;
    else
        return 0x1000;
}

uint32_t Image::cacheOffset() const
{
    uint32_t size;
    const DyldCacheSegment* segments = (DyldCacheSegment*)findAttributePayload(Type::cacheSegment, &size);
    assert(segments != nullptr);
    assert((size % sizeof(DyldCacheSegment)) == 0);
    return segments[0].cacheOffset;
}

void Image::forEachCacheSegment(void (^handler)(uint32_t segIndex, uint64_t vmOffset, uint64_t vmSize, uint8_t permissions, bool& stop)) const
{
    uint32_t size;
    const DyldCacheSegment* segments = (DyldCacheSegment*)findAttributePayload(Type::cacheSegment, &size);
    assert(segments != nullptr);
    assert((size % sizeof(DyldCacheSegment)) == 0);
    const uint32_t  count = size / sizeof(DyldCacheSegment);
    bool stop = false;
    for (uint32_t i=0; i < count; ++i) {
        uint64_t vmOffset    = segments[i].cacheOffset - segments[0].cacheOffset;
        uint64_t vmSize      = segments[i].size;
        uint8_t  permissions = segments[i].permissions;
        handler(i, vmOffset, vmSize, permissions, stop);
        if ( stop )
            break;
    }
}

uint64_t Image::textSize() const
{
    __block uint64_t result = 0;
    if ( inDyldCache() ) {
        forEachCacheSegment(^(uint32_t segIndex, uint64_t vmOffset, uint64_t vmSize, uint8_t permissions, bool& stop) {
            result = vmSize;
            stop = true;
        });
    }
    else {
        forEachDiskSegment(^(uint32_t segIndex, uint32_t fileOffset, uint32_t fileSize, int64_t vmOffset, uint64_t vmSize, uint8_t permissions, bool laterReadOnly, bool& stop) {
            if ( permissions != 0) {
                result = vmSize;
                stop = true;
            }
        });
    }
    return result;
}

bool Image::containsAddress(const void* addr, const void* imageLoadAddress, uint8_t* permsResult) const
{
    __block bool  result     = false;
    uint64_t      targetAddr = (uint64_t)addr;
    uint64_t      imageStart = (uint64_t)imageLoadAddress;
    if ( inDyldCache() ) {
        forEachCacheSegment(^(uint32_t segIndex, uint64_t vmOffset, uint64_t vmSize, uint8_t permissions, bool& stop) {
            if ( (targetAddr >= imageStart+vmOffset) && (targetAddr < imageStart+vmOffset+vmSize) ) {
                result = true;
                if ( permsResult )
                    *permsResult = permissions;
                stop = true;
            }
        });
    }
    else {
        forEachDiskSegment(^(uint32_t segIndex, uint32_t fileOffset, uint32_t fileSize, int64_t vmOffset, uint64_t vmSize, uint8_t permissions, bool laterReadOnly, bool& stop) {
            if ( (targetAddr >= imageStart+vmOffset) && (targetAddr < imageStart+vmOffset+vmSize) ) {
                result = true;
                if ( permsResult )
                    *permsResult = permissions;
                stop = true;
            }
        });
    }
    return result;
}

uint64_t Image::vmSizeToMap() const
{
    uint32_t size;
    const Image::MappingInfo* info = (Image::MappingInfo*)(findAttributePayload(Type::mappingInfo, &size));
    assert(info != nullptr);
    assert(size == sizeof(Image::MappingInfo));
    return info->totalVmPages * pageSize();
}

uint64_t Image::sliceOffsetInFile() const
{
    uint32_t size;
    const Image::MappingInfo* info = (Image::MappingInfo*)(findAttributePayload(Type::mappingInfo, &size));
    assert(info != nullptr);
    assert(size == sizeof(Image::MappingInfo));
    return info->sliceOffsetIn4K * 0x1000;
}

void Image::forEachInitializer(const void* imageLoadAddress, void (^handler)(const void* initializer)) const
{
    uint32_t size;
    const uint32_t* inits = (uint32_t*)findAttributePayload(Type::initOffsets, &size);
    if ( inits != nullptr ) {
        assert((size % sizeof(uint32_t)) == 0);
        const uint32_t count = size / sizeof(uint32_t);
        for (uint32_t i=0; i < count; ++i) {
            uint32_t offset = inits[i];
            const void* init = (void*)((uint8_t*)imageLoadAddress + offset);
            handler(init);
        }
        return;
    }
    const Image::InitializerSectionRange* range = (Image::InitializerSectionRange*)findAttributePayload(Type::initsSection, &size);
    if ( range != nullptr ) {
        const uint32_t pointerSize = is64() ? 8 : 4;
        const uint32_t* start32 = (uint32_t*)((uint8_t*)imageLoadAddress + range->sectionOffset);
        const uint64_t* start64 = (uint64_t*)((uint8_t*)imageLoadAddress + range->sectionOffset);
        const uint32_t count = range->sectionSize / pointerSize;
        for (uint32_t i=0; i < count; ++i) {
            if ( pointerSize == 8 )
                handler((void*)(long)(start64[i]));
            else
                handler((void*)(long)(start32[i]));
        }
    }
}

bool Image::forEachInitializerSection(void (^handler)(uint32_t sectionOffset, uint32_t sectionSize)) const
{
    __block bool result = false;
    forEachAttributePayload(Type::initsSection, ^(const void* payload, uint32_t size, bool& stop) {
        const Image::InitializerSectionRange* range = (Image::InitializerSectionRange*)payload;
        assert((size % sizeof(Image::InitializerSectionRange)) == 0);
        handler(range->sectionOffset, range->sectionSize);
        result = true;
    });
    return result;
}

bool Image::hasInitializers() const
{
    uint32_t size;
    return ( findAttributePayload(Type::initOffsets, &size) != nullptr );
}

bool Image::hasTerminators() const
{
    return getFlags().hasTerminators;
}

bool Image::hasReadOnlyData() const
{
    return getFlags().hasReadOnlyData;
}

bool Image::hasChainedFixups() const
{
    return getFlags().hasChainedFixups;
}

bool Image::hasPrecomputedObjC() const
{
    return getFlags().hasPrecomputedObjC;
}

bool Image::fixupsNotEncoded() const
{
    return getFlags().fixupsNotEncoded;
}

bool Image::rebasesNotEncoded() const
{
    return getFlags().rebasesNotEncoded;
}

void Image::forEachTerminator(const void* imageLoadAddress, void (^handler)(const void* terminator)) const
{
    uint32_t size;
    const uint32_t* terms = (uint32_t*)findAttributePayload(Type::termOffsets, &size);
    if ( terms != nullptr ) {
        assert((size % sizeof(uint32_t)) == 0);
        const uint32_t count = size / sizeof(uint32_t);
        for (uint32_t i=0; i < count; ++i) {
            uint32_t offset = terms[i];
            const void* term = (void*)((uint8_t*)imageLoadAddress + offset);
            handler(term);
        }
    }
}

void Image::forEachDOF(const void* imageLoadAddress, void (^handler)(const void* dofSection)) const
{
    uint32_t size;
    const uint32_t* dofs = (uint32_t*)findAttributePayload(Type::dofOffsets, &size);
    if ( dofs != nullptr ) {
        assert((size % sizeof(uint32_t)) == 0);
        const uint32_t count = size / sizeof(uint32_t);
        for (uint32_t i=0; i < count; ++i) {
            uint32_t offset = dofs[i];
            const void* sect = (void*)((uint8_t*)imageLoadAddress + offset);
            handler(sect);
        }
    }
}

void Image::forEachFixup(void (^rebase)(uint64_t imageOffsetToRebase, bool& stop),
                         void (^bind)(uint64_t imageOffsetToBind, ResolvedSymbolTarget bindTarget, bool& stop),
                         void (^chainedFixups)(uint64_t imageOffsetToStarts, const Array<ResolvedSymbolTarget>& targets, bool& stop),
                         void (^fixupObjCImageInfo)(uint64_t imageOffsetToFixup),
                         void (^fixupObjCProtocol)(uint64_t imageOffsetToBind, ResolvedSymbolTarget bindTarget, bool& stop),
                         void (^fixupObjCSelRef)(uint64_t imageOffsetToFixup, uint32_t selectorIndex, bool inSharedCache, bool& stop),
                         void (^fixupObjCStableSwift)(uint64_t imageOffsetToFixup, bool& stop),
                         void (^fixupObjCMethodList)(uint64_t imageOffsetToFixup, bool& stop)) const
{
    const uint32_t pointerSize = is64() ? 8 : 4;
	uint64_t curRebaseOffset = 0;
	bool stop = false;
    for (const Image::RebasePattern& rebasePat : rebaseFixups()) {
        //fprintf(stderr, " repeat=0x%04X, contig=%d, skip=%d\n", rebasePat.repeatCount, rebasePat.contigCount, rebasePat.skipCount);
        if ( rebasePat.contigCount == 0 ) {
            // note: contigCount==0 means this just advances location
            if ( (rebasePat.repeatCount == 0) && (rebasePat.skipCount == 0) ) {
                // all zeros is special pattern that means reset to rebase offset to zero
                curRebaseOffset = 0;
            }
            else {
                curRebaseOffset += rebasePat.repeatCount * rebasePat.skipCount;
            }
        }
        else {
            for (int r=0; r < rebasePat.repeatCount && !stop; ++r) {
                for (int i=0; i < rebasePat.contigCount && !stop; ++i) {
                    //fprintf(stderr, "  0x%08llX\n", curRebaseOffset);
                    rebase(curRebaseOffset, stop);
                    curRebaseOffset += pointerSize;
                }
                curRebaseOffset += pointerSize * rebasePat.skipCount;
            }
        }
        if ( stop )
            break;
    }
    if ( stop )
        return;

    stop = this->forEachBind(bind);
    if ( stop )
        return;


    if (hasChainedFixups())
        chainedFixups(chainedStartsOffset(), chainedTargets(), stop);

    if ( hasPrecomputedObjC() ) {
        ResolvedSymbolTarget objcProtocolClassTarget;
        uint64_t objcImageInfoVMOffset = 0;
        Array<ProtocolISAFixup> protocolISAFixups;
        Array<Image::SelectorReferenceFixup> selRefFixupEntries;
        Array<Image::ClassStableSwiftFixup> classStableSwiftFixups;
        Array<Image::MethodListFixup> methodListFixups;
        objcFixups(objcProtocolClassTarget, objcImageInfoVMOffset, protocolISAFixups,
                   selRefFixupEntries, classStableSwiftFixups, methodListFixups);

        // Set the objc image info bit to tell libobjc we are optimized
        fixupObjCImageInfo(objcImageInfoVMOffset);


        // First bind all the protocols to the same Protocol class in libobjc
        for (const Image::ProtocolISAFixup& bindPat : protocolISAFixups) {
            uint64_t curBindOffset = bindPat.startVmOffset;
            for (uint16_t i=0; i < bindPat.repeatCount; ++i) {
                fixupObjCProtocol(curBindOffset, objcProtocolClassTarget, stop);
                curBindOffset += (pointerSize * (1 + bindPat.skipCount));
                if ( stop )
                    break;
            }
            if ( stop )
                break;
        }

        for (uintptr_t i = 0, e = selRefFixupEntries.count(); i != e; ++i) {
            Image::SelectorReferenceFixup fixupEntry = selRefFixupEntries[i];
            // Start a new chain
            uint64_t curFixupOffset = fixupEntry.chainStartVMOffset;
            // Now walk the chain until we get a 'next' of 0
            while (i != e) {
                fixupEntry = selRefFixupEntries[++i];
                fixupObjCSelRef(curFixupOffset, fixupEntry.chainEntry.index, fixupEntry.chainEntry.inSharedCache, stop);
                if ( stop )
                    break;
                if ( fixupEntry.chainEntry.next == 0 )
                    break;
                curFixupOffset += (4 * fixupEntry.chainEntry.next);
            }
        }

        // Set classes to have stable Swift
        for (const Image::ClassStableSwiftFixup& bindPat : classStableSwiftFixups) {
            uint64_t curBindOffset = bindPat.startVmOffset;
            for (uint16_t i=0; i < bindPat.repeatCount; ++i) {
                fixupObjCStableSwift(curBindOffset, stop);
                curBindOffset += (pointerSize * (1 + bindPat.skipCount));
                if ( stop )
                    break;
            }
            if ( stop )
                break;
        }

        // Set method lists to be fixed up
        for (const Image::MethodListFixup& bindPat : methodListFixups) {
            uint64_t curBindOffset = bindPat.startVmOffset;
            for (uint16_t i=0; i < bindPat.repeatCount; ++i) {
                fixupObjCMethodList(curBindOffset, stop);
                curBindOffset += (pointerSize * (1 + bindPat.skipCount));
                if ( stop )
                    break;
            }
            if ( stop )
                break;
        }
    }
}

bool Image::forEachBind(void (^bind)(uint64_t imageOffsetToBind, ResolvedSymbolTarget bindTarget, bool& stop)) const
{
    const uint32_t pointerSize = is64() ? 8 : 4;
    bool stop = false;
    for (const Image::BindPattern& bindPat : bindFixups()) {
        uint64_t curBindOffset = bindPat.startVmOffset;
        for (uint16_t i=0; i < bindPat.repeatCount; ++i) {
            bind(curBindOffset, bindPat.target, stop);
            curBindOffset += (pointerSize * (1 + bindPat.skipCount));
            if ( stop )
                break;
        }
        if ( stop )
            break;
    }
    return stop;
}

void Image::forEachTextReloc(void (^rebase)(uint32_t imageOffsetToRebase, bool& stop),
                             void (^bind)(uint32_t imageOffsetToBind, ResolvedSymbolTarget bindTarget, bool& stop)) const
{
    bool stop = false;
    const Array<Image::TextFixupPattern> f = textFixups();
    for (const Image::TextFixupPattern& pat : f) {
        uint32_t curOffset = pat.startVmOffset;
        for (uint16_t i=0; i < pat.repeatCount; ++i) {
            if ( pat.target.raw == 0 )
                rebase(curOffset, stop);
            else
                bind(curOffset, pat.target, stop);
            curOffset += pat.skipCount;
        }
    }
}

const Array<Image::RebasePattern> Image::rebaseFixups() const
{
    uint32_t rebaseFixupsSize;
    Image::RebasePattern* rebaseFixupsContent = (RebasePattern*)findAttributePayload(Type::rebaseFixups, &rebaseFixupsSize);
    uint32_t rebaseCount = rebaseFixupsSize/sizeof(RebasePattern);
    return Array<RebasePattern>(rebaseFixupsContent, rebaseCount, rebaseCount);
}

const Array<Image::BindPattern> Image::bindFixups() const
{
    uint32_t bindFixupsSize;
    BindPattern* bindFixupsContent = (BindPattern*)findAttributePayload(Type::bindFixups, &bindFixupsSize);
    uint32_t bindCount = bindFixupsSize/sizeof(BindPattern);
    return Array<BindPattern>(bindFixupsContent, bindCount, bindCount);
}

uint64_t Image::chainedStartsOffset() const
{
    uint32_t size;
    uint64_t* startsOffset = (uint64_t*)findAttributePayload(Type::chainedStartsOffset, &size);
    if ( startsOffset == nullptr )
        return 0; // means no pre-computed offset to starts table
    assert(size == sizeof(uint64_t));
    return *startsOffset;
}
    
void Image::objcFixups(ResolvedSymbolTarget& objcProtocolClassTarget,
                       uint64_t& objcImageInfoVMOffset,
                       Array<ProtocolISAFixup>& protocolISAFixups,
                       Array<SelectorReferenceFixup>& selRefFixups,
                       Array<ClassStableSwiftFixup>& classStableSwiftFixups,
                       Array<MethodListFixup>& methodListFixups) const
{
    // 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

    if (!hasPrecomputedObjC())
        return;

    uint32_t contentSize;
    const uint8_t* fixupsContent = (uint8_t*)findAttributePayload(Type::objcFixups, &contentSize);
    const uint8_t* fixupsContentEnd = fixupsContent + contentSize;

    // Get the statically sized data
    uint32_t protocolFixupCount = 0;
    uint32_t selRefFixupCount = 0;
    memcpy(&objcProtocolClassTarget, fixupsContent, sizeof(ResolvedSymbolTarget));
    fixupsContent += sizeof(ResolvedSymbolTarget);
    memcpy(&objcImageInfoVMOffset, fixupsContent, sizeof(uint64_t));
    fixupsContent += sizeof(uint64_t);
    memcpy(&protocolFixupCount, fixupsContent, sizeof(uint32_t));
    fixupsContent += sizeof(uint32_t);
    memcpy(&selRefFixupCount, fixupsContent, sizeof(uint32_t));
    fixupsContent += sizeof(uint32_t);

    // Get the protocol fixups
    if ( protocolFixupCount != 0) {
        protocolISAFixups = Array<ProtocolISAFixup>((ProtocolISAFixup*)fixupsContent, protocolFixupCount, protocolFixupCount);
        fixupsContent += (sizeof(ProtocolISAFixup) * protocolFixupCount);
    }

    // Get the selector reference fixups
    if ( selRefFixupCount != 0) {
        selRefFixups = Array<SelectorReferenceFixup>((SelectorReferenceFixup*)fixupsContent, selRefFixupCount, selRefFixupCount);
        fixupsContent += (sizeof(SelectorReferenceFixup) * selRefFixupCount);
    }

    // Old closures end here, but newer ones might have additional fixups
    if (fixupsContent == fixupsContentEnd)
        return;

    uint32_t stableSwiftFixupCount = 0;
    uint32_t methodListFixupCount = 0;
    memcpy(&stableSwiftFixupCount, fixupsContent, sizeof(uint32_t));
    fixupsContent += sizeof(uint32_t);
    memcpy(&methodListFixupCount, fixupsContent, sizeof(uint32_t));
    fixupsContent += sizeof(uint32_t);

    // Get the stable swift fixups
    if ( stableSwiftFixupCount != 0) {
        classStableSwiftFixups = Array<ClassStableSwiftFixup>((ClassStableSwiftFixup*)fixupsContent, stableSwiftFixupCount, stableSwiftFixupCount);
        fixupsContent += (sizeof(ClassStableSwiftFixup) * stableSwiftFixupCount);
    }

    // Get the method list fixups
    if ( methodListFixupCount != 0) {
        methodListFixups = Array<MethodListFixup>((MethodListFixup*)fixupsContent, methodListFixupCount, methodListFixupCount);
        fixupsContent += (sizeof(MethodListFixup) * methodListFixupCount);
    }
}

const Array<Image::ResolvedSymbolTarget> Image::chainedTargets() const
{
    uint32_t size;
    ResolvedSymbolTarget* targetsContent = (ResolvedSymbolTarget*)findAttributePayload(Type::chainedFixupsTargets, &size);
    uint32_t count = size/sizeof(ResolvedSymbolTarget);
    return Array<ResolvedSymbolTarget>(targetsContent, count, count);
}

const Array<Image::TextFixupPattern> Image::textFixups() const
{
    uint32_t fixupsSize;
    TextFixupPattern* fixupsContent = (TextFixupPattern*)findAttributePayload(Type::textFixups, &fixupsSize);
    uint32_t count = fixupsSize/sizeof(TextFixupPattern);
    return Array<TextFixupPattern>(fixupsContent, count, count);
}

bool Image::isOverrideOfDyldCacheImage(ImageNum& imageNum) const
{
	uint32_t size;
	const uint32_t* content = (uint32_t*)findAttributePayload(Type::imageOverride, &size);
	if ( content != nullptr ) {
        assert(size == sizeof(uint32_t));
        imageNum = *content;
        return true;
    }
    return false;
}

void Image::forEachImageToInitBefore(void (^handler)(ImageNum imageToInit, bool& stop)) const
{
    uint32_t size;
    const ImageNum* initBefores = (ImageNum*)findAttributePayload(Type::initBefores, &size);
    if ( initBefores != nullptr ) {
        assert((size % sizeof(ImageNum)) == 0);
        const uint32_t count = size / sizeof(ImageNum);
        bool stop = false;
        for (uint32_t i=0; (i < count) && !stop; ++i) {
            handler(initBefores[i], stop);
        }
    }
}

const char* Image::variantString() const
{
    return (this->fixupsNotEncoded() ? "minimal" : "full");
}

////////////////////////////  ImageArray ////////////////////////////////////////

size_t ImageArray::size() const
{
    return sizeof(TypedBytes) + this->payloadLength;
}

size_t ImageArray::startImageNum() const
{
    return firstImageNum;
}

uint32_t ImageArray::imageCount() const
{
    return count;
}

void ImageArray::forEachImage(void (^callback)(const Image* image, bool& stop)) const
{
    bool stop = false;
    for (uint32_t i=0; i < count && !stop; ++i) {
        const Image* image = (Image*)((uint8_t*)payload() + offsets[i]);
        callback(image, stop);
        if (stop)
            break;
    }
}

bool ImageArray::hasPath(const char* path, ImageNum& num) const
{
    const uint32_t hash = Image::hashFunction(path);
    __block bool found = false;
    forEachImage(^(const Image* image, bool& stop) {
        if ( image->hasPathWithHash(path, hash) ) {
            num   = image->imageNum();
            found = true;
            stop  = true;
        }
    });
    return found;
}

const Image* ImageArray::imageForNum(ImageNum num) const
{
    if (hasRoots) {
        __block const Image* foundImage = nullptr;
        forEachImage(^(const Image *image, bool &stop) {
            if (image->imageNum() == num) {
                foundImage = image;
                stop = true;
            }
        });
        return foundImage;
    }
    if ( num < firstImageNum )
        return nullptr;

    uint32_t index = num - firstImageNum;
    if ( index >= count )
        return nullptr;

    return (Image*)((uint8_t*)payload() + offsets[index]);
}

const Image* ImageArray::findImage(const Array<const ImageArray*> imagesArrays, ImageNum imageNum)
{
    // Search image arrays backwards as the main closure, or dlopen closures, may rebuild closures
    // for shared cache images which are not roots, but whose initialisers must rebuild as they depend
    // on a root
    for (uintptr_t index = imagesArrays.count(); index > 0; --index) {
        const ImageArray* ia = imagesArrays[index - 1];
        if ( const Image* result = ia->imageForNum(imageNum) )
            return result;
    }
    return nullptr;
}

void ImageArray::deallocate() const
{
    ::vm_deallocate(mach_task_self(), (long)this, size());
}

////////////////////////////  Closure ////////////////////////////////////////

size_t Closure::size() const
{
    return sizeof(TypedBytes) + this->payloadLength;
}

const ImageArray* Closure::images() const
{
    __block const TypedBytes* result = nullptr;
    forEachAttribute(^(const TypedBytes* typedBytes, bool& stop) {
        if ( (Type)(typedBytes->type) == Type::imageArray ) {
            result = typedBytes;
            stop = true;
        }
    });

    return (ImageArray*)result;
}

ImageNum Closure::topImageNum() const
{
    uint32_t size;
    const ImageNum* top = (ImageNum*)findAttributePayload(Type::topImage, &size);
    assert(top != nullptr);
    assert(size == sizeof(ImageNum));
    return *top;
}

const Image* Closure::topImage() const
{
    ImageNum                            imageNum      = this->topImageNum();
    const dyld3::closure::ImageArray*   closureImages = this->images();
    return closureImages->imageForNum(imageNum);
}

void Closure::forEachPatchEntry(void (^handler)(const PatchEntry& entry)) const
{
	forEachAttributePayload(Type::cacheOverrides, ^(const void* payload, uint32_t size, bool& stop) {
        assert((size % sizeof(Closure::PatchEntry)) == 0);
        const PatchEntry* patches    = (PatchEntry*)payload;
        const PatchEntry* patchesEnd = (PatchEntry*)((char*)payload + size);
        for (const PatchEntry* p=patches; p < patchesEnd; ++p)
            handler(*p);
	});
}

void Closure::forEachWarning(Closure::Warning::Type type, void (^handler)(const char* warning, bool& stop)) const
{
    forEachAttributePayload(Type::warning, ^(const void* payload, uint32_t size, bool& stop) {
        const Closure::Warning* warning = (const Closure::Warning*)payload;
        if ( warning->type != type )
            return;
        handler(warning->message, stop);
    });
}

void Closure::deallocate() const
{
    ::vm_deallocate(mach_task_self(), (long)this, size());
}


////////////////////////////  LaunchClosure ////////////////////////////////////////

void LaunchClosure::forEachMustBeMissingFile(void (^handler)(const char* path, bool& stop)) const
{
    uint32_t size;
    const char* paths = (const char*)findAttributePayload(Type::missingFiles, &size);
    bool stop = false;
    for (const char* s=paths; s < &paths[size]; ++s) {
        if ( *s != '\0' )
            handler(s, stop);
        if ( stop )
            break;
        s += strlen(s);
    }
}

void LaunchClosure::forEachSkipIfExistsFile(void (^handler)(const SkippedFile& file, bool& stop)) const
{
    uint32_t size;
    const uint64_t* files = (const uint64_t*)findAttributePayload(Type::existingFiles, &size);
    if (files == nullptr)
        return;

    // The first entry is the length of the array
    uint64_t fileCount = *files++;

    // Followed by count number of mod times and inodes
    const char* paths = (const char*)(files + (2 * fileCount));
    bool stop = false;
    for (const char* s=paths; s < &paths[size]; ++s) {
        if ( *s != '\0' ) {
            uint64_t inode = *files++;
            uint64_t mtime = *files++;
            SkippedFile skippedFile = { s, inode, mtime };
            handler(skippedFile, stop);
        }
        if ( stop )
            break;
        s += strlen(s);
    }
}

bool LaunchClosure::builtAgainstDyldCache(uuid_t cacheUUID) const
{
    uint32_t size;
    const uint8_t* uuidBytes = (uint8_t*)findAttributePayload(Type::dyldCacheUUID, &size);
    if ( uuidBytes == nullptr )
        return false;
    assert(size == sizeof(uuid_t));
    memcpy(cacheUUID, uuidBytes, sizeof(uuid_t));
    return true;
}

void LaunchClosure::forEachEnvVar(void (^handler)(const char* keyEqualValue, bool& stop)) const
{
    forEachAttributePayload(Type::envVar, ^(const void* payload, uint32_t size, bool& stop) {
        handler((char*)payload, stop);
    });
}

ImageNum LaunchClosure::libSystemImageNum() const
{
    uint32_t size;
    const ImageNum* num = (ImageNum*)findAttributePayload(Type::libSystemNum, &size);
    assert(num != nullptr);
    assert(size == sizeof(ImageNum));
    return *num;
}

void LaunchClosure::libDyldEntry(Image::ResolvedSymbolTarget& loc) const
{
    uint32_t size;
    const Image::ResolvedSymbolTarget* data = (Image::ResolvedSymbolTarget*)findAttributePayload(Type::libDyldEntry, &size);
    assert(data != nullptr);
    assert(size == sizeof(Image::ResolvedSymbolTarget));
    loc = *data;
}

bool LaunchClosure::mainEntry(Image::ResolvedSymbolTarget& mainLoc) const
{
    uint32_t size;
    const Image::ResolvedSymbolTarget* data = (Image::ResolvedSymbolTarget*)findAttributePayload(Type::mainEntry, &size);
    if ( data == nullptr )
        return false;
    assert(size == sizeof(Image::ResolvedSymbolTarget));
    mainLoc = *data;
    return true;
}

bool LaunchClosure::startEntry(Image::ResolvedSymbolTarget& startLoc) const
{
    uint32_t size;
    const Image::ResolvedSymbolTarget* data = (Image::ResolvedSymbolTarget*)findAttributePayload(Type::startEntry, &size);
    if ( data == nullptr )
        return false;
    assert(size == sizeof(Image::ResolvedSymbolTarget));
    startLoc = *data;
    return true;
}

const LaunchClosure::Flags& LaunchClosure::getFlags() const
{
    uint32_t size;
    const Flags* flags = (Flags*)findAttributePayload(Type::closureFlags, &size);
    assert(flags != nullptr && "Closure missing Flags");
    return *flags;
}

uint32_t LaunchClosure::initialLoadCount() const
{
    return getFlags().initImageCount;
}

bool LaunchClosure::usedAtPaths() const
{
    return getFlags().usedAtPaths;
}

bool LaunchClosure::usedFallbackPaths() const
{
	return getFlags().usedFallbackPaths;
}

bool LaunchClosure::hasInsertedLibraries() const
{
    return getFlags().hasInsertedLibraries;
}

bool LaunchClosure::hasProgramVars(uint32_t& runtimeOffset) const
{
    if ( !getFlags().hasProgVars )
        return false;
    uint32_t payloadSize = 0;
    const uint8_t* buffer = (const uint8_t*)findAttributePayload(Type::progVars, &payloadSize);
    if (buffer == nullptr)
        return false;
    runtimeOffset = *((uint32_t*)buffer);
    return true;
}

bool LaunchClosure::usedInterposing() const
{
    return getFlags().usedInterposing;
}

bool LaunchClosure::hasInterposings() const
{
    __block bool result = false;

    forEachInterposingTuple(^(const InterposingTuple&, bool& stop) {
        result = true;
        stop = true;
    });

    return result;
}

void LaunchClosure::forEachInterposingTuple(void (^handler)(const InterposingTuple& tuple, bool& stop)) const
{
	forEachAttributePayload(Type::interposeTuples, ^(const void* payload, uint32_t size, bool& stop) {
        assert((size % sizeof(InterposingTuple)) == 0);
        uintptr_t count = size / sizeof(InterposingTuple);
        const InterposingTuple* tuples = (InterposingTuple*)payload;
        for (uint32_t i=0; i < count && !stop; ++i) {
            handler(tuples[i], stop);
        }
	});
}
bool LaunchClosure::selectorHashTable(Array<Image::ObjCSelectorImage>& imageNums,
                                      const closure::ObjCSelectorOpt*& hashTable) const {
    uint32_t payloadSize = 0;
    const uint8_t* buffer = (const uint8_t*)findAttributePayload(Type::selectorTable, &payloadSize);
    if (buffer == nullptr)
        return false;

    // Get count
    uint32_t count = 0;
    memcpy(&count, buffer, sizeof(uint32_t));
    buffer += sizeof(uint32_t);

    // Get image nums
    imageNums = Array<Image::ObjCSelectorImage>((Image::ObjCSelectorImage*)buffer, count, count);
    buffer += sizeof(Image::ObjCSelectorImage) * count;

    // Get hash table
    hashTable = (const closure::ObjCSelectorOpt*)buffer;

    return true;
}

bool LaunchClosure::classAndProtocolHashTables(Array<Image::ObjCClassImage>& imageNums,
                                               const ObjCClassOpt*& classHashTable,
                                               const ObjCClassOpt*& protocolHashTable) const {
    // 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 payloadSize = 0;
    const uint8_t* buffer = (const uint8_t*)findAttributePayload(Type::classTable, &payloadSize);
    if (buffer == nullptr)
        return false;

    uint32_t headerSize = sizeof(uint32_t) * 3;

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

    // Get the image nums
    imageNums = Array<Image::ObjCClassImage>((Image::ObjCClassImage*)(buffer + headerSize), numImages, numImages);

    // Get the class hash table if there is one
    if ( offsetToClassTable != 0 )
        classHashTable = (const ObjCClassOpt*)(buffer + offsetToClassTable);

    // Write out out the protocol hash table if there is one
    if ( offsetToProtocolTable != 0 )
        protocolHashTable = (const ObjCClassOpt*)(buffer + offsetToProtocolTable);
    
    return true;
}

void LaunchClosure::duplicateClassesHashTable(const ObjCClassDuplicatesOpt*& duplicateClassesHashTable) const {
    uint32_t payloadSize = 0;
    const uint8_t* buffer = (const uint8_t*)findAttributePayload(Type::duplicateClassesTable, &payloadSize);
    if (buffer == nullptr)
        return;

    duplicateClassesHashTable = (const ObjCClassDuplicatesOpt*)buffer;
}


static bool getContainerLibraryCachesDir(const char* envp[], char libCacheDir[])
{
    // $HOME is root of writable data container
    const char* homeDir = _simple_getenv(envp, "HOME");
    if ( homeDir == nullptr )
        return false;

    // Use realpath to block malicious values like HOME=/tmp/../usr/bin
    char realHomePath[PATH_MAX];
    if ( realpath(homeDir, realHomePath) != nullptr )
        homeDir = realHomePath;
#if TARGET_OS_OSX
    // <rdar://problem/66593232> iOS apps on Apple Silicon macOS have a different data container location
    if ( strstr(homeDir, "/Library/Containers/") == nullptr )
        return false;
#else
    // <rdar://problem/47688842> dyld3 should only save closures to disk for containerized apps
    if ( strncmp(homeDir, "/private/var/mobile/Containers/Data/", 36) != 0 )
        return false;
#endif

    // return $HOME/Library/Caches/
    strlcpy(libCacheDir, homeDir, PATH_MAX);
    strlcat(libCacheDir, "/Library/Caches", PATH_MAX);
    return true;
}

bool LaunchClosure::buildClosureCachePath(const char* mainExecutablePath, const char* envp[],
                                          bool makeDirsIfMissing, char closurePath[])
{
    // get path to data container's Library/Caches/ dir
    if ( !getContainerLibraryCachesDir(envp, closurePath) )
        return false;

    // make sure XXX/Library/Caches/ exists
    struct stat statbuf;
    if ( dyld3::stat(closurePath, &statbuf) != 0 )
        return false;

    // add dyld sub-dir
    strlcat(closurePath, "/com.apple.dyld", PATH_MAX);
    if ( makeDirsIfMissing ) {
        if ( dyld3::stat(closurePath, &statbuf) != 0 ) {
            if ( ::mkdir(closurePath, S_IRWXU) != 0 )
                return false;
        }
    }

    // add <prog-name> + ".closure"
    const char* leafName = strrchr(mainExecutablePath, '/');
    if ( leafName == nullptr )
        leafName = mainExecutablePath;
    else
        ++leafName;
    strlcat(closurePath, "/", PATH_MAX);
    strlcat(closurePath, leafName, PATH_MAX);

    strlcat(closurePath, ".closure", PATH_MAX);
    return true;
}


////////////////////////////  ObjCStringTable ////////////////////////////////////////
    
uint32_t ObjCStringTable::hash(const char *key, size_t keylen) const
{
    uint64_t val = objc_opt::lookup8((uint8_t*)key, keylen, salt);
    uint32_t index = (uint32_t)((shift == 64) ? 0 : (val>>shift)) ^ scramble[tab[val&mask]];
    return index;
}

const char* ObjCStringTable::getString(const char* selName, const Array<uintptr_t>& baseAddresses) const {
    StringTarget target = getPotentialTarget(selName);
    if (target == sentinelTarget)
        return nullptr;

    dyld3::closure::Image::ObjCImageOffset imageAndOffset;
    imageAndOffset.raw = target;

    uintptr_t sectionBaseAddress = baseAddresses[imageAndOffset.imageIndex];

    const char* value = (const char*)(sectionBaseAddress + imageAndOffset.imageOffset);
    if (!strcmp(selName, value))
        return value;
    return nullptr;
}

////////////////////////////  ObjCSelectorOpt ////////////////////////////////////////
bool ObjCSelectorOpt::getStringLocation(uint32_t index, const Array<closure::Image::ObjCSelectorImage>& selImages,
                                        ImageNum& imageNum, uint64_t& vmOffset) const {
    if ( index >= capacity )
        return false;

    StringTarget target = targets()[index];
    if ( target == indexNotFound )
        return false;

    dyld3::closure::Image::ObjCImageOffset imageAndOffset;
    imageAndOffset.raw = target;

    imageNum = selImages[imageAndOffset.imageIndex].imageNum;
    vmOffset = selImages[imageAndOffset.imageIndex].offset + imageAndOffset.imageOffset;
    return true;;
}

void ObjCSelectorOpt::forEachString(const Array<Image::ObjCSelectorImage>& selectorImages,
                                    void (^callback)(uint64_t selVMOffset, ImageNum imageNum)) const {
    dyld3::Array<StringTarget> stringTargets = targets();
    for (unsigned i = 0; i != capacity; ++i) {
        dyld3::closure::Image::ObjCImageOffset imageAndOffset;
        imageAndOffset.raw = stringTargets[i];

        if (imageAndOffset.raw == sentinelTarget)
            continue;

        callback(selectorImages[imageAndOffset.imageIndex].offset + imageAndOffset.imageOffset,
                 selectorImages[imageAndOffset.imageIndex].imageNum);
    }
}

////////////////////////////  ObjCClassOpt ////////////////////////////////////////

void ObjCClassOpt::forEachClass(const char* className, const Array<std::pair<uintptr_t, uintptr_t>>& nameAndDataBaseAddresses,
                                void (^callback)(void* classPtr, bool isLoaded, bool* stop)) const {
    uint32_t index = getIndex(className);
    if ( index == closure::ObjCStringTable::indexNotFound )
        return;

    StringTarget target = targets()[index];
    if ( target == sentinelTarget )
        return;

    // We have a potential target.  First check if the name is an exact match given the hash matched
    closure::Image::ObjCImageOffset classNameImageAndOffset;
    classNameImageAndOffset.raw = target;

    uintptr_t nameBaseAddress = 0;
    uintptr_t dataBaseAddress = 0;
    std::tie(nameBaseAddress, dataBaseAddress) = nameAndDataBaseAddresses[classNameImageAndOffset.imageIndex];

    const char* value = (const char*)(nameBaseAddress + classNameImageAndOffset.imageOffset);
    if ( strcmp(className, value) != 0 )
        return;

    // The name matched so now call the handler on all the classes for this name
    Array<closure::ObjCClassOpt::ClassTarget> classOffsetsArray = classOffsets();
    Array<closure::ObjCClassOpt::ClassTarget> duplicatesArray = duplicateOffsets(duplicateCount());

    const closure::ObjCClassOpt::ClassTarget& classOffset = classOffsetsArray[index];
    if (classOffset.classData.isDuplicate == 0) {
        // This class has a single implementation
        void* classImpl = (void*)(dataBaseAddress + classOffset.classData.imageOffset);
        bool stop = false;
        callback(classImpl, true, &stop);
    } else {
        // This class has mulitple implementations
        uint32_t duplicateCount = classOffset.duplicateData.count;
        uint32_t duplicateStartIndex = classOffset.duplicateData.index;
        for (uint32_t dupeIndex = 0; dupeIndex != duplicateCount; ++dupeIndex) {
            closure::ObjCClassOpt::ClassTarget& duplicateClass = duplicatesArray[duplicateStartIndex + dupeIndex];

            std::tie(nameBaseAddress, dataBaseAddress) = nameAndDataBaseAddresses[duplicateClass.classData.imageIndex];
            void* classImpl = (void*)(dataBaseAddress + duplicateClass.classData.imageOffset);
            bool            stop                = false;
            callback(classImpl, true, &stop);
            if (stop)
                break;
        }
    }
}

void ObjCClassOpt::forEachClass(const Array<Image::ObjCClassImage>& classImages,
                                void (^nameCallback)(uint64_t classNameVMOffset, ImageNum imageNum),
                                void (^implCallback)(uint64_t classVMOffset, ImageNum imageNum)) const {

    dyld3::Array<StringTarget> stringTargets = targets();
    dyld3::Array<ObjCClassOpt::ClassTarget> classOffsetsArray = classOffsets();
    dyld3::Array<ObjCClassOpt::ClassTarget> duplicatesArray = duplicateOffsets(duplicateCount());
    for (unsigned i = 0; i != capacity; ++i) {
        dyld3::closure::Image::ObjCImageOffset classNameImageAndOffset;
        classNameImageAndOffset.raw = stringTargets[i];

        if (classNameImageAndOffset.raw == sentinelTarget)
            continue;

        nameCallback(classImages[classNameImageAndOffset.imageIndex].offsetOfClassNames + classNameImageAndOffset.imageOffset,
                     classImages[classNameImageAndOffset.imageIndex].imageNum);

        // Walk each class for this key
        const ObjCClassOpt::ClassTarget& classOffset = classOffsetsArray[i];
        if (classOffset.classData.isDuplicate == 0) {
            // This class has a single implementation
            implCallback(classImages[classOffset.classData.imageIndex].offsetOfClasses + classOffset.classData.imageOffset,
                         classImages[classOffset.classData.imageIndex].imageNum);
        } else {
            // This class has mulitple implementations
            uint32_t duplicateCount = classOffset.duplicateData.count;
            uint32_t duplicateStartIndex = classOffset.duplicateData.index;
            for (uint32_t dupeIndex = 0; dupeIndex != duplicateCount; ++dupeIndex) {
                ObjCClassOpt::ClassTarget& duplicateClass = duplicatesArray[duplicateStartIndex + dupeIndex];
                implCallback(classImages[duplicateClass.classData.imageIndex].offsetOfClasses + duplicateClass.classData.imageOffset,
                             classImages[duplicateClass.classData.imageIndex].imageNum);
            }
        }
    }
}

////////////////////////////  ObjCClassDuplicatesOpt ////////////////////////////////////////

bool ObjCClassDuplicatesOpt::getClassLocation(const char* className, const objc_opt::objc_opt_t* objCOpt, void*& classImpl) const {
    uint32_t potentialTarget = getPotentialTarget(className);
    if (potentialTarget == sentinelTarget)
        return false;

    objc_opt::objc_clsopt_t* clsOpt = objCOpt->clsopt();

    Image::ObjCDuplicateClass duplicateClass;
    duplicateClass.raw = potentialTarget;

    const char* sharedCacheClassName = clsOpt->getClassNameForIndex(duplicateClass.sharedCacheClassOptIndex);
    if (strcmp(className, sharedCacheClassName) != 0)
        return false;

    classImpl = clsOpt->getClassForIndex(duplicateClass.sharedCacheClassOptIndex, duplicateClass.sharedCacheClassDuplicateIndex);
    return true;
}

void ObjCClassDuplicatesOpt::forEachClass(void (^callback)(Image::ObjCDuplicateClass duplicateClass)) const {
    dyld3::Array<StringTarget> stringTargets = targets();
    for (unsigned i = 0; i != capacity; ++i) {
        StringTarget target = stringTargets[i];
        if ( target == sentinelTarget )
            continue;
        Image::ObjCDuplicateClass duplicateClass;
        duplicateClass.raw = (uint32_t)target;
        callback(duplicateClass);
    }
}


} // namespace closure
} // namespace dyld3