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 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2015 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@ */ #if __arm__ || __arm64__ #include <System/sys/mman.h> #else #include <sys/mman.h> #endif #include <string.h> #include <dlfcn.h> #include <fcntl.h> #include <errno.h> #include <sys/types.h> #include <sys/fcntl.h> #include <sys/stat.h> #include <sys/param.h> #include <mach/mach.h> #include <mach/thread_status.h> #include <mach-o/loader.h> #include <mach-o/dyld_images.h> #include <libkern/OSAtomic.h> #include "ImageLoaderMegaDylib.h" #include "ImageLoaderMachO.h" #include "dyldLibSystemInterface.h" #include "Tracing.h" #include "dyld2.h" // from dyld_gdb.cpp extern void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[]); extern "C" int _dyld_func_lookup(const char* name, void** address); #ifndef EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE #define EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE 0x02 #endif // relocation_info.r_length field has value 3 for 64-bit executables and value 2 for 32-bit executables #if __LP64__ #define RELOC_SIZE 3 #define LC_SEGMENT_COMMAND LC_SEGMENT_64 #define LC_ROUTINES_COMMAND LC_ROUTINES_64 struct macho_segment_command : public segment_command_64 {}; struct macho_section : public section_64 {}; struct macho_routines_command : public routines_command_64 {}; #else #define RELOC_SIZE 2 #define LC_SEGMENT_COMMAND LC_SEGMENT #define LC_ROUTINES_COMMAND LC_ROUTINES struct macho_segment_command : public segment_command {}; struct macho_section : public section {}; struct macho_routines_command : public routines_command {}; #endif #if SUPPORT_ACCELERATE_TABLES ImageLoaderMegaDylib* ImageLoaderMegaDylib::makeImageLoaderMegaDylib(const dyld_cache_header* header, long slide, const macho_header* mainMH, const LinkContext& context) { return new ImageLoaderMegaDylib(header, slide, mainMH, context); } struct DATAdyld { void* dyldLazyBinder; // filled in at launch by dyld to point into dyld to &stub_binding_helper void* dyldFuncLookup; // filled in at launch by dyld to point into dyld to &_dyld_func_lookup ProgramVars vars; }; ImageLoaderMegaDylib::ImageLoaderMegaDylib(const dyld_cache_header* header, long slide, const macho_header* mainMH, const LinkContext& context) : ImageLoader("dyld shared cache", 0), _header(header), _linkEditBias(NULL), _slide(slide), _lockArray(NULL), _lockArrayInUseCount(0) { pthread_mutex_init(&_lockArrayGuard, NULL); const dyld_cache_mapping_info* mappings = (const dyld_cache_mapping_info*)((uint8_t*)_header + _header->mappingOffset); const dyld_cache_mapping_info* lastMapping = &mappings[_header->mappingCount-1]; const dyld_cache_accelerator_info* accHeader = (dyld_cache_accelerator_info*)(_header->accelerateInfoAddr + slide); for (const dyld_cache_mapping_info* m=mappings; m <= lastMapping; ++m) { if ( m->initProt == VM_PROT_READ ) { _linkEditBias = (uint8_t*)(m->address - m->fileOffset) + _slide; } } _endOfCacheInMemory = (void*)(lastMapping->address + lastMapping->size + slide); _images = (const dyld_cache_image_info*)((uint8_t*)_header + _header->imagesOffset); _imageExtras = (dyld_cache_image_info_extra*)((char*)accHeader + accHeader->imagesExtrasOffset); _initializers = (dyld_cache_accelerator_initializer*)((char*)accHeader + accHeader->initializersOffset); _reExportsArray = (uint16_t*)((char*)accHeader + accHeader->reExportListOffset); _dependenciesArray = (uint16_t*)((char*)accHeader + accHeader->depListOffset); _bottomUpArray = (uint16_t*)((char*)accHeader + accHeader->bottomUpListOffset); _rangeTable = (dyld_cache_range_entry*)((char*)accHeader + accHeader->rangeTableOffset); _rangeTableCount = accHeader->rangeTableCount; _imageCount = accHeader->imageExtrasCount; _stateFlags = (uint8_t*)calloc(_imageCount, 1); _initializerCount = accHeader->initializersCount; _dylibsTrieStart = (uint8_t*)accHeader + accHeader->dylibTrieOffset; _dylibsTrieEnd = _dylibsTrieStart + accHeader->dylibTrieSize; _imageTextInfo = (dyld_cache_image_text_info*)((uint8_t*)_header + _header->imagesTextOffset); DATAdyld* dyldSection = (DATAdyld*)(accHeader->dyldSectionAddr + slide); dyldSection->dyldLazyBinder = NULL; // not used by libdyld.dylib dyldSection->dyldFuncLookup = (void*)&_dyld_func_lookup; dyldSection->vars.mh = mainMH; context.setNewProgramVars(dyldSection->vars); } void ImageLoaderMegaDylib::unreachable() const { abort(); } ImageLoaderMegaDylib::~ImageLoaderMegaDylib() { } const char* ImageLoaderMegaDylib::getInstallPath() const { unreachable(); } const macho_header* ImageLoaderMegaDylib::getIndexedMachHeader(unsigned index) const { if ( index > _header->imagesCount ) dyld::throwf("cache image index out of range (%u), max=%u", index, _header->imagesCount - 1); return (const macho_header*)(_images[index].address + _slide); } const char* ImageLoaderMegaDylib::getIndexedPath(unsigned index) const { if ( index > _header->imagesCount ) dyld::throwf("cache image index out of range (%u), max=%u", index, _header->imagesCount - 1); return (char*)_header + _images[index].pathFileOffset; } const char* ImageLoaderMegaDylib::getIndexedShortName(unsigned index) const { const char* path = getIndexedPath(index); const char* lastSlash = strrchr(path, '/'); if ( lastSlash == NULL ) return path; else return lastSlash+1; } void ImageLoaderMegaDylib::getDylibUUID(unsigned int index, uuid_t uuid) const { if ( index > _header->imagesCount ) dyld::throwf("cache image index out of range (%u), max=%u", index, _header->imagesCount - 1); memcpy(uuid, _imageTextInfo[index].uuid, 16); } void ImageLoaderMegaDylib::printSegments(const macho_header* mh) const { const uint32_t cmd_count = mh->ncmds; const struct load_command* const cmds = (struct load_command*)((uint8_t*)mh + sizeof(macho_header)); const struct load_command* cmd = cmds; const macho_segment_command* seg; for (uint32_t i = 0; i < cmd_count; ++i) { switch (cmd->cmd) { case LC_SEGMENT_COMMAND: seg = (macho_segment_command*)cmd; dyld::log("%18s at 0x%08lX->0x%08lX\n", seg->segname, (long)(seg->vmaddr + _slide), (long)(seg->vmaddr + seg->vmsize + _slide)); break; } cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize); } } bool ImageLoaderMegaDylib::hasDylib(const char* path, unsigned* index) const { const uint8_t* imageNode = ImageLoader::trieWalk(_dylibsTrieStart, _dylibsTrieEnd, path); if ( imageNode == NULL ) { #if __MAC_OS_X_VERSION_MIN_REQUIRED // not all symlinks are recorded as aliases in accelerator tables if ( (strncmp(path, "/usr/lib/", 9) == 0) || (strncmp(path, "/System/Library/", 16) == 0) ) { char resolvedPath[PATH_MAX]; if ( realpath(path, resolvedPath) != NULL ) { imageNode = ImageLoader::trieWalk(_dylibsTrieStart, _dylibsTrieEnd, resolvedPath); } } if ( imageNode == NULL ) return false; #else return false; #endif } *index = (unsigned)read_uleb128(imageNode, _dylibsTrieEnd); return true; } bool ImageLoaderMegaDylib::addressInCache(const void* address, const mach_header** mh, const char** path, unsigned* index) { // quick out of bounds check #if __x86_64__ if ( (uintptr_t)address < 0x7FFF70000000LL ) return false; #else if ( address < (void*)_header ) return false; #endif if ( address > _endOfCacheInMemory ) return false; uint64_t unslidAddress = (uint64_t)address - _slide; // linear search for now const dyld_cache_range_entry* rangeTableEnd = &_rangeTable[_rangeTableCount]; for (const dyld_cache_range_entry* r = _rangeTable; r < rangeTableEnd; ++r) { if ( (r->startAddress <= unslidAddress) && (unslidAddress < r->startAddress+r->size) ) { *index = r->imageIndex; *mh = (mach_header*)getIndexedMachHeader(r->imageIndex); *path = getIndexedPath(r->imageIndex); return true; } } return false; } bool ImageLoaderMegaDylib::findUnwindSections(const void* address, dyld_unwind_sections* info) { const char* path; unsigned index; if ( addressInCache(address, &info->mh, &path, &index) ) { info->dwarf_section = NULL; info->dwarf_section_length = 0; ImageLoaderMachO::findSection(info->mh, "__TEXT", "__eh_frame", (void**)&info->dwarf_section, &info->dwarf_section_length); info->compact_unwind_section = NULL; info->compact_unwind_section_length = 0; ImageLoaderMachO::findSection(info->mh, "__TEXT", "__unwind_info", (void**)&info->compact_unwind_section, &info->compact_unwind_section_length); return true; } return false; } unsigned ImageLoaderMegaDylib::findImageIndex(const LinkContext& context, const char* path) const { unsigned index; if ( hasDylib(path, &index) ) return index; if ( strncmp(path, "@rpath/libswift", 15) == 0 ) { // <rdar://problem/51352017> a stable swift app built to run on pre-iOS-12.2 will use @rpath to reference swift dylibs in OS const char* trailingPath = &path[7]; char possiblePath[strlen(trailingPath)+16]; strcpy(possiblePath, "/usr/lib/swift/"); strcat(possiblePath, trailingPath); if ( hasDylib(possiblePath, &index) ) return index; } else if ( strncmp(path, "@rpath/", 7) == 0 ) { // <rdar://problem/26934069> Somehow we found the dylib in the cache, but it is not this literal string, try simple expansions of @rpath std::vector<const char*> rpathsFromMainExecutable; context.mainExecutable->getRPaths(context, rpathsFromMainExecutable); rpathsFromMainExecutable.push_back("/System/Library/Frameworks/"); rpathsFromMainExecutable.push_back("/usr/lib/swift/"); const char* trailingPath = &path[7]; for (const char* anRPath : rpathsFromMainExecutable) { if ( anRPath[0] != '/' ) continue; char possiblePath[strlen(anRPath) + strlen(trailingPath)+2]; strcpy(possiblePath, anRPath); if ( possiblePath[strlen(possiblePath)-1] != '/' ) strcat(possiblePath, "/"); strcat(possiblePath, trailingPath); if ( hasDylib(possiblePath, &index) ) { return index; } } } else { // handle symlinks embedded in load commands char resolvedPath[PATH_MAX]; realpath(path, resolvedPath); int realpathErrno = errno; // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT if ( (realpathErrno == ENOENT) || (realpathErrno == 0) ) { if ( strcmp(resolvedPath, path) != 0 ) return findImageIndex(context, resolvedPath); } } dyld::throwf("no cache image with name (%s)", path); } void ImageLoaderMegaDylib::initializeCoalIterator(CoalIterator& it, unsigned int loadOrder, unsigned imageIndex) { it.image = this; it.symbolName = " "; it.loadOrder = loadOrder; it.weakSymbol = false; it.symbolMatches = false; it.done = false; it.curIndex = 0; it.endIndex = _imageExtras[imageIndex].weakBindingsSize; it.address = 0; it.type = 0; it.addend = 0; it.imageIndex = imageIndex; } bool ImageLoaderMegaDylib::incrementCoalIterator(CoalIterator& it) { if ( it.done ) return false; if ( _imageExtras[it.imageIndex].weakBindingsSize == 0 ) { /// hmmm, ld set MH_WEAK_DEFINES or MH_BINDS_TO_WEAK, but there is no weak binding info it.done = true; it.symbolName = "~~~"; return true; } const uint8_t* start = (uint8_t*)(_imageExtras[it.imageIndex].weakBindingsAddr + _slide); const uint8_t* end = (uint8_t*)(_imageExtras[it.imageIndex].weakBindingsAddr + _slide + _imageExtras[it.imageIndex].weakBindingsSize); const uint8_t* p = start + it.curIndex; uintptr_t count; uintptr_t skip; uint64_t segOffset; unsigned segIndex; const mach_header* mh; while ( p < end ) { uint8_t immediate = *p & BIND_IMMEDIATE_MASK; uint8_t opcode = *p & BIND_OPCODE_MASK; ++p; switch (opcode) { case BIND_OPCODE_DONE: it.done = true; it.curIndex = p - start; it.symbolName = "~~~"; // sorts to end return true; case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: it.symbolName = (char*)p; it.weakSymbol = ((immediate & BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) == 0); it.symbolMatches = false; while (*p != '\0') ++p; ++p; it.curIndex = p - start; return false; case BIND_OPCODE_SET_TYPE_IMM: it.type = immediate; break; case BIND_OPCODE_SET_ADDEND_SLEB: it.addend = read_sleb128(p, end); break; case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: segIndex = immediate; segOffset = read_uleb128(p, end); mh = (mach_header*)getIndexedMachHeader((unsigned)it.imageIndex); if ( uintptr_t segPrefAddress = ImageLoaderMachO::segPreferredAddress(mh, segIndex) ) it.address = segPrefAddress + (uintptr_t)segOffset + _slide; else dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large", segIndex); break; case BIND_OPCODE_ADD_ADDR_ULEB: it.address += read_uleb128(p, end); break; case BIND_OPCODE_DO_BIND: it.address += sizeof(intptr_t); break; case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: it.address += read_uleb128(p, end) + sizeof(intptr_t); break; case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: it.address += immediate*sizeof(intptr_t) + sizeof(intptr_t); break; case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: count = read_uleb128(p, end); skip = read_uleb128(p, end); for (uint32_t i=0; i < count; ++i) { it.address += skip + sizeof(intptr_t); } break; default: dyld::throwf("bad weak bind opcode '%d' found after processing %d bytes in '%s'", *p, (int)(p-start), this->getPath()); } } /// hmmm, BIND_OPCODE_DONE is missing... it.done = true; it.symbolName = "~~~"; //dyld::log("missing BIND_OPCODE_DONE for image %s\n", this->getPath()); return true; } uintptr_t ImageLoaderMegaDylib::getAddressCoalIterator(CoalIterator& it, const LinkContext& context) { //dyld::log("looking for %s in %s\n", it.symbolName, this->getPath()); uintptr_t address; if ( findInChainedTries(context, it.symbolName, (unsigned)it.imageIndex, NULL, false, &address) ) { return address; } return 0; } void ImageLoaderMegaDylib::updateUsesCoalIterator(CoalIterator& it, uintptr_t value, ImageLoader* targetImage, unsigned targetIndex, const LinkContext& context) { const uint8_t* start = (uint8_t*)(_imageExtras[it.imageIndex].weakBindingsAddr + _slide); const uint8_t* end = (uint8_t*)(_imageExtras[it.imageIndex].weakBindingsAddr + _slide + _imageExtras[it.imageIndex].weakBindingsSize); const uint8_t* p = start + it.curIndex; uint8_t type = it.type; uintptr_t address = it.address; const char* symbolName = it.symbolName; intptr_t addend = it.addend; uint64_t segOffset; unsigned segIndex; const mach_header* mh; uintptr_t count; uintptr_t skip; bool done = false; bool boundSomething = false; const char* targetImagePath = targetImage ? targetImage->getIndexedPath(targetIndex) : NULL; while ( !done && (p < end) ) { uint8_t immediate = *p & BIND_IMMEDIATE_MASK; uint8_t opcode = *p & BIND_OPCODE_MASK; ++p; switch (opcode) { case BIND_OPCODE_DONE: done = true; break; case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: done = true; break; case BIND_OPCODE_SET_TYPE_IMM: type = immediate; break; case BIND_OPCODE_SET_ADDEND_SLEB: addend = read_sleb128(p, end); break; case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: segIndex = immediate; segOffset = read_uleb128(p, end); mh = (mach_header*)getIndexedMachHeader((unsigned)it.imageIndex); if ( uintptr_t segPrefAddress = ImageLoaderMachO::segPreferredAddress(mh, segIndex) ) address = segPrefAddress + (uintptr_t)segOffset + _slide; else dyld::throwf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment %d which is too large", segIndex); break; case BIND_OPCODE_ADD_ADDR_ULEB: address += read_uleb128(p, end); break; case BIND_OPCODE_DO_BIND: ImageLoaderMachO::bindLocation(context, 0, address, value, type, symbolName, addend, getIndexedPath((unsigned)it.imageIndex), targetImagePath, "weak ", NULL, _slide); boundSomething = true; address += sizeof(intptr_t); break; case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: ImageLoaderMachO::bindLocation(context, 0, address, value, type, symbolName, addend, getIndexedPath((unsigned)it.imageIndex), targetImagePath, "weak ", NULL, _slide); boundSomething = true; address += read_uleb128(p, end) + sizeof(intptr_t); break; case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: ImageLoaderMachO::bindLocation(context, 0, address, value, type, symbolName, addend, getIndexedPath((unsigned)it.imageIndex), targetImagePath, "weak ", NULL, _slide); boundSomething = true; address += immediate*sizeof(intptr_t) + sizeof(intptr_t); break; case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: count = read_uleb128(p, end); skip = read_uleb128(p, end); for (uint32_t i=0; i < count; ++i) { ImageLoaderMachO::bindLocation(context, 0, address, value, type, symbolName, addend, getIndexedPath((unsigned)it.imageIndex), targetImagePath, "weak ", NULL, _slide); boundSomething = true; address += skip + sizeof(intptr_t); } break; default: dyld::throwf("bad bind opcode %d in weak binding info", *p); } } // C++ weak coalescing cannot be tracked by reference counting. Error on side of never unloading. if ( boundSomething && (targetImage != this) ) context.addDynamicReference(this, targetImage); } void ImageLoaderMegaDylib::appendImagesNeedingCoalescing(ImageLoader* images[], unsigned imageIndexes[], unsigned& count) { for (unsigned i=0; i < _imageCount; ++i) { uint16_t index = _bottomUpArray[i]; if ( _stateFlags[index] == kStateUnused ) continue; if ( _imageExtras[index].weakBindingsSize == 0 ) continue; images[count] = this; imageIndexes[count] = index; ++count; } } bool ImageLoaderMegaDylib::weakSymbolsBound(unsigned index) const { return ( _stateFlags[index] >= kStateFlagWeakBound ); } void ImageLoaderMegaDylib::setWeakSymbolsBound(unsigned index) { if ( _stateFlags[index] == kStateFlagBound ) _stateFlags[index] = kStateFlagWeakBound; } void ImageLoaderMegaDylib::recursiveMarkLoaded(const LinkContext& context, unsigned imageIndex) { if ( _stateFlags[imageIndex] != kStateUnused ) return; const macho_header* mh = getIndexedMachHeader(imageIndex); const char* path = getIndexedPath(imageIndex); if ( context.verboseLoading ) dyld::log("dyld: loaded: %s\n", path); if ( context.verboseMapping ) { dyld::log("dyld: Using shared cached for %s\n", path); printSegments(mh); } // change state to "loaded" before recursing to break cycles _stateFlags[imageIndex] = kStateLoaded; ++fgImagesUsedFromSharedCache; dyld_image_info debuggerInfo; debuggerInfo.imageLoadAddress = (mach_header*)mh; debuggerInfo.imageFilePath = path; debuggerInfo.imageFileModDate = 0; addImagesToAllImages(1, &debuggerInfo); if ( _imageExtras[imageIndex].weakBindingsSize != 0 ) { ++fgImagesRequiringCoalescing; ++fgImagesHasWeakDefinitions; } unsigned startArrayIndex = _imageExtras[imageIndex].dependentsStartArrayIndex; for (int i=startArrayIndex; _dependenciesArray[i] != 0xFFFF; ++i) { unsigned subDep = (_dependenciesArray[i] & 0x7FFF); // mask off upward bit recursiveMarkLoaded(context, subDep); } } void ImageLoaderMegaDylib::recursiveLoadLibraries(const LinkContext& context, bool preflightOnly, const RPathChain& loaderRPaths, const char* loadPath) { unsigned index = findImageIndex(context, loadPath); recursiveMarkLoaded(context, index); } unsigned int ImageLoaderMegaDylib::recursiveUpdateDepth(unsigned int maxDepth) { setDepth(maxDepth); return maxDepth; } const ImageLoader::Symbol* ImageLoaderMegaDylib::findExportedSymbol(const char* name, bool searchReExports, const char* thisPath, const ImageLoader** foundIn) const { unsigned index; if ( !hasDylib(thisPath, &index) ) return NULL; const uint8_t* exportNode; const uint8_t* exportTrieEnd; unsigned foundinIndex; // <rdar://problem/22068598> always search re-exports // the point of searchReExports was to break cycles in dylibs, we don't have cycles in cache, so ok to search deep searchReExports = true; if ( searchReExports ) { if ( !exportTrieHasNodeRecursive(name, index, &exportNode, &exportTrieEnd, &foundinIndex) ) return NULL; } else { if ( !exportTrieHasNode(name, index, &exportNode, &exportTrieEnd) ) return NULL; } *foundIn = this; return (ImageLoader::Symbol*)exportNode; } bool ImageLoaderMegaDylib::exportTrieHasNode(const char* symbolName, unsigned index, const uint8_t** exportNode, const uint8_t** exportTrieEnd) const { const uint8_t* start = (uint8_t*)(_imageExtras[index].exportsTrieAddr + _slide); const uint32_t size = _imageExtras[index].exportsTrieSize; if ( size == 0 ) return false; const uint8_t* end = start + size; const uint8_t* node = ImageLoader::trieWalk(start, end, symbolName); if ( node == NULL ) return false; *exportNode = node; *exportTrieEnd = end; return true; } bool ImageLoaderMegaDylib::exportTrieHasNodeRecursive(const char* symbolName, unsigned index, const uint8_t** exportNode, const uint8_t** exportTrieEnd, unsigned* foundinIndex) const { // look in trie for image index if ( exportTrieHasNode(symbolName, index, exportNode, exportTrieEnd) ) { *foundinIndex = index; return true; } // recursively look in all re-exported tries unsigned startArrayIndex = _imageExtras[index].reExportsStartArrayIndex; for (int i=startArrayIndex; _reExportsArray[i] != 0xFFFF; ++i) { unsigned reExIndex = _reExportsArray[i]; if ( exportTrieHasNodeRecursive(symbolName, reExIndex, exportNode, exportTrieEnd, foundinIndex) ) return true; } return false; } bool ImageLoaderMegaDylib::findExportedSymbolAddress(const LinkContext& context, const char* symbolName, const ImageLoader* requestorImage, int requestorOrdinalOfDef, bool runResolver, const ImageLoader** foundIn, uintptr_t* address) const { const char* definedImagePath = requestorImage->libPath(requestorOrdinalOfDef-1); unsigned index = findImageIndex(context, definedImagePath); *foundIn = this; return findInChainedTries(context, symbolName, index, requestorImage, runResolver, address); } uintptr_t ImageLoaderMegaDylib::getExportedSymbolAddress(const Symbol* sym, const LinkContext& context, const ImageLoader* requestor, bool runResolver, const char* symbolName) const { // scan for with trie contains this node const uint8_t* exportTrieEnd = NULL; unsigned imageIndex = 0xFFFF; const macho_header* mh = NULL; uint64_t unslidTrieNode = ((uintptr_t)sym) - _slide; for (unsigned i=0; i < _imageCount; ++i) { uint64_t start = _imageExtras[i].exportsTrieAddr; uint64_t end = _imageExtras[i].exportsTrieAddr + _imageExtras[i].exportsTrieSize; if ( (start < unslidTrieNode) && (unslidTrieNode < end) ) { exportTrieEnd = (uint8_t*)(end + _slide); imageIndex = i; mh = (macho_header*)(_images[imageIndex].address + _slide); break; } } if ( mh == NULL ) dyld::throwf("getExportedSymbolAddress(Symbol=%p) not in a cache trie", sym); const uint8_t* exportNode = (const uint8_t*)sym; uintptr_t address; processExportNode(context, symbolName ? symbolName : "unknown", imageIndex, exportNode, exportTrieEnd, requestor, runResolver, &address); return address; } void ImageLoaderMegaDylib::processExportNode(const LinkContext& context, const char* symbolName, unsigned definedImageIndex, const uint8_t* exportNode, const uint8_t* exportTrieEnd, const ImageLoader* requestorImage, bool runResolver, uintptr_t* address) const { const macho_header* mh = getIndexedMachHeader(definedImageIndex); uintptr_t flags = read_uleb128(exportNode, exportTrieEnd); uintptr_t rawAddress; switch ( flags & EXPORT_SYMBOL_FLAGS_KIND_MASK ) { case EXPORT_SYMBOL_FLAGS_KIND_REGULAR: if ( runResolver && (flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) ) { // this node has a stub and resolver, run the resolver to get target address uintptr_t stub = read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)mh; // skip over stub // <rdar://problem/10657737> interposing dylibs have the stub address as their replacee uintptr_t interposedStub = interposedAddress(context, stub, requestorImage); if ( interposedStub != stub ) { *address = interposedStub; return; } // stub was not interposed, so run resolver typedef uintptr_t (*ResolverProc)(void); ResolverProc resolver = (ResolverProc)(read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)mh); *address = (*resolver)(); if ( context.verboseBind ) dyld::log("dyld: resolver at %p returned 0x%08lX\n", resolver, *address); return; } if ( flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) { // re-export from another dylib, lookup there const uintptr_t ordinal = read_uleb128(exportNode, exportTrieEnd); const char* importedName = (char*)exportNode; if ( importedName[0] == '\0' ) importedName = symbolName; unsigned startArrayIndex = _imageExtras[definedImageIndex].dependentsStartArrayIndex; unsigned reExImageIndex = _dependenciesArray[startArrayIndex + ordinal-1] & 0x7FFF; if ( findInChainedTries(context, importedName, reExImageIndex, requestorImage, runResolver, address) ) return; dyld::throwf("re-exported symbol '%s' not found for image %s expected re-exported in %s, node=%p", symbolName, getIndexedShortName(definedImageIndex), getIndexedShortName(reExImageIndex), exportNode); } rawAddress = read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)mh; *address = interposedAddress(context, rawAddress, requestorImage); return; case EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL: if ( flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER ) dyld::throwf("unsupported exported symbol kind. flags=%lu at node=%p", flags, exportNode); *address = read_uleb128(exportNode, exportTrieEnd) + (uintptr_t)mh; return; case EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE: if ( flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER ) dyld::throwf("unsupported exported symbol kind. flags=%lu at node=%p", flags, exportNode); *address = read_uleb128(exportNode, exportTrieEnd); return; default: dyld::throwf("unsupported exported symbol kind. flags=%lu at node=%p", flags, exportNode); } //dyld::throwf("unsupported exported symbol node=%p", exportNode); } bool ImageLoaderMegaDylib::findInChainedTries(const LinkContext& context, const char* symbolName, unsigned definedImageIndex, const ImageLoader* requestorImage, bool runResolver, uintptr_t* address) const { //dyld::log("findInChainedTries(sym=%s, index=%u, path=%s)\n", symbolName, definedImageIndex, getIndexedPath(definedImageIndex)); const uint8_t* exportNode; const uint8_t* exportTrieEnd; unsigned foundinIndex; if ( !exportTrieHasNodeRecursive(symbolName, definedImageIndex, &exportNode, &exportTrieEnd, &foundinIndex) ) return false; processExportNode(context, symbolName, foundinIndex, exportNode, exportTrieEnd, requestorImage, runResolver, address); return true; } bool ImageLoaderMegaDylib::findInChainedTriesAndDependentsExcept(const LinkContext& context, const char* symbolName, unsigned imageIndex, const ImageLoader* requestorImage, bool runResolver, bool alreadyVisited[], uintptr_t* address) const { //dyld::log("findInChainedTriesAndDependentsExcept(sym=%s, index=%u, path=%s)\n", symbolName, imageIndex, getIndexedPath(imageIndex)); if ( alreadyVisited[imageIndex] ) return false; alreadyVisited[imageIndex] = true; if ( findInChainedTries(context, symbolName, imageIndex, requestorImage, runResolver, address) ) return true; unsigned startArrayIndex = _imageExtras[imageIndex].dependentsStartArrayIndex; for (int i=startArrayIndex; _dependenciesArray[i] != 0xFFFF; ++i) { // ignore upward links if ( (_dependenciesArray[i] & 0x8000) == 0 ) { unsigned depIndex = _dependenciesArray[i] & 0x7FFF; if ( _stateFlags[depIndex] != kStateFlagInitialized ) continue; if ( findInChainedTriesAndDependentsExcept(context, symbolName, depIndex, requestorImage, runResolver, alreadyVisited, address) ) return true; } } return false; } bool ImageLoaderMegaDylib::findInChainedTriesAndDependents(const LinkContext& context, const char* symbolName, unsigned definedImageIndex, const ImageLoader* requestorImage, bool runResolver, uintptr_t* address) const { //dyld::log("findInChainedTriesAndDependents(sym=%s, index=%u, path=%s)\n", symbolName, definedImageIndex, getIndexedPath(definedImageIndex)); if ( findInChainedTries(context, symbolName, definedImageIndex, requestorImage, runResolver, address) ) return true; bool alreadyVisited[_header->imagesCount]; bzero(alreadyVisited, sizeof(alreadyVisited)); return findInChainedTriesAndDependentsExcept(context, symbolName, definedImageIndex, requestorImage, runResolver, alreadyVisited, address); } bool ImageLoaderMegaDylib::flatFindSymbol(const char* name, bool onlyInCoalesced, const ImageLoader::Symbol** sym, const ImageLoader** image, ImageLoader::CoalesceNotifier notifier) { bool found = false; // check export trie of all in-use images for (unsigned i=0; i < _imageCount ; ++i) { uint16_t imageIndex = _bottomUpArray[i]; if ( _stateFlags[imageIndex] == kStateUnused ) continue; const macho_header* mh = getIndexedMachHeader(imageIndex); if ( onlyInCoalesced && (mh->flags & MH_WEAK_DEFINES) == 0 ) continue; const uint8_t* exportNode; const uint8_t* exportTrieEnd; if ( exportTrieHasNode(name, imageIndex, &exportNode, &exportTrieEnd) ) { if ( notifier ) notifier((Symbol*)exportNode, this, (mach_header*)getIndexedMachHeader(imageIndex)); if ( !found ) { *sym = (Symbol*)exportNode; *image = this; found = true; } if ( !onlyInCoalesced ) return true; } } return found; } void ImageLoaderMegaDylib::markAllbound(const LinkContext& context) { for (unsigned i=0; i < _imageCount; ++i) { uint16_t imageIndex = _bottomUpArray[i]; if ( _stateFlags[imageIndex] == kStateLoaded ) { _stateFlags[imageIndex] = kStateFlagBound; context.notifySingleFromCache(dyld_image_state_bound, (mach_header*)getIndexedMachHeader(imageIndex), getIndexedPath(imageIndex)); } } } void ImageLoaderMegaDylib::recursiveSpinLockAcquire(unsigned int imageIndex, mach_port_t thisThread) { pthread_mutex_lock(&_lockArrayGuard); if ( _lockArray == NULL ) _lockArray = (recursive_lock*)calloc(_imageCount, sizeof(recursive_lock)); _lockArrayInUseCount++; pthread_mutex_unlock(&_lockArrayGuard); recursive_lock* imagesLock = &_lockArray[imageIndex]; while ( !OSAtomicCompareAndSwap32Barrier(0, thisThread, (int*)&imagesLock->thread) ) { if ( imagesLock->thread == thisThread ) break; } imagesLock->count++; } void ImageLoaderMegaDylib::recursiveSpinLockRelease(unsigned int imageIndex, mach_port_t thisThread) { recursive_lock* imagesLock = &_lockArray[imageIndex]; if ( --imagesLock->count == 0 ) imagesLock->thread = 0; pthread_mutex_lock(&_lockArrayGuard); _lockArrayInUseCount--; if ( _lockArrayInUseCount == 0 ) { free((void*)_lockArray); _lockArray = NULL; } pthread_mutex_unlock(&_lockArrayGuard); } void ImageLoaderMegaDylib::recursiveInitialization(const LinkContext& context, mach_port_t thisThread, unsigned int imageIndex, InitializerTimingList& timingInfo, UpwardIndexes& upInits) { // Don't do any locking until libSystem.dylib is initialized, so we can malloc() the lock array bool useLock = dyld::gProcessInfo->libSystemInitialized; if ( useLock ) recursiveSpinLockAcquire(imageIndex, thisThread); // only run initializers if currently in bound state if ( (_stateFlags[imageIndex] == kStateFlagBound) || (_stateFlags[imageIndex] == kStateFlagWeakBound) ) { // Each image in cache has its own lock. We only set the state to Initialized if we hold the lock for the image. _stateFlags[imageIndex] = kStateFlagInitialized; // first recursively init all dependents unsigned startArrayIndex = _imageExtras[imageIndex].dependentsStartArrayIndex; for (int i=startArrayIndex; _dependenciesArray[i] != 0xFFFF; ++i) { unsigned subDepIndex = _dependenciesArray[i]; // ignore upward links if ( (subDepIndex & 0x8000) == 0 ) recursiveInitialization(context, thisThread, subDepIndex, timingInfo, upInits); else upInits.images[upInits.count++] = (subDepIndex & 0x7FFF); } // notify objc about this image context.notifySingleFromCache(dyld_image_state_dependents_initialized, (mach_header*)getIndexedMachHeader(imageIndex), getIndexedPath(imageIndex)); // run all initializers for imageIndex const dyld_cache_accelerator_initializer* pInitStart = _initializers; const dyld_cache_accelerator_initializer* pInitEnd = &pInitStart[_initializerCount]; bool ranSomeInitializers = false; uint64_t t1 = mach_absolute_time(); for (const dyld_cache_accelerator_initializer* p=pInitStart; p < pInitEnd; ++p) { if ( p->imageIndex == imageIndex ) { Initializer func = (Initializer)(p->functionOffset + (uintptr_t)_header); if ( context.verboseInit ) dyld::log("dyld: calling initializer function %p in %s\n", func, getIndexedPath(imageIndex)); bool haveLibSystemHelpersBefore = (dyld::gLibSystemHelpers != NULL); { dyld3::ScopedTimer timer(DBG_DYLD_TIMING_STATIC_INITIALIZER, (uint64_t)getIndexedMachHeader(imageIndex), (uint64_t)func, 0); func(context.argc, context.argv, context.envp, context.apple, &context.programVars); }; bool haveLibSystemHelpersAfter = (dyld::gLibSystemHelpers != NULL); ranSomeInitializers = true; if ( !haveLibSystemHelpersBefore && haveLibSystemHelpersAfter ) { // now safe to use malloc() and other calls in libSystem.dylib dyld::gProcessInfo->libSystemInitialized = true; } } } if ( ranSomeInitializers ) { uint64_t t2 = mach_absolute_time(); const char* shortName = strrchr(getIndexedPath(imageIndex), '/'); if ( shortName == NULL ) shortName = getIndexedPath(imageIndex); else ++shortName; timingInfo.images[timingInfo.count].shortName = shortName; timingInfo.images[timingInfo.count].initTime = (t2-t1); timingInfo.count++; } } // only unlock if this frame locked (note: libSystemInitialized changes after libSystem's initializer is run) if ( useLock ) recursiveSpinLockRelease(imageIndex, thisThread); } void ImageLoaderMegaDylib::recursiveInitialization(const LinkContext& context, mach_port_t thisThread, const char* pathToInitialize, InitializerTimingList& timingInfo, UninitedUpwards&) { UpwardIndexes upsBuffer[256]; UpwardIndexes& ups = upsBuffer[0]; ups.count = 0; unsigned imageIndex = findImageIndex(context, pathToInitialize); this->recursiveInitialization(context, thisThread, imageIndex, timingInfo, ups); for (int i=0; i < ups.count; ++i) { UpwardIndexes upsBuffer2[256]; UpwardIndexes& ignoreUp = upsBuffer2[0]; ignoreUp.count = 0; this->recursiveInitialization(context, thisThread, ups.images[i], timingInfo, ignoreUp); } } void ImageLoaderMegaDylib::recursiveBind(const LinkContext& context, bool forceLazysBound, bool neverUnload) { markAllbound(context); } uint8_t ImageLoaderMegaDylib::dyldStateToCacheState(dyld_image_states state) { switch (state) { case dyld_image_state_mapped: case dyld_image_state_dependents_mapped: return kStateLoaded; case dyld_image_state_bound: return kStateFlagBound; case dyld_image_state_initialized: return kStateFlagInitialized; case dyld_image_state_rebased: case dyld_image_state_dependents_initialized: case dyld_image_state_terminated: return kStateUnused; } return kStateUnused; } void ImageLoaderMegaDylib::recursiveApplyInterposing(const LinkContext& context) { if ( context.verboseInterposing ) dyld::log("dyld: interposing %lu tuples onto shared cache\n", fgInterposingTuples.size()); } unsigned ImageLoaderMegaDylib::appendImagesToNotify(dyld_image_states state, bool orLater, dyld_image_info* infos) { uint8_t targetCacheState = dyldStateToCacheState(state); if ( targetCacheState == kStateUnused ) return 0; unsigned usedCount = 0; for (int i=_imageCount-1; i >= 0; --i) { uint16_t index = _bottomUpArray[i]; uint8_t imageState = _stateFlags[index]; if ( imageState == kStateFlagWeakBound ) imageState = kStateFlagBound; if ( (imageState == targetCacheState) || (orLater && (imageState > targetCacheState)) ) { infos[usedCount].imageLoadAddress = (mach_header*)getIndexedMachHeader(index); infos[usedCount].imageFilePath = getIndexedPath(index); infos[usedCount].imageFileModDate = 0; ++usedCount; } } return usedCount; } bool ImageLoaderMegaDylib::dlopenFromCache(const LinkContext& context, const char* path, int mode, void** handle) { unsigned imageIndex; if ( !hasDylib(path, &imageIndex) ) { return false; } // RTLD_NOLOAD means return handle if already loaded, but don't now load it if ( mode & RTLD_NOLOAD ) { dyld::gLibSystemHelpers->releaseGlobalDyldLock(); if ( _stateFlags[imageIndex] == kStateUnused ) { *handle = NULL; return true; } } else { this->recursiveMarkLoaded(context, imageIndex); context.notifyBatch(dyld_image_state_dependents_mapped, false); this->markAllbound(context); context.notifyBatch(dyld_image_state_bound, false); this->weakBind(context); // <rdar://problem/25069046> Release dyld global lock before running initializers in dlopen() with customer cache dyld::gLibSystemHelpers->releaseGlobalDyldLock(); InitializerTimingList timingInfo[_initializerCount]; timingInfo[0].count = 0; mach_port_t thisThread = mach_thread_self(); UpwardIndexes upsBuffer[256]; // room for 511 dangling upward links UpwardIndexes& ups = upsBuffer[0]; ups.count = 0; this->recursiveInitialization(context, thisThread, imageIndex, timingInfo[0], ups); // make sure any upward linked dylibs were initialized for (int i=0; i < ups.count; ++i) { UpwardIndexes upsBuffer2[256]; UpwardIndexes& ignoreUp = upsBuffer2[0]; ignoreUp.count = 0; this->recursiveInitialization(context, thisThread, ups.images[i], timingInfo[0], ignoreUp); } mach_port_deallocate(mach_task_self(), thisThread); context.notifyBatch(dyld_image_state_initialized, false); } *handle = makeCacheHandle(imageIndex, mode); return true; } bool ImageLoaderMegaDylib::makeCacheHandle(const LinkContext& context, unsigned cacheIndex, int mode, void** result) { if ( cacheIndex >= _imageCount ) return false; *result = makeCacheHandle(cacheIndex, mode); return true; } void* ImageLoaderMegaDylib::makeCacheHandle(unsigned index, int mode) { uint8_t flags = ((mode & RTLD_FIRST) ? 1 : 0); #if __LP64__ return (void*)(uintptr_t)( 0xFFEEDDCC00000000LL | (index << 8) | flags); #else return (void*)(uintptr_t)( 0xF8000000 | (index << 8) | flags); #endif } bool ImageLoaderMegaDylib::isCacheHandle(void* handle, unsigned* index, uint8_t* flags) { #if __LP64__ if ( (((uintptr_t)handle) >> 32) == 0xFFEEDDCC ) { if ( index ) *index = (((uintptr_t)handle) >> 8) & 0xFFFF; if ( flags ) *flags = ((uintptr_t)handle) & 0xFF; return true; } #else if ( (((uintptr_t)handle) >> 24) == 0xF8 ) { if ( index ) *index = (((uintptr_t)handle) >> 8) & 0xFFFF; if ( flags ) *flags = ((uintptr_t)handle) & 0xFF; return true; } #endif return false; } void* ImageLoaderMegaDylib::dlsymFromCache(const LinkContext& context, void* handle, const char* symbolName, unsigned imageIndex) { unsigned indexInHandle; uint8_t flags; uintptr_t symAddress; if ( handle == RTLD_SELF ) { if ( findInChainedTriesAndDependents(context, symbolName, imageIndex, NULL, true, &symAddress) ) return (void*)symAddress; } else if ( handle == RTLD_NEXT ) { // FIXME: really need to not look in imageIndex, but look in others. if ( findInChainedTriesAndDependents(context, symbolName, imageIndex, NULL, true, &symAddress) ) return (void*)symAddress; } else if ( isCacheHandle(handle, &indexInHandle, &flags) ) { bool searchOnlyFirst = (flags & 1); // RTLD_FIRST // normal dlsym(handle,) semantics is that the handle is just the first place to search. RTLD_FIRST disables that if ( searchOnlyFirst ) { if ( findInChainedTries(context, symbolName, indexInHandle, NULL, true, &symAddress) ) return (void*)symAddress; } else { if ( findInChainedTriesAndDependents(context, symbolName, indexInHandle, NULL, true, &symAddress) ) return (void*)symAddress; } } return NULL; } bool ImageLoaderMegaDylib::dladdrFromCache(const void* address, Dl_info* info) { const mach_header* mh; unsigned index; if ( !addressInCache(address, &mh, &info->dli_fname, &index) ) return false; info->dli_fbase = (void*)mh; if ( address == mh ) { // special case lookup of header info->dli_sname = "__dso_handle"; info->dli_saddr = info->dli_fbase; return true; } // find closest symbol in the image info->dli_sname = ImageLoaderMachO::findClosestSymbol(mh, address, (const void**)&info->dli_saddr); // never return the mach_header symbol if ( info->dli_saddr == info->dli_fbase ) { info->dli_sname = NULL; info->dli_saddr = NULL; return true; } // strip off leading underscore if ( info->dli_sname != NULL ) { if ( info->dli_sname[0] == '_' ) info->dli_sname = info->dli_sname +1; } return true; } uintptr_t ImageLoaderMegaDylib::bindLazy(uintptr_t lazyBindingInfoOffset, const LinkContext& context, const mach_header* mh, unsigned imageIndex) { const dyld_info_command* dyldInfoCmd = ImageLoaderMachO::findDyldInfoLoadCommand(mh); if ( dyldInfoCmd == NULL ) return 0; const uint8_t* const lazyInfoStart = &_linkEditBias[dyldInfoCmd->lazy_bind_off]; const uint8_t* const lazyInfoEnd = &lazyInfoStart[dyldInfoCmd->lazy_bind_size]; uint32_t lbOffset = (uint32_t)lazyBindingInfoOffset; uint8_t segIndex; uintptr_t segOffset; int libraryOrdinal; const char* symbolName; bool doneAfterBind; if ( ImageLoaderMachO::getLazyBindingInfo(lbOffset, lazyInfoStart, lazyInfoEnd, &segIndex, &segOffset, &libraryOrdinal, &symbolName, &doneAfterBind) ) { //const char* thisPath = getIndexedPath(imageIndex); //dyld::log("%s needs symbol '%s' from ordinal=%d\n", thisPath, symbolName, libraryOrdinal); unsigned startDepArrayIndex = _imageExtras[imageIndex].dependentsStartArrayIndex; unsigned targetIndex; if ( libraryOrdinal == BIND_SPECIAL_DYLIB_SELF ) targetIndex = imageIndex; else targetIndex = _dependenciesArray[startDepArrayIndex+libraryOrdinal-1] & 0x7FFF; //const char* targetPath = getIndexedPath(targetIndex); //dyld::log("%s needs symbol '%s' from %s\n", thisPath, symbolName, targetPath); uintptr_t targetAddress; if ( findInChainedTries(context, symbolName, targetIndex, this, true, &targetAddress) ) { if ( uintptr_t segPrefAddress = ImageLoaderMachO::segPreferredAddress(mh, segIndex) ) { uintptr_t* lp = (uintptr_t*)(segPrefAddress + segOffset + _slide); //dyld::log(" storing 0x%0lX to lp %p\n", targetAddress, lp); *lp = targetAddress; return targetAddress; } } } return 0; } #endif // SUPPORT_ACCELERATE_TABLES |