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