1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- 2 * 3 * Copyright (c) 2004-2012 Apple Inc. All rights reserved. 4 * 5 * @APPLE_LICENSE_HEADER_START@ 6 * 7 * This file contains Original Code and/or Modifications of Original Code 8 * as defined in and that are subject to the Apple Public Source License 9 * Version 2.0 (the 'License'). You may not use this file except in 10 * compliance with the License. Please obtain a copy of the License at 11 * http://www.opensource.apple.com/apsl/ and read it before using this 12 * file. 13 * 14 * The Original Code and all software distributed under the License are 15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 19 * Please see the License for the specific language governing rights and 20 * limitations under the License. 21 * 22 * @APPLE_LICENSE_HEADER_END@ 23 */ 24 25#include <stddef.h> 26#include <string.h> 27#include <malloc/malloc.h> 28#include <sys/mman.h> 29 30#include <crt_externs.h> 31#include <Availability.h> 32#include <vproc_priv.h> 33 34#include "mach-o/dyld.h" 35#include "mach-o/dyld_priv.h" 36 37#include "ImageLoader.h" 38#include "dyldLock.h" 39#include "start_glue.h" 40 41extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso); 42extern "C" void __cxa_finalize(const void *dso); 43extern "C" void __cxa_finalize_ranges(const struct __cxa_range_t ranges[], int count); 44 45 46#ifndef LC_VERSION_MIN_MACOSX 47 #define LC_VERSION_MIN_MACOSX 0x24 48 struct version_min_command { 49 uint32_t cmd; /* LC_VERSION_MIN_MACOSX or 50 LC_VERSION_MIN_IPHONEOS */ 51 uint32_t cmdsize; /* sizeof(struct min_version_command) */ 52 uint32_t version; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */ 53 uint32_t sdk; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */ 54 }; 55#endif 56 57#ifndef LC_VERSION_MIN_IPHONEOS 58 #define LC_VERSION_MIN_IPHONEOS 0x25 59#endif 60 61 62#ifndef LC_LOAD_UPWARD_DYLIB 63 #define LC_LOAD_UPWARD_DYLIB (0x23|LC_REQ_DYLD) /* load of dylib whose initializers run later */ 64#endif 65 66 67// deprecated APIs are still availble on Mac OS X, but not on iPhone OS 68#if __IPHONE_OS_VERSION_MIN_REQUIRED 69 #define DEPRECATED_APIS_SUPPORTED 0 70#else 71 #define DEPRECATED_APIS_SUPPORTED 1 72#endif 73 74/* 75 * names_match() takes an install_name from an LC_LOAD_DYLIB command and a 76 * libraryName (which is -lx or -framework Foo argument passed to the static 77 * link editor for the same library) and determines if they match. This depends 78 * on conventional use of names including major versioning. 79 */ 80static 81bool 82names_match( 83const char *install_name, 84const char* libraryName) 85{ 86 const char *basename; 87 unsigned long n; 88 89 /* 90 * Conventional install names have these forms: 91 * /System/Library/Frameworks/AppKit.framework/Versions/A/Appkit 92 * /Local/Library/Frameworks/AppKit.framework/Appkit 93 * /lib/libsys_s.A.dylib 94 * /usr/lib/libsys_s.dylib 95 */ 96 basename = strrchr(install_name, '/'); 97 if(basename == NULL) 98 basename = install_name; 99 else 100 basename++; 101 102 /* 103 * By checking the base name matching the library name we take care 104 * of the -framework cases. 105 */ 106 if(strcmp(basename, libraryName) == 0) 107 return true; 108 109 /* 110 * Now check the base name for "lib" if so proceed to check for the 111 * -lx case dealing with a possible .X.dylib and a .dylib extension. 112 */ 113 if(strncmp(basename, "lib", 3) ==0){ 114 n = strlen(libraryName); 115 if(strncmp(basename+3, libraryName, n) == 0){ 116 if(strncmp(basename+3+n, ".dylib", 6) == 0) 117 return true; 118 if(basename[3+n] == '.' && 119 basename[3+n+1] != '\0' && 120 strncmp(basename+3+n+2, ".dylib", 6) == 0) 121 return true; 122 } 123 } 124 return false; 125} 126 127#if DEPRECATED_APIS_SUPPORTED 128 129void NSInstallLinkEditErrorHandlers( 130const NSLinkEditErrorHandlers* handlers) 131{ 132 DYLD_LOCK_THIS_BLOCK; 133 typedef void (*ucallback_t)(const char* symbol_name); 134 typedef NSModule (*mcallback_t)(NSSymbol s, NSModule old, NSModule newhandler); 135 typedef void (*lcallback_t)(NSLinkEditErrors c, int errorNumber, 136 const char* fileName, const char* errorString); 137 static void (*p)(ucallback_t undefined, mcallback_t multiple, lcallback_t linkEdit) = NULL; 138 139 if(p == NULL) 140 _dyld_func_lookup("__dyld_install_handlers", (void**)&p); 141 mcallback_t m = handlers->multiple; 142 p(handlers->undefined, m, handlers->linkEdit); 143} 144 145const char* 146NSNameOfModule( 147NSModule module) 148{ 149 DYLD_LOCK_THIS_BLOCK; 150 static const char* (*p)(NSModule module) = NULL; 151 152 if(p == NULL) 153 _dyld_func_lookup("__dyld_NSNameOfModule", (void**)&p); 154 return(p(module)); 155} 156 157const char* 158NSLibraryNameForModule( 159NSModule module) 160{ 161 DYLD_LOCK_THIS_BLOCK; 162 static const char* (*p)(NSModule module) = NULL; 163 164 if(p == NULL) 165 _dyld_func_lookup("__dyld_NSLibraryNameForModule", (void**)&p); 166 return(p(module)); 167} 168 169bool 170NSIsSymbolNameDefined( 171const char* symbolName) 172{ 173 DYLD_LOCK_THIS_BLOCK; 174 static bool (*p)(const char* symbolName) = NULL; 175 176 if(p == NULL) 177 _dyld_func_lookup("__dyld_NSIsSymbolNameDefined", (void**)&p); 178 return(p(symbolName)); 179} 180 181bool 182NSIsSymbolNameDefinedWithHint( 183const char* symbolName, 184const char* libraryNameHint) 185{ 186 DYLD_LOCK_THIS_BLOCK; 187 static bool (*p)(const char* symbolName, 188 const char* libraryNameHint) = NULL; 189 190 if(p == NULL) 191 _dyld_func_lookup("__dyld_NSIsSymbolNameDefinedWithHint", (void**)&p); 192 return(p(symbolName, libraryNameHint)); 193} 194 195bool 196NSIsSymbolNameDefinedInImage( 197const struct mach_header *image, 198const char* symbolName) 199{ 200 DYLD_LOCK_THIS_BLOCK; 201 static bool (*p)(const struct mach_header *image, 202 const char* symbolName) = NULL; 203 204 if(p == NULL) 205 _dyld_func_lookup("__dyld_NSIsSymbolNameDefinedInImage", (void**)&p); 206 return(p(image, symbolName)); 207} 208 209NSSymbol 210NSLookupAndBindSymbol( 211const char* symbolName) 212{ 213 DYLD_LOCK_THIS_BLOCK; 214 static NSSymbol (*p)(const char* symbolName) = NULL; 215 216 if(p == NULL) 217 _dyld_func_lookup("__dyld_NSLookupAndBindSymbol", (void**)&p); 218 return(p(symbolName)); 219} 220 221NSSymbol 222NSLookupAndBindSymbolWithHint( 223const char* symbolName, 224const char* libraryNameHint) 225{ 226 DYLD_LOCK_THIS_BLOCK; 227 static NSSymbol (*p)(const char* symbolName, 228 const char* libraryNameHint) = NULL; 229 230 if(p == NULL) 231 _dyld_func_lookup("__dyld_NSLookupAndBindSymbolWithHint", (void**)&p); 232 return(p(symbolName, libraryNameHint)); 233} 234 235NSSymbol 236NSLookupSymbolInModule( 237NSModule module, 238const char* symbolName) 239{ 240 DYLD_LOCK_THIS_BLOCK; 241 static NSSymbol (*p)(NSModule module, const char* symbolName) = NULL; 242 243 if(p == NULL) 244 _dyld_func_lookup("__dyld_NSLookupSymbolInModule", (void**)&p); 245 return(p(module, symbolName)); 246} 247 248NSSymbol 249NSLookupSymbolInImage( 250const struct mach_header *image, 251const char* symbolName, 252uint32_t options) 253{ 254 DYLD_LOCK_THIS_BLOCK; 255 static NSSymbol (*p)(const struct mach_header *image, 256 const char* symbolName, 257 uint32_t options) = NULL; 258 259 if(p == NULL) 260 _dyld_func_lookup("__dyld_NSLookupSymbolInImage", (void**)&p); 261 return(p(image, symbolName, options)); 262} 263 264const char* 265NSNameOfSymbol( 266NSSymbol symbol) 267{ 268 DYLD_LOCK_THIS_BLOCK; 269 static char * (*p)(NSSymbol symbol) = NULL; 270 271 if(p == NULL) 272 _dyld_func_lookup("__dyld_NSNameOfSymbol",(void**)&p); 273 return(p(symbol)); 274} 275 276void * 277NSAddressOfSymbol( 278NSSymbol symbol) 279{ 280 DYLD_LOCK_THIS_BLOCK; 281 static void * (*p)(NSSymbol symbol) = NULL; 282 283 if(p == NULL) 284 _dyld_func_lookup("__dyld_NSAddressOfSymbol", (void**)&p); 285 return(p(symbol)); 286} 287 288NSModule 289NSModuleForSymbol( 290NSSymbol symbol) 291{ 292 DYLD_LOCK_THIS_BLOCK; 293 static NSModule (*p)(NSSymbol symbol) = NULL; 294 295 if(p == NULL) 296 _dyld_func_lookup("__dyld_NSModuleForSymbol", (void**)&p); 297 return(p(symbol)); 298} 299 300bool 301NSAddLibrary( 302const char* pathName) 303{ 304 DYLD_LOCK_THIS_BLOCK; 305 static bool (*p)(const char* pathName) = NULL; 306 307 if(p == NULL) 308 _dyld_func_lookup("__dyld_NSAddLibrary", (void**)&p); 309 return(p(pathName)); 310} 311 312bool 313NSAddLibraryWithSearching( 314const char* pathName) 315{ 316 DYLD_LOCK_THIS_BLOCK; 317 static bool (*p)(const char* pathName) = NULL; 318 319 if(p == NULL) 320 _dyld_func_lookup("__dyld_NSAddLibraryWithSearching", (void**)&p); 321 return(p(pathName)); 322} 323 324const struct mach_header * 325NSAddImage( 326const char* image_name, 327uint32_t options) 328{ 329 DYLD_LOCK_THIS_BLOCK; 330 static const struct mach_header * (*p)(const char* image_name, 331 uint32_t options) = NULL; 332 333 if(p == NULL) 334 _dyld_func_lookup("__dyld_NSAddImage", (void**)&p); 335 return(p(image_name, options)); 336} 337#endif // DEPRECATED_APIS_SUPPORTED 338 339/* 340 * This routine returns the current version of the named shared library the 341 * executable it was built with. The libraryName parameter is the same as the 342 * -lx or -framework Foo argument passed to the static link editor when building 343 * the executable (with -lx it would be "x" and with -framework Foo it would be 344 * "Foo"). If this the executable was not built against the specified library 345 * it returns -1. It should be noted that if this only returns the value the 346 * current version of the named shared library the executable was built with 347 * and not a list of current versions that dependent libraries and bundles the 348 * program is using were built with. 349 */ 350int32_t NSVersionOfLinkTimeLibrary(const char* libraryName) 351{ 352 // Lazily call _NSGetMachExecuteHeader() and cache result 353#if __LP64__ 354 static mach_header_64* mh = NULL; 355#else 356 static mach_header* mh = NULL; 357#endif 358 if ( mh == NULL ) 359 mh = _NSGetMachExecuteHeader(); 360#if __LP64__ 361 const load_command* lc = (load_command*)((char*)mh + sizeof(mach_header_64)); 362#else 363 const load_command* lc = (load_command*)((char*)mh + sizeof(mach_header)); 364#endif 365 for(uint32_t i = 0; i < mh->ncmds; i++){ 366 switch ( lc->cmd ) { 367 case LC_LOAD_DYLIB: 368 case LC_LOAD_WEAK_DYLIB: 369 case LC_LOAD_UPWARD_DYLIB: 370 const dylib_command* dl = (dylib_command *)lc; 371 const char* install_name = (char*)dl + dl->dylib.name.offset; 372 if ( names_match(install_name, libraryName) ) 373 return dl->dylib.current_version; 374 break; 375 } 376 lc = (load_command*)((char*)lc + lc->cmdsize); 377 } 378 return (-1); 379} 380 381/* 382 * This routine returns the current version of the named shared library the 383 * program it is running against. The libraryName parameter is the same as 384 * would be static link editor using the -lx or -framework Foo flags (with -lx 385 * it would be "x" and with -framework Foo it would be "Foo"). If the program 386 * is not using the specified library it returns -1. 387 */ 388int32_t NSVersionOfRunTimeLibrary(const char* libraryName) 389{ 390 uint32_t n = _dyld_image_count(); 391 for(uint32_t i = 0; i < n; i++){ 392 const mach_header* mh = _dyld_get_image_header(i); 393 if ( mh == NULL ) 394 continue; 395 if ( mh->filetype != MH_DYLIB ) 396 continue; 397#if __LP64__ 398 const load_command* lc = (load_command*)((char*)mh + sizeof(mach_header_64)); 399#else 400 const load_command* lc = (load_command*)((char*)mh + sizeof(mach_header)); 401#endif 402 for(uint32_t j = 0; j < mh->ncmds; j++){ 403 if ( lc->cmd == LC_ID_DYLIB ) { 404 const dylib_command* dl = (dylib_command*)lc; 405 const char* install_name = (char *)dl + dl->dylib.name.offset; 406 if ( names_match(install_name, libraryName) ) 407 return dl->dylib.current_version; 408 } 409 lc = (load_command*)((char*)lc + lc->cmdsize); 410 } 411 } 412 return (-1); 413} 414 415#define PACKED_VERSION(major, minor, tiny) ((((major) & 0xffff) << 16) | (((minor) & 0xff) << 8) | ((tiny) & 0xff)) 416 417 418/* 419 * Returns the sdk version (encode as nibble XXXX.YY.ZZ) the 420 * specified binary was built against. 421 * 422 * First looks for LC_VERSION_MIN_MACOSX/LC_VERSION_MIN_IPHONEOS 423 * in binary and if sdk field is not zero, return that value. 424 * Otherwise, looks for the libSystem.B.dylib the binary linked 425 * against and uses a table to convert that to an sdk version. 426 */ 427uint32_t dyld_get_sdk_version(const mach_header* mh) 428{ 429 const load_command* startCmds = NULL; 430 if ( mh->magic == MH_MAGIC_64 ) 431 startCmds = (load_command*)((char *)mh + sizeof(mach_header_64)); 432 else if ( mh->magic == MH_MAGIC ) 433 startCmds = (load_command*)((char *)mh + sizeof(mach_header)); 434 else 435 return 0; // not a mach-o file, or wrong endianness 436 437 const load_command* const cmdsEnd = (load_command*)((char*)startCmds + mh->sizeofcmds); 438 const version_min_command* versCmd; 439 const dylib_command* dylibCmd; 440 const load_command* cmd = startCmds; 441 const char* dylibName; 442#if __IPHONE_OS_VERSION_MIN_REQUIRED 443 uint32_t foundationVers = 0; 444#else 445 uint32_t libSystemVers = 0; 446#endif 447 for(uint32_t i = 0; i < mh->ncmds; ++i) { 448 const load_command* nextCmd = (load_command*)((char *)cmd + cmd->cmdsize); 449 // <rdar://problem/14381579&16050962> sanity check size of command 450 if ( (cmd->cmdsize < 8) || (nextCmd > cmdsEnd) || (nextCmd < startCmds)) { 451 return 0; 452 } 453 switch ( cmd->cmd ) { 454#if __IPHONE_OS_VERSION_MIN_REQUIRED 455 case LC_VERSION_MIN_IPHONEOS: 456#else 457 case LC_VERSION_MIN_MACOSX: 458#endif 459 versCmd = (version_min_command*)cmd; 460#ifdef DICE_KIND_DATA 461 if ( versCmd->sdk != 0 ) 462 return versCmd->sdk; // found explicit SDK version 463#else 464 if ( versCmd->reserved != 0 ) 465 return versCmd->reserved; // found explicit SDK version 466#endif 467 break; 468 case LC_LOAD_DYLIB: 469 case LC_LOAD_WEAK_DYLIB: 470 case LC_LOAD_UPWARD_DYLIB: 471 dylibCmd = (dylib_command*)cmd; 472 // sanity check dylib command layout 473 if ( dylibCmd->dylib.name.offset > cmd->cmdsize ) 474 return 0; 475 dylibName = (char*)dylibCmd + dylibCmd->dylib.name.offset; 476#if __IPHONE_OS_VERSION_MIN_REQUIRED 477 if ( strcmp(dylibName, "/System/Library/Frameworks/Foundation.framework/Foundation") == 0 ) 478 foundationVers = dylibCmd->dylib.current_version; 479#else 480 if ( strcmp(dylibName, "/usr/lib/libSystem.B.dylib") == 0 ) 481 libSystemVers = dylibCmd->dylib.current_version; 482#endif 483 break; 484 } 485 cmd = nextCmd; 486 } 487 488 struct DylibToOSMapping { 489 uint32_t dylibVersion; 490 uint32_t osVersion; 491 }; 492 493#if __IPHONE_OS_VERSION_MIN_REQUIRED 494 static const DylibToOSMapping foundationMapping[] = { 495 { PACKED_VERSION(678,24,0), DYLD_IOS_VERSION_2_0 }, 496 { PACKED_VERSION(678,26,0), DYLD_IOS_VERSION_2_1 }, 497 { PACKED_VERSION(678,29,0), DYLD_IOS_VERSION_2_2 }, 498 { PACKED_VERSION(678,47,0), DYLD_IOS_VERSION_3_0 }, 499 { PACKED_VERSION(678,51,0), DYLD_IOS_VERSION_3_1 }, 500 { PACKED_VERSION(678,60,0), DYLD_IOS_VERSION_3_2 }, 501 { PACKED_VERSION(751,32,0), DYLD_IOS_VERSION_4_0 }, 502 { PACKED_VERSION(751,37,0), DYLD_IOS_VERSION_4_1 }, 503 { PACKED_VERSION(751,49,0), DYLD_IOS_VERSION_4_2 }, 504 { PACKED_VERSION(751,58,0), DYLD_IOS_VERSION_4_3 }, 505 { PACKED_VERSION(881,0,0), DYLD_IOS_VERSION_5_0 }, 506 { PACKED_VERSION(890,1,0), DYLD_IOS_VERSION_5_1 }, 507 { PACKED_VERSION(992,0,0), DYLD_IOS_VERSION_6_0 }, 508 { PACKED_VERSION(993,0,0), DYLD_IOS_VERSION_6_1 }, 509 { PACKED_VERSION(1038,14,0),DYLD_IOS_VERSION_7_0 }, // check final 510 { PACKED_VERSION(0,0,0), DYLD_IOS_VERSION_7_0 } 511 }; 512 513 if ( foundationVers != 0 ) { 514 uint32_t lastOsVersion = 0; 515 for (const DylibToOSMapping* p=foundationMapping; ; ++p) { 516 if ( p->dylibVersion == 0 ) 517 return p->osVersion; 518 if ( foundationVers < p->dylibVersion ) 519 return lastOsVersion; 520 lastOsVersion = p->osVersion; 521 } 522 } 523 524#else 525 // Note: versions are for the GM release. The last entry should 526 // always be zero. At the start of the next major version, 527 // a new last entry needs to be added and the previous zero 528 // updated to the GM dylib version. 529 static const DylibToOSMapping libSystemMapping[] = { 530 { PACKED_VERSION(88,1,3), DYLD_MACOSX_VERSION_10_4 }, 531 { PACKED_VERSION(111,0,0), DYLD_MACOSX_VERSION_10_5 }, 532 { PACKED_VERSION(123,0,0), DYLD_MACOSX_VERSION_10_6 }, 533 { PACKED_VERSION(159,0,0), DYLD_MACOSX_VERSION_10_7 }, 534 { PACKED_VERSION(169,3,0), DYLD_MACOSX_VERSION_10_8 }, 535 { PACKED_VERSION(1197,0,0), DYLD_MACOSX_VERSION_10_9 }, 536 { PACKED_VERSION(0,0,0), DYLD_MACOSX_VERSION_10_9 } 537 }; 538 539 if ( libSystemVers != 0 ) { 540 uint32_t lastOsVersion = 0; 541 for (const DylibToOSMapping* p=libSystemMapping; ; ++p) { 542 if ( p->dylibVersion == 0 ) 543 return p->osVersion; 544 if ( libSystemVers < p->dylibVersion ) 545 return lastOsVersion; 546 lastOsVersion = p->osVersion; 547 } 548 } 549#endif 550 551 return 0; 552} 553 554uint32_t dyld_get_program_sdk_version() 555{ 556 return dyld_get_sdk_version((mach_header*)_NSGetMachExecuteHeader()); 557} 558 559uint32_t dyld_get_min_os_version(const struct mach_header* mh) 560{ 561 const load_command* startCmds = NULL; 562 if ( mh->magic == MH_MAGIC_64 ) 563 startCmds = (load_command*)((char *)mh + sizeof(mach_header_64)); 564 else if ( mh->magic == MH_MAGIC ) 565 startCmds = (load_command*)((char *)mh + sizeof(mach_header)); 566 else 567 return 0; // not a mach-o file, or wrong endianness 568 569 const load_command* const cmdsEnd = (load_command*)((char*)startCmds + mh->sizeofcmds); 570 const version_min_command* versCmd; 571 const load_command* cmd = startCmds; 572 for(uint32_t i = 0; i < mh->ncmds; ++i) { 573 const load_command* nextCmd = (load_command*)((char *)cmd + cmd->cmdsize); 574 // <rdar://problem/14381579&16050962> sanity check size of command 575 if ( (cmd->cmdsize < 8) || (nextCmd > cmdsEnd) || (nextCmd < startCmds)) { 576 return 0; 577 } 578 switch ( cmd->cmd ) { 579#if __IPHONE_OS_VERSION_MIN_REQUIRED 580 case LC_VERSION_MIN_IPHONEOS: 581#else 582 case LC_VERSION_MIN_MACOSX: 583#endif 584 versCmd = (version_min_command*)cmd; 585 return versCmd->version; // found explicit min OS version 586 break; 587 } 588 cmd = nextCmd; 589 } 590 return 0; 591} 592 593 594uint32_t dyld_get_program_min_os_version() 595{ 596 return dyld_get_min_os_version((mach_header*)_NSGetMachExecuteHeader()); 597} 598 599 600#if DEPRECATED_APIS_SUPPORTED 601/* 602 * NSCreateObjectFileImageFromFile() creates an NSObjectFileImage for the 603 * specified file name if the file is a correct Mach-O file that can be loaded 604 * with NSloadModule(). For return codes of NSObjectFileImageFailure and 605 * NSObjectFileImageFormat an error message is printed to stderr. All 606 * other codes cause no printing. 607 */ 608NSObjectFileImageReturnCode 609NSCreateObjectFileImageFromFile( 610const char* pathName, 611NSObjectFileImage *objectFileImage) 612{ 613 DYLD_LOCK_THIS_BLOCK; 614 static NSObjectFileImageReturnCode (*p)(const char*, NSObjectFileImage*) = NULL; 615 616 if(p == NULL) 617 _dyld_func_lookup("__dyld_NSCreateObjectFileImageFromFile", (void**)&p); 618 return p(pathName, objectFileImage); 619} 620 621 622/* 623 * NSCreateObjectFileImageFromMemory() creates an NSObjectFileImage for the 624 * object file mapped into memory at address of size length if the object file 625 * is a correct Mach-O file that can be loaded with NSloadModule(). For return 626 * codes of NSObjectFileImageFailure and NSObjectFileImageFormat an error 627 * message is printed to stderr. All other codes cause no printing. 628 */ 629NSObjectFileImageReturnCode 630NSCreateObjectFileImageFromMemory( 631const void* address, 632size_t size, 633NSObjectFileImage *objectFileImage) 634{ 635 DYLD_LOCK_THIS_BLOCK; 636 static NSObjectFileImageReturnCode (*p)(const void*, size_t, NSObjectFileImage*) = NULL; 637 638 if(p == NULL) 639 _dyld_func_lookup("__dyld_NSCreateObjectFileImageFromMemory", (void**)&p); 640 return p(address, size, objectFileImage); 641} 642 643#if OBSOLETE_DYLD_API 644/* 645 * NSCreateCoreFileImageFromFile() creates an NSObjectFileImage for the 646 * specified core file name if the file is a correct Mach-O core file. 647 * For return codes of NSObjectFileImageFailure and NSObjectFileImageFormat 648 * an error message is printed to stderr. All other codes cause no printing. 649 */ 650NSObjectFileImageReturnCode 651NSCreateCoreFileImageFromFile( 652const char* pathName, 653NSObjectFileImage *objectFileImage) 654{ 655 DYLD_LOCK_THIS_BLOCK; 656 static NSObjectFileImageReturnCode (*p)(const char*, NSObjectFileImage*) = NULL; 657 658 if(p == NULL) 659 _dyld_func_lookup("__dyld_NSCreateCoreFileImageFromFile", (void**)&p); 660 return p(pathName, objectFileImage); 661} 662#endif 663 664bool 665NSDestroyObjectFileImage( 666NSObjectFileImage objectFileImage) 667{ 668 DYLD_LOCK_THIS_BLOCK; 669 static bool (*p)(NSObjectFileImage) = NULL; 670 671 if(p == NULL) 672 _dyld_func_lookup("__dyld_NSDestroyObjectFileImage", (void**)&p); 673 return p(objectFileImage); 674} 675 676 677NSModule 678NSLinkModule( 679NSObjectFileImage objectFileImage, 680const char* moduleName, 681uint32_t options) 682{ 683 DYLD_LOCK_THIS_BLOCK; 684 static NSModule (*p)(NSObjectFileImage, const char*, unsigned long) = NULL; 685 686 if(p == NULL) 687 _dyld_func_lookup("__dyld_NSLinkModule", (void**)&p); 688 689 return p(objectFileImage, moduleName, options); 690} 691 692 693 694 695/* 696 * NSSymbolDefinitionCountInObjectFileImage() returns the number of symbol 697 * definitions in the NSObjectFileImage. 698 */ 699uint32_t 700NSSymbolDefinitionCountInObjectFileImage( 701NSObjectFileImage objectFileImage) 702{ 703 DYLD_LOCK_THIS_BLOCK; 704 static unsigned long (*p)(NSObjectFileImage) = NULL; 705 706 if(p == NULL) 707 _dyld_func_lookup("__dyld_NSSymbolDefinitionCountInObjectFileImage", (void**)&p); 708 709 return p(objectFileImage); 710} 711 712/* 713 * NSSymbolDefinitionNameInObjectFileImage() returns the name of the i'th 714 * symbol definitions in the NSObjectFileImage. If the ordinal specified is 715 * outside the range [0..NSSymbolDefinitionCountInObjectFileImage], NULL will 716 * be returned. 717 */ 718const char* 719NSSymbolDefinitionNameInObjectFileImage( 720NSObjectFileImage objectFileImage, 721uint32_t ordinal) 722{ 723 DYLD_LOCK_THIS_BLOCK; 724 static const char* (*p)(NSObjectFileImage, uint32_t) = NULL; 725 726 if(p == NULL) 727 _dyld_func_lookup("__dyld_NSSymbolDefinitionNameInObjectFileImage", (void**)&p); 728 729 return p(objectFileImage, ordinal); 730} 731 732/* 733 * NSSymbolReferenceCountInObjectFileImage() returns the number of references 734 * to undefined symbols the NSObjectFileImage. 735 */ 736uint32_t 737NSSymbolReferenceCountInObjectFileImage( 738NSObjectFileImage objectFileImage) 739{ 740 DYLD_LOCK_THIS_BLOCK; 741 static unsigned long (*p)(NSObjectFileImage) = NULL; 742 743 if(p == NULL) 744 _dyld_func_lookup("__dyld_NSSymbolReferenceCountInObjectFileImage", (void**)&p); 745 746 return p(objectFileImage); 747} 748 749/* 750 * NSSymbolReferenceNameInObjectFileImage() returns the name of the i'th 751 * undefined symbol in the NSObjectFileImage. If the ordinal specified is 752 * outside the range [0..NSSymbolReferenceCountInObjectFileImage], NULL will be 753 * returned. 754 */ 755const char* 756NSSymbolReferenceNameInObjectFileImage( 757NSObjectFileImage objectFileImage, 758uint32_t ordinal, 759bool *tentative_definition) /* can be NULL */ 760{ 761 DYLD_LOCK_THIS_BLOCK; 762 static const char* (*p)(NSObjectFileImage, uint32_t, bool*) = NULL; 763 764 if(p == NULL) 765 _dyld_func_lookup("__dyld_NSSymbolReferenceNameInObjectFileImage", (void**)&p); 766 767 return p(objectFileImage, ordinal, tentative_definition); 768} 769 770/* 771 * NSIsSymbolDefinedInObjectFileImage() returns TRUE if the specified symbol 772 * name has a definition in the NSObjectFileImage and FALSE otherwise. 773 */ 774bool 775NSIsSymbolDefinedInObjectFileImage( 776NSObjectFileImage objectFileImage, 777const char* symbolName) 778{ 779 DYLD_LOCK_THIS_BLOCK; 780 static bool (*p)(NSObjectFileImage, const char*) = NULL; 781 782 if(p == NULL) 783 _dyld_func_lookup("__dyld_NSIsSymbolDefinedInObjectFileImage", (void**)&p); 784 785 return p(objectFileImage, symbolName); 786} 787 788/* 789 * NSGetSectionDataInObjectFileImage() returns a pointer to the section contents 790 * in the NSObjectFileImage for the specified segmentName and sectionName if 791 * it exists and it is not a zerofill section. If not it returns NULL. If 792 * the parameter size is not NULL the size of the section is also returned 793 * indirectly through that pointer. 794 */ 795void * 796NSGetSectionDataInObjectFileImage( 797NSObjectFileImage objectFileImage, 798const char* segmentName, 799const char* sectionName, 800unsigned long *size) /* can be NULL */ 801{ 802 DYLD_LOCK_THIS_BLOCK; 803 static void* (*p)(NSObjectFileImage, const char*, const char*, unsigned long*) = NULL; 804 805 if(p == NULL) 806 _dyld_func_lookup("__dyld_NSGetSectionDataInObjectFileImage", (void**)&p); 807 808 return p(objectFileImage, segmentName, sectionName, size); 809} 810 811 812void 813NSLinkEditError( 814NSLinkEditErrors *c, 815int *errorNumber, 816const char* *fileName, 817const char* *errorString) 818{ 819 DYLD_LOCK_THIS_BLOCK; 820 static void (*p)(NSLinkEditErrors *c, 821 int *errorNumber, 822 const char* *fileName, 823 const char* *errorString) = NULL; 824 825 if(p == NULL) 826 _dyld_func_lookup("__dyld_link_edit_error", (void**)&p); 827 if(p != NULL) 828 p(c, errorNumber, fileName, errorString); 829} 830 831bool 832NSUnLinkModule( 833NSModule module, 834uint32_t options) 835{ 836 DYLD_LOCK_THIS_BLOCK; 837 static bool (*p)(NSModule module, uint32_t options) = NULL; 838 839 if(p == NULL) 840 _dyld_func_lookup("__dyld_unlink_module", (void**)&p); 841 842 return p(module, options); 843} 844 845#if OBSOLETE_DYLD_API 846NSModule 847NSReplaceModule( 848NSModule moduleToReplace, 849NSObjectFileImage newObjectFileImage, 850uint32_t options) 851{ 852 return(NULL); 853} 854#endif 855 856 857#endif // DEPRECATED_APIS_SUPPORTED 858 859/* 860 *_NSGetExecutablePath copies the path of the executable into the buffer and 861 * returns 0 if the path was successfully copied in the provided buffer. If the 862 * buffer is not large enough, -1 is returned and the expected buffer size is 863 * copied in *bufsize. Note that _NSGetExecutablePath will return "a path" to 864 * the executable not a "real path" to the executable. That is the path may be 865 * a symbolic link and not the real file. And with deep directories the total 866 * bufsize needed could be more than MAXPATHLEN. 867 */ 868int 869_NSGetExecutablePath( 870char *buf, 871uint32_t *bufsize) 872{ 873 DYLD_LOCK_THIS_BLOCK; 874 static int (*p)(char *buf, uint32_t *bufsize) = NULL; 875 876 if(p == NULL) 877 _dyld_func_lookup("__dyld__NSGetExecutablePath", (void**)&p); 878 return(p(buf, bufsize)); 879} 880 881#if DEPRECATED_APIS_SUPPORTED 882void 883_dyld_lookup_and_bind( 884const char* symbol_name, 885void** address, 886NSModule* module) 887{ 888 DYLD_LOCK_THIS_BLOCK; 889 static void (*p)(const char*, void** , NSModule*) = NULL; 890 891 if(p == NULL) 892 _dyld_func_lookup("__dyld_lookup_and_bind", (void**)&p); 893 p(symbol_name, address, module); 894} 895 896void 897_dyld_lookup_and_bind_with_hint( 898const char* symbol_name, 899const char* library_name_hint, 900void** address, 901NSModule* module) 902{ 903 DYLD_LOCK_THIS_BLOCK; 904 static void (*p)(const char*, const char*, void**, NSModule*) = NULL; 905 906 if(p == NULL) 907 _dyld_func_lookup("__dyld_lookup_and_bind_with_hint", (void**)&p); 908 p(symbol_name, library_name_hint, address, module); 909} 910 911#if OBSOLETE_DYLD_API 912void 913_dyld_lookup_and_bind_objc( 914const char* symbol_name, 915void** address, 916NSModule* module) 917{ 918 DYLD_LOCK_THIS_BLOCK; 919 static void (*p)(const char* , void**, NSModule*) = NULL; 920 921 if(p == NULL) 922 _dyld_func_lookup("__dyld_lookup_and_bind_objc", (void**)&p); 923 p(symbol_name, address, module); 924} 925#endif 926 927void 928_dyld_lookup_and_bind_fully( 929const char* symbol_name, 930void** address, 931NSModule* module) 932{ 933 DYLD_LOCK_THIS_BLOCK; 934 static void (*p)(const char*, void**, NSModule*) = NULL; 935 936 if(p == NULL) 937 _dyld_func_lookup("__dyld_lookup_and_bind_fully", (void**)&p); 938 p(symbol_name, address, module); 939} 940 941bool 942_dyld_bind_fully_image_containing_address( 943const void* address) 944{ 945 DYLD_LOCK_THIS_BLOCK; 946 static bool (*p)(const void*) = NULL; 947 948 if(p == NULL) 949 _dyld_func_lookup("__dyld_bind_fully_image_containing_address", (void**)&p); 950 return p(address); 951} 952#endif // DEPRECATED_APIS_SUPPORTED 953 954 955/* 956 * _dyld_register_func_for_add_image registers the specified function to be 957 * called when a new image is added (a bundle or a dynamic shared library) to 958 * the program. When this function is first registered it is called for once 959 * for each image that is currently part of the program. 960 */ 961void 962_dyld_register_func_for_add_image( 963void (*func)(const struct mach_header *mh, intptr_t vmaddr_slide)) 964{ 965 DYLD_LOCK_THIS_BLOCK; 966 typedef void (*callback_t)(const struct mach_header *mh, intptr_t vmaddr_slide); 967 static void (*p)(callback_t func) = NULL; 968 969 if(p == NULL) 970 _dyld_func_lookup("__dyld_register_func_for_add_image", (void**)&p); 971 p(func); 972} 973 974/* 975 * _dyld_register_func_for_remove_image registers the specified function to be 976 * called when an image is removed (a bundle or a dynamic shared library) from 977 * the program. 978 */ 979void 980_dyld_register_func_for_remove_image( 981void (*func)(const struct mach_header *mh, intptr_t vmaddr_slide)) 982{ 983 DYLD_LOCK_THIS_BLOCK; 984 typedef void (*callback_t)(const struct mach_header *mh, intptr_t vmaddr_slide); 985 static void (*p)(callback_t func) = NULL; 986 987 if(p == NULL) 988 _dyld_func_lookup("__dyld_register_func_for_remove_image", (void**)&p); 989 p(func); 990} 991 992#if OBSOLETE_DYLD_API 993/* 994 * _dyld_register_func_for_link_module registers the specified function to be 995 * called when a module is bound into the program. When this function is first 996 * registered it is called for once for each module that is currently bound into 997 * the program. 998 */ 999void 1000_dyld_register_func_for_link_module( 1001void (*func)(NSModule module)) 1002{ 1003 DYLD_LOCK_THIS_BLOCK; 1004 static void (*p)(void (*func)(NSModule module)) = NULL; 1005 1006 if(p == NULL) 1007 _dyld_func_lookup("__dyld_register_func_for_link_module", (void**)&p); 1008 p(func); 1009} 1010 1011/* 1012 * _dyld_register_func_for_unlink_module registers the specified function to be 1013 * called when a module is unbound from the program. 1014 */ 1015void 1016_dyld_register_func_for_unlink_module( 1017void (*func)(NSModule module)) 1018{ 1019 DYLD_LOCK_THIS_BLOCK; 1020 static void (*p)(void (*func)(NSModule module)) = NULL; 1021 1022 if(p == NULL) 1023 _dyld_func_lookup("__dyld_register_func_for_unlink_module", (void**)&p); 1024 p(func); 1025} 1026 1027/* 1028 * _dyld_register_func_for_replace_module registers the specified function to be 1029 * called when a module is to be replace with another module in the program. 1030 */ 1031void 1032_dyld_register_func_for_replace_module( 1033void (*func)(NSModule oldmodule, NSModule newmodule)) 1034{ 1035 DYLD_LOCK_THIS_BLOCK; 1036 static void (*p)(void (*func)(NSModule oldmodule, 1037 NSModule newmodule)) = NULL; 1038 1039 if(p == NULL) 1040 _dyld_func_lookup("__dyld_register_func_for_replace_module", (void**)&p); 1041 p(func); 1042} 1043 1044 1045/* 1046 * _dyld_get_objc_module_sect_for_module is passed a module and sets a 1047 * pointer to the (__OBJC,__module) section and its size for the specified 1048 * module. 1049 */ 1050void 1051_dyld_get_objc_module_sect_for_module( 1052NSModule module, 1053void **objc_module, 1054unsigned long *size) 1055{ 1056 DYLD_LOCK_THIS_BLOCK; 1057 static void (*p)(NSModule module, 1058 void **objc_module, 1059 unsigned long *size) = NULL; 1060 1061 if(p == NULL) 1062 _dyld_func_lookup("__dyld_get_objc_module_sect_for_module", (void**)&p); 1063 p(module, objc_module, size); 1064} 1065 1066/* 1067 * _dyld_bind_objc_module() is passed a pointer to something in an (__OBJC, 1068 * __module) section and causes the module that is associated with that address 1069 * to be bound. 1070 */ 1071void 1072_dyld_bind_objc_module(const void* objc_module) 1073{ 1074 DYLD_LOCK_THIS_BLOCK; 1075 static void (*p)(const void *objc_module) = NULL; 1076 1077 if(p == NULL) 1078 _dyld_func_lookup("__dyld_bind_objc_module", (void**)&p); 1079 p(objc_module); 1080} 1081#endif 1082 1083#if DEPRECATED_APIS_SUPPORTED 1084bool 1085_dyld_present(void) 1086{ 1087 // this function exists for compatiblity only 1088 return true; 1089} 1090#endif 1091 1092uint32_t 1093_dyld_image_count(void) 1094{ 1095 DYLD_NO_LOCK_THIS_BLOCK; 1096 static uint32_t (*p)(void) = NULL; 1097 1098 if(p == NULL) 1099 _dyld_func_lookup("__dyld_image_count", (void**)&p); 1100 return(p()); 1101} 1102 1103const struct mach_header * 1104_dyld_get_image_header(uint32_t image_index) 1105{ 1106 DYLD_NO_LOCK_THIS_BLOCK; 1107 static struct mach_header * (*p)(uint32_t image_index) = NULL; 1108 1109 if(p == NULL) 1110 _dyld_func_lookup("__dyld_get_image_header", (void**)&p); 1111 return(p(image_index)); 1112} 1113 1114intptr_t 1115_dyld_get_image_vmaddr_slide(uint32_t image_index) 1116{ 1117 DYLD_NO_LOCK_THIS_BLOCK; 1118 static unsigned long (*p)(uint32_t image_index) = NULL; 1119 1120 if(p == NULL) 1121 _dyld_func_lookup("__dyld_get_image_vmaddr_slide", (void**)&p); 1122 return(p(image_index)); 1123} 1124 1125const char* 1126_dyld_get_image_name(uint32_t image_index) 1127{ 1128 DYLD_NO_LOCK_THIS_BLOCK; 1129 static const char* (*p)(uint32_t image_index) = NULL; 1130 1131 if(p == NULL) 1132 _dyld_func_lookup("__dyld_get_image_name", (void**)&p); 1133 return(p(image_index)); 1134} 1135 1136// SPI in Mac OS X 10.6 1137intptr_t _dyld_get_image_slide(const struct mach_header* mh) 1138{ 1139 DYLD_NO_LOCK_THIS_BLOCK; 1140 static intptr_t (*p)(const struct mach_header*) = NULL; 1141 1142 if(p == NULL) 1143 _dyld_func_lookup("__dyld_get_image_slide", (void**)&p); 1144 return(p(mh)); 1145} 1146 1147 1148bool 1149_dyld_image_containing_address(const void* address) 1150{ 1151 DYLD_LOCK_THIS_BLOCK; 1152 static bool (*p)(const void*) = NULL; 1153 1154 if(p == NULL) 1155 _dyld_func_lookup("__dyld_image_containing_address", (void**)&p); 1156 return(p(address)); 1157} 1158 1159const struct mach_header * 1160_dyld_get_image_header_containing_address( 1161const void* address) 1162{ 1163 DYLD_LOCK_THIS_BLOCK; 1164 static const struct mach_header * (*p)(const void*) = NULL; 1165 1166 if(p == NULL) 1167 _dyld_func_lookup("__dyld_get_image_header_containing_address", (void**)&p); 1168 return p(address); 1169} 1170 1171 1172#if DEPRECATED_APIS_SUPPORTED 1173bool _dyld_launched_prebound(void) 1174{ 1175 DYLD_LOCK_THIS_BLOCK; 1176 static bool (*p)(void) = NULL; 1177 1178 if(p == NULL) 1179 _dyld_func_lookup("__dyld_launched_prebound", (void**)&p); 1180 return(p()); 1181} 1182 1183bool _dyld_all_twolevel_modules_prebound(void) 1184{ 1185 DYLD_LOCK_THIS_BLOCK; 1186 static bool (*p)(void) = NULL; 1187 1188 if(p == NULL) 1189 _dyld_func_lookup("__dyld_all_twolevel_modules_prebound", (void**)&p); 1190 return(p()); 1191} 1192#endif // DEPRECATED_APIS_SUPPORTED 1193 1194 1195#include <dlfcn.h> 1196#include <stddef.h> 1197#include <pthread.h> 1198#include <stdlib.h> 1199#include <mach-o/dyld.h> 1200#include <servers/bootstrap.h> 1201#include "dyldLibSystemInterface.h" 1202 1203 1204// pthread key used to access per-thread dlerror message 1205static pthread_key_t dlerrorPerThreadKey; 1206static bool dlerrorPerThreadKeyInitialized = false; 1207 1208// data kept per-thread 1209struct dlerrorPerThreadData 1210{ 1211 size_t sizeAllocated; 1212 char message[1]; 1213}; 1214 1215// function called by dyld to get buffer to store dlerror message 1216static char* getPerThreadBufferFor_dlerror(size_t sizeRequired) 1217{ 1218 // ok to create key lazily because this function is called within dyld lock, so there is no race condition 1219 if (!dlerrorPerThreadKeyInitialized ) { 1220 // create key and tell pthread package to call free() on any data associated with key if thread dies 1221 pthread_key_create(&dlerrorPerThreadKey, &free); 1222 dlerrorPerThreadKeyInitialized = true; 1223 } 1224 1225 const size_t size = (sizeRequired < 256) ? 256 : sizeRequired; 1226 dlerrorPerThreadData* data = (dlerrorPerThreadData*)pthread_getspecific(dlerrorPerThreadKey); 1227 if ( data == NULL ) { 1228 //int mallocSize = offsetof(dlerrorPerThreadData, message[size]); 1229 const size_t mallocSize = sizeof(dlerrorPerThreadData)+size; 1230 data = (dlerrorPerThreadData*)malloc(mallocSize); 1231 data->sizeAllocated = size; 1232 pthread_setspecific(dlerrorPerThreadKey, data); 1233 } 1234 else if ( data->sizeAllocated < sizeRequired ) { 1235 free(data); 1236 //int mallocSize = offsetof(dlerrorPerThreadData, message[size]); 1237 const size_t mallocSize = sizeof(dlerrorPerThreadData)+size; 1238 data = (dlerrorPerThreadData*)malloc(mallocSize); 1239 data->sizeAllocated = size; 1240 pthread_setspecific(dlerrorPerThreadKey, data); 1241 } 1242 return data->message; 1243} 1244 1245// <rdar://problem/10595338> dlerror buffer leak 1246// Only allocate buffer if an actual error message needs to be set 1247static bool hasPerThreadBufferFor_dlerror() 1248{ 1249 if (!dlerrorPerThreadKeyInitialized ) 1250 return false; 1251 1252 return (pthread_getspecific(dlerrorPerThreadKey) != NULL); 1253} 1254 1255// use non-lazy pointer to vproc_swap_integer so that lazy binding does not recurse 1256typedef vproc_err_t (*vswapproc)(vproc_t vp, vproc_gsk_t key,int64_t *inval, int64_t *outval); 1257static vswapproc swapProc = &vproc_swap_integer; 1258 1259static bool isLaunchdOwned() { 1260 static bool first = true; 1261 static bool result; 1262 if ( first ) { 1263 int64_t val = 0; 1264 (*swapProc)(NULL, VPROC_GSK_IS_MANAGED, NULL, &val); 1265 result = ( val != 0 ); 1266 first = false; 1267 } 1268 return result; 1269} 1270 1271 1272#if DYLD_SHARED_CACHE_SUPPORT 1273static void shared_cache_missing() 1274{ 1275 // leave until dyld's that might call this are rare 1276} 1277 1278static void shared_cache_out_of_date() 1279{ 1280 // leave until dyld's that might call this are rare 1281} 1282#endif // DYLD_SHARED_CACHE_SUPPORT 1283 1284 1285// the table passed to dyld containing thread helpers 1286static dyld::LibSystemHelpers sHelpers = { 13, &dyldGlobalLockAcquire, &dyldGlobalLockRelease, 1287 &getPerThreadBufferFor_dlerror, &malloc, &free, &__cxa_atexit, 1288 #if DYLD_SHARED_CACHE_SUPPORT 1289 &shared_cache_missing, &shared_cache_out_of_date, 1290 #else 1291 NULL, NULL, 1292 #endif 1293 NULL, NULL, 1294 &pthread_key_create, &pthread_setspecific, 1295 &malloc_size, 1296 &pthread_getspecific, 1297 &__cxa_finalize, 1298 address_of_start, 1299 &hasPerThreadBufferFor_dlerror, 1300 &isLaunchdOwned, 1301 &vm_allocate, 1302 &mmap, 1303 &__cxa_finalize_ranges}; 1304 1305 1306// 1307// during initialization of libSystem this routine will run 1308// and call dyld, registering the helper functions. 1309// 1310extern "C" void tlv_initializer(); 1311extern "C" void _dyld_initializer(); 1312void _dyld_initializer() 1313{ 1314 void (*p)(dyld::LibSystemHelpers*); 1315 1316 _dyld_func_lookup("__dyld_register_thread_helpers", (void**)&p); 1317 if(p != NULL) 1318 p(&sHelpers); 1319 1320 tlv_initializer(); 1321} 1322 1323 1324char* dlerror() 1325{ 1326 DYLD_LOCK_THIS_BLOCK; 1327 static char* (*p)() = NULL; 1328 1329 if(p == NULL) 1330 _dyld_func_lookup("__dyld_dlerror", (void**)&p); 1331 return(p()); 1332} 1333 1334int dladdr(const void* addr, Dl_info* info) 1335{ 1336 DYLD_LOCK_THIS_BLOCK; 1337 static int (*p)(const void* , Dl_info*) = NULL; 1338 1339 if(p == NULL) 1340 _dyld_func_lookup("__dyld_dladdr", (void**)&p); 1341 return(p(addr, info)); 1342} 1343 1344int dlclose(void* handle) 1345{ 1346 DYLD_LOCK_THIS_BLOCK; 1347 static int (*p)(void* handle) = NULL; 1348 1349 if(p == NULL) 1350 _dyld_func_lookup("__dyld_dlclose", (void**)&p); 1351 return(p(handle)); 1352} 1353 1354void* dlopen(const char* path, int mode) 1355{ 1356 // dlopen is special. locking is done inside dyld to allow initializer to run without lock 1357 DYLD_NO_LOCK_THIS_BLOCK; 1358 1359 static void* (*p)(const char* path, int) = NULL; 1360 1361 if(p == NULL) 1362 _dyld_func_lookup("__dyld_dlopen", (void**)&p); 1363 void* result = p(path, mode); 1364 // use asm block to prevent tail call optimization 1365 // this is needed because dlopen uses __builtin_return_address() and depends on this glue being in the frame chain 1366 // <rdar://problem/5313172 dlopen() looks too far up stack, can cause crash> 1367 __asm__ volatile(""); 1368 1369 return result; 1370} 1371 1372bool dlopen_preflight(const char* path) 1373{ 1374 DYLD_LOCK_THIS_BLOCK; 1375 static bool (*p)(const char* path) = NULL; 1376 1377 if(p == NULL) 1378 _dyld_func_lookup("__dyld_dlopen_preflight", (void**)&p); 1379 return(p(path)); 1380} 1381 1382void* dlsym(void* handle, const char* symbol) 1383{ 1384 DYLD_LOCK_THIS_BLOCK; 1385 static void* (*p)(void* handle, const char* symbol) = NULL; 1386 1387 if(p == NULL) 1388 _dyld_func_lookup("__dyld_dlsym", (void**)&p); 1389 return(p(handle, symbol)); 1390} 1391 1392void dyld_register_image_state_change_handler(dyld_image_states state, 1393 bool batch, dyld_image_state_change_handler handler) 1394{ 1395 DYLD_LOCK_THIS_BLOCK; 1396 static void* (*p)(dyld_image_states, bool, dyld_image_state_change_handler) = NULL; 1397 1398 if(p == NULL) 1399 _dyld_func_lookup("__dyld_dyld_register_image_state_change_handler", (void**)&p); 1400 p(state, batch, handler); 1401} 1402 1403 1404const struct dyld_all_image_infos* _dyld_get_all_image_infos() 1405{ 1406 DYLD_NO_LOCK_THIS_BLOCK; 1407 static struct dyld_all_image_infos* (*p)() = NULL; 1408 1409 if(p == NULL) 1410 _dyld_func_lookup("__dyld_get_all_image_infos", (void**)&p); 1411 return p(); 1412} 1413 1414#if SUPPORT_ZERO_COST_EXCEPTIONS 1415bool _dyld_find_unwind_sections(void* addr, dyld_unwind_sections* info) 1416{ 1417 DYLD_NO_LOCK_THIS_BLOCK; 1418 static void* (*p)(void*, dyld_unwind_sections*) = NULL; 1419 1420 if(p == NULL) 1421 _dyld_func_lookup("__dyld_find_unwind_sections", (void**)&p); 1422 return p(addr, info); 1423} 1424#endif 1425 1426 1427#if __i386__ || __x86_64__ || __arm__ || __arm64__ 1428__attribute__((visibility("hidden"))) 1429void* _dyld_fast_stub_entry(void* loadercache, long lazyinfo) 1430{ 1431 DYLD_NO_LOCK_THIS_BLOCK; 1432 static void* (*p)(void*, long) = NULL; 1433 1434 if(p == NULL) 1435 _dyld_func_lookup("__dyld_fast_stub_entry", (void**)&p); 1436 return p(loadercache, lazyinfo); 1437} 1438#endif 1439 1440 1441const char* dyld_image_path_containing_address(const void* addr) 1442{ 1443 DYLD_NO_LOCK_THIS_BLOCK; 1444 static const char* (*p)(const void*) = NULL; 1445 1446 if(p == NULL) 1447 _dyld_func_lookup("__dyld_image_path_containing_address", (void**)&p); 1448 return p(addr); 1449} 1450 1451bool dyld_shared_cache_some_image_overridden() 1452{ 1453 DYLD_NO_LOCK_THIS_BLOCK; 1454 static bool (*p)() = NULL; 1455 1456 if(p == NULL) 1457 _dyld_func_lookup("__dyld_shared_cache_some_image_overridden", (void**)&p); 1458 return p(); 1459} 1460 1461 1462bool dyld_process_is_restricted() 1463{ 1464 DYLD_NO_LOCK_THIS_BLOCK; 1465 static bool (*p)() = NULL; 1466 1467 if(p == NULL) 1468 _dyld_func_lookup("__dyld_process_is_restricted", (void**)&p); 1469 return p(); 1470} 1471 1472 1473void dyld_dynamic_interpose(const struct mach_header* mh, const struct dyld_interpose_tuple array[], size_t count) 1474{ 1475 DYLD_LOCK_THIS_BLOCK; 1476 static void (*p)(const struct mach_header* mh, const struct dyld_interpose_tuple array[], size_t count) = NULL; 1477 1478 if (p == NULL) 1479 _dyld_func_lookup("__dyld_dynamic_interpose", (void**)&p); 1480 p(mh, array, count); 1481} 1482 1483 1484// SPI called __fork 1485void _dyld_fork_child() 1486{ 1487 DYLD_NO_LOCK_THIS_BLOCK; 1488 static void (*p)() = NULL; 1489 1490 if(p == NULL) 1491 _dyld_func_lookup("__dyld_fork_child", (void**)&p); 1492 return p(); 1493} 1494 1495 1496 1497 1498