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_verror(int severity, const char *fmt, va_list va)
73 vfprintf(stderr, fmt, va);
75 if (severity & ERR_FATAL)
79 int main(int argc, char **argv)
81 char buffer[INSN_MAX * 2], *p, *ep, *q;
84 char *filename = NULL;
85 uint32_t nextsync, synclen, initskip = 0L;
88 bool autosync = false;
97 nasm_set_verror(ndisasm_verror);
98 nasm_init_malloc_error();
99 iflag_clear_all(&prefer);
105 char *v, *vv, *p = *++argv;
106 if (*p == '-' && p[1]) {
109 switch (nasm_tolower(*p)) {
110 case 'a': /* auto or intelligent sync */
121 "NDISASM version %s compiled on %s\n",
122 nasm_version, nasm_date);
124 case 'u': /* -u for -b 32, -uu for -b 64 */
130 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
132 fprintf(stderr, "%s: `-b' requires an argument\n",
136 b = strtoul(v, &ep, 10);
137 if (*ep || !(bits == 16 || bits == 32 || bits == 64)) {
138 fprintf(stderr, "%s: argument to `-b' should"
139 " be 16, 32 or 64\n", pname);
143 p = ""; /* force to next argument */
145 case 'o': /* origin */
146 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
148 fprintf(stderr, "%s: `-o' requires an argument\n",
152 offset = readnum(v, &rn_error);
155 "%s: `-o' requires a numeric argument\n",
159 p = ""; /* force to next argument */
161 case 's': /* sync point */
162 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
164 fprintf(stderr, "%s: `-s' requires an argument\n",
168 add_sync(readnum(v, &rn_error), 0L);
171 "%s: `-s' requires a numeric argument\n",
175 p = ""; /* force to next argument */
177 case 'e': /* skip a header */
178 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
180 fprintf(stderr, "%s: `-e' requires an argument\n",
184 initskip = readnum(v, &rn_error);
187 "%s: `-e' requires a numeric argument\n",
191 p = ""; /* force to next argument */
193 case 'k': /* skip a region */
194 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
196 fprintf(stderr, "%s: `-k' requires an argument\n",
203 "%s: `-k' requires two numbers separated"
204 " by a comma\n", pname);
208 nextsync = readnum(v, &rn_error);
211 "%s: `-k' requires numeric arguments\n",
215 synclen = readnum(vv, &rn_error);
218 "%s: `-k' requires numeric arguments\n",
222 add_sync(nextsync, synclen);
223 p = ""; /* force to next argument */
225 case 'p': /* preferred vendor */
226 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
228 fprintf(stderr, "%s: `-p' requires an argument\n",
232 if (!strcmp(v, "intel")) {
233 iflag_clear_all(&prefer); /* default */
234 } else if (!strcmp(v, "amd")) {
235 iflag_clear_all(&prefer);
236 iflag_set(&prefer, IF_AMD);
237 iflag_set(&prefer, IF_3DNOW);
238 } else if (!strcmp(v, "cyrix")) {
239 iflag_clear_all(&prefer);
240 iflag_set(&prefer, IF_CYRIX);
241 iflag_set(&prefer, IF_3DNOW);
242 } else if (!strcmp(v, "idt") ||
243 !strcmp(v, "centaur") ||
244 !strcmp(v, "winchip")) {
245 iflag_clear_all(&prefer);
246 iflag_set(&prefer, IF_3DNOW);
249 "%s: unknown vendor `%s' specified with `-p'\n",
253 p = ""; /* force to next argument */
256 fprintf(stderr, "%s: unrecognised option `-%c'\n",
260 } else if (!filename) {
263 fprintf(stderr, "%s: more than one filename specified\n",
270 fprintf(stderr, help, pname);
274 if (strcmp(filename, "-")) {
275 fp = fopen(filename, "rb");
277 fprintf(stderr, "%s: unable to open `%s': %s\n",
278 pname, filename, strerror(errno));
288 * This main loop is really horrible, and wants rewriting with
289 * an axe. It'll stay the way it is for a while though, until I
294 nextsync = next_sync(offset, &synclen);
296 uint32_t to_read = buffer + sizeof(buffer) - p;
297 if ((nextsync || synclen) &&
298 to_read > nextsync - offset - (p - q))
299 to_read = nextsync - offset - (p - q);
301 lenread = fread(p, 1, to_read, fp);
303 eof = true; /* help along systems with bad feof */
307 if ((nextsync || synclen) &&
308 (uint32_t)offset == nextsync) {
310 fprintf(stdout, "%08"PRIX32" skipping 0x%"PRIX32" bytes\n",
316 nextsync = next_sync(offset, &synclen);
318 while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
320 disasm((uint8_t *) q, outbuf, sizeof(outbuf), bits,
321 offset, autosync, &prefer);
322 if (!lendis || lendis > (p - q)
323 || ((nextsync || synclen) &&
324 (uint32_t)lendis > nextsync - offset))
325 lendis = eatbyte((uint8_t *) q, outbuf, sizeof(outbuf), bits);
326 output_ins(offset, (uint8_t *) q, lendis, outbuf);
330 if (q >= buffer + INSN_MAX) {
331 uint8_t *r = (uint8_t *) buffer, *s = (uint8_t *) q;
338 } while (lenread > 0 || !(eof || feof(fp)));
346 static void output_ins(uint32_t offset, uint8_t *data,
347 int datalen, char *insn)
350 fprintf(stdout, "%08"PRIX32" ", offset);
353 while (datalen > 0 && bytes < BPL) {
354 fprintf(stdout, "%02X", *data++);
359 fprintf(stdout, "%*s%s\n", (BPL + 1 - bytes) * 2, "", insn);
361 while (datalen > 0) {
362 fprintf(stdout, " -");
364 while (datalen > 0 && bytes < BPL) {
365 fprintf(stdout, "%02X", *data++);
369 fprintf(stdout, "\n");
374 * Skip a certain amount of data in a file, either by seeking if
375 * possible, or if that fails then by reading and discarding.
377 static void skip(uint32_t dist, FILE * fp)
379 char buffer[256]; /* should fit on most stacks :-) */
382 * Got to be careful with fseek: at least one fseek I've tried
383 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
384 * ftell... horrible but apparently necessary.
386 if (fseek(fp, dist + ftell(fp), SEEK_SET)) {
388 uint32_t len = (dist < sizeof(buffer) ?
389 dist : sizeof(buffer));
390 if (fread(buffer, 1, len, fp) < len) {