diff options
author | comex | 2015-02-08 23:45:24 -0500 |
---|---|---|
committer | comex | 2015-02-08 23:45:24 -0500 |
commit | eb93cee2a22cde812ccd6b9bd418d36185c058f5 (patch) | |
tree | 43a22ccf021a1513dba3a9c99f7b81822fe950fa /lib/arm | |
parent | formatting (diff) | |
download | substitute-eb93cee2a22cde812ccd6b9bd418d36185c058f5.tar.gz |
Refactor disassembly so x86 works, and add x86 transform-dis.
This patch is a monolithic mess, because I was too lazy to do the
refactor first (that would require some stash fun, since I wasn't
actually sure before doing x86 transform-dis what would be needed).
Anyway, the resulting code should be cleaner - less duplication.
This breaks ARM/ARM64.
Diffstat (limited to '')
-rw-r--r-- | lib/arm/arch-dis.h | 60 | ||||
-rw-r--r-- | lib/arm/arch-transform-dis.inc.h (renamed from lib/arm/transform-dis-arm-multi.inc.h) | 0 | ||||
-rw-r--r-- | lib/arm/dis-main.inc.h (renamed from lib/arm/dis-arm-multi.inc.h) | 0 | ||||
-rw-r--r-- | lib/arm/misc.h | 58 | ||||
-rw-r--r-- | lib/arm64/arch-dis.h | 37 | ||||
-rw-r--r-- | lib/arm64/arch-transform-dis.inc.h (renamed from lib/arm64/transform-dis-arm64.inc.h) | 4 | ||||
-rw-r--r-- | lib/arm64/dis-main.inc.h (renamed from lib/arm64/dis-arm64.inc.h) | 0 | ||||
-rw-r--r-- | lib/arm64/misc.h | 35 |
8 files changed, 101 insertions, 93 deletions
diff --git a/lib/arm/arch-dis.h b/lib/arm/arch-dis.h new file mode 100644 index 0000000..c64ff2e --- /dev/null +++ b/lib/arm/arch-dis.h @@ -0,0 +1,60 @@ +#pragma once +#define MIN_INSN_SIZE 2 +/* each input instruction might turn into: + * - 2 bytes for Bcc, if in IT + * then ONE of: + * - 2/4 bytes for just the instruction + * - 2+8 bytes for branch (which in *valid* code rules out IT but whatever) + * - up to 7 4-byte insns for pcrel (if dest=pc, and while these can be subject + * to IT, there can only reasonably be two per block, and if there are both + * then that's an unconditional exit - but we don't enforce any of this + * currently) + * - up to 7 4-byte insns for similar moves to PC that fall under 'data' + * the maximum number of possible inputs is 4, plus 4 extras if the last one + * was an IT (but in that case it can't be one of the above cases) + * while this looks huge, it's overly conservative and doesn't matter much, + * since only the actually used space will be taken up in the final output + */ +#define TD_MAX_REWRITTEN_SIZE (7*4*7 + 4) /* 196 */ + +struct arch_pcrel_info { + unsigned reg; + enum pcrel_load_mode lm; +}; + +struct arch_dis_ctx { + /* thumb? */ + bool pc_low_bit; + /* if thumb, IT cond for the next 5 instructions + * (5 because we still advance after IT) */ + uint8_t it_conds[5]; + /* for transform_dis - did we add space for a Bccrel? */ + uint8_t bccrel_bits; + void *bccrel_p; +}; + +static inline void arch_dis_ctx_init(struct arch_dis_ctx *ctx) { + ctx->pc_low_bit = false; + ctx->bccrel_p = NULL; + memset(ctx->it_conds, 0xe, 5); +} + +static inline void advance_it_cond(struct arch_dis_ctx *ctx) { + ctx->it_conds[0] = ctx->it_conds[1]; + ctx->it_conds[1] = ctx->it_conds[2]; + ctx->it_conds[2] = ctx->it_conds[3]; + ctx->it_conds[3] = ctx->it_conds[4]; + ctx->it_conds[4] = 0xe; +} + +#define DFLAG_IS_LDRD_STRD (1 << 16) + +/* Types of conditionals for 'branch' */ +/* a regular old branch-with-condition */ +#define CC_ARMCC (CC_CONDITIONAL | 0x400) +/* already in an IT block - in transform_dis this will be rewritten to a branch + * anyway, so it can be treated as unconditional; in jump_dis we have to know + * to keep going */ +#define CC_ALREADY_IN_IT (CC_CONDITIONAL | 0x800) +/* CBZ/CBNZ is rewritten */ +#define CC_CBXZ (CC_CONDITIONAL | 0xc00) diff --git a/lib/arm/transform-dis-arm-multi.inc.h b/lib/arm/arch-transform-dis.inc.h index 6e91ff5..6e91ff5 100644 --- a/lib/arm/transform-dis-arm-multi.inc.h +++ b/lib/arm/arch-transform-dis.inc.h diff --git a/lib/arm/dis-arm-multi.inc.h b/lib/arm/dis-main.inc.h index bf2767e..bf2767e 100644 --- a/lib/arm/dis-arm-multi.inc.h +++ b/lib/arm/dis-main.inc.h diff --git a/lib/arm/misc.h b/lib/arm/misc.h index ef11a05..c18367d 100644 --- a/lib/arm/misc.h +++ b/lib/arm/misc.h @@ -1,59 +1,3 @@ #pragma once +#define TARGET_POINTER_SIZE 4 #define TARGET_DIS_SUPPORTED -#define TARGET_DIS_HEADER "arm/dis-arm-multi.inc.h" -#define TARGET_JUMP_PATCH_HDR "arm/jump-patch.h" -#define TARGET_TRANSFORM_DIS_HEADER "arm/transform-dis-arm-multi.inc.h" -#define MIN_INSN_SIZE 2 -/* each input instruction might turn into: - * - 2 bytes for Bcc, if in IT - * then ONE of: - * - 2/4 bytes for just the instruction - * - 2+8 bytes for branch (which in *valid* code rules out IT but whatever) - * - up to 7 4-byte insns for pcrel (if dest=pc, and while these can be subject - * to IT, there can only reasonably be two per block, and if there are both - * then that's an unconditional exit - but we don't enforce any of this - * currently) - * - up to 7 4-byte insns for similar moves to PC that fall under 'data' - * the maximum number of possible inputs is 4, plus 4 extras if the last one - * was an IT (but in that case it can't be one of the above cases) - * while this looks huge, it's overly conservative and doesn't matter much, - * since only the actually used space will be taken up in the final output - */ -#define TD_MAX_REWRITTEN_SIZE (7*4*7 + 4) /* 196 */ - -struct arch_dis_ctx { - /* thumb? */ - bool pc_low_bit; - /* if thumb, IT cond for the next 5 instructions - * (5 because we still advance after IT) */ - uint8_t it_conds[5]; - /* for transform_dis - did we add space for a Bccrel? */ - uint8_t bccrel_bits; - void *bccrel_p; -}; - -static inline void arch_dis_ctx_init(struct arch_dis_ctx *ctx) { - ctx->pc_low_bit = false; - ctx->bccrel_p = NULL; - memset(ctx->it_conds, 0xe, 5); -} - -static inline void advance_it_cond(struct arch_dis_ctx *ctx) { - ctx->it_conds[0] = ctx->it_conds[1]; - ctx->it_conds[1] = ctx->it_conds[2]; - ctx->it_conds[2] = ctx->it_conds[3]; - ctx->it_conds[3] = ctx->it_conds[4]; - ctx->it_conds[4] = 0xe; -} - -#define DFLAG_IS_LDRD_STRD (1 << 16) - -/* Types of conditionals for 'branch' */ -/* a regular old branch-with-condition */ -#define CC_ARMCC (CC_CONDITIONAL | 0x400) -/* already in an IT block - in transform_dis this will be rewritten to a branch - * anyway, so it can be treated as unconditional; in jump_dis we have to know - * to keep going */ -#define CC_ALREADY_IN_IT (CC_CONDITIONAL | 0x800) -/* CBZ/CBNZ is rewritten */ -#define CC_CBXZ (CC_CONDITIONAL | 0xc00) diff --git a/lib/arm64/arch-dis.h b/lib/arm64/arch-dis.h new file mode 100644 index 0000000..f91328b --- /dev/null +++ b/lib/arm64/arch-dis.h @@ -0,0 +1,37 @@ +#pragma once +#define MIN_INSN_SIZE 4 +#define TD_MAX_REWRITTEN_SIZE (7 * 2 * 4) /* also conservative */ + +struct arch_pcrel_info { + unsigned reg; + enum pcrel_load_mode lm; +}; + +struct arch_dis_ctx { + /* For transform_dis only - used to get temporary registers. We assume + * that we can use any caller-saved or IP register which was not written, + * so r9-r18. + * This is a massive overestimate: we just OR in each instruction's bits + * 4:0 (Rd for data, Rt for loads, most common), 14:10 (Rt2 for load-pair + * instructions), and 20:16 (Rs for store-exclusive insturctions). It + * would be easy to restrict the latter two to the few instructions that + * actually use them, but with 10 available registers, and a patch of at + * most 3 instructions (and none of the instructions that require a temp + * use Rt2/Rs or could read their Rd, so the third doesn't count), we won't + * run out even with the dumbest possible thing. */ + uint32_t regs_possibly_written; +}; + +static inline void arch_dis_ctx_init(struct arch_dis_ctx *ctx) { + ctx->regs_possibly_written = 0; +} + +static inline int arm64_get_unwritten_temp_reg(struct arch_dis_ctx *ctx) { + uint32_t avail = ~ctx->regs_possibly_written & ((1 << 19) - (1 << 9)); + if (!avail) + __builtin_abort(); + return 31 - __builtin_clz(avail); +} + +#define CC_ARMCC (CC_CONDITIONAL | 0x400) +#define CC_XBXZ (CC_CONDITIONAL | 0x800) diff --git a/lib/arm64/transform-dis-arm64.inc.h b/lib/arm64/arch-transform-dis.inc.h index 792b835..d8f831d 100644 --- a/lib/arm64/transform-dis-arm64.inc.h +++ b/lib/arm64/arch-transform-dis.inc.h @@ -1,7 +1,7 @@ #include "arm64/assemble.h" static NOINLINE UNUSED -void transform_dis_pcrel(struct transform_dis_ctx *ctx, uintptr_t dpc, unsigned reg, +void transform_dis_pcrel(struct transform_dis_ctx *ctx, uint_tptr dpc, unsigned reg, enum pcrel_load_mode load_mode) { ctx->write_newop_here = NULL; void **codep = ctx->rewritten_ptr_ptr; @@ -16,7 +16,7 @@ void transform_dis_pcrel(struct transform_dis_ctx *ctx, uintptr_t dpc, unsigned } static NOINLINE UNUSED -void transform_dis_branch(struct transform_dis_ctx *ctx, uintptr_t dpc, int cc) { +void transform_dis_branch(struct transform_dis_ctx *ctx, uint_tptr dpc, int cc) { /* TODO fix BL */ #ifdef TRANSFORM_DIS_VERBOSE printf("transform_dis (%p): branch => %p\n", (void *) ctx->pc, (void *) dpc); diff --git a/lib/arm64/dis-arm64.inc.h b/lib/arm64/dis-main.inc.h index 04349f2..04349f2 100644 --- a/lib/arm64/dis-arm64.inc.h +++ b/lib/arm64/dis-main.inc.h diff --git a/lib/arm64/misc.h b/lib/arm64/misc.h index f5a6154..066e9d5 100644 --- a/lib/arm64/misc.h +++ b/lib/arm64/misc.h @@ -1,36 +1,3 @@ #pragma once +#define TARGET_POINTER_SIZE 8 #define TARGET_DIS_SUPPORTED -#define TARGET_DIS_HEADER "arm64/dis-arm64.inc.h" -#define TARGET_JUMP_PATCH_HDR "arm64/jump-patch.h" -#define TARGET_TRANSFORM_DIS_HEADER "arm64/transform-dis-arm64.inc.h" -#define MIN_INSN_SIZE 4 -#define TD_MAX_REWRITTEN_SIZE (7 * 2 * 4) /* also conservative */ - -struct arch_dis_ctx { - /* For transform_dis only - used to get temporary registers. We assume - * that we can use any caller-saved or IP register which was not written, - * so r9-r18. - * This is a massive overestimate: we just OR in each instruction's bits - * 4:0 (Rd for data, Rt for loads, most common), 14:10 (Rt2 for load-pair - * instructions), and 20:16 (Rs for store-exclusive insturctions). It - * would be easy to restrict the latter two to the few instructions that - * actually use them, but with 10 available registers, and a patch of at - * most 3 instructions (and none of the instructions that require a temp - * use Rt2/Rs or could read their Rd, so the third doesn't count), we won't - * run out even with the dumbest possible thing. */ - uint32_t regs_possibly_written; -}; - -static inline void arch_dis_ctx_init(struct arch_dis_ctx *ctx) { - ctx->regs_possibly_written = 0; -} - -static inline int arm64_get_unwritten_temp_reg(struct arch_dis_ctx *ctx) { - uint32_t avail = ~ctx->regs_possibly_written & ((1 << 19) - (1 << 9)); - if (!avail) - __builtin_abort(); - return 31 - __builtin_clz(avail); -} - -#define CC_ARMCC (CC_CONDITIONAL | 0x400) -#define CC_XBXZ (CC_CONDITIONAL | 0x800) |