rbtree: add rb_search_exact()
[nasm.git] / disasm / ndisasm.c
blob01e0c5577d1b2ddb9b57b8c6e7c48e103fa28ed3
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
9 * conditions are met:
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
38 #include "compiler.h"
40 #include "nctype.h"
41 #include <errno.h>
43 #include "insns.h"
44 #include "nasm.h"
45 #include "nasmlib.h"
46 #include "error.h"
47 #include "ver.h"
48 #include "sync.h"
49 #include "disasm.h"
51 #define BPL 8 /* bytes per line of hex dump */
53 static const char *help =
54 "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
55 " [-e bytes] [-k start,bytes] [-p vendor] file\n"
56 " -a or -i activates auto (intelligent) sync\n"
57 " -u same as -b 32\n"
58 " -b 16, -b 32 or -b 64 sets the processor mode\n"
59 " -h displays this text\n"
60 " -r or -v displays the version number\n"
61 " -e skips <bytes> bytes of header\n"
62 " -k avoids disassembling <bytes> bytes from position <start>\n"
63 " -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)\n";
65 static void output_ins(uint64_t, uint8_t *, int, char *);
66 static void skip(uint32_t dist, FILE * fp);
68 void nasm_verror(errflags severity, const char *fmt, va_list val)
70 severity &= ERR_MASK;
72 vfprintf(stderr, fmt, val);
73 if (severity >= ERR_FATAL)
74 exit(severity - ERR_FATAL + 1);
77 fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list val)
79 nasm_verror(severity, fmt, val);
80 abort();
83 int main(int argc, char **argv)
85 char buffer[INSN_MAX * 2], *p, *ep, *q;
86 char outbuf[256];
87 char *pname = *argv;
88 char *filename = NULL;
89 uint32_t nextsync, synclen, initskip = 0L;
90 int lenread;
91 int32_t lendis;
92 bool autosync = false;
93 int bits = 16, b;
94 bool eof = false;
95 iflag_t prefer;
96 bool rn_error;
97 int64_t offset;
98 FILE *fp;
100 nasm_ctype_init();
101 iflag_clear_all(&prefer);
103 offset = 0;
104 init_sync();
106 while (--argc) {
107 char *v, *vv, *p = *++argv;
108 if (*p == '-' && p[1]) {
109 p++;
110 while (*p)
111 switch (nasm_tolower(*p)) {
112 case 'a': /* auto or intelligent sync */
113 case 'i':
114 autosync = true;
115 p++;
116 break;
117 case 'h':
118 fputs(help, stderr);
119 return 0;
120 case 'r':
121 case 'v':
122 fprintf(stderr,
123 "NDISASM version %s compiled on %s\n",
124 nasm_version, nasm_date);
125 return 0;
126 case 'u': /* -u for -b 32, -uu for -b 64 */
127 if (bits < 64)
128 bits <<= 1;
129 p++;
130 break;
131 case 'b': /* bits */
132 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
133 if (!v) {
134 fprintf(stderr, "%s: `-b' requires an argument\n",
135 pname);
136 return 1;
138 b = strtoul(v, &ep, 10);
139 if (*ep || !(bits == 16 || bits == 32 || bits == 64)) {
140 fprintf(stderr, "%s: argument to `-b' should"
141 " be 16, 32 or 64\n", pname);
142 } else {
143 bits = b;
145 p = ""; /* force to next argument */
146 break;
147 case 'o': /* origin */
148 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
149 if (!v) {
150 fprintf(stderr, "%s: `-o' requires an argument\n",
151 pname);
152 return 1;
154 offset = readnum(v, &rn_error);
155 if (rn_error) {
156 fprintf(stderr,
157 "%s: `-o' requires a numeric argument\n",
158 pname);
159 return 1;
161 p = ""; /* force to next argument */
162 break;
163 case 's': /* sync point */
164 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
165 if (!v) {
166 fprintf(stderr, "%s: `-s' requires an argument\n",
167 pname);
168 return 1;
170 add_sync(readnum(v, &rn_error), 0L);
171 if (rn_error) {
172 fprintf(stderr,
173 "%s: `-s' requires a numeric argument\n",
174 pname);
175 return 1;
177 p = ""; /* force to next argument */
178 break;
179 case 'e': /* skip a header */
180 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
181 if (!v) {
182 fprintf(stderr, "%s: `-e' requires an argument\n",
183 pname);
184 return 1;
186 initskip = readnum(v, &rn_error);
187 if (rn_error) {
188 fprintf(stderr,
189 "%s: `-e' requires a numeric argument\n",
190 pname);
191 return 1;
193 p = ""; /* force to next argument */
194 break;
195 case 'k': /* skip a region */
196 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
197 if (!v) {
198 fprintf(stderr, "%s: `-k' requires an argument\n",
199 pname);
200 return 1;
202 vv = strchr(v, ',');
203 if (!vv) {
204 fprintf(stderr,
205 "%s: `-k' requires two numbers separated"
206 " by a comma\n", pname);
207 return 1;
209 *vv++ = '\0';
210 nextsync = readnum(v, &rn_error);
211 if (rn_error) {
212 fprintf(stderr,
213 "%s: `-k' requires numeric arguments\n",
214 pname);
215 return 1;
217 synclen = readnum(vv, &rn_error);
218 if (rn_error) {
219 fprintf(stderr,
220 "%s: `-k' requires numeric arguments\n",
221 pname);
222 return 1;
224 add_sync(nextsync, synclen);
225 p = ""; /* force to next argument */
226 break;
227 case 'p': /* preferred vendor */
228 v = p[1] ? p + 1 : --argc ? *++argv : NULL;
229 if (!v) {
230 fprintf(stderr, "%s: `-p' requires an argument\n",
231 pname);
232 return 1;
234 if (!strcmp(v, "intel")) {
235 iflag_clear_all(&prefer); /* default */
236 } else if (!strcmp(v, "amd")) {
237 iflag_clear_all(&prefer);
238 iflag_set(&prefer, IF_AMD);
239 iflag_set(&prefer, IF_3DNOW);
240 } else if (!strcmp(v, "cyrix")) {
241 iflag_clear_all(&prefer);
242 iflag_set(&prefer, IF_CYRIX);
243 iflag_set(&prefer, IF_3DNOW);
244 } else if (!strcmp(v, "idt") ||
245 !strcmp(v, "centaur") ||
246 !strcmp(v, "winchip")) {
247 iflag_clear_all(&prefer);
248 iflag_set(&prefer, IF_3DNOW);
249 } else {
250 fprintf(stderr,
251 "%s: unknown vendor `%s' specified with `-p'\n",
252 pname, v);
253 return 1;
255 p = ""; /* force to next argument */
256 break;
257 default: /*bf */
258 fprintf(stderr, "%s: unrecognised option `-%c'\n",
259 pname, *p);
260 return 1;
262 } else if (!filename) {
263 filename = p;
264 } else {
265 fprintf(stderr, "%s: more than one filename specified\n",
266 pname);
267 return 1;
271 if (!filename) {
272 fprintf(stderr, help, pname);
273 return 0;
276 if (strcmp(filename, "-")) {
277 fp = fopen(filename, "rb");
278 if (!fp) {
279 fprintf(stderr, "%s: unable to open `%s': %s\n",
280 pname, filename, strerror(errno));
281 return 1;
283 } else {
284 nasm_set_binary_mode(stdin);
285 fp = stdin;
288 if (initskip > 0)
289 skip(initskip, fp);
292 * This main loop is really horrible, and wants rewriting with
293 * an axe. It'll stay the way it is for a while though, until I
294 * find the energy...
297 p = q = buffer;
298 nextsync = next_sync(offset, &synclen);
299 do {
300 uint32_t to_read = buffer + sizeof(buffer) - p;
301 if ((nextsync || synclen) &&
302 to_read > nextsync - offset - (p - q))
303 to_read = nextsync - offset - (p - q);
304 if (to_read) {
305 lenread = fread(p, 1, to_read, fp);
306 if (lenread == 0)
307 eof = true; /* help along systems with bad feof */
308 } else
309 lenread = 0;
310 p += lenread;
311 if ((nextsync || synclen) &&
312 (uint32_t)offset == nextsync) {
313 if (synclen) {
314 fprintf(stdout, "%08"PRIX64" skipping 0x%"PRIX32" bytes\n",
315 offset, synclen);
316 offset += synclen;
317 skip(synclen, fp);
319 p = q = buffer;
320 nextsync = next_sync(offset, &synclen);
322 while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
323 lendis = disasm((uint8_t *)q, INSN_MAX, outbuf, sizeof(outbuf),
324 bits, offset, autosync, &prefer);
325 if (!lendis || lendis > (p - q)
326 || ((nextsync || synclen) &&
327 (uint32_t)lendis > nextsync - offset))
328 lendis = eatbyte((uint8_t *) q, outbuf, sizeof(outbuf), bits);
329 output_ins(offset, (uint8_t *) q, lendis, outbuf);
330 q += lendis;
331 offset += lendis;
333 if (q >= buffer + INSN_MAX) {
334 uint8_t *r = (uint8_t *) buffer, *s = (uint8_t *) q;
335 int count = p - q;
336 while (count--)
337 *r++ = *s++;
338 p -= (q - buffer);
339 q = buffer;
341 } while (lenread > 0 || !(eof || feof(fp)));
343 if (fp != stdin)
344 fclose(fp);
346 return 0;
349 static void output_ins(uint64_t offset, uint8_t *data,
350 int datalen, char *insn)
352 int bytes;
353 fprintf(stdout, "%08"PRIX64" ", offset);
355 bytes = 0;
356 while (datalen > 0 && bytes < BPL) {
357 fprintf(stdout, "%02X", *data++);
358 bytes++;
359 datalen--;
362 fprintf(stdout, "%*s%s\n", (BPL + 1 - bytes) * 2, "", insn);
364 while (datalen > 0) {
365 fprintf(stdout, " -");
366 bytes = 0;
367 while (datalen > 0 && bytes < BPL) {
368 fprintf(stdout, "%02X", *data++);
369 bytes++;
370 datalen--;
372 fprintf(stdout, "\n");
377 * Skip a certain amount of data in a file, either by seeking if
378 * possible, or if that fails then by reading and discarding.
380 static void skip(uint32_t dist, FILE * fp)
382 char buffer[256]; /* should fit on most stacks :-) */
385 * Got to be careful with fseek: at least one fseek I've tried
386 * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
387 * ftell... horrible but apparently necessary.
389 if (fseek(fp, dist + ftell(fp), SEEK_SET)) {
390 while (dist > 0) {
391 uint32_t len = (dist < sizeof(buffer) ?
392 dist : sizeof(buffer));
393 if (fread(buffer, 1, len, fp) < len) {
394 perror("fread");
395 exit(1);
397 dist -= len;