1 /* ndisasm.c the Netwide Disassembler main module
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
25 #define BPL 8 /* bytes per line of hex dump */
27 static const char *help
=
28 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
29 " [-e bytes] [-k start,bytes] [-p vendor] file\n"
30 " -a or -i activates auto (intelligent) sync\n"
32 " -b 16, -b 32 or -b 64 sets the processor mode\n"
33 " -h displays this text\n"
34 " -r or -v displays the version number\n"
35 " -e skips <bytes> bytes of header\n"
36 " -k avoids disassembling <bytes> bytes from position <start>\n"
37 " -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)\n";
39 static void output_ins(uint32_t, uint8_t *, int, char *);
40 static void skip(uint32_t dist
, FILE * fp
);
42 static void ndisasm_error(int severity
, const char *fmt
, ...)
47 vfprintf(stderr
, fmt
, va
);
49 if (severity
& ERR_FATAL
)
53 int main(int argc
, char **argv
)
55 char buffer
[INSN_MAX
* 2], *p
, *ep
, *q
;
58 char *filename
= NULL
;
59 uint32_t nextsync
, synclen
, initskip
= 0L;
62 bool autosync
= false;
70 nasm_set_malloc_error(ndisasm_error
);
76 char *v
, *vv
, *p
= *++argv
;
77 if (*p
== '-' && p
[1]) {
80 switch (tolower(*p
)) {
81 case 'a': /* auto or intelligent sync */
87 fprintf(stderr
, help
);
92 "NDISASM version %s compiled " __DATE__
"\n",
95 case 'u': /* -u for -b 32, -uu for -b 64 */
101 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
103 fprintf(stderr
, "%s: `-b' requires an argument\n",
107 b
= strtoul(v
, &ep
, 10);
108 if (*ep
|| !(bits
== 16 || bits
== 32 || bits
== 64)) {
109 fprintf(stderr
, "%s: argument to `-b' should"
110 " be 16, 32 or 64\n", pname
);
114 p
= ""; /* force to next argument */
116 case 'o': /* origin */
117 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
119 fprintf(stderr
, "%s: `-o' requires an argument\n",
123 offset
= readnum(v
, &rn_error
);
126 "%s: `-o' requires a numeric argument\n",
130 p
= ""; /* force to next argument */
132 case 's': /* sync point */
133 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
135 fprintf(stderr
, "%s: `-s' requires an argument\n",
139 add_sync(readnum(v
, &rn_error
), 0L);
142 "%s: `-s' requires a numeric argument\n",
146 p
= ""; /* force to next argument */
148 case 'e': /* skip a header */
149 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
151 fprintf(stderr
, "%s: `-e' requires an argument\n",
155 initskip
= readnum(v
, &rn_error
);
158 "%s: `-e' requires a numeric argument\n",
162 p
= ""; /* force to next argument */
164 case 'k': /* skip a region */
165 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
167 fprintf(stderr
, "%s: `-k' requires an argument\n",
174 "%s: `-k' requires two numbers separated"
175 " by a comma\n", pname
);
179 nextsync
= readnum(v
, &rn_error
);
182 "%s: `-k' requires numeric arguments\n",
186 synclen
= readnum(vv
, &rn_error
);
189 "%s: `-k' requires numeric arguments\n",
193 add_sync(nextsync
, synclen
);
194 p
= ""; /* force to next argument */
196 case 'p': /* preferred vendor */
197 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
199 fprintf(stderr
, "%s: `-p' requires an argument\n",
203 if (!strcmp(v
, "intel")) {
204 prefer
= 0; /* Default */
205 } else if (!strcmp(v
, "amd")) {
206 prefer
= IF_AMD
| IF_3DNOW
;
207 } else if (!strcmp(v
, "cyrix")) {
208 prefer
= IF_CYRIX
| IF_3DNOW
;
209 } else if (!strcmp(v
, "idt") || !strcmp(v
, "centaur")
210 || !strcmp(v
, "winchip")) {
214 "%s: unknown vendor `%s' specified with `-p'\n",
218 p
= ""; /* force to next argument */
221 fprintf(stderr
, "%s: unrecognised option `-%c'\n",
225 } else if (!filename
) {
228 fprintf(stderr
, "%s: more than one filename specified\n",
235 fprintf(stderr
, help
, pname
);
239 if (strcmp(filename
, "-")) {
240 fp
= fopen(filename
, "rb");
242 fprintf(stderr
, "%s: unable to open `%s': %s\n",
243 pname
, filename
, strerror(errno
));
253 * This main loop is really horrible, and wants rewriting with
254 * an axe. It'll stay the way it is for a while though, until I
259 nextsync
= next_sync(offset
, &synclen
);
261 uint32_t to_read
= buffer
+ sizeof(buffer
) - p
;
262 if (to_read
> nextsync
- offset
- (p
- q
))
263 to_read
= nextsync
- offset
- (p
- q
);
265 lenread
= fread(p
, 1, to_read
, fp
);
267 eof
= true; /* help along systems with bad feof */
271 if ((uint32_t)offset
== nextsync
) {
273 fprintf(stdout
, "%08"PRIX32
" skipping 0x%"PRIX32
" bytes\n", offset
, synclen
);
278 nextsync
= next_sync(offset
, &synclen
);
280 while (p
> q
&& (p
- q
>= INSN_MAX
|| lenread
== 0)) {
282 disasm(q
, outbuf
, sizeof(outbuf
), bits
, offset
, autosync
,
284 if (!lendis
|| lendis
> (p
- q
)
285 || (uint32_t)lendis
> nextsync
- offset
)
286 lendis
= eatbyte(q
, outbuf
, sizeof(outbuf
));
287 output_ins(offset
, q
, lendis
, outbuf
);
291 if (q
>= buffer
+ INSN_MAX
) {
292 uint8_t *r
= buffer
, *s
= q
;
299 } while (lenread
> 0 || !(eof
|| feof(fp
)));
307 static void output_ins(uint32_t offset
, uint8_t *data
,
308 int datalen
, char *insn
)
311 fprintf(stdout
, "%08"PRIX32
" ", offset
);
314 while (datalen
> 0 && bytes
< BPL
) {
315 fprintf(stdout
, "%02X", *data
++);
320 fprintf(stdout
, "%*s%s\n", (BPL
+ 1 - bytes
) * 2, "", insn
);
322 while (datalen
> 0) {
323 fprintf(stdout
, " -");
325 while (datalen
> 0 && bytes
< BPL
) {
326 fprintf(stdout
, "%02X", *data
++);
330 fprintf(stdout
, "\n");
335 * Skip a certain amount of data in a file, either by seeking if
336 * possible, or if that fails then by reading and discarding.
338 static void skip(uint32_t dist
, FILE * fp
)
340 char buffer
[256]; /* should fit on most stacks :-) */
343 * Got to be careful with fseek: at least one fseek I've tried
344 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
345 * ftell... horrible but apparently necessary.
347 if (fseek(fp
, dist
+ ftell(fp
), SEEK_SET
)) {
349 uint32_t len
= (dist
< sizeof(buffer
) ?
350 dist
: sizeof(buffer
));
351 if (fread(buffer
, 1, len
, fp
) < len
) {