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 #include <TargetConditionals.h>
23 #if defined(__arm__) || defined(__arm64__)
24 #undef TARGET_IPHONE_SIMULATOR
25 #define TARGET_IPHONE_SIMULATOR 1
27 #define _PTHREAD_ATTR_T
28 #include <pthread_internals.h>
29 #if defined(__arm__) || defined(__arm64__)
30 #undef TARGET_IPHONE_SIMULATOR
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>
38 #include "Standard.hpp"
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
)
47 static int $
strcmp(const char *lhs
, const char *rhs
) {
48 while (*lhs
== *rhs
) {
52 } return *lhs
< *rhs
? -1 : 1;
55 static void $
strlcpy(char *dst
, const char *src
, size_t size
) {
59 while (i
!= size
- 1) {
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
;
72 static const uint32_t LC_SEGMENT_XX
= LC_SEGMENT_64
;
73 static const uint32_t MH_MAGIC_XX
= MH_MAGIC_64
;
75 typedef struct mach_header mach_header_xx
;
76 typedef struct nlist nlist_xx
;
77 typedef struct segment_command segment_command_xx
;
79 static const uint32_t LC_SEGMENT_XX
= LC_SEGMENT
;
80 static const uint32_t MH_MAGIC_XX
= MH_MAGIC
;
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)) \
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 \
92 else if (lcp->cmd != lc) \
94 else if (lcp->cmdsize < sizeof(type)) \
96 else if (const type *command = reinterpret_cast<const type *>(lcp))
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
)
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)
117 static void *Symbol(const mach_header_xx
*mach
, const char *name
) {
118 const struct symtab_command
*stp(NULL
);
119 forlc (command
, mach
, LC_SYMTAB
, struct symtab_command
)
124 size_t slide(_not(size_t));
125 const nlist_xx
*symbols(NULL
);
126 const char *strings(NULL
);
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
);
137 if (slide
== _not(size_t) || symbols
== NULL
|| strings
== NULL
)
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)
145 const char *nambuf(strings
+ symbol
->n_un
.n_strx
);
146 if ($
strcmp(name
, nambuf
) != 0)
149 uintptr_t value(symbol
->n_value
);
154 if ((symbol
->n_desc
& N_ARM_THUMB_DEF
) != 0)
159 return reinterpret_cast<void *>(value
);
165 template <typename Type_
>
166 static _finline
void cyset(Type_
&function
, const char *name
, const mach_header_xx
*mach
) {
167 function
= reinterpret_cast<Type_
>(Symbol(mach
, name
));
170 static _finline
const mach_header_xx
*Library(Baton
*baton
, const char *name
) {
171 struct dyld_all_image_infos
*infos(reinterpret_cast<struct dyld_all_image_infos
*>(baton
->dyld
));
172 return Library(infos
, name
);
175 void *Routine(void *arg
) {
176 Baton
*baton(reinterpret_cast<Baton
*>(arg
));
178 const mach_header_xx
*dyld(NULL
);
180 dyld
= Library(baton
, "/usr/lib/system/libdyld.dylib");
182 dyld
= Library(baton
, "/usr/lib/libSystem.B.dylib");
185 cyset($dlerror
, "_dlerror", dyld
);
187 void *(*$dlopen
)(const char *, int);
188 cyset($dlopen
, "_dlopen", dyld
);
190 void *handle($
dlopen(baton
->library
, RTLD_LAZY
| RTLD_LOCAL
));
191 if (handle
== NULL
) {
192 $
strlcpy(baton
->error
, $
dlerror(), sizeof(baton
->error
));
196 void *(*$dlsym
)(void *, const char *);
197 cyset($dlsym
, "_dlsym", dyld
);
199 void (*CYHandleServer
)(pid_t
);
200 CYHandleServer
= reinterpret_cast<void (*)(pid_t
)>($
dlsym(handle
, "CYHandleServer"));
201 if (CYHandleServer
== NULL
) {
202 $
strlcpy(baton
->error
, $
dlerror(), sizeof(baton
->error
));
206 CYHandleServer(baton
->pid
);
210 extern "C" void Start(Baton
*baton
) {
211 struct _pthread self
;
212 $
bzero(&self
, sizeof(self
));
214 const mach_header_xx
*pthread(NULL
);
216 pthread
= Library(baton
, "/usr/lib/system/libsystem_pthread.dylib");
218 pthread
= Library(baton
, "/usr/lib/system/libsystem_c.dylib");
220 pthread
= Library(baton
, "/usr/lib/libSystem.B.dylib");
222 void (*$__pthread_set_self
)(pthread_t
);
223 cyset($__pthread_set_self
, "___pthread_set_self", pthread
);
226 $
__pthread_set_self(&self
);
228 int (*$pthread_attr_init
)(pthread_attr_t
*);
229 cyset($pthread_attr_init
, "_pthread_attr_init", pthread
);
233 $
pthread_attr_init(&attr
);
235 int (*$pthread_attr_setdetachstate
)(pthread_attr_t
*, int);
236 cyset($pthread_attr_setdetachstate
, "_pthread_attr_setdetachstate", pthread
);
238 $
pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
241 int (*$pthread_create
)(pthread_t
*, const pthread_attr_t
*, void *(*)(void *), void *);
242 cyset($pthread_create
, "_pthread_create", pthread
);
245 $
pthread_create(&thread
, NULL
, &Routine
, baton
);
248 int (*$pthread_attr_destroy
)(pthread_attr_t
*);
249 cyset($pthread_attr_destroy
, "_pthread_attr_destroy", pthread
);
251 $
pthread_attr_destroy(&attr
);
254 #if defined(__arm__) || defined(__arm64__)
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
));
265 tsd
= reinterpret_cast<void **>(tpid
& ~3);
269 _pthread_setspecific_direct(0, &self
);
272 int (*$pthread_join
)(pthread_t
, void **);
273 cyset($pthread_join
, "_pthread_join", pthread
);
276 $
pthread_join(thread
, &status
);
278 const mach_header_xx
*kernel(NULL
);
280 kernel
= Library(baton
, "/usr/lib/system/libsystem_kernel.dylib");
282 kernel
= Library(baton
, "/usr/lib/libSystem.B.dylib");
284 mach_port_t (*$mach_thread_self
)();
285 cyset($mach_thread_self
, "_mach_thread_self", kernel
);
287 kern_return_t (*$thread_terminate
)(thread_act_t
);
288 cyset($thread_terminate
, "_thread_terminate", kernel
);
290 $
thread_terminate($
mach_thread_self());