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