diff options
author | comex | 2015-01-21 01:41:38 -0500 |
---|---|---|
committer | comex | 2015-01-21 01:41:38 -0500 |
commit | 1100ff21d6b8c8e168af55dbf8932e095795e42f (patch) | |
tree | dcac2558e81f14aa94d7cbe9ca37e43f83740359 /lib/darwin | |
parent | woo, inject seems to work on all platforms (diff) | |
download | substitute-1100ff21d6b8c8e168af55dbf8932e095795e42f.tar.gz |
Some cleanup.
Diffstat (limited to 'lib/darwin')
-rw-r--r-- | lib/darwin/inject-asm-raw.c | 18 | ||||
-rw-r--r-- | lib/darwin/inject-asm.S | 19 | ||||
-rw-r--r-- | lib/darwin/inject.c | 15 |
3 files changed, 23 insertions, 29 deletions
diff --git a/lib/darwin/inject-asm-raw.c b/lib/darwin/inject-asm-raw.c index bb92828..0519dc6 100644 --- a/lib/darwin/inject-asm-raw.c +++ b/lib/darwin/inject-asm-raw.c @@ -9,14 +9,16 @@ __attribute__((always_inline)) #if defined(__x86_64__) -static int syscall(long s, long a, long b, long c, long d, long _) { +static int syscall(long s, long a, long b, long c, long d, long e) { if (s < 0) s = -s | 1 << 24; else s |= 2 << 24; REG(s, rax); REG(a, rdi); REG(b, rsi); REG(c, rdx); REG(d, rcx); OREG(out, rax); - asm volatile("syscall" : "=r"(out) : "r"(_s), "r"(_a), "r"(_b), "r"(_c), "r"(_d)); + asm volatile("push %1; syscall; pop %1" + : "=r"(out) + : "r"(e), "r"(_s), "r"(_a), "r"(_b), "r"(_c), "r"(_d)); return out; } #elif defined(__i386__) @@ -36,17 +38,21 @@ static int syscall(long s, long a, long b, long c, long d, long e) { return out; } #elif defined(__arm__) -static int syscall(long s, long a, long b, long c, long d, long _) { +static int syscall(long s, long a, long b, long c, long d, long e) { REG(s, r12); REG(a, r0); REG(b, r1); REG(c, r2); REG(d, r3); OREG(out, r0); - asm volatile("svc #0x80" : "=r"(out) : "r"(_s), "r"(_a), "r"(_b), "r"(_c), "r"(_d)); + asm volatile("push {%1}; svc #0x80; pop {%1}" + : "=r"(out) + : "r"(e), "r"(_s), "r"(_a), "r"(_b), "r"(_c), "r"(_d)); return out; } #elif defined(__arm64__) -static int syscall(long s, long a, long b, long c, long d, long _) { +static int syscall(long s, long a, long b, long c, long d, long e) { REG(s, x16); REG(a, x0); REG(b, x1); REG(c, x2); REG(d, x3); OREG(out, x0); - asm volatile("svc #0x80" : "=r"(out) : "r"(_s), "r"(_a), "r"(_b), "r"(_c), "r"(_d)); + asm volatile("str %1, [sp, #-0x10]!\n svc #0x80\n ldr %1, [sp], #0x10" + : "=r"(out) + : "r"(e), "r"(_s), "r"(_a), "r"(_b), "r"(_c), "r"(_d)); return out; } #else diff --git a/lib/darwin/inject-asm.S b/lib/darwin/inject-asm.S deleted file mode 100644 index a909879..0000000 --- a/lib/darwin/inject-asm.S +++ /dev/null @@ -1,19 +0,0 @@ -.align 12 -.globl _inject_page_start -_inject_page_start: -.align 2 -.globl _inject_start_x86_64 -_inject_start_x86_64: -.byte 0x55, 0x48, 0x89, 0xe5, 0x53, 0x50, 0x48, 0x89, 0xfb, 0x48, 0x8d, 0x15, 0x53, 0x00, 0x00, 0x00, 0x48, 0x8d, 0x7d, 0xf4, 0x31, 0xf6, 0x48, 0x89, 0xd9, 0xff, 0x13, 0xeb, 0x15, 0xb8, 0x3e, 0x00, 0x00, 0x01, 0x31, 0xff, 0xbe, 0x01, 0x00, 0x00, 0x00, 0x31, 0xd2, 0xb9, 0x40, 0x1f, 0x00, 0x00, 0x0f, 0x05, 0x48, 0x83, 0x7b, 0x18, 0x00, 0x74, 0xe4, 0x48, 0x81, 0xe3, 0x00, 0xf0, 0xff, 0xff, 0xb8, 0x69, 0x01, 0x00, 0x02, 0xbe, 0x00, 0x20, 0x00, 0x00, 0x31, 0xd2, 0x31, 0xc9, 0x48, 0x89, 0xdf, 0x0f, 0x05, 0xb9, 0xad, 0x0b, 0x00, 0x00, 0x31, 0xc0, 0xff, 0xd1, 0x48, 0x83, 0xc4, 0x08, 0x5b, 0x5d, 0xc3, 0x55, 0x48, 0x89, 0xe5, 0x53, 0x50, 0x48, 0x89, 0xfb, 0x48, 0x8b, 0x7b, 0x10, 0x31, 0xf6, 0xff, 0x53, 0x08, 0x48, 0xc7, 0x43, 0x18, 0x01, 0x00, 0x00, 0x00, 0x31, 0xc0, 0x48, 0x83, 0xc4, 0x08, 0x5b, 0x5d, 0xc3 -.align 2 -.globl _inject_start_i386 -_inject_start_i386: -.byte 0x55, 0x89, 0xe5, 0x53, 0x57, 0x56, 0x83, 0xec, 0x1c, 0x89, 0xce, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x58, 0x89, 0x74, 0x24, 0x0c, 0x8d, 0x80, 0x90, 0x00, 0x00, 0x00, 0x89, 0x44, 0x24, 0x08, 0x8d, 0x45, 0xf0, 0x89, 0x04, 0x24, 0xc7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0xff, 0x16, 0x8b, 0x46, 0x0c, 0x89, 0xe1, 0x83, 0xf8, 0x00, 0x75, 0x33, 0x31, 0xff, 0xbb, 0x01, 0x00, 0x00, 0x00, 0xb8, 0xff, 0xff, 0xff, 0xff, 0x50, 0xb8, 0x40, 0x1f, 0x00, 0x00, 0x50, 0x57, 0x53, 0x57, 0xb8, 0xc2, 0xff, 0xff, 0xff, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x81, 0xc2, 0x09, 0x00, 0x00, 0x00, 0x0f, 0x34, 0x8b, 0x46, 0x0c, 0x89, 0xe1, 0x83, 0xf8, 0x00, 0x74, 0xd4, 0x81, 0xe6, 0x00, 0xf0, 0xff, 0xff, 0x31, 0xc0, 0x50, 0x50, 0x50, 0xb8, 0x00, 0x20, 0x00, 0x00, 0x50, 0x56, 0xb8, 0x69, 0x01, 0x00, 0x00, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x81, 0xc2, 0x09, 0x00, 0x00, 0x00, 0x0f, 0x34, 0xb8, 0xad, 0x0b, 0x00, 0x00, 0xff, 0xd0, 0x83, 0xc4, 0x1c, 0x5e, 0x5f, 0x5b, 0x5d, 0xc3, 0x55, 0x89, 0xe5, 0x56, 0x83, 0xec, 0x14, 0x8b, 0x75, 0x08, 0x8b, 0x46, 0x08, 0x89, 0x04, 0x24, 0xc7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0xff, 0x56, 0x04, 0xc7, 0x46, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x31, 0xc0, 0x83, 0xc4, 0x14, 0x5e, 0x5d, 0xc3 -.align 2 -.globl _inject_start_arm -_inject_start_arm: -.byte 0x90, 0x40, 0x2d, 0xe9, 0x04, 0x70, 0x8d, 0xe2, 0x04, 0xd0, 0x4d, 0xe2, 0x00, 0x40, 0xa0, 0xe1, 0x0d, 0x00, 0xa0, 0xe1, 0x00, 0x90, 0x94, 0xe5, 0x60, 0x20, 0x00, 0xe3, 0x00, 0x20, 0x40, 0xe3, 0x00, 0x10, 0xa0, 0xe3, 0x02, 0x20, 0x8f, 0xe0, 0x04, 0x30, 0xa0, 0xe1, 0x39, 0xff, 0x2f, 0xe1, 0x0c, 0x00, 0x94, 0xe5, 0x00, 0x00, 0x50, 0xe3, 0x08, 0x00, 0x00, 0x1a, 0x3d, 0xc0, 0xe0, 0xe3, 0x01, 0x10, 0xa0, 0xe3, 0x00, 0x20, 0xa0, 0xe3, 0x7d, 0x3d, 0xa0, 0xe3, 0x00, 0x00, 0xa0, 0xe3, 0x80, 0x00, 0x00, 0xef, 0x0c, 0x00, 0x94, 0xe5, 0x00, 0x00, 0x50, 0xe3, 0xfa, 0xff, 0xff, 0x0a, 0x1f, 0x40, 0xcb, 0xe7, 0x69, 0xc1, 0x00, 0xe3, 0x02, 0x1a, 0xa0, 0xe3, 0x00, 0x20, 0xa0, 0xe3, 0x04, 0x00, 0xa0, 0xe1, 0x00, 0x30, 0xa0, 0xe3, 0x80, 0x00, 0x00, 0xef, 0xad, 0x0b, 0x00, 0xe3, 0x30, 0xff, 0x2f, 0xe1, 0x04, 0xd0, 0x47, 0xe2, 0x90, 0x80, 0xbd, 0xe8, 0x90, 0x40, 0x2d, 0xe9, 0x00, 0x40, 0xa0, 0xe1, 0x00, 0x10, 0xa0, 0xe3, 0xd4, 0x20, 0xc4, 0xe1, 0x04, 0x70, 0x8d, 0xe2, 0x03, 0x00, 0xa0, 0xe1, 0x32, 0xff, 0x2f, 0xe1, 0x01, 0x00, 0xa0, 0xe3, 0x0c, 0x00, 0x84, 0xe5, 0x00, 0x00, 0xa0, 0xe3, 0x90, 0x80, 0xbd, 0xe8 -.align 2 -.globl _inject_start_arm64 -_inject_start_arm64: -.byte 0xf4, 0x4f, 0xbe, 0xa9, 0xfd, 0x7b, 0x01, 0xa9, 0xfd, 0x43, 0x00, 0x91, 0xff, 0x43, 0x00, 0xd1, 0xf3, 0x03, 0x00, 0xaa, 0x68, 0x02, 0x40, 0xf9, 0x01, 0x00, 0x80, 0xd2, 0x62, 0x03, 0x00, 0x10, 0x1f, 0x20, 0x03, 0xd5, 0xe0, 0x33, 0x00, 0x91, 0xe3, 0x03, 0x13, 0xaa, 0x00, 0x01, 0x3f, 0xd6, 0x68, 0x0e, 0x40, 0xf9, 0x28, 0x01, 0x00, 0xb5, 0xb0, 0x07, 0x80, 0x92, 0xe1, 0x03, 0x40, 0xb2, 0x03, 0xe8, 0x83, 0xd2, 0x02, 0x00, 0x80, 0xd2, 0x00, 0x00, 0x80, 0xd2, 0x01, 0x10, 0x00, 0xd4, 0x68, 0x0e, 0x40, 0xf9, 0xa8, 0xff, 0xff, 0xb4, 0x60, 0xc6, 0x72, 0x92, 0x30, 0x2d, 0x80, 0xd2, 0xe1, 0x03, 0x73, 0xb2, 0x02, 0x00, 0x80, 0xd2, 0x03, 0x00, 0x80, 0xd2, 0x01, 0x10, 0x00, 0xd4, 0xa8, 0x75, 0x81, 0xd2, 0x00, 0x01, 0x3f, 0xd6, 0xbf, 0x43, 0x00, 0xd1, 0xfd, 0x7b, 0x41, 0xa9, 0xf4, 0x4f, 0xc2, 0xa8, 0xc0, 0x03, 0x5f, 0xd6, 0xf4, 0x4f, 0xbe, 0xa9, 0xfd, 0x7b, 0x01, 0xa9, 0xfd, 0x43, 0x00, 0x91, 0xf3, 0x03, 0x00, 0xaa, 0x68, 0x82, 0x40, 0xa9, 0x01, 0x00, 0x80, 0x52, 0x00, 0x01, 0x3f, 0xd6, 0xe8, 0x03, 0x40, 0xb2, 0x68, 0x0e, 0x00, 0xf9, 0x00, 0x00, 0x80, 0xd2, 0xfd, 0x7b, 0x41, 0xa9, 0xf4, 0x4f, 0xc2, 0xa8, 0xc0, 0x03, 0x5f, 0xd6 diff --git a/lib/darwin/inject.c b/lib/darwin/inject.c index fb35a10..2c0c1ce 100644 --- a/lib/darwin/inject.c +++ b/lib/darwin/inject.c @@ -206,6 +206,7 @@ static int get_foreign_image_export(mach_port_t task, uint64_t hdr_addr, if (total_size > hdr_buf_size) { vm_deallocate(mach_task_self(), (vm_offset_t) hdr_buf, (vm_size_t) hdr_buf_size); hdr_buf_size = total_size; + hdr_buf = 0; kr = mach_vm_remap(mach_task_self(), &hdr_buf, hdr_buf_size, 0, VM_FLAGS_ANYWHERE, task, hdr_addr, /*copy*/ true, &cur, &max, VM_INHERIT_NONE); @@ -277,7 +278,7 @@ static int get_foreign_image_export(mach_port_t task, uint64_t hdr_addr, } uint64_t linkedit_addr = vmaddr + slide; - mach_vm_address_t linkedit_buf; + mach_vm_address_t linkedit_buf = 0; kr = mach_vm_remap(mach_task_self(), &linkedit_buf, filesize, 0, VM_FLAGS_ANYWHERE, task, linkedit_addr, /*copy*/ true, &cur, &max, VM_INHERIT_NONE); @@ -375,8 +376,6 @@ struct _arm_thread_state_64 { uint32_t cpsr, pad; }; - -EXPORT int substitute_dlopen_in_pid(int pid, const char *filename, int options, char **error) { mach_port_t task; mach_vm_address_t target_stack = 0; @@ -421,6 +420,7 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, char ** } } + __attribute__((unused)) extern char inject_page_start[], inject_start_x86_64[], inject_start_i386[], @@ -479,7 +479,6 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, char ** p[2] = (uint32_t) vals[2]; } - printf("target_stack=%llx\n", target_stack_top); kr = mach_vm_write(task, target_stack_top, (mach_vm_address_t) stackbuf, baton_len + filelen_rounded); free(stackbuf); @@ -500,6 +499,7 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, char ** memset(&u, 0, sizeof(u)); switch (cputype) { +#if defined(__x86_64__) || defined(__i386__) case CPU_TYPE_X86_64: u.x64.rsp = target_stack_top; u.x64.rdi = target_stack_top; @@ -514,6 +514,8 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, char ** state_size = sizeof(u.x32); flavor = 1; break; +#endif +#if defined(__arm__) || defined(__arm64__) case CPU_TYPE_ARM: u.a32.sp = target_stack_top; u.a32.r[0] = target_stack_top; @@ -528,6 +530,11 @@ int substitute_dlopen_in_pid(int pid, const char *filename, int options, char ** state_size = sizeof(u.a64); flavor = 6; break; +#endif + default: + asprintf(error, "unknown target cputype %d", cputype); + ret = SUBSTITUTE_ERR_MISC; + goto fail; } mach_port_t thread; |