From 066a1fa86407c80c3b7ef2c3e8c86f1ffbd2929d Mon Sep 17 00:00:00 2001 From: comex Date: Mon, 19 Jan 2015 19:08:48 -0500 Subject: some reorganization --- Makefile | 11 +- lib/arm/dis-arm-multi.inc.h | 16 +++ lib/arm/dis-arm.inc.h | 214 ++++++++++++++++++++++++++++++++++ lib/arm/dis-thumb.inc.h | 79 +++++++++++++ lib/arm/dis-thumb2.inc.h | 185 +++++++++++++++++++++++++++++ lib/arm/misc.h | 7 ++ lib/arm/transform-dis-arm-multi.inc.h | 189 ++++++++++++++++++++++++++++++ lib/arm64/dis-arm64.inc.h | 49 ++++++++ lib/arm64/misc.h | 6 + lib/arm64/transform-dis-arm64.inc.h | 52 +++++++++ lib/dis-arm-multi.inc.h | 16 --- lib/dis-arm.inc.h | 214 ---------------------------------- lib/dis-arm64.inc.h | 49 -------- lib/dis-thumb.inc.h | 79 ------------- lib/dis-thumb2.inc.h | 185 ----------------------------- lib/dis.h | 20 +--- lib/jump-dis.c | 4 +- lib/substitute-internal.h | 11 +- lib/transform-dis-arm-multi.inc.h | 189 ------------------------------ lib/transform-dis-arm64.inc.h | 52 --------- lib/transform-dis.c | 10 +- 21 files changed, 817 insertions(+), 820 deletions(-) create mode 100644 lib/arm/dis-arm-multi.inc.h create mode 100644 lib/arm/dis-arm.inc.h create mode 100644 lib/arm/dis-thumb.inc.h create mode 100644 lib/arm/dis-thumb2.inc.h create mode 100644 lib/arm/misc.h create mode 100644 lib/arm/transform-dis-arm-multi.inc.h create mode 100644 lib/arm64/dis-arm64.inc.h create mode 100644 lib/arm64/misc.h create mode 100644 lib/arm64/transform-dis-arm64.inc.h delete mode 100644 lib/dis-arm-multi.inc.h delete mode 100644 lib/dis-arm.inc.h delete mode 100644 lib/dis-arm64.inc.h delete mode 100644 lib/dis-thumb.inc.h delete mode 100644 lib/dis-thumb2.inc.h delete mode 100644 lib/transform-dis-arm-multi.inc.h delete mode 100644 lib/transform-dis-arm64.inc.h diff --git a/Makefile b/Makefile index 37ec88a..4004dfd 100644 --- a/Makefile +++ b/Makefile @@ -27,8 +27,10 @@ $(eval $(call do_prefix,arm,-n _arm,ARM)) $(eval $(call do_prefix,arm64,,AArch64)) out/%.o: lib/%.c Makefile $(HEADERS) + @mkdir -p $(dir $@) $(CC) -fvisibility=hidden -std=c11 -c -o $@ $< out/%.o: lib/%.S Makefile $(HEADERS) + @mkdir -p $(dir $@) $(CC) -fvisibility=hidden -c -o $@ $< out/jump-dis.o: $(GENERATED) out/transform-dis.o: $(GENERATED) @@ -41,6 +43,7 @@ LIB_OBJS := \ out/substrate-compat.o \ out/jump-dis.o \ out/transform-dis.o + out/libsubstitute.dylib: $(LIB_OBJS) $(CC) -o $@ $(LIB_OBJS) $(LIB_LDFLAGS) @@ -49,10 +52,10 @@ out/test-$(1): test/test-$(2).[cm]* $(HEADERS) $(GENERATED) Makefile out/libsubs $(3) -g -o $$@ $$< -Ilib -Isubstrate -Lout -lsubstitute all: out/test-$(1) endef -$(eval $(call define_test,tdarm-simple,td-simple,$(CC) -std=c11 -DHDR='"dis-arm.inc.h"' -Dxdis=dis_arm -DFORCE_TARGET_arm)) -$(eval $(call define_test,tdthumb-simple,td-simple,$(CC) -std=c11 -DHDR='"dis-thumb.inc.h"' -Dxdis=dis_thumb -DFORCE_TARGET_arm)) -$(eval $(call define_test,tdthumb2-simple,td-simple,$(CC) -std=c11 -DHDR='"dis-thumb2.inc.h"' -Dxdis=dis_thumb2 -DFORCE_TARGET_arm)) -$(eval $(call define_test,tdarm64-simple,td-simple,$(CC) -std=c11 -DHDR='"dis-arm64.inc.h"' -Dxdis=dis -DFORCE_TARGET_arm64)) +$(eval $(call define_test,tdarm-simple,td-simple,$(CC) -std=c11 -DHDR='"arm/dis-arm.inc.h"' -Dxdis=dis_arm -DFORCE_TARGET_arm)) +$(eval $(call define_test,tdthumb-simple,td-simple,$(CC) -std=c11 -DHDR='"arm/dis-thumb.inc.h"' -Dxdis=dis_thumb -DFORCE_TARGET_arm)) +$(eval $(call define_test,tdthumb2-simple,td-simple,$(CC) -std=c11 -DHDR='"arm/dis-thumb2.inc.h"' -Dxdis=dis_thumb2 -DFORCE_TARGET_arm)) +$(eval $(call define_test,tdarm64-simple,td-simple,$(CC) -std=c11 -DHDR='"arm64/dis-arm64.inc.h"' -Dxdis=dis -DFORCE_TARGET_arm64)) $(eval $(call define_test,dis-arm,dis,$(CC) -std=c11 -DFORCE_TARGET_arm)) $(eval $(call define_test,dis-arm64,dis,$(CC) -std=c11 -DFORCE_TARGET_arm64)) $(eval $(call define_test,jump-dis-arm,jump-dis,$(CC) -std=c11 -DFORCE_TARGET_arm -O0)) diff --git a/lib/arm/dis-arm-multi.inc.h b/lib/arm/dis-arm-multi.inc.h new file mode 100644 index 0000000..56cde35 --- /dev/null +++ b/lib/arm/dis-arm-multi.inc.h @@ -0,0 +1,16 @@ +#include "dis-thumb.inc.h" +#include "dis-thumb2.inc.h" +#include "dis-arm.inc.h" + +static INLINE void P(dis)(tdis_ctx ctx) { + if (ctx->pc_low_bit) { + uint16_t op = *(uint16_t *) ctx->ptr; + bool is_32 = (op >> 13 & 7) == 7 && (op >> 11 & 3) != 0; + if (is_32) + return P(dis_thumb2)(ctx); + else + return P(dis_thumb)(ctx); + } else { + return P(dis_arm)(ctx); + } +} diff --git a/lib/arm/dis-arm.inc.h b/lib/arm/dis-arm.inc.h new file mode 100644 index 0000000..2f06234 --- /dev/null +++ b/lib/arm/dis-arm.inc.h @@ -0,0 +1,214 @@ +#include "dis.h" + +/* + ARM + 65 24-20 + LDRSB: 10 xx1x1 + LDRH: 01 xx1x1 + LDRSH: 11 xx1x1 + LDRD: 10 xx1x0 + + LDRB: ii 1u101 + LDR: ii 1u001 + + Thumb (such logical) + LDRB: 11111 00 0 U 00 1 1111 + LDRSB: 11111 00 1 U 00 1 1111 + LDRH: 11111 00 0 U 01 1 1111 + LDRSH: 11111 00 1 U 01 1 1111 + LDR: 11111 00 0 U 10 1 1111 +*/ + +/* TODO: bx lr, and handle conditionals */ + +static inline enum pcrel_load_mode get_arm_load_mode(unsigned op) { + if ((op & 0x7000090) == 0x90) { + return ((op >> 22) & 1) ? PLM_U8 : PLM_U32; + } else { + switch ((op >> 4) & 3) { + default: __builtin_abort(); + case 1: return PLM_U16; + case 2: return (op & (1 << 20)) ? PLM_S8 : PLM_U128; + case 3: return PLM_S16; + } + } +} + +static INLINE void P(GPRPairOp_Rt_addr_offset_none_addr_unk_Rd_S_2_STLEXD)(tdis_ctx ctx, struct bitslice Rt, struct bitslice Rd, struct bitslice addr) { + data(r(Rt), r(Rd), r(addr)); +} +static INLINE void P(GPR_Rm_unk_Rd_1_MOVr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) { + data(rout(Rd), r(Rm)); +} +static INLINE void P(GPR_Rn_GPR_Rm_unk_Rd_1_ADDrr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd, struct bitslice Rn) { + data(rout(Rd), r(Rm), r(Rn)); +} +static INLINE void P(GPR_Rn_so_reg_imm_shift_unk_Rd_1_ADDrsi)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rd, struct bitslice Rn) { + data(rout(Rd), rs(shift, 0, 4), r(Rn)); +} +static INLINE void P(GPR_Rn_so_reg_reg_shift_unk_Rd_1_ADDrsr)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rd, struct bitslice Rn) { + data(rout(Rd), rs(shift, 0, 4), rs(shift, 8, 4), r(Rn)); +} +static INLINE void P(GPR_Rn_unk_Rd_1_ADDri)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rn) { + data(rout(Rd), r(Rn)); +} +static INLINE void P(GPR_Rt_4_MCR)(tdis_ctx ctx, struct bitslice Rt) { + data(r(Rt)); +} +static INLINE void P(GPR_Rt_addr_offset_none_addr_S_3_STL)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr)); +} +static INLINE void P(GPR_Rt_addr_offset_none_addr_am2offset_imm_offset_S_4_STRBT_POST_IMM)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) { + data(r(addr), r(Rt)); +} +static INLINE void P(GPR_Rt_addr_offset_none_addr_am2offset_reg_offset_S_4_STRBT_POST_REG)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) { + data(r(addr), rs(offset, 0, 4), r(Rt)); +} +static INLINE void P(GPR_Rt_addr_offset_none_addr_am3offset_offset_S_2_STRD_POST)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) { + data_flags(IS_LDRD_STRD, r(Rt), r(addr), rs(offset, 0, 4)); +} +static INLINE void P(GPR_Rt_addr_offset_none_addr_postidx_imm8_offset_S_1_STRHTi)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) { + data(r(addr), r(Rt)); +} +static INLINE void P(GPR_Rt_addrmode3_addr_S_2_STRD)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + unsigned addr_val = bs_get(addr, ctx->op); + if (addr_val & 1 << 13) + data_flags(IS_LDRD_STRD, r(Rt), rs(addr, 9, 4)); + else + data_flags(IS_LDRD_STRD, r(Rt), rs(addr, 9, 4), rs(addr, 0, 4)); +} +static INLINE void P(GPR_Rt_addrmode3_pre_addr_S_2_STRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + return P(GPR_Rt_addrmode3_addr_S_2_STRD)(ctx, addr, Rt); +} +static INLINE void P(GPR_Rt_addrmode_imm12_addr_S_1_STRi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rs(addr, 13, 4), r(Rt)); +} +static INLINE void P(GPR_Rt_addrmode_imm12_pre_addr_S_2_STRB_PRE_IMM)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rs(addr, 13, 4), r(Rt)); +} +static INLINE void P(GPR_Rt_ldst_so_reg_addr_S_2_STRB_PRE_REG)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rs(addr, 0, 4), rs(addr, 13, 4), r(Rt)); +} +static INLINE void P(GPR_Rt_ldst_so_reg_shift_S_1_STRrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) { + data(rs(shift, 0, 4), rs(shift, 13, 4), r(Rt)); +} +static INLINE void P(GPRnopc_Rt_4_MCRR)(tdis_ctx ctx, UNUSED struct bitslice Rt) { + /* need Rt2 but whatever */ + return P(unidentified)(ctx); +} +static INLINE void P(GPRnopc_Rt_addrmode_imm12_addr_S_1_STRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rs(addr, 13, 4), r(Rt)); +} +static INLINE void P(GPRnopc_Rt_ldst_so_reg_shift_S_1_STRBrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) { + data(rs(shift, 13, 4), rs(shift, 0, 4), r(Rt)); +} +static INLINE void P(addr_offset_none_addr_4_LDC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { + data(r(addr)); +} +static INLINE void P(addr_offset_none_addr_S_4_STC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { + data(r(addr)); +} +static INLINE void P(addr_offset_none_addr_am2offset_imm_offset_unk_Rt_4_LDRBT_POST_IMM)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr)); +} +static INLINE void P(addr_offset_none_addr_am2offset_reg_offset_unk_Rt_4_LDRBT_POST_REG)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr), rs(offset, 0, 4)); +} +static INLINE void P(addr_offset_none_addr_am3offset_offset_unk_Rt_4_LDRD_POST)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr), rs(offset, 0, 4)); +} +static INLINE void P(addr_offset_none_addr_postidx_imm8_offset_unk_Rt_3_LDRHTi)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr)); +} +static INLINE void P(addr_offset_none_addr_postidx_imm8s4_offset_4_LDC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) { + data(r(addr)); +} +static INLINE void P(addr_offset_none_addr_postidx_imm8s4_offset_S_4_STC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) { + data(r(addr)); +} +static INLINE void P(addr_offset_none_addr_unk_Rt_13_LDA)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr)); +} +static INLINE void P(addrmode3_addr_unk_Rt_4_LDRD)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + /* ignoring Rt2 = Rt + 1, but LDRD itself isn't supposed to load PC anyway */ + unsigned addr_val = bs_get(addr, ctx->op); + if (addr_val & 1 << 13) + data_flags(IS_LDRD_STRD, rout(Rt), rs(addr, 9, 4)); + else + data_flags(IS_LDRD_STRD, rout(Rt), rs(addr, 9, 4), rs(addr, 0, 4)); +} +static INLINE void P(addrmode3_pre_addr_unk_Rt_4_LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + return P(addrmode3_addr_unk_Rt_4_LDRD)(ctx, addr, Rt); +} +static INLINE void P(addrmode5_addr_8_LDC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) { + data(rs(addr, 9, 4)); +} +static INLINE void P(addrmode5_addr_S_4_STC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) { + data(rs(addr, 9, 4)); +} +static INLINE void P(addrmode5_pre_addr_4_LDC2L_PRE)(tdis_ctx ctx, struct bitslice addr) { + data(rs(addr, 9, 4)); +} +static INLINE void P(addrmode5_pre_addr_S_4_STC2L_PRE)(tdis_ctx ctx, struct bitslice addr) { + data(rs(addr, 9, 4)); +} +static INLINE void P(addrmode_imm12_addr_unk_Rt_2_LDRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 13, 4)); +} +static INLINE void P(addrmode_imm12_pre_addr_unk_Rt_2_LDRB_PRE_IMM)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 13, 4)); +} +static INLINE void P(adrlabel_label_unk_Rd_1_ADR)(tdis_ctx ctx, struct bitslice label, struct bitslice Rd) { + return P(pcrel)(ctx, ctx->pc + 8 + bs_get(label, ctx->op), bs_get(Rd, ctx->op), PLM_ADR); +} +static INLINE void P(br_target_target_B_1_Bcc)(tdis_ctx ctx, struct bitslice target) { + bool cond = (ctx->op >> 28) != 0xe; + return P(branch)(ctx, ctx->pc + 8 + sext(bs_get(target, ctx->op), 24), /*cond*/ cond); +} +static INLINE void P(ldst_so_reg_addr_unk_Rt_2_LDRB_PRE_REG)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 0, 4), rs(addr, 13, 4)); +} +static INLINE void P(ldst_so_reg_shift_unk_Rt_2_LDRBrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) { + data(rout(Rt), rs(shift, 0, 4), rs(shift, 13, 4)); +} +static INLINE void P(tcGPR_Rm_unk_Rd_1_MOVr_TC)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) { + data(rout(Rd), r(Rm)); +} +static INLINE void P(unk_Rd_5_MOVTi16)(tdis_ctx ctx, struct bitslice Rd) { + data(rout(Rd)); +} +static INLINE void P(unk_Rt_13_MRC)(tdis_ctx ctx, struct bitslice Rt) { + data(rout(Rt)); +} +static INLINE void P(GPR_Rn_reglist_regs_16_LDMDA)(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 void P(GPR_Rn_reglist_regs_S_16_STMDA)(tdis_ctx ctx, UNUSED struct bitslice regs, UNUSED struct bitslice Rn) { + unsigned regs_val = bs_get(regs, ctx->op); + if (regs_val & (1 << 15)) + return P(bad)(ctx); + return P(unidentified)(ctx); +} +static INLINE void P(GPR_Rt_addr_offset_none_addr_unk_Rd_S_6_STLEX)(tdis_ctx ctx, struct bitslice Rt, struct bitslice Rd, struct bitslice addr) { + data(r(addr), r(Rt), r(Rd)); +} +static INLINE void P(addr_offset_none_addr_postidx_reg_Rm_unk_Rt_3_LDRHTr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr), r(Rm)); +} +static INLINE void P(GPR_Rt_addr_offset_none_addr_postidx_reg_Rm_S_1_STRHTr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rt, struct bitslice addr) { + data(r(addr), r(Rt), r(Rm)); +} +static INLINE void P(GPR_dst_B_2_BX)(tdis_ctx ctx, UNUSED struct bitslice dst) { + return P(ret)(ctx); +} + +static INLINE void P(dis_arm)(tdis_ctx ctx) { + uint32_t op = ctx->op = *(uint32_t *) ctx->ptr; + ctx->op_size = 4; + #include "../generated/generic-dis-arm.inc.h" + __builtin_abort(); +} +#define GENERATED_HEADER "../generated/generic-dis-arm.inc.h" diff --git a/lib/arm/dis-thumb.inc.h b/lib/arm/dis-thumb.inc.h new file mode 100644 index 0000000..4e6d106 --- /dev/null +++ b/lib/arm/dis-thumb.inc.h @@ -0,0 +1,79 @@ +#include "dis.h" +static INLINE void P(GPR_Rm_unk_Rdn_1_tADDhirr)(tdis_ctx ctx, struct bitslice Rdn, struct bitslice Rm) { + data(rout(Rdn), r(Rm), r(Rdn)); /* yes, twice */ +} +static INLINE void P(unk_Rdn_1_tADDrSP)(tdis_ctx ctx, UNUSED struct bitslice Rdn) { + /* this doesn't support constants, and nobody's going to add pc, sp, so... */ + return P(unidentified)(ctx); +} +static INLINE void P(GPR_Rm_1_tADDspr)(tdis_ctx ctx, UNUSED struct bitslice Rm) { + /* ditto */ + return P(unidentified)(ctx); +} +static INLINE void P(GPR_Rm_B_1_tBX)(tdis_ctx ctx, struct bitslice Rm) { + unsigned val = bs_get(Rm, ctx->op); + if (val == 15) /* bx pc */ + return P(bad)(ctx); + else if (val == 14) /* bx lr */ + return P(ret)(ctx); + return P(unidentified)(ctx); +} +static INLINE void P(GPR_Rm_unk_Rd_1_tMOVr)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rm) { + unsigned Rd_val = bs_get(Rd, ctx->op); + unsigned Rm_val = bs_get(Rm, ctx->op); + /* does anyone do this? */ + if (Rd_val == 15) + return P(bad)(ctx); + if (Rm_val == 15) + return P(pcrel)(ctx, ctx->pc + 4, Rd_val, PLM_ADR); + return P(unidentified)(ctx); +} +static INLINE void P(tGPR_Rn_reglist_regs_1_tLDMIA)(tdis_ctx ctx, UNUSED struct bitslice regs, UNUSED struct bitslice Rn) { + return P(unidentified)(ctx); +} +static INLINE void 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 void P(reglist_regs_1_tPOP)(tdis_ctx ctx, struct bitslice regs) { + unsigned regs_val = bs_get(regs, ctx->op); + if(regs_val & (1 << 15)) + return P(ret)(ctx); + return P(unidentified)(ctx); +} +static INLINE void P(reglist_regs_S_1_tPUSH)(tdis_ctx ctx, UNUSED struct bitslice regs) { + return P(unidentified)(ctx); +} +static INLINE void 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), PLM_U32); +} +static INLINE void 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), PLM_ADR); +} +static INLINE void P(t_bcctarget_target_B_1_tBcc)(tdis_ctx ctx, struct bitslice target) { + return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 8), /*cond*/ true); +} +static INLINE void P(t_brtarget_target_B_1_tB)(tdis_ctx ctx, struct bitslice target) { + bool cond = ctx->arch.thumb_it_length > 0; + return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 11), cond); +} +static INLINE void P(t_cbtarget_target_B_2_tCBNZ)(tdis_ctx ctx, struct bitslice target) { + return P(branch)(ctx, ctx->pc + 4 + 2 * bs_get(target, ctx->op), /*cond*/ true); +} +static INLINE void P(it_pred_cc_it_mask_mask_1_t2IT)(tdis_ctx ctx, struct bitslice mask, UNUSED struct bitslice cc) { + /* why */ + unsigned mask_val = bs_get(mask, ctx->op); + unsigned length = __builtin_ctz(mask_val); + if (length >= 3) + return P(unidentified)(ctx); /* nop */ + ctx->arch.thumb_it_length = length; + return P(unidentified)(ctx); +} + +static INLINE void P(dis_thumb)(tdis_ctx ctx) { + uint16_t op = ctx->op = *(uint16_t *) ctx->ptr; + ctx->op_size = 2; + if (ctx->arch.thumb_it_length) + ctx->arch.thumb_it_length--; + #include "../generated/generic-dis-thumb.inc.h" + __builtin_abort(); +} diff --git a/lib/arm/dis-thumb2.inc.h b/lib/arm/dis-thumb2.inc.h new file mode 100644 index 0000000..a9d7f9d --- /dev/null +++ b/lib/arm/dis-thumb2.inc.h @@ -0,0 +1,185 @@ +#include "dis.h" + +static inline unsigned flip16(unsigned op) { + return op >> 16 | op << 16; +} + +static inline enum pcrel_load_mode get_thumb2_load_mode(unsigned op) { + op = flip16(op); + bool sign = (op >> 8) & 1; + switch ((op >> 5) & 3) { + case 0: return sign ? PLM_S8 : PLM_U8; + case 1: return sign ? PLM_S16 : PLM_U16; + case 2: return PLM_U32; + default: __builtin_abort(); + } +} + +static INLINE void P(GPR_Rm_unk_Rd_1_t2MOVr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) { + data(rout(Rd), r(Rm)); +} +static INLINE void 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 void 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 void P(GPR_Rn_unk_Rd_1_t2ADDri12)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rn) { + data(rout(Rd), r(Rn)); +} +static INLINE void P(GPR_Rt_8_VMOVDRR)(tdis_ctx ctx, UNUSED struct bitslice Rt) { + return P(unidentified)(ctx); /* don't care */ +} +static INLINE void P(GPR_Rt_t2addrmode_imm12_addr_S_1_t2STRi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rs(addr, 13, 4), r(Rt)); +} +static INLINE void P(GPR_Rt_t2addrmode_negimm8_addr_S_1_t2STRi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rs(addr, 9, 4), r(Rt)); +} +static INLINE void P(GPR_Rt_t2addrmode_so_reg_addr_S_1_t2STRs)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rs(addr, 6, 4), rs(addr, 2, 4), r(Rt)); +} +static INLINE void P(GPRnopc_Rn_rGPR_Rm_unk_Rd_1_t2ADDrr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd, struct bitslice Rn) { + data(rout(Rd), r(Rm), r(Rn)); +} +static INLINE void P(GPRnopc_Rn_unk_Rd_2_t2ADDri)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rn) { + data(rout(Rd), r(Rn)); +} +static INLINE void 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 void 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 void 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 void 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), r(Rt), r(addr)); +} +static INLINE void P(addr_offset_none_addr_4_t2LDC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { + data(r(addr)); +} +static INLINE void P(addr_offset_none_addr_S_4_t2STC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { + data(r(addr)); +} +static INLINE void P(addr_offset_none_addr_postidx_imm8s4_offset_4_t2LDC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) { + data(r(addr)); +} +static INLINE void P(addr_offset_none_addr_postidx_imm8s4_offset_S_4_t2STC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) { + data(r(addr)); +} +static INLINE void P(addr_offset_none_addr_unk_Rt_11_t2LDA)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { + data(rout(Rt), r(addr)); +} +static INLINE void P(addrmode5_addr_8_VLDRD)(tdis_ctx ctx, struct bitslice addr) { + data(rs(addr, 9, 4)); +} +static INLINE void P(addrmode5_addr_S_4_t2STC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) { + data(rs(addr, 9, 4)); +} +static INLINE void P(addrmode5_pre_addr_4_t2LDC2L_PRE)(tdis_ctx ctx, struct bitslice addr) { + data(rs(addr, 9, 4)); +} +static INLINE void P(addrmode5_pre_addr_S_4_t2STC2L_PRE)(tdis_ctx ctx, struct bitslice addr) { + data(rs(addr, 9, 4)); +} +static INLINE void P(brtarget_target_B_1_t2Bcc)(tdis_ctx ctx, struct bitslice target) { + return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 20), /*cond*/ true); +} +static INLINE void 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 void 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)); +} +static INLINE void P(rGPR_Rt_t2addrmode_imm8_pre_addr_S_2_t2STRB_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(r(Rt), rs(addr, 9, 4)); +} +static INLINE void P(rGPR_Rt_t2addrmode_imm8s4_addr_S_1_t2STRDi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data_flags(IS_LDRD_STRD, r(Rt), rs(addr, 9, 4)); +} +static INLINE void P(rGPR_Rt_t2addrmode_imm8s4_pre_addr_S_1_t2STRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data_flags(IS_LDRD_STRD, r(Rt), rs(addr, 9, 4)); +} +static INLINE void P(rGPR_Rt_t2addrmode_negimm8_addr_S_2_t2STRBi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(r(Rt), rs(addr, 9, 4)); +} +static INLINE void 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 void 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 void P(t2addrmode_imm0_1020s4_addr_unk_Rt_1_t2LDREX)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 8, 4)); +} +static INLINE void P(t2addrmode_imm12_addr_unk_Rt_5_t2LDRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 13, 4)); +} +static INLINE void P(t2addrmode_imm8_addr_unk_Rt_S_3_t2STRBT)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(r(Rt), rs(addr, 9, 4)); +} +static INLINE void P(t2addrmode_imm8_pre_addr_unk_Rt_5_t2LDRB_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 9, 4)); +} +static INLINE void 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 void P(t2addrmode_imm8s4_addr_unk_Rt_1_t2LDRDi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data_flags(IS_LDRD_STRD, rout(Rt), rs(addr, 9, 4)); +} +static INLINE void P(t2addrmode_imm8s4_pre_addr_unk_Rt_1_t2LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data_flags(IS_LDRD_STRD, rout(Rt), rs(addr, 9, 4)); +} +static INLINE void P(t2addrmode_negimm8_addr_unk_Rt_5_t2LDRBi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 9, 4)); +} +static INLINE void P(t2addrmode_posimm8_addr_unk_Rt_5_t2LDRBT)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { + data(rout(Rt), rs(addr, 9, 4)); +} +static INLINE void 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 void 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), PLM_ADR); +} +static INLINE void 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(Rt, ctx->op), get_thumb2_load_mode(ctx->op)); +} +static INLINE void P(uncondbrtarget_target_B_1_t2B)(tdis_ctx ctx, struct bitslice target) { + bool cond = ctx->arch.thumb_it_length > 0; + return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 24), cond); +} +static INLINE void P(unk_Rd_3_t2MOVTi16)(tdis_ctx ctx, struct bitslice Rd) { + data(rout(Rd)); +} +static INLINE void 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 void P(unk_Rt_13_VMOVRRD)(tdis_ctx ctx, UNUSED struct bitslice Rt) { + return P(unidentified)(ctx); +} + +static INLINE void do_it(tdis_ctx ctx) { + uint32_t op = ctx->op; + #include "../generated/generic-dis-thumb2.inc.h" + __builtin_abort(); +} + +static INLINE void P(dis_thumb2)(tdis_ctx ctx) { + ctx->op = *(uint32_t *) ctx->ptr; + ctx->op_size = 4; + if (ctx->arch.thumb_it_length) + ctx->arch.thumb_it_length--; + /* LLVM likes to think about Thumb2 instructions the way the ARM manual + * does - 15..0 15..0 rather than 31..0 as actually laid out in memory... */ + ctx->op = flip16(ctx->op); + do_it(ctx); + TDIS_CTX_SET_NEWOP(ctx, flip16(TDIS_CTX_NEWOP(ctx))); + ctx->op = flip16(ctx->op); +} diff --git a/lib/arm/misc.h b/lib/arm/misc.h new file mode 100644 index 0000000..44d8e7a --- /dev/null +++ b/lib/arm/misc.h @@ -0,0 +1,7 @@ +#pragma once +#define TARGET_DIS_SUPPORTED +#define TARGET_DIS_HEADER "arm/dis-arm-multi.inc.h" +#define TARGET_TRANSFORM_DIS_HEADER "arm/transform-dis-arm-multi.inc.h" +#define MIN_INSN_SIZE 2 +struct arch_dis_ctx { unsigned thumb_it_length; }; +enum { IS_LDRD_STRD = 1 << 16 }; diff --git a/lib/arm/transform-dis-arm-multi.inc.h b/lib/arm/transform-dis-arm-multi.inc.h new file mode 100644 index 0000000..662b501 --- /dev/null +++ b/lib/arm/transform-dis-arm-multi.inc.h @@ -0,0 +1,189 @@ +static inline void PUSHone(struct transform_dis_ctx *ctx, int Rt) { + if (ctx->pc_low_bit) + op32(ctx, 0x0d04f84d | Rt << 28); + else + op32(ctx, 0xe52d0004 | Rt << 12); +} + +static inline void POPone(struct transform_dis_ctx *ctx, int Rt) { + if (ctx->pc_low_bit) + op32(ctx, 0x0b04f85d | Rt << 28); + else + op32(ctx, 0xe49d0004 | Rt << 12); +} + +static inline void POPmulti(struct transform_dis_ctx *ctx, uint16_t mask) { + if (ctx->pc_low_bit) + op32(ctx, 0x0000e8bd | mask << 16); + else + op32(ctx, 0xe8bd0000 | mask); +} + +static inline void MOVW_MOVT(struct transform_dis_ctx *ctx, int Rd, uint32_t val) { + uint16_t hi = val >> 16, lo = (uint16_t) val; + if (ctx->pc_low_bit) { + op32(ctx, 0x0000f240 | Rd << 24 | lo >> 12 | (lo >> 11 & 1) << 10 | + (lo >> 8 & 7) << 28 | (lo & 0xff) << 16); + op32(ctx, 0x0000f2c0 | Rd << 24 | hi >> 12 | (hi >> 11 & 1) << 10 | + (hi >> 8 & 7) << 28 | (hi & 0xff) << 16); + + } else { + op32(ctx, 0xe3000000 | Rd << 12 | (lo >> 12) << 16 | (lo & 0xfff)); + op32(ctx, 0xe3400000 | Rd << 12 | (hi >> 12) << 16 | (hi & 0xfff)); + } + +} + +static inline void STRri(struct transform_dis_ctx *ctx, int Rt, int Rn, uint32_t off) { + if (ctx->pc_low_bit) + op32(ctx, 0x0000f8c0 | Rn | Rt << 28 | off << 16); + else + op32(ctx, 0xe4800000 | Rn << 16 | Rt << 12 | off); +} + +static inline void LDRxi(struct transform_dis_ctx *ctx, int Rt, int Rn, uint32_t off, + enum pcrel_load_mode load_mode) { + if (ctx->pc_low_bit) { + int subop, sign; + switch (load_mode) { + case PLM_U8: subop = 0; sign = 0; break; + case PLM_S8: subop = 0; sign = 1; break; + case PLM_U16: subop = 1; sign = 0; break; + case PLM_S16: subop = 1; sign = 1; break; + case PLM_U32: subop = 2; sign = 0; break; + default: __builtin_abort(); + } + op32(ctx, 0x0000f890 | Rn | Rt << 28 | subop << 5 | sign << 8 | off << 16); + } else { + int is_byte, subop, not_ldrd; + switch (load_mode) { + case PLM_U8: is_byte = 1; goto type1; + case PLM_S8: subop = 13; not_ldrd = 1; goto type2; + case PLM_U16: subop = 11; not_ldrd = 1; goto type2; + case PLM_S16: subop = 15; not_ldrd = 1; goto type2; + case PLM_U32: is_byte = 0; goto type1; + case PLM_U128: subop = 13; not_ldrd = 0; goto type2; + type1: + op32(ctx, 0xe5900000 | Rn << 16 | Rt << 12 | off); + break; + type2: + op32(ctx, 0xe1c00000 | Rn << 16 | Rt << 12 | subop << 4 | + (off & 0xf) | (off & 0xf0) << 4 | not_ldrd << 20); + break; + default: + __builtin_abort(); + } + } +} + +static NOINLINE UNUSED void transform_dis_data(struct transform_dis_ctx *ctx, + unsigned o0, unsigned o1, unsigned o2, unsigned o3, unsigned out_mask) { +#ifdef TRANSFORM_DIS_VERBOSE + printf("transform_dis_data: (%p) %x %x %x %x out_mask=%x\n", (void *) ctx->pc, + o0, o1, o2, o3, out_mask); +#endif + /* We only care if at least one op is PC, so quickly test that. */ + if (((o0 | o1 | o2 | o3) & 15) != 15) + return; + unsigned *newval = ctx->newval; + newval[0] = o0; + newval[1] = o1; + newval[2] = o2; + newval[3] = o3; + + void **rpp = ctx->rewritten_ptr_ptr; + + /* A few cases: + * 1. Move to PC that does not read PC. Probably fine. + * 2. Move to PC that does read PC, e.g. 'ldrls pc, [pc, r0, lsl #2]'. + * This is different from #4 mainly in that we can't need to do + * something like pop {temp, pc}. Not terribly plausible (only likely + * in non-position-independent code in ARM mode, and I can't get it to + * happen in the first 8 bytes then), but we may as well handle it. + * 3. Read of PC that does not read the register(s) it writes, e.g. adr r3, + * X. In this case we can use that register as a temporary. + * 4. Read of PC that does, or doesn't have any output register, e.g. add + * r3, pc. In this case we use the stack because reliably finding a + * free register would be work, and might not even be possible (thumb + * mov r9, r0; mov r12, r1; ) + * the out register is always first. + */ + uint16_t in_regs = 0; + int out_reg = -1; + for (int i = 0; i < 4; i++) { + if (out_mask & 1 << i) + out_reg = newval[i]; + else if (newval[i] != null_op) + in_regs |= 1 << newval[i]; + } + if (out_mask & IS_LDRD_STRD) + in_regs |= 1 << (newval[0] + 1); + uint32_t pc = ctx->pc + (ctx->pc_low_bit ? 4 : 8); + int scratch = __builtin_ctz(~(in_regs | (1 << out_reg))); + +#ifdef TRANSFORM_DIS_VERBOSE + printf("transform_dis_data: in_regs=%x out_reg=%d pc=%x scratch=%d\n", + in_regs, out_reg, pc, scratch); +#endif + + if (out_reg == 15) { + if (in_regs & 1 << 15) + return; /* case 1 */ + /* case 2 */ + PUSHone(ctx, scratch); + PUSHone(ctx, scratch); + MOVW_MOVT(ctx, scratch, pc); + for (int i = 0; i < 4; i++) + if (newval[i] == 15) + newval[i] = scratch; + ctx->write_newop_here = *rpp; *rpp += ctx->op_size; + STRri(ctx, scratch, 13, 4); + POPmulti(ctx, 1 << scratch | 1 << 15); + } else { + if (out_reg != -1 && !(in_regs & 1 << out_reg)) { + /* case 3 - ignore scratch */ + MOVW_MOVT(ctx, out_reg, pc); + for (int i = 0; i < 4; i++) + if (newval[i] == 15) + newval[i] = out_reg; + ctx->write_newop_here = *rpp; *rpp += ctx->op_size; + } else { + /* case 4 */ + PUSHone(ctx, scratch); + MOVW_MOVT(ctx, scratch, pc); + for (int i = 0; i < 4; i++) + if (newval[i] == 15) + newval[i] = scratch; + ctx->write_newop_here = *rpp; *rpp += ctx->op_size; + POPone(ctx, scratch); + } + } + ctx->modify = true; +#ifdef TRANSFORM_DIS_VERBOSE + printf("transform_dis_data: => %x %x %x %x\n", + newval[0], newval[1], newval[2], newval[3]); +#endif +} + +static NOINLINE UNUSED void transform_dis_pcrel(struct transform_dis_ctx *ctx, + uintptr_t dpc, unsigned reg, enum pcrel_load_mode load_mode) { +#ifdef TRANSFORM_DIS_VERBOSE + printf("transform_dis_pcrel: (%p) dpc=%p reg=%x mode=%d\n", (void *) ctx->pc, + (void *) dpc, reg, load_mode); +#endif + ctx->write_newop_here = NULL; + if (reg == 15) { + int scratch = 0; + PUSHone(ctx, scratch); + PUSHone(ctx, scratch); + MOVW_MOVT(ctx, scratch, dpc); + if (load_mode != PLM_ADR) + LDRxi(ctx, scratch, scratch, 0, load_mode); + STRri(ctx, scratch, 13, 4); + POPmulti(ctx, 1 << scratch | 1 << 15); + } else { + MOVW_MOVT(ctx, reg, dpc); + if (load_mode != PLM_ADR) + LDRxi(ctx, reg, reg, 0, load_mode); + } +} diff --git a/lib/arm64/dis-arm64.inc.h b/lib/arm64/dis-arm64.inc.h new file mode 100644 index 0000000..5317b89 --- /dev/null +++ b/lib/arm64/dis-arm64.inc.h @@ -0,0 +1,49 @@ +static INLINE void P(adrlabel_label_unk_Xd_1_ADR)(tdis_ctx ctx, struct bitslice Xd, struct bitslice label) { + return P(pcrel)(ctx, ctx->pc + sext(bs_get(label, ctx->op), 22), bs_get(Xd, ctx->op), PLM_ADR); +} +static INLINE void P(adrplabel_label_unk_Xd_1_ADRP)(tdis_ctx ctx, struct bitslice Xd, struct bitslice label) { + return P(pcrel)(ctx, ctx->pc + (sext(bs_get(label, ctx->op), 22) << 12), bs_get(Xd, ctx->op), PLM_ADR); +} +static INLINE void P(am_b_target_addr_B_1_B)(tdis_ctx ctx, struct bitslice addr) { + return P(branch)(ctx, ctx->pc + sext(bs_get(addr, ctx->op), 26) * 4, /*cond*/ false); +} +static INLINE void P(am_bl_target_addr_1_BL)(tdis_ctx ctx, struct bitslice addr) { + return P(branch)(ctx, ctx->pc + sext(bs_get(addr, ctx->op), 26) * 4, /*cond*/ false); +} +static INLINE void P(am_brcond_target_B_5_Bcc)(tdis_ctx ctx, struct bitslice target) { + return P(branch)(ctx, ctx->pc + sext(bs_get(target, ctx->op), 19) * 4, /*cond*/ true); +} +static INLINE void P(am_ldrlit_label_unk_Rt_6_LDRDl)(tdis_ctx ctx, struct bitslice Rt, struct bitslice label) { + enum pcrel_load_mode mode; + if ((ctx->op >> 26) & 1) { + switch (ctx->op >> 30) { + case 0: mode = PLM_U32_SIMD; break; + case 1: mode = PLM_U64_SIMD; break; + case 2: mode = PLM_U128_SIMD; break; + default: __builtin_abort(); + } + } else { + switch (ctx->op >> 30) { + case 0: mode = PLM_U32; break; + case 1: mode = PLM_U64; break; + case 2: mode = PLM_S32; break; + default: __builtin_abort(); + } + } + return P(pcrel)(ctx, ctx->pc + sext(bs_get(label, ctx->op), 19) * 4, bs_get(Rt, ctx->op), mode); +} +static INLINE void P(am_tbrcond_target_B_4_TBNZW)(tdis_ctx ctx, struct bitslice target) { + return P(branch)(ctx, ctx->pc + sext(bs_get(target, ctx->op), 14) * 4, /*cond*/ true); +} +static INLINE void P(GPR64_Rn_1_RET)(tdis_ctx ctx, UNUSED struct bitslice Rn) { + return P(ret)(ctx); +} + +static INLINE void P(dis)(tdis_ctx ctx) { + uint32_t op = ctx->op = *(uint32_t *) ctx->ptr; + ctx->op_size = 4; + /* clang doesn't realize that this is unreachable and generates code like + * "and ecx, 0x1f; cmp ecx, 0x1f; ja abort". Yeah, nice job there. */ + #include "../generated/generic-dis-arm64.inc.h" + __builtin_abort(); +} diff --git a/lib/arm64/misc.h b/lib/arm64/misc.h new file mode 100644 index 0000000..672e1bd --- /dev/null +++ b/lib/arm64/misc.h @@ -0,0 +1,6 @@ +#pragma once +#define TARGET_DIS_SUPPORTED +#define TARGET_DIS_HEADER "arm64/dis-arm64.inc.h" +#define TARGET_TRANSFORM_DIS_HEADER "arm64/transform-dis-arm64.inc.h" +#define MIN_INSN_SIZE 4 +struct arch_dis_ctx {}; diff --git a/lib/arm64/transform-dis-arm64.inc.h b/lib/arm64/transform-dis-arm64.inc.h new file mode 100644 index 0000000..c47971b --- /dev/null +++ b/lib/arm64/transform-dis-arm64.inc.h @@ -0,0 +1,52 @@ +static inline void MOVi64(struct transform_dis_ctx *ctx, int Rd, uint64_t val) { + int shift_nybbles = 0; + do { + int k = shift_nybbles != 0 ? 1 : 0; + op32(ctx, 0x69400000 | k << 28 | Rd | (val & 0xffff) << 4 | shift_nybbles << 20); + shift_nybbles++; + val >>= 16; + } while(val); +} + +static inline void LDRxi(struct transform_dis_ctx *ctx, int Rt, int Rn, uint32_t off, + bool regsize_64, enum pcrel_load_mode load_mode) { + int size, opc; + bool sign, simd; + switch (load_mode) { + case PLM_U8: size = 0; sign = false; simd = false; break; + case PLM_S8: size = 0; sign = true; simd = false; break; + case PLM_U16: size = 1; sign = false; simd = false; break; + case PLM_S16: size = 1; sign = true; simd = false; break; + case PLM_U32: size = 2; sign = false; simd = false; break; + case PLM_S32: size = 2; sign = true; simd = false; break; + case PLM_U64: size = 3; sign = false; simd = false; break; + case PLM_U32_SIMD: size = 2; opc = 1; simd = true; break; + case PLM_U64_SIMD: size = 3; opc = 1; simd = true; break; + case PLM_U128_SIMD: size = 0; opc = 3; simd = true; break; + default: __builtin_abort(); + } + if (simd) { + off /= 1 << (size | (opc & 1) << 2); + } else { + off /= 1 << size; + opc = sign ? (regsize_64 ? 2 : 3) : 1; + } + op32(ctx, 0x39000000 | Rt | Rn << 5 | off << 10 | opc << 22 | simd << 26 | size << 30); +} + + +static NOINLINE UNUSED void transform_dis_pcrel(struct transform_dis_ctx *ctx, + uintptr_t dpc, unsigned reg, enum pcrel_load_mode load_mode) { + ctx->write_newop_here = NULL; + if (load_mode >= PLM_U32_SIMD) { + /* use x0 as scratch */ + op32(ctx, 0xf81f0fe0); /* str x0, [sp, #-0x10]! */ + MOVi64(ctx, 0, dpc); + LDRxi(ctx, reg, 0, 0, true, load_mode); + op32(ctx, 0xf84107e0); /* ldr x0, [sp], #0x10 */ + } else { + MOVi64(ctx, reg, dpc); + LDRxi(ctx, reg, reg, 0, true, load_mode); + } +} + diff --git a/lib/dis-arm-multi.inc.h b/lib/dis-arm-multi.inc.h deleted file mode 100644 index 56cde35..0000000 --- a/lib/dis-arm-multi.inc.h +++ /dev/null @@ -1,16 +0,0 @@ -#include "dis-thumb.inc.h" -#include "dis-thumb2.inc.h" -#include "dis-arm.inc.h" - -static INLINE void P(dis)(tdis_ctx ctx) { - if (ctx->pc_low_bit) { - uint16_t op = *(uint16_t *) ctx->ptr; - bool is_32 = (op >> 13 & 7) == 7 && (op >> 11 & 3) != 0; - if (is_32) - return P(dis_thumb2)(ctx); - else - return P(dis_thumb)(ctx); - } else { - return P(dis_arm)(ctx); - } -} diff --git a/lib/dis-arm.inc.h b/lib/dis-arm.inc.h deleted file mode 100644 index 2f06234..0000000 --- a/lib/dis-arm.inc.h +++ /dev/null @@ -1,214 +0,0 @@ -#include "dis.h" - -/* - ARM - 65 24-20 - LDRSB: 10 xx1x1 - LDRH: 01 xx1x1 - LDRSH: 11 xx1x1 - LDRD: 10 xx1x0 - - LDRB: ii 1u101 - LDR: ii 1u001 - - Thumb (such logical) - LDRB: 11111 00 0 U 00 1 1111 - LDRSB: 11111 00 1 U 00 1 1111 - LDRH: 11111 00 0 U 01 1 1111 - LDRSH: 11111 00 1 U 01 1 1111 - LDR: 11111 00 0 U 10 1 1111 -*/ - -/* TODO: bx lr, and handle conditionals */ - -static inline enum pcrel_load_mode get_arm_load_mode(unsigned op) { - if ((op & 0x7000090) == 0x90) { - return ((op >> 22) & 1) ? PLM_U8 : PLM_U32; - } else { - switch ((op >> 4) & 3) { - default: __builtin_abort(); - case 1: return PLM_U16; - case 2: return (op & (1 << 20)) ? PLM_S8 : PLM_U128; - case 3: return PLM_S16; - } - } -} - -static INLINE void P(GPRPairOp_Rt_addr_offset_none_addr_unk_Rd_S_2_STLEXD)(tdis_ctx ctx, struct bitslice Rt, struct bitslice Rd, struct bitslice addr) { - data(r(Rt), r(Rd), r(addr)); -} -static INLINE void P(GPR_Rm_unk_Rd_1_MOVr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) { - data(rout(Rd), r(Rm)); -} -static INLINE void P(GPR_Rn_GPR_Rm_unk_Rd_1_ADDrr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd, struct bitslice Rn) { - data(rout(Rd), r(Rm), r(Rn)); -} -static INLINE void P(GPR_Rn_so_reg_imm_shift_unk_Rd_1_ADDrsi)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rd, struct bitslice Rn) { - data(rout(Rd), rs(shift, 0, 4), r(Rn)); -} -static INLINE void P(GPR_Rn_so_reg_reg_shift_unk_Rd_1_ADDrsr)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rd, struct bitslice Rn) { - data(rout(Rd), rs(shift, 0, 4), rs(shift, 8, 4), r(Rn)); -} -static INLINE void P(GPR_Rn_unk_Rd_1_ADDri)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rn) { - data(rout(Rd), r(Rn)); -} -static INLINE void P(GPR_Rt_4_MCR)(tdis_ctx ctx, struct bitslice Rt) { - data(r(Rt)); -} -static INLINE void P(GPR_Rt_addr_offset_none_addr_S_3_STL)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { - data(rout(Rt), r(addr)); -} -static INLINE void P(GPR_Rt_addr_offset_none_addr_am2offset_imm_offset_S_4_STRBT_POST_IMM)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) { - data(r(addr), r(Rt)); -} -static INLINE void P(GPR_Rt_addr_offset_none_addr_am2offset_reg_offset_S_4_STRBT_POST_REG)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) { - data(r(addr), rs(offset, 0, 4), r(Rt)); -} -static INLINE void P(GPR_Rt_addr_offset_none_addr_am3offset_offset_S_2_STRD_POST)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) { - data_flags(IS_LDRD_STRD, r(Rt), r(addr), rs(offset, 0, 4)); -} -static INLINE void P(GPR_Rt_addr_offset_none_addr_postidx_imm8_offset_S_1_STRHTi)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) { - data(r(addr), r(Rt)); -} -static INLINE void P(GPR_Rt_addrmode3_addr_S_2_STRD)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - unsigned addr_val = bs_get(addr, ctx->op); - if (addr_val & 1 << 13) - data_flags(IS_LDRD_STRD, r(Rt), rs(addr, 9, 4)); - else - data_flags(IS_LDRD_STRD, r(Rt), rs(addr, 9, 4), rs(addr, 0, 4)); -} -static INLINE void P(GPR_Rt_addrmode3_pre_addr_S_2_STRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - return P(GPR_Rt_addrmode3_addr_S_2_STRD)(ctx, addr, Rt); -} -static INLINE void P(GPR_Rt_addrmode_imm12_addr_S_1_STRi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rs(addr, 13, 4), r(Rt)); -} -static INLINE void P(GPR_Rt_addrmode_imm12_pre_addr_S_2_STRB_PRE_IMM)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rs(addr, 13, 4), r(Rt)); -} -static INLINE void P(GPR_Rt_ldst_so_reg_addr_S_2_STRB_PRE_REG)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rs(addr, 0, 4), rs(addr, 13, 4), r(Rt)); -} -static INLINE void P(GPR_Rt_ldst_so_reg_shift_S_1_STRrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) { - data(rs(shift, 0, 4), rs(shift, 13, 4), r(Rt)); -} -static INLINE void P(GPRnopc_Rt_4_MCRR)(tdis_ctx ctx, UNUSED struct bitslice Rt) { - /* need Rt2 but whatever */ - return P(unidentified)(ctx); -} -static INLINE void P(GPRnopc_Rt_addrmode_imm12_addr_S_1_STRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rs(addr, 13, 4), r(Rt)); -} -static INLINE void P(GPRnopc_Rt_ldst_so_reg_shift_S_1_STRBrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) { - data(rs(shift, 13, 4), rs(shift, 0, 4), r(Rt)); -} -static INLINE void P(addr_offset_none_addr_4_LDC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { - data(r(addr)); -} -static INLINE void P(addr_offset_none_addr_S_4_STC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { - data(r(addr)); -} -static INLINE void P(addr_offset_none_addr_am2offset_imm_offset_unk_Rt_4_LDRBT_POST_IMM)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) { - data(rout(Rt), r(addr)); -} -static INLINE void P(addr_offset_none_addr_am2offset_reg_offset_unk_Rt_4_LDRBT_POST_REG)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) { - data(rout(Rt), r(addr), rs(offset, 0, 4)); -} -static INLINE void P(addr_offset_none_addr_am3offset_offset_unk_Rt_4_LDRD_POST)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) { - data(rout(Rt), r(addr), rs(offset, 0, 4)); -} -static INLINE void P(addr_offset_none_addr_postidx_imm8_offset_unk_Rt_3_LDRHTi)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) { - data(rout(Rt), r(addr)); -} -static INLINE void P(addr_offset_none_addr_postidx_imm8s4_offset_4_LDC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) { - data(r(addr)); -} -static INLINE void P(addr_offset_none_addr_postidx_imm8s4_offset_S_4_STC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) { - data(r(addr)); -} -static INLINE void P(addr_offset_none_addr_unk_Rt_13_LDA)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { - data(rout(Rt), r(addr)); -} -static INLINE void P(addrmode3_addr_unk_Rt_4_LDRD)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - /* ignoring Rt2 = Rt + 1, but LDRD itself isn't supposed to load PC anyway */ - unsigned addr_val = bs_get(addr, ctx->op); - if (addr_val & 1 << 13) - data_flags(IS_LDRD_STRD, rout(Rt), rs(addr, 9, 4)); - else - data_flags(IS_LDRD_STRD, rout(Rt), rs(addr, 9, 4), rs(addr, 0, 4)); -} -static INLINE void P(addrmode3_pre_addr_unk_Rt_4_LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - return P(addrmode3_addr_unk_Rt_4_LDRD)(ctx, addr, Rt); -} -static INLINE void P(addrmode5_addr_8_LDC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) { - data(rs(addr, 9, 4)); -} -static INLINE void P(addrmode5_addr_S_4_STC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) { - data(rs(addr, 9, 4)); -} -static INLINE void P(addrmode5_pre_addr_4_LDC2L_PRE)(tdis_ctx ctx, struct bitslice addr) { - data(rs(addr, 9, 4)); -} -static INLINE void P(addrmode5_pre_addr_S_4_STC2L_PRE)(tdis_ctx ctx, struct bitslice addr) { - data(rs(addr, 9, 4)); -} -static INLINE void P(addrmode_imm12_addr_unk_Rt_2_LDRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rout(Rt), rs(addr, 13, 4)); -} -static INLINE void P(addrmode_imm12_pre_addr_unk_Rt_2_LDRB_PRE_IMM)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rout(Rt), rs(addr, 13, 4)); -} -static INLINE void P(adrlabel_label_unk_Rd_1_ADR)(tdis_ctx ctx, struct bitslice label, struct bitslice Rd) { - return P(pcrel)(ctx, ctx->pc + 8 + bs_get(label, ctx->op), bs_get(Rd, ctx->op), PLM_ADR); -} -static INLINE void P(br_target_target_B_1_Bcc)(tdis_ctx ctx, struct bitslice target) { - bool cond = (ctx->op >> 28) != 0xe; - return P(branch)(ctx, ctx->pc + 8 + sext(bs_get(target, ctx->op), 24), /*cond*/ cond); -} -static INLINE void P(ldst_so_reg_addr_unk_Rt_2_LDRB_PRE_REG)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rout(Rt), rs(addr, 0, 4), rs(addr, 13, 4)); -} -static INLINE void P(ldst_so_reg_shift_unk_Rt_2_LDRBrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) { - data(rout(Rt), rs(shift, 0, 4), rs(shift, 13, 4)); -} -static INLINE void P(tcGPR_Rm_unk_Rd_1_MOVr_TC)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) { - data(rout(Rd), r(Rm)); -} -static INLINE void P(unk_Rd_5_MOVTi16)(tdis_ctx ctx, struct bitslice Rd) { - data(rout(Rd)); -} -static INLINE void P(unk_Rt_13_MRC)(tdis_ctx ctx, struct bitslice Rt) { - data(rout(Rt)); -} -static INLINE void P(GPR_Rn_reglist_regs_16_LDMDA)(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 void P(GPR_Rn_reglist_regs_S_16_STMDA)(tdis_ctx ctx, UNUSED struct bitslice regs, UNUSED struct bitslice Rn) { - unsigned regs_val = bs_get(regs, ctx->op); - if (regs_val & (1 << 15)) - return P(bad)(ctx); - return P(unidentified)(ctx); -} -static INLINE void P(GPR_Rt_addr_offset_none_addr_unk_Rd_S_6_STLEX)(tdis_ctx ctx, struct bitslice Rt, struct bitslice Rd, struct bitslice addr) { - data(r(addr), r(Rt), r(Rd)); -} -static INLINE void P(addr_offset_none_addr_postidx_reg_Rm_unk_Rt_3_LDRHTr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rt, struct bitslice addr) { - data(rout(Rt), r(addr), r(Rm)); -} -static INLINE void P(GPR_Rt_addr_offset_none_addr_postidx_reg_Rm_S_1_STRHTr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rt, struct bitslice addr) { - data(r(addr), r(Rt), r(Rm)); -} -static INLINE void P(GPR_dst_B_2_BX)(tdis_ctx ctx, UNUSED struct bitslice dst) { - return P(ret)(ctx); -} - -static INLINE void P(dis_arm)(tdis_ctx ctx) { - uint32_t op = ctx->op = *(uint32_t *) ctx->ptr; - ctx->op_size = 4; - #include "../generated/generic-dis-arm.inc.h" - __builtin_abort(); -} -#define GENERATED_HEADER "../generated/generic-dis-arm.inc.h" diff --git a/lib/dis-arm64.inc.h b/lib/dis-arm64.inc.h deleted file mode 100644 index 5317b89..0000000 --- a/lib/dis-arm64.inc.h +++ /dev/null @@ -1,49 +0,0 @@ -static INLINE void P(adrlabel_label_unk_Xd_1_ADR)(tdis_ctx ctx, struct bitslice Xd, struct bitslice label) { - return P(pcrel)(ctx, ctx->pc + sext(bs_get(label, ctx->op), 22), bs_get(Xd, ctx->op), PLM_ADR); -} -static INLINE void P(adrplabel_label_unk_Xd_1_ADRP)(tdis_ctx ctx, struct bitslice Xd, struct bitslice label) { - return P(pcrel)(ctx, ctx->pc + (sext(bs_get(label, ctx->op), 22) << 12), bs_get(Xd, ctx->op), PLM_ADR); -} -static INLINE void P(am_b_target_addr_B_1_B)(tdis_ctx ctx, struct bitslice addr) { - return P(branch)(ctx, ctx->pc + sext(bs_get(addr, ctx->op), 26) * 4, /*cond*/ false); -} -static INLINE void P(am_bl_target_addr_1_BL)(tdis_ctx ctx, struct bitslice addr) { - return P(branch)(ctx, ctx->pc + sext(bs_get(addr, ctx->op), 26) * 4, /*cond*/ false); -} -static INLINE void P(am_brcond_target_B_5_Bcc)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx, ctx->pc + sext(bs_get(target, ctx->op), 19) * 4, /*cond*/ true); -} -static INLINE void P(am_ldrlit_label_unk_Rt_6_LDRDl)(tdis_ctx ctx, struct bitslice Rt, struct bitslice label) { - enum pcrel_load_mode mode; - if ((ctx->op >> 26) & 1) { - switch (ctx->op >> 30) { - case 0: mode = PLM_U32_SIMD; break; - case 1: mode = PLM_U64_SIMD; break; - case 2: mode = PLM_U128_SIMD; break; - default: __builtin_abort(); - } - } else { - switch (ctx->op >> 30) { - case 0: mode = PLM_U32; break; - case 1: mode = PLM_U64; break; - case 2: mode = PLM_S32; break; - default: __builtin_abort(); - } - } - return P(pcrel)(ctx, ctx->pc + sext(bs_get(label, ctx->op), 19) * 4, bs_get(Rt, ctx->op), mode); -} -static INLINE void P(am_tbrcond_target_B_4_TBNZW)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx, ctx->pc + sext(bs_get(target, ctx->op), 14) * 4, /*cond*/ true); -} -static INLINE void P(GPR64_Rn_1_RET)(tdis_ctx ctx, UNUSED struct bitslice Rn) { - return P(ret)(ctx); -} - -static INLINE void P(dis)(tdis_ctx ctx) { - uint32_t op = ctx->op = *(uint32_t *) ctx->ptr; - ctx->op_size = 4; - /* clang doesn't realize that this is unreachable and generates code like - * "and ecx, 0x1f; cmp ecx, 0x1f; ja abort". Yeah, nice job there. */ - #include "../generated/generic-dis-arm64.inc.h" - __builtin_abort(); -} diff --git a/lib/dis-thumb.inc.h b/lib/dis-thumb.inc.h deleted file mode 100644 index 4e6d106..0000000 --- a/lib/dis-thumb.inc.h +++ /dev/null @@ -1,79 +0,0 @@ -#include "dis.h" -static INLINE void P(GPR_Rm_unk_Rdn_1_tADDhirr)(tdis_ctx ctx, struct bitslice Rdn, struct bitslice Rm) { - data(rout(Rdn), r(Rm), r(Rdn)); /* yes, twice */ -} -static INLINE void P(unk_Rdn_1_tADDrSP)(tdis_ctx ctx, UNUSED struct bitslice Rdn) { - /* this doesn't support constants, and nobody's going to add pc, sp, so... */ - return P(unidentified)(ctx); -} -static INLINE void P(GPR_Rm_1_tADDspr)(tdis_ctx ctx, UNUSED struct bitslice Rm) { - /* ditto */ - return P(unidentified)(ctx); -} -static INLINE void P(GPR_Rm_B_1_tBX)(tdis_ctx ctx, struct bitslice Rm) { - unsigned val = bs_get(Rm, ctx->op); - if (val == 15) /* bx pc */ - return P(bad)(ctx); - else if (val == 14) /* bx lr */ - return P(ret)(ctx); - return P(unidentified)(ctx); -} -static INLINE void P(GPR_Rm_unk_Rd_1_tMOVr)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rm) { - unsigned Rd_val = bs_get(Rd, ctx->op); - unsigned Rm_val = bs_get(Rm, ctx->op); - /* does anyone do this? */ - if (Rd_val == 15) - return P(bad)(ctx); - if (Rm_val == 15) - return P(pcrel)(ctx, ctx->pc + 4, Rd_val, PLM_ADR); - return P(unidentified)(ctx); -} -static INLINE void P(tGPR_Rn_reglist_regs_1_tLDMIA)(tdis_ctx ctx, UNUSED struct bitslice regs, UNUSED struct bitslice Rn) { - return P(unidentified)(ctx); -} -static INLINE void 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 void P(reglist_regs_1_tPOP)(tdis_ctx ctx, struct bitslice regs) { - unsigned regs_val = bs_get(regs, ctx->op); - if(regs_val & (1 << 15)) - return P(ret)(ctx); - return P(unidentified)(ctx); -} -static INLINE void P(reglist_regs_S_1_tPUSH)(tdis_ctx ctx, UNUSED struct bitslice regs) { - return P(unidentified)(ctx); -} -static INLINE void 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), PLM_U32); -} -static INLINE void 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), PLM_ADR); -} -static INLINE void P(t_bcctarget_target_B_1_tBcc)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 8), /*cond*/ true); -} -static INLINE void P(t_brtarget_target_B_1_tB)(tdis_ctx ctx, struct bitslice target) { - bool cond = ctx->arch.thumb_it_length > 0; - return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 11), cond); -} -static INLINE void P(t_cbtarget_target_B_2_tCBNZ)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx, ctx->pc + 4 + 2 * bs_get(target, ctx->op), /*cond*/ true); -} -static INLINE void P(it_pred_cc_it_mask_mask_1_t2IT)(tdis_ctx ctx, struct bitslice mask, UNUSED struct bitslice cc) { - /* why */ - unsigned mask_val = bs_get(mask, ctx->op); - unsigned length = __builtin_ctz(mask_val); - if (length >= 3) - return P(unidentified)(ctx); /* nop */ - ctx->arch.thumb_it_length = length; - return P(unidentified)(ctx); -} - -static INLINE void P(dis_thumb)(tdis_ctx ctx) { - uint16_t op = ctx->op = *(uint16_t *) ctx->ptr; - ctx->op_size = 2; - if (ctx->arch.thumb_it_length) - ctx->arch.thumb_it_length--; - #include "../generated/generic-dis-thumb.inc.h" - __builtin_abort(); -} diff --git a/lib/dis-thumb2.inc.h b/lib/dis-thumb2.inc.h deleted file mode 100644 index a9d7f9d..0000000 --- a/lib/dis-thumb2.inc.h +++ /dev/null @@ -1,185 +0,0 @@ -#include "dis.h" - -static inline unsigned flip16(unsigned op) { - return op >> 16 | op << 16; -} - -static inline enum pcrel_load_mode get_thumb2_load_mode(unsigned op) { - op = flip16(op); - bool sign = (op >> 8) & 1; - switch ((op >> 5) & 3) { - case 0: return sign ? PLM_S8 : PLM_U8; - case 1: return sign ? PLM_S16 : PLM_U16; - case 2: return PLM_U32; - default: __builtin_abort(); - } -} - -static INLINE void P(GPR_Rm_unk_Rd_1_t2MOVr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) { - data(rout(Rd), r(Rm)); -} -static INLINE void 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 void 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 void P(GPR_Rn_unk_Rd_1_t2ADDri12)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rn) { - data(rout(Rd), r(Rn)); -} -static INLINE void P(GPR_Rt_8_VMOVDRR)(tdis_ctx ctx, UNUSED struct bitslice Rt) { - return P(unidentified)(ctx); /* don't care */ -} -static INLINE void P(GPR_Rt_t2addrmode_imm12_addr_S_1_t2STRi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rs(addr, 13, 4), r(Rt)); -} -static INLINE void P(GPR_Rt_t2addrmode_negimm8_addr_S_1_t2STRi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rs(addr, 9, 4), r(Rt)); -} -static INLINE void P(GPR_Rt_t2addrmode_so_reg_addr_S_1_t2STRs)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rs(addr, 6, 4), rs(addr, 2, 4), r(Rt)); -} -static INLINE void P(GPRnopc_Rn_rGPR_Rm_unk_Rd_1_t2ADDrr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd, struct bitslice Rn) { - data(rout(Rd), r(Rm), r(Rn)); -} -static INLINE void P(GPRnopc_Rn_unk_Rd_2_t2ADDri)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rn) { - data(rout(Rd), r(Rn)); -} -static INLINE void 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 void 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 void 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 void 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), r(Rt), r(addr)); -} -static INLINE void P(addr_offset_none_addr_4_t2LDC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { - data(r(addr)); -} -static INLINE void P(addr_offset_none_addr_S_4_t2STC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) { - data(r(addr)); -} -static INLINE void P(addr_offset_none_addr_postidx_imm8s4_offset_4_t2LDC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) { - data(r(addr)); -} -static INLINE void P(addr_offset_none_addr_postidx_imm8s4_offset_S_4_t2STC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) { - data(r(addr)); -} -static INLINE void P(addr_offset_none_addr_unk_Rt_11_t2LDA)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) { - data(rout(Rt), r(addr)); -} -static INLINE void P(addrmode5_addr_8_VLDRD)(tdis_ctx ctx, struct bitslice addr) { - data(rs(addr, 9, 4)); -} -static INLINE void P(addrmode5_addr_S_4_t2STC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) { - data(rs(addr, 9, 4)); -} -static INLINE void P(addrmode5_pre_addr_4_t2LDC2L_PRE)(tdis_ctx ctx, struct bitslice addr) { - data(rs(addr, 9, 4)); -} -static INLINE void P(addrmode5_pre_addr_S_4_t2STC2L_PRE)(tdis_ctx ctx, struct bitslice addr) { - data(rs(addr, 9, 4)); -} -static INLINE void P(brtarget_target_B_1_t2Bcc)(tdis_ctx ctx, struct bitslice target) { - return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 20), /*cond*/ true); -} -static INLINE void 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 void 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)); -} -static INLINE void P(rGPR_Rt_t2addrmode_imm8_pre_addr_S_2_t2STRB_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(r(Rt), rs(addr, 9, 4)); -} -static INLINE void P(rGPR_Rt_t2addrmode_imm8s4_addr_S_1_t2STRDi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data_flags(IS_LDRD_STRD, r(Rt), rs(addr, 9, 4)); -} -static INLINE void P(rGPR_Rt_t2addrmode_imm8s4_pre_addr_S_1_t2STRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data_flags(IS_LDRD_STRD, r(Rt), rs(addr, 9, 4)); -} -static INLINE void P(rGPR_Rt_t2addrmode_negimm8_addr_S_2_t2STRBi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(r(Rt), rs(addr, 9, 4)); -} -static INLINE void 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 void 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 void P(t2addrmode_imm0_1020s4_addr_unk_Rt_1_t2LDREX)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rout(Rt), rs(addr, 8, 4)); -} -static INLINE void P(t2addrmode_imm12_addr_unk_Rt_5_t2LDRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rout(Rt), rs(addr, 13, 4)); -} -static INLINE void P(t2addrmode_imm8_addr_unk_Rt_S_3_t2STRBT)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(r(Rt), rs(addr, 9, 4)); -} -static INLINE void P(t2addrmode_imm8_pre_addr_unk_Rt_5_t2LDRB_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rout(Rt), rs(addr, 9, 4)); -} -static INLINE void 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 void P(t2addrmode_imm8s4_addr_unk_Rt_1_t2LDRDi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data_flags(IS_LDRD_STRD, rout(Rt), rs(addr, 9, 4)); -} -static INLINE void P(t2addrmode_imm8s4_pre_addr_unk_Rt_1_t2LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data_flags(IS_LDRD_STRD, rout(Rt), rs(addr, 9, 4)); -} -static INLINE void P(t2addrmode_negimm8_addr_unk_Rt_5_t2LDRBi8)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rout(Rt), rs(addr, 9, 4)); -} -static INLINE void P(t2addrmode_posimm8_addr_unk_Rt_5_t2LDRBT)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - data(rout(Rt), rs(addr, 9, 4)); -} -static INLINE void 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 void 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), PLM_ADR); -} -static INLINE void 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(Rt, ctx->op), get_thumb2_load_mode(ctx->op)); -} -static INLINE void P(uncondbrtarget_target_B_1_t2B)(tdis_ctx ctx, struct bitslice target) { - bool cond = ctx->arch.thumb_it_length > 0; - return P(branch)(ctx, ctx->pc + 4 + 2 * sext(bs_get(target, ctx->op), 24), cond); -} -static INLINE void P(unk_Rd_3_t2MOVTi16)(tdis_ctx ctx, struct bitslice Rd) { - data(rout(Rd)); -} -static INLINE void 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 void P(unk_Rt_13_VMOVRRD)(tdis_ctx ctx, UNUSED struct bitslice Rt) { - return P(unidentified)(ctx); -} - -static INLINE void do_it(tdis_ctx ctx) { - uint32_t op = ctx->op; - #include "../generated/generic-dis-thumb2.inc.h" - __builtin_abort(); -} - -static INLINE void P(dis_thumb2)(tdis_ctx ctx) { - ctx->op = *(uint32_t *) ctx->ptr; - ctx->op_size = 4; - if (ctx->arch.thumb_it_length) - ctx->arch.thumb_it_length--; - /* LLVM likes to think about Thumb2 instructions the way the ARM manual - * does - 15..0 15..0 rather than 31..0 as actually laid out in memory... */ - ctx->op = flip16(ctx->op); - do_it(ctx); - TDIS_CTX_SET_NEWOP(ctx, flip16(TDIS_CTX_NEWOP(ctx))); - ctx->op = flip16(ctx->op); -} diff --git a/lib/dis.h b/lib/dis.h index 7a53f27..675cf17 100644 --- a/lib/dis.h +++ b/lib/dis.h @@ -107,22 +107,6 @@ static const unsigned null_op = -0x100; return; \ } while (0) -#if defined(TARGET_x86_64) - #define MIN_INSN_SIZE 1 - #error "no x86 dis yet" - struct arch_dis_ctx {}; -#elif defined(TARGET_i386) - #define MIN_INSN_SIZE 1 - #error "no x86 dis yet" - struct arch_dis_ctx {}; -#elif defined(TARGET_arm) - #define MIN_INSN_SIZE 2 - #define TARGET_DIS_HEADER "dis-arm-multi.inc.h" - struct arch_dis_ctx { unsigned thumb_it_length; }; - enum { IS_LDRD_STRD = 1 << 16 }; -#elif defined(TARGET_arm64) - #define MIN_INSN_SIZE 4 - #define TARGET_DIS_HEADER "dis-arm64.inc.h" - struct arch_dis_ctx {}; +#ifndef TARGET_DIS_SUPPORTED + #error "no disassembler for the target architecture yet" #endif - diff --git a/lib/jump-dis.c b/lib/jump-dis.c index 355ac0f..2a4f94c 100644 --- a/lib/jump-dis.c +++ b/lib/jump-dis.c @@ -1,5 +1,5 @@ #include "substitute-internal.h" -#ifdef TARGET_SUPPORTED +#ifdef TARGET_DIS_SUPPORTED #include "dis.h" #include #include @@ -163,4 +163,4 @@ fail: } #include TARGET_DIS_HEADER -#endif /* TARGET_SUPPORTED */ +#endif /* TARGET_DIS_SUPPORTED */ diff --git a/lib/substitute-internal.h b/lib/substitute-internal.h index e89715f..0f3e0ad 100644 --- a/lib/substitute-internal.h +++ b/lib/substitute-internal.h @@ -32,23 +32,24 @@ typedef struct section section_x; #define TARGET_x86_64 #elif defined(FORCE_TARGET_i386) #define TARGET_i386 - #define TARGET_UNSUPPORTED #elif defined(FORCE_TARGET_arm) #define TARGET_arm - #define TARGET_SUPPORTED #elif defined(FORCE_TARGET_arm64) #define TARGET_arm64 - #define TARGET_SUPPORTED #elif defined(__x86_64__) #define TARGET_x86_64 #elif defined(__i386__) #define TARGET_i386 #elif defined(__arm__) #define TARGET_arm - #define TARGET_SUPPORTED #elif defined(__arm64__) #define TARGET_arm64 - #define TARGET_SUPPORTED #else #error target? #endif + +#if defined(TARGET_arm) + #include "arm/misc.h" +#elif defined(TARGET_arm64) + #include "arm64/misc.h" +#endif diff --git a/lib/transform-dis-arm-multi.inc.h b/lib/transform-dis-arm-multi.inc.h deleted file mode 100644 index 662b501..0000000 --- a/lib/transform-dis-arm-multi.inc.h +++ /dev/null @@ -1,189 +0,0 @@ -static inline void PUSHone(struct transform_dis_ctx *ctx, int Rt) { - if (ctx->pc_low_bit) - op32(ctx, 0x0d04f84d | Rt << 28); - else - op32(ctx, 0xe52d0004 | Rt << 12); -} - -static inline void POPone(struct transform_dis_ctx *ctx, int Rt) { - if (ctx->pc_low_bit) - op32(ctx, 0x0b04f85d | Rt << 28); - else - op32(ctx, 0xe49d0004 | Rt << 12); -} - -static inline void POPmulti(struct transform_dis_ctx *ctx, uint16_t mask) { - if (ctx->pc_low_bit) - op32(ctx, 0x0000e8bd | mask << 16); - else - op32(ctx, 0xe8bd0000 | mask); -} - -static inline void MOVW_MOVT(struct transform_dis_ctx *ctx, int Rd, uint32_t val) { - uint16_t hi = val >> 16, lo = (uint16_t) val; - if (ctx->pc_low_bit) { - op32(ctx, 0x0000f240 | Rd << 24 | lo >> 12 | (lo >> 11 & 1) << 10 | - (lo >> 8 & 7) << 28 | (lo & 0xff) << 16); - op32(ctx, 0x0000f2c0 | Rd << 24 | hi >> 12 | (hi >> 11 & 1) << 10 | - (hi >> 8 & 7) << 28 | (hi & 0xff) << 16); - - } else { - op32(ctx, 0xe3000000 | Rd << 12 | (lo >> 12) << 16 | (lo & 0xfff)); - op32(ctx, 0xe3400000 | Rd << 12 | (hi >> 12) << 16 | (hi & 0xfff)); - } - -} - -static inline void STRri(struct transform_dis_ctx *ctx, int Rt, int Rn, uint32_t off) { - if (ctx->pc_low_bit) - op32(ctx, 0x0000f8c0 | Rn | Rt << 28 | off << 16); - else - op32(ctx, 0xe4800000 | Rn << 16 | Rt << 12 | off); -} - -static inline void LDRxi(struct transform_dis_ctx *ctx, int Rt, int Rn, uint32_t off, - enum pcrel_load_mode load_mode) { - if (ctx->pc_low_bit) { - int subop, sign; - switch (load_mode) { - case PLM_U8: subop = 0; sign = 0; break; - case PLM_S8: subop = 0; sign = 1; break; - case PLM_U16: subop = 1; sign = 0; break; - case PLM_S16: subop = 1; sign = 1; break; - case PLM_U32: subop = 2; sign = 0; break; - default: __builtin_abort(); - } - op32(ctx, 0x0000f890 | Rn | Rt << 28 | subop << 5 | sign << 8 | off << 16); - } else { - int is_byte, subop, not_ldrd; - switch (load_mode) { - case PLM_U8: is_byte = 1; goto type1; - case PLM_S8: subop = 13; not_ldrd = 1; goto type2; - case PLM_U16: subop = 11; not_ldrd = 1; goto type2; - case PLM_S16: subop = 15; not_ldrd = 1; goto type2; - case PLM_U32: is_byte = 0; goto type1; - case PLM_U128: subop = 13; not_ldrd = 0; goto type2; - type1: - op32(ctx, 0xe5900000 | Rn << 16 | Rt << 12 | off); - break; - type2: - op32(ctx, 0xe1c00000 | Rn << 16 | Rt << 12 | subop << 4 | - (off & 0xf) | (off & 0xf0) << 4 | not_ldrd << 20); - break; - default: - __builtin_abort(); - } - } -} - -static NOINLINE UNUSED void transform_dis_data(struct transform_dis_ctx *ctx, - unsigned o0, unsigned o1, unsigned o2, unsigned o3, unsigned out_mask) { -#ifdef TRANSFORM_DIS_VERBOSE - printf("transform_dis_data: (%p) %x %x %x %x out_mask=%x\n", (void *) ctx->pc, - o0, o1, o2, o3, out_mask); -#endif - /* We only care if at least one op is PC, so quickly test that. */ - if (((o0 | o1 | o2 | o3) & 15) != 15) - return; - unsigned *newval = ctx->newval; - newval[0] = o0; - newval[1] = o1; - newval[2] = o2; - newval[3] = o3; - - void **rpp = ctx->rewritten_ptr_ptr; - - /* A few cases: - * 1. Move to PC that does not read PC. Probably fine. - * 2. Move to PC that does read PC, e.g. 'ldrls pc, [pc, r0, lsl #2]'. - * This is different from #4 mainly in that we can't need to do - * something like pop {temp, pc}. Not terribly plausible (only likely - * in non-position-independent code in ARM mode, and I can't get it to - * happen in the first 8 bytes then), but we may as well handle it. - * 3. Read of PC that does not read the register(s) it writes, e.g. adr r3, - * X. In this case we can use that register as a temporary. - * 4. Read of PC that does, or doesn't have any output register, e.g. add - * r3, pc. In this case we use the stack because reliably finding a - * free register would be work, and might not even be possible (thumb - * mov r9, r0; mov r12, r1; ) - * the out register is always first. - */ - uint16_t in_regs = 0; - int out_reg = -1; - for (int i = 0; i < 4; i++) { - if (out_mask & 1 << i) - out_reg = newval[i]; - else if (newval[i] != null_op) - in_regs |= 1 << newval[i]; - } - if (out_mask & IS_LDRD_STRD) - in_regs |= 1 << (newval[0] + 1); - uint32_t pc = ctx->pc + (ctx->pc_low_bit ? 4 : 8); - int scratch = __builtin_ctz(~(in_regs | (1 << out_reg))); - -#ifdef TRANSFORM_DIS_VERBOSE - printf("transform_dis_data: in_regs=%x out_reg=%d pc=%x scratch=%d\n", - in_regs, out_reg, pc, scratch); -#endif - - if (out_reg == 15) { - if (in_regs & 1 << 15) - return; /* case 1 */ - /* case 2 */ - PUSHone(ctx, scratch); - PUSHone(ctx, scratch); - MOVW_MOVT(ctx, scratch, pc); - for (int i = 0; i < 4; i++) - if (newval[i] == 15) - newval[i] = scratch; - ctx->write_newop_here = *rpp; *rpp += ctx->op_size; - STRri(ctx, scratch, 13, 4); - POPmulti(ctx, 1 << scratch | 1 << 15); - } else { - if (out_reg != -1 && !(in_regs & 1 << out_reg)) { - /* case 3 - ignore scratch */ - MOVW_MOVT(ctx, out_reg, pc); - for (int i = 0; i < 4; i++) - if (newval[i] == 15) - newval[i] = out_reg; - ctx->write_newop_here = *rpp; *rpp += ctx->op_size; - } else { - /* case 4 */ - PUSHone(ctx, scratch); - MOVW_MOVT(ctx, scratch, pc); - for (int i = 0; i < 4; i++) - if (newval[i] == 15) - newval[i] = scratch; - ctx->write_newop_here = *rpp; *rpp += ctx->op_size; - POPone(ctx, scratch); - } - } - ctx->modify = true; -#ifdef TRANSFORM_DIS_VERBOSE - printf("transform_dis_data: => %x %x %x %x\n", - newval[0], newval[1], newval[2], newval[3]); -#endif -} - -static NOINLINE UNUSED void transform_dis_pcrel(struct transform_dis_ctx *ctx, - uintptr_t dpc, unsigned reg, enum pcrel_load_mode load_mode) { -#ifdef TRANSFORM_DIS_VERBOSE - printf("transform_dis_pcrel: (%p) dpc=%p reg=%x mode=%d\n", (void *) ctx->pc, - (void *) dpc, reg, load_mode); -#endif - ctx->write_newop_here = NULL; - if (reg == 15) { - int scratch = 0; - PUSHone(ctx, scratch); - PUSHone(ctx, scratch); - MOVW_MOVT(ctx, scratch, dpc); - if (load_mode != PLM_ADR) - LDRxi(ctx, scratch, scratch, 0, load_mode); - STRri(ctx, scratch, 13, 4); - POPmulti(ctx, 1 << scratch | 1 << 15); - } else { - MOVW_MOVT(ctx, reg, dpc); - if (load_mode != PLM_ADR) - LDRxi(ctx, reg, reg, 0, load_mode); - } -} diff --git a/lib/transform-dis-arm64.inc.h b/lib/transform-dis-arm64.inc.h deleted file mode 100644 index c47971b..0000000 --- a/lib/transform-dis-arm64.inc.h +++ /dev/null @@ -1,52 +0,0 @@ -static inline void MOVi64(struct transform_dis_ctx *ctx, int Rd, uint64_t val) { - int shift_nybbles = 0; - do { - int k = shift_nybbles != 0 ? 1 : 0; - op32(ctx, 0x69400000 | k << 28 | Rd | (val & 0xffff) << 4 | shift_nybbles << 20); - shift_nybbles++; - val >>= 16; - } while(val); -} - -static inline void LDRxi(struct transform_dis_ctx *ctx, int Rt, int Rn, uint32_t off, - bool regsize_64, enum pcrel_load_mode load_mode) { - int size, opc; - bool sign, simd; - switch (load_mode) { - case PLM_U8: size = 0; sign = false; simd = false; break; - case PLM_S8: size = 0; sign = true; simd = false; break; - case PLM_U16: size = 1; sign = false; simd = false; break; - case PLM_S16: size = 1; sign = true; simd = false; break; - case PLM_U32: size = 2; sign = false; simd = false; break; - case PLM_S32: size = 2; sign = true; simd = false; break; - case PLM_U64: size = 3; sign = false; simd = false; break; - case PLM_U32_SIMD: size = 2; opc = 1; simd = true; break; - case PLM_U64_SIMD: size = 3; opc = 1; simd = true; break; - case PLM_U128_SIMD: size = 0; opc = 3; simd = true; break; - default: __builtin_abort(); - } - if (simd) { - off /= 1 << (size | (opc & 1) << 2); - } else { - off /= 1 << size; - opc = sign ? (regsize_64 ? 2 : 3) : 1; - } - op32(ctx, 0x39000000 | Rt | Rn << 5 | off << 10 | opc << 22 | simd << 26 | size << 30); -} - - -static NOINLINE UNUSED void transform_dis_pcrel(struct transform_dis_ctx *ctx, - uintptr_t dpc, unsigned reg, enum pcrel_load_mode load_mode) { - ctx->write_newop_here = NULL; - if (load_mode >= PLM_U32_SIMD) { - /* use x0 as scratch */ - op32(ctx, 0xf81f0fe0); /* str x0, [sp, #-0x10]! */ - MOVi64(ctx, 0, dpc); - LDRxi(ctx, reg, 0, 0, true, load_mode); - op32(ctx, 0xf84107e0); /* ldr x0, [sp], #0x10 */ - } else { - MOVi64(ctx, reg, dpc); - LDRxi(ctx, reg, reg, 0, true, load_mode); - } -} - diff --git a/lib/transform-dis.c b/lib/transform-dis.c index 6d5fc66..6067ea0 100644 --- a/lib/transform-dis.c +++ b/lib/transform-dis.c @@ -1,5 +1,5 @@ #include "substitute-internal.h" -#ifdef TARGET_SUPPORTED +#ifdef TARGET_DIS_SUPPORTED #include "substitute.h" #include "dis.h" @@ -123,11 +123,7 @@ static inline void op32(struct transform_dis_ctx *ctx, uint32_t op) { *rpp += 4; } -#ifdef TARGET_arm - #include "transform-dis-arm-multi.inc.h" -#else - #include "transform-dis-arm64.inc.h" -#endif +#include TARGET_TRANSFORM_DIS_HEADER #include TARGET_DIS_HEADER -#endif /* TARGET_SUPPORTED */ +#endif /* TARGET_DIS_SUPPORTED */ -- cgit v1.2.3