+ static const size_t Stack_(8 * 1024);
+ size_t length(strlen(library) + 1), depth(sizeof(Baton) + length);
+ depth = (depth + align + 1) / align * align;
+
+ CYPool pool;
+ uint8_t *local(pool.malloc<uint8_t>(depth));
+ Baton *baton(reinterpret_cast<Baton *>(local));
+
+ baton->dyld = info.all_image_info_addr;
+ baton->pid = getpid();
+ memset(baton->error, 0, sizeof(baton->error));
+ memcpy(baton->library, library, length);
+
+ mach_vm_size_t size(depth + Stack_);
+ mach_vm_address_t stack;
+ _krncall(mach_vm_allocate(task, &stack, size, true));
+
+ mach_vm_address_t data(stack + Stack_);
+ _krncall(mach_vm_write(task, data, reinterpret_cast<mach_vm_address_t>(baton), depth));
+
+ mach_vm_address_t code;
+ _krncall(mach_vm_allocate(task, &code, trampoline->size_, true));
+ _krncall(mach_vm_write(task, code, reinterpret_cast<vm_offset_t>(trampoline->data_), trampoline->size_));
+ _krncall(mach_vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE));
+
+ uint32_t frame[push];
+ if (sizeof(frame) != 0)
+ memset(frame, 0, sizeof(frame));
+
+#if defined(__i386__) || defined(__x86_64__)
+ switch (state.tsh.flavor) {
+ case i386_THREAD_STATE:
+ frame[1] = data;
+ state.uts.ts32.__eip = code + trampoline->entry_;
+ state.uts.ts32.__esp = stack + Stack_ - sizeof(frame);
+ break;
+ case x86_THREAD_STATE64:
+ state.uts.ts64.__rdi = data;
+ state.uts.ts64.__rip = code + trampoline->entry_;
+ state.uts.ts64.__rsp = stack + Stack_ - sizeof(frame);
+ break;
+ default:
+ _assert(false);
+ }
+#elif defined(__arm__) || defined(__arm64__)
+ switch (state.ash.flavor) {
+ case ARM_THREAD_STATE32:
+ state.ts_32.__r[0] = data;
+ state.ts_32.__pc = code + trampoline->entry_;
+ state.ts_32.__sp = stack + Stack_ - sizeof(frame);
+
+ if ((state.ts_32.__pc & 0x1) != 0) {
+ state.ts_32.__pc &= ~0x1;
+ state.ts_32.__cpsr |= 0x20;
+ }
+
+ break;
+
+ case ARM_THREAD_STATE64:
+ state.ts_64.__x[0] = data;
+ state.ts_64.__pc = code + trampoline->entry_;
+ state.ts_64.__sp = stack + Stack_ - sizeof(frame);
+ break;
+
+ default:
+ _assert(false);
+ }
+#else
+ #error XXX: implement
+#endif
+
+ if (sizeof(frame) != 0)
+ _krncall(mach_vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<mach_vm_address_t>(frame), sizeof(frame)));
+
+ _krncall(thread_set_state(thread, flavor, bottom, read));