From bc37c99380a04b3adbe7b7b50414bbbb19335048 Mon Sep 17 00:00:00 2001 From: Thomas A Date: Wed, 20 Apr 2022 21:35:25 -0700 Subject: [PATCH] Add Back Darling Specific Changes --- darling-scripts/add-version-macros.sh | 14 ++++++++++++++ dyld3/AllImages.cpp | 4 ++++ dyld3/Loading.cpp | 2 +- dyld3/MachOLoaded.cpp | 2 ++ dyld3/Tracing.cpp | 5 +++++ dyld3/Tracing.h | 4 ++++ dyld3/shared-cache/FileUtils.cpp | 6 ++++++ hell.c | 11 +++++++++++ sandbox-dummy.c | 3 +++ src/ImageLoaderMachO.cpp | 4 ++++ src/dyld2.cpp | 21 ++++++++++++++++++--- src/dyldAPIs.cpp | 9 +++++++++ src/dyldAPIsInLibSystem.cpp | 2 ++ src/dyldInitialization.cpp | 7 ++++++- src/dyldNew.cpp | 4 ++++ src/dyld_process_info.cpp | 4 ++++ version.c | 3 +++ 17 files changed, 100 insertions(+), 5 deletions(-) create mode 100755 darling-scripts/add-version-macros.sh create mode 100644 hell.c create mode 100644 sandbox-dummy.c create mode 100644 version.c diff --git a/darling-scripts/add-version-macros.sh b/darling-scripts/add-version-macros.sh new file mode 100755 index 0000000..a423613 --- /dev/null +++ b/darling-scripts/add-version-macros.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -x + +pushd $(dirname "$0") > /dev/null +SCRIPTDIR=$(pwd -P) +popd > /dev/null + +export SRCROOT="$SCRIPTDIR/.." +export SDKROOT="$SRCROOT/../../Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk" + +# copied and modified from dyld.xcodeproj +${SRCROOT}/bin/expand.rb < "${SRCROOT}/include/mach-o/dyld_priv.h" > "${SRCROOT}/include/mach-o/dyld_priv.h.tmp" + +mv "${SRCROOT}/include/mach-o/dyld_priv.h.tmp" "${SRCROOT}/include/mach-o/dyld_priv.h" diff --git a/dyld3/AllImages.cpp b/dyld3/AllImages.cpp index 2c41594..0dca454 100644 --- a/dyld3/AllImages.cpp +++ b/dyld3/AllImages.cpp @@ -61,6 +61,10 @@ extern "C" void __cxa_finalize_ranges(const __cxa_range_t ranges[], unsigned int extern "C" int __cxa_atexit(void (*func)(void *), void* arg, void* dso); +#ifdef DARLING +#define kdebug_is_enabled(...) 0 +#endif + VIS_HIDDEN void* __ptrauth_dyld_address_auth gUseDyld3 = nullptr; diff --git a/dyld3/Loading.cpp b/dyld3/Loading.cpp index ffc92e5..e2a9720 100644 --- a/dyld3/Loading.cpp +++ b/dyld3/Loading.cpp @@ -751,7 +751,7 @@ void Loader::registerDOFs(const Array& dofs) bool Loader::dtraceUserProbesEnabled() { -#if !TARGET_OS_SIMULATOR +#if !TARGET_OS_SIMULATOR && !defined(DARLING) uint8_t dofEnabled = *((uint8_t*)_COMM_PAGE_DTRACE_DOF_ENABLED); return ( (dofEnabled & 1) ); #else diff --git a/dyld3/MachOLoaded.cpp b/dyld3/MachOLoaded.cpp index 13bfca7..c740b20 100644 --- a/dyld3/MachOLoaded.cpp +++ b/dyld3/MachOLoaded.cpp @@ -797,6 +797,7 @@ const uint8_t* MachOLoaded::trieWalk(Diagnostics& diag, const uint8_t* start, co void MachOLoaded::forEachCDHashOfCodeSignature(const void* codeSigStart, size_t codeSignLen, void (^callback)(const uint8_t cdHash[20])) const { +#ifndef DARLING forEachCodeDirectoryBlob(codeSigStart, codeSignLen, ^(const void *cdBuffer) { const CS_CodeDirectory* cd = (const CS_CodeDirectory*)cdBuffer; uint32_t cdLength = htonl(cd->length); @@ -839,6 +840,7 @@ void MachOLoaded::forEachCDHashOfCodeSignature(const void* codeSigStart, size_t return; } }); +#endif } diff --git a/dyld3/Tracing.cpp b/dyld3/Tracing.cpp index 233ef25..e2fcd87 100644 --- a/dyld3/Tracing.cpp +++ b/dyld3/Tracing.cpp @@ -32,6 +32,11 @@ #include "Loading.h" #include "Tracing.h" +#ifdef DARLING +#define kdebug_trace(...) +#define kdebug_is_enabled(...) false +#endif + // Workaround for header issues in rdar://49073930 // #include extern "C" int diff --git a/dyld3/Tracing.h b/dyld3/Tracing.h index fab3fd1..21b84b9 100644 --- a/dyld3/Tracing.h +++ b/dyld3/Tracing.h @@ -35,6 +35,10 @@ #include #include +#ifdef DARLING +#define kdebug_trace_string(...) ((uint64_t)-1) +#endif + #define DBG_DYLD_INTERNAL_SUBCLASS (7) #define DBG_DYLD_API_SUBCLASS (8) diff --git a/dyld3/shared-cache/FileUtils.cpp b/dyld3/shared-cache/FileUtils.cpp index 051c6d7..c27a5dd 100644 --- a/dyld3/shared-cache/FileUtils.cpp +++ b/dyld3/shared-cache/FileUtils.cpp @@ -38,7 +38,9 @@ #include #include #include +#ifndef DARLING #include +#endif #include #include @@ -49,6 +51,10 @@ #include "Diagnostics.h" #include "JSONReader.h" +#ifdef DARLING +static int rootless_check_trusted(const char* path) { return -1; } +static int rootless_check_trusted_class(const char* path, const char* cls) { return -1; } +#endif void iterateDirectoryTree(const std::string& pathPrefix, const std::string& path, bool (^dirFilter)(const std::string& path), void (^fileCallback)(const std::string& path, const struct stat&), bool processFiles, bool recurse) { diff --git a/hell.c b/hell.c new file mode 100644 index 0000000..b2925a4 --- /dev/null +++ b/hell.c @@ -0,0 +1,11 @@ +// This file is plain wrong, broken and horrible. +// The problem is Apple's ld fails to resolve some global variables in the static libraries we link into dyld. +// I have confirmed the problem even on macOS. +// +// If the sought-after symbol is defined (really defined) in a static library, then ld fails to find it. +// When it is defined in an object file passed on the command line, ld has no problem. + +void* __cleanup; +char __gdtoa_locks[32]; +void* _libkernel_functions[16]; + diff --git a/sandbox-dummy.c b/sandbox-dummy.c new file mode 100644 index 0000000..36bc95f --- /dev/null +++ b/sandbox-dummy.c @@ -0,0 +1,3 @@ +int sandbox_check(void) { return 0; } +int SANDBOX_CHECK_NO_REPORT = 0; + diff --git a/src/ImageLoaderMachO.cpp b/src/ImageLoaderMachO.cpp index 5ff8cf4..9242278 100644 --- a/src/ImageLoaderMachO.cpp +++ b/src/ImageLoaderMachO.cpp @@ -2573,6 +2573,9 @@ uintptr_t ImageLoaderMachO::reserveAnAddressRange(size_t length, const ImageLoad { vm_address_t addr = 0; vm_size_t size = length; + // In Darling, we're not the only ones doing memory mapping. + // Therefore, we cannot dictate addresses, because we could (would!) conflict with the ELF loader. +#ifndef DARLING // in PIE programs, load initial dylibs after main executable so they don't have fixed addresses either if ( fgNextPIEDylibAddress != 0 ) { // add small (0-3 pages) random padding between dylibs @@ -2585,6 +2588,7 @@ uintptr_t ImageLoaderMachO::reserveAnAddressRange(size_t length, const ImageLoad } fgNextPIEDylibAddress = 0; } +#endif kern_return_t r = vm_alloc(&addr, size, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_DYLIB)); if ( r != KERN_SUCCESS ) throw "out of address space"; diff --git a/src/dyld2.cpp b/src/dyld2.cpp index 7a5a7fc..3b7f97b 100644 --- a/src/dyld2.cpp +++ b/src/dyld2.cpp @@ -86,7 +86,7 @@ #endif -#if TARGET_OS_SIMULATOR +#if TARGET_OS_SIMULATOR || defined(DARLING) enum { AMFI_DYLD_INPUT_PROC_IN_SIMULATOR = (1 << 0), }; @@ -100,6 +100,9 @@ AMFI_DYLD_OUTPUT_ALLOW_LIBRARY_INTERPOSING = (1 << 6), }; extern "C" int amfi_check_dyld_policy_self(uint64_t input_flags, uint64_t* output_flags); +#ifdef DARLING + int amfi_check_dyld_policy_self(uint64_t input_flags, uint64_t* output_flags) { *output_flags = 0x3F; return 0; } +#endif #else #include #endif @@ -1465,7 +1468,7 @@ static void setRunInitialzersOldWay() static bool sandboxBlocked(const char* path, const char* kind) { -#if TARGET_OS_SIMULATOR +#if TARGET_OS_SIMULATOR || defined(DARLING) // sandbox calls not yet supported in simulator runtime return false; #else @@ -5314,7 +5317,7 @@ static void configureProcessRestrictions(const macho_header* mainExecutableMH, c uint64_t amfiInputFlags = 0; #if TARGET_OS_SIMULATOR amfiInputFlags |= AMFI_DYLD_INPUT_PROC_IN_SIMULATOR; -#elif TARGET_OS_OSX +#elif TARGET_OS_OSX && !defined(DARLING) if ( hasRestrictedSegment(mainExecutableMH) ) amfiInputFlags |= AMFI_DYLD_INPUT_PROC_HAS_RESTRICT_SEG; #elif TARGET_OS_IPHONE @@ -5454,6 +5457,16 @@ void notifyKernelAboutImage(const struct macho_header* mh, const char* fileInfo) #if TARGET_OS_OSX static void* getProcessInfo() { return dyld::gProcessInfo; } +#ifdef DARLING +#undef kdebug_is_enabled +#undef kdebug_trace +#undef kdebug_trace_string +static bool kdebug_is_enabled(uint32_t code) { return false; } +static int kdebug_trace(uint32_t code, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) { return 0;} +static uint64_t kdebug_trace_string(uint32_t debugid, uint64_t str_id, const char *str) { return 0; } +//int (*amfi_check_dyld_policy_self)(uint64_t input_flags, uint64_t* output_flags); +#endif + static const SyscallHelpers sSysCalls = { 14, // added in version 1 @@ -6869,8 +6882,10 @@ _main(const macho_header* mainExecutableMH, uintptr_t mainExecutableSlide, // First test to see if we forced in dyld2 via a kernel boot-arg if ( dyld3::BootArgs::forceDyld2() ) { sClosureMode = ClosureMode::Off; +#ifndef DARLING } else if ( inDenyList(sExecPath) ) { sClosureMode = ClosureMode::Off; +#endif } else if ( sEnv.hasOverride ) { sClosureMode = ClosureMode::Off; } else if ( dyld3::BootArgs::forceDyld3() ) { diff --git a/src/dyldAPIs.cpp b/src/dyldAPIs.cpp index adb0000..428f08f 100644 --- a/src/dyldAPIs.cpp +++ b/src/dyldAPIs.cpp @@ -123,6 +123,12 @@ static NSLinkEditErrors sLastErrorFileCode; static int sLastErrorNo; #endif +#ifdef DARLING +extern "C" int mach_driver_get_dyld_fd(void); +extern "C" void* elfcalls_get_pointer(void); +extern "C" void mach_driver_set_dyld_fd(int fd); +#endif + // In 10.3.x and earlier all the NSObjectFileImage API's were implemeneted in libSystem.dylib // Beginning in 10.4 the NSObjectFileImage API's are implemented in dyld and libSystem just forwards // This conditional keeps support for old libSystem's which needed some help implementing the API's @@ -254,6 +260,9 @@ static const struct dyld_func dyld_funcs[] = { #if OLD_LIBSYSTEM_SUPPORT {"__dyld_link_module", (void*)_dyld_link_module }, #endif +#ifdef DARLING + {"__dyld_get_elfcalls", (void*)elfcalls_get_pointer }, +#endif #pragma clang diagnostic pop #endif //DEPRECATED_APIS_SUPPORTED diff --git a/src/dyldAPIsInLibSystem.cpp b/src/dyldAPIsInLibSystem.cpp index 07e20ac..48dc1ac 100644 --- a/src/dyldAPIsInLibSystem.cpp +++ b/src/dyldAPIsInLibSystem.cpp @@ -780,6 +780,7 @@ const void* address, size_t size, NSObjectFileImage *objectFileImage) { +#ifndef DARLING // NSCreatObjectFileImageFromMemory fail opaquely if Hardened runtime is enabled uint32_t flags; if ( csops(0, CS_OPS_STATUS, &flags, sizeof(flags)) != -1 ) { @@ -788,6 +789,7 @@ NSObjectFileImage *objectFileImage) return NSObjectFileImageAccess; } } +#endif if ( gUseDyld3 ) return dyld3::NSCreateObjectFileImageFromMemory(address, size, objectFileImage); diff --git a/src/dyldInitialization.cpp b/src/dyldInitialization.cpp index 76d6e61..db7a2a9 100644 --- a/src/dyldInitialization.cpp +++ b/src/dyldInitialization.cpp @@ -107,7 +107,9 @@ static void rebaseDyld(const dyld3::MachOLoaded* dyldMH) }); } - +#ifdef DARLING +extern "C" void sigexc_setup(void); +#endif // // This is code to bootstrap dyld. This work in normally done for a program by dyld and crt. @@ -135,6 +137,9 @@ uintptr_t start(const dyld3::MachOLoaded* appsMachHeader, int argc, const char* // set up random value for stack canary __guard_setup(apple); +#ifdef DARLING + sigexc_setup(); +#endif #if DYLD_INITIALIZER_SUPPORT // run all C++ initializers inside dyld runDyldInitializers(argc, argv, envp, apple); diff --git a/src/dyldNew.cpp b/src/dyldNew.cpp index 2bdafba..fdc2212 100644 --- a/src/dyldNew.cpp +++ b/src/dyldNew.cpp @@ -153,7 +153,11 @@ void* calloc(size_t count, size_t size) void* realloc(void *ptr, size_t size) { void* result = malloc(size); +#ifdef DARLING + if (ptr) { memcpy(result, ptr, size); } +#else memcpy(result, ptr, size); +#endif return result; } diff --git a/src/dyld_process_info.cpp b/src/dyld_process_info.cpp index 437d6ac..c455442 100644 --- a/src/dyld_process_info.cpp +++ b/src/dyld_process_info.cpp @@ -229,7 +229,11 @@ private: dyld_process_info_base(dyld_platform_t platform, unsigned imageCount, unsigned aotImageCount, size_t totalSize); void* operator new (size_t, void* buf) { return buf; } +#ifndef DARLING static bool inCache(uint64_t addr) { return (addr > SHARED_REGION_BASE) && (addr < SHARED_REGION_BASE+SHARED_REGION_SIZE); } +#else + static bool inCache(uint64_t addr) { return false; } +#endif bool addImage(task_t task, bool sameCacheAsThisProcess, uint64_t imageAddress, uint64_t imagePath, const char* imagePathLocal); bool addAotImage(dyld_aot_image_info_64 aotImageInfo); diff --git a/version.c b/version.c new file mode 100644 index 0000000..2fb7075 --- /dev/null +++ b/version.c @@ -0,0 +1,3 @@ +const char dyldVersionString[] = "@(#)PROGRAM:dyld PROJECT:dyld-733.6\r"; +const unsigned long long dyldVersionNumber = 0x4086ECCCCCCCCCCDull; +