aboutsummaryrefslogtreecommitdiff
path: root/lib/arm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arm')
-rw-r--r--lib/arm/dis-arm-multi.inc.h16
-rw-r--r--lib/arm/dis-arm.inc.h214
-rw-r--r--lib/arm/dis-thumb.inc.h79
-rw-r--r--lib/arm/dis-thumb2.inc.h185
-rw-r--r--lib/arm/misc.h7
-rw-r--r--lib/arm/transform-dis-arm-multi.inc.h189
6 files changed, 690 insertions, 0 deletions
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; <some PC using insn(s)>)
+ * 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);
+ }
+}