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