aboutsummaryrefslogtreecommitdiff
path: root/xexamine.c
blob: 3c277e5bb1b684b1fd34916712f0837c68973759 (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "get_address.h"
#include "xex.h"

#define SELF "xexamine"

/* #define CLASSIFY_DEBUG */

/*
	show all segments of a xex file, including:
	has ffff header or not
	start address, end address, length
	whether the segment contains code or just data (heuristics)
	checksum of the segment contents
*/

/* crc32() and crc32_for_byte() come from public domain code:
	http://home.thep.lu.se/~bjorn/crc/
*/

uint32_t crc32_for_byte(uint32_t r) {
	int j;
	for(j = 0; j < 8; ++j)
		r = (r & 1? 0: (uint32_t)0xEDB88320L) ^ r >> 1;
	return r ^ (uint32_t)0xFF000000L;
}

void crc32(const void *data, size_t n_bytes, uint32_t* crc) {
	size_t i;
	static uint32_t table[0x100];
	if(!*table)
		for(i = 0; i < 0x100; ++i)
			table[i] = crc32_for_byte(i);
	for(i = 0; i < n_bytes; ++i)
		*crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
}

void usage(int status) {
	printf("Usage: " SELF " [-h] [-d] [-v] [-s segment] file.xex\n");
	exit(status);
}

#define CL_DATA 0
#define CL_OPCODE 1
#define CL_OPERAND 2

/* 3 tables used by classify_seg() */

int opcode_valid[] = {
/* 0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
	1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, /* 0 */
	1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, /* 1 */
	1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, /* 2 */
	1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, /* 3 */
	1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, /* 4 */
	1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, /* 5 */
	1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, /* 6 */
	1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, /* 7 */
	0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, /* 8 */
	1, 1, 0, 0, 1, 1, 1, 0, 1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0, 0, 0, 0, 0, 0,
};

void find_dlist(const unsigned char *mem, unsigned char *map, int len) {
	int i, dlstart = -1, dlend = -1;

	for(i = 2; i < len-2; i++) {
		if(mem[i] == 'p' && mem[i-1] == 'p' && mem[i-2] == 'p') {
			dlstart = i - 2;
		}

		if(dlstart > -1 && mem[i] == 'A') {
			dlend = i;
			break;
		}
	}
	if(dlstart != -1 && dlend != -1) {
#ifdef CLASSIFY_DEBUG
		printf("display list found, offsets %d - %d\n", dlstart, dlend);
#endif
		for(i = dlstart; i <= dlend; i++)
			map[i] = CL_DATA;
	}
}

/*
	classify_seg() returns code percentage estimate.
	possible strategies:
	if seg is only 1 byte, it's data (return 0)
	iterate over segment, semi-disassemble, classify each
	byte as opcode, operand, or data.
	instructions that aren't control transfers (aka jmp, jsr, rts, rti,
	branches) that are immediately followed by non-code, will get marked
	as data (back to the last transfer instruction).
	branches that branch back before the start of the segment are data?
	branch/jmp/jsr whose target is data, are also data?
	but jmp/jsr outside of the segment can't be assumed data...
	long runs (>=8) of the same byte value are data.
*/

int classify_seg(const xex_segment seg) {
	int i, j, addr, byte, oplen, target, last_cltxfr = 0, changed;
	int runstart = 0, runbyte = -1, runcount = 0;
	float f;
	unsigned char map[65536];

	memset(map, 0, 65535);

	/* pass 1: mark valid opcodes as CL_OPCODE, their operands as
		CL_OPERAND, and anything else as CL_DATA. */
	for(i = 0; i < seg.len; ) {
		byte = seg.object[i];
		oplen = opcode_lengths[byte];
		if(opcode_valid[byte]) {
			map[i] = CL_OPCODE;
			if(oplen >= 2) map[i + 1] = CL_OPERAND;
			if(oplen == 3) map[i + 2] = CL_OPERAND;
		} else {
			map[i] = CL_DATA;
		}
		i += oplen;
	}

	/* pass 1.5: if there's a display list, it's data */
	find_dlist(seg.object, map, seg.len);

	/* pass 3: runs of >=3 of the same byte value are data, unless
		they're ASL A, LSR A, or NOP. */
	for(i = 0; i < seg.len; i++) {
		byte = seg.object[i];
		if(byte == runbyte) {
			runcount++;
		} else {
			if(
					runcount > 8 ||
					(runcount >= 3 && !(runbyte == 0x0a || runbyte == 0x4a || runbyte == 0xea))
			  )
			{
				#ifdef CLASSIFY_DEBUG
				printf("run of %d bytes, $%02x, at %d\n", runcount, runbyte, runstart);
				#endif
				for(j = runstart; j < i; j++)
					map[j] = CL_DATA;
			}
			runstart = i;
			runbyte = byte;
			runcount = 0;
		}
		/*
		printf("got here, i=%d, runbyte=%02x, runstart=%d, runcount=%d\n", i, runbyte, runstart, runcount);
		*/
	}

	/* pass 4: code that doesn't branch/jump/return and runs into data
		gets marked as data. */
	runcount = runstart = 0;
	do {
		runcount++;
		changed = 0;
		for(i = 0; i < seg.len; i++) {
			if(map[i] == CL_OPCODE && opcode_is_ctlxfr[seg.object[i]]) {
				last_cltxfr = i;
				#ifdef CLASSIFY_DEBUG
				/*
				printf("last_cltxfr = %04x, opcode %02x;\n", last_cltxfr, seg.object[i]);
				*/
				#endif
			} else if(map[i] == CL_DATA) {
				#ifdef CLASSIFY_DEBUG
				/*
				printf("marking range as data: %04x - %04x\n", last_cltxfr, i);
				*/
				#endif
				for(j = last_cltxfr; j < i; j++) {
					map[j] = CL_DATA;
				}
				last_cltxfr = i;
			}
		}

		/* pass 4: branch and jmp abs instructions whose target is data,
			are also data. repeats until nothing is changed. */
		for(i = 0; i < seg.len; i++) {
			addr = seg.start_addr + i;
			byte = seg.object[i];
			target = -1;
			if(map[i] == CL_OPCODE) switch(byte) {
				case 0x4c: /* JMP absolute */
				case 0x20: /* JSR absolute */
					target = addr + (seg.object[i + 1] | (seg.object[i + 2] << 8));
					if((target < addr) || (target > (addr + seg.len)))
						target = -1; /* jsr/jmp out of segment */
					else {
						if(map[target - addr] != CL_OPCODE) {
							map[i] = map[i + 1] = map[i + 2] = CL_DATA;
							changed = 1;
							runstart += 3;
						}
					}
					break;

				case 0x10: /* BPL */
				case 0x30: /* BMI */
				case 0x50: /* BVC */
				case 0x70: /* BVS */
				case 0x90: /* BCC */
				case 0xb0: /* BCS */
				case 0xd0: /* BNE */
				case 0xf0: /* BEQ */
					target = addr + i + 2 + ((signed char)seg.object[i + 1]);
					if((target < addr) || (target > (addr + seg.len))) {
						/* branch out of segment, assume data */
						target = -1;
						map[i] = map[i + 1] = CL_DATA;
						runstart += 2;
						changed = 1;
					} else if(map[target - addr] != CL_OPCODE) {
						/* branch to data! */
						map[i] = map[i + 1] = CL_DATA;
						runstart += 2;
						changed = 1;
					}
					break;

				default:
					break;
			}
		}
	} while(changed);
#ifdef CLASSIFY_DEBUG
	printf("pass 4 ran %d times, changed %d bytes\n", runcount, runstart);
#endif

	/* last pass: calculate opcode/operand percentage */
	j = 0;
	for(i = 0; i < seg.len; i++) {
		if(map[i] != CL_DATA) j++;
	}

#ifdef CLASSIFY_DEBUG
	for(i = 0; i < seg.len; i++) {
		if(i % 16 == 0) {
			printf("\n%04x: ", i);
		}
		printf("%02x/%d ", seg.object[i], map[i]);
	}
	printf("\n");
#endif

	f = (float)j / (float)seg.len * 100.0;
	return (int)f;
}

int main(int argc, char **argv) {
	FILE *f;
	xex_segment seg;
	unsigned char buffer[65536];
	char *filename;
	int opt, offset, segcount = 0, only_segment = 0, header_printed = 0, decimal = 0, print_filenames = 0;
	uint32_t crc;

	while((opt = getopt(argc, argv, "vhs:d")) != -1) {
		switch(opt) {
			case 'd':
				decimal = 1;
				break;
			case 's':
				if( (only_segment = get_address(SELF, optarg)) < 0 )
					exit(1);
				if(!only_segment) {
					fprintf(stderr, SELF ": 0 is not a valid segment number (they start with 1).\n");
					exit(1);
				}
				break;
			case 'v':
				xex_verbose = 1;
				break;
			case 'h':
				usage(0);
				break;
			default:
				usage(1);
				break;
		}
	}

	if(optind >= argc) {
		fprintf(stderr, SELF ": no xex file argument.\n");
		usage(1);
	}

	if(argc > optind + 1) print_filenames = 1;
	while(optind < argc) {
		filename = argv[optind++];
		if( !(f = fopen(filename, "rb")) ) {
			fprintf(stderr, "%s: ", SELF);
			perror(filename);
			exit(1);
		}
		if(print_filenames) printf("%s:\n", filename);

		seg.object = buffer;

		offset = segcount = 0;
		while(xex_fread_seg(&seg, f)) {
			segcount++;

			crc = 0;
			crc32(seg.object, seg.len, &crc);

			if(!only_segment || (only_segment == segcount)) {
				if(!header_printed) {
					printf("Seg | Offset | Start | End   | Bytes | CRC32    | Type\n");
					header_printed++;
				}

				printf((decimal ? 
							"%3d | %6d | %5d | %5d | %5d | %08x | " :
							"%3d | %6d | $%04x | $%04x | %5d | %08x | "),
						segcount, offset, seg.start_addr, seg.end_addr, seg.len, crc);

				/* TODO: what if there's a >=4 byte segment that loads at RUNAD?
					the next 2 bytes are INITAD, we would silently ignore that fact. */
				if(seg.start_addr == XEX_RUNAD && seg.len > 1)
					printf((decimal ? "Run  %5d"  : "Run  $%04x"), (seg.object[0] | (seg.object[1] << 8)));
				else if(seg.start_addr == XEX_INITAD && seg.len > 1)
					printf((decimal ? "Init %5d"  : "Init $%04x"), (seg.object[0] | (seg.object[1] << 8)));
				else printf("%d%% code", classify_seg(seg));

				putchar('\n');
			}

			offset = ftell(f);
		}

		if(only_segment && (segcount < only_segment)) {
			fprintf(stderr, SELF ": can't show segment %d, only %d segments in file.\n", only_segment, segcount);
			return 1;
		}

		if(!segcount) {
			fprintf(stderr, SELF ": %s is not an Atari 8-bit executable.\n", filename);
			return 1;
		}
	}

	return 0;
}