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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* SPDX-License-Identifier: Unlicense
*/
#include "disasm.h"
#include "data_buffer.h"
#include "common.h"
#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static
#include "optparse/optparse.h"
#include <cassert>
#include <cinttypes>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <climits>
/*
* We need to be able to modify output to place a mark when some jumping back
* is found, hence we should build output table instead of emitting asm right
* away into the output stream.
*
* I think the output should be an ordered map of decoded instructions. When the
* output is built according to the map we must walk through all the binary file
* again alongside with the output map and emit the final output right into the
* output stream.
*
* Trace data parser is needed. Maybe just using atol(3) will be ok.
*/
enum class TracedNodeType {
kInstruction,
kData,
};
struct DisasmNode {
TracedNodeType type{};
uint32_t offset{};
size_t size{kInstructionSizeStepBytes};
char *asm_string{}; // Disassembly of an instruction at the current offset
void Disasm(const DataBuffer &code);
~DisasmNode();
};
void DisasmNode::Disasm(const DataBuffer &code)
{
constexpr size_t kBufferSize = 100;
char *asm_str = new char [kBufferSize]{};
assert(asm_str);
this->asm_string = asm_str;
// We assume that no MMU and ROM is always starts with 0
assert(this->offset < code.occupied_size);
const uint16_t instr = GetU16BE(code.buffer + this->offset);
const size_t rendered_sz = m68k_disasm(
asm_str, kBufferSize, &this->size, instr, this->offset, code);
const size_t comment_rendered_sz = m68k_render_raw_data_comment(
asm_str + rendered_sz, kBufferSize - rendered_sz, this->offset, this->size, code);
(void) comment_rendered_sz;
}
DisasmNode::~DisasmNode()
{
if (asm_string) {
delete [] asm_string;
asm_string = nullptr;
}
}
class DisasmMap {
DisasmNode *_map[kDisasmMapSizeElements]{};
DisasmNode *findNodeByOffset(uint32_t offset) const;
public:
const DisasmNode *FindNodeByOffset(uint32_t offset) const
{
return findNodeByOffset(offset);
};
// Returns true if node inserted, false if node already exist and has not
// been changed
bool InsertTracedNode(uint32_t offset, TracedNodeType);
// This function disassembles everything that has been traced
void DisasmAll(const DataBuffer &code);
~DisasmMap();
};
DisasmNode *DisasmMap::findNodeByOffset(uint32_t offset) const
{
if (offset < kRomSizeBytes)
return _map[offset / kInstructionSizeStepBytes];
return nullptr;
}
bool DisasmMap::InsertTracedNode(uint32_t offset, TracedNodeType type)
{
if (findNodeByOffset(offset))
return false;
auto *node = new DisasmNode(DisasmNode{type, offset});
assert(node);
_map[offset / kInstructionSizeStepBytes] = node;
return true;
}
void DisasmMap::DisasmAll(const DataBuffer &code)
{
for (size_t i = 0; i < kDisasmMapSizeElements; i++) {
auto *node = _map[i];
if (node) {
_map[i]->Disasm(code);
}
}
}
DisasmMap::~DisasmMap()
{
for (size_t i = 0; i < kDisasmMapSizeElements; i++) {
delete _map[i];
_map[i] = nullptr;
}
}
static void RenderDisassembly(FILE *output, const DisasmMap &disasm_map, const DataBuffer &code)
{
for (size_t i = 0; i < code.occupied_size;) {
const DisasmNode *node = disasm_map.FindNodeByOffset(i);
if (node) {
assert(node->asm_string);
fputs(node->asm_string, output);
fputc('\n', output);
i += node->size;
} else {
fprintf(output, " .short 0x%02x%02x\n", code.buffer[i], code.buffer[i + 1]);
i += kInstructionSizeStepBytes;
}
}
}
static void ParseTraceData(DisasmMap &disasm_map, const DataBuffer &trace_data)
{
// FIXME make a full blown parser with various radixes support and different
// trace types support
bool parse = true;
for (size_t i = 0; i < trace_data.occupied_size; i++) {
if (trace_data.buffer[i] == '\n' || trace_data.buffer[i] == '\r') {
parse = true;
} else if (parse) {
errno = 0;
char *startptr = reinterpret_cast<char *>(trace_data.buffer + i);
char *endptr = startptr;
const long offset = strtol(startptr, &endptr, 10);
if ((offset == LONG_MAX || offset == LONG_MIN) && errno == ERANGE) {
// Error, just skip
} else if (startptr == endptr) {
// Error, just skip
} else {
// Valid value
disasm_map.InsertTracedNode(offset, TracedNodeType::kInstruction);
}
if (startptr != endptr) {
i += endptr - startptr - 1;
}
parse = false;
}
}
}
static size_t ReadFromStream(DataBuffer &db, FILE *stream)
{
assert(db.buffer && db.buffer_size >= db.kInitialSize);
while (1) {
const size_t read_size = db.buffer_size - db.occupied_size;
const size_t fread_ret = fread(
db.buffer + db.occupied_size, sizeof(*db.buffer), read_size, stream);
db.occupied_size += fread_ret;
if (fread_ret >= db.buffer_size) {
assert(fread_ret == db.buffer_size);
db.Expand(db.buffer_size * 2);
} else {
const int err = errno;
if (feof(stream)) {
break;
} else if (ferror(stream)) {
fprintf(stderr, "ReadFromStream: fread(%zu): Error (%d): \"%s\"\n", read_size, err, strerror(err));
return EXIT_FAILURE;
} else if (db.buffer_size == db.occupied_size) {
db.Expand(db.buffer_size * 2);
} else {
assert(false);
}
}
}
return db.occupied_size;
}
static int M68kDisasmByTrace(FILE *input_stream, FILE *output_stream, FILE *trace_stream)
{
// Read machine code into buffer
DataBuffer code{};
const size_t input_size = ReadFromStream(code, input_stream);
if (input_size == 0) {
fprintf(stderr, "ReadFromStream(code, input_stream): Error: No data has been read\n");
return EXIT_FAILURE;
}
// Read trace file into buffer
DataBuffer trace_data{};
const size_t trace_size = ReadFromStream(trace_data, trace_stream);
if (trace_size == 0) {
fprintf(stderr, "ReadFromStream(trace_data, trace_stream): Error: No data has been read\n");
return EXIT_FAILURE;
}
// Parse trace file into map
DisasmMap *disasm_map = new DisasmMap{};
assert(disasm_map);
ParseTraceData(*disasm_map, trace_data);
// Disasm into output map
disasm_map->DisasmAll(code);
// Print output into output_stream
RenderDisassembly(output_stream, *disasm_map, code);
delete disasm_map;
return 0;
}
static int M68kDisasmAll(FILE *input_stream, FILE *output_stream)
{
uint8_t instruction[kInstructionSizeStepBytes]{};
const size_t read_size = kInstructionSizeStepBytes;
while (1) {
const size_t fread_ret = fread(instruction, 1, read_size, input_stream);
if (fread_ret == 0) {
const int err = errno;
if (feof(input_stream)) {
break;
} else {
fprintf(stderr, "ReadFromStream: fread(%zu): Error (%d): \"%s\"\n", read_size, err, strerror(err));
return EXIT_FAILURE;
}
}
fprintf(output_stream, " .short 0x%02x%02x\n", instruction[0], instruction[1]);
}
return 0;
}
static void PrintUsage(FILE *stream, const char *argv0)
{
fprintf(stream, "Usage: %s [options] [<input-file-name>]\n", argv0);
fprintf(stream, " -h, --help, Show this message\n");
fprintf(stream, " -o, --output, Where to write disassembly to (stdout if not set)\n");
fprintf(stream, " -t, --pc-trace, File containing PC trace\n");
fprintf(stream, " <input_file_name> Binary file with machine code (stdin if not set)\n");
}
int main(int, char* argv[])
{
struct optparse_long longopts[] = {
{"help", 'h', OPTPARSE_NONE},
{"output", 'o', OPTPARSE_REQUIRED},
{"pc-trace", 't', OPTPARSE_REQUIRED},
{},
};
const char *trace_file_name = nullptr;
const char *output_file_name = nullptr;
const char *input_file_name = nullptr;
struct optparse options;
optparse_init(&options, argv);
// Parse opts
int option;
while ((option = optparse_long(&options, longopts, NULL)) != -1) {
switch (option) {
case 'h':
PrintUsage(stdout, argv[0]);
return EXIT_SUCCESS;
break;
case 'o':
output_file_name = options.optarg;
break;
case 't':
trace_file_name = options.optarg;
break;
case '?':
fprintf(stderr, "main: optparse_long: Error: \"%s\"\n", options.errmsg);
return EXIT_FAILURE;
}
}
// Parse input file name
char *arg;
while ((arg = optparse_arg(&options))) {
if (input_file_name == nullptr) {
input_file_name = arg;
} else {
fprintf(stderr, "error: too many free arguments provided\n");
return EXIT_FAILURE;
}
}
// Open the files
FILE *input_stream = stdin;
FILE *output_stream = stdout;
FILE *trace_stream = nullptr;
if (input_file_name) {
input_stream = fopen(input_file_name, "r");
if (input_stream == nullptr) {
const int err = errno;
fprintf(stderr, "main: fopen(\"%s\", \"r\"): Error (%d): \"%s\"\n", input_file_name, err, strerror(err));
return EXIT_FAILURE;
}
}
if (output_file_name) {
output_stream = fopen(output_file_name, "w");
if (output_stream == nullptr) {
const int err = errno;
fprintf(stderr, "main: fopen(\"%s\", \"w\"): Error (%d): \"%s\"\n", output_file_name, err, strerror(err));
return EXIT_FAILURE;
}
}
if (trace_file_name) {
trace_stream = fopen(trace_file_name, "r");
if (trace_stream == nullptr) {
const int err = errno;
fprintf(stderr, "main: fopen(\"%s\", \"r\"): Error (%d): \"%s\"\n", trace_file_name, err, strerror(err));
return EXIT_FAILURE;
}
}
// Run the program
const int ret = trace_stream
? M68kDisasmByTrace(input_stream, output_stream, trace_stream)
: M68kDisasmAll(input_stream, output_stream);
if (trace_stream != nullptr) {
fclose(trace_stream);
}
if (output_stream != stdout) {
fclose(output_stream);
}
if (input_stream != stdin) {
fclose(input_stream);
}
return ret;
}
|