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
);
24 if ((name
.n_desc
& N_ARM_THUMB_DEF
) != 0)
29 void InjectLibrary(pid_t pid
) {
30 // XXX: break this into the build environment
31 const char *library("/usr/lib/libcycript.dylib");
33 static const size_t Stack_(8 * 1024);
34 size_t length(strlen(library
) + 1), depth(sizeof(Baton
) + length
);
35 depth
= (depth
+ sizeof(uintptr_t) + 1) / sizeof(uintptr_t) * sizeof(uintptr_t);
38 uint8_t *local(reinterpret_cast<uint8_t *>(apr_palloc(pool
, depth
)));
39 Baton
*baton(reinterpret_cast<Baton
*>(local
));
41 uintptr_t set_self_internal
;
42 uintptr_t set_self_external
;
45 memset(nl
, 0, sizeof(nl
));
46 nl
[0].n_un
.n_name
= (char *) "__pthread_set_self";
47 nl
[1].n_un
.n_name
= (char *) "___pthread_set_self";
48 nlist("/usr/lib/libSystem.B.dylib", nl
);
49 nlset(set_self_internal
, nl
, 0);
50 nlset(set_self_external
, nl
, 1);
52 baton
->_pthread_set_self
= reinterpret_cast<void (*)(pthread_t
)>(reinterpret_cast<uintptr_t>(&__pthread_set_self
) - set_self_external
+ set_self_internal
);
54 baton
->pthread_create
= &pthread_create
;
55 baton
->pthread_join
= &pthread_join
;
57 baton
->dlopen
= &dlopen
;
58 baton
->dlsym
= &dlsym
;
60 baton
->mach_thread_self
= &mach_thread_self
;
61 baton
->thread_terminate
= &thread_terminate
;
63 baton
->pid
= getpid();
64 memcpy(baton
->library
, library
, length
);
66 vm_size_t
size(depth
+ Stack_
);
68 mach_port_t
self(mach_task_self()), task
;
69 _krncall(task_for_pid(self
, pid
, &task
));
72 _krncall(vm_allocate(task
, &stack
, size
, true));
73 vm_address_t
data(stack
+ Stack_
);
75 vm_write(task
, data
, reinterpret_cast<vm_address_t
>(baton
), depth
);
78 _krncall(vm_allocate(task
, &code
, sizeof(Trampoline_
), true));
79 vm_write(task
, code
, reinterpret_cast<vm_address_t
>(Trampoline_
), sizeof(Trampoline_
));
80 _krncall(vm_protect(task
, code
, sizeof(Trampoline_
), false, VM_PROT_READ
| VM_PROT_EXECUTE
));
83 _krncall(thread_create(task
, &thread
));
85 thread_state_flavor_t flavor
;
86 mach_msg_type_number_t count
;
90 arm_thread_state_t state
;
91 flavor
= ARM_THREAD_STATE
;
92 count
= ARM_THREAD_STATE_COUNT
;
94 #elif defined(__i386__) || defined(__x86_64__)
95 i386_thread_state_t state
;
96 flavor
= i386_THREAD_STATE
;
97 count
= i386_THREAD_STATE_COUNT
;
100 #error XXX: implement
103 uintptr_t frame
[push
];
104 if (sizeof(frame
) != 0)
105 memset(frame
, 0, sizeof(frame
));
107 memset(&state
, 0, sizeof(state
));
109 mach_msg_type_number_t
read(count
);
110 _krncall(thread_get_state(thread
, flavor
, reinterpret_cast<thread_state_t
>(&state
), &read
));
111 _assert(count
== count
);
115 state
.sp
= stack
+ Stack_
;
118 if ((state
.pc
& 0x1) != 0) {
122 #elif defined(__i386__) || defined(__x86_64__)
127 state
.__esp
= stack
+ Stack_
- sizeof(frame
);
129 #error XXX: implement
132 if (sizeof(frame
) != 0)
133 vm_write(task
, stack
+ Stack_
- sizeof(frame
), reinterpret_cast<vm_address_t
>(frame
), sizeof(frame
));
135 _krncall(thread_set_state(thread
, flavor
, reinterpret_cast<thread_state_t
>(&state
), count
));
136 _krncall(thread_resume(thread
));
138 _krncall(mach_port_deallocate(self
, task
));