X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/3615a2f7f3a1a066c6c7e3ef66724a9398c238c2..278e0646badec22a6a49e09cee07078b70e3017c:/Trampoline.t.cpp diff --git a/Trampoline.t.cpp b/Trampoline.t.cpp index 297486e..4ebf820 100644 --- a/Trampoline.t.cpp +++ b/Trampoline.t.cpp @@ -20,12 +20,15 @@ /* }}} */ #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 -#define TARGET_IPHONE_SIMULATOR 0 +#endif #include #include @@ -49,6 +52,38 @@ 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 (src == NULL) + src = "(null)"; + 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'; +} + +__attribute__((__unused__)) +static void $snprintfp(char *dst, size_t size, const void *pointer) { + uintptr_t value(reinterpret_cast(pointer)); + char buffer[32]; + char *end(buffer + sizeof(buffer)); + *--end = '\0'; + if (value == 0) + *--end = '0'; + else do { + unsigned digit(value & 0xf); + value >>= 4; + *--end = (digit < 10 ? '0' : 'a' - 10) + digit; + } while (value != 0); + *--end = 'x'; + *--end = '0'; + $strlcpy(dst, end, size); +} + #ifdef __LP64__ typedef struct mach_header_64 mach_header_xx; typedef struct nlist_64 nlist_xx; @@ -174,21 +209,21 @@ void *Routine(void *arg) { void *handle($dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL)); if (handle == NULL) { - $dlerror(); + $strlcpy(baton->error, $dlerror(), sizeof(baton->error)); return NULL; } void *(*$dlsym)(void *, const char *); cyset($dlsym, "_dlsym", dyld); - void (*CYHandleServer)(pid_t); - CYHandleServer = reinterpret_cast($dlsym(handle, "CYHandleServer")); + void (*CYHandleServer)(pid_t, char *, size_t); + CYHandleServer = reinterpret_cast($dlsym(handle, "CYHandleServer")); if (CYHandleServer == NULL) { - $dlerror(); + $strlcpy(baton->error, $dlerror(), sizeof(baton->error)); return NULL; } - CYHandleServer(baton->pid); + CYHandleServer(baton->pid, baton->error, sizeof(baton->error)); return NULL; } @@ -204,11 +239,11 @@ extern "C" void Start(Baton *baton) { if (pthread == NULL) pthread = Library(baton, "/usr/lib/libSystem.B.dylib"); - void (*$__pthread_set_self)(pthread_t); + void (*$__pthread_set_self)(void **); cyset($__pthread_set_self, "___pthread_set_self", pthread); self.tsd[0] = &self; - $__pthread_set_self(&self); + $__pthread_set_self(&self.tsd[0]); int (*$pthread_attr_init)(pthread_attr_t *); cyset($pthread_attr_init, "_pthread_attr_init", pthread); @@ -236,22 +271,6 @@ extern "C" void Start(Baton *baton) { $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; -#endif - int (*$pthread_join)(pthread_t, void **); cyset($pthread_join, "_pthread_join", pthread);