1 | /* $Id: tstDisasm-2.cpp 41501 2012-05-30 15:58:23Z 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.alldomusa.eu.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 ; %08llu %s", szTmp, pState->uAddress, 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(RTUINTPTR uSrcAddr, uint8_t *pbDst, uint32_t cbRead, void *pvDisCpu)
|
---|
174 | {
|
---|
175 | PMYDISSTATE pState = (PMYDISSTATE)pvDisCpu;
|
---|
176 | if (RT_LIKELY( pState->uNextAddr == uSrcAddr
|
---|
177 | && pState->cbLeft >= cbRead))
|
---|
178 | {
|
---|
179 | /*
|
---|
180 | * Straight forward reading.
|
---|
181 | */
|
---|
182 | if (cbRead == 1)
|
---|
183 | {
|
---|
184 | pState->cbLeft--;
|
---|
185 | *pbDst = *pState->pbNext++;
|
---|
186 | pState->uNextAddr++;
|
---|
187 | }
|
---|
188 | else
|
---|
189 | {
|
---|
190 | memcpy(pbDst, pState->pbNext, cbRead);
|
---|
191 | pState->pbNext += cbRead;
|
---|
192 | pState->cbLeft -= cbRead;
|
---|
193 | pState->uNextAddr += cbRead;
|
---|
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, cbRead);
|
---|
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 >= cbRead)
|
---|
230 | {
|
---|
231 | memcpy(pbDst, pState->pbNext, cbRead);
|
---|
232 | pState->cbLeft -= cbRead;
|
---|
233 | pState->pbNext += cbRead;
|
---|
234 | pState->uNextAddr += cbRead;
|
---|
235 | }
|
---|
236 | else
|
---|
237 | {
|
---|
238 | if (pState->cbLeft > 0)
|
---|
239 | {
|
---|
240 | memcpy(pbDst, pState->pbNext, pState->cbLeft);
|
---|
241 | pbDst += pState->cbLeft;
|
---|
242 | cbRead -= (uint32_t)pState->cbLeft;
|
---|
243 | pState->pbNext += pState->cbLeft;
|
---|
244 | pState->uNextAddr += pState->cbLeft;
|
---|
245 | pState->cbLeft = 0;
|
---|
246 | }
|
---|
247 | memset(pbDst, 0xcc, cbRead);
|
---|
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, cbRead);
|
---|
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.Cpu.mode = enmCpuMode;
|
---|
289 | State.Cpu.pfnReadBytes = MyDisasInstrRead;
|
---|
290 | State.uAddress = uAddress;
|
---|
291 | State.pbInstr = pbFile;
|
---|
292 | State.cbInstr = 0;
|
---|
293 | State.enmUndefOp = enmUndefOp;
|
---|
294 | State.rc = VINF_SUCCESS;
|
---|
295 | State.cbLeft = cbFile;
|
---|
296 | State.pbNext = pbFile;
|
---|
297 | State.uNextAddr = uAddress;
|
---|
298 |
|
---|
299 | void (*pfnFormatter)(PMYDISSTATE pState);
|
---|
300 | switch (enmStyle)
|
---|
301 | {
|
---|
302 | case kAsmStyle_Default:
|
---|
303 | pfnFormatter = MyDisasDefaultFormatter;
|
---|
304 | break;
|
---|
305 |
|
---|
306 | case kAsmStyle_yasm:
|
---|
307 | RTPrintf(" BITS %d\n", enmCpuMode == CPUMODE_16BIT ? 16 : enmCpuMode == CPUMODE_32BIT ? 32 : 64);
|
---|
308 | pfnFormatter = MyDisasYasmFormatter;
|
---|
309 | break;
|
---|
310 |
|
---|
311 | case kAsmStyle_masm:
|
---|
312 | pfnFormatter = MyDisasMasmFormatter;
|
---|
313 | break;
|
---|
314 |
|
---|
315 | default:
|
---|
316 | AssertFailedReturn(VERR_INTERNAL_ERROR);
|
---|
317 | }
|
---|
318 |
|
---|
319 | /*
|
---|
320 | * The loop.
|
---|
321 | */
|
---|
322 | int rcRet = VINF_SUCCESS;
|
---|
323 | while (State.cbLeft > 0)
|
---|
324 | {
|
---|
325 | /*
|
---|
326 | * Disassemble it.
|
---|
327 | */
|
---|
328 | State.cbInstr = 0;
|
---|
329 | State.cbLeft += State.pbNext - State.pbInstr;
|
---|
330 | State.uNextAddr = State.uAddress;
|
---|
331 | State.pbNext = State.pbInstr;
|
---|
332 |
|
---|
333 | int rc = DISInstr(&State.Cpu, State.uAddress, 0, &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 | RTPrintf(" db");
|
---|
348 | if (!State.cbInstr)
|
---|
349 | State.cbInstr = 1;
|
---|
350 | for (unsigned off = 0; off < State.cbInstr; off++)
|
---|
351 | {
|
---|
352 | uint8_t b;
|
---|
353 | State.Cpu.pfnReadBytes(State.uAddress + off, &b, 1, &State.Cpu);
|
---|
354 | RTPrintf(off ? ", %03xh" : " %03xh", b);
|
---|
355 | }
|
---|
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 | {
|
---|
379 | uint8_t b;
|
---|
380 | State.Cpu.pfnReadBytes(State.uAddress + off, &b, 1, &State.Cpu);
|
---|
381 | RTPrintf(off ? ", %03xh" : " %03xh", b);
|
---|
382 | }
|
---|
383 | RTPrintf(" ; ");
|
---|
384 | }
|
---|
385 |
|
---|
386 | pfnFormatter(&State);
|
---|
387 | }
|
---|
388 | }
|
---|
389 | else
|
---|
390 | {
|
---|
391 | State.cbInstr = State.pbNext - State.pbInstr;
|
---|
392 | if (!State.cbLeft)
|
---|
393 | RTPrintf("%s: error at %#RX64: read beyond the end (%Rrc)\n", argv0, State.uAddress, rc);
|
---|
394 | else if (State.cbInstr)
|
---|
395 | RTPrintf("%s: error at %#RX64: %Rrc cbInstr=%d\n", argv0, State.uAddress, rc, State.cbInstr);
|
---|
396 | else
|
---|
397 | {
|
---|
398 | RTPrintf("%s: error at %#RX64: %Rrc cbInstr=%d!\n", argv0, State.uAddress, rc, State.cbInstr);
|
---|
399 | if (rcRet == VINF_SUCCESS)
|
---|
400 | rcRet = rc;
|
---|
401 | break;
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | /* Highlight this instruction? */
|
---|
406 | if (uHighlightAddr - State.uAddress < State.cbInstr)
|
---|
407 | RTPrintf("; ^^^^^^^^^^^^^^^^^^^^^\n");
|
---|
408 |
|
---|
409 | /* next */
|
---|
410 | State.uAddress += State.cbInstr;
|
---|
411 | State.pbInstr += State.cbInstr;
|
---|
412 | }
|
---|
413 |
|
---|
414 | return rcRet;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Converts a hex char to a number.
|
---|
419 | *
|
---|
420 | * @returns 0..15 on success, -1 on failure.
|
---|
421 | * @param ch The character.
|
---|
422 | */
|
---|
423 | static int HexDigitToNum(char ch)
|
---|
424 | {
|
---|
425 | switch (ch)
|
---|
426 | {
|
---|
427 | case '0': return 0;
|
---|
428 | case '1': return 1;
|
---|
429 | case '2': return 2;
|
---|
430 | case '3': return 3;
|
---|
431 | case '4': return 4;
|
---|
432 | case '5': return 5;
|
---|
433 | case '6': return 6;
|
---|
434 | case '7': return 7;
|
---|
435 | case '8': return 8;
|
---|
436 | case '9': return 9;
|
---|
437 | case 'A':
|
---|
438 | case 'a': return 0xa;
|
---|
439 | case 'B':
|
---|
440 | case 'b': return 0xb;
|
---|
441 | case 'C':
|
---|
442 | case 'c': return 0xc;
|
---|
443 | case 'D':
|
---|
444 | case 'd': return 0xd;
|
---|
445 | case 'E':
|
---|
446 | case 'e': return 0xe;
|
---|
447 | case 'F':
|
---|
448 | case 'f': return 0xf;
|
---|
449 | default:
|
---|
450 | RTPrintf("error: Invalid hex digig '%c'\n", ch);
|
---|
451 | return -1;
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Prints usage info.
|
---|
457 | *
|
---|
458 | * @returns 1.
|
---|
459 | * @param argv0 The program name.
|
---|
460 | */
|
---|
461 | static int Usage(const char *argv0)
|
---|
462 | {
|
---|
463 | RTStrmPrintf(g_pStdErr,
|
---|
464 | "usage: %s [options] <file1> [file2..fileN]\n"
|
---|
465 | " or: %s [options] <-x|--hex-bytes> <hex byte> [more hex..]\n"
|
---|
466 | " or: %s <--help|-h>\n"
|
---|
467 | "\n"
|
---|
468 | "Options:\n"
|
---|
469 | " --address|-a <address>\n"
|
---|
470 | " The base address. Default: 0\n"
|
---|
471 | " --max-bytes|-b <bytes>\n"
|
---|
472 | " The maximum number of bytes to disassemble. Default: 1GB\n"
|
---|
473 | " --cpumode|-c <16|32|64>\n"
|
---|
474 | " The cpu mode. Default: 32\n"
|
---|
475 | " --listing|-l, --no-listing|-L\n"
|
---|
476 | " Enables or disables listing mode. Default: --no-listing\n"
|
---|
477 | " --offset|-o <offset>\n"
|
---|
478 | " The file offset at which to start disassembling. Default: 0\n"
|
---|
479 | " --style|-s <default|yasm|masm>\n"
|
---|
480 | " The assembly output style. Default: default\n"
|
---|
481 | " --undef-op|-u <fail|all|db>\n"
|
---|
482 | " How to treat undefined opcodes. Default: fail\n"
|
---|
483 | , argv0, argv0);
|
---|
484 | return 1;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | int main(int argc, char **argv)
|
---|
489 | {
|
---|
490 | RTR3InitExe(argc, &argv, 0);
|
---|
491 | const char * const argv0 = RTPathFilename(argv[0]);
|
---|
492 |
|
---|
493 | /* options */
|
---|
494 | uint64_t uAddress = 0;
|
---|
495 | uint64_t uHighlightAddr = UINT64_MAX;
|
---|
496 | ASMSTYLE enmStyle = kAsmStyle_Default;
|
---|
497 | UNDEFOPHANDLING enmUndefOp = kUndefOp_Fail;
|
---|
498 | bool fListing = true;
|
---|
499 | DISCPUMODE enmCpuMode = CPUMODE_32BIT;
|
---|
500 | RTFOFF off = 0;
|
---|
501 | RTFOFF cbMax = _1G;
|
---|
502 | bool fHexBytes = false;
|
---|
503 |
|
---|
504 | /*
|
---|
505 | * Parse arguments.
|
---|
506 | */
|
---|
507 | static const RTGETOPTDEF g_aOptions[] =
|
---|
508 | {
|
---|
509 | { "--address", 'a', RTGETOPT_REQ_UINT64 },
|
---|
510 | { "--cpumode", 'c', RTGETOPT_REQ_UINT32 },
|
---|
511 | { "--bytes", 'b', RTGETOPT_REQ_INT64 },
|
---|
512 | { "--listing", 'l', RTGETOPT_REQ_NOTHING },
|
---|
513 | { "--no-listing", 'L', RTGETOPT_REQ_NOTHING },
|
---|
514 | { "--offset", 'o', RTGETOPT_REQ_INT64 },
|
---|
515 | { "--style", 's', RTGETOPT_REQ_STRING },
|
---|
516 | { "--undef-op", 'u', RTGETOPT_REQ_STRING },
|
---|
517 | { "--hex-bytes", 'x', RTGETOPT_REQ_NOTHING },
|
---|
518 | };
|
---|
519 |
|
---|
520 | int ch;
|
---|
521 | RTGETOPTUNION ValueUnion;
|
---|
522 | RTGETOPTSTATE GetState;
|
---|
523 | RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
524 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
525 | && ch != VINF_GETOPT_NOT_OPTION)
|
---|
526 | {
|
---|
527 | switch (ch)
|
---|
528 | {
|
---|
529 | case 'a':
|
---|
530 | uAddress = ValueUnion.u64;
|
---|
531 | break;
|
---|
532 |
|
---|
533 | case 'b':
|
---|
534 | cbMax = ValueUnion.i64;
|
---|
535 | break;
|
---|
536 |
|
---|
537 | case 'c':
|
---|
538 | if (ValueUnion.u32 == 16)
|
---|
539 | enmCpuMode = CPUMODE_16BIT;
|
---|
540 | else if (ValueUnion.u32 == 32)
|
---|
541 | enmCpuMode = CPUMODE_32BIT;
|
---|
542 | else if (ValueUnion.u32 == 64)
|
---|
543 | enmCpuMode = CPUMODE_64BIT;
|
---|
544 | else
|
---|
545 | {
|
---|
546 | RTStrmPrintf(g_pStdErr, "%s: Invalid CPU mode value %RU32\n", argv0, ValueUnion.u32);
|
---|
547 | return 1;
|
---|
548 | }
|
---|
549 | break;
|
---|
550 |
|
---|
551 | case 'h':
|
---|
552 | return Usage(argv0);
|
---|
553 |
|
---|
554 | case 'l':
|
---|
555 | fListing = true;
|
---|
556 | break;
|
---|
557 |
|
---|
558 | case 'L':
|
---|
559 | fListing = false;
|
---|
560 | break;
|
---|
561 |
|
---|
562 | case 'o':
|
---|
563 | off = ValueUnion.i64;
|
---|
564 | break;
|
---|
565 |
|
---|
566 | case 's':
|
---|
567 | if (!strcmp(ValueUnion.psz, "default"))
|
---|
568 | enmStyle = kAsmStyle_Default;
|
---|
569 | else if (!strcmp(ValueUnion.psz, "yasm"))
|
---|
570 | enmStyle = kAsmStyle_yasm;
|
---|
571 | else if (!strcmp(ValueUnion.psz, "masm"))
|
---|
572 | {
|
---|
573 | enmStyle = kAsmStyle_masm;
|
---|
574 | RTStrmPrintf(g_pStdErr, "%s: masm style isn't implemented yet\n", argv0);
|
---|
575 | return 1;
|
---|
576 | }
|
---|
577 | else
|
---|
578 | {
|
---|
579 | RTStrmPrintf(g_pStdErr, "%s: unknown assembly style: %s\n", argv0, ValueUnion.psz);
|
---|
580 | return 1;
|
---|
581 | }
|
---|
582 | break;
|
---|
583 |
|
---|
584 | case 'u':
|
---|
585 | if (!strcmp(ValueUnion.psz, "fail"))
|
---|
586 | enmUndefOp = kUndefOp_Fail;
|
---|
587 | else if (!strcmp(ValueUnion.psz, "all"))
|
---|
588 | enmUndefOp = kUndefOp_All;
|
---|
589 | else if (!strcmp(ValueUnion.psz, "db"))
|
---|
590 | enmUndefOp = kUndefOp_DefineByte;
|
---|
591 | else
|
---|
592 | {
|
---|
593 | RTStrmPrintf(g_pStdErr, "%s: unknown undefined opcode handling method: %s\n", argv0, ValueUnion.psz);
|
---|
594 | return 1;
|
---|
595 | }
|
---|
596 | break;
|
---|
597 |
|
---|
598 | case 'x':
|
---|
599 | fHexBytes = true;
|
---|
600 | break;
|
---|
601 |
|
---|
602 | case 'V':
|
---|
603 | RTPrintf("$Revision: $\n");
|
---|
604 | return 0;
|
---|
605 |
|
---|
606 | default:
|
---|
607 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
608 | }
|
---|
609 | }
|
---|
610 | int iArg = GetState.iNext - 1; /** @todo Not pretty, add RTGetOptInit flag for this. */
|
---|
611 | if (iArg >= argc)
|
---|
612 | return Usage(argv0);
|
---|
613 |
|
---|
614 | int rc = VINF_SUCCESS;
|
---|
615 | if (fHexBytes)
|
---|
616 | {
|
---|
617 | /*
|
---|
618 | * Convert the remaining arguments from a hex byte string into
|
---|
619 | * a buffer that we disassemble.
|
---|
620 | */
|
---|
621 | size_t cb = 0;
|
---|
622 | uint8_t *pb = NULL;
|
---|
623 | for ( ; iArg < argc; iArg++)
|
---|
624 | {
|
---|
625 | char ch2;
|
---|
626 | const char *psz = argv[iArg];
|
---|
627 | while (*psz)
|
---|
628 | {
|
---|
629 | /** @todo this stuff belongs in IPRT, same stuff as mac address reading. Could be reused for IPv6 with a different item size.*/
|
---|
630 | /* skip white space, and for the benefit of linux panics '<' and '>'. */
|
---|
631 | while (RT_C_IS_SPACE(ch2 = *psz) || ch2 == '<' || ch2 == '>')
|
---|
632 | {
|
---|
633 | if (ch2 == '<')
|
---|
634 | uHighlightAddr = uAddress + cb;
|
---|
635 | psz++;
|
---|
636 | }
|
---|
637 | if (!ch2)
|
---|
638 | break;
|
---|
639 |
|
---|
640 | /* one digit followed by a space or EOS, or two digits. */
|
---|
641 | int iNum = HexDigitToNum(*psz++);
|
---|
642 | if (iNum == -1)
|
---|
643 | return 1;
|
---|
644 | if (!RT_C_IS_SPACE(ch2 = *psz) && ch2 != '\0' && ch2 != '>')
|
---|
645 | {
|
---|
646 | int iDigit = HexDigitToNum(*psz++);
|
---|
647 | if (iDigit == -1)
|
---|
648 | return 1;
|
---|
649 | iNum = iNum * 16 + iDigit;
|
---|
650 | }
|
---|
651 |
|
---|
652 | /* add the byte */
|
---|
653 | if (!(cb % 4 /*64*/))
|
---|
654 | {
|
---|
655 | pb = (uint8_t *)RTMemRealloc(pb, cb + 64);
|
---|
656 | if (!pb)
|
---|
657 | {
|
---|
658 | RTPrintf("%s: error: RTMemRealloc failed\n", argv[0]);
|
---|
659 | return 1;
|
---|
660 | }
|
---|
661 | }
|
---|
662 | pb[cb++] = (uint8_t)iNum;
|
---|
663 | }
|
---|
664 | }
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * Disassemble it.
|
---|
668 | */
|
---|
669 | rc = MyDisasmBlock(argv0, enmCpuMode, uAddress, uHighlightAddr, pb, cb, enmStyle, fListing, enmUndefOp);
|
---|
670 | }
|
---|
671 | else
|
---|
672 | {
|
---|
673 | /*
|
---|
674 | * Process the files.
|
---|
675 | */
|
---|
676 | for ( ; iArg < argc; iArg++)
|
---|
677 | {
|
---|
678 | /*
|
---|
679 | * Read the file into memory.
|
---|
680 | */
|
---|
681 | void *pvFile;
|
---|
682 | size_t cbFile;
|
---|
683 | rc = RTFileReadAllEx(argv[iArg], off, cbMax, RTFILE_RDALL_O_DENY_NONE, &pvFile, &cbFile);
|
---|
684 | if (RT_FAILURE(rc))
|
---|
685 | {
|
---|
686 | RTStrmPrintf(g_pStdErr, "%s: %s: %Rrc\n", argv0, argv[iArg], rc);
|
---|
687 | break;
|
---|
688 | }
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * Disassemble it.
|
---|
692 | */
|
---|
693 | rc = MyDisasmBlock(argv0, enmCpuMode, uAddress, uHighlightAddr, (uint8_t *)pvFile, cbFile, enmStyle, fListing, enmUndefOp);
|
---|
694 | if (RT_FAILURE(rc))
|
---|
695 | break;
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 | return RT_SUCCESS(rc) ? 0 : 1;
|
---|
700 | }
|
---|
701 |
|
---|