diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 3c4b9bb..d7545d0 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -84,6 +84,17 @@ jobs: # FIXME: For some reason some of the object files are rebuilt make CC=gcc CXX=g++-12 TARGET=aarch64 test-ci + MACOS_x86_64: + runs-on: macos-11 + steps: + - uses: actions/checkout@v3 + - name: brew + run: brew install capstone + - name: make + run: make TARGET=x86_64 all + - name: test + run: make CXX=g++-12 TARGET=x86_64 test-ci + Windows: strategy: matrix: diff --git a/Makefile b/Makefile index ffa2c67..253fe4e 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,7 @@ $(SRC_DIR)/ir_load.c: $(SRC_DIR)/ir.g $(BUILD_DIR)/ir_fold_hash.h: $(BUILD_DIR)/gen_ir_fold_hash $(SRC_DIR)/ir_fold.h $(SRC_DIR)/ir.h $(BUILD_DIR)/gen_ir_fold_hash < $(SRC_DIR)/ir_fold.h > $(BUILD_DIR)/ir_fold_hash.h $(BUILD_DIR)/gen_ir_fold_hash: $(SRC_DIR)/gen_ir_fold_hash.c $(SRC_DIR)/ir_strtab.c $(SRC_DIR)/ir.h - $(BUILD_CC) $(CFLAGS) $(LDFALGS) -o $@ $^ + $(BUILD_CC) $(CFLAGS) $(LDFALGS) -o $@ $< $(BUILD_DIR)/minilua: $(SRC_DIR)/dynasm/minilua.c $(BUILD_CC) $(SRC_DIR)/dynasm/minilua.c -lm -o $@ diff --git a/gen_ir_fold_hash.c b/gen_ir_fold_hash.c index ff9e4b6..a7571fe 100644 --- a/gen_ir_fold_hash.c +++ b/gen_ir_fold_hash.c @@ -10,6 +10,8 @@ #include "ir.h" #include +#include "ir_strtab.c" + #define MAX_RULES 2048 #define MAX_SLOTS (MAX_RULES * 4) diff --git a/ir.h b/ir.h index c7b2917..75f0aa0 100644 --- a/ir.h +++ b/ir.h @@ -714,6 +714,9 @@ int32_t ir_get_spill_slot_offset(ir_ctx *ctx, ir_ref ref); int ir_match(ir_ctx *ctx); void *ir_emit_code(ir_ctx *ctx, size_t *size); +/* Target address resolution (implementation in ir_emit.c) */ +void *ir_resolve_sym_name(const char *name); + /* Target CPU disassembler (implementation in ir_disasm.c) */ int ir_disasm_init(void); void ir_disasm_free(void); diff --git a/ir_emit.c b/ir_emit.c index 33f4e73..acaceff 100644 --- a/ir_emit.c +++ b/ir_emit.c @@ -260,7 +260,7 @@ static bool ir_is_same_mem_var(const ir_ctx *ctx, ir_ref r1, int32_t offset) return o1 == offset; } -static void *ir_resolve_sym_name(const char *name) +void *ir_resolve_sym_name(const char *name) { void *handle = NULL; void *addr; diff --git a/ir_main.c b/ir_main.c index 93ef968..a3a735e 100644 --- a/ir_main.c +++ b/ir_main.c @@ -453,12 +453,20 @@ int main(int argc, char **argv) if (entry) { if (dump_asm) { + ir_ref i; + ir_insn *insn; + ir_disasm_add_symbol("test", (uintptr_t)entry, size); -#ifdef _WIN32 - /* Quick workaraund to prevent *.irt tests failures */ - // TODO: try to find a general solution ??? - ir_disasm_add_symbol("printf", (uintptr_t)(void*)printf, sizeof(void*)); -#endif + + for (i = IR_UNUSED + 1, insn = ctx.ir_base - i; i < ctx.consts_count; i++, insn--) { + if (insn->op == IR_FUNC) { + const char *name = ir_get_str(&ctx, insn->val.i32); + void *addr = ir_resolve_sym_name(name); + + ir_disasm_add_symbol(name, (uintptr_t)addr, sizeof(void*)); + } + } + ir_disasm("test", entry, size, 0, &ctx, stderr); } if (dump_size) { diff --git a/tests/debug.aarch64/args_001.irt b/tests/debug.aarch64/args_001.irt index 81383a7..9c2c01e 100644 --- a/tests/debug.aarch64/args_001.irt +++ b/tests/debug.aarch64/args_001.irt @@ -41,7 +41,7 @@ test: str x16, [sp, #8] movz x16, #0x1, lsl #32 str x16, [sp, #0x10] - bl _IO_printf + bl printf add sp, sp, #0x20 ldp x29, x30, [sp], #0x10 ret diff --git a/tests/debug.aarch64/args_002.irt b/tests/debug.aarch64/args_002.irt index 74597f0..ba56910 100644 --- a/tests/debug.aarch64/args_002.irt +++ b/tests/debug.aarch64/args_002.irt @@ -40,7 +40,7 @@ test: str d31, [sp] fmov d31, xzr str d31, [sp, #8] - bl _IO_printf + bl printf add sp, sp, #0x10 ldp x29, x30, [sp], #0x10 ret diff --git a/tests/debug.aarch64/call-O0.irt b/tests/debug.aarch64/call-O0.irt index 4665e05..faf264d 100644 --- a/tests/debug.aarch64/call-O0.irt +++ b/tests/debug.aarch64/call-O0.irt @@ -22,7 +22,7 @@ test: mov x29, sp adr x0, .L1 movz w1, #0x2a - bl _IO_printf + bl printf str w0, [x29, #0x10] ldr w0, [x29, #0x10] ldp x29, x30, [sp], #0x20 diff --git a/tests/debug.aarch64/call.irt b/tests/debug.aarch64/call.irt index 489eac3..8a2475e 100644 --- a/tests/debug.aarch64/call.irt +++ b/tests/debug.aarch64/call.irt @@ -22,7 +22,7 @@ test: mov x29, sp adr x0, .L1 movz w1, #0x2a - bl _IO_printf + bl printf ldp x29, x30, [sp], #0x10 ret .rodata diff --git a/tests/debug.aarch64/call2.irt b/tests/debug.aarch64/call2.irt index 48c2301..f0a9836 100644 --- a/tests/debug.aarch64/call2.irt +++ b/tests/debug.aarch64/call2.irt @@ -32,7 +32,7 @@ test: mov w2, w3 mov w3, w16 adr x0, .L1 - bl _IO_printf + bl printf add w0, w0, w19 ldr x19, [x29, #0x18] ldp x29, x30, [sp], #0x20 diff --git a/tests/debug.aarch64/call_alloca.irt b/tests/debug.aarch64/call_alloca.irt index 16a7d3e..f04753d 100644 --- a/tests/debug.aarch64/call_alloca.irt +++ b/tests/debug.aarch64/call_alloca.irt @@ -58,7 +58,7 @@ test: strb w1, [x0, #5] strb wzr, [x0, #6] movz w1, #0x2a - bl _IO_printf + bl printf mov sp, x29 ldp x29, x30, [sp], #0x10 ret diff --git a/tests/debug.aarch64/call_vaddr.irt b/tests/debug.aarch64/call_vaddr.irt index 7e7ac38..a718406 100644 --- a/tests/debug.aarch64/call_vaddr.irt +++ b/tests/debug.aarch64/call_vaddr.irt @@ -57,7 +57,7 @@ test: strb w1, [x0, #5] strb wzr, [x0, #6] movz w1, #0x2a - bl _IO_printf + bl printf ldp x29, x30, [sp], #0x20 ret diff --git a/tests/debug.aarch64/params_003.irt b/tests/debug.aarch64/params_003.irt index 12b8902..140eb2b 100644 --- a/tests/debug.aarch64/params_003.irt +++ b/tests/debug.aarch64/params_003.irt @@ -41,7 +41,7 @@ test: str w16, [sp, #8] ldr w16, [x29, #0x28] str w16, [sp, #0x10] - bl _IO_printf + bl printf add sp, sp, #0x20 ldp x29, x30, [sp], #0x10 ret diff --git a/tests/debug.aarch64/regset-fib.irt b/tests/debug.aarch64/regset-fib.irt index dd60f0c..9881528 100644 --- a/tests/debug.aarch64/regset-fib.irt +++ b/tests/debug.aarch64/regset-fib.irt @@ -91,7 +91,7 @@ test: fsub d0, d1, d0 str d0, [x29, #0x10] adr x0, .L5 - bl _IO_printf + bl printf b .L1 .L2: mov w0, wzr diff --git a/tests/debug.aarch64/regset-fib2.irt b/tests/debug.aarch64/regset-fib2.irt index 7df3077..aa01855 100644 --- a/tests/debug.aarch64/regset-fib2.irt +++ b/tests/debug.aarch64/regset-fib2.irt @@ -91,10 +91,10 @@ test: fsub d0, d1, d0 str d0, [x29, #0x10] adr x0, .L5 - bl _IO_printf + bl printf adr x0, .L5 ldr d0, [x29, #0x10] - bl _IO_printf + bl printf b .L1 .L2: mov w0, wzr diff --git a/tests/debug.aarch64/regset-fibi.irt b/tests/debug.aarch64/regset-fibi.irt index 5f5816f..35377f1 100644 --- a/tests/debug.aarch64/regset-fibi.irt +++ b/tests/debug.aarch64/regset-fibi.irt @@ -77,7 +77,7 @@ test: add w20, w19, w0 mov w1, w19 adr x0, .L3 - bl _IO_printf + bl printf mov w0, w19 mov w19, w20 b .L1 diff --git a/tests/debug.aarch64/tailcall_001.irt b/tests/debug.aarch64/tailcall_001.irt index 3f0311e..c32bf0c 100644 --- a/tests/debug.aarch64/tailcall_001.irt +++ b/tests/debug.aarch64/tailcall_001.irt @@ -20,7 +20,7 @@ aarch64 test: adr x0, .L1 movz w1, #0x2a - b _IO_printf + b printf .rodata .db 0x1f, 0x20, 0x03, 0xd5 .L1: diff --git a/tests/debug.x86/tailcall_001.irt b/tests/debug.x86/tailcall_001.irt index 952c840..0d3d4e3 100644 --- a/tests/debug.x86/tailcall_001.irt +++ b/tests/debug.x86/tailcall_001.irt @@ -22,7 +22,7 @@ TAILCALL for functions with stack argumnts is not implemented test: leaq .L1(%rip), %rdi movl $0x2a, %esi - movabsq $_IO_printf, %rax + movabsq $printf, %rax jmpq *%rax .rodata .L1: diff --git a/tests/debug/args_001.irt b/tests/debug/args_001.irt index ac30c32..72bd0b7 100644 --- a/tests/debug/args_001.irt +++ b/tests/debug/args_001.irt @@ -37,7 +37,7 @@ test: movq $9, 0x18(%rsp) movabsq $0x100000000, %rax movq %rax, 0x20(%rsp) - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax addq $0x28, %rsp retq diff --git a/tests/debug/args_002.irt b/tests/debug/args_002.irt index 8a8440b..828e98e 100644 --- a/tests/debug/args_002.irt +++ b/tests/debug/args_002.irt @@ -37,7 +37,7 @@ test: movabsq $0x3feccccccccccccd, %rax movq %rax, (%rsp) movq $0, 8(%rsp) - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax addq $0x18, %rsp retq diff --git a/tests/debug/call-O0.irt b/tests/debug/call-O0.irt index 1990491..ab15581 100644 --- a/tests/debug/call-O0.irt +++ b/tests/debug/call-O0.irt @@ -21,7 +21,7 @@ test: subq $8, %rsp leaq .L1(%rip), %rdi movl $0x2a, %esi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax movl %eax, (%rsp) movl (%rsp), %eax diff --git a/tests/debug/call.irt b/tests/debug/call.irt index 87ab088..41a145a 100644 --- a/tests/debug/call.irt +++ b/tests/debug/call.irt @@ -21,7 +21,7 @@ test: subq $8, %rsp leaq .L1(%rip), %rdi movl $0x2a, %esi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax addq $8, %rsp retq diff --git a/tests/debug/call2.irt b/tests/debug/call2.irt index 4265480..dd05f33 100644 --- a/tests/debug/call2.irt +++ b/tests/debug/call2.irt @@ -31,7 +31,7 @@ test: movl %edx, %esi movl %eax, %edx leaq .L1(%rip), %rdi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax addl %ebx, %eax movq (%rsp), %rbx diff --git a/tests/debug/call_003.irt b/tests/debug/call_003.irt index 5194f58..7869f05 100644 --- a/tests/debug/call_003.irt +++ b/tests/debug/call_003.irt @@ -22,7 +22,7 @@ test: movl %edi, %edx movl %edx, %esi leaq .L1(%rip), %rdi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax addq $8, %rsp retq diff --git a/tests/debug/call_alloca.irt b/tests/debug/call_alloca.irt index 4b114d0..775b379 100644 --- a/tests/debug/call_alloca.irt +++ b/tests/debug/call_alloca.irt @@ -52,7 +52,7 @@ test: movb $0xa, 5(%rdi) movb $0, 6(%rdi) movl $0x2a, %esi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax movq %rbp, %rsp popq %rbp diff --git a/tests/debug/call_vaddr.irt b/tests/debug/call_vaddr.irt index 37e5237..bbcc052 100644 --- a/tests/debug/call_vaddr.irt +++ b/tests/debug/call_vaddr.irt @@ -50,7 +50,7 @@ test: movb $0xa, 5(%rdi) movb $0, 6(%rdi) movl $0x2a, %esi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax addq $8, %rsp retq diff --git a/tests/debug/params_003.irt b/tests/debug/params_003.irt index a343602..62f9b8b 100644 --- a/tests/debug/params_003.irt +++ b/tests/debug/params_003.irt @@ -41,7 +41,7 @@ test: movl %eax, 0x18(%rsp) movl 0x48(%rsp), %eax movl %eax, 0x20(%rsp) - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax addq $0x28, %rsp retq diff --git a/tests/debug/ra_002.irt b/tests/debug/ra_002.irt index 7b3b5c9..13ad802 100644 --- a/tests/debug/ra_002.irt +++ b/tests/debug/ra_002.irt @@ -25,7 +25,7 @@ test: movsd %xmm1, 8(%rsp) movsd %xmm2, 0x10(%rsp) leaq .L1(%rip), %rdi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax movsd (%rsp), %xmm1 subsd 8(%rsp), %xmm1 diff --git a/tests/debug/ra_003.irt b/tests/debug/ra_003.irt index 5640aba..ea689e5 100644 --- a/tests/debug/ra_003.irt +++ b/tests/debug/ra_003.irt @@ -25,7 +25,7 @@ test: movsd %xmm1, 8(%rsp) movsd %xmm2, 0x10(%rsp) leaq .L1(%rip), %rdi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax movsd (%rsp), %xmm1 addsd %xmm1, %xmm1 diff --git a/tests/debug/regset-fib.irt b/tests/debug/regset-fib.irt index f9874ae..1af4046 100644 --- a/tests/debug/regset-fib.irt +++ b/tests/debug/regset-fib.irt @@ -88,7 +88,7 @@ test: subsd %xmm1, %xmm0 movsd %xmm0, 8(%rsp) leaq .L5(%rip), %rdi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax movsd 8(%rsp), %xmm1 jmp .L1 diff --git a/tests/debug/regset-fib2.irt b/tests/debug/regset-fib2.irt index ce531a7..3c48a06 100644 --- a/tests/debug/regset-fib2.irt +++ b/tests/debug/regset-fib2.irt @@ -88,11 +88,11 @@ test: subsd %xmm1, %xmm0 movsd %xmm0, 8(%rsp) leaq .L5(%rip), %rdi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax leaq .L5(%rip), %rdi movsd 8(%rsp), %xmm0 - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax movsd 8(%rsp), %xmm1 jmp .L1 diff --git a/tests/debug/regset-fibi.irt b/tests/debug/regset-fibi.irt index 1f83b04..98c7d48 100644 --- a/tests/debug/regset-fibi.irt +++ b/tests/debug/regset-fibi.irt @@ -76,7 +76,7 @@ test: leal (%rbx, %rax), %ebp movl %ebx, %esi leaq .L3(%rip), %rdi - movabsq $_IO_printf, %rax + movabsq $printf, %rax callq *%rax movl %ebx, %eax movl %ebp, %ebx diff --git a/tests/debug/tailcall_001.irt b/tests/debug/tailcall_001.irt index 50d877a..0811c7b 100644 --- a/tests/debug/tailcall_001.irt +++ b/tests/debug/tailcall_001.irt @@ -20,7 +20,7 @@ x86_64 test: leaq .L1(%rip), %rdi movl $0x2a, %esi - movabsq $_IO_printf, %rax + movabsq $printf, %rax jmpq *%rax .rodata .L1: diff --git a/win32/Makefile b/win32/Makefile index 1556c16..f2003a8 100644 --- a/win32/Makefile +++ b/win32/Makefile @@ -93,7 +93,7 @@ $(BUILD_DIR)\ir_test.exe: $(OBJS_IR_TEST) $(OBJS_COMMON) $(LIBCAPSTONE) $(BUILD_DIR)\ir_fold_hash.h: $(BUILD_DIR)\gen_ir_fold_hash.exe $(SRC_DIR)\ir_fold.h $(SRC_DIR)\ir.h $(BUILD_DIR)\gen_ir_fold_hash.exe < $(SRC_DIR)\ir_fold.h > $(BUILD_DIR)\ir_fold_hash.h $(BUILD_DIR)\gen_ir_fold_hash.exe: $(SRC_DIR)\gen_ir_fold_hash.c $(SRC_DIR)\ir_strtab.c $(SRC_DIR)\ir.h - "$(CC)" $(CFLAGS) /Fo$(BUILD_DIR)\ /Fe$@ $(SRC_DIR)\gen_ir_fold_hash.c $(SRC_DIR)\ir_strtab.c + "$(CC)" $(CFLAGS) /Fo$(BUILD_DIR)\ /Fe$@ $(SRC_DIR)\gen_ir_fold_hash.c $(BUILD_DIR)\minilua.exe: $(SRC_DIR)\dynasm\minilua.c "$(CC)" /Fo$(BUILD_DIR)\ /Fe$@ $**