NASM 0.96
[nasm/avx512.git] / ndisasm.c
blobffe6c129b419f136d34de62190aa83e5bdbc58a7
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.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <errno.h>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "sync.h"
18 #include "disasm.h"
20 #define BPL 8 /* bytes per line of hex dump */
22 static const char *help =
23 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
24 " [-e bytes] [-k start,bytes] file\n"
25 " -a or -i activates auto (intelligent) sync\n"
26 " -u sets USE32 (32-bit mode)\n"
27 " -b 16 or -b 32 sets number of bits too\n"
28 " -h displays this text\n"
29 " -r displays the version number\n"
30 " -e skips <bytes> bytes of header\n"
31 " -k avoids disassembling <bytes> bytes from position <start>\n";
33 static void output_ins (unsigned long, unsigned char *, int, char *);
34 static void skip (unsigned long dist, FILE *fp);
36 int main(int argc, char **argv) {
37 unsigned char buffer[INSN_MAX * 2], *p, *q;
38 char outbuf[256];
39 char *pname = *argv;
40 char *filename = NULL;
41 unsigned long nextsync, synclen, initskip = 0L;
42 int lenread, lendis;
43 int autosync = FALSE;
44 int bits = 16;
45 int rn_error;
46 long offset;
47 FILE *fp;
49 offset = 0;
50 init_sync();
52 while (--argc) {
53 char *v, *vv, *p = *++argv;
54 if (*p == '-') {
55 p++;
56 while (*p) switch (tolower(*p)) {
57 case 'a': /* auto or intelligent sync */
58 case 'i':
59 autosync = TRUE;
60 p++;
61 break;
62 case 'h':
63 fprintf(stderr, help);
64 return 0;
65 case 'r':
66 fprintf(stderr, "NDISASM version " NASM_VER "\n");
67 return 0;
68 case 'u': /* USE32 */
69 bits = 32;
70 p++;
71 break;
72 case 'b': /* bits */
73 v = p[1] ? p+1 : --argc ? *++argv : NULL;
74 if (!v) {
75 fprintf(stderr, "%s: `-b' requires an argument\n", pname);
76 return 1;
78 if (!strcmp(v, "16"))
79 bits = 16;
80 else if (!strcmp(v, "32"))
81 bits = 32;
82 else {
83 fprintf(stderr, "%s: argument to `-b' should"
84 " be `16' or `32'\n", pname);
86 p = ""; /* force to next argument */
87 break;
88 case 'o': /* origin */
89 v = p[1] ? p+1 : --argc ? *++argv : NULL;
90 if (!v) {
91 fprintf(stderr, "%s: `-o' requires an argument\n", pname);
92 return 1;
94 offset = readnum (v, &rn_error);
95 if (rn_error) {
96 fprintf(stderr, "%s: `-o' requires a numeric argument\n",
97 pname);
98 return 1;
100 p = ""; /* force to next argument */
101 break;
102 case 's': /* sync point */
103 v = p[1] ? p+1 : --argc ? *++argv : NULL;
104 if (!v) {
105 fprintf(stderr, "%s: `-s' requires an argument\n", pname);
106 return 1;
108 add_sync (readnum (v, &rn_error), 0L);
109 if (rn_error) {
110 fprintf(stderr, "%s: `-s' requires a numeric argument\n",
111 pname);
112 return 1;
114 p = ""; /* force to next argument */
115 break;
116 case 'e': /* skip a header */
117 v = p[1] ? p+1 : --argc ? *++argv : NULL;
118 if (!v) {
119 fprintf(stderr, "%s: `-e' requires an argument\n", pname);
120 return 1;
122 initskip = readnum (v, &rn_error);
123 if (rn_error) {
124 fprintf(stderr, "%s: `-e' requires a numeric argument\n",
125 pname);
126 return 1;
128 p = ""; /* force to next argument */
129 break;
130 case 'k': /* skip a region */
131 v = p[1] ? p+1 : --argc ? *++argv : NULL;
132 if (!v) {
133 fprintf(stderr, "%s: `-k' requires an argument\n", pname);
134 return 1;
136 vv = strchr(v, ',');
137 if (!vv) {
138 fprintf(stderr, "%s: `-k' requires two numbers separated"
139 " by a comma\n", pname);
140 return 1;
142 *vv++ = '\0';
143 nextsync = readnum (v, &rn_error);
144 if (rn_error) {
145 fprintf(stderr, "%s: `-k' requires numeric arguments\n",
146 pname);
147 return 1;
149 synclen = readnum (vv, &rn_error);
150 if (rn_error) {
151 fprintf(stderr, "%s: `-k' requires numeric arguments\n",
152 pname);
153 return 1;
155 add_sync (nextsync, synclen);
156 p = ""; /* force to next argument */
157 break;
159 } else if (!filename) {
160 filename = p;
161 } else {
162 fprintf(stderr, "%s: more than one filename specified\n", pname);
163 return 1;
167 if (!filename) {
168 fprintf(stderr, help, pname);
169 return 0;
172 fp = fopen(filename, "rb");
173 if (!fp) {
174 fprintf(stderr, "%s: unable to open `%s': %s\n",
175 pname, filename, strerror(errno));
176 return 1;
178 if (initskip > 0)
179 skip (initskip, fp);
182 * This main loop is really horrible, and wants rewriting with
183 * an axe. It'll stay the way it is for a while though, until I
184 * find the energy...
187 p = q = buffer;
188 nextsync = next_sync (offset, &synclen);
189 do {
190 unsigned long to_read = buffer+sizeof(buffer)-p;
191 if (to_read > nextsync-offset-(p-q))
192 to_read = nextsync-offset-(p-q);
193 lenread = fread (p, 1, to_read, fp);
194 p += lenread;
195 if (offset == nextsync) {
196 if (synclen) {
197 printf("%08lX skipping 0x%lX bytes\n", offset, synclen);
198 offset += synclen;
199 skip (synclen, fp);
201 p = q = buffer;
202 nextsync = next_sync (offset, &synclen);
204 while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
205 lendis = disasm (q, outbuf, bits, offset, autosync);
206 if (!lendis || lendis > (p - q) ||
207 lendis > nextsync-offset)
208 lendis = eatbyte (q, outbuf);
209 output_ins (offset, q, lendis, outbuf);
210 q += lendis;
211 offset += lendis;
213 if (q >= buffer+INSN_MAX) {
214 unsigned char *r = buffer, *s = q;
215 int count = p - q;
216 while (count--)
217 *r++ = *s++;
218 p -= (q - buffer);
219 q = buffer;
221 } while (lenread > 0 || !feof(fp));
222 fclose (fp);
223 return 0;
226 static void output_ins (unsigned long offset, unsigned char *data,
227 int datalen, char *insn) {
228 int bytes;
229 printf("%08lX ", offset);
231 bytes = 0;
232 while (datalen > 0 && bytes < BPL) {
233 printf("%02X", *data++);
234 bytes++;
235 datalen--;
238 printf("%*s%s\n", (BPL+1-bytes)*2, "", insn);
240 while (datalen > 0) {
241 printf(" -");
242 bytes = 0;
243 while (datalen > 0 && bytes < BPL) {
244 printf("%02X", *data++);
245 bytes++;
246 datalen--;
248 printf("\n");
253 * Skip a certain amount of data in a file, either by seeking if
254 * possible, or if that fails then by reading and discarding.
256 static void skip (unsigned long dist, FILE *fp) {
257 char buffer[256]; /* should fit on most stacks :-) */
260 * Got to be careful with fseek: at least one fseek I've tried
261 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
262 * ftell... horrible but apparently necessary.
264 if (fseek (fp, dist+ftell(fp), SEEK_SET)) {
265 while (dist > 0) {
266 unsigned long len = (dist < sizeof(buffer) ?
267 dist : sizeof(buffer));
268 if (fread (buffer, 1, len, fp) < len) {
269 perror("fread");
270 exit(1);
272 dist -= len;