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