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 license given in the file "LICENSE"
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;
71 nasm_set_malloc_error(ndisasm_error
);
77 char *v
, *vv
, *p
= *++argv
;
78 if (*p
== '-' && p
[1]) {
81 switch (nasm_tolower(*p
)) {
82 case 'a': /* auto or intelligent sync */
88 fprintf(stderr
, help
);
93 "NDISASM version %s compiled on %s\n",
94 nasm_version
, nasm_date
);
96 case 'u': /* -u for -b 32, -uu for -b 64 */
102 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
104 fprintf(stderr
, "%s: `-b' requires an argument\n",
108 b
= strtoul(v
, &ep
, 10);
109 if (*ep
|| !(bits
== 16 || bits
== 32 || bits
== 64)) {
110 fprintf(stderr
, "%s: argument to `-b' should"
111 " be 16, 32 or 64\n", pname
);
115 p
= ""; /* force to next argument */
117 case 'o': /* origin */
118 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
120 fprintf(stderr
, "%s: `-o' requires an argument\n",
124 offset
= readnum(v
, &rn_error
);
127 "%s: `-o' requires a numeric argument\n",
131 p
= ""; /* force to next argument */
133 case 's': /* sync point */
134 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
136 fprintf(stderr
, "%s: `-s' requires an argument\n",
140 add_sync(readnum(v
, &rn_error
), 0L);
143 "%s: `-s' requires a numeric argument\n",
147 p
= ""; /* force to next argument */
149 case 'e': /* skip a header */
150 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
152 fprintf(stderr
, "%s: `-e' requires an argument\n",
156 initskip
= readnum(v
, &rn_error
);
159 "%s: `-e' requires a numeric argument\n",
163 p
= ""; /* force to next argument */
165 case 'k': /* skip a region */
166 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
168 fprintf(stderr
, "%s: `-k' requires an argument\n",
175 "%s: `-k' requires two numbers separated"
176 " by a comma\n", pname
);
180 nextsync
= readnum(v
, &rn_error
);
183 "%s: `-k' requires numeric arguments\n",
187 synclen
= readnum(vv
, &rn_error
);
190 "%s: `-k' requires numeric arguments\n",
194 add_sync(nextsync
, synclen
);
195 p
= ""; /* force to next argument */
197 case 'p': /* preferred vendor */
198 v
= p
[1] ? p
+ 1 : --argc
? *++argv
: NULL
;
200 fprintf(stderr
, "%s: `-p' requires an argument\n",
204 if (!strcmp(v
, "intel")) {
205 prefer
= 0; /* Default */
206 } else if (!strcmp(v
, "amd")) {
207 prefer
= IF_AMD
| IF_3DNOW
;
208 } else if (!strcmp(v
, "cyrix")) {
209 prefer
= IF_CYRIX
| IF_3DNOW
;
210 } else if (!strcmp(v
, "idt") || !strcmp(v
, "centaur")
211 || !strcmp(v
, "winchip")) {
215 "%s: unknown vendor `%s' specified with `-p'\n",
219 p
= ""; /* force to next argument */
222 fprintf(stderr
, "%s: unrecognised option `-%c'\n",
226 } else if (!filename
) {
229 fprintf(stderr
, "%s: more than one filename specified\n",
236 fprintf(stderr
, help
, pname
);
240 if (strcmp(filename
, "-")) {
241 fp
= fopen(filename
, "rb");
243 fprintf(stderr
, "%s: unable to open `%s': %s\n",
244 pname
, filename
, strerror(errno
));
254 * This main loop is really horrible, and wants rewriting with
255 * an axe. It'll stay the way it is for a while though, until I
260 nextsync
= next_sync(offset
, &synclen
);
262 uint32_t to_read
= buffer
+ sizeof(buffer
) - p
;
263 if ((nextsync
|| synclen
) &&
264 to_read
> nextsync
- offset
- (p
- q
))
265 to_read
= nextsync
- offset
- (p
- q
);
267 lenread
= fread(p
, 1, to_read
, fp
);
269 eof
= true; /* help along systems with bad feof */
273 if ((nextsync
|| synclen
) &&
274 (uint32_t)offset
== nextsync
) {
276 fprintf(stdout
, "%08"PRIX32
" skipping 0x%"PRIX32
" bytes\n",
282 nextsync
= next_sync(offset
, &synclen
);
284 while (p
> q
&& (p
- q
>= INSN_MAX
|| lenread
== 0)) {
286 disasm((uint8_t *) q
, outbuf
, sizeof(outbuf
), bits
,
287 offset
, autosync
, prefer
);
288 if (!lendis
|| lendis
> (p
- q
)
289 || ((nextsync
|| synclen
) &&
290 (uint32_t)lendis
> nextsync
- offset
))
291 lendis
= eatbyte((uint8_t *) q
, outbuf
, sizeof(outbuf
), bits
);
292 output_ins(offset
, (uint8_t *) q
, lendis
, outbuf
);
296 if (q
>= buffer
+ INSN_MAX
) {
297 uint8_t *r
= (uint8_t *) buffer
, *s
= (uint8_t *) q
;
304 } while (lenread
> 0 || !(eof
|| feof(fp
)));
312 static void output_ins(uint32_t offset
, uint8_t *data
,
313 int datalen
, char *insn
)
316 fprintf(stdout
, "%08"PRIX32
" ", offset
);
319 while (datalen
> 0 && bytes
< BPL
) {
320 fprintf(stdout
, "%02X", *data
++);
325 fprintf(stdout
, "%*s%s\n", (BPL
+ 1 - bytes
) * 2, "", insn
);
327 while (datalen
> 0) {
328 fprintf(stdout
, " -");
330 while (datalen
> 0 && bytes
< BPL
) {
331 fprintf(stdout
, "%02X", *data
++);
335 fprintf(stdout
, "\n");
340 * Skip a certain amount of data in a file, either by seeking if
341 * possible, or if that fails then by reading and discarding.
343 static void skip(uint32_t dist
, FILE * fp
)
345 char buffer
[256]; /* should fit on most stacks :-) */
348 * Got to be careful with fseek: at least one fseek I've tried
349 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
350 * ftell... horrible but apparently necessary.
352 if (fseek(fp
, dist
+ ftell(fp
), SEEK_SET
)) {
354 uint32_t len
= (dist
< sizeof(buffer
) ?
355 dist
: sizeof(buffer
));
356 if (fread(buffer
, 1, len
, fp
) < len
) {