Loading...
--- dyld/dyld-195.5/src/dyldAPIs.cpp
+++ dyld/dyld-360.22/src/dyldAPIs.cpp
@@ -70,6 +70,7 @@
#define DEPRECATED_APIS_SUPPORTED 1
#endif
+static bool sDynamicInterposing = false;
#if DEPRECATED_APIS_SUPPORTED
static char sLastErrorFilePath[1024];
@@ -80,8 +81,7 @@
// In 10.3.x and earlier all the NSObjectFileImage API's were implemeneted in libSystem.dylib
// Beginning in 10.4 the NSObjectFileImage API's are implemented in dyld and libSystem just forwards
// This conditional keeps support for old libSystem's which needed some help implementing the API's
-#define OLD_LIBSYSTEM_SUPPORT (__ppc__ || __i386__)
-
+#define OLD_LIBSYSTEM_SUPPORT (__i386__)
// The following functions have no prototype in any header. They are special cases
// where _dyld_func_lookup() is used directly.
@@ -101,7 +101,7 @@
static void client_dyld_lookup_and_bind(const char* symbolName, void** address, NSModule* module);
static bool client_NSIsSymbolNameDefined(const char* symbolName);
#endif // DEPRECATED_APIS_SUPPORTED
-#if !__arm__
+#if SUPPORT_ZERO_COST_EXCEPTIONS
static bool client_dyld_find_unwind_sections(void* addr, dyld_unwind_sections* info);
#endif
@@ -135,23 +135,25 @@
{"__dyld_dyld_register_image_state_change_handler", (void*)dyld_register_image_state_change_handler },
{"__dyld_register_thread_helpers", (void*)registerThreadHelpers },
{"__dyld_fork_child", (void*)_dyld_fork_child },
- {"__dyld_moninit", (void*)_dyld_moninit },
{"__dyld_make_delayed_module_initializer_calls", (void*)_dyld_make_delayed_module_initializer_calls },
{"__dyld_get_all_image_infos", (void*)_dyld_get_all_image_infos },
-#if !__arm__
+#if SUPPORT_ZERO_COST_EXCEPTIONS
{"__dyld_find_unwind_sections", (void*)client_dyld_find_unwind_sections },
#endif
-#if __i386__ || __x86_64__ || __arm__
+#if __i386__ || __x86_64__ || __arm__ || __arm64__
{"__dyld_fast_stub_entry", (void*)dyld::fastBindLazySymbol },
#endif
{"__dyld_image_path_containing_address", (void*)dyld_image_path_containing_address },
-#if __IPHONE_OS_VERSION_MIN_REQUIRED
{"__dyld_shared_cache_some_image_overridden", (void*)dyld_shared_cache_some_image_overridden },
+ {"__dyld_process_is_restricted", (void*)dyld::processIsRestricted },
+ {"__dyld_dynamic_interpose", (void*)dyld_dynamic_interpose },
+#if DYLD_SHARED_CACHE_SUPPORT
+ {"__dyld_shared_cache_file_path", (void*)dyld::getStandardSharedCacheFilePath },
#endif
+ {"__dyld_get_image_header_containing_address", (void*)dyld_image_header_containing_address },
// deprecated
#if DEPRECATED_APIS_SUPPORTED
- {"__dyld_get_image_header_containing_address", (void*)_dyld_get_image_header_containing_address },
{"__dyld_lookup_and_bind", (void*)client_dyld_lookup_and_bind },
{"__dyld_lookup_and_bind_with_hint", (void*)_dyld_lookup_and_bind_with_hint },
{"__dyld_lookup_and_bind_fully", (void*)_dyld_lookup_and_bind_fully },
@@ -240,6 +242,9 @@
const void* imageBaseAddress; // not used with OFI created from files
size_t imageLength; // not used with OFI created from files
};
+
+
+VECTOR_NEVER_DESTRUCTED(NSObjectFileImage);
static std::vector<NSObjectFileImage> sObjectFileImages;
@@ -280,7 +285,7 @@
dyld::log("%s(...)\n", __func__);
const char* exePath = dyld::getExecutablePath();
if(*bufsize < strlen(exePath) + 1){
- *bufsize = strlen(exePath) + 1;
+ *bufsize = (uint32_t)(strlen(exePath) + 1);
return -1;
}
strcpy(buf, exePath);
@@ -334,12 +339,12 @@
dyld::log("%s(%u)\n", __func__, image_index);
ImageLoader* image = dyld::getIndexedImage(image_index);
if ( image != NULL )
- return image->getPath();
+ return image->getRealPath();
else
return NULL;
}
-const struct mach_header * _dyld_get_image_header_containing_address(const void* address)
+const struct mach_header * dyld_image_header_containing_address(const void* address)
{
if ( dyld::gLogAPIs )
dyld::log("%s(%p)\n", __func__, address);
@@ -543,7 +548,7 @@
if ( image != NULL ) {
if ( context.matchByInstallName )
image->setMatchInstallPath(true);
- dyld::link(image, false, callersRPaths);
+ dyld::link(image, false, false, callersRPaths);
dyld::runInitializers(image);
// images added with NSAddImage() can never be unloaded
image->setNeverUnload();
@@ -582,7 +587,7 @@
const bool dontLoad = ( (options & NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED) != 0 );
const bool search = ( (options & NSADDIMAGE_OPTION_WITH_SEARCHING) != 0 );
const bool matchInstallName = ( (options & NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME) != 0 );
- const bool abortOnError = ( (options & NSADDIMAGE_OPTION_RETURN_ON_ERROR|NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED) == 0 );
+ const bool abortOnError = ( (options & (NSADDIMAGE_OPTION_RETURN_ON_ERROR|NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED)) == 0 );
void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
return addImage(callerAddress, path, search, dontLoad, matchInstallName, abortOnError);
}
@@ -973,6 +978,9 @@
dyld::clearErrorMessage();
try {
+ if ( (options & NSLINKMODULE_OPTION_CAN_UNLOAD) != 0 )
+ objectFileImage->image->setCanUnload();
+
// NSLinkModule allows a bundle to be link multpile times
// each link causes the bundle to be copied to a new address
if ( objectFileImage->image->isLinked() ) {
@@ -999,7 +1007,7 @@
bool forceLazysBound = ( (options & NSLINKMODULE_OPTION_BINDNOW) != 0 );
// load libraries, rebase, bind, to make this image usable
- dyld::link(objectFileImage->image, forceLazysBound, ImageLoader::RPathChain(NULL,NULL));
+ dyld::link(objectFileImage->image, forceLazysBound, false, ImageLoader::RPathChain(NULL,NULL));
// bump reference count to keep this bundle from being garbage collected
objectFileImage->image->incrementDlopenReferenceCount();
@@ -1045,7 +1053,7 @@
bool forceLazysBound = ( (options & NSLINKMODULE_OPTION_BINDNOW) != 0 );
// load libraries, rebase, bind, to make this image usable
- dyld::link(image, forceLazysBound, ImageLoader::RPathChain(NULL,NULL));
+ dyld::link(image, forceLazysBound, false, ImageLoader::RPathChain(NULL,NULL));
// run initializers unless magic flag says not to
if ( (options & NSLINKMODULE_OPTION_DONT_CALL_MOD_INIT_ROUTINES) == 0 )
@@ -1108,6 +1116,19 @@
ImageLoader* image = NSModuleToImageLoader(module);
if ( image == NULL )
return false;
+ dyld::runImageStaticTerminators(image);
+ if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 13) ) {
+ __cxa_range_t ranges[image->segmentCount()];
+ int rangeCount = 0;
+ for (unsigned int j=0; j < image->segmentCount(); ++j) {
+ if ( !image->segExecutable(j) )
+ continue;
+ ranges[rangeCount].addr = (const void*)image->segActualLoadAddress(j);
+ ranges[rangeCount].length = image->segSize(j);
+ ++rangeCount;
+ }
+ (*dyld::gLibSystemHelpers->cxa_finalize_ranges)(ranges, rangeCount);
+ }
dyld::removeImage(image);
if ( (options & NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED) != 0 )
@@ -1183,34 +1204,11 @@
// If dyld is sending load/unload notices to CoreSymbolication, the shared memory
// page is not copied on fork. <rdar://problem/6797342>
// NULL the CoreSymbolication shared memory pointer to prevent a crash.
- dyld_all_image_infos.coreSymbolicationShmPage = NULL;
+ dyld::gProcessInfo->coreSymbolicationShmPage = NULL;
// for safety, make sure child starts with clean systemOrderFlag
- dyld_all_image_infos.systemOrderFlag = 0;
-}
-
-
-
-typedef void (*MonitorProc)(char *lowpc, char *highpc);
-
-static void monInitCallback(ImageLoader* image, void* userData)
-{
- MonitorProc proc = (MonitorProc)userData;
- void* start;
- size_t length;
- if ( image->getSectionContent("__TEXT", "__text", &start, &length) ) {
- proc((char*)start, (char*)start+length);
- }
-}
-
-//
-// _dyld_moninit is called from profiling runtime routine moninit().
-// dyld calls back with the range of each __TEXT/__text section in every
-// linked image.
-//
-void _dyld_moninit(MonitorProc proc)
-{
- dyld::forEachImageDo(&monInitCallback, (void*)proc);
-}
+ dyld::gProcessInfo->systemOrderFlag = 0;
+}
+
#if DEPRECATED_APIS_SUPPORTED
// returns true if prebinding was used in main executable
@@ -1245,13 +1243,13 @@
#endif // DEPRECATED_APIS_SUPPORTED
-bool lookupDyldFunction(const char* name, uintptr_t* address)
+int _dyld_func_lookup(const char* name, void** address)
{
for (const dyld_func* p = dyld_funcs; p->name != NULL; ++p) {
if ( strcmp(p->name, name) == 0 ) {
if( p->implementation == unimplemented )
dyld::log("unimplemented dyld function: %s\n", p->name);
- *address = (uintptr_t)p->implementation;
+ *address = p->implementation;
return true;
}
}
@@ -1265,9 +1263,9 @@
dyld::gLibSystemHelpers = helpers;
// let gdb know it is safe to run code in inferior that might call malloc()
- dyld_all_image_infos.libSystemInitialized = true;
-
-#if __arm__
+ dyld::gProcessInfo->libSystemInitialized = true;
+
+#if !SUPPORT_ZERO_COST_EXCEPTIONS
if ( helpers->version >= 5 ) {
// create key use by dyld exception handling
pthread_key_t key;
@@ -1282,6 +1280,13 @@
static void dlerrorClear()
{
if ( dyld::gLibSystemHelpers != NULL ) {
+ // <rdar://problem/10595338> dlerror buffer leak
+ // dlerrorClear() should not force allocation, but zero it if already allocated
+ if ( dyld::gLibSystemHelpers->version >= 10 ) {
+ if ( ! (*dyld::gLibSystemHelpers->hasPerThreadBufferFor_dlerror)() )
+ return;
+ }
+
// first char of buffer is flag whether string (starting at second char) is valid
char* buffer = (*dyld::gLibSystemHelpers->getThreadBufferFor_dlerror)(2);
buffer[0] = '\0';
@@ -1415,15 +1420,16 @@
void* result = NULL;
ImageLoader* image = NULL;
std::vector<const char*> rpathsFromCallerImage;
+ ImageLoader::RPathChain callersRPaths(NULL, &rpathsFromCallerImage);
try {
void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
ImageLoader* callerImage = dyld::findImageContainingAddress(callerAddress);
- // for dlopen, use rpath from caller image and from main executable
- if ( callerImage != NULL )
- callerImage->getRPaths(dyld::gLinkContext, rpathsFromCallerImage);
- ImageLoader::RPathChain callersRPaths(NULL, &rpathsFromCallerImage);
- if ( callerImage != dyld::mainExecutable() ) {
- dyld::mainExecutable()->getRPaths(dyld::gLinkContext, rpathsFromCallerImage);
+ if ( (mode & RTLD_NOLOAD) == 0 ) {
+ // for dlopen, use rpath from caller image and from main executable
+ if ( callerImage != NULL )
+ callerImage->getRPaths(dyld::gLinkContext, rpathsFromCallerImage);
+ if ( callerImage != dyld::mainExecutable() )
+ dyld::mainExecutable()->getRPaths(dyld::gLinkContext, rpathsFromCallerImage);
}
const bool leafName = (strchr(path, '/') == NULL);
@@ -1471,7 +1477,7 @@
if ( (mode & RTLD_NOLOAD) == 0 ) {
bool alreadyLinked = image->isLinked();
bool forceLazysBound = ( (mode & RTLD_NOW) != 0 );
- dyld::link(image, forceLazysBound, callersRPaths);
+ dyld::link(image, forceLazysBound, false, callersRPaths);
if ( ! alreadyLinked ) {
// only hide exports if image is not already in use
if ( (mode & RTLD_LOCAL) != 0 )
@@ -1517,9 +1523,12 @@
// load() succeeded but, link() failed
// back down reference count and do GC
image->decrementDlopenReferenceCount();
- dyld::garbageCollectImages();
+ if ( image->dlopenCount() == 0 )
+ dyld::garbageCollectImages();
}
const char* str = dyld::mkstringf("dlopen(%s, %d): %s", path, mode, msg);
+ if ( dyld::gLogAPIs )
+ dyld::log(" %s() failed, error: '%s'\n", __func__, str);
dlerrorSet(str);
free((void*)str);
free((void*)msg); // our free() will do nothing if msg is a string literal
@@ -1540,6 +1549,8 @@
CRSetCrashLogMessage(NULL);
dyld::gLibSystemHelpers->releaseGlobalDyldLock();
}
+ if ( dyld::gLogAPIs && (result != NULL) )
+ dyld::log(" %s(%s) ==> %p\n", __func__, path, result);
return result;
}
@@ -1565,7 +1576,8 @@
return -1;
}
// remove image if reference count went to zero
- dyld::garbageCollectImages();
+ if ( image->dlopenCount() == 0 )
+ dyld::garbageCollectImages();
return 0;
}
else {
@@ -1584,7 +1596,7 @@
CRSetCrashLogMessage("dyld: in dladdr()");
ImageLoader* image = dyld::findImageContainingAddress(address);
if ( image != NULL ) {
- info->dli_fname = image->getPath();
+ info->dli_fname = image->getRealPath();
info->dli_fbase = (void*)image->machHeader();
if ( address == info->dli_fbase ) {
// special case lookup of header
@@ -1595,6 +1607,13 @@
}
// find closest symbol in the image
info->dli_sname = image->findClosestSymbol(address, (const void**)&info->dli_saddr);
+ // never return the mach_header symbol
+ if ( info->dli_saddr == info->dli_fbase ) {
+ info->dli_sname = NULL;
+ info->dli_saddr = NULL;
+ CRSetCrashLogMessage(NULL);
+ return 1; // success
+ }
if ( info->dli_sname != NULL ) {
if ( info->dli_sname[0] == '_' )
info->dli_sname = info->dli_sname +1; // strip off leading underscore
@@ -1618,6 +1637,12 @@
dyld::log("%s()\n", __func__);
if ( dyld::gLibSystemHelpers != NULL ) {
+ // if using newer libdyld.dylib and buffer if buffer not yet allocated, return NULL
+ if ( dyld::gLibSystemHelpers->version >= 10 ) {
+ if ( ! (*dyld::gLibSystemHelpers->hasPerThreadBufferFor_dlerror)() )
+ return NULL;
+ }
+
// first char of buffer is flag whether string (starting at second char) is valid
char* buffer = (*dyld::gLibSystemHelpers->getThreadBufferFor_dlerror)(2);
if ( buffer[0] != '\0' ) { // if valid buffer
@@ -1675,15 +1700,7 @@
// magic "search what I would see" handle
if ( handle == RTLD_NEXT ) {
-#if __ppc__
- // <rdar://problem/7628929> work around for llvmgcc bug
- void* fa = __builtin_frame_address(0);
- fa = *(void**)fa;
- fa = *(void**)fa;
- void* callerAddress = *((void**)(((int)fa)+8));
-#else
void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
-#endif
ImageLoader* callerImage = dyld::findImageContainingAddress(callerAddress);
sym = callerImage->findExportedSymbolInDependentImages(underscoredName, dyld::gLinkContext, &image); // don't search image, but do search what it links against
if ( sym != NULL ) {
@@ -1721,7 +1738,13 @@
if ( sym != NULL ) {
CRSetCrashLogMessage(NULL);
- return (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext);
+ ImageLoader* callerImage = NULL;
+ if ( sDynamicInterposing ) {
+ // only take time to look up caller, if dynamic interposing in use
+ void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue
+ callerImage = dyld::findImageContainingAddress(callerAddress);
+ }
+ return (void*)image->getExportedSymbolAddress(sym, dyld::gLinkContext, callerImage);
}
const char* str = dyld::mkstringf("dlsym(%p, %s): symbol not found", handle, symbolName);
dlerrorSet(str);
@@ -1745,10 +1768,10 @@
const struct dyld_all_image_infos* _dyld_get_all_image_infos()
{
- return &dyld_all_image_infos;
-}
-
-#if !__arm__
+ return dyld::gProcessInfo;
+}
+
+#if SUPPORT_ZERO_COST_EXCEPTIONS
static bool client_dyld_find_unwind_sections(void* addr, dyld_unwind_sections* info)
{
//if ( dyld::gLogAPIs )
@@ -1782,18 +1805,46 @@
ImageLoader* image = dyld::findImageContainingAddress(address);
if ( image != NULL )
- return image->getPath();
+ return image->getRealPath();
return NULL;
}
-#if __IPHONE_OS_VERSION_MIN_REQUIRED
bool dyld_shared_cache_some_image_overridden()
{
+ #if DYLD_SHARED_CACHE_SUPPORT
return dyld::gSharedCacheOverridden;
-}
-#endif
-
-
-
+ #else
+ return true;
+ #endif
+}
+
+
+void dyld_dynamic_interpose(const struct mach_header* mh, const struct dyld_interpose_tuple array[], size_t count)
+{
+ if ( mh == NULL )
+ return;
+ if ( array == NULL )
+ return;
+ if ( count == 0 )
+ return;
+ ImageLoader* image = dyld::findImageByMachHeader(mh);
+ if ( image == NULL )
+ return;
+
+ // make pass at bound references in this image and update them
+ dyld::gLinkContext.dynamicInterposeArray = array;
+ dyld::gLinkContext.dynamicInterposeCount = count;
+ image->dynamicInterpose(dyld::gLinkContext);
+ dyld::gLinkContext.dynamicInterposeArray = NULL;
+ dyld::gLinkContext.dynamicInterposeCount = 0;
+
+ // leave interposing info so any future (lazy) binding will get it too
+ image->addDynamicInterposingTuples(array, count);
+
+ sDynamicInterposing = true;
+}
+
+
+