tstDisasm-2.cpp revision fe95fa9fc2a364e3d678307971cc900b89f5c94a
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Testcase - Generic Disassembler Tool.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2008 Oracle Corporation
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * available from http://www.virtualbox.org. This file is free software;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * General Public License (GPL) as published by the Free Software
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync/*******************************************************************************
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync* Header Files *
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync*******************************************************************************/
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync/*******************************************************************************
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync* Structures and Typedefs *
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync*******************************************************************************/
8599686860198730ae53d5895386d1b57dbc060evboxsynctypedef enum { kAsmStyle_Default, kAsmStyle_yasm, kAsmStyle_masm, kAsmStyle_gas, kAsmStyle_invalid } ASMSTYLE;
7748b9362d6a39df9045d5d05ccb57871145a649vboxsynctypedef enum { kUndefOp_Fail, kUndefOp_All, kUndefOp_DefineByte, kUndefOp_End } UNDEFOPHANDLING;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsynctypedef struct MYDISSTATE
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync uint64_t uAddress; /**< The current instruction address. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync uint8_t *pbInstr; /**< The current instruction (pointer). */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync uint32_t cbInstr; /**< The size of the current instruction. */
7748b9362d6a39df9045d5d05ccb57871145a649vboxsync bool fUndefOp; /**< Whether the current instruction is really an undefined opcode.*/
7748b9362d6a39df9045d5d05ccb57871145a649vboxsync UNDEFOPHANDLING enmUndefOp; /**< How to treat undefined opcodes. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync size_t cbLeft; /**< The number of bytes left. (read) */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync uint64_t uNextAddr; /**< The address of the next byte. (read) */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync char szLine[256]; /**< The disassembler text output. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Default style.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param pState The disassembler state.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsyncstatic void MyDisasDefaultFormatter(PMYDISSTATE pState)
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Yasm style.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param pState The disassembler state.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsyncstatic void MyDisasYasmFormatter(PMYDISSTATE pState)
8599686860198730ae53d5895386d1b57dbc060evboxsync /* a very quick hack. */
8fa1e8a8a8702ee38195fbe7ad5959bf971eb93bvboxsync strcpy(szTmp, RTStrStripL(strchr(pState->szLine, ':') + 1));
de6ba1989c812829372828f1801b232b3e7f09bfvboxsync size_t cch = DISFormatYasmEx(&pState->Cpu, szTmp, sizeof(szTmp),
92473d1de9ab080ff886ad61a4d908f7c3429608vboxsync DIS_FMT_FLAGS_STRICT | DIS_FMT_FLAGS_ADDR_RIGHT | DIS_FMT_FLAGS_ADDR_COMMENT
de6ba1989c812829372828f1801b232b3e7f09bfvboxsync | DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_BYTES_COMMENT | DIS_FMT_FLAGS_BYTES_SPACED,
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Masm style.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param pState The disassembler state.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsyncstatic void MyDisasMasmFormatter(PMYDISSTATE pState)
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("masm not implemented: %s", pState->szLine);
8599686860198730ae53d5895386d1b57dbc060evboxsync * This is a temporary workaround for catching a few illegal opcodes
8599686860198730ae53d5895386d1b57dbc060evboxsync * that the disassembler is currently letting thru, just enough to make
8599686860198730ae53d5895386d1b57dbc060evboxsync * the assemblers happy.
8599686860198730ae53d5895386d1b57dbc060evboxsync * We're too close to a release to dare mess with these things now as
8599686860198730ae53d5895386d1b57dbc060evboxsync * they may consequences for performance and let alone introduce bugs.
8599686860198730ae53d5895386d1b57dbc060evboxsync * @returns true if it's valid. false if it isn't.
8599686860198730ae53d5895386d1b57dbc060evboxsync * @param pCpu The disassembler output.
8599686860198730ae53d5895386d1b57dbc060evboxsyncstatic bool MyDisasIsValidInstruction(DISCPUSTATE const *pCpu)
8599686860198730ae53d5895386d1b57dbc060evboxsync /* These doesn't take memory operands. */
8599686860198730ae53d5895386d1b57dbc060evboxsync return false;
8599686860198730ae53d5895386d1b57dbc060evboxsync /* The 0x8f /0 variant of this instruction doesn't get its /r value verified. */
8599686860198730ae53d5895386d1b57dbc060evboxsync return false;
8599686860198730ae53d5895386d1b57dbc060evboxsync /* The 0xc6 /0 and 0xc7 /0 variants of this instruction don't get their /r values verified. */
8599686860198730ae53d5895386d1b57dbc060evboxsync return false;
8599686860198730ae53d5895386d1b57dbc060evboxsync return true;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Callback for reading bytes.
8599686860198730ae53d5895386d1b57dbc060evboxsync * @todo This should check that the disassembler doesn't do unnecessary reads,
8599686860198730ae53d5895386d1b57dbc060evboxsync * however the current doesn't do this and is just complicated...
99cd1ce586a12bf6b8c6084cbcdebe8fe3553cc2vboxsyncstatic DECLCALLBACK(int) MyDisasInstrRead(PDISCPUSTATE pDisState, uint8_t *pbDst, RTUINTPTR uSrcAddr, uint32_t cbToRead)
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Straight forward reading.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Jumping up the stream.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * This occurs when the byte sequence is added to the output string.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync /* reset the stream. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync /* skip ahead. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync /* do the reading. */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTStrmPrintf(g_pStdErr, "Reading before current instruction!\n");
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Disassembles a block of memory.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @returns VBox status code.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param argv0 Program name (for errors and warnings).
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param enmCpuMode The cpu mode to disassemble in.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param uAddress The address we're starting to disassemble at.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param uHighlightAddr The address of the instruction that should be
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * highlighted. Pass UINT64_MAX to keep quiet.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param pbFile Where to start disassemble.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param cbFile How much to disassemble.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param enmStyle The assembly output style.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param fListing Whether to print in a listing like mode.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync * @param enmUndefOp How to deal with undefined opcodes.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsyncstatic int MyDisasmBlock(const char *argv0, DISCPUMODE enmCpuMode, uint64_t uAddress,
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync uint64_t uHighlightAddr, uint8_t *pbFile, size_t cbFile,
7748b9362d6a39df9045d5d05ccb57871145a649vboxsync ASMSTYLE enmStyle, bool fListing, UNDEFOPHANDLING enmUndefOp)
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Initialize the CPU context.
98502a585f6eda30527b54f7df18dd3de3d3d7c6vboxsync RTPrintf(" BITS %d\n", enmCpuMode == DISCPUMODE_16BIT ? 16 : enmCpuMode == DISCPUMODE_32BIT ? 32 : 64);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * The loop.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Disassemble it.
66b5c940dde8fe7fc7ab14b7c7f1b65d852fb4ddvboxsync int rc = DISInstrToStrWithReader(State.uAddress, enmCpuMode, MyDisasInstrRead, &State,
66b5c940dde8fe7fc7ab14b7c7f1b65d852fb4ddvboxsync &State.Cpu, &State.cbInstr, State.szLine, sizeof(State.szLine));
8599686860198730ae53d5895386d1b57dbc060evboxsync if (State.fUndefOp && State.enmUndefOp == kUndefOp_DefineByte)
99cd1ce586a12bf6b8c6084cbcdebe8fe3553cc2vboxsync State.Cpu.pfnReadBytes(&State.Cpu, &State.Cpu.abInstr[0], State.uAddress, 1);
99cd1ce586a12bf6b8c6084cbcdebe8fe3553cc2vboxsync RTPrintf(off ? ", %03xh" : " %03xh", State.Cpu.abInstr[off]);
8599686860198730ae53d5895386d1b57dbc060evboxsync else if (!State.fUndefOp && State.enmUndefOp == kUndefOp_All)
fe95fa9fc2a364e3d678307971cc900b89f5c94avboxsync RTPrintf("%s: error at %#RX64: unexpected valid instruction (op=%d)\n", argv0, State.uAddress, State.Cpu.pCurInstr->uOpcode);
7748b9362d6a39df9045d5d05ccb57871145a649vboxsync else if (State.fUndefOp && State.enmUndefOp == kUndefOp_Fail)
fe95fa9fc2a364e3d678307971cc900b89f5c94avboxsync RTPrintf("%s: error at %#RX64: undefined opcode (op=%d)\n", argv0, State.uAddress, State.Cpu.pCurInstr->uOpcode);
8599686860198730ae53d5895386d1b57dbc060evboxsync /* Use db for odd encodings that we can't make the assembler use. */
99cd1ce586a12bf6b8c6084cbcdebe8fe3553cc2vboxsync RTPrintf(off ? ", %03xh" : " %03xh", State.Cpu.abInstr[off]);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("%s: error at %#RX64: read beyond the end (%Rrc)\n", argv0, State.uAddress, rc);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("%s: error at %#RX64: %Rrc cbInstr=%d\n", argv0, State.uAddress, rc, State.cbInstr);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync RTPrintf("%s: error at %#RX64: %Rrc cbInstr=%d!\n", argv0, State.uAddress, rc, State.cbInstr);
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync /* Highlight this instruction? */
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync if (uHighlightAddr - State.uAddress < State.cbInstr)
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync * Converts a hex char to a number.
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync * @returns 0..15 on success, -1 on failure.
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync * @param ch The character.
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync case '0': return 0;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Prints usage info.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @returns 1.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * @param argv0 The program name.
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync"usage: %s [options] <file1> [file2..fileN]\n"
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync" or: %s [options] <-x|--hex-bytes> <hex byte> [more hex..]\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" or: %s <--help|-h>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync"Options:\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --address|-a <address>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The base address. Default: 0\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --max-bytes|-b <bytes>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The maximum number of bytes to disassemble. Default: 1GB\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --cpumode|-c <16|32|64>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The cpu mode. Default: 32\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --listing|-l, --no-listing|-L\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" Enables or disables listing mode. Default: --no-listing\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --offset|-o <offset>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The file offset at which to start disassembling. Default: 0\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" --style|-s <default|yasm|masm>\n"
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync" The assembly output style. Default: default\n"
7748b9362d6a39df9045d5d05ccb57871145a649vboxsync" --undef-op|-u <fail|all|db>\n"
7748b9362d6a39df9045d5d05ccb57871145a649vboxsync" How to treat undefined opcodes. Default: fail\n"
8fa1e8a8a8702ee38195fbe7ad5959bf971eb93bvboxsync const char * const argv0 = RTPathFilename(argv[0]);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync /* options */
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync bool fListing = true;
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync bool fHexBytes = false;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Parse arguments.
fb1975a6972d89de9e515bed0248db93f04ec9d8vboxsync RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
8fa1e8a8a8702ee38195fbe7ad5959bf971eb93bvboxsync RTStrmPrintf(g_pStdErr, "%s: Invalid CPU mode value %RU32\n", argv0, ValueUnion.u32);
8fa1e8a8a8702ee38195fbe7ad5959bf971eb93bvboxsync RTStrmPrintf(g_pStdErr, "%s: masm style isn't implemented yet\n", argv0);
8fa1e8a8a8702ee38195fbe7ad5959bf971eb93bvboxsync RTStrmPrintf(g_pStdErr, "%s: unknown assembly style: %s\n", argv0, ValueUnion.psz);
7748b9362d6a39df9045d5d05ccb57871145a649vboxsync RTStrmPrintf(g_pStdErr, "%s: unknown undefined opcode handling method: %s\n", argv0, ValueUnion.psz);
83dc9ca94cd3c31dabc33a35b945de124d43aaeavboxsync int iArg = GetState.iNext - 1; /** @todo Not pretty, add RTGetOptInit flag for this. */
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync * Convert the remaining arguments from a hex byte string into
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync * a buffer that we disassemble.
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync /** @todo this stuff belongs in IPRT, same stuff as mac address reading. Could be reused for IPv6 with a different item size.*/
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /* skip white space, and for the benefit of linux panics '<' and '>'. */
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync while (RT_C_IS_SPACE(ch2 = *psz) || ch2 == '<' || ch2 == '>')
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync /* one digit followed by a space or EOS, or two digits. */
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync if (!RT_C_IS_SPACE(ch2 = *psz) && ch2 != '\0' && ch2 != '>')
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync /* add the byte */
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync RTPrintf("%s: error: RTMemRealloc failed\n", argv[0]);
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Disassemble it.
e37bf11b9c725db1bb30da247fedebdb4ed20d29vboxsync rc = MyDisasmBlock(argv0, enmCpuMode, uAddress, uHighlightAddr, pb, cb, enmStyle, fListing, enmUndefOp);
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync * Process the files.
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync * Read the file into memory.
3f53db546002b7bd0fcfdfa6da646d518490888dvboxsync rc = RTFileReadAllEx(argv[iArg], off, cbMax, RTFILE_RDALL_O_DENY_NONE, &pvFile, &cbFile);
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync RTStrmPrintf(g_pStdErr, "%s: %s: %Rrc\n", argv0, argv[iArg], rc);
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync * Disassemble it.