]> git.saurik.com Git - cycript.git/blob - Trampoline.t.cpp
b3bf31dc49cc741b51631f52976460ab6fce000d
[cycript.git] / Trampoline.t.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
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/>.
19 **/
20 /* }}} */
21
22 #include <TargetConditionals.h>
23 #if defined(__arm__) || defined(__arm64__)
24 #undef TARGET_IPHONE_SIMULATOR
25 #define TARGET_IPHONE_SIMULATOR 1
26 #endif
27 #define _PTHREAD_ATTR_T
28 #include <pthread_internals.h>
29 #if defined(__arm__) || defined(__arm64__)
30 #undef TARGET_IPHONE_SIMULATOR
31 #endif
32
33 #include <mach-o/dyld.h>
34 #include <mach-o/dyld_images.h>
35 #include <mach-o/loader.h>
36 #include <mach-o/nlist.h>
37
38 #include "Standard.hpp"
39 #include "Baton.hpp"
40
41 static void $bzero(void *data, size_t size) {
42 char *bytes(reinterpret_cast<char *>(data));
43 for (size_t i(0); i != size; ++i)
44 bytes[i] = 0;
45 }
46
47 static int $strcmp(const char *lhs, const char *rhs) {
48 while (*lhs == *rhs) {
49 if (*lhs == '\0')
50 return 0;
51 ++lhs, ++rhs;
52 } return *lhs < *rhs ? -1 : 1;
53 }
54
55 static void $strlcpy(char *dst, const char *src, size_t size) {
56 if (src == NULL)
57 src = "(null)";
58 if (size == 0)
59 return;
60 size_t i(0);
61 while (i != size - 1) {
62 char value(src[i]);
63 if (value == '\0')
64 break;
65 dst[i++] = value;
66 } dst[i] = '\0';
67 }
68
69 #ifdef __LP64__
70 typedef struct mach_header_64 mach_header_xx;
71 typedef struct nlist_64 nlist_xx;
72 typedef struct segment_command_64 segment_command_xx;
73
74 static const uint32_t LC_SEGMENT_XX = LC_SEGMENT_64;
75 static const uint32_t MH_MAGIC_XX = MH_MAGIC_64;
76 #else
77 typedef struct mach_header mach_header_xx;
78 typedef struct nlist nlist_xx;
79 typedef struct segment_command segment_command_xx;
80
81 static const uint32_t LC_SEGMENT_XX = LC_SEGMENT;
82 static const uint32_t MH_MAGIC_XX = MH_MAGIC;
83 #endif
84
85 #define forlc(command, mach, lc, type) \
86 if (const struct load_command *load_commands = reinterpret_cast<const struct load_command *>(mach + 1)) \
87 if (const struct load_command *lcp = load_commands) \
88 for (uint32_t i(0); i != mach->ncmds; ++i, lcp = reinterpret_cast<const struct load_command *>(reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize)) \
89 if ( \
90 lcp->cmdsize % sizeof(long) != 0 || lcp->cmdsize <= 0 || \
91 reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize > reinterpret_cast<const uint8_t *>(load_commands) + mach->sizeofcmds \
92 ) \
93 return NULL; \
94 else if (lcp->cmd != lc) \
95 continue; \
96 else if (lcp->cmdsize < sizeof(type)) \
97 return NULL; \
98 else if (const type *command = reinterpret_cast<const type *>(lcp))
99
100 static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) {
101 for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
102 const dyld_image_info &info(infos->infoArray[i]);
103 const mach_header_xx *mach(reinterpret_cast<const mach_header_xx *>(info.imageLoadAddress));
104 if (mach->magic != MH_MAGIC_XX)
105 continue;
106
107 const char *path(info.imageFilePath);
108 forlc (dylib, mach, LC_ID_DYLIB, dylib_command)
109 path = reinterpret_cast<const char *>(dylib) + dylib->dylib.name.offset;
110 if ($strcmp(path, name) != 0)
111 continue;
112
113 return mach;
114 }
115
116 return NULL;
117 }
118
119 static void *Symbol(const mach_header_xx *mach, const char *name) {
120 const struct symtab_command *stp(NULL);
121 forlc (command, mach, LC_SYMTAB, struct symtab_command)
122 stp = command;
123 if (stp == NULL)
124 return NULL;
125
126 size_t slide(_not(size_t));
127 const nlist_xx *symbols(NULL);
128 const char *strings(NULL);
129
130 forlc (segment, mach, LC_SEGMENT_XX, segment_command_xx) {
131 if (segment->fileoff == 0)
132 slide = reinterpret_cast<size_t>(mach) - segment->vmaddr;
133 if (stp->symoff >= segment->fileoff && stp->symoff < segment->fileoff + segment->filesize)
134 symbols = reinterpret_cast<const nlist_xx *>(stp->symoff - segment->fileoff + segment->vmaddr + slide);
135 if (stp->stroff >= segment->fileoff && stp->stroff < segment->fileoff + segment->filesize)
136 strings = reinterpret_cast<const char *>(stp->stroff - segment->fileoff + segment->vmaddr + slide);
137 }
138
139 if (slide == _not(size_t) || symbols == NULL || strings == NULL)
140 return NULL;
141
142 for (size_t i(0); i != stp->nsyms; ++i) {
143 const nlist_xx *symbol(&symbols[i]);
144 if (symbol->n_un.n_strx == 0 || (symbol->n_type & N_STAB) != 0)
145 continue;
146
147 const char *nambuf(strings + symbol->n_un.n_strx);
148 if ($strcmp(name, nambuf) != 0)
149 continue;
150
151 uintptr_t value(symbol->n_value);
152 if (value == 0)
153 continue;
154
155 #ifdef __arm__
156 if ((symbol->n_desc & N_ARM_THUMB_DEF) != 0)
157 value |= 0x00000001;
158 #endif
159
160 value += slide;
161 return reinterpret_cast<void *>(value);
162 }
163
164 return NULL;
165 }
166
167 template <typename Type_>
168 static _finline void cyset(Type_ &function, const char *name, const mach_header_xx *mach) {
169 function = reinterpret_cast<Type_>(Symbol(mach, name));
170 }
171
172 static _finline const mach_header_xx *Library(Baton *baton, const char *name) {
173 struct dyld_all_image_infos *infos(reinterpret_cast<struct dyld_all_image_infos *>(baton->dyld));
174 return Library(infos, name);
175 }
176
177 void *Routine(void *arg) {
178 Baton *baton(reinterpret_cast<Baton *>(arg));
179
180 const mach_header_xx *dyld(NULL);
181 if (dyld == NULL)
182 dyld = Library(baton, "/usr/lib/system/libdyld.dylib");
183 if (dyld == NULL)
184 dyld = Library(baton, "/usr/lib/libSystem.B.dylib");
185
186 char *(*$dlerror)();
187 cyset($dlerror, "_dlerror", dyld);
188
189 void *(*$dlopen)(const char *, int);
190 cyset($dlopen, "_dlopen", dyld);
191
192 void *handle($dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
193 if (handle == NULL) {
194 $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
195 return NULL;
196 }
197
198 void *(*$dlsym)(void *, const char *);
199 cyset($dlsym, "_dlsym", dyld);
200
201 void (*CYHandleServer)(pid_t, char *, size_t);
202 CYHandleServer = reinterpret_cast<void (*)(pid_t, char *, size_t)>($dlsym(handle, "CYHandleServer"));
203 if (CYHandleServer == NULL) {
204 $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
205 return NULL;
206 }
207
208 CYHandleServer(baton->pid, baton->error, sizeof(baton->error));
209 return NULL;
210 }
211
212 extern "C" void Start(Baton *baton) {
213 struct _pthread self;
214 $bzero(&self, sizeof(self));
215
216 const mach_header_xx *pthread(NULL);
217 if (pthread == NULL)
218 pthread = Library(baton, "/usr/lib/system/libsystem_pthread.dylib");
219 if (pthread == NULL)
220 pthread = Library(baton, "/usr/lib/system/libsystem_c.dylib");
221 if (pthread == NULL)
222 pthread = Library(baton, "/usr/lib/libSystem.B.dylib");
223
224 void (*$__pthread_set_self)(void **);
225 cyset($__pthread_set_self, "___pthread_set_self", pthread);
226
227 self.tsd[0] = &self;
228 $__pthread_set_self(&self.tsd[0]);
229
230 int (*$pthread_attr_init)(pthread_attr_t *);
231 cyset($pthread_attr_init, "_pthread_attr_init", pthread);
232
233 #if 0
234 pthread_attr_t attr;
235 $pthread_attr_init(&attr);
236
237 int (*$pthread_attr_setdetachstate)(pthread_attr_t *, int);
238 cyset($pthread_attr_setdetachstate, "_pthread_attr_setdetachstate", pthread);
239
240 $pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
241 #endif
242
243 int (*$pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
244 cyset($pthread_create, "_pthread_create", pthread);
245
246 pthread_t thread;
247 $pthread_create(&thread, NULL, &Routine, baton);
248
249 #if 0
250 int (*$pthread_attr_destroy)(pthread_attr_t *);
251 cyset($pthread_attr_destroy, "_pthread_attr_destroy", pthread);
252
253 $pthread_attr_destroy(&attr);
254 #endif
255
256 int (*$pthread_join)(pthread_t, void **);
257 cyset($pthread_join, "_pthread_join", pthread);
258
259 void *status;
260 $pthread_join(thread, &status);
261
262 const mach_header_xx *kernel(NULL);
263 if (kernel == NULL)
264 kernel = Library(baton, "/usr/lib/system/libsystem_kernel.dylib");
265 if (kernel == NULL)
266 kernel = Library(baton, "/usr/lib/libSystem.B.dylib");
267
268 mach_port_t (*$mach_thread_self)();
269 cyset($mach_thread_self, "_mach_thread_self", kernel);
270
271 kern_return_t (*$thread_terminate)(thread_act_t);
272 cyset($thread_terminate, "_thread_terminate", kernel);
273
274 $thread_terminate($mach_thread_self());
275 }