]>
Commit | Line | Data |
---|---|---|
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 |
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 | ||
8964aa08 JF |
55 | static 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__ |
68 | typedef struct mach_header_64 mach_header_xx; | |
69 | typedef struct nlist_64 nlist_xx; | |
70 | typedef struct segment_command_64 segment_command_xx; | |
71 | ||
72 | static const uint32_t LC_SEGMENT_XX = LC_SEGMENT_64; | |
73 | static const uint32_t MH_MAGIC_XX = MH_MAGIC_64; | |
74 | #else | |
75 | typedef struct mach_header mach_header_xx; | |
76 | typedef struct nlist nlist_xx; | |
77 | typedef struct segment_command segment_command_xx; | |
78 | ||
79 | static const uint32_t LC_SEGMENT_XX = LC_SEGMENT; | |
80 | static 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 |
98 | static 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 | ||
117 | static 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 | 165 | template <typename Type_> |
85b57b57 JF |
166 | static _finline void cyset(Type_ &function, const char *name, const mach_header_xx *mach) { |
167 | function = reinterpret_cast<Type_>(Symbol(mach, name)); | |
168 | } | |
169 | ||
170 | static _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 | 175 | void *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 | ||
eed4f174 | 199 | void (*CYHandleServer)(pid_t); |
3615a2f7 | 200 | CYHandleServer = reinterpret_cast<void (*)(pid_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 | |
6e51aaf8 | 206 | CYHandleServer(baton->pid); |
95a2c7e5 | 207 | return NULL; |
eed4f174 JF |
208 | } |
209 | ||
49e976ae JF |
210 | extern "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 | |
38ae783d | 222 | void (*$__pthread_set_self)(pthread_t); |
85b57b57 | 223 | cyset($__pthread_set_self, "___pthread_set_self", pthread); |
38ae783d | 224 | |
49e976ae | 225 | self.tsd[0] = &self; |
38ae783d | 226 | $__pthread_set_self(&self); |
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 | ||
254 | #if defined(__arm__) || defined(__arm64__) | |
255 | uintptr_t tpid; | |
256 | #if defined(__arm__) | |
257 | __asm__ ("mrc p15, 0, %0, c13, c0, 3\n" : "=r" (tpid)); | |
258 | #elif defined(__arm64__) | |
259 | __asm__ ("mrs %0, tpidrro_el0\n" : "=r" (tpid)); | |
260 | #else | |
261 | #error XXX | |
262 | #endif | |
263 | ||
264 | void **tsd; | |
265 | tsd = reinterpret_cast<void **>(tpid & ~3); | |
266 | if (tsd != NULL) | |
267 | tsd[0] = &self; | |
e1858938 JF |
268 | #else |
269 | _pthread_setspecific_direct(0, &self); | |
3615a2f7 JF |
270 | #endif |
271 | ||
272 | int (*$pthread_join)(pthread_t, void **); | |
273 | cyset($pthread_join, "_pthread_join", pthread); | |
274 | ||
275 | void *status; | |
276 | $pthread_join(thread, &status); | |
277 | ||
278 | const mach_header_xx *kernel(NULL); | |
279 | if (kernel == NULL) | |
280 | kernel = Library(baton, "/usr/lib/system/libsystem_kernel.dylib"); | |
281 | if (kernel == NULL) | |
282 | kernel = Library(baton, "/usr/lib/libSystem.B.dylib"); | |
85b57b57 | 283 | |
38ae783d | 284 | mach_port_t (*$mach_thread_self)(); |
85b57b57 | 285 | cyset($mach_thread_self, "_mach_thread_self", kernel); |
eed4f174 | 286 | |
38ae783d | 287 | kern_return_t (*$thread_terminate)(thread_act_t); |
85b57b57 | 288 | cyset($thread_terminate, "_thread_terminate", kernel); |
49e976ae | 289 | |
38ae783d | 290 | $thread_terminate($mach_thread_self()); |
b6961e53 | 291 | } |