diff options
author | comex | 2015-01-12 01:21:43 -0500 |
---|---|---|
committer | comex | 2015-01-12 01:22:20 -0500 |
commit | 21277f290a705c6382453f348e4a224cd7695320 (patch) | |
tree | b3eac4e66f7aec0dede880e7405171c413497a68 /lib | |
parent | minor improvements (diff) | |
download | substitute-21277f290a705c6382453f348e4a224cd7695320.tar.gz |
these functions are actually pointless, since it's easier to change the *offset* for actual pc-rel loads (and the rest are accounted for, albeit implicitly in the case of add pc)
i might end up using the stack :/
Diffstat (limited to 'lib')
-rw-r--r-- | lib/dis-arm.inc.h | 42 | ||||
-rw-r--r-- | lib/dis-arm64.inc.h | 28 | ||||
-rw-r--r-- | lib/dis-thumb.inc.h | 13 | ||||
-rw-r--r-- | lib/dis-thumb2.inc.h | 17 | ||||
-rw-r--r-- | lib/dis.h | 12 | ||||
-rw-r--r-- | lib/transform-dis.inc.h | 55 |
6 files changed, 137 insertions, 30 deletions
diff --git a/lib/dis-arm.inc.h b/lib/dis-arm.inc.h index 6ed4bc6..88fefb8 100644 --- a/lib/dis-arm.inc.h +++ b/lib/dis-arm.inc.h @@ -1,5 +1,37 @@ #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 +*/ + +static inline enum pcrel_load_mode get_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 tdis_ret 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)); } @@ -117,7 +149,7 @@ static INLINE tdis_ret P(addrmode_imm12_pre_addr_unk_Rt_2_LDRB_PRE_IMM)(tdis_ctx data(rs(addr, 13, 4), rout(Rt)); } static INLINE tdis_ret 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), false); + return P(pcrel)(ctx, ctx->pc + 8 + bs_get(label, ctx->op), bs_get(Rd, ctx->op), PLM_ADR); } static INLINE tdis_ret P(br_target_target_B_1_Bcc)(tdis_ctx ctx, struct bitslice target) { return P(branch)(ctx, ctx->pc + 8 + sext(bs_get(target, ctx->op), 24)); @@ -156,10 +188,4 @@ static INLINE tdis_ret P(GPR_Rt_addr_offset_none_addr_postidx_reg_Rm_S_1_STRHTr) data(r(addr), r(Rt), r(Rm)); } -static tdis_ret P(dis_arm)(tdis_ctx ctx) { - unsigned op = ctx->op; - #include "../generated/transform-dis-arm.inc.h" - /* clang doesn't realize that this is unreachable * and generates code like - * "and ecx, 0x1f; cmp ecx, 0x1f; ja abort". Yeah, nice job there. */ - __builtin_abort(); -} +#define GENERATED_HEADER "../generated/transform-dis-arm.inc.h" diff --git a/lib/dis-arm64.inc.h b/lib/dis-arm64.inc.h index 19f986a..333ecb1 100644 --- a/lib/dis-arm64.inc.h +++ b/lib/dis-arm64.inc.h @@ -1,8 +1,8 @@ static INLINE tdis_ret 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), false); + return P(pcrel)(ctx, ctx->pc + sext(bs_get(label, ctx->op), 22), bs_get(Xd, ctx->op), PLM_ADR); } static INLINE tdis_ret 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), false); + return P(pcrel)(ctx, ctx->pc + (sext(bs_get(label, ctx->op), 22) << 12), bs_get(Xd, ctx->op), PLM_ADR); } static INLINE tdis_ret 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); @@ -14,7 +14,23 @@ static INLINE tdis_ret P(am_brcond_target_B_5_Bcc)(tdis_ctx ctx, struct bitslice return P(branch)(ctx, ctx->pc + sext(bs_get(target, ctx->op), 19) * 4); } static INLINE tdis_ret P(am_ldrlit_label_unk_Rt_6_LDRDl)(tdis_ctx ctx, struct bitslice Rt, struct bitslice label) { - return P(pcrel)(ctx, ctx->pc + sext(bs_get(label, ctx->op), 19) * 4, bs_get(Rt, ctx->op), true); + enum pcrel_load_mode mode; + if ((ctx->op >> 26) & 1) { + switch (ctx->op >> 30) { + case 0b00: mode = PLM_U32_SIMD; break; + case 0b01: mode = PLM_U64_SIMD; break; + case 0b10: mode = PLM_U128_SIMD; break; + default: __builtin_abort(); + } + } else { + switch (ctx->op >> 30) { + case 0b00: mode = PLM_U32; break; + case 0b01: mode = PLM_U64; break; + case 0b10: 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 tdis_ret 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); @@ -23,8 +39,4 @@ static INLINE tdis_ret P(GPR64_Rn_1_RET)(tdis_ctx ctx, UNUSED struct bitslice Rn return P(ret)(ctx); } -static tdis_ret P(dis_arm64)(tdis_ctx ctx) { - unsigned op = ctx->op; - #include "../generated/transform-dis-arm64.inc.h" - __builtin_abort(); -} +#define GENERATED_HEADER "../generated/transform-dis-arm64.inc.h" diff --git a/lib/dis-thumb.inc.h b/lib/dis-thumb.inc.h index eb1aae4..56b1129 100644 --- a/lib/dis-thumb.inc.h +++ b/lib/dis-thumb.inc.h @@ -17,7 +17,7 @@ static INLINE tdis_ret P(GPR_Rm_unk_Rd_1_tMOVr)(tdis_ctx ctx, struct bitslice Rd if (Rd_val == 15) return P(bad)(ctx); if (Rm_val == 15) - return P(pcrel)(ctx, ctx->pc + 4, Rd_val, false); + return P(pcrel)(ctx, ctx->pc + 4, Rd_val, PLM_ADR); return P(unidentified)(ctx); } static INLINE tdis_ret P(tGPR_Rn_reglist_regs_1_tLDMIA)(tdis_ctx ctx, UNUSED struct bitslice regs, UNUSED struct bitslice Rn) { @@ -27,10 +27,10 @@ static INLINE tdis_ret P(tGPR_Rn_reglist_regs_S_1_tSTMIA_UPD)(tdis_ctx ctx, UNUS return P(unidentified)(ctx); } static INLINE tdis_ret P(t_addrmode_pc_addr_unk_Rt_1_tLDRpci)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) { - return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + bs_get(addr, ctx->op), bs_get(Rt, ctx->op), true); + return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + bs_get(addr, ctx->op), bs_get(Rt, ctx->op), PLM_U32); } static INLINE tdis_ret P(t_adrlabel_addr_unk_Rd_1_tADR)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rd) { - return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + bs_get(addr, ctx->op), bs_get(Rd, ctx->op), false); + return P(pcrel)(ctx, ((ctx->pc + 4) & ~2) + bs_get(addr, ctx->op), bs_get(Rd, ctx->op), PLM_ADR); } static INLINE tdis_ret 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)); @@ -42,9 +42,4 @@ static INLINE tdis_ret P(t_cbtarget_target_B_2_tCBNZ)(tdis_ctx ctx, struct bitsl return P(branch)(ctx, ctx->pc + 4 + 2 * bs_get(target, ctx->op)); } -static tdis_ret P(dis_thumb)(tdis_ctx ctx) { - unsigned op = ctx->op; - #include "../generated/transform-dis-thumb.inc.h" - __builtin_abort(); -} - +#define GENERATED_HEADER "../generated/transform-dis-thumb.inc.h" diff --git a/lib/dis-thumb2.inc.h b/lib/dis-thumb2.inc.h index e18f268..819471d 100644 --- a/lib/dis-thumb2.inc.h +++ b/lib/dis-thumb2.inc.h @@ -1,4 +1,15 @@ #include "dis.h" + +static inline enum pcrel_load_mode get_load_mode(unsigned 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 sign ? PLM_S32 : PLM_U32; + default: __builtin_abort(); + } +} + static INLINE tdis_ret P(GPR_Rm_unk_Rd_1_t2MOVr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) { data(rout(Rd), r(Rm)); } @@ -148,8 +159,4 @@ static INLINE tdis_ret P(unk_Rt_13_VMOVRRD)(tdis_ctx ctx, UNUSED struct bitslice return P(unidentified)(ctx); } -static tdis_ret P(dis_thumb2)(tdis_ctx ctx) { - unsigned op = ctx->op; - #include "../generated/transform-dis-thumb2.inc.h" - __builtin_abort(); -} +#define GENERATED_HEADER "../generated/transform-dis-thumb2.inc.h" @@ -59,6 +59,18 @@ static inline struct bitslice bs_slice_(struct bitslice bs, struct bitslice_run #define bs_slice(bs, lo, size) \ bs_slice_(bs, alloca((bs).nruns * sizeof(struct bitslice_run)), lo, size) +enum pcrel_load_mode { + PLM_ADR, /* just want the address */ + PLM_U8, PLM_S8, + PLM_U16, PLM_S16, + PLM_U32, PLM_S32, + PLM_U64, + PLM_U128, + PLM_U32_SIMD, + PLM_U64_SIMD, + PLM_U128_SIMD, +}; + static const struct bitslice nullbs = { 0, NULL }; #define r(nn) nn, false, true #define rs(nn, l, s) bs_slice(nn, l, s), false, true diff --git a/lib/transform-dis.inc.h b/lib/transform-dis.inc.h new file mode 100644 index 0000000..6889127 --- /dev/null +++ b/lib/transform-dis.inc.h @@ -0,0 +1,55 @@ +typedef struct { + bool modify; +} tdis_ret; +typedef struct tc { + uintptr_t pc; + int op_size; + uint32_t op; + uint32_t newop; + uint32_t newval[4]; + uintptr_t pc_patch_start; + uintptr_t pc_patch_end; + bool got_bad; + +} *tdis_ctx; + +NOINLINE UNUSED +static tdis_ret P_data(struct tc *ctx, unsigned o0, unsigned o1, unsigned o2, unsigned o3, unsigned out_mask) { + __builtin_abort(); +} + +NOINLINE UNUSED +static tdis_ret P_pcrel(struct tc *ctx, uintptr_t dpc, unsigned reg, bool is_load) { + __builtin_abort(); +} + +NOINLINE UNUSED +static tdis_ret P_ret(struct tc *ctx) { + /* ret is okay if it's at the end of the patch */ + if (ctx->pc + op_size < ctx->pc_patch_end) + ctx->got_bad = true; + printf("ret: %08x\n", ctx->op); + return (tdis_ret) {false}; +} + +NOINLINE UNUSED +static tdis_ret P_branch(struct tc *ctx, uintptr_t dpc) { + if (dpc >= ctx->pc_patch_start && dpc < ctx->pc_patch_end) { + /* don't support this for now */ + ctx->got_bad = true; + } + return (tdis_ret) {false}; +} + +NOINLINE UNUSED +static tdis_ret P_unidentified(struct tc *ctx) { + return (tdis_ret) {false}; +} + +NOINLINE UNUSED +static tdis_ret P_bad(struct tc *ctx) { + ctx->got_bad = true; + return (tdis_ret) {false}; +} + +#define P(x) transform_dis_##x |