ndisasm.c 14 KB

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