iflags: Add IF_EVEX for checking {evex} availability
[platform/upstream/nasm.git] / ndisasm.c
1 /* ----------------------------------------------------------------------- *
2  *   
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.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following
9  *   conditions are met:
10  *
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.
17  *     
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.
31  *
32  * ----------------------------------------------------------------------- */
33
34 /*
35  * ndisasm.c   the Netwide Disassembler main module
36  */
37
38 #include "compiler.h"
39
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <errno.h>
46 #include <inttypes.h>
47
48 #include "insns.h"
49 #include "nasm.h"
50 #include "nasmlib.h"
51 #include "sync.h"
52 #include "disasm.h"
53
54 #define BPL 8                   /* bytes per line of hex dump */
55
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"
60     "   -u same as -b 32\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";
67
68 static void output_ins(uint32_t, uint8_t *, int, char *);
69 static void skip(uint32_t dist, FILE * fp);
70
71 static void ndisasm_verror(int severity, const char *fmt, va_list va)
72 {
73     vfprintf(stderr, fmt, va);
74
75     if (severity & ERR_FATAL)
76         exit(1);
77 }
78
79 int main(int argc, char **argv)
80 {
81     char buffer[INSN_MAX * 2], *p, *ep, *q;
82     char outbuf[256];
83     char *pname = *argv;
84     char *filename = NULL;
85     uint32_t nextsync, synclen, initskip = 0L;
86     int lenread;
87     int32_t lendis;
88     bool autosync = false;
89     int bits = 16, b;
90     bool eof = false;
91     iflag_t prefer;
92     bool rn_error;
93     int32_t offset;
94     FILE *fp;
95
96     tolower_init();
97     nasm_set_verror(ndisasm_verror);
98     nasm_init_malloc_error();
99     iflag_clear_all(&prefer);
100
101     offset = 0;
102     init_sync();
103
104     while (--argc) {
105         char *v, *vv, *p = *++argv;
106         if (*p == '-' && p[1]) {
107             p++;
108             while (*p)
109                 switch (nasm_tolower(*p)) {
110                 case 'a':      /* auto or intelligent sync */
111                 case 'i':
112                     autosync = true;
113                     p++;
114                     break;
115                 case 'h':
116                     fputs(help, stderr);
117                     return 0;
118                 case 'r':
119                 case 'v':
120                     fprintf(stderr,
121                             "NDISASM version %s compiled on %s\n",
122                             nasm_version, nasm_date);
123                     return 0;
124                 case 'u':       /* -u for -b 32, -uu for -b 64 */
125                     if (bits < 64)
126                         bits <<= 1;
127                     p++;
128                     break;
129                 case 'b':      /* bits */
130                     v = p[1] ? p + 1 : --argc ? *++argv : NULL;
131                     if (!v) {
132                         fprintf(stderr, "%s: `-b' requires an argument\n",
133                                 pname);
134                         return 1;
135                     }
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);
140                     } else {
141                         bits = b;
142                     }
143                     p = "";     /* force to next argument */
144                     break;
145                 case 'o':      /* origin */
146                     v = p[1] ? p + 1 : --argc ? *++argv : NULL;
147                     if (!v) {
148                         fprintf(stderr, "%s: `-o' requires an argument\n",
149                                 pname);
150                         return 1;
151                     }
152                     offset = readnum(v, &rn_error);
153                     if (rn_error) {
154                         fprintf(stderr,
155                                 "%s: `-o' requires a numeric argument\n",
156                                 pname);
157                         return 1;
158                     }
159                     p = "";     /* force to next argument */
160                     break;
161                 case 's':      /* sync point */
162                     v = p[1] ? p + 1 : --argc ? *++argv : NULL;
163                     if (!v) {
164                         fprintf(stderr, "%s: `-s' requires an argument\n",
165                                 pname);
166                         return 1;
167                     }
168                     add_sync(readnum(v, &rn_error), 0L);
169                     if (rn_error) {
170                         fprintf(stderr,
171                                 "%s: `-s' requires a numeric argument\n",
172                                 pname);
173                         return 1;
174                     }
175                     p = "";     /* force to next argument */
176                     break;
177                 case 'e':      /* skip a header */
178                     v = p[1] ? p + 1 : --argc ? *++argv : NULL;
179                     if (!v) {
180                         fprintf(stderr, "%s: `-e' requires an argument\n",
181                                 pname);
182                         return 1;
183                     }
184                     initskip = readnum(v, &rn_error);
185                     if (rn_error) {
186                         fprintf(stderr,
187                                 "%s: `-e' requires a numeric argument\n",
188                                 pname);
189                         return 1;
190                     }
191                     p = "";     /* force to next argument */
192                     break;
193                 case 'k':      /* skip a region */
194                     v = p[1] ? p + 1 : --argc ? *++argv : NULL;
195                     if (!v) {
196                         fprintf(stderr, "%s: `-k' requires an argument\n",
197                                 pname);
198                         return 1;
199                     }
200                     vv = strchr(v, ',');
201                     if (!vv) {
202                         fprintf(stderr,
203                                 "%s: `-k' requires two numbers separated"
204                                 " by a comma\n", pname);
205                         return 1;
206                     }
207                     *vv++ = '\0';
208                     nextsync = readnum(v, &rn_error);
209                     if (rn_error) {
210                         fprintf(stderr,
211                                 "%s: `-k' requires numeric arguments\n",
212                                 pname);
213                         return 1;
214                     }
215                     synclen = readnum(vv, &rn_error);
216                     if (rn_error) {
217                         fprintf(stderr,
218                                 "%s: `-k' requires numeric arguments\n",
219                                 pname);
220                         return 1;
221                     }
222                     add_sync(nextsync, synclen);
223                     p = "";     /* force to next argument */
224                     break;
225                 case 'p':      /* preferred vendor */
226                     v = p[1] ? p + 1 : --argc ? *++argv : NULL;
227                     if (!v) {
228                         fprintf(stderr, "%s: `-p' requires an argument\n",
229                                 pname);
230                         return 1;
231                     }
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);
247                     } else {
248                         fprintf(stderr,
249                                 "%s: unknown vendor `%s' specified with `-p'\n",
250                                 pname, v);
251                         return 1;
252                     }
253                     p = "";     /* force to next argument */
254                     break;
255                 default:       /*bf */
256                     fprintf(stderr, "%s: unrecognised option `-%c'\n",
257                             pname, *p);
258                     return 1;
259                 }
260         } else if (!filename) {
261             filename = p;
262         } else {
263             fprintf(stderr, "%s: more than one filename specified\n",
264                     pname);
265             return 1;
266         }
267     }
268
269     if (!filename) {
270         fprintf(stderr, help, pname);
271         return 0;
272     }
273
274     if (strcmp(filename, "-")) {
275         fp = fopen(filename, "rb");
276         if (!fp) {
277             fprintf(stderr, "%s: unable to open `%s': %s\n",
278                     pname, filename, strerror(errno));
279             return 1;
280         }
281     } else
282         fp = stdin;
283
284     if (initskip > 0)
285         skip(initskip, fp);
286
287     /*
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
290      * find the energy...
291      */
292
293     p = q = buffer;
294     nextsync = next_sync(offset, &synclen);
295     do {
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);
300         if (to_read) {
301             lenread = fread(p, 1, to_read, fp);
302             if (lenread == 0)
303                 eof = true;     /* help along systems with bad feof */
304         } else
305             lenread = 0;
306         p += lenread;
307         if ((nextsync || synclen) &&
308             (uint32_t)offset == nextsync) {
309             if (synclen) {
310                 fprintf(stdout, "%08"PRIX32"  skipping 0x%"PRIX32" bytes\n",
311                         offset, synclen);
312                 offset += synclen;
313                 skip(synclen, fp);
314             }
315             p = q = buffer;
316             nextsync = next_sync(offset, &synclen);
317         }
318         while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
319             lendis =
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);
327             q += lendis;
328             offset += lendis;
329         }
330         if (q >= buffer + INSN_MAX) {
331             uint8_t *r = (uint8_t *) buffer, *s = (uint8_t *) q;
332             int count = p - q;
333             while (count--)
334                 *r++ = *s++;
335             p -= (q - buffer);
336             q = buffer;
337         }
338     } while (lenread > 0 || !(eof || feof(fp)));
339
340     if (fp != stdin)
341         fclose(fp);
342
343     return 0;
344 }
345
346 static void output_ins(uint32_t offset, uint8_t *data,
347                        int datalen, char *insn)
348 {
349     int bytes;
350     fprintf(stdout, "%08"PRIX32"  ", offset);
351
352     bytes = 0;
353     while (datalen > 0 && bytes < BPL) {
354         fprintf(stdout, "%02X", *data++);
355         bytes++;
356         datalen--;
357     }
358
359     fprintf(stdout, "%*s%s\n", (BPL + 1 - bytes) * 2, "", insn);
360
361     while (datalen > 0) {
362         fprintf(stdout, "         -");
363         bytes = 0;
364         while (datalen > 0 && bytes < BPL) {
365             fprintf(stdout, "%02X", *data++);
366             bytes++;
367             datalen--;
368         }
369         fprintf(stdout, "\n");
370     }
371 }
372
373 /*
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.
376  */
377 static void skip(uint32_t dist, FILE * fp)
378 {
379     char buffer[256];           /* should fit on most stacks :-) */
380
381     /*
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.
385      */
386     if (fseek(fp, dist + ftell(fp), SEEK_SET)) {
387         while (dist > 0) {
388             uint32_t len = (dist < sizeof(buffer) ?
389                                  dist : sizeof(buffer));
390             if (fread(buffer, 1, len, fp) < len) {
391                 perror("fread");
392                 exit(1);
393             }
394             dist -= len;
395         }
396     }
397 }