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 | /* * Copyright (c) 2019-2020 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <assert.h> #include <strings.h> #include <TargetConditionals.h> #if !TARGET_OS_EXCLAVEKIT #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #endif #include "Defines.h" #include "Loader.h" #include "JustInTimeLoader.h" #include "MachOAnalyzer.h" #include "DyldProcessConfig.h" #include "DyldRuntimeState.h" // mach_o #include "Header.h" #include "Version32.h" using dyld3::MachOAnalyzer; using dyld3::MachOFile; using dyld3::FatFile; using mach_o::Header; using mach_o::Version32; namespace dyld4 { //////////////////////// "virtual" functions ///////////////////////////////// const dyld3::MachOFile* JustInTimeLoader::mf(const RuntimeState&) const { #if SUPPORT_VM_LAYOUT return this->mappedAddress; #else return this->mappedAddress.operator->(); #endif // SUPPORT_VM_LAYOUT } #if SUPPORT_VM_LAYOUT const MachOLoaded* JustInTimeLoader::loadAddress(const RuntimeState&) const { return mappedAddress; } #endif // SUPPORT_VM_LAYOUT const char* JustInTimeLoader::path(const RuntimeState& state) const { return this->pathOffset ? ((char*)this + this->pathOffset) : nullptr; } const char* JustInTimeLoader::installName(const RuntimeState& state) const { mach_o::Header* mh = (mach_o::Header*)this->mf(state); if ( mh->isDylib() ) return mh->installName(); return nullptr; } #if BUILDING_DYLD || BUILDING_CLOSURE_UTIL || BUILDING_UNIT_TESTS bool JustInTimeLoader::contains(RuntimeState& state, const void* addr, const void** segAddr, uint64_t* segSize, uint8_t* segPerms) const { if ( addr < this->mappedAddress ) return false; if ( pd ) { if ( pd->contains(addr) ) { // FIXME: We might want a path to find the __TEXT segment, to avoid a // contradiction between the load command in the JITDylib mach // header and the values returned here. // We might also want to punt down to a pseudo-dylib // callback. In some cases it could provide a usable answer. segAddr = 0; segSize = 0; segPerms = 0; return true; } } __block bool result = false; const MachOAnalyzer* ma = (const MachOAnalyzer*)this->mappedAddress; uint64_t vmTextAddr = ma->preferredLoadAddress(); uint64_t slide = (uintptr_t)ma - vmTextAddr; uint64_t targetAddr = (uint64_t)addr; ma->forEachSegment(^(const MachOAnalyzer::SegmentInfo& info, bool& stop) { if ( ((info.vmAddr + slide) <= targetAddr) && (targetAddr < (info.vmAddr + slide + info.vmSize)) ) { *segAddr = (void*)(info.vmAddr + slide); *segSize = info.vmSize; *segPerms = info.protections; result = true; stop = true; } }); return result; } #endif // BUILDING_DYLD || BUILDING_CLOSURE_UTIL || BUILDING_UNIT_TESTS bool JustInTimeLoader::matchesPath(const RuntimeState& state, const char* path) const { if ( strcmp(path, this->path(state)) == 0 ) return true; if ( this->altInstallName ) { if ( strcmp(path, this->mappedAddress->installName()) == 0 ) return true; } if ( pd ) { if ( auto *canonicalPath = pd->loadableAtPath(path) ) { // Dispose of canonicalPath if it is different from path // (loadableAtPath is allowed to return its argument, which should not be freed). if ( canonicalPath != path ) pd->disposeString(canonicalPath); return true; } } return false; } FileID JustInTimeLoader::fileID(const RuntimeState& state) const { return this->fileIdent; } struct HashPointer { static size_t hash(const void* v, void* state) { return std::hash<uintptr_t>{}((uintptr_t)v); } }; struct EqualPointer { static bool equal(const void* s1, const void* s2, void* state) { return s1 == s2; } }; typedef dyld3::Map<const void*, bool, HashPointer, EqualPointer> PointerSet; // A class in the root can be patched only if the __objc_classlist entry for that class is bind to self. We need to // find the class list and check each class. For meta classes, the ISA in the class should be a bind to self #if SUPPORT_VM_LAYOUT static void getObjCPatchClasses(const dyld3::MachOAnalyzer* ma, PointerSet& classPointers) { if ( !ma->hasChainedFixups() ) return; Diagnostics diag; STACK_ALLOC_OVERFLOW_SAFE_ARRAY(const void*, bindTargets, 32); ma->forEachBindTarget(diag, false, ^(const dyld3::MachOAnalyzer::BindTargetInfo& info, bool& stop) { if ( diag.hasError() ) { stop = true; return; } if ( info.libOrdinal == BIND_SPECIAL_DYLIB_SELF ) { void* result = nullptr; bool resultPointsToInstructions = false; if ( ma->hasExportedSymbol(info.symbolName, nullptr, &result, &resultPointsToInstructions) ) { bindTargets.push_back(result); } else { bindTargets.push_back(nullptr); } } else { bindTargets.push_back(nullptr); } }, ^(const MachOAnalyzer::BindTargetInfo& info, bool& stop) { }); if ( diag.hasError() ) return; // Find the classlist and see which entries are binds to self uint64_t classListRuntimeOffset; uint64_t classListSize; bool foundSection = ma->findObjCDataSection("__objc_classlist", classListRuntimeOffset, classListSize); if ( !foundSection ) return; const uint64_t ptrSize = ma->pointerSize(); if ( (classListSize % ptrSize) != 0 ) { diag.error("Invalid objc class section size"); return; } uint64_t classListCount = classListSize / ptrSize; uint16_t chainedPointerFormat = ma->chainedPointerFormat(); const uint8_t* arrayBase = (uint8_t*)ma + classListRuntimeOffset; if ( ptrSize == 8 ) { typedef uint64_t PtrTy; for ( uint64_t i = 0; i != classListCount; ++i ) { const PtrTy* classListEntry = (PtrTy*)(arrayBase + (i * sizeof(PtrTy))); // Add the class to the patch list if its a bind to self const void* classPtr = nullptr; { const auto* classFixup = (const dyld3::MachOLoaded::ChainedFixupPointerOnDisk*)classListEntry; uint32_t bindOrdinal = 0; int64_t unusedAddend = 0; if ( classFixup->isBind(chainedPointerFormat, bindOrdinal, unusedAddend) ) { if ( bindOrdinal < bindTargets.count() ) { classPtr = bindTargets[bindOrdinal]; // Only non-null entries will be binds to self if ( classPtr != nullptr ) classPointers.insert({ classPtr, true }); } } } // Add the metaclass to the patch list if its a bind to self if ( classPtr != nullptr ) { // The metaclass is the class ISA, which is the first field of the class const auto* metaclassFixup = (const dyld3::MachOLoaded::ChainedFixupPointerOnDisk*)classPtr; uint32_t bindOrdinal = 0; int64_t unusedAddend = 0; if ( metaclassFixup->isBind(chainedPointerFormat, bindOrdinal, unusedAddend) ) { if ( bindOrdinal < bindTargets.count() ) { const void* metaclassPtr = bindTargets[bindOrdinal]; // Only non-null entries will be binds to self if ( metaclassPtr != nullptr ) classPointers.insert({ metaclassPtr, true }); } } } } } else { typedef uint32_t PtrTy; for ( uint64_t i = 0; i != classListCount; ++i ) { const PtrTy* classListEntry = (PtrTy*)(arrayBase + (i * sizeof(PtrTy))); // Add the class to the patch list if its a bind to self const void* classPtr = nullptr; { const auto* classFixup = (const dyld3::MachOLoaded::ChainedFixupPointerOnDisk*)classListEntry; uint32_t bindOrdinal = 0; int64_t unusedAddend = 0; if ( classFixup->isBind(chainedPointerFormat, bindOrdinal, unusedAddend) ) { if ( bindOrdinal < bindTargets.count() ) { classPtr = bindTargets[bindOrdinal]; // Only non-null entries will be binds to self if ( classPtr != nullptr ) classPointers.insert({ classPtr, true }); } } } // Add the metaclass to the patch list if its a bind to self if ( classPtr != nullptr ) { // The metaclass is the class ISA, which is the first field of the class const auto* metaclassFixup = (const dyld3::MachOLoaded::ChainedFixupPointerOnDisk*)classPtr; uint32_t bindOrdinal = 0; int64_t unusedAddend = 0; if ( metaclassFixup->isBind(chainedPointerFormat, bindOrdinal, unusedAddend) ) { if ( bindOrdinal < bindTargets.count() ) { const void* metaclassPtr = bindTargets[bindOrdinal]; // Only non-null entries will be binds to self if ( metaclassPtr != nullptr ) classPointers.insert({ metaclassPtr, true }); } } } } } } // A singleton object can only be patched if it matches the layout/authentication expected by the patcher // This finds all eligible singleton classes static void getSingletonPatches(const dyld3::MachOAnalyzer* ma, PointerSet& objectPointers) { Diagnostics diag; ma->forEachSingletonPatch(diag, ^(dyld3::MachOAnalyzer::SingletonPatchKind kind, uint64_t runtimeOffset) { // We only handle a single kind of singleton object for now if ( kind != dyld3::MachOAnalyzer::SingletonPatchKind::cfObj2 ) return; void* value = (uint8_t*)ma + runtimeOffset; objectPointers.insert({ value, true }); }); } // Feature flag. Enable this once we have ld64-804 everywhere static const bool enableObjCPatching = true; static const bool enableSingletonPatching = true; static bool isEligibleForObjCPatching(RuntimeState& state, uint32_t indexOfOverriddenCachedDylib) { const char* path = state.config.dyldCache.addr->getIndexedImagePath(indexOfOverriddenCachedDylib); if ( path == nullptr ) return false; // Some dylibs put data next to their classes. Eg, libdispatch puts a vtable before the class. We can't // make objc patching work in these cases if ( strstr(path, "libdispatch.dylib") != nullptr ) return false; if ( strstr(path, "libxpc.dylib") != nullptr ) return false; if ( strcmp(path, "/usr/lib/libodmodule.dylib") == 0 ) return false; if ( strcmp(path, "/usr/lib/log/liblog_odtypes.dylib") == 0 ) return false; return true; } #endif // SUPPORT_VM_LAYOUT const Loader::DylibPatch* JustInTimeLoader::makePatchTable(RuntimeState& state, uint32_t indexOfOverriddenCachedDylib) const { static const bool extra = false; const PatchTable& patchTable = state.config.dyldCache.patchTable; assert(patchTable.hasValue()); //const DyldSharedCache* dyldCache = state.config.dyldCache.addr; //assert(dyldCache != nullptr); if ( extra ) state.log("Found %s overrides dyld cache index 0x%04X\n", this->path(state), indexOfOverriddenCachedDylib); uint32_t patchCount = patchTable.patchableExportCount(indexOfOverriddenCachedDylib); if ( patchCount != 0 ) { DylibPatch* table = (DylibPatch*)state.persistentAllocator.malloc(sizeof(DylibPatch) * (patchCount + 1));; __block uint32_t patchIndex = 0; #if SUPPORT_VM_LAYOUT const uint8_t* thisAddress = (uint8_t*)(this->loadAddress(state)); const uint8_t* cacheDylibAddress = (uint8_t*)state.config.dyldCache.addr->getIndexedImageEntry(indexOfOverriddenCachedDylib); __block PointerSet eligibleClasses; // The cache builder doesn't analyze objc classes as isEligibleForObjCPatching() relies on // parsing the on-disk chained fixup format if ( isEligibleForObjCPatching(state, indexOfOverriddenCachedDylib) ) getObjCPatchClasses(this->analyzer(), eligibleClasses); __block PointerSet eligibleSingletons; getSingletonPatches(this->analyzer(), eligibleSingletons); patchTable.forEachPatchableExport(indexOfOverriddenCachedDylib, ^(uint32_t dylibVMOffsetOfImpl, const char* exportName, PatchKind patchKind) { Diagnostics exportDiag; ResolvedSymbol foundSymbolInfo; if ( this->hasExportedSymbol(exportDiag, state, exportName, staticLink, skipResolver, &foundSymbolInfo) ) { if ( extra ) state.log(" will patch cache uses of '%s' %s\n", exportName, PatchTable::patchKindName(patchKind)); const dyld3::MachOAnalyzer* implMA = (const dyld3::MachOAnalyzer*)foundSymbolInfo.targetLoader->loadAddress(state); uint8_t* newImplAddress = (uint8_t*)implMA + foundSymbolInfo.targetRuntimeOffset; bool foundUsableObjCClass = false; bool foundSingletonObject = false; switch ( patchKind ) { case PatchKind::regular: break; case PatchKind::cfObj2: { if ( !enableSingletonPatching ) break; if ( eligibleSingletons.find(newImplAddress) == eligibleSingletons.end() ) break; const dyld3::MachOAnalyzer* cacheMA = (const dyld3::MachOAnalyzer*)cacheDylibAddress; const uint8_t* cacheImpl = (uint8_t*)cacheMA + dylibVMOffsetOfImpl; state.patchedSingletons.push_back({ (uintptr_t)cacheImpl, (uintptr_t)newImplAddress }); foundSingletonObject = true; break; } case PatchKind::objcClass: { // Check if we can use ObjC patching. For now this is only for non-swift classes if ( !enableObjCPatching ) break; if ( eligibleClasses.find(newImplAddress) == eligibleClasses.end() ) break; const dyld3::MachOAnalyzer* cacheMA = (const dyld3::MachOAnalyzer*)cacheDylibAddress; const uint8_t* cacheImpl = (uint8_t*)cacheMA + dylibVMOffsetOfImpl; if ( implMA->isSwiftClass(newImplAddress) ) break; if ( cacheMA->isSwiftClass(cacheImpl) ) break; // Interpose so that if anyone tries to bind to the class in the root, then they'll instead bind // to the class in the shared cache state.patchedObjCClasses.push_back({ (uintptr_t)cacheImpl, (uintptr_t)newImplAddress }); state.objcReplacementClasses.push_back({ cacheMA, (uintptr_t)cacheImpl, implMA, (uintptr_t)newImplAddress }); foundUsableObjCClass = true; break; } } if ( foundUsableObjCClass ) { table[patchIndex].overrideOffsetOfImpl = DylibPatch::objcClass; } else if ( foundSingletonObject ) { table[patchIndex].overrideOffsetOfImpl = DylibPatch::singleton; } else { // note: we are saving a signed 64-bit offset to the impl. This is to support re-exported symbols table[patchIndex].overrideOffsetOfImpl = newImplAddress - thisAddress; } } else { if ( extra ) state.log(" override missing '%s', so uses will be patched to NULL\n", exportName); table[patchIndex].overrideOffsetOfImpl = DylibPatch::missingSymbol; } ++patchIndex; }); // mark end of table table[patchIndex].overrideOffsetOfImpl = DylibPatch::endOfPatchTable; // record in Loader return table; #else CacheVMAddress thisVMAddr(this->mf(state)->preferredLoadAddress()); // The cache builder doesn't lay out dylibs in VM layout, so we need to use VMAddr/VMOffset everywhere patchTable.forEachPatchableExport(indexOfOverriddenCachedDylib, ^(uint32_t dylibVMOffsetOfImpl, const char* exportName, PatchKind patchKind) { Diagnostics exportDiag; ResolvedSymbol foundSymbolInfo; if ( this->hasExportedSymbol(exportDiag, state, exportName, staticLink, skipResolver, &foundSymbolInfo) ) { if ( extra ) state.log(" will patch cache uses of '%s' %s\n", exportName, PatchTable::patchKindName(patchKind)); CacheVMAddress implBaseVMAddr(foundSymbolInfo.targetLoader->mf(state)->preferredLoadAddress()); CacheVMAddress newImplVMAddr = implBaseVMAddr + VMOffset(foundSymbolInfo.targetRuntimeOffset); // note: we are saving a signed 64-bit offset to the impl. This is to support re-exported symbols VMOffset offsetToImpl = newImplVMAddr - thisVMAddr; table[patchIndex].overrideOffsetOfImpl = offsetToImpl.rawValue(); } else { if ( extra ) state.log(" override missing '%s', so uses will be patched to NULL\n", exportName); table[patchIndex].overrideOffsetOfImpl = DylibPatch::missingSymbol; } ++patchIndex; }); // mark end of table table[patchIndex].overrideOffsetOfImpl = DylibPatch::endOfPatchTable; // record in Loader return table; #endif // SUPPORT_VM_LAYOUT } return nullptr; } void JustInTimeLoader::loadDependents(Diagnostics& diag, RuntimeState& state, const LoadOptions& options) { if ( dependentsSet ) return; // add first level of dependents __block int depIndex = 0; const mach_o::MachOFileRef& mf = this->mappedAddress; const Header* mh = (Header*)(&mf->magic); // Better way? mh->forEachLinkedDylib(^(const char* loadPath, LinkedDylibAttributes depAttrs, Version32 compatVersion, Version32 curVersion, bool& stop) { // fix illegal combinations of dylib attributes if ( depAttrs.reExport && depAttrs.delayInit ) depAttrs.delayInit = false; if ( depAttrs.reExport && depAttrs.weakLink ) depAttrs.weakLink = false; if ( !this->allDepsAreNormal ) dependentAttrs(depIndex) = depAttrs; // If this is a shared cache JITLoader then there's likely a root installed and we // had to invalidate the prebuilt loaders. This shared cache dylib may have weakly linked // something outside the cache, and the cache builder would break that weak edge. We // want to mimic that behaviour to ensure consistency if ( this->dylibInDyldCache && depAttrs.weakLink ) { // FIXME: Could we ever not have a cache here, given that we aren't an app loader? const DyldSharedCache* cache = state.config.dyldCache.addr; __block uint32_t unusedDylibInCacheIndex; if ( (cache != nullptr) && !state.config.dyldCache.indexOfPath(loadPath, unusedDylibInCacheIndex) ) { if ( state.config.log.loaders ) state.log("Skipping shared cache weak-linked dylib '%s' from '%s'\n", loadPath, this->path(state)); dependents[depIndex] = nullptr; depIndex++; return; } } const Loader* depLoader = nullptr; // for absolute paths, do a quick check if this is already loaded with exact match if ( loadPath[0] == '/' ) { for ( const Loader* ldr : state.loaded ) { if ( ldr->matchesPath(state, loadPath) ) { depLoader = ldr; break; } } } if ( depLoader == nullptr ) { // first load, so do full search LoadChain nextChain { options.rpathStack, this }; Diagnostics depDiag; LoadOptions depOptions = options; depOptions.requestorNeedsFallbacks = this->pre2022Binary; depOptions.rpathStack = &nextChain; depOptions.canBeMissing = depAttrs.weakLink; depLoader = options.finder ? options.finder(depDiag, state.config.process.platform, loadPath, depOptions) : getLoader(depDiag, state, loadPath, depOptions); if ( depDiag.hasError() ) { char fromUuidStr[64]; this->getUuidStr(fromUuidStr); // rdar://15648948 (On fatal errors, check binary's min-OS version and note if from the future) Diagnostics tooNewBinaryDiag; this->tooNewErrorAddendum(tooNewBinaryDiag, state); diag.error("Library not loaded: %s\n Referenced from: <%s> %s%s\n Reason: %s", loadPath, fromUuidStr, this->path(state), tooNewBinaryDiag.errorMessageCStr(), depDiag.errorMessageCStr()); #if BUILDING_DYLD if ( options.launching ) state.setLaunchMissingDylib(loadPath, this->path(state)); #endif stop = true; } } dependents[depIndex] = (Loader*)depLoader; depIndex++; }); dependentsSet = true; if ( diag.hasError() ) return; // breadth first recurse LoadChain nextChain { options.rpathStack, this }; LoadOptions depOptions = options; depOptions.rpathStack = &nextChain; for ( depIndex = 0; depIndex < this->depCount; ++depIndex ) { if ( Loader* depLoader = dependents[depIndex] ) { depLoader->loadDependents(diag, state, depOptions); } } // if this image overrides something in the dyld cache, build a table of its patches for use by other dylibs later if ( this->overridesCache ) { this->overridePatches = makePatchTable(state, this->overrideIndex); // Also build patches for overrides of unzippered twins // The above case handled an iOSMac dylib rooting an iOSMac unzippered twin. This handles the iOSMac dylib // overriding the macOS unzippered twin this->overridePatchesCatalystMacTwin = nullptr; if ( state.config.process.catalystRuntime ) { // Find the macOS twin overridden index uint16_t macOSTwinIndex = Loader::indexOfUnzipperedTwin(state, this->overrideIndex); if ( macOSTwinIndex != kNoUnzipperedTwin ) this->overridePatchesCatalystMacTwin = makePatchTable(state, macOSTwinIndex); } } } uint32_t JustInTimeLoader::dependentCount() const { return this->depCount; } JustInTimeLoader::LinkedDylibAttributes& JustInTimeLoader::dependentAttrs(uint32_t depIndex) { assert(depIndex < this->depCount); assert(!this->allDepsAreNormal); // Dependent kinds are after the dependent loaders uint8_t* firstDepKind = (uint8_t*)&dependents[this->depCount]; return ((JustInTimeLoader::LinkedDylibAttributes*)firstDepKind)[depIndex]; } Loader* JustInTimeLoader::dependent(const RuntimeState& state, uint32_t depIndex, LinkedDylibAttributes* depAttrs) const { assert(depIndex < this->depCount); if ( depAttrs != nullptr ) { if ( this->allDepsAreNormal ) *depAttrs = LinkedDylibAttributes::regular; else *depAttrs = ((JustInTimeLoader*)this)->dependentAttrs(depIndex); } return dependents[depIndex]; } bool JustInTimeLoader::getExportsTrie(uint64_t& runtimeOffset, uint32_t& size) const { if ( this->exportsTrieRuntimeOffset != 0 ) { runtimeOffset = this->exportsTrieRuntimeOffset; size = this->exportsTrieSize; return true; } return false; } bool JustInTimeLoader::hiddenFromFlat(bool forceGlobal) const { if ( forceGlobal ) this->hidden = false; return this->hidden; } bool JustInTimeLoader::representsCachedDylibIndex(uint16_t dylibIndex) const { // check if this is an override of the specified cached dylib if ( this->overridesCache && (this->overrideIndex == dylibIndex) ) return true; // check if this is the specified dylib in the cache if ( this->dylibInDyldCache && (this->ref.index == dylibIndex) ) return true; return false; } #if BUILDING_DYLD || BUILDING_UNIT_TESTS void JustInTimeLoader::logFixup(RuntimeState& state, uint64_t fixupLocRuntimeOffset, uintptr_t newValue, PointerMetaData pmd, const Loader::ResolvedSymbol& target) const { const MachOAnalyzer* ma = this->analyzer(); uintptr_t* fixupLoc = (uintptr_t*)((uint8_t*)ma + fixupLocRuntimeOffset); switch ( target.kind ) { case Loader::ResolvedSymbol::Kind::rebase: #if BUILDING_DYLD && __has_feature(ptrauth_calls) if ( pmd.authenticated ) state.log("rebase: *0x%012lX = 0x%012lX (*%s+0x%012lX = 0x%012lX+0x%012lX) (JOP: diversity=0x%04X, addr-div=%d, key=%s)\n", (long)fixupLoc, newValue, this->leafName(state), (long)fixupLocRuntimeOffset, (uintptr_t)ma, (long)target.targetRuntimeOffset, pmd.diversity, pmd.usesAddrDiversity, MachOLoaded::ChainedFixupPointerOnDisk::Arm64e::keyName(pmd.key)); else #endif // BUILDING_DYLD && __has_feature(ptrauth_calls) state.log("rebase: *0x%012lX = 0x%012lX (*%s+0x%012lX = 0x%012lX+0x%012lX)\n", (long)fixupLoc, newValue, this->leafName(state), (long)fixupLocRuntimeOffset, (uintptr_t)ma, (long)target.targetRuntimeOffset); break; case Loader::ResolvedSymbol::Kind::bindToImage: #if BUILDING_DYLD && __has_feature(ptrauth_calls) if ( pmd.authenticated ) state.log("bind: *0x%012lX = 0x%012lX (*%s+0x%012lX = %s/%s) (JOP: diversity=0x%04X, addr-div=%d, key=%s)\n", (long)fixupLoc, newValue, this->leafName(state), (long)fixupLocRuntimeOffset, target.targetLoader->leafName(state), target.targetSymbolName, pmd.diversity, pmd.usesAddrDiversity, MachOLoaded::ChainedFixupPointerOnDisk::Arm64e::keyName(pmd.key)); else #endif // BUILDING_DYLD && __has_feature(ptrauth_calls) state.log("bind: *0x%012lX = 0x%012lX (*%s+0x%012lX = %s/%s)\n", (long)fixupLoc, newValue, this->leafName(state), (long)fixupLocRuntimeOffset, target.targetLoader->leafName(state), target.targetSymbolName); break; case Loader::ResolvedSymbol::Kind::bindAbsolute: state.log("bind: *0x%012lX = 0x%012lX (*%s+0x%012lX = 0x%012lX(%s))\n", (long)fixupLoc, newValue, this->leafName(state), (long)fixupLocRuntimeOffset, (long)target.targetRuntimeOffset, target.targetSymbolName); break; } } #endif // BUILDING_DYLD || BUILDING_UNIT_TESTS bool JustInTimeLoader::overridesDylibInCache(const DylibPatch*& patchTable, uint16_t& cacheDylibOverriddenIndex) const { if ( !this->overridesCache ) return false; patchTable = this->overridePatches; cacheDylibOverriddenIndex = this->overrideIndex; return true; } void JustInTimeLoader::withLayout(Diagnostics &diag, RuntimeState& state, void (^callback)(const mach_o::Layout &layout)) const { #if SUPPORT_VM_LAYOUT this->analyzer()->withVMLayout(diag, callback); #else // In the cache builder, we must have set a layout if this is a cache dylib if ( this->dylibInDyldCache ) { assert(this->nonRuntimeLayout != nullptr); callback(*this->nonRuntimeLayout); return; } // Not in the cache, but the cache builder never uses MachOAnalyzer, so use the MachOFile layout mach_o::MachOFileRef fileRef = this->mf(state); fileRef->withFileLayout(diag, callback); #endif // SUPPORT_VM_LAYOUT } bool JustInTimeLoader::dyldDoesObjCFixups() const { // JustInTimeLoaders do not do objc fixups, except for dylibs in dyld cache (which we fixed up at cache build time) return this->dylibInDyldCache; } const SectionLocations* JustInTimeLoader::getSectionLocations() const { return &this->sectionLocations; } #if BUILDING_DYLD || BUILDING_UNIT_TESTS void JustInTimeLoader::handleStrongWeakDefOverrides(RuntimeState& state, DyldCacheDataConstLazyScopedWriter& cacheDataConst) { CacheWeakDefOverride cacheWeakDefFixup = ^(uint32_t cachedDylibIndex, uint32_t cachedDylibVMOffset, const ResolvedSymbol& target) { JustInTimeLoader::cacheWeakDefFixup(state, cacheDataConst, cachedDylibIndex, cachedDylibVMOffset, target); }; // Find an on-disk dylib with weak-defs, if one exists. If we find one, look for strong overrides of all the special weak symbols // On all platforms we look in the main executable for strong symbols const Loader* weakDefLoader = nullptr; if ( state.mainExecutableLoader->hasWeakDefs ) weakDefLoader = state.mainExecutableLoader; // On macOS, we also allow check on-disk dylibs for strong symbols #if TARGET_OS_OSX if ( weakDefLoader == nullptr ) { for (const Loader* loader : state.loaded) { if ( !loader->dylibInDyldCache ) { const dyld3::MachOAnalyzer *ma = loader->analyzer(state); if ( loader->hasWeakDefs && ma->hasOpcodeFixups() ) { weakDefLoader = loader; break; } } } } #endif // TARGET_OS_OSX if ( weakDefLoader != nullptr ) { MachOAnalyzer::forEachTreatAsWeakDef(^(const char* symbolName) { Diagnostics weakBindDiag; // ignore failures here (void)weakDefLoader->resolveSymbol(weakBindDiag, state, BIND_SPECIAL_DYLIB_WEAK_LOOKUP, symbolName, true, false, cacheWeakDefFixup); }); } } void JustInTimeLoader::cacheWeakDefFixup(RuntimeState& state, DyldCacheDataConstLazyScopedWriter& cacheDataConst, uint32_t cachedDylibIndex, uint32_t cachedDylibVMOffset, const ResolvedSymbol& target) { const DyldSharedCache* dyldcache = state.config.dyldCache.addr; //state.log("cache patch: dylibIndex=%d, exportCacheOffset=0x%08X, target=%s\n", cachedDylibIndex, exportCacheOffset,target.targetSymbolName); dyldcache->forEachPatchableUseOfExport(cachedDylibIndex, cachedDylibVMOffset, ^(uint64_t cacheVMOffset, dyld3::MachOLoaded::PointerMetaData pmd, uint64_t addend, bool isWeakImport) { uintptr_t* loc = (uintptr_t*)(((uint8_t*)dyldcache) + cacheVMOffset); uintptr_t newImpl = (uintptr_t)(Loader::resolvedAddress(state, target) + addend); #if __has_feature(ptrauth_calls) if ( pmd.authenticated ) newImpl = MachOLoaded::ChainedFixupPointerOnDisk::Arm64e::signPointer(newImpl, loc, pmd.usesAddrDiversity, pmd.diversity, pmd.key); #endif // ignore duplicate patch entries if ( *loc != newImpl ) { cacheDataConst.makeWriteable(); if ( state.config.log.fixups ) state.log("cache patch: %p = 0x%0lX\n", loc, newImpl); *loc = newImpl; } }); } void JustInTimeLoader::applyFixups(Diagnostics& diag, RuntimeState& state, DyldCacheDataConstLazyScopedWriter& cacheDataConst, bool allowLazyBinds, lsl::Vector<PseudoDylibSymbolToMaterialize>* materializingSymbols) const { //state.log("applyFixups: %s\n", this->path()); // if this is in the dyld cache there is normally no fixups need if ( this->dylibInDyldCache ) { // But if some lower level cached dylib has a root, we // need to patch this image's uses of that rooted dylib. if ( state.hasOverriddenCachedDylib() ) { // have each other image apply to me any cache patching it has for ( const Loader* ldr : state.loaded ) { ldr->applyCachePatchesTo(state, this, cacheDataConst); } } // images in shared cache don't need any more fixups this->fixUpsApplied = true; return; } if ( this->pd ) { // FIXME: Do we need to handle anything here? We probably do if we want // to support things like extending the main executable with JIT'd code. return; } CacheWeakDefOverride cacheWeakDefFixup = ^(uint32_t cachedDylibIndex, uint32_t cachedDylibVMOffset, const ResolvedSymbol& target) { JustInTimeLoader::cacheWeakDefFixup(state, cacheDataConst, cachedDylibIndex, cachedDylibVMOffset, target); }; // build targets table STACK_ALLOC_OVERFLOW_SAFE_ARRAY(const void*, bindTargets, 512); STACK_ALLOC_OVERFLOW_SAFE_ARRAY(const void*, overrideTargetAddrs, 32); STACK_ALLOC_OVERFLOW_SAFE_ARRAY(MissingFlatLazySymbol, missingFlatLazySymbols, 4); this->forEachBindTarget(diag, state, cacheWeakDefFixup, allowLazyBinds, ^(const ResolvedSymbol& target, bool& stop) { const void* targetAddr = (const void*)Loader::interpose(state, Loader::resolvedAddress(state, target), this); if ( state.config.log.fixups ) { const char* targetLoaderName = target.targetLoader ? target.targetLoader->leafName(state) : "<none>"; state.log("<%s/bind#%llu> -> %p (%s/%s)\n", this->leafName(state), bindTargets.count(), targetAddr, targetLoaderName, target.targetSymbolName); } // Record missing flat-namespace lazy symbols if ( target.isMissingFlatLazy ) missingFlatLazySymbols.push_back({ target.targetSymbolName, (uint32_t)bindTargets.count() }); // Record pseudo dylib symbols we need to materialize if ( target.isMaterializing && materializingSymbols ) materializingSymbols->push_back({ target.targetLoader, target.targetSymbolName }); bindTargets.push_back(targetAddr); }, ^(const ResolvedSymbol& target, bool& stop) { // Missing weak binds need placeholders to make the target indices line up, but we should otherwise ignore them if ( (target.kind == Loader::ResolvedSymbol::Kind::bindToImage) && (target.targetLoader == nullptr) ) { if ( state.config.log.fixups ) state.log("<%s/bind#%llu> -> missing-weak-bind (%s)\n", this->leafName(state), overrideTargetAddrs.count(), target.targetSymbolName); overrideTargetAddrs.push_back((const void*)UINTPTR_MAX); } else { const void* targetAddr = (const void*)Loader::interpose(state, Loader::resolvedAddress(state, target), this); if ( state.config.log.fixups ) { const char* targetLoaderName = target.targetLoader ? target.targetLoader->leafName(state) : "<none>"; state.log("<%s/bind#%llu> -> %p (%s/%s)\n", this->leafName(state), overrideTargetAddrs.count(), targetAddr, targetLoaderName, target.targetSymbolName); } // Record missing flat-namespace lazy symbols if ( target.isMissingFlatLazy ) missingFlatLazySymbols.push_back({ target.targetSymbolName, (uint32_t)overrideTargetAddrs.count() }); overrideTargetAddrs.push_back(targetAddr); } }); if ( diag.hasError() ) return; // do fixups using bind targets table this->applyFixupsGeneric(diag, state, this->sliceOffset, bindTargets, overrideTargetAddrs, true, missingFlatLazySymbols); // some old macOS games need __dyld section set up if ( (state.config.process.platform == dyld3::Platform::macOS) && (state.libdyldLoader != nullptr) ) { const MachOAnalyzer* ma = this->analyzer(); if ( !ma->inDyldCache() ) { ma->forEachSupportedPlatform(^(dyld3::Platform platform, uint32_t minOS, uint32_t sdk) { // rdar://84760053 (SEED: Web: Crash in libobjc.A.dylib's load_images when loading certain bundles in Monterey) if ( (platform == dyld3::Platform::macOS) && (minOS <= 0x000A0900) ) { struct DATAdyld { void* dyldLazyBinder; dyld3::DyldLookFunc dyldFuncLookup; }; uint64_t sectSize; if ( DATAdyld* dyldSect = (DATAdyld*)ma->findSectionContent("__DATA", "__dyld", sectSize) ) { //state.log("found __dyld section in %s\n", this->path()); uint64_t dyld4SectSize; const MachOLoaded* libdyldML = state.libdyldLoader->loadAddress(state); if ( LibdyldDyld4Section* libdyld4Section = (LibdyldDyld4Section*)libdyldML->findSectionContent("__TPRO_CONST", "__dyld4", dyld4SectSize) ) { dyldSect->dyldLazyBinder = nullptr; dyldSect->dyldFuncLookup = (dyld3::DyldLookFunc)libdyld4Section->dyldLookupFuncAddr; } } } }); } } // mark any __DATA_CONST segments read-only if ( this->hasConstantSegmentsToProtect() ) this->makeSegmentsReadOnly(state); if ( diag.noError() ) this->fixUpsApplied = true; } #if SUPPORT_IMAGE_UNLOADING void JustInTimeLoader::unmap(RuntimeState& state, bool force) const { if ( this->dylibInDyldCache ) return; if ( this->pd ) return; if ( !force && this->neverUnload ) state.log("trying to unmap %s\n", this->path(state)); assert(force || !this->neverUnload); size_t vmSize = (size_t)this->analyzer()->mappedSize(); void* vmStart = (void*)(this->loadAddress(state)); state.config.syscall.munmap(vmStart, vmSize); if ( state.config.log.segments ) state.log("unmapped 0x%012lX->0x%012lX for %s\n", (long)vmStart, (long)vmStart + (long)vmSize, this->path(state)); } #endif // SUPPORT_IMAGE_UNLOADING #endif // BUILDING_DYLD || BUILDING_UNIT_TESTS bool JustInTimeLoader::hasBeenFixedUp(RuntimeState&) const { return fixUpsApplied; } bool JustInTimeLoader::beginInitializers(RuntimeState&) { // do nothing if already initializers already run if ( inited ) return true; // switch to being-inited state inited = true; return false; } #if BUILDING_DYLD || BUILDING_UNIT_TESTS void JustInTimeLoader::runInitializers(RuntimeState& state) const { this->findAndRunAllInitializers(state); // FIXME: Should we run "JIT" initializers *after* regular initializers, or // should it be either/or? // The main use-case for extending an existing image with JIT'd code is the // main executable (for previews), but there may be others. // FIXME: Error plumbing? if ( pd ) { if ( char *errMsg = pd->initialize() ) { state.log("error running pseudo-dylib initializers: %s", errMsg); pd->disposeString(errMsg); } } } #endif bool JustInTimeLoader::isDelayInit(RuntimeState&) const { return this->delayInit; } void JustInTimeLoader::setDelayInit(RuntimeState&, bool value) const { if ( value ) { // "mark" phase // if this image has already been initialized, then there is no point in re-evaluting if it is not-delayed if ( !inited ) this->delayInit = value; } else { // "sweep" phase this->delayInit = value; } } //////////////////////// other functions ///////////////////////////////// static bool hasPlusLoad(const MachOFile* mh) { Diagnostics diag; return mh->hasPlusLoadMethod(diag); } static bool hasDataConst(const MachOFile* mh) { __block bool result = false; mh->forEachSegment(^(const MachOAnalyzer::SegmentInfo& info, bool& stop) { if ( info.readOnlyData ) result = true; }); return result; } JustInTimeLoader::JustInTimeLoader(const MachOFile* mh, const Loader::InitialOptions& options, const FileID& fileID, const mach_o::Layout* layout, bool isPremapped) : Loader(options, false, false, 0, isPremapped), mappedAddress((const dyld3::MachOLoaded*)mh), fileIdent(fileID) { } JustInTimeLoader* JustInTimeLoader::make(RuntimeState& state, const MachOFile* mh, const char* path, const FileID& fileID, uint64_t sliceOffset, bool willNeverUnload, bool leaveMapped, bool overridesCache, uint16_t overridesDylibIndex, const mach_o::Layout* layout) { //state.log("JustInTimeLoader::make(%s) willNeverUnload=%d\n", path, willNeverUnload); // use malloc and placement new to create object big enough for all info bool allDepsAreNormal = true; uint32_t depCount = ((Header*)mh)->linkedDylibCount(&allDepsAreNormal); uint32_t minDepCount = (depCount ? depCount - 1 : 1); size_t sizeNeeded = sizeof(JustInTimeLoader) + (minDepCount * sizeof(AuthLoader)) + (allDepsAreNormal ? 0 : depCount) + strlen(path) + 1; void* storage = state.persistentAllocator.malloc(sizeNeeded); uuid_t uuid; Loader::InitialOptions options; #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS options.inDyldCache = mh->inDyldCache(); #else options.inDyldCache = DyldSharedCache::inDyldCache(state.config.dyldCache.addr, mh); #endif options.hasObjc = mh->hasObjC(); options.mayHavePlusLoad = hasPlusLoad(mh); options.roData = hasDataConst(mh); options.neverUnloaded = willNeverUnload || overridesCache; // dylibs in cache never unload, be consistent and don't unload roots either options.leaveMapped = leaveMapped; options.roObjC = options.hasObjc && mh->hasConstObjCSection(); options.pre2022Binary = !mh->enforceFormat(MachOAnalyzer::Malformed::sdkOnOrAfter2022); options.hasUUID = mh->getUuid(uuid); options.hasWeakDefs = mh->hasWeakDefs(); options.hasTLVs = mh->hasThreadLocalVariables(); options.belowLibSystem = mh->isDylib() && (strncmp(mh->installName(), "/usr/lib/system/lib", 19) == 0); JustInTimeLoader* p = new (storage) JustInTimeLoader(mh, options, fileID, layout, false); #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS p->nonRuntimeLayout = layout; #endif // fill in extra data p->pathOffset = sizeof(JustInTimeLoader) + (minDepCount * sizeof(AuthLoader)) + (allDepsAreNormal ? 0 : depCount); p->dependentsSet = false; p->fixUpsApplied = false; p->inited = false; p->hidden = false; p->altInstallName = mh->isDylib() && (strcmp(mh->installName(), path) != 0); p->lateLeaveMapped = false; p->allDepsAreNormal = allDepsAreNormal; p->padding = 0; p->sliceOffset = sliceOffset; if ( options.hasUUID ) { memcpy(p->uuid, uuid, sizeof(uuid_t)); } else { // for reproducibility bzero(p->uuid, sizeof(uuid_t)); } p->cpusubtype = mh->cpusubtype; parseSectionLocations(mh, p->sectionLocations); if ( !mh->hasExportTrie(p->exportsTrieRuntimeOffset, p->exportsTrieSize) ) { p->exportsTrieRuntimeOffset = 0; p->exportsTrieSize = 0; } p->overridePatches = nullptr; p->pd = nullptr; p->overridesCache = overridesCache; p->overrideIndex = overridesDylibIndex; p->depCount = depCount; for ( unsigned i = 0; i < depCount; ++i ) { new (&p->dependents[i]) (AuthLoader) { nullptr }; if ( !allDepsAreNormal ) p->dependentAttrs(i) = LinkedDylibAttributes::regular; // will be set to correct kind in loadDependents() } strlcpy(((char*)p) + p->pathOffset, path, PATH_MAX); //state.log("JustInTimeLoader::make(%p, %s) => %p\n", ma, path, p); p->delayInit = false; state.add(p); #if BUILDING_DYLD if ( overridesCache ) { // The only case where a library in the dyld cache overrides another library in the cache is when an unzippered twin overrides its macOS counterpart. // We don't want hasOverriddenCachedDylib to be set in such case. if ( options.inDyldCache ) { state.setHasOverriddenUnzipperedTwin(); } else { state.setHasOverriddenCachedDylib(); } } if ( state.config.log.loaders ) state.log("using JustInTimeLoader %p for %s\n", p, path); #endif return p; } void JustInTimeLoader::parseSectionLocations(const dyld3::MachOFile* mf, SectionLocations& metadata) { for ( uint32_t i = 0; i < SectionLocations::count; ++i ) { metadata.offsets[i] = 0; metadata.sizes[i] = 0; } uint64_t baseAddress = mf->preferredLoadAddress(); auto setSectionOffset = ^(uint32_t sectionKind, const dyld3::MachOFile::SectionInfo& sectInfo) { uint64_t sectionOffset = sectInfo.sectAddr - baseAddress; metadata.offsets[sectionKind] = sectionOffset; metadata.sizes[sectionKind] = sectInfo.sectSize; }; mf->forEachSection(^(const dyld3::MachOFile::SectionInfo& sectInfo, bool malformedSectionRange, bool& stop) { if ( !strcmp(sectInfo.segInfo.segName, "__TEXT") ) { if ( !strcmp(sectInfo.sectName, "__swift5_protos") ) setSectionOffset(_dyld_section_location_text_swift5_protos, sectInfo); else if ( !strcmp(sectInfo.sectName, "__swift5_proto") ) setSectionOffset(_dyld_section_location_text_swift5_proto, sectInfo); else if ( !strcmp(sectInfo.sectName, "__swift5_types") ) setSectionOffset(_dyld_section_location_text_swift5_types, sectInfo); else if ( !strcmp(sectInfo.sectName, "__swift5_replace") ) setSectionOffset(_dyld_section_location_text_swift5_replace, sectInfo); else if ( !strcmp(sectInfo.sectName, "__swift5_replac2") ) setSectionOffset(_dyld_section_location_text_swift5_replace2, sectInfo); else if ( !strcmp(sectInfo.sectName, "__swift5_acfuncs") ) setSectionOffset(_dyld_section_location_text_swift5_ac_funcs, sectInfo); return; } if ( !strncmp(sectInfo.segInfo.segName, "__DATA", 6) ) { if ( !strcmp(sectInfo.sectName, "__objc_imageinfo") ) setSectionOffset(_dyld_section_location_objc_image_info, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_selrefs") ) setSectionOffset(_dyld_section_location_data_sel_refs, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_msgrefs") ) setSectionOffset(_dyld_section_location_data_msg_refs, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_classrefs") ) setSectionOffset(_dyld_section_location_data_class_refs, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_superrefs") ) setSectionOffset(_dyld_section_location_data_super_refs, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_protorefs") ) setSectionOffset(_dyld_section_location_data_protocol_refs, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_classlist") ) setSectionOffset(_dyld_section_location_data_class_list, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_stublist") ) setSectionOffset(_dyld_section_location_data_stub_list, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_nlclslist") ) setSectionOffset(_dyld_section_location_data_non_lazy_class_list, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_catlist") ) setSectionOffset(_dyld_section_location_data_category_list, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_catlist2") ) setSectionOffset(_dyld_section_location_data_category_list2, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_nlcatlist") ) setSectionOffset(_dyld_section_location_data_non_lazy_category_list, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_protolist") ) setSectionOffset(_dyld_section_location_data_protocol_list, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_fork_ok") ) setSectionOffset(_dyld_section_location_data_objc_fork_ok, sectInfo); else if ( !strcmp(sectInfo.sectName, "__objc_rawisa") ) setSectionOffset(_dyld_section_location_data_raw_isa, sectInfo); return; } }); } Loader::FileValidationInfo JustInTimeLoader::getFileValidationInfo(RuntimeState& state) const { __block FileValidationInfo result; // set checkInodeMtime and checkCdHash to false by default bzero(&result, sizeof(FileValidationInfo)); if ( this->fileIdent.valid() ) { result.checkInodeMtime = true; result.sliceOffset = this->sliceOffset; result.inode = this->fileIdent.inode(); result.mtime = this->fileIdent.mtime(); } if ( !this->dylibInDyldCache ) { #if SUPPORT_VM_LAYOUT const MachOAnalyzer* ma = this->analyzer(); ma->forEachCDHash(^(const uint8_t aCdHash[20]) { result.checkCdHash = true; memcpy(&result.cdHash[0], &aCdHash[0], 20); }); #else uint32_t codeSignFileOffset = 0; uint32_t codeSignFileSize = 0; const mach_o::MachOFileRef& ref = this->mappedAddress; if ( ref->hasCodeSignature(codeSignFileOffset, codeSignFileSize) ) { ref->forEachCDHashOfCodeSignature(ref.getOffsetInToFile(codeSignFileOffset), codeSignFileSize, ^(const uint8_t aCdHash[20]) { result.checkCdHash = true; memcpy(&result.cdHash[0], &aCdHash[0], 20); }); } #endif #if !SUPPORT_CREATING_PREMAPPEDLOADERS result.deviceID = this->fileIdent.device(); #endif // !SUPPORT_CREATING_PREMAPPEDLOADERS } return result; } const Loader::DylibPatch* JustInTimeLoader::getCatalystMacTwinPatches() const { return this->overridePatchesCatalystMacTwin; } void JustInTimeLoader::withRegions(const MachOFile* mf, void (^callback)(const Array<Region>& regions)) { uint32_t segCount = mf->segmentCount(); uint64_t vmTextAddr = mf->preferredLoadAddress(); STACK_ALLOC_ARRAY(Region, regions, segCount * 2); mf->forEachSegment(^(const MachOAnalyzer::SegmentInfo& segInfo, bool& stop) { Region region; if ( !segInfo.hasZeroFill || (segInfo.fileSize != 0) ) { // add region for content that is not wholely zerofill region.vmOffset = segInfo.vmAddr - vmTextAddr; region.perms = segInfo.protections; region.readOnlyData = segInfo.readOnlyData; region.isZeroFill = false; region.fileOffset = (uint32_t)segInfo.fileOffset; region.fileSize = (uint32_t)segInfo.fileSize; // special case LINKEDIT, the vmsize is often larger than the filesize // but we need to mmap off end of file, otherwise we may have r/w pages at end if ( (segInfo.segIndex == segCount - 1) && (segInfo.protections == 1) ) { region.fileSize = (uint32_t)segInfo.vmSize; } regions.push_back(region); } if ( segInfo.hasZeroFill ) { Region fill; fill.vmOffset = segInfo.vmAddr - vmTextAddr + segInfo.fileSize; fill.perms = segInfo.protections; fill.readOnlyData = false; fill.isZeroFill = true; fill.fileOffset = 0; fill.fileSize = (uint32_t)(segInfo.vmSize - segInfo.fileSize); regions.push_back(fill); } }); callback(regions); } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS JustInTimeLoader* JustInTimeLoader::makeJustInTimeLoaderDyldCache(RuntimeState& state, const MachOFile* mf, const char* installName, uint32_t dylibCacheIndex, const FileID& fileID, bool catalystTwin, uint32_t twinIndex, const mach_o::Layout* layout) { bool cacheOverride = catalystTwin; JustInTimeLoader* jitLoader = JustInTimeLoader::make(state, mf, installName, fileID, 0, true, false, cacheOverride, twinIndex, layout); jitLoader->ref.app = false; jitLoader->ref.index = dylibCacheIndex; return jitLoader; } #endif // BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS #if BUILDING_CACHE_BUILDER_UNIT_TESTS || BUILDING_UNIT_TESTS JustInTimeLoader* JustInTimeLoader::makeJustInTimeLoader(RuntimeState& state, const MachOFile* mf, const char* installName) { const mach_o::Layout* layout = nullptr; JustInTimeLoader* jitLoader = JustInTimeLoader::make(state, mf, installName, FileID::none(), 0, true, false, false, 0, layout); return jitLoader; } #endif // BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS Loader* JustInTimeLoader::makeJustInTimeLoaderDyldCache(Diagnostics& diag, RuntimeState& state, const char* loadPath, const LoadOptions& options, uint32_t dylibCacheIndex, const mach_o::Layout* layout) { uint64_t mtime = 0; uint64_t inode = 0; const MachOFile* cacheMF = (MachOFile*)state.config.dyldCache.getIndexedImageEntry(dylibCacheIndex, mtime, inode); bool fileIDValid = state.config.dyldCache.dylibsExpectedOnDisk; uint64_t device = 0; #if TARGET_OS_SIMULATOR if ( fileIDValid ) { // We need to get the simulator dylib device ID. This is required if we later want to match this loader by fileID device = state.config.process.dyldSimFSID; } #endif UUID fsUUID; FileID fileID(inode, device, mtime, fileIDValid); if ( !cacheMF->loadableIntoProcess(state.config.process.platform, loadPath, state.config.security.isInternalOS) ) { diag.error("wrong platform to load into process"); return nullptr; } bool catalystOverrideOfMacSide = false; uint32_t catalystOverideDylibIndex = 0; if ( strncmp(loadPath, "/System/iOSSupport/", 19) == 0 ) { uint32_t macIndex; if ( state.config.dyldCache.indexOfPath(&loadPath[18], macIndex) ) { catalystOverrideOfMacSide = true; catalystOverideDylibIndex = macIndex; } } JustInTimeLoader* result = JustInTimeLoader::make(state, cacheMF, loadPath, fileID, 0, true, false, catalystOverrideOfMacSide, catalystOverideDylibIndex, layout); result->ref.index = dylibCacheIndex; #if BUILDING_DYLD if ( state.config.log.segments ) result->logSegmentsFromSharedCache(state); if ( state.config.log.libraries ) result->logLoad(state, loadPath); #endif return result; } #if !SUPPORT_CREATING_PREMAPPEDLOADERS Loader* JustInTimeLoader::makeJustInTimeLoaderDisk(Diagnostics& diag, RuntimeState& state, const char* loadPath, const LoadOptions& options, bool overridesCache, uint32_t overridesCacheIndex, const mach_o::Layout* layout) { __block Loader* result = nullptr; bool checkIfOSBinary = state.config.process.archs->checksOSBinary(); state.config.syscall.withReadOnlyMappedFile(diag, loadPath, checkIfOSBinary, ^(const void* mapping, size_t mappedSize, bool isOSBinary, const FileID& fileID, const char* canonicalPath) { uint64_t sliceSize = 0; if ( const MachOFile* mf = MachOFile::compatibleSlice(diag, sliceSize, mapping, mappedSize, loadPath, state.config.process.platform, isOSBinary, *state.config.process.archs, state.config.security.internalInstall) ) { // verify the filetype is loadable in this context if ( mf->isDylib() ) { if ( !options.canBeDylib ) { diag.error("cannot load dylib '%s'", loadPath); return; } } else if ( mf->isBundle() ) { if ( !options.canBeBundle ) { diag.error("cannot link against bundle '%s'", loadPath); return; } } else if ( mf->isMainExecutable() ) { if ( !options.canBeExecutable ) { if ( options.staticLinkage ) diag.error("cannot link against a main executable '%s'", loadPath); else diag.error("cannot dlopen a main executable '%s'", loadPath); return; } } else { diag.error("unloadable mach-o file type %d '%s'", mf->filetype, loadPath); return; } const MachOAnalyzer* ma = (MachOAnalyzer*)mf; #if !BUILDING_CACHE_BUILDER #if 0 if ( !ma->validMachOForArchAndPlatform(diag, mappedSize, loadPath, *state.config.process.archs, state.config.process.platform, isOSBinary, state.config.security.internalInstall) && ma->enforceFormat(dyld3::MachOAnalyzer::Malformed::sdkOnOrAfter2021)) { return; } #endif #endif bool leaveMapped = options.rtldNoDelete; bool neverUnload; #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS // The cache builder only builds executable and shared cache loaders, which are always never unloadable neverUnload = true; #else neverUnload = !options.forceUnloadable && (options.launching || ma->neverUnload()); #endif uint64_t vmSpace = ma->mappedSize(); FileValidationInfo fileValidation; fileValidation.checkInodeMtime = fileID.valid(); if ( fileValidation.checkInodeMtime ) { fileValidation.inode = fileID.inode(); fileValidation.mtime = fileID.mtime(); } fileValidation.sliceOffset = (uint8_t*)mf - (uint8_t*)mapping; JustInTimeLoader::withRegions(ma, ^(const Array<Region>& regions) { #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS // in cache builder, files are already mapped (void)vmSpace; result = JustInTimeLoader::make(state, ma, canonicalPath, FileID::none(), fileValidation.sliceOffset, neverUnload, leaveMapped, overridesCache, overridesCacheIndex, layout); #else CodeSignatureInFile codeSignature; bool hasCodeSignature = ma->hasCodeSignature(codeSignature.fileOffset, codeSignature.size); if ( const MachOAnalyzer* realMA = Loader::mapSegments(diag, state, canonicalPath, vmSpace, codeSignature, hasCodeSignature, regions, neverUnload, false, fileValidation) ) { result = JustInTimeLoader::make(state, realMA, canonicalPath, fileID, fileValidation.sliceOffset, neverUnload, leaveMapped, overridesCache, overridesCacheIndex, layout); #if BUILDING_DYLD if ( state.config.log.libraries ) result->logLoad(state, canonicalPath); #endif if ( options.rtldLocal ) ((JustInTimeLoader*)result)->hidden = true; } #endif }); } }); return result; } Loader* JustInTimeLoader::makeLaunchLoader(Diagnostics& diag, RuntimeState& state, const MachOAnalyzer* mainExe, const char* mainExePath, const mach_o::Layout* layout) { FileID mainFileID = FileID::none(); uint64_t mainSliceOffset = Loader::getOnDiskBinarySliceOffset(state, mainExe, mainExePath); #if !BUILDING_CACHE_BUILDER state.config.syscall.fileExists(mainExePath, &mainFileID); #endif // !BUILDING_CACHE_BUILDER return JustInTimeLoader::make(state, mainExe, mainExePath, mainFileID, mainSliceOffset, true, false, false, 0, layout); } #if BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS Loader* JustInTimeLoader::makeLaunchLoader(Diagnostics& diag, RuntimeState& state, const MachOFile* mainExe, const char* mainExePath, const mach_o::Layout* layout) { FileID mainFileID = FileID::none(); uint64_t mainSliceOffset = 0; // FIXME return JustInTimeLoader::make(state, mainExe, mainExePath, mainFileID, mainSliceOffset, true, false, false, 0, layout); } #endif // BUILDING_CACHE_BUILDER || BUILDING_CACHE_BUILDER_UNIT_TESTS #endif // !SUPPORT_CREATING_PREMAPPEDLOADERS const Loader* JustInTimeLoader::makePseudoDylibLoader(Diagnostics& diag, RuntimeState &state, const char* path, const LoadOptions& options, const PseudoDylib* pd) { const MachOFile* pseudoDylibMF = (const MachOFile*)pd->getAddress(); FileID fileID = FileID::none(); if (!pseudoDylibMF->loadableIntoProcess(state.config.process.platform, path)) { diag.error("wrong platform to load into process"); return nullptr; } JustInTimeLoader* result = JustInTimeLoader::make(state, pseudoDylibMF, path, fileID, 0, false, false, false, 0, nullptr); result->pd = pd; return result; } } // namespace |