]>
git.saurik.com Git - cycript.git/blob - Trampoline.t.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #define _PTHREAD_ATTR_T
23 #include <pthread_internals.h>
25 #include "Standard.hpp"
28 template <typename Type_
>
29 static _finline
void dlset(Baton
*baton
, Type_
&function
, const char *name
, void *handle
= RTLD_DEFAULT
) {
30 function
= reinterpret_cast<Type_
>(baton
->dlsym(handle
, name
));
35 // XXX: where you find this needs to be relative to CoreFoundation (or something)
36 // XXX: this needs to check if the framework is under PrivateFrameworks instead
37 #define Framework(framework) \
38 "/System/Library/Frameworks/" #framework ".framework/" #framework
40 void *Routine(void *arg
) {
41 Baton
*baton(reinterpret_cast<Baton
*>(arg
));
43 int (*pthread_detach
)(pthread_t
);
44 dlset(baton
, pthread_detach
, "pthread_detach");
46 pthread_t (*pthread_self
)();
47 dlset(baton
, pthread_self
, "pthread_self");
49 pthread_detach(pthread_self());
51 void *(*dlopen
)(const char *, int);
52 dlset(baton
, dlopen
, "dlopen");
54 if (baton
->dlsym(RTLD_DEFAULT
, "JSEvaluateScript") == NULL
)
55 dlopen(Framework(JavaScriptCore
), RTLD_GLOBAL
| RTLD_LAZY
);
57 void *(*objc_getClass
)(const char *);
58 dlset(baton
, objc_getClass
, "objc_getClass");
60 if (objc_getClass("WebUndefined") == NULL
)
61 dlopen(Framework(WebKit
), RTLD_GLOBAL
| RTLD_LAZY
);
63 void *handle(dlopen(baton
->library
, RTLD_LAZY
| RTLD_LOCAL
));
69 void (*CYHandleServer
)(pid_t
);
70 dlset(baton
, CYHandleServer
, "CYHandleServer", handle
);
71 if (CYHandleServer
== NULL
) {
76 CYHandleServer(baton
->pid
);
80 static void $
bzero(void *data
, size_t size
) {
81 char *bytes(reinterpret_cast<char *>(data
));
82 for (size_t i(0); i
!= size
; ++i
)
86 extern "C" void Start(Baton
*baton
) {
88 $
bzero(&self
, sizeof(self
));
90 // this code comes from _pthread_set_self
92 baton
->__pthread_set_self(&self
);
94 //int (*pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
95 //dlset(baton, pthread_create, "pthread_create");
98 baton
->pthread_create(&thread
, NULL
, &Routine
, baton
);
100 //mach_port_t (*mach_thread_self)();
101 //dlset(baton, mach_thread_self, "mach_thread_self");
103 //kern_return_t (*thread_terminate)(thread_act_t);
104 //dlset(baton, thread_terminate, "thread_terminate");
106 baton
->thread_terminate(baton
->mach_thread_self());