]> git.saurik.com Git - cycript.git/blame - Trampoline.t.cpp
Restructure Mach/Inject for iOS 4.3.
[cycript.git] / Trampoline.t.cpp
CommitLineData
b3378a02
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
e91fbe93
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
e91fbe93 6/*
b3378a02
JF
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
e91fbe93 11 *
b3378a02
JF
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
e91fbe93 16 *
b3378a02
JF
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
e91fbe93
JF
20/* }}} */
21
b6961e53
JF
22#define _PTHREAD_ATTR_T
23#include <pthread_internals.h>
24
eed4f174 25#include "Standard.hpp"
b6961e53
JF
26#include "Baton.hpp"
27
eed4f174
JF
28template <typename Type_>
29static _finline void dlset(Baton *baton, Type_ &function, const char *name, void *handle = RTLD_DEFAULT) {
30 function = reinterpret_cast<Type_>(baton->dlsym(handle, name));
95a2c7e5
JF
31 if (function == NULL)
32 baton->dlerror();
eed4f174
JF
33}
34
7cdfdc9f
JF
35#define Framework(framework) \
36 "/System/Library/Frameworks/" #framework ".framework/" #framework
37
b4b71b79 38static void *Routine(void *arg) {
eed4f174
JF
39 Baton *baton(reinterpret_cast<Baton *>(arg));
40
41 void *(*dlopen)(const char *, int);
42 dlset(baton, dlopen, "dlopen");
43
5d7cc6d5 44 if (baton->dlsym(RTLD_DEFAULT, "JSEvaluateScript") == NULL)
7cdfdc9f 45 dlopen(Framework(JavaScriptCore), RTLD_GLOBAL | RTLD_LAZY);
5d7cc6d5
JF
46
47 void *(*objc_getClass)(const char *);
48 dlset(baton, objc_getClass, "objc_getClass");
49
50 if (objc_getClass("WebUndefined") == NULL)
7cdfdc9f 51 dlopen(Framework(WebKit), RTLD_GLOBAL | RTLD_LAZY);
5d7cc6d5 52
eed4f174 53 void *handle(dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
95a2c7e5
JF
54 if (handle == NULL) {
55 baton->dlerror();
56 return NULL;
57 }
eed4f174
JF
58
59 void (*CYHandleServer)(pid_t);
60 dlset(baton, CYHandleServer, "CYHandleServer", handle);
61
62 CYHandleServer(baton->pid);
63
95a2c7e5 64 return NULL;
eed4f174
JF
65}
66
b4b71b79
JF
67static void *Thread(void *arg) {
68 Baton *baton(reinterpret_cast<Baton *>(arg));
eed4f174
JF
69
70 int (*pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
71 dlset(baton, pthread_create, "pthread_create");
b6961e53
JF
72
73 pthread_t thread;
b4b71b79 74 pthread_create(&thread, NULL, &Routine, baton);
b6961e53 75
eed4f174
JF
76 int (*pthread_join)(pthread_t, void **);
77 dlset(baton, pthread_join, "pthread_join");
78
b6961e53 79 void *result;
b4b71b79 80 pthread_join(thread, &result);
b6961e53 81
b4b71b79
JF
82 return NULL;
83}
eed4f174 84
b4b71b79
JF
85extern "C" void Start(Baton *baton) {
86 struct _pthread self;
87 baton->_pthread_start(&self, NULL, &Thread, baton, 8 * 1024, 0);
b6961e53 88}