]> git.saurik.com Git - cycript.git/blame - Trampoline.t.cpp
Allow APR link flags to be configure overridden.
[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
b6961e53
JF
22#define _PTHREAD_ATTR_T
23#include <pthread_internals.h>
24
38ae783d
JF
25#include <mach-o/dyld.h>
26#include <mach-o/dyld_images.h>
27#include <mach-o/loader.h>
28
29extern "C" {
30#include <mach-o/nlist.h>
31}
32
eed4f174 33#include "Standard.hpp"
b6961e53
JF
34#include "Baton.hpp"
35
38ae783d
JF
36static void $bzero(void *data, size_t size) {
37 char *bytes(reinterpret_cast<char *>(data));
38 for (size_t i(0); i != size; ++i)
39 bytes[i] = 0;
40}
41
42static int $strcmp(const char *lhs, const char *rhs) {
43 while (*lhs == *rhs) {
44 if (*lhs == '\0')
45 return 0;
46 ++lhs, ++rhs;
47 } return *lhs < *rhs ? -1 : 1;
48}
49
50#ifdef __LP64__
51typedef struct mach_header_64 mach_header_xx;
52typedef struct nlist_64 nlist_xx;
53typedef struct segment_command_64 segment_command_xx;
54
55static const uint32_t LC_SEGMENT_XX = LC_SEGMENT_64;
56static const uint32_t MH_MAGIC_XX = MH_MAGIC_64;
57#else
58typedef struct mach_header mach_header_xx;
59typedef struct nlist nlist_xx;
60typedef struct segment_command segment_command_xx;
61
62static const uint32_t LC_SEGMENT_XX = LC_SEGMENT;
63static const uint32_t MH_MAGIC_XX = MH_MAGIC;
64#endif
65
66#define forlc(command, mach, lc, type) \
67 if (const struct load_command *load_commands = reinterpret_cast<const struct load_command *>(mach + 1)) \
68 if (const struct load_command *lcp = load_commands) \
69 for (uint32_t i(0); i != mach->ncmds; ++i, lcp = reinterpret_cast<const struct load_command *>(reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize)) \
70 if ( \
71 lcp->cmdsize % sizeof(long) != 0 || lcp->cmdsize <= 0 || \
72 reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize > reinterpret_cast<const uint8_t *>(load_commands) + mach->sizeofcmds \
73 ) \
74 return NULL; \
75 else if (lcp->cmd != lc) \
76 continue; \
77 else if (lcp->cmdsize < sizeof(type)) \
78 return NULL; \
79 else if (const type *command = reinterpret_cast<const type *>(lcp))
80
85b57b57
JF
81static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) {
82 for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
83 const dyld_image_info &info(infos->infoArray[i]);
84 const mach_header_xx *mach(reinterpret_cast<const mach_header_xx *>(info.imageLoadAddress));
85 if (mach->magic != MH_MAGIC_XX)
86 continue;
87
88 const char *path(info.imageFilePath);
89 forlc (dylib, mach, LC_ID_DYLIB, dylib_command)
90 path = reinterpret_cast<const char *>(dylib) + dylib->dylib.name.offset;
91 if ($strcmp(path, name) != 0)
92 continue;
93
94 return mach;
95 }
38ae783d 96
85b57b57
JF
97 return NULL;
98}
99
100static void *Symbol(const mach_header_xx *mach, const char *name) {
38ae783d
JF
101 const struct symtab_command *stp(NULL);
102 forlc (command, mach, LC_SYMTAB, struct symtab_command)
103 stp = command;
104 if (stp == NULL)
85b57b57 105 return NULL;
38ae783d
JF
106
107 size_t slide(_not(size_t));
108 const nlist_xx *symbols(NULL);
109 const char *strings(NULL);
110
111 forlc (segment, mach, LC_SEGMENT_XX, segment_command_xx) {
112 if (segment->fileoff == 0)
113 slide = reinterpret_cast<size_t>(mach) - segment->vmaddr;
114 if (stp->symoff >= segment->fileoff && stp->symoff < segment->fileoff + segment->filesize)
115 symbols = reinterpret_cast<const nlist_xx *>(stp->symoff - segment->fileoff + segment->vmaddr + slide);
116 if (stp->stroff >= segment->fileoff && stp->stroff < segment->fileoff + segment->filesize)
117 strings = reinterpret_cast<const char *>(stp->stroff - segment->fileoff + segment->vmaddr + slide);
118 }
119
120 if (slide == _not(size_t) || symbols == NULL || strings == NULL)
85b57b57 121 return NULL;
38ae783d
JF
122
123 for (size_t i(0); i != stp->nsyms; ++i) {
124 const nlist_xx *symbol(&symbols[i]);
125 if (symbol->n_un.n_strx == 0 || (symbol->n_type & N_STAB) != 0)
126 continue;
127
128 const char *nambuf(strings + symbol->n_un.n_strx);
129 if ($strcmp(name, nambuf) != 0)
130 continue;
131
132 uintptr_t value(symbol->n_value);
133 if (value == 0)
134 continue;
135
136 value += slide;
137 return reinterpret_cast<void *>(value);
138 }
85b57b57
JF
139
140 return NULL;
141}
38ae783d
JF
142
143struct Dynamic {
144 char *(*dlerror)();
145 void *(*dlsym)(void *, const char *);
146};
147
eed4f174 148template <typename Type_>
38ae783d
JF
149static _finline void dlset(Dynamic *dynamic, Type_ &function, const char *name, void *handle = RTLD_DEFAULT) {
150 function = reinterpret_cast<Type_>(dynamic->dlsym(handle, name));
95a2c7e5 151 if (function == NULL)
38ae783d
JF
152 dynamic->dlerror();
153}
154
155template <typename Type_>
85b57b57
JF
156static _finline void cyset(Type_ &function, const char *name, const mach_header_xx *mach) {
157 function = reinterpret_cast<Type_>(Symbol(mach, name));
158}
159
160static _finline const mach_header_xx *Library(Baton *baton, const char *name) {
38ae783d 161 struct dyld_all_image_infos *infos(reinterpret_cast<struct dyld_all_image_infos *>(baton->dyld));
85b57b57 162 return Library(infos, name);
eed4f174
JF
163}
164
49e976ae 165void *Routine(void *arg) {
eed4f174
JF
166 Baton *baton(reinterpret_cast<Baton *>(arg));
167
85b57b57
JF
168 const mach_header_xx *dyld(Library(baton, "/usr/lib/system/libdyld.dylib"));
169
38ae783d 170 Dynamic dynamic;
85b57b57
JF
171 cyset(dynamic.dlerror, "_dlerror", dyld);
172 cyset(dynamic.dlsym, "_dlsym", dyld);
38ae783d 173
40b1517d 174 int (*pthread_detach)(pthread_t);
38ae783d 175 dlset(&dynamic, pthread_detach, "pthread_detach");
40b1517d
JF
176
177 pthread_t (*pthread_self)();
38ae783d 178 dlset(&dynamic, pthread_self, "pthread_self");
40b1517d
JF
179
180 pthread_detach(pthread_self());
181
eed4f174 182 void *(*dlopen)(const char *, int);
38ae783d 183 dlset(&dynamic, dlopen, "dlopen");
eed4f174
JF
184
185 void *handle(dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
95a2c7e5 186 if (handle == NULL) {
38ae783d 187 dynamic.dlerror();
95a2c7e5
JF
188 return NULL;
189 }
eed4f174
JF
190
191 void (*CYHandleServer)(pid_t);
38ae783d 192 dlset(&dynamic, CYHandleServer, "CYHandleServer", handle);
6e51aaf8 193 if (CYHandleServer == NULL) {
38ae783d 194 dynamic.dlerror();
6e51aaf8
JF
195 return NULL;
196 }
eed4f174 197
6e51aaf8 198 CYHandleServer(baton->pid);
95a2c7e5 199 return NULL;
eed4f174
JF
200}
201
49e976ae
JF
202extern "C" void Start(Baton *baton) {
203 struct _pthread self;
204 $bzero(&self, sizeof(self));
205
85b57b57
JF
206 const mach_header_xx *pthread(Library(baton, "/usr/lib/system/libsystem_pthread.dylib"));
207 if (pthread == NULL)
208 pthread = Library(baton, "/usr/lib/system/libsystem_c.dylib");
209
38ae783d 210 void (*$__pthread_set_self)(pthread_t);
85b57b57 211 cyset($__pthread_set_self, "___pthread_set_self", pthread);
38ae783d 212
49e976ae 213 self.tsd[0] = &self;
38ae783d 214 $__pthread_set_self(&self);
eed4f174 215
38ae783d 216 int (*$pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
85b57b57 217 cyset($pthread_create, "_pthread_create", pthread);
b6961e53
JF
218
219 pthread_t thread;
38ae783d 220 $pthread_create(&thread, NULL, &Routine, baton);
b6961e53 221
85b57b57
JF
222 const mach_header_xx *kernel(Library(baton, "/usr/lib/system/libsystem_kernel.dylib"));
223
38ae783d 224 mach_port_t (*$mach_thread_self)();
85b57b57 225 cyset($mach_thread_self, "_mach_thread_self", kernel);
eed4f174 226
38ae783d 227 kern_return_t (*$thread_terminate)(thread_act_t);
85b57b57 228 cyset($thread_terminate, "_thread_terminate", kernel);
49e976ae 229
38ae783d 230 $thread_terminate($mach_thread_self());
b6961e53 231}