#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/param.h>
#include <mach/mach_time.h> // mach_absolute_time()
#include <mach/mach_init.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/syslog.h>
#include <sys/uio.h>
#include <mach-o/fat.h>
#include <mach-o/loader.h>
#include <mach-o/ldsyms.h>
#include <libkern/OSByteOrder.h>
#include <libkern/OSAtomic.h>
#include <mach/mach.h>
#include <sys/sysctl.h>
#include <sys/mman.h>
#include <sys/dtrace.h>
#include <libkern/OSAtomic.h>
#include <Availability.h>
#include <System/sys/codesign.h>
#include <_simple.h>
#include <os/lock_private.h>
#ifndef CPU_SUBTYPE_ARM_V5TEJ
#define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
#endif
#ifndef CPU_SUBTYPE_ARM_XSCALE
#define CPU_SUBTYPE_ARM_XSCALE ((cpu_subtype_t) 8)
#endif
#ifndef CPU_SUBTYPE_ARM_V7
#define CPU_SUBTYPE_ARM_V7 ((cpu_subtype_t) 9)
#endif
#ifndef CPU_SUBTYPE_ARM_V7F
#define CPU_SUBTYPE_ARM_V7F ((cpu_subtype_t) 10)
#endif
#ifndef CPU_SUBTYPE_ARM_V7S
#define CPU_SUBTYPE_ARM_V7S ((cpu_subtype_t) 11)
#endif
#ifndef CPU_SUBTYPE_ARM_V7K
#define CPU_SUBTYPE_ARM_V7K ((cpu_subtype_t) 12)
#endif
#ifndef LC_DYLD_ENVIRONMENT
#define LC_DYLD_ENVIRONMENT 0x27
#endif
#ifndef CPU_SUBTYPE_X86_64_H
#define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t) 8)
#endif
#ifndef VM_PROT_SLIDE
#define VM_PROT_SLIDE 0x20
#endif
#include <vector>
#include <algorithm>
#include "mach-o/dyld_gdb.h"
#include "dyld.h"
#include "ImageLoader.h"
#include "ImageLoaderMachO.h"
#include "dyldLibSystemInterface.h"
#include "dyldSyscallInterface.h"
#if DYLD_SHARED_CACHE_SUPPORT
#include "dyld_cache_format.h"
#endif
#if TARGET_IPHONE_SIMULATOR
void coresymbolication_load_image(void*, const ImageLoader*, uint64_t);
void coresymbolication_unload_image(void*, const ImageLoader*);
#else
#include "coreSymbolicationDyldSupport.hpp"
#endif
extern "C" ssize_t __sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t);
#define CPU_SUBTYPES_SUPPORTED ((__arm__ || __x86_64__) && !TARGET_IPHONE_SIMULATOR)
#define CPU_TYPE_MASK 0x00FFFFFF
extern void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[]);
extern void removeImageFromAllImages(const mach_header* mh);
extern void setAlImageInfosHalt(const char* message, uintptr_t flags);
extern void addNonSharedCacheImageUUID(const dyld_uuid_info& info);
extern const char* notifyGDB(enum dyld_image_states state, uint32_t infoCount, const dyld_image_info info[]);
extern "C" {
char error_string[1024];
}
extern "C" void dyld_fatal_error(const char* errString) __attribute__((noreturn));
extern "C" const macho_header __dso_handle;
namespace dyld {
struct RegisteredDOF { const mach_header* mh; int registrationID; };
struct DylibOverride { const char* installName; const char* override; };
}
VECTOR_NEVER_DESTRUCTED(ImageLoader*);
VECTOR_NEVER_DESTRUCTED(dyld::RegisteredDOF);
VECTOR_NEVER_DESTRUCTED(dyld::ImageCallback);
VECTOR_NEVER_DESTRUCTED(dyld::DylibOverride);
VECTOR_NEVER_DESTRUCTED(ImageLoader::DynamicReference);
VECTOR_NEVER_DESTRUCTED(dyld_image_state_change_handler);
namespace dyld {
struct EnvironmentVariables {
const char* const * DYLD_FRAMEWORK_PATH;
const char* const * DYLD_FALLBACK_FRAMEWORK_PATH;
const char* const * DYLD_LIBRARY_PATH;
const char* const * DYLD_FALLBACK_LIBRARY_PATH;
const char* const * DYLD_INSERT_LIBRARIES;
const char* const * LD_LIBRARY_PATH; const char* const * DYLD_VERSIONED_LIBRARY_PATH;
const char* const * DYLD_VERSIONED_FRAMEWORK_PATH;
bool DYLD_PRINT_LIBRARIES;
bool DYLD_PRINT_LIBRARIES_POST_LAUNCH;
bool DYLD_BIND_AT_LAUNCH;
bool DYLD_PRINT_STATISTICS;
bool DYLD_PRINT_OPTS;
bool DYLD_PRINT_ENV;
bool DYLD_DISABLE_DOFS;
bool DYLD_PRINT_CS_NOTIFICATIONS;
};
typedef std::vector<dyld_image_state_change_handler> StateHandlers;
enum RestrictedReason { restrictedNot, restrictedBySetGUid, restrictedBySegment, restrictedByEntitlements };
static const char* sExecPath = NULL;
static const char* sExecShortName = NULL;
static const macho_header* sMainExecutableMachHeader = NULL;
#if CPU_SUBTYPES_SUPPORTED
static cpu_type_t sHostCPU;
static cpu_subtype_t sHostCPUsubtype;
#endif
static ImageLoader* sMainExecutable = NULL;
static bool sProcessIsRestricted = false;
static RestrictedReason sRestrictedReason = restrictedNot;
static size_t sInsertedDylibCount = 0;
static std::vector<ImageLoader*> sAllImages;
static std::vector<ImageLoader*> sImageRoots;
static std::vector<ImageLoader*> sImageFilesNeedingTermination;
static std::vector<RegisteredDOF> sImageFilesNeedingDOFUnregistration;
static std::vector<ImageCallback> sAddImageCallbacks;
static std::vector<ImageCallback> sRemoveImageCallbacks;
static bool sRemoveImageCallbacksInUse = false;
static void* sSingleHandlers[7][3];
static void* sBatchHandlers[7][3];
static ImageLoader* sLastImageByAddressCache;
static EnvironmentVariables sEnv;
static const char* sFrameworkFallbackPaths[] = { "$HOME/Library/Frameworks", "/Library/Frameworks", "/Network/Library/Frameworks", "/System/Library/Frameworks", NULL };
static const char* sLibraryFallbackPaths[] = { "$HOME/lib", "/usr/local/lib", "/usr/lib", NULL };
static UndefinedHandler sUndefinedHandler = NULL;
static ImageLoader* sBundleBeingLoaded = NULL; #if DYLD_SHARED_CACHE_SUPPORT
static const dyld_cache_header* sSharedCache = NULL;
static long sSharedCacheSlide = 0;
static bool sSharedCacheIgnoreInodeAndTimeStamp = false;
bool gSharedCacheOverridden = false;
#if __IPHONE_OS_VERSION_MIN_REQUIRED
static const char* sSharedCacheDir = IPHONE_DYLD_SHARED_CACHE_DIR;
static bool sDylibsOverrideCache = false;
#define ENABLE_DYLIBS_TO_OVERRIDE_CACHE_SIZE 1024
#else
static const char* sSharedCacheDir = MACOSX_DYLD_SHARED_CACHE_DIR;
#endif
#endif
ImageLoader::LinkContext gLinkContext;
bool gLogAPIs = false;
const struct LibSystemHelpers* gLibSystemHelpers = NULL;
#if SUPPORT_OLD_CRT_INITIALIZATION
bool gRunInitializersOldWay = false;
#endif
static std::vector<DylibOverride> sDylibOverrides;
#if !TARGET_IPHONE_SIMULATOR
static int sLogSocket = -1;
#endif
static bool sFrameworksFoundAsDylibs = false;
#if __x86_64__
static bool sHaswell = false;
#endif
static std::vector<ImageLoader::DynamicReference> sDynamicReferences;
static bool sLogToFile = false;
static char sLoadingCrashMessage[1024] = "dyld: launch, loading dependent libraries";
struct MappedRanges
{
enum { count=400 };
struct {
ImageLoader* image;
uintptr_t start;
uintptr_t end;
} array[count];
MappedRanges* next;
};
static MappedRanges sMappedRangesStart;
void addMappedRange(ImageLoader* image, uintptr_t start, uintptr_t end)
{
for (MappedRanges* p = &sMappedRangesStart; p != NULL; p = p->next) {
for (int i=0; i < MappedRanges::count; ++i) {
if ( p->array[i].image == NULL ) {
p->array[i].start = start;
p->array[i].end = end;
OSMemoryBarrier();
p->array[i].image = image;
return;
}
}
}
MappedRanges* newRanges = (MappedRanges*)malloc(sizeof(MappedRanges));
bzero(newRanges, sizeof(MappedRanges));
newRanges->array[0].start = start;
newRanges->array[0].end = end;
newRanges->array[0].image = image;
for (MappedRanges* p = &sMappedRangesStart; p != NULL; p = p->next) {
if ( p->next == NULL ) {
OSMemoryBarrier();
p->next = newRanges;
break;
}
}
}
void removedMappedRanges(ImageLoader* image)
{
for (MappedRanges* p = &sMappedRangesStart; p != NULL; p = p->next) {
for (int i=0; i < MappedRanges::count; ++i) {
if ( p->array[i].image == image ) {
OSMemoryBarrier();
p->array[i].image = NULL;
}
}
}
}
ImageLoader* findMappedRange(uintptr_t target)
{
for (MappedRanges* p = &sMappedRangesStart; p != NULL; p = p->next) {
for (int i=0; i < MappedRanges::count; ++i) {
if ( p->array[i].image != NULL ) {
if ( (p->array[i].start <= target) && (target < p->array[i].end) )
return p->array[i].image;
}
}
}
return NULL;
}
const char* mkstringf(const char* format, ...)
{
_SIMPLE_STRING buf = _simple_salloc();
if ( buf != NULL ) {
va_list list;
va_start(list, format);
_simple_vsprintf(buf, format, list);
va_end(list);
const char* t = strdup(_simple_string(buf));
_simple_sfree(buf);
if ( t != NULL )
return t;
}
return "mkstringf, out of memory error";
}
void throwf(const char* format, ...)
{
_SIMPLE_STRING buf = _simple_salloc();
if ( buf != NULL ) {
va_list list;
va_start(list, format);
_simple_vsprintf(buf, format, list);
va_end(list);
const char* t = strdup(_simple_string(buf));
_simple_sfree(buf);
if ( t != NULL )
throw t;
}
throw "throwf, out of memory error";
}
#if !TARGET_IPHONE_SIMULATOR
static int sLogfile = STDERR_FILENO;
#endif
#if LOG_BINDINGS
static int sBindingsLogfile = -1;
static void mysprintf(char* dst, const char* format, ...)
{
_SIMPLE_STRING buf = _simple_salloc();
if ( buf != NULL ) {
va_list list;
va_start(list, format);
_simple_vsprintf(buf, format, list);
va_end(list);
strcpy(dst, _simple_string(buf));
_simple_sfree(buf);
}
else {
strcpy(dst, "out of memory");
}
}
void logBindings(const char* format, ...)
{
if ( sBindingsLogfile != -1 ) {
va_list list;
va_start(list, format);
_simple_vdprintf(sBindingsLogfile, format, list);
va_end(list);
}
}
#endif
#if !TARGET_IPHONE_SIMULATOR
static bool useSyslog()
{
if ( (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 11) ) {
if ( (*gLibSystemHelpers->isLaunchdOwned)() ) {
return true;
}
}
struct stat sb;
int result = fstat(STDERR_FILENO, &sb);
if ( result < 0 )
return true;
return false;
}
static void socket_syslogv(int priority, const char* format, va_list list)
{
if ( sLogSocket == -1 ) {
sLogSocket = ::socket(AF_UNIX, SOCK_DGRAM, 0);
if (sLogSocket == -1)
return; ::fcntl(sLogSocket, F_SETFD, 1);
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, _PATH_LOG, sizeof(addr.sun_path));
if ( ::connect(sLogSocket, (struct sockaddr *)&addr, sizeof(addr)) == -1 ) {
::close(sLogSocket);
sLogSocket = -1;
return;
}
}
_SIMPLE_STRING buf = _simple_salloc();
if ( buf == NULL )
return;
if ( _simple_sprintf(buf, "<%d>%s[%d]: ", LOG_USER|LOG_NOTICE, sExecShortName, getpid()) == 0 ) {
if ( _simple_vsprintf(buf, format, list) == 0 ) {
const char* p = _simple_string(buf);
::__sendto(sLogSocket, p, strlen(p), 0, NULL, 0);
}
}
_simple_sfree(buf);
}
void vlog(const char* format, va_list list)
{
if ( !sLogToFile && useSyslog() )
socket_syslogv(LOG_ERR, format, list);
else {
_simple_vdprintf(sLogfile, format, list);
}
}
void log(const char* format, ...)
{
va_list list;
va_start(list, format);
vlog(format, list);
va_end(list);
}
void vwarn(const char* format, va_list list)
{
_simple_dprintf(sLogfile, "dyld: warning, ");
_simple_vdprintf(sLogfile, format, list);
}
void warn(const char* format, ...)
{
va_list list;
va_start(list, format);
vwarn(format, list);
va_end(list);
}
#endif // !TARGET_IPHONE_SIMULATOR
static OSSpinLock sAllImagesLock = 0;
static void allImagesLock()
{
#if TARGET_IPHONE_SIMULATOR
while ( ! OSAtomicCompareAndSwapPtrBarrier((void*)0, (void*)1, (void**)&sAllImagesLock) ) {
}
#else
OSSpinLockLock(&sAllImagesLock);
#endif
}
static void allImagesUnlock()
{
#if TARGET_IPHONE_SIMULATOR
while ( ! OSAtomicCompareAndSwapPtrBarrier((void*)1, (void*)0, (void**)&sAllImagesLock) ) {
}
#else
OSSpinLockUnlock(&sAllImagesLock);
#endif
}
class FileOpener {
public:
FileOpener(const char* path);
~FileOpener();
int getFileDescriptor() { return fd; }
private:
int fd;
};
FileOpener::FileOpener(const char* path)
: fd(-1)
{
fd = my_open(path, O_RDONLY, 0);
}
FileOpener::~FileOpener()
{
if ( fd != -1 )
close(fd);
}
static void registerDOFs(const std::vector<ImageLoader::DOFInfo>& dofs)
{
const size_t dofSectionCount = dofs.size();
if ( !sEnv.DYLD_DISABLE_DOFS && (dofSectionCount != 0) ) {
int fd = open("/dev/" DTRACEMNR_HELPER, O_RDWR);
if ( fd < 0 ) {
}
else {
uint8_t buffer[sizeof(dof_ioctl_data_t) + dofSectionCount*sizeof(dof_helper_t)];
dof_ioctl_data_t* ioctlData = (dof_ioctl_data_t*)buffer;
ioctlData->dofiod_count = dofSectionCount;
for (unsigned int i=0; i < dofSectionCount; ++i) {
strlcpy(ioctlData->dofiod_helpers[i].dofhp_mod, dofs[i].imageShortName, DTRACE_MODNAMELEN);
ioctlData->dofiod_helpers[i].dofhp_dof = (uintptr_t)(dofs[i].dof);
ioctlData->dofiod_helpers[i].dofhp_addr = (uintptr_t)(dofs[i].dof);
}
user_addr_t val = (user_addr_t)(unsigned long)ioctlData;
if ( ioctl(fd, DTRACEHIOC_ADDDOF, &val) != -1 ) {
for (unsigned int i=0; i < dofSectionCount; ++i) {
RegisteredDOF info;
info.mh = dofs[i].imageHeader;
info.registrationID = (int)(ioctlData->dofiod_helpers[i].dofhp_dof);
sImageFilesNeedingDOFUnregistration.push_back(info);
if ( gLinkContext.verboseDOF ) {
dyld::log("dyld: registering DOF section %p in %s with dtrace, ID=0x%08X\n",
dofs[i].dof, dofs[i].imageShortName, info.registrationID);
}
}
}
else {
}
close(fd);
}
}
}
static void unregisterDOF(int registrationID)
{
int fd = open("/dev/" DTRACEMNR_HELPER, O_RDWR);
if ( fd < 0 ) {
dyld::warn("can't open /dev/" DTRACEMNR_HELPER " to unregister dtrace DOF section\n");
}
else {
ioctl(fd, DTRACEHIOC_REMOVE, registrationID);
close(fd);
if ( gLinkContext.verboseInit )
dyld::warn("unregistering DOF section ID=0x%08X with dtrace\n", registrationID);
}
}
static void notifyAddImageCallbacks(ImageLoader* image)
{
if ( ! image->addFuncNotified() ) {
for (std::vector<ImageCallback>::iterator it=sAddImageCallbacks.begin(); it != sAddImageCallbacks.end(); it++)
(*it)(image->machHeader(), image->getSlide());
image->setAddFuncNotified();
}
}
static const char* updateAllImages(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
{
if ( info[0].imageFilePath != NULL )
addImagesToAllImages(infoCount, info);
return NULL;
}
static StateHandlers* stateToHandlers(dyld_image_states state, void* handlersArray[7][3])
{
switch ( state ) {
case dyld_image_state_mapped:
return reinterpret_cast<StateHandlers*>(&handlersArray[0]);
case dyld_image_state_dependents_mapped:
return reinterpret_cast<StateHandlers*>(&handlersArray[1]);
case dyld_image_state_rebased:
return reinterpret_cast<StateHandlers*>(&handlersArray[2]);
case dyld_image_state_bound:
return reinterpret_cast<StateHandlers*>(&handlersArray[3]);
case dyld_image_state_dependents_initialized:
return reinterpret_cast<StateHandlers*>(&handlersArray[4]);
case dyld_image_state_initialized:
return reinterpret_cast<StateHandlers*>(&handlersArray[5]);
case dyld_image_state_terminated:
return reinterpret_cast<StateHandlers*>(&handlersArray[6]);
}
return NULL;
}
static void notifySingle(dyld_image_states state, const ImageLoader* image)
{
std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(state, sSingleHandlers);
if ( handlers != NULL ) {
dyld_image_info info;
info.imageLoadAddress = image->machHeader();
info.imageFilePath = image->getRealPath();
info.imageFileModDate = image->lastModified();
for (std::vector<dyld_image_state_change_handler>::iterator it = handlers->begin(); it != handlers->end(); ++it) {
const char* result = (*it)(state, 1, &info);
if ( (result != NULL) && (state == dyld_image_state_mapped) ) {
const char* str = strdup(result);
throw str;
}
}
}
if ( state == dyld_image_state_mapped ) {
if ( !image->inSharedCache() ) {
dyld_uuid_info info;
if ( image->getUUID(info.imageUUID) ) {
info.imageLoadAddress = image->machHeader();
addNonSharedCacheImageUUID(info);
}
}
}
if ( image->addFuncNotified() && (state == dyld_image_state_terminated) ) {
if ( sEnv.DYLD_PRINT_CS_NOTIFICATIONS ) {
dyld::log("dyld core symbolication unload notification: %p %s\n", image->machHeader(), image->getPath());
}
if ( dyld::gProcessInfo->coreSymbolicationShmPage != NULL) {
#if TARGET_IPHONE_SIMULATOR
void* connection = dyld::gProcessInfo->coreSymbolicationShmPage;
if ( *((uint32_t*)connection) == 2 ) {
#else
CSCppDyldSharedMemoryPage* connection = (CSCppDyldSharedMemoryPage*)dyld::gProcessInfo->coreSymbolicationShmPage;
if ( connection->is_valid_version() ) {
#endif
coresymbolication_unload_image(connection, image);
}
}
}
}
void syncAllImages()
{
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); ++it) {
dyld_image_info info;
ImageLoader* image = *it;
info.imageLoadAddress = image->machHeader();
info.imageFilePath = image->getRealPath();
info.imageFileModDate = image->lastModified();
bool found = false;
int existingCount = dyld::gProcessInfo->infoArrayCount;
const dyld_image_info* existing = dyld::gProcessInfo->infoArray;
if ( existing != NULL ) {
for (int i=0; i < existingCount; ++i) {
if ( existing[i].imageLoadAddress == info.imageLoadAddress ) {
found = true;
break;
}
}
}
if ( ! found ) {
addImagesToAllImages(1, &info);
}
}
}
static int imageSorter(const void* l, const void* r)
{
const ImageLoader* left = *((ImageLoader**)l);
const ImageLoader* right= *((ImageLoader**)r);
return left->compare(right);
}
static void notifyBatchPartial(dyld_image_states state, bool orLater, dyld_image_state_change_handler onlyHandler)
{
std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(state, sBatchHandlers);
if ( handlers != NULL ) {
allImagesLock();
ImageLoader* images[sAllImages.size()+1];
ImageLoader** end = images;
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
dyld_image_states imageState = (*it)->getState();
if ( (imageState == state) || (orLater && (imageState > state)) )
*end++ = *it;
}
if ( sBundleBeingLoaded != NULL ) {
dyld_image_states imageState = sBundleBeingLoaded->getState();
if ( (imageState == state) || (orLater && (imageState > state)) )
*end++ = sBundleBeingLoaded;
}
const char* dontLoadReason = NULL;
uint32_t count = (uint32_t)(end-images);
if ( end != images ) {
qsort(images, count, sizeof(ImageLoader*), &imageSorter);
dyld_image_info infos[count];
for (unsigned int i=0; i < count; ++i) {
dyld_image_info* p = &infos[i];
ImageLoader* image = images[i];
p->imageLoadAddress = image->machHeader();
p->imageFilePath = image->getRealPath();
p->imageFileModDate = image->lastModified();
if ( state == dyld_image_state_bound )
notifyAddImageCallbacks(image);
}
if ( onlyHandler != NULL ) {
const char* result = (*onlyHandler)(state, count, infos);
if ( (result != NULL) && (state == dyld_image_state_dependents_mapped) ) {
dontLoadReason = strdup(result);
}
}
else {
for (std::vector<dyld_image_state_change_handler>::iterator it = handlers->begin(); it != handlers->end(); ++it) {
const char* result = (*it)(state, count, infos);
if ( (result != NULL) && (state == dyld_image_state_dependents_mapped) ) {
dontLoadReason = strdup(result);
break;
}
}
}
}
allImagesUnlock();
if ( dontLoadReason != NULL )
throw dontLoadReason;
}
if ( state == dyld_image_state_rebased ) {
if ( sEnv.DYLD_PRINT_CS_NOTIFICATIONS ) {
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
dyld_image_states imageState = (*it)->getState();
if ( (imageState == dyld_image_state_rebased) || (orLater && (imageState > dyld_image_state_rebased)) )
dyld::log("dyld core symbolication load notification: %p %s\n", (*it)->machHeader(), (*it)->getPath());
}
}
if ( dyld::gProcessInfo->coreSymbolicationShmPage != NULL) {
#if TARGET_IPHONE_SIMULATOR
void* connection = dyld::gProcessInfo->coreSymbolicationShmPage;
if ( *((uint32_t*)connection) == 2 ) {
#else
CSCppDyldSharedMemoryPage* connection = (CSCppDyldSharedMemoryPage*)dyld::gProcessInfo->coreSymbolicationShmPage;
if ( connection->is_valid_version() ) {
#endif
uint64_t load_timestamp = mach_absolute_time();
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
dyld_image_states imageState = (*it)->getState();
if ( (imageState == state) || (orLater && (imageState > state)) )
coresymbolication_load_image(connection, *it, load_timestamp);
}
}
}
}
}
static void notifyBatch(dyld_image_states state)
{
notifyBatchPartial(state, false, NULL);
}
static void addRootImage(ImageLoader* image)
{
sImageRoots.push_back(image);
}
static void clearAllDepths()
{
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++)
(*it)->clearDepth();
}
static void printAllDepths()
{
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++)
dyld::log("%03d %s\n", (*it)->getDepth(), (*it)->getShortName());
}
static unsigned int imageCount()
{
return (unsigned int)sAllImages.size();
}
static void setNewProgramVars(const ProgramVars& newVars)
{
gLinkContext.programVars = newVars;
*gLinkContext.programVars.NXArgcPtr = gLinkContext.argc;
*gLinkContext.programVars.NXArgvPtr = gLinkContext.argv;
*gLinkContext.programVars.environPtr = gLinkContext.envp;
*gLinkContext.programVars.__prognamePtr = gLinkContext.progname;
}
#if SUPPORT_OLD_CRT_INITIALIZATION
static void setRunInitialzersOldWay()
{
gRunInitializersOldWay = true;
}
#endif
static void addDynamicReference(ImageLoader* from, ImageLoader* to) {
if( from->inSharedCache() )
return;
if( to->inSharedCache() )
return;
if ( from->dependsOn(to) )
return;
for (std::vector<ImageLoader::DynamicReference>::iterator it=sDynamicReferences.begin(); it != sDynamicReferences.end(); ++it) {
if ( (it->from == from) && (it->to == to) )
return;
}
ImageLoader::DynamicReference t;
t.from = from;
t.to = to;
sDynamicReferences.push_back(t);
}
static void addImage(ImageLoader* image)
{
allImagesLock();
sAllImages.push_back(image);
allImagesUnlock();
uintptr_t lastSegStart = 0;
uintptr_t lastSegEnd = 0;
for(unsigned int i=0, e=image->segmentCount(); i < e; ++i) {
if ( image->segUnaccessible(i) )
continue;
uintptr_t start = image->segActualLoadAddress(i);
uintptr_t end = image->segActualEndAddress(i);
if ( start == lastSegEnd ) {
lastSegEnd = end;
}
else {
if ( lastSegEnd != 0 )
addMappedRange(image, lastSegStart, lastSegEnd);
lastSegStart = start;
lastSegEnd = end;
}
}
if ( lastSegEnd != 0 )
addMappedRange(image, lastSegStart, lastSegEnd);
if ( sEnv.DYLD_PRINT_LIBRARIES || (sEnv.DYLD_PRINT_LIBRARIES_POST_LAUNCH && (sMainExecutable!=NULL) && sMainExecutable->isLinked()) ) {
dyld::log("dyld: loaded: %s\n", image->getPath());
}
}
class RefUsesImage {
public:
RefUsesImage(ImageLoader* image) : _image(image) {}
bool operator()(const ImageLoader::DynamicReference& ref) const {
return ( (ref.from == _image) || (ref.to == _image) );
}
private:
ImageLoader* _image;
};
void removeImage(ImageLoader* image)
{
for (std::vector<RegisteredDOF>::iterator it=sImageFilesNeedingDOFUnregistration.begin(); it != sImageFilesNeedingDOFUnregistration.end(); ) {
if ( it->mh == image->machHeader() ) {
unregisterDOF(it->registrationID);
sImageFilesNeedingDOFUnregistration.erase(it);
}
else {
++it;
}
}
if ( image->getState() >= dyld_image_state_bound ) {
sRemoveImageCallbacksInUse = true; for (std::vector<ImageCallback>::iterator it=sRemoveImageCallbacks.begin(); it != sRemoveImageCallbacks.end(); it++) {
(*it)(image->machHeader(), image->getSlide());
}
sRemoveImageCallbacksInUse = false;
}
notifySingle(dyld_image_state_terminated, image);
removedMappedRanges(image);
allImagesLock();
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
if ( *it == image ) {
sAllImages.erase(it);
break;
}
}
allImagesUnlock();
sDynamicReferences.erase(std::remove_if(sDynamicReferences.begin(), sDynamicReferences.end(), RefUsesImage(image)), sDynamicReferences.end());
if ( sLastImageByAddressCache == image )
sLastImageByAddressCache = NULL;
for (std::vector<ImageLoader*>::iterator it=sImageRoots.begin(); it != sImageRoots.end(); it++) {
if ( *it == image ) {
sImageRoots.erase(it);
break;
}
}
if ( sEnv.DYLD_PRINT_LIBRARIES || (sEnv.DYLD_PRINT_LIBRARIES_POST_LAUNCH && (sMainExecutable!=NULL) && sMainExecutable->isLinked()) ) {
dyld::log("dyld: unloaded: %s\n", image->getPath());
}
removeImageFromAllImages(image->machHeader());
}
void runImageStaticTerminators(ImageLoader* image)
{
bool mightBeMore;
do {
mightBeMore = false;
for (std::vector<ImageLoader*>::iterator it=sImageFilesNeedingTermination.begin(); it != sImageFilesNeedingTermination.end(); it++) {
if ( *it == image ) {
sImageFilesNeedingTermination.erase(it);
if (gLogAPIs) dyld::log("dlclose(), running static terminators for %p %s\n", image, image->getShortName());
image->doTermination(gLinkContext);
mightBeMore = true;
break;
}
}
} while ( mightBeMore );
}
static void terminationRecorder(ImageLoader* image)
{
sImageFilesNeedingTermination.push_back(image);
}
const char* getExecutablePath()
{
return sExecPath;
}
static void runAllStaticTerminators(void* extra)
{
try {
const size_t imageCount = sImageFilesNeedingTermination.size();
for(size_t i=imageCount; i > 0; --i){
ImageLoader* image = sImageFilesNeedingTermination[i-1];
image->doTermination(gLinkContext);
}
sImageFilesNeedingTermination.clear();
notifyBatch(dyld_image_state_terminated);
}
catch (const char* msg) {
halt(msg);
}
}
void initializeMainExecutable()
{
gLinkContext.startedInitializingMainExecutable = true;
ImageLoader::InitializerTimingList initializerTimes[sAllImages.size()];
initializerTimes[0].count = 0;
const size_t rootCount = sImageRoots.size();
if ( rootCount > 1 ) {
for(size_t i=1; i < rootCount; ++i) {
sImageRoots[i]->runInitializers(gLinkContext, initializerTimes[0]);
}
}
sMainExecutable->runInitializers(gLinkContext, initializerTimes[0]);
if ( gLibSystemHelpers != NULL )
(*gLibSystemHelpers->cxa_atexit)(&runAllStaticTerminators, NULL, NULL);
if ( sEnv.DYLD_PRINT_STATISTICS )
ImageLoaderMachO::printStatistics((unsigned int)sAllImages.size(), initializerTimes[0]);
}
bool mainExecutablePrebound()
{
return sMainExecutable->usablePrebinding(gLinkContext);
}
ImageLoader* mainExecutable()
{
return sMainExecutable;
}
#if SUPPORT_VERSIONED_PATHS
static bool getDylibVersionAndInstallname(const char* dylibPath, uint32_t* version, char* installName);
static void checkDylibOverride(const char* dylibFile)
{
uint32_t altVersion;
char sysInstallName[PATH_MAX];
if ( getDylibVersionAndInstallname(dylibFile, &altVersion, sysInstallName) ) {
uint32_t sysVersion;
if ( getDylibVersionAndInstallname(sysInstallName, &sysVersion, NULL) ) {
if ( altVersion > sysVersion ) {
bool entryExists = false;
for (std::vector<DylibOverride>::iterator it = sDylibOverrides.begin(); it != sDylibOverrides.end(); ++it) {
if ( strcmp(it->installName, sysInstallName) == 0 ) {
entryExists = true;
uint32_t prevVersion;
if ( getDylibVersionAndInstallname(it->override, &prevVersion, NULL) ) {
if ( altVersion > prevVersion ) {
free((void*)(it->override));
char resolvedPath[PATH_MAX];
if ( realpath(dylibFile, resolvedPath) != NULL )
it->override = strdup(resolvedPath);
else
it->override = strdup(dylibFile);
break;
}
}
}
}
if ( ! entryExists ) {
DylibOverride entry;
entry.installName = strdup(sysInstallName);
char resolvedPath[PATH_MAX];
if ( realpath(dylibFile, resolvedPath) != NULL )
entry.override = strdup(resolvedPath);
else
entry.override = strdup(dylibFile);
sDylibOverrides.push_back(entry);
}
}
}
}
}
static void checkDylibOverridesInDir(const char* dirPath)
{
char dylibPath[PATH_MAX];
int dirPathLen = strlen(dirPath);
strlcpy(dylibPath, dirPath, PATH_MAX);
DIR* dirp = opendir(dirPath);
if ( dirp != NULL) {
dirent entry;
dirent* entp = NULL;
while ( readdir_r(dirp, &entry, &entp) == 0 ) {
if ( entp == NULL )
break;
if ( entp->d_type != DT_REG )
continue;
dylibPath[dirPathLen] = '/';
dylibPath[dirPathLen+1] = '\0';
if ( strlcat(dylibPath, entp->d_name, PATH_MAX) > PATH_MAX )
continue;
checkDylibOverride(dylibPath);
}
closedir(dirp);
}
}
static void checkFrameworkOverridesInDir(const char* dirPath)
{
char frameworkPath[PATH_MAX];
int dirPathLen = strlen(dirPath);
strlcpy(frameworkPath, dirPath, PATH_MAX);
DIR* dirp = opendir(dirPath);
if ( dirp != NULL) {
dirent entry;
dirent* entp = NULL;
while ( readdir_r(dirp, &entry, &entp) == 0 ) {
if ( entp == NULL )
break;
if ( entp->d_type != DT_DIR )
continue;
frameworkPath[dirPathLen] = '/';
frameworkPath[dirPathLen+1] = '\0';
int dirNameLen = strlen(entp->d_name);
if ( dirNameLen < 11 )
continue;
if ( strcmp(&entp->d_name[dirNameLen-10], ".framework") != 0 )
continue;
if ( strlcat(frameworkPath, entp->d_name, PATH_MAX) > PATH_MAX )
continue;
if ( strlcat(frameworkPath, "/", PATH_MAX) > PATH_MAX )
continue;
if ( strlcat(frameworkPath, entp->d_name, PATH_MAX) > PATH_MAX )
continue;
frameworkPath[strlen(frameworkPath)-10] = '\0';
checkDylibOverride(frameworkPath);
}
closedir(dirp);
}
}
#endif // SUPPORT_VERSIONED_PATHS
static const char** parseColonList(const char* list, const char* mainExecutableDir)
{
static const char* sEmptyList[] = { NULL };
if ( list[0] == '\0' )
return sEmptyList;
int colonCount = 0;
for(const char* s=list; *s != '\0'; ++s) {
if (*s == ':')
++colonCount;
}
int index = 0;
const char* start = list;
char** result = new char*[colonCount+2];
for(const char* s=list; *s != '\0'; ++s) {
if (*s == ':') {
size_t len = s-start;
if ( (mainExecutableDir != NULL) && (strncmp(start, "@loader_path/", 13) == 0) ) {
size_t mainExecDirLen = strlen(mainExecutableDir);
char* str = new char[mainExecDirLen+len+1];
strcpy(str, mainExecutableDir);
strlcat(str, &start[13], mainExecDirLen+len+1);
str[mainExecDirLen+len-13] = '\0';
start = &s[1];
result[index++] = str;
}
else if ( (mainExecutableDir != NULL) && (strncmp(start, "@executable_path/", 17) == 0) ) {
size_t mainExecDirLen = strlen(mainExecutableDir);
char* str = new char[mainExecDirLen+len+1];
strcpy(str, mainExecutableDir);
strlcat(str, &start[17], mainExecDirLen+len+1);
str[mainExecDirLen+len-17] = '\0';
start = &s[1];
result[index++] = str;
}
else {
char* str = new char[len+1];
strncpy(str, start, len);
str[len] = '\0';
start = &s[1];
result[index++] = str;
}
}
}
size_t len = strlen(start);
if ( (mainExecutableDir != NULL) && (strncmp(start, "@loader_path/", 13) == 0) ) {
size_t mainExecDirLen = strlen(mainExecutableDir);
char* str = new char[mainExecDirLen+len+1];
strcpy(str, mainExecutableDir);
strlcat(str, &start[13], mainExecDirLen+len+1);
str[mainExecDirLen+len-13] = '\0';
result[index++] = str;
}
else if ( (mainExecutableDir != NULL) && (strncmp(start, "@executable_path/", 17) == 0) ) {
size_t mainExecDirLen = strlen(mainExecutableDir);
char* str = new char[mainExecDirLen+len+1];
strcpy(str, mainExecutableDir);
strlcat(str, &start[17], mainExecDirLen+len+1);
str[mainExecDirLen+len-17] = '\0';
result[index++] = str;
}
else {
char* str = new char[len+1];
strcpy(str, start);
result[index++] = str;
}
result[index] = NULL;
return (const char**)result;
}
static void appendParsedColonList(const char* list, const char* mainExecutableDir, const char* const ** storage)
{
const char** newlist = parseColonList(list, mainExecutableDir);
if ( *storage == NULL ) {
*storage = newlist;
}
else {
const char* const* existing = *storage;
int count = 0;
for(int i=0; existing[i] != NULL; ++i)
++count;
for(int i=0; newlist[i] != NULL; ++i)
++count;
const char** combinedList = new const char*[count+2];
int index = 0;
for(int i=0; existing[i] != NULL; ++i)
combinedList[index++] = existing[i];
for(int i=0; newlist[i] != NULL; ++i)
combinedList[index++] = newlist[i];
combinedList[index] = NULL;
*storage = combinedList;
}
}
static void paths_expand_roots(const char **paths, const char *key, const char *val)
{
if(NULL != key) {
size_t keyLen = strlen(key);
for(int i=0; paths[i] != NULL; ++i) {
if ( strncmp(paths[i], key, keyLen) == 0 ) {
char* newPath = new char[strlen(val) + (strlen(paths[i]) - keyLen) + 1];
strcpy(newPath, val);
strcat(newPath, &paths[i][keyLen]);
paths[i] = newPath;
}
}
}
return;
}
static void removePathWithPrefix(const char* paths[], const char* prefix)
{
size_t prefixLen = strlen(prefix);
int skip = 0;
int i;
for(i = 0; paths[i] != NULL; ++i) {
if ( strncmp(paths[i], prefix, prefixLen) == 0 )
++skip;
else
paths[i-skip] = paths[i];
}
paths[i-skip] = NULL;
}
#if 0
static void paths_dump(const char **paths)
{
const char **strs = paths;
while(*strs != NULL)
{
dyld::log("\"%s\"\n", *strs);
strs++;
}
return;
}
#endif
static void printOptions(const char* argv[])
{
uint32_t i = 0;
while ( NULL != argv[i] ) {
dyld::log("opt[%i] = \"%s\"\n", i, argv[i]);
i++;
}
}
static void printEnvironmentVariables(const char* envp[])
{
while ( NULL != *envp ) {
dyld::log("%s\n", *envp);
envp++;
}
}
void processDyldEnvironmentVariable(const char* key, const char* value, const char* mainExecutableDir)
{
if ( strcmp(key, "DYLD_FRAMEWORK_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_FRAMEWORK_PATH);
}
else if ( strcmp(key, "DYLD_FALLBACK_FRAMEWORK_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_FALLBACK_FRAMEWORK_PATH);
}
else if ( strcmp(key, "DYLD_LIBRARY_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_LIBRARY_PATH);
}
else if ( strcmp(key, "DYLD_FALLBACK_LIBRARY_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_FALLBACK_LIBRARY_PATH);
}
else if ( (strcmp(key, "DYLD_ROOT_PATH") == 0) || (strcmp(key, "DYLD_PATHS_ROOT") == 0) ) {
if ( strcmp(value, "/") != 0 ) {
gLinkContext.rootPaths = parseColonList(value, mainExecutableDir);
for (int i=0; gLinkContext.rootPaths[i] != NULL; ++i) {
if ( gLinkContext.rootPaths[i][0] != '/' ) {
dyld::warn("DYLD_ROOT_PATH not used because it contains a non-absolute path\n");
gLinkContext.rootPaths = NULL;
break;
}
}
}
}
else if ( strcmp(key, "DYLD_IMAGE_SUFFIX") == 0 ) {
gLinkContext.imageSuffix = value;
}
else if ( strcmp(key, "DYLD_INSERT_LIBRARIES") == 0 ) {
sEnv.DYLD_INSERT_LIBRARIES = parseColonList(value, NULL);
}
else if ( strcmp(key, "DYLD_PRINT_OPTS") == 0 ) {
sEnv.DYLD_PRINT_OPTS = true;
}
else if ( strcmp(key, "DYLD_PRINT_ENV") == 0 ) {
sEnv.DYLD_PRINT_ENV = true;
}
else if ( strcmp(key, "DYLD_DISABLE_DOFS") == 0 ) {
sEnv.DYLD_DISABLE_DOFS = true;
}
else if ( strcmp(key, "DYLD_DISABLE_PREFETCH") == 0 ) {
gLinkContext.preFetchDisabled = true;
}
else if ( strcmp(key, "DYLD_PRINT_LIBRARIES") == 0 ) {
sEnv.DYLD_PRINT_LIBRARIES = true;
}
else if ( strcmp(key, "DYLD_PRINT_LIBRARIES_POST_LAUNCH") == 0 ) {
sEnv.DYLD_PRINT_LIBRARIES_POST_LAUNCH = true;
}
else if ( strcmp(key, "DYLD_BIND_AT_LAUNCH") == 0 ) {
sEnv.DYLD_BIND_AT_LAUNCH = true;
}
else if ( strcmp(key, "DYLD_FORCE_FLAT_NAMESPACE") == 0 ) {
gLinkContext.bindFlat = true;
}
else if ( strcmp(key, "DYLD_NEW_LOCAL_SHARED_REGIONS") == 0 ) {
}
else if ( strcmp(key, "DYLD_NO_FIX_PREBINDING") == 0 ) {
}
else if ( strcmp(key, "DYLD_PREBIND_DEBUG") == 0 ) {
gLinkContext.verbosePrebinding = true;
}
else if ( strcmp(key, "DYLD_PRINT_INITIALIZERS") == 0 ) {
gLinkContext.verboseInit = true;
}
else if ( strcmp(key, "DYLD_PRINT_DOFS") == 0 ) {
gLinkContext.verboseDOF = true;
}
else if ( strcmp(key, "DYLD_PRINT_STATISTICS") == 0 ) {
sEnv.DYLD_PRINT_STATISTICS = true;
}
else if ( strcmp(key, "DYLD_PRINT_SEGMENTS") == 0 ) {
gLinkContext.verboseMapping = true;
}
else if ( strcmp(key, "DYLD_PRINT_BINDINGS") == 0 ) {
gLinkContext.verboseBind = true;
}
else if ( strcmp(key, "DYLD_PRINT_WEAK_BINDINGS") == 0 ) {
gLinkContext.verboseWeakBind = true;
}
else if ( strcmp(key, "DYLD_PRINT_REBASINGS") == 0 ) {
gLinkContext.verboseRebase = true;
}
else if ( strcmp(key, "DYLD_PRINT_APIS") == 0 ) {
gLogAPIs = true;
}
else if ( strcmp(key, "DYLD_PRINT_WARNINGS") == 0 ) {
gLinkContext.verboseWarnings = true;
}
else if ( strcmp(key, "DYLD_PRINT_RPATHS") == 0 ) {
gLinkContext.verboseRPaths = true;
}
else if ( strcmp(key, "DYLD_PRINT_CS_NOTIFICATIONS") == 0 ) {
sEnv.DYLD_PRINT_CS_NOTIFICATIONS = true;
}
else if ( strcmp(key, "DYLD_PRINT_INTERPOSING") == 0 ) {
gLinkContext.verboseInterposing = true;
}
else if ( strcmp(key, "DYLD_PRINT_CODE_SIGNATURES") == 0 ) {
gLinkContext.verboseCodeSignatures = true;
}
else if ( strcmp(key, "DYLD_SHARED_REGION") == 0 ) {
if ( strcmp(value, "private") == 0 ) {
gLinkContext.sharedRegionMode = ImageLoader::kUsePrivateSharedRegion;
}
else if ( strcmp(value, "avoid") == 0 ) {
gLinkContext.sharedRegionMode = ImageLoader::kDontUseSharedRegion;
}
else if ( strcmp(value, "use") == 0 ) {
gLinkContext.sharedRegionMode = ImageLoader::kUseSharedRegion;
}
else if ( value[0] == '\0' ) {
gLinkContext.sharedRegionMode = ImageLoader::kUseSharedRegion;
}
else {
dyld::warn("unknown option to DYLD_SHARED_REGION. Valid options are: use, private, avoid\n");
}
}
#if DYLD_SHARED_CACHE_SUPPORT
else if ( strcmp(key, "DYLD_SHARED_CACHE_DIR") == 0 ) {
sSharedCacheDir = value;
}
else if ( strcmp(key, "DYLD_SHARED_CACHE_DONT_VALIDATE") == 0 ) {
sSharedCacheIgnoreInodeAndTimeStamp = true;
}
#endif
else if ( strcmp(key, "DYLD_IGNORE_PREBINDING") == 0 ) {
if ( strcmp(value, "all") == 0 ) {
gLinkContext.prebindUsage = ImageLoader::kUseNoPrebinding;
}
else if ( strcmp(value, "app") == 0 ) {
gLinkContext.prebindUsage = ImageLoader::kUseAllButAppPredbinding;
}
else if ( strcmp(value, "nonsplit") == 0 ) {
gLinkContext.prebindUsage = ImageLoader::kUseSplitSegPrebinding;
}
else if ( value[0] == '\0' ) {
gLinkContext.prebindUsage = ImageLoader::kUseSplitSegPrebinding;
}
else {
dyld::warn("unknown option to DYLD_IGNORE_PREBINDING. Valid options are: all, app, nonsplit\n");
}
}
#if SUPPORT_VERSIONED_PATHS
else if ( strcmp(key, "DYLD_VERSIONED_LIBRARY_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_VERSIONED_LIBRARY_PATH);
}
else if ( strcmp(key, "DYLD_VERSIONED_FRAMEWORK_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_VERSIONED_FRAMEWORK_PATH);
}
#endif
else if ( strcmp(key, "DYLD_PRINT_TO_FILE") == 0 ) {
}
else {
dyld::warn("unknown environment variable: %s\n", key);
}
}
#if SUPPORT_LC_DYLD_ENVIRONMENT
static void checkLoadCommandEnvironmentVariables()
{
const uint32_t cmd_count = sMainExecutableMachHeader->ncmds;
const struct load_command* const cmds = (struct load_command*)(((char*)sMainExecutableMachHeader)+sizeof(macho_header));
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
case LC_DYLD_ENVIRONMENT:
{
const struct dylinker_command* envcmd = (struct dylinker_command*)cmd;
const char* keyEqualsValue = (char*)envcmd + envcmd->name.offset;
char mainExecutableDir[strlen(sExecPath)+2];
strcpy(mainExecutableDir, sExecPath);
char* lastSlash = strrchr(mainExecutableDir, '/');
if ( lastSlash != NULL)
lastSlash[1] = '\0';
if ( (strncmp(keyEqualsValue, "DYLD_", 5) == 0) ) {
const char* equals = strchr(keyEqualsValue, '=');
if ( equals != NULL ) {
if ( strncmp(&equals[-5], "_PATH", 5) == 0 ) {
const char* value = &equals[1];
const size_t keyLen = equals-keyEqualsValue;
char key[keyLen+1];
strncpy(key, keyEqualsValue, keyLen);
key[keyLen] = '\0';
processDyldEnvironmentVariable(key, value, mainExecutableDir);
}
}
}
}
break;
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
}
#endif // SUPPORT_LC_DYLD_ENVIRONMENT
static bool hasCodeSignatureLoadCommand(const macho_header* mh)
{
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
if (cmd->cmd == LC_CODE_SIGNATURE)
return true;
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
return false;
}
#if SUPPORT_VERSIONED_PATHS
static void checkVersionedPaths()
{
if ( sEnv.DYLD_VERSIONED_LIBRARY_PATH != NULL ) {
for(const char* const* lp = sEnv.DYLD_VERSIONED_LIBRARY_PATH; *lp != NULL; ++lp) {
checkDylibOverridesInDir(*lp);
}
}
if ( sEnv.DYLD_VERSIONED_FRAMEWORK_PATH != NULL ) {
for(const char* const* fp = sEnv.DYLD_VERSIONED_FRAMEWORK_PATH; *fp != NULL; ++fp) {
checkFrameworkOverridesInDir(*fp);
}
}
}
#endif
static void pruneEnvironmentVariables(const char* envp[], const char*** applep)
{
int removedCount = 0;
const char** d = envp;
for(const char** s = envp; *s != NULL; s++) {
if ( (strncmp(*s, "DYLD_", 5) != 0) && (strncmp(*s, "LD_LIBRARY_PATH=", 16) != 0) ) {
*d++ = *s;
}
else {
++removedCount;
}
}
*d++ = NULL;
#if 0
if ( removedCount != 0 ) {
dyld::log("dyld: DYLD_ environment variables being ignored because ");
switch (sRestrictedReason) {
case restrictedNot:
break;
case restrictedBySetGUid:
dyld::log("main executable (%s) is setuid or setgid\n", sExecPath);
break;
case restrictedBySegment:
dyld::log("main executable (%s) has __RESTRICT/__restrict section\n", sExecPath);
break;
case restrictedByEntitlements:
dyld::log("main executable (%s) is code signed with entitlements\n", sExecPath);
break;
}
}
#endif
if ( removedCount > 0 ) {
*applep = d;
do {
*d = d[removedCount];
} while ( *d++ != NULL );
for(int i=0; i < removedCount; ++i)
*d++ = NULL;
}
sEnv.DYLD_FALLBACK_FRAMEWORK_PATH = NULL;
sEnv.DYLD_FALLBACK_LIBRARY_PATH = NULL;
if ( removedCount > 0 )
strlcat(sLoadingCrashMessage, ", ignoring DYLD_* env vars", sizeof(sLoadingCrashMessage));
}
static void checkEnvironmentVariables(const char* envp[], bool ignoreEnviron)
{
const char* home = NULL;
const char** p;
for(p = envp; *p != NULL; p++) {
const char* keyEqualsValue = *p;
if ( strncmp(keyEqualsValue, "DYLD_", 5) == 0 ) {
const char* equals = strchr(keyEqualsValue, '=');
if ( (equals != NULL) && !ignoreEnviron ) {
strlcat(sLoadingCrashMessage, "\n", sizeof(sLoadingCrashMessage));
strlcat(sLoadingCrashMessage, keyEqualsValue, sizeof(sLoadingCrashMessage));
const char* value = &equals[1];
const size_t keyLen = equals-keyEqualsValue;
char key[keyLen+1];
strncpy(key, keyEqualsValue, keyLen);
key[keyLen] = '\0';
processDyldEnvironmentVariable(key, value, NULL);
}
}
else if ( strncmp(keyEqualsValue, "HOME=", 5) == 0 ) {
home = &keyEqualsValue[5];
}
else if ( strncmp(keyEqualsValue, "LD_LIBRARY_PATH=", 16) == 0 ) {
const char* path = &keyEqualsValue[16];
sEnv.LD_LIBRARY_PATH = parseColonList(path, NULL);
}
}
#if SUPPORT_LC_DYLD_ENVIRONMENT
checkLoadCommandEnvironmentVariables();
#endif // SUPPORT_LC_DYLD_ENVIRONMENT
if ( sEnv.DYLD_FALLBACK_FRAMEWORK_PATH == NULL ) {
const char** paths = sFrameworkFallbackPaths;
if ( home == NULL )
removePathWithPrefix(paths, "$HOME");
else
paths_expand_roots(paths, "$HOME", home);
sEnv.DYLD_FALLBACK_FRAMEWORK_PATH = paths;
}
if ( sEnv.DYLD_FALLBACK_LIBRARY_PATH == NULL ) {
const char** paths = sLibraryFallbackPaths;
if ( home == NULL )
removePathWithPrefix(paths, "$HOME");
else
paths_expand_roots(paths, "$HOME", home);
sEnv.DYLD_FALLBACK_LIBRARY_PATH = paths;
}
if ( (gLinkContext.imageSuffix != NULL) && (gLinkContext.rootPaths != NULL) ) {
dyld::warn("Ignoring DYLD_IMAGE_SUFFIX because DYLD_ROOT_PATH is used.\n");
gLinkContext.imageSuffix = NULL;
}
#if SUPPORT_VERSIONED_PATHS
checkVersionedPaths();
#endif
}
static void getHostInfo()
{
#if CPU_SUBTYPES_SUPPORTED
#if __ARM_ARCH_7K__
sHostCPU = CPU_TYPE_ARM;
sHostCPUsubtype = CPU_SUBTYPE_ARM_V7K;
#elif __ARM_ARCH_7A__
sHostCPU = CPU_TYPE_ARM;
sHostCPUsubtype = CPU_SUBTYPE_ARM_V7;
#elif __ARM_ARCH_6K__
sHostCPU = CPU_TYPE_ARM;
sHostCPUsubtype = CPU_SUBTYPE_ARM_V6;
#elif __ARM_ARCH_7F__
sHostCPU = CPU_TYPE_ARM;
sHostCPUsubtype = CPU_SUBTYPE_ARM_V7F;
#elif __ARM_ARCH_7S__
sHostCPU = CPU_TYPE_ARM;
sHostCPUsubtype = CPU_SUBTYPE_ARM_V7S;
#else
struct host_basic_info info;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
mach_port_t hostPort = mach_host_self();
kern_return_t result = host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&info, &count);
if ( result != KERN_SUCCESS )
throw "host_info() failed";
sHostCPU = info.cpu_type;
sHostCPUsubtype = info.cpu_subtype;
mach_port_deallocate(mach_task_self(), hostPort);
#endif
#endif
}
static void checkSharedRegionDisable()
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED
if ( sMainExecutable->overlapsWithAddressRange((void*)(uintptr_t)SHARED_REGION_BASE, (void*)(uintptr_t)(SHARED_REGION_BASE + SHARED_REGION_SIZE)) ) {
gLinkContext.sharedRegionMode = ImageLoader::kDontUseSharedRegion;
if ( gLinkContext.verboseMapping )
dyld::warn("disabling shared region because main executable overlaps\n");
}
#if __i386__
if ( sProcessIsRestricted ) {
gLinkContext.sharedRegionMode = ImageLoader::kUsePrivateSharedRegion;
}
#endif
#endif
}
bool validImage(const ImageLoader* possibleImage)
{
const size_t imageCount = sAllImages.size();
for(size_t i=0; i < imageCount; ++i) {
if ( possibleImage == sAllImages[i] ) {
return true;
}
}
return false;
}
uint32_t getImageCount()
{
return (uint32_t)sAllImages.size();
}
ImageLoader* getIndexedImage(unsigned int index)
{
if ( index < sAllImages.size() )
return sAllImages[index];
return NULL;
}
ImageLoader* findImageByMachHeader(const struct mach_header* target)
{
return findMappedRange((uintptr_t)target);
}
ImageLoader* findImageContainingAddress(const void* addr)
{
return findMappedRange((uintptr_t)addr);
}
ImageLoader* findImageContainingSymbol(const void* symbol)
{
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* anImage = *it;
if ( anImage->containsSymbol(symbol) )
return anImage;
}
return NULL;
}
void forEachImageDo( void (*callback)(ImageLoader*, void* userData), void* userData)
{
const size_t imageCount = sAllImages.size();
for(size_t i=0; i < imageCount; ++i) {
ImageLoader* anImage = sAllImages[i];
(*callback)(anImage, userData);
}
}
ImageLoader* findLoadedImage(const struct stat& stat_buf)
{
const size_t imageCount = sAllImages.size();
for(size_t i=0; i < imageCount; ++i){
ImageLoader* anImage = sAllImages[i];
if ( anImage->statMatch(stat_buf) )
return anImage;
}
return NULL;
}
static const char* strrstr(const char* str, const char* sub)
{
const size_t sublen = strlen(sub);
for(const char* p = &str[strlen(str)]; p != str; --p) {
if ( strncmp(p, sub, sublen) == 0 )
return p;
}
return NULL;
}
static const char* getFrameworkPartialPath(const char* path)
{
const char* dirDot = strrstr(path, ".framework/");
if ( dirDot != NULL ) {
const char* dirStart = dirDot;
for ( ; dirStart >= path; --dirStart) {
if ( (*dirStart == '/') || (dirStart == path) ) {
const char* frameworkStart = &dirStart[1];
if ( dirStart == path )
--frameworkStart;
size_t len = dirDot - frameworkStart;
char framework[len+1];
strncpy(framework, frameworkStart, len);
framework[len] = '\0';
const char* leaf = strrchr(path, '/');
if ( leaf != NULL ) {
if ( strcmp(framework, &leaf[1]) == 0 ) {
return frameworkStart;
}
if ( gLinkContext.imageSuffix != NULL ) {
if ( strncmp(framework, &leaf[1], len) == 0 ) {
if ( strcmp( gLinkContext.imageSuffix, &leaf[len+1]) == 0 )
return frameworkStart;
}
}
}
}
}
}
return NULL;
}
static const char* getLibraryLeafName(const char* path)
{
const char* start = strrchr(path, '/');
if ( start != NULL )
return &start[1];
else
return path;
}
#if CPU_SUBTYPES_SUPPORTED
const cpu_subtype_t CPU_SUBTYPE_END_OF_LIST = -1;
#if __arm__
const int kARM_RowCount = 8;
static const cpu_subtype_t kARM[kARM_RowCount][9] = {
{ CPU_SUBTYPE_ARM_V7F, CPU_SUBTYPE_ARM_V7, CPU_SUBTYPE_ARM_V6, CPU_SUBTYPE_ARM_V5TEJ, CPU_SUBTYPE_ARM_V4T, CPU_SUBTYPE_ARM_ALL, CPU_SUBTYPE_END_OF_LIST },
{ CPU_SUBTYPE_ARM_V7K, CPU_SUBTYPE_END_OF_LIST },
{ CPU_SUBTYPE_ARM_V7S, CPU_SUBTYPE_ARM_V7, CPU_SUBTYPE_ARM_V7F, CPU_SUBTYPE_ARM_V6, CPU_SUBTYPE_ARM_V5TEJ, CPU_SUBTYPE_ARM_V4T, CPU_SUBTYPE_ARM_ALL, CPU_SUBTYPE_END_OF_LIST },
{ CPU_SUBTYPE_ARM_V7, CPU_SUBTYPE_ARM_V6, CPU_SUBTYPE_ARM_V5TEJ, CPU_SUBTYPE_ARM_V4T, CPU_SUBTYPE_ARM_ALL, CPU_SUBTYPE_END_OF_LIST },
{ CPU_SUBTYPE_ARM_V6, CPU_SUBTYPE_ARM_V5TEJ, CPU_SUBTYPE_ARM_V4T, CPU_SUBTYPE_ARM_ALL, CPU_SUBTYPE_END_OF_LIST, CPU_SUBTYPE_END_OF_LIST },
{ CPU_SUBTYPE_ARM_XSCALE, CPU_SUBTYPE_ARM_V5TEJ, CPU_SUBTYPE_ARM_V4T, CPU_SUBTYPE_ARM_ALL, CPU_SUBTYPE_END_OF_LIST, CPU_SUBTYPE_END_OF_LIST },
{ CPU_SUBTYPE_ARM_V5TEJ, CPU_SUBTYPE_ARM_V4T, CPU_SUBTYPE_ARM_ALL, CPU_SUBTYPE_END_OF_LIST, CPU_SUBTYPE_END_OF_LIST, CPU_SUBTYPE_END_OF_LIST },
{ CPU_SUBTYPE_ARM_V4T, CPU_SUBTYPE_ARM_ALL, CPU_SUBTYPE_END_OF_LIST, CPU_SUBTYPE_END_OF_LIST, CPU_SUBTYPE_END_OF_LIST, CPU_SUBTYPE_END_OF_LIST },
};
#endif
#if __x86_64__
const int kX86_64_RowCount = 2;
static const cpu_subtype_t kX86_64[kX86_64_RowCount][5] = {
{ CPU_SUBTYPE_X86_64_H, CPU_SUBTYPE_LIB64|CPU_SUBTYPE_X86_64_H, CPU_SUBTYPE_LIB64|CPU_SUBTYPE_X86_64_ALL, CPU_SUBTYPE_X86_64_ALL, CPU_SUBTYPE_END_OF_LIST },
{ CPU_SUBTYPE_X86_64_ALL, CPU_SUBTYPE_LIB64|CPU_SUBTYPE_X86_64_ALL, CPU_SUBTYPE_END_OF_LIST },
};
#endif
static const cpu_subtype_t* findCPUSubtypeList(cpu_type_t cpu, cpu_subtype_t subtype)
{
switch (cpu) {
#if __arm__
case CPU_TYPE_ARM:
for (int i=0; i < kARM_RowCount ; ++i) {
if ( kARM[i][0] == subtype )
return kARM[i];
}
break;
#endif
#if __x86_64__
case CPU_TYPE_X86_64:
for (int i=0; i < kX86_64_RowCount ; ++i) {
if ( kX86_64[i][0] == subtype )
return kX86_64[i];
}
break;
#endif
}
return NULL;
}
static bool fatFindBestFromOrderedList(cpu_type_t cpu, const cpu_subtype_t list[], const fat_header* fh, uint64_t* offset, uint64_t* len)
{
const fat_arch* const archs = (fat_arch*)(((char*)fh)+sizeof(fat_header));
for (uint32_t subTypeIndex=0; list[subTypeIndex] != CPU_SUBTYPE_END_OF_LIST; ++subTypeIndex) {
for(uint32_t fatIndex=0; fatIndex < OSSwapBigToHostInt32(fh->nfat_arch); ++fatIndex) {
if ( ((cpu_type_t)OSSwapBigToHostInt32(archs[fatIndex].cputype) == cpu)
&& (list[subTypeIndex] == (cpu_subtype_t)OSSwapBigToHostInt32(archs[fatIndex].cpusubtype)) ) {
*offset = OSSwapBigToHostInt32(archs[fatIndex].offset);
*len = OSSwapBigToHostInt32(archs[fatIndex].size);
return true;
}
}
}
return false;
}
static bool fatFindExactMatch(cpu_type_t cpu, cpu_subtype_t subtype, const fat_header* fh, uint64_t* offset, uint64_t* len)
{
const fat_arch* archs = (fat_arch*)(((char*)fh)+sizeof(fat_header));
for(uint32_t i=0; i < OSSwapBigToHostInt32(fh->nfat_arch); ++i) {
if ( ((cpu_type_t)OSSwapBigToHostInt32(archs[i].cputype) == cpu)
&& ((cpu_subtype_t)OSSwapBigToHostInt32(archs[i].cpusubtype) == subtype) ) {
*offset = OSSwapBigToHostInt32(archs[i].offset);
*len = OSSwapBigToHostInt32(archs[i].size);
return true;
}
}
return false;
}
static bool fatFindRunsOnAllCPUs(cpu_type_t cpu, const fat_header* fh, uint64_t* offset, uint64_t* len)
{
const fat_arch* archs = (fat_arch*)(((char*)fh)+sizeof(fat_header));
for(uint32_t i=0; i < OSSwapBigToHostInt32(fh->nfat_arch); ++i) {
if ( (cpu_type_t)OSSwapBigToHostInt32(archs[i].cputype) == cpu) {
switch (cpu) {
#if __arm__
case CPU_TYPE_ARM:
if ( (cpu_subtype_t)OSSwapBigToHostInt32(archs[i].cpusubtype) == CPU_SUBTYPE_ARM_ALL ) {
*offset = OSSwapBigToHostInt32(archs[i].offset);
*len = OSSwapBigToHostInt32(archs[i].size);
return true;
}
break;
#endif
#if __x86_64__
case CPU_TYPE_X86_64:
if ( (cpu_subtype_t)OSSwapBigToHostInt32(archs[i].cpusubtype) == CPU_SUBTYPE_X86_64_ALL ) {
*offset = OSSwapBigToHostInt32(archs[i].offset);
*len = OSSwapBigToHostInt32(archs[i].size);
return true;
}
break;
#endif
}
}
}
return false;
}
#endif // CPU_SUBTYPES_SUPPORTED
static bool fatFindBest(const fat_header* fh, uint64_t* offset, uint64_t* len)
{
#if CPU_SUBTYPES_SUPPORTED
const cpu_type_t cpu = sMainExecutableMachHeader->cputype;
if ( (cpu & CPU_TYPE_MASK) == sHostCPU ) {
const cpu_subtype_t* subTypePreferenceList = findCPUSubtypeList(cpu, sHostCPUsubtype);
if ( subTypePreferenceList != NULL )
return fatFindBestFromOrderedList(cpu, subTypePreferenceList, fh, offset, len);
if ( fatFindExactMatch(cpu, sHostCPUsubtype, fh, offset, len) )
return true;
}
return fatFindRunsOnAllCPUs(cpu, fh, offset, len);
#else
const fat_arch* archs = (fat_arch*)(((char*)fh)+sizeof(fat_header));
for(uint32_t i=0; i < OSSwapBigToHostInt32(fh->nfat_arch); ++i) {
if ( (cpu_type_t)OSSwapBigToHostInt32(archs[i].cputype) == sMainExecutableMachHeader->cputype) {
*offset = OSSwapBigToHostInt32(archs[i].offset);
*len = OSSwapBigToHostInt32(archs[i].size);
return true;
}
}
return false;
#endif
}
bool isCompatibleMachO(const uint8_t* firstPage, const char* path)
{
#if CPU_SUBTYPES_SUPPORTED
const mach_header* mh = (mach_header*)firstPage;
if ( mh->magic == sMainExecutableMachHeader->magic ) {
if ( mh->cputype == sMainExecutableMachHeader->cputype ) {
if ( (mh->cputype & CPU_TYPE_MASK) == sHostCPU ) {
const cpu_subtype_t* subTypePreferenceList = findCPUSubtypeList(mh->cputype, sHostCPUsubtype);
if ( subTypePreferenceList != NULL ) {
for (const cpu_subtype_t* p = subTypePreferenceList; *p != CPU_SUBTYPE_END_OF_LIST; ++p) {
if ( *p == mh->cpusubtype )
return true;
}
throwf("incompatible cpu-subtype: 0x%08X in %s", mh->cpusubtype, path);
}
if ( mh->cpusubtype == sHostCPUsubtype )
return true;
}
switch (mh->cputype) {
case CPU_TYPE_I386:
case CPU_TYPE_X86_64:
return true;
}
}
}
#else
const mach_header* mh = (mach_header*)firstPage;
if ( mh->magic == sMainExecutableMachHeader->magic ) {
if ( mh->cputype == sMainExecutableMachHeader->cputype ) {
return true;
}
}
#endif
return false;
}
static ImageLoader* instantiateFromLoadedImage(const macho_header* mh, uintptr_t slide, const char* path)
{
if ( isCompatibleMachO((const uint8_t*)mh, path) ) {
ImageLoader* image = ImageLoaderMachO::instantiateMainExecutable(mh, slide, path, gLinkContext);
addImage(image);
return image;
}
throw "main executable not a known format";
}
#if DYLD_SHARED_CACHE_SUPPORT
static bool findInSharedCacheImage(const char* path, bool searchByPath, const struct stat* stat_buf, const macho_header** mh, const char** pathInCache, long* slide)
{
if ( sSharedCache != NULL ) {
#if __MAC_OS_X_VERSION_MIN_REQUIRED
struct stat statb;
if ( stat_buf == NULL ) {
if ( my_stat(path, &statb) == -1 )
return false;
stat_buf = &statb;
}
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED
uint64_t hash = 0;
for (const char* s=path; *s != '\0'; ++s)
hash += hash*4 + *s;
#endif
const dyld_cache_image_info* const start = (dyld_cache_image_info*)((uint8_t*)sSharedCache + sSharedCache->imagesOffset);
const dyld_cache_image_info* const end = &start[sSharedCache->imagesCount];
#if __IPHONE_OS_VERSION_MIN_REQUIRED
const bool cacheHasHashInfo = (start->modTime == 0);
#endif
for( const dyld_cache_image_info* p = start; p != end; ++p) {
#if __IPHONE_OS_VERSION_MIN_REQUIRED
const char* aPath = (char*)sSharedCache + p->pathFileOffset;
if ( cacheHasHashInfo && (p->inode != hash) )
continue;
if ( strcmp(path, aPath) == 0 ) {
*mh = (macho_header*)(p->address+sSharedCacheSlide);
*pathInCache = aPath;
*slide = sSharedCacheSlide;
return true;
}
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
bool inodeMatch = ( ((time_t)p->modTime == stat_buf->st_mtime) && ((ino_t)p->inode == stat_buf->st_ino) );
if ( searchByPath || sSharedCacheIgnoreInodeAndTimeStamp || inodeMatch ) {
const char* aPath = (char*)sSharedCache + p->pathFileOffset;
bool cacheHit = (strcmp(path, aPath) == 0);
if ( inodeMatch && !cacheHit ) {
struct stat pathInCacheStatBuf;
if ( my_stat(aPath, &pathInCacheStatBuf) != -1 )
cacheHit = ( (pathInCacheStatBuf.st_dev == stat_buf->st_dev) && (pathInCacheStatBuf.st_ino == stat_buf->st_ino) );
}
if ( cacheHit ) {
*mh = (macho_header*)(p->address+sSharedCacheSlide);
*pathInCache = aPath;
*slide = sSharedCacheSlide;
return true;
}
}
#endif
}
}
return false;
}
bool inSharedCache(const char* path)
{
const macho_header* mhInCache;
const char* pathInCache;
long slide;
return findInSharedCacheImage(path, true, NULL, &mhInCache, &pathInCache, &slide);
}
#endif
static ImageLoader* checkandAddImage(ImageLoader* image, const LoadContext& context)
{
const char* loadedImageInstallPath = image->getInstallPath();
if ( image->isDylib() && (loadedImageInstallPath != NULL) && (loadedImageInstallPath[0] == '/') ) {
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* anImage = *it;
const char* installPath = anImage->getInstallPath();
if ( installPath != NULL) {
if ( strcmp(loadedImageInstallPath, installPath) == 0 ) {
removeImage(image);
ImageLoader::deleteImage(image);
return anImage;
}
}
}
}
if ( context.mustBeBundle && !image->isBundle() )
throw "not a bundle";
if ( context.mustBeDylib && !image->isDylib() )
throw "not a dylib";
if ( image->isExecutable() ) {
if ( !context.canBePIE || !image->isPositionIndependentExecutable() )
throw "can't load a main executable";
}
if ( ! image->isBundle() )
addImage(image);
return image;
}
#if TARGET_IPHONE_SIMULATOR
static bool isSimulatorBinary(const uint8_t* firstPage, const char* path)
{
const macho_header* mh = (macho_header*)firstPage;
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
const struct load_command* const cmdsReadEnd = (struct load_command*)(((char*)mh)+4096);
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
case LC_VERSION_MIN_IPHONEOS:
return true;
case LC_VERSION_MIN_MACOSX:
if ( strncmp(path, "/usr/lib/system/libsystem_", 26) == 0 )
return true;
return false;
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
if ( cmd > cmdsReadEnd )
return true;
}
return false;
}
#endif
static ImageLoader* loadPhase6(int fd, const struct stat& stat_buf, const char* path, const LoadContext& context)
{
uint64_t fileOffset = 0;
uint64_t fileLength = stat_buf.st_size;
if ( (stat_buf.st_mode & S_IFMT) != S_IFREG )
throw "not a file";
uint8_t firstPage[4096];
bool shortPage = false;
if ( fileLength < 4096 ) {
if ( pread(fd, firstPage, fileLength, 0) != (ssize_t)fileLength )
throwf("pread of short file failed: %d", errno);
shortPage = true;
}
else {
if ( pread(fd, firstPage, 4096,0) != 4096 )
throwf("pread of first 4K failed: %d", errno);
}
const fat_header* fileStartAsFat = (fat_header*)firstPage;
if ( fileStartAsFat->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) {
if ( fatFindBest(fileStartAsFat, &fileOffset, &fileLength) ) {
if ( (fileOffset+fileLength) > (uint64_t)(stat_buf.st_size) )
throwf("truncated fat file. file length=%llu, but needed slice goes to %llu", stat_buf.st_size, fileOffset+fileLength);
if (pread(fd, firstPage, 4096, fileOffset) != 4096)
throwf("pread of fat file failed: %d", errno);
}
else {
throw "no matching architecture in universal wrapper";
}
}
if ( shortPage )
throw "file too short";
if ( isCompatibleMachO(firstPage, path) ) {
switch ( ((mach_header*)firstPage)->filetype ) {
case MH_EXECUTE:
case MH_DYLIB:
case MH_BUNDLE:
break;
default:
throw "mach-o, but wrong filetype";
}
#if TARGET_IPHONE_SIMULATOR
if ( !isSimulatorBinary(firstPage, path) ) {
throw "mach-o, but not built for iOS simulator";
}
#endif
ImageLoader* image = ImageLoaderMachO::instantiateFromFile(path, fd, firstPage, fileOffset, fileLength, stat_buf, gLinkContext);
return checkandAddImage(image, context);
}
switch (*(uint32_t*)firstPage) {
case MH_MAGIC:
case MH_CIGAM:
case MH_MAGIC_64:
case MH_CIGAM_64:
throw "mach-o, but wrong architecture";
default:
throwf("unknown file type, first eight bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
firstPage[0], firstPage[1], firstPage[2], firstPage[3], firstPage[4], firstPage[5], firstPage[6],firstPage[7]);
}
}
static ImageLoader* loadPhase5open(const char* path, const LoadContext& context, const struct stat& stat_buf, std::vector<const char*>* exceptions)
{
FileOpener file(path);
if ( file.getFileDescriptor() == -1 ) {
int err = errno;
if ( err != ENOENT ) {
const char* newMsg = dyld::mkstringf("%s: open() failed with errno=%d", path, err);
exceptions->push_back(newMsg);
}
return NULL;
}
try {
return loadPhase6(file.getFileDescriptor(), stat_buf, path, context);
}
catch (const char* msg) {
const char* newMsg = dyld::mkstringf("%s: %s", path, msg);
exceptions->push_back(newMsg);
free((void*)msg);
return NULL;
}
}
#if __MAC_OS_X_VERSION_MIN_REQUIRED
static ImageLoader* loadPhase5load(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
ImageLoader* image = NULL;
struct stat stat_buf;
if ( my_stat(path, &stat_buf) == -1 ) {
int err = errno;
if ( err != ENOENT ) {
exceptions->push_back(dyld::mkstringf("%s: stat() failed with errno=%d", path, err));
}
return NULL;
}
image = findLoadedImage(stat_buf);
if ( image != NULL )
return image;
if ( context.dontLoad )
return NULL;
#if DYLD_SHARED_CACHE_SUPPORT
const macho_header* mhInCache;
const char* pathInCache;
long slideInCache;
if ( findInSharedCacheImage(path, false, &stat_buf, &mhInCache, &pathInCache, &slideInCache) ) {
image = ImageLoaderMachO::instantiateFromCache(mhInCache, pathInCache, slideInCache, stat_buf, gLinkContext);
return checkandAddImage(image, context);
}
#endif
return loadPhase5open(path, context, stat_buf, exceptions);
}
#endif // __MAC_OS_X_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED
static ImageLoader* loadPhase5stat(const char* path, const LoadContext& context, struct stat* stat_buf,
int* statErrNo, bool* imageFound, std::vector<const char*>* exceptions)
{
ImageLoader* image = NULL;
*imageFound = false;
*statErrNo = 0;
if ( my_stat(path, stat_buf) == 0 ) {
image = findLoadedImage(*stat_buf);
if ( image != NULL ) {
*imageFound = true;
return image;
}
if ( context.dontLoad ) {
*imageFound = true;
return NULL;
}
image = loadPhase5open(path, context, *stat_buf, exceptions);
if ( image != NULL ) {
*imageFound = true;
return image;
}
}
else {
*statErrNo = errno;
}
return NULL;
}
static ImageLoader* loadPhase5load(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
struct stat stat_buf;
bool imageFound;
int statErrNo;
ImageLoader* image;
#if DYLD_SHARED_CACHE_SUPPORT
if ( sDylibsOverrideCache ) {
image = loadPhase5stat(path, context, &stat_buf, &statErrNo, &imageFound, exceptions);
if ( imageFound )
return image;
}
const macho_header* mhInCache;
const char* pathInCache;
long slideInCache;
if ( findInSharedCacheImage(path, true, NULL, &mhInCache, &pathInCache, &slideInCache) ) {
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); ++it) {
ImageLoader* anImage = *it;
if ( (const macho_header*)anImage->machHeader() == mhInCache )
return anImage;
}
if ( context.dontLoad )
return NULL;
bzero(&stat_buf, sizeof(stat_buf));
image = ImageLoaderMachO::instantiateFromCache(mhInCache, pathInCache, slideInCache, stat_buf, gLinkContext);
return checkandAddImage(image, context);
}
if ( !sDylibsOverrideCache ) {
image = loadPhase5stat(path, context, &stat_buf, &statErrNo, &imageFound, exceptions);
if ( imageFound )
return image;
}
#else
image = loadPhase5stat(path, context, &stat_buf, &statErrNo, &imageFound, exceptions);
if ( imageFound )
return image;
#endif
if ( (statErrNo != ENOENT) && (statErrNo != 0) ) {
exceptions->push_back(dyld::mkstringf("%s: stat() failed with errno=%d", path, statErrNo));
}
return NULL;
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED
static ImageLoader* loadPhase5check(const char* path, const char* orgPath, const LoadContext& context)
{
uint32_t hash = ImageLoader::hash(path);
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* anImage = *it;
if ( anImage->getPathHash() == hash ) {
if ( strcmp(path, anImage->getPath()) == 0 ) {
if ( !context.mustBeDylib || anImage->isDylib() )
return anImage;
}
}
if ( context.matchByInstallName || anImage->matchInstallPath() ) {
const char* installPath = anImage->getInstallPath();
if ( installPath != NULL) {
if ( strcmp(path, installPath) == 0 ) {
if ( !context.mustBeDylib || anImage->isDylib() )
return anImage;
}
}
}
if ( (orgPath[0] == '@') && (strncmp(orgPath, "@rpath/", 7) == 0) ) {
const char* installPath = anImage->getInstallPath();
if ( installPath != NULL) {
if ( !context.mustBeDylib || anImage->isDylib() ) {
if ( strcmp(orgPath, installPath) == 0 )
return anImage;
}
}
}
}
return NULL;
}
static ImageLoader* loadPhase5(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
for (std::vector<DylibOverride>::iterator it = sDylibOverrides.begin(); it != sDylibOverrides.end(); ++it) {
if ( strcmp(it->installName, path) == 0 ) {
path = it->override;
break;
}
}
if ( exceptions != NULL )
return loadPhase5load(path, orgPath, context, exceptions);
else
return loadPhase5check(path, orgPath, context);
}
static ImageLoader* loadPhase4(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
ImageLoader* image = NULL;
if ( gLinkContext.imageSuffix != NULL ) {
char pathWithSuffix[strlen(path)+strlen( gLinkContext.imageSuffix)+2];
ImageLoader::addSuffix(path, gLinkContext.imageSuffix, pathWithSuffix);
image = loadPhase5(pathWithSuffix, orgPath, context, exceptions);
}
if ( image == NULL )
image = loadPhase5(path, orgPath, context, exceptions);
return image;
}
static ImageLoader* loadPhase2(const char* path, const char* orgPath, const LoadContext& context,
const char* const frameworkPaths[], const char* const libraryPaths[],
std::vector<const char*>* exceptions);
static ImageLoader* loadPhase3(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
ImageLoader* image = NULL;
if ( strncmp(path, "@executable_path/", 17) == 0 ) {
if ( sProcessIsRestricted )
throwf("unsafe use of @executable_path in %s with restricted binary", context.origin);
const char* executablePath = sExecPath;
char newPath[strlen(executablePath) + strlen(path)];
strcpy(newPath, executablePath);
char* addPoint = strrchr(newPath,'/');
if ( addPoint != NULL )
strcpy(&addPoint[1], &path[17]);
else
strcpy(newPath, &path[17]);
image = loadPhase4(newPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
char resolvedPath[PATH_MAX];
if ( realpath(sExecPath, resolvedPath) != NULL ) {
char newRealPath[strlen(resolvedPath) + strlen(path)];
strcpy(newRealPath, resolvedPath);
char* addPoint = strrchr(newRealPath,'/');
if ( addPoint != NULL )
strcpy(&addPoint[1], &path[17]);
else
strcpy(newRealPath, &path[17]);
image = loadPhase4(newRealPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
}
}
else if ( (strncmp(path, "@loader_path/", 13) == 0) && (context.origin != NULL) ) {
if ( sProcessIsRestricted && (strcmp(context.origin, sExecPath) == 0) )
throwf("unsafe use of @loader_path in %s with restricted binary", context.origin);
char newPath[strlen(context.origin) + strlen(path)];
strcpy(newPath, context.origin);
char* addPoint = strrchr(newPath,'/');
if ( addPoint != NULL )
strcpy(&addPoint[1], &path[13]);
else
strcpy(newPath, &path[13]);
image = loadPhase4(newPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
char resolvedPath[PATH_MAX];
if ( realpath(context.origin, resolvedPath) != NULL ) {
char newRealPath[strlen(resolvedPath) + strlen(path)];
strcpy(newRealPath, resolvedPath);
char* addPoint = strrchr(newRealPath,'/');
if ( addPoint != NULL )
strcpy(&addPoint[1], &path[13]);
else
strcpy(newRealPath, &path[13]);
image = loadPhase4(newRealPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
}
}
else if ( context.implicitRPath || (strncmp(path, "@rpath/", 7) == 0) ) {
const char* trailingPath = (strncmp(path, "@rpath/", 7) == 0) ? &path[7] : path;
for(const ImageLoader::RPathChain* rp=context.rpath; rp != NULL; rp=rp->next) {
if (rp->paths != NULL ) {
for(std::vector<const char*>::iterator it=rp->paths->begin(); it != rp->paths->end(); ++it) {
const char* anRPath = *it;
char newPath[strlen(anRPath) + strlen(trailingPath)+2];
strcpy(newPath, anRPath);
strcat(newPath, "/");
strcat(newPath, trailingPath);
image = loadPhase4(newPath, orgPath, context, exceptions);
if ( gLinkContext.verboseRPaths && (exceptions != NULL) ) {
if ( image != NULL )
dyld::log("RPATH successful expansion of %s to: %s\n", orgPath, newPath);
else
dyld::log("RPATH failed to expanding %s to: %s\n", orgPath, newPath);
}
if ( image != NULL )
return image;
}
}
}
if ( sEnv.LD_LIBRARY_PATH != NULL ) {
image = loadPhase2(trailingPath, orgPath, context, NULL, sEnv.LD_LIBRARY_PATH, exceptions);
if ( image != NULL )
return image;
}
if ( (exceptions != NULL) && (trailingPath != path) )
return NULL;
}
else if (sProcessIsRestricted && (path[0] != '/' )) {
throwf("unsafe use of relative rpath %s in %s with restricted binary", path, context.origin);
}
return loadPhase4(path, orgPath, context, exceptions);
}
static ImageLoader* loadPhase2(const char* path, const char* orgPath, const LoadContext& context,
const char* const frameworkPaths[], const char* const libraryPaths[],
std::vector<const char*>* exceptions)
{
ImageLoader* image = NULL;
const char* frameworkPartialPath = getFrameworkPartialPath(path);
if ( frameworkPaths != NULL ) {
if ( frameworkPartialPath != NULL ) {
const size_t frameworkPartialPathLen = strlen(frameworkPartialPath);
for(const char* const* fp = frameworkPaths; *fp != NULL; ++fp) {
char npath[strlen(*fp)+frameworkPartialPathLen+8];
strcpy(npath, *fp);
strcat(npath, "/");
strcat(npath, frameworkPartialPath);
image = loadPhase4(npath, orgPath, context, exceptions);
if ( image != NULL )
return image;
}
}
}
if ( (libraryPaths != NULL) && ((frameworkPartialPath == NULL) || sFrameworksFoundAsDylibs) ) {
const char* libraryLeafName = getLibraryLeafName(path);
const size_t libraryLeafNameLen = strlen(libraryLeafName);
for(const char* const* lp = libraryPaths; *lp != NULL; ++lp) {
char libpath[strlen(*lp)+libraryLeafNameLen+8];
strcpy(libpath, *lp);
strcat(libpath, "/");
strcat(libpath, libraryLeafName);
image = loadPhase4(libpath, orgPath, context, exceptions);
if ( image != NULL )
return image;
}
}
return NULL;
}
static ImageLoader* loadPhase1(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
ImageLoader* image = NULL;
if ( context.useLdLibraryPath && (sEnv.LD_LIBRARY_PATH != NULL) ) {
image = loadPhase2(path, orgPath, context, NULL, sEnv.LD_LIBRARY_PATH, exceptions);
if ( image != NULL )
return image;
}
if ( context.useSearchPaths && ((sEnv.DYLD_FRAMEWORK_PATH != NULL) || (sEnv.DYLD_LIBRARY_PATH != NULL)) ) {
image = loadPhase2(path, orgPath, context, sEnv.DYLD_FRAMEWORK_PATH, sEnv.DYLD_LIBRARY_PATH, exceptions);
if ( image != NULL )
return image;
}
image = loadPhase3(path, orgPath, context, exceptions);
if ( image != NULL )
return image;
const char* const* fallbackLibraryPaths = sEnv.DYLD_FALLBACK_LIBRARY_PATH;
if ( (fallbackLibraryPaths != NULL) && !context.useFallbackPaths )
fallbackLibraryPaths = NULL;
if ( !context.dontLoad && (exceptions != NULL) && ((sEnv.DYLD_FALLBACK_FRAMEWORK_PATH != NULL) || (fallbackLibraryPaths != NULL)) ) {
image = loadPhase2(path, orgPath, context, sEnv.DYLD_FALLBACK_FRAMEWORK_PATH, fallbackLibraryPaths, exceptions);
if ( image != NULL )
return image;
}
return NULL;
}
static ImageLoader* loadPhase0(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
if ( (gLinkContext.rootPaths != NULL) && (path[0] == '/') ) {
for(const char* const* rootPath = gLinkContext.rootPaths ; *rootPath != NULL; ++rootPath) {
char newPath[strlen(*rootPath) + strlen(path)+2];
strcpy(newPath, *rootPath);
strcat(newPath, path);
ImageLoader* image = loadPhase1(newPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
}
}
return loadPhase1(path, orgPath, context, exceptions);
}
#if DYLD_SHARED_CACHE_SUPPORT
static bool cacheablePath(const char* path) {
if (strncmp(path, "/usr/lib/", 9) == 0)
return true;
if (strncmp(path, "/System/Library/", 16) == 0)
return true;
return false;
}
#endif
ImageLoader* load(const char* path, const LoadContext& context)
{
CRSetCrashLogMessage2(path);
const char* orgPath = path;
char realPath[PATH_MAX];
if ( context.useSearchPaths && ( gLinkContext.imageSuffix != NULL) ) {
if ( realpath(path, realPath) != NULL )
path = realPath;
}
ImageLoader* image = loadPhase0(path, orgPath, context, NULL);
if ( image != NULL ) {
CRSetCrashLogMessage2(NULL);
return image;
}
std::vector<const char*> exceptions;
image = loadPhase0(path, orgPath, context, &exceptions);
#if __IPHONE_OS_VERSION_MIN_REQUIRED && DYLD_SHARED_CACHE_SUPPORT && !TARGET_IPHONE_SIMULATOR
if ( (image == NULL) && cacheablePath(path) && !context.dontLoad ) {
char resolvedPath[PATH_MAX];
realpath(path, resolvedPath);
int myerr = errno;
if ( (myerr == ENOENT) || (myerr == 0) )
{
const macho_header* mhInCache;
const char* pathInCache;
long slideInCache;
if ( findInSharedCacheImage(resolvedPath, false, NULL, &mhInCache, &pathInCache, &slideInCache) ) {
struct stat stat_buf;
bzero(&stat_buf, sizeof(stat_buf));
try {
image = ImageLoaderMachO::instantiateFromCache(mhInCache, pathInCache, slideInCache, stat_buf, gLinkContext);
image = checkandAddImage(image, context);
}
catch (...) {
image = NULL;
}
}
}
}
#endif
CRSetCrashLogMessage2(NULL);
if ( image != NULL ) {
for (std::vector<const char*>::iterator it = exceptions.begin(); it != exceptions.end(); ++it) {
free((void*)(*it));
}
#if DYLD_SHARED_CACHE_SUPPORT
if ( !gSharedCacheOverridden && !image->inSharedCache() && image->isDylib() && cacheablePath(path) && inSharedCache(path) ) {
gSharedCacheOverridden = true;
}
#endif
return image;
}
else if ( exceptions.size() == 0 ) {
if ( context.dontLoad ) {
return NULL;
}
else
throw "image not found";
}
else {
const char* msgStart = "no suitable image found. Did find:";
const char* delim = "\n\t";
size_t allsizes = strlen(msgStart)+8;
for (size_t i=0; i < exceptions.size(); ++i)
allsizes += (strlen(exceptions[i]) + strlen(delim));
char* fullMsg = new char[allsizes];
strcpy(fullMsg, msgStart);
for (size_t i=0; i < exceptions.size(); ++i) {
strcat(fullMsg, delim);
strcat(fullMsg, exceptions[i]);
free((void*)exceptions[i]);
}
throw (const char*)fullMsg;
}
}
#if DYLD_SHARED_CACHE_SUPPORT
#if __i386__
#define ARCH_NAME "i386"
#define ARCH_CACHE_MAGIC "dyld_v1 i386"
#elif __x86_64__
#define ARCH_NAME "x86_64"
#define ARCH_CACHE_MAGIC "dyld_v1 x86_64"
#define ARCH_NAME_H "x86_64h"
#define ARCH_CACHE_MAGIC_H "dyld_v1 x86_64h"
#elif __ARM_ARCH_5TEJ__
#define ARCH_NAME "armv5"
#define ARCH_CACHE_MAGIC "dyld_v1 armv5"
#elif __ARM_ARCH_6K__
#define ARCH_NAME "armv6"
#define ARCH_CACHE_MAGIC "dyld_v1 armv6"
#elif __ARM_ARCH_7F__
#define ARCH_NAME "armv7f"
#define ARCH_CACHE_MAGIC "dyld_v1 armv7f"
#elif __ARM_ARCH_7K__
#define ARCH_NAME "armv7k"
#define ARCH_CACHE_MAGIC "dyld_v1 armv7k"
#elif __ARM_ARCH_7A__
#define ARCH_NAME "armv7"
#define ARCH_CACHE_MAGIC "dyld_v1 armv7"
#elif __ARM_ARCH_7S__
#define ARCH_NAME "armv7s"
#define ARCH_CACHE_MAGIC "dyld_v1 armv7s"
#elif __arm64__
#define ARCH_NAME "arm64"
#define ARCH_CACHE_MAGIC "dyld_v1 arm64"
#endif
static int __attribute__((noinline)) _shared_region_check_np(uint64_t* start_address)
{
if ( gLinkContext.sharedRegionMode == ImageLoader::kUseSharedRegion )
return syscall(294, start_address);
return -1;
}
static int __attribute__((noinline)) _shared_region_map_and_slide_np(int fd, uint32_t count, const shared_file_mapping_np mappings[],
int codeSignatureMappingIndex, long slide, void* slideInfo, unsigned long slideInfoSize)
{
if ( codeSignatureMappingIndex != -1 ) {
fsignatures_t siginfo;
siginfo.fs_file_start = 0; siginfo.fs_blob_start = (void*)mappings[codeSignatureMappingIndex].sfm_file_offset;
siginfo.fs_blob_size = mappings[codeSignatureMappingIndex].sfm_size;
int result = fcntl(fd, F_ADDFILESIGS, &siginfo);
if ( (result == -1) && gLinkContext.verboseMapping )
dyld::log("dyld: code signature registration for shared cache failed with errno=%d\n", errno);
}
if ( gLinkContext.sharedRegionMode == ImageLoader::kUseSharedRegion ) {
return syscall(438, fd, count, mappings, slide, slideInfo, slideInfoSize);
}
vm_deallocate(mach_task_self(), (vm_address_t)SHARED_REGION_BASE, SHARED_REGION_SIZE);
dyld::gProcessInfo->processDetachedFromSharedRegion = true;
const shared_file_mapping_np* const start = mappings;
const shared_file_mapping_np* const end = &mappings[count];
for (const shared_file_mapping_np* p = start; p < end; ++p ) {
void* mmapAddress = (void*)(uintptr_t)(p->sfm_address);
size_t size = p->sfm_size;
int protection = 0;
if ( p->sfm_init_prot & VM_PROT_EXECUTE )
protection |= PROT_EXEC;
if ( p->sfm_init_prot & VM_PROT_READ )
protection |= PROT_READ;
if ( p->sfm_init_prot & VM_PROT_WRITE )
protection |= PROT_WRITE;
off_t offset = p->sfm_file_offset;
if ( mmap(mmapAddress, size, protection, MAP_FIXED | MAP_PRIVATE, fd, offset) != mmapAddress ) {
vm_deallocate(mach_task_self(), (vm_address_t)SHARED_REGION_BASE, SHARED_REGION_SIZE);
gLinkContext.sharedRegionMode = ImageLoader::kDontUseSharedRegion;
if ( gLinkContext.verboseMapping ) {
dyld::log("dyld: shared cached region cannot be mapped at address %p with size 0x%08lX\n",
mmapAddress, size);
}
return -1;
}
}
if ( slide != 0 ) {
const uintptr_t dataPagesStart = mappings[1].sfm_address;
const dyld_cache_slide_info* slideInfoHeader = (dyld_cache_slide_info*)slideInfo;
const uint16_t* toc = (uint16_t*)((long)(slideInfoHeader) + slideInfoHeader->toc_offset);
const uint8_t* entries = (uint8_t*)((long)(slideInfoHeader) + slideInfoHeader->entries_offset);
for(uint32_t i=0; i < slideInfoHeader->toc_count; ++i) {
const uint8_t* entry = &entries[toc[i]*slideInfoHeader->entries_size];
const uint8_t* page = (uint8_t*)(long)(dataPagesStart + (4096*i));
for(int j=0; j < 128; ++j) {
uint8_t b = entry[j];
if ( b != 0 ) {
for(int k=0; k < 8; ++k) {
if ( b & (1<<k) ) {
uintptr_t* p = (uintptr_t*)(page + j*8*4 + k*4);
uintptr_t value = *p;
*p = value + slide;
}
}
}
}
}
}
gLinkContext.sharedRegionMode = ImageLoader::kUsePrivateSharedRegion;
return 0;
}
const void* imMemorySharedCacheHeader()
{
return sSharedCache;
}
int openSharedCacheFile()
{
char path[MAXPATHLEN];
strlcpy(path, sSharedCacheDir, MAXPATHLEN);
strlcat(path, "/", MAXPATHLEN);
#if __x86_64__
if ( sHaswell ) {
strlcat(path, DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME_H, MAXPATHLEN);
int fd = my_open(path, O_RDONLY, 0);
if ( fd != -1 ) {
if ( gLinkContext.verboseMapping )
dyld::log("dyld: Mapping%s shared cache from %s\n", (gLinkContext.sharedRegionMode == ImageLoader::kUsePrivateSharedRegion) ? " private": "", path);
return fd;
}
strlcpy(path, sSharedCacheDir, MAXPATHLEN);
}
#endif
strlcat(path, DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME, MAXPATHLEN);
if ( gLinkContext.verboseMapping )
dyld::log("dyld: Mapping%s shared cache from %s\n", (gLinkContext.sharedRegionMode == ImageLoader::kUsePrivateSharedRegion) ? " private": "", path);
return my_open(path, O_RDONLY, 0);
}
static void getCacheBounds(uint32_t mappingsCount, const shared_file_mapping_np mappings[], uint64_t& lowAddress, uint64_t& highAddress)
{
lowAddress = 0;
highAddress = 0;
for(uint32_t i=0; i < mappingsCount; ++i) {
if ( lowAddress == 0 ) {
lowAddress = mappings[i].sfm_address;
highAddress = mappings[i].sfm_address + mappings[i].sfm_size;
}
else {
if ( mappings[i].sfm_address < lowAddress )
lowAddress = mappings[i].sfm_address;
if ( (mappings[i].sfm_address + mappings[i].sfm_size) > highAddress )
highAddress = mappings[i].sfm_address + mappings[i].sfm_size;
}
}
}
static long pickCacheSlide(uint32_t mappingsCount, shared_file_mapping_np mappings[])
{
#if __x86_64__
if ( (mappingsCount >= 3) && (mappings[1].sfm_init_prot == (VM_PROT_READ|VM_PROT_WRITE)) && (mappings[1].sfm_address == 0x00007FFF70000000) ) {
const uint64_t rwSize = mappings[1].sfm_size;
const uint64_t rwSlop = 0x10000000ULL - rwSize;
const uint64_t roSize = (mappings[2].sfm_address + mappings[2].sfm_size) - mappings[0].sfm_address;
const uint64_t roSlop = 0x40000000ULL - roSize;
const uint64_t space = (rwSlop < roSlop) ? rwSlop : roSlop;
long slide = (arc4random() % space) & (-4096);
for(uint32_t i=0; i < mappingsCount; ++i) {
mappings[i].sfm_address += slide;
}
return slide;
}
#endif
uint64_t lowAddress;
uint64_t highAddress;
getCacheBounds(mappingsCount, mappings, lowAddress, highAddress);
const uint64_t space = (SHARED_REGION_BASE + SHARED_REGION_SIZE) - highAddress;
long slide = dyld_page_trunc(arc4random() % space);
for(uint32_t i=0; i < mappingsCount; ++i) {
mappings[i].sfm_address += slide;
}
return slide;
}
static void mapSharedCache()
{
uint64_t cacheBaseAddress = 0;
if ( _shared_region_check_np(&cacheBaseAddress) == 0 ) {
sSharedCache = (dyld_cache_header*)cacheBaseAddress;
#if __x86_64__
const char* magic = (sHaswell ? ARCH_CACHE_MAGIC_H : ARCH_CACHE_MAGIC);
#else
const char* magic = ARCH_CACHE_MAGIC;
#endif
if ( strcmp(sSharedCache->magic, magic) != 0 ) {
sSharedCache = NULL;
if ( gLinkContext.verboseMapping ) {
dyld::log("dyld: existing shared cached in memory is not compatible\n");
return;
}
}
const dyld_cache_header* header = sSharedCache;
if ( (header->mappingOffset >= 0x48) && (header->slideInfoSize != 0) ) {
const uint8_t* loadedAddress = (uint8_t*)sSharedCache;
const dyld_cache_mapping_info* const mappings = (dyld_cache_mapping_info*)(loadedAddress+header->mappingOffset);
const uint8_t* preferedLoadAddress = (uint8_t*)(long)(mappings[0].address);
sSharedCacheSlide = loadedAddress - preferedLoadAddress;
dyld::gProcessInfo->sharedCacheSlide = sSharedCacheSlide;
}
if ( header->mappingOffset >= 0x68 ) {
memcpy(dyld::gProcessInfo->sharedCacheUUID, header->uuid, 16);
}
if ( gLinkContext.verboseMapping ) {
dyld::log("dyld: re-using existing shared cache mapping\n");
}
}
else {
#if __i386__ || __x86_64__
uint32_t safeBootValue = 0;
size_t safeBootValueSize = sizeof(safeBootValue);
if ( (sysctlbyname("kern.safeboot", &safeBootValue, &safeBootValueSize, NULL, 0) == 0) && (safeBootValue != 0) ) {
struct stat dyldCacheStatInfo;
if ( my_stat(MACOSX_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME, &dyldCacheStatInfo) == 0 ) {
struct timeval bootTimeValue;
size_t bootTimeValueSize = sizeof(bootTimeValue);
if ( (sysctlbyname("kern.boottime", &bootTimeValue, &bootTimeValueSize, NULL, 0) == 0) && (bootTimeValue.tv_sec != 0) ) {
if ( dyldCacheStatInfo.st_mtime < bootTimeValue.tv_sec ) {
::unlink(MACOSX_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME);
gLinkContext.sharedRegionMode = ImageLoader::kDontUseSharedRegion;
return;
}
}
}
}
#endif
int fd = openSharedCacheFile();
if ( fd != -1 ) {
uint8_t firstPages[8192];
if ( ::read(fd, firstPages, 8192) == 8192 ) {
dyld_cache_header* header = (dyld_cache_header*)firstPages;
#if __x86_64__
const char* magic = (sHaswell ? ARCH_CACHE_MAGIC_H : ARCH_CACHE_MAGIC);
#else
const char* magic = ARCH_CACHE_MAGIC;
#endif
if ( strcmp(header->magic, magic) == 0 ) {
const dyld_cache_mapping_info* const fileMappingsStart = (dyld_cache_mapping_info*)&firstPages[header->mappingOffset];
const dyld_cache_mapping_info* const fileMappingsEnd = &fileMappingsStart[header->mappingCount];
shared_file_mapping_np mappings[header->mappingCount+1]; unsigned int mappingCount = header->mappingCount;
int codeSignatureMappingIndex = -1;
int readWriteMappingIndex = -1;
int readOnlyMappingIndex = -1;
bool goodCache = false;
struct stat stat_buf;
if ( fstat(fd, &stat_buf) == 0 ) {
goodCache = true;
int i=0;
for (const dyld_cache_mapping_info* p = fileMappingsStart; p < fileMappingsEnd; ++p, ++i) {
mappings[i].sfm_address = p->address;
mappings[i].sfm_size = p->size;
mappings[i].sfm_file_offset = p->fileOffset;
mappings[i].sfm_max_prot = p->maxProt;
mappings[i].sfm_init_prot = p->initProt;
if ( p->fileOffset+p->size > (uint64_t)(stat_buf.st_size+4095 & (-4096)) ) {
dyld::log("dyld: shared cached file is corrupt: %s" DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME "\n", sSharedCacheDir);
goodCache = false;
}
if ( (mappings[i].sfm_init_prot & (VM_PROT_READ|VM_PROT_WRITE)) == (VM_PROT_READ|VM_PROT_WRITE) ) {
readWriteMappingIndex = i;
}
if ( mappings[i].sfm_init_prot == VM_PROT_READ ) {
readOnlyMappingIndex = i;
}
}
uint64_t signatureSize = header->codeSignatureSize;
if ( signatureSize == 0 )
signatureSize = stat_buf.st_size - header->codeSignatureOffset;
if ( signatureSize != 0 ) {
int linkeditMapping = mappingCount-1;
codeSignatureMappingIndex = mappingCount++;
mappings[codeSignatureMappingIndex].sfm_address = mappings[linkeditMapping].sfm_address + mappings[linkeditMapping].sfm_size;
#if __arm__ || __arm64__
mappings[codeSignatureMappingIndex].sfm_size = (signatureSize+16383) & (-16384);
#else
mappings[codeSignatureMappingIndex].sfm_size = (signatureSize+4095) & (-4096);
#endif
mappings[codeSignatureMappingIndex].sfm_file_offset = header->codeSignatureOffset;
mappings[codeSignatureMappingIndex].sfm_max_prot = VM_PROT_READ;
mappings[codeSignatureMappingIndex].sfm_init_prot = VM_PROT_READ;
}
}
#if __MAC_OS_X_VERSION_MIN_REQUIRED
if ( header->imagesCount * sizeof(dyld_cache_image_info) + header->imagesOffset < 8192 ) {
bool foundLibSystem = false;
if ( my_stat("/usr/lib/libSystem.B.dylib", &stat_buf) == 0 ) {
const dyld_cache_image_info* images = (dyld_cache_image_info*)&firstPages[header->imagesOffset];
const dyld_cache_image_info* const imagesEnd = &images[header->imagesCount];
for (const dyld_cache_image_info* p = images; p < imagesEnd; ++p) {
if ( ((time_t)p->modTime == stat_buf.st_mtime) && ((ino_t)p->inode == stat_buf.st_ino) ) {
foundLibSystem = true;
break;
}
}
}
if ( !sSharedCacheIgnoreInodeAndTimeStamp && !foundLibSystem ) {
dyld::log("dyld: shared cached file was built against a different libSystem.dylib, ignoring cache.\n"
"to update dyld shared cache run: 'sudo update_dyld_shared_cache' then reboot.\n");
goodCache = false;
}
}
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED
{
uint64_t lowAddress;
uint64_t highAddress;
getCacheBounds(mappingCount, mappings, lowAddress, highAddress);
if ( (highAddress-lowAddress) > SHARED_REGION_SIZE )
throw "dyld shared cache is too big to fit in shared region";
}
#endif
if ( goodCache && (readWriteMappingIndex == -1) ) {
dyld::log("dyld: shared cached file is missing read/write mapping: %s" DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME "\n", sSharedCacheDir);
goodCache = false;
}
if ( goodCache && (readOnlyMappingIndex == -1) ) {
dyld::log("dyld: shared cached file is missing read-only mapping: %s" DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME "\n", sSharedCacheDir);
goodCache = false;
}
if ( goodCache ) {
long cacheSlide = 0;
void* slideInfo = NULL;
uint64_t slideInfoSize = 0;
if ( header->slideInfoSize != 0 ) {
if ( sMainExecutable->isPositionIndependentExecutable() && (sMainExecutable->getSlide() == 0) )
cacheSlide = 0;
else {
cacheSlide = pickCacheSlide(mappingCount, mappings);
slideInfo = (void*)(long)(mappings[readOnlyMappingIndex].sfm_address + (header->slideInfoOffset - mappings[readOnlyMappingIndex].sfm_file_offset));
slideInfoSize = header->slideInfoSize;
mappings[readWriteMappingIndex].sfm_max_prot |= VM_PROT_SLIDE;
mappings[readWriteMappingIndex].sfm_init_prot |= VM_PROT_SLIDE;
}
}
if ( gLinkContext.verboseMapping ) {
dyld::log("dyld: calling _shared_region_map_and_slide_np() with regions:\n");
for (int i=0; i < mappingCount; ++i) {
dyld::log(" address=0x%08llX, size=0x%08llX, fileOffset=0x%08llX\n", mappings[i].sfm_address, mappings[i].sfm_size, mappings[i].sfm_file_offset);
}
}
if (_shared_region_map_and_slide_np(fd, mappingCount, mappings, codeSignatureMappingIndex, cacheSlide, slideInfo, slideInfoSize) == 0) {
sSharedCache = (dyld_cache_header*)mappings[0].sfm_address;
sSharedCacheSlide = cacheSlide;
dyld::gProcessInfo->sharedCacheSlide = cacheSlide;
if ( header->mappingOffset >= 0x68 ) {
memcpy(dyld::gProcessInfo->sharedCacheUUID, header->uuid, 16);
}
}
else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED
throw "dyld shared cache could not be mapped";
#endif
if ( gLinkContext.verboseMapping )
dyld::log("dyld: shared cached file could not be mapped\n");
}
}
}
else {
if ( gLinkContext.verboseMapping )
dyld::log("dyld: shared cached file is invalid\n");
}
}
else {
if ( gLinkContext.verboseMapping )
dyld::log("dyld: shared cached file cannot be read\n");
}
close(fd);
}
else {
if ( gLinkContext.verboseMapping )
dyld::log("dyld: shared cached file cannot be opened\n");
}
}
if ( sSharedCache != NULL ) {
gLinkContext.dyldLoadedAtSameAddressNeededBySharedCache = ((uintptr_t)(sSharedCache->dyldBaseAddress) == (uintptr_t)&_mh_dylinker_header);
}
if ( sSharedCache != NULL ) {
const dyld_cache_mapping_info* const start = (dyld_cache_mapping_info*)((uint8_t*)sSharedCache + sSharedCache->mappingOffset);
dyld_shared_cache_ranges.sharedRegionsCount = sSharedCache->mappingCount;
if ( dyld_shared_cache_ranges.sharedRegionsCount > 4 )
dyld_shared_cache_ranges.sharedRegionsCount = 4;
const dyld_cache_mapping_info* const end = &start[dyld_shared_cache_ranges.sharedRegionsCount];
int index = 0;
for (const dyld_cache_mapping_info* p = start; p < end; ++p, ++index ) {
dyld_shared_cache_ranges.ranges[index].start = p->address+sSharedCacheSlide;
dyld_shared_cache_ranges.ranges[index].length = p->size;
if ( gLinkContext.verboseMapping ) {
dyld::log(" 0x%08llX->0x%08llX %s%s%s init=%x, max=%x\n",
p->address+sSharedCacheSlide, p->address+sSharedCacheSlide+p->size-1,
((p->initProt & VM_PROT_READ) ? "read " : ""),
((p->initProt & VM_PROT_WRITE) ? "write " : ""),
((p->initProt & VM_PROT_EXECUTE) ? "execute " : ""), p->initProt, p->maxProt);
}
#if __i386__
if ( (p->initProt == (VM_PROT_READ|VM_PROT_EXECUTE)) && ((p->address & 0xF0000000) == 0xA0000000) ) {
if ( p->size != 0 ) {
vm_prot_t prot = VM_PROT_EXECUTE | PROT_READ | VM_PROT_WRITE;
vm_protect(mach_task_self(), p->address, p->size, false, prot);
if ( gLinkContext.verboseMapping ) {
dyld::log("%18s at 0x%08llX->0x%08llX altered permissions to %c%c%c\n", "", p->address,
p->address+p->size-1,
(prot & PROT_READ) ? 'r' : '.', (prot & PROT_WRITE) ? 'w' : '.', (prot & PROT_EXEC) ? 'x' : '.' );
}
}
}
#endif
}
if ( gLinkContext.verboseMapping ) {
dyld_cache_header* header = (dyld_cache_header*)sSharedCache;
uint64_t signatureSize = header->codeSignatureSize;
if ( signatureSize == 0 ) {
struct stat stat_buf;
if ( my_stat(IPHONE_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME, &stat_buf) == 0 )
signatureSize = stat_buf.st_size - header->codeSignatureOffset;
}
if ( signatureSize != 0 ) {
const dyld_cache_mapping_info* const last = &start[dyld_shared_cache_ranges.sharedRegionsCount-1];
uint64_t codeBlobStart = last->address + last->size;
dyld::log(" 0x%08llX->0x%08llX (code signature)\n", codeBlobStart, codeBlobStart+signatureSize);
}
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED
struct stat enableStatBuf;
sDylibsOverrideCache = ( (my_stat(IPHONE_DYLD_SHARED_CACHE_DIR "enable-dylibs-to-override-cache", &enableStatBuf) == 0)
&& (enableStatBuf.st_size < ENABLE_DYLIBS_TO_OVERRIDE_CACHE_SIZE) );
#endif
}
}
#endif // #if DYLD_SHARED_CACHE_SUPPORT
ImageLoader* cloneImage(ImageLoader* image)
{
FileOpener file(image->getPath());
struct stat stat_buf;
if ( fstat(file.getFileDescriptor(), &stat_buf) == -1)
throw "stat error";
dyld::LoadContext context;
context.useSearchPaths = false;
context.useFallbackPaths = false;
context.useLdLibraryPath = false;
context.implicitRPath = false;
context.matchByInstallName = false;
context.dontLoad = false;
context.mustBeBundle = true;
context.mustBeDylib = false;
context.canBePIE = false;
context.origin = NULL;
context.rpath = NULL;
return loadPhase6(file.getFileDescriptor(), stat_buf, image->getPath(), context);
}
ImageLoader* loadFromMemory(const uint8_t* mem, uint64_t len, const char* moduleName)
{
const fat_header* memStartAsFat = (fat_header*)mem;
uint64_t fileOffset = 0;
uint64_t fileLength = len;
if ( memStartAsFat->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) {
if ( fatFindBest(memStartAsFat, &fileOffset, &fileLength) ) {
mem = &mem[fileOffset];
len = fileLength;
}
else {
throw "no matching architecture in universal wrapper";
}
}
if ( isCompatibleMachO(mem, moduleName) ) {
ImageLoader* image = ImageLoaderMachO::instantiateFromMemory(moduleName, (macho_header*)mem, len, gLinkContext);
if ( ! image->isBundle() )
addImage(image);
return image;
}
switch (*(uint32_t*)mem) {
case MH_MAGIC:
case MH_CIGAM:
case MH_MAGIC_64:
case MH_CIGAM_64:
throw "mach-o, but wrong architecture";
default:
throwf("unknown file type, first eight bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
mem[0], mem[1], mem[2], mem[3], mem[4], mem[5], mem[6],mem[7]);
}
}
void registerAddCallback(ImageCallback func)
{
sAddImageCallbacks.push_back(func);
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
if ( image->getState() >= dyld_image_state_bound && image->getState() < dyld_image_state_terminated )
(*func)(image->machHeader(), image->getSlide());
}
}
void registerRemoveCallback(ImageCallback func)
{
if ( sRemoveImageCallbacksInUse )
return;
sRemoveImageCallbacks.push_back(func);
}
void clearErrorMessage()
{
error_string[0] = '\0';
}
void setErrorMessage(const char* message)
{
strlcpy(error_string, message, sizeof(error_string));
}
const char* getErrorMessage()
{
return error_string;
}
void halt(const char* message)
{
dyld::log("dyld: %s\n", message);
setErrorMessage(message);
uintptr_t terminationFlags = 0;
if ( !gLinkContext.startedInitializingMainExecutable )
terminationFlags = 1;
setAlImageInfosHalt(error_string, terminationFlags);
dyld_fatal_error(error_string);
}
static void setErrorStrings(unsigned errorCode, const char* errorClientOfDylibPath,
const char* errorTargetDylibPath, const char* errorSymbol)
{
dyld::gProcessInfo->errorKind = errorCode;
dyld::gProcessInfo->errorClientOfDylibPath = errorClientOfDylibPath;
dyld::gProcessInfo->errorTargetDylibPath = errorTargetDylibPath;
dyld::gProcessInfo->errorSymbol = errorSymbol;
}
uintptr_t bindLazySymbol(const mach_header* mh, uintptr_t* lazyPointer)
{
uintptr_t result = 0;
#if 0 // rdar://problem/3811777 turn off locking until deadlock is resolved
if ( gLibSystemHelpers != NULL )
(*gLibSystemHelpers->lockForReading)();
#endif
try {
ImageLoader* target;
#if __i386__
if ( mh == NULL )
target = dyld::findImageContainingAddress(lazyPointer);
else
target = dyld::findImageByMachHeader(mh);
#else
target = dyld::findImageByMachHeader(mh);
#endif
if ( target == NULL )
throwf("image not found for lazy pointer at %p", lazyPointer);
result = target->doBindLazySymbol(lazyPointer, gLinkContext);
}
catch (const char* message) {
dyld::log("dyld: lazy symbol binding failed: %s\n", message);
halt(message);
}
#if 0
if ( gLibSystemHelpers != NULL )
(*gLibSystemHelpers->unlockForReading)();
#endif
return result;
}
uintptr_t fastBindLazySymbol(ImageLoader** imageLoaderCache, uintptr_t lazyBindingInfoOffset)
{
uintptr_t result = 0;
if ( *imageLoaderCache == NULL ) {
*imageLoaderCache = dyld::findMappedRange((uintptr_t)imageLoaderCache);
if ( *imageLoaderCache == NULL ) {
const char* message = "fast lazy binding from unknown image";
dyld::log("dyld: %s\n", message);
halt(message);
}
}
try {
result = (*imageLoaderCache)->doBindFastLazySymbol((uint32_t)lazyBindingInfoOffset, gLinkContext,
(dyld::gLibSystemHelpers != NULL) ? dyld::gLibSystemHelpers->acquireGlobalDyldLock : NULL,
(dyld::gLibSystemHelpers != NULL) ? dyld::gLibSystemHelpers->releaseGlobalDyldLock : NULL);
}
catch (const char* message) {
dyld::log("dyld: lazy symbol binding failed: %s\n", message);
halt(message);
}
return result;
}
void registerUndefinedHandler(UndefinedHandler handler)
{
sUndefinedHandler = handler;
}
static void undefinedHandler(const char* symboName)
{
if ( sUndefinedHandler != NULL ) {
(*sUndefinedHandler)(symboName);
}
}
static bool findExportedSymbol(const char* name, bool onlyInCoalesced, const ImageLoader::Symbol** sym, const ImageLoader** image)
{
const ImageLoader* firstWeakImage = NULL;
const ImageLoader::Symbol* firstWeakSym = NULL;
const size_t imageCount = sAllImages.size();
for(size_t i=0; i < imageCount; ++i) {
ImageLoader* anImage = sAllImages[i];
if ( sInsertedDylibCount > 0 ) {
if ( i < sInsertedDylibCount )
anImage = sAllImages[i+1];
else if ( i == sInsertedDylibCount )
anImage = sAllImages[0];
}
if ( ! anImage->hasHiddenExports() && (!onlyInCoalesced || anImage->hasCoalescedExports()) ) {
*sym = anImage->findExportedSymbol(name, false, image);
if ( *sym != NULL ) {
if ( ((*image)->getExportedSymbolInfo(*sym) & ImageLoader::kWeakDefinition) != 0 ) {
if ( firstWeakImage == NULL ) {
firstWeakImage = *image;
firstWeakSym = *sym;
}
}
else {
return true;
}
}
}
}
if ( firstWeakSym != NULL ) {
*sym = firstWeakSym;
*image = firstWeakImage;
return true;
}
return false;
}
bool flatFindExportedSymbol(const char* name, const ImageLoader::Symbol** sym, const ImageLoader** image)
{
return findExportedSymbol(name, false, sym, image);
}
bool findCoalescedExportedSymbol(const char* name, const ImageLoader::Symbol** sym, const ImageLoader** image)
{
return findExportedSymbol(name, true, sym, image);
}
bool flatFindExportedSymbolWithHint(const char* name, const char* librarySubstring, const ImageLoader::Symbol** sym, const ImageLoader** image)
{
const size_t imageCount = sAllImages.size();
for(size_t i=0; i < imageCount; ++i){
ImageLoader* anImage = sAllImages[i];
if ( ! anImage->isBundle() && ((librarySubstring==NULL) || (strstr(anImage->getPath(), librarySubstring) != NULL)) ) {
*sym = anImage->findExportedSymbol(name, false, image);
if ( *sym != NULL ) {
return true;
}
}
}
return false;
}
unsigned int getCoalescedImages(ImageLoader* images[])
{
unsigned int count = 0;
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
if ( image->participatesInCoalescing() ) {
*images++ = *it;
++count;
}
}
return count;
}
static ImageLoader::MappedRegion* getMappedRegions(ImageLoader::MappedRegion* regions)
{
ImageLoader::MappedRegion* end = regions;
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
(*it)->getMappedRegions(end);
}
return end;
}
void registerImageStateSingleChangeHandler(dyld_image_states state, dyld_image_state_change_handler handler)
{
ImageLoader* handlerImage = findImageContainingAddress((void*)handler);
if ( handlerImage != NULL )
handlerImage->setNeverUnload();
std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(state, sSingleHandlers);
if ( handlers != NULL ) {
if ( state == dyld_image_state_mapped )
handlers->insert(handlers->begin(), handler);
else
handlers->push_back(handler);
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
dyld_image_info info;
info.imageLoadAddress = image->machHeader();
info.imageFilePath = image->getRealPath();
info.imageFileModDate = image->lastModified();
if ( image->getState() == state )
(*handler)(state, 1, &info);
}
}
}
void registerImageStateBatchChangeHandler(dyld_image_states state, dyld_image_state_change_handler handler)
{
ImageLoader* handlerImage = findImageContainingAddress((void*)handler);
if ( handlerImage != NULL )
handlerImage->setNeverUnload();
std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(state, sBatchHandlers);
if ( handlers != NULL ) {
handlers->insert(handlers->begin(), handler);
try {
notifyBatchPartial(state, true, handler);
}
catch (const char* msg) {
}
}
}
static ImageLoader* libraryLocator(const char* libraryName, bool search, const char* origin, const ImageLoader::RPathChain* rpaths)
{
dyld::LoadContext context;
context.useSearchPaths = search;
context.useFallbackPaths = search;
context.useLdLibraryPath = false;
context.implicitRPath = false;
context.matchByInstallName = false;
context.dontLoad = false;
context.mustBeBundle = false;
context.mustBeDylib = true;
context.canBePIE = false;
context.origin = origin;
context.rpath = rpaths;
return load(libraryName, context);
}
static const char* basename(const char* path)
{
const char* last = path;
for (const char* s = path; *s != '\0'; s++) {
if (*s == '/')
last = s+1;
}
return last;
}
static void setContext(const macho_header* mainExecutableMH, int argc, const char* argv[], const char* envp[], const char* apple[])
{
gLinkContext.loadLibrary = &libraryLocator;
gLinkContext.terminationRecorder = &terminationRecorder;
gLinkContext.flatExportFinder = &flatFindExportedSymbol;
gLinkContext.coalescedExportFinder = &findCoalescedExportedSymbol;
gLinkContext.getCoalescedImages = &getCoalescedImages;
gLinkContext.undefinedHandler = &undefinedHandler;
gLinkContext.getAllMappedRegions = &getMappedRegions;
gLinkContext.bindingHandler = NULL;
gLinkContext.notifySingle = ¬ifySingle;
gLinkContext.notifyBatch = ¬ifyBatch;
gLinkContext.removeImage = &removeImage;
gLinkContext.registerDOFs = ®isterDOFs;
gLinkContext.clearAllDepths = &clearAllDepths;
gLinkContext.printAllDepths = &printAllDepths;
gLinkContext.imageCount = &imageCount;
gLinkContext.setNewProgramVars = &setNewProgramVars;
#if DYLD_SHARED_CACHE_SUPPORT
gLinkContext.inSharedCache = &inSharedCache;
#endif
gLinkContext.setErrorStrings = &setErrorStrings;
#if SUPPORT_OLD_CRT_INITIALIZATION
gLinkContext.setRunInitialzersOldWay= &setRunInitialzersOldWay;
#endif
gLinkContext.findImageContainingAddress = &findImageContainingAddress;
gLinkContext.addDynamicReference = &addDynamicReference;
gLinkContext.bindingOptions = ImageLoader::kBindingNone;
gLinkContext.argc = argc;
gLinkContext.argv = argv;
gLinkContext.envp = envp;
gLinkContext.apple = apple;
gLinkContext.progname = (argv[0] != NULL) ? basename(argv[0]) : "";
gLinkContext.programVars.mh = mainExecutableMH;
gLinkContext.programVars.NXArgcPtr = &gLinkContext.argc;
gLinkContext.programVars.NXArgvPtr = &gLinkContext.argv;
gLinkContext.programVars.environPtr = &gLinkContext.envp;
gLinkContext.programVars.__prognamePtr=&gLinkContext.progname;
gLinkContext.mainExecutable = NULL;
gLinkContext.imageSuffix = NULL;
gLinkContext.dynamicInterposeArray = NULL;
gLinkContext.dynamicInterposeCount = 0;
gLinkContext.prebindUsage = ImageLoader::kUseAllPrebinding;
#if TARGET_IPHONE_SIMULATOR
gLinkContext.sharedRegionMode = ImageLoader::kDontUseSharedRegion;
#else
gLinkContext.sharedRegionMode = ImageLoader::kUseSharedRegion;
#endif
}
#if __LP64__
#define LC_SEGMENT_COMMAND LC_SEGMENT_64
#define macho_segment_command segment_command_64
#define macho_section section_64
#else
#define LC_SEGMENT_COMMAND LC_SEGMENT
#define macho_segment_command segment_command
#define macho_section section
#endif
static bool hasRestrictedSegment(const macho_header* mh)
{
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
case LC_SEGMENT_COMMAND:
{
const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
if (strcmp(seg->segname, "__RESTRICT") == 0) {
const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
const struct macho_section* const sectionsEnd = §ionsStart[seg->nsects];
for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
if (strcmp(sect->sectname, "__restrict") == 0)
return true;
}
}
}
break;
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
return false;
}
#if SUPPORT_VERSIONED_PATHS
static bool getDylibVersionAndInstallname(const char* dylibPath, uint32_t* version, char* installName)
{
FileOpener file(dylibPath);
if ( file.getFileDescriptor() == -1 )
return false;
uint8_t firstPage[4096];
if ( pread(file.getFileDescriptor(), firstPage, 4096, 0) != 4096 )
return false;
const fat_header* fileStartAsFat = (fat_header*)firstPage;
if ( fileStartAsFat->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) {
uint64_t fileOffset;
uint64_t fileLength;
if ( fatFindBest(fileStartAsFat, &fileOffset, &fileLength) ) {
if ( pread(file.getFileDescriptor(), firstPage, 4096, fileOffset) != 4096 )
return false;
}
else {
return false;
}
}
const mach_header* mh = (mach_header*)firstPage;
if ( mh->magic != sMainExecutableMachHeader->magic )
return false;
if ( mh->cputype != sMainExecutableMachHeader->cputype )
return false;
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
const struct load_command* const cmdsReadEnd = (struct load_command*)(((char*)mh)+4096);
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
case LC_ID_DYLIB:
{
const struct dylib_command* id = (struct dylib_command*)cmd;
*version = id->dylib.current_version;
if ( installName != NULL )
strlcpy(installName, (char *)id + id->dylib.name.offset, PATH_MAX);
return true;
}
break;
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
if ( cmd > cmdsReadEnd )
return false;
}
return false;
}
#endif // SUPPORT_VERSIONED_PATHS
#if 0
static void printAllImages()
{
dyld::log("printAllImages()\n");
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
dyld_image_states imageState = image->getState();
dyld::log(" state=%d, dlopen-count=%d, never-unload=%d, in-use=%d, name=%s\n",
imageState, image->dlopenCount(), image->neverUnload(), image->isMarkedInUse(), image->getShortName());
}
}
#endif
void link(ImageLoader* image, bool forceLazysBound, bool neverUnload, const ImageLoader::RPathChain& loaderRPaths)
{
if ( image->isBundle() && !image->isLinked() )
addImage(image);
if ( !image->isLinked() )
addRootImage(image);
try {
image->link(gLinkContext, forceLazysBound, false, neverUnload, loaderRPaths);
}
catch (const char* msg) {
garbageCollectImages();
throw;
}
}
void runInitializers(ImageLoader* image)
{
ImageLoader::InitializerTimingList initializerTimes[sAllImages.size()];
initializerTimes[0].count = 0;
image->runInitializers(gLinkContext, initializerTimes[0]);
}
void garbageCollectImages()
{
static bool sDoingGC = false;
static bool sRedo = false;
if ( sDoingGC ) {
sRedo = true;
return;
}
sDoingGC = true;
do {
sRedo = false;
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
image->markNotUsed();
}
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
if ( (image->dlopenCount() != 0) || image->neverUnload() ) {
image->markedUsedRecursive(sDynamicReferences);
}
}
ImageLoader* deadImages[sAllImages.size()];
unsigned deadCount = 0;
unsigned i = 0;
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
if ( ! image->isMarkedInUse() ) {
deadImages[i++] = image;
if (gLogAPIs) dyld::log("dlclose(), found unused image %p %s\n", image, image->getShortName());
++deadCount;
}
}
const int maxRangeCount = deadCount*2;
__cxa_range_t ranges[maxRangeCount];
int rangeCount = 0;
for (unsigned i=0; i < deadCount; ++i) {
ImageLoader* image = deadImages[i];
for (unsigned int j=0; j < image->segmentCount(); ++j) {
if ( !image->segExecutable(j) )
continue;
if ( rangeCount < maxRangeCount ) {
ranges[rangeCount].addr = (const void*)image->segActualLoadAddress(j);
ranges[rangeCount].length = image->segSize(j);
++rangeCount;
}
}
try {
runImageStaticTerminators(image);
}
catch (const char* msg) {
dyld::warn("problem running terminators for image: %s\n", msg);
}
}
if ( (rangeCount > 0) && (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 13) )
(*gLibSystemHelpers->cxa_finalize_ranges)(ranges, rangeCount);
bool mightBeMore;
do {
mightBeMore = false;
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
if ( ! image->isMarkedInUse() ) {
try {
if (gLogAPIs) dyld::log("dlclose(), deleting %p %s\n", image, image->getShortName());
removeImage(image);
ImageLoader::deleteImage(image);
mightBeMore = true;
break; }
catch (const char* msg) {
dyld::warn("problem deleting image: %s\n", msg);
}
}
}
} while ( mightBeMore );
} while (sRedo);
sDoingGC = false;
}
static void preflight_finally(ImageLoader* image)
{
if ( image->isBundle() ) {
removeImageFromAllImages(image->machHeader());
ImageLoader::deleteImage(image);
}
sBundleBeingLoaded = NULL;
dyld::garbageCollectImages();
}
void preflight(ImageLoader* image, const ImageLoader::RPathChain& loaderRPaths)
{
try {
if ( image->isBundle() )
sBundleBeingLoaded = image; image->link(gLinkContext, false, true, false, loaderRPaths);
}
catch (const char* msg) {
preflight_finally(image);
throw;
}
preflight_finally(image);
}
#if __x86_64__
static bool isHaswell()
{
#if TARGET_IPHONE_SIMULATOR
return false;
#else
struct host_basic_info info;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
mach_port_t hostPort = mach_host_self();
kern_return_t result = host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&info, &count);
mach_port_deallocate(mach_task_self(), hostPort);
if ( result != KERN_SUCCESS )
return false;
return ( info.cpu_subtype == CPU_SUBTYPE_X86_64_H );
#endif
}
#endif
static void loadInsertedDylib(const char* path)
{
ImageLoader* image = NULL;
try {
LoadContext context;
context.useSearchPaths = false;
context.useFallbackPaths = false;
context.useLdLibraryPath = false;
context.implicitRPath = false;
context.matchByInstallName = false;
context.dontLoad = false;
context.mustBeBundle = false;
context.mustBeDylib = true;
context.canBePIE = false;
context.origin = NULL; context.rpath = NULL;
image = load(path, context);
}
catch (const char* msg) {
#if TARGET_IPHONE_SIMULATOR
dyld::log("dyld: warning: could not load inserted library '%s' because %s\n", path, msg);
#else
halt(dyld::mkstringf("could not load inserted library '%s' because %s\n", path, msg));
#endif
}
catch (...) {
halt(dyld::mkstringf("could not load inserted library '%s'\n", path));
}
}
static bool processRestricted(const macho_header* mainExecutableMH)
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED
uint32_t flags;
if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) != -1 ) {
if ( flags & CS_ENFORCEMENT ) {
gLinkContext.codeSigningEnforced = true;
}
}
if (flags & CS_RESTRICT) {
sRestrictedReason = restrictedByEntitlements;
return true;
}
#else
gLinkContext.codeSigningEnforced = true;
#endif
if ( issetugid() ) {
sRestrictedReason = restrictedBySetGUid;
return true;
}
if ( hasRestrictedSegment(mainExecutableMH) ) {
sRestrictedReason = restrictedBySegment;
return true;
}
return false;
}
bool processIsRestricted()
{
return sProcessIsRestricted;
}
static void addDyldImageToUUIDList()
{
const struct macho_header* mh = (macho_header*)&__dso_handle;
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)((char*)mh + sizeof(macho_header));
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
case LC_UUID: {
uuid_command* uc = (uuid_command*)cmd;
dyld_uuid_info info;
info.imageLoadAddress = (mach_header*)mh;
memcpy(info.imageUUID, uc->uuid, 16);
addNonSharedCacheImageUUID(info);
return;
}
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
}
#if __MAC_OS_X_VERSION_MIN_REQUIRED
typedef int (*open_proc_t)(const char*, int, int);
typedef int (*fcntl_proc_t)(int, int, void*);
typedef int (*ioctl_proc_t)(int, unsigned long, void*);
static void* getProcessInfo() { return dyld::gProcessInfo; }
static SyscallHelpers sSysCalls = {
3,
(open_proc_t)&open,
&close,
&pread,
&write,
&mmap,
&munmap,
&madvise,
&stat,
(fcntl_proc_t)&fcntl,
(ioctl_proc_t)&ioctl,
&issetugid,
&getcwd,
&realpath,
&vm_allocate,
&vm_deallocate,
&vm_protect,
&vlog,
&vwarn,
&pthread_mutex_lock,
&pthread_mutex_unlock,
&mach_thread_self,
&mach_port_deallocate,
&task_self_trap,
&mach_timebase_info,
&OSAtomicCompareAndSwapPtrBarrier,
&OSMemoryBarrier,
&getProcessInfo,
&__error,
&mach_absolute_time,
&thread_switch,
&opendir,
&readdir_r,
&closedir
};
__attribute__((noinline))
static uintptr_t useSimulatorDyld(int fd, const macho_header* mainExecutableMH, const char* dyldPath,
int argc, const char* argv[], const char* envp[], const char* apple[], uintptr_t* startGlue)
{
*startGlue = 0;
struct stat sb;
if ( fstat(fd, &sb) == -1 )
return 0;
if ( sb.st_uid != 0 )
return 0;
uint8_t firstPage[4096];
if ( pread(fd, firstPage, 4096, 0) != 4096 )
return 0;
uint64_t fileOffset = 0;
uint64_t fileLength = sb.st_size;
const fat_header* fileStartAsFat = (fat_header*)firstPage;
if ( fileStartAsFat->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) {
if ( !fatFindBest(fileStartAsFat, &fileOffset, &fileLength) )
return 0;
if ( pread(fd, firstPage, 4096, fileOffset) != 4096 )
return 0;
}
else if ( !isCompatibleMachO(firstPage, dyldPath) ) {
return 0;
}
const macho_header* mh = (const macho_header*)firstPage;
uintptr_t mappingSize = 0;
uintptr_t preferredLoadAddress = 0;
const uint32_t cmd_count = mh->ncmds;
const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
case LC_SEGMENT_COMMAND:
{
struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
mappingSize += seg->vmsize;
if ( seg->fileoff == 0 )
preferredLoadAddress = seg->vmaddr;
}
break;
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
vm_address_t loadAddress = 0;
uintptr_t entry = 0;
if ( ::vm_allocate(mach_task_self(), &loadAddress, mappingSize, VM_FLAGS_ANYWHERE) != 0 )
return 0;
cmd = cmds;
struct linkedit_data_command* codeSigCmd = NULL;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
case LC_SEGMENT_COMMAND:
{
struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
uintptr_t requestedLoadAddress = seg->vmaddr - preferredLoadAddress + loadAddress;
void* segAddress = ::mmap((void*)requestedLoadAddress, seg->filesize, seg->initprot, MAP_FIXED | MAP_PRIVATE, fd, fileOffset + seg->fileoff);
if ( segAddress == (void*)(-1) )
return 0;
}
break;
case LC_UNIXTHREAD:
{
#if __i386__
const i386_thread_state_t* registers = (i386_thread_state_t*)(((char*)cmd) + 16);
entry = (registers->__eip + loadAddress - preferredLoadAddress);
#elif __x86_64__
const x86_thread_state64_t* registers = (x86_thread_state64_t*)(((char*)cmd) + 16);
entry = (registers->__rip + loadAddress - preferredLoadAddress);
#endif
}
break;
case LC_CODE_SIGNATURE:
codeSigCmd = (struct linkedit_data_command*)cmd;
break;
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
if ( codeSigCmd != NULL ) {
fsignatures_t siginfo;
siginfo.fs_file_start=fileOffset; siginfo.fs_blob_start=(void*)(long)(codeSigCmd->dataoff); siginfo.fs_blob_size=codeSigCmd->datasize; int result = fcntl(fd, F_ADDFILESIGS, &siginfo);
if ( result == -1 ) {
if ( (errno == EPERM) || (errno == EBADEXEC) )
return 0;
}
}
close(fd);
dyld_image_info info;
info.imageLoadAddress = (mach_header*)loadAddress;
info.imageFilePath = strdup(dyldPath);
info.imageFileModDate = sb.st_mtime;
addImagesToAllImages(1, &info);
dyld::gProcessInfo->notification(dyld_image_adding, 1, &info);
typedef uintptr_t (*sim_entry_proc_t)(int argc, const char* argv[], const char* envp[], const char* apple[],
const macho_header* mainExecutableMH, const macho_header* dyldMH, uintptr_t dyldSlide,
const dyld::SyscallHelpers* vtable, uintptr_t* startGlue);
sim_entry_proc_t newDyld = (sim_entry_proc_t)entry;
return (*newDyld)(argc, argv, envp, apple, mainExecutableMH, (macho_header*)loadAddress,
loadAddress - preferredLoadAddress,
&sSysCalls, startGlue);
}
#endif
uintptr_t
_main(const macho_header* mainExecutableMH, uintptr_t mainExecutableSlide,
int argc, const char* argv[], const char* envp[], const char* apple[],
uintptr_t* startGlue)
{
uintptr_t result = 0;
sMainExecutableMachHeader = mainExecutableMH;
#if !TARGET_IPHONE_SIMULATOR
const char* loggingPath = _simple_getenv(envp, "DYLD_PRINT_TO_FILE");
if ( loggingPath != NULL ) {
int fd = open(loggingPath, O_WRONLY | O_CREAT | O_APPEND, 0644);
if ( fd != -1 ) {
sLogfile = fd;
sLogToFile = true;
}
else {
dyld::log("dyld: could not open DYLD_PRINT_TO_FILE='%s', errno=%d\n", loggingPath, errno);
}
}
#endif
#if __MAC_OS_X_VERSION_MIN_REQUIRED
const char* rootPath = _simple_getenv(envp, "DYLD_ROOT_PATH");
if ( rootPath != NULL ) {
char simDyldPath[PATH_MAX];
strlcpy(simDyldPath, rootPath, PATH_MAX);
strlcat(simDyldPath, "/usr/lib/dyld_sim", PATH_MAX);
int fd = my_open(simDyldPath, O_RDONLY, 0);
if ( fd != -1 ) {
result = useSimulatorDyld(fd, mainExecutableMH, simDyldPath, argc, argv, envp, apple, startGlue);
if ( !result && (*startGlue == 0) )
halt("problem loading iOS simulator dyld");
return result;
}
}
#endif
CRSetCrashLogMessage("dyld: launch started");
#if LOG_BINDINGS
char bindingsLogPath[256];
const char* shortProgName = "unknown";
if ( argc > 0 ) {
shortProgName = strrchr(argv[0], '/');
if ( shortProgName == NULL )
shortProgName = argv[0];
else
++shortProgName;
}
mysprintf(bindingsLogPath, "/tmp/bindings/%d-%s", getpid(), shortProgName);
sBindingsLogfile = open(bindingsLogPath, O_WRONLY | O_CREAT, 0666);
if ( sBindingsLogfile == -1 ) {
::mkdir("/tmp/bindings", 0777);
sBindingsLogfile = open(bindingsLogPath, O_WRONLY | O_CREAT, 0666);
}
#endif
setContext(mainExecutableMH, argc, argv, envp, apple);
sExecPath = _simple_getenv(apple, "executable_path");
if (!sExecPath) sExecPath = apple[0];
sExecPath = apple[0];
bool ignoreEnvironmentVariables = false;
if ( sExecPath[0] != '/' ) {
char cwdbuff[MAXPATHLEN];
if ( getcwd(cwdbuff, MAXPATHLEN) != NULL ) {
char* s = new char[strlen(cwdbuff) + strlen(sExecPath) + 2];
strcpy(s, cwdbuff);
strcat(s, "/");
strcat(s, sExecPath);
sExecPath = s;
}
}
sExecShortName = ::strrchr(sExecPath, '/');
if ( sExecShortName != NULL )
++sExecShortName;
else
sExecShortName = sExecPath;
sProcessIsRestricted = processRestricted(mainExecutableMH);
if ( sProcessIsRestricted ) {
#if SUPPORT_LC_DYLD_ENVIRONMENT
checkLoadCommandEnvironmentVariables();
#if SUPPORT_VERSIONED_PATHS
checkVersionedPaths();
#endif
#endif
pruneEnvironmentVariables(envp, &apple);
setContext(mainExecutableMH, argc, argv, envp, apple);
}
else
checkEnvironmentVariables(envp, ignoreEnvironmentVariables);
if ( sEnv.DYLD_PRINT_OPTS )
printOptions(argv);
if ( sEnv.DYLD_PRINT_ENV )
printEnvironmentVariables(envp);
getHostInfo();
stateToHandlers(dyld_image_state_dependents_mapped, sBatchHandlers)->push_back(notifyGDB);
stateToHandlers(dyld_image_state_mapped, sSingleHandlers)->push_back(updateAllImages);
sAllImages.reserve(INITIAL_IMAGE_COUNT);
sImageRoots.reserve(16);
sAddImageCallbacks.reserve(4);
sRemoveImageCallbacks.reserve(4);
sImageFilesNeedingTermination.reserve(16);
sImageFilesNeedingDOFUnregistration.reserve(8);
#ifdef WAIT_FOR_SYSTEM_ORDER_HANDSHAKE
WAIT_FOR_SYSTEM_ORDER_HANDSHAKE(dyld::gProcessInfo->systemOrderFlag);
#endif
try {
addDyldImageToUUIDList();
CRSetCrashLogMessage(sLoadingCrashMessage);
sMainExecutable = instantiateFromLoadedImage(mainExecutableMH, mainExecutableSlide, sExecPath);
gLinkContext.mainExecutable = sMainExecutable;
gLinkContext.processIsRestricted = sProcessIsRestricted;
gLinkContext.mainExecutableCodeSigned = hasCodeSignatureLoadCommand(mainExecutableMH);
#if TARGET_IPHONE_SIMULATOR
{
if ( ! isSimulatorBinary((uint8_t*)mainExecutableMH, sExecPath) ) {
throwf("program was built for Mac OS X and cannot be run in simulator");
}
uint32_t mainMinOS = sMainExecutable->minOSVersion();
uint32_t dyldMinOS = ImageLoaderMachO::minOSVersion((const mach_header*)&__dso_handle);
if ( mainMinOS > dyldMinOS ) {
throwf("app was built for iOS %d.%d which is newer than this simulator %d.%d",
mainMinOS >> 16, ((mainMinOS >> 8) & 0xFF),
dyldMinOS >> 16, ((dyldMinOS >> 8) & 0xFF));
}
}
#endif
#if __x86_64__
sHaswell = isHaswell();
#endif
checkSharedRegionDisable();
#if DYLD_SHARED_CACHE_SUPPORT
if ( gLinkContext.sharedRegionMode != ImageLoader::kDontUseSharedRegion )
mapSharedCache();
#endif
if ( sEnv.DYLD_INSERT_LIBRARIES != NULL ) {
for (const char* const* lib = sEnv.DYLD_INSERT_LIBRARIES; *lib != NULL; ++lib)
loadInsertedDylib(*lib);
}
sInsertedDylibCount = sAllImages.size()-1;
gLinkContext.linkingMainExecutable = true;
link(sMainExecutable, sEnv.DYLD_BIND_AT_LAUNCH, true, ImageLoader::RPathChain(NULL, NULL));
sMainExecutable->setNeverUnloadRecursive();
if ( sMainExecutable->forceFlat() ) {
gLinkContext.bindFlat = true;
gLinkContext.prebindUsage = ImageLoader::kUseNoPrebinding;
}
if ( sInsertedDylibCount > 0 ) {
for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
ImageLoader* image = sAllImages[i+1];
link(image, sEnv.DYLD_BIND_AT_LAUNCH, true, ImageLoader::RPathChain(NULL, NULL));
image->setNeverUnloadRecursive();
}
for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
ImageLoader* image = sAllImages[i+1];
image->registerInterposing();
}
}
for(int i=0; i < sImageRoots.size(); ++i) {
sImageRoots[i]->applyInterposing(gLinkContext);
}
gLinkContext.linkingMainExecutable = false;
sMainExecutable->weakBind(gLinkContext);
CRSetCrashLogMessage("dyld: launch, running initializers");
#if SUPPORT_OLD_CRT_INITIALIZATION
if ( ! gRunInitializersOldWay )
initializeMainExecutable();
#else
initializeMainExecutable();
#endif
result = (uintptr_t)sMainExecutable->getThreadPC();
if ( result != 0 ) {
if ( (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 9) )
*startGlue = (uintptr_t)gLibSystemHelpers->startGlueToCallExit;
else
halt("libdyld.dylib support not present for LC_MAIN");
}
else {
result = (uintptr_t)sMainExecutable->getMain();
*startGlue = 0;
}
}
catch(const char* message) {
syncAllImages();
halt(message);
}
catch(...) {
dyld::log("dyld: launch failed\n");
}
CRSetCrashLogMessage(NULL);
return result;
}
}