diff options
author | comex | 2015-01-11 21:52:31 -0500 |
---|---|---|
committer | comex | 2015-01-11 21:52:31 -0500 |
commit | 6fda830d15d4c0d4289c568320b70c9af11cc902 (patch) | |
tree | 904488445ae619897d67a588e524e84be1b9678e | |
parent | thumb2 progress (diff) | |
download | substitute-6fda830d15d4c0d4289c568320b70c9af11cc902.tar.gz |
thumb2 and stuff
-rw-r--r-- | Makefile | 13 | ||||
-rw-r--r-- | lib/dis-arm.inc.h | 6 | ||||
-rw-r--r-- | lib/dis-thumb.inc.h | 23 | ||||
-rw-r--r-- | lib/dis-thumb2.inc.h | 56 | ||||
-rw-r--r-- | lib/dis.h | 4 | ||||
-rw-r--r-- | test/test-td-simple.c (renamed from test/test-tdarm-simple.c) | 20 |
6 files changed, 78 insertions, 44 deletions
@@ -9,12 +9,7 @@ IMAON2 := /Users/comex/c/imaon2 GEN_JS := node $(IMAON2)/tables/gen.js all: \ - out/libsubstitute.dylib \ - out/test-find-syms \ - out/test-find-syms-cpp \ - out/test-substrate \ - out/test-dis \ - out/test-tdarm-simple + out/libsubstitute.dylib $(shell mkdir -p out) @@ -45,8 +40,11 @@ out/libsubstitute.dylib: $(LIB_OBJS) define define_test out/test-$(1): test/test-$(2).c* $(HEADERS) Makefile out/libsubstitute.dylib $(3) -o $$@ $$< -Ilib -Isubstrate -Lout -lsubstitute +all: out/test-$(1) endef -$(eval $(call define_test,tdarm-simple,tdarm-simple,$(CC) -std=c11)) +$(eval $(call define_test,tdarm-simple,td-simple,$(CC) -std=c11 -DPDIS=P_dis_arm -DHDR='"dis-arm.inc.h"')) +$(eval $(call define_test,tdthumb-simple,td-simple,$(CC) -std=c11 -DPDIS=P_dis_thumb -DHDR='"dis-thumb.inc.h"')) +$(eval $(call define_test,tdthumb2-simple,td-simple,$(CC) -std=c11 -DPDIS=P_dis_thumb2 -DHDR='"dis-thumb2.inc.h"')) $(eval $(call define_test,dis,dis,$(CC) -std=c11)) $(eval $(call define_test,find-syms,find-syms,$(CC) -std=c89)) $(eval $(call define_test,find-syms-cpp,find-syms,$(CXX) -x c++ -std=c++98)) @@ -56,7 +54,6 @@ generated: Makefile rm -rf generated mkdir generated - clean: rm -rf out distclean: diff --git a/lib/dis-arm.inc.h b/lib/dis-arm.inc.h index 550b8e7..6ed4bc6 100644 --- a/lib/dis-arm.inc.h +++ b/lib/dis-arm.inc.h @@ -91,11 +91,11 @@ static INLINE tdis_ret P(addr_offset_none_addr_postidx_imm8s4_offset_S_4_STC2L_P static INLINE tdis_ret P(addr_offset_none_addr_unk_Rt_13_LDA)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { data(r(addr), rout(Rt)); } -static INLINE tdis_ret P(addrmode3_addr_unk_Rt_4_LDRD)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { +static INLINE tdis_ret P(addrmode3_addr_unk_Rt_4_LDRD)(tdis_ctx ctx, struct bitslice addr, UNUSED struct bitslice Rt) { /* ignoring Rt2 = Rt + 1, but it isn't supposed to load PC anyway */ data(rs(addr, 9, 4), rs(addr, 0, 4)); } -static INLINE tdis_ret P(addrmode3_pre_addr_unk_Rt_4_LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { +static INLINE tdis_ret P(addrmode3_pre_addr_unk_Rt_4_LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, UNUSED struct bitslice Rt) { data(rs(addr, 9, 4), rs(addr, 0, 4)); } static INLINE tdis_ret P(addrmode5_addr_8_LDC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) { @@ -159,5 +159,7 @@ static INLINE tdis_ret P(GPR_Rt_addr_offset_none_addr_postidx_reg_Rm_S_1_STRHTr) static tdis_ret P(dis_arm)(tdis_ctx ctx) { unsigned op = ctx->op; #include "../generated/transform-dis-arm.inc.h" + /* clang doesn't realize that this is unreachable * and generates code like + * "and ecx, 0x1f; cmp ecx, 0x1f; ja abort". Yeah, nice job there. */ __builtin_abort(); } diff --git a/lib/dis-thumb.inc.h b/lib/dis-thumb.inc.h index 5541510..eb1aae4 100644 --- a/lib/dis-thumb.inc.h +++ b/lib/dis-thumb.inc.h @@ -1,6 +1,6 @@ #include "dis.h" static INLINE tdis_ret P(GPR_Rm_2_tADDhirr)(tdis_ctx ctx, struct bitslice Rm) { - return P(regs)(ctx, Rm, 0, 3, Rm, 0, 0); + data(r(Rm)); } static INLINE tdis_ret P(GPR_Rm_B_1_tBX)(tdis_ctx ctx, struct bitslice Rm) { unsigned val = bs_get(Rm, ctx->op); @@ -17,27 +17,34 @@ static INLINE tdis_ret P(GPR_Rm_unk_Rd_1_tMOVr)(tdis_ctx ctx, struct bitslice Rd if (Rd_val == 15) return P(bad)(ctx); if (Rm_val == 15) - return P(pcrel)(ctx, ctx->pc + 4, Rd_val); + return P(pcrel)(ctx, ctx->pc + 4, Rd_val, false); return P(unidentified)(ctx); } -static INLINE tdis_ret P(t_addrmode_pc_addr_1_tLDRpci)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { +static INLINE tdis_ret P(tGPR_Rn_reglist_regs_1_tLDMIA)(tdis_ctx ctx, UNUSED struct bitslice regs, UNUSED struct bitslice Rn) { + return P(unidentified)(ctx); +} +static INLINE tdis_ret P(tGPR_Rn_reglist_regs_S_1_tSTMIA_UPD)(tdis_ctx ctx, UNUSED struct bitslice regs, UNUSED struct bitslice Rn) { + return P(unidentified)(ctx); +} +static INLINE tdis_ret P(t_addrmode_pc_addr_unk_Rt_1_tLDRpci)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + bs_get(addr, ctx->op), bs_get(Rt, ctx->op), true); } -static INLINE tdis_ret P(t_adrlabel_addr_1_tADR)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rd) { +static INLINE tdis_ret P(t_adrlabel_addr_unk_Rd_1_tADR)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rd) { return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + bs_get(addr, ctx->op), bs_get(Rd, ctx->op), false); } static INLINE tdis_ret P(t_bcctarget_target_B_1_tBcc)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 8); + return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 8)); } static INLINE tdis_ret P(t_brtarget_target_B_1_tB)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 11); + return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 11)); } static INLINE tdis_ret P(t_cbtarget_target_B_2_tCBNZ)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx->pc + 4 + 2 * bs_get(target, ctx->op); + return P(branch)(ctx, ctx->pc + 4 + 2 * bs_get(target, ctx->op)); } static tdis_ret P(dis_thumb)(tdis_ctx ctx) { unsigned op = ctx->op; - #include "../generated/transform-dis-thumb.inc" + #include "../generated/transform-dis-thumb.inc.h" + __builtin_abort(); } diff --git a/lib/dis-thumb2.inc.h b/lib/dis-thumb2.inc.h index 02b9ebe..e18f268 100644 --- a/lib/dis-thumb2.inc.h +++ b/lib/dis-thumb2.inc.h @@ -2,10 +2,19 @@ static INLINE tdis_ret P(GPR_Rm_unk_Rd_1_t2MOVr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) { data(rout(Rd), r(Rm)); } +static INLINE tdis_ret P(GPR_Rn_reglist_regs_4_t2LDMDB)(tdis_ctx ctx, struct bitslice regs, UNUSED struct bitslice Rn) { + unsigned regs_val = bs_get(regs, ctx->op); + if(regs_val & (1 << 15)) + return P(ret)(ctx); + return P(unidentified)(ctx); +} +static INLINE tdis_ret P(GPR_Rn_reglist_regs_S_4_t2STMDB)(tdis_ctx ctx, UNUSED struct bitslice regs, UNUSED struct bitslice Rn) { + return P(unidentified)(ctx); +} static INLINE tdis_ret P(GPR_Rn_unk_Rd_1_t2ADDri12)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rn) { data(rout(Rd), r(Rn)); } -static INLINE tdis_ret P(GPR_Rt_8_VMOVDRR)(tdis_ctx ctx, struct bitslice Rt) { +static INLINE tdis_ret P(GPR_Rt_8_VMOVDRR)(tdis_ctx ctx, UNUSED struct bitslice Rt) { return P(unidentified)(ctx); /* don't care */ } static INLINE tdis_ret P(GPR_Rt_t2addrmode_imm12_addr_S_1_t2STRi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { @@ -26,9 +35,15 @@ static INLINE tdis_ret P(GPRnopc_Rn_unk_Rd_2_t2ADDri)(tdis_ctx ctx, struct bitsl static INLINE tdis_ret P(GPRnopc_Rt_t2addrmode_imm8_pre_addr_S_1_t2STR_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { data(rs(addr, 9, 4), r(Rt)); } -static INLINE tdis_ret P(GPRnopc_Rt_t2am_imm8_offset_offset_S_1_t2STR_POST)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt) { +static INLINE tdis_ret P(GPRnopc_Rt_addr_offset_none_Rn_t2am_imm8_offset_offset_S_1_t2STR_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice Rn) { data(r(Rt), r(Rn)); } +static INLINE tdis_ret P(rGPR_Rt_addr_offset_none_addr_S_4_t2STL)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr)); +} +static INLINE tdis_ret P(rGPR_Rt_addr_offset_none_addr_unk_Rd_S_7_t2STLEX)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rt, struct bitslice addr) { + data(rout(Rd), rout(Rt), r(addr)); +} static INLINE tdis_ret P(addr_offset_none_addr_4_t2LDC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { data(r(addr)); } @@ -57,13 +72,10 @@ static INLINE tdis_ret P(addrmode5_pre_addr_S_4_t2STC2L_PRE)(tdis_ctx ctx, struc data(rs(addr, 9, 4)); } static INLINE tdis_ret P(brtarget_target_B_1_t2Bcc)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 20); -} -static INLINE tdis_ret P(rGPR_Rt_addr_offset_none_addr_S_11_t2STL)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { - data(r(Rt), r(addr)); + return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 20)); } -static INLINE tdis_ret P(rGPR_Rt_t2addrmode_imm0_1020s4_addr_S_1_t2STREX)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(r(Rt), rs(addr, 8, 4)); +static INLINE tdis_ret P(rGPR_Rt_t2addrmode_imm0_1020s4_addr_unk_Rd_S_1_t2STREX)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt, struct bitslice Rd) { + data(rout(Rd), r(Rt), rs(addr, 8, 4)); } static INLINE tdis_ret P(rGPR_Rt_t2addrmode_imm12_addr_S_2_t2STRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { data(r(Rt), rs(addr, 13, 4)); @@ -83,7 +95,7 @@ static INLINE tdis_ret P(rGPR_Rt_t2addrmode_negimm8_addr_S_2_t2STRBi8)(tdis_ctx static INLINE tdis_ret P(rGPR_Rt_t2addrmode_so_reg_addr_S_2_t2STRBs)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { data(rs(addr, 6, 4), rs(Rt, 2, 4), r(Rt)); } -static INLINE tdis_ret P(rGPR_Rt_t2am_imm8_offset_offset_S_2_t2STRB_POST)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt) { +static INLINE tdis_ret P(rGPR_Rt_addr_offset_none_Rn_t2am_imm8_offset_offset_S_2_t2STRB_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice Rn) { data(r(Rt), r(Rn)); } static INLINE tdis_ret P(t2addrmode_imm0_1020s4_addr_unk_Rt_1_t2LDREX)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { @@ -96,12 +108,15 @@ static INLINE tdis_ret P(t2addrmode_imm8_addr_unk_Rt_S_3_t2STRBT)(tdis_ctx ctx, data(r(Rt), rs(addr, 9, 4)); } static INLINE tdis_ret P(t2addrmode_imm8_pre_addr_unk_Rt_5_t2LDRB_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rs(addr, 9, 4)); + data(rout(Rt), rs(addr, 9, 4)); +} +static INLINE tdis_ret P(addr_offset_none_Rn_t2am_imm8_offset_offset_unk_Rt_5_t2LDRB_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice Rn) { + data(rout(Rt), r(Rn)); } -static INLINE tdis_ret P(t2addrmode_imm8s4_addr_unk_Rt_1_t2LDRDi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { +static INLINE tdis_ret P(t2addrmode_imm8s4_addr_unk_Rt_1_t2LDRDi8)(tdis_ctx ctx, struct bitslice addr, UNUSED struct bitslice Rt) { data(rs(addr, 9, 4)); } -static INLINE tdis_ret P(t2addrmode_imm8s4_pre_addr_unk_Rt_1_t2LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { +static INLINE tdis_ret P(t2addrmode_imm8s4_pre_addr_unk_Rt_1_t2LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, UNUSED struct bitslice Rt) { data(rs(addr, 9, 4)); } static INLINE tdis_ret P(t2addrmode_negimm8_addr_unk_Rt_5_t2LDRBi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { @@ -111,23 +126,30 @@ static INLINE tdis_ret P(t2addrmode_posimm8_addr_unk_Rt_5_t2LDRBT)(tdis_ctx ctx, data(rout(Rt), rs(addr, 9, 4)); } static INLINE tdis_ret P(t2addrmode_so_reg_addr_unk_Rt_5_t2LDRBs)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 6, 4), rs(addr, 2, 4)); } static INLINE tdis_ret P(t2adrlabel_addr_unk_Rd_1_t2ADR)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rd) { return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + (bs_get(addr, ctx->op) & ((1 << 12) - 1)), bs_get(Rd, ctx->op), false); } static INLINE tdis_ret P(t2ldrlabel_addr_unk_Rt_5_t2LDRBpci)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + (bs_get(addr, ctx->op) & ((1 << 12) - 1)), bs_get(Rd, ctx->op), true); + return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + (bs_get(addr, ctx->op) & ((1 << 12) - 1)), bs_get(Rt, ctx->op), true); } static INLINE tdis_ret P(uncondbrtarget_target_B_1_t2B)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 24); + return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 24)); } static INLINE tdis_ret P(unk_Rd_3_t2MOVTi16)(tdis_ctx ctx, struct bitslice Rd) { - data(rout(Rt)); + data(rout(Rd)); } -static INLINE tdis_ret P(unk_Rm_B_2_t2TBB)(tdis_ctx ctx, struct bitslice Rm) { +static INLINE tdis_ret P(unk_Rm_B_2_t2TBB)(tdis_ctx ctx, UNUSED struct bitslice Rm) { /* Ew. Don't bother for now - this is hard to make show up in the first 8 bytes... */ return P(bad)(ctx); } -static INLINE tdis_ret P(unk_Rt_13_VMOVRRD)(tdis_ctx ctx, struct bitslice Rt) { +static INLINE tdis_ret P(unk_Rt_13_VMOVRRD)(tdis_ctx ctx, UNUSED struct bitslice Rt) { return P(unidentified)(ctx); } + +static tdis_ret P(dis_thumb2)(tdis_ctx ctx) { + unsigned op = ctx->op; + #include "../generated/transform-dis-thumb2.inc.h" + __builtin_abort(); +} @@ -3,8 +3,8 @@ #include <stdbool.h> #define UNUSED __attribute__((unused)) -#define INLINE inline __attribute__((always_inline)) -#define NOINLINE inline __attribute__((noinline)) +#define INLINE __attribute__((always_inline)) +#define NOINLINE __attribute__((noinline)) struct bitslice_run { int inpos, outpos, len; diff --git a/test/test-tdarm-simple.c b/test/test-td-simple.c index 7211e9b..9844fbd 100644 --- a/test/test-tdarm-simple.c +++ b/test/test-td-simple.c @@ -13,7 +13,7 @@ typedef struct tc { } *tdis_ctx; #define P(x) P_##x -NOINLINE +NOINLINE UNUSED static tdis_ret P_data(struct tc *ctx, unsigned o0, unsigned o1, unsigned o2, unsigned o3, unsigned out_mask) { printf("data: %08x\n", ctx->op); unsigned os[] = {o0, o1, o2, o3}; @@ -27,37 +27,43 @@ static tdis_ret P_data(struct tc *ctx, unsigned o0, unsigned o1, unsigned o2, un return (tdis_ret) {true}; } -NOINLINE +NOINLINE UNUSED static tdis_ret P_pcrel(struct tc *ctx, UNUSED uint32_t dpc, UNUSED unsigned reg, UNUSED bool is_load) { printf("adr: %08x\n", ctx->op); return (tdis_ret) {false}; } -NOINLINE +NOINLINE UNUSED static tdis_ret P_ret(struct tc *ctx) { printf("ret: %08x\n", ctx->op); return (tdis_ret) {false}; } -NOINLINE +NOINLINE UNUSED static tdis_ret P_branch(struct tc *ctx, UNUSED uint32_t dpc) { printf("branch: %08x\n", ctx->op); return (tdis_ret) {false}; } -NOINLINE +NOINLINE UNUSED static tdis_ret P_unidentified(struct tc *ctx) { printf("unidentified: %08x\n", ctx->op); return (tdis_ret) {false}; +} +NOINLINE UNUSED +static tdis_ret P_bad(struct tc *ctx) { + printf("bad: %08x\n", ctx->op); + return (tdis_ret) {false}; } -#include "dis-arm.inc.h" + +#include HDR int main(UNUSED int argc, char **argv) { struct tc ctx; ctx.pc = 0xdead0000; ctx.op = (uint32_t) strtoll(argv[1] ? argv[1] : "deadbeef", NULL, 16); - P_dis_arm(&ctx); + PDIS(&ctx); printf("==> %x\n", ctx.newop); } |