X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/38ae783db781810a19b3103bb55c81ea70e7d952..ab7588a8f7461ad7b046675e993cf91eca1db3ec:/Trampoline.t.cpp diff --git a/Trampoline.t.cpp b/Trampoline.t.cpp index aff0296..0ae088d 100644 --- a/Trampoline.t.cpp +++ b/Trampoline.t.cpp @@ -19,16 +19,21 @@ **/ /* }}} */ +#include +#if defined(__arm__) || defined(__arm64__) +#undef TARGET_IPHONE_SIMULATOR +#define TARGET_IPHONE_SIMULATOR 1 +#endif #define _PTHREAD_ATTR_T #include +#if defined(__arm__) || defined(__arm64__) +#undef TARGET_IPHONE_SIMULATOR +#endif #include #include #include - -extern "C" { #include -} #include "Standard.hpp" #include "Baton.hpp" @@ -47,6 +52,18 @@ static int $strcmp(const char *lhs, const char *rhs) { } return *lhs < *rhs ? -1 : 1; } +static void $strlcpy(char *dst, const char *src, size_t size) { + if (size == 0) + return; + size_t i(0); + while (i != size - 1) { + char value(src[i]); + if (value == '\0') + break; + dst[i++] = value; + } dst[i] = '\0'; +} + #ifdef __LP64__ typedef struct mach_header_64 mach_header_xx; typedef struct nlist_64 nlist_xx; @@ -78,18 +95,26 @@ static const uint32_t MH_MAGIC_XX = MH_MAGIC; return NULL; \ else if (const type *command = reinterpret_cast(lcp)) -static void *Symbol(struct dyld_all_image_infos *infos, const char *library, const char *name) { - const dyld_image_info *info(NULL); - for (uint32_t i(0); i != infos->infoArrayCount; ++i) - if ($strcmp(infos->infoArray[i].imageFilePath, library) == 0) - info = &infos->infoArray[i]; - if (info == NULL) - return NULL; +static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) { + for (uint32_t i(0); i != infos->infoArrayCount; ++i) { + const dyld_image_info &info(infos->infoArray[i]); + const mach_header_xx *mach(reinterpret_cast(info.imageLoadAddress)); + if (mach->magic != MH_MAGIC_XX) + continue; - const mach_header_xx *mach(reinterpret_cast(info->imageLoadAddress)); - if (mach->magic != MH_MAGIC_XX) - return NULL; + const char *path(info.imageFilePath); + forlc (dylib, mach, LC_ID_DYLIB, dylib_command) + path = reinterpret_cast(dylib) + dylib->dylib.name.offset; + if ($strcmp(path, name) != 0) + continue; + + return mach; + } + + return NULL; +} +static void *Symbol(const mach_header_xx *mach, const char *name) { const struct symtab_command *stp(NULL); forlc (command, mach, LC_SYMTAB, struct symtab_command) stp = command; @@ -125,6 +150,11 @@ static void *Symbol(struct dyld_all_image_infos *infos, const char *library, con if (value == 0) continue; +#ifdef __arm__ + if ((symbol->n_desc & N_ARM_THUMB_DEF) != 0) + value |= 0x00000001; +#endif + value += slide; return reinterpret_cast(value); } @@ -132,66 +162,44 @@ static void *Symbol(struct dyld_all_image_infos *infos, const char *library, con return NULL; } -struct Dynamic { - char *(*dlerror)(); - void *(*dlsym)(void *, const char *); -}; - template -static _finline void dlset(Dynamic *dynamic, Type_ &function, const char *name, void *handle = RTLD_DEFAULT) { - function = reinterpret_cast(dynamic->dlsym(handle, name)); - if (function == NULL) - dynamic->dlerror(); +static _finline void cyset(Type_ &function, const char *name, const mach_header_xx *mach) { + function = reinterpret_cast(Symbol(mach, name)); } -template -static _finline void cyset(Baton *baton, Type_ &function, const char *name, const char *library) { +static _finline const mach_header_xx *Library(Baton *baton, const char *name) { struct dyld_all_image_infos *infos(reinterpret_cast(baton->dyld)); - function = reinterpret_cast(Symbol(infos, library, name)); + return Library(infos, name); } -// XXX: where you find this needs to be relative to CoreFoundation (or something) -// XXX: this needs to check if the framework is under PrivateFrameworks instead -#define Framework(framework) \ - "/System/Library/Frameworks/" #framework ".framework/" #framework - void *Routine(void *arg) { Baton *baton(reinterpret_cast(arg)); - Dynamic dynamic; - cyset(baton, dynamic.dlerror, "_dlerror", "/usr/lib/system/libdyld.dylib"); - cyset(baton, dynamic.dlsym, "_dlsym", "/usr/lib/system/libdyld.dylib"); - - int (*pthread_detach)(pthread_t); - dlset(&dynamic, pthread_detach, "pthread_detach"); - - pthread_t (*pthread_self)(); - dlset(&dynamic, pthread_self, "pthread_self"); + const mach_header_xx *dyld(NULL); + if (dyld == NULL) + dyld = Library(baton, "/usr/lib/system/libdyld.dylib"); + if (dyld == NULL) + dyld = Library(baton, "/usr/lib/libSystem.B.dylib"); - pthread_detach(pthread_self()); + char *(*$dlerror)(); + cyset($dlerror, "_dlerror", dyld); - void *(*dlopen)(const char *, int); - dlset(&dynamic, dlopen, "dlopen"); + void *(*$dlopen)(const char *, int); + cyset($dlopen, "_dlopen", dyld); - if (dynamic.dlsym(RTLD_DEFAULT, "JSEvaluateScript") == NULL) - dlopen(Framework(JavaScriptCore), RTLD_GLOBAL | RTLD_LAZY); - - void *(*objc_getClass)(const char *); - dlset(&dynamic, objc_getClass, "objc_getClass"); - - if (objc_getClass("WebUndefined") == NULL) - dlopen(Framework(WebKit), RTLD_GLOBAL | RTLD_LAZY); - - void *handle(dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL)); + void *handle($dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL)); if (handle == NULL) { - dynamic.dlerror(); + $strlcpy(baton->error, $dlerror(), sizeof(baton->error)); return NULL; } + void *(*$dlsym)(void *, const char *); + cyset($dlsym, "_dlsym", dyld); + void (*CYHandleServer)(pid_t); - dlset(&dynamic, CYHandleServer, "CYHandleServer", handle); + CYHandleServer = reinterpret_cast($dlsym(handle, "CYHandleServer")); if (CYHandleServer == NULL) { - dynamic.dlerror(); + $strlcpy(baton->error, $dlerror(), sizeof(baton->error)); return NULL; } @@ -203,23 +211,81 @@ extern "C" void Start(Baton *baton) { struct _pthread self; $bzero(&self, sizeof(self)); + const mach_header_xx *pthread(NULL); + if (pthread == NULL) + pthread = Library(baton, "/usr/lib/system/libsystem_pthread.dylib"); + if (pthread == NULL) + pthread = Library(baton, "/usr/lib/system/libsystem_c.dylib"); + if (pthread == NULL) + pthread = Library(baton, "/usr/lib/libSystem.B.dylib"); + void (*$__pthread_set_self)(pthread_t); - cyset(baton, $__pthread_set_self, "___pthread_set_self", "/usr/lib/system/libsystem_c.dylib"); + cyset($__pthread_set_self, "___pthread_set_self", pthread); self.tsd[0] = &self; $__pthread_set_self(&self); + int (*$pthread_attr_init)(pthread_attr_t *); + cyset($pthread_attr_init, "_pthread_attr_init", pthread); + +#if 0 + pthread_attr_t attr; + $pthread_attr_init(&attr); + + int (*$pthread_attr_setdetachstate)(pthread_attr_t *, int); + cyset($pthread_attr_setdetachstate, "_pthread_attr_setdetachstate", pthread); + + $pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); +#endif + int (*$pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *); - cyset(baton, $pthread_create, "_pthread_create", "/usr/lib/system/libsystem_c.dylib"); + cyset($pthread_create, "_pthread_create", pthread); pthread_t thread; $pthread_create(&thread, NULL, &Routine, baton); +#if 0 + int (*$pthread_attr_destroy)(pthread_attr_t *); + cyset($pthread_attr_destroy, "_pthread_attr_destroy", pthread); + + $pthread_attr_destroy(&attr); +#endif + +#if defined(__arm__) || defined(__arm64__) + uintptr_t tpid; +#if defined(__arm__) + __asm__ ("mrc p15, 0, %0, c13, c0, 3\n" : "=r" (tpid)); +#elif defined(__arm64__) + __asm__ ("mrs %0, tpidrro_el0\n" : "=r" (tpid)); +#else +#error XXX +#endif + + void **tsd; + tsd = reinterpret_cast(tpid & ~3); + if (tsd != NULL) + tsd[0] = &self; +#else + _pthread_setspecific_direct(0, &self); +#endif + + int (*$pthread_join)(pthread_t, void **); + cyset($pthread_join, "_pthread_join", pthread); + + void *status; + $pthread_join(thread, &status); + + const mach_header_xx *kernel(NULL); + if (kernel == NULL) + kernel = Library(baton, "/usr/lib/system/libsystem_kernel.dylib"); + if (kernel == NULL) + kernel = Library(baton, "/usr/lib/libSystem.B.dylib"); + mach_port_t (*$mach_thread_self)(); - cyset(baton, $mach_thread_self, "_mach_thread_self", "/usr/lib/system/libsystem_kernel.dylib"); + cyset($mach_thread_self, "_mach_thread_self", kernel); kern_return_t (*$thread_terminate)(thread_act_t); - cyset(baton, $thread_terminate, "_thread_terminate", "/usr/lib/system/libsystem_kernel.dylib"); + cyset($thread_terminate, "_thread_terminate", kernel); $thread_terminate($mach_thread_self()); }