aboutsummaryrefslogtreecommitdiffstats
path: root/ndisasm.c
blob: 71d07df381440fb264270a34bada559bece98e94 (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
/* ndisasm.c   the Netwide Disassembler main module
 *
 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
 * Julian Hall. All rights reserved. The software is
 * redistributable under the licence given in the file "Licence"
 * distributed in the NASM archive.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "insns.h"
#include "nasm.h"
#include "nasmlib.h"
#include "sync.h"
#include "disasm.h"

#define BPL 8			       /* bytes per line of hex dump */

static const char *help =
"usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
"               [-e bytes] [-k start,bytes] [-p vendor] file\n"
"   -a or -i activates auto (intelligent) sync\n"
"   -u sets USE32 (32-bit mode)\n"
"   -b 16 or -b 32 sets number of bits too\n"
"   -h displays this text\n"
"   -r or -v displays the version number\n"
"   -e skips <bytes> bytes of header\n"
"   -k avoids disassembling <bytes> bytes from position <start>\n"
"   -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)\n";

static void output_ins (unsigned long, unsigned char *, int, char *);
static void skip (unsigned long dist, FILE *fp);

int main(int argc, char **argv) 
{
    unsigned char buffer[INSN_MAX * 2], *p, *q;
    char outbuf[256];
    char *pname = *argv;
    char *filename = NULL;
    unsigned long nextsync, synclen, initskip = 0L;
    int lenread;
    long lendis;
    int autosync = FALSE;
    int bits = 16;
    int eof = FALSE;
    unsigned long prefer = 0;
    int rn_error;
    long offset;
    FILE *fp;

    offset = 0;
    init_sync();

    while (--argc) {
	char *v, *vv, *p = *++argv;
	if (*p == '-' && p[1]) {
	    p++;
	    while (*p) switch (tolower(*p)) {
	      case 'a':		       /* auto or intelligent sync */
	      case 'i':
		autosync = TRUE;
		p++;
		break;
	      case 'h':
		fprintf(stderr, help);
		return 0;
	      case 'r':
              case 'v':
                fprintf(stderr, "NDISASM version %s compiled " __DATE__ "\n", NASM_VER);
		return 0;
	      case 'u':		       /* USE32 */
		bits = 32;
		p++;
		break;
	      case 'b':		       /* bits */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-b' requires an argument\n", pname);
		    return 1;
		}
		if (!strcmp(v, "16"))
		    bits = 16;
		else if (!strcmp(v, "32"))
		    bits = 32;
		else {
		    fprintf(stderr, "%s: argument to `-b' should"
			    " be `16' or `32'\n", pname);
		}
		p = "";		       /* force to next argument */
		break;
	      case 'o':		       /* origin */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-o' requires an argument\n", pname);
		    return 1;
		}
		offset = readnum (v, &rn_error);
		if (rn_error) {
		    fprintf(stderr, "%s: `-o' requires a numeric argument\n",
			    pname);
		    return 1;
		}
		p = "";		       /* force to next argument */
		break;
	      case 's':		       /* sync point */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-s' requires an argument\n", pname);
		    return 1;
		}
		add_sync (readnum (v, &rn_error), 0L);
		if (rn_error) {
		    fprintf(stderr, "%s: `-s' requires a numeric argument\n",
			    pname);
		    return 1;
		}
		p = "";		       /* force to next argument */
		break;
	      case 'e':		       /* skip a header */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-e' requires an argument\n", pname);
		    return 1;
		}
		initskip = readnum (v, &rn_error);
		if (rn_error) {
		    fprintf(stderr, "%s: `-e' requires a numeric argument\n",
			    pname);
		    return 1;
		}
		p = "";		       /* force to next argument */
		break;
	      case 'k':		       /* skip a region */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-k' requires an argument\n", pname);
		    return 1;
		}
		vv = strchr(v, ',');
		if (!vv) {
		    fprintf(stderr, "%s: `-k' requires two numbers separated"
			    " by a comma\n", pname);
		    return 1;
		}
		*vv++ = '\0';
		nextsync = readnum (v, &rn_error);
		if (rn_error) {
		    fprintf(stderr, "%s: `-k' requires numeric arguments\n",
			    pname);
		    return 1;
		}
		synclen = readnum (vv, &rn_error);
		if (rn_error) {
		    fprintf(stderr, "%s: `-k' requires numeric arguments\n",
			    pname);
		    return 1;
		}
		add_sync (nextsync, synclen);
		p = "";		       /* force to next argument */
		break;
	    case 'p':		       /* preferred vendor */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-p' requires an argument\n", pname);
		    return 1;
		}
		if ( !strcmp(v, "intel") ) {
		  prefer = 0;	/* Default */
		} else if ( !strcmp(v, "amd") ) {
		  prefer = IF_AMD|IF_3DNOW;
		} else if ( !strcmp(v, "cyrix") ) {
		  prefer = IF_CYRIX|IF_3DNOW;
		} else if ( !strcmp(v, "idt") || !strcmp(v, "centaur") ||
			    !strcmp(v, "winchip") ) {
		  prefer = IF_3DNOW;
		} else {
		  fprintf(stderr, "%s: unknown vendor `%s' specified with `-p'\n", pname, v);
		  return 1;
		}
		p = "";		       /* force to next argument */
		break;
	    default:							/*bf*/
		fprintf(stderr, "%s: unrecognised option `-%c'\n",
			pname, *p);
		return 1;
	    }
	} else if (!filename) {
	    filename = p;
	} else {
	    fprintf(stderr, "%s: more than one filename specified\n", pname);
	    return 1;
	}
    }

    if (!filename) {
	fprintf(stderr, help, pname);
	return 0;
    }

    if (strcmp(filename, "-")) {
	fp = fopen(filename, "rb");
	if (!fp) {
	    fprintf(stderr, "%s: unable to open `%s': %s\n",
		    pname, filename, strerror(errno));
	    return 1;
	}
    } else
	fp = stdin;

    if (initskip > 0)
	skip (initskip, fp);

    /*
     * This main loop is really horrible, and wants rewriting with
     * an axe. It'll stay the way it is for a while though, until I
     * find the energy...
     */

    p = q = buffer;
    nextsync = next_sync (offset, &synclen);
    do {
	unsigned long to_read = buffer+sizeof(buffer)-p;
	if (to_read > nextsync-offset-(p-q))
	    to_read = nextsync-offset-(p-q);
	if (to_read) {
	    lenread = fread (p, 1, to_read, fp);
	    if (lenread == 0)
		eof = TRUE;	       /* help along systems with bad feof */
	} else
	    lenread = 0;
	p += lenread;
	if ((unsigned long)offset == nextsync) {
	    if (synclen) {
		fprintf(stdout, "%08lX  skipping 0x%lX bytes\n", offset, synclen);
		offset += synclen;
		skip (synclen, fp);
	    }
	    p = q = buffer;
	    nextsync = next_sync (offset, &synclen);
	}
	while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
	    lendis = disasm (q, outbuf, bits, offset, autosync, prefer);
	    if (!lendis || lendis > (p - q) ||
		(unsigned long)lendis > nextsync-offset)
		lendis = eatbyte (q, outbuf);
	    output_ins (offset, q, lendis, outbuf);
	    q += lendis;
	    offset += lendis;
	}
	if (q >= buffer+INSN_MAX) {
	    unsigned char *r = buffer, *s = q;
	    int count = p - q;
	    while (count--)
		*r++ = *s++;
	    p -= (q - buffer);
	    q = buffer;
	}
    } while (lenread > 0 || !(eof || feof(fp)));

    if (fp != stdin)
	fclose (fp);

    return 0;
}

static void output_ins (unsigned long offset, unsigned char *data,
			int datalen, char *insn) 
{
    int bytes;
    fprintf(stdout, "%08lX  ", offset);

    bytes = 0;
    while (datalen > 0 && bytes < BPL) {
	fprintf(stdout, "%02X", *data++);
	bytes++;
	datalen--;
    }

    fprintf(stdout, "%*s%s\n", (BPL+1-bytes)*2, "", insn);

    while (datalen > 0) {
	fprintf(stdout, "         -");
	bytes = 0;
	while (datalen > 0 && bytes < BPL) {
	    fprintf(stdout, "%02X", *data++);
	    bytes++;
	    datalen--;
	}
	fprintf(stdout, "\n");
    }
}

/*
 * Skip a certain amount of data in a file, either by seeking if
 * possible, or if that fails then by reading and discarding.
 */
static void skip (unsigned long dist, FILE *fp) 
{
    char buffer[256];		       /* should fit on most stacks :-) */

    /*
     * Got to be careful with fseek: at least one fseek I've tried
     * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
     * ftell... horrible but apparently necessary.
     */
    if (fseek (fp, dist+ftell(fp), SEEK_SET)) {
	while (dist > 0) {
	    unsigned long len = (dist < sizeof(buffer) ?
				 dist : sizeof(buffer));
	    if (fread (buffer, 1, len, fp) < len) {
		perror("fread");
		exit(1);
	    }
	    dist -= len;
	}
    }
}