1 /* ----------------------------------------------------------------------- *
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
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
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
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"
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
, ...)
88 vfprintf(stderr
, fmt
, va
);
90 if (severity
& ERR_FATAL
)
94 int main(int argc
, char **argv
)
96 char buffer
[INSN_MAX
* 2], *p
, *ep
, *q
;
99 char *filename
= NULL
;
100 uint32_t nextsync
, synclen
, initskip
= 0L;
103 bool autosync
= false;
112 nasm_set_malloc_error(ndisasm_error
);
118 char *v
, *vv
, *p
= *++argv
;
119 if (*p
== '-' && p
[1]) {
122 switch (nasm_tolower(*p
)) {
123 case 'a': /* auto or intelligent sync */
129 fprintf(stderr
, help
);
134 "NDISASM version %s compiled on %s\n",
135 nasm_version
, nasm_date
);
137 case 'u': /* -u for -b 32, -uu for -b 64 */
143 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
145 fprintf(stderr
, "%s: `-b' requires an argument\n",
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
);
156 p
= ""; /* force to next argument */
158 case 'o': /* origin */
159 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
161 fprintf(stderr
, "%s: `-o' requires an argument\n",
165 offset
= readnum(v
, &rn_error
);
168 "%s: `-o' requires a numeric argument\n",
172 p
= ""; /* force to next argument */
174 case 's': /* sync point */
175 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
177 fprintf(stderr
, "%s: `-s' requires an argument\n",
181 add_sync(readnum(v
, &rn_error
), 0L);
184 "%s: `-s' requires a numeric argument\n",
188 p
= ""; /* force to next argument */
190 case 'e': /* skip a header */
191 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
193 fprintf(stderr
, "%s: `-e' requires an argument\n",
197 initskip
= readnum(v
, &rn_error
);
200 "%s: `-e' requires a numeric argument\n",
204 p
= ""; /* force to next argument */
206 case 'k': /* skip a region */
207 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
209 fprintf(stderr
, "%s: `-k' requires an argument\n",
216 "%s: `-k' requires two numbers separated"
217 " by a comma\n", pname
);
221 nextsync
= readnum(v
, &rn_error
);
224 "%s: `-k' requires numeric arguments\n",
228 synclen
= readnum(vv
, &rn_error
);
231 "%s: `-k' requires numeric arguments\n",
235 add_sync(nextsync
, synclen
);
236 p
= ""; /* force to next argument */
238 case 'p': /* preferred vendor */
239 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
241 fprintf(stderr
, "%s: `-p' requires an argument\n",
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")) {
256 "%s: unknown vendor `%s' specified with `-p'\n",
260 p
= ""; /* force to next argument */
263 fprintf(stderr
, "%s: unrecognised option `-%c'\n",
267 } else if (!filename
) {
270 fprintf(stderr
, "%s: more than one filename specified\n",
277 fprintf(stderr
, help
, pname
);
281 if (strcmp(filename
, "-")) {
282 fp
= fopen(filename
, "rb");
284 fprintf(stderr
, "%s: unable to open `%s': %s\n",
285 pname
, filename
, strerror(errno
));
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
301 nextsync
= next_sync(offset
, &synclen
);
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
);
308 lenread
= fread(p
, 1, to_read
, fp
);
310 eof
= true; /* help along systems with bad feof */
314 if ((nextsync
|| synclen
) &&
315 (uint32_t)offset
== nextsync
) {
317 fprintf(stdout
, "%08"PRIX32
" skipping 0x%"PRIX32
" bytes\n",
323 nextsync
= next_sync(offset
, &synclen
);
325 while (p
> q
&& (p
- q
>= INSN_MAX
|| lenread
== 0)) {
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
);
337 if (q
>= buffer
+ INSN_MAX
) {
338 uint8_t *r
= (uint8_t *) buffer
, *s
= (uint8_t *) q
;
345 } while (lenread
> 0 || !(eof
|| feof(fp
)));
353 static void output_ins(uint32_t offset
, uint8_t *data
,
354 int datalen
, char *insn
)
357 fprintf(stdout
, "%08"PRIX32
" ", offset
);
360 while (datalen
> 0 && bytes
< BPL
) {
361 fprintf(stdout
, "%02X", *data
++);
366 fprintf(stdout
, "%*s%s\n", (BPL
+ 1 - bytes
) * 2, "", insn
);
368 while (datalen
> 0) {
369 fprintf(stdout
, " -");
371 while (datalen
> 0 && bytes
< BPL
) {
372 fprintf(stdout
, "%02X", *data
++);
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
)) {
395 uint32_t len
= (dist
< sizeof(buffer
) ?
396 dist
: sizeof(buffer
));
397 if (fread(buffer
, 1, len
, fp
) < len
) {