]>
Commit | Line | Data |
---|---|---|
14c7c974 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
4f6e3300 A |
6 | * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.1 (the "License"). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
14c7c974 A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
4f6e3300 | 15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
14c7c974 A |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
4f6e3300 A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License. | |
14c7c974 A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* ndisasm.c the Netwide Disassembler main module | |
25 | * | |
26 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and | |
27 | * Julian Hall. All rights reserved. The software is | |
28 | * redistributable under the licence given in the file "Licence" | |
29 | * distributed in the NASM archive. | |
30 | */ | |
31 | ||
32 | #include <stdio.h> | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
35 | #include <ctype.h> | |
36 | #include <errno.h> | |
37 | ||
38 | #include "nasm.h" | |
39 | #include "nasmlib.h" | |
40 | #include "sync.h" | |
41 | #include "disasm.h" | |
42 | ||
43 | #define BPL 8 /* bytes per line of hex dump */ | |
44 | ||
45 | static const char *help = | |
46 | "usage: ndisasm [-a] [-i] [-h] [-r] [-u] [-b bits] [-o origin] [-s sync...]\n" | |
47 | " [-e bytes] [-k start,bytes] file\n" | |
48 | " -a or -i activates auto (intelligent) sync\n" | |
49 | " -u sets USE32 (32-bit mode)\n" | |
50 | " -b 16 or -b 32 sets number of bits too\n" | |
51 | " -h displays this text\n" | |
52 | " -r displays the version number\n" | |
53 | " -e skips <bytes> bytes of header\n" | |
54 | " -k avoids disassembling <bytes> bytes from position <start>\n"; | |
55 | ||
56 | static void output_ins (unsigned long, unsigned char *, int, char *); | |
57 | static void skip (unsigned long dist, FILE *fp); | |
58 | ||
59 | int main(int argc, char **argv) { | |
60 | unsigned char buffer[INSN_MAX * 2], *p, *q; | |
61 | char outbuf[256]; | |
62 | char *pname = *argv; | |
63 | char *filename = NULL; | |
64 | unsigned long nextsync, synclen, initskip = 0L; | |
65 | int lenread, lendis; | |
66 | int autosync = FALSE; | |
67 | int bits = 16; | |
68 | int eof = FALSE; | |
69 | int rn_error; | |
70 | long offset; | |
71 | FILE *fp; | |
72 | ||
73 | offset = 0; | |
74 | init_sync(); | |
75 | ||
76 | while (--argc) { | |
77 | char *v, *vv, *p = *++argv; | |
78 | if (*p == '-') { | |
79 | p++; | |
80 | while (*p) switch (tolower(*p)) { | |
81 | case 'a': /* auto or intelligent sync */ | |
82 | case 'i': | |
83 | autosync = TRUE; | |
84 | p++; | |
85 | break; | |
86 | case 'h': | |
87 | fprintf(stderr, help); | |
88 | return 0; | |
89 | case 'r': | |
90 | fprintf(stderr, "NDISASM version " NASM_VER "\n"); | |
91 | return 0; | |
92 | case 'u': /* USE32 */ | |
93 | bits = 32; | |
94 | p++; | |
95 | break; | |
96 | case 'b': /* bits */ | |
97 | v = p[1] ? p+1 : --argc ? *++argv : NULL; | |
98 | if (!v) { | |
99 | fprintf(stderr, "%s: `-b' requires an argument\n", pname); | |
100 | return 1; | |
101 | } | |
102 | if (!strcmp(v, "16")) | |
103 | bits = 16; | |
104 | else if (!strcmp(v, "32")) | |
105 | bits = 32; | |
106 | else { | |
107 | fprintf(stderr, "%s: argument to `-b' should" | |
108 | " be `16' or `32'\n", pname); | |
109 | } | |
110 | p = ""; /* force to next argument */ | |
111 | break; | |
112 | case 'o': /* origin */ | |
113 | v = p[1] ? p+1 : --argc ? *++argv : NULL; | |
114 | if (!v) { | |
115 | fprintf(stderr, "%s: `-o' requires an argument\n", pname); | |
116 | return 1; | |
117 | } | |
118 | offset = readnum (v, &rn_error); | |
119 | if (rn_error) { | |
120 | fprintf(stderr, "%s: `-o' requires a numeric argument\n", | |
121 | pname); | |
122 | return 1; | |
123 | } | |
124 | p = ""; /* force to next argument */ | |
125 | break; | |
126 | case 's': /* sync point */ | |
127 | v = p[1] ? p+1 : --argc ? *++argv : NULL; | |
128 | if (!v) { | |
129 | fprintf(stderr, "%s: `-s' requires an argument\n", pname); | |
130 | return 1; | |
131 | } | |
132 | add_sync (readnum (v, &rn_error), 0L); | |
133 | if (rn_error) { | |
134 | fprintf(stderr, "%s: `-s' requires a numeric argument\n", | |
135 | pname); | |
136 | return 1; | |
137 | } | |
138 | p = ""; /* force to next argument */ | |
139 | break; | |
140 | case 'e': /* skip a header */ | |
141 | v = p[1] ? p+1 : --argc ? *++argv : NULL; | |
142 | if (!v) { | |
143 | fprintf(stderr, "%s: `-e' requires an argument\n", pname); | |
144 | return 1; | |
145 | } | |
146 | initskip = readnum (v, &rn_error); | |
147 | if (rn_error) { | |
148 | fprintf(stderr, "%s: `-e' requires a numeric argument\n", | |
149 | pname); | |
150 | return 1; | |
151 | } | |
152 | p = ""; /* force to next argument */ | |
153 | break; | |
154 | case 'k': /* skip a region */ | |
155 | v = p[1] ? p+1 : --argc ? *++argv : NULL; | |
156 | if (!v) { | |
157 | fprintf(stderr, "%s: `-k' requires an argument\n", pname); | |
158 | return 1; | |
159 | } | |
160 | vv = strchr(v, ','); | |
161 | if (!vv) { | |
162 | fprintf(stderr, "%s: `-k' requires two numbers separated" | |
163 | " by a comma\n", pname); | |
164 | return 1; | |
165 | } | |
166 | *vv++ = '\0'; | |
167 | nextsync = readnum (v, &rn_error); | |
168 | if (rn_error) { | |
169 | fprintf(stderr, "%s: `-k' requires numeric arguments\n", | |
170 | pname); | |
171 | return 1; | |
172 | } | |
173 | synclen = readnum (vv, &rn_error); | |
174 | if (rn_error) { | |
175 | fprintf(stderr, "%s: `-k' requires numeric arguments\n", | |
176 | pname); | |
177 | return 1; | |
178 | } | |
179 | add_sync (nextsync, synclen); | |
180 | p = ""; /* force to next argument */ | |
181 | break; | |
182 | } | |
183 | } else if (!filename) { | |
184 | filename = p; | |
185 | } else { | |
186 | fprintf(stderr, "%s: more than one filename specified\n", pname); | |
187 | return 1; | |
188 | } | |
189 | } | |
190 | ||
191 | if (!filename) { | |
192 | fprintf(stderr, help, pname); | |
193 | return 0; | |
194 | } | |
195 | ||
196 | fp = fopen(filename, "rb"); | |
197 | if (!fp) { | |
198 | fprintf(stderr, "%s: unable to open `%s': %s\n", | |
199 | pname, filename, strerror(errno)); | |
200 | return 1; | |
201 | } | |
202 | if (initskip > 0) | |
203 | skip (initskip, fp); | |
204 | ||
205 | /* | |
206 | * This main loop is really horrible, and wants rewriting with | |
207 | * an axe. It'll stay the way it is for a while though, until I | |
208 | * find the energy... | |
209 | */ | |
210 | ||
211 | p = q = buffer; | |
212 | nextsync = next_sync (offset, &synclen); | |
213 | do { | |
214 | unsigned long to_read = buffer+sizeof(buffer)-p; | |
215 | if (to_read > nextsync-offset-(p-q)) | |
216 | to_read = nextsync-offset-(p-q); | |
217 | lenread = fread (p, 1, to_read, fp); | |
218 | if (lenread == 0) | |
219 | eof = TRUE; /* help along systems with bad feof */ | |
220 | p += lenread; | |
f083c6c3 | 221 | if (offset == (long)nextsync) { |
14c7c974 A |
222 | if (synclen) { |
223 | printf("%08lX skipping 0x%lX bytes\n", offset, synclen); | |
224 | offset += synclen; | |
225 | skip (synclen, fp); | |
226 | } | |
227 | p = q = buffer; | |
228 | nextsync = next_sync (offset, &synclen); | |
229 | } | |
230 | while (p > q && (p - q >= INSN_MAX || lenread == 0)) { | |
231 | lendis = disasm (q, outbuf, bits, offset, autosync); | |
232 | if (!lendis || lendis > (p - q) || | |
f083c6c3 | 233 | lendis > (int)(nextsync-offset)) |
14c7c974 A |
234 | lendis = eatbyte (q, outbuf); |
235 | output_ins (offset, q, lendis, outbuf); | |
236 | q += lendis; | |
237 | offset += lendis; | |
238 | } | |
239 | if (q >= buffer+INSN_MAX) { | |
240 | unsigned char *r = buffer, *s = q; | |
241 | int count = p - q; | |
242 | while (count--) | |
243 | *r++ = *s++; | |
244 | p -= (q - buffer); | |
245 | q = buffer; | |
246 | } | |
247 | } while (lenread > 0 || !(eof || feof(fp))); | |
248 | fclose (fp); | |
249 | return 0; | |
250 | } | |
251 | ||
252 | static void output_ins (unsigned long offset, unsigned char *data, | |
253 | int datalen, char *insn) { | |
254 | int bytes; | |
255 | printf("%08lX ", offset); | |
256 | ||
257 | bytes = 0; | |
258 | while (datalen > 0 && bytes < BPL) { | |
259 | printf("%02X", *data++); | |
260 | bytes++; | |
261 | datalen--; | |
262 | } | |
263 | ||
264 | printf("%*s%s\n", (BPL+1-bytes)*2, "", insn); | |
265 | ||
266 | while (datalen > 0) { | |
267 | printf(" -"); | |
268 | bytes = 0; | |
269 | while (datalen > 0 && bytes < BPL) { | |
270 | printf("%02X", *data++); | |
271 | bytes++; | |
272 | datalen--; | |
273 | } | |
274 | printf("\n"); | |
275 | } | |
276 | } | |
277 | ||
278 | /* | |
279 | * Skip a certain amount of data in a file, either by seeking if | |
280 | * possible, or if that fails then by reading and discarding. | |
281 | */ | |
282 | static void skip (unsigned long dist, FILE *fp) { | |
283 | char buffer[256]; /* should fit on most stacks :-) */ | |
284 | ||
285 | /* | |
286 | * Got to be careful with fseek: at least one fseek I've tried | |
287 | * doesn't approve of SEEK_CUR. So I'll use SEEK_SET and | |
288 | * ftell... horrible but apparently necessary. | |
289 | */ | |
290 | if (fseek (fp, dist+ftell(fp), SEEK_SET)) { | |
291 | while (dist > 0) { | |
292 | unsigned long len = (dist < sizeof(buffer) ? | |
293 | dist : sizeof(buffer)); | |
294 | if (fread (buffer, 1, len, fp) < len) { | |
295 | perror("fread"); | |
296 | exit(1); | |
297 | } | |
298 | dist -= len; | |
299 | } | |
300 | } | |
301 | } |