Loading...
--- dyld/dyld-625.13/src/dyld.cpp
+++ dyld/dyld-360.18/src/dyld.cpp
@@ -28,8 +28,6 @@
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
-#include <pthread.h>
-#include <libproc.h>
#include <sys/param.h>
#include <mach/mach_time.h> // mach_absolute_time()
#include <mach/mach_init.h>
@@ -40,7 +38,6 @@
#include <sys/un.h>
#include <sys/syslog.h>
#include <sys/uio.h>
-#include <mach/mach.h>
#include <mach-o/fat.h>
#include <mach-o/loader.h>
#include <mach-o/ldsyms.h>
@@ -56,40 +53,7 @@
#include <System/sys/csr.h>
#include <_simple.h>
#include <os/lock_private.h>
-#include <System/machine/cpu_capabilities.h>
-#include <System/sys/reason.h>
-#include <kern/kcdata.h>
-#if TARGET_IPHONE_SIMULATOR
- enum {
- AMFI_DYLD_INPUT_PROC_IN_SIMULATOR = (1 << 0),
- };
- enum amfi_dyld_policy_output_flag_set {
- AMFI_DYLD_OUTPUT_ALLOW_AT_PATH = (1 << 0),
- AMFI_DYLD_OUTPUT_ALLOW_PATH_VARS = (1 << 1),
- AMFI_DYLD_OUTPUT_ALLOW_CUSTOM_SHARED_CACHE = (1 << 2),
- AMFI_DYLD_OUTPUT_ALLOW_FALLBACK_PATHS = (1 << 3),
- AMFI_DYLD_OUTPUT_ALLOW_PRINT_VARS = (1 << 4),
- AMFI_DYLD_OUTPUT_ALLOW_FAILED_LIBRARY_INSERTION = (1 << 5),
- };
- extern "C" int amfi_check_dyld_policy_self(uint64_t input_flags, uint64_t* output_flags);
-#else
- #include <libamfi.h>
-#endif
-extern "C" {
- #include <corecrypto/ccdigest.h>
- #include <corecrypto/ccsha2.h>
-}
-#include <sandbox.h>
-#include <sandbox/private.h>
-#if __has_feature(ptrauth_calls)
- #include <ptrauth.h>
-#endif
-
-extern "C" int __fork();
-
-#include <array>
-#include <algorithm>
-#include <vector>
+
#ifndef CPU_SUBTYPE_ARM_V5TEJ
#define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
@@ -117,23 +81,12 @@
#define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t) 8)
#endif
-#ifndef CPU_SUBTYPE_ARM64_E
- #define CPU_SUBTYPE_ARM64_E 2
-#endif
-
-#ifndef CPU_ARCH_ABI64_32
- #define CPU_ARCH_ABI64_32 ((cpu_type_t) 0x02000000)
-#endif
-#ifndef CPU_TYPE_ARM64_32
- #define CPU_TYPE_ARM64_32 ((cpu_type_t) (CPU_TYPE_ARM | CPU_ARCH_ABI64_32))
-#endif
-#ifndef CPU_SUBTYPE_ARM64_32_V8
- #define CPU_SUBTYPE_ARM64_32_V8 ((cpu_subtype_t) 1)
-#endif
-
#ifndef VM_PROT_SLIDE
#define VM_PROT_SLIDE 0x20
#endif
+
+#include <vector>
+#include <algorithm>
#include "mach-o/dyld_gdb.h"
@@ -141,49 +94,33 @@
#include "ImageLoader.h"
#include "ImageLoaderMachO.h"
#include "dyldLibSystemInterface.h"
+#include "dyldSyscallInterface.h"
+#if DYLD_SHARED_CACHE_SUPPORT
#include "dyld_cache_format.h"
-#include "dyld_process_info_internal.h"
-
-#if SUPPORT_ACCELERATE_TABLES
- #include "ImageLoaderMegaDylib.h"
-#endif
-
+#endif
+#include <coreSymbolicationDyldSupport.h>
#if TARGET_IPHONE_SIMULATOR
- extern "C" void* gSyscallHelpers;
-#else
- #include "dyldSyscallInterface.h"
-#endif
-
-#include "Closure.h"
-#include "libdyldEntryVector.h"
-#include "MachOLoaded.h"
-#include "Loading.h"
-#include "DyldSharedCache.h"
-#include "SharedCacheRuntime.h"
-#include "StringUtils.h"
-#include "Tracing.h"
-#include "ClosureBuilder.h"
-#include "ClosureFileSystemPhysical.h"
-#include "FileUtils.h"
-
+ extern "C" void xcoresymbolication_load_notifier(void *connection, uint64_t load_timestamp, const char *image_path, const struct mach_header *mach_header);
+ extern "C" void xcoresymbolication_unload_notifier(void *connection, uint64_t unload_timestamp, const char *image_path, const struct mach_header *mach_header);
+ #define coresymbolication_load_notifier(c, t, p, h) xcoresymbolication_load_notifier(c, t, p, h)
+ #define coresymbolication_unload_notifier(c, t, p, h) xcoresymbolication_unload_notifier(c, t, p, h)
+#endif
// not libc header for send() syscall interface
extern "C" ssize_t __sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t);
// ARM and x86_64 are the only architecture that use cpu-sub-types
-#define CPU_SUBTYPES_SUPPORTED ((__arm__ || __arm64__ || __x86_64__) && !TARGET_IPHONE_SIMULATOR)
+#define CPU_SUBTYPES_SUPPORTED ((__arm__ || __x86_64__) && !TARGET_IPHONE_SIMULATOR)
#if __LP64__
#define LC_SEGMENT_COMMAND LC_SEGMENT_64
#define LC_SEGMENT_COMMAND_WRONG LC_SEGMENT
- #define LC_ENCRYPT_COMMAND LC_ENCRYPTION_INFO
#define macho_segment_command segment_command_64
#define macho_section section_64
#else
#define LC_SEGMENT_COMMAND LC_SEGMENT
#define LC_SEGMENT_COMMAND_WRONG LC_SEGMENT_64
- #define LC_ENCRYPT_COMMAND LC_ENCRYPTION_INFO_64
#define macho_segment_command segment_command
#define macho_section section
#endif
@@ -194,17 +131,18 @@
/* implemented in dyld_gdb.cpp */
-extern void resetAllImages();
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 size_t allImagesCount();
// magic so CrashReporter logs message
extern "C" {
char error_string[1024];
}
+// implemented in dyldStartup.s for CrashReporter
+extern "C" void dyld_fatal_error(const char* errString) __attribute__((noreturn));
// magic linker symbol for start of dyld binary
extern "C" const macho_header __dso_handle;
@@ -247,14 +185,16 @@
const char* const * LD_LIBRARY_PATH; // for unix conformance
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_STATISTICS_DETAILS;
bool DYLD_PRINT_OPTS;
bool DYLD_PRINT_ENV;
bool DYLD_DISABLE_DOFS;
- // DYLD_SHARED_CACHE_DIR ==> sSharedCacheOverrideDir
+ bool DYLD_PRINT_CS_NOTIFICATIONS;
+ // DYLD_SHARED_CACHE_DONT_VALIDATE ==> sSharedCacheIgnoreInodeAndTimeStamp
+ // DYLD_SHARED_CACHE_DIR ==> sSharedCacheDir
// DYLD_ROOT_PATH ==> gLinkContext.rootPaths
// DYLD_IMAGE_SUFFIX ==> gLinkContext.imageSuffix
// DYLD_PRINT_OPTS ==> gLinkContext.verboseOpts
@@ -274,7 +214,6 @@
// DYLD_PRINT_WARNINGS ==> gLinkContext.verboseWarnings
// DYLD_PRINT_RPATHS ==> gLinkContext.verboseRPaths
// DYLD_PRINT_INTERPOSING ==> gLinkContext.verboseInterposing
- // DYLD_PRINT_LIBRARIES ==> gLinkContext.verboseLoading
};
@@ -282,18 +221,20 @@
typedef std::vector<dyld_image_state_change_handler> StateHandlers;
-enum EnvVarMode { envNone, envPrintOnly, envAll };
+enum RestrictedReason { restrictedNot, restrictedBySetGUid, restrictedBySegment, restrictedByEntitlements };
// all global state
static const char* sExecPath = NULL;
static const char* sExecShortName = NULL;
static const macho_header* sMainExecutableMachHeader = NULL;
-static uintptr_t sMainExecutableSlide = 0;
#if CPU_SUBTYPES_SUPPORTED
static cpu_type_t sHostCPU;
static cpu_subtype_t sHostCPUsubtype;
#endif
-static ImageLoaderMachO* sMainExecutable = NULL;
+static ImageLoader* sMainExecutable = NULL;
+static bool sProcessIsRestricted = false;
+static bool sProcessRequiresLibraryValidation = false;
+static RestrictedReason sRestrictedReason = restrictedNot;
static size_t sInsertedDylibCount = 0;
static std::vector<ImageLoader*> sAllImages;
static std::vector<ImageLoader*> sImageRoots;
@@ -301,7 +242,6 @@
static std::vector<RegisteredDOF> sImageFilesNeedingDOFUnregistration;
static std::vector<ImageCallback> sAddImageCallbacks;
static std::vector<ImageCallback> sRemoveImageCallbacks;
-static std::vector<LoadImageCallback> sAddLoadImageCallbacks;
static bool sRemoveImageCallbacksInUse = false;
static void* sSingleHandlers[7][3];
static void* sBatchHandlers[7][3];
@@ -310,22 +250,27 @@
#if __MAC_OS_X_VERSION_MIN_REQUIRED
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 const char* sRestrictedFrameworkFallbackPaths[] = { "/System/Library/Frameworks", NULL };
-static const char* sRestrictedLibraryFallbackPaths[] = { "/usr/lib", NULL };
#else
static const char* sFrameworkFallbackPaths[] = { "/System/Library/Frameworks", NULL };
static const char* sLibraryFallbackPaths[] = { "/usr/local/lib", "/usr/lib", NULL };
#endif
static UndefinedHandler sUndefinedHandler = NULL;
static ImageLoader* sBundleBeingLoaded = NULL; // hack until OFI is reworked
-static dyld3::SharedCacheLoadInfo sSharedCacheLoadInfo;
-static const char* sSharedCacheOverrideDir;
- bool gSharedCacheOverridden = false;
+#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;
-#if SUPPORT_ACCELERATE_TABLES
-bool gLogAppAPIs = false;
-#endif
const struct LibSystemHelpers* gLibSystemHelpers = NULL;
#if SUPPORT_OLD_CRT_INITIALIZATION
bool gRunInitializersOldWay = false;
@@ -335,33 +280,13 @@
static int sLogSocket = -1;
#endif
static bool sFrameworksFoundAsDylibs = false;
-#if __x86_64__ && !TARGET_IPHONE_SIMULATOR
+#if __x86_64__
static bool sHaswell = false;
#endif
static std::vector<ImageLoader::DynamicReference> sDynamicReferences;
static OSSpinLock sDynamicReferencesLock = 0;
-#if !TARGET_IPHONE_SIMULATOR
static bool sLogToFile = false;
-#endif
static char sLoadingCrashMessage[1024] = "dyld: launch, loading dependent libraries";
-static _dyld_objc_notify_mapped sNotifyObjCMapped;
-static _dyld_objc_notify_init sNotifyObjCInit;
-static _dyld_objc_notify_unmapped sNotifyObjCUnmapped;
-
-#if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
-static bool sForceStderr = false;
-#endif
-
-
-#if SUPPORT_ACCELERATE_TABLES
-static ImageLoaderMegaDylib* sAllCacheImagesProxy = NULL;
-static bool sDisableAcceleratorTables = false;
-#endif
-
-bool gUseDyld3 = false;
-static bool sSkipMain = false;
-static bool sEnableClosures = false;
-static uint64_t launchTraceID = 0;
//
// The MappedRanges structure is used for fast address->image lookups.
@@ -374,22 +299,22 @@
//
struct MappedRanges
{
- MappedRanges* next;
- unsigned long count;
+ enum { count=400 };
struct {
ImageLoader* image;
uintptr_t start;
uintptr_t end;
- } array[1];
+ } array[count];
+ MappedRanges* next;
};
-static MappedRanges* sMappedRangesStart;
+static MappedRanges sMappedRangesStart;
void addMappedRange(ImageLoader* image, uintptr_t start, uintptr_t end)
{
//dyld::log("addMappedRange(0x%lX->0x%lX) for %s\n", start, end, image->getShortName());
- for (MappedRanges* p = sMappedRangesStart; p != NULL; p = p->next) {
- for (unsigned long i=0; i < p->count; ++i) {
+ 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;
@@ -401,37 +326,24 @@
}
}
// table must be full, chain another
-#if SUPPORT_ACCELERATE_TABLES
- unsigned count = (sAllCacheImagesProxy != NULL) ? 16 : 400;
-#else
- unsigned count = 400;
-#endif
- size_t allocationSize = sizeof(MappedRanges) + (count-1)*3*sizeof(void*);
- MappedRanges* newRanges = (MappedRanges*)malloc(allocationSize);
- bzero(newRanges, allocationSize);
- newRanges->count = count;
+ 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;
- OSMemoryBarrier();
- if ( sMappedRangesStart == NULL ) {
- sMappedRangesStart = newRanges;
- }
- else {
- for (MappedRanges* p = sMappedRangesStart; p != NULL; p = p->next) {
- if ( p->next == NULL ) {
- OSMemoryBarrier();
- p->next = newRanges;
- break;
- }
+ 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 (unsigned long i=0; i < p->count; ++i) {
+ for (MappedRanges* p = &sMappedRangesStart; p != NULL; p = p->next) {
+ for (int i=0; i < MappedRanges::count; ++i) {
if ( p->array[i].image == image ) {
// clear with a barrier so that any reader will see consistent records
OSMemoryBarrier();
@@ -443,8 +355,8 @@
ImageLoader* findMappedRange(uintptr_t target)
{
- for (MappedRanges* p = sMappedRangesStart; p != NULL; p = p->next) {
- for (unsigned long i=0; i < p->count; ++i) {
+ 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;
@@ -494,22 +406,44 @@
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
// based on CFUtilities.c: also_do_stderr()
static bool useSyslog()
{
// Use syslog() for processes managed by launchd
- static bool launchdChecked = false;
- static bool launchdOwned = false;
- if ( !launchdChecked && gProcessInfo->libSystemInitialized ) {
- if ( (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 11) ) {
- // <rdar://problem/23520449> only call isLaunchdOwned() after libSystem is initialized
- launchdOwned = (*gLibSystemHelpers->isLaunchdOwned)();
- launchdChecked = true;
- }
- }
- if ( launchdChecked && launchdOwned )
- return true;
+ if ( (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 11) ) {
+ if ( (*gLibSystemHelpers->isLaunchdOwned)() ) {
+ return true;
+ }
+ }
// If stderr is not available, use syslog()
struct stat sb;
@@ -553,16 +487,9 @@
_simple_sfree(buf);
}
-
-
void vlog(const char* format, va_list list)
{
-#if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
- // <rdar://problem/25965832> log to console when running iOS app from Xcode
- if ( !sLogToFile && !sForceStderr && useSyslog() )
-#else
- if ( !sLogToFile && useSyslog() )
-#endif
+ if ( !sLogToFile && useSyslog() )
socket_syslogv(LOG_ERR, format, list);
else {
_simple_vdprintf(sLogfile, format, list);
@@ -592,8 +519,7 @@
va_end(list);
}
-#else
- extern void vlog(const char* format, va_list list);
+
#endif // !TARGET_IPHONE_SIMULATOR
@@ -703,14 +629,8 @@
{
// use guard so that we cannot notify about the same image twice
if ( ! image->addFuncNotified() ) {
- for (std::vector<ImageCallback>::iterator it=sAddImageCallbacks.begin(); it != sAddImageCallbacks.end(); it++) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE, (uint64_t)image->machHeader(), (uint64_t)(*it), 0);
+ for (std::vector<ImageCallback>::iterator it=sAddImageCallbacks.begin(); it != sAddImageCallbacks.end(); it++)
(*it)(image->machHeader(), image->getSlide());
- }
- for (LoadImageCallback func : sAddLoadImageCallbacks) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE, (uint64_t)image->machHeader(), (uint64_t)(*func), 0);
- (*func)(image->machHeader(), image->getPath(), !image->neverUnload());
- }
image->setAddFuncNotified();
}
}
@@ -754,157 +674,7 @@
return NULL;
}
-#if SUPPORT_ACCELERATE_TABLES
-static dyld_image_state_change_handler getPreInitNotifyHandler(unsigned index)
-{
- std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(dyld_image_state_dependents_initialized, sSingleHandlers);
- if ( index >= handlers->size() )
- return NULL;
- return (*handlers)[index];
-}
-
-static dyld_image_state_change_handler getBoundBatchHandler(unsigned index)
-{
- std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(dyld_image_state_bound, sBatchHandlers);
- if ( index >= handlers->size() )
- return NULL;
- return (*handlers)[index];
-}
-
-static void notifySingleFromCache(dyld_image_states state, const mach_header* mh, const char* path)
-{
- //dyld::log("notifySingle(state=%d, image=%s)\n", state, image->getPath());
- std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(state, sSingleHandlers);
- if ( handlers != NULL ) {
- dyld_image_info info;
- info.imageLoadAddress = mh;
- info.imageFilePath = path;
- info.imageFileModDate = 0;
- for (dyld_image_state_change_handler handler : *handlers) {
- const char* result = (*handler)(state, 1, &info);
- if ( (result != NULL) && (state == dyld_image_state_mapped) ) {
- //fprintf(stderr, " image rejected by handler=%p\n", *it);
- // make copy of thrown string so that later catch clauses can free it
- const char* str = strdup(result);
- throw str;
- }
- }
- }
- if ( (state == dyld_image_state_dependents_initialized) && (sNotifyObjCInit != NULL) && (mh->flags & MH_HAS_OBJC) ) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_OBJC_INIT, (uint64_t)mh, 0, 0);
- (*sNotifyObjCInit)(path, mh);
- }
-}
-#endif
-
-#if !TARGET_OS_SIMULATOR
-static void sendMessage(unsigned portSlot, mach_msg_id_t msgId, mach_msg_size_t sendSize, mach_msg_header_t* buffer, mach_msg_size_t bufferSize) {
- // Allocate a port to listen on in this monitoring task
- mach_port_t sendPort = dyld::gProcessInfo->notifyPorts[portSlot];
- if (sendPort == MACH_PORT_NULL) {
- return;
- }
- mach_port_t replyPort = MACH_PORT_NULL;
- mach_port_options_t options = { .flags = MPO_CONTEXT_AS_GUARD | MPO_STRICT,
- .mpl = { 1 }};
- kern_return_t kr = mach_port_construct(mach_task_self(), &options, (mach_port_context_t)&replyPort, &replyPort);
- if (kr != KERN_SUCCESS) {
- return;
- }
- // Assemble a message
- mach_msg_header_t* h = buffer;
- h->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND,MACH_MSG_TYPE_MAKE_SEND_ONCE);
- h->msgh_id = msgId;
- h->msgh_local_port = replyPort;
- h->msgh_remote_port = sendPort;
- h->msgh_reserved = 0;
- h->msgh_size = sendSize;
- kr = mach_msg(h, MACH_SEND_MSG | MACH_RCV_MSG, h->msgh_size, bufferSize, replyPort, 0, MACH_PORT_NULL);
- mach_msg_destroy(h);
- if ( kr == MACH_SEND_INVALID_DEST ) {
- if (OSAtomicCompareAndSwap32(sendPort, 0, (volatile int32_t*)&dyld::gProcessInfo->notifyPorts[portSlot])) {
- mach_port_deallocate(mach_task_self(), sendPort);
- }
- }
- mach_port_destruct(mach_task_self(), replyPort, 0, (mach_port_context_t)&replyPort);
-}
-
-static void notifyMonitoringDyld(bool unloading, unsigned imageCount, const struct mach_header* loadAddresses[],
- const char* imagePaths[])
-{
- dyld3::ScopedTimer(DBG_DYLD_REMOTE_IMAGE_NOTIFIER, 0, 0, 0);
- for (int slot=0; slot < DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT; ++slot) {
- if ( dyld::gProcessInfo->notifyPorts[slot] == 0) continue;
- unsigned entriesSize = imageCount*sizeof(dyld_process_info_image_entry);
- unsigned pathsSize = 0;
- for (unsigned j=0; j < imageCount; ++j) {
- pathsSize += (strlen(imagePaths[j]) + 1);
- }
- unsigned totalSize = (sizeof(dyld_process_info_notify_header) + entriesSize + pathsSize + 127) & -128; // align
- if ( totalSize > DYLD_PROCESS_INFO_NOTIFY_MAX_BUFFER_SIZE ) {
- // Putting all image paths into one message would make buffer too big.
- // Instead split into two messages. Recurse as needed until paths fit in buffer.
- unsigned imageHalfCount = imageCount/2;
- notifyMonitoringDyld(unloading, imageHalfCount, loadAddresses, imagePaths);
- notifyMonitoringDyld(unloading, imageCount - imageHalfCount, &loadAddresses[imageHalfCount], &imagePaths[imageHalfCount]);
- return;
- }
- uint8_t buffer[totalSize + MAX_TRAILER_SIZE];
- dyld_process_info_notify_header* header = (dyld_process_info_notify_header*)buffer;
- header->version = 1;
- header->imageCount = imageCount;
- header->imagesOffset = sizeof(dyld_process_info_notify_header);
- header->stringsOffset = sizeof(dyld_process_info_notify_header) + entriesSize;
- header->timestamp = dyld::gProcessInfo->infoArrayChangeTimestamp;
- dyld_process_info_image_entry* entries = (dyld_process_info_image_entry*)&buffer[header->imagesOffset];
- char* const pathPoolStart = (char*)&buffer[header->stringsOffset];
- char* pathPool = pathPoolStart;
- for (unsigned j=0; j < imageCount; ++j) {
- strcpy(pathPool, imagePaths[j]);
- uint32_t len = (uint32_t)strlen(pathPool);
- bzero(entries->uuid, 16);
- dyld3::MachOFile* mf = (dyld3::MachOFile*)loadAddresses[j];
- mf->getUuid(entries->uuid);
- entries->loadAddress = (uint64_t)loadAddresses[j];
- entries->pathStringOffset = (uint32_t)(pathPool - pathPoolStart);
- entries->pathLength = len;
- pathPool += (len +1);
- ++entries;
- }
- if (unloading) {
- sendMessage(slot, DYLD_PROCESS_INFO_NOTIFY_UNLOAD_ID, totalSize, (mach_msg_header_t*)buffer, totalSize+MAX_TRAILER_SIZE);
- } else {
- sendMessage(slot, DYLD_PROCESS_INFO_NOTIFY_LOAD_ID, totalSize, (mach_msg_header_t*)buffer, totalSize+MAX_TRAILER_SIZE);
- }
- }
-}
-
-static void notifyMonitoringDyldMain()
-{
- dyld3::ScopedTimer(DBG_DYLD_REMOTE_IMAGE_NOTIFIER, 0, 0, 0);
- for (int slot=0; slot < DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT; ++slot) {
- if ( dyld::gProcessInfo->notifyPorts[slot] == 0) continue;
- uint8_t buffer[sizeof(mach_msg_header_t) + MAX_TRAILER_SIZE];
- sendMessage(slot, DYLD_PROCESS_INFO_NOTIFY_MAIN_ID, sizeof(mach_msg_header_t), (mach_msg_header_t*)buffer, sizeof(mach_msg_header_t) + MAX_TRAILER_SIZE);
- }
-}
-#else
-extern void notifyMonitoringDyldMain() VIS_HIDDEN;
-extern void notifyMonitoringDyld(bool unloading, unsigned imageCount, const struct mach_header* loadAddresses[],
- const char* imagePaths[]) VIS_HIDDEN;
-#endif
-
-void notifyKernel(const ImageLoader& image, bool loading) {
- if ( !image.inSharedCache() ) {
- uint32_t baseCode = loading ? DBG_DYLD_UUID_MAP_A : DBG_DYLD_UUID_UNMAP_A;
- uuid_t uuid;
- ino_t inode = image.getInode();
- image.getUUID(uuid);
- dyld3::kdebug_trace_dyld_image(baseCode, (const uuid_t *)&uuid, *(fsobj_id_t*)&inode, {{ image.getDevice(), 0 }}, image.machHeader());
- }
-}
-
-static void notifySingle(dyld_image_states state, const ImageLoader* image, ImageLoader::InitializerTimingList* timingInfo)
+static void notifySingle(dyld_image_states state, const ImageLoader* image)
{
//dyld::log("notifySingle(state=%d, image=%s)\n", state, image->getPath());
std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(state, sSingleHandlers);
@@ -933,26 +703,20 @@
}
}
}
- if ( (state == dyld_image_state_dependents_initialized) && (sNotifyObjCInit != NULL) && image->notifyObjC() ) {
- uint64_t t0 = mach_absolute_time();
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_OBJC_INIT, (uint64_t)image->machHeader(), 0, 0);
- (*sNotifyObjCInit)(image->getRealPath(), image->machHeader());
- uint64_t t1 = mach_absolute_time();
- uint64_t t2 = mach_absolute_time();
- uint64_t timeInObjC = t1-t0;
- uint64_t emptyTime = (t2-t1)*100;
- if ( (timeInObjC > emptyTime) && (timingInfo != NULL) ) {
- timingInfo->addTime(image->getShortName(), timeInObjC);
- }
- }
// mach message csdlc about dynamically unloaded images
if ( image->addFuncNotified() && (state == dyld_image_state_terminated) ) {
- notifyKernel(*image, false);
- const struct mach_header* loadAddress[] = { image->machHeader() };
- const char* loadPath[] = { image->getPath() };
- notifyMonitoringDyld(true, 1, loadAddress, loadPath);
- }
-}
+ uint64_t loadTimestamp = mach_absolute_time();
+ if ( sEnv.DYLD_PRINT_CS_NOTIFICATIONS ) {
+ dyld::log("dyld: coresymbolication_unload_notifier(%p, 0x%016llX, %p, %s)\n",
+ dyld::gProcessInfo->coreSymbolicationShmPage, loadTimestamp, image->machHeader(), image->getPath());
+ }
+ if ( dyld::gProcessInfo->coreSymbolicationShmPage != NULL) {
+ coresymbolication_unload_notifier(dyld::gProcessInfo->coreSymbolicationShmPage, loadTimestamp, image->getPath(), image->machHeader());
+ }
+ }
+}
+
+
//
@@ -1000,14 +764,13 @@
return left->compare(right);
}
-static void notifyBatchPartial(dyld_image_states state, bool orLater, dyld_image_state_change_handler onlyHandler, bool preflightOnly, bool onlyObjCMappedNotification)
+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) || ((state == dyld_image_state_bound) && (sNotifyObjCMapped != NULL)) ) {
+ if ( handlers != NULL ) {
// don't use a vector because it will use malloc/free and we want notifcation to be low cost
allImagesLock();
- dyld_image_info infos[allImagesCount()+1];
- ImageLoader* images[allImagesCount()+1];
+ 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();
@@ -1020,145 +783,67 @@
*end++ = sBundleBeingLoaded;
}
const char* dontLoadReason = NULL;
- uint32_t imageCount = (uint32_t)(end-images);
- if ( imageCount != 0 ) {
+ uint32_t count = (uint32_t)(end-images);
+ if ( end != images ) {
// sort bottom up
- qsort(images, imageCount, sizeof(ImageLoader*), &imageSorter);
+ qsort(images, count, sizeof(ImageLoader*), &imageSorter);
// build info array
- for (unsigned int i=0; i < imageCount; ++i) {
+ dyld_image_info infos[count];
+ for (unsigned int i=0; i < count; ++i) {
dyld_image_info* p = &infos[i];
ImageLoader* image = images[i];
//dyld::log(" state=%d, name=%s\n", state, image->getPath());
p->imageLoadAddress = image->machHeader();
- p->imageFilePath = image->getRealPath();
+ p->imageFilePath = image->getRealPath();
p->imageFileModDate = image->lastModified();
- // get these registered with the kernel as early as possible
- if ( state == dyld_image_state_dependents_mapped)
- notifyKernel(*image, true);
// special case for add_image hook
if ( state == dyld_image_state_bound )
notifyAddImageCallbacks(image);
}
- }
-#if SUPPORT_ACCELERATE_TABLES
- if ( sAllCacheImagesProxy != NULL ) {
- unsigned cacheCount = sAllCacheImagesProxy->appendImagesToNotify(state, orLater, &infos[imageCount]);
- // support _dyld_register_func_for_add_image()
- if ( state == dyld_image_state_bound ) {
- for (ImageCallback callback : sAddImageCallbacks) {
- for (unsigned i=0; i < cacheCount; ++i) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE, (uint64_t)infos[imageCount+i].imageLoadAddress, (uint64_t)(*callback), 0);
- (*callback)(infos[imageCount+i].imageLoadAddress, sSharedCacheLoadInfo.slide);
+
+ if ( onlyHandler != NULL ) {
+ const char* result = (*onlyHandler)(state, count, infos);
+ if ( (result != NULL) && (state == dyld_image_state_dependents_mapped) ) {
+ //fprintf(stderr, " images rejected by handler=%p\n", onlyHandler);
+ // make copy of thrown string so that later catch clauses can free it
+ dontLoadReason = strdup(result);
+ }
+ }
+ else {
+ // call each handler with whole array
+ 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) ) {
+ //fprintf(stderr, " images rejected by handler=%p\n", *it);
+ // make copy of thrown string so that later catch clauses can free it
+ dontLoadReason = strdup(result);
+ break;
}
}
- for (LoadImageCallback func : sAddLoadImageCallbacks) {
- for (unsigned i=0; i < cacheCount; ++i) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE, (uint64_t)infos[imageCount+i].imageLoadAddress, (uint64_t)(*func), 0);
- (*func)(infos[imageCount+i].imageLoadAddress, infos[imageCount+i].imageFilePath, false);
+ }
+ if ( (state == dyld_image_state_dependents_mapped) && ((dyld::gProcessInfo->coreSymbolicationShmPage != NULL) || sEnv.DYLD_PRINT_CS_NOTIFICATIONS) ) {
+ // mach message csdlc about loaded images
+ uint64_t loadTimestamp = mach_absolute_time();
+ for (unsigned j=0; j < count; ++j) {
+ if ( sEnv.DYLD_PRINT_CS_NOTIFICATIONS ) {
+ dyld::log("dyld: coresymbolication_load_notifier(%p, 0x%016llX, %p, %s)\n",
+ dyld::gProcessInfo->coreSymbolicationShmPage, loadTimestamp, infos[j].imageLoadAddress, infos[j].imageFilePath);
}
- }
- }
- imageCount += cacheCount;
- }
-#endif
- if ( imageCount != 0 ) {
- if ( !onlyObjCMappedNotification ) {
- if ( onlyHandler != NULL ) {
- const char* result = NULL;
- if ( result == NULL ) {
- result = (*onlyHandler)(state, imageCount, infos);
- }
- if ( (result != NULL) && (state == dyld_image_state_dependents_mapped) ) {
- //fprintf(stderr, " images rejected by handler=%p\n", onlyHandler);
- // make copy of thrown string so that later catch clauses can free it
- dontLoadReason = strdup(result);
- }
- }
- else {
- // call each handler with whole array
- if ( handlers != NULL ) {
- for (std::vector<dyld_image_state_change_handler>::iterator it = handlers->begin(); it != handlers->end(); ++it) {
- const char* result = (*it)(state, imageCount, infos);
- if ( (result != NULL) && (state == dyld_image_state_dependents_mapped) ) {
- //fprintf(stderr, " images rejected by handler=%p\n", *it);
- // make copy of thrown string so that later catch clauses can free it
- dontLoadReason = strdup(result);
- break;
- }
- }
- }
- }
- }
- // tell objc about new images
- if ( (onlyHandler == NULL) && ((state == dyld_image_state_bound) || (orLater && (dyld_image_state_bound > state))) && (sNotifyObjCMapped != NULL) ) {
- const char* paths[imageCount];
- const mach_header* mhs[imageCount];
- unsigned objcImageCount = 0;
- for (int i=0; i < imageCount; ++i) {
- const ImageLoader* image = findImageByMachHeader(infos[i].imageLoadAddress);
- bool hasObjC = false;
- if ( image != NULL ) {
- hasObjC = image->notifyObjC();
- }
-#if SUPPORT_ACCELERATE_TABLES
- else if ( sAllCacheImagesProxy != NULL ) {
- const mach_header* mh;
- const char* path;
- unsigned index;
- if ( sAllCacheImagesProxy->addressInCache(infos[i].imageLoadAddress, &mh, &path, &index) ) {
- hasObjC = (mh->flags & MH_HAS_OBJC);
- }
- }
-#endif
- if ( hasObjC ) {
- paths[objcImageCount] = infos[i].imageFilePath;
- mhs[objcImageCount] = infos[i].imageLoadAddress;
- ++objcImageCount;
- }
- }
- if ( objcImageCount != 0 ) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_OBJC_MAP, 0, 0, 0);
- uint64_t t0 = mach_absolute_time();
- (*sNotifyObjCMapped)(objcImageCount, paths, mhs);
- uint64_t t1 = mach_absolute_time();
- ImageLoader::fgTotalObjCSetupTime += (t1-t0);
+ coresymbolication_load_notifier(dyld::gProcessInfo->coreSymbolicationShmPage, loadTimestamp, infos[j].imageFilePath, infos[j].imageLoadAddress);
}
}
}
allImagesUnlock();
if ( dontLoadReason != NULL )
throw dontLoadReason;
- if ( !preflightOnly && (state == dyld_image_state_dependents_mapped) ) {
- const struct mach_header* loadAddresses[imageCount];
- const char* loadPaths[imageCount];
- for(uint32_t i = 0; i<imageCount; ++i) {
- loadAddresses[i] = infos[i].imageLoadAddress;
- loadPaths[i] = infos[i].imageFilePath;
- }
- notifyMonitoringDyld(false, imageCount, loadAddresses, loadPaths);
- }
- }
-}
-
-static void notifyBatch(dyld_image_states state, bool preflightOnly)
-{
- notifyBatchPartial(state, false, NULL, preflightOnly, false);
-}
-
-static
-void coresymbolication_load_notifier(void* connection, uint64_t timestamp, const char* path, const struct mach_header* mh)
-{
- const struct mach_header* loadAddress[] = { mh };
- const char* loadPath[] = { path };
- notifyMonitoringDyld(false, 1, loadAddress, loadPath);
-}
-
-static
-void coresymbolication_unload_notifier(void* connection, uint64_t timestamp, const char* path, const struct mach_header* mh)
-{
- const struct mach_header* loadAddress = { mh };
- const char* loadPath = { path };
- notifyMonitoringDyld(true, 1, &loadAddress, &loadPath);
+ }
+}
+
+
+
+static void notifyBatch(dyld_image_states state)
+{
+ notifyBatchPartial(state, false, NULL);
}
// In order for register_func_for_add_image() callbacks to to be called bottom up,
@@ -1188,10 +873,7 @@
static unsigned int imageCount()
{
- allImagesLock();
- unsigned int result = (unsigned int)sAllImages.size();
- allImagesUnlock();
- return (result);
+ return (unsigned int)sAllImages.size();
}
@@ -1214,36 +896,11 @@
}
#endif
-static bool sandboxBlocked(const char* path, const char* kind)
-{
-#if TARGET_IPHONE_SIMULATOR
- // sandbox calls not yet supported in simulator runtime
- return false;
-#else
- sandbox_filter_type filter = (sandbox_filter_type)(SANDBOX_FILTER_PATH | SANDBOX_CHECK_NO_REPORT);
- return ( sandbox_check(getpid(), kind, filter, path) > 0 );
-#endif
-}
-
-bool sandboxBlockedMmap(const char* path)
-{
- return sandboxBlocked(path, "file-map-executable");
-}
-
-bool sandboxBlockedOpen(const char* path)
-{
- return sandboxBlocked(path, "file-read-data");
-}
-
-bool sandboxBlockedStat(const char* path)
-{
- return sandboxBlocked(path, "file-read-metadata");
-}
-
-
static void addDynamicReference(ImageLoader* from, ImageLoader* to) {
- // don't add dynamic reference if target is in the shared cache (since it can't be unloaded)
- if ( to->inSharedCache() )
+ // don't add dynamic reference if either are in the shared cache
+ if( from->inSharedCache() )
+ return;
+ if( to->inSharedCache() )
return;
// don't add dynamic reference if there already is a static one
@@ -1298,7 +955,7 @@
addMappedRange(image, lastSegStart, lastSegEnd);
- if ( gLinkContext.verboseLoading || (sEnv.DYLD_PRINT_LIBRARIES_POST_LAUNCH && (sMainExecutable!=NULL) && sMainExecutable->isLinked()) ) {
+ if ( sEnv.DYLD_PRINT_LIBRARIES || (sEnv.DYLD_PRINT_LIBRARIES_POST_LAUNCH && (sMainExecutable!=NULL) && sMainExecutable->isLinked()) ) {
dyld::log("dyld: loaded: %s\n", image->getPath());
}
@@ -1338,17 +995,13 @@
if ( image->getState() >= dyld_image_state_bound ) {
sRemoveImageCallbacksInUse = true; // This only runs inside dyld's global lock, so ok to use a global for the in-use flag.
for (std::vector<ImageCallback>::iterator it=sRemoveImageCallbacks.begin(); it != sRemoveImageCallbacks.end(); it++) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_REMOVE_IMAGE, (uint64_t)image->machHeader(), (uint64_t)(*it), 0);
(*it)(image->machHeader(), image->getSlide());
}
sRemoveImageCallbacksInUse = false;
-
- if ( sNotifyObjCUnmapped != NULL && image->notifyObjC() )
- (*sNotifyObjCUnmapped)(image->getRealPath(), image->machHeader());
}
// notify
- notifySingle(dyld_image_state_terminated, image, NULL);
+ notifySingle(dyld_image_state_terminated, image);
// remove from mapped images table
removedMappedRanges(image);
@@ -1381,7 +1034,7 @@
}
// log if requested
- if ( gLinkContext.verboseLoading || (sEnv.DYLD_PRINT_LIBRARIES_POST_LAUNCH && (sMainExecutable!=NULL) && sMainExecutable->isLinked()) ) {
+ if ( sEnv.DYLD_PRINT_LIBRARIES || (sEnv.DYLD_PRINT_LIBRARIES_POST_LAUNCH && (sMainExecutable!=NULL) && sMainExecutable->isLinked()) ) {
dyld::log("dyld: unloaded: %s\n", image->getPath());
}
@@ -1427,7 +1080,7 @@
image->doTermination(gLinkContext);
}
sImageFilesNeedingTermination.clear();
- notifyBatch(dyld_image_state_terminated, false);
+ notifyBatch(dyld_image_state_terminated);
}
catch (const char* msg) {
halt(msg);
@@ -1440,7 +1093,7 @@
gLinkContext.startedInitializingMainExecutable = true;
// run initialzers for any inserted dylibs
- ImageLoader::InitializerTimingList initializerTimes[allImagesCount()];
+ ImageLoader::InitializerTimingList initializerTimes[sAllImages.size()];
initializerTimes[0].count = 0;
const size_t rootCount = sImageRoots.size();
if ( rootCount > 1 ) {
@@ -1458,9 +1111,7 @@
// dump info if requested
if ( sEnv.DYLD_PRINT_STATISTICS )
- ImageLoader::printStatistics((unsigned int)allImagesCount(), initializerTimes[0]);
- if ( sEnv.DYLD_PRINT_STATISTICS_DETAILS )
- ImageLoaderMachO::printStatisticsDetails((unsigned int)allImagesCount(), initializerTimes[0]);
+ ImageLoaderMachO::printStatistics((unsigned int)sAllImages.size(), initializerTimes[0]);
}
bool mainExecutablePrebound()
@@ -1539,8 +1190,7 @@
{
//dyld::log("checkDylibOverridesInDir('%s')\n", dirPath);
char dylibPath[PATH_MAX];
- long dirPathLen = strlcpy(dylibPath, dirPath, PATH_MAX-1);
- if ( dirPathLen >= PATH_MAX )
+ if ( strlcpy(dylibPath, dirPath, PATH_MAX) >= PATH_MAX )
return;
DIR* dirp = opendir(dirPath);
if ( dirp != NULL) {
@@ -1551,8 +1201,8 @@
break;
if ( entp->d_type != DT_REG )
continue;
- dylibPath[dirPathLen] = '/';
- dylibPath[dirPathLen+1] = '\0';
+ if ( strlcat(dylibPath, "/", PATH_MAX) >= PATH_MAX )
+ continue;
if ( strlcat(dylibPath, entp->d_name, PATH_MAX) >= PATH_MAX )
continue;
checkDylibOverride(dylibPath);
@@ -1566,8 +1216,7 @@
{
//dyld::log("checkFrameworkOverridesInDir('%s')\n", dirPath);
char frameworkPath[PATH_MAX];
- long dirPathLen = strlcpy(frameworkPath, dirPath, PATH_MAX-1);
- if ( dirPathLen >= PATH_MAX )
+ if ( strlcpy(frameworkPath, dirPath, PATH_MAX) >= PATH_MAX )
return;
DIR* dirp = opendir(dirPath);
if ( dirp != NULL) {
@@ -1578,9 +1227,9 @@
break;
if ( entp->d_type != DT_DIR )
continue;
- frameworkPath[dirPathLen] = '/';
- frameworkPath[dirPathLen+1] = '\0';
- int dirNameLen = (int)strlen(entp->d_name);
+ if ( strlcat(frameworkPath, "/", PATH_MAX) >= PATH_MAX )
+ continue;
+ int dirNameLen = strlen(entp->d_name);
if ( dirNameLen < 11 )
continue;
if ( strcmp(&entp->d_name[dirNameLen-10], ".framework") != 0 )
@@ -1625,10 +1274,6 @@
if (*s == ':') {
size_t len = s-start;
if ( (mainExecutableDir != NULL) && (strncmp(start, "@loader_path/", 13) == 0) ) {
- if ( !gLinkContext.allowAtPaths ) {
- dyld::log("dyld: warning: @loader_path/ ignored because of amfi policy\n");
- continue;
- }
size_t mainExecDirLen = strlen(mainExecutableDir);
char* str = new char[mainExecDirLen+len+1];
strcpy(str, mainExecutableDir);
@@ -1638,10 +1283,6 @@
result[index++] = str;
}
else if ( (mainExecutableDir != NULL) && (strncmp(start, "@executable_path/", 17) == 0) ) {
- if ( !gLinkContext.allowAtPaths ) {
- dyld::log("dyld: warning: @executable_path/ ignored because of amfi policy\n");
- continue;
- }
size_t mainExecDirLen = strlen(mainExecutableDir);
char* str = new char[mainExecDirLen+len+1];
strcpy(str, mainExecutableDir);
@@ -1661,32 +1302,20 @@
}
size_t len = strlen(start);
if ( (mainExecutableDir != NULL) && (strncmp(start, "@loader_path/", 13) == 0) ) {
- if ( !gLinkContext.allowAtPaths ) {
- dyld::log("dyld: warning: @loader_path/ ignored because of amfi policy\n");
- }
- else
- {
- 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;
- }
+ 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) ) {
- if ( !gLinkContext.allowAtPaths ) {
- dyld::log("dyld: warning: @executable_path/ ignored because of amfi policy\n");
- }
- else
- {
- 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;
- }
+ 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];
@@ -1728,7 +1357,7 @@
}
}
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
+
static void paths_expand_roots(const char **paths, const char *key, const char *val)
{
// assert(val != NULL);
@@ -1760,7 +1389,6 @@
}
paths[i-skip] = NULL;
}
-#endif
#if 0
@@ -1777,8 +1405,6 @@
}
#endif
-
-
static void printOptions(const char* argv[])
{
uint32_t i = 0;
@@ -1810,7 +1436,6 @@
else if ( strcmp(key, "DYLD_FALLBACK_LIBRARY_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_FALLBACK_LIBRARY_PATH);
}
-#if SUPPORT_ROOT_PATH
else if ( (strcmp(key, "DYLD_ROOT_PATH") == 0) || (strcmp(key, "DYLD_PATHS_ROOT") == 0) ) {
if ( strcmp(value, "/") != 0 ) {
gLinkContext.rootPaths = parseColonList(value, mainExecutableDir);
@@ -1822,16 +1447,12 @@
}
}
}
- }
-#endif
+ }
else if ( strcmp(key, "DYLD_IMAGE_SUFFIX") == 0 ) {
- gLinkContext.imageSuffix = parseColonList(value, NULL);
+ gLinkContext.imageSuffix = value;
}
else if ( strcmp(key, "DYLD_INSERT_LIBRARIES") == 0 ) {
sEnv.DYLD_INSERT_LIBRARIES = parseColonList(value, NULL);
-#if SUPPORT_ACCELERATE_TABLES
- sDisableAcceleratorTables = true;
-#endif
}
else if ( strcmp(key, "DYLD_PRINT_OPTS") == 0 ) {
sEnv.DYLD_PRINT_OPTS = true;
@@ -1846,7 +1467,7 @@
gLinkContext.preFetchDisabled = true;
}
else if ( strcmp(key, "DYLD_PRINT_LIBRARIES") == 0 ) {
- gLinkContext.verboseLoading = true;
+ sEnv.DYLD_PRINT_LIBRARIES = true;
}
else if ( strcmp(key, "DYLD_PRINT_LIBRARIES_POST_LAUNCH") == 0 ) {
sEnv.DYLD_PRINT_LIBRARIES_POST_LAUNCH = true;
@@ -1873,19 +1494,6 @@
}
else if ( strcmp(key, "DYLD_PRINT_STATISTICS") == 0 ) {
sEnv.DYLD_PRINT_STATISTICS = true;
-#if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
- // <rdar://problem/26614838> DYLD_PRINT_STATISTICS no longer logs to xcode console for device apps
- sForceStderr = true;
-#endif
- }
- else if ( strcmp(key, "DYLD_PRINT_TO_STDERR") == 0 ) {
-#if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
- // <rdar://problem/26633440> DYLD_PRINT_STATISTICS no longer logs to xcode console for device apps
- sForceStderr = true;
-#endif
- }
- else if ( strcmp(key, "DYLD_PRINT_STATISTICS_DETAILS") == 0 ) {
- sEnv.DYLD_PRINT_STATISTICS_DETAILS = true;
}
else if ( strcmp(key, "DYLD_PRINT_SEGMENTS") == 0 ) {
gLinkContext.verboseMapping = true;
@@ -1902,24 +1510,22 @@
else if ( strcmp(key, "DYLD_PRINT_APIS") == 0 ) {
gLogAPIs = true;
}
-#if SUPPORT_ACCELERATE_TABLES
- else if ( strcmp(key, "DYLD_PRINT_APIS_APP") == 0 ) {
- gLogAppAPIs = true;
- }
-#endif
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) && gLinkContext.allowEnvVarsSharedCache ) {
+ else if ( strcmp(key, "DYLD_SHARED_REGION") == 0 ) {
if ( strcmp(value, "private") == 0 ) {
gLinkContext.sharedRegionMode = ImageLoader::kUsePrivateSharedRegion;
}
@@ -1936,18 +1542,14 @@
dyld::warn("unknown option to DYLD_SHARED_REGION. Valid options are: use, private, avoid\n");
}
}
- else if ( (strcmp(key, "DYLD_SHARED_CACHE_DIR") == 0) && gLinkContext.allowEnvVarsSharedCache ) {
- sSharedCacheOverrideDir = value;
- }
- else if ( strcmp(key, "DYLD_USE_CLOSURES") == 0 ) {
- if ( dyld3::internalInstall() ) {
-#if __MAC_OS_X_VERSION_MIN_REQUIRED && __i386__
- // don't support dyld3 for 32-bit macOS
-#else
- sEnableClosures = true;
-#endif
- }
- }
+#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;
@@ -1968,19 +1570,13 @@
#if SUPPORT_VERSIONED_PATHS
else if ( strcmp(key, "DYLD_VERSIONED_LIBRARY_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_VERSIONED_LIBRARY_PATH);
- #if SUPPORT_ACCELERATE_TABLES
- sDisableAcceleratorTables = true;
- #endif
}
else if ( strcmp(key, "DYLD_VERSIONED_FRAMEWORK_PATH") == 0 ) {
appendParsedColonList(value, mainExecutableDir, &sEnv.DYLD_VERSIONED_FRAMEWORK_PATH);
- #if SUPPORT_ACCELERATE_TABLES
- sDisableAcceleratorTables = true;
- #endif
}
#endif
#if !TARGET_IPHONE_SIMULATOR
- else if ( (strcmp(key, "DYLD_PRINT_TO_FILE") == 0) && (mainExecutableDir == NULL) && gLinkContext.allowEnvVarsSharedCache ) {
+ else if ( (strcmp(key, "DYLD_PRINT_TO_FILE") == 0) && (mainExecutableDir == NULL) ) {
int fd = open(value, O_WRONLY | O_CREAT | O_APPEND, 0644);
if ( fd != -1 ) {
sLogfile = fd;
@@ -1989,10 +1585,6 @@
else {
dyld::log("dyld: could not open DYLD_PRINT_TO_FILE='%s', errno=%d\n", value, errno);
}
- }
- else if ( (strcmp(key, "DYLD_SKIP_MAIN") == 0)) {
- if ( dyld3::internalInstall() )
- sSkipMain = true;
}
#endif
else {
@@ -2081,7 +1673,6 @@
#endif
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
//
// For security, setuid programs ignore DYLD_* environment variables.
// Additionally, the DYLD_* enviroment variables are removed
@@ -2089,21 +1680,10 @@
//
static void pruneEnvironmentVariables(const char* envp[], const char*** applep)
{
-#if SUPPORT_LC_DYLD_ENVIRONMENT
- checkLoadCommandEnvironmentVariables();
-#endif
-
- // Are we testing dyld on an internal config?
- if ( _simple_getenv(envp, "DYLD_SKIP_MAIN") != NULL ) {
- if ( dyld3::internalInstall() )
- sSkipMain = true;
- }
-
// delete all DYLD_* and LD_LIBRARY_PATH environment variables
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;
}
@@ -2112,6 +1692,25 @@
}
}
*d++ = NULL;
+// <rdar://11894054> Disable warnings about DYLD_ env vars being ignored. The warnings are causing too much confusion.
+#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
// slide apple parameters
if ( removedCount > 0 ) {
*applep = d;
@@ -2129,17 +1728,10 @@
if ( removedCount > 0 )
strlcat(sLoadingCrashMessage, ", ignoring DYLD_* env vars", sizeof(sLoadingCrashMessage));
}
-#endif
static void defaultUninitializedFallbackPaths(const char* envp[])
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED
- if ( !gLinkContext.allowClassicFallbackPaths ) {
- sEnv.DYLD_FALLBACK_FRAMEWORK_PATH = sRestrictedFrameworkFallbackPaths;
- sEnv.DYLD_FALLBACK_LIBRARY_PATH = sRestrictedLibraryFallbackPaths;
- return;
- }
-
// default value for DYLD_FALLBACK_FRAMEWORK_PATH, if not set in environment
const char* home = _simple_getenv(envp, "HOME");;
if ( sEnv.DYLD_FALLBACK_FRAMEWORK_PATH == NULL ) {
@@ -2172,8 +1764,6 @@
static void checkEnvironmentVariables(const char* envp[])
{
- if ( !gLinkContext.allowEnvVarsPath && !gLinkContext.allowEnvVarsPrint )
- return;
const char** p;
for(p = envp; *p != NULL; p++) {
const char* keyEqualsValue = *p;
@@ -2187,8 +1777,6 @@
char key[keyLen+1];
strncpy(key, keyEqualsValue, keyLen);
key[keyLen] = '\0';
- if ( (strncmp(key, "DYLD_PRINT_", 11) == 0) && !gLinkContext.allowEnvVarsPrint )
- continue;
processDyldEnvironmentVariable(key, value, NULL);
}
}
@@ -2202,16 +1790,14 @@
checkLoadCommandEnvironmentVariables();
#endif // SUPPORT_LC_DYLD_ENVIRONMENT
-#if SUPPORT_ROOT_PATH
// <rdar://problem/11281064> DYLD_IMAGE_SUFFIX and DYLD_ROOT_PATH cannot be used together
- if ( (gLinkContext.imageSuffix != NULL && *gLinkContext.imageSuffix != NULL) && (gLinkContext.rootPaths != NULL) ) {
+ if ( (gLinkContext.imageSuffix != NULL) && (gLinkContext.rootPaths != NULL) ) {
dyld::warn("Ignoring DYLD_IMAGE_SUFFIX because DYLD_ROOT_PATH is used.\n");
- gLinkContext.imageSuffix = NULL; // this leaks allocations from parseColonList
- }
-#endif
-}
-
-#if __x86_64__ && !TARGET_IPHONE_SIMULATOR
+ gLinkContext.imageSuffix = NULL;
+ }
+}
+
+#if __x86_64__
static bool isGCProgram(const macho_header* mh, uintptr_t slide)
{
const uint32_t cmd_count = mh->ncmds;
@@ -2240,7 +1826,6 @@
return false;
}
#endif
-
static void getHostInfo(const macho_header* mainExecutableMH, uintptr_t mainExecutableSlide)
{
#if CPU_SUBTYPES_SUPPORTED
@@ -2259,15 +1844,6 @@
#elif __ARM_ARCH_7S__
sHostCPU = CPU_TYPE_ARM;
sHostCPUsubtype = CPU_SUBTYPE_ARM_V7S;
-#elif __ARM64_ARCH_8_32__
- sHostCPU = CPU_TYPE_ARM64_32;
- sHostCPUsubtype = CPU_SUBTYPE_ARM64_32_V8;
-#elif __arm64e__
- sHostCPU = CPU_TYPE_ARM64;
- sHostCPUsubtype = CPU_SUBTYPE_ARM64_E;
-#elif __arm64__
- sHostCPU = CPU_TYPE_ARM64;
- sHostCPUsubtype = CPU_SUBTYPE_ARM64_V8;
#else
struct host_basic_info info;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
@@ -2279,10 +1855,9 @@
sHostCPUsubtype = info.cpu_subtype;
mach_port_deallocate(mach_task_self(), hostPort);
#if __x86_64__
- // host_info returns CPU_TYPE_I386 even for x86_64. Override that here so that
- // we don't need to mask the cpu type later.
- sHostCPU = CPU_TYPE_X86_64;
- #if !TARGET_IPHONE_SIMULATOR
+ #if TARGET_IPHONE_SIMULATOR
+ sHaswell = false;
+ #else
sHaswell = (sHostCPUsubtype == CPU_SUBTYPE_X86_64_H);
// <rdar://problem/18528074> x86_64h: Fall back to the x86_64 slice if an app requires GC.
if ( sHaswell ) {
@@ -2299,24 +1874,24 @@
#endif
}
-static void checkSharedRegionDisable(const dyld3::MachOLoaded* mainExecutableMH, uintptr_t mainExecutableSlide)
+static void checkSharedRegionDisable()
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED
- // if main executable has segments that overlap the shared region,
+ // if main executable has segments that overlap the shared region,
// then disable using the shared region
- if ( mainExecutableMH->intersectsRange(SHARED_REGION_BASE, SHARED_REGION_SIZE) ) {
+ 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 ( !gLinkContext.allowEnvVarsPath ) {
+ if ( sProcessIsRestricted ) {
// <rdar://problem/15280847> use private or no shared region for suid processes
gLinkContext.sharedRegionMode = ImageLoader::kUsePrivateSharedRegion;
}
#endif
#endif
- // iOS cannot run without shared region
+ // iPhoneOS cannot run without shared region
}
bool validImage(const ImageLoader* possibleImage)
@@ -2350,15 +1925,6 @@
ImageLoader* findImageContainingAddress(const void* addr)
{
- #if SUPPORT_ACCELERATE_TABLES
- if ( sAllCacheImagesProxy != NULL ) {
- const mach_header* mh;
- const char* path;
- unsigned index;
- if ( sAllCacheImagesProxy->addressInCache(addr, &mh, &path, &index) )
- return sAllCacheImagesProxy;
- }
- #endif
return findMappedRange((uintptr_t)addr);
}
@@ -2440,10 +2006,8 @@
if ( gLinkContext.imageSuffix != NULL ) {
// some debug frameworks have install names that end in _debug
if ( strncmp(framework, &leaf[1], len) == 0 ) {
- for (const char* const* suffix=gLinkContext.imageSuffix; *suffix != NULL; ++suffix) {
- if ( strcmp(*suffix, &leaf[len+1]) == 0 )
- return frameworkStart;
- }
+ if ( strcmp( gLinkContext.imageSuffix, &leaf[len+1]) == 0 )
+ return frameworkStart;
}
}
}
@@ -2516,34 +2080,6 @@
};
#endif
-#if __arm64__
-//
-// ARM64 sub-type lists
-//
-const int kARM64_RowCount = 2;
-static const cpu_subtype_t kARM64[kARM64_RowCount][4] = {
-
- // armv64e can run: 64e, 64
- { CPU_SUBTYPE_ARM64_E, CPU_SUBTYPE_ARM64_V8, CPU_SUBTYPE_ARM64_ALL, CPU_SUBTYPE_END_OF_LIST },
-
- // armv64 can run: 64
- { CPU_SUBTYPE_ARM64_V8, CPU_SUBTYPE_ARM64_ALL, CPU_SUBTYPE_END_OF_LIST },
-};
-
-#if __ARM64_ARCH_8_32__
-const int kARM64_32_RowCount = 2;
-static const cpu_subtype_t kARM64_32[kARM64_32_RowCount][4] = {
-
- // armv64_32 can run: v8
- { CPU_SUBTYPE_ARM64_32_V8, CPU_SUBTYPE_END_OF_LIST },
-
- // armv64 can run: 64
- { CPU_SUBTYPE_ARM64_V8, CPU_SUBTYPE_ARM64_ALL, CPU_SUBTYPE_END_OF_LIST },
-};
-#endif
-
-#endif
-
#if __x86_64__
//
// x86_64 sub-type lists
@@ -2552,10 +2088,10 @@
static const cpu_subtype_t kX86_64[kX86_64_RowCount][5] = {
// x86_64h can run: x86_64h, x86_64h(lib), x86_64(lib), and x86_64
- { CPU_SUBTYPE_X86_64_H, (cpu_subtype_t)(CPU_SUBTYPE_LIB64|CPU_SUBTYPE_X86_64_H), (cpu_subtype_t)(CPU_SUBTYPE_LIB64|CPU_SUBTYPE_X86_64_ALL), CPU_SUBTYPE_X86_64_ALL, CPU_SUBTYPE_END_OF_LIST },
+ { 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 },
// x86_64 can run: x86_64(lib) and x86_64
- { CPU_SUBTYPE_X86_64_ALL, (cpu_subtype_t)(CPU_SUBTYPE_LIB64|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
@@ -2572,24 +2108,6 @@
return kARM[i];
}
break;
-#endif
-#if __arm64__
- case CPU_TYPE_ARM64:
- for (int i=0; i < kARM64_RowCount ; ++i) {
- if ( kARM64[i][0] == subtype )
- return kARM64[i];
- }
- break;
-
-#if __ARM64_ARCH_8_32__
- case CPU_TYPE_ARM64_32:
- for (int i=0; i < kARM64_32_RowCount ; ++i) {
- if ( kARM64_32[i][0] == subtype )
- return kARM64_32[i];
- }
- break;
-#endif
-
#endif
#if __x86_64__
case CPU_TYPE_X86_64:
@@ -2654,15 +2172,6 @@
}
break;
#endif
-#if __arm64__
- case CPU_TYPE_ARM64:
- if ( (cpu_subtype_t)OSSwapBigToHostInt32(archs[i].cpusubtype) == CPU_SUBTYPE_ARM64_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 ) {
@@ -2680,66 +2189,6 @@
#endif // CPU_SUBTYPES_SUPPORTED
-
-//
-// Validate the fat_header and fat_arch array:
-//
-// 1) arch count would not cause array to extend past 4096 byte read buffer
-// 2) no slice overlaps the fat_header and arch array
-// 3) arch list does not contain duplicate cputype/cpusubtype tuples
-// 4) arch list does not have two overlapping slices.
-//
-static bool fatValidate(const fat_header* fh)
-{
- if ( fh->magic != OSSwapBigToHostInt32(FAT_MAGIC) )
- return false;
-
- // since only first 4096 bytes of file read, we can only handle up to 204 slices.
- const uint32_t sliceCount = OSSwapBigToHostInt32(fh->nfat_arch);
- if ( sliceCount > 204 )
- return false;
-
- // compare all slices looking for conflicts
- const fat_arch* archs = (fat_arch*)(((char*)fh)+sizeof(fat_header));
- for (uint32_t i=0; i < sliceCount; ++i) {
- uint32_t i_offset = OSSwapBigToHostInt32(archs[i].offset);
- uint32_t i_size = OSSwapBigToHostInt32(archs[i].size);
- uint32_t i_cputype = OSSwapBigToHostInt32(archs[i].cputype);
- uint32_t i_cpusubtype = OSSwapBigToHostInt32(archs[i].cpusubtype);
- uint32_t i_end = i_offset + i_size;
- // slice cannot overlap with header
- if ( i_offset < 4096 )
- return false;
- // slice size cannot overflow
- if ( i_end < i_offset )
- return false;
- for (uint32_t j=i+1; j < sliceCount; ++j) {
- uint32_t j_offset = OSSwapBigToHostInt32(archs[j].offset);
- uint32_t j_size = OSSwapBigToHostInt32(archs[j].size);
- uint32_t j_cputype = OSSwapBigToHostInt32(archs[j].cputype);
- uint32_t j_cpusubtype = OSSwapBigToHostInt32(archs[j].cpusubtype);
- uint32_t j_end = j_offset + j_size;
- // duplicate slices types not allowed
- if ( (i_cputype == j_cputype) && (i_cpusubtype == j_cpusubtype) )
- return false;
- // slice size cannot overflow
- if ( j_end < j_offset )
- return false;
- // check for overlap of slices
- if ( i_offset <= j_offset ) {
- if ( j_offset < i_end )
- return false; // j overlaps end of i
- }
- else {
- // j overlaps end of i
- if ( i_offset < j_end )
- return false; // i overlaps end of j
- }
- }
- }
- return true;
-}
-
//
// A fat file may contain multiple sub-images for the same cpu-type,
// each optimized for a different cpu-sub-type (e.g G3 or G5).
@@ -2747,24 +2196,19 @@
//
static bool fatFindBest(const fat_header* fh, uint64_t* offset, uint64_t* len)
{
- if ( !fatValidate(fh) )
- return false;
-
#if CPU_SUBTYPES_SUPPORTED
// assume all dylibs loaded must have same cpu type as main executable
const cpu_type_t cpu = sMainExecutableMachHeader->cputype;
// We only know the subtype to use if the main executable cpu type matches the host
- if ( cpu == sHostCPU ) {
+ if ( (cpu & CPU_TYPE_MASK) == sHostCPU ) {
// get preference ordered list of subtypes
const cpu_subtype_t* subTypePreferenceList = findCPUSubtypeList(cpu, sHostCPUsubtype);
// use ordered list to find best sub-image in fat file
- if ( subTypePreferenceList != NULL ) {
- if ( fatFindBestFromOrderedList(cpu, subTypePreferenceList, fh, offset, len) )
- return true;
- }
-
+ if ( subTypePreferenceList != NULL )
+ return fatFindBestFromOrderedList(cpu, subTypePreferenceList, fh, offset, len);
+
// if running cpu is not in list, try for an exact match
if ( fatFindExactMatch(cpu, sHostCPUsubtype, fh, offset, len) )
return true;
@@ -2801,7 +2245,7 @@
const mach_header* mh = (mach_header*)firstPage;
if ( mh->magic == sMainExecutableMachHeader->magic ) {
if ( mh->cputype == sMainExecutableMachHeader->cputype ) {
- if ( mh->cputype == sHostCPU ) {
+ if ( (mh->cputype & CPU_TYPE_MASK) == sHostCPU ) {
// get preference ordered list of subtypes that this machine can use
const cpu_subtype_t* subTypePreferenceList = findCPUSubtypeList(mh->cputype, sHostCPUsubtype);
if ( subTypePreferenceList != NULL ) {
@@ -2845,59 +2289,97 @@
// The kernel maps in main executable before dyld gets control. We need to
// make an ImageLoader* for the already mapped in main executable.
-static ImageLoaderMachO* instantiateFromLoadedImage(const macho_header* mh, uintptr_t slide, const char* path)
+static ImageLoader* instantiateFromLoadedImage(const macho_header* mh, uintptr_t slide, const char* path)
{
// try mach-o loader
if ( isCompatibleMachO((const uint8_t*)mh, path) ) {
ImageLoader* image = ImageLoaderMachO::instantiateMainExecutable(mh, slide, path, gLinkContext);
addImage(image);
- return (ImageLoaderMachO*)image;
+ return image;
}
throw "main executable not a known format";
}
-#if SUPPORT_ACCELERATE_TABLES
-static bool dylibsCanOverrideCache()
-{
- if ( !dyld3::internalInstall() )
- return false;
- return ( (sSharedCacheLoadInfo.loadAddress != nullptr) && (sSharedCacheLoadInfo.loadAddress->header.cacheType == kDyldSharedCacheTypeDevelopment) );
-}
-#endif
-
-const void* imMemorySharedCacheHeader()
-{
- return sSharedCacheLoadInfo.loadAddress;
-}
-
-
-const char* getStandardSharedCacheFilePath()
-{
- if ( sSharedCacheLoadInfo.loadAddress != nullptr )
- return sSharedCacheLoadInfo.path;
- else
- return nullptr;
-}
-
-
+
+#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)
{
- dyld3::SharedCacheFindDylibResults results;
- if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo, path, &results) ) {
- *mh = (macho_header*)results.mhInCache;
- *pathInCache = results.pathInCache;
- *slide = results.slideInCache;
- return true;
+ if ( sSharedCache != NULL ) {
+#if __MAC_OS_X_VERSION_MIN_REQUIRED
+ // Mac OS X always requires inode/mtime to valid cache
+ // if stat() not done yet, do it now
+ 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
+
+ // walk shared cache to see if there is a cached image that matches the inode/mtime/path desired
+ 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
+ // just check path
+ const char* aPath = (char*)sSharedCache + p->pathFileOffset;
+ if ( cacheHasHashInfo && (p->inode != hash) )
+ continue;
+ if ( strcmp(path, aPath) == 0 ) {
+ // found image in cache
+ *mh = (macho_header*)(p->address+sSharedCacheSlide);
+ *pathInCache = aPath;
+ *slide = sSharedCacheSlide;
+ return true;
+ }
+#elif __MAC_OS_X_VERSION_MIN_REQUIRED
+ // check mtime and inode first because it is fast
+ bool inodeMatch = ( ((time_t)p->modTime == stat_buf->st_mtime) && ((ino_t)p->inode == stat_buf->st_ino) );
+ if ( searchByPath || sSharedCacheIgnoreInodeAndTimeStamp || inodeMatch ) {
+ // mod-time and inode match an image in the shared cache, now check path
+ const char* aPath = (char*)sSharedCache + p->pathFileOffset;
+ bool cacheHit = (strcmp(path, aPath) == 0);
+ if ( inodeMatch && !cacheHit ) {
+ // path does not match install name of dylib in cache, but inode and mtime does match
+ // perhaps path is a symlink to the cached dylib
+ 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 ) {
+ // found image in cache, return info
+ *mh = (macho_header*)(p->address+sSharedCacheSlide);
+ //dyld::log("findInSharedCacheImage(), mh=%p, p->address=0x%0llX, slid=0x%0lX, path=%p\n",
+ // *mh, p->address, sSharedCacheSlide, aPath);
+ *pathInCache = aPath;
+ *slide = sSharedCacheSlide;
+ return true;
+ }
+ }
+#endif
+ }
}
return false;
}
bool inSharedCache(const char* path)
{
- return dyld3::pathIsInSharedCacheImage(sSharedCacheLoadInfo, 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)
{
@@ -2938,116 +2420,31 @@
}
#if TARGET_IPHONE_SIMULATOR
-static bool isSimulatorBinary(const uint8_t* firstPages, const char* path)
-{
- const macho_header* mh = (macho_header*)firstPages;
+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 load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
- const load_command* const cmdsEnd = (load_command*)((char*)cmds + mh->sizeofcmds);
+ 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) {
- #if TARGET_OS_WATCH
+ case LC_VERSION_MIN_IPHONEOS:
+ case LC_VERSION_MIN_TVOS:
case LC_VERSION_MIN_WATCHOS:
return true;
- #elif TARGET_OS_TV
- case LC_VERSION_MIN_TVOS:
- return true;
- #elif TARGET_OS_IOS
- case LC_VERSION_MIN_IPHONEOS:
- return true;
- #endif
case LC_VERSION_MIN_MACOSX:
// grandfather in a few libSystem dylibs
- if ((strcmp(path, "/usr/lib/system/libsystem_kernel.dylib") == 0) ||
- (strcmp(path, "/usr/lib/system/libsystem_platform.dylib") == 0) ||
- (strcmp(path, "/usr/lib/system/libsystem_pthread.dylib") == 0) ||
- (strcmp(path, "/usr/lib/system/libsystem_platform_debug.dylib") == 0) ||
- (strcmp(path, "/usr/lib/system/libsystem_pthread_debug.dylib") == 0))
- return true;
+ if (strstr(path, "/usr/lib/system") || strstr(path, "/usr/lib/libSystem"))
+ return true;
return false;
- case LC_BUILD_VERSION:
- {
- // Same logic as above, but for LC_BUILD_VERSION instead of legacy load commands
- const struct build_version_command* buildVersionCmd = (build_version_command*)cmd;
- switch(buildVersionCmd->platform) {
- case PLATFORM_IOSSIMULATOR:
- case PLATFORM_TVOSSIMULATOR:
- case PLATFORM_WATCHOSSIMULATOR:
- case PLATFORM_WATCHOS:
- return true;
- #if TARGET_OS_IOSMAC
- case 6:
- return true;
- #endif
- case PLATFORM_MACOS:
- if ((strcmp(path, "/usr/lib/system/libsystem_kernel.dylib") == 0) ||
- (strcmp(path, "/usr/lib/system/libsystem_platform.dylib") == 0) ||
- (strcmp(path, "/usr/lib/system/libsystem_pthread.dylib") == 0) ||
- (strcmp(path, "/usr/lib/system/libsystem_platform_debug.dylib") == 0) ||
- (strcmp(path, "/usr/lib/system/libsystem_pthread_debug.dylib") == 0))
- return true;
- }
- }
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
- if ( cmd > cmdsEnd )
- return false;
+ if ( cmd > cmdsReadEnd )
+ return true;
}
return false;
}
-#endif
-
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
-static bool iOSMacWhiteListed(const char* path)
-{
- static char* whiteListBuffer = nullptr;
- static size_t whiteListSize = 0;
- static bool tried = false;
- if ( !tried ) {
- // only try to map file once
- whiteListBuffer = (char*)mapFileReadOnly("/System/iOSSupport/dyld/macOS-whitelist.txt", whiteListSize);
- tried = true;
- }
- __block bool result = false;
- if ( whiteListBuffer != nullptr ) {
- dyld3::forEachLineInFile(whiteListBuffer, whiteListSize, ^(const char* line, bool& stop) {
- // lines in the file are prefixes. Any path that starts with one of these lines is allowed to be unzippered
- size_t lineLen = strlen(line);
- if ( (*line == '/') && strncmp(line, path, lineLen) == 0 ) {
- result = true;
- stop = true;
- }
- });
- }
- return result;
-}
-
-static bool iOSMacBlackListed(const char* path)
-{
- static char* blackListBuffer = nullptr;
- static size_t blackListSize = 0;
- static bool tried = false;
- if ( !tried ) {
- // only try to map file once
- blackListBuffer = (char*)mapFileReadOnly("/System/iOSSupport/dyld/macOS-blacklist.txt", blackListSize);
- tried = true;
- }
- __block bool result = false;
- if ( blackListBuffer != nullptr ) {
- dyld3::forEachLineInFile(blackListBuffer, blackListSize, ^(const char* line, bool& stop) {
- // lines in the file are prefixes. Any path that starts with one of these lines is allowed to be unzippered
- size_t lineLen = strlen(line);
- if ( (*line == '/') && strncmp(line, path, lineLen) == 0 ) {
- result = true;
- stop = true;
- }
- });
- }
- return result;
-}
-
-
#endif
// map in file and instantiate an ImageLoader
@@ -3061,31 +2458,27 @@
if ( (stat_buf.st_mode & S_IFMT) != S_IFREG )
throw "not a file";
- uint8_t firstPages[MAX_MACH_O_HEADER_AND_LOAD_COMMANDS_SIZE];
- uint8_t *firstPagesPtr = firstPages;
+ uint8_t firstPage[4096];
bool shortPage = false;
// min mach-o file is 4K
if ( fileLength < 4096 ) {
- if ( pread(fd, firstPages, (size_t)fileLength, 0) != (ssize_t)fileLength )
+ if ( pread(fd, firstPage, fileLength, 0) != (ssize_t)fileLength )
throwf("pread of short file failed: %d", errno);
shortPage = true;
}
else {
- // optimistically read only first 4KB
- if ( pread(fd, firstPages, 4096, 0) != 4096 )
+ if ( pread(fd, firstPage, 4096,0) != 4096 )
throwf("pread of first 4K failed: %d", errno);
}
// if fat wrapper, find usable sub-file
- const fat_header* fileStartAsFat = (fat_header*)firstPages;
+ const fat_header* fileStartAsFat = (fat_header*)firstPage;
if ( fileStartAsFat->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) {
- if ( OSSwapBigToHostInt32(fileStartAsFat->nfat_arch) > ((4096 - sizeof(fat_header)) / sizeof(fat_arch)) )
- throwf("fat header too large: %u entries", OSSwapBigToHostInt32(fileStartAsFat->nfat_arch));
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, firstPages, 4096, fileOffset) != 4096)
+ if (pread(fd, firstPage, 4096, fileOffset) != 4096)
throwf("pread of fat file failed: %d", errno);
}
else {
@@ -3096,12 +2489,10 @@
// try mach-o loader
if ( shortPage )
throw "file too short";
-
- if ( isCompatibleMachO(firstPages, path) ) {
+ if ( isCompatibleMachO(firstPage, path) ) {
// only MH_BUNDLE, MH_DYLIB, and some MH_EXECUTE can be dynamically loaded
- const mach_header* mh = (mach_header*)firstPages;
- switch ( mh->filetype ) {
+ switch ( ((mach_header*)firstPage)->filetype ) {
case MH_EXECUTE:
case MH_DYLIB:
case MH_BUNDLE:
@@ -3110,54 +2501,19 @@
throw "mach-o, but wrong filetype";
}
- uint32_t headerAndLoadCommandsSize = sizeof(macho_header) + mh->sizeofcmds;
- if ( headerAndLoadCommandsSize > MAX_MACH_O_HEADER_AND_LOAD_COMMANDS_SIZE )
- throwf("malformed mach-o: load commands size (%u) > %u", headerAndLoadCommandsSize, MAX_MACH_O_HEADER_AND_LOAD_COMMANDS_SIZE);
-
- if ( headerAndLoadCommandsSize > fileLength )
- dyld::throwf("malformed mach-o: load commands size (%u) > mach-o file size (%llu)", headerAndLoadCommandsSize, fileLength);
-
- if ( headerAndLoadCommandsSize > 4096 ) {
- // read more pages
- unsigned readAmount = headerAndLoadCommandsSize - 4096;
- if ( pread(fd, &firstPages[4096], readAmount, fileOffset+4096) != readAmount )
- throwf("pread of extra load commands past 4KB failed: %d", errno);
- }
-
#if TARGET_IPHONE_SIMULATOR
+ #if TARGET_OS_WATCH || TARGET_OS_TV
+ // disable error during bring up of these simulators
+ #else
// <rdar://problem/14168872> dyld_sim should restrict loading osx binaries
- if ( !isSimulatorBinary(firstPages, path) ) {
- #if TARGET_OS_WATCH
- throw "mach-o, but not built for watchOS simulator";
- #elif TARGET_OS_TV
- throw "mach-o, but not built for tvOS simulator";
- #else
+ if ( !isSimulatorBinary(firstPage, path) ) {
throw "mach-o, but not built for iOS simulator";
+ }
#endif
- }
-#endif
-
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- if ( gLinkContext.marzipan ) {
- const dyld3::MachOFile* mf = (dyld3::MachOFile*)firstPages;
- bool isiOSMacBinary = mf->supportsPlatform(dyld3::Platform::iOSMac) || iOSMacWhiteListed(path);
- bool isProhibitedMacOSBinary = !isiOSMacBinary && iOSMacBlackListed(path);
- if ( (context.enforceIOSMac && !isiOSMacBinary) || isProhibitedMacOSBinary ) {
- throw "mach-o, but not built for iOSMac";
- }
- }
-#endif
-
-#if __arm64e__
- if ( (sMainExecutableMachHeader->cpusubtype == CPU_SUBTYPE_ARM64_E) && (mh->cpusubtype != CPU_SUBTYPE_ARM64_E) )
- throw "arm64 dylibs cannot be loaded into arm64e processes";
-#endif
- ImageLoader* image = nullptr;
- {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_MAP_IMAGE, path, 0, 0);
- image = ImageLoaderMachO::instantiateFromFile(path, fd, firstPagesPtr, headerAndLoadCommandsSize, fileOffset, fileLength, stat_buf, gLinkContext);
- timer.setData4((uint64_t)image->machHeader());
- }
+#endif
+
+ // instantiate an image
+ ImageLoader* image = ImageLoaderMachO::instantiateFromFile(path, fd, firstPage, fileOffset, fileLength, stat_buf, gLinkContext);
// validate
return checkandAddImage(image, context);
@@ -3167,7 +2523,7 @@
// throw error about what was found
- switch (*(uint32_t*)firstPages) {
+ switch (*(uint32_t*)firstPage) {
case MH_MAGIC:
case MH_CIGAM:
case MH_MAGIC_64:
@@ -3175,7 +2531,7 @@
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",
- firstPages[0], firstPages[1], firstPages[2], firstPages[3], firstPages[4], firstPages[5], firstPages[6],firstPages[7]);
+ firstPage[0], firstPage[1], firstPage[2], firstPage[3], firstPage[4], firstPage[5], firstPage[6],firstPage[7]);
}
}
@@ -3191,11 +2547,7 @@
if ( file.getFileDescriptor() == -1 ) {
int err = errno;
if ( err != ENOENT ) {
- const char* newMsg;
- if ( (err == EPERM) && sandboxBlockedOpen(path) )
- newMsg = dyld::mkstringf("file system sandbox blocked open() of '%s'", path);
- else
- newMsg = dyld::mkstringf("%s: open() failed with errno=%d", path, err);
+ const char* newMsg = dyld::mkstringf("%s: open() failed with errno=%d", path, err);
exceptions->push_back(newMsg);
}
return NULL;
@@ -3213,127 +2565,134 @@
}
+#if __MAC_OS_X_VERSION_MIN_REQUIRED
+static ImageLoader* loadPhase5load(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
+{
+ //dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
+ ImageLoader* image = NULL;
+
+ // just return NULL if file not found, but record any other errors
+ 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;
+ }
+
+ // in case image was renamed or found via symlinks, check for inode match
+ image = findLoadedImage(stat_buf);
+ if ( image != NULL )
+ return image;
+
+ // do nothing if not already loaded and if RTLD_NOLOAD or NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED
+ if ( context.dontLoad )
+ return NULL;
+
+#if DYLD_SHARED_CACHE_SUPPORT
+ // see if this image is in shared cache
+ 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
+ // file exists and is not in dyld shared cache, so open it
+ 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 ) {
+ // in case image was renamed or found via symlinks, check for inode match
+ image = findLoadedImage(*stat_buf);
+ if ( image != NULL ) {
+ *imageFound = true;
+ return image;
+ }
+ // do nothing if not already loaded and if RTLD_NOLOAD
+ 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;
+}
// try to open file
-static ImageLoader* loadPhase5load(const char* path, const char* orgPath, const LoadContext& context, unsigned& cacheIndex, std::vector<const char*>* exceptions)
+static ImageLoader* loadPhase5load(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
//dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
-#if SUPPORT_ACCELERATE_TABLES
- if ( sAllCacheImagesProxy != NULL ) {
- if ( sAllCacheImagesProxy->hasDylib(path, &cacheIndex) )
- return sAllCacheImagesProxy;
- }
-#endif
-#if TARGET_IPHONE_SIMULATOR
- // in simulators, 'path' has DYLD_ROOT_PATH prepended, but cache index does not have the prefix, so use orgPath
- const char* pathToFindInCache = orgPath;
-#else
- const char* pathToFindInCache = path;
-#endif
- uint statErrNo;
- struct stat statBuf;
- bool didStat = false;
- bool existsOnDisk;
- dyld3::SharedCacheFindDylibResults shareCacheResults;
- shareCacheResults.image = nullptr;
- if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo, pathToFindInCache, &shareCacheResults) ) {
+ struct stat stat_buf;
+ bool imageFound;
+ int statErrNo;
+ ImageLoader* image;
+#if DYLD_SHARED_CACHE_SUPPORT
+ if ( sDylibsOverrideCache ) {
+ // flag is set that allows installed framework roots to override dyld shared cache
+ image = loadPhase5stat(path, context, &stat_buf, &statErrNo, &imageFound, exceptions);
+ if ( imageFound )
+ return image;
+ }
+ // see if this image is in shared cache
+ const macho_header* mhInCache;
+ const char* pathInCache;
+ long slideInCache;
+ if ( findInSharedCacheImage(path, true, NULL, &mhInCache, &pathInCache, &slideInCache) ) {
// see if this image in the cache was already loaded via a different path
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); ++it) {
ImageLoader* anImage = *it;
- if ( (const mach_header*)anImage->machHeader() == shareCacheResults.mhInCache )
+ if ( (const macho_header*)anImage->machHeader() == mhInCache )
return anImage;
}
- // if RTLD_NOLOAD, do nothing if not already loaded
- if ( context.dontLoad ) {
- // <rdar://33412890> possible that there is an override of cache
- if ( my_stat(path, &statBuf) == 0 ) {
- ImageLoader* imageLoader = findLoadedImage(statBuf);
- if ( imageLoader != NULL )
- return imageLoader;
- }
- return NULL;
- }
- bool useCache = false;
- if ( shareCacheResults.image == nullptr ) {
- // HACK to support old caches
- existsOnDisk = ( my_stat(path, &statBuf) == 0 );
- didStat = true;
- statErrNo = errno;
- useCache = !existsOnDisk;
- }
- else {
- // <rdar://problem/7014995> zero out stat buffer so mtime, etc are zero for items from the shared cache
- bzero(&statBuf, sizeof(statBuf));
- if ( shareCacheResults.image->overridableDylib() ) {
- existsOnDisk = ( my_stat(path, &statBuf) == 0 );
- didStat = true;
- statErrNo = errno;
- if ( sSharedCacheLoadInfo.loadAddress->header.dylibsExpectedOnDisk ) {
- uint64_t expectedINode;
- uint64_t expectedMtime;
- if ( shareCacheResults.image->hasFileModTimeAndInode(expectedINode, expectedMtime) ) {
- if ( (expectedMtime == statBuf.st_mtime) && (expectedINode == statBuf.st_ino) )
- useCache = true;
- }
- }
- else {
- if ( !existsOnDisk )
- useCache = true;
- }
- }
- else {
- useCache = true;
- }
- }
- if ( useCache ) {
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- if ( gLinkContext.marzipan ) {
- const dyld3::MachOFile* mf = (dyld3::MachOFile*)shareCacheResults.mhInCache;
- bool isiOSMacBinary = mf->supportsPlatform(dyld3::Platform::iOSMac) || iOSMacWhiteListed(path);
- bool isProhibitedMacOSBinary = !isiOSMacBinary && iOSMacBlackListed(path);
- if ( (context.enforceIOSMac && !isiOSMacBinary) || isProhibitedMacOSBinary ) {
- throw "mach-o, but not built for iOSMac";
- }
- }
-#endif
- ImageLoader* imageLoader = ImageLoaderMachO::instantiateFromCache((macho_header*)shareCacheResults.mhInCache, shareCacheResults.pathInCache, shareCacheResults.slideInCache, statBuf, gLinkContext);
- return checkandAddImage(imageLoader, context);
- }
- }
-
- // not in cache or cache not usable
- if ( !didStat ) {
- existsOnDisk = ( my_stat(path, &statBuf) == 0 );
- statErrNo = errno;
- }
- if ( existsOnDisk ) {
- // in case image was renamed or found via symlinks, check for inode match
- ImageLoader* imageLoader = findLoadedImage(statBuf);
- if ( imageLoader != NULL )
- return imageLoader;
// do nothing if not already loaded and if RTLD_NOLOAD
if ( context.dontLoad )
return NULL;
- // try opening file
- imageLoader = loadPhase5open(path, context, statBuf, exceptions);
- if ( imageLoader != NULL ) {
- if ( shareCacheResults.image != nullptr ) {
- // if image was found in cache, but is overridden by a newer file on disk, record what the image overrides
- imageLoader->setOverridesCachedDylib(shareCacheResults.image->imageNum());
- }
- return imageLoader;
- }
- }
-
+ // nope, so instantiate a new image from dyld shared cache
+ // <rdar://problem/7014995> zero out stat buffer so mtime, etc are zero for items from the shared cache
+ bzero(&stat_buf, sizeof(stat_buf));
+ image = ImageLoaderMachO::instantiateFromCache(mhInCache, pathInCache, slideInCache, stat_buf, gLinkContext);
+ return checkandAddImage(image, context);
+ }
+
+ if ( !sDylibsOverrideCache ) {
+ // flag is not set, and not in cache to try opening it
+ 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
// just return NULL if file not found, but record any other errors
if ( (statErrNo != ENOENT) && (statErrNo != 0) ) {
- if ( (statErrNo == EPERM) && sandboxBlockedStat(path) )
- exceptions->push_back(dyld::mkstringf("%s: file system sandbox blocked stat()", path));
- else
- exceptions->push_back(dyld::mkstringf("%s: stat() failed with errno=%d", path, statErrNo));
+ exceptions->push_back(dyld::mkstringf("%s: stat() failed with errno=%d", path, statErrNo));
}
return NULL;
}
+#endif // __IPHONE_OS_VERSION_MIN_REQUIRED
+
// look for path match with existing loaded images
static ImageLoader* loadPhase5check(const char* path, const char* orgPath, const LoadContext& context)
@@ -3381,7 +2740,7 @@
// open or check existing
-static ImageLoader* loadPhase5(const char* path, const char* orgPath, const LoadContext& context, unsigned& cacheIndex, std::vector<const char*>* exceptions)
+static ImageLoader* loadPhase5(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
//dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
@@ -3394,49 +2753,39 @@
}
if ( exceptions != NULL )
- return loadPhase5load(path, orgPath, context, cacheIndex, exceptions);
+ return loadPhase5load(path, orgPath, context, exceptions);
else
return loadPhase5check(path, orgPath, context);
}
// try with and without image suffix
-static ImageLoader* loadPhase4(const char* path, const char* orgPath, const LoadContext& context, unsigned& cacheIndex, std::vector<const char*>* exceptions)
+static ImageLoader* loadPhase4(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
//dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
ImageLoader* image = NULL;
- if ( gLinkContext.imageSuffix != NULL ) {
- for (const char* const* suffix=gLinkContext.imageSuffix; *suffix != NULL; ++suffix) {
- char pathWithSuffix[strlen(path)+strlen(*suffix)+2];
- ImageLoader::addSuffix(path, *suffix, pathWithSuffix);
- image = loadPhase5(pathWithSuffix, orgPath, context, cacheIndex, exceptions);
- if ( image != NULL )
- break;
- }
- if ( image != NULL ) {
- // if original path is in the dyld cache, then mark this one found as an override
- dyld3::SharedCacheFindDylibResults shareCacheResults;
- if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo, path, &shareCacheResults) && (shareCacheResults.image != nullptr) )
- image->setOverridesCachedDylib(shareCacheResults.image->imageNum());
- }
+ 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, cacheIndex, exceptions);
+ 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[],
- unsigned& cacheIndex, std::vector<const char*>* exceptions); // forward reference
+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); // forward reference
// expand @ variables
-static ImageLoader* loadPhase3(const char* path, const char* orgPath, const LoadContext& context, unsigned& cacheIndex, std::vector<const char*>* exceptions)
+static ImageLoader* loadPhase3(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
//dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
ImageLoader* image = NULL;
if ( strncmp(path, "@executable_path/", 17) == 0 ) {
// executable_path cannot be in used in any binary in a setuid process rdar://problem/4589305
- if ( !gLinkContext.allowAtPaths )
+ if ( sProcessIsRestricted && !sProcessRequiresLibraryValidation )
throwf("unsafe use of @executable_path in %s with restricted binary", context.origin);
// handle @executable_path path prefix
const char* executablePath = sExecPath;
@@ -3447,7 +2796,7 @@
strcpy(&addPoint[1], &path[17]);
else
strcpy(newPath, &path[17]);
- image = loadPhase4(newPath, orgPath, context, cacheIndex, exceptions);
+ image = loadPhase4(newPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
@@ -3456,19 +2805,19 @@
if ( realpath(sExecPath, resolvedPath) != NULL ) {
char newRealPath[strlen(resolvedPath) + strlen(path)];
strcpy(newRealPath, resolvedPath);
- addPoint = strrchr(newRealPath,'/');
+ char* addPoint = strrchr(newRealPath,'/');
if ( addPoint != NULL )
strcpy(&addPoint[1], &path[17]);
else
strcpy(newRealPath, &path[17]);
- image = loadPhase4(newRealPath, orgPath, context, cacheIndex, exceptions);
+ image = loadPhase4(newRealPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
}
}
else if ( (strncmp(path, "@loader_path/", 13) == 0) && (context.origin != NULL) ) {
// @loader_path cannot be used from the main executable of a setuid process rdar://problem/4589305
- if ( !gLinkContext.allowAtPaths && (strcmp(context.origin, sExecPath) == 0) )
+ if ( sProcessIsRestricted && (strcmp(context.origin, sExecPath) == 0) && !sProcessRequiresLibraryValidation )
throwf("unsafe use of @loader_path in %s with restricted binary", context.origin);
// handle @loader_path path prefix
char newPath[strlen(context.origin) + strlen(path)];
@@ -3478,7 +2827,7 @@
strcpy(&addPoint[1], &path[13]);
else
strcpy(newPath, &path[13]);
- image = loadPhase4(newPath, orgPath, context, cacheIndex, exceptions);
+ image = loadPhase4(newPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
@@ -3487,12 +2836,12 @@
if ( realpath(context.origin, resolvedPath) != NULL ) {
char newRealPath[strlen(resolvedPath) + strlen(path)];
strcpy(newRealPath, resolvedPath);
- addPoint = strrchr(newRealPath,'/');
+ char* addPoint = strrchr(newRealPath,'/');
if ( addPoint != NULL )
strcpy(&addPoint[1], &path[13]);
else
strcpy(newRealPath, &path[13]);
- image = loadPhase4(newRealPath, orgPath, context, cacheIndex, exceptions);
+ image = loadPhase4(newRealPath, orgPath, context, exceptions);
if ( image != NULL )
return image;
}
@@ -3506,10 +2855,9 @@
const char* anRPath = *it;
char newPath[strlen(anRPath) + strlen(trailingPath)+2];
strcpy(newPath, anRPath);
- if ( newPath[strlen(newPath)-1] != '/' )
- strcat(newPath, "/");
+ strcat(newPath, "/");
strcat(newPath, trailingPath);
- image = loadPhase4(newPath, orgPath, context, cacheIndex, exceptions);
+ 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);
@@ -3524,7 +2872,7 @@
// substitute @rpath with LD_LIBRARY_PATH
if ( sEnv.LD_LIBRARY_PATH != NULL ) {
- image = loadPhase2(trailingPath, orgPath, context, NULL, sEnv.LD_LIBRARY_PATH, cacheIndex, exceptions);
+ image = loadPhase2(trailingPath, orgPath, context, NULL, sEnv.LD_LIBRARY_PATH, exceptions);
if ( image != NULL )
return image;
}
@@ -3533,35 +2881,18 @@
if ( (exceptions != NULL) && (trailingPath != path) )
return NULL;
}
- else if ( !gLinkContext.allowEnvVarsPath && (path[0] != '/' ) ) {
+ else if (sProcessIsRestricted && (path[0] != '/' ) && !sProcessRequiresLibraryValidation) {
throwf("unsafe use of relative rpath %s in %s with restricted binary", path, context.origin);
}
-
- return loadPhase4(path, orgPath, context, cacheIndex, exceptions);
-}
-
-static ImageLoader* loadPhase2cache(const char* path, const char *orgPath, const LoadContext& context, unsigned& cacheIndex, std::vector<const char*>* exceptions) {
- ImageLoader* image = NULL;
-#if !TARGET_IPHONE_SIMULATOR
- if ( exceptions != NULL) {
- char resolvedPath[PATH_MAX];
- realpath(path, resolvedPath);
- int myerr = errno;
- // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
- if ( (myerr == ENOENT) || (myerr == 0) )
- {
- image = loadPhase4(resolvedPath, orgPath, context, cacheIndex, exceptions);
- }
- }
-#endif
- return image;
+
+ return loadPhase4(path, orgPath, context, exceptions);
}
// try search paths
-static ImageLoader* loadPhase2(const char* path, const char* orgPath, const LoadContext& context,
+static ImageLoader* loadPhase2(const char* path, const char* orgPath, const LoadContext& context,
const char* const frameworkPaths[], const char* const libraryPaths[],
- unsigned& cacheIndex, std::vector<const char*>* exceptions)
+ std::vector<const char*>* exceptions)
{
//dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
ImageLoader* image = NULL;
@@ -3575,17 +2906,9 @@
strcat(npath, "/");
strcat(npath, frameworkPartialPath);
//dyld::log("dyld: fallback framework path used: %s() -> loadPhase4(\"%s\", ...)\n", __func__, npath);
- image = loadPhase4(npath, orgPath, context, cacheIndex, exceptions);
- // Look in the cache if appropriate
- if ( image == NULL)
- image = loadPhase2cache(npath, orgPath, context, cacheIndex, exceptions);
- if ( image != NULL ) {
- // if original path is in the dyld cache, then mark this one found as an override
- dyld3::SharedCacheFindDylibResults shareCacheResults;
- if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo, path, &shareCacheResults) && (shareCacheResults.image != nullptr) )
- image->setOverridesCachedDylib(shareCacheResults.image->imageNum());
+ image = loadPhase4(npath, orgPath, context, exceptions);
+ if ( image != NULL )
return image;
- }
}
}
}
@@ -3600,44 +2923,36 @@
strcat(libpath, "/");
strcat(libpath, libraryLeafName);
//dyld::log("dyld: fallback library path used: %s() -> loadPhase4(\"%s\", ...)\n", __func__, libpath);
- image = loadPhase4(libpath, orgPath, context, cacheIndex, exceptions);
- // Look in the cache if appropriate
- if ( image == NULL)
- image = loadPhase2cache(libpath, orgPath, context, cacheIndex, exceptions);
- if ( image != NULL ) {
- // if original path is in the dyld cache, then mark this one found as an override
- dyld3::SharedCacheFindDylibResults shareCacheResults;
- if ( dyld3::findInSharedCacheImage(sSharedCacheLoadInfo, path, &shareCacheResults) && (shareCacheResults.image != nullptr) )
- image->setOverridesCachedDylib(shareCacheResults.image->imageNum());
+ image = loadPhase4(libpath, orgPath, context, exceptions);
+ if ( image != NULL )
return image;
- }
}
}
return NULL;
}
// try search overrides and fallbacks
-static ImageLoader* loadPhase1(const char* path, const char* orgPath, const LoadContext& context, unsigned& cacheIndex, std::vector<const char*>* exceptions)
+static ImageLoader* loadPhase1(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
//dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
ImageLoader* image = NULL;
// handle LD_LIBRARY_PATH environment variables that force searching
if ( context.useLdLibraryPath && (sEnv.LD_LIBRARY_PATH != NULL) ) {
- image = loadPhase2(path, orgPath, context, NULL, sEnv.LD_LIBRARY_PATH, cacheIndex,exceptions);
+ image = loadPhase2(path, orgPath, context, NULL, sEnv.LD_LIBRARY_PATH, exceptions);
if ( image != NULL )
return image;
}
// handle DYLD_ environment variables that force searching
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, cacheIndex, exceptions);
+ image = loadPhase2(path, orgPath, context, sEnv.DYLD_FRAMEWORK_PATH, sEnv.DYLD_LIBRARY_PATH, exceptions);
if ( image != NULL )
return image;
}
// try raw path
- image = loadPhase3(path, orgPath, context, cacheIndex, exceptions);
+ image = loadPhase3(path, orgPath, context, exceptions);
if ( image != NULL )
return image;
@@ -3646,7 +2961,7 @@
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, cacheIndex, exceptions);
+ image = loadPhase2(path, orgPath, context, sEnv.DYLD_FALLBACK_FRAMEWORK_PATH, fallbackLibraryPaths, exceptions);
if ( image != NULL )
return image;
}
@@ -3655,60 +2970,35 @@
}
// try root substitutions
-static ImageLoader* loadPhase0(const char* path, const char* orgPath, const LoadContext& context, unsigned& cacheIndex, std::vector<const char*>* exceptions)
+static ImageLoader* loadPhase0(const char* path, const char* orgPath, const LoadContext& context, std::vector<const char*>* exceptions)
{
//dyld::log("%s(%s, %p)\n", __func__ , path, exceptions);
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- // handle macOS dylibs dlopen()ing versioned path which needs to map to flat path in mazipan simulator
- if ( gLinkContext.marzipan && strstr(path, ".framework/Versions/")) {
- uintptr_t sourceOffset = 0;
- uintptr_t destOffset = 0;
- size_t sourceLangth = strlen(path);
- char flatPath[sourceLangth];
- flatPath[0] = 0;
- const char* frameworkBase = NULL;
- while ((frameworkBase = strstr(&path[sourceOffset], ".framework/Versions/"))) {
- uintptr_t foundLength = (frameworkBase - &path[sourceOffset]) + strlen(".framework/") ;
- strlcat(&flatPath[destOffset], &path[sourceOffset], foundLength);
- sourceOffset += foundLength + strlen("Versions/") + 1;
- destOffset += foundLength - 1;
- }
- strlcat(&flatPath[destOffset], &path[sourceOffset], sourceLangth);
- ImageLoader* image = loadPhase0(flatPath, orgPath, context, cacheIndex, exceptions);
- if ( image != NULL )
- return image;
- }
-#endif
-
-#if SUPPORT_ROOT_PATH
// handle DYLD_ROOT_PATH which forces absolute paths to use a new root
if ( (gLinkContext.rootPaths != NULL) && (path[0] == '/') ) {
- for(const char* const* rootPath = gLinkContext.rootPaths; *rootPath != NULL; ++rootPath) {
- size_t rootLen = strlen(*rootPath);
- if ( strncmp(path, *rootPath, rootLen) != 0 ) {
- char newPath[rootLen + strlen(path)+2];
- strcpy(newPath, *rootPath);
- strcat(newPath, path);
- ImageLoader* image = loadPhase1(newPath, orgPath, context, cacheIndex, exceptions);
- if ( image != NULL )
- return image;
- }
- }
- }
-#endif
+ 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;
+ }
+ }
// try raw path
- return loadPhase1(path, orgPath, context, cacheIndex, exceptions);
-}
-
-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;
-}
+ 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
//
// Given all the DYLD_ environment variables, the general case for loading libraries
@@ -3724,23 +3014,21 @@
// the path. Either time, if an image is found, the phases all unwind without checking
// for other paths.
//
-ImageLoader* load(const char* path, const LoadContext& context, unsigned& cacheIndex)
+ImageLoader* load(const char* path, const LoadContext& context)
{
CRSetCrashLogMessage2(path);
const char* orgPath = path;
- cacheIndex = UINT32_MAX;
//dyld::log("%s(%s)\n", __func__ , path);
char realPath[PATH_MAX];
// when DYLD_IMAGE_SUFFIX is in used, do a realpath(), otherwise a load of "Foo.framework/Foo" will not match
- if ( context.useSearchPaths && ( gLinkContext.imageSuffix != NULL && *gLinkContext.imageSuffix != NULL) ) {
+ if ( context.useSearchPaths && ( gLinkContext.imageSuffix != NULL) ) {
if ( realpath(path, realPath) != NULL )
path = realPath;
}
// try all path permutations and check against existing loaded images
-
- ImageLoader* image = loadPhase0(path, orgPath, context, cacheIndex, NULL);
+ ImageLoader* image = loadPhase0(path, orgPath, context, NULL);
if ( image != NULL ) {
CRSetCrashLogMessage2(NULL);
return image;
@@ -3748,11 +3036,33 @@
// try all path permutations and try open() until first success
std::vector<const char*> exceptions;
- image = loadPhase0(path, orgPath, context, cacheIndex, &exceptions);
-#if !TARGET_IPHONE_SIMULATOR
+ image = loadPhase0(path, orgPath, context, &exceptions);
+#if __IPHONE_OS_VERSION_MIN_REQUIRED && DYLD_SHARED_CACHE_SUPPORT && !TARGET_IPHONE_SIMULATOR
// <rdar://problem/16704628> support symlinks on disk to a path in dyld shared cache
- if ( image == NULL)
- image = loadPhase2cache(path, orgPath, context, cacheIndex, &exceptions);
+ if ( (image == NULL) && cacheablePath(path) && !context.dontLoad ) {
+ char resolvedPath[PATH_MAX];
+ realpath(path, resolvedPath);
+ int myerr = errno;
+ // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
+ if ( (myerr == ENOENT) || (myerr == 0) )
+ {
+ // see if this image is in shared cache
+ 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 ) {
@@ -3760,11 +3070,13 @@
for (std::vector<const char*>::iterator it = exceptions.begin(); it != exceptions.end(); ++it) {
free((void*)(*it));
}
+#if DYLD_SHARED_CACHE_SUPPORT
// if loaded image is not from cache, but original path is in cache
// set gSharedCacheOverridden flag to disable some ObjC optimizations
if ( !gSharedCacheOverridden && !image->inSharedCache() && image->isDylib() && cacheablePath(path) && inSharedCache(path) ) {
gSharedCacheOverridden = true;
}
+#endif
return image;
}
else if ( exceptions.size() == 0 ) {
@@ -3793,44 +3105,549 @@
-
+#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)
+{
+ // register code signature blob for whole dyld cache
+ if ( codeSignatureMappingIndex != -1 ) {
+ fsignatures_t siginfo;
+ siginfo.fs_file_start = 0; // cache always starts at beginning of file
+ 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);
+ // <rdar://problem/12891874> don't warn in chrooted case because mapping syscall is about to fail too
+ 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);
+ }
+
+ // remove the shared region sub-map
+ vm_deallocate(mach_task_self(), (vm_address_t)SHARED_REGION_BASE, SHARED_REGION_SIZE);
+
+ // notify gdb or other lurkers that this process is no longer using the shared region
+ dyld::gProcessInfo->processDetachedFromSharedRegion = true;
+
+ // map cache just for this process with mmap()
+ 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;
+ //dyld::log("dyld: mapping address %p with size 0x%08lX\n", mmapAddress, 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 ) {
+ // failed to map some chunk of this shared cache file
+ // clear shared region
+ vm_deallocate(mach_task_self(), (vm_address_t)SHARED_REGION_BASE, SHARED_REGION_SIZE);
+ // go back to not using shared region at all
+ 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 failure
+ return -1;
+ }
+ }
+
+ // update all __DATA pages with slide info
+ 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));
+ //dyld::log("page=%p toc[%d]=%d entries=%p\n", page, i, toc[i], entry);
+ for(int j=0; j < 128; ++j) {
+ uint8_t b = entry[j];
+ //dyld::log(" entry[%d] = 0x%02X\n", j, b);
+ 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;
+ //dyld::log(" *%p was 0x%lX will be 0x%lX\n", p, value, value+sSharedCacheSlide);
+ *p = value + slide;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // succesfully mapped shared cache for just this process
+ gLinkContext.sharedRegionMode = ImageLoader::kUsePrivateSharedRegion;
+
+ return 0;
+}
+
+
+const void* imMemorySharedCacheHeader()
+{
+ return sSharedCache;
+}
+
+const char* getStandardSharedCacheFilePath()
+{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED
+ return IPHONE_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME;
+#else
+ #if __x86_64__
+ if ( sHaswell ) {
+ const char* path2 = MACOSX_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME_H;
+ struct stat statBuf;
+ if ( my_stat(path2, &statBuf) == 0 )
+ return path2;
+ }
+ #endif
+ return MACOSX_DYLD_SHARED_CACHE_DIR DYLD_SHARED_CACHE_BASE_NAME ARCH_NAME;
+#endif
+}
+
+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__
+ // x86_64 has a two memory regions:
+ // 256MB at 0x00007FFF70000000
+ // 1024MB at 0x00007FFF80000000
+ // Some old shared caches have r/w region after rx region, so all regions slide within 1GB range
+ // Newer shared caches have r/w region based at 0x7FFF70000000 and r/o regions at 0x7FFF80000000, so each part has max slide
+ 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;
+
+ // choose new random slide
+ long slide = (arc4random() % space) & (-4096);
+ //dyld::log("rwSlop=0x%0llX, roSlop=0x%0llX\n", rwSlop, roSlop);
+ //dyld::log("space=0x%0llX, slide=0x%0lX\n", space, slide);
+
+ // update mappings
+ for(uint32_t i=0; i < mappingsCount; ++i) {
+ mappings[i].sfm_address += slide;
+ }
+
+ return slide;
+ }
+ // else fall through to handle old style cache
+#endif
+ // get bounds of cache
+ uint64_t lowAddress;
+ uint64_t highAddress;
+ getCacheBounds(mappingsCount, mappings, lowAddress, highAddress);
+
+ // find slop space
+ const uint64_t space = (SHARED_REGION_BASE + SHARED_REGION_SIZE) - highAddress;
+
+ // choose new random slide
+ long slide = dyld_page_trunc(arc4random() % space);
+ //dyld::log("slideSpace=0x%0llX\n", space);
+ //dyld::log("slide=0x%0lX\n", slide);
+
+ // update mappings
+ for(uint32_t i=0; i < mappingsCount; ++i) {
+ mappings[i].sfm_address += slide;
+ }
+
+ return slide;
+}
static void mapSharedCache()
{
- dyld3::SharedCacheOptions opts;
- opts.cacheDirOverride = sSharedCacheOverrideDir;
- opts.forcePrivate = (gLinkContext.sharedRegionMode == ImageLoader::kUsePrivateSharedRegion);
-
-
-#if __x86_64__ && !TARGET_IPHONE_SIMULATOR
- opts.useHaswell = sHaswell;
+ uint64_t cacheBaseAddress = 0;
+ // quick check if a cache is already mapped into shared region
+ if ( _shared_region_check_np(&cacheBaseAddress) == 0 ) {
+ sSharedCache = (dyld_cache_header*)cacheBaseAddress;
+ // if we don't understand the currently mapped shared cache, then ignore
+#if __x86_64__
+ const char* magic = (sHaswell ? ARCH_CACHE_MAGIC_H : ARCH_CACHE_MAGIC);
#else
- opts.useHaswell = false;
-#endif
- opts.verbose = gLinkContext.verboseMapping;
- loadDyldCache(opts, &sSharedCacheLoadInfo);
-
- // update global state
- if ( sSharedCacheLoadInfo.loadAddress != nullptr ) {
- gLinkContext.dyldCache = sSharedCacheLoadInfo.loadAddress;
- dyld::gProcessInfo->processDetachedFromSharedRegion = opts.forcePrivate;
- dyld::gProcessInfo->sharedCacheSlide = sSharedCacheLoadInfo.slide;
- dyld::gProcessInfo->sharedCacheBaseAddress = (unsigned long)sSharedCacheLoadInfo.loadAddress;
- sSharedCacheLoadInfo.loadAddress->getUUID(dyld::gProcessInfo->sharedCacheUUID);
- dyld3::kdebug_trace_dyld_image(DBG_DYLD_UUID_SHARED_CACHE_A, (const uuid_t *)&dyld::gProcessInfo->sharedCacheUUID[0], {0,0}, {{ 0, 0 }}, (const mach_header *)sSharedCacheLoadInfo.loadAddress);
- }
-
-//#if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
-// RAM disk booting does not have shared cache yet
-// Don't make lack of a shared cache fatal in that case
-// if ( sSharedCacheLoadInfo.loadAddress == nullptr ) {
-// if ( sSharedCacheLoadInfo.errorMessage != nullptr )
-// halt(sSharedCacheLoadInfo.errorMessage);
-// else
-// halt("error loading dyld shared cache");
-// }
-//#endif
-}
+ 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;
+ }
+ }
+ // check if cache file is slidable
+ const dyld_cache_header* header = sSharedCache;
+ if ( (header->mappingOffset >= 0x48) && (header->slideInfoSize != 0) ) {
+ // solve for slide by comparing loaded address to address of first region
+ 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;
+ //dyld::log("sSharedCacheSlide=0x%08lX, loadedAddress=%p, preferedLoadAddress=%p\n", sSharedCacheSlide, loadedAddress, preferedLoadAddress);
+ }
+ // if cache has a uuid, copy it
+ if ( header->mappingOffset >= 0x68 ) {
+ memcpy(dyld::gProcessInfo->sharedCacheUUID, header->uuid, 16);
+ }
+ // verbose logging
+ if ( gLinkContext.verboseMapping ) {
+ dyld::log("dyld: re-using existing shared cache mapping\n");
+ }
+ }
+ else {
+#if __i386__ || __x86_64__
+ // <rdar://problem/5925940> Safe Boot should disable dyld shared cache
+ // if we are in safe-boot mode and the cache was not made during this boot cycle,
+ // delete the cache file
+ uint32_t safeBootValue = 0;
+ size_t safeBootValueSize = sizeof(safeBootValue);
+ if ( (sysctlbyname("kern.safeboot", &safeBootValue, &safeBootValueSize, NULL, 0) == 0) && (safeBootValue != 0) ) {
+ // user booted machine in safe-boot mode
+ struct stat dyldCacheStatInfo;
+ // Don't use custom DYLD_SHARED_CACHE_DIR if provided, use standard path
+ 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 the cache file was created before this boot, then throw it away and let it rebuild itself
+ 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
+ // map in shared cache to shared region
+ 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]; // add room for code-sig
+ unsigned int mappingCount = header->mappingCount;
+ int codeSignatureMappingIndex = -1;
+ int readWriteMappingIndex = -1;
+ int readOnlyMappingIndex = -1;
+ // validate that the cache file has not been truncated
+ 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;
+ // rdar://problem/5694507 old update_dyld_shared_cache tool could make a cache file
+ // that is not page aligned, but otherwise ok.
+ 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;
+ }
+ }
+ // if shared cache is code signed, add a mapping for the code signature
+ uint64_t signatureSize = header->codeSignatureSize;
+ // zero size in header means signature runs to end-of-file
+ 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
+ // sanity check that /usr/lib/libSystem.B.dylib stat() info matches cache
+ 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;
+ // check if shared cache contains slid info
+ if ( header->slideInfoSize != 0 ) {
+ // <rdar://problem/8611968> don't slide shared cache if ASLR disabled (main executable didn't slide)
+ if ( sMainExecutable->isPositionIndependentExecutable() && (sMainExecutable->getSlide() == 0) )
+ cacheSlide = 0;
+ else {
+ // generate random slide amount
+ cacheSlide = pickCacheSlide(mappingCount, mappings);
+ slideInfo = (void*)(long)(mappings[readOnlyMappingIndex].sfm_address + (header->slideInfoOffset - mappings[readOnlyMappingIndex].sfm_file_offset));
+ slideInfoSize = header->slideInfoSize;
+ // add VM_PROT_SLIDE bit to __DATA area of cache
+ 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) {
+ // successfully mapped cache into shared region
+ sSharedCache = (dyld_cache_header*)mappings[0].sfm_address;
+ sSharedCacheSlide = cacheSlide;
+ dyld::gProcessInfo->sharedCacheSlide = cacheSlide;
+ //dyld::log("sSharedCache=%p sSharedCacheSlide=0x%08lX\n", sSharedCache, sSharedCacheSlide);
+ // if cache has a uuid, copy it
+ 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");
+ }
+ }
+
+ // remember if dyld loaded at same address as when cache built
+ if ( sSharedCache != NULL ) {
+ gLinkContext.dyldLoadedAtSameAddressNeededBySharedCache = ((uintptr_t)(sSharedCache->dyldBaseAddress) == (uintptr_t)&_mh_dylinker_header);
+ }
+
+ // tell gdb where the shared cache is
+ 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;
+ // only room to tell gdb about first four regions
+ 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 a non-writable and executable region is found in the R/W shared region, then this is __IMPORT segments
+ // This is an old cache. Make writable. dyld no longer supports turn W on and off as it binds
+ 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 ) {
+ // list the code blob
+ dyld_cache_header* header = (dyld_cache_header*)sSharedCache;
+ uint64_t signatureSize = header->codeSignatureSize;
+ // zero size in header means signature runs to end-of-file
+ if ( signatureSize == 0 ) {
+ struct stat stat_buf;
+ // FIXME: need size of cache file actually used
+ 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
+ // check for file that enables dyld shared cache dylibs to be overridden
+ struct stat enableStatBuf;
+ // check file size to determine if correct file is in place.
+ // See <rdar://problem/13591370> Need a way to disable roots without removing /S/L/C/com.apple.dyld/enable...
+ 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
@@ -3854,7 +3671,6 @@
context.mustBeBundle = true;
context.mustBeDylib = false;
context.canBePIE = false;
- context.enforceIOSMac = false;
context.origin = NULL;
context.rpath = NULL;
return loadPhase6(file.getFileDescriptor(), stat_buf, image->getPath(), context);
@@ -3910,45 +3726,9 @@
// call callback with all existing images
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 ) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE, (uint64_t)image->machHeader(), (uint64_t)(*func), 0);
+ if ( image->getState() >= dyld_image_state_bound && image->getState() < dyld_image_state_terminated )
(*func)(image->machHeader(), image->getSlide());
- }
- }
-#if SUPPORT_ACCELERATE_TABLES
- if ( sAllCacheImagesProxy != NULL ) {
- dyld_image_info infos[allImagesCount()+1];
- unsigned cacheCount = sAllCacheImagesProxy->appendImagesToNotify(dyld_image_state_bound, true, infos);
- for (unsigned i=0; i < cacheCount; ++i) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE, (uint64_t)infos[i].imageLoadAddress, (uint64_t)(*func), 0);
- (*func)(infos[i].imageLoadAddress, sSharedCacheLoadInfo.slide);
- }
- }
-#endif
-}
-
-void registerLoadCallback(LoadImageCallback func)
-{
- // now add to list to get notified when any more images are added
- sAddLoadImageCallbacks.push_back(func);
-
- // call callback with all existing images
- for (ImageLoader* image : sAllImages) {
- if ( image->getState() >= dyld_image_state_bound && image->getState() < dyld_image_state_terminated ) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE, (uint64_t)image->machHeader(), (uint64_t)(*func), 0);
- (*func)(image->machHeader(), image->getPath(), !image->neverUnload());
- }
- }
-#if SUPPORT_ACCELERATE_TABLES
- if ( sAllCacheImagesProxy != NULL ) {
- dyld_image_info infos[allImagesCount()+1];
- unsigned cacheCount = sAllCacheImagesProxy->appendImagesToNotify(dyld_image_state_bound, true, infos);
- for (unsigned i=0; i < cacheCount; ++i) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_FUNC_FOR_ADD_IMAGE, (uint64_t)infos[i].imageLoadAddress, (uint64_t)(*func), 0);
- (*func)(infos[i].imageLoadAddress, infos[i].imageFilePath, false);
- }
- }
-#endif
+ }
}
void registerRemoveCallback(ImageCallback func)
@@ -3975,40 +3755,16 @@
return error_string;
}
+
void halt(const char* message)
{
dyld::log("dyld: %s\n", message);
setErrorMessage(message);
- dyld::gProcessInfo->errorMessage = error_string;
- if ( !gLinkContext.startedInitializingMainExecutable )
- dyld::gProcessInfo->terminationFlags = 1;
- else
- dyld::gProcessInfo->terminationFlags = 0;
-
- char payloadBuffer[EXIT_REASON_PAYLOAD_MAX_LEN];
- dyld_abort_payload* payload = (dyld_abort_payload*)payloadBuffer;
- payload->version = 1;
- payload->flags = gLinkContext.startedInitializingMainExecutable ? 0 : 1;
- payload->targetDylibPathOffset = 0;
- payload->clientPathOffset = 0;
- payload->symbolOffset = 0;
- int payloadSize = sizeof(dyld_abort_payload);
-
- if ( dyld::gProcessInfo->errorTargetDylibPath != NULL ) {
- payload->targetDylibPathOffset = payloadSize;
- payloadSize += strlcpy(&payloadBuffer[payloadSize], dyld::gProcessInfo->errorTargetDylibPath, sizeof(payloadBuffer)-payloadSize) + 1;
- }
- if ( dyld::gProcessInfo->errorClientOfDylibPath != NULL ) {
- payload->clientPathOffset = payloadSize;
- payloadSize += strlcpy(&payloadBuffer[payloadSize], dyld::gProcessInfo->errorClientOfDylibPath, sizeof(payloadBuffer)-payloadSize) + 1;
- }
- if ( dyld::gProcessInfo->errorSymbol != NULL ) {
- payload->symbolOffset = payloadSize;
- payloadSize += strlcpy(&payloadBuffer[payloadSize], dyld::gProcessInfo->errorSymbol, sizeof(payloadBuffer)-payloadSize) + 1;
- }
- char truncMessage[EXIT_REASON_USER_DESC_MAX_LEN];
- strlcpy(truncMessage, message, EXIT_REASON_USER_DESC_MAX_LEN);
- abort_with_payload(OS_REASON_DYLD, dyld::gProcessInfo->errorKind ? dyld::gProcessInfo->errorKind : DYLD_EXIT_REASON_OTHER, payloadBuffer, payloadSize, truncMessage, 0);
+ 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,
@@ -4068,20 +3824,6 @@
// save in cache
*imageLoaderCache = dyld::findMappedRange((uintptr_t)imageLoaderCache);
if ( *imageLoaderCache == NULL ) {
-#if SUPPORT_ACCELERATE_TABLES
- if ( sAllCacheImagesProxy != NULL ) {
- const mach_header* mh;
- const char* path;
- unsigned index;
- if ( sAllCacheImagesProxy->addressInCache(imageLoaderCache, &mh, &path, &index) ) {
- result = sAllCacheImagesProxy->bindLazy(lazyBindingInfoOffset, gLinkContext, mh, index);
- if ( result == 0 ) {
- halt("dyld: lazy symbol binding failed for image in dyld shared\n");
- }
- return result;
- }
- }
-#endif
const char* message = "fast lazy binding from unknown image";
dyld::log("dyld: %s\n", message);
halt(message);
@@ -4117,13 +3859,11 @@
}
}
-static bool findExportedSymbol(const char* name, bool onlyInCoalesced, const ImageLoader::Symbol** sym, const ImageLoader** image, ImageLoader::CoalesceNotifier notifier=NULL)
+static bool findExportedSymbol(const char* name, bool onlyInCoalesced, const ImageLoader::Symbol** sym, const ImageLoader** image)
{
// search all images in order
const ImageLoader* firstWeakImage = NULL;
const ImageLoader::Symbol* firstWeakSym = NULL;
- const ImageLoader* firstNonWeakImage = NULL;
- const ImageLoader::Symbol* firstNonWeakSym = NULL;
const size_t imageCount = sAllImages.size();
for(size_t i=0; i < imageCount; ++i) {
ImageLoader* anImage = sAllImages[i];
@@ -4135,41 +3875,22 @@
else if ( i == sInsertedDylibCount )
anImage = sAllImages[0];
}
- //dyld::log("findExportedSymbol(%s) looking at %s\n", name, anImage->getPath());
if ( ! anImage->hasHiddenExports() && (!onlyInCoalesced || anImage->hasCoalescedExports()) ) {
- const ImageLoader* foundInImage;
- *sym = anImage->findExportedSymbol(name, false, &foundInImage);
- //dyld::log("findExportedSymbol(%s) found: sym=%p, anImage=%p, foundInImage=%p\n", name, *sym, anImage, foundInImage /*, (foundInImage ? foundInImage->getPath() : "")*/);
+ *sym = anImage->findExportedSymbol(name, false, image);
if ( *sym != NULL ) {
- if ( notifier && (foundInImage == anImage) )
- notifier(*sym, foundInImage, foundInImage->machHeader());
// if weak definition found, record first one found
- if ( (foundInImage->getExportedSymbolInfo(*sym) & ImageLoader::kWeakDefinition) != 0 ) {
+ if ( ((*image)->getExportedSymbolInfo(*sym) & ImageLoader::kWeakDefinition) != 0 ) {
if ( firstWeakImage == NULL ) {
- firstWeakImage = foundInImage;
+ firstWeakImage = *image;
firstWeakSym = *sym;
}
}
else {
- // found non-weak
- if ( !onlyInCoalesced ) {
- // for flat lookups, return first found
- *image = foundInImage;
- return true;
- }
- if ( firstNonWeakImage == NULL ) {
- firstNonWeakImage = foundInImage;
- firstNonWeakSym = *sym;
- }
+ // found non-weak, so immediately return with it
+ return true;
}
}
}
- }
- if ( firstNonWeakImage != NULL ) {
- // found a weak definition, but no non-weak, so return first weak found
- *sym = firstNonWeakSym;
- *image = firstNonWeakImage;
- return true;
}
if ( firstWeakSym != NULL ) {
// found a weak definition, but no non-weak, so return first weak found
@@ -4177,13 +3898,7 @@
*image = firstWeakImage;
return true;
}
-#if SUPPORT_ACCELERATE_TABLES
- if ( sAllCacheImagesProxy != NULL ) {
- if ( sAllCacheImagesProxy->flatFindSymbol(name, onlyInCoalesced, sym, image, notifier) )
- return true;
- }
-#endif
-
+
return false;
}
@@ -4192,9 +3907,9 @@
return findExportedSymbol(name, false, sym, image);
}
-bool findCoalescedExportedSymbol(const char* name, const ImageLoader::Symbol** sym, const ImageLoader** image, ImageLoader::CoalesceNotifier notifier)
-{
- return findExportedSymbol(name, true, sym, image, notifier);
+bool findCoalescedExportedSymbol(const char* name, const ImageLoader::Symbol** sym, const ImageLoader** image)
+{
+ return findExportedSymbol(name, true, sym, image);
}
@@ -4215,23 +3930,16 @@
return false;
}
-
-unsigned int getCoalescedImages(ImageLoader* images[], unsigned imageIndex[])
+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[count] = *it;
- imageIndex[count] = 0;
+ *images++ = *it;
++count;
}
}
-#if SUPPORT_ACCELERATE_TABLES
- if ( sAllCacheImagesProxy != NULL ) {
- sAllCacheImagesProxy->appendImagesNeedingCoalescing(images, imageIndex, count);
- }
-#endif
return count;
}
@@ -4292,7 +4000,7 @@
// call callback with all existing images
try {
- notifyBatchPartial(state, true, handler, false, false);
+ notifyBatchPartial(state, true, handler);
}
catch (const char* msg) {
// ignore request to abort during registration
@@ -4300,131 +4008,7 @@
}
}
-
-void registerObjCNotifiers(_dyld_objc_notify_mapped mapped, _dyld_objc_notify_init init, _dyld_objc_notify_unmapped unmapped)
-{
- // record functions to call
- sNotifyObjCMapped = mapped;
- sNotifyObjCInit = init;
- sNotifyObjCUnmapped = unmapped;
-
- // call 'mapped' function with all images mapped so far
- try {
- notifyBatchPartial(dyld_image_state_bound, true, NULL, false, true);
- }
- catch (const char* msg) {
- // ignore request to abort during registration
- }
-
- // <rdar://problem/32209809> call 'init' function on all images already init'ed (below libSystem)
- for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
- ImageLoader* image = *it;
- if ( (image->getState() == dyld_image_state_initialized) && image->notifyObjC() ) {
- dyld3::ScopedTimer timer(DBG_DYLD_TIMING_OBJC_INIT, (uint64_t)image->machHeader(), 0, 0);
- (*sNotifyObjCInit)(image->getRealPath(), image->machHeader());
- }
- }
-}
-
-bool sharedCacheUUID(uuid_t uuid)
-{
- if ( sSharedCacheLoadInfo.loadAddress == nullptr )
- return false;
-
- sSharedCacheLoadInfo.loadAddress->getUUID(uuid);
- return true;
-}
-
-#if SUPPORT_ACCELERATE_TABLES
-
-bool dlopenFromCache(const char* path, int mode, void** handle)
-{
- if ( sAllCacheImagesProxy == NULL )
- return false;
- char fallbackPath[PATH_MAX];
- bool result = sAllCacheImagesProxy->dlopenFromCache(gLinkContext, path, mode, handle);
- if ( !result && (strchr(path, '/') == NULL) ) {
- // POSIX says you can call dlopen() with a leaf name (e.g. dlopen("libz.dylb"))
- strcpy(fallbackPath, "/usr/lib/");
- strlcat(fallbackPath, path, PATH_MAX);
- result = sAllCacheImagesProxy->dlopenFromCache(gLinkContext, fallbackPath, mode, handle);
- if ( !result )
- path = fallbackPath;
- }
- if ( !result ) {
- // leaf name could be a symlink
- char resolvedPath[PATH_MAX];
- realpath(path, resolvedPath);
- int realpathErrno = errno;
- // If realpath() resolves to a path which does not exist on disk, errno is set to ENOENT
- if ( (realpathErrno == ENOENT) || (realpathErrno == 0) ) {
- result = sAllCacheImagesProxy->dlopenFromCache(gLinkContext, resolvedPath, mode, handle);
- }
- }
-
- return result;
-}
-
-bool makeCacheHandle(ImageLoader* image, unsigned cacheIndex, int mode, void** result)
-{
- if ( sAllCacheImagesProxy == NULL )
- return false;
- return sAllCacheImagesProxy->makeCacheHandle(gLinkContext, cacheIndex, mode, result);
-}
-
-bool isCacheHandle(void* handle)
-{
- if ( sAllCacheImagesProxy == NULL )
- return false;
- return sAllCacheImagesProxy->isCacheHandle(handle, NULL, NULL);
-}
-
-bool isPathInCache(const char* path)
-{
- if ( sAllCacheImagesProxy == NULL )
- return false;
- unsigned index;
- return sAllCacheImagesProxy->hasDylib(path, &index);
-}
-
-const char* getPathFromIndex(unsigned cacheIndex)
-{
- if ( sAllCacheImagesProxy == NULL )
- return NULL;
- return sAllCacheImagesProxy->getIndexedPath(cacheIndex);
-}
-
-void* dlsymFromCache(void* handle, const char* symName, unsigned index)
-{
- if ( sAllCacheImagesProxy == NULL )
- return NULL;
- return sAllCacheImagesProxy->dlsymFromCache(gLinkContext, handle, symName, index);
-}
-
-bool addressInCache(const void* address, const mach_header** mh, const char** path, unsigned* index)
-{
- if ( sAllCacheImagesProxy == NULL )
- return false;
- unsigned ignore;
- return sAllCacheImagesProxy->addressInCache(address, mh, path, index ? index : &ignore);
-}
-
-bool findUnwindSections(const void* addr, dyld_unwind_sections* info)
-{
- if ( sAllCacheImagesProxy == NULL )
- return false;
- return sAllCacheImagesProxy->findUnwindSections(addr, info);
-}
-
-bool dladdrFromCache(const void* address, Dl_info* info)
-{
- if ( sAllCacheImagesProxy == NULL )
- return false;
- return sAllCacheImagesProxy->dladdrFromCache(address, info);
-}
-#endif
-
-static ImageLoader* libraryLocator(const char* libraryName, bool search, const char* origin, const ImageLoader::RPathChain* rpaths, bool enforceIOSMac, unsigned& cacheIndex)
+static ImageLoader* libraryLocator(const char* libraryName, bool search, const char* origin, const ImageLoader::RPathChain* rpaths)
{
dyld::LoadContext context;
context.useSearchPaths = search;
@@ -4436,10 +4020,9 @@
context.mustBeBundle = false;
context.mustBeDylib = true;
context.canBePIE = false;
- context.enforceIOSMac = enforceIOSMac;
context.origin = origin;
context.rpath = rpaths;
- return load(libraryName, context, cacheIndex);
+ return load(libraryName, context);
}
static const char* basename(const char* path)
@@ -4470,18 +4053,15 @@
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;
-#if SUPPORT_ACCELERATE_TABLES
- gLinkContext.notifySingleFromCache = ¬ifySingleFromCache;
- gLinkContext.getPreInitNotifyHandler= &getPreInitNotifyHandler;
- gLinkContext.getBoundBatchHandler = &getBoundBatchHandler;
-#endif
gLinkContext.bindingOptions = ImageLoader::kBindingNone;
gLinkContext.argc = argc;
gLinkContext.argv = argv;
@@ -4499,7 +4079,7 @@
gLinkContext.dynamicInterposeCount = 0;
gLinkContext.prebindUsage = ImageLoader::kUseAllPrebinding;
#if TARGET_IPHONE_SIMULATOR
- gLinkContext.sharedRegionMode = ImageLoader::kUsePrivateSharedRegion;
+ gLinkContext.sharedRegionMode = ImageLoader::kDontUseSharedRegion;
#else
gLinkContext.sharedRegionMode = ImageLoader::kUseSharedRegion;
#endif
@@ -4512,7 +4092,6 @@
// Its presences means that the binary wants to have DYLD ignore
// DYLD_ environment variables.
//
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
static bool hasRestrictedSegment(const macho_header* mh)
{
const uint32_t cmd_count = mh->ncmds;
@@ -4541,25 +4120,7 @@
return false;
}
-#endif
-
-#if __IPHONE_OS_VERSION_MIN_REQUIRED && !TARGET_IPHONE_SIMULATOR
-static bool isFairPlayEncrypted(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_ENCRYPT_COMMAND ) {
- const encryption_info_command* enc = (encryption_info_command*)cmd;
- return (enc->cryptid != 0);
- }
- cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
- }
-
- return false;
-}
-#endif
+
#if SUPPORT_VERSIONED_PATHS
@@ -4601,6 +4162,7 @@
uint8_t firstPage[4096];
const macho_header* mh = (macho_header*)firstPage;
if ( !readFirstPage(dylibPath, firstPage) ) {
+ #if DYLD_SHARED_CACHE_SUPPORT
// If file cannot be read, check to see if path is in shared cache
const macho_header* mhInCache;
const char* pathInCache;
@@ -4608,6 +4170,9 @@
if ( !findInSharedCacheImage(dylibPath, true, NULL, &mhInCache, &pathInCache, &slideInCache) )
return false;
mh = mhInCache;
+ #else
+ return false;
+ #endif
}
// check mach-o header
@@ -4656,7 +4221,7 @@
}
#endif
-void link(ImageLoader* image, bool forceLazysBound, bool neverUnload, const ImageLoader::RPathChain& loaderRPaths, unsigned cacheIndex)
+void link(ImageLoader* image, bool forceLazysBound, bool neverUnload, const ImageLoader::RPathChain& loaderRPaths)
{
// add to list of known images. This did not happen at creation time for bundles
if ( image->isBundle() && !image->isLinked() )
@@ -4668,12 +4233,7 @@
// process images
try {
- const char* path = image->getPath();
-#if SUPPORT_ACCELERATE_TABLES
- if ( image == sAllCacheImagesProxy )
- path = sAllCacheImagesProxy->getIndexedPath(cacheIndex);
-#endif
- image->link(gLinkContext, forceLazysBound, false, neverUnload, loaderRPaths, path);
+ image->link(gLinkContext, forceLazysBound, false, neverUnload, loaderRPaths);
}
catch (const char* msg) {
garbageCollectImages();
@@ -4685,7 +4245,7 @@
void runInitializers(ImageLoader* image)
{
// do bottom up initialization
- ImageLoader::InitializerTimingList initializerTimes[allImagesCount()];
+ ImageLoader::InitializerTimingList initializerTimes[sAllImages.size()];
initializerTimes[0].count = 0;
image->runInitializers(gLinkContext, initializerTimes[0]);
}
@@ -4728,7 +4288,7 @@
// sweep phase: mark as in-use, images reachable from never-unload or in-use image
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
- if ( (image->dlopenCount() != 0) || image->neverUnload() || (image == sMainExecutable) ) {
+ if ( (image->dlopenCount() != 0) || image->neverUnload() ) {
OSSpinLockLock(&sDynamicReferencesLock);
image->markedUsedRecursive(sDynamicReferences);
OSSpinLockUnlock(&sDynamicReferencesLock);
@@ -4739,42 +4299,42 @@
ImageLoader* deadImages[sAllImages.size()];
unsigned deadCount = 0;
int maxRangeCount = 0;
+ unsigned i = 0;
for (std::vector<ImageLoader*>::iterator it=sAllImages.begin(); it != sAllImages.end(); it++) {
ImageLoader* image = *it;
if ( ! image->isMarkedInUse() ) {
- deadImages[deadCount++] = image;
+ deadImages[i++] = image;
if (gLogAPIs) dyld::log("dlclose(), found unused image %p %s\n", image, image->getShortName());
+ ++deadCount;
maxRangeCount += image->segmentCount();
}
}
// collect phase: run termination routines for images not marked in-use
- if ( maxRangeCount != 0 ) {
- __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;
- }
+ __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);
- }
- }
-
- // <rdar://problem/14718598> dyld should call __cxa_finalize_ranges()
- if ( (rangeCount > 0) && (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 13) )
- (*gLibSystemHelpers->cxa_finalize_ranges)(ranges, rangeCount);
- }
+ }
+ try {
+ runImageStaticTerminators(image);
+ }
+ catch (const char* msg) {
+ dyld::warn("problem running terminators for image: %s\n", msg);
+ }
+ }
+
+ // <rdar://problem/14718598> dyld should call __cxa_finalize_ranges()
+ if ( (rangeCount > 0) && (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 13) )
+ (*gLibSystemHelpers->cxa_finalize_ranges)(ranges, rangeCount);
// collect phase: delete all images which are not marked in-use
bool mightBeMore;
@@ -4815,17 +4375,12 @@
}
-void preflight(ImageLoader* image, const ImageLoader::RPathChain& loaderRPaths, unsigned cacheIndex)
+void preflight(ImageLoader* image, const ImageLoader::RPathChain& loaderRPaths)
{
try {
if ( image->isBundle() )
sBundleBeingLoaded = image; // hack
- const char* path = image->getPath();
-#if SUPPORT_ACCELERATE_TABLES
- if ( image == sAllCacheImagesProxy )
- path = sAllCacheImagesProxy->getIndexedPath(cacheIndex);
-#endif
- image->link(gLinkContext, false, true, false, loaderRPaths, path);
+ image->link(gLinkContext, false, true, false, loaderRPaths);
}
catch (const char* msg) {
preflight_finally(image);
@@ -4837,7 +4392,6 @@
static void loadInsertedDylib(const char* path)
{
ImageLoader* image = NULL;
- unsigned cacheIndex;
try {
LoadContext context;
context.useSearchPaths = false;
@@ -4849,86 +4403,69 @@
context.mustBeBundle = false;
context.mustBeDylib = true;
context.canBePIE = false;
- context.enforceIOSMac = true;
context.origin = NULL; // can't use @loader_path with DYLD_INSERT_LIBRARIES
context.rpath = NULL;
- image = load(path, context, cacheIndex);
+ image = load(path, context);
}
catch (const char* msg) {
- if ( gLinkContext.allowInsertFailures )
- dyld::log("dyld: warning: could not load inserted library '%s' into hardened process because %s\n", path, msg);
- else
- halt(dyld::mkstringf("could not load inserted library '%s' because %s\n", path, 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 void configureProcessRestrictions(const macho_header* mainExecutableMH)
-{
- uint64_t amfiInputFlags = 0;
+static bool processRestricted(const macho_header* mainExecutableMH, bool* ignoreEnvVars, bool* processRequiresLibraryValidation)
+{
#if TARGET_IPHONE_SIMULATOR
- amfiInputFlags |= AMFI_DYLD_INPUT_PROC_IN_SIMULATOR;
-#elif __MAC_OS_X_VERSION_MIN_REQUIRED
- if ( hasRestrictedSegment(mainExecutableMH) )
- amfiInputFlags |= AMFI_DYLD_INPUT_PROC_HAS_RESTRICT_SEG;
-#elif __IPHONE_OS_VERSION_MIN_REQUIRED
- if ( isFairPlayEncrypted(mainExecutableMH) )
- amfiInputFlags |= AMFI_DYLD_INPUT_PROC_IS_ENCRYPTED;
-#endif
- uint64_t amfiOutputFlags = 0;
- if ( amfi_check_dyld_policy_self(amfiInputFlags, &amfiOutputFlags) == 0 ) {
- gLinkContext.allowAtPaths = (amfiOutputFlags & AMFI_DYLD_OUTPUT_ALLOW_AT_PATH);
- gLinkContext.allowEnvVarsPrint = (amfiOutputFlags & AMFI_DYLD_OUTPUT_ALLOW_PRINT_VARS);
- gLinkContext.allowEnvVarsPath = (amfiOutputFlags & AMFI_DYLD_OUTPUT_ALLOW_PATH_VARS);
- gLinkContext.allowEnvVarsSharedCache = (amfiOutputFlags & AMFI_DYLD_OUTPUT_ALLOW_CUSTOM_SHARED_CACHE);
- gLinkContext.allowClassicFallbackPaths = (amfiOutputFlags & AMFI_DYLD_OUTPUT_ALLOW_FALLBACK_PATHS);
- gLinkContext.allowInsertFailures = (amfiOutputFlags & AMFI_DYLD_OUTPUT_ALLOW_FAILED_LIBRARY_INSERTION);
- }
- else {
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- // support chrooting from old kernel
- bool isRestricted = false;
- bool libraryValidation = false;
- // any processes with setuid or setgid bit set or with __RESTRICT segment is restricted
- if ( issetugid() || hasRestrictedSegment(mainExecutableMH) ) {
- isRestricted = true;
- }
- bool usingSIP = (csr_check(CSR_ALLOW_TASK_FOR_PID) != 0);
- uint32_t flags;
- if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) != -1 ) {
- // On OS X CS_RESTRICT means the program was signed with entitlements
- if ( ((flags & CS_RESTRICT) == CS_RESTRICT) && usingSIP ) {
- isRestricted = true;
- }
- // Library Validation loosens searching but requires everything to be code signed
- if ( flags & CS_REQUIRE_LV ) {
- isRestricted = false;
- libraryValidation = true;
- }
- }
- gLinkContext.allowAtPaths = !isRestricted;
- gLinkContext.allowEnvVarsPrint = !isRestricted;
- gLinkContext.allowEnvVarsPath = !isRestricted;
- gLinkContext.allowEnvVarsSharedCache = !libraryValidation || !usingSIP;
- gLinkContext.allowClassicFallbackPaths = !isRestricted;
- gLinkContext.allowInsertFailures = false;
+ gLinkContext.codeSigningEnforced = true;
#else
- halt("amfi_check_dyld_policy_self() failed\n");
-#endif
- }
+ // ask kernel if code signature of program makes it restricted
+ uint32_t flags;
+ if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) != -1 ) {
+ if (flags & CS_REQUIRE_LV)
+ *processRequiresLibraryValidation = true;
+
+ #if __MAC_OS_X_VERSION_MIN_REQUIRED
+ if ( flags & CS_ENFORCEMENT ) {
+ gLinkContext.codeSigningEnforced = true;
+ }
+ if ( ((flags & CS_RESTRICT) == CS_RESTRICT) && (csr_check(CSR_ALLOW_TASK_FOR_PID) != 0) ) {
+ sRestrictedReason = restrictedByEntitlements;
+ return true;
+ }
+ #else
+ if ((flags & CS_ENFORCEMENT) && !(flags & CS_GET_TASK_ALLOW)) {
+ *ignoreEnvVars = true;
+ }
+ gLinkContext.codeSigningEnforced = true;
+ #endif
+ }
+#endif
+
+ // all processes with setuid or setgid bit set are restricted
+ if ( issetugid() ) {
+ sRestrictedReason = restrictedBySetGUid;
+ return true;
+ }
+
+ // <rdar://problem/13158444&13245742> Respect __RESTRICT,__restrict section for root processes
+ if ( hasRestrictedSegment(mainExecutableMH) ) {
+ // existence of __RESTRICT/__restrict section make process restricted
+ sRestrictedReason = restrictedBySegment;
+ return true;
+ }
+ return false;
}
bool processIsRestricted()
{
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- return !gLinkContext.allowEnvVarsPath;
-#else
- return false;
-#endif
+ return sProcessIsRestricted;
}
@@ -4954,37 +4491,13 @@
}
}
-void notifyKernelAboutImage(const struct macho_header* mh, const char* fileInfo)
-{
- const char *endptr = nullptr;
- uint64_t fsid_scalar = hexToUInt64(fileInfo, &endptr);
- uint64_t fsobj_id_scalar = 0;
- if (endptr != nullptr) {
- fsobj_id_scalar = hexToUInt64(endptr+1, &endptr);
- }
- 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: {
- // Add dyld to the kernel image info
- uuid_command* uc = (uuid_command*)cmd;
- dyld3::kdebug_trace_dyld_image(DBG_DYLD_UUID_MAP_A, (const uuid_t *)&uc->uuid[0], *reinterpret_cast<fsobj_id_t *>(&fsobj_id_scalar), *reinterpret_cast<fsid_t *>(&fsid_scalar), (const mach_header *)mh);
- 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 = {
- 12,
+ 4,
// added in version 1
(open_proc_t)&open,
&close,
@@ -5023,86 +4536,38 @@
&closedir,
// added in version 4
&coresymbolication_load_notifier,
- &coresymbolication_unload_notifier,
- // Added in version 5
- &proc_regionfilename,
- &getpid,
- &mach_port_insert_right,
- &mach_port_allocate,
- &mach_msg,
- // Added in version 6
- &abort_with_payload,
- // Added in version 7
- &task_register_dyld_image_infos,
- &task_unregister_dyld_image_infos,
- &task_get_dyld_image_infos,
- &task_register_dyld_shared_cache_image_info,
- &task_register_dyld_set_dyld_state,
- &task_register_dyld_get_process_state,
- // Added in version 8
- &task_info,
- &thread_info,
- &kdebug_is_enabled,
- &kdebug_trace,
- // Added in version 9
- &kdebug_trace_string,
- // Added in version 10
- &amfi_check_dyld_policy_self,
- // Added in version 11
- ¬ifyMonitoringDyldMain,
- ¬ifyMonitoringDyld,
- // Add in version 12
- &mach_msg_destroy,
- &mach_port_construct,
- &mach_port_destruct
+ &coresymbolication_unload_notifier
};
__attribute__((noinline))
-static const char* useSimulatorDyld(int fd, const macho_header* mainExecutableMH, const char* dyldPath,
- int argc, const char* argv[], const char* envp[], const char* apple[],
- uintptr_t* startGlue, uintptr_t* mainAddr)
+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;
- *mainAddr = 0;
-
- // <rdar://35873436> HACK to allow marzipan dyld_sim to run entitled processes
- if ( strncmp(dyldPath, "/System/", 8) != 0 ) {
- uint32_t flags;
- if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) == -1 )
- return "csops() failed";
- if ( (flags & CS_RESTRICT) == CS_RESTRICT )
- return "dyld_sim cannot be loaded in a restricted process";
- }
-
- // <rdar://problem/25311921> simulator does not support restricted processes
- if ( issetugid() )
- return "dyld_sim cannot be loaded in a setuid process";
- if ( hasRestrictedSegment(mainExecutableMH) )
- return "dyld_sim cannot be loaded in a restricted process";
-
- // get file size of dyld_sim
+
+ // verify simulator dyld file is owned by root
struct stat sb;
if ( fstat(fd, &sb) == -1 )
- return "stat(dyld_sim) failed";
-
- // read first page of dyld_sim file
+ return 0;
+
+ // read first page of dyld file
uint8_t firstPage[4096];
if ( pread(fd, firstPage, 4096, 0) != 4096 )
- return "pread(dyld_sim) failed";
-
+ return 0;
+
// if fat file, pick matching slice
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 "no matching arch in dyld_sim";
+ return 0;
// re-read buffer from start of mach-o slice in fat file
if ( pread(fd, firstPage, 4096, fileOffset) != 4096 )
- return "pread(dyld_sim) failed";
+ return 0;
}
else if ( !isCompatibleMachO(firstPage, dyldPath) ) {
- return "dyld_sim is not compatible with the loaded process, likely due to architecture mismatch";
+ return 0;
}
// calculate total size of dyld segments
@@ -5112,99 +4577,67 @@
uintptr_t mappingSize = 0;
uintptr_t preferredLoadAddress = 0;
const uint32_t cmd_count = mh->ncmds;
- if ( mh->sizeofcmds > 4096 )
- return "dyld_sim load commands to large";
if ( (sizeof(macho_header) + mh->sizeofcmds) > 4096 )
- return "dyld_sim load commands to large";
- struct linkedit_data_command* codeSigCmd = NULL;
+ return 0;
const struct load_command* const cmds = (struct load_command*)(((char*)mh)+sizeof(macho_header));
const struct load_command* const endCmds = (struct load_command*)(((char*)mh) + sizeof(macho_header) + mh->sizeofcmds);
const struct load_command* cmd = cmds;
for (uint32_t i = 0; i < cmd_count; ++i) {
uint32_t cmdLength = cmd->cmdsize;
if ( cmdLength < 8 )
- return "dyld_sim load command too small";
+ return 0;
const struct load_command* const nextCmd = (const struct load_command*)(((char*)cmd)+cmdLength);
if ( (nextCmd > endCmds) || (nextCmd < cmd) )
- return "dyld_sim load command too large";
+ return 0;
switch (cmd->cmd) {
case LC_SEGMENT_COMMAND:
{
struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
if ( seg->vmaddr + seg->vmsize < seg->vmaddr )
- return "dyld_sim seg wraps address space";
+ return 0;
if ( seg->vmsize < seg->filesize )
- return "dyld_sim seg vmsize too small";
- if ( (seg->fileoff + seg->filesize) < seg->fileoff )
- return "dyld_sim seg size wraps address space";
+ return 0;
if ( lastSeg == NULL ) {
// first segment must be __TEXT and start at beginning of file/slice
firstSeg = seg;
if ( strcmp(seg->segname, "__TEXT") != 0 )
- return "dyld_sim first segment not __TEXT";
+ return 0;
if ( seg->fileoff != 0 )
- return "dyld_sim first segment not at file offset zero";
+ return 0;
if ( seg->filesize < (sizeof(macho_header) + mh->sizeofcmds) )
- return "dyld_sim first segment smaller than load commands";
+ return 0;
preferredLoadAddress = seg->vmaddr;
}
else {
// other sements must be continguous with previous segment and not executable
if ( lastSeg->fileoff + lastSeg->filesize != seg->fileoff )
- return "dyld_sim segments not contiguous";
+ return 0;
if ( lastSeg->vmaddr + lastSeg->vmsize != seg->vmaddr )
- return "dyld_sim segments not address contiguous";
+ return 0;
if ( (seg->initprot & VM_PROT_EXECUTE) != 0 )
- return "dyld_sim non-first segment is executable";
+ return 0;
}
mappingSize += seg->vmsize;
lastSeg = seg;
}
break;
case LC_SEGMENT_COMMAND_WRONG:
- return "dyld_sim wrong load segment load command";
- case LC_CODE_SIGNATURE:
- codeSigCmd = (struct linkedit_data_command*)cmd;
- break;
+ return 0;
}
cmd = nextCmd;
}
// last segment must be named __LINKEDIT and not writable
- if ( lastSeg == NULL )
- return "dyld_sim has no segments";
if ( strcmp(lastSeg->segname, "__LINKEDIT") != 0 )
- return "dyld_sim last segment not __LINKEDIT";
+ return 0;
if ( lastSeg->initprot & VM_PROT_WRITE )
- return "dyld_sim __LINKEDIT segment writable";
-
- // must have code signature which is contained within LINKEDIT segment
- if ( codeSigCmd == NULL )
- return "dyld_sim not code signed";
- if ( codeSigCmd->dataoff < lastSeg->fileoff )
- return "dyld_sim code signature not in __LINKEDIT";
- if ( (codeSigCmd->dataoff + codeSigCmd->datasize) < codeSigCmd->dataoff )
- return "dyld_sim code signature size wraps";
- if ( (codeSigCmd->dataoff + codeSigCmd->datasize) > (lastSeg->fileoff + lastSeg->filesize) )
- return "dyld_sim code signature extends beyond __LINKEDIT";
-
- // register code signature with kernel before mmap()ing segments
- fsignatures_t siginfo;
- siginfo.fs_file_start=fileOffset; // start of mach-o slice in fat file
- siginfo.fs_blob_start=(void*)(long)(codeSigCmd->dataoff); // start of code-signature in mach-o file
- siginfo.fs_blob_size=codeSigCmd->datasize; // size of code-signature
- int result = fcntl(fd, F_ADDFILESIGS_FOR_DYLD_SIM, &siginfo);
- if ( result == -1 ) {
- return mkstringf("dyld_sim fcntl(F_ADDFILESIGS_FOR_DYLD_SIM) failed with errno=%d", errno);
- }
- // file range covered by code signature must extend up to code signature itself
- if ( siginfo.fs_file_start < codeSigCmd->dataoff )
- return mkstringf("dyld_sim code signature does not cover all of dyld_sim. Signature covers up to 0x%08lX. Signature starts at 0x%08X", (unsigned long)siginfo.fs_file_start, codeSigCmd->dataoff);
+ return 0;
// reserve space, then mmap each segment
vm_address_t loadAddress = 0;
if ( ::vm_allocate(mach_task_self(), &loadAddress, mappingSize, VM_FLAGS_ANYWHERE) != 0 )
- return "dyld_sim cannot allocate space";
+ return 0;
cmd = cmds;
+ struct linkedit_data_command* codeSigCmd = NULL;
struct source_version_command* dyldVersionCmd = NULL;
for (uint32_t i = 0; i < cmd_count; ++i) {
switch (cmd->cmd) {
@@ -5215,10 +4648,11 @@
void* segAddress = ::mmap((void*)requestedLoadAddress, seg->filesize, seg->initprot, MAP_FIXED | MAP_PRIVATE, fd, fileOffset + seg->fileoff);
//dyld::log("dyld_sim %s mapped at %p\n", seg->segname, segAddress);
if ( segAddress == (void*)(-1) )
- return "dyld_sim mmap() of segment failed";
- if ( ((uintptr_t)segAddress < loadAddress) || ((uintptr_t)segAddress+seg->filesize > loadAddress+mappingSize) )
- return "dyld_sim mmap() to wrong location";
+ return 0;
}
+ break;
+ case LC_CODE_SIGNATURE:
+ codeSigCmd = (struct linkedit_data_command*)cmd;
break;
case LC_SOURCE_VERSION:
dyldVersionCmd = (struct source_version_command*)cmd;
@@ -5226,7 +4660,28 @@
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
+
+ // must have code signature which is contained within LINKEDIT segment
+ if ( codeSigCmd == NULL )
+ return 0;
+ if ( codeSigCmd->dataoff < lastSeg->fileoff )
+ return 0;
+ if ( (codeSigCmd->dataoff + codeSigCmd->datasize) > (lastSeg->fileoff + lastSeg->filesize) )
+ return 0;
+
+ fsignatures_t siginfo;
+ siginfo.fs_file_start=fileOffset; // start of mach-o slice in fat file
+ siginfo.fs_blob_start=(void*)(long)(codeSigCmd->dataoff); // start of code-signature in mach-o file
+ siginfo.fs_blob_size=codeSigCmd->datasize; // size of code-signature
+ int result = fcntl(fd, F_ADDFILESIGS_FOR_DYLD_SIM, &siginfo);
+ if ( result == -1 ) {
+ dyld::log("fcntl(F_ADDFILESIGS_FOR_DYLD_SIM) failed with errno=%d\n", errno);
+ return 0;
+ }
close(fd);
+ // file range covered by code signature must extend up to code signature itself
+ if ( siginfo.fs_file_start < codeSigCmd->dataoff )
+ return 0;
// walk newly mapped dyld_sim __TEXT load commands to find entry point
uintptr_t entry = 0;
@@ -5238,24 +4693,22 @@
const i386_thread_state_t* registers = (i386_thread_state_t*)(((char*)cmd) + 16);
// entry point must be in first segment
if ( registers->__eip < firstSeg->vmaddr )
- return "dyld_sim entry point not in __TEXT segment";
+ return 0;
if ( registers->__eip > (firstSeg->vmaddr + firstSeg->vmsize) )
- return "dyld_sim entry point not in __TEXT segment";
+ return 0;
entry = (registers->__eip + loadAddress - preferredLoadAddress);
#elif __x86_64__
const x86_thread_state64_t* registers = (x86_thread_state64_t*)(((char*)cmd) + 16);
// entry point must be in first segment
if ( registers->__rip < firstSeg->vmaddr )
- return "dyld_sim entry point not in __TEXT segment";
+ return 0;
if ( registers->__rip > (firstSeg->vmaddr + firstSeg->vmsize) )
- return "dyld_sim entry point not in __TEXT segment";
+ return 0;
entry = (registers->__rip + loadAddress - preferredLoadAddress);
#endif
}
cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
}
- if ( entry == 0 )
- return "dyld_sim entry not found";
// notify debugger that dyld_sim is loaded
dyld_image_info info;
@@ -5271,611 +4724,12 @@
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;
- *mainAddr = (*newDyld)(argc, argv, envp, appleParams, mainExecutableMH, (macho_header*)loadAddress,
+ return (*newDyld)(argc, argv, envp, appleParams, mainExecutableMH, (macho_header*)loadAddress,
loadAddress - preferredLoadAddress,
&sSysCalls, startGlue);
- return NULL;
-}
-#endif
-
-//
-// If the DYLD_SKIP_MAIN environment is set to 1, dyld will return the
-// address of this function instead of main() in the target program which
-// __dyld_start jumps to. Useful for qualifying dyld itself.
-//
-int
-fake_main()
-{
- return 0;
-}
-
-
-
-
-static bool envVarMatches(const dyld3::closure::LaunchClosure* mainClosure, const char* envp[], const char* varName)
-{
- __block const char* valueFromClosure = nullptr;
- mainClosure->forEachEnvVar(^(const char* keyEqualValue, bool& stop) {
- size_t keyLen = strlen(varName);
- if ( (strncmp(varName, keyEqualValue, keyLen) == 0) && (keyEqualValue[keyLen] == '=') ) {
- valueFromClosure = &keyEqualValue[keyLen+1];
- stop = true;
- }
- });
-
- const char* valueFromEnv = _simple_getenv(envp, varName);
-
- bool inClosure = (valueFromClosure != nullptr);
- bool inEnv = (valueFromEnv != nullptr);
- if ( inClosure != inEnv )
- return false;
- if ( !inClosure && !inEnv )
- return true;
- return ( strcmp(valueFromClosure, valueFromEnv) == 0 );
-}
-
-static const char* const sEnvVarsToCheck[] = {
- "DYLD_LIBRARY_PATH",
- "DYLD_FRAMEWORK_PATH",
- "DYLD_FALLBACK_LIBRARY_PATH",
- "DYLD_FALLBACK_FRAMEWORK_PATH",
- "DYLD_INSERT_LIBRARIES",
- "DYLD_IMAGE_SUFFIX",
- "DYLD_VERSIONED_FRAMEWORK_PATH",
- "DYLD_VERSIONED_LIBRARY_PATH",
- "DYLD_ROOT_PATH"
-};
-
-static bool envVarsMatch(const dyld3::closure::LaunchClosure* mainClosure, const char* envp[])
-{
- for (const char* envVar : sEnvVarsToCheck) {
- if ( !envVarMatches(mainClosure, envp, envVar) ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because %s changed\n", mainClosure, envVar);
- return false;
- }
- }
-
- // FIXME: dyld3 doesn't support versioned paths so we need to fall back to dyld2 if we have them.
- // <rdar://problem/37004660> dyld3: support DYLD_VERSIONED_*_PATHs ?
- if ( sEnv.DYLD_VERSIONED_LIBRARY_PATH != nullptr ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because DYLD_VERSIONED_LIBRARY_PATH used\n", mainClosure);
- return false;
- }
- if ( sEnv.DYLD_VERSIONED_FRAMEWORK_PATH != nullptr ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because DYLD_VERSIONED_FRAMEWORK_PATH used\n", mainClosure);
- return false;
- }
-
- return true;
-}
-
-static bool closureValid(const dyld3::closure::LaunchClosure* mainClosure, const dyld3::closure::LoadedFileInfo& mainFileInfo,
- const uint8_t* mainExecutableCDHash, bool closureInCache, const char* envp[])
-{
- if ( !closureInCache ) {
- // verify current dyld cache is same as expected
- uuid_t expectedCacheUUID;
- if ( mainClosure->builtAgainstDyldCache(expectedCacheUUID) ) {
- if ( sSharedCacheLoadInfo.loadAddress == nullptr ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p dyld cache not loaded\n", mainClosure);
- return false;
- }
- else {
- uuid_t actualCacheUUID;
- sSharedCacheLoadInfo.loadAddress->getUUID(actualCacheUUID);
- if ( memcmp(expectedCacheUUID, actualCacheUUID, sizeof(uuid_t)) != 0 ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because built against different dyld cache\n", mainClosure);
- return false;
- }
- }
- }
- else {
- // closure built assume there is no dyld cache
- if ( sSharedCacheLoadInfo.loadAddress != nullptr ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p built expecting no dyld cache\n", mainClosure);
- return false;
- }
- }
-#if __IPHONE_OS_VERSION_MIN_REQUIRED
- // verify this closure is not from a previous reboot
- const char* expectedBootUUID = mainClosure->bootUUID();
- char actualBootSessionUUID[256] = { 0 };
- size_t bootSize = sizeof(actualBootSessionUUID);
- bool gotActualBootUUID = (sysctlbyname("kern.bootsessionuuid", actualBootSessionUUID, &bootSize, NULL, 0) == 0);
- if ( !gotActualBootUUID || (expectedBootUUID == nullptr) || (strcmp(expectedBootUUID, actualBootSessionUUID) != 0) ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p built in different boot context\n", mainClosure);
- return false;
- }
-#endif
- }
-
- // verify all mach-o files have not changed since closure was built
- __block bool foundFileThatInvalidatesClosure = false;
- mainClosure->images()->forEachImage(^(const dyld3::closure::Image* image, bool& stop) {
- __block uint64_t expectedInode;
- __block uint64_t expectedMtime;
- if ( image->hasFileModTimeAndInode(expectedInode, expectedMtime) ) {
- struct stat statBuf;
- if ( ::stat(image->path(), &statBuf) == 0 ) {
- if ( (statBuf.st_mtime != expectedMtime) || (statBuf.st_ino != expectedInode) ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because mtime/inode for '%s' has changed since closure was built\n", mainClosure, image->path());
- foundFileThatInvalidatesClosure = true;
- stop = true;
- }
- }
- else {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because '%s' is needed by closure but is missing\n", mainClosure, image->path());
- foundFileThatInvalidatesClosure = true;
- stop = true;
- }
- }
- });
- if ( foundFileThatInvalidatesClosure )
- return false;
-
- // verify cdHash of main executable is same as recorded in closure
- uint8_t expectedHash[20];
- const dyld3::closure::Image* mainImage = mainClosure->images()->imageForNum(mainClosure->topImage());
- if ( mainImage->hasCdHash(expectedHash) ) {
- if ( mainExecutableCDHash == nullptr ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because main executable is not code signed but was expected to be\n", mainClosure);
- return false;
- }
- if ( memcmp(mainExecutableCDHash, expectedHash, 20) != 0 ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because main executable cd-hash changed since closure was built\n", mainClosure);
- return false;
- }
- }
-
- // verify UUID of main executable is same as recorded in closure
- uuid_t expectedUUID;
- bool hasExpect = mainImage->getUuid(expectedUUID);
- uuid_t actualUUID;
- const dyld3::MachOLoaded* mainExecutableMH = (const dyld3::MachOLoaded*)mainFileInfo.fileContent;
- bool hasActual = mainExecutableMH->getUuid(actualUUID);
- if ( hasExpect != hasActual ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because UUID of executable changed since closure was built\n", mainClosure);
- return false;
- }
- if ( hasExpect && hasActual && memcmp(actualUUID, expectedUUID, sizeof(uuid_t)) != 0 ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because UUID of executable changed since closure was built\n", mainClosure);
- return false;
- }
-
- // verify DYLD_* env vars are same as when closure was built
- if ( !envVarsMatch(mainClosure, envp) ) {
- return false;
- }
-
- // verify files that are supposed to be missing actually are missing
- mainClosure->forEachMustBeMissingFile(^(const char* path, bool& stop) {
- struct stat statBuf;
- if ( ::stat(path, &statBuf) == 0 ) {
- stop = true;
- foundFileThatInvalidatesClosure = true;
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because found unexpected file '%s'\n", mainClosure, path);
- }
- });
-
- // verify closure did not require anything unavailable
- if ( mainClosure->usedAtPaths() && !gLinkContext.allowAtPaths ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because is used @paths, but process does not allow that\n", mainClosure);
- return false;
- }
- if ( mainClosure->usedFallbackPaths() && !gLinkContext.allowClassicFallbackPaths ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: closure %p not used because is used default fallback paths, but process does not allow that\n", mainClosure);
- return false;
- }
-
- return !foundFileThatInvalidatesClosure;
-}
-
-static bool nolog(const char* format, ...)
-{
- return false;
-}
-
-static bool dolog(const char* format, ...)
-{
- va_list list;
- va_start(list, format);
- vlog(format, list);
- va_end(list);
- return true;
-}
-
-static bool launchWithClosure(const dyld3::closure::LaunchClosure* mainClosure,
- const DyldSharedCache* dyldCache,
- const dyld3::MachOLoaded* mainExecutableMH, uintptr_t mainExecutableSlide,
- int argc, const char* argv[], const char* envp[], const char* apple[],
- uintptr_t* entry, uintptr_t* startGlue)
-{
- // build list of all known ImageArrays (at most three: cached dylibs, other OS dylibs, and main prog)
- STACK_ALLOC_ARRAY(const dyld3::closure::ImageArray*, imagesArrays, 3);
- const dyld3::closure::ImageArray* mainClosureImages = mainClosure->images();
- if ( dyldCache != nullptr ) {
- imagesArrays.push_back(dyldCache->cachedDylibsImageArray());
- if ( auto others = dyldCache->otherOSImageArray() )
- imagesArrays.push_back(others);
- }
- imagesArrays.push_back(mainClosureImages);
-
- // allocate space for Array<LoadedImage>
- STACK_ALLOC_ARRAY(dyld3::LoadedImage, allImages, mainClosure->initialLoadCount());
-
- __block dyld3::Loader loader(allImages, dyldCache, imagesArrays, (gLinkContext.verboseLoading ? &dolog : &nolog),
- (gLinkContext.verboseMapping ? &dolog : &nolog),
- (gLinkContext.verboseBind ? &dolog : &nolog),
- (gLinkContext.verboseDOF ? &dolog : &nolog));
- dyld3::closure::ImageNum mainImageNum = mainClosure->topImage();
- mainClosureImages->forEachImage(^(const dyld3::closure::Image* image, bool& stop) {
- if ( image->imageNum() == mainImageNum ) {
- // add main executable (which is already mapped by kernel) to list
- dyld3::LoadedImage mainLoadedImage = dyld3::LoadedImage::make(image, mainExecutableMH);
- mainLoadedImage.setState(dyld3::LoadedImage::State::mapped);
- mainLoadedImage.markLeaveMapped();
- loader.addImage(mainLoadedImage);
- stop = true;
- }
- else {
- // add inserted library to initial list
- loader.addImage(dyld3::LoadedImage::make(image));
- }
- });
-
- // recursively load all dependents and fill in allImages array
- Diagnostics diag;
- loader.completeAllDependents(diag);
- if ( diag.noError() )
- loader.mapAndFixupAllImages(diag, dyld3::Loader::dtraceUserProbesEnabled());
- if ( diag.hasError() ) {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: %s\n", diag.errorMessage());
- return false;
- }
-
- //dyld::log("loaded image list:\n");
- //for (const dyld3::LoadedImage& info : allImages) {
- // dyld::log("mh=%p, path=%s\n", info.loadedAddress(), info.image()->path());
- //}
-
- // find libdyld entry
- dyld3::closure::Image::ResolvedSymbolTarget dyldEntry;
- mainClosure->libDyldEntry(dyldEntry);
- const dyld3::LibDyldEntryVector* libDyldEntry = (dyld3::LibDyldEntryVector*)loader.resolveTarget(dyldEntry);
-
- // send info on all images to libdyld.dylb
- libDyldEntry->setVars(mainExecutableMH, argc, argv, envp, apple);
- if ( libDyldEntry->vectorVersion > 4 )
- libDyldEntry->setRestrictions(gLinkContext.allowAtPaths, gLinkContext.allowEnvVarsPath);
- libDyldEntry->setHaltFunction(&halt);
- if ( libDyldEntry->vectorVersion > 5 ) {
- libDyldEntry->setNotifyMonitoringDyldMain(¬ifyMonitoringDyldMain);
- libDyldEntry->setNotifyMonitoringDyld(¬ifyMonitoringDyld);
- }
- if ( libDyldEntry->vectorVersion > 2 )
- libDyldEntry->setChildForkFunction(&_dyld_fork_child);
-#if !TARGET_IPHONE_SIMULATOR
- if ( libDyldEntry->vectorVersion > 3 )
- libDyldEntry->setLogFunction(&dyld::vlog);
-#endif
- libDyldEntry->setOldAllImageInfo(gProcessInfo);
- const dyld3::LoadedImage* libSys = loader.findImage(mainClosure->libSystemImageNum());
- libDyldEntry->setInitialImageList(mainClosure, dyldCache, sSharedCacheLoadInfo.path, allImages, *libSys);
- // run initializers
- CRSetCrashLogMessage("dyld3: launch, running initializers");
- libDyldEntry->runInitialzersBottomUp((mach_header*)mainExecutableMH);
- //dyld::log("returned from runInitialzersBottomUp()\n");
-
- if (dyld3::kdebug_trace_dyld_enabled(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE)) {
- dyld3::kdebug_trace_dyld_duration_end(launchTraceID, DBG_DYLD_TIMING_LAUNCH_EXECUTABLE, 0, 0, 3);
- }
- dyld3::closure::Image::ResolvedSymbolTarget progEntry;
- if ( mainClosure->mainEntry(progEntry) ) {
- // modern app with LC_MAIN
- // set startGlue to "start" function in libdyld.dylib
- // set entry to "main" function in program
- *startGlue = (uintptr_t)(libDyldEntry->startFunc);
- *entry = loader.resolveTarget(progEntry);
- }
- else if ( mainClosure->startEntry(progEntry) ) {
- // old style app linked with crt1.o
- // entry is "start" function in program
- *startGlue = 0;
- *entry = loader.resolveTarget(progEntry);
- }
- else {
- assert(0);
- }
-
- CRSetCrashLogMessage("dyld3 mode");
- return true;
-}
-
-
-#if !TARGET_IPHONE_SIMULATOR
-
-static void putHexNibble(uint8_t value, char*& p)
-{
- if ( value < 10 )
- *p++ = '0' + value;
- else
- *p++ = 'A' + value - 10;
-}
-
-static void putHexByte(uint8_t value, char*& p)
-{
- value &= 0xFF;
- putHexNibble(value >> 4, p);
- putHexNibble(value & 0x0F, p);
-}
-
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
-static void makeHashOfProgramAndEnv(const char* mainExecutablePath, const uint8_t* mainExecutableCDHash, const char* envp[], uint8_t hash32[32])
-{
- // create hash of main path, main cd hash, cache UUID, DYLD_* env vars
- const struct ccdigest_info* di = ccsha256_di();
- ccdigest_di_decl(di, hashTemp); // defines hashTemp array in stack
- ccdigest_init(di, hashTemp);
- // hash in main executable path
- ccdigest_update(di, hashTemp, strlen(mainExecutablePath), mainExecutablePath);
- // hash in cdHash of main executable
- if ( mainExecutableCDHash != nullptr )
- ccdigest_update(di, hashTemp, 20, mainExecutableCDHash);
- // hash in shared cache UUID
- if ( sSharedCacheLoadInfo.loadAddress != nullptr ) {
- uuid_t cacheUUID;
- sSharedCacheLoadInfo.loadAddress->getUUID(cacheUUID);
- ccdigest_update(di, hashTemp, sizeof(uuid_t), cacheUUID);
- }
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- // hash in if process is restricted
- ccdigest_update(di, hashTemp, sizeof(gLinkContext.allowEnvVarsPath), &gLinkContext.allowEnvVarsPath);
-#endif
- // include dyld's UUID so changing dyld invalidates closures
- uuid_t dyldUUID;
- if ( ((const dyld3::MachOLoaded*)&__dso_handle)->getUuid(dyldUUID) )
- ccdigest_update(di, hashTemp, sizeof(uuid_t), dyldUUID);
-
- // hash in DYLD_* env vars
- for (const char* envVar : sEnvVarsToCheck) {
- if ( const char* keyValue = _simple_getenv(envp, envVar) )
- ccdigest_update(di, hashTemp, strlen(keyValue), keyValue);
- }
- // finish SHA256 into 32-byte value
- ccdigest_final(di, hashTemp, hash32);
- ccdigest_di_clear(di, hashTemp);
-}
-#endif
-
-static void buildClosureCachePath(const char* mainExecutablePath,const dyld3::MachOLoaded* mainExecutableMH,
- const uint8_t* mainExecutableCDHash, const char* envp[], char closurePath[])
-{
- // build base path of $TMPDIR/dyld/<prog-name>-
- const char* tempDir = _simple_getenv(envp, "TMPDIR");
- if ( tempDir == nullptr )
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- tempDir = "/private/tmp/";
-#else
- tempDir = "/private/var/tmp/";
-#endif
- strlcpy(closurePath, tempDir, PATH_MAX);
- strlcat(closurePath, "/com.apple.dyld/", PATH_MAX);
-
- // make sure dyld sub-dir exists
- struct stat statbuf;
- if ( ::stat(closurePath, &statbuf) != 0 ) {
- ::mkdir(closurePath, S_IRWXU);
- }
-
- const char* leafName = strrchr(mainExecutablePath, '/');
- if ( leafName == nullptr )
- leafName = mainExecutablePath;
- else
- ++leafName;
- strlcat(closurePath, leafName, PATH_MAX);
-
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- // on macOS we allow multiple closures by hashing the env vars into the cache filename
- strlcat(closurePath, "-", PATH_MAX);
- uint8_t hash32[32];
- makeHashOfProgramAndEnv(mainExecutablePath, mainExecutableCDHash, envp, hash32);
- char hashString[72];
- char* s = hashString;
- for (int i=0; i < 32; ++i)
- putHexByte(hash32[i], s);
- *s = '\0';
- strlcat(closurePath, hashString, PATH_MAX);
-#else
- // on iOS, the file name is the leaf name and UUID
- uuid_t mainExeUUID;
- if ( mainExecutableMH->getUuid(mainExeUUID) ) {
- char mainUuidStr[40];
- bytesToHex(mainExeUUID, sizeof(uuid_t), mainUuidStr);
- strlcat(closurePath, "-", PATH_MAX);
- strlcat(closurePath, mainUuidStr, PATH_MAX);
- }
-#endif
- strlcat(closurePath, ".closure", PATH_MAX);
-}
-
-static const dyld3::closure::LaunchClosure* mapClosureFile(const char* closurePath)
-{
- struct stat statbuf;
- if ( ::stat(closurePath, &statbuf) == -1 )
- return nullptr;
-
- int fd = ::open(closurePath, O_RDONLY);
- if ( fd < 0 )
- return nullptr;
-
- const dyld3::closure::LaunchClosure* closure = (dyld3::closure::LaunchClosure*)::mmap(NULL, (size_t)statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- ::close(fd);
-
- if ( closure == MAP_FAILED )
- return nullptr;
-
- return closure;
-}
-
-static const dyld3::closure::LaunchClosure* buildLaunchClosure(const uint8_t* mainExecutableCDHash,
- const dyld3::closure::LoadedFileInfo& mainFileInfo, const char* envp[])
-{
- const dyld3::MachOLoaded* mainExecutableMH = (const dyld3::MachOLoaded*)mainFileInfo.fileContent;
- dyld3::closure::PathOverrides pathOverrides;
- pathOverrides.setFallbackPathHandling(gLinkContext.allowClassicFallbackPaths ? dyld3::closure::PathOverrides::FallbackPathMode::classic : dyld3::closure::PathOverrides::FallbackPathMode::restricted);
- pathOverrides.setEnvVars(envp, mainExecutableMH, mainFileInfo.path);
- STACK_ALLOC_ARRAY(const dyld3::closure::ImageArray*, imagesArrays, 3);
- if ( sSharedCacheLoadInfo.loadAddress != nullptr ) {
- imagesArrays.push_back(sSharedCacheLoadInfo.loadAddress->cachedDylibsImageArray());
- if ( auto others = sSharedCacheLoadInfo.loadAddress->otherOSImageArray() )
- imagesArrays.push_back(others);
- }
-
- dyld3::closure::ClosureBuilder::LaunchErrorInfo* errorInfo = (dyld3::closure::ClosureBuilder::LaunchErrorInfo*)&gProcessInfo->errorKind;
- dyld3::closure::FileSystemPhysical fileSystem;
- dyld3::closure::ClosureBuilder::AtPath atPathHanding = (gLinkContext.allowAtPaths ? dyld3::closure::ClosureBuilder::AtPath::all : dyld3::closure::ClosureBuilder::AtPath::none);
- dyld3::closure::ClosureBuilder builder(dyld3::closure::kFirstLaunchClosureImageNum, fileSystem, sSharedCacheLoadInfo.loadAddress, true, pathOverrides, atPathHanding, errorInfo, mainExecutableMH->archName());
- const dyld3::closure::LaunchClosure* result = builder.makeLaunchClosure(mainFileInfo, gLinkContext.allowInsertFailures);
- if ( builder.diagnostics().hasError() )
- halt(builder.diagnostics().errorMessage());
-
- if ( result == nullptr )
- return nullptr;
-
- if ( !closureValid(result, mainFileInfo, mainExecutableCDHash, false, envp) ) {
- // some how the freshly generated closure is invalid...
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: somehow just built closure is invalid\n");
- return nullptr;
- }
- // try to save closure to disk for next launch (atomically)
- char closurePath[PATH_MAX];
- buildClosureCachePath(mainFileInfo.path, mainExecutableMH, mainExecutableCDHash, envp, closurePath);
- char closurePathTemp[PATH_MAX];
- strlcpy(closurePathTemp, closurePath, PATH_MAX);
- int mypid = getpid();
- char pidBuf[16];
- char* s = pidBuf;
- *s++ = '.';
- putHexByte(mypid >> 24, s);
- putHexByte(mypid >> 16, s);
- putHexByte(mypid >> 8, s);
- putHexByte(mypid, s);
- *s = '\0';
- strlcat(closurePathTemp, pidBuf, PATH_MAX);
- int fd = ::open(closurePathTemp, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
- if ( fd != -1 ) {
- ::ftruncate(fd, result->size());
- ::write(fd, result, result->size());
- ::fchmod(fd, S_IRUSR);
- ::close(fd);
- ::rename(closurePathTemp, closurePath);
- // free built closure and mmap file() to reduce dirty memory
- result->deallocate();
- result = mapClosureFile(closurePath);
- }
- else if ( gLinkContext.verboseWarnings ) {
- dyld::log("could not save closure (errno=%d) to: %s\n", errno, closurePathTemp);
- }
-
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: just built closure %p (size=%lu) for %s\n", result, result->size(), sExecPath);
-
- return result;
-}
-
-static const dyld3::closure::LaunchClosure* findCachedLaunchClosure(const uint8_t* mainExecutableCDHash,
- const dyld3::closure::LoadedFileInfo& mainFileInfo,
- const char* envp[])
-{
- char closurePath[PATH_MAX];
- buildClosureCachePath(mainFileInfo.path, (const dyld3::MachOLoaded*)mainFileInfo.fileContent, mainExecutableCDHash, envp, closurePath);
- const dyld3::closure::LaunchClosure* closure = mapClosureFile(closurePath);
- if ( closure == nullptr )
- return nullptr;
-
- if ( !closureValid(closure, mainFileInfo, mainExecutableCDHash, false, envp) ) {
- ::munmap((void*)closure, closure->size());
- return nullptr;
- }
-
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: used cached closure %p (size=%lu) for %s\n", closure, closure->size(), sExecPath);
-
- return closure;
-}
-
-#endif // !TARGET_IPHONE_SIMULATOR
-
-
-
-#if !__MAC_OS_X_VERSION_MIN_REQUIRED
-static const char* sWhiteListDirs[] = {
- "/bin/",
- "/sbin/",
- "/usr/bin/"
-};
-#endif
-
-static bool inWhiteList(const char* execPath)
-{
- // First test to see if we forced in dyld2 via a kernel boot-arg
- if ( dyld3::bootArgsContains("force_dyld2=1") )
- return false;
-
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
-
- // rdar://problem/32701418: Don't use dyld3 for i386 for now.
-#if __i386__
- return false;
-#else
-
-
- return true;
-#endif // #if __i386__
-
-#else
-
-
- // <rdar://problem/33171968> enable dyld3 mode for all OS programs when using customer dyld cache (no roots)
- if ( (sSharedCacheLoadInfo.loadAddress != nullptr) && (sSharedCacheLoadInfo.loadAddress->header.cacheType == kDyldSharedCacheTypeProduction) )
- return true;
-
- return dyld3::bootArgsContains("force_dyld3=1");
-#endif
-}
-
-#if !TARGET_IPHONE_SIMULATOR
-static bool isStagedApp(const dyld3::MachOFile* mainExecutableMH, const char* mainExecutablePath)
-{
-#if !__MAC_OS_X_VERSION_MIN_REQUIRED
- if ( (strncmp(mainExecutablePath, "/var/containers/Bundle/Application/", 35) == 0)
- || (strncmp(mainExecutablePath, "/private/var/containers/Bundle/Application/", 43) == 0) ) {
- // staged apps are built without LC_ENCRYPTION_INFO
- if ( !mainExecutableMH->canBeFairPlayEncrypted() )
- return true;
- }
-#endif
- return false;
-}
-#endif
+}
+#endif
+
//
// Entry point for dyld. The kernel loads dyld and jumps to __dyld_start which
@@ -5888,39 +4742,21 @@
int argc, const char* argv[], const char* envp[], const char* apple[],
uintptr_t* startGlue)
{
- if (dyld3::kdebug_trace_dyld_enabled(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE)) {
- launchTraceID = dyld3::kdebug_trace_dyld_duration_start(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE, (uint64_t)mainExecutableMH, 0, 0);
- }
-
- // Grab the cdHash of the main executable from the environment
- uint8_t mainExecutableCDHashBuffer[20];
- const uint8_t* mainExecutableCDHash = nullptr;
- if ( hexToBytes(_simple_getenv(apple, "executable_cdhash"), 40, mainExecutableCDHashBuffer) )
- mainExecutableCDHash = mainExecutableCDHashBuffer;
-
- // Trace dyld's load
- notifyKernelAboutImage((macho_header*)&__dso_handle, _simple_getenv(apple, "dyld_file"));
-#if !TARGET_IPHONE_SIMULATOR
- // Trace the main executable's load
- notifyKernelAboutImage(mainExecutableMH, _simple_getenv(apple, "executable_file"));
-#endif
-
uintptr_t result = 0;
sMainExecutableMachHeader = mainExecutableMH;
- sMainExecutableSlide = mainExecutableSlide;
#if __MAC_OS_X_VERSION_MIN_REQUIRED
// if this is host dyld, check to see if iOS simulator is being run
const char* rootPath = _simple_getenv(envp, "DYLD_ROOT_PATH");
- if ( (rootPath != NULL) ) {
+ if ( rootPath != NULL ) {
// look to see if simulator has its own dyld
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 ) {
- const char* errMessage = useSimulatorDyld(fd, mainExecutableMH, simDyldPath, argc, argv, envp, apple, startGlue, &result);
- if ( errMessage != NULL )
- halt(errMessage);
+ result = useSimulatorDyld(fd, mainExecutableMH, simDyldPath, argc, argv, envp, apple, startGlue);
+ if ( !result && (*startGlue == 0) )
+ halt("problem loading iOS simulator dyld");
return result;
}
}
@@ -5928,6 +4764,25 @@
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);
+ }
+ //dyld::log("open(%s) => %d, errno = %d\n", bindingsLogPath, sBindingsLogfile, errno);
+#endif
setContext(mainExecutableMH, argc, argv, envp, apple);
// Pickup the pointer to the exec path.
@@ -5936,6 +4791,7 @@
// <rdar://problem/13868260> Remove interim apple[0] transition code from dyld
if (!sExecPath) sExecPath = apple[0];
+ bool ignoreEnvironmentVariables = false;
if ( sExecPath[0] != '/' ) {
// have relative path, use cwd to make absolute
char cwdbuff[MAXPATHLEN];
@@ -5948,214 +4804,86 @@
sExecPath = s;
}
}
-
// Remember short name of process for later logging
sExecShortName = ::strrchr(sExecPath, '/');
if ( sExecShortName != NULL )
++sExecShortName;
else
sExecShortName = sExecPath;
-
- configureProcessRestrictions(mainExecutableMH);
-
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- if ( !gLinkContext.allowEnvVarsPrint && !gLinkContext.allowEnvVarsPath && !gLinkContext.allowEnvVarsSharedCache ) {
+ sProcessIsRestricted = processRestricted(mainExecutableMH, &ignoreEnvironmentVariables, &sProcessRequiresLibraryValidation);
+ if ( sProcessIsRestricted ) {
+#if SUPPORT_LC_DYLD_ENVIRONMENT
+ checkLoadCommandEnvironmentVariables();
+#endif
pruneEnvironmentVariables(envp, &apple);
// set again because envp and apple may have changed or moved
setContext(mainExecutableMH, argc, argv, envp, apple);
}
- else
-#endif
- {
- checkEnvironmentVariables(envp);
+ else {
+ if ( !ignoreEnvironmentVariables )
+ checkEnvironmentVariables(envp);
defaultUninitializedFallbackPaths(envp);
}
-#if __MAC_OS_X_VERSION_MIN_REQUIRED
- if ( ((dyld3::MachOFile*)mainExecutableMH)->supportsPlatform(dyld3::Platform::iOSMac)
- && !((dyld3::MachOFile*)mainExecutableMH)->supportsPlatform(dyld3::Platform::macOS)) {
- gLinkContext.rootPaths = parseColonList("/System/iOSSupport", NULL);
- gLinkContext.marzipan = true;
- if ( sEnv.DYLD_FALLBACK_LIBRARY_PATH == sLibraryFallbackPaths )
- sEnv.DYLD_FALLBACK_LIBRARY_PATH = sRestrictedLibraryFallbackPaths;
- if ( sEnv.DYLD_FALLBACK_FRAMEWORK_PATH == sFrameworkFallbackPaths )
- sEnv.DYLD_FALLBACK_FRAMEWORK_PATH = sRestrictedFrameworkFallbackPaths;
- }
-#endif
if ( sEnv.DYLD_PRINT_OPTS )
printOptions(argv);
if ( sEnv.DYLD_PRINT_ENV )
printEnvironmentVariables(envp);
getHostInfo(mainExecutableMH, mainExecutableSlide);
-
- // load shared cache
- checkSharedRegionDisable((dyld3::MachOLoaded*)mainExecutableMH, mainExecutableSlide);
-#if TARGET_IPHONE_SIMULATOR
- // <HACK> until <rdar://30773711> is fixed
- gLinkContext.sharedRegionMode = ImageLoader::kUsePrivateSharedRegion;
- // </HACK>
-#endif
- if ( gLinkContext.sharedRegionMode != ImageLoader::kDontUseSharedRegion ) {
- mapSharedCache();
- }
- bool cacheCompatible = (sSharedCacheLoadInfo.loadAddress == nullptr) || (sSharedCacheLoadInfo.loadAddress->header.formatVersion == dyld3::closure::kFormatVersion);
- if ( cacheCompatible && (sEnableClosures || inWhiteList(sExecPath)) ) {
- const dyld3::closure::LaunchClosure* mainClosure = nullptr;
- dyld3::closure::LoadedFileInfo mainFileInfo;
- mainFileInfo.fileContent = mainExecutableMH;
- mainFileInfo.path = sExecPath;
- // FIXME: If we are saving this closure, this slice offset/length is probably wrong in the case of FAT files.
- mainFileInfo.sliceOffset = 0;
- mainFileInfo.sliceLen = std::numeric_limits<__typeof(mainFileInfo.sliceLen)>::max();
- struct stat mainExeStatBuf;
- if ( ::stat(sExecPath, &mainExeStatBuf) == 0 ) {
- mainFileInfo.inode = mainExeStatBuf.st_ino;
- mainFileInfo.mtime = mainExeStatBuf.st_mtime;
- }
- // check for closure in cache first
- if ( sSharedCacheLoadInfo.loadAddress != nullptr ) {
- mainClosure = sSharedCacheLoadInfo.loadAddress->findClosure(sExecPath);
- if ( gLinkContext.verboseWarnings && (mainClosure != nullptr) )
- dyld::log("dyld: found closure %p (size=%lu) in dyld shared cache\n", mainClosure, mainClosure->size());
- }
- #if !TARGET_IPHONE_SIMULATOR
- if ( (mainClosure == nullptr) || !closureValid(mainClosure, mainFileInfo, mainExecutableCDHash, true, envp) ) {
- mainClosure = nullptr;
- if ( sEnableClosures || isStagedApp((dyld3::MachOFile*)mainExecutableMH, sExecPath) ) {
- // if forcing closures, and no closure in cache, or it is invalid, check for cached closure
- mainClosure = findCachedLaunchClosure(mainExecutableCDHash, mainFileInfo, envp);
- if ( mainClosure == nullptr ) {
- // if no cached closure found, build new one
- mainClosure = buildLaunchClosure(mainExecutableCDHash, mainFileInfo, envp);
- }
- }
- }
- #endif
- // try using launch closure
- if ( mainClosure != nullptr ) {
- CRSetCrashLogMessage("dyld3: launch started");
- bool launched = launchWithClosure(mainClosure, sSharedCacheLoadInfo.loadAddress, (dyld3::MachOLoaded*)mainExecutableMH,
- mainExecutableSlide, argc, argv, envp, apple, &result, startGlue);
- #if !TARGET_IPHONE_SIMULATOR
- if ( !launched ) {
- // closure is out of date, build new one
- mainClosure = buildLaunchClosure(mainExecutableCDHash, mainFileInfo, envp);
- if ( mainClosure != nullptr ) {
- launched = launchWithClosure(mainClosure, sSharedCacheLoadInfo.loadAddress, (dyld3::MachOLoaded*)mainExecutableMH,
- mainExecutableSlide, argc, argv, envp, apple, &result, startGlue);
- }
- }
- #endif
- if ( launched ) {
-#if __has_feature(ptrauth_calls)
- // start() calls the result pointer as a function pointer so we need to sign it.
- result = (uintptr_t)__builtin_ptrauth_sign_unauthenticated((void*)result, 0, 0);
-#endif
- if (sSkipMain)
- result = (uintptr_t)&fake_main;
- return result;
- }
- else {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: unable to use closure %p\n", mainClosure);
- }
- }
- }
- else {
- if ( gLinkContext.verboseWarnings )
- dyld::log("dyld: not using closure because shared cache format version does not match dyld's\n");
- }
- // could not use closure info, launch old way
-
-
-
// install gdb notifier
stateToHandlers(dyld_image_state_dependents_mapped, sBatchHandlers)->push_back(notifyGDB);
stateToHandlers(dyld_image_state_mapped, sSingleHandlers)->push_back(updateAllImages);
// make initial allocations large enough that it is unlikely to need to be re-alloced
+ sAllImages.reserve(INITIAL_IMAGE_COUNT);
sImageRoots.reserve(16);
sAddImageCallbacks.reserve(4);
sRemoveImageCallbacks.reserve(4);
- sAddLoadImageCallbacks.reserve(4);
sImageFilesNeedingTermination.reserve(16);
sImageFilesNeedingDOFUnregistration.reserve(8);
-
-#if !TARGET_IPHONE_SIMULATOR
+
#ifdef WAIT_FOR_SYSTEM_ORDER_HANDSHAKE
// <rdar://problem/6849505> Add gating mechanism to dyld support system order file generation process
WAIT_FOR_SYSTEM_ORDER_HANDSHAKE(dyld::gProcessInfo->systemOrderFlag);
#endif
-#endif
-
+
try {
// add dyld itself to UUID list
addDyldImageToUUIDList();
-
-#if SUPPORT_ACCELERATE_TABLES
-#if __arm64e__
- // Disable accelerator tables when we have threaded rebase/bind, which is arm64e executables only for now.
- if (sMainExecutableMachHeader->cpusubtype == CPU_SUBTYPE_ARM64_E)
- sDisableAcceleratorTables = true;
-#endif
- bool mainExcutableAlreadyRebased = false;
- if ( (sSharedCacheLoadInfo.loadAddress != nullptr) && !dylibsCanOverrideCache() && !sDisableAcceleratorTables && (sSharedCacheLoadInfo.loadAddress->header.accelerateInfoAddr != 0) ) {
- struct stat statBuf;
- if ( ::stat(IPHONE_DYLD_SHARED_CACHE_DIR "no-dyld2-accelerator-tables", &statBuf) != 0 )
- sAllCacheImagesProxy = ImageLoaderMegaDylib::makeImageLoaderMegaDylib(&sSharedCacheLoadInfo.loadAddress->header, sSharedCacheLoadInfo.slide, mainExecutableMH, gLinkContext);
- }
-
-reloadAllImages:
-#endif
-
CRSetCrashLogMessage(sLoadingCrashMessage);
// instantiate ImageLoader for main executable
sMainExecutable = instantiateFromLoadedImage(mainExecutableMH, mainExecutableSlide, sExecPath);
gLinkContext.mainExecutable = sMainExecutable;
+ gLinkContext.processIsRestricted = sProcessIsRestricted;
+ gLinkContext.processRequiresLibraryValidation = sProcessRequiresLibraryValidation;
gLinkContext.mainExecutableCodeSigned = hasCodeSignatureLoadCommand(mainExecutableMH);
#if TARGET_IPHONE_SIMULATOR
+ #if TARGET_OS_WATCH || TARGET_OS_TV
+ // disable error during bring up of these simulators
+ #else
// check main executable is not too new for this OS
{
if ( ! isSimulatorBinary((uint8_t*)mainExecutableMH, sExecPath) ) {
- throwf("program was built for a platform that is not supported by this runtime");
+ throwf("program was built for Mac OS X and cannot be run in simulator");
}
uint32_t mainMinOS = sMainExecutable->minOSVersion();
-
// dyld is always built for the current OS, so we can get the current OS version
// from the load command in dyld itself.
uint32_t dyldMinOS = ImageLoaderMachO::minOSVersion((const mach_header*)&__dso_handle);
if ( mainMinOS > dyldMinOS ) {
- #if TARGET_OS_WATCH
- throwf("app was built for watchOS %d.%d which is newer than this simulator %d.%d",
+ 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));
- #elif TARGET_OS_TV
- throwf("app was built for tvOS %d.%d which is newer than this simulator %d.%d",
- mainMinOS >> 16, ((mainMinOS >> 8) & 0xFF),
- dyldMinOS >> 16, ((dyldMinOS >> 8) & 0xFF));
- #else
- 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
- }
- }
-#endif
-
-
- #if __MAC_OS_X_VERSION_MIN_REQUIRED
- // <rdar://problem/22805519> be less strict about old mach-o binaries
- uint32_t mainSDK = sMainExecutable->sdkVersion();
- gLinkContext.strictMachORequired = (mainSDK >= DYLD_MACOSX_VERSION_10_12) || gLinkContext.allowInsertFailures;
- #else
- // simulators, iOS, tvOS, and watchOS are always strict
- gLinkContext.strictMachORequired = true;
- #endif
-
- #if SUPPORT_ACCELERATE_TABLES
- sAllImages.reserve((sAllCacheImagesProxy != NULL) ? 16 : INITIAL_IMAGE_COUNT);
- #else
- sAllImages.reserve(INITIAL_IMAGE_COUNT);
+#endif
+
+ // load shared cache
+ checkSharedRegionDisable();
+ #if DYLD_SHARED_CACHE_SUPPORT
+ if ( gLinkContext.sharedRegionMode != ImageLoader::kDontUseSharedRegion )
+ mapSharedCache();
#endif
// Now that shared cache is loaded, setup an versioned dylib overrides
@@ -6163,25 +4891,6 @@
checkVersionedPaths();
#endif
-
- // dyld_all_image_infos image list does not contain dyld
- // add it as dyldPath field in dyld_all_image_infos
- // for simulator, dyld_sim is in image list, need host dyld added
-#if TARGET_IPHONE_SIMULATOR
- // get path of host dyld from table of syscall vectors in host dyld
- void* addressInDyld = gSyscallHelpers;
-#else
- // get path of dyld itself
- void* addressInDyld = (void*)&__dso_handle;
-#endif
- char dyldPathBuffer[MAXPATHLEN+1];
- int len = proc_regionfilename(getpid(), (uint64_t)(long)addressInDyld, dyldPathBuffer, MAXPATHLEN);
- if ( len > 0 ) {
- dyldPathBuffer[len] = '\0'; // proc_regionfilename() does not zero terminate returned string
- if ( strcmp(dyldPathBuffer, gProcessInfo->dyldPath) != 0 )
- gProcessInfo->dyldPath = strdup(dyldPathBuffer);
- }
-
// load any inserted libraries
if ( sEnv.DYLD_INSERT_LIBRARIES != NULL ) {
for (const char* const* lib = sEnv.DYLD_INSERT_LIBRARIES; *lib != NULL; ++lib)
@@ -6193,14 +4902,7 @@
// link main executable
gLinkContext.linkingMainExecutable = true;
-#if SUPPORT_ACCELERATE_TABLES
- if ( mainExcutableAlreadyRebased ) {
- // previous link() on main executable has already adjusted its internal pointers for ASLR
- // work around that by rebasing by inverse amount
- sMainExecutable->rebase(gLinkContext, -mainExecutableSlide);
- }
-#endif
- link(sMainExecutable, sEnv.DYLD_BIND_AT_LAUNCH, true, ImageLoader::RPathChain(NULL, NULL), -1);
+ link(sMainExecutable, sEnv.DYLD_BIND_AT_LAUNCH, true, ImageLoader::RPathChain(NULL, NULL));
sMainExecutable->setNeverUnloadRecursive();
if ( sMainExecutable->forceFlat() ) {
gLinkContext.bindFlat = true;
@@ -6213,101 +4915,34 @@
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), -1);
+ link(image, sEnv.DYLD_BIND_AT_LAUNCH, true, ImageLoader::RPathChain(NULL, NULL));
image->setNeverUnloadRecursive();
}
// only INSERTED libraries can interpose
// register interposing info after all inserted libraries are bound so chaining works
for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
ImageLoader* image = sAllImages[i+1];
- image->registerInterposing(gLinkContext);
+ image->registerInterposing();
}
}
// <rdar://problem/19315404> dyld should support interposition even without DYLD_INSERT_LIBRARIES
- for (long i=sInsertedDylibCount+1; i < sAllImages.size(); ++i) {
+ for (int i=sInsertedDylibCount+1; i < sAllImages.size(); ++i) {
ImageLoader* image = sAllImages[i];
if ( image->inSharedCache() )
continue;
- image->registerInterposing(gLinkContext);
- }
- #if SUPPORT_ACCELERATE_TABLES
- if ( (sAllCacheImagesProxy != NULL) && ImageLoader::haveInterposingTuples() ) {
- // Accelerator tables cannot be used with implicit interposing, so relaunch with accelerator tables disabled
- ImageLoader::clearInterposingTuples();
- // unmap all loaded dylibs (but not main executable)
- for (long i=1; i < sAllImages.size(); ++i) {
- ImageLoader* image = sAllImages[i];
- if ( image == sMainExecutable )
- continue;
- if ( image == sAllCacheImagesProxy )
- continue;
- image->setCanUnload();
- ImageLoader::deleteImage(image);
- }
- // note: we don't need to worry about inserted images because if DYLD_INSERT_LIBRARIES was set we would not be using the accelerator table
- sAllImages.clear();
- sImageRoots.clear();
- sImageFilesNeedingTermination.clear();
- sImageFilesNeedingDOFUnregistration.clear();
- sAddImageCallbacks.clear();
- sRemoveImageCallbacks.clear();
- sAddLoadImageCallbacks.clear();
- sDisableAcceleratorTables = true;
- sAllCacheImagesProxy = NULL;
- sMappedRangesStart = NULL;
- mainExcutableAlreadyRebased = true;
- gLinkContext.linkingMainExecutable = false;
- resetAllImages();
- goto reloadAllImages;
- }
- #endif
+ image->registerInterposing();
+ }
// apply interposing to initial set of images
for(int i=0; i < sImageRoots.size(); ++i) {
sImageRoots[i]->applyInterposing(gLinkContext);
}
- ImageLoader::applyInterposingToDyldCache(gLinkContext);
gLinkContext.linkingMainExecutable = false;
-
- // Bind and notify for the main executable now that interposing has been registered
- uint64_t bindMainExecutableStartTime = mach_absolute_time();
- sMainExecutable->recursiveBindWithAccounting(gLinkContext, sEnv.DYLD_BIND_AT_LAUNCH, true);
- uint64_t bindMainExecutableEndTime = mach_absolute_time();
- ImageLoaderMachO::fgTotalBindTime += bindMainExecutableEndTime - bindMainExecutableStartTime;
- gLinkContext.notifyBatch(dyld_image_state_bound, false);
-
- // Bind and notify for the inserted images now interposing has been registered
- if ( sInsertedDylibCount > 0 ) {
- for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
- ImageLoader* image = sAllImages[i+1];
- image->recursiveBind(gLinkContext, sEnv.DYLD_BIND_AT_LAUNCH, true);
- }
- }
// <rdar://problem/12186933> do weak binding only after all inserted images linked
sMainExecutable->weakBind(gLinkContext);
-
- // If cache has branch island dylibs, tell debugger about them
- if ( (sSharedCacheLoadInfo.loadAddress != NULL) && (sSharedCacheLoadInfo.loadAddress->header.mappingOffset >= 0x78) && (sSharedCacheLoadInfo.loadAddress->header.branchPoolsOffset != 0) ) {
- uint32_t count = sSharedCacheLoadInfo.loadAddress->header.branchPoolsCount;
- dyld_image_info info[count];
- const uint64_t* poolAddress = (uint64_t*)((char*)sSharedCacheLoadInfo.loadAddress + sSharedCacheLoadInfo.loadAddress->header.branchPoolsOffset);
- // <rdar://problem/20799203> empty branch pools can be in development cache
- if ( ((mach_header*)poolAddress)->magic == sMainExecutableMachHeader->magic ) {
- for (int poolIndex=0; poolIndex < count; ++poolIndex) {
- uint64_t poolAddr = poolAddress[poolIndex] + sSharedCacheLoadInfo.slide;
- info[poolIndex].imageLoadAddress = (mach_header*)(long)poolAddr;
- info[poolIndex].imageFilePath = "dyld_shared_cache_branch_islands";
- info[poolIndex].imageFileModDate = 0;
- }
- // add to all_images list
- addImagesToAllImages(count, info);
- // tell gdb about new branch island images
- gProcessInfo->notification(dyld_image_adding, count, info);
- }
- }
-
+
CRSetCrashLogMessage("dyld: launch, running initializers");
#if SUPPORT_OLD_CRT_INITIALIZATION
// Old way is to run initializers via a callback from crt1.o
@@ -6317,17 +4952,10 @@
// run all initializers
initializeMainExecutable();
#endif
-
- // notify any montoring proccesses that this process is about to enter main()
- if (dyld3::kdebug_trace_dyld_enabled(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE)) {
- dyld3::kdebug_trace_dyld_duration_end(launchTraceID, DBG_DYLD_TIMING_LAUNCH_EXECUTABLE, 0, 0, 2);
- }
- notifyMonitoringDyldMain();
-
// find entry point for main executable
- result = (uintptr_t)sMainExecutable->getEntryFromLC_MAIN();
+ result = (uintptr_t)sMainExecutable->getThreadPC();
if ( result != 0 ) {
- // main executable uses LC_MAIN, we need to use helper in libdyld to call into main()
+ // main executable uses LC_MAIN, needs to return to glue in libdyld.dylib
if ( (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 9) )
*startGlue = (uintptr_t)gLibSystemHelpers->startGlueToCallExit;
else
@@ -6335,13 +4963,9 @@
}
else {
// main executable uses LC_UNIXTHREAD, dyld needs to let "start" in program set up for main()
- result = (uintptr_t)sMainExecutable->getEntryFromLC_UNIXTHREAD();
+ result = (uintptr_t)sMainExecutable->getMain();
*startGlue = 0;
}
-#if __has_feature(ptrauth_calls)
- // start() calls the result pointer as a function pointer so we need to sign it.
- result = (uintptr_t)__builtin_ptrauth_sign_unauthenticated((void*)result, 0, 0);
-#endif
}
catch(const char* message) {
syncAllImages();
@@ -6351,20 +4975,13 @@
dyld::log("dyld: launch failed\n");
}
- CRSetCrashLogMessage("dyld2 mode");
-
- if (sSkipMain) {
- if (dyld3::kdebug_trace_dyld_enabled(DBG_DYLD_TIMING_LAUNCH_EXECUTABLE)) {
- dyld3::kdebug_trace_dyld_duration_end(launchTraceID, DBG_DYLD_TIMING_LAUNCH_EXECUTABLE, 0, 0, 2);
- }
- result = (uintptr_t)&fake_main;
- *startGlue = (uintptr_t)gLibSystemHelpers->startGlueToCallExit;
- }
+ CRSetCrashLogMessage(NULL);
return result;
}
+
} // namespace