Fix the handling of pass1 warnings, display control option for warnings
[nasm.git] / ndisasm.c
blob754033c496a8da1f2958408fea6c710ad4894ec6
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * ndisasm.c the Netwide Disassembler main module
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <errno.h>
46 #include <inttypes.h>
48 #include "insns.h"
49 #include "nasm.h"
50 #include "nasmlib.h"
51 #include "sync.h"
52 #include "disasm.h"
54 #define BPL 8 /* bytes per line of hex dump */
56 static const char *help =
57 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
58 " [-e bytes] [-k start,bytes] [-p vendor] file\n"
59 " -a or -i activates auto (intelligent) sync\n"
60 " -u same as -b 32\n"
61 " -b 16, -b 32 or -b 64 sets the processor mode\n"
62 " -h displays this text\n"
63 " -r or -v displays the version number\n"
64 " -e skips <bytes> bytes of header\n"
65 " -k avoids disassembling <bytes> bytes from position <start>\n"
66 " -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)\n";
68 static void output_ins(uint32_t, uint8_t *, int, char *);
69 static void skip(uint32_t dist, FILE * fp);
71 static void ndisasm_verror(int severity, const char *fmt, va_list va)
73 vfprintf(stderr, fmt, va);
75 if (severity & ERR_FATAL)
76 exit(1);
79 int main(int argc, char **argv)
81 char buffer[INSN_MAX * 2], *p, *ep, *q;
82 char outbuf[256];
83 char *pname = *argv;
84 char *filename = NULL;
85 uint32_t nextsync, synclen, initskip = 0L;
86 int lenread;
87 int32_t lendis;
88 bool autosync = false;
89 int bits = 16, b;
90 bool eof = false;
91 iflag_t prefer;
92 bool rn_error;
93 int32_t offset;
94 FILE *fp;
96 tolower_init();
97 nasm_set_verror(ndisasm_verror);
98 iflag_clear_all(&prefer);
100 offset = 0;
101 init_sync();
103 while (--argc) {
104 char *v, *vv, *p = *++argv;
105 if (*p == '-' && p[1]) {
106 p++;
107 while (*p)
108 switch (nasm_tolower(*p)) {
109 case 'a': /* auto or intelligent sync */
110 case 'i':
111 autosync = true;
112 p++;
113 break;
114 case 'h':
115 fputs(help, stderr);
116 return 0;
117 case 'r':
118 case 'v':
119 fprintf(stderr,
120 "NDISASM version %s compiled on %s\n",
121 nasm_version, nasm_date);
122 return 0;
123 case 'u': /* -u for -b 32, -uu for -b 64 */
124 if (bits < 64)
125 bits <<= 1;
126 p++;
127 break;
128 case 'b': /* bits */
129 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
130 if (!v) {
131 fprintf(stderr, "%s: `-b' requires an argument\n",
132 pname);
133 return 1;
135 b = strtoul(v, &ep, 10);
136 if (*ep || !(bits == 16 || bits == 32 || bits == 64)) {
137 fprintf(stderr, "%s: argument to `-b' should"
138 " be 16, 32 or 64\n", pname);
139 } else {
140 bits = b;
142 p = ""; /* force to next argument */
143 break;
144 case 'o': /* origin */
145 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
146 if (!v) {
147 fprintf(stderr, "%s: `-o' requires an argument\n",
148 pname);
149 return 1;
151 offset = readnum(v, &rn_error);
152 if (rn_error) {
153 fprintf(stderr,
154 "%s: `-o' requires a numeric argument\n",
155 pname);
156 return 1;
158 p = ""; /* force to next argument */
159 break;
160 case 's': /* sync point */
161 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
162 if (!v) {
163 fprintf(stderr, "%s: `-s' requires an argument\n",
164 pname);
165 return 1;
167 add_sync(readnum(v, &rn_error), 0L);
168 if (rn_error) {
169 fprintf(stderr,
170 "%s: `-s' requires a numeric argument\n",
171 pname);
172 return 1;
174 p = ""; /* force to next argument */
175 break;
176 case 'e': /* skip a header */
177 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
178 if (!v) {
179 fprintf(stderr, "%s: `-e' requires an argument\n",
180 pname);
181 return 1;
183 initskip = readnum(v, &rn_error);
184 if (rn_error) {
185 fprintf(stderr,
186 "%s: `-e' requires a numeric argument\n",
187 pname);
188 return 1;
190 p = ""; /* force to next argument */
191 break;
192 case 'k': /* skip a region */
193 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
194 if (!v) {
195 fprintf(stderr, "%s: `-k' requires an argument\n",
196 pname);
197 return 1;
199 vv = strchr(v, ',');
200 if (!vv) {
201 fprintf(stderr,
202 "%s: `-k' requires two numbers separated"
203 " by a comma\n", pname);
204 return 1;
206 *vv++ = '\0';
207 nextsync = readnum(v, &rn_error);
208 if (rn_error) {
209 fprintf(stderr,
210 "%s: `-k' requires numeric arguments\n",
211 pname);
212 return 1;
214 synclen = readnum(vv, &rn_error);
215 if (rn_error) {
216 fprintf(stderr,
217 "%s: `-k' requires numeric arguments\n",
218 pname);
219 return 1;
221 add_sync(nextsync, synclen);
222 p = ""; /* force to next argument */
223 break;
224 case 'p': /* preferred vendor */
225 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
226 if (!v) {
227 fprintf(stderr, "%s: `-p' requires an argument\n",
228 pname);
229 return 1;
231 if (!strcmp(v, "intel")) {
232 iflag_clear_all(&prefer); /* default */
233 } else if (!strcmp(v, "amd")) {
234 iflag_clear_all(&prefer);
235 iflag_set(&prefer, IF_AMD);
236 iflag_set(&prefer, IF_3DNOW);
237 } else if (!strcmp(v, "cyrix")) {
238 iflag_clear_all(&prefer);
239 iflag_set(&prefer, IF_CYRIX);
240 iflag_set(&prefer, IF_3DNOW);
241 } else if (!strcmp(v, "idt") ||
242 !strcmp(v, "centaur") ||
243 !strcmp(v, "winchip")) {
244 iflag_clear_all(&prefer);
245 iflag_set(&prefer, IF_3DNOW);
246 } else {
247 fprintf(stderr,
248 "%s: unknown vendor `%s' specified with `-p'\n",
249 pname, v);
250 return 1;
252 p = ""; /* force to next argument */
253 break;
254 default: /*bf */
255 fprintf(stderr, "%s: unrecognised option `-%c'\n",
256 pname, *p);
257 return 1;
259 } else if (!filename) {
260 filename = p;
261 } else {
262 fprintf(stderr, "%s: more than one filename specified\n",
263 pname);
264 return 1;
268 if (!filename) {
269 fprintf(stderr, help, pname);
270 return 0;
273 if (strcmp(filename, "-")) {
274 fp = fopen(filename, "rb");
275 if (!fp) {
276 fprintf(stderr, "%s: unable to open `%s': %s\n",
277 pname, filename, strerror(errno));
278 return 1;
280 } else
281 fp = stdin;
283 if (initskip > 0)
284 skip(initskip, fp);
287 * This main loop is really horrible, and wants rewriting with
288 * an axe. It'll stay the way it is for a while though, until I
289 * find the energy...
292 p = q = buffer;
293 nextsync = next_sync(offset, &synclen);
294 do {
295 uint32_t to_read = buffer + sizeof(buffer) - p;
296 if ((nextsync || synclen) &&
297 to_read > nextsync - offset - (p - q))
298 to_read = nextsync - offset - (p - q);
299 if (to_read) {
300 lenread = fread(p, 1, to_read, fp);
301 if (lenread == 0)
302 eof = true; /* help along systems with bad feof */
303 } else
304 lenread = 0;
305 p += lenread;
306 if ((nextsync || synclen) &&
307 (uint32_t)offset == nextsync) {
308 if (synclen) {
309 fprintf(stdout, "%08"PRIX32" skipping 0x%"PRIX32" bytes\n",
310 offset, synclen);
311 offset += synclen;
312 skip(synclen, fp);
314 p = q = buffer;
315 nextsync = next_sync(offset, &synclen);
317 while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
318 lendis =
319 disasm((uint8_t *) q, outbuf, sizeof(outbuf), bits,
320 offset, autosync, &prefer);
321 if (!lendis || lendis > (p - q)
322 || ((nextsync || synclen) &&
323 (uint32_t)lendis > nextsync - offset))
324 lendis = eatbyte((uint8_t *) q, outbuf, sizeof(outbuf), bits);
325 output_ins(offset, (uint8_t *) q, lendis, outbuf);
326 q += lendis;
327 offset += lendis;
329 if (q >= buffer + INSN_MAX) {
330 uint8_t *r = (uint8_t *) buffer, *s = (uint8_t *) q;
331 int count = p - q;
332 while (count--)
333 *r++ = *s++;
334 p -= (q - buffer);
335 q = buffer;
337 } while (lenread > 0 || !(eof || feof(fp)));
339 if (fp != stdin)
340 fclose(fp);
342 return 0;
345 static void output_ins(uint32_t offset, uint8_t *data,
346 int datalen, char *insn)
348 int bytes;
349 fprintf(stdout, "%08"PRIX32" ", offset);
351 bytes = 0;
352 while (datalen > 0 && bytes < BPL) {
353 fprintf(stdout, "%02X", *data++);
354 bytes++;
355 datalen--;
358 fprintf(stdout, "%*s%s\n", (BPL + 1 - bytes) * 2, "", insn);
360 while (datalen > 0) {
361 fprintf(stdout, " -");
362 bytes = 0;
363 while (datalen > 0 && bytes < BPL) {
364 fprintf(stdout, "%02X", *data++);
365 bytes++;
366 datalen--;
368 fprintf(stdout, "\n");
373 * Skip a certain amount of data in a file, either by seeking if
374 * possible, or if that fails then by reading and discarding.
376 static void skip(uint32_t dist, FILE * fp)
378 char buffer[256]; /* should fit on most stacks :-) */
381 * Got to be careful with fseek: at least one fseek I've tried
382 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
383 * ftell... horrible but apparently necessary.
385 if (fseek(fp, dist + ftell(fp), SEEK_SET)) {
386 while (dist > 0) {
387 uint32_t len = (dist < sizeof(buffer) ?
388 dist : sizeof(buffer));
389 if (fread(buffer, 1, len, fp) < len) {
390 perror("fread");
391 exit(1);
393 dist -= len;