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 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2018-2019 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 <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> #include <stdarg.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <mach-o/dyld_introspection.h> #include <mach-o/dyld_priv.h> #include <vector> #include <tuple> #include <set> #include <unordered_set> #include <string> #include "Array.h" #include "MachOFile.h" #include "MachOLoaded.h" #include "MachOAnalyzer.h" #include "ClosureFileSystemPhysical.h" #include "DyldSharedCache.h" #include "JSONWriter.h" #include "FileUtils.h" using dyld3::MachOAnalyzer; typedef dyld3::MachOLoaded::ChainedFixupPointerOnDisk ChainedFixupPointerOnDisk; typedef dyld3::MachOLoaded::PointerMetaData PointerMetaData; static void versionToString(uint32_t value, char buffer[32]) { if ( value == 0 ) strcpy(buffer, "n/a"); else if ( value & 0xFF ) sprintf(buffer, "%d.%d.%d", value >> 16, (value >> 8) & 0xFF, value & 0xFF); else sprintf(buffer, "%d.%d", value >> 16, (value >> 8) & 0xFF); } static void printPlatforms(const dyld3::MachOAnalyzer* ma) { printf(" -platform:\n"); printf(" platform minOS sdk\n"); ma->forEachSupportedPlatform(^(dyld3::Platform platform, uint32_t minOS, uint32_t sdk) { char osVers[32]; char sdkVers[32]; versionToString(minOS, osVers); versionToString(sdk, sdkVers); printf(" %15s %-7s %-7s\n", dyld3::MachOFile::platformName(platform), osVers, sdkVers); }); } static void permString(uint32_t permFlags, char str[4]) { str[0] = (permFlags & VM_PROT_READ) ? 'r' : '.'; str[1] = (permFlags & VM_PROT_WRITE) ? 'w' : '.'; str[2] = (permFlags & VM_PROT_EXECUTE) ? 'x' : '.'; str[3] = '\0'; } static void printSegments(const dyld3::MachOAnalyzer* ma, const DyldSharedCache* cache) { if ( DyldSharedCache::inDyldCache(cache, ma) ) { printf(" -segments:\n"); printf(" load-address segment section sect-size seg-size perm\n"); __block const char* lastSegName = ""; ma->forEachSection(^(const dyld3::MachOFile::SectionInfo& sectInfo, bool malformedSectionRange, bool& stop) { if ( strcmp(lastSegName, sectInfo.segInfo.segName) != 0 ) { char permChars[8]; permString(sectInfo.segInfo.protections, permChars); printf(" 0x%08llX %-16s %16lluKB %s\n", sectInfo.segInfo.vmAddr, sectInfo.segInfo.segName, sectInfo.segInfo.vmSize/1024, permChars); lastSegName = sectInfo.segInfo.segName; } printf(" 0x%08llX %-16s %6llu\n", sectInfo.sectAddr, sectInfo.sectName, sectInfo.sectSize); }); } else { printf(" -segments:\n"); printf(" load-offset segment section sect-size seg-size perm\n"); __block const char* lastSegName = ""; __block uint64_t firstSegVmAddr = 0; ma->forEachSection(^(const dyld3::MachOFile::SectionInfo& sectInfo, bool malformedSectionRange, bool& stop) { if ( lastSegName[0] == '\0' ) firstSegVmAddr = sectInfo.segInfo.vmAddr; if ( strcmp(lastSegName, sectInfo.segInfo.segName) != 0 ) { char permChars[8]; permString(sectInfo.segInfo.protections, permChars); printf(" 0x%08llX %-16s %6lluKB %s\n", sectInfo.segInfo.vmAddr - firstSegVmAddr, sectInfo.segInfo.segName, sectInfo.segInfo.vmSize/1024, permChars); lastSegName = sectInfo.segInfo.segName; } printf(" 0x%08llX %-16s %6llu\n", sectInfo.sectAddr-firstSegVmAddr, sectInfo.sectName, sectInfo.sectSize); }); } } static void printDependents(const dyld3::MachOAnalyzer* ma) { printf(" -dependents:\n"); printf(" attributes load path\n"); ma->forEachDependentDylib(^(const char* loadPath, bool isWeak, bool isReExport, bool isUpward, uint32_t compatVersion, uint32_t curVersion, bool &stop) { const char* attribute = ""; if ( isWeak ) attribute = "weak_import"; else if ( isReExport ) attribute = "re-export"; else if ( isUpward ) attribute = "upward"; printf(" %-12s %s\n", attribute, loadPath); }); } static bool liveMachO(const dyld3::MachOAnalyzer* ma, const DyldSharedCache* dyldCache, size_t cacheLen) { if ( dyldCache == nullptr ) return false; const uint8_t* cacheStart = (uint8_t*)dyldCache; const uint8_t* cacheEnd = &cacheStart[cacheLen]; if ( (uint8_t*)ma < cacheStart) return false; if ( (uint8_t*)ma > cacheEnd) return false; // only return true for live images return ( dyld_image_header_containing_address(ma) != nullptr ); } static void printInitializers(const dyld3::MachOAnalyzer* ma, const DyldSharedCache* dyldCache, size_t cacheLen) { printf(" -inits:\n"); __block Diagnostics diag; const dyld3::MachOAnalyzer::VMAddrConverter vmAddrConverter = (DyldSharedCache::inDyldCache(dyldCache, ma) ? dyldCache->makeVMAddrConverter(true) : ma->makeVMAddrConverter(false)); ma->forEachInitializer(diag, vmAddrConverter, ^(uint32_t offset) { uint64_t targetLoadAddr = (uint64_t)ma+offset; const char* symbolName; uint64_t symbolLoadAddr; if ( ma->findClosestSymbol(targetLoadAddr, &symbolName, &symbolLoadAddr) ) { uint64_t delta = targetLoadAddr - symbolLoadAddr; if ( delta == 0 ) printf(" 0x%08X %s\n", offset, symbolName); else printf(" 0x%08X %s + 0x%llX\n", offset, symbolName, delta); } else printf(" 0x%08X\n", offset); }); if ( ma->hasPlusLoadMethod(diag) ) { // can't inspect ObjC of a live dylib if ( liveMachO(ma, dyldCache, cacheLen) ) { printf(" <<<cannot print objc data on live dylib>>>\n"); return; } const uint32_t pointerSize = ma->pointerSize(); uint64_t prefLoadAddress = ma->preferredLoadAddress(); // print all +load methods on classes in this image auto visitClass = ^(uint64_t classVMAddr, uint64_t classSuperclassVMAddr, uint64_t classDataVMAddr, const dyld3::MachOAnalyzer::ObjCClassInfo& objcClass, bool isMetaClass, bool& stop) { if (!isMetaClass) return; dyld3::MachOAnalyzer::PrintableStringResult classNameResult; const char* className = ma->getPrintableString(objcClass.nameVMAddr(pointerSize), classNameResult); if ( classNameResult == dyld3::MachOAnalyzer::PrintableStringResult::CanPrint ) { ma->forEachObjCMethod(objcClass.baseMethodsVMAddr(pointerSize), vmAddrConverter, 0, ^(uint64_t methodVMAddr, const dyld3::MachOAnalyzer::ObjCMethod& method, bool& stopMethod) { dyld3::MachOAnalyzer::PrintableStringResult methodNameResult; const char* methodName = ma->getPrintableString(method.nameVMAddr, methodNameResult); if ( methodNameResult == dyld3::MachOAnalyzer::PrintableStringResult::CanPrint ) { if ( strcmp(methodName, "load") == 0 ) printf(" 0x%08llX +[%s %s]\n", methodVMAddr-prefLoadAddress, className, methodName); } }); } }; ma->forEachObjCClass(diag, vmAddrConverter, visitClass); // print all +load methods on categories in this image auto visitCategory = ^(uint64_t categoryVMAddr, const dyld3::MachOAnalyzer::ObjCCategory& objcCategory, bool& stop) { dyld3::MachOAnalyzer::PrintableStringResult categoryNameResult; const char* categoryName = ma->getPrintableString(objcCategory.nameVMAddr, categoryNameResult); if ( categoryNameResult == dyld3::MachOAnalyzer::PrintableStringResult::CanPrint ) { ma->forEachObjCMethod(objcCategory.classMethodsVMAddr, vmAddrConverter, 0, ^(uint64_t methodVMAddr, const dyld3::MachOAnalyzer::ObjCMethod& method, bool& stopMethod) { dyld3::MachOAnalyzer::PrintableStringResult methodNameResult; const char* methodName = ma->getPrintableString(method.nameVMAddr, methodNameResult); if ( methodNameResult == dyld3::MachOAnalyzer::PrintableStringResult::CanPrint ) { if ( strcmp(methodName, "load") == 0 ) { // FIXME: if category is on class in another image, forEachObjCCategory returns null for objcCategory.clsVMAddr, need way to get name __block const char* catOnClassName = ""; auto visitOtherImageClass = ^(uint64_t classVMAddr, uint64_t classSuperclassVMAddr, uint64_t classDataVMAddr, const dyld3::MachOAnalyzer::ObjCClassInfo& objcClass, bool isMetaClass, bool& stopOtherClass) { if ( objcCategory.clsVMAddr == classVMAddr ) { dyld3::MachOAnalyzer::PrintableStringResult classNameResult; const char* className = ma->getPrintableString(objcClass.nameVMAddr(pointerSize), classNameResult); if ( classNameResult == dyld3::MachOAnalyzer::PrintableStringResult::CanPrint ) { catOnClassName = className; } } }; ma->forEachObjCClass(diag, vmAddrConverter, visitOtherImageClass); printf(" 0x%08llX +[%s(%s) %s]\n", methodVMAddr-prefLoadAddress, catOnClassName, categoryName, methodName); } } }); } }; ma->forEachObjCCategory(diag, vmAddrConverter, visitCategory); } } static const char* pointerFormat(uint16_t format) { switch (format) { case DYLD_CHAINED_PTR_ARM64E: return "authenticated arm64e, 8-byte stride, target vmadddr"; case DYLD_CHAINED_PTR_ARM64E_USERLAND: return "authenticated arm64e, 8-byte stride, target vmoffset"; case DYLD_CHAINED_PTR_ARM64E_FIRMWARE: return "authenticated arm64e, 4-byte stride, target vmadddr"; case DYLD_CHAINED_PTR_ARM64E_KERNEL: return "authenticated arm64e, 4-byte stride, target vmoffset"; case DYLD_CHAINED_PTR_64: return "generic 64-bit, 4-byte stride, target vmadddr"; case DYLD_CHAINED_PTR_64_OFFSET: return "generic 64-bit, 4-byte stride, target vmoffset "; case DYLD_CHAINED_PTR_32: return "generic 32-bit"; case DYLD_CHAINED_PTR_32_CACHE: return "32-bit for dyld cache"; case DYLD_CHAINED_PTR_64_KERNEL_CACHE: return "64-bit for kernel cache"; case DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE: return "64-bit for x86_64 kernel cache"; case DYLD_CHAINED_PTR_ARM64E_USERLAND24: return "authenticated arm64e, 8-byte stride, target vmoffset, 24-bit bind ordinals"; } return "unknown"; } static void printChains(const dyld3::MachOAnalyzer* ma) { Diagnostics diag; ma->withChainStarts(diag, 0, ^(const dyld_chained_starts_in_image* starts) { for (int i=0; i < starts->seg_count; ++i) { if ( starts->seg_info_offset[i] == 0 ) continue; const dyld_chained_starts_in_segment* seg = (dyld_chained_starts_in_segment*)((uint8_t*)starts + starts->seg_info_offset[i]); if ( seg->page_count == 0 ) continue; printf("seg[%d]:\n", i); printf(" page_size: 0x%04X\n", seg->page_size); printf(" pointer_format: %d (%s)\n", seg->pointer_format, pointerFormat(seg->pointer_format)); printf(" segment_offset: 0x%08llX\n", seg->segment_offset); printf(" max_pointer: 0x%08X\n", seg->max_valid_pointer); printf(" pages: %d\n", seg->page_count); for (int pageIndex=0; pageIndex < seg->page_count; ++pageIndex) { uint16_t offsetInPage = seg->page_start[pageIndex]; if ( offsetInPage == DYLD_CHAINED_PTR_START_NONE ) continue; if ( offsetInPage & DYLD_CHAINED_PTR_START_MULTI ) { // 32-bit chains which may need multiple starts per page uint32_t overflowIndex = offsetInPage & ~DYLD_CHAINED_PTR_START_MULTI; bool chainEnd = false; while (!chainEnd) { chainEnd = (seg->page_start[overflowIndex] & DYLD_CHAINED_PTR_START_LAST); offsetInPage = (seg->page_start[overflowIndex] & ~DYLD_CHAINED_PTR_START_LAST); printf(" start[% 2d]: 0x%04X\n", pageIndex, offsetInPage); ++overflowIndex; } } else { // one chain per page printf(" start[% 2d]: 0x%04X\n", pageIndex, offsetInPage); } } } }); } static void printImports(const dyld3::MachOAnalyzer* ma) { __block uint32_t bindOrdinal = 0; Diagnostics diag; ma->forEachChainedFixupTarget(diag, ^(int libOrdinal, const char* symbolName, uint64_t addend, bool weakImport, bool& stop) { const char* weakStr = ( weakImport ? "[weak-import]" : ""); if ( addend == 0 ) printf("0x%04X 0x%03X %s %s\n", bindOrdinal, libOrdinal, symbolName, weakStr); else printf("0x%04X 0x%03X %s+0x%llX %s\n", bindOrdinal, libOrdinal, symbolName, addend, weakStr); ++bindOrdinal; }); } static void printChainDetails(const dyld3::MachOAnalyzer* ma) { __block Diagnostics diag; ma->withChainStarts(diag, 0, ^(const dyld_chained_starts_in_image* starts) { ma->forEachFixupInAllChains(diag, starts, true, ^(ChainedFixupPointerOnDisk* fixupLoc, const dyld_chained_starts_in_segment* segInfo, bool& stop) { uint64_t vmOffset = (uint8_t*)fixupLoc - (uint8_t*)ma; switch (segInfo->pointer_format) { case DYLD_CHAINED_PTR_ARM64E: case DYLD_CHAINED_PTR_ARM64E_KERNEL: case DYLD_CHAINED_PTR_ARM64E_USERLAND: case DYLD_CHAINED_PTR_ARM64E_FIRMWARE: case DYLD_CHAINED_PTR_ARM64E_USERLAND24: if ( fixupLoc->arm64e.authRebase.auth ) { uint32_t bindOrdinal = (segInfo->pointer_format == DYLD_CHAINED_PTR_ARM64E_USERLAND24) ? fixupLoc->arm64e.authBind24.ordinal : fixupLoc->arm64e.authBind.ordinal; if ( fixupLoc->arm64e.authBind.bind ) { printf(" 0x%08llX: raw: 0x%016llX auth-bind: (next: %03d, key: %s, addrDiv: %d, diversity: 0x%04X, ordinal: %04X)\n", vmOffset, fixupLoc->raw64, fixupLoc->arm64e.authBind.next, fixupLoc->arm64e.keyName(), fixupLoc->arm64e.authBind.addrDiv, fixupLoc->arm64e.authBind.diversity, bindOrdinal); } else { printf(" 0x%08llX: raw: 0x%016llX auth-rebase: (next: %03d, key: %s, addrDiv: %d, diversity: 0x%04X, target: 0x%08X)\n", vmOffset, fixupLoc->raw64, fixupLoc->arm64e.authRebase.next, fixupLoc->arm64e.keyName(), fixupLoc->arm64e.authBind.addrDiv, fixupLoc->arm64e.authBind.diversity, fixupLoc->arm64e.authRebase.target); } } else { uint32_t bindOrdinal = (segInfo->pointer_format == DYLD_CHAINED_PTR_ARM64E_USERLAND24) ? fixupLoc->arm64e.bind24.ordinal : fixupLoc->arm64e.bind.ordinal; if ( fixupLoc->arm64e.rebase.bind ) { printf(" 0x%08llX: raw: 0x%016llX bind: (next: %03d, ordinal: %04X, addend: %d)\n", vmOffset, fixupLoc->raw64, fixupLoc->arm64e.bind.next, bindOrdinal, fixupLoc->arm64e.bind.addend); } else { printf(" 0x%08llX: raw: 0x%016llX rebase: (next: %03d, target: 0x%011llX, high8: 0x%02X)\n", vmOffset, fixupLoc->raw64, fixupLoc->arm64e.rebase.next, fixupLoc->arm64e.rebase.target, fixupLoc->arm64e.rebase.high8); } } break; case DYLD_CHAINED_PTR_64: case DYLD_CHAINED_PTR_64_OFFSET: if ( fixupLoc->generic64.rebase.bind ) { printf(" 0x%08llX: raw: 0x%016llX bind: (next: %03d, ordinal: %06X, addend: %d)\n", vmOffset, fixupLoc->raw64, fixupLoc->generic64.bind.next, fixupLoc->generic64.bind.ordinal, fixupLoc->generic64.bind.addend); } else { printf(" 0x%08llX: raw: 0x%016llX rebase: (next: %03d, target: 0x%011llX, high8: 0x%02X)\n", vmOffset, fixupLoc->raw64, fixupLoc->generic64.rebase.next, fixupLoc->generic64.rebase.target, fixupLoc->generic64.rebase.high8); } break; case DYLD_CHAINED_PTR_32: if ( fixupLoc->generic32.bind.bind ) { printf(" 0x%08llX: raw: 0x%08X bind: (next:%02d ordinal:%05X addend:%d)\n", vmOffset, fixupLoc->raw32, fixupLoc->generic32.bind.next, fixupLoc->generic32.bind.ordinal, fixupLoc->generic32.bind.addend); } else if ( fixupLoc->generic32.rebase.target > segInfo->max_valid_pointer ) { uint32_t bias = (0x04000000 + segInfo->max_valid_pointer)/2; uint32_t value = fixupLoc->generic32.rebase.target - bias; printf(" 0x%08llX: raw: 0x%08X nonptr: (next:%02d value: 0x%08X)\n", vmOffset, fixupLoc->raw32, fixupLoc->generic32.rebase.next, value); } else { printf(" 0x%08llX: raw: 0x%08X rebase: (next:%02d target: 0x%07X)\n", vmOffset, fixupLoc->raw32, fixupLoc->generic32.rebase.next, fixupLoc->generic32.rebase.target); } break; default: fprintf(stderr, "unknown pointer type %d\n", segInfo->pointer_format); break; } }); }); if ( diag.hasError() ) fprintf(stderr, "dyld_info: %s\n", diag.errorMessage()); } struct FixupInfo { std::string segName; std::string sectName; uint64_t address; PointerMetaData pmd; const char* type; uint64_t targetValue; const char* targetDylib; const char* targetSymbolName; uint64_t targetAddend; bool targetWeakImport; }; struct SymbolicFixupInfo { uint64_t address; const char* kind; std::string target; }; static const char* ordinalName(const dyld3::MachOAnalyzer* ma, int libraryOrdinal) { if ( libraryOrdinal > 0 ) { const char* path = ma->dependentDylibLoadPath(libraryOrdinal-1); if ( path == nullptr ) return "ordinal-too-large"; const char* leafName = path; if ( const char* lastSlash = strrchr(path, '/') ) leafName = lastSlash+1; return leafName; } else { switch ( libraryOrdinal) { case BIND_SPECIAL_DYLIB_SELF: return "this-image"; case BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: return "main-executable"; case BIND_SPECIAL_DYLIB_FLAT_LOOKUP: return "flat-namespace"; case BIND_SPECIAL_DYLIB_WEAK_LOOKUP: return "weak-coalesce"; } } return "unknown-ordinal"; } class SectionFinder { public: SectionFinder(const dyld3::MachOAnalyzer* ma); const char* segmentName(uint64_t vmOffset) const; const char* sectionName(uint64_t vmOffset) const; uint64_t baseAddress() const { return _baseAddress; } uint64_t currentSectionAddress() const { return _lastSection.sectAddr; } bool isNewSection(uint64_t vmOffset) const; private: void updateLastSection(uint64_t vmOffset) const; const dyld3::MachOAnalyzer* _ma; uint64_t _baseAddress; mutable dyld3::MachOFile::SectionInfo _lastSection; mutable char _lastSegName[20]; mutable char _lastSectName[20]; }; SectionFinder::SectionFinder(const dyld3::MachOAnalyzer* ma) : _ma(ma) { _baseAddress = ma->preferredLoadAddress(); _lastSection.sectAddr = 0; _lastSection.sectSize = 0; } bool SectionFinder::isNewSection(uint64_t vmOffset) const { uint64_t vmAddr = _baseAddress + vmOffset; return ( (vmAddr < _lastSection.sectAddr) || (vmAddr >= _lastSection.sectAddr+_lastSection.sectSize) ); } void SectionFinder::updateLastSection(uint64_t vmOffset) const { if ( isNewSection(vmOffset) ) { _lastSegName[0] = '\0'; _lastSectName[0] = '\0'; uint64_t vmAddr = _baseAddress + vmOffset; _ma->forEachSection(^(const dyld3::MachOFile::SectionInfo& sectInfo, bool malformedSectionRange, bool& sectStop) { if ( (sectInfo.sectAddr <= vmAddr) && (vmAddr < sectInfo.sectAddr+sectInfo.sectSize) ) { _lastSection = sectInfo; strcpy(_lastSegName, _lastSection.segInfo.segName); strcpy(_lastSectName, _lastSection.sectName); sectStop = true; } }); } } const char* SectionFinder::segmentName(uint64_t vmOffset) const { updateLastSection(vmOffset); return _lastSegName; } const char* SectionFinder::sectionName(uint64_t vmOffset) const { updateLastSection(vmOffset); return _lastSectName; } static inline std::string decimal(int64_t value) { char buff[64]; sprintf(buff, "%lld", value); return buff; } static std::string rebaseTargetString(const dyld3::MachOAnalyzer* ma, uint64_t vmAddr) { uint64_t targetLoadAddr = (uint64_t)ma+vmAddr; const char* targetSymbolName; uint64_t targetSymbolLoadAddr; if ( ma->findClosestSymbol(targetLoadAddr, &targetSymbolName, &targetSymbolLoadAddr) ) { uint64_t delta = targetLoadAddr - targetSymbolLoadAddr; if ( delta == 0 ) { return targetSymbolName; } else { if ( (delta == 1) && (ma->cputype == CPU_TYPE_ARM) ) return std::string(targetSymbolName) + std::string(" [thumb]"); else return std::string(targetSymbolName) + std::string("+") + decimal(delta); } } else { __block std::string result; ma->forEachSection(^(const dyld3::MachOAnalyzer::SectionInfo& sectInfo, bool malformedSectionRange, bool& stop) { if ( (sectInfo.sectAddr <= vmAddr) && (vmAddr < sectInfo.sectAddr+sectInfo.sectSize) ) { if ( (sectInfo.sectFlags & SECTION_TYPE) == S_CSTRING_LITERALS ) { const char* cstring = (char*)ma + (vmAddr-ma->preferredLoadAddress()); result = std::string("\"") + cstring + std::string("\""); } else { result = std::string(sectInfo.segInfo.segName) + "/" + sectInfo.sectName + "+" + decimal(vmAddr - sectInfo.sectAddr); } } }); return result; } } static void printFixups(const dyld3::MachOAnalyzer* ma, const char* path) { printf(" -fixups:\n"); // build targets table __block Diagnostics diag; __block std::vector<MachOAnalyzer::BindTargetInfo> bindTargets; __block std::vector<MachOAnalyzer::BindTargetInfo> overrideBindTargets; ma->forEachBindTarget(diag, false, ^(const MachOAnalyzer::BindTargetInfo& info, bool& stop) { bindTargets.push_back(info); if ( diag.hasError() ) stop = true; }, ^(const MachOAnalyzer::BindTargetInfo& info, bool& stop) { overrideBindTargets.push_back(info); if ( diag.hasError() ) stop = true; }); // walk fixups bool is64 = ma->is64(); SectionFinder namer(ma); __block std::vector<FixupInfo> fixups; const uint64_t prefLoadAddr = ma->preferredLoadAddress(); uint16_t fwPointerFormat; uint32_t fwStartsCount; const uint32_t* fwStarts; if ( ma->hasChainedFixups() ) { // walk all chains ma->withChainStarts(diag, ma->chainStartsOffset(), ^(const dyld_chained_starts_in_image* startsInfo) { ma->forEachFixupInAllChains(diag, startsInfo, false, ^(ChainedFixupPointerOnDisk* fixupLocation, const dyld_chained_starts_in_segment* segInfo, bool& stop) { FixupInfo fixup; uint32_t bindOrdinal; int64_t embeddedAddend; long fixupLocRuntimeOffset = (uint8_t*)fixupLocation - (uint8_t*)ma; fixup.segName = namer.segmentName(fixupLocRuntimeOffset); fixup.sectName = namer.sectionName(fixupLocRuntimeOffset); fixup.address = prefLoadAddr + fixupLocRuntimeOffset; fixup.pmd = PointerMetaData(fixupLocation, segInfo->pointer_format); if ( fixupLocation->isBind(segInfo->pointer_format, bindOrdinal, embeddedAddend) ) { fixup.targetWeakImport = bindTargets[bindOrdinal].weakImport; fixup.type = "bind"; fixup.targetSymbolName = bindTargets[bindOrdinal].symbolName; fixup.targetDylib = ordinalName(ma, bindTargets[bindOrdinal].libOrdinal); fixup.targetAddend = bindTargets[bindOrdinal].addend + embeddedAddend; if ( fixup.pmd.high8 ) fixup.targetAddend += ((uint64_t)fixup.pmd.high8 << 56); fixups.push_back(fixup); } else if ( fixupLocation->isRebase(segInfo->pointer_format, prefLoadAddr, fixup.targetValue) ) { fixup.targetWeakImport = false; fixup.type = "rebase"; fixup.targetSymbolName = nullptr; fixup.targetDylib = nullptr; fixup.targetAddend = 0; fixups.push_back(fixup); } }); }); } else if ( ma->hasOpcodeFixups() ) { // process all rebase opcodes ma->forEachRebaseLocation_Opcodes(diag, ^(uint64_t runtimeOffset, bool& stop) { uintptr_t fixupLoc = (uintptr_t)ma + (uintptr_t)runtimeOffset; uint64_t value = is64 ? *(uint64_t*)fixupLoc : *(uint32_t*)fixupLoc; FixupInfo fixup; fixup.segName = namer.segmentName(runtimeOffset); fixup.sectName = namer.sectionName(runtimeOffset); fixup.address = prefLoadAddr + runtimeOffset; fixup.targetValue = value; fixup.targetWeakImport = false; fixup.type = "rebase"; fixup.targetSymbolName = nullptr; fixup.targetDylib = nullptr; fixup.targetAddend = 0; fixups.push_back(fixup); }); if ( diag.hasError() ) return; // process all bind opcodes ma->forEachBindLocation_Opcodes(diag, ^(uint64_t runtimeOffset, unsigned targetIndex, bool& stop) { FixupInfo fixup; fixup.segName = namer.segmentName(runtimeOffset); fixup.sectName = namer.sectionName(runtimeOffset); fixup.address = prefLoadAddr + runtimeOffset; fixup.targetWeakImport = bindTargets[targetIndex].weakImport; fixup.type = "bind"; fixup.targetSymbolName = bindTargets[targetIndex].symbolName; fixup.targetDylib = ordinalName(ma, bindTargets[targetIndex].libOrdinal); fixup.targetAddend = 0; fixups.push_back(fixup); }, ^(uint64_t runtimeOffset, unsigned overrideBindTargetIndex, bool& stop) { FixupInfo fixup; fixup.segName = namer.segmentName(runtimeOffset); fixup.sectName = namer.sectionName(runtimeOffset); fixup.address = prefLoadAddr + runtimeOffset; fixup.targetWeakImport = overrideBindTargets[overrideBindTargetIndex].weakImport; fixup.type = "weak-bind"; fixup.targetSymbolName = overrideBindTargets[overrideBindTargetIndex].symbolName; fixup.targetDylib = ordinalName(ma, overrideBindTargets[overrideBindTargetIndex].libOrdinal); fixup.targetAddend = 0; fixups.push_back(fixup); }); } else if ( ma->hasFirmwareChainStarts(&fwPointerFormat, &fwStartsCount, &fwStarts) ) { // This is firmware which only has rebases, the chain starts info is in a section (not LINKEDIT) ma->forEachFixupInAllChains(diag, fwPointerFormat, fwStartsCount, fwStarts, ^(ChainedFixupPointerOnDisk* fixupLoc, bool& stop) { uint64_t fixupOffset = (uint8_t*)fixupLoc - (uint8_t*)ma; PointerMetaData pmd(fixupLoc, fwPointerFormat); uint64_t targetOffset; fixupLoc->isRebase(fwPointerFormat, prefLoadAddr, targetOffset); FixupInfo fixup; fixup.segName = namer.segmentName(fixupOffset); fixup.sectName = namer.sectionName(fixupOffset); fixup.address = prefLoadAddr + fixupOffset; fixup.targetValue = prefLoadAddr + targetOffset; fixup.targetWeakImport = false; fixup.type = "rebase"; fixup.targetSymbolName = nullptr; fixup.targetDylib = nullptr; fixup.targetAddend = 0; fixups.push_back(fixup); }); } else { // process internal relocations ma->forEachRebaseLocation_Relocations(diag, ^(uint64_t runtimeOffset, bool& stop) { uintptr_t fixupLoc = (uintptr_t)ma + (uintptr_t)runtimeOffset; uint64_t value = is64 ? *(uint64_t*)fixupLoc : *(uint32_t*)fixupLoc; FixupInfo fixup; fixup.segName = namer.segmentName(runtimeOffset); fixup.sectName = namer.sectionName(runtimeOffset); fixup.address = prefLoadAddr + runtimeOffset; fixup.targetValue = value; fixup.targetWeakImport = false; fixup.type = "rebase"; fixup.targetSymbolName = nullptr; fixup.targetDylib = nullptr; fixup.targetAddend = 0; fixups.push_back(fixup); }); if ( diag.hasError() ) return; // process external relocations ma->forEachBindLocation_Relocations(diag, ^(uint64_t runtimeOffset, unsigned targetIndex, bool& stop) { FixupInfo fixup; fixup.segName = namer.segmentName(runtimeOffset); fixup.sectName = namer.sectionName(runtimeOffset); fixup.address = prefLoadAddr + runtimeOffset; fixup.targetWeakImport = bindTargets[targetIndex].weakImport; fixup.type = "bind"; fixup.targetSymbolName = bindTargets[targetIndex].symbolName; fixup.targetDylib = ordinalName(ma, bindTargets[targetIndex].libOrdinal); fixup.targetAddend = 0; fixups.push_back(fixup); }); } // sort fixups by location address std::sort(fixups.begin(), fixups.end(), [](const FixupInfo& l, const FixupInfo& r) { if ( &l == &r ) return false; if ( l.address == r.address ) return (l.targetSymbolName == nullptr); return ( l.address < r.address ); }); printf(" segment section address type target\n"); for (const FixupInfo& fixup : fixups) { char authInfo[128]; authInfo[0] = '\0'; if ( fixup.pmd.authenticated ) { sprintf(authInfo, " (div=0x%04X ad=%d key=%s)", fixup.pmd.diversity, fixup.pmd.usesAddrDiversity, ChainedFixupPointerOnDisk::Arm64e::keyName(fixup.pmd.key)); } if ( fixup.targetSymbolName == nullptr ) printf(" %-12s %-16s 0x%08llX %16s 0x%08llX%s\n", fixup.segName.c_str(), fixup.sectName.c_str(), fixup.address, fixup.type, fixup.targetValue, authInfo); else if ( fixup.targetAddend != 0 ) printf(" %-12s %-16s 0x%08llX %16s %s/%s + 0x%llX%s\n", fixup.segName.c_str(), fixup.sectName.c_str(), fixup.address, fixup.type, fixup.targetDylib, fixup.targetSymbolName, fixup.targetAddend, authInfo); else if ( fixup.targetWeakImport ) printf(" %-12s %-16s 0x%08llX %16s %s/%s [weak-import]%s\n", fixup.segName.c_str(), fixup.sectName.c_str(), fixup.address, fixup.type, fixup.targetDylib, fixup.targetSymbolName, authInfo); else printf(" %-12s %-16s 0x%08llX %16s %s/%s%s\n", fixup.segName.c_str(), fixup.sectName.c_str(), fixup.address, fixup.type, fixup.targetDylib, fixup.targetSymbolName, authInfo); } } static void printSymbolicFixups(const dyld3::MachOAnalyzer* ma, const char* path) { printf(" -symbolic_fixups:\n"); __block std::vector<SymbolicFixupInfo> fixups; // build targets table __block Diagnostics diag; __block std::vector<MachOAnalyzer::BindTargetInfo> bindTargets; __block std::vector<MachOAnalyzer::BindTargetInfo> overrideBindTargets; ma->forEachBindTarget(diag, false, ^(const MachOAnalyzer::BindTargetInfo& info, bool& stop) { bindTargets.push_back(info); if ( diag.hasError() ) stop = true; }, ^(const MachOAnalyzer::BindTargetInfo& info, bool& stop) { overrideBindTargets.push_back(info); if ( diag.hasError() ) stop = true; }); // walk fixups SectionFinder namer(ma); const uint64_t prefLoadAddr = ma->preferredLoadAddress(); if ( ma->hasChainedFixups() ) { // walk all chains ma->withChainStarts(diag, ma->chainStartsOffset(), ^(const dyld_chained_starts_in_image* startsInfo) { ma->forEachFixupInAllChains(diag, startsInfo, false, ^(ChainedFixupPointerOnDisk* fixupLocation, const dyld_chained_starts_in_segment* segInfo, bool& stop) { SymbolicFixupInfo fixup; uint32_t bindOrdinal; int64_t addend; uint64_t targetAddress; long fixupLocRuntimeOffset = (uint8_t*)fixupLocation - (uint8_t*)ma; fixup.address = prefLoadAddr + fixupLocRuntimeOffset; if ( fixupLocation->isBind(segInfo->pointer_format, bindOrdinal, addend) ) { const MachOAnalyzer::BindTargetInfo& bindTarget = bindTargets[bindOrdinal]; fixup.kind = "bind pointer"; fixup.target = std::string(ordinalName(ma, bindTarget.libOrdinal)) + "/" + bindTarget.symbolName; if ( bindTarget.addend != 0 ) addend += bindTarget.addend; PointerMetaData pmd(fixupLocation, segInfo->pointer_format); if ( pmd.high8 ) addend |= ((uint64_t)pmd.high8 << 56); if ( addend != 0 ) fixup.target += std::string("+") + decimal(addend); if ( pmd.authenticated ) { char authInfo[256]; sprintf(authInfo, " (div=0x%04X ad=%d key=%s)", pmd.diversity, pmd.usesAddrDiversity, ChainedFixupPointerOnDisk::Arm64e::keyName(pmd.key)); fixup.target += authInfo; } fixups.push_back(fixup); } else if ( fixupLocation->isRebase(segInfo->pointer_format, prefLoadAddr, targetAddress) ) { PointerMetaData pmd(fixupLocation, segInfo->pointer_format); fixup.kind = "rebase pointer"; fixup.target = rebaseTargetString(ma, targetAddress); if ( pmd.authenticated ) { char authInfo[256]; sprintf(authInfo, " (div=0x%04X ad=%d key=%s)", pmd.diversity, pmd.usesAddrDiversity, ChainedFixupPointerOnDisk::Arm64e::keyName(pmd.key)); fixup.target += authInfo; } fixups.push_back(fixup); } }); }); } else if ( ma->hasOpcodeFixups() ) { // process all rebase opcodes bool is64 = ma->is64(); ma->forEachRebaseLocation_Opcodes(diag, ^(uint64_t runtimeOffset, bool& stop) { uintptr_t fixupLoc = (uintptr_t)ma + (uintptr_t)runtimeOffset; uint64_t value = is64 ? *(uint64_t*)fixupLoc : *(uint32_t*)fixupLoc; SymbolicFixupInfo fixup; fixup.address = prefLoadAddr + runtimeOffset; fixup.kind = "rebase pointer"; fixup.target = rebaseTargetString(ma, value); fixups.push_back(fixup); }); if ( diag.hasError() ) return; // process all bind opcodes ma->forEachBindLocation_Opcodes(diag, ^(uint64_t runtimeOffset, unsigned targetIndex, bool& stop) { const MachOAnalyzer::BindTargetInfo& bindTarget = bindTargets[targetIndex]; uint64_t addend = 0; SymbolicFixupInfo fixup; fixup.address = prefLoadAddr + runtimeOffset; fixup.kind = "bind pointer"; fixup.target = std::string(ordinalName(ma, bindTarget.libOrdinal)) + "/" + bindTarget.symbolName; if ( bindTarget.addend != 0 ) addend += bindTarget.addend; if ( addend != 0 ) fixup.target += std::string("+") + decimal(addend); fixups.push_back(fixup); }, ^(uint64_t runtimeOffset, unsigned overrideBindTargetIndex, bool& stop) { const MachOAnalyzer::BindTargetInfo& bindTarget = overrideBindTargets[overrideBindTargetIndex]; uint64_t addend = 0; SymbolicFixupInfo fixup; fixup.address = prefLoadAddr + runtimeOffset; fixup.kind = "bind pointer"; fixup.target = std::string(ordinalName(ma, bindTarget.libOrdinal)) + "/" + bindTarget.symbolName; if ( bindTarget.addend != 0 ) addend += bindTarget.addend; if ( addend != 0 ) fixup.target += std::string("+") + decimal(addend); fixups.push_back(fixup); }); } else { // process internal relocations bool is64 = ma->is64(); ma->forEachRebaseLocation_Relocations(diag, ^(uint64_t runtimeOffset, bool& stop) { uintptr_t fixupLoc = (uintptr_t)ma + (uintptr_t)runtimeOffset; uint64_t value = is64 ? *(uint64_t*)fixupLoc : *(uint32_t*)fixupLoc; SymbolicFixupInfo fixup; fixup.address = prefLoadAddr + runtimeOffset; fixup.kind = "rebase pointer"; fixup.target = rebaseTargetString(ma, value); fixups.push_back(fixup); }); if ( diag.hasError() ) return; // process external relocations ma->forEachBindLocation_Relocations(diag, ^(uint64_t runtimeOffset, unsigned targetIndex, bool& stop) { const MachOAnalyzer::BindTargetInfo& bindTarget = bindTargets[targetIndex]; uint64_t addend = 0; SymbolicFixupInfo fixup; fixup.address = prefLoadAddr + runtimeOffset; fixup.kind = "bind pointer"; fixup.target = std::string(ordinalName(ma, bindTarget.libOrdinal)) + "/" + bindTarget.symbolName; if ( bindTarget.addend != 0 ) addend += bindTarget.addend; if ( addend != 0 ) fixup.target += std::string("+") + decimal(addend); fixups.push_back(fixup); }); } // sort fixups by location std::sort(fixups.begin(), fixups.end(), [](const SymbolicFixupInfo& l, const SymbolicFixupInfo& r) { if ( &l == &r ) return false; return ( l.address < r.address ); }); SectionFinder sectionTracker(ma); uint64_t lastSymbolVmOffset = 0; for (const SymbolicFixupInfo& fixup : fixups) { uint64_t vmAddr = fixup.address; uint64_t vmOffset = vmAddr - prefLoadAddr; if ( sectionTracker.isNewSection(vmOffset) ) { printf(" 0x%08llX %-12s %-16s \n", vmAddr, sectionTracker.segmentName(vmOffset), sectionTracker.sectionName(vmOffset)); } const char* symbolName; uint64_t symbolLoadAddr = 0; if ( ma->findClosestSymbol((uint64_t)ma+vmOffset, &symbolName, &symbolLoadAddr) ) { uint64_t symbolVmOffset = symbolLoadAddr - (uint64_t)ma; if ( symbolVmOffset != lastSymbolVmOffset ) { printf(" %s:\n", symbolName); lastSymbolVmOffset = symbolVmOffset; } } printf(" +0x%04llX %16s %s\n", vmOffset - lastSymbolVmOffset, fixup.kind, fixup.target.c_str()); } } static void printExports(const dyld3::MachOAnalyzer* ma) { printf(" -exports:\n"); printf(" offset symbol\n"); Diagnostics diag; ma->forEachExportedSymbol(diag, ^(const char* symbolName, uint64_t imageOffset, uint64_t flags, uint64_t other, const char* importName, bool& stop) { //printf("0x%08llX %s\n", imageOffset, symbolName); const bool reExport = (flags & EXPORT_SYMBOL_FLAGS_REEXPORT); const bool weakDef = (flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION); const bool resolver = (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER); const bool threadLocal = ((flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL); const bool abs = ((flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE); if ( reExport ) printf(" [re-export] "); else printf(" 0x%08llX ", imageOffset); printf("%s", symbolName); if ( weakDef || threadLocal || resolver || abs ) { bool needComma = false; printf(" ["); if ( weakDef ) { printf("weak_def"); needComma = true; } if ( threadLocal ) { if ( needComma ) printf(", "); printf("per-thread"); needComma = true; } if ( abs ) { if ( needComma ) printf(", "); printf("absolute"); needComma = true; } if ( resolver ) { if ( needComma ) printf(", "); printf("resolver=0x%08llX", other); needComma = true; } printf("]"); } if ( reExport ) { if ( importName[0] == '\0' ) printf(" (from %s)", ordinalName(ma, (int)other)); else printf(" (%s from %s)", importName, ordinalName(ma, (int)other)); } printf("\n"); }); } static void printObjC(const dyld3::MachOAnalyzer* ma, const DyldSharedCache* dyldCache, size_t cacheLen) { Diagnostics diag; const uint32_t pointerSize = ma->pointerSize(); const dyld3::MachOAnalyzer::VMAddrConverter vmAddrConverter = (DyldSharedCache::inDyldCache(dyldCache, ma) ? dyldCache->makeVMAddrConverter(true) : ma->makeVMAddrConverter(false)); auto printMethod = ^(uint64_t methodVMAddr, const dyld3::MachOAnalyzer::ObjCMethod& method, bool& stop) { const char* type = "method"; dyld3::MachOAnalyzer::PrintableStringResult methodNameResult; const char* methodName = ma->getPrintableString(method.nameVMAddr, methodNameResult); switch (methodNameResult) { case dyld3::MachOAnalyzer::PrintableStringResult::CanPrint: // The string is already valid break; case dyld3::MachOAnalyzer::PrintableStringResult::FairPlayEncrypted: methodName = "### fairplay encrypted"; break; case dyld3::MachOAnalyzer::PrintableStringResult::ProtectedSection: methodName = "### protected section"; break; case dyld3::MachOAnalyzer::PrintableStringResult::UnknownSection: methodName = "### unknown section"; break; } printf(" %10s 0x%08llX %s\n", type, methodVMAddr, methodName); }; printf(" -objc:\n"); // can't inspect ObjC of a live dylib if ( liveMachO(ma, dyldCache, cacheLen) ) { printf(" <<<cannot print objc data on live dylib>>>\n"); return; } printf(" type vmaddr data-vmaddr name\n"); auto printClass = ^(uint64_t classVMAddr, uint64_t classSuperclassVMAddr, uint64_t classDataVMAddr, const dyld3::MachOAnalyzer::ObjCClassInfo& objcClass, bool isMetaClass, bool& stop) { const char* type = "class"; if (isMetaClass) type = "meta-class"; dyld3::MachOAnalyzer::PrintableStringResult classNameResult; const char* className = ma->getPrintableString(objcClass.nameVMAddr(pointerSize), classNameResult); switch (classNameResult) { case dyld3::MachOAnalyzer::PrintableStringResult::CanPrint: // The string is already valid break; case dyld3::MachOAnalyzer::PrintableStringResult::FairPlayEncrypted: className = "### fairplay encrypted"; break; case dyld3::MachOAnalyzer::PrintableStringResult::ProtectedSection: className = "### protected section"; break; case dyld3::MachOAnalyzer::PrintableStringResult::UnknownSection: className = "### unknown section"; break; } printf(" %10s 0x%08llX 0x%08llX %s\n", type, classVMAddr, objcClass.dataVMAddr, className); // Now print the methods on this class ma->forEachObjCMethod(objcClass.baseMethodsVMAddr(pointerSize), vmAddrConverter, 0, printMethod); }; auto printCategory = ^(uint64_t categoryVMAddr, const dyld3::MachOAnalyzer::ObjCCategory& objcCategory, bool& stop) { const char* type = "category"; dyld3::MachOAnalyzer::PrintableStringResult categoryNameResult; const char* categoryName = ma->getPrintableString(objcCategory.nameVMAddr, categoryNameResult); switch (categoryNameResult) { case dyld3::MachOAnalyzer::PrintableStringResult::CanPrint: // The string is already valid break; case dyld3::MachOAnalyzer::PrintableStringResult::FairPlayEncrypted: categoryName = "### fairplay encrypted"; break; case dyld3::MachOAnalyzer::PrintableStringResult::ProtectedSection: categoryName = "### protected section"; break; case dyld3::MachOAnalyzer::PrintableStringResult::UnknownSection: categoryName = "### unknown section"; break; } printf(" %10s 0x%08llX %s\n", type, categoryVMAddr, categoryName); // Now print the methods on this category ma->forEachObjCMethod(objcCategory.instanceMethodsVMAddr, vmAddrConverter, 0, printMethod); ma->forEachObjCMethod(objcCategory.classMethodsVMAddr, vmAddrConverter, 0, printMethod); }; auto printProtocol = ^(uint64_t protocolVMAddr, const dyld3::MachOAnalyzer::ObjCProtocol& objCProtocol, bool& stop) { const char* type = "protocol"; dyld3::MachOAnalyzer::PrintableStringResult protocolNameResult; const char* protocolName = ma->getPrintableString(objCProtocol.nameVMAddr, protocolNameResult); switch (protocolNameResult) { case dyld3::MachOAnalyzer::PrintableStringResult::CanPrint: // The string is already valid break; case dyld3::MachOAnalyzer::PrintableStringResult::FairPlayEncrypted: protocolName = "### fairplay encrypted"; break; case dyld3::MachOAnalyzer::PrintableStringResult::ProtectedSection: protocolName = "### protected section"; break; case dyld3::MachOAnalyzer::PrintableStringResult::UnknownSection: protocolName = "### unknown section"; break; } printf(" %10s 0x%08llX %s\n", type, protocolVMAddr, protocolName); // Now print the methods on this protocol ma->forEachObjCMethod(objCProtocol.instanceMethodsVMAddr, vmAddrConverter, 0, printMethod); ma->forEachObjCMethod(objCProtocol.classMethodsVMAddr, vmAddrConverter, 0, printMethod); ma->forEachObjCMethod(objCProtocol.optionalInstanceMethodsVMAddr, vmAddrConverter, 0, printMethod); ma->forEachObjCMethod(objCProtocol.optionalClassMethodsVMAddr, vmAddrConverter, 0, printMethod); }; ma->forEachObjCClass(diag, vmAddrConverter, printClass); ma->forEachObjCCategory(diag, vmAddrConverter, printCategory); ma->forEachObjCProtocol(diag, vmAddrConverter, printProtocol); } static void printSwiftProtocolConformances(const dyld3::MachOAnalyzer* ma, const DyldSharedCache* dyldCache, size_t cacheLen) { Diagnostics diag; const dyld3::MachOAnalyzer::VMAddrConverter vmAddrConverter = (DyldSharedCache::inDyldCache(dyldCache, ma) ? dyldCache->makeVMAddrConverter(true) : ma->makeVMAddrConverter(false)); __block std::vector<std::string> chainedFixupTargets; ma->forEachChainedFixupTarget(diag, ^(int libOrdinal, const char *symbolName, uint64_t addend, bool weakImport, bool &stop) { chainedFixupTargets.push_back(symbolName); }); printf(" -swift-proto:\n"); printf(" address protocol-target type-descriptor-target\n"); uint64_t loadAddress = ma->preferredLoadAddress(); auto printProtocolConformance = ^(uint64_t protocolConformanceRuntimeOffset, const dyld3::MachOAnalyzer::SwiftProtocolConformance& protocolConformance, bool& stopProtocolConformance) { uint64_t protocolConformanceVMAddr = loadAddress + protocolConformanceRuntimeOffset; uint64_t protocolVMAddr = loadAddress + protocolConformance.protocolRuntimeOffset; uint64_t typeDescriptorVMAddr = loadAddress + protocolConformance.typeConformanceRuntimeOffset; const char* protocolConformanceFixup = ""; const char* protocolFixup = ""; const char* typeDescriptorFixup = ""; { ChainedFixupPointerOnDisk fixup; fixup.raw64 = protocolVMAddr; uint32_t bindOrdinal = 0; int64_t addend = 0; if ( fixup.isBind(DYLD_CHAINED_PTR_ARM64E_USERLAND, bindOrdinal, addend) ) { protocolFixup = chainedFixupTargets[bindOrdinal].c_str(); } } printf(" 0x%016llX(%s) 0x%016llX(%s) 0x%016llX(%s)\n", protocolConformanceVMAddr, protocolConformanceFixup, protocolVMAddr, protocolFixup, typeDescriptorVMAddr, typeDescriptorFixup); //stopProtocolConformance = true; }; ma->forEachSwiftProtocolConformance(diag, vmAddrConverter, false, printProtocolConformance); } static void usage() { fprintf(stderr, "Usage: dyld_info [-arch <arch>]* <options>* <mach-o file>+ | -all_dir <dir> \n" "\t-platform print platform (default if no options specified)\n" "\t-segments print segments (default if no options specified)\n" "\t-dependents print dependent dylibs (default if no options specified)\n" "\t-inits print initializers dylibs\n" "\t-fixups print locations dyld will rebase/bind\n" "\t-exports print addresses of all symbols this file exports\n" "\t-imports print all symbols needed from other dylibs\n" "\t-fixup_chains print info about chain format and starts\n" "\t-fixup_chain_details print detailed info about every fixup in chain\n" "\t-symbolic_fixups print ranges of each atom of DATA with symbol name and fixups\n" "\t-swift_protocols print swift protocols\n" "\t-objc print objc classes, categories, etc\n" "\t-validate_only only prints an malformedness about file(s)\n" ); } static bool inStringVector(const std::vector<const char*>& vect, const char* target) { for (const char* str : vect) { if ( strcmp(str, target) == 0 ) return true; } return false; } struct PrintOptions { bool platform = false; bool segments = false; bool dependents = false; bool initializers = false; bool exports = false; bool imports = false; bool fixups = false; bool fixupChains = false; bool fixupChainDetails = false; bool symbolicFixups = false; bool objc = false; bool swiftProtocols = false; }; int main(int argc, const char* argv[]) { if ( argc == 1 ) { usage(); return 0; } bool validateOnly = false; PrintOptions printOptions; __block std::vector<std::string> files; std::vector<const char*> cmdLineArchs; for (int i=1; i < argc; ++i) { const char* arg = argv[i]; if ( strcmp(arg, "-platform") == 0 ) { printOptions.platform = true; } else if ( strcmp(arg, "-segments") == 0 ) { printOptions.segments = true; } else if ( strcmp(arg, "-dependents") == 0 ) { printOptions.dependents = true; } else if ( strcmp(arg, "-inits") == 0 ) { printOptions.initializers = true; } else if ( strcmp(arg, "-fixups") == 0 ) { printOptions.fixups = true; } else if ( strcmp(arg, "-fixup_chains") == 0 ) { printOptions.fixupChains = true; } else if ( strcmp(arg, "-fixup_chain_details") == 0 ) { printOptions.fixupChainDetails = true; } else if ( strcmp(arg, "-symbolic_fixups") == 0 ) { printOptions.symbolicFixups = true; } else if ( strcmp(arg, "-exports") == 0 ) { printOptions.exports = true; } else if ( strcmp(arg, "-imports") == 0 ) { printOptions.imports = true; } else if ( strcmp(arg, "-objc") == 0 ) { printOptions.objc = true; } else if ( strcmp(arg, "-swift_protocols") == 0 ) { printOptions.swiftProtocols = true; } else if ( strcmp(arg, "-validate_only") == 0 ) { validateOnly = true; } else if ( strcmp(arg, "-arch") == 0 ) { if ( ++i < argc ) { cmdLineArchs.push_back(argv[i]); } else { fprintf(stderr, "-arch missing architecture name"); return 1; } } else if ( strcmp(arg, "-all_dir") == 0 ) { if ( ++i < argc ) { const char* searchDir = argv[i]; iterateDirectoryTree("", searchDir, ^(const std::string& dirPath) { return false; }, ^(const std::string& path, const struct stat& statBuf) { if ( statBuf.st_size > 4096 ) files.push_back(path); }, true /* process files */, true /* recurse */); } else { fprintf(stderr, "-all_dir directory"); return 1; } } else if ( arg[0] == '-' ) { fprintf(stderr, "dyld_info: unknown option: %s\n", arg); return 1; } else { files.push_back(arg); } } // check some files specified if ( files.size() == 0 ) { usage(); return 0; } // if no options specified, use default set PrintOptions noOptions; if ( memcmp(&printOptions, &noOptions, sizeof(PrintOptions)) == 0 ) { printOptions.platform = true; printOptions.segments = true; printOptions.dependents = true; } size_t cacheLen; __block const DyldSharedCache* dyldCache = (DyldSharedCache*)_dyld_get_shared_cache_range(&cacheLen); __block const char* currentArch = dyldCache->archName(); for (const std::string& pathstr : files) { const char* path = pathstr.c_str(); //fprintf(stderr, "doing: %s\n", path); Diagnostics diag; bool fromSharedCache = false; dyld3::closure::FileSystemPhysical fileSystem; dyld3::closure::LoadedFileInfo info; __block std::vector<const char*> archesForFile; char realerPath[MAXPATHLEN]; __block bool printedError = false; if (!fileSystem.loadFile(path, info, realerPath, ^(const char* format, ...) { fprintf(stderr, "dyld_info: '%s' ", path); va_list list; va_start(list, format); vfprintf(stderr, format, list); va_end(list); fprintf(stderr, "\n"); printedError = true; })) { if ( printedError ) continue; if ( strncmp(path, "/System/DriverKit/", 18) == 0 ) { dyld_for_each_installed_shared_cache(^(dyld_shared_cache_t cache) { __block bool mainFile = true; dyld_shared_cache_for_each_file(cache, ^(const char *file_path) { // skip subcaches files if ( !mainFile ) return; mainFile = false; if ( strncmp(file_path, "/System/DriverKit/", 18) != 0 ) return; // skip caches for uneeded architectures const char* found = strstr(file_path, currentArch); if ( found == nullptr || strlen(found) != strlen(currentArch) ) return; std::vector<const DyldSharedCache*> dyldCaches; dyldCaches = DyldSharedCache::mapCacheFiles(file_path); if ( dyldCaches.empty() ) return; dyldCache = dyldCaches.front(); }); }); } // see if path is in current dyld shared cache info.fileContent = nullptr; if ( dyldCache != nullptr ) { uint32_t imageIndex; if ( dyldCache->hasImagePath(path, imageIndex) ) { uint64_t mTime; uint64_t inode; const mach_header* mh = dyldCache->getIndexedImageEntry(imageIndex, mTime, inode); info.fileContent = mh; info.path = path; fromSharedCache = true; archesForFile.push_back(currentArch); } } if ( !fromSharedCache ) { fprintf(stderr, "dyld_info: '%s' file not found\n", path); continue; } } __block dyld3::Platform platform = dyld3::Platform::unknown; if ( dyld3::FatFile::isFatFile(info.fileContent) ) { const dyld3::FatFile* ff = (dyld3::FatFile*)info.fileContent; ff->forEachSlice(diag, info.fileContentLen, ^(uint32_t sliceCpuType, uint32_t sliceCpuSubType, const void* sliceStart, uint64_t sliceSize, bool& stop) { const char* sliceArchName = dyld3::MachOFile::archName(sliceCpuType, sliceCpuSubType); if ( cmdLineArchs.empty() || inStringVector(cmdLineArchs, sliceArchName) ) { archesForFile.push_back(sliceArchName); const dyld3::MachOFile* mf = (dyld3::MachOFile*)sliceStart; mf->forEachSupportedPlatform(^(dyld3::Platform plat, uint32_t minOS, uint32_t sdk) { if ( platform == dyld3::Platform::unknown) platform = plat; }); } }); } else if ( !fromSharedCache ) { const dyld3::MachOFile* mo = (dyld3::MachOFile*)info.fileContent; if ( mo->isMachO(diag, info.sliceLen) ) { archesForFile.push_back(mo->archName()); mo->forEachSupportedPlatform(^(dyld3::Platform plat, uint32_t minOS, uint32_t sdk) { if ( platform == dyld3::Platform::unknown) platform = plat; }); } else { if ( !diag.errorMessageContains("MH_MAGIC") || !validateOnly ) fprintf(stderr, "dyld_info: '%s' %s\n", path, diag.errorMessage()); continue; } } if ( archesForFile.empty() ) { fprintf(stderr, "dyld_info: '%s' does not contain specified arch(s)\n", path); continue; } char loadedPath[MAXPATHLEN]; for (const char* sliceArch : archesForFile) { if ( !fromSharedCache ) info = dyld3::MachOAnalyzer::load(diag, fileSystem, path, dyld3::GradedArchs::forName(sliceArch), platform, loadedPath); if ( diag.hasError() ) { fprintf(stderr, "dyld_info: '%s' %s\n", path, diag.errorMessage()); continue; } if ( !validateOnly ) { const dyld3::MachOAnalyzer* ma = (dyld3::MachOAnalyzer*)info.fileContent; printf("%s [%s]:\n", path, sliceArch); if ( printOptions.platform ) printPlatforms(ma); if ( printOptions.segments ) printSegments(ma, dyldCache); if ( printOptions.dependents ) printDependents(ma); if ( printOptions.initializers ) printInitializers(ma, dyldCache, cacheLen); if ( printOptions.exports ) printExports(ma); if ( printOptions.imports ) printImports(ma); if ( printOptions.fixups ) printFixups(ma, path); if ( printOptions.fixupChains ) printChains(ma); if ( printOptions.fixupChainDetails ) printChainDetails(ma); if ( printOptions.symbolicFixups ) printSymbolicFixups(ma, path); if ( printOptions.objc ) printObjC(ma, dyldCache, cacheLen); if ( printOptions.swiftProtocols ) printSwiftProtocolConformances(ma, dyldCache, cacheLen); } if ( !fromSharedCache ) fileSystem.unloadFile(info); } } return 0; } |