-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdisassembler_tool.cc
147 lines (136 loc) · 3.99 KB
/
disassembler_tool.cc
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// (c) Robert Muth - see LICENSE for more info
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string_view>
#include "BE/CpuX64/opcode_gen.h"
#include "BE/CpuX64/symbolic.h"
#include "Util/assert.h"
#include "Util/parse.h"
using namespace cwerg::x64;
std::string ExtractData(std::string_view line) {
std::string out;
out.reserve(line.size());
unsigned nibble;
bool have_nibble = false;
for (char c : line) {
if (cwerg::IsWhiteSpace(c)) {
if (have_nibble) {
out.push_back(nibble);
have_nibble = false;
}
} else {
const int hex_digit = cwerg::HexDigit(c);
ASSERT(hex_digit >= 0, "");
if (have_nibble) {
out.push_back((nibble << 4) | unsigned(hex_digit));
have_nibble = false;
} else {
nibble = hex_digit;
have_nibble = true;
}
}
}
if (have_nibble) {
out.push_back(nibble);
have_nibble = false;
}
return out;
}
void batch(std::string_view data, const std::string& line) {
Ins ins;
if (!Disassemble(&ins, data)) {
std::cout << "could not find opcode for: " << line << "\n";
return;
}
std::vector<std::string> ops;
std::string_view enum_name = InsSymbolize(ins, true, false, &ops);
std::cout << std::left << std::setw(30) << line << std::dec << " "
<< enum_name;
std::string_view sep = " ";
for (const std::string& op : ops) {
std::cout << sep << op;
sep = ", ";
}
std::cout << "\n";
}
void disass_short(const Ins& ins, const std::string& line) {
std::vector<std::string> ops;
std::string_view enum_name = InsSymbolize(ins, true, true, &ops);
std::cout << line << " " << enum_name;
std::string_view sep = " ";
for (const std::string& op : ops) {
std::cout << sep << op;
sep = " ";
}
std::cout << "\n";
}
void disass_long(const Ins& ins, const std::string& line) {
std::vector<std::string> ops;
std::string_view enum_name = InsSymbolize(ins, true, false, &ops);
std::cout << " " << enum_name << "\n";
for (unsigned x = 0; x < ins.opcode->num_fields; ++x) {
int64_t v = ins.operands[x];
char buffer[64];
char* s = buffer;
if (int64_t(v) < 0) {
*s++ = '-';
v = -v;
}
*s++ = '0';
*s++ = 'x';
ToHexString(v, s);
std::cout << " " << std::left << std::setw(35)
<< EnumToString(ins.opcode->fields[x]) << " " << std::setw(10)
<< ops[x] << " (" << buffer << ")\n";
}
std::cout << "\n";
}
void disass(std::string_view data, const std::string& line) {
Ins ins;
if (!Disassemble(&ins, data)) {
std::cout << "could not disassemble " << line << std::dec << "\n";
return;
}
disass_short(ins, line);
disass_long(ins, line);
char buffer[128];
const uint32_t num_bytes = Assemble(ins, buffer);
ASSERT(num_bytes == UsesRex(ins) + ins.opcode->num_bytes,
"size mismatch " << num_bytes << " vs "
<< UsesRex(ins) + ins.opcode->num_bytes);
ASSERT(num_bytes == data.size(), "re-assembler size mismatch");
/*
for (uint32_t i = 0; i < num_bytes; ++i) {
std::cout << std::hex << (unsigned(data[i]) & 0xff) << " "
<< (unsigned(buffer[i]) & 0xff) << "\n";
}
*/
for (uint32_t i = 0; i < num_bytes; ++i) {
ASSERT(data[i] == buffer[i], "assembler byte mismatch "
<< std::dec << i << " " << std::hex
<< unsigned(data[i]) << " "
<< unsigned(buffer[i]));
}
}
int main(int argc, char* argv[]) {
if (argc <= 1) {
std::cout << "no command specified\n";
return 1;
}
if (std::string_view("batch") == argv[1]) {
for (std::string line; getline(std::cin, line);) {
if (line[0] == '#') continue;
const std::string data = ExtractData(line);
batch(data, line);
}
} else {
std::string out;
out.reserve(argc);
for (unsigned i = 1; i < argc; ++i) {
const std::string data = ExtractData(argv[i]);
disass(data, argv[i]);
}
return 0;
}
}