assemble: Only treat a displacement as signed if it is < asize
[nasm.git] / ndisasm.c
blob3212629fac0900dd49ad9c6d19c513d64cc3a9ed
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.
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 <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>
48 #include "insns.h"
49 #include "nasm.h"
50 #include "nasmlib.h"
51 #include "sync.h"
52 #include "disasm.h"
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"
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";
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)
76 exit(1);
79 int main(int argc, char **argv)
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;
96 tolower_init();
97 nasm_set_verror(ndisasm_verror);
98 nasm_init_malloc_error();
99 iflag_clear_all(&prefer);
101 offset = 0;
102 init_sync();
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
260 } else if (!filename) {
261 filename = p;
262 } else {
263 fprintf(stderr, "%s: more than one filename specified\n",
264 pname);
265 return 1;
269 if (!filename) {
270 fprintf(stderr, help, pname);
271 return 0;
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;
281 } else
282 fp = stdin;
284 if (initskip > 0)
285 skip(initskip, fp);
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...
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);
315 p = q = buffer;
316 nextsync = next_sync(offset, &synclen);
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;
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;
338 } while (lenread > 0 || !(eof || feof(fp)));
340 if (fp != stdin)
341 fclose(fp);
343 return 0;
346 static void output_ins(uint32_t offset, uint8_t *data,
347 int datalen, char *insn)
349 int bytes;
350 fprintf(stdout, "%08"PRIX32" ", offset);
352 bytes = 0;
353 while (datalen > 0 && bytes < BPL) {
354 fprintf(stdout, "%02X", *data++);
355 bytes++;
356 datalen--;
359 fprintf(stdout, "%*s%s\n", (BPL + 1 - bytes) * 2, "", insn);
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--;
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)) {
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);
394 dist -= len;