]> git.saurik.com Git - cycript.git/blobdiff - Trampoline.t.cpp
Update copyright for 2014 and relicense to AGPLv3.
[cycript.git] / Trampoline.t.cpp
index 4ebf8200d2f587007ded3df6faf796364cc22e6e..5a51be04494564541e5150afd21bdadb13b508b8 100644 (file)
@@ -1,21 +1,21 @@
 /* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2013  Jay Freeman (saurik)
+ * Copyright (C) 2009-2014  Jay Freeman (saurik)
 */
 
-/* GNU General Public License, Version 3 {{{ */
+/* GNU Affero General Public License, Version 3 {{{ */
 /*
- * Cycript is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published
- * by the Free Software Foundation, either version 3 of the License,
- * or (at your option) any later version.
- *
- * Cycript is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Cycript.  If not, see <http://www.gnu.org/licenses/>.
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
 /* }}} */
 
@@ -44,6 +44,12 @@ static void $bzero(void *data, size_t size) {
         bytes[i] = 0;
 }
 
+__attribute__((__unused__))
+static void $memcpy(char *dst, const char *src, size_t size) {
+    for (size_t i(0); i != size; ++i)
+        dst[i] = src[i];
+}
+
 static int $strcmp(const char *lhs, const char *rhs) {
     while (*lhs == *rhs) {
         if (*lhs == '\0')
@@ -66,6 +72,26 @@ static void $strlcpy(char *dst, const char *src, size_t size) {
     } dst[i] = '\0';
 }
 
+__attribute__((__unused__))
+static char *$strstr(const char *haystack, const char *needle) {
+    if (*needle == '\0')
+        return NULL;
+    for (; *haystack != '\0'; ++haystack)
+        for (size_t i(0); ; ++i)
+            if (needle[i] == '\0')
+                return const_cast<char *>(haystack);
+            else if (needle[i] != haystack[i])
+                break;
+    return NULL;
+}
+
+__attribute__((__unused__))
+static size_t $strlen(const char *data) {
+    for (size_t i(0); ; ++i)
+        if (data[i] == '\0')
+            return i;
+}
+
 __attribute__((__unused__))
 static void $snprintfp(char *dst, size_t size, const void *pointer) {
     uintptr_t value(reinterpret_cast<uintptr_t>(pointer));
@@ -108,11 +134,11 @@ static const uint32_t MH_MAGIC_XX = MH_MAGIC;
                     lcp->cmdsize % sizeof(long) != 0 || lcp->cmdsize <= 0 || \
                     reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize > reinterpret_cast<const uint8_t *>(load_commands) + mach->sizeofcmds \
                 ) \
-                    return NULL; \
+                    break; \
                 else if (lcp->cmd != lc) \
                     continue; \
                 else if (lcp->cmdsize < sizeof(type)) \
-                    return NULL; \
+                    break; \
                 else if (const type *command = reinterpret_cast<const type *>(lcp))
 
 static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) {
@@ -192,6 +218,22 @@ static _finline const mach_header_xx *Library(Baton *baton, const char *name) {
     return Library(infos, name);
 }
 
+#if defined(__i386__) || defined(__x86_64__)
+static bool Simulator(struct dyld_all_image_infos *infos) {
+    for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
+        const dyld_image_info &info(infos->infoArray[i]);
+        const char *path(info.imageFilePath);
+        if ($strstr(path, "/SDKs/iPhoneSimulator") != NULL)
+            return true;
+    } return false;
+}
+
+static bool Simulator(Baton *baton) {
+    struct dyld_all_image_infos *infos(reinterpret_cast<struct dyld_all_image_infos *>(baton->dyld));
+    return Simulator(infos);
+}
+#endif
+
 void *Routine(void *arg) {
     Baton *baton(reinterpret_cast<Baton *>(arg));
 
@@ -207,6 +249,12 @@ void *Routine(void *arg) {
     void *(*$dlopen)(const char *, int);
     cyset($dlopen, "_dlopen", dyld);
 
+#if defined(__i386__) || defined(__x86_64__)
+    size_t length($strlen(baton->library));
+    if (length >= 10 && $strcmp(baton->library + length - 10, "-###.dylib") == 0)
+        $memcpy(baton->library + length - 10, Simulator(baton) ? "-sim" : "-sys", 4);
+#endif
+
     void *handle($dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
     if (handle == NULL) {
         $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
@@ -216,14 +264,14 @@ void *Routine(void *arg) {
     void *(*$dlsym)(void *, const char *);
     cyset($dlsym, "_dlsym", dyld);
 
-    void (*CYHandleServer)(pid_t, char *, size_t);
-    CYHandleServer = reinterpret_cast<void (*)(pid_t, char *, size_t)>($dlsym(handle, "CYHandleServer"));
+    void (*CYHandleServer)(pid_t);
+    CYHandleServer = reinterpret_cast<void (*)(pid_t)>($dlsym(handle, "CYHandleServer"));
     if (CYHandleServer == NULL) {
         $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
         return NULL;
     }
 
-    CYHandleServer(baton->pid, baton->error, sizeof(baton->error));
+    CYHandleServer(baton->pid);
     return NULL;
 }