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
/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
 *
 * Copyright (c) 2018-2024 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 "SymbolicatedImage.h"

// OS
#include <SoftLinking/WeakLinking.h>

// mach-o
#include "Instructions.h"

// llvm
#if HAVE_LIBLTO
    extern "C" void lto_initialize_disassembler();  // from libLTO.dylib but not in Disassembler.h
    extern "C" int LLVMSetDisasmOptions(LLVMDisasmContextRef context, uint64_t options);\
    WEAK_LINK_FORCE_IMPORT(LLVMCreateDisasm);
    WEAK_LINK_FORCE_IMPORT(LLVMDisasmDispose);
    WEAK_LINK_FORCE_IMPORT(LLVMDisasmInstruction);
    WEAK_LINK_FORCE_IMPORT(LLVMSetDisasmOptions);
    WEAK_LINK_FORCE_IMPORT(lto_initialize_disassembler);
    #ifndef LLVMDisassembler_ReferenceType_In_ARM64_ADRP
        #define LLVMDisassembler_ReferenceType_In_ARM64_ADRP   0x100000001
    #endif
    #ifndef LLVMDisassembler_ReferenceType_In_ARM64_ADDXri
        #define LLVMDisassembler_ReferenceType_In_ARM64_ADDXri 0x100000002
    #endif
    #ifndef LLVMDisassembler_ReferenceType_In_ARM64_LDRXui
        #define LLVMDisassembler_ReferenceType_In_ARM64_LDRXui 0x100000003
    #endif
    #ifndef LLVMDisassembler_ReferenceType_In_ARM64_LDRXl
        #define LLVMDisassembler_ReferenceType_In_ARM64_LDRXl  0x100000004
    #endif
    #ifndef LLVMDisassembler_ReferenceType_In_ARM64_ADR
        #define LLVMDisassembler_ReferenceType_In_ARM64_ADR    0x100000005
    #endif
    #ifndef LLVMDisassembler_Option_PrintImmHex
        #define LLVMDisassembler_Option_PrintImmHex 2
    #endif
    asm(".linker_option \"-lLTO\"");
#endif

using mach_o::Architecture;
using mach_o::Instructions;

namespace other_tools {

SymbolicatedImage::SymbolicatedImage(const Image& im)
: _image(im), _is64(im.header()->is64()), _ptrSize(_is64 ? 8 : 4), _prefLoadAddress(im.header()->preferredLoadAddress())
{
    // build list of sections
    _image.header()->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
        std::string sectName(sectInfo.segmentName);
        sectName += ",";
        sectName += sectInfo.sectionName;
        _sectionSymbols.push_back({sectName, sectInfo});
    });

    // check for encrypted range
    uint32_t fairplayTextOffsetStart;
    uint32_t fairplaySize;
    if ( _image.header()->isFairPlayEncrypted(fairplayTextOffsetStart, fairplaySize) ) {
        _fairplayEncryptedStartAddr = _prefLoadAddress + fairplayTextOffsetStart;
        _fairplayEncryptedEndAddr   = _fairplayEncryptedStartAddr + fairplaySize;
    }

    // add entries for all functions for function-starts table
    if ( _image.hasFunctionStarts() ) {
        _image.functionStarts().forEachFunctionStart(0, ^(uint64_t funcAddr) {
            //printf("addr: 0x%08llX\n", funcAddr);
            char* label;
            asprintf(&label, "<anon-%08llX>", funcAddr);
            _symbolsMap[funcAddr] = label;
        });
    }
    __block bool hasLocalSymbols = false;
    if ( _image.hasSymbolTable() ) {
        // add symbols from nlist
        _image.symbolTable().forEachDefinedSymbol(^(const Symbol& symbol, uint32_t symbolIndex, bool& stop) {
            uint64_t absAddress;
            if ( !symbol.isAbsolute(absAddress) && (symbol.implOffset() != 0 || prefLoadAddress() == 0) && (symbol.sectionOrdinal()-1) < _sectionSymbols.size() ) {
                const char* symName = symbol.name().c_str();
                _symbolsMap[_prefLoadAddress+symbol.implOffset()] = symName;

                SectionSymbols& ss = _sectionSymbols[symbol.sectionOrdinal()-1];
                uint64_t offsetInSection = _prefLoadAddress+symbol.implOffset()-ss.sectInfo.address;
                ss.symbols.push_back({offsetInSection, symName, symbol.isThumb()});
            }
            if ( symbol.scope() == Symbol::Scope::translationUnit )
                hasLocalSymbols = true;
        });
        // add stubs and cstring literals
        std::span<const uint32_t> indirectTable = _image.indirectSymbolTable();
        __block std::vector<const char*> symbolNames;
        symbolNames.resize(_image.symbolTable().totalCount());
        _image.symbolTable().forEachSymbol(^(const char* symbolName, uint64_t n_value, uint8_t n_type, uint8_t n_sect, uint16_t n_desc, uint32_t symbolIndex, bool& stop) {
            symbolNames[symbolIndex] = symbolName;
        });
        _image.header()->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
            uint32_t type = (sectInfo.flags & SECTION_TYPE);
            if ( type == S_SYMBOL_STUBS ) {
                const int stubSize          = sectInfo.reserved2;
                int stubsIndirectStartIndex = sectInfo.reserved1;
                int stubsIndirectCount      = (int)(sectInfo.size / stubSize);
                for (int i=0; i < stubsIndirectCount; ++i) {
                    uint32_t symbolIndex = indirectTable[stubsIndirectStartIndex+i];
                    if ( (symbolIndex & (INDIRECT_SYMBOL_LOCAL|INDIRECT_SYMBOL_ABS)) == 0 && symbolIndex < symbolNames.size() )
                        _symbolsMap[sectInfo.address + stubSize*i] = symbolNames[symbolIndex];
                }
            }
            else if ( type == S_NON_LAZY_SYMBOL_POINTERS ) {
                int gotsIndirectStartIndex = sectInfo.reserved1;
                int gotsIndirectCount      = (int)(sectInfo.size / 8);  // FIXME: arm64_32
                for (int i=0; i < gotsIndirectCount; ++i) {
                    uint32_t symbolIndex = indirectTable[gotsIndirectStartIndex+i];
                    if ( (symbolIndex & (INDIRECT_SYMBOL_LOCAL|INDIRECT_SYMBOL_ABS)) == 0 && symbolIndex < symbolNames.size() )
                        _symbolsMap[sectInfo.address + 8*i] = symbolNames[symbolIndex];
                }
            }
        });
    }
    // options like -T in strip removes global Swift symbols from the nlist, but they'll
    // still present in the exports trie so we should parse both
    if ( _image.hasExportsTrie() ) {
        _image.exportsTrie().forEachExportedSymbol(^(const Symbol &symbol, bool &stop) {
            uint64_t absAddress=0;
            int ordinal=0;
            const char* importName = nullptr;
            if ( symbol.isAbsolute(absAddress) || symbol.isReExport(ordinal, importName) )
                return;

            uint64_t addr = _prefLoadAddress+symbol.implOffset();
            for ( SectionSymbols& sect : _sectionSymbols ) {
                if ( addr >= sect.sectInfo.address && addr < (sect.sectInfo.address+sect.sectInfo.size) ) {
                    if ( auto it = _symbolsMap.find(addr); it == _symbolsMap.end() ) {
                        const char* copiedSym = strdup(symbol.name().c_str());
                        sect.symbols.push_back({ addr-sect.sectInfo.address, copiedSym });
                        _symbolsMap[addr] = copiedSym;
                    } else if ( CString(it->second) != symbol.name() ) {
                        // different symbol names at the same location
                        sect.symbols.push_back({ addr-sect.sectInfo.address, strdup(symbol.name().c_str()) });
                    }
                }
            }
        });
    }

    // add c-string labels
    _image.header()->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
        uint32_t type = (sectInfo.flags & SECTION_TYPE);
        if ( type == S_CSTRING_LITERALS ) {
            const char* sectionContent  = (char*)content(sectInfo);
            const char* stringStart     = sectionContent;
            uint64_t    stringAddr      = sectInfo.address;
            for (int i=0; i < sectInfo.size; ++i) {
                if ( sectionContent[i] == '\0' ) {
                    if ( *stringStart != '\0' ) {
                        _stringLiteralsMap[stringAddr] = stringStart;
                        //fprintf(stderr, "0x%08llX -> \"%s\"\n", sectInfo.address + i, stringStart);
                    }
                    stringStart = &sectionContent[i+1];
                    stringAddr  = sectInfo.address + i + 1;
                }
            }
        }
    });
    _image.withSegments(^(std::span<const MappedSegment> segments) {
        for (const MappedSegment& seg : segments) {
            _mappedSegments.push_back(seg);
        }
    });

    // build list of fixups
    if ( image().header()->inDyldCache() ) {
        // images in dyld cache have no fixup info in LINKEDIT
        addDyldCacheBasedFixups();
    }
    else {
        // on disk image can use regular fixup inspection
        _image.forEachBindTarget(^(const Fixup::BindTarget& target, bool& stop) {
            _fixupTargets.push_back(target);
        });
        _image.forEachFixup(^(const Fixup& fixup, bool& stop) {
            this->addFixup(fixup);
        });
    }
    // if has ObjC and stripped
    if ( !hasLocalSymbols && _image.header()->hasObjC() ) {
        // add back stripped class and method names
        this->forEachDefinedObjCClass(^(uint64_t classVmAddr) {
            const char* classname       = this->className(classVmAddr);
            _symbolsMap[classVmAddr] = classname;
            this->forEachMethodInClass(classVmAddr, ^(const char* methodName, uint64_t implAddr) {
                char* label;
                asprintf(&label, "-[%s %s]", classname, methodName);
                _symbolsMap[implAddr] = label;
            });
            uint64_t    metaClassVmaddr = this->metaClassVmAddr(classVmAddr);
            this->forEachMethodInClass(metaClassVmaddr, ^(const char* methodName, uint64_t implAddr) {
                char* label;
                asprintf(&label, "+[%s %s]", classname, methodName);
                _symbolsMap[implAddr] = label;
            });
        });
        // add back objc stub names
        _image.header()->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
            if ( (sectInfo.sectionName == "__objc_stubs") && sectInfo.segmentName.starts_with("__TEXT") ) {
                const uint8_t* sectionContent = content(sectInfo);
                uint64_t       sectionVmAdr   = sectInfo.address;
                for (uint32_t offset=0; offset < sectInfo.size; ) {
                    uint64_t    labelAddr    = sectionVmAdr+offset;
                    const char* stubSelector = this->selectorFromObjCStub(sectionVmAdr, sectionContent, offset);
                    char* label;
                    asprintf(&label, "_objc_msgSend$%s", stubSelector);
                    _symbolsMap[labelAddr] = label;
                }
            }
        });
    }

    // add synthetic symbols that depend on fixups
    for (SectionSymbols& ss : _sectionSymbols) {
        if ( (ss.sectInfo.sectionName == "__objc_selrefs") && ss.sectInfo.segmentName.starts_with("__DATA") ) {
            for ( uint32_t sectOff=0; sectOff < ss.sectInfo.size; sectOff += _ptrSize) {
                const uint8_t* loc = content(ss.sectInfo) + sectOff;
                const auto&    pos = _fixupsMap.find(loc);
                if ( pos != _fixupsMap.end() ) {
                    const Fixup& fixup = _fixups[pos->second].fixup;
                    if ( !fixup.isBind ) {
                        if ( const char* selector = this->cStringAt(_prefLoadAddress + fixup.rebase.targetVmOffset) ) {
                            char* label;
                            asprintf(&label, "selector \"%s\"", selector);
                            _symbolsMap[ss.sectInfo.address+sectOff] = label;
                        }
                    }
                }
            }
        }
        else if ( (ss.sectInfo.sectionName == "__objc_superrefs") && ss.sectInfo.segmentName.starts_with("__DATA") ) {
            for ( uint32_t sectOff=0; sectOff < ss.sectInfo.size; sectOff += _ptrSize) {
                const uint8_t* loc = content(ss.sectInfo) + sectOff;
                const auto&    pos = _fixupsMap.find(loc);
                if ( pos != _fixupsMap.end() ) {
                    const Fixup& fixup = _fixups[pos->second].fixup;
                    if ( fixup.isBind ) {
                        // FIXME
                    }
                    else {
                        const auto& symbolPos = _symbolsMap.find(_prefLoadAddress + fixup.rebase.targetVmOffset);
                        if ( symbolPos != _symbolsMap.end() ) {
                            char* label;
                            asprintf(&label, "super %s", symbolPos->second);
                            _symbolsMap[ss.sectInfo.address+sectOff] = label;
                        }
                    }
                }
            }
        }
        else if ( (ss.sectInfo.sectionName == "__cfstring") && ss.sectInfo.segmentName.starts_with("__DATA") ) {
            const size_t cfStringSize = _ptrSize * 4;
            for ( uint32_t sectOff=0; sectOff < ss.sectInfo.size; sectOff += cfStringSize) {
                const uint8_t* curContent = content(ss.sectInfo) + sectOff;
                uint64_t       stringVmAddr;
                if ( this->isRebase(&curContent[cfStringSize/2], stringVmAddr) ) {
                    if ( const char* str = this->cStringAt(stringVmAddr) ) {
                        char* label;
                        asprintf(&label, "@\"%s\"", str);
                        _symbolsMap[ss.sectInfo.address+sectOff] = label;
                    }
                    else if ( *((uint32_t*)(&curContent[3*cfStringSize/4])) == 0 ) {
                        // empty string has no cstring
                        _symbolsMap[ss.sectInfo.address+sectOff] = "@\"\"";
                    }
                }
            }
        }
    }

    // sort symbols within section
    for (SectionSymbols& ss : _sectionSymbols) {
        std::sort(ss.symbols.begin(), ss.symbols.end(), [](const SectionSymbols::Sym& a, const SectionSymbols::Sym& b) {
            if ( a.offsetInSection == b.offsetInSection )
                return CString(a.name) < CString(b.name);
            return (a.offsetInSection < b.offsetInSection);
        });
    }
    // for any sections that don't have a symbol at start, add seg/sect synthetic symbol
    for (SectionSymbols& ss : _sectionSymbols) {
        if ( ss.symbols.empty() || (ss.symbols[0].offsetInSection != 0) ) {
            // section has no symbols, so makeup one at start
            std::string* str = new std::string(ss.sectInfo.segmentName);
            str->append(",");
            str->append(ss.sectInfo.sectionName);
            ss.symbols.insert(ss.symbols.begin(), {0, str->c_str()});
        }
    }
}

bool SymbolicatedImage::fairplayEncryptsSomeObjcStrings() const
{
    if ( _fairplayEncryptedStartAddr == 0 )
        return false;

    for (const SectionSymbols& ss : _sectionSymbols) {
        if ( ss.sectInfo.address < _fairplayEncryptedEndAddr ) {
            if ( ss.sectInfo.sectionName.starts_with("__objc_") )
                return true;
        }
    }
    return false;
}


const uint8_t* SymbolicatedImage::content(const Header::SectionInfo& sectInfo) const
{
    const Header* header = image().header();
    if ( header->inDyldCache() ) {
        return (uint8_t*)(sectInfo.address + header->getSlide());
    }
    else {
        return (uint8_t*)(header) + sectInfo.fileOffset;
    }
}

void SymbolicatedImage::addFixup(const Fixup& fixup)
{
    _fixupsMap[fixup.location] = _fixups.size();
    uint64_t    segOffset     = (uint8_t*)fixup.location - (uint8_t*)(fixup.segment->content);
    uint64_t    runtimeOffset = fixup.segment->runtimeOffset + segOffset;
    uint64_t    address       = _prefLoadAddress + runtimeOffset;
    SymbolLoc   symbolLoc = findClosestSymbol(address);
    uint32_t    sectNum = 1;
    for ( const SectionSymbols& ss : _sectionSymbols ) {
        if ( ss.sectInfo.segmentName == fixup.segment->segName ) {
            if ( (ss.sectInfo.address <= address) && (address < ss.sectInfo.address+ss.sectInfo.size) )
                break;
        }
        sectNum++;
    }
    _fixups.push_back({fixup, symbolLoc, address, sectNum});
}


void SymbolicatedImage::addDyldCacheBasedFixups()
{
    // FIXME:
}

SymbolicatedImage::SymbolLoc SymbolicatedImage::findClosestSymbol(uint64_t runtimeOffset) const
{
    SymbolLoc loc;
    for (const SectionSymbols& ss : _sectionSymbols) {
        if ( (runtimeOffset >= ss.sectInfo.address) && (runtimeOffset < ss.sectInfo.address+ss.sectInfo.size) ) {
            if ( ss.symbols.empty() ) {
                loc.name           = ss.sectStartName.c_str();
                loc.inSymbolOffset = (uint32_t)(runtimeOffset - ss.sectInfo.address);
                loc.isThumb = false;
                continue;
            }

            // find largest symbol address that is <= target address
            const uint64_t targetSectOffset = runtimeOffset-ss.sectInfo.address;
            auto it = std::lower_bound(ss.symbols.begin(), ss.symbols.end(), targetSectOffset, [](const SectionSymbols::Sym& sym, uint64_t sectOffset) -> bool {
                return sym.offsetInSection <= sectOffset;
            });
            // lower_bound returns the symbol after the one we need
            if ( it != ss.symbols.begin() ) {
                --it;
                loc.name           = it->name;
                loc.inSymbolOffset = (uint32_t)(runtimeOffset - (ss.sectInfo.address+it->offsetInSection));
                loc.isThumb        = it->thumb;
            }
            else {
                const SectionSymbols::Sym& sym = ss.symbols.front();
                loc.name           = sym.name;
                loc.isThumb        = sym.thumb;
                loc.inSymbolOffset = (uint32_t)(runtimeOffset - (ss.sectInfo.address + sym.offsetInSection));
            }
            break;
        }
    }
    return loc;
}

const char* SymbolicatedImage::selectorFromObjCStub(uint64_t sectionVmAdr, const uint8_t* sectionContent, uint32_t& offset) const
{
    if ( _image.header()->arch().usesArm64Instructions() ) {
        const uint32_t* instructions = (uint32_t*)(sectionContent + offset);
        uint32_t selAdrpInstruction  = instructions[0];
        uint32_t selLdrInstruction   = instructions[1];
        if ( (selAdrpInstruction & 0x9F000000) == 0x90000000 ) {
            int32_t  adrpAddend     = (int32_t)(((selAdrpInstruction & 0x60000000) >> 29) | ((selAdrpInstruction & 0x01FFFFE0) >> 3));
            uint64_t adrpTargetAddr = ((sectionVmAdr+offset) & (-4096)) + adrpAddend*0x1000;
            if ( (selLdrInstruction & 0x3B000000) == 0x39000000 ) {
                uint64_t    ldrAddend       = ((selLdrInstruction & 0x003FFC00) >> 10) * _ptrSize;
                uint64_t    selectorVmAddr  = adrpTargetAddr + ldrAddend;
                const void* selectorContent = this->locationFromVmAddr(selectorVmAddr);
                uint64_t    rebaseTargetVmAddr;
                if ( this->isRebase(selectorContent, rebaseTargetVmAddr) ) {
                    if ( const char* selector = this->cStringAt(rebaseTargetVmAddr) ) {
                        offset += 0x20;
                        return selector;
                    }
                }
            }
        }
        offset += 0x20;
    }

    return nullptr;
}


const char* SymbolicatedImage::symbolNameAt(uint64_t addr) const
{
    const auto& pos = _symbolsMap.find(addr);
    if ( pos != _symbolsMap.end() )
        return pos->second;
    return nullptr;
}

const char* SymbolicatedImage::cStringAt(uint64_t addr) const
{
    if ( (_fairplayEncryptedStartAddr <= addr) && (addr < _fairplayEncryptedEndAddr) )
        return "##unavailable##";

    const auto& pos = _stringLiteralsMap.find(addr);
    if ( pos != _stringLiteralsMap.end() )
        return pos->second;
    return nullptr;
}

bool SymbolicatedImage::isBind(const void* location, const Fixup::BindTarget*& bindTarget) const
{
    const auto& pos = _fixupsMap.find(location);
    if ( pos == _fixupsMap.end() )
        return false;
    const Fixup& fixup = _fixups[pos->second].fixup;
    if ( !fixup.isBind )
        return false;
    bindTarget = &_fixupTargets[fixup.bind.bindOrdinal];
    return true;
}

bool SymbolicatedImage::isRebase(const void* location, uint64_t& rebaseTargetVmAddr) const
{
    const auto& pos = _fixupsMap.find(location);
    if ( pos == _fixupsMap.end() )
        return false;
    const Fixup& fixup = _fixups[pos->second].fixup;
    if ( fixup.isBind )
        return false;
    if ( !_is64 && _image.header()->isMainExecutable() )
        rebaseTargetVmAddr = fixup.rebase.targetVmOffset;   // arm64_32 main rebases start at 0 not start of TEXt
    else
        rebaseTargetVmAddr = _prefLoadAddress + fixup.rebase.targetVmOffset;
    return true;
}

const void* SymbolicatedImage::locationFromVmAddr(uint64_t addr) const
{
    uint64_t vmOffset = addr - _prefLoadAddress;
    for ( const MappedSegment& seg : _mappedSegments ) {
        if ( seg.readable && (seg.runtimeOffset <= vmOffset) && (vmOffset < seg.runtimeOffset+seg.runtimeSize) ) {
            return (void*)((uint8_t*)seg.content + vmOffset - seg.runtimeOffset);
        }
    }
    return nullptr;
}

// for a section with symbols in it, divides up section by symbols and call callback once for each range
// if there is no symbol at start of section, callback is called for that range with NULL for name
void SymbolicatedImage::forEachSymbolRangeInSection(size_t sectNum, void (^callback)(const char* name, uint64_t addr, uint64_t size)) const
{
    const SectionSymbols&   ss       = _sectionSymbols[sectNum-1];
    uint64_t                lastAddr = ss.sectInfo.address;
    const char*             lastName = nullptr;
    for (const SectionSymbols::Sym& sym : ss.symbols) {
        uint64_t addr = ss.sectInfo.address + sym.offsetInSection;
        if ( (lastName == nullptr) && (addr == ss.sectInfo.address) ) {
            // if first symbol is start of section, we don't need extra callback
        }
        else {
            callback(lastName, lastAddr, addr - lastAddr);
        }
        lastAddr = addr;
        lastName = sym.name;
    }
    if ( lastName != nullptr )
        callback(lastName, lastAddr, ss.sectInfo.address + ss.sectInfo.size - lastAddr);
}


const char* SymbolicatedImage::className(uint64_t classVmAddr) const
{
    uint64_t roDataFieldAddr = classVmAddr + 4*_ptrSize;
    if ( const void* roDataFieldContent = this->locationFromVmAddr(roDataFieldAddr) ) {
        uint64_t roDataVmAddr;
        if ( this->isRebase(roDataFieldContent, roDataVmAddr) ) {
            roDataVmAddr &= -4; // remove swift bits
            uint64_t nameFieldAddr = roDataVmAddr + 3*_ptrSize;
            if ( const void* nameFieldContent = this->locationFromVmAddr(nameFieldAddr) ) {
                uint64_t nameAddr;
                if ( this->isRebase(nameFieldContent, nameAddr) ) {
                    if ( const char* className = this->cStringAt(nameAddr) ) {
                        return className;
                    }
                }
            }
        }
    }
    return nullptr;
}

const char* SymbolicatedImage::superClassName(uint64_t classVmAddr) const
{
    uint64_t superClassFieldAddr = classVmAddr + _ptrSize;
    if ( const void* superClassFieldContent = this->locationFromVmAddr(superClassFieldAddr) ) {
        uint64_t                 superClassVmAddr;
        const Fixup::BindTarget* superClassBindTarget;
        if ( this->isRebase(superClassFieldContent, superClassVmAddr) ) {
            return this->className(superClassVmAddr);
        }
        else if ( this->isBind(superClassFieldContent, superClassBindTarget) ) {
            CString supername = superClassBindTarget->symbolName;
            if ( supername.starts_with("_OBJC_CLASS_$_") ) {
                return supername.substr(14).c_str();
            }
            return supername.c_str();
        }
    }
    return nullptr;
}

// if object conforms to protocols, return string of protocol names, eg. "<NSFoo, NSBar>"
void SymbolicatedImage::getProtocolNames(uint64_t protocolListFieldAddr, char names[1024]) const
{
    if ( const void* baseProtocolsFieldContent = this->locationFromVmAddr(protocolListFieldAddr) ) {
        uint64_t baseProtocolsListAddr;
        if ( this->isRebase(baseProtocolsFieldContent, baseProtocolsListAddr) ) {
            if ( const void* protocolListContent = this->locationFromVmAddr(baseProtocolsListAddr) ) {
                uint32_t count = *(uint32_t*)protocolListContent;
                strlcpy(names, "<", 1024);
                bool needComma = false;
                for (uint32_t i=0; i < count; ++i) {
                    if ( needComma )
                        strlcat(names, ", ", 1024);
                    uint64_t protocolPtrAddr = baseProtocolsListAddr + (i+1)*_ptrSize;
                    if ( const void* protocolPtrContent = this->locationFromVmAddr(protocolPtrAddr) ) {
                        uint64_t protocolAddr;
                        if ( this->isRebase(protocolPtrContent, protocolAddr) ) {
                            uint64_t protocolNameFieldAddr = protocolAddr + _ptrSize;
                            if ( const void* protocolNameFieldContent = this->locationFromVmAddr(protocolNameFieldAddr) ) {
                                uint64_t protocolStringAddr;
                                if ( this->isRebase(protocolNameFieldContent, protocolStringAddr) ) {
                                    if ( const char* ptotocolName = this->cStringAt(protocolStringAddr) ) {
                                        strlcat(names, ptotocolName, 1024);
                                    }
                                }
                            }
                        }
                    }
                    needComma = true;
                }
                strlcat(names, ">", 1024);
            }
        }
    }
}

// if class conforms to protocols, return string of protocol names, eg. "<NSFoo, NSBar>"
void SymbolicatedImage::getClassProtocolNames(uint64_t classVmAddr, char names[1024]) const
{
    names[0] = '\0';
    uint64_t roDataFieldAddr = classVmAddr + 4*_ptrSize;
    if ( const void* roDataFieldContent = this->locationFromVmAddr(roDataFieldAddr) ) {
        uint64_t roDataVmAddr;
        if ( this->isRebase(roDataFieldContent, roDataVmAddr) ) {
            roDataVmAddr &= -4; // remove swift bits
            uint64_t baseProtocolsFieldAddr = roDataVmAddr + ((_ptrSize==8) ? 40 : 24);
            getProtocolNames(baseProtocolsFieldAddr, names);
        }
    }
}

// if protocol conforms to protocols, return string of protocol names, eg. "<NSFoo, NSBar>"
void SymbolicatedImage::getProtocolProtocolNames(uint64_t protocolVmAddr, char names[1024]) const
{
    names[0] = '\0';
    uint64_t baseProtocolsFieldAddr = protocolVmAddr + 2*_ptrSize;
    getProtocolNames(baseProtocolsFieldAddr, names);
}

uint64_t SymbolicatedImage::metaClassVmAddr(uint64_t classVmAddr) const
{
    uint64_t metaClassFieldAddr = classVmAddr;
    if ( const void* metaClassFieldContent = this->locationFromVmAddr(metaClassFieldAddr) ) {
        uint64_t                 metaClassVmAddress;
        const Fixup::BindTarget* bindTarget;
        if ( this->isRebase(metaClassFieldContent, metaClassVmAddress) ) {
            return metaClassVmAddress;
        }
        else if ( this->isBind(metaClassFieldContent, bindTarget) ) {
            // for faster dyld cache patch, classlist is sometimes binds to self for class instead of rebase
            Symbol symbol;
            if ( _image.exportsTrie().hasExportedSymbol(bindTarget->symbolName.c_str(), symbol) ) {
                return symbol.implOffset();
            }
        }
    }
    return 0;
}

const char* SymbolicatedImage::categoryName(uint64_t categoryVmAddr) const
{
    uint64_t catNameFieldAddr = categoryVmAddr;
    if ( const void* catNameFieldContent = this->locationFromVmAddr(catNameFieldAddr) ) {
        uint64_t nameVmAddr;
        if ( this->isRebase(catNameFieldContent, nameVmAddr) ) {
            if ( const char* className = this->cStringAt(nameVmAddr) ) {
                return className;
            }
        }
    }
    return nullptr;
}

const char* SymbolicatedImage::categoryClassName(uint64_t categoryVmAddr) const
{
    uint64_t classFieldAddr = categoryVmAddr + _ptrSize;
    if ( const void* classFieldContent = this->locationFromVmAddr(classFieldAddr) ) {
        uint64_t                 classVmAddr;
        const Fixup::BindTarget* classBindTarget;
        if ( this->isRebase(classFieldContent, classVmAddr) ) {
            return this->className(classVmAddr);
        }
        else if ( this->isBind(classFieldContent, classBindTarget) ) {
            CString supername = classBindTarget->symbolName;
            if ( supername.starts_with("_OBJC_CLASS_$_") ) {
                return supername.substr(14).c_str();
            }
            return supername.c_str();
        }
    }
    return nullptr;
}

const char* SymbolicatedImage::protocolName(uint64_t protocolVmAddr) const
{
    uint64_t protocolNameFieldAddr = protocolVmAddr + _ptrSize;
    if ( const void* protocolNameFieldContent = this->locationFromVmAddr(protocolNameFieldAddr) ) {
        uint64_t nameVmAddr;
        if ( this->isRebase(protocolNameFieldContent, nameVmAddr) ) {
            if ( const char* protocolName = this->cStringAt(nameVmAddr) ) {
                return protocolName;
            }
        }
    }
    return nullptr;
}


void SymbolicatedImage::forEachMethodInClass(uint64_t classVmAddr, void (^callback)(const char* methodName, uint64_t implAddr)) const
{
    uint64_t roDataFieldAddr = classVmAddr + 4*_ptrSize;
    if ( const void* roDataFieldContent = this->locationFromVmAddr(roDataFieldAddr) ) {
        uint64_t roDataVmAddr;
        if ( this->isRebase(roDataFieldContent, roDataVmAddr) ) {
            roDataVmAddr &= -4; // remove swift bits
            uint64_t methodListFieldAddr = roDataVmAddr + ((_ptrSize==8) ? 32 : 20);
            if ( const void* methodListFieldContent = this->locationFromVmAddr(methodListFieldAddr) ) {
                uint64_t methodListAddr;
                if ( this->isRebase(methodListFieldContent, methodListAddr) ) {
                    this->forEachMethodInList(methodListAddr, callback);
                }
            }
        }
    }
}

void SymbolicatedImage::forEachMethodInCategory(uint64_t categoryVmAddr, void (^instanceCallback)(const char* methodName, uint64_t implAddr),
                                                void (^classCallback)(const char* methodName, uint64_t implAddr)) const
{
    uint64_t instanceMethodsFieldAddr = categoryVmAddr + 2*_ptrSize;
    if ( const void* instanceMethodsFieldContent = this->locationFromVmAddr(instanceMethodsFieldAddr) ) {
        uint64_t  methodListVmAddr;
        if ( this->isRebase(instanceMethodsFieldContent, methodListVmAddr) ) {
            this->forEachMethodInList(methodListVmAddr, instanceCallback);
        }
    }
    uint64_t classMethodsFieldAddr = categoryVmAddr + 3*_ptrSize;
    if ( const void* classMethodsFieldContent = this->locationFromVmAddr(classMethodsFieldAddr) ) {
        uint64_t  methodListVmAddr;
        if ( this->isRebase(classMethodsFieldContent, methodListVmAddr) ) {
            this->forEachMethodInList(methodListVmAddr, classCallback);
        }
    }
}

void SymbolicatedImage::forEachMethodInProtocol(uint64_t protocolVmAddr,
                                                void (^instanceCallback)(const char* methodName),
                                                void (^classCallback)(const char* methodName),
                                                void (^optionalInstanceCallback)(const char* methodName),
                                                void (^optionalClassCallback)(const char* methodName)) const
{
    uint64_t instanceMethodsFieldAddr = protocolVmAddr + 3*_ptrSize;
    if ( const void* instanceMethodsFieldContent = this->locationFromVmAddr(instanceMethodsFieldAddr) ) {
        uint64_t  methodListVmAddr;
        if ( this->isRebase(instanceMethodsFieldContent, methodListVmAddr) ) {
            this->forEachMethodInList(methodListVmAddr, ^(const char* methodName, uint64_t implAddr) {
                instanceCallback(methodName);
            });
        }
    }
    uint64_t classMethodsFieldAddr = protocolVmAddr + 4*_ptrSize;
    if ( const void* classMethodsFieldContent = this->locationFromVmAddr(classMethodsFieldAddr) ) {
        uint64_t  methodListVmAddr;
        if ( this->isRebase(classMethodsFieldContent, methodListVmAddr) ) {
            this->forEachMethodInList(methodListVmAddr, ^(const char* methodName, uint64_t implAddr) {
                classCallback(methodName);
            });
        }
    }
    uint64_t optionalInstanceMethodsFieldAddr = protocolVmAddr + 5*_ptrSize;
    if ( const void* instanceMethodsFieldContent = this->locationFromVmAddr(optionalInstanceMethodsFieldAddr) ) {
        uint64_t  methodListVmAddr;
        if ( this->isRebase(instanceMethodsFieldContent, methodListVmAddr) ) {
            this->forEachMethodInList(methodListVmAddr, ^(const char* methodName, uint64_t implAddr) {
                optionalInstanceCallback(methodName);
            });
        }
    }
    uint64_t optionalClassMethodsFieldAddr = protocolVmAddr + 6*_ptrSize;
    if ( const void* classMethodsFieldContent = this->locationFromVmAddr(optionalClassMethodsFieldAddr) ) {
        uint64_t  methodListVmAddr;
        if ( this->isRebase(classMethodsFieldContent, methodListVmAddr) ) {
            this->forEachMethodInList(methodListVmAddr, ^(const char* methodName, uint64_t implAddr) {
                optionalClassCallback(methodName);
            });
        }
    }
}


void SymbolicatedImage::forEachMethodInList(uint64_t methodListVmAddr, void (^callback)(const char* methodName, uint64_t implAddr)) const
{
    if ( const void* methodListContent = this->locationFromVmAddr(methodListVmAddr) ) {
        const uint32_t* methodListArray = (uint32_t*)methodListContent;
        uint32_t entrySize = methodListArray[0];
        uint32_t count     = methodListArray[1];
        if ( entrySize == 0x8000000C ) {
            // relative method lists
            for (uint32_t i=0; i < count; ++i) {
                int32_t  nameOffset           = methodListArray[i*3+2];
                uint64_t methodSelectorVmAddr = methodListVmAddr+i*12+nameOffset+8;
                int32_t  implOffset           = methodListArray[i*3+4];
                uint64_t implAddr             = methodListVmAddr+i*12+implOffset+16;
                if ( const void* methodSelectorContent = this->locationFromVmAddr(methodSelectorVmAddr) ) {
                    uint64_t selectorTargetAddr;
                    if ( this->isRebase(methodSelectorContent, selectorTargetAddr) ) {
                        if ( const char* methodName = this->cStringAt(selectorTargetAddr) )
                            callback(methodName, implAddr);
                    }
                }
            }
        }
        else if ( entrySize == 24 ) {
            // 64-bit absolute method lists
            for (uint32_t i=0; i < count; ++i) {
                if ( const void* methodNameContent = this->locationFromVmAddr(methodListVmAddr+i*24+8) ) {
                    uint64_t methodNameAddr;
                    if ( this->isRebase(methodNameContent, methodNameAddr) ) {
                        if ( const char* methodName = this->cStringAt(methodNameAddr) ) {
                            if ( const void* methodImplContent = this->locationFromVmAddr(methodListVmAddr+i*24+24) ) {
                                uint64_t implAddr;
                                if ( this->isRebase(methodImplContent, implAddr) ) {
                                    callback(methodName, implAddr);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}


void SymbolicatedImage::forEachDefinedObjCClass(void (^callback)(uint64_t classVmAddr)) const
{
    _image.header()->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
        if ( (sectInfo.sectionName == "__objc_classlist") && sectInfo.segmentName.starts_with("__DATA") ) {
            const uint8_t* sectionContent    = content(sectInfo);
            const uint8_t* sectionContentEnd = sectionContent + sectInfo.size;
            const uint8_t* curContent        = sectionContent;
            while ( curContent < sectionContentEnd ) {
                const Fixup::BindTarget* bindTarget;
                uint64_t                 rebaseTargetVmAddr;
                if ( this->isRebase(curContent, rebaseTargetVmAddr) ) {
                    callback(rebaseTargetVmAddr);
                }
                else if ( this->isBind(curContent, bindTarget) ) {
                    // for faster dyld cache patch, classlist is sometimes binds to self for class instead of rebase
                    Symbol symbol;
                    if ( _image.exportsTrie().hasExportedSymbol(bindTarget->symbolName.c_str(), symbol) ) {
                        callback(symbol.implOffset());
                    }
                }
                curContent += _ptrSize;
            }
        }
    });
}

void SymbolicatedImage::forEachObjCCategory(void (^callback)(uint64_t categoryVmAddr)) const
{
    _image.header()->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
        if ( (sectInfo.sectionName == "__objc_catlist") && sectInfo.segmentName.starts_with("__DATA") ) {
            const uint8_t* sectionContent    = content(sectInfo);
            const uint8_t* sectionContentEnd = sectionContent + sectInfo.size;
            const uint8_t* curContent        = sectionContent;
            while ( curContent < sectionContentEnd ) {
                uint64_t   rebaseTargetVmAddr;
                if ( this->isRebase(curContent, rebaseTargetVmAddr) )
                    callback(rebaseTargetVmAddr);
                curContent += _ptrSize;
            }
        }
    });
}

void SymbolicatedImage::forEachObjCProtocol(void (^callback)(uint64_t protocolVmAddr)) const
{
    _image.header()->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
        if ( sectInfo.sectionName == "__objc_protolist" ) {
            const uint8_t* sectionContent    = content(sectInfo);
            const uint8_t* sectionContentEnd = sectionContent + sectInfo.size;
            const uint8_t* curContent        = sectionContent;
            while ( curContent < sectionContentEnd ) {
                uint64_t   rebaseTargetVmAddr;
                if ( this->isRebase(curContent, rebaseTargetVmAddr) )
                    callback(rebaseTargetVmAddr);
                curContent += _ptrSize;
            }
        }
    });
}

const char* SymbolicatedImage::libOrdinalName(const Header* header, int libOrdinal, char buffer[128])
{
    CString name = header->libOrdinalName(libOrdinal);
    // convert:
    //  /path/stuff/Foo.framework/Foo   => Foo
    //  /path/stuff/libfoo.dylib        => libfoo
    //  /path/stuff/libfoo.A.dylib      => libfoo
    strlcpy(buffer, name.leafName().c_str(), 128);
    if ( name.ends_with(".dylib") ) {
        size_t len = strlen(buffer);
        buffer[len-6] = '\0';
        if ( (len > 8) && (buffer[len-8] == '.') )
            buffer[len-8] = '\0';
    }

    return buffer;
}

const char* SymbolicatedImage::fixupTypeString(size_t fixupIndex) const
{
    const Fixup& fixup = _fixups[fixupIndex].fixup;
    if ( fixup.isBind  ) {
        if ( fixup.authenticated )
            return "auth-bind";
        else if ( fixup.isLazyBind )
            return "lazy-bind";
        else
            return "bind";
    }
    else {
        if ( fixup.authenticated )
            return "auth-rebase";
        else
            return "rebase";
    }
}

const char* SymbolicatedImage::fixupTargetString(size_t fixupIndex, bool symbolic, char buffer[4096]) const
{
    const Fixup& fixup = _fixups[fixupIndex].fixup;
    char         authInfo[64];

    authInfo[0] = '\0';
    if ( fixup.authenticated )
        snprintf(authInfo, sizeof(authInfo), " (div=0x%04X ad=%d key=%s)", fixup.auth.diversity, fixup.auth.usesAddrDiversity, fixup.keyName());

    if ( fixup.isBind ) {
        char                     dylibBuf[128];
        const Fixup::BindTarget& bindTarget = _fixupTargets[fixup.bind.bindOrdinal];
        int64_t                  addend     = bindTarget.addend + fixup.bind.embeddedAddend;

        if ( addend != 0 )
            snprintf(buffer, 4096, "%s/%s + 0x%llX%s",      libOrdinalName(bindTarget.libOrdinal, dylibBuf), bindTarget.symbolName.c_str(), addend, authInfo);
        else if ( bindTarget.weakImport )
            snprintf(buffer, 4096, "%s/%s [weak-import]%s", libOrdinalName(bindTarget.libOrdinal, dylibBuf), bindTarget.symbolName.c_str(), authInfo);
        else
            snprintf(buffer, 4096, "%s/%s%s",               libOrdinalName(bindTarget.libOrdinal, dylibBuf), bindTarget.symbolName.c_str(), authInfo);
    }
    else {
        if ( symbolic ) {
            SymbolLoc loc = findClosestSymbol(fixup.rebase.targetVmOffset);
            if ( loc.name.starts_with("__TEXT,") ) {
                const char* str = this->cStringAt(_prefLoadAddress+fixup.rebase.targetVmOffset);
                snprintf(buffer, 4096, "\"%s\"%s", str, authInfo);
            }
            else if ( loc.inSymbolOffset == 0 ) {
                snprintf(buffer, 4096, "%s%s", loc.name.c_str(), authInfo);
            }
            else {
                snprintf(buffer, 4096, "%s+%u%s", loc.name.c_str(), loc.inSymbolOffset, authInfo);
            }
        }
        else {
            snprintf(buffer, 4096, "0x%08llX%s",_prefLoadAddress+fixup.rebase.targetVmOffset, authInfo);
        }
    }
    return buffer;
}

SymbolicatedImage::~SymbolicatedImage()
{
#if HAVE_LIBLTO
    if ( _llvmRef != nullptr ) {
        LLVMDisasmDispose(_llvmRef);
        _llvmRef = nullptr;
    }
    if ( _llvmThumbRef != nullptr ) {
        LLVMDisasmDispose(_llvmThumbRef);
        _llvmThumbRef = nullptr;
    }
#endif
}


#if HAVE_LIBLTO

static const char* printDumpSymbolCallback(void* di, uint64_t referenceValue, uint64_t* referenceType,
                                           uint64_t referencePC, const char** referenceName)
{
    return ((SymbolicatedImage*)di)->lookupSymbol(referencePC, referenceValue, *referenceType, *referenceName);
}

static int printDumpOpInfoCallback(void* di, uint64_t pc, uint64_t offset, uint64_t opSize, /* uint64_t instSize, */
                                   int tagType, void* tagBuf)
{
    return ((SymbolicatedImage*)di)->opInfo(pc, offset, opSize, tagType, tagBuf);
}

const char* SymbolicatedImage::targetTriple() const
{
    Architecture arch = _image.header()->arch();
    if ( arch.usesArm64AuthPointers() )
        return "arm64e-apple-darwin";
    else if ( arch.usesArm64Instructions() )
        return "arm64-apple-darwin";
    else if ( arch.usesx86_64Instructions() )
        return "x86_64h-apple-darwin";
    else if ( arch.usesThumbInstructions() && !arch.usesArm32Instructions() )
        return "thumbv7em-apple-darwin";
    else if ( arch.usesArm32Instructions() )
        return "armv7-apple-darwin";
    else if ( arch == Architecture::i386 )
        return "i386-apple-darwin";
    else
        return "unknown";
}

void SymbolicatedImage::loadDisassembler()
{
    // libLTO is weak linked, need to bail out if libLTO.dylib not loaded
    if ( &lto_initialize_disassembler == nullptr )
        return;

    // Hack for using llvm disassemble in libLTO.dylib
    static bool llvmDisAssemblerInitialized = false;
    if ( !llvmDisAssemblerInitialized ) {
        lto_initialize_disassembler();
        llvmDisAssemblerInitialized = true;
    }

    // instantiate llvm disassembler object
    _llvmRef = LLVMCreateDisasm(targetTriple(), this, 0, &printDumpOpInfoCallback, &printDumpSymbolCallback);
    if ( _llvmRef != nullptr )
        LLVMSetDisasmOptions(_llvmRef, LLVMDisassembler_Option_PrintImmHex);

    Architecture arch = _image.header()->arch();
    if ( arch.usesArm32Instructions() && arch.usesThumbInstructions() ) {
        _llvmThumbRef = LLVMCreateDisasm("thumbv7em-apple-darwin", this, 0, &printDumpOpInfoCallback, &printDumpSymbolCallback);
        if ( _llvmThumbRef != nullptr )
            LLVMSetDisasmOptions(_llvmThumbRef, LLVMDisassembler_Option_PrintImmHex);
    }
}


const char* SymbolicatedImage::lookupSymbol(uint64_t refPC, uint64_t refValue, uint64_t& refType, const char*& refName)
{
    //printf("refPC=0x%08llX, refType=0x%llX\n", refPC, refType);
    refName = nullptr;
    if ( refType == LLVMDisassembler_ReferenceType_In_Branch ) {
        refType = LLVMDisassembler_ReferenceType_InOut_None;
        const auto& pos = _symbolsMap.find(refValue);
        if ( pos != _symbolsMap.end() ) {
            return pos->second;
        }
        return nullptr;
    }
    else if ( refType == LLVMDisassembler_ReferenceType_In_ARM64_ADR ) {
        const auto& pos = _stringLiteralsMap.find(refValue);
        if ( pos != _stringLiteralsMap.end() ) {
            refType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
            refName = pos->second;
            return nullptr;
        }
        else {
            const auto& pos2 = _symbolsMap.find(refValue);
            if ( pos2 != _symbolsMap.end() ) {
                refName = pos2->second;
                refType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
                return nullptr;
            }

        }
    }
    else if ( refType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ) {
        const auto& pos = _symbolsMap.find(refValue);
        if ( pos != _symbolsMap.end() ) {
            refType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
            refName = pos->second;
            return nullptr;
        }
    }
    else if ( (refType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui) || (refType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri) ) {
        const uint32_t* instructionPtr = (uint32_t*)(&_disasmSectContentBias[refPC]);
        uint32_t thisInstruction = instructionPtr[0];
        uint32_t prevInstruction = instructionPtr[-1];
        // if prev instruction was ADRP and it matches register, the we can compute target
        Instructions::arm64::AdrpInfo  adrpInfo;
        Instructions::arm64::Imm12Info imm12Info;
        if ( Instructions::arm64::isADRP(prevInstruction, adrpInfo) && Instructions::arm64::isImm12(thisInstruction, imm12Info) ) {
            if ( adrpInfo.dstReg == imm12Info.srcReg ) {
                uint64_t targetAddr = (refPC & -4096) + adrpInfo.pageOffset*4096 + imm12Info.offset;
                const auto& pos = _symbolsMap.find(targetAddr);
                if ( pos != _symbolsMap.end() ) {
                    refName = pos->second;
                    refType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
                    return nullptr;
                }
            }
        }
    }
    else if ( refType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP ) {
    }
    else if ( refType == LLVMDisassembler_ReferenceType_In_PCrel_Load ) {
        const auto& pos = _stringLiteralsMap.find(refValue);
        if ( pos != _stringLiteralsMap.end() ) {
            refType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
            refName = pos->second;
            return nullptr;
        }
        const auto& pos2 = _symbolsMap.find(refValue);
        if ( pos2 != _symbolsMap.end() ) {
            refName = pos2->second;
            return nullptr;
        }
        return nullptr;
    }
    else if ( refType == LLVMDisassembler_ReferenceType_InOut_None ) {
        return nullptr;
    }
    else {
        //printf("refPC=0x%08llX, refType=0x%llX\n", refPC, refType);
    }
    return nullptr;
}

int SymbolicatedImage::opInfo(uint64_t pc, uint64_t offset, uint64_t opSize, /* uint64_t instSize, */int tagType, void* tagBuf)
{
    return 0;
}
#endif // HAVE_LIBLTO
} // namespace other_tools