1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#define IF_BOTHER_WITH_MODIFY(...) __VA_ARGS__
#include "dis.h"
typedef struct tc {
uint32_t pc;
const void *ptr;
#if defined(TARGET_x86_64) || defined(TARGET_i386)
uint8_t newop[16];
#else
uint32_t op;
uint32_t newop;
#endif
uint32_t newval[4];
bool modify;
int op_size;
struct arch_dis_ctx arch;
} *tdis_ctx;
#define P(x) P_##x
#define TDIS_CTX_MODIFY(ctx) ((ctx)->modify)
#define TDIS_CTX_NEWVAL(ctx, n) ((ctx)->newval[n])
#define TDIS_CTX_NEWOP(ctx) ((ctx)->newop)
#define TDIS_CTX_SET_NEWOP(ctx, new) ((ctx)->newop = (new))
#if defined(TARGET_x86_64) || defined(TARGET_i386)
NOINLINE UNUSED
static void P_pcrel(UNUSED struct tc *ctx, uint32_t dpc) {
printf("adr => %08x\n", dpc);
}
#else
NOINLINE UNUSED
static void P_data(UNUSED struct tc *ctx, unsigned o0, unsigned o1, unsigned o2,
unsigned o3, unsigned out_mask) {
printf("data\n", ctx->op);
unsigned os[] = {o0, o1, o2, o3};
for(size_t i = 0; i < 4; i++) {
unsigned val = os[i];
if(val == -1u)
break;
printf(" reg %x: %s\n", val, out_mask & (1 << i) ? "out" : "in");
ctx->newval[i] = i;
}
ctx->modify = true;
}
NOINLINE UNUSED
static void P_pcrel(UNUSED struct tc *ctx, uint32_t dpc,
unsigned reg, enum pcrel_load_mode lm) {
printf("adr => %08x r%u lm:%d\n", dpc, reg, lm);
}
NOINLINE UNUSED
static void P_thumb_it(UNUSED struct tc *ctx) {
printf("thumb_it\n");
}
#endif
NOINLINE UNUSED
static void P_ret(UNUSED struct tc *ctx) {
printf("ret\n");
}
NOINLINE UNUSED
static void P_branch(UNUSED struct tc *ctx, uint64_t dpc, int cc) {
printf("branch(%s,%s) => %08llx\n",
(cc & CC_CONDITIONAL) ? "cond" : "uncond",
(cc & CC_CALL) ? "call" : "!call",
(unsigned long long) dpc);
}
NOINLINE UNUSED
static void P_unidentified(UNUSED struct tc *ctx) {
printf("unidentified\n");
}
NOINLINE UNUSED
static void P_bad(UNUSED struct tc *ctx) {
printf("bad\n");
}
#include HDR
#define P_(x) P(x)
int main(UNUSED int argc, char **argv) {
struct tc ctx;
ctx.pc = 0xdead0000;
const char *op_str = argv[1];
#if defined(TARGET_x86_64) || defined(TARGET_i386)
uint8_t op[20] = {0};
if (!op_str)
op_str = "deadbeef";
size_t len = strlen(op_str);
if (len % 1 || len > 32) {
printf("bad op_str len\n");
return 1;
}
for (size_t i = 0; i < len; i += 2) {
char str[3] = {op_str[i], op_str[i+1], 0};
char *end;
uint8_t byte = strtol(str, &end, 16);
if (*end) {
printf("bad op_str byte\n");
return 1;
}
op[i/2] = byte;
}
ctx.ptr = op;
ctx.modify = false;
P_(xdis)(&ctx);
printf("(size=%d/%zd)\n", ctx.op_size, len / 2);
#else
uint32_t op = strtoll(op_str ? op_str : "deadbeef", NULL, 16);
ctx.ptr = &op;
ctx.newop = 0;
ctx.modify = false;
printf("%08x: ", op);
P_(xdis)(&ctx);
printf("==> %x (size=%d)\n", ctx.newop, ctx.op_size);
#endif
}
|