5 #include <mach-o/nlist.h>
13 #include "Exception.hpp"
14 #include "Pooling.hpp"
15 #include "Trampoline.t.hpp"
17 extern "C" void _pthread_set_self(pthread_t
);
19 template <typename Type_
>
20 static void nlset(Type_
&function
, struct nlist
*nl
, size_t index
) {
21 struct nlist
&name(nl
[index
]);
22 uintptr_t value(name
.n_value
);
23 if ((name
.n_desc
& N_ARM_THUMB_DEF
) != 0)
25 function
= reinterpret_cast<Type_
>(value
);
28 void InjectLibrary(pid_t pid
) {
29 // XXX: break this into the build environment
30 const char *library("/usr/lib/libcycript.dylib");
32 static const size_t Stack_(8 * 1024);
33 size_t length(strlen(library
) + 1), depth(sizeof(Baton
) + length
);
34 depth
= (depth
+ sizeof(uintptr_t) + 1) / sizeof(uintptr_t) * sizeof(uintptr_t);
37 uint8_t *local(reinterpret_cast<uint8_t *>(apr_palloc(pool
, depth
)));
38 Baton
*baton(reinterpret_cast<Baton
*>(local
));
41 memset(nl
, 0, sizeof(nl
));
42 nl
[0].n_un
.n_name
= (char *) "__pthread_set_self";
43 nlist("/usr/lib/libSystem.B.dylib", nl
);
44 nlset(baton
->_pthread_set_self
, nl
, 0);
45 _assert(baton
->_pthread_set_self
!= NULL
);
47 baton
->pthread_create
= &pthread_create
;
48 baton
->pthread_join
= &pthread_join
;
50 baton
->dlopen
= &dlopen
;
51 baton
->dlsym
= &dlsym
;
53 baton
->mach_thread_self
= &mach_thread_self
;
54 baton
->thread_terminate
= &thread_terminate
;
56 baton
->pid
= getpid();
57 memcpy(baton
->library
, library
, length
);
59 vm_size_t
size(depth
+ Stack_
);
61 mach_port_t
self(mach_task_self()), task
;
62 _krncall(task_for_pid(self
, pid
, &task
));
65 _krncall(vm_allocate(task
, &data
, size
, true));
66 vm_address_t
stack(data
+ depth
);
67 vm_write(task
, data
, reinterpret_cast<vm_address_t
>(baton
), depth
);
70 _krncall(vm_allocate(task
, &code
, sizeof(Trampoline_
), true));
71 vm_write(task
, code
, reinterpret_cast<vm_address_t
>(Trampoline_
), sizeof(Trampoline_
));
72 _krncall(vm_protect(task
, code
, sizeof(Trampoline_
), false, VM_PROT_READ
| VM_PROT_EXECUTE
));
75 _krncall(thread_create(task
, &thread
));
77 thread_state_flavor_t flavor
;
78 mach_msg_type_number_t count
;
81 arm_thread_state_t state
;
82 flavor
= ARM_THREAD_STATE
;
83 count
= ARM_THREAD_STATE_COUNT
;
88 memset(&state
, 0, sizeof(state
));
90 mach_msg_type_number_t
read(count
);
91 _krncall(thread_get_state(thread
, flavor
, reinterpret_cast<thread_state_t
>(&state
), &read
));
92 _assert(count
== count
);
96 state
.r
[1] = RTLD_LAZY
| RTLD_GLOBAL
;
97 state
.sp
= stack
+ Stack_
;
100 if ((state
.pc
& 0x1) != 0) {
105 #error XXX: implement
108 _krncall(thread_set_state(thread
, flavor
, reinterpret_cast<thread_state_t
>(&state
), count
));
109 _krncall(thread_resume(thread
));
111 _krncall(mach_port_deallocate(self
, task
));