1 | /* $Id: tstDisasm-2.cpp 41668 2012-06-12 13:15:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Testcase - Generic Disassembler Tool.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include <VBox/dis.h>
|
---|
22 | #include <VBox/err.h>
|
---|
23 | #include <iprt/alloc.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/initterm.h>
|
---|
26 | #include <iprt/getopt.h>
|
---|
27 | #include <iprt/file.h>
|
---|
28 | #include <iprt/path.h>
|
---|
29 | #include <iprt/stream.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <iprt/ctype.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | /*******************************************************************************
|
---|
35 | * Structures and Typedefs *
|
---|
36 | *******************************************************************************/
|
---|
37 | typedef enum { kAsmStyle_Default, kAsmStyle_yasm, kAsmStyle_masm, kAsmStyle_gas, kAsmStyle_invalid } ASMSTYLE;
|
---|
38 | typedef enum { kUndefOp_Fail, kUndefOp_All, kUndefOp_DefineByte, kUndefOp_End } UNDEFOPHANDLING;
|
---|
39 |
|
---|
40 | typedef struct MYDISSTATE
|
---|
41 | {
|
---|
42 | DISCPUSTATE Cpu;
|
---|
43 | uint64_t uAddress; /**< The current instruction address. */
|
---|
44 | uint8_t *pbInstr; /**< The current instruction (pointer). */
|
---|
45 | uint32_t cbInstr; /**< The size of the current instruction. */
|
---|
46 | bool fUndefOp; /**< Whether the current instruction is really an undefined opcode.*/
|
---|
47 | UNDEFOPHANDLING enmUndefOp; /**< How to treat undefined opcodes. */
|
---|
48 | int rc; /**< Set if we hit EOF. */
|
---|
49 | size_t cbLeft; /**< The number of bytes left. (read) */
|
---|
50 | uint8_t *pbNext; /**< The next byte. (read) */
|
---|
51 | uint64_t uNextAddr; /**< The address of the next byte. (read) */
|
---|
52 | char szLine[256]; /**< The disassembler text output. */
|
---|
53 | } MYDISSTATE;
|
---|
54 | typedef MYDISSTATE *PMYDISSTATE;
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Default style.
|
---|
60 | *
|
---|
61 | * @param pState The disassembler state.
|
---|
62 | */
|
---|
63 | static void MyDisasDefaultFormatter(PMYDISSTATE pState)
|
---|
64 | {
|
---|
65 | RTPrintf("%s", pState->szLine);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Yasm style.
|
---|
71 | *
|
---|
72 | * @param pState The disassembler state.
|
---|
73 | */
|
---|
74 | static void MyDisasYasmFormatter(PMYDISSTATE pState)
|
---|
75 | {
|
---|
76 | char szTmp[256];
|
---|
77 | #if 0
|
---|
78 | /* a very quick hack. */
|
---|
79 | strcpy(szTmp, RTStrStripL(strchr(pState->szLine, ':') + 1));
|
---|
80 |
|
---|
81 | char *psz = strrchr(szTmp, '[');
|
---|
82 | *psz = '\0';
|
---|
83 | RTStrStripR(szTmp);
|
---|
84 |
|
---|
85 | psz = strstr(szTmp, " ptr ");
|
---|
86 | if (psz)
|
---|
87 | memset(psz, ' ', 5);
|
---|
88 |
|
---|
89 | char *pszEnd = strchr(szTmp, '\0');
|
---|
90 | while (pszEnd - &szTmp[0] < 71)
|
---|
91 | *pszEnd++ = ' ';
|
---|
92 | *pszEnd = '\0';
|
---|
93 |
|
---|
94 | #else
|
---|
95 | size_t cch = DISFormatYasmEx(&pState->Cpu, szTmp, sizeof(szTmp),
|
---|
96 | DIS_FMT_FLAGS_STRICT | DIS_FMT_FLAGS_ADDR_RIGHT | DIS_FMT_FLAGS_ADDR_COMMENT
|
---|
97 | | DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_BYTES_COMMENT | DIS_FMT_FLAGS_BYTES_SPACED,
|
---|
98 | NULL, NULL);
|
---|
99 | Assert(cch < sizeof(szTmp));
|
---|
100 | while (cch < 71)
|
---|
101 | szTmp[cch++] = ' ';
|
---|
102 | szTmp[cch] = '\0';
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | RTPrintf(" %s ; %s", szTmp, pState->szLine);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Masm style.
|
---|
111 | *
|
---|
112 | * @param pState The disassembler state.
|
---|
113 | */
|
---|
114 | static void MyDisasMasmFormatter(PMYDISSTATE pState)
|
---|
115 | {
|
---|
116 | RTPrintf("masm not implemented: %s", pState->szLine);
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * This is a temporary workaround for catching a few illegal opcodes
|
---|
122 | * that the disassembler is currently letting thru, just enough to make
|
---|
123 | * the assemblers happy.
|
---|
124 | *
|
---|
125 | * We're too close to a release to dare mess with these things now as
|
---|
126 | * they may consequences for performance and let alone introduce bugs.
|
---|
127 | *
|
---|
128 | * @returns true if it's valid. false if it isn't.
|
---|
129 | *
|
---|
130 | * @param pCpu The disassembler output.
|
---|
131 | */
|
---|
132 | static bool MyDisasIsValidInstruction(DISCPUSTATE const *pCpu)
|
---|
133 | {
|
---|
134 | switch (pCpu->pCurInstr->opcode)
|
---|
135 | {
|
---|
136 | /* These doesn't take memory operands. */
|
---|
137 | case OP_MOV_CR:
|
---|
138 | case OP_MOV_DR:
|
---|
139 | case OP_MOV_TR:
|
---|
140 | if (pCpu->ModRM.Bits.Mod != 3)
|
---|
141 | return false;
|
---|
142 | break;
|
---|
143 |
|
---|
144 | /* The 0x8f /0 variant of this instruction doesn't get its /r value verified. */
|
---|
145 | case OP_POP:
|
---|
146 | if ( pCpu->opcode == 0x8f
|
---|
147 | && pCpu->ModRM.Bits.Reg != 0)
|
---|
148 | return false;
|
---|
149 | break;
|
---|
150 |
|
---|
151 | /* The 0xc6 /0 and 0xc7 /0 variants of this instruction don't get their /r values verified. */
|
---|
152 | case OP_MOV:
|
---|
153 | if ( ( pCpu->opcode == 0xc6
|
---|
154 | || pCpu->opcode == 0xc7)
|
---|
155 | && pCpu->ModRM.Bits.Reg != 0)
|
---|
156 | return false;
|
---|
157 | break;
|
---|
158 |
|
---|
159 | default:
|
---|
160 | break;
|
---|
161 | }
|
---|
162 |
|
---|
163 | return true;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Callback for reading bytes.
|
---|
169 | *
|
---|
170 | * @todo This should check that the disassembler doesn't do unnecessary reads,
|
---|
171 | * however the current doesn't do this and is just complicated...
|
---|
172 | */
|
---|
173 | static DECLCALLBACK(int) MyDisasInstrRead(PDISCPUSTATE pDisState, uint8_t *pbDst, RTUINTPTR uSrcAddr, uint32_t cbToRead)
|
---|
174 | {
|
---|
175 | PMYDISSTATE pState = (PMYDISSTATE)pDisState;
|
---|
176 | if (RT_LIKELY( pState->uNextAddr == uSrcAddr
|
---|
177 | && pState->cbLeft >= cbToRead))
|
---|
178 | {
|
---|
179 | /*
|
---|
180 | * Straight forward reading.
|
---|
181 | */
|
---|
182 | if (cbToRead == 1)
|
---|
183 | {
|
---|
184 | pState->cbLeft--;
|
---|
185 | *pbDst = *pState->pbNext++;
|
---|
186 | pState->uNextAddr++;
|
---|
187 | }
|
---|
188 | else
|
---|
189 | {
|
---|
190 | memcpy(pbDst, pState->pbNext, cbToRead);
|
---|
191 | pState->pbNext += cbToRead;
|
---|
192 | pState->cbLeft -= cbToRead;
|
---|
193 | pState->uNextAddr += cbToRead;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | /*
|
---|
199 | * Jumping up the stream.
|
---|
200 | * This occurs when the byte sequence is added to the output string.
|
---|
201 | */
|
---|
202 | uint64_t offReq64 = uSrcAddr - pState->uAddress;
|
---|
203 | if (offReq64 < 32)
|
---|
204 | {
|
---|
205 | uint32_t offReq = offReq64;
|
---|
206 | uintptr_t off = pState->pbNext - pState->pbInstr;
|
---|
207 | if (off + pState->cbLeft <= offReq)
|
---|
208 | {
|
---|
209 | pState->pbNext += pState->cbLeft;
|
---|
210 | pState->uNextAddr += pState->cbLeft;
|
---|
211 | pState->cbLeft = 0;
|
---|
212 |
|
---|
213 | memset(pbDst, 0xcc, cbToRead);
|
---|
214 | pState->rc = VERR_EOF;
|
---|
215 | return VERR_EOF;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /* reset the stream. */
|
---|
219 | pState->cbLeft += off;
|
---|
220 | pState->pbNext = pState->pbInstr;
|
---|
221 | pState->uNextAddr = pState->uAddress;
|
---|
222 |
|
---|
223 | /* skip ahead. */
|
---|
224 | pState->cbLeft -= offReq;
|
---|
225 | pState->pbNext += offReq;
|
---|
226 | pState->uNextAddr += offReq;
|
---|
227 |
|
---|
228 | /* do the reading. */
|
---|
229 | if (pState->cbLeft >= cbToRead)
|
---|
230 | {
|
---|
231 | memcpy(pbDst, pState->pbNext, cbToRead);
|
---|
232 | pState->cbLeft -= cbToRead;
|
---|
233 | pState->pbNext += cbToRead;
|
---|
234 | pState->uNextAddr += cbToRead;
|
---|
235 | }
|
---|
236 | else
|
---|
237 | {
|
---|
238 | if (pState->cbLeft > 0)
|
---|
239 | {
|
---|
240 | memcpy(pbDst, pState->pbNext, pState->cbLeft);
|
---|
241 | pbDst += pState->cbLeft;
|
---|
242 | cbToRead -= (uint32_t)pState->cbLeft;
|
---|
243 | pState->pbNext += pState->cbLeft;
|
---|
244 | pState->uNextAddr += pState->cbLeft;
|
---|
245 | pState->cbLeft = 0;
|
---|
246 | }
|
---|
247 | memset(pbDst, 0xcc, cbToRead);
|
---|
248 | pState->rc = VERR_EOF;
|
---|
249 | return VERR_EOF;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | else
|
---|
253 | {
|
---|
254 | RTStrmPrintf(g_pStdErr, "Reading before current instruction!\n");
|
---|
255 | memset(pbDst, 0x90, cbToRead);
|
---|
256 | pState->rc = VERR_INTERNAL_ERROR;
|
---|
257 | return VERR_INTERNAL_ERROR;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | return VINF_SUCCESS;
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Disassembles a block of memory.
|
---|
267 | *
|
---|
268 | * @returns VBox status code.
|
---|
269 | * @param argv0 Program name (for errors and warnings).
|
---|
270 | * @param enmCpuMode The cpu mode to disassemble in.
|
---|
271 | * @param uAddress The address we're starting to disassemble at.
|
---|
272 | * @param uHighlightAddr The address of the instruction that should be
|
---|
273 | * highlighted. Pass UINT64_MAX to keep quiet.
|
---|
274 | * @param pbFile Where to start disassemble.
|
---|
275 | * @param cbFile How much to disassemble.
|
---|
276 | * @param enmStyle The assembly output style.
|
---|
277 | * @param fListing Whether to print in a listing like mode.
|
---|
278 | * @param enmUndefOp How to deal with undefined opcodes.
|
---|
279 | */
|
---|
280 | static int MyDisasmBlock(const char *argv0, DISCPUMODE enmCpuMode, uint64_t uAddress,
|
---|
281 | uint64_t uHighlightAddr, uint8_t *pbFile, size_t cbFile,
|
---|
282 | ASMSTYLE enmStyle, bool fListing, UNDEFOPHANDLING enmUndefOp)
|
---|
283 | {
|
---|
284 | /*
|
---|
285 | * Initialize the CPU context.
|
---|
286 | */
|
---|
287 | MYDISSTATE State;
|
---|
288 | State.uAddress = uAddress;
|
---|
289 | State.pbInstr = pbFile;
|
---|
290 | State.cbInstr = 0;
|
---|
291 | State.enmUndefOp = enmUndefOp;
|
---|
292 | State.rc = VINF_SUCCESS;
|
---|
293 | State.cbLeft = cbFile;
|
---|
294 | State.pbNext = pbFile;
|
---|
295 | State.uNextAddr = uAddress;
|
---|
296 |
|
---|
297 | void (*pfnFormatter)(PMYDISSTATE pState);
|
---|
298 | switch (enmStyle)
|
---|
299 | {
|
---|
300 | case kAsmStyle_Default:
|
---|
301 | pfnFormatter = MyDisasDefaultFormatter;
|
---|
302 | break;
|
---|
303 |
|
---|
304 | case kAsmStyle_yasm:
|
---|
305 | RTPrintf(" BITS %d\n", enmCpuMode == CPUMODE_16BIT ? 16 : enmCpuMode == CPUMODE_32BIT ? 32 : 64);
|
---|
306 | pfnFormatter = MyDisasYasmFormatter;
|
---|
307 | break;
|
---|
308 |
|
---|
309 | case kAsmStyle_masm:
|
---|
310 | pfnFormatter = MyDisasMasmFormatter;
|
---|
311 | break;
|
---|
312 |
|
---|
313 | default:
|
---|
314 | AssertFailedReturn(VERR_INTERNAL_ERROR);
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * The loop.
|
---|
319 | */
|
---|
320 | int rcRet = VINF_SUCCESS;
|
---|
321 | while (State.cbLeft > 0)
|
---|
322 | {
|
---|
323 | /*
|
---|
324 | * Disassemble it.
|
---|
325 | */
|
---|
326 | State.cbInstr = 0;
|
---|
327 | State.cbLeft += State.pbNext - State.pbInstr;
|
---|
328 | State.uNextAddr = State.uAddress;
|
---|
329 | State.pbNext = State.pbInstr;
|
---|
330 |
|
---|
331 |
|
---|
332 | int rc = DISInstrWithReader(State.uAddress, enmCpuMode, MyDisasInstrRead, &State,
|
---|
333 | &State.Cpu, &State.cbInstr, State.szLine);
|
---|
334 | if ( RT_SUCCESS(rc)
|
---|
335 | || ( ( rc == VERR_DIS_INVALID_OPCODE
|
---|
336 | || rc == VERR_DIS_GEN_FAILURE)
|
---|
337 | && State.enmUndefOp == kUndefOp_DefineByte))
|
---|
338 | {
|
---|
339 | State.fUndefOp = rc == VERR_DIS_INVALID_OPCODE
|
---|
340 | || rc == VERR_DIS_GEN_FAILURE
|
---|
341 | || State.Cpu.pCurInstr->opcode == OP_INVALID
|
---|
342 | || State.Cpu.pCurInstr->opcode == OP_ILLUD2
|
---|
343 | || ( State.enmUndefOp == kUndefOp_DefineByte
|
---|
344 | && !MyDisasIsValidInstruction(&State.Cpu));
|
---|
345 | if (State.fUndefOp && State.enmUndefOp == kUndefOp_DefineByte)
|
---|
346 | {
|
---|
347 | if (!State.cbInstr)
|
---|
348 | {
|
---|
349 | State.Cpu.abInstr[0] = 0;
|
---|
350 | State.Cpu.pfnReadBytes(&State.Cpu, &State.Cpu.abInstr[0], State.uAddress, 1);
|
---|
351 | State.cbInstr = 1;
|
---|
352 | }
|
---|
353 | RTPrintf(" db");
|
---|
354 | for (unsigned off = 0; off < State.cbInstr; off++)
|
---|
355 | RTPrintf(off ? ", %03xh" : " %03xh", State.Cpu.abInstr[off]);
|
---|
356 | RTPrintf(" ; %s\n", State.szLine);
|
---|
357 | }
|
---|
358 | else if (!State.fUndefOp && State.enmUndefOp == kUndefOp_All)
|
---|
359 | {
|
---|
360 | RTPrintf("%s: error at %#RX64: unexpected valid instruction (op=%d)\n", argv0, State.uAddress, State.Cpu.pCurInstr->opcode);
|
---|
361 | pfnFormatter(&State);
|
---|
362 | rcRet = VERR_GENERAL_FAILURE;
|
---|
363 | }
|
---|
364 | else if (State.fUndefOp && State.enmUndefOp == kUndefOp_Fail)
|
---|
365 | {
|
---|
366 | RTPrintf("%s: error at %#RX64: undefined opcode (op=%d)\n", argv0, State.uAddress, State.Cpu.pCurInstr->opcode);
|
---|
367 | pfnFormatter(&State);
|
---|
368 | rcRet = VERR_GENERAL_FAILURE;
|
---|
369 | }
|
---|
370 | else
|
---|
371 | {
|
---|
372 | /* Use db for odd encodings that we can't make the assembler use. */
|
---|
373 | if ( State.enmUndefOp == kUndefOp_DefineByte
|
---|
374 | && DISFormatYasmIsOddEncoding(&State.Cpu))
|
---|
375 | {
|
---|
376 | RTPrintf(" db");
|
---|
377 | for (unsigned off = 0; off < State.cbInstr; off++)
|
---|
378 | RTPrintf(off ? ", %03xh" : " %03xh", State.Cpu.abInstr[off]);
|
---|
379 | RTPrintf(" ; ");
|
---|
380 | }
|
---|
381 |
|
---|
382 | pfnFormatter(&State);
|
---|
383 | }
|
---|
384 | }
|
---|
385 | else
|
---|
386 | {
|
---|
387 | State.cbInstr = State.pbNext - State.pbInstr;
|
---|
388 | if (!State.cbLeft)
|
---|
389 | RTPrintf("%s: error at %#RX64: read beyond the end (%Rrc)\n", argv0, State.uAddress, rc);
|
---|
390 | else if (State.cbInstr)
|
---|
391 | RTPrintf("%s: error at %#RX64: %Rrc cbInstr=%d\n", argv0, State.uAddress, rc, State.cbInstr);
|
---|
392 | else
|
---|
393 | {
|
---|
394 | RTPrintf("%s: error at %#RX64: %Rrc cbInstr=%d!\n", argv0, State.uAddress, rc, State.cbInstr);
|
---|
395 | if (rcRet == VINF_SUCCESS)
|
---|
396 | rcRet = rc;
|
---|
397 | break;
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | /* Highlight this instruction? */
|
---|
402 | if (uHighlightAddr - State.uAddress < State.cbInstr)
|
---|
403 | RTPrintf("; ^^^^^^^^^^^^^^^^^^^^^\n");
|
---|
404 |
|
---|
405 | /* next */
|
---|
406 | State.uAddress += State.cbInstr;
|
---|
407 | State.pbInstr += State.cbInstr;
|
---|
408 | }
|
---|
409 |
|
---|
410 | return rcRet;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Converts a hex char to a number.
|
---|
415 | *
|
---|
416 | * @returns 0..15 on success, -1 on failure.
|
---|
417 | * @param ch The character.
|
---|
418 | */
|
---|
419 | static int HexDigitToNum(char ch)
|
---|
420 | {
|
---|
421 | switch (ch)
|
---|
422 | {
|
---|
423 | case '0': return 0;
|
---|
424 | case '1': return 1;
|
---|
425 | case '2': return 2;
|
---|
426 | case '3': return 3;
|
---|
427 | case '4': return 4;
|
---|
428 | case '5': return 5;
|
---|
429 | case '6': return 6;
|
---|
430 | case '7': return 7;
|
---|
431 | case '8': return 8;
|
---|
432 | case '9': return 9;
|
---|
433 | case 'A':
|
---|
434 | case 'a': return 0xa;
|
---|
435 | case 'B':
|
---|
436 | case 'b': return 0xb;
|
---|
437 | case 'C':
|
---|
438 | case 'c': return 0xc;
|
---|
439 | case 'D':
|
---|
440 | case 'd': return 0xd;
|
---|
441 | case 'E':
|
---|
442 | case 'e': return 0xe;
|
---|
443 | case 'F':
|
---|
444 | case 'f': return 0xf;
|
---|
445 | default:
|
---|
446 | RTPrintf("error: Invalid hex digig '%c'\n", ch);
|
---|
447 | return -1;
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Prints usage info.
|
---|
453 | *
|
---|
454 | * @returns 1.
|
---|
455 | * @param argv0 The program name.
|
---|
456 | */
|
---|
457 | static int Usage(const char *argv0)
|
---|
458 | {
|
---|
459 | RTStrmPrintf(g_pStdErr,
|
---|
460 | "usage: %s [options] <file1> [file2..fileN]\n"
|
---|
461 | " or: %s [options] <-x|--hex-bytes> <hex byte> [more hex..]\n"
|
---|
462 | " or: %s <--help|-h>\n"
|
---|
463 | "\n"
|
---|
464 | "Options:\n"
|
---|
465 | " --address|-a <address>\n"
|
---|
466 | " The base address. Default: 0\n"
|
---|
467 | " --max-bytes|-b <bytes>\n"
|
---|
468 | " The maximum number of bytes to disassemble. Default: 1GB\n"
|
---|
469 | " --cpumode|-c <16|32|64>\n"
|
---|
470 | " The cpu mode. Default: 32\n"
|
---|
471 | " --listing|-l, --no-listing|-L\n"
|
---|
472 | " Enables or disables listing mode. Default: --no-listing\n"
|
---|
473 | " --offset|-o <offset>\n"
|
---|
474 | " The file offset at which to start disassembling. Default: 0\n"
|
---|
475 | " --style|-s <default|yasm|masm>\n"
|
---|
476 | " The assembly output style. Default: default\n"
|
---|
477 | " --undef-op|-u <fail|all|db>\n"
|
---|
478 | " How to treat undefined opcodes. Default: fail\n"
|
---|
479 | , argv0, argv0);
|
---|
480 | return 1;
|
---|
481 | }
|
---|
482 |
|
---|
483 |
|
---|
484 | int main(int argc, char **argv)
|
---|
485 | {
|
---|
486 | RTR3InitExe(argc, &argv, 0);
|
---|
487 | const char * const argv0 = RTPathFilename(argv[0]);
|
---|
488 |
|
---|
489 | /* options */
|
---|
490 | uint64_t uAddress = 0;
|
---|
491 | uint64_t uHighlightAddr = UINT64_MAX;
|
---|
492 | ASMSTYLE enmStyle = kAsmStyle_Default;
|
---|
493 | UNDEFOPHANDLING enmUndefOp = kUndefOp_Fail;
|
---|
494 | bool fListing = true;
|
---|
495 | DISCPUMODE enmCpuMode = CPUMODE_32BIT;
|
---|
496 | RTFOFF off = 0;
|
---|
497 | RTFOFF cbMax = _1G;
|
---|
498 | bool fHexBytes = false;
|
---|
499 |
|
---|
500 | /*
|
---|
501 | * Parse arguments.
|
---|
502 | */
|
---|
503 | static const RTGETOPTDEF g_aOptions[] =
|
---|
504 | {
|
---|
505 | { "--address", 'a', RTGETOPT_REQ_UINT64 },
|
---|
506 | { "--cpumode", 'c', RTGETOPT_REQ_UINT32 },
|
---|
507 | { "--bytes", 'b', RTGETOPT_REQ_INT64 },
|
---|
508 | { "--listing", 'l', RTGETOPT_REQ_NOTHING },
|
---|
509 | { "--no-listing", 'L', RTGETOPT_REQ_NOTHING },
|
---|
510 | { "--offset", 'o', RTGETOPT_REQ_INT64 },
|
---|
511 | { "--style", 's', RTGETOPT_REQ_STRING },
|
---|
512 | { "--undef-op", 'u', RTGETOPT_REQ_STRING },
|
---|
513 | { "--hex-bytes", 'x', RTGETOPT_REQ_NOTHING },
|
---|
514 | };
|
---|
515 |
|
---|
516 | int ch;
|
---|
517 | RTGETOPTUNION ValueUnion;
|
---|
518 | RTGETOPTSTATE GetState;
|
---|
519 | RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
520 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
521 | && ch != VINF_GETOPT_NOT_OPTION)
|
---|
522 | {
|
---|
523 | switch (ch)
|
---|
524 | {
|
---|
525 | case 'a':
|
---|
526 | uAddress = ValueUnion.u64;
|
---|
527 | break;
|
---|
528 |
|
---|
529 | case 'b':
|
---|
530 | cbMax = ValueUnion.i64;
|
---|
531 | break;
|
---|
532 |
|
---|
533 | case 'c':
|
---|
534 | if (ValueUnion.u32 == 16)
|
---|
535 | enmCpuMode = CPUMODE_16BIT;
|
---|
536 | else if (ValueUnion.u32 == 32)
|
---|
537 | enmCpuMode = CPUMODE_32BIT;
|
---|
538 | else if (ValueUnion.u32 == 64)
|
---|
539 | enmCpuMode = CPUMODE_64BIT;
|
---|
540 | else
|
---|
541 | {
|
---|
542 | RTStrmPrintf(g_pStdErr, "%s: Invalid CPU mode value %RU32\n", argv0, ValueUnion.u32);
|
---|
543 | return 1;
|
---|
544 | }
|
---|
545 | break;
|
---|
546 |
|
---|
547 | case 'h':
|
---|
548 | return Usage(argv0);
|
---|
549 |
|
---|
550 | case 'l':
|
---|
551 | fListing = true;
|
---|
552 | break;
|
---|
553 |
|
---|
554 | case 'L':
|
---|
555 | fListing = false;
|
---|
556 | break;
|
---|
557 |
|
---|
558 | case 'o':
|
---|
559 | off = ValueUnion.i64;
|
---|
560 | break;
|
---|
561 |
|
---|
562 | case 's':
|
---|
563 | if (!strcmp(ValueUnion.psz, "default"))
|
---|
564 | enmStyle = kAsmStyle_Default;
|
---|
565 | else if (!strcmp(ValueUnion.psz, "yasm"))
|
---|
566 | enmStyle = kAsmStyle_yasm;
|
---|
567 | else if (!strcmp(ValueUnion.psz, "masm"))
|
---|
568 | {
|
---|
569 | enmStyle = kAsmStyle_masm;
|
---|
570 | RTStrmPrintf(g_pStdErr, "%s: masm style isn't implemented yet\n", argv0);
|
---|
571 | return 1;
|
---|
572 | }
|
---|
573 | else
|
---|
574 | {
|
---|
575 | RTStrmPrintf(g_pStdErr, "%s: unknown assembly style: %s\n", argv0, ValueUnion.psz);
|
---|
576 | return 1;
|
---|
577 | }
|
---|
578 | break;
|
---|
579 |
|
---|
580 | case 'u':
|
---|
581 | if (!strcmp(ValueUnion.psz, "fail"))
|
---|
582 | enmUndefOp = kUndefOp_Fail;
|
---|
583 | else if (!strcmp(ValueUnion.psz, "all"))
|
---|
584 | enmUndefOp = kUndefOp_All;
|
---|
585 | else if (!strcmp(ValueUnion.psz, "db"))
|
---|
586 | enmUndefOp = kUndefOp_DefineByte;
|
---|
587 | else
|
---|
588 | {
|
---|
589 | RTStrmPrintf(g_pStdErr, "%s: unknown undefined opcode handling method: %s\n", argv0, ValueUnion.psz);
|
---|
590 | return 1;
|
---|
591 | }
|
---|
592 | break;
|
---|
593 |
|
---|
594 | case 'x':
|
---|
595 | fHexBytes = true;
|
---|
596 | break;
|
---|
597 |
|
---|
598 | case 'V':
|
---|
599 | RTPrintf("$Revision: $\n");
|
---|
600 | return 0;
|
---|
601 |
|
---|
602 | default:
|
---|
603 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
604 | }
|
---|
605 | }
|
---|
606 | int iArg = GetState.iNext - 1; /** @todo Not pretty, add RTGetOptInit flag for this. */
|
---|
607 | if (iArg >= argc)
|
---|
608 | return Usage(argv0);
|
---|
609 |
|
---|
610 | int rc = VINF_SUCCESS;
|
---|
611 | if (fHexBytes)
|
---|
612 | {
|
---|
613 | /*
|
---|
614 | * Convert the remaining arguments from a hex byte string into
|
---|
615 | * a buffer that we disassemble.
|
---|
616 | */
|
---|
617 | size_t cb = 0;
|
---|
618 | uint8_t *pb = NULL;
|
---|
619 | for ( ; iArg < argc; iArg++)
|
---|
620 | {
|
---|
621 | char ch2;
|
---|
622 | const char *psz = argv[iArg];
|
---|
623 | while (*psz)
|
---|
624 | {
|
---|
625 | /** @todo this stuff belongs in IPRT, same stuff as mac address reading. Could be reused for IPv6 with a different item size.*/
|
---|
626 | /* skip white space, and for the benefit of linux panics '<' and '>'. */
|
---|
627 | while (RT_C_IS_SPACE(ch2 = *psz) || ch2 == '<' || ch2 == '>')
|
---|
628 | {
|
---|
629 | if (ch2 == '<')
|
---|
630 | uHighlightAddr = uAddress + cb;
|
---|
631 | psz++;
|
---|
632 | }
|
---|
633 | if (!ch2)
|
---|
634 | break;
|
---|
635 |
|
---|
636 | /* one digit followed by a space or EOS, or two digits. */
|
---|
637 | int iNum = HexDigitToNum(*psz++);
|
---|
638 | if (iNum == -1)
|
---|
639 | return 1;
|
---|
640 | if (!RT_C_IS_SPACE(ch2 = *psz) && ch2 != '\0' && ch2 != '>')
|
---|
641 | {
|
---|
642 | int iDigit = HexDigitToNum(*psz++);
|
---|
643 | if (iDigit == -1)
|
---|
644 | return 1;
|
---|
645 | iNum = iNum * 16 + iDigit;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /* add the byte */
|
---|
649 | if (!(cb % 4 /*64*/))
|
---|
650 | {
|
---|
651 | pb = (uint8_t *)RTMemRealloc(pb, cb + 64);
|
---|
652 | if (!pb)
|
---|
653 | {
|
---|
654 | RTPrintf("%s: error: RTMemRealloc failed\n", argv[0]);
|
---|
655 | return 1;
|
---|
656 | }
|
---|
657 | }
|
---|
658 | pb[cb++] = (uint8_t)iNum;
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 | /*
|
---|
663 | * Disassemble it.
|
---|
664 | */
|
---|
665 | rc = MyDisasmBlock(argv0, enmCpuMode, uAddress, uHighlightAddr, pb, cb, enmStyle, fListing, enmUndefOp);
|
---|
666 | }
|
---|
667 | else
|
---|
668 | {
|
---|
669 | /*
|
---|
670 | * Process the files.
|
---|
671 | */
|
---|
672 | for ( ; iArg < argc; iArg++)
|
---|
673 | {
|
---|
674 | /*
|
---|
675 | * Read the file into memory.
|
---|
676 | */
|
---|
677 | void *pvFile;
|
---|
678 | size_t cbFile;
|
---|
679 | rc = RTFileReadAllEx(argv[iArg], off, cbMax, RTFILE_RDALL_O_DENY_NONE, &pvFile, &cbFile);
|
---|
680 | if (RT_FAILURE(rc))
|
---|
681 | {
|
---|
682 | RTStrmPrintf(g_pStdErr, "%s: %s: %Rrc\n", argv0, argv[iArg], rc);
|
---|
683 | break;
|
---|
684 | }
|
---|
685 |
|
---|
686 | /*
|
---|
687 | * Disassemble it.
|
---|
688 | */
|
---|
689 | rc = MyDisasmBlock(argv0, enmCpuMode, uAddress, uHighlightAddr, (uint8_t *)pvFile, cbFile, enmStyle, fListing, enmUndefOp);
|
---|
690 | if (RT_FAILURE(rc))
|
---|
691 | break;
|
---|
692 | }
|
---|
693 | }
|
---|
694 |
|
---|
695 | return RT_SUCCESS(rc) ? 0 : 1;
|
---|
696 | }
|
---|
697 |
|
---|