NASM 2.06.01rc1
[nasm/avx512.git] / ndisasm.c
blobc4cf66d235d6ff7035db42fcb97e38a85d4828fd
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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
12 * reference.
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
21 * conditions are met:
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * ndisasm.c the Netwide Disassembler main module
50 #include "compiler.h"
52 #include <stdio.h>
53 #include <stdarg.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include <errno.h>
58 #include <inttypes.h>
60 #include "insns.h"
61 #include "nasm.h"
62 #include "nasmlib.h"
63 #include "sync.h"
64 #include "disasm.h"
66 #define BPL 8 /* bytes per line of hex dump */
68 static const char *help =
69 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
70 " [-e bytes] [-k start,bytes] [-p vendor] file\n"
71 " -a or -i activates auto (intelligent) sync\n"
72 " -u same as -b 32\n"
73 " -b 16, -b 32 or -b 64 sets the processor mode\n"
74 " -h displays this text\n"
75 " -r or -v displays the version number\n"
76 " -e skips <bytes> bytes of header\n"
77 " -k avoids disassembling <bytes> bytes from position <start>\n"
78 " -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)\n";
80 static void output_ins(uint32_t, uint8_t *, int, char *);
81 static void skip(uint32_t dist, FILE * fp);
83 static void ndisasm_error(int severity, const char *fmt, ...)
85 va_list va;
87 va_start(va, fmt);
88 vfprintf(stderr, fmt, va);
90 if (severity & ERR_FATAL)
91 exit(1);
94 int main(int argc, char **argv)
96 char buffer[INSN_MAX * 2], *p, *ep, *q;
97 char outbuf[256];
98 char *pname = *argv;
99 char *filename = NULL;
100 uint32_t nextsync, synclen, initskip = 0L;
101 int lenread;
102 int32_t lendis;
103 bool autosync = false;
104 int bits = 16, b;
105 bool eof = false;
106 uint32_t prefer = 0;
107 bool rn_error;
108 int32_t offset;
109 FILE *fp;
111 tolower_init();
112 nasm_set_malloc_error(ndisasm_error);
114 offset = 0;
115 init_sync();
117 while (--argc) {
118 char *v, *vv, *p = *++argv;
119 if (*p == '-' && p[1]) {
120 p++;
121 while (*p)
122 switch (nasm_tolower(*p)) {
123 case 'a': /* auto or intelligent sync */
124 case 'i':
125 autosync = true;
126 p++;
127 break;
128 case 'h':
129 fprintf(stderr, help);
130 return 0;
131 case 'r':
132 case 'v':
133 fprintf(stderr,
134 "NDISASM version %s compiled on %s\n",
135 nasm_version, nasm_date);
136 return 0;
137 case 'u': /* -u for -b 32, -uu for -b 64 */
138 if (bits < 64)
139 bits <<= 1;
140 p++;
141 break;
142 case 'b': /* bits */
143 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
144 if (!v) {
145 fprintf(stderr, "%s: `-b' requires an argument\n",
146 pname);
147 return 1;
149 b = strtoul(v, &ep, 10);
150 if (*ep || !(bits == 16 || bits == 32 || bits == 64)) {
151 fprintf(stderr, "%s: argument to `-b' should"
152 " be 16, 32 or 64\n", pname);
153 } else {
154 bits = b;
156 p = ""; /* force to next argument */
157 break;
158 case 'o': /* origin */
159 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
160 if (!v) {
161 fprintf(stderr, "%s: `-o' requires an argument\n",
162 pname);
163 return 1;
165 offset = readnum(v, &rn_error);
166 if (rn_error) {
167 fprintf(stderr,
168 "%s: `-o' requires a numeric argument\n",
169 pname);
170 return 1;
172 p = ""; /* force to next argument */
173 break;
174 case 's': /* sync point */
175 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
176 if (!v) {
177 fprintf(stderr, "%s: `-s' requires an argument\n",
178 pname);
179 return 1;
181 add_sync(readnum(v, &rn_error), 0L);
182 if (rn_error) {
183 fprintf(stderr,
184 "%s: `-s' requires a numeric argument\n",
185 pname);
186 return 1;
188 p = ""; /* force to next argument */
189 break;
190 case 'e': /* skip a header */
191 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
192 if (!v) {
193 fprintf(stderr, "%s: `-e' requires an argument\n",
194 pname);
195 return 1;
197 initskip = readnum(v, &rn_error);
198 if (rn_error) {
199 fprintf(stderr,
200 "%s: `-e' requires a numeric argument\n",
201 pname);
202 return 1;
204 p = ""; /* force to next argument */
205 break;
206 case 'k': /* skip a region */
207 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
208 if (!v) {
209 fprintf(stderr, "%s: `-k' requires an argument\n",
210 pname);
211 return 1;
213 vv = strchr(v, ',');
214 if (!vv) {
215 fprintf(stderr,
216 "%s: `-k' requires two numbers separated"
217 " by a comma\n", pname);
218 return 1;
220 *vv++ = '\0';
221 nextsync = readnum(v, &rn_error);
222 if (rn_error) {
223 fprintf(stderr,
224 "%s: `-k' requires numeric arguments\n",
225 pname);
226 return 1;
228 synclen = readnum(vv, &rn_error);
229 if (rn_error) {
230 fprintf(stderr,
231 "%s: `-k' requires numeric arguments\n",
232 pname);
233 return 1;
235 add_sync(nextsync, synclen);
236 p = ""; /* force to next argument */
237 break;
238 case 'p': /* preferred vendor */
239 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
240 if (!v) {
241 fprintf(stderr, "%s: `-p' requires an argument\n",
242 pname);
243 return 1;
245 if (!strcmp(v, "intel")) {
246 prefer = 0; /* Default */
247 } else if (!strcmp(v, "amd")) {
248 prefer = IF_AMD | IF_3DNOW;
249 } else if (!strcmp(v, "cyrix")) {
250 prefer = IF_CYRIX | IF_3DNOW;
251 } else if (!strcmp(v, "idt") || !strcmp(v, "centaur")
252 || !strcmp(v, "winchip")) {
253 prefer = IF_3DNOW;
254 } else {
255 fprintf(stderr,
256 "%s: unknown vendor `%s' specified with `-p'\n",
257 pname, v);
258 return 1;
260 p = ""; /* force to next argument */
261 break;
262 default: /*bf */
263 fprintf(stderr, "%s: unrecognised option `-%c'\n",
264 pname, *p);
265 return 1;
267 } else if (!filename) {
268 filename = p;
269 } else {
270 fprintf(stderr, "%s: more than one filename specified\n",
271 pname);
272 return 1;
276 if (!filename) {
277 fprintf(stderr, help, pname);
278 return 0;
281 if (strcmp(filename, "-")) {
282 fp = fopen(filename, "rb");
283 if (!fp) {
284 fprintf(stderr, "%s: unable to open `%s': %s\n",
285 pname, filename, strerror(errno));
286 return 1;
288 } else
289 fp = stdin;
291 if (initskip > 0)
292 skip(initskip, fp);
295 * This main loop is really horrible, and wants rewriting with
296 * an axe. It'll stay the way it is for a while though, until I
297 * find the energy...
300 p = q = buffer;
301 nextsync = next_sync(offset, &synclen);
302 do {
303 uint32_t to_read = buffer + sizeof(buffer) - p;
304 if ((nextsync || synclen) &&
305 to_read > nextsync - offset - (p - q))
306 to_read = nextsync - offset - (p - q);
307 if (to_read) {
308 lenread = fread(p, 1, to_read, fp);
309 if (lenread == 0)
310 eof = true; /* help along systems with bad feof */
311 } else
312 lenread = 0;
313 p += lenread;
314 if ((nextsync || synclen) &&
315 (uint32_t)offset == nextsync) {
316 if (synclen) {
317 fprintf(stdout, "%08"PRIX32" skipping 0x%"PRIX32" bytes\n",
318 offset, synclen);
319 offset += synclen;
320 skip(synclen, fp);
322 p = q = buffer;
323 nextsync = next_sync(offset, &synclen);
325 while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
326 lendis =
327 disasm((uint8_t *) q, outbuf, sizeof(outbuf), bits,
328 offset, autosync, prefer);
329 if (!lendis || lendis > (p - q)
330 || ((nextsync || synclen) &&
331 (uint32_t)lendis > nextsync - offset))
332 lendis = eatbyte((uint8_t *) q, outbuf, sizeof(outbuf), bits);
333 output_ins(offset, (uint8_t *) q, lendis, outbuf);
334 q += lendis;
335 offset += lendis;
337 if (q >= buffer + INSN_MAX) {
338 uint8_t *r = (uint8_t *) buffer, *s = (uint8_t *) q;
339 int count = p - q;
340 while (count--)
341 *r++ = *s++;
342 p -= (q - buffer);
343 q = buffer;
345 } while (lenread > 0 || !(eof || feof(fp)));
347 if (fp != stdin)
348 fclose(fp);
350 return 0;
353 static void output_ins(uint32_t offset, uint8_t *data,
354 int datalen, char *insn)
356 int bytes;
357 fprintf(stdout, "%08"PRIX32" ", offset);
359 bytes = 0;
360 while (datalen > 0 && bytes < BPL) {
361 fprintf(stdout, "%02X", *data++);
362 bytes++;
363 datalen--;
366 fprintf(stdout, "%*s%s\n", (BPL + 1 - bytes) * 2, "", insn);
368 while (datalen > 0) {
369 fprintf(stdout, " -");
370 bytes = 0;
371 while (datalen > 0 && bytes < BPL) {
372 fprintf(stdout, "%02X", *data++);
373 bytes++;
374 datalen--;
376 fprintf(stdout, "\n");
381 * Skip a certain amount of data in a file, either by seeking if
382 * possible, or if that fails then by reading and discarding.
384 static void skip(uint32_t dist, FILE * fp)
386 char buffer[256]; /* should fit on most stacks :-) */
389 * Got to be careful with fseek: at least one fseek I've tried
390 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
391 * ftell... horrible but apparently necessary.
393 if (fseek(fp, dist + ftell(fp), SEEK_SET)) {
394 while (dist > 0) {
395 uint32_t len = (dist < sizeof(buffer) ?
396 dist : sizeof(buffer));
397 if (fread(buffer, 1, len, fp) < len) {
398 perror("fread");
399 exit(1);
401 dist -= len;