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
/*
 * Copyright (c) 2019-2020 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */

#include <TargetConditionals.h>

#if !TARGET_OS_EXCLAVEKIT

#include <assert.h>
#include <unistd.h>
#include <sys/types.h>

#include "Defines.h"
#include "Header.h"
#include "Loader.h"
#include "PrebuiltLoader.h"
#include "JustInTimeLoader.h"
#include "MachOFile.h"
#include "BumpAllocator.h"
#include "DyldProcessConfig.h"
#include "DyldRuntimeState.h"
#include "OptimizerObjC.h"
#include "ObjCVisitor.h"
#include "PerfectHash.h"
#include "PrebuiltObjC.h"
#include "objc-shared-cache.h"

using mach_o::Header;

#if SUPPORT_PREBUILTLOADERS || BUILDING_UNIT_TESTS || BUILDING_CACHE_BUILDER_UNIT_TESTS
using dyld3::OverflowSafeArray;
typedef dyld4::PrebuiltObjC::ObjCOptimizerImage ObjCOptimizerImage;

// This holds all the maps we are going to serialize.
namespace prebuilt_objc
{

void forEachSelectorStringEntry(const void* selMap, void (^handler)(const PrebuiltLoader::BindTargetRef& target))
{
    // The on-disk map is really an ObjCSelectorMapOnDisk
    const ObjCSelectorMapOnDisk map(selMap);
    map.forEachEntry(^(const ObjCSelectorMapOnDisk::NodeT& node) {
        handler(node.first.stringTarget);
    });
}

#if SUPPORT_VM_LAYOUT

const char* findSelector(dyld4::RuntimeState* state, const ObjCSelectorMapOnDisk& map,
                         const char* selectorName)
{
    auto it = map.find((void*)state, selectorName);
    if ( it == map.end() )
        return nullptr;

    return (const char*)it->first.stringTarget.value(*state);
}

void forEachClass(dyld4::RuntimeState* state, const ObjCClassMapOnDisk& classMap, const char* className,
                  void (^handler)(const dyld3::Array<const PrebuiltLoader::BindTargetRef*>& values))
{
    classMap.forEachEntry(state, className, ^(const dyld3::Array<const ObjCObjectOnDiskLocation*>& values) {
        if ( values.empty() )
            return;

        STACK_ALLOC_ARRAY(const PrebuiltLoader::BindTargetRef*, newValues, values.count());
        for ( const ObjCObjectOnDiskLocation* value : values )
            newValues.push_back(&value->objectLocation);

        handler(newValues);
    });
}

void forEachProtocol(dyld4::RuntimeState* state, const ObjCProtocolMapOnDisk& protocolMap, const char* protocolName,
                     void (^handler)(const dyld3::Array<const PrebuiltLoader::BindTargetRef*>& values))
{
    protocolMap.forEachEntry(state, protocolName, ^(const dyld3::Array<const ObjCObjectOnDiskLocation*>& values) {
        if ( values.empty() )
            return;

        STACK_ALLOC_ARRAY(const PrebuiltLoader::BindTargetRef*, newValues, values.count());
        for ( const ObjCObjectOnDiskLocation* value : values )
            newValues.push_back(&value->objectLocation);

        handler(newValues);
    });
}

#endif // SUPPORT_VM_LAYOUT

void forEachClass(const void* classMap,
                  void (^handler)(const PrebuiltLoader::BindTargetRef& nameTarget,
                                  const dyld3::Array<const PrebuiltLoader::BindTargetRef*>& values))
{
    // The on-disk map is really an ObjCClassMapOnDisk
    const ObjCClassMapOnDisk map(classMap);
    map.forEachEntry(^(const ObjCStringKeyOnDisk& key, const dyld3::Array<const ObjCObjectOnDiskLocation*>& values) {
        STACK_ALLOC_ARRAY(const PrebuiltLoader::BindTargetRef*, newValues, values.count());
        for ( const ObjCObjectOnDiskLocation* value : values )
            newValues.push_back(&value->objectLocation);
        handler(key.stringTarget, newValues);
    });
}

void forEachProtocol(const void* protocolMap,
                     void (^handler)(const PrebuiltLoader::BindTargetRef& nameTarget,
                                     const dyld3::Array<const PrebuiltLoader::BindTargetRef*>& values))
{
    // The on-disk map is really an ObjCProtocolMapOnDisk
    const ObjCProtocolMapOnDisk map(protocolMap);
    map.forEachEntry(^(const ObjCStringKeyOnDisk& key, const dyld3::Array<const ObjCObjectOnDiskLocation*>& values) {
        STACK_ALLOC_ARRAY(const PrebuiltLoader::BindTargetRef*, newValues, values.count());
        for ( const ObjCObjectOnDiskLocation* value : values )
            newValues.push_back(&value->objectLocation);
        handler(key.stringTarget, newValues);
    });
}

uint64_t hashStringKey(const std::string_view& str)
{
    return murmurHash(str.data(), (int)str.size(), 0);
}

} // namespace prebuilt_objc

namespace dyld4 {

//////////////////////// ObjCOptimizerImage /////////////////////////////////

ObjCOptimizerImage::ObjCOptimizerImage(const JustInTimeLoader* jitLoader, uint64_t loadAddress, uint32_t pointerSize)
    : jitLoader(jitLoader)
    , pointerSize(pointerSize)
    , loadAddress(loadAddress)
{
}

#if BUILDING_CACHE_BUILDER || BUILDING_CLOSURE_UTIL
void ObjCOptimizerImage::calculateMissingWeakImports(RuntimeState& state)
{
    const mach_o::MachOFileRef& mf = jitLoader->mf(state);

    // build targets table
    STACK_ALLOC_OVERFLOW_SAFE_ARRAY(bool, bindTargetsAreWeakImports, 512);
    STACK_ALLOC_OVERFLOW_SAFE_ARRAY(bool, overrideBindTargetsAreWeakImports, 16);
    __block bool                           foundMissingWeakImport = false;
    bool                                   allowLazyBinds         = false;
    JustInTimeLoader::CacheWeakDefOverride cacheWeakDefFixup      = ^(uint32_t cachedDylibIndex, uint32_t cachedDylibVMOffset, const JustInTimeLoader::ResolvedSymbol& target) {};
    jitLoader->forEachBindTarget(diag, state, cacheWeakDefFixup, allowLazyBinds, ^(const JustInTimeLoader::ResolvedSymbol& target, bool& stop) {
        if ( (target.kind == Loader::ResolvedSymbol::Kind::bindAbsolute) && (target.targetRuntimeOffset == 0) ) {
            foundMissingWeakImport = true;
            bindTargetsAreWeakImports.push_back(true);
        }
        else {
            bindTargetsAreWeakImports.push_back(false);
        }
    }, ^(const JustInTimeLoader::ResolvedSymbol& target, bool& stop) {
        if ( (target.kind == Loader::ResolvedSymbol::Kind::bindAbsolute) && (target.targetRuntimeOffset == 0) ) {
            foundMissingWeakImport = true;
            overrideBindTargetsAreWeakImports.push_back(true);
        }
        else {
            overrideBindTargetsAreWeakImports.push_back(false);
        }
    });
    if ( diag.hasError() )
        return;

    if ( foundMissingWeakImport ) {
        jitLoader->withLayout(diag, state, ^(const mach_o::Layout& layout) {
            mach_o::Fixups fixups(layout);

            if ( mf->hasChainedFixups() ) {
                // walk all chains
                auto handler = ^(dyld3::MachOFile::ChainedFixupPointerOnDisk *fixupLocation,
                                 InputDylibVMAddress fixupVMAddr, uint16_t pointerFormat,
                                 bool &stopChain) {
                    uint32_t bindOrdinal;
                    int64_t  addend;
                    if ( fixupLocation->isBind(pointerFormat, bindOrdinal, addend) ) {
                        if ( bindOrdinal < bindTargetsAreWeakImports.count() ) {
                            if ( bindTargetsAreWeakImports[bindOrdinal] )
                                missingWeakImports.insert(fixupVMAddr);
                        }
                        else {
                            diag.error("out of range bind ordinal %d (max %llu)", bindOrdinal, bindTargetsAreWeakImports.count());
                            stopChain = true;
                        }
                    }
                };

                fixups.withChainStarts(diag, ^(const dyld_chained_starts_in_image* startsInfo) {
                    fixups.forEachFixupChainSegment(diag, startsInfo, ^(const dyld_chained_starts_in_segment *segInfo,
                                                                        uint32_t segIndex, bool &stopSegment) {
                        InputDylibVMAddress segmentVMAddr(layout.segments[segIndex].vmAddr);
                        auto adaptor = ^(dyld3::MachOFile::ChainedFixupPointerOnDisk *fixupLocation,
                                         uint64_t fixupSegmentOffset,
                                         bool &stopChain) {
                            InputDylibVMAddress fixupVMAddr = segmentVMAddr + VMOffset(fixupSegmentOffset);
                            handler(fixupLocation, fixupVMAddr, segInfo->pointer_format, stopChain);
                        };
                        fixups.forEachFixupInSegmentChains(diag, segInfo, segIndex, true, adaptor);
                    });
                });
                if ( diag.hasError() )
                    return;
            } else if ( mf->hasOpcodeFixups() ) {
                // process all bind opcodes
                fixups.forEachBindLocation_Opcodes(diag, ^(uint64_t runtimeOffset, uint32_t segmentIndex,
                                                           unsigned targetIndex, bool& fixupsStop) {
                    if ( targetIndex < bindTargetsAreWeakImports.count() ) {
                        if ( bindTargetsAreWeakImports[targetIndex] ) {
                            InputDylibVMAddress fixupVMAddr(layout.textUnslidVMAddr() + runtimeOffset);
                            missingWeakImports.insert(fixupVMAddr);
                        }
                    }
                    else {
                        diag.error("out of range bind ordinal %d (max %llu)", targetIndex, bindTargetsAreWeakImports.count());
                        fixupsStop = true;
                    }
                }, ^(uint64_t runtimeOffset, uint32_t segmentIndex,
                     unsigned overrideBindTargetIndex, bool& fixupsStop) {
                    if ( overrideBindTargetIndex < overrideBindTargetsAreWeakImports.count() ) {
                        if ( overrideBindTargetsAreWeakImports[overrideBindTargetIndex] ) {
                            InputDylibVMAddress fixupVMAddr(layout.textUnslidVMAddr() + runtimeOffset);
                            missingWeakImports.insert(fixupVMAddr);
                        }
                    }
                    else {
                        diag.error("out of range bind ordinal %d (max %llu)", overrideBindTargetIndex, overrideBindTargetsAreWeakImports.count());
                        fixupsStop = true;
                    }
                });
                if ( diag.hasError() )
                    return;
            }
            else {
                // process external relocations
                fixups.forEachBindLocation_Relocations(diag, ^(uint64_t runtimeOffset, unsigned targetIndex, bool& fixupsStop) {
                    if ( targetIndex < bindTargetsAreWeakImports.count() ) {
                        if ( bindTargetsAreWeakImports[targetIndex] ) {
                            InputDylibVMAddress fixupVMAddr(layout.textUnslidVMAddr() + runtimeOffset);
                            missingWeakImports.insert(fixupVMAddr);
                        }
                    }
                    else {
                        diag.error("out of range bind ordinal %d (max %llu)", targetIndex, bindTargetsAreWeakImports.count());
                        fixupsStop = true;
                    }
                });
                if ( diag.hasError() )
                    return;
            }
        });
    }
}
#endif // (BUILDING_CACHE_BUILDER || BUILDING_CLOSURE_UTIL)

bool ObjCOptimizerImage::isNull(InputDylibVMAddress vmAddr, const void* address) const
{
#if BUILDING_CACHE_BUILDER || BUILDING_CLOSURE_UTIL
    return (missingWeakImports.find(vmAddr) != missingWeakImports.end());
#elif BUILDING_DYLD
    // In dyld, we are live, so we can just check if we point to a null value
    uintptr_t* pointer = (uintptr_t*)address;
    return (*pointer == 0);
#else
    // FIXME: Have we been slide or not in the non-dyld case?
    assert(0);
    return false;
#endif
}

void ObjCOptimizerImage::visitReferenceToObjCSelector(const objc::SelectorHashTable* objcSelOpt,
                                                      PrebuiltObjC::SelectorMapTy& appSelectorMap,
                                                      VMOffset selectorReferenceRuntimeOffset, VMOffset selectorStringRuntimeOffset,
                                                      const char* selectorString)
{

    // fprintf(stderr, "selector: %p -> %p %s\n", (void*)selectorReferenceRuntimeOffset, (void*)selectorStringRuntimeOffset, selectorString);
    if ( const char* sharedCacheSelector = objcSelOpt->get(selectorString) ) {
        // We got the selector from the cache so add a fixup to point there.
        // We use an absolute bind here, to reference the offset from the shared cache selector table base
        uint64_t sharedCacheOffset = (uint64_t)sharedCacheSelector - (uint64_t)objcSelOpt;
        PrebuiltLoader::BindTargetRef bindTarget = PrebuiltLoader::BindTargetRef::makeAbsolute(sharedCacheOffset);

        //printf("Overriding fixup at 0x%08llX to cache offset 0x%08llX\n", selectorUseImageOffset, (uint64_t)objcSelOpt->getEntryForIndex(cacheSelectorIndex) - (uint64_t)state.config.dyldCache());
        selectorFixups.push_back(bindTarget);
        return;
    }

    // See if this selector is already in the app map from a previous image
    prebuilt_objc::ObjCStringKey selectorMapKey { selectorString };
    auto appSelectorIt = appSelectorMap.find(selectorMapKey);
    if ( appSelectorIt != appSelectorMap.end() ) {
        // This selector was found in a previous image, so use it here.

        //printf("Overriding fixup at 0x%08llX to other image\n", selectorUseImageOffset);
        selectorFixups.push_back(PrebuiltLoader::BindTargetRef(appSelectorIt->second.nameLocation));
        return;
    }

    // See if this selector is already in the map for this image
    prebuilt_objc::ObjCSelectorLocation selectorMapValue = { Loader::BindTarget() };
    auto itAndInserted = selectorMap.insert({ selectorMapKey, selectorMapValue });
    if ( itAndInserted.second ) {
        // We added the selector so its pointing in to our own image.
        Loader::BindTarget target;
        target.loader               = jitLoader;
        target.runtimeOffset        = selectorStringRuntimeOffset.rawValue();
        itAndInserted.first->second.nameLocation = target;

        // We'll add a fixup anyway as we want a sel ref fixup for every entry in the sel refs section

        //printf("Fixup at 0x%08llX to '%s' offset 0x%08llX\n", selectorUseImageOffset, findLoadedImage(target.image.imageNum).path(), target.image.offset);
        selectorFixups.push_back(PrebuiltLoader::BindTargetRef(target));
        return;
    }

    // This selector was found elsewhere in our image.  As we want a fixup for every selref, we'll
    // add one here too
    Loader::BindTarget& target = itAndInserted.first->second.nameLocation;

    //printf("Overriding fixup at 0x%08llX to '%s' offset 0x%08llX\n", selectorUseImageOffset, findLoadedImage(target.image.imageNum).path(), target.image.offset);
    selectorFixups.push_back(PrebuiltLoader::BindTargetRef(target));
}

// Check if the given class is in an image loaded in the shared cache.
// If so, add the class to the duplicate map
static void checkForDuplicateClass(const VMAddress dyldCacheBaseAddress,
                                   const char* className, const objc::ClassHashTable* objcClassOpt,
                                   PrebuiltObjC::SharedCacheImagesMapTy& sharedCacheImagesMap,
                                   PrebuiltObjC::DuplicateClassesMapTy& duplicateSharedCacheClasses,
                                   ObjCOptimizerImage& image)
{
    objcClassOpt->forEachClass(className,
                               ^(uint64_t classCacheOffset, uint16_t dylibObjCIndex, bool &stopObjects) {
        // Check if this image is loaded
        if ( auto cacheIt = sharedCacheImagesMap.find(dylibObjCIndex); cacheIt != sharedCacheImagesMap.end() ) {
            const Loader* ldr = cacheIt->second.second;

            // We have a duplicate class, so check if we've already got it in our map.
            if ( duplicateSharedCacheClasses.find(className) == duplicateSharedCacheClasses.end() ) {
                // We haven't seen this one yet, so record it in the map for this image
                VMAddress cacheDylibUnslidVMAddr = cacheIt->second.first;
                VMAddress          classVMAddr = dyldCacheBaseAddress + VMOffset(classCacheOffset);
                VMOffset           classDylibVMOffset = classVMAddr - cacheDylibUnslidVMAddr;
                Loader::BindTarget classTarget   = { ldr, classDylibVMOffset.rawValue() };
                image.duplicateSharedCacheClassMap.insert({ className, classTarget });
            }

            stopObjects = true;
        }
    });
}

void ObjCOptimizerImage::visitClass(const VMAddress dyldCacheBaseAddress,
                                    const objc::ClassHashTable* objcClassOpt,
                                    SharedCacheImagesMapTy& sharedCacheImagesMap,
                                    DuplicateClassesMapTy& duplicateSharedCacheClasses,
                                    InputDylibVMAddress classVMAddr, InputDylibVMAddress classNameVMAddr, const char* className)
{

    // If the class also exists in a shared cache image which is loaded, then objc
    // would have found that one, regardless of load order.
    // In that case, we still add this class to the map, but also track which shared cache class it is a duplicate of
    checkForDuplicateClass(dyldCacheBaseAddress, className, objcClassOpt, sharedCacheImagesMap,
                           duplicateSharedCacheClasses, *this);

    VMOffset classNameVMOffset   = classNameVMAddr - loadAddress;
    VMOffset classObjectVMOffset = classVMAddr - loadAddress;
    classLocations.push_back({ className, classNameVMOffset, classObjectVMOffset });
}

static bool protocolIsInSharedCache(const char* protocolName,
                                    const objc::ProtocolHashTable* objcProtocolOpt,
                                    PrebuiltObjC::SharedCacheImagesMapTy& sharedCacheImagesMap)
{
    __block bool foundProtocol = false;
    objcProtocolOpt->forEachProtocol(protocolName,
                                     ^(uint64_t classCacheOffset, uint16_t dylibObjCIndex, bool &stopObjects) {
        // Check if this image is loaded
        if ( auto cacheIt = sharedCacheImagesMap.find(dylibObjCIndex); cacheIt != sharedCacheImagesMap.end() ) {
            foundProtocol = true;
            stopObjects = true;
        }
    });
    return foundProtocol;
}

void ObjCOptimizerImage::visitProtocol(const objc::ProtocolHashTable* objcProtocolOpt,
                                       SharedCacheImagesMapTy& sharedCacheImagesMap,
                                       InputDylibVMAddress protocolVMAddr, InputDylibVMAddress protocolNameVMAddr,
                                       const char* protocolName)
{

    uint32_t protocolIndex = (uint32_t)protocolISAFixups.count();
    protocolISAFixups.push_back(false);

    // If the protocol also exists in a shared cache image which is loaded, then objc
    // would have found that one, regardless of load order.  So we can just skip this one.
    if ( protocolIsInSharedCache(protocolName, objcProtocolOpt, sharedCacheImagesMap) )
        return;

    VMOffset protocolNameVMOffset   = protocolNameVMAddr - loadAddress;
    VMOffset protocolObjectVMOffset = protocolVMAddr - loadAddress;
    protocolLocations.push_back({ protocolName, protocolNameVMOffset, protocolObjectVMOffset });

    // Record which index this protocol uses in protocolISAFixups.  Later we can change its entry if we
    // choose this protocol as the canonical definition.
    protocolIndexMap[protocolObjectVMOffset] = protocolIndex;
}

//////////////////////// ObjC Optimisations /////////////////////////////////

// HACK!: dyld3 used to know if each image in a closure has been rebased or not when it was building the closure
// Now we try to make good guesses based on whether its the shared cache or not, and which binary is executing this code
#if 0
static bool hasBeenRebased(const Loader* ldr)
{
#if BUILDING_DYLD
    // In dyld, we always run this analysis after everything has already been fixed up
    return true;
#elif BUILDING_CLOSURE_UTIL
    // dyld_closure_util assumes that on disk binaries haven't had fixups applied
    return false;
#else
    // In the shared cache builder, nothing has been rebased yet
    return false;
#endif
}
#endif

static objc_visitor::Visitor makeObjCVisitor(Diagnostics& diag, RuntimeState& state,
                                             const Loader* ldr)
{

#if POINTERS_ARE_UNSLID
    const dyld3::MachOAnalyzer* dylibMA = ldr->analyzer(state);

    const DyldSharedCache* dyldCache = (const DyldSharedCache*)state.config.dyldCache.addr;
    uint64_t sharedCacheRelativeSelectorBaseVMAddress = dyldCache->sharedCacheRelativeSelectorBaseVMAddress();
    objc_visitor::Visitor objcVisitor(dyldCache, dylibMA, VMAddress(sharedCacheRelativeSelectorBaseVMAddress));
    return objcVisitor;
#elif SUPPORT_VM_LAYOUT
    const dyld3::MachOAnalyzer* dylibMA = ldr->analyzer(state);

    objc_visitor::Visitor objcVisitor(dylibMA);
    return objcVisitor;
#else
    const dyld3::MachOFile* dylibMF = ldr->mf(state);
    return dylibMF->makeObjCVisitor(diag);
#endif
}

static void optimizeObjCSelectors(RuntimeState& state,
                                  const objc::SelectorHashTable* objcSelOpt,
                                  PrebuiltObjC::SelectorMapTy& appSelectorMap,
                                  ObjCOptimizerImage&                image)
{

    const Header*   hdr         = (const Header*)image.jitLoader->mf(state);
    uint32_t        pointerSize = hdr->pointerSize();

    // The legacy (objc1) codebase uses a bunch of sections we don't want to reason about.  If we see them just give up.
    __block bool foundBadSection = false;
    hdr->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
        if ( sectInfo.segmentName != "__OBJC" )
            return;
        if ( sectInfo.sectionName == "__module_info" ) {
            foundBadSection = true;
            stop            = true;
            return;
        }
        if ( sectInfo.sectionName == "__protocol" ) {
            foundBadSection = true;
            stop            = true;
            return;
        }
        if ( sectInfo.sectionName == "__message_refs" ) {
            foundBadSection = true;
            stop            = true;
            return;
        }
    });
    if ( foundBadSection ) {
        image.diag.error("Old objc section");
        return;
    }

    // Visit the message refs
    // Note this isn't actually supported in libobjc any more.  Its logic for deciding whether to support it is if this is true:
    // #if (defined(__x86_64__) && (TARGET_OS_OSX || TARGET_OS_SIMULATOR))
    // So to keep it simple, lets only do this walk if we are x86_64
    if ( hdr->isArch("x86_64") || hdr->isArch("x86_64h") ) {
        if ( hdr->hasObjCMessageReferences() ) {
            image.diag.error("Cannot handle message refs");
            return;
        }
    }

    // FIXME: Don't make a duplicate one of these if we can pass one in instead
    __block objc_visitor::Visitor objcVisitor = makeObjCVisitor(image.diag, state, image.jitLoader);
    if ( image.diag.hasError() )
        return;

    // We only record selector references for __objc_selrefs and pointer based method lists.
    // If we find a relative method list pointing outside of __objc_selrefs then we give up for now
    uint64_t selRefsStartRuntimeOffset = image.binaryInfo.selRefsRuntimeOffset;
    uint64_t selRefsEndRuntimeOffset   = selRefsStartRuntimeOffset + (pointerSize * image.binaryInfo.selRefsCount);
    auto     visitRelativeMethod = ^(const objc_visitor::Method& method, bool& stop) {
        VMAddress selectorRefVMAddress = method.getNameSelRefVMAddr(objcVisitor);
        VMOffset selectorReferenceRuntimeOffset = selectorRefVMAddress - VMAddress(image.loadAddress.rawValue());
        if ( (selectorReferenceRuntimeOffset.rawValue() < selRefsStartRuntimeOffset)
            || (selectorReferenceRuntimeOffset.rawValue() >= selRefsEndRuntimeOffset) ) {
            image.diag.error("Cannot handle relative method list pointing outside of __objc_selrefs");
            stop = true;
        }
    };

    auto visitMethodList = ^(const objc_visitor::MethodList& methodList,
                             bool& hasPointerBasedMethodList, bool &stop) {
        if ( methodList.numMethods() == 0 )
            return;

        if ( methodList.usesRelativeOffsets() ) {
            // Check relative method lists
            uint32_t numMethods = methodList.numMethods();
            for ( uint32_t i = 0; i != numMethods; ++i ) {
                const objc_visitor::Method& method = methodList.getMethod(objcVisitor, i);
                visitRelativeMethod(method, stop);
            }
        } else {
            // Record if we found a pointer based method list.  This lets us skip walking method lists later if
            // they are all relative method lists
            hasPointerBasedMethodList = true;
        }
    };

    if ( image.binaryInfo.classListCount != 0 ) {
        __block bool hasPointerBasedMethodList = false;
        objcVisitor.forEachClassAndMetaClass(^(const objc_visitor::Class &objcClass, bool &stopClass) {
            objc_visitor::MethodList methodList = objcClass.getBaseMethods(objcVisitor);
            visitMethodList(methodList, hasPointerBasedMethodList, stopClass);
        });
        image.binaryInfo.hasClassMethodListsToUnique     = hasPointerBasedMethodList;
        image.binaryInfo.hasClassMethodListsToSetUniqued = hasPointerBasedMethodList;
    }

    if ( image.binaryInfo.categoryCount != 0 ) {
        __block bool hasPointerBasedMethodList = false;
        objcVisitor.forEachCategory(^(const objc_visitor::Category& objcCategory, bool &stopCategory) {
            objc_visitor::MethodList instanceMethodList = objcCategory.getInstanceMethods(objcVisitor);
            objc_visitor::MethodList classMethodList    = objcCategory.getClassMethods(objcVisitor);

            visitMethodList(instanceMethodList, hasPointerBasedMethodList, stopCategory);
            if ( stopCategory )
                return;

            visitMethodList(classMethodList, hasPointerBasedMethodList, stopCategory);
        });
        image.binaryInfo.hasCategoryMethodListsToUnique     = hasPointerBasedMethodList;
        image.binaryInfo.hasCategoryMethodListsToSetUniqued = hasPointerBasedMethodList;
    }

    if ( image.binaryInfo.protocolListCount != 0 ) {
        __block bool hasPointerBasedMethodList = false;
        objcVisitor.forEachProtocol(^(const objc_visitor::Protocol& objcProtocol, bool& stopProtocol) {
            objc_visitor::MethodList instanceMethodList         = objcProtocol.getInstanceMethods(objcVisitor);
            objc_visitor::MethodList classMethodList            = objcProtocol.getClassMethods(objcVisitor);
            objc_visitor::MethodList optionalInstanceMethodList = objcProtocol.getOptionalInstanceMethods(objcVisitor);
            objc_visitor::MethodList optionalClassMethodList    = objcProtocol.getOptionalClassMethods(objcVisitor);

            visitMethodList(instanceMethodList, hasPointerBasedMethodList, stopProtocol);
            if ( stopProtocol )
                return;

            visitMethodList(classMethodList, hasPointerBasedMethodList, stopProtocol);
            if ( stopProtocol )
                return;

            visitMethodList(optionalInstanceMethodList, hasPointerBasedMethodList, stopProtocol);
            if ( stopProtocol )
                return;

            visitMethodList(optionalClassMethodList, hasPointerBasedMethodList, stopProtocol);
        });
        image.binaryInfo.hasProtocolMethodListsToUnique     = hasPointerBasedMethodList;
        image.binaryInfo.hasProtocolMethodListsToSetUniqued = hasPointerBasedMethodList;
    }

    auto visitSelRef = ^(uint64_t selectorReferenceRuntimeOffset, uint64_t selectorStringRuntimeOffset,
                         const char* selectorString) {
        // Note we don't check if the string is printable.  We already checked earlier that this image doesn't have
        // Fairplay or protected segments, which would prevent seeing the strings.
        image.visitReferenceToObjCSelector(objcSelOpt, appSelectorMap,
                                           VMOffset(selectorReferenceRuntimeOffset),
                                           VMOffset(selectorStringRuntimeOffset), selectorString);
    };

    PrebuiltObjC::forEachSelectorReferenceToUnique(state, image.jitLoader, image.loadAddress.rawValue(), image.binaryInfo, visitSelRef);
}

static void optimizeObjCClasses(RuntimeState& state,
                                const objc::ClassHashTable* objcClassOpt,
                                PrebuiltObjC::SharedCacheImagesMapTy& sharedCacheImagesMap,
                                PrebuiltObjC::DuplicateClassesMapTy& duplicateSharedCacheClasses,
                                ObjCOptimizerImage& image)
{
    if ( image.binaryInfo.classListCount == 0 )
        return;

#if BUILDING_CACHE_BUILDER || BUILDING_CLOSURE_UTIL
    image.calculateMissingWeakImports(state);
    if ( image.diag.hasError() )
        return;
#endif

    // FIXME: Don't make a duplicate one of these if we can pass one in instead
    __block objc_visitor::Visitor objcVisitor = makeObjCVisitor(image.diag, state, image.jitLoader);
    if ( image.diag.hasError() )
        return;

    VMAddress dyldCacheBaseAddress(state.config.dyldCache.unslidLoadAddress);

    // Note we skip metaclasses
    objcVisitor.forEachClass(^(const objc_visitor::Class& objcClass, bool &stopClass) {
        // Make sure the superclass pointer is not nil.  Unless we are a root class as those don't have a superclass
        if ( !objcClass.isRootClass(objcVisitor) ) {
            metadata_visitor::ResolvedValue classSuperclassField = objcClass.getSuperclassField(objcVisitor);
            InputDylibVMAddress superclassFieldVMAddr(classSuperclassField.vmAddress().rawValue());
            if ( image.isNull(superclassFieldVMAddr, classSuperclassField.value()) ) {
                const char* className = objcClass.getName(objcVisitor);
                image.diag.error("Missing weak superclass of class %s in %s", className, image.jitLoader->path(state));
                return;
            }
        }

        // Does this class need to be fixed up for stable Swift ABI.
        // Note the order matches the objc runtime in that we always do this fix before checking for dupes,
        // but after excluding classes with missing weak superclasses.
        if ( objcClass.isUnfixedBackwardDeployingStableSwift(objcVisitor) ) {
            // Class really is stable Swift, pretending to be pre-stable.
            image.binaryInfo.hasClassStableSwiftFixups = true;
        }

        VMAddress classVMAddr = objcClass.getVMAddress();
        VMAddress classNameVMAddr = objcClass.getNameVMAddr(objcVisitor);
        // Note we don't check if the string is printable.  We already checked earlier that this image doesn't have
        // Fairplay or protected segments, which would prevent seeing the strings.
        const char* className = objcClass.getName(objcVisitor);
        image.visitClass(dyldCacheBaseAddress, objcClassOpt, sharedCacheImagesMap, duplicateSharedCacheClasses,
                         InputDylibVMAddress(classVMAddr.rawValue()), InputDylibVMAddress(classNameVMAddr.rawValue()),
                         className);
    });
}

static void optimizeObjCProtocols(RuntimeState& state,
                                  const objc::ProtocolHashTable* objcProtocolOpt,
                                  PrebuiltObjC::SharedCacheImagesMapTy& sharedCacheImagesMap,
                                  ObjCOptimizerImage& image)
{
    if ( image.binaryInfo.protocolListCount == 0 )
        return;

    image.protocolISAFixups.reserve(image.binaryInfo.protocolListCount);

    // FIXME: Don't make a duplicate one of these if we can pass one in instead
    __block objc_visitor::Visitor objcVisitor = makeObjCVisitor(image.diag, state, image.jitLoader);
    if ( image.diag.hasError() )
        return;

    objcVisitor.forEachProtocol(^(const objc_visitor::Protocol& objcProtocol, bool& stopProtocol) {
        std::optional<VMAddress> isaVMAddr = objcProtocol.getISAVMAddr(objcVisitor);
        if ( isaVMAddr.has_value() ) {
            // We can't optimize this protocol if it has an ISA as we want to override it
            image.diag.error("Protocol ISA must be null");
            stopProtocol = true;
            return;
        }

        VMAddress protocolVMAddr = objcProtocol.getVMAddress();
        VMAddress protocolNameVMAddr = objcProtocol.getNameVMAddr(objcVisitor);
        // Note we don't check if the string is printable.  We already checked earlier that this image doesn't have
        // Fairplay or protected segments, which would prevent seeing the strings.
        const char* protocolName = objcProtocol.getName(objcVisitor);

        image.visitProtocol(objcProtocolOpt, sharedCacheImagesMap, InputDylibVMAddress(protocolVMAddr.rawValue()),
                            InputDylibVMAddress(protocolNameVMAddr.rawValue()), protocolName);
    });
}

static void optimizeObjCProtocolReferences(RuntimeState& state,
                                           const objc::ProtocolHashTable* objcProtocolOpt,
                                           PrebuiltObjC::SharedCacheImagesMapTy& sharedCacheImagesMap,
                                           PrebuiltObjC::ProtocolMapTy& protocolMap,
                                           ObjCOptimizerImage& image)
{
    if ( image.binaryInfo.protocolRefsCount == 0 )
        return;

    image.protocolFixups.reserve(image.binaryInfo.protocolRefsCount);

    // FIXME: Don't make a duplicate one of these if we can pass one in instead
    __block objc_visitor::Visitor objcVisitor = makeObjCVisitor(image.diag, state, image.jitLoader);
    if ( image.diag.hasError() )
        return;

    objcVisitor.forEachProtocolReference(^(metadata_visitor::ResolvedValue& protocolRefValue) {
        if ( image.diag.hasError() )
            return;

        // Follow the protocol reference to get to the actual protocol
        metadata_visitor::ResolvedValue protocolValue = objcVisitor.resolveRebase(protocolRefValue);
        objc_visitor::Protocol objcProtocol(protocolValue);

        const char* protocolName = objcProtocol.getName(objcVisitor);

        // Check if this protocol is in the map in the shared cache.  If so use that one
        __block std::optional<uint64_t> protocolCacheOffset;
        objcProtocolOpt->forEachProtocol(protocolName,
                                         ^(uint64_t classCacheOffset, uint16_t dylibObjCIndex, bool& stopObjects) {
            // Check if this image is loaded
            if ( auto cacheIt = sharedCacheImagesMap.find(dylibObjCIndex); cacheIt != sharedCacheImagesMap.end() ) {
                protocolCacheOffset = classCacheOffset;
                stopObjects = true;
            }
        });
        if ( protocolCacheOffset.has_value() ) {
            // We use an absolute bind to point in to the shared cache protocols
            PrebuiltLoader::BindTargetRef bindTarget = PrebuiltLoader::BindTargetRef::makeAbsolute(protocolCacheOffset.value());
            image.protocolFixups.push_back(bindTarget);
            return;
        }

        // Not using the shared cache, so we should find the protocol in the map in the closure
        prebuilt_objc::ObjCStringKey key = { protocolName };
        auto nameIt = protocolMap.find(key);
        if ( nameIt == protocolMap.end() ) {
            // FIXME: What do we do here?  The protocols are wrong?  Skip this image for now.
            image.diag.error("Could not find protocol '%s'", protocolName);
            return;
        }
        const prebuilt_objc::ObjCObjectLocation& protocolLocation = nameIt->value;
        image.protocolFixups.push_back(protocolLocation.objectLocation);
    });
}


static void
generateClassOrProtocolHashTable(PrebuiltObjC::ObjCStructKind objcKind,
                                 Array<ObjCOptimizerImage>& objcImages,
                                 const PrebuiltObjC::DuplicateClassesMapTy& duplicateSharedCacheClassMap,
                                 PrebuiltObjC::ObjectMapTy& objectMap, bool& hasDuplicates)
{
    // Note we walk the images backwards as we want them in load order to match the order they are registered with objc
    for ( uint64_t imageIndex = 0, reverseIndex = (objcImages.count() - 1); imageIndex != objcImages.count(); ++imageIndex, --reverseIndex ) {
        if ( objcImages[reverseIndex].diag.hasError() )
            continue;
        ObjCOptimizerImage& image = objcImages[reverseIndex];

        if ( objcKind == PrebuiltObjC::ObjCStructKind::classes ) {
            for ( const ObjCOptimizerImage::ObjCObject& classLocation : image.classLocations ) {
                //uint64_t nameVMAddr     = ma->preferredLoadAddress() + classImage.offsetOfClassNames + classNameTarget.classNameImageOffset;
                //printf("%s: 0x%08llx = '%s'\n", li.path(), nameVMAddr, className);

                // Also track the name
                PrebuiltLoader::BindTarget nameTarget  = { image.jitLoader, classLocation.nameRuntimeOffset.rawValue() };
                PrebuiltLoader::BindTarget valueTarget = { image.jitLoader, classLocation.valueRuntimeOffset.rawValue() };
                prebuilt_objc::ObjCStringKey key = { classLocation.name };
                prebuilt_objc::ObjCObjectLocation value = {
                    nameTarget,
                    valueTarget
                };
                bool alreadyHaveNodeWithKey = false;
                auto objectIt = objectMap.insert({ key, value }, alreadyHaveNodeWithKey);
                if ( !alreadyHaveNodeWithKey ) {
                    // Check if we have a duplicate.  If we do, it will be on the last image which had a duplicate class name,
                    // but as we walk images backwards, we'll see this before all other images with duplicates.
                    // Note we only check for duplicates when we know we just inserted the object name in to the map, as this
                    // ensure's that we only insert each duplicate once
                    auto duplicateClassIt = duplicateSharedCacheClassMap.find(classLocation.name);
                    if ( duplicateClassIt != duplicateSharedCacheClassMap.end() ) {
                        // This is gross.  Change this entry to the duplicate, and add a new one
                        objectIt->value = { nameTarget, duplicateClassIt->second };

                        bool unusedAlreadyHaveNodeWithKey;
                        objectMap.insert({ key, value }, unusedAlreadyHaveNodeWithKey);
                        hasDuplicates = true;
                    }
                } else {
                    // We didn't add the node, so we have duplicates
                    hasDuplicates = true;
                }
            }
        }

        if ( objcKind == PrebuiltObjC::ObjCStructKind::protocols ) {
            for ( const ObjCOptimizerImage::ObjCObject& protocolLocation : image.protocolLocations ) {
                // Also track the name
                PrebuiltLoader::BindTarget nameTarget  = { image.jitLoader, protocolLocation.nameRuntimeOffset.rawValue() };
                PrebuiltLoader::BindTarget valueTarget = { image.jitLoader, protocolLocation.valueRuntimeOffset.rawValue() };
                prebuilt_objc::ObjCStringKey key = { protocolLocation.name };
                prebuilt_objc::ObjCObjectLocation value = {
                    nameTarget,
                    valueTarget
                };
                bool alreadyHaveNodeWithKey = false;
                objectMap.insert({ key, value }, alreadyHaveNodeWithKey);
                if ( !alreadyHaveNodeWithKey ) {
                    // We are processing protocols, and this is the first one we've seen, so track its ISA to be fixed up
                    auto protocolIndexIt = image.protocolIndexMap.find(protocolLocation.valueRuntimeOffset);
                    assert(protocolIndexIt != image.protocolIndexMap.end());
                    image.protocolISAFixups[protocolIndexIt->second] = true;
                }
            }
        }
    }
}

//////////////////////// PrebuiltObjC /////////////////////////////////

void PrebuiltObjC::commitImage(const ObjCOptimizerImage& image)
{
    // As this image is still valid, then add its intermediate results to the main tables
    for ( const auto& stringAndDuplicate : image.duplicateSharedCacheClassMap ) {
        // Note we want to overwrite any existing entries here.  We want the last seen
        // class with a duplicate to be in the map as writeClassOrProtocolHashTable walks the images
        // from back to front.
        duplicateSharedCacheClassMap[stringAndDuplicate.first] = stringAndDuplicate.second;
    }

    // Selector results
    // Note we don't need to add the selector binds here.  Its easier just to process them later from each image
    for ( const auto& stringAndTarget : image.selectorMap ) {
        this->selectorMap[stringAndTarget.first] = stringAndTarget.second;
    }
}

uint32_t PrebuiltObjC::serializeSelectorMap(dyld4::BumpAllocator& alloc) const
{
    // The key on the new map is the name bind target
    typedef prebuilt_objc::ObjCSelectorMapOnDisk::KeyType (^KeyFuncTy)(const SelectorMapTy::KeyType&, const SelectorMapTy::ValueType&);
    KeyFuncTy convertKey = ^(const SelectorMapTy::KeyType& key, const SelectorMapTy::ValueType& value) {
        return (prebuilt_objc::ObjCSelectorMapOnDisk::KeyType){ PrebuiltLoader::BindTargetRef(value.nameLocation) };
    };

    // The value on the new map is unused
    typedef prebuilt_objc::ObjCSelectorMapOnDisk::ValueType (^ValueFuncTy)(const SelectorMapTy::KeyType&, const SelectorMapTy::ValueType&);
    ValueFuncTy convertValue = ^(const SelectorMapTy::KeyType& key, const SelectorMapTy::ValueType& value) {
        return (prebuilt_objc::ObjCSelectorMapOnDisk::ValueType)0;
    };

    uint32_t offset = (uint32_t)alloc.size();
    this->selectorMap.serialize(alloc, convertKey, convertValue);
    return offset;
}

uint32_t PrebuiltObjC::serializeClassMap(dyld4::BumpAllocator& alloc) const
{
    // The key on the new map is the name bind taret
    typedef prebuilt_objc::ObjCClassMapOnDisk::KeyType (^KeyFuncTy)(const ClassMapTy::KeyType&, const ClassMapTy::ValueType&);
    KeyFuncTy convertKey = ^(const ClassMapTy::KeyType& key, const ClassMapTy::ValueType& value) {
        return (prebuilt_objc::ObjCClassMapOnDisk::KeyType){ PrebuiltLoader::BindTargetRef(value.nameLocation) };
    };

    // The value on the new map is just the class impl
    typedef prebuilt_objc::ObjCClassMapOnDisk::ValueType (^ValueFuncTy)(const ClassMapTy::KeyType&, const ClassMapTy::ValueType&);
    ValueFuncTy convertValue = ^(const ClassMapTy::KeyType& key, const ClassMapTy::ValueType& value) {
        return (prebuilt_objc::ObjCClassMapOnDisk::ValueType){ PrebuiltLoader::BindTargetRef(value.objectLocation) };
    };

    uint32_t offset = (uint32_t)alloc.size();
    this->classMap.serialize(alloc, convertKey, convertValue);
    return offset;
}

uint32_t PrebuiltObjC::serializeProtocolMap(dyld4::BumpAllocator& alloc) const
{
    // The key on the new map is the name bind taret
    typedef prebuilt_objc::ObjCProtocolMapOnDisk::KeyType (^KeyFuncTy)(const ProtocolMapTy::KeyType&, const ProtocolMapTy::ValueType&);
    KeyFuncTy convertKey = ^(const ProtocolMapTy::KeyType& key, const ProtocolMapTy::ValueType& value) {
        return (prebuilt_objc::ObjCProtocolMapOnDisk::KeyType){ PrebuiltLoader::BindTargetRef(value.nameLocation) };
    };

    // The value on the new map is just the protocol impl
    typedef prebuilt_objc::ObjCProtocolMapOnDisk::ValueType (^ValueFuncTy)(const ProtocolMapTy::KeyType&, const ProtocolMapTy::ValueType&);
    ValueFuncTy convertValue = ^(const ProtocolMapTy::KeyType& key, const ProtocolMapTy::ValueType& value) {
        return (prebuilt_objc::ObjCProtocolMapOnDisk::ValueType){ PrebuiltLoader::BindTargetRef(value.objectLocation) };
    };

    uint32_t offset = (uint32_t)alloc.size();
    this->protocolMap.serialize(alloc, convertKey, convertValue);
    return offset;
}

void PrebuiltObjC::generateHashTables()
{
    generateClassOrProtocolHashTable(PrebuiltObjC::ObjCStructKind::classes, objcImages, 
                                     duplicateSharedCacheClassMap, classMap, this->hasClassDuplicates);

    bool unusedHasProtocolDuplicates = false;
    generateClassOrProtocolHashTable(PrebuiltObjC::ObjCStructKind::protocols, objcImages, 
                                     duplicateSharedCacheClassMap, protocolMap, unusedHasProtocolDuplicates);
}

void PrebuiltObjC::generatePerImageFixups(RuntimeState& state, uint32_t pointerSize)
{
    // Find the largest JIT loader index so that we know how many images we might serialize
    uint16_t largestLoaderIndex = 0;
    for ( const Loader* l : state.loaded ) {
        if ( !l->isPrebuilt ) {
            JustInTimeLoader* jl = (JustInTimeLoader*)l;
            assert(jl->ref.app);
            largestLoaderIndex = std::max(largestLoaderIndex, jl->ref.index);
        }
    }
    ++largestLoaderIndex;

    imageFixups.reserve(largestLoaderIndex);
    for ( uint16_t i = 0; i != largestLoaderIndex; ++i ) {
        imageFixups.default_constuct_back();
    }

    // Add per-image fixups
    for ( ObjCOptimizerImage& image : objcImages ) {
        if ( image.diag.hasError() )
            continue;

        ObjCImageFixups& fixups = imageFixups[image.jitLoader->ref.index];

        // Copy all the binary info for use later when applying fixups
        fixups.binaryInfo = image.binaryInfo;

        // Protocol ISA references
        // These are a single boolean value for each protocol to identify if it is canonical or not
        // We convert from bool to uint8_t as that seems better for saving to disk.
        if ( !image.protocolISAFixups.empty() ) {
            fixups.protocolISAFixups.reserve(image.protocolISAFixups.count());
            for ( bool isCanonical : image.protocolISAFixups )
                fixups.protocolISAFixups.push_back(isCanonical ? 1 : 0);
        }

        // Selector references.
        // These are a BindTargetRef for every selector reference to fixup
        if ( !image.selectorFixups.empty() ) {
            fixups.selectorReferenceFixups.reserve(image.selectorFixups.count());
            for ( const PrebuiltLoader::BindTargetRef& target : image.selectorFixups ) {
                fixups.selectorReferenceFixups.push_back(target);
            }
        }

        // Protocol references.
        // These are a BindTargetRef for every protocol reference to fixup
        if ( !image.protocolFixups.empty() ) {
            fixups.protocolReferenceFixups.reserve(image.protocolFixups.count());
            for ( const PrebuiltLoader::BindTargetRef& target : image.protocolFixups ) {
                fixups.protocolReferenceFixups.push_back(target);
            }
        }
    }
}

__attribute__((noinline))
static void forEachSelectorReferenceToUnique(objc_visitor::Visitor& objcVisitor,
                                             uint64_t                loadAddress,
                                             const ObjCBinaryInfo&   binaryInfo,
                                             void (^callback)(uint64_t selectorReferenceRuntimeOffset,
                                                              uint64_t selectorStringRuntimeOffset,
                                                              const char* selectorString))
{
    if ( binaryInfo.selRefsCount != 0 ) {
        objcVisitor.forEachSelectorReference(^(VMAddress selRefVMAddr, VMAddress selRefTargetVMAddr,
                                               const char* selectorString) {
            VMOffset selectorReferenceRuntimeOffset = selRefVMAddr - VMAddress(loadAddress);
            VMOffset selectorStringRuntimeOffset    = selRefTargetVMAddr - VMAddress(loadAddress);
            callback(selectorReferenceRuntimeOffset.rawValue(), selectorStringRuntimeOffset.rawValue(),
                     selectorString);
        });
    }
}

__attribute__((noinline))
static void forEachClassSelectorReferenceToUnique(objc_visitor::Visitor& objcVisitor,
                                                  uint64_t                loadAddress,
                                                  const ObjCBinaryInfo&   binaryInfo,
                                                  void (^callback)(uint64_t selectorReferenceRuntimeOffset,
                                                                   uint64_t selectorStringRuntimeOffset,
                                                                   const char* selectorString))
{

    // We only make the callback for method list selrefs which are not already covered by the __objc_selrefs section.
    // For pointer based method lists, this is all sel ref pointers.
    // For relative method lists, we should always point to the __objc_selrefs section.  This was checked earlier, so
    // we skip this callback on relative method lists as we know here they must point to the (already uniqied) __objc_selrefs.
    auto visitPointerBasedMethod = ^(const objc_visitor::Method& method) {
        VMAddress nameVMAddr = method.getNameVMAddr(objcVisitor);
        VMAddress nameLocationVMAddr = method.getNameField(objcVisitor).vmAddress();
        const char* selectorString = method.getName(objcVisitor);

        VMOffset selectorStringRuntimeOffset    = nameVMAddr - VMAddress(loadAddress);
        VMOffset selectorReferenceRuntimeOffset = nameLocationVMAddr - VMAddress(loadAddress);
        callback(selectorReferenceRuntimeOffset.rawValue(), selectorStringRuntimeOffset.rawValue(), selectorString);
    };

    auto visitMethodList = ^(const objc_visitor::MethodList& methodList) {
        if ( methodList.numMethods() == 0 )
            return;
        if ( methodList.usesRelativeOffsets() )
            return;

        // Check pointer based method lists
        uint32_t numMethods = methodList.numMethods();
        for ( uint32_t i = 0; i != numMethods; ++i ) {
            const objc_visitor::Method& method = methodList.getMethod(objcVisitor, i);
            visitPointerBasedMethod(method);
        }
    };

    if ( binaryInfo.hasClassMethodListsToUnique && (binaryInfo.classListCount != 0) ) {
        // FIXME: Use binaryInfo.classListRuntimeOffset and binaryInfo.classListCount
        objcVisitor.forEachClassAndMetaClass(^(const objc_visitor::Class &objcClass, bool &stopClass) {
            objc_visitor::MethodList methodList = objcClass.getBaseMethods(objcVisitor);
            visitMethodList(methodList);
        });
    }
}

__attribute__((noinline))
static void forEachCategorySelectorReferenceToUnique(objc_visitor::Visitor& objcVisitor,
                                                     uint64_t                loadAddress,
                                                     const ObjCBinaryInfo&   binaryInfo,
                                                     void (^callback)(uint64_t selectorReferenceRuntimeOffset,
                                                                      uint64_t selectorStringRuntimeOffset,
                                                                      const char* selectorString))
{
    // We only make the callback for method list selrefs which are not already covered by the __objc_selrefs section.
    // For pointer based method lists, this is all sel ref pointers.
    // For relative method lists, we should always point to the __objc_selrefs section.  This was checked earlier, so
    // we skip this callback on relative method lists as we know here they must point to the (already uniqied) __objc_selrefs.
    auto visitPointerBasedMethod = ^(const objc_visitor::Method& method) {
        VMAddress nameVMAddr = method.getNameVMAddr(objcVisitor);
        VMAddress nameLocationVMAddr = method.getNameField(objcVisitor).vmAddress();
        const char* selectorString = method.getName(objcVisitor);

        VMOffset selectorStringRuntimeOffset    = nameVMAddr - VMAddress(loadAddress);
        VMOffset selectorReferenceRuntimeOffset = nameLocationVMAddr - VMAddress(loadAddress);
        callback(selectorReferenceRuntimeOffset.rawValue(), selectorStringRuntimeOffset.rawValue(), selectorString);
    };

    auto visitMethodList = ^(const objc_visitor::MethodList& methodList) {
        if ( methodList.numMethods() == 0 )
            return;
        if ( methodList.usesRelativeOffsets() )
            return;

        // Check pointer based method lists
        uint32_t numMethods = methodList.numMethods();
        for ( uint32_t i = 0; i != numMethods; ++i ) {
            const objc_visitor::Method& method = methodList.getMethod(objcVisitor, i);
            visitPointerBasedMethod(method);
        }
    };

    if ( binaryInfo.hasCategoryMethodListsToUnique && (binaryInfo.categoryCount != 0) ) {
        // FIXME: Use binaryInfo.categoryListRuntimeOffset and binaryInfo.categoryCount
        objcVisitor.forEachCategory(^(const objc_visitor::Category& objcCategory, bool &stopCategory) {
            objc_visitor::MethodList instanceMethodList = objcCategory.getInstanceMethods(objcVisitor);
            objc_visitor::MethodList classMethodList    = objcCategory.getClassMethods(objcVisitor);

            visitMethodList(instanceMethodList);
            visitMethodList(classMethodList);
        });
    }
}

__attribute__((noinline))
static void forEachProtocolSelectorReferenceToUnique(objc_visitor::Visitor& objcVisitor,
                                                     uint64_t                loadAddress,
                                                     const ObjCBinaryInfo&   binaryInfo,
                                                     void (^callback)(uint64_t selectorReferenceRuntimeOffset,
                                                                      uint64_t selectorStringRuntimeOffset,
                                                                      const char* selectorString))
{
    // We only make the callback for method list selrefs which are not already covered by the __objc_selrefs section.
    // For pointer based method lists, this is all sel ref pointers.
    // For relative method lists, we should always point to the __objc_selrefs section.  This was checked earlier, so
    // we skip this callback on relative method lists as we know here they must point to the (already uniqied) __objc_selrefs.
    auto visitPointerBasedMethod = ^(const objc_visitor::Method& method) {
        VMAddress nameVMAddr = method.getNameVMAddr(objcVisitor);
        VMAddress nameLocationVMAddr = method.getNameField(objcVisitor).vmAddress();
        const char* selectorString = method.getName(objcVisitor);

        VMOffset selectorStringRuntimeOffset    = nameVMAddr - VMAddress(loadAddress);
        VMOffset selectorReferenceRuntimeOffset = nameLocationVMAddr - VMAddress(loadAddress);
        callback(selectorReferenceRuntimeOffset.rawValue(), selectorStringRuntimeOffset.rawValue(), selectorString);
    };

    auto visitMethodList = ^(const objc_visitor::MethodList& methodList) {
        if ( methodList.numMethods() == 0 )
            return;
        if ( methodList.usesRelativeOffsets() )
            return;

        // Check pointer based method lists
        uint32_t numMethods = methodList.numMethods();
        for ( uint32_t i = 0; i != numMethods; ++i ) {
            const objc_visitor::Method& method = methodList.getMethod(objcVisitor, i);
            visitPointerBasedMethod(method);
        }
    };

    if ( binaryInfo.hasProtocolMethodListsToUnique && (binaryInfo.protocolListCount != 0) ) {
        // FIXME: Use binaryInfo.protocolListRuntimeOffset and binaryInfo.protocolListCount
        objcVisitor.forEachProtocol(^(const objc_visitor::Protocol& objcProtocol, bool& stopProtocol) {
            objc_visitor::MethodList instanceMethodList         = objcProtocol.getInstanceMethods(objcVisitor);
            objc_visitor::MethodList classMethodList            = objcProtocol.getClassMethods(objcVisitor);
            objc_visitor::MethodList optionalInstanceMethodList = objcProtocol.getOptionalInstanceMethods(objcVisitor);
            objc_visitor::MethodList optionalClassMethodList    = objcProtocol.getOptionalClassMethods(objcVisitor);

            visitMethodList(instanceMethodList);
            visitMethodList(classMethodList);
            visitMethodList(optionalInstanceMethodList);
            visitMethodList(optionalClassMethodList);
        });
    }
}

// Visits each selector reference once, in order.  Note the order this visits selector references has to
// match for serializing/deserializing the PrebuiltLoader.
void PrebuiltObjC::forEachSelectorReferenceToUnique(RuntimeState&           state,
                                                    const Loader*           ldr,
                                                    uint64_t                loadAddress,
                                                    const ObjCBinaryInfo&   binaryInfo,
                                                    void (^callback)(uint64_t selectorReferenceRuntimeOffset,
                                                                     uint64_t selectorStringRuntimeOffset,
                                                                     const char* selectorString))

{
    // FIXME: Don't make a duplicate one of these if we can pass one in instead
    Diagnostics diag;
    __block objc_visitor::Visitor objcVisitor = makeObjCVisitor(diag, state, ldr);
    assert(!diag.hasError());

    dyld4::forEachSelectorReferenceToUnique(objcVisitor, loadAddress, binaryInfo, callback);
    dyld4::forEachClassSelectorReferenceToUnique(objcVisitor, loadAddress, binaryInfo, callback);
    dyld4::forEachCategorySelectorReferenceToUnique(objcVisitor, loadAddress, binaryInfo, callback);
    dyld4::forEachProtocolSelectorReferenceToUnique(objcVisitor, loadAddress, binaryInfo, callback);
}

static std::optional<VMOffset> getImageInfo(Diagnostics& diag, RuntimeState& state,
                                            const Loader* ldr, const Header* hdr)
{
    __block std::optional<VMOffset> objcImageInfoRuntimeOffset;
    hdr->forEachSection(^(const Header::SectionInfo& sectionInfo, bool& stop) {
        if ( !sectionInfo.segmentName.starts_with("__DATA") )
            return;
        if ( sectionInfo.sectionName != "__objc_imageinfo" )
            return;
        if ( sectionInfo.size != 8 ) {
            stop = true;
            return;
        }

        // We can't just access the image info directly from the MachOFile.  Instead we have to
        // use the layout to find the actual location of the segment, as we might be in the cache builder
        ldr->withLayout(diag, state, ^(const mach_o::Layout& layout) {
            const mach_o::SegmentLayout& segment = layout.segments[sectionInfo.segIndex];
            uint64_t offsetInSegment = sectionInfo.address - segment.vmAddr;
            const auto* imageInfo = (MachOAnalyzer::ObjCImageInfo*)(segment.buffer + offsetInSegment);

            if ( (imageInfo->flags & MachOAnalyzer::ObjCImageInfo::dyldPreoptimized) != 0 )
                return;

            objcImageInfoRuntimeOffset = VMOffset(sectionInfo.address - layout.textUnslidVMAddr());
        });
        stop = true;
    });

    return objcImageInfoRuntimeOffset;
}

static std::optional<VMOffset> getProtocolClassCacheOffset(RuntimeState& state)
{
#if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS
        assert(state.config.dyldCache.objcProtocolClassCacheOffset != 0);
        return VMOffset(state.config.dyldCache.objcProtocolClassCacheOffset);
#else
        // Make sure we have the pointers section with the pointer to the protocol class
        const void* objcOptPtrs = state.config.dyldCache.addr->objcOptPtrs();
        if ( objcOptPtrs == nullptr )
            return { };

        uint32_t pointerSize = state.mainExecutableLoader->loadAddress(state)->pointerSize();
        uint64_t classProtocolVMAddr = (pointerSize == 8) ? *(uint64_t*)objcOptPtrs : *(uint32_t*)objcOptPtrs;

#if BUILDING_DYLD || BUILDING_UNIT_TESTS
        // As we are running in dyld/tests, the cache is live

#if __has_feature(ptrauth_calls)
        // If we are on arm64e, the protocol ISA in the shared cache was signed.  We don't
        // want the signature bits in the encoded value
        classProtocolVMAddr = (uint64_t)__builtin_ptrauth_strip((void*)classProtocolVMAddr, ptrauth_key_asda);
#endif // __has_feature(ptrauth_calls)

        return VMOffset(classProtocolVMAddr - (uint64_t)state.config.dyldCache.addr);
#elif BUILDING_CLOSURE_UTIL
        // FIXME: This assumes an on-disk cache
        classProtocolVMAddr          = state.config.dyldCache.addr->makeVMAddrConverter(false).convertToVMAddr(classProtocolVMAddr);
        return VMOffset(classProtocolVMAddr - state.config.dyldCache.addr->unslidLoadAddress());
#else
        // Running offline so the cache is not live
        //objcProtocolClassCacheOffset = classProtocolVMAddr - dyldCache->unslidLoadAddress();
#error Unknown tool
#endif // BUILDING_DYLD

#endif // BUILDING_CACHE_BUILDER
}

void PrebuiltObjC::make(Diagnostics& diag, RuntimeState& state)
{

    // If we have the read only data, make sure it has a valid selector table inside.
    const objc::ClassHashTable*    objcClassOpt             = state.config.dyldCache.objcClassHashTable;
    const objc::SelectorHashTable* objcSelOpt               = state.config.dyldCache.objcSelectorHashTable;
    const objc::ProtocolHashTable* objcProtocolOpt          = state.config.dyldCache.objcProtocolHashTable;
    const void*                    headerInfoRO             = state.config.dyldCache.objcHeaderInfoRO;
    const void*                    headerInfoRW             = state.config.dyldCache.objcHeaderInfoRW;
    VMAddress                      headerInfoROUnslidVMAddr(state.config.dyldCache.objcHeaderInfoROUnslidVMAddr);

    if ( !objcClassOpt || !objcSelOpt || !objcProtocolOpt)
        return;

    if ( std::optional<VMOffset> offset = getProtocolClassCacheOffset(state); offset.has_value() )
        objcProtocolClassCacheOffset = offset.value();

    for ( const Loader* ldr : state.delayLoaded ) {
        if ( ldr->isJustInTimeLoader() ) {
            // TODO: Handle apps which delay-init on-disk dylibs
            // This will lead to the closure not optimizing the objc, and libobjc will do it instead
            // in map_images().  This is safe as we tell objc (via dyldDoesObjCFixups()) whether we optimized or not
            return;
        }
    }

    // Find all the images with valid objc info
    SharedCacheImagesMapTy sharedCacheImagesMap;

    // Note we have done the delay-init partitioning by this point, so state.loaded is just the loaders
    // we known we need at launch.  This is important for the shared cache in particular as the shared cache
    // classes/protocols are always preferred over the app ones, so a shared cache image being delayed or not
    // impacts the choice of classes/protocols.  See protocolIsInSharedCache() for example.
    for ( const Loader* ldr : state.loaded ) {
        const Header* hdr = (const Header*)ldr->mf(state);
        uint32_t pointerSize = hdr->pointerSize();

        std::optional<VMOffset> objcImageInfoRuntimeOffset = getImageInfo(diag, state, ldr, hdr);

        if ( !objcImageInfoRuntimeOffset.has_value() )
            continue;

        if ( ldr->dylibInDyldCache ) {
            // Add shared cache images to a map so that we can see them later for looking up classes
            uint64_t dylibUnslidVMAddr = hdr->preferredLoadAddress();

            std::optional<uint16_t> objcIndex;
            objcIndex = objc::getPreoptimizedHeaderROIndex(headerInfoRO, headerInfoRW,
                                                           headerInfoROUnslidVMAddr.rawValue(),
                                                           dylibUnslidVMAddr,
                                                           hdr->is64());
            if ( !objcIndex.has_value() )
                return;
            sharedCacheImagesMap.insert({ *objcIndex, { VMAddress(dylibUnslidVMAddr), ldr } });
            continue;
        }

        // If we have a root of libobjc, just give up for now
        if ( ldr->matchesPath(state, "/usr/lib/libobjc.A.dylib") )
            return;

        // dyld can see the strings in Fairplay binaries and protected segments, but other tools cannot.
        // Skip generating the PrebuiltObjC in these other cases
#if !BUILDING_DYLD
        // Find FairPlay encryption range if encrypted
        uint32_t fairPlayFileOffset;
        uint32_t fairPlaySize;
        if ( hdr->isFairPlayEncrypted(fairPlayFileOffset, fairPlaySize) )
            return;

        __block bool hasProtectedSegment = false;
        hdr->forEachSegment(^(const Header::SegmentInfo& segInfo, bool& stop) {
            if ( segInfo.isProtected() ) {
                hasProtectedSegment = true;
                stop                = true;
            }
        });
        if ( hasProtectedSegment )
            return;
#endif

#if BUILDING_CACHE_BUILDER
        // The cache builder will crash if it gets a binary with cheaper roots and bind opcodes
        // Given up if we see this case
        if ( hdr->hasOpcodeFixups() )
            return;
#endif

        // This image is good so record it for use later.
        objcImages.emplace_back((const JustInTimeLoader*)ldr, hdr->preferredLoadAddress(), pointerSize);
        ObjCOptimizerImage& image = objcImages.back();
        image.jitLoader           = (const JustInTimeLoader*)ldr;

        // Set the offset to the objc image info
        image.binaryInfo.imageInfoRuntimeOffset = objcImageInfoRuntimeOffset->rawValue();

        // Get the range of a section which is required to contain pointers, i.e., be pointer sized.
        auto getPointerBasedSection = ^(const char* name, uint64_t& runtimeOffset, uint32_t& pointerCount) {
            uint64_t offset;
            uint64_t count;
            if ( hdr->findObjCDataSection(name, offset, count) ) {
                if ( (count % pointerSize) != 0 ) {
                    image.diag.error("Invalid objc pointer section size");
                    return;
                }
                runtimeOffset = offset;
                pointerCount  = (uint32_t)count / pointerSize;
            }
            else {
                runtimeOffset = 0;
                pointerCount  = 0;
            }
        };

        // Find the offsets to all other sections we need for the later optimizations
        getPointerBasedSection("__objc_selrefs", image.binaryInfo.selRefsRuntimeOffset, image.binaryInfo.selRefsCount);
        getPointerBasedSection("__objc_classlist", image.binaryInfo.classListRuntimeOffset, image.binaryInfo.classListCount);
        getPointerBasedSection("__objc_catlist", image.binaryInfo.categoryListRuntimeOffset, image.binaryInfo.categoryCount);
        getPointerBasedSection("__objc_protolist", image.binaryInfo.protocolListRuntimeOffset, image.binaryInfo.protocolListCount);
        getPointerBasedSection("__objc_protorefs", image.binaryInfo.protocolRefsRuntimeOffset, image.binaryInfo.protocolRefsCount);
    }

    for ( ObjCOptimizerImage& image : objcImages ) {
        if ( image.diag.hasError() )
            continue;

        optimizeObjCClasses(state, objcClassOpt, sharedCacheImagesMap, duplicateSharedCacheClassMap, image);
        if ( image.diag.hasError() )
            continue;

        optimizeObjCProtocols(state, objcProtocolOpt, sharedCacheImagesMap, image);
        if ( image.diag.hasError() )
            continue;

        optimizeObjCSelectors(state, objcSelOpt, selectorMap, image);
        if ( image.diag.hasError() )
            continue;

        commitImage(image);
    }

    // If we successfully analyzed the classes and selectors, we can now make the maps
    generateHashTables();

    // Once we have the hash tables with the canonical protocols, we can generate the fixups
    // for the protorefs, which need to point to the canonical protocol
    for ( ObjCOptimizerImage& image : objcImages ) {
        if ( image.diag.hasError() )
            continue;

        optimizeObjCProtocolReferences(state, objcProtocolOpt, sharedCacheImagesMap, protocolMap, image);
    }

    uint32_t pointerSize = state.mainExecutableLoader->mf(state)->pointerSize();
    generatePerImageFixups(state, pointerSize);

    builtObjC = true;
}

uint32_t PrebuiltObjC::serializeFixups(const Loader& jitLoader, BumpAllocator& allocator)
{
    if ( !builtObjC )
        return 0;

    assert(jitLoader.ref.app);
    uint16_t index = jitLoader.ref.index;

    const ObjCImageFixups& fixups = imageFixups[index];

    if ( fixups.binaryInfo.imageInfoRuntimeOffset == 0 ) {
        // No fixups to apply
        return 0;
    }

    uint32_t                         serializationStart = (uint32_t)allocator.size();
    BumpAllocatorPtr<ObjCBinaryInfo> fixupInfo(allocator, serializationStart);

    allocator.append(&fixups.binaryInfo, sizeof(fixups.binaryInfo));

    // Protocols
    if ( !fixups.protocolISAFixups.empty() ) {
        // If we have protocol fixups, then we must have 1 for every protocol in this image.
        assert(fixups.protocolISAFixups.count() == fixups.binaryInfo.protocolListCount);

        uint16_t protocolArrayOff       = allocator.size() - serializationStart;
        fixupInfo->protocolFixupsOffset = protocolArrayOff;
        allocator.zeroFill(fixups.protocolISAFixups.count() * sizeof(uint8_t));
        allocator.align(8);
        BumpAllocatorPtr<uint8_t> protocolArray(allocator, serializationStart + protocolArrayOff);
        memcpy(protocolArray.get(), fixups.protocolISAFixups.data(), (size_t)(fixups.protocolISAFixups.count() * sizeof(uint8_t)));
    }

    // Selector references
    if ( !fixups.selectorReferenceFixups.empty() ) {
        uint64_t selectorsArrayOff                = allocator.size() - serializationStart;
        fixupInfo->selectorReferencesFixupsOffset = (uint32_t)selectorsArrayOff;
        fixupInfo->selectorReferencesFixupsCount  = (uint32_t)fixups.selectorReferenceFixups.count();
        allocator.zeroFill(fixups.selectorReferenceFixups.count() * sizeof(PrebuiltLoader::BindTargetRef));
        BumpAllocatorPtr<uint8_t> selectorsArray(allocator, serializationStart + selectorsArrayOff);
        memcpy(selectorsArray.get(), fixups.selectorReferenceFixups.data(), (size_t)(fixups.selectorReferenceFixups.count() * sizeof(PrebuiltLoader::BindTargetRef)));
    }

    // Protocol references
    if ( !fixups.protocolReferenceFixups.empty() ) {
        uint64_t protocolsArrayOff                = allocator.size() - serializationStart;
        fixupInfo->protocolReferencesFixupsOffset = (uint32_t)protocolsArrayOff;
        fixupInfo->protocolReferencesFixupsCount  = (uint32_t)fixups.protocolReferenceFixups.count();
        allocator.zeroFill(fixups.protocolReferenceFixups.count() * sizeof(PrebuiltLoader::BindTargetRef));
        BumpAllocatorPtr<uint8_t> protocolsArray(allocator, serializationStart + protocolsArrayOff);
        memcpy(protocolsArray.get(), fixups.protocolReferenceFixups.data(), (size_t)(fixups.protocolReferenceFixups.count() * sizeof(PrebuiltLoader::BindTargetRef)));
    }

    return serializationStart;
}

} // namespace dyld4
#endif // SUPPORT_PREBUILTLOADERS || BUILDING_UNIT_TESTS || BUILDING_CACHE_BUILDER_UNIT_TESTS

#endif // !TARGET_OS_EXCLAVEKIT