2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
|
|
|
*
|
|
|
|
* Copyright 1996-2009 The NASM Authors - All Rights Reserved
|
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ndisasm.c the Netwide Disassembler main module
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdio.h>
|
2007-09-20 12:40:37 +08:00
|
|
|
#include <stdarg.h>
|
2002-05-01 04:52:08 +08:00
|
|
|
#include <stdlib.h>
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2002-05-01 04:52:49 +08:00
|
|
|
#include <errno.h>
|
2007-04-12 10:40:54 +08:00
|
|
|
#include <inttypes.h>
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:56:43 +08:00
|
|
|
#include "insns.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.h"
|
|
|
|
#include "sync.h"
|
|
|
|
#include "disasm.h"
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
#define BPL 8 /* bytes per line of hex dump */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static const char *help =
|
2005-01-16 06:15:51 +08:00
|
|
|
"usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n"
|
|
|
|
" [-e bytes] [-k start,bytes] [-p vendor] file\n"
|
|
|
|
" -a or -i activates auto (intelligent) sync\n"
|
2007-04-16 07:12:17 +08:00
|
|
|
" -u same as -b 32\n"
|
|
|
|
" -b 16, -b 32 or -b 64 sets the processor mode\n"
|
2005-01-16 06:15:51 +08:00
|
|
|
" -h displays this text\n"
|
|
|
|
" -r or -v displays the version number\n"
|
|
|
|
" -e skips <bytes> bytes of header\n"
|
|
|
|
" -k avoids disassembling <bytes> bytes from position <start>\n"
|
|
|
|
" -p selects the preferred vendor instruction set (intel, amd, cyrix, idt)\n";
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void output_ins(uint32_t, uint8_t *, int, char *);
|
2007-04-12 10:40:54 +08:00
|
|
|
static void skip(uint32_t dist, FILE * fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2009-07-19 09:07:17 +08:00
|
|
|
static void ndisasm_verror(int severity, const char *fmt, va_list va)
|
2007-09-20 12:40:37 +08:00
|
|
|
{
|
|
|
|
vfprintf(stderr, fmt, va);
|
|
|
|
|
|
|
|
if (severity & ERR_FATAL)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-04-13 00:54:50 +08:00
|
|
|
int main(int argc, char **argv)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-16 07:12:17 +08:00
|
|
|
char buffer[INSN_MAX * 2], *p, *ep, *q;
|
2007-04-14 00:47:53 +08:00
|
|
|
char outbuf[256];
|
|
|
|
char *pname = *argv;
|
|
|
|
char *filename = NULL;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t nextsync, synclen, initskip = 0L;
|
2002-05-01 05:01:38 +08:00
|
|
|
int lenread;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t lendis;
|
2007-10-11 05:58:45 +08:00
|
|
|
bool autosync = false;
|
2007-04-16 07:12:17 +08:00
|
|
|
int bits = 16, b;
|
2007-10-11 05:58:45 +08:00
|
|
|
bool eof = false;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t prefer = 0;
|
2007-10-11 15:05:31 +08:00
|
|
|
bool rn_error;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t offset;
|
2002-05-01 04:51:32 +08:00
|
|
|
FILE *fp;
|
|
|
|
|
2008-06-12 06:49:41 +08:00
|
|
|
tolower_init();
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_set_verror(ndisasm_verror);
|
|
|
|
nasm_init_malloc_error();
|
2007-09-20 12:40:37 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
offset = 0;
|
|
|
|
init_sync();
|
|
|
|
|
|
|
|
while (--argc) {
|
2007-04-14 00:47:53 +08:00
|
|
|
char *v, *vv, *p = *++argv;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (*p == '-' && p[1]) {
|
|
|
|
p++;
|
|
|
|
while (*p)
|
2008-06-12 06:49:41 +08:00
|
|
|
switch (nasm_tolower(*p)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case 'a': /* auto or intelligent sync */
|
|
|
|
case 'i':
|
2007-10-11 05:58:45 +08:00
|
|
|
autosync = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2009-08-13 21:07:59 +08:00
|
|
|
fputs(help, stderr);
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
|
|
|
case 'r':
|
|
|
|
case 'v':
|
|
|
|
fprintf(stderr,
|
2008-11-01 07:53:49 +08:00
|
|
|
"NDISASM version %s compiled on %s\n",
|
|
|
|
nasm_version, nasm_date);
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
2007-04-16 07:12:17 +08:00
|
|
|
case 'u': /* -u for -b 32, -uu for -b 64 */
|
|
|
|
if (bits < 64)
|
|
|
|
bits <<= 1;
|
2005-01-16 06:15:51 +08:00
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
case 'b': /* bits */
|
|
|
|
v = p[1] ? p + 1 : --argc ? *++argv : NULL;
|
|
|
|
if (!v) {
|
|
|
|
fprintf(stderr, "%s: `-b' requires an argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
2007-04-16 07:12:17 +08:00
|
|
|
b = strtoul(v, &ep, 10);
|
|
|
|
if (*ep || !(bits == 16 || bits == 32 || bits == 64)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(stderr, "%s: argument to `-b' should"
|
2007-04-16 07:12:17 +08:00
|
|
|
" be 16, 32 or 64\n", pname);
|
|
|
|
} else {
|
|
|
|
bits = b;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
p = ""; /* force to next argument */
|
|
|
|
break;
|
|
|
|
case 'o': /* origin */
|
|
|
|
v = p[1] ? p + 1 : --argc ? *++argv : NULL;
|
|
|
|
if (!v) {
|
|
|
|
fprintf(stderr, "%s: `-o' requires an argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
offset = readnum(v, &rn_error);
|
|
|
|
if (rn_error) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: `-o' requires a numeric argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
p = ""; /* force to next argument */
|
|
|
|
break;
|
|
|
|
case 's': /* sync point */
|
|
|
|
v = p[1] ? p + 1 : --argc ? *++argv : NULL;
|
|
|
|
if (!v) {
|
|
|
|
fprintf(stderr, "%s: `-s' requires an argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
add_sync(readnum(v, &rn_error), 0L);
|
|
|
|
if (rn_error) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: `-s' requires a numeric argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
p = ""; /* force to next argument */
|
|
|
|
break;
|
|
|
|
case 'e': /* skip a header */
|
|
|
|
v = p[1] ? p + 1 : --argc ? *++argv : NULL;
|
|
|
|
if (!v) {
|
|
|
|
fprintf(stderr, "%s: `-e' requires an argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
initskip = readnum(v, &rn_error);
|
|
|
|
if (rn_error) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: `-e' requires a numeric argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
p = ""; /* force to next argument */
|
|
|
|
break;
|
|
|
|
case 'k': /* skip a region */
|
|
|
|
v = p[1] ? p + 1 : --argc ? *++argv : NULL;
|
|
|
|
if (!v) {
|
|
|
|
fprintf(stderr, "%s: `-k' requires an argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
vv = strchr(v, ',');
|
|
|
|
if (!vv) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: `-k' requires two numbers separated"
|
|
|
|
" by a comma\n", pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*vv++ = '\0';
|
|
|
|
nextsync = readnum(v, &rn_error);
|
|
|
|
if (rn_error) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: `-k' requires numeric arguments\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
synclen = readnum(vv, &rn_error);
|
|
|
|
if (rn_error) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: `-k' requires numeric arguments\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
add_sync(nextsync, synclen);
|
|
|
|
p = ""; /* force to next argument */
|
|
|
|
break;
|
|
|
|
case 'p': /* preferred vendor */
|
|
|
|
v = p[1] ? p + 1 : --argc ? *++argv : NULL;
|
|
|
|
if (!v) {
|
|
|
|
fprintf(stderr, "%s: `-p' requires an argument\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcmp(v, "intel")) {
|
|
|
|
prefer = 0; /* Default */
|
|
|
|
} else if (!strcmp(v, "amd")) {
|
|
|
|
prefer = IF_AMD | IF_3DNOW;
|
|
|
|
} else if (!strcmp(v, "cyrix")) {
|
|
|
|
prefer = IF_CYRIX | IF_3DNOW;
|
|
|
|
} else if (!strcmp(v, "idt") || !strcmp(v, "centaur")
|
|
|
|
|| !strcmp(v, "winchip")) {
|
|
|
|
prefer = IF_3DNOW;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: unknown vendor `%s' specified with `-p'\n",
|
|
|
|
pname, v);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
p = ""; /* force to next argument */
|
|
|
|
break;
|
|
|
|
default: /*bf */
|
|
|
|
fprintf(stderr, "%s: unrecognised option `-%c'\n",
|
|
|
|
pname, *p);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (!filename) {
|
|
|
|
filename = p;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: more than one filename specified\n",
|
|
|
|
pname);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!filename) {
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(stderr, help, pname);
|
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
if (strcmp(filename, "-")) {
|
2005-01-16 06:15:51 +08:00
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
if (!fp) {
|
|
|
|
fprintf(stderr, "%s: unable to open `%s': %s\n",
|
|
|
|
pname, filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
} else
|
2005-01-16 06:15:51 +08:00
|
|
|
fp = stdin;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
if (initskip > 0)
|
2005-01-16 06:15:51 +08:00
|
|
|
skip(initskip, fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This main loop is really horrible, and wants rewriting with
|
|
|
|
* an axe. It'll stay the way it is for a while though, until I
|
|
|
|
* find the energy...
|
|
|
|
*/
|
|
|
|
|
|
|
|
p = q = buffer;
|
2005-01-16 06:15:51 +08:00
|
|
|
nextsync = next_sync(offset, &synclen);
|
2002-05-01 04:51:32 +08:00
|
|
|
do {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t to_read = buffer + sizeof(buffer) - p;
|
2007-12-21 04:24:17 +08:00
|
|
|
if ((nextsync || synclen) &&
|
|
|
|
to_read > nextsync - offset - (p - q))
|
2005-01-16 06:15:51 +08:00
|
|
|
to_read = nextsync - offset - (p - q);
|
|
|
|
if (to_read) {
|
|
|
|
lenread = fread(p, 1, to_read, fp);
|
|
|
|
if (lenread == 0)
|
2007-10-11 05:58:45 +08:00
|
|
|
eof = true; /* help along systems with bad feof */
|
2005-01-16 06:15:51 +08:00
|
|
|
} else
|
|
|
|
lenread = 0;
|
|
|
|
p += lenread;
|
2007-12-21 04:24:17 +08:00
|
|
|
if ((nextsync || synclen) &&
|
|
|
|
(uint32_t)offset == nextsync) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (synclen) {
|
2009-03-19 14:10:19 +08:00
|
|
|
fprintf(stdout, "%08"PRIX32" skipping 0x%"PRIX32" bytes\n",
|
|
|
|
offset, synclen);
|
2005-01-16 06:15:51 +08:00
|
|
|
offset += synclen;
|
|
|
|
skip(synclen, fp);
|
|
|
|
}
|
|
|
|
p = q = buffer;
|
|
|
|
nextsync = next_sync(offset, &synclen);
|
|
|
|
}
|
|
|
|
while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
|
|
|
|
lendis =
|
2009-03-19 14:10:19 +08:00
|
|
|
disasm((uint8_t *) q, outbuf, sizeof(outbuf), bits,
|
|
|
|
offset, autosync, prefer);
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!lendis || lendis > (p - q)
|
2007-12-21 04:24:17 +08:00
|
|
|
|| ((nextsync || synclen) &&
|
|
|
|
(uint32_t)lendis > nextsync - offset))
|
2009-03-19 14:10:19 +08:00
|
|
|
lendis = eatbyte((uint8_t *) q, outbuf, sizeof(outbuf), bits);
|
2007-10-19 12:17:20 +08:00
|
|
|
output_ins(offset, (uint8_t *) q, lendis, outbuf);
|
2005-01-16 06:15:51 +08:00
|
|
|
q += lendis;
|
|
|
|
offset += lendis;
|
|
|
|
}
|
|
|
|
if (q >= buffer + INSN_MAX) {
|
2007-10-19 12:17:20 +08:00
|
|
|
uint8_t *r = (uint8_t *) buffer, *s = (uint8_t *) q;
|
2005-01-16 06:15:51 +08:00
|
|
|
int count = p - q;
|
|
|
|
while (count--)
|
|
|
|
*r++ = *s++;
|
|
|
|
p -= (q - buffer);
|
|
|
|
q = buffer;
|
|
|
|
}
|
2002-05-01 04:53:16 +08:00
|
|
|
} while (lenread > 0 || !(eof || feof(fp)));
|
2002-05-01 04:53:55 +08:00
|
|
|
|
|
|
|
if (fp != stdin)
|
2005-01-16 06:15:51 +08:00
|
|
|
fclose(fp);
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static void output_ins(uint32_t offset, uint8_t *data,
|
2007-04-14 00:47:53 +08:00
|
|
|
int datalen, char *insn)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
int bytes;
|
2007-04-14 08:10:59 +08:00
|
|
|
fprintf(stdout, "%08"PRIX32" ", offset);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
bytes = 0;
|
|
|
|
while (datalen > 0 && bytes < BPL) {
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(stdout, "%02X", *data++);
|
|
|
|
bytes++;
|
|
|
|
datalen--;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(stdout, "%*s%s\n", (BPL + 1 - bytes) * 2, "", insn);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
while (datalen > 0) {
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(stdout, " -");
|
|
|
|
bytes = 0;
|
|
|
|
while (datalen > 0 && bytes < BPL) {
|
|
|
|
fprintf(stdout, "%02X", *data++);
|
|
|
|
bytes++;
|
|
|
|
datalen--;
|
|
|
|
}
|
|
|
|
fprintf(stdout, "\n");
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip a certain amount of data in a file, either by seeking if
|
|
|
|
* possible, or if that fails then by reading and discarding.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
static void skip(uint32_t dist, FILE * fp)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char buffer[256]; /* should fit on most stacks :-) */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Got to be careful with fseek: at least one fseek I've tried
|
|
|
|
* doesn't approve of SEEK_CUR. So I'll use SEEK_SET and
|
|
|
|
* ftell... horrible but apparently necessary.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
if (fseek(fp, dist + ftell(fp), SEEK_SET)) {
|
|
|
|
while (dist > 0) {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t len = (dist < sizeof(buffer) ?
|
2005-01-16 06:15:51 +08:00
|
|
|
dist : sizeof(buffer));
|
|
|
|
if (fread(buffer, 1, len, fp) < len) {
|
|
|
|
perror("fread");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dist -= len;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|