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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
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
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"
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_error(int severity
, const char *fmt
, ...)
76 vfprintf(stderr
, fmt
, va
);
78 if (severity
& ERR_FATAL
)
82 int main(int argc
, char **argv
)
84 char buffer
[INSN_MAX
* 2], *p
, *ep
, *q
;
87 char *filename
= NULL
;
88 uint32_t nextsync
, synclen
, initskip
= 0L;
91 bool autosync
= false;
100 nasm_set_malloc_error(ndisasm_error
);
106 char *v
, *vv
, *p
= *++argv
;
107 if (*p
== '-' && p
[1]) {
110 switch (nasm_tolower(*p
)) {
111 case 'a': /* auto or intelligent sync */
117 fprintf(stderr
, help
);
122 "NDISASM version %s compiled on %s\n",
123 nasm_version
, nasm_date
);
125 case 'u': /* -u for -b 32, -uu for -b 64 */
131 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
133 fprintf(stderr
, "%s: `-b' requires an argument\n",
137 b
= strtoul(v
, &ep
, 10);
138 if (*ep
|| !(bits
== 16 || bits
== 32 || bits
== 64)) {
139 fprintf(stderr
, "%s: argument to `-b' should"
140 " be 16, 32 or 64\n", pname
);
144 p
= ""; /* force to next argument */
146 case 'o': /* origin */
147 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
149 fprintf(stderr
, "%s: `-o' requires an argument\n",
153 offset
= readnum(v
, &rn_error
);
156 "%s: `-o' requires a numeric argument\n",
160 p
= ""; /* force to next argument */
162 case 's': /* sync point */
163 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
165 fprintf(stderr
, "%s: `-s' requires an argument\n",
169 add_sync(readnum(v
, &rn_error
), 0L);
172 "%s: `-s' requires a numeric argument\n",
176 p
= ""; /* force to next argument */
178 case 'e': /* skip a header */
179 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
181 fprintf(stderr
, "%s: `-e' requires an argument\n",
185 initskip
= readnum(v
, &rn_error
);
188 "%s: `-e' requires a numeric argument\n",
192 p
= ""; /* force to next argument */
194 case 'k': /* skip a region */
195 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
197 fprintf(stderr
, "%s: `-k' requires an argument\n",
204 "%s: `-k' requires two numbers separated"
205 " by a comma\n", pname
);
209 nextsync
= readnum(v
, &rn_error
);
212 "%s: `-k' requires numeric arguments\n",
216 synclen
= readnum(vv
, &rn_error
);
219 "%s: `-k' requires numeric arguments\n",
223 add_sync(nextsync
, synclen
);
224 p
= ""; /* force to next argument */
226 case 'p': /* preferred vendor */
227 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
229 fprintf(stderr
, "%s: `-p' requires an argument\n",
233 if (!strcmp(v
, "intel")) {
234 prefer
= 0; /* Default */
235 } else if (!strcmp(v
, "amd")) {
236 prefer
= IF_AMD
| IF_3DNOW
;
237 } else if (!strcmp(v
, "cyrix")) {
238 prefer
= IF_CYRIX
| IF_3DNOW
;
239 } else if (!strcmp(v
, "idt") || !strcmp(v
, "centaur")
240 || !strcmp(v
, "winchip")) {
244 "%s: unknown vendor `%s' specified with `-p'\n",
248 p
= ""; /* force to next argument */
251 fprintf(stderr
, "%s: unrecognised option `-%c'\n",
255 } else if (!filename
) {
258 fprintf(stderr
, "%s: more than one filename specified\n",
265 fprintf(stderr
, help
, pname
);
269 if (strcmp(filename
, "-")) {
270 fp
= fopen(filename
, "rb");
272 fprintf(stderr
, "%s: unable to open `%s': %s\n",
273 pname
, filename
, strerror(errno
));
283 * This main loop is really horrible, and wants rewriting with
284 * an axe. It'll stay the way it is for a while though, until I
289 nextsync
= next_sync(offset
, &synclen
);
291 uint32_t to_read
= buffer
+ sizeof(buffer
) - p
;
292 if ((nextsync
|| synclen
) &&
293 to_read
> nextsync
- offset
- (p
- q
))
294 to_read
= nextsync
- offset
- (p
- q
);
296 lenread
= fread(p
, 1, to_read
, fp
);
298 eof
= true; /* help along systems with bad feof */
302 if ((nextsync
|| synclen
) &&
303 (uint32_t)offset
== nextsync
) {
305 fprintf(stdout
, "%08"PRIX32
" skipping 0x%"PRIX32
" bytes\n",
311 nextsync
= next_sync(offset
, &synclen
);
313 while (p
> q
&& (p
- q
>= INSN_MAX
|| lenread
== 0)) {
315 disasm((uint8_t *) q
, outbuf
, sizeof(outbuf
), bits
,
316 offset
, autosync
, prefer
);
317 if (!lendis
|| lendis
> (p
- q
)
318 || ((nextsync
|| synclen
) &&
319 (uint32_t)lendis
> nextsync
- offset
))
320 lendis
= eatbyte((uint8_t *) q
, outbuf
, sizeof(outbuf
), bits
);
321 output_ins(offset
, (uint8_t *) q
, lendis
, outbuf
);
325 if (q
>= buffer
+ INSN_MAX
) {
326 uint8_t *r
= (uint8_t *) buffer
, *s
= (uint8_t *) q
;
333 } while (lenread
> 0 || !(eof
|| feof(fp
)));
341 static void output_ins(uint32_t offset
, uint8_t *data
,
342 int datalen
, char *insn
)
345 fprintf(stdout
, "%08"PRIX32
" ", offset
);
348 while (datalen
> 0 && bytes
< BPL
) {
349 fprintf(stdout
, "%02X", *data
++);
354 fprintf(stdout
, "%*s%s\n", (BPL
+ 1 - bytes
) * 2, "", insn
);
356 while (datalen
> 0) {
357 fprintf(stdout
, " -");
359 while (datalen
> 0 && bytes
< BPL
) {
360 fprintf(stdout
, "%02X", *data
++);
364 fprintf(stdout
, "\n");
369 * Skip a certain amount of data in a file, either by seeking if
370 * possible, or if that fails then by reading and discarding.
372 static void skip(uint32_t dist
, FILE * fp
)
374 char buffer
[256]; /* should fit on most stacks :-) */
377 * Got to be careful with fseek: at least one fseek I've tried
378 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
379 * ftell... horrible but apparently necessary.
381 if (fseek(fp
, dist
+ ftell(fp
), SEEK_SET
)) {
383 uint32_t len
= (dist
< sizeof(buffer
) ?
384 dist
: sizeof(buffer
));
385 if (fread(buffer
, 1, len
, fp
) < len
) {