1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- 
   3  * Copyright (c) 2004-2009 Apple Inc. All rights reserved. 
   5  * @APPLE_LICENSE_HEADER_START@ 
   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 
  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. 
  22  * @APPLE_LICENSE_HEADER_END@ 
  26 // This file implements that API's in <mach-o/dyld.h> 
  30 #define __STDC_LIMIT_MACROS 
  34 #include <sys/param.h> 
  35 #include <sys/mount.h> 
  36 #include <Availability.h> 
  43 #include <mach/mach.h> 
  45 #include <sys/sysctl.h> 
  46 #include <mach/mach_traps.h> // for task_self_trap() 
  49 #include "mach-o/dyld_images.h" 
  50 #include "mach-o/dyld.h" 
  51 #include "mach-o/dyld_priv.h" 
  53 #include "ImageLoader.h" 
  55 #include "dyldLibSystemInterface.h" 
  57 #undef _POSIX_C_SOURCE 
  60 // from dyldExceptions.c 
  61 extern "C" void __Unwind_SjLj_SetThreadKey(pthread_key_t key
); 
  64 extern void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[]); 
  66 // deprecated APIs are still availble on Mac OS X, but not on iPhone OS 
  67 #if __IPHONE_OS_VERSION_MIN_REQUIRED     
  68         #define DEPRECATED_APIS_SUPPORTED 0 
  70         #define DEPRECATED_APIS_SUPPORTED 1 
  74 #if DEPRECATED_APIS_SUPPORTED 
  75 static char sLastErrorFilePath
[1024]; 
  76 static NSLinkEditErrors sLastErrorFileCode
; 
  77 static int sLastErrorNo
; 
  80 // In 10.3.x and earlier all the NSObjectFileImage API's were implemeneted in libSystem.dylib 
  81 // Beginning in 10.4 the NSObjectFileImage API's are implemented in dyld and libSystem just forwards 
  82 // This conditional keeps support for old libSystem's which needed some help implementing the API's 
  83 #define OLD_LIBSYSTEM_SUPPORT (__i386__) 
  85 // The following functions have no prototype in any header.  They are special cases 
  86 // where _dyld_func_lookup() is used directly. 
  87 static void _dyld_make_delayed_module_initializer_calls(); 
  88 static void registerThreadHelpers(const dyld::LibSystemHelpers
*); 
  89 #if DEPRECATED_APIS_SUPPORTED 
  90 static void _dyld_install_handlers(void* undefined
, void* multiple
, void* linkEdit
); 
  91 #if OLD_LIBSYSTEM_SUPPORT 
  92 static NSModule 
_dyld_link_module(NSObjectFileImage object_addr
, size_t object_size
, const char* moduleName
, uint32_t options
); 
  94 static void _dyld_register_binding_handler(void * (*)(const char *, const char *, void *), ImageLoader::BindingOptions
); 
  95 static bool NSMakePrivateModulePublic(NSModule 
module); 
  96 static void _dyld_call_module_initializers_for_dylib(const struct mach_header
* mh_dylib_header
); 
  98 // The following functions are dyld API's, but since dyld links with a static copy of libc.a 
  99 // the public name cannot be used. 
 100 static void             client_dyld_lookup_and_bind(const char* symbolName
, void** address
, NSModule
* module); 
 101 static bool             client_NSIsSymbolNameDefined(const char* symbolName
); 
 102 #endif // DEPRECATED_APIS_SUPPORTED 
 104 static bool client_dyld_find_unwind_sections(void* addr
, dyld_unwind_sections
* info
); 
 107 static void unimplemented() 
 109         dyld::halt("unimplemented dyld function\n"); 
 114     void*               implementation
; 
 117 static struct dyld_func dyld_funcs
[] = { 
 118     {"__dyld_register_func_for_add_image",                              (void*)_dyld_register_func_for_add_image 
}, 
 119     {"__dyld_register_func_for_remove_image",                   (void*)_dyld_register_func_for_remove_image 
}, 
 120     {"__dyld_dladdr",                                                                   (void*)dladdr 
}, 
 121     {"__dyld_dlclose",                                                                  (void*)dlclose 
}, 
 122     {"__dyld_dlerror",                                                                  (void*)dlerror 
}, 
 123     {"__dyld_dlopen",                                                                   (void*)dlopen 
}, 
 124     {"__dyld_dlsym",                                                                    (void*)dlsym 
}, 
 125     {"__dyld_dlopen_preflight",                                                 (void*)dlopen_preflight 
}, 
 126         {"__dyld_image_count",                                                          (void*)_dyld_image_count 
}, 
 127     {"__dyld_get_image_header",                                                 (void*)_dyld_get_image_header 
}, 
 128     {"__dyld_get_image_vmaddr_slide",                                   (void*)_dyld_get_image_vmaddr_slide 
}, 
 129     {"__dyld_get_image_name",                                                   (void*)_dyld_get_image_name 
}, 
 130     {"__dyld_get_image_slide",                                                  (void*)_dyld_get_image_slide 
}, 
 131     {"__dyld__NSGetExecutablePath",                                             (void*)_NSGetExecutablePath 
}, 
 134         {"__dyld_dyld_register_image_state_change_handler",     (void*)dyld_register_image_state_change_handler 
}, 
 135         {"__dyld_register_thread_helpers",                                      (void*)registerThreadHelpers 
}, 
 136         {"__dyld_fork_child",                                                           (void*)_dyld_fork_child 
}, 
 137     {"__dyld_moninit",                                                                  (void*)_dyld_moninit 
}, 
 138     {"__dyld_make_delayed_module_initializer_calls",    (void*)_dyld_make_delayed_module_initializer_calls 
}, 
 139         {"__dyld_get_all_image_infos",                                          (void*)_dyld_get_all_image_infos 
}, 
 141         {"__dyld_find_unwind_sections",                                         (void*)client_dyld_find_unwind_sections 
}, 
 143 #if __i386__ || __x86_64__ || __arm__ 
 144         {"__dyld_fast_stub_entry",                                                      (void*)dyld::fastBindLazySymbol 
}, 
 146         {"__dyld_image_path_containing_address",                        (void*)dyld_image_path_containing_address 
}, 
 147 #if __IPHONE_OS_VERSION_MIN_REQUIRED     
 148         {"__dyld_shared_cache_some_image_overridden",           (void*)dyld_shared_cache_some_image_overridden 
}, 
 150         {"__dyld_process_is_restricted",                                        (void*)dyld::processIsRestricted 
}, 
 153 #if DEPRECATED_APIS_SUPPORTED 
 154     {"__dyld_get_image_header_containing_address",              (void*)_dyld_get_image_header_containing_address 
}, 
 155     {"__dyld_lookup_and_bind",                                          (void*)client_dyld_lookup_and_bind 
}, 
 156     {"__dyld_lookup_and_bind_with_hint",                        (void*)_dyld_lookup_and_bind_with_hint 
}, 
 157     {"__dyld_lookup_and_bind_fully",                            (void*)_dyld_lookup_and_bind_fully 
}, 
 158     {"__dyld_install_handlers",                                         (void*)_dyld_install_handlers 
}, 
 159     {"__dyld_link_edit_error",                                          (void*)NSLinkEditError 
}, 
 160     {"__dyld_unlink_module",                                            (void*)NSUnLinkModule 
}, 
 161     {"__dyld_bind_objc_module",                                         (void*)_dyld_bind_objc_module 
}, 
 162     {"__dyld_bind_fully_image_containing_address",  (void*)_dyld_bind_fully_image_containing_address 
}, 
 163     {"__dyld_image_containing_address",                         (void*)_dyld_image_containing_address 
}, 
 164     {"__dyld_register_binding_handler",                         (void*)_dyld_register_binding_handler 
}, 
 165     {"__dyld_NSNameOfSymbol",                                           (void*)NSNameOfSymbol 
}, 
 166     {"__dyld_NSAddressOfSymbol",                                        (void*)NSAddressOfSymbol 
}, 
 167     {"__dyld_NSModuleForSymbol",                                        (void*)NSModuleForSymbol 
}, 
 168     {"__dyld_NSLookupAndBindSymbol",                            (void*)NSLookupAndBindSymbol 
}, 
 169     {"__dyld_NSLookupAndBindSymbolWithHint",            (void*)NSLookupAndBindSymbolWithHint 
}, 
 170     {"__dyld_NSLookupSymbolInModule",                           (void*)NSLookupSymbolInModule
}, 
 171     {"__dyld_NSLookupSymbolInImage",                            (void*)NSLookupSymbolInImage
}, 
 172     {"__dyld_NSMakePrivateModulePublic",                        (void*)NSMakePrivateModulePublic
}, 
 173     {"__dyld_NSIsSymbolNameDefined",                            (void*)client_NSIsSymbolNameDefined
}, 
 174     {"__dyld_NSIsSymbolNameDefinedWithHint",            (void*)NSIsSymbolNameDefinedWithHint 
}, 
 175     {"__dyld_NSIsSymbolNameDefinedInImage",                     (void*)NSIsSymbolNameDefinedInImage
}, 
 176     {"__dyld_NSNameOfModule",                                           (void*)NSNameOfModule 
}, 
 177     {"__dyld_NSLibraryNameForModule",                           (void*)NSLibraryNameForModule 
}, 
 178     {"__dyld_NSAddLibrary",                                                     (void*)NSAddLibrary 
}, 
 179     {"__dyld_NSAddLibraryWithSearching",                        (void*)NSAddLibraryWithSearching 
}, 
 180     {"__dyld_NSAddImage",                                                       (void*)NSAddImage 
}, 
 181     {"__dyld_launched_prebound",                                        (void*)_dyld_launched_prebound 
}, 
 182     {"__dyld_all_twolevel_modules_prebound",            (void*)_dyld_all_twolevel_modules_prebound 
}, 
 183     {"__dyld_call_module_initializers_for_dylib",   (void*)_dyld_call_module_initializers_for_dylib 
}, 
 184     {"__dyld_NSCreateObjectFileImageFromFile",                  (void*)NSCreateObjectFileImageFromFile 
}, 
 185     {"__dyld_NSCreateObjectFileImageFromMemory",                (void*)NSCreateObjectFileImageFromMemory 
}, 
 186     {"__dyld_NSDestroyObjectFileImage",                                 (void*)NSDestroyObjectFileImage 
}, 
 187     {"__dyld_NSLinkModule",                                                             (void*)NSLinkModule 
}, 
 188     {"__dyld_NSHasModInitObjectFileImage",                              (void*)NSHasModInitObjectFileImage 
}, 
 189     {"__dyld_NSSymbolDefinitionCountInObjectFileImage", (void*)NSSymbolDefinitionCountInObjectFileImage 
}, 
 190     {"__dyld_NSSymbolDefinitionNameInObjectFileImage",  (void*)NSSymbolDefinitionNameInObjectFileImage 
}, 
 191     {"__dyld_NSIsSymbolDefinedInObjectFileImage",               (void*)NSIsSymbolDefinedInObjectFileImage 
}, 
 192     {"__dyld_NSSymbolReferenceNameInObjectFileImage",   (void*)NSSymbolReferenceNameInObjectFileImage 
}, 
 193     {"__dyld_NSSymbolReferenceCountInObjectFileImage",  (void*)NSSymbolReferenceCountInObjectFileImage 
}, 
 194     {"__dyld_NSGetSectionDataInObjectFileImage",                (void*)NSGetSectionDataInObjectFileImage 
}, 
 195 #if OLD_LIBSYSTEM_SUPPORT 
 196     {"__dyld_link_module",                                                      (void*)_dyld_link_module 
}, 
 198 #endif //DEPRECATED_APIS_SUPPORTED 
 205 #if DEPRECATED_APIS_SUPPORTED 
 207 static void dyldAPIhalt(const char* apiName
, const char* errorMsg
) 
 209         dyld::log("dyld: %s() error\n", apiName
); 
 210         dyld::halt(errorMsg
); 
 213 // dyld's abstract type NSSymbol is implemented as const ImageLoader::Symbol* 
 214 inline NSSymbol 
SymbolToNSSymbol(const ImageLoader::Symbol
* sym
) 
 216         return (NSSymbol
)sym
; 
 218 inline const ImageLoader::Symbol
* NSSymbolToSymbol(NSSymbol sym
) 
 220         return (const ImageLoader::Symbol
*)sym
; 
 223 // dyld's abstract type NSModule is implemented as ImageLoader* 
 224 inline NSModule 
ImageLoaderToNSModule(const ImageLoader
* image
) 
 226         return (NSModule
)image
; 
 228 inline ImageLoader
* NSModuleToImageLoader(NSModule 
module) 
 230         ImageLoader
* image 
= (ImageLoader
*)module; 
 231         if ( dyld::validImage(image
) ) 
 236 // actual definition for opaque type 
 237 struct __NSObjectFileImage
 
 240         const void*             imageBaseAddress
;       // not used with OFI created from files 
 241         size_t                  imageLength
;            // not used with OFI created from files 
 245 VECTOR_NEVER_DESTRUCTED(NSObjectFileImage
); 
 246 static std::vector
<NSObjectFileImage
> sObjectFileImages
; 
 251 // __NSObjectFileImage are deleted in NSDestroyObjectFileImage() 
 252 // The contained image is delete in one of two places: 
 253 //      NSUnLinkModule deletes the image if there is no __NSObjectFileImage with a reference to it 
 254 //      NSDestroyObjectFileImage deletes the image if image is not in list of valid images 
 259 static void setLastError(NSLinkEditErrors code
, int errnum
, const char* file
, const char* message
) 
 261         dyld::setErrorMessage(message
); 
 262         strncpy(sLastErrorFilePath
, file
, 1024); 
 263         sLastErrorFilePath
[1023] = '\0'; 
 264         sLastErrorFileCode 
= code
; 
 265         sLastErrorNo 
= errnum
; 
 268 #endif // DEPRECATED_APIS_SUPPORTED 
 271  *_dyld_NSGetExecutablePath is the dyld side of _NSGetExecutablePath which 
 272  * copies the path of the executable into the buffer and returns 0 if the path 
 273  * was successfully copied in the provided buffer. If the buffer is not large 
 274  * enough, -1 is returned and the expected buffer size is copied in *bufsize. 
 275  * Note that _NSGetExecutablePath will return "a path" to the executable not a 
 276  * "real path" to the executable. That is the path may be a symbolic link and 
 277  * not the real file. And with deep directories the total bufsize needed could 
 278  * be more than MAXPATHLEN. 
 280 int _NSGetExecutablePath(char* buf
, uint32_t *bufsize
) 
 282         if ( dyld::gLogAPIs 
) 
 283                 dyld::log("%s(...)\n", __func__
); 
 284         const char* exePath 
= dyld::getExecutablePath(); 
 285         if(*bufsize 
< strlen(exePath
) + 1){ 
 286             *bufsize 
= strlen(exePath
) + 1; 
 289         strcpy(buf
, exePath
); 
 293 uint32_t _dyld_image_count(void) 
 295         if ( dyld::gLogAPIs 
) 
 296                 dyld::log("%s()\n", __func__
); 
 297         return dyld::getImageCount(); 
 300 const struct mach_header
* _dyld_get_image_header(uint32_t image_index
) 
 302         if ( dyld::gLogAPIs 
) 
 303                 dyld::log("%s(%u)\n", __func__
, image_index
); 
 304         ImageLoader
* image 
= dyld::getIndexedImage(image_index
); 
 306                 return (struct mach_header
*)image
->machHeader(); 
 311 intptr_t _dyld_get_image_vmaddr_slide(uint32_t image_index
) 
 313         if ( dyld::gLogAPIs 
) 
 314                 dyld::log("%s(%u)\n", __func__
, image_index
); 
 315         ImageLoader
* image 
= dyld::getIndexedImage(image_index
); 
 317                 return image
->getSlide(); 
 322 intptr_t _dyld_get_image_slide(const struct mach_header
* mh
) 
 324         if ( dyld::gLogAPIs 
) 
 325                 dyld::log("%s(%p)\n", __func__
, mh
); 
 326         ImageLoader
* image 
= dyld::findImageByMachHeader(mh
); 
 328                 return image
->getSlide(); 
 334 const char* _dyld_get_image_name(uint32_t image_index
) 
 336         if ( dyld::gLogAPIs 
) 
 337                 dyld::log("%s(%u)\n", __func__
, image_index
); 
 338         ImageLoader
* image 
= dyld::getIndexedImage(image_index
); 
 340                 return image
->getRealPath(); 
 345 const struct mach_header 
* _dyld_get_image_header_containing_address(const void* address
) 
 347         if ( dyld::gLogAPIs 
) 
 348                 dyld::log("%s(%p)\n", __func__
, address
); 
 349         ImageLoader
* image 
= dyld::findImageContainingAddress(address
); 
 351                 return image
->machHeader(); 
 356 void _dyld_register_func_for_add_image(void (*func
)(const struct mach_header 
*mh
, intptr_t vmaddr_slide
)) 
 358         if ( dyld::gLogAPIs 
) 
 359                 dyld::log("%s(%p)\n", __func__
, (void *)func
); 
 360         dyld::registerAddCallback(func
); 
 363 void _dyld_register_func_for_remove_image(void (*func
)(const struct mach_header 
*mh
, intptr_t vmaddr_slide
)) 
 365         if ( dyld::gLogAPIs 
) 
 366                 dyld::log("%s(%p)\n", __func__
, (void *)func
); 
 367         dyld::registerRemoveCallback(func
); 
 372 // called by crt before main() by programs linked with 10.4 or earlier crt1.o 
 373 static void _dyld_make_delayed_module_initializer_calls() 
 375         if ( dyld::gLogAPIs 
) 
 376                 dyld::log("%s()\n", __func__
); 
 378 #if SUPPORT_OLD_CRT_INITIALIZATION 
 379         if ( dyld::gRunInitializersOldWay 
) 
 380                 dyld::initializeMainExecutable(); 
 386 #if DEPRECATED_APIS_SUPPORTED 
 389 // _dyld_call_module_initializers_for_dylib() is the dyld side of 
 390 // __initialize_Cplusplus() which is in dylib1.o. 
 391 // It is intended to only be called inside -init rouintes. 
 392 // -init routines are called before module initializers (what C++ 
 393 // initializers use).  Calling __initialize_Cplusplus() in a -init 
 394 // routine causes the module initializers for an image to be called 
 395 // which then allows C++ to be used inside a -init routine 
 397 static void _dyld_call_module_initializers_for_dylib(const struct mach_header
* mh_dylib_header
) 
 399         if ( dyld::gLogAPIs 
) 
 400                 dyld::log("__initialize_Cplusplus()\n"); 
 402         // for now, do nothing... 
 406 void _dyld_lookup_and_bind_fully(const char* symbolName
, void** address
, NSModule
* module) 
 408         if ( dyld::gLogAPIs 
) 
 409                 dyld::log("%s(\"%s\", %p, %p)\n", __func__
, symbolName
, address
, module); 
 411         const ImageLoader::Symbol
* sym
; 
 412         dyld::clearErrorMessage(); 
 413         if ( dyld::flatFindExportedSymbol(symbolName
, &sym
, (const ImageLoader
**)&image
) ) {     
 415                         image
->bindAllLazyPointers(dyld::gLinkContext
, true); 
 416                         if ( address 
!= NULL
) 
 417                                 *address 
= (void*)image
->getExportedSymbolAddress(sym
, dyld::gLinkContext
); 
 419                                 *module = ImageLoaderToNSModule(image
); 
 421                 catch (const char* msg
) { 
 422                         dyldAPIhalt(__func__
, msg
); 
 426                 // on failure to find symbol return NULLs 
 427                 if ( address 
!= NULL
) 
 434 // Note: This cannot have public name because dyld is built with a static copy of libc.a 
 435 // which calls dyld_lookup_and_bind() and expects to find dyld's symbols not host process 
 436 static void client_dyld_lookup_and_bind(const char* symbolName
, void** address
, NSModule
* module) 
 438         if ( dyld::gLogAPIs 
) 
 439                 dyld::log("_dyld_lookup_and_bind(\"%s\", %p, %p)\n", symbolName
, address
, module); 
 440         const ImageLoader
* image
; 
 441         const ImageLoader::Symbol
* sym
; 
 442         if ( dyld::flatFindExportedSymbol(symbolName
, &sym
, &image
) ) { 
 443                 if ( address 
!= NULL
) 
 444                         *address 
= (void*)image
->getExportedSymbolAddress(sym
, dyld::gLinkContext
); 
 446                         *module = ImageLoaderToNSModule(image
); 
 449                 // on failure to find symbol return NULLs 
 450                 if ( address 
!= NULL
) 
 457 void _dyld_lookup_and_bind_with_hint(const char* symbolName
, const char* library_name_hint
, void** address
, NSModule
* module) 
 459         if ( dyld::gLogAPIs 
) 
 460                 dyld::log("%s(\"%s\", \"%s\", %p, %p)\n", __func__
, symbolName
, library_name_hint
, address
, module); 
 461         const ImageLoader
* image
; 
 462         const ImageLoader::Symbol
* sym
; 
 463         // Look for library whose path contains the hint.  If that fails search everywhere 
 464         if (  dyld::flatFindExportedSymbolWithHint(symbolName
, library_name_hint
, &sym
, &image
)  
 465           ||  dyld::flatFindExportedSymbol(symbolName
, &sym
, &image
) ) { 
 466                 if ( address 
!= NULL
) 
 467                         *address 
= (void*)image
->getExportedSymbolAddress(sym
, dyld::gLinkContext
); 
 469                         *module = ImageLoaderToNSModule(image
); 
 472                 // on failure to find symbol return NULLs 
 473                 if ( address 
!= NULL
) 
 481 NSSymbol 
NSLookupAndBindSymbol(const char *symbolName
) 
 483         if ( dyld::gLogAPIs 
) 
 484                 dyld::log("%s(\"%s\")\n", __func__
, symbolName
); 
 485         const ImageLoader
* image
; 
 486         const ImageLoader::Symbol
* sym
; 
 487         if ( dyld::flatFindExportedSymbol(symbolName
, &sym
, &image
) ) { 
 488                 return SymbolToNSSymbol(sym
); 
 490         // return NULL on failure 
 494 NSSymbol 
NSLookupAndBindSymbolWithHint(const char* symbolName
, const char* libraryNameHint
) 
 496         if ( dyld::gLogAPIs 
) 
 497                 dyld::log("%s(\"%s\", \"%s\")\n", __func__
, symbolName
, libraryNameHint
); 
 498         const ImageLoader
* image
; 
 499         const ImageLoader::Symbol
* sym
; 
 500         bool found 
= dyld::flatFindExportedSymbolWithHint(symbolName
, libraryNameHint
, &sym
, &image
); 
 502                 // hint failed, do slow search of all images 
 503                  found 
= dyld::flatFindExportedSymbol(symbolName
, &sym
, &image
); 
 506                 return SymbolToNSSymbol(sym
); 
 508         // return NULL on failure and log 
 509         if ( dyld::gLogAPIs 
) 
 510                 dyld::log("%s(\"%s\", \"%s\") => NULL \n", __func__
, symbolName
, libraryNameHint
); 
 517 static __attribute__((noinline
))  
 518 const struct mach_header
* addImage(void* callerAddress
, const char* path
, bool search
, bool dontLoad
, bool matchInstallName
, bool abortOnError
) 
 520         ImageLoader
*    image 
= NULL
; 
 521         std::vector
<const char*> rpathsFromCallerImage
; 
 523                 dyld::clearErrorMessage(); 
 524                 ImageLoader
* callerImage 
= dyld::findImageContainingAddress(callerAddress
); 
 525                 // like dlopen, use rpath from caller image and from main executable 
 526                 if ( callerImage 
!= NULL 
) 
 527                         callerImage
->getRPaths(dyld::gLinkContext
, rpathsFromCallerImage
); 
 528                 ImageLoader::RPathChain 
callersRPaths(NULL
, &rpathsFromCallerImage
); 
 529                 if ( callerImage 
!= dyld::mainExecutable() ) { 
 530                         dyld::mainExecutable()->getRPaths(dyld::gLinkContext
, rpathsFromCallerImage
); 
 532                 dyld::LoadContext context
; 
 533                 context
.useSearchPaths          
= search
; 
 534                 context
.useFallbackPaths        
= search
; 
 535                 context
.useLdLibraryPath        
= false; 
 536                 context
.implicitRPath           
= false; 
 537                 context
.matchByInstallName      
= matchInstallName
; 
 538                 context
.dontLoad                        
= dontLoad
; 
 539                 context
.mustBeBundle            
= false; 
 540                 context
.mustBeDylib                     
= true; 
 541                 context
.canBePIE                        
= false; 
 542                 context
.origin                          
= callerImage 
!= NULL 
? callerImage
->getPath() : NULL
; // caller's image's path 
 543                 context
.rpath                           
= &callersRPaths
;       // rpaths from caller and main executable 
 545                 image 
= load(path
, context
); 
 546                 if ( image 
!= NULL 
) { 
 547                         if ( context
.matchByInstallName 
) 
 548                                 image
->setMatchInstallPath(true); 
 549                         dyld::link(image
, false, false, callersRPaths
); 
 550                         dyld::runInitializers(image
); 
 551                         // images added with NSAddImage() can never be unloaded 
 552                         image
->setNeverUnload();  
 555         catch (const char* msg
) { 
 556                 dyld::garbageCollectImages(); 
 558                         char pathMsg
[strlen(msg
)+strlen(path
)+4]; 
 559                         strcpy(pathMsg
, msg
); 
 560                         strcat(pathMsg
, " "); 
 561                         strcat(pathMsg
, path
); 
 562                         dyldAPIhalt("NSAddImage", pathMsg
); 
 564                 // not halting, so set error state for NSLinkEditError to find 
 565                 setLastError(NSLinkEditOtherError
, 0, path
, msg
); 
 566                 free((void*)msg
);       // our free() will do nothing if msg is a string literal 
 569         // free rpaths (getRPaths() malloc'ed each string) 
 570         for(std::vector
<const char*>::iterator it
=rpathsFromCallerImage
.begin(); it 
!= rpathsFromCallerImage
.end(); ++it
) { 
 571                 const char* str 
= *it
; 
 577                 return image
->machHeader(); 
 581 const struct mach_header
* NSAddImage(const char* path
, uint32_t options
) 
 583         if ( dyld::gLogAPIs 
) 
 584                 dyld::log("%s(\"%s\", 0x%08X)\n", __func__
, path
, options
); 
 585         const bool dontLoad 
= ( (options 
& NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED
) != 0 ); 
 586         const bool search 
= ( (options 
& NSADDIMAGE_OPTION_WITH_SEARCHING
) != 0 ); 
 587         const bool matchInstallName 
= ( (options 
& NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME
) != 0 ); 
 588         const bool abortOnError 
= ( (options 
& (NSADDIMAGE_OPTION_RETURN_ON_ERROR
|NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED
)) == 0 ); 
 589         void* callerAddress 
= __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue 
 590         return addImage(callerAddress
, path
, search
, dontLoad
, matchInstallName
, abortOnError
); 
 593 bool NSAddLibrary(const char* path
) 
 595         if ( dyld::gLogAPIs 
) 
 596                 dyld::log("%s(\"%s\")\n", __func__
, path
); 
 597         void* callerAddress 
= __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue 
 598         return (addImage(callerAddress
, path
, false, false, false, false) != NULL
); 
 601 bool NSAddLibraryWithSearching(const char* path
) 
 603         if ( dyld::gLogAPIs 
) 
 604                 dyld::log("%s(\"%s\")\n", __func__
, path
); 
 605         void* callerAddress 
= __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue 
 606         return (addImage(callerAddress
, path
, true, false, false, false) != NULL
); 
 611 //#define NSADDIMAGE_OPTION_NONE                        0x0 
 612 //#define NSADDIMAGE_OPTION_RETURN_ON_ERROR             0x1 
 613 //#define NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME       0x8 
 615 bool NSIsSymbolNameDefinedInImage(const struct mach_header
* mh
, const char* symbolName
) 
 617         if ( dyld::gLogAPIs 
) 
 618                 dyld::log("%s(%p, \"%s\")\n", __func__
, (void *)mh
, symbolName
); 
 619         ImageLoader
* image 
= dyld::findImageByMachHeader(mh
); 
 620         if ( image 
!= NULL 
) { 
 621                 if ( image
->findExportedSymbol(symbolName
, true, NULL
) != NULL
) 
 628 NSSymbol 
NSLookupSymbolInImage(const struct mach_header
* mh
, const char* symbolName
, uint32_t options
) 
 630         if ( dyld::gLogAPIs 
) 
 631                 dyld::log("%s(%p, \"%s\", 0x%08X)\n", __func__
, mh
, symbolName
, options
); 
 632         const ImageLoader::Symbol
* symbol 
= NULL
; 
 633         dyld::clearErrorMessage(); 
 634         ImageLoader
* image 
= dyld::findImageByMachHeader(mh
); 
 635         if ( image 
!= NULL 
) { 
 637                         if ( options 
& NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_FULLY 
) { 
 638                                 image
->bindAllLazyPointers(dyld::gLinkContext
, true); 
 640                         else if ( options 
& NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW 
) { 
 641                                 image
->bindAllLazyPointers(dyld::gLinkContext
, false); 
 644                 catch (const char* msg
) { 
 645                         if ( (options 
& NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR
) == 0 ) { 
 646                                 dyldAPIhalt(__func__
, msg
); 
 649                 symbol 
= image
->findExportedSymbol(symbolName
, true, NULL
); 
 651         if ( dyld::gLogAPIs 
&& (symbol 
== NULL
) ) 
 652                 dyld::log("%s(%p, \"%s\", 0x%08X) ==> NULL\n", __func__
, mh
, symbolName
, options
); 
 653         return SymbolToNSSymbol(symbol
); 
 657 // Note: This cannot have public name because dyld is built with a static copy of libc.a 
 658 // which calls NSIsSymbolNameDefined() and expects to find dyld's symbols not host process 
 659 static bool client_NSIsSymbolNameDefined(const char* symbolName
) 
 661         if ( dyld::gLogAPIs 
) 
 662                 dyld::log("NSIsSymbolNameDefined(\"%s\")\n", symbolName
); 
 663         const ImageLoader
* image
; 
 664         const ImageLoader::Symbol
* sym
; 
 665         return dyld::flatFindExportedSymbol(symbolName
, &sym
, &image
); 
 668 bool NSIsSymbolNameDefinedWithHint(const char* symbolName
, const char* libraryNameHint
) 
 670         if ( dyld::gLogAPIs 
) 
 671                 dyld::log("%s(\"%s\", \"%s\")\n", __func__
, symbolName
, libraryNameHint
); 
 672         const ImageLoader
* image
; 
 673         const ImageLoader::Symbol
* sym
; 
 674         bool found 
= dyld::flatFindExportedSymbolWithHint(symbolName
, libraryNameHint
, &sym
, &image
); 
 676                 // hint failed, do slow search of all images 
 677                  found 
= dyld::flatFindExportedSymbol(symbolName
, &sym
, &image
); 
 679         if ( !found 
&& dyld::gLogAPIs 
) 
 680                 dyld::log("%s(\"%s\", \"%s\") => false \n", __func__
, symbolName
, libraryNameHint
); 
 684 const char* NSNameOfSymbol(NSSymbol symbol
) 
 686         if ( dyld::gLogAPIs 
) 
 687                 dyld::log("%s(%p)\n", __func__
, (void *)symbol
); 
 688         const char* result 
= NULL
; 
 689         ImageLoader
* image 
= dyld::findImageContainingSymbol(symbol
); 
 691                 result 
= image
->getExportedSymbolName(NSSymbolToSymbol(symbol
)); 
 695 void* NSAddressOfSymbol(NSSymbol symbol
) 
 697         if ( dyld::gLogAPIs 
) 
 698                 dyld::log("%s(%p)\n", __func__
, (void *)symbol
); 
 699         if ( symbol 
== NULL 
) 
 702         ImageLoader
* image 
= dyld::findImageContainingSymbol(symbol
); 
 704                 result 
= (void*)image
->getExportedSymbolAddress(NSSymbolToSymbol(symbol
), dyld::gLinkContext
); 
 708 NSModule 
NSModuleForSymbol(NSSymbol symbol
) 
 710         if ( dyld::gLogAPIs 
) 
 711                 dyld::log("%s(%p)\n", __func__
, (void *)symbol
); 
 712         NSModule result 
= NULL
; 
 713         ImageLoader
* image 
= dyld::findImageContainingSymbol(symbol
); 
 715                 result 
= ImageLoaderToNSModule(image
); 
 722 bool _dyld_all_twolevel_modules_prebound(void) 
 724         if ( dyld::gLogAPIs 
) 
 725                 dyld::log("%s()\n", __func__
); 
 729 void _dyld_bind_objc_module(const void *objc_module
) 
 731         if ( dyld::gLogAPIs 
) 
 732                 dyld::log("%s(%p)\n", __func__
, objc_module
); 
 733         // do nothing, with new dyld everything already bound 
 737 bool _dyld_bind_fully_image_containing_address(const void* address
) 
 739         if ( dyld::gLogAPIs 
) 
 740                 dyld::log("%s(%p)\n", __func__
, address
); 
 741         dyld::clearErrorMessage(); 
 742         ImageLoader
* image 
= dyld::findImageContainingAddress(address
); 
 743         if ( image 
!= NULL 
) { 
 745                         image
->bindAllLazyPointers(dyld::gLinkContext
, true); 
 748                 catch (const char* msg
) { 
 749                         dyldAPIhalt(__func__
, msg
); 
 755 bool _dyld_image_containing_address(const void* address
) 
 757         if ( dyld::gLogAPIs 
) 
 758                 dyld::log("%s(%p)\n", __func__
, address
); 
 759         ImageLoader 
*imageLoader 
= dyld::findImageContainingAddress(address
); 
 760         return (NULL 
!= imageLoader
); 
 763 static NSObjectFileImage 
createObjectImageFile(ImageLoader
* image
, const void* address 
= NULL
, size_t len
=0) 
 765         NSObjectFileImage result 
= new __NSObjectFileImage(); 
 766         result
->image 
= image
; 
 767         result
->imageBaseAddress 
= address
; 
 768         result
->imageLength 
= len
; 
 769         sObjectFileImages
.push_back(result
); 
 773 NSObjectFileImageReturnCode 
NSCreateObjectFileImageFromFile(const char* pathName
, NSObjectFileImage 
*objectFileImage
) 
 775         if ( dyld::gLogAPIs 
) 
 776                 dyld::log("%s(\"%s\", ...)\n", __func__
, pathName
); 
 778                 void* callerAddress 
= __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue 
 779                 ImageLoader
* callerImage 
= dyld::findImageContainingAddress(callerAddress
); 
 781                 dyld::LoadContext context
; 
 782                 context
.useSearchPaths          
= false; 
 783                 context
.useFallbackPaths        
= false; 
 784                 context
.useLdLibraryPath        
= false; 
 785                 context
.implicitRPath           
= false; 
 786                 context
.matchByInstallName      
= false; 
 787                 context
.dontLoad                        
= false; 
 788                 context
.mustBeBundle            
= true; 
 789                 context
.mustBeDylib                     
= false; 
 790                 context
.canBePIE                        
= false; 
 791                 context
.origin                          
= callerImage 
!= NULL 
? callerImage
->getPath() : NULL
; // caller's image's path 
 792                 context
.rpath                           
= NULL
; // support not yet implemented 
 794                 ImageLoader
* image 
= dyld::load(pathName
, context
); 
 795                 // Note:  We DO NOT link the image!  NSLinkModule will do that 
 796                 if ( image 
!= NULL 
) { 
 797                         if ( !image
->isBundle() ) { 
 798                                 // the image must have been already loaded (since context.mustBeBundle will prevent it from being loaded) 
 799                                 return NSObjectFileImageInappropriateFile
; 
 801                         *objectFileImage 
= createObjectImageFile(image
); 
 802                         return NSObjectFileImageSuccess
; 
 805         catch (const char* msg
) { 
 806                 //dyld::log("dyld: NSCreateObjectFileImageFromFile() error: %s\n", msg); 
 807                 dyld::garbageCollectImages(); 
 809                 return NSObjectFileImageInappropriateFile
; 
 811         return NSObjectFileImageFailure
; 
 815 NSObjectFileImageReturnCode 
NSCreateObjectFileImageFromMemory(const void* address
, size_t size
, NSObjectFileImage 
*objectFileImage
) 
 817         if ( dyld::gLogAPIs 
) 
 818                 dyld::log("%s(%p, %lu, %p)\n", __func__
, address
, size
, objectFileImage
); 
 821                 ImageLoader
* image 
= dyld::loadFromMemory((const uint8_t*)address
, size
, NULL
);  
 822                 if ( ! image
->isBundle() ) { 
 823                         // this API can only be used with bundles... 
 824                         dyld::garbageCollectImages(); 
 825                         return NSObjectFileImageInappropriateFile
; 
 827                 // Note:  We DO NOT link the image!  NSLinkModule will do that 
 828                 if ( image 
!= NULL 
) { 
 829                         *objectFileImage 
= createObjectImageFile(image
, address
, size
); 
 830                         return NSObjectFileImageSuccess
; 
 833         catch (const char* msg
) { 
 835                 dyld::garbageCollectImages(); 
 836                 //dyld::log("dyld: NSCreateObjectFileImageFromMemory() error: %s\n", msg); 
 838         return NSObjectFileImageFailure
; 
 841 static bool validOFI(NSObjectFileImage objectFileImage
) 
 843         const int ofiCount 
= sObjectFileImages
.size(); 
 844         for (int i
=0; i 
< ofiCount
; ++i
) { 
 845                 if ( sObjectFileImages
[i
] == objectFileImage 
) 
 851 bool NSDestroyObjectFileImage(NSObjectFileImage objectFileImage
) 
 853         if ( dyld::gLogAPIs 
) 
 854                 dyld::log("%s(%p)\n", __func__
, objectFileImage
); 
 856         if ( validOFI(objectFileImage
) ) { 
 857                 // a failure during NSLinkModule will delete the image 
 858                 if ( objectFileImage
->image 
!= NULL 
) { 
 859                         // if the image has never been linked or has been unlinked, the image is not in the list of valid images 
 860                         // and we should delete it 
 861                         bool linkedImage 
= dyld::validImage(objectFileImage
->image
); 
 862                         if ( ! linkedImage 
)  { 
 863                                 ImageLoader::deleteImage(objectFileImage
->image
); 
 864                                 objectFileImage
->image 
= NULL
; 
 868                 // remove from list of ofi's 
 869                 for (std::vector
<NSObjectFileImage
>::iterator it
=sObjectFileImages
.begin(); it 
!= sObjectFileImages
.end(); it
++) { 
 870                         if ( *it 
== objectFileImage 
) { 
 871                                 sObjectFileImages
.erase(it
); 
 876                 // if object was created from a memory, release that memory 
 877                 // NOTE: this is the way dyld has always done this. NSCreateObjectFileImageFromMemory() hands over ownership of the memory to dyld 
 878                 if ( objectFileImage
->imageBaseAddress 
!= NULL 
) { 
 880                         if ( (dyld::gLibSystemHelpers 
!= NULL
) && (dyld::gLibSystemHelpers
->version 
>= 6) ) { 
 881                                 size_t sz 
= (*dyld::gLibSystemHelpers
->malloc_size
)(objectFileImage
->imageBaseAddress
); 
 883                                         (*dyld::gLibSystemHelpers
->free
)((void*)(objectFileImage
->imageBaseAddress
)); 
 888                                 vm_deallocate(mach_task_self(), (vm_address_t
)objectFileImage
->imageBaseAddress
, objectFileImage
->imageLength
); 
 892                 delete objectFileImage
; 
 899 bool NSHasModInitObjectFileImage(NSObjectFileImage objectFileImage
) 
 901         if ( dyld::gLogAPIs 
) 
 902                 dyld::log("%s(%p)\n", __func__
, objectFileImage
); 
 903         return objectFileImage
->image
->needsInitialization(); 
 906 uint32_t NSSymbolDefinitionCountInObjectFileImage(NSObjectFileImage objectFileImage
) 
 908         if ( dyld::gLogAPIs 
) 
 909                 dyld::log("%s(%p)\n", __func__
, objectFileImage
); 
 910         return objectFileImage
->image
->getExportedSymbolCount(); 
 913 const char* NSSymbolDefinitionNameInObjectFileImage(NSObjectFileImage objectFileImage
, uint32_t ordinal
) 
 915         if ( dyld::gLogAPIs 
) 
 916                 dyld::log("%s(%p,%d)\n", __func__
, objectFileImage
, ordinal
); 
 917         const ImageLoader::Symbol
* sym 
= objectFileImage
->image
->getIndexedExportedSymbol(ordinal
); 
 918         return objectFileImage
->image
->getExportedSymbolName(sym
);       
 921 uint32_t NSSymbolReferenceCountInObjectFileImage(NSObjectFileImage objectFileImage
) 
 923         if ( dyld::gLogAPIs 
) 
 924                 dyld::log("%s(%p)\n", __func__
, objectFileImage
); 
 925         return objectFileImage
->image
->getImportedSymbolCount(); 
 928 const char * NSSymbolReferenceNameInObjectFileImage(NSObjectFileImage objectFileImage
, uint32_t ordinal
,  
 929                                                                                                         bool* tentative_definition
) 
 931         if ( dyld::gLogAPIs 
) 
 932                 dyld::log("%s(%p,%d)\n", __func__
, objectFileImage
, ordinal
); 
 933         const ImageLoader::Symbol
* sym 
= objectFileImage
->image
->getIndexedImportedSymbol(ordinal
); 
 934         if ( tentative_definition 
!= NULL 
) { 
 935                 ImageLoader::ReferenceFlags flags 
= objectFileImage
->image
->getImportedSymbolInfo(sym
); 
 936                 if ( (flags 
& ImageLoader::kTentativeDefinition
) != 0 ) 
 937                         *tentative_definition 
= true; 
 939                         *tentative_definition 
= false; 
 941         return objectFileImage
->image
->getImportedSymbolName(sym
);       
 944 void* NSGetSectionDataInObjectFileImage(NSObjectFileImage objectFileImage
, 
 945                                                                                 const char* segmentName
, const char* sectionName
, unsigned long* size
) 
 947         if ( dyld::gLogAPIs 
) 
 948                 dyld::log("%s(%p,%s, %s)\n", __func__
, objectFileImage
, segmentName
, sectionName
); 
 952         if ( objectFileImage
->image
->getSectionContent(segmentName
, sectionName
, &start
, &length
) ) { 
 962 bool NSIsSymbolDefinedInObjectFileImage(NSObjectFileImage objectFileImage
, const char* symbolName
) 
 964         if ( dyld::gLogAPIs 
) 
 965                 dyld::log("%s(%p,%s)\n", __func__
, objectFileImage
, symbolName
); 
 966         const ImageLoader::Symbol
* sym 
= objectFileImage
->image
->findExportedSymbol(symbolName
, true, NULL
); 
 967         return ( sym 
!= NULL 
); 
 972 NSModule 
NSLinkModule(NSObjectFileImage objectFileImage
, const char* moduleName
, uint32_t options
) 
 974         if ( dyld::gLogAPIs 
) 
 975                 dyld::log("%s(%p, \"%s\", 0x%08X)\n", __func__
, objectFileImage
, moduleName
, options
);  
 977         dyld::clearErrorMessage(); 
 979                 if ( (options 
& NSLINKMODULE_OPTION_CAN_UNLOAD
) != 0 ) 
 980                         objectFileImage
->image
->setCanUnload(); 
 982                 // NSLinkModule allows a bundle to be link multpile times 
 983                 // each link causes the bundle to be copied to a new address 
 984                 if ( objectFileImage
->image
->isLinked() ) { 
 985                         // already linked, so clone a new one and link it 
 986                         objectFileImage
->image 
= dyld::cloneImage(objectFileImage
->image
); 
 989                 // for memory based images, set moduleName as the name anyone calling _dyld_get_image_name() will see 
 990                 if ( objectFileImage
->image
->getPath() == NULL 
) { 
 991                         objectFileImage
->image
->setPath(moduleName
); 
 992                         // <rdar://problem/8812589> dyld has NULL paths in image info array 
 993                         dyld_image_info info
; 
 994                         info
.imageLoadAddress 
= objectFileImage
->image
->machHeader(); 
 995                         info
.imageFilePath 
= moduleName
; 
 996                         info
.imageFileModDate 
= 0; 
 997                         addImagesToAllImages(1, &info
); 
1000                 // support private bundles 
1001                 if ( (options 
& NSLINKMODULE_OPTION_PRIVATE
) != 0 ) 
1002                         objectFileImage
->image
->setHideExports(); 
1004                 // set up linking options 
1005                 bool forceLazysBound 
= ( (options 
& NSLINKMODULE_OPTION_BINDNOW
) != 0 ); 
1007                 // load libraries, rebase, bind, to make this image usable 
1008                 dyld::link(objectFileImage
->image
, forceLazysBound
, false, ImageLoader::RPathChain(NULL
,NULL
)); 
1010                 // bump reference count to keep this bundle from being garbage collected 
1011                 objectFileImage
->image
->incrementDlopenReferenceCount(); 
1013                 // run initializers unless magic flag says not to 
1014                 if ( (options 
& NSLINKMODULE_OPTION_DONT_CALL_MOD_INIT_ROUTINES
) == 0 ) 
1015                         dyld::runInitializers(objectFileImage
->image
); 
1017                 return ImageLoaderToNSModule(objectFileImage
->image
); 
1019         catch (const char* msg
) { 
1020                 dyld::garbageCollectImages(); 
1021                 if ( (options 
& NSLINKMODULE_OPTION_RETURN_ON_ERROR
) == 0 ) 
1022                         dyldAPIhalt(__func__
, msg
); 
1023                 // not halting, so set error state for NSLinkEditError to find 
1024                 setLastError(NSLinkEditOtherError
, 0, moduleName
, msg
); 
1025                 // dyld::link() deleted the image so lose our reference 
1026                 objectFileImage
->image 
= NULL
; 
1033 #if OLD_LIBSYSTEM_SUPPORT 
1034 // This is for compatibility with old libSystems (libdyld.a) which process ObjectFileImages outside dyld 
1035 static NSModule 
_dyld_link_module(NSObjectFileImage object_addr
, size_t object_size
, const char* moduleName
, uint32_t options
) 
1037         if ( dyld::gLogAPIs 
) 
1038                 dyld::log("%s(%p, \"%s\", 0x%08X)\n", "NSLinkModule", object_addr
,  moduleName
, options
); // note name/args translation 
1039         ImageLoader
* image 
= NULL
; 
1040         dyld::clearErrorMessage(); 
1042                 const char* imageName 
= moduleName
;                      
1043                 image 
= dyld::loadFromMemory((const uint8_t*)object_addr
, object_size
, imageName
);  
1045                 if ( image 
!= NULL 
) {           
1046                         // support private bundles 
1047                         if ( (options 
& NSLINKMODULE_OPTION_PRIVATE
) != 0 ) 
1048                                 image
->setHideExports(); 
1050                         // set up linking options 
1051                         bool forceLazysBound 
= ( (options 
& NSLINKMODULE_OPTION_BINDNOW
) != 0 ); 
1053                         // load libraries, rebase, bind, to make this image usable 
1054                         dyld::link(image
, forceLazysBound
, false, ImageLoader::RPathChain(NULL
,NULL
)); 
1056                         // run initializers unless magic flag says not to 
1057                         if ( (options 
& NSLINKMODULE_OPTION_DONT_CALL_MOD_INIT_ROUTINES
) == 0 ) 
1058                                 dyld::runInitializers(image
); 
1061         catch (const char* msg
) { 
1062                 if ( (options 
& NSLINKMODULE_OPTION_RETURN_ON_ERROR
) == 0 ) 
1063                         dyldAPIhalt("NSLinkModule", msg
); 
1064                 // not halting, so set error state for NSLinkEditError to find 
1065                 setLastError(NSLinkEditOtherError
, 0, moduleName
, msg
); 
1066                 // if image was created for this bundle, destroy it 
1067                 if ( image 
!= NULL 
) { 
1068                         dyld::removeImage(image
); 
1069                         ImageLoader::deleteImage(image
); 
1074         return ImageLoaderToNSModule(image
); 
1078 NSSymbol 
NSLookupSymbolInModule(NSModule 
module, const char* symbolName
) 
1080         if ( dyld::gLogAPIs 
) 
1081                 dyld::log("%s(%p, \"%s\")\n", __func__
, (void *)module, symbolName
); 
1082         ImageLoader
* image 
= NSModuleToImageLoader(module); 
1083         if ( image 
== NULL 
)  
1085         return SymbolToNSSymbol(image
->findExportedSymbol(symbolName
, false, NULL
)); 
1088 const char* NSNameOfModule(NSModule 
module) 
1090         if ( dyld::gLogAPIs 
) 
1091                 dyld::log("%s(%p)\n", __func__
, module); 
1092         ImageLoader
* image 
= NSModuleToImageLoader(module); 
1093         if ( image 
== NULL 
)  
1095         return image
->getPath(); 
1098 const char* NSLibraryNameForModule(NSModule 
module) 
1100         if ( dyld::gLogAPIs 
) 
1101                 dyld::log("%s(%p)\n", __func__
, module); 
1102         ImageLoader
* image 
= NSModuleToImageLoader(module); 
1103         if ( image 
== NULL 
)  
1105         return image
->getPath(); 
1108 bool NSUnLinkModule(NSModule 
module, uint32_t options
) 
1110         if ( dyld::gLogAPIs 
) 
1111                 dyld::log("%s(%p, 0x%08X)\n", __func__
, module, options
);  
1112         if ( module == NULL 
) 
1114         ImageLoader
* image 
= NSModuleToImageLoader(module); 
1115         if ( image 
== NULL 
)  
1117         dyld::runImageTerminators(image
); 
1118         dyld::removeImage(image
); 
1120         if ( (options 
& NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED
) != 0 ) 
1121                 image
->setLeaveMapped(); 
1123         // TODO: NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES 
1125         // Only delete image if there is no ofi referencing it 
1126         // That means the ofi was destroyed after linking, so no one is left to delete this image        
1127         const int ofiCount 
= sObjectFileImages
.size(); 
1129         for (int i
=0; i 
< ofiCount
; ++i
) { 
1130                 NSObjectFileImage ofi 
= sObjectFileImages
[i
]; 
1131                 if ( ofi
->image 
== image 
) 
1135                 ImageLoader::deleteImage(image
); 
1140 // internal name and parameters do not match public name and parameters... 
1141 static void _dyld_install_handlers(void* undefined
, void* multiple
, void* linkEdit
) 
1143         if ( dyld::gLogAPIs 
) 
1144                 dyld::log("NSLinkEditErrorHandlers()\n"); 
1146         dyld::registerUndefinedHandler((dyld::UndefinedHandler
)undefined
); 
1147         // no support for multiple or linkedit handlers 
1153 void NSLinkEditError(NSLinkEditErrors
* c
, int* errorNumber
, const char** fileName
, const char** errorString
) 
1156         *c 
= sLastErrorFileCode
; 
1157         *errorNumber 
= sLastErrorNo
; 
1158         *fileName 
= sLastErrorFilePath
; 
1159         *errorString 
= dyld::getErrorMessage(); 
1164 static void _dyld_register_binding_handler(void * (*bindingHandler
)(const char *, const char *, void *), ImageLoader::BindingOptions bindingOptions
) 
1166         if ( dyld::gLogAPIs 
) 
1167                 dyld::log("%s()\n", __func__
); 
1168         dyld::gLinkContext
.bindingHandler 
= bindingHandler
; 
1169         dyld::gLinkContext
.bindingOptions 
= bindingOptions
; 
1172 #endif //DEPRECATED_APIS_SUPPORTED 
1175 // Call by fork() in libSystem after the kernel trap is done on the child side 
1176 void _dyld_fork_child() 
1178         if ( dyld::gLogAPIs 
) 
1179                 dyld::log("%s()\n", __func__
); 
1180         // The implementation of fork() in libSystem knows to reset the variable mach_task_self_ 
1181         // in libSystem for the child of a fork.  But dyld is built with a static copy 
1182         // of libc.a and has its own copy of mach_task_self_ which we reset here. 
1184         // In mach_init.h mach_task_self() is #defined to mach_task_self_ and 
1185         // in mach_init() mach_task_self_ is initialized to task_self_trap(). 
1187         extern mach_port_t      mach_task_self_
; 
1188         mach_task_self_ 
= task_self_trap(); 
1190         // If dyld is sending load/unload notices to CoreSymbolication, the shared memory 
1191         // page is not copied on fork. <rdar://problem/6797342> 
1192         // NULL the CoreSymbolication shared memory pointer to prevent a crash. 
1193         dyld::gProcessInfo
->coreSymbolicationShmPage 
= NULL
; 
1194         // for safety, make sure child starts with clean systemOrderFlag 
1195         dyld::gProcessInfo
->systemOrderFlag 
= 0; 
1198 typedef void (*MonitorProc
)(char *lowpc
, char *highpc
); 
1200 static void monInitCallback(ImageLoader
* image
, void* userData
) 
1202         MonitorProc proc 
= (MonitorProc
)userData
; 
1205         if ( image
->getSectionContent("__TEXT", "__text", &start
, &length
) ) { 
1206                 proc((char*)start
, (char*)start
+length
); 
1211 // _dyld_moninit is called from profiling runtime routine moninit(). 
1212 // dyld calls back with the range of each __TEXT/__text section in every 
1215 void _dyld_moninit(MonitorProc proc
) 
1217         dyld::forEachImageDo(&monInitCallback
, (void*)proc
); 
1220 #if DEPRECATED_APIS_SUPPORTED 
1221 // returns true if prebinding was used in main executable 
1222 bool _dyld_launched_prebound() 
1224         if ( dyld::gLogAPIs 
) 
1225                 dyld::log("%s()\n", __func__
); 
1227         // ¥¥¥Êif we deprecate prebinding, we may want to consider always returning true or false here 
1228         return dyld::mainExecutablePrebound(); 
1233 // _dyld_NSMakePrivateModulePublic() is the dyld side of the hack 
1234 // NSMakePrivateModulePublic() needed for the dlopen() to turn it's 
1235 // RTLD_LOCAL handles into RTLD_GLOBAL.  It just simply turns off the private 
1236 // flag on the image for this module.  If the module was found and it was 
1237 // private then everything worked and TRUE is returned else FALSE is returned. 
1239 static bool NSMakePrivateModulePublic(NSModule 
module) 
1241         ImageLoader
* image 
= NSModuleToImageLoader(module); 
1242         if ( image 
!= NULL 
) { 
1243                 if ( image
->hasHiddenExports() ) { 
1244                         image
->setHideExports(false); 
1251 #endif // DEPRECATED_APIS_SUPPORTED 
1253 bool lookupDyldFunction(const char* name
, uintptr_t* address
) 
1255         for (const dyld_func
* p 
= dyld_funcs
; p
->name 
!= NULL
; ++p
) { 
1256             if ( strcmp(p
->name
, name
) == 0 ) { 
1257                         if( p
->implementation 
== unimplemented 
) 
1258                                 dyld::log("unimplemented dyld function: %s\n", p
->name
); 
1259                         *address 
= (uintptr_t)p
->implementation
; 
1268 static void registerThreadHelpers(const dyld::LibSystemHelpers
* helpers
) 
1270         dyld::gLibSystemHelpers 
= helpers
; 
1272         // let gdb know it is safe to run code in inferior that might call malloc() 
1273         dyld::gProcessInfo
->libSystemInitialized 
= true;         
1276         if ( helpers
->version 
>= 5 )  { 
1277                 // create key use by dyld exception handling 
1279                 int result 
= helpers
->pthread_key_create(&key
, NULL
); 
1281                         __Unwind_SjLj_SetThreadKey(key
); 
1287 static void dlerrorClear() 
1289         if ( dyld::gLibSystemHelpers 
!= NULL 
) { 
1290                 // <rdar://problem/10595338> dlerror buffer leak 
1291                 // dlerrorClear() should not force allocation, but zero it if already allocated 
1292                 if ( dyld::gLibSystemHelpers
->version 
>= 10 ) { 
1293                         if ( ! (*dyld::gLibSystemHelpers
->hasPerThreadBufferFor_dlerror
)() ) 
1297                 // first char of buffer is flag whether string (starting at second char) is valid 
1298                 char* buffer 
= (*dyld::gLibSystemHelpers
->getThreadBufferFor_dlerror
)(2); 
1304 static void dlerrorSet(const char* msg
) 
1306         if ( dyld::gLibSystemHelpers 
!= NULL 
) { 
1307                 // first char of buffer is flag whether string (starting at second char) is valid 
1308                 char* buffer 
= (*dyld::gLibSystemHelpers
->getThreadBufferFor_dlerror
)(strlen(msg
)+2); 
1310                 strcpy(&buffer
[1], msg
); 
1315 bool dlopen_preflight(const char* path
) 
1317         if ( dyld::gLogAPIs 
) 
1318                 dyld::log("%s(%s)\n", __func__
, path
); 
1322 #if DYLD_SHARED_CACHE_SUPPORT 
1323         // <rdar://problem/5910137> dlopen_preflight() on image in shared cache leaves it loaded but not objc initialized 
1324         // if requested path is to something in the dyld shared cache, always succeed 
1325         if ( dyld::inSharedCache(path
) ) 
1329         CRSetCrashLogMessage("dyld: in dlopen_preflight()"); 
1331         bool result 
= false; 
1332         std::vector
<const char*> rpathsFromCallerImage
; 
1334                 void* callerAddress 
= __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue 
1335                 ImageLoader
* callerImage 
= dyld::findImageContainingAddress(callerAddress
); 
1336                 // for dlopen, use rpath from caller image and from main executable 
1337                 if ( callerImage 
!= NULL 
) 
1338                         callerImage
->getRPaths(dyld::gLinkContext
, rpathsFromCallerImage
); 
1339                 ImageLoader::RPathChain 
callersRPaths(NULL
, &rpathsFromCallerImage
); 
1340                 if ( callerImage 
!= dyld::mainExecutable() ) { 
1341                         dyld::mainExecutable()->getRPaths(dyld::gLinkContext
, rpathsFromCallerImage
); 
1344                 ImageLoader
*    image 
= NULL
; 
1345                 const bool leafName 
= (strchr(path
, '/') == NULL
); 
1346                 const bool absolutePath 
= (path
[0] == '/'); 
1347 #if __IPHONE_OS_VERSION_MIN_REQUIRED 
1348                 char canonicalPath
[PATH_MAX
];  
1349                 // <rdar://problem/7017050> dlopen() not opening frameworks from shared cache with // or ./ in path 
1351                         // make path canonical if it contains a // or ./ 
1352                         if ( (strstr(path
, "//") != NULL
) || (strstr(path
, "./") != NULL
) ) { 
1353                                 const char* lastSlash 
= strrchr(path
, '/'); 
1354                                 char dirPath
[PATH_MAX
];  
1355                                 if ( strlcpy(dirPath
, path
, sizeof(dirPath
)) < sizeof(dirPath
) ) { 
1356                                         dirPath
[lastSlash
-path
] = '\0'; 
1357                                         if ( realpath(dirPath
, canonicalPath
) ) { 
1358                                                 strlcat(canonicalPath
, "/", sizeof(canonicalPath
)); 
1359                                                 if ( strlcat(canonicalPath
, lastSlash
+1, sizeof(canonicalPath
)) < sizeof(canonicalPath
) ) { 
1360                                                         // if all fit in buffer, use new canonical path 
1361                                                         path 
= canonicalPath
; 
1368                 dyld::LoadContext context
; 
1369                 context
.useSearchPaths  
= true; 
1370                 context
.useFallbackPaths
= leafName
;                                     // a partial path implies don't use fallback paths 
1371                 context
.useLdLibraryPath
= leafName
;                                     // a leafname implies should search  
1372                 context
.implicitRPath   
= !absolutePath
;                        // a non-absolute path implies try rpath searching  
1373                 context
.matchByInstallName 
= true; 
1374                 context
.dontLoad                
= false; 
1375                 context
.mustBeBundle    
= false; 
1376                 context
.mustBeDylib             
= false; 
1377                 context
.canBePIE                
= true; 
1378                 context
.origin                  
= callerImage 
!= NULL 
? callerImage
->getPath() : NULL
; // caller's image's path 
1379                 context
.rpath                   
= &callersRPaths
;       // rpaths from caller and main executable 
1381                 image 
= load(path
, context
); 
1382                 if ( image 
!= NULL 
) { 
1383                         dyld::preflight(image
, callersRPaths
);  // image object deleted by dyld::preflight() 
1387         catch (const char* msg
) { 
1388                 const char* str 
= dyld::mkstringf("dlopen_preflight(%s): %s", path
, msg
); 
1391                 free((void*)msg
);       // our free() will do nothing if msg is a string literal 
1393         // free rpaths (getRPaths() malloc'ed each string) 
1394         for(std::vector
<const char*>::iterator it
=rpathsFromCallerImage
.begin(); it 
!= rpathsFromCallerImage
.end(); ++it
) { 
1395                 const char* str 
= *it
; 
1398         CRSetCrashLogMessage(NULL
); 
1403 void* dlopen(const char* path
, int mode
) 
1405         if ( dyld::gLogAPIs 
) 
1406                 dyld::log("%s(%s, 0x%08X)\n", __func__
, ((path
==NULL
) ? "NULL" : path
), mode
); 
1410         // passing NULL for path means return magic object 
1411         if ( path 
== NULL 
) { 
1412                 // RTLD_FIRST means any dlsym() calls on the handle should only search that handle and not subsequent images 
1413                 if ( (mode 
& RTLD_FIRST
) != 0 ) 
1414                         return RTLD_MAIN_ONLY
; 
1416                         return RTLD_DEFAULT
; 
1419         // acquire global dyld lock (dlopen is special - libSystem glue does not do locking) 
1420         bool lockHeld 
= false; 
1421         if ( (dyld::gLibSystemHelpers 
!= NULL
) && (dyld::gLibSystemHelpers
->version 
>= 4) ) { 
1422                 dyld::gLibSystemHelpers
->acquireGlobalDyldLock(); 
1423                 CRSetCrashLogMessage("dyld: in dlopen()"); 
1427         void* result 
= NULL
; 
1428         ImageLoader
* image 
= NULL
; 
1429         std::vector
<const char*> rpathsFromCallerImage
; 
1431                 void* callerAddress 
= __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue 
1432                 ImageLoader
* callerImage 
= dyld::findImageContainingAddress(callerAddress
); 
1433                 // for dlopen, use rpath from caller image and from main executable 
1434                 if ( callerImage 
!= NULL 
) 
1435                         callerImage
->getRPaths(dyld::gLinkContext
, rpathsFromCallerImage
); 
1436                 ImageLoader::RPathChain 
callersRPaths(NULL
, &rpathsFromCallerImage
); 
1437                 if ( callerImage 
!= dyld::mainExecutable() ) { 
1438                         dyld::mainExecutable()->getRPaths(dyld::gLinkContext
, rpathsFromCallerImage
); 
1441                 const bool leafName 
= (strchr(path
, '/') == NULL
); 
1442                 const bool absolutePath 
= (path
[0] == '/'); 
1443 #if __IPHONE_OS_VERSION_MIN_REQUIRED 
1444                 char canonicalPath
[PATH_MAX
];  
1445                 // <rdar://problem/7017050> dlopen() not opening frameworks from shared cache with // or ./ in path 
1447                         // make path canonical if it contains a // or ./ 
1448                         if ( (strstr(path
, "//") != NULL
) || (strstr(path
, "./") != NULL
) ) { 
1449                                 const char* lastSlash 
= strrchr(path
, '/'); 
1450                                 char dirPath
[PATH_MAX
];  
1451                                 if ( strlcpy(dirPath
, path
, sizeof(dirPath
)) < sizeof(dirPath
) ) { 
1452                                         dirPath
[lastSlash
-path
] = '\0'; 
1453                                         if ( realpath(dirPath
, canonicalPath
) ) { 
1454                                                 strlcat(canonicalPath
, "/", sizeof(canonicalPath
)); 
1455                                                 if ( strlcat(canonicalPath
, lastSlash
+1, sizeof(canonicalPath
)) < sizeof(canonicalPath
) ) { 
1456                                                         // if all fit in buffer, use new canonical path 
1457                                                         path 
= canonicalPath
; 
1464                 dyld::LoadContext context
; 
1465                 context
.useSearchPaths  
= true; 
1466                 context
.useFallbackPaths
= leafName
;                             // a partial path means no fallback paths 
1467                 context
.useLdLibraryPath
= leafName
;                             // a leafname implies should search  
1468                 context
.implicitRPath   
= !absolutePath
;                // a non-absolute path implies try rpath searching  
1469                 context
.matchByInstallName 
= true; 
1470                 context
.dontLoad                
= ( (mode 
& RTLD_NOLOAD
) != 0 ); 
1471                 context
.mustBeBundle    
= false; 
1472                 context
.mustBeDylib             
= false; 
1473                 context
.canBePIE                
= true; 
1474                 context
.origin                  
= callerImage 
!= NULL 
? callerImage
->getPath() : NULL
; // caller's image's path 
1475                 context
.rpath                   
= &callersRPaths
;                               // rpaths from caller and main executable 
1477                 image 
= load(path
, context
); 
1478                 if ( image 
!= NULL 
) { 
1479                         // bump reference count.  Do this before link() so that if an initializer calls dlopen and fails 
1480                         // this image is not garbage collected 
1481                         image
->incrementDlopenReferenceCount(); 
1482                         // link in all dependents 
1483                         if ( (mode 
& RTLD_NOLOAD
) == 0 ) { 
1484                                 bool alreadyLinked 
= image
->isLinked(); 
1485                                 bool forceLazysBound 
= ( (mode 
& RTLD_NOW
) != 0 ); 
1486                                 dyld::link(image
, forceLazysBound
, false, callersRPaths
); 
1487                                 if ( ! alreadyLinked 
) { 
1488                                         // only hide exports if image is not already in use 
1489                                         if ( (mode 
& RTLD_LOCAL
) != 0 ) 
1490                                                 image
->setHideExports(true); 
1494                         // RTLD_NODELETE means don't unmap image even after dlclosed. This is what dlcompat did on Mac OS X 10.3 
1495                         // On other *nix OS's, it means dlclose() should do nothing, but the handle should be invalidated.  
1496                         // The subtle differences are:  
1497                         //  1) if the image has any termination routines, whether they are run during dlclose or when the process terminates 
1498                         //  2) If someone does a supsequent dlopen() on the same image, whether the same address should be used.  
1499                         if ( (mode 
& RTLD_NODELETE
) != 0 ) 
1500                                 image
->setLeaveMapped(); 
1502                         // release global dyld lock early, this enables initializers to do threaded operations 
1504                                 CRSetCrashLogMessage(NULL
); 
1505                                 dyld::gLibSystemHelpers
->releaseGlobalDyldLock(); 
1509                         // RTLD_NOLOAD means dlopen should fail unless path is already loaded.  
1510                         // don't run initializers when RTLD_NOLOAD is set.  This only matters if dlopen() is 
1511                         // called from within an initializer because it can cause initializers to run 
1512                         // out of order. Most uses of RTLD_NOLOAD are "probes".  If they want initialzers 
1513                         // to run, then don't use RTLD_NOLOAD. 
1514                         if ( (mode 
& RTLD_NOLOAD
) == 0 ) { 
1516                                 dyld::runInitializers(image
); 
1519                         // RTLD_FIRST means any dlsym() calls on the handle should only search that handle and not subsequent images 
1520                         // this is tracked by setting the low bit of the handle, which is usually zero by malloc alignment 
1521                         if ( (mode 
& RTLD_FIRST
) != 0 ) 
1522                                 result 
= (void*)(((uintptr_t)image
)|1); 
1527         catch (const char* msg
) { 
1528                 if ( image 
!= NULL 
) { 
1529                         // load() succeeded but, link() failed 
1530                         // back down reference count and do GC 
1531                         image
->decrementDlopenReferenceCount(); 
1532                         if ( image
->dlopenCount() == 0 ) 
1533                                 dyld::garbageCollectImages(); 
1535                 const char* str 
= dyld::mkstringf("dlopen(%s, %d): %s", path
, mode
, msg
); 
1536                 if ( dyld::gLogAPIs 
) 
1537                         dyld::log("  %s() failed, error: '%s'\n", __func__
, str
); 
1540                 free((void*)msg
);       // our free() will do nothing if msg is a string literal 
1543         // free rpaths (getRPaths() malloc'ed each string) 
1544         for(std::vector
<const char*>::iterator it
=rpathsFromCallerImage
.begin(); it 
!= rpathsFromCallerImage
.end(); ++it
) { 
1545                 const char* str 
= *it
; 
1549         // when context.dontLoad is set, load() returns NULL instead of throwing an exception  
1550         if ( (mode 
& RTLD_NOLOAD
) && (result 
== NULL
) ) { 
1551                 dlerrorSet("image not already loaded"); 
1555                 CRSetCrashLogMessage(NULL
); 
1556                 dyld::gLibSystemHelpers
->releaseGlobalDyldLock(); 
1558         if ( dyld::gLogAPIs 
&& (result 
!= NULL
) ) 
1559                 dyld::log("  %s(%s) ==> %p\n", __func__
, path
, result
); 
1565 int dlclose(void* handle
) 
1567         if ( dyld::gLogAPIs 
) 
1568                 dyld::log("%s(%p)\n", __func__
, handle
); 
1570         // silently accept magic handles for main executable 
1571         if ( handle 
== RTLD_MAIN_ONLY 
) 
1573         if ( handle 
== RTLD_DEFAULT 
) 
1576         ImageLoader
* image 
= (ImageLoader
*)(((uintptr_t)handle
) & (-4));        // clear mode bits 
1577         if ( dyld::validImage(image
) ) { 
1579                 // decrement use count 
1580                 if ( image
->decrementDlopenReferenceCount() ) { 
1581                         dlerrorSet("dlclose() called too many times"); 
1584                 // remove image if reference count went to zero 
1585                 if ( image
->dlopenCount() == 0 ) 
1586                         dyld::garbageCollectImages(); 
1590                 dlerrorSet("invalid handle passed to dlclose()"); 
1597 int dladdr(const void* address
, Dl_info
* info
) 
1599         if ( dyld::gLogAPIs 
) 
1600                 dyld::log("%s(%p, %p)\n", __func__
, address
, info
); 
1602         CRSetCrashLogMessage("dyld: in dladdr()"); 
1603         ImageLoader
* image 
= dyld::findImageContainingAddress(address
); 
1604         if ( image 
!= NULL 
) { 
1605                 info
->dli_fname 
= image
->getRealPath(); 
1606                 info
->dli_fbase 
= (void*)image
->machHeader(); 
1607                 if ( address 
== info
->dli_fbase 
) { 
1608                         // special case lookup of header 
1609                         info
->dli_sname 
= "__dso_handle"; 
1610                         info
->dli_saddr 
= info
->dli_fbase
; 
1611                         CRSetCrashLogMessage(NULL
); 
1612                         return 1; // success 
1614                 // find closest symbol in the image 
1615                 info
->dli_sname 
= image
->findClosestSymbol(address
, (const void**)&info
->dli_saddr
); 
1616                 // never return the mach_header symbol 
1617                 if ( info
->dli_saddr 
== info
->dli_fbase 
) { 
1618                         info
->dli_sname 
= NULL
; 
1619                         info
->dli_saddr 
= NULL
; 
1620                         CRSetCrashLogMessage(NULL
); 
1621                         return 1; // success 
1623                 if ( info
->dli_sname 
!= NULL 
) { 
1624                         if ( info
->dli_sname
[0] == '_' ) 
1625                                 info
->dli_sname 
= info
->dli_sname 
+1; // strip off leading underscore 
1626                         //dyld::log("dladdr(%p) => %p %s\n", address, info->dli_saddr, info->dli_sname); 
1627                         CRSetCrashLogMessage(NULL
); 
1628                         return 1; // success 
1630                 info
->dli_sname 
= NULL
; 
1631                 info
->dli_saddr 
= NULL
; 
1632                 CRSetCrashLogMessage(NULL
); 
1633                 return 1; // success 
1635         CRSetCrashLogMessage(NULL
); 
1636         return 0;  // failure 
1642         if ( dyld::gLogAPIs 
) 
1643                 dyld::log("%s()\n", __func__
); 
1645         if ( dyld::gLibSystemHelpers 
!= NULL 
) { 
1646                 // if using newer libdyld.dylib and buffer if buffer not yet allocated, return NULL 
1647                 if ( dyld::gLibSystemHelpers
->version 
>= 10 ) { 
1648                         if ( ! (*dyld::gLibSystemHelpers
->hasPerThreadBufferFor_dlerror
)() ) 
1652                 // first char of buffer is flag whether string (starting at second char) is valid 
1653                 char* buffer 
= (*dyld::gLibSystemHelpers
->getThreadBufferFor_dlerror
)(2); 
1654                 if ( buffer
[0] != '\0' ) {      // if valid buffer 
1655                         buffer
[0] = '\0';               // mark invalid, so next call to dlerror returns NULL 
1656                         return &buffer
[1];              // return message 
1662 void* dlsym(void* handle
, const char* symbolName
) 
1664         if ( dyld::gLogAPIs 
) 
1665                 dyld::log("%s(%p, %s)\n", __func__
, handle
, symbolName
); 
1667         CRSetCrashLogMessage("dyld: in dlsym()"); 
1670         const ImageLoader
* image
; 
1671         const ImageLoader::Symbol
* sym
; 
1673         // dlsym() assumes symbolName passed in is same as in C source code 
1674         // dyld assumes all symbol names have an underscore prefix 
1675         char underscoredName
[strlen(symbolName
)+2]; 
1676         underscoredName
[0] = '_'; 
1677         strcpy(&underscoredName
[1], symbolName
); 
1679         // magic "search all" handle 
1680         if ( handle 
== RTLD_DEFAULT 
) { 
1681                 if ( dyld::flatFindExportedSymbol(underscoredName
, &sym
, &image
) ) { 
1682                         CRSetCrashLogMessage(NULL
); 
1683                         return (void*)image
->getExportedSymbolAddress(sym
, dyld::gLinkContext
); 
1685                 const char* str 
= dyld::mkstringf("dlsym(RTLD_DEFAULT, %s): symbol not found", symbolName
); 
1688                 CRSetCrashLogMessage(NULL
); 
1692         // magic "search only main executable" handle 
1693         if ( handle 
== RTLD_MAIN_ONLY 
) { 
1694                 image 
= dyld::mainExecutable(); 
1695                 sym 
= image
->findExportedSymbol(underscoredName
, true, &image
); // search RTLD_FIRST way 
1696                 if ( sym 
!= NULL 
) { 
1697                         CRSetCrashLogMessage(NULL
); 
1698                         return (void*)image
->getExportedSymbolAddress(sym
, dyld::gLinkContext
); 
1700                 const char* str 
= dyld::mkstringf("dlsym(RTLD_MAIN_ONLY, %s): symbol not found", symbolName
); 
1703                 CRSetCrashLogMessage(NULL
); 
1707         // magic "search what I would see" handle 
1708         if ( handle 
== RTLD_NEXT 
) { 
1709                 void* callerAddress 
= __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue 
1710                 ImageLoader
* callerImage 
= dyld::findImageContainingAddress(callerAddress
); 
1711                 sym 
= callerImage
->findExportedSymbolInDependentImages(underscoredName
, dyld::gLinkContext
, &image
); // don't search image, but do search what it links against 
1712                 if ( sym 
!= NULL 
) { 
1713                         CRSetCrashLogMessage(NULL
); 
1714                         return (void*)image
->getExportedSymbolAddress(sym
, dyld::gLinkContext
); 
1716                 const char* str 
= dyld::mkstringf("dlsym(RTLD_NEXT, %s): symbol not found", symbolName
); 
1719                 CRSetCrashLogMessage(NULL
); 
1722         // magic "search me, then what I would see" handle 
1723         if ( handle 
== RTLD_SELF 
) { 
1724                 void* callerAddress 
= __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue 
1725                 ImageLoader
* callerImage 
= dyld::findImageContainingAddress(callerAddress
); 
1726                 sym 
= callerImage
->findExportedSymbolInImageOrDependentImages(underscoredName
, dyld::gLinkContext
, &image
); // search image and what it links against 
1727                 if ( sym 
!= NULL 
) { 
1728                         CRSetCrashLogMessage(NULL
); 
1729                         return (void*)image
->getExportedSymbolAddress(sym
, dyld::gLinkContext
); 
1731                 const char* str 
= dyld::mkstringf("dlsym(RTLD_SELF, %s): symbol not found", symbolName
); 
1734                 CRSetCrashLogMessage(NULL
); 
1738         image 
= (ImageLoader
*)(((uintptr_t)handle
) & (-4));     // clear mode bits 
1739         if ( dyld::validImage(image
) ) { 
1740                 if ( (((uintptr_t)handle
) & 1) != 0 ) 
1741                         sym 
= image
->findExportedSymbol(underscoredName
, true, &image
); // search RTLD_FIRST way 
1743                         sym 
= image
->findExportedSymbolInImageOrDependentImages(underscoredName
, dyld::gLinkContext
, &image
); // search image and what it links against 
1745                 if ( sym 
!= NULL 
) { 
1746                         CRSetCrashLogMessage(NULL
); 
1747                         return (void*)image
->getExportedSymbolAddress(sym
, dyld::gLinkContext
); 
1749                 const char* str 
= dyld::mkstringf("dlsym(%p, %s): symbol not found", handle
, symbolName
); 
1754                 dlerrorSet("invalid handle passed to dlsym()"); 
1756         CRSetCrashLogMessage(NULL
); 
1769 const struct dyld_all_image_infos
* _dyld_get_all_image_infos() 
1771         return dyld::gProcessInfo
; 
1775 static bool client_dyld_find_unwind_sections(void* addr
, dyld_unwind_sections
* info
) 
1777         //if ( dyld::gLogAPIs ) 
1778         //      dyld::log("%s(%p, %p)\n", __func__, addr, info); 
1780         ImageLoader
* image 
= dyld::findImageContainingAddress(addr
); 
1781         if ( image 
!= NULL 
) { 
1782                 image
->getUnwindInfo(info
); 
1790 void dyld_register_image_state_change_handler(dyld_image_states state
, bool batch
,  
1791                                                                                         dyld_image_state_change_handler handler
) 
1793         if ( dyld::gLogAPIs 
) 
1794                 dyld::log("%s(%d, %d, %p)\n", __func__
, state
, batch
, handler
); 
1796                 dyld::registerImageStateBatchChangeHandler(state
, handler
); 
1798                 dyld::registerImageStateSingleChangeHandler(state
, handler
); 
1801 const char* dyld_image_path_containing_address(const void* address
) 
1803         if ( dyld::gLogAPIs 
) 
1804                 dyld::log("%s(%p)\n", __func__
, address
); 
1806         ImageLoader
* image 
= dyld::findImageContainingAddress(address
); 
1807         if ( image 
!= NULL 
) 
1808                 return image
->getRealPath(); 
1814 #if __IPHONE_OS_VERSION_MIN_REQUIRED     
1815 bool dyld_shared_cache_some_image_overridden() 
1817  #if DYLD_SHARED_CACHE_SUPPORT 
1818         return dyld::gSharedCacheOverridden
;