7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Testcase - Generic Disassembler Tool.
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2008-2012 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;
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));
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync size_t cch = DISFormatYasmEx(&pState->Dis, 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.
377e22fd02a277efa645dfe4e1a327f4fa26c2aavboxsync * @param pDis The disassembler output.
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsyncstatic bool MyDisasIsValidInstruction(DISSTATE const *pDis)
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;
e20d4a49a29e2814ae21d3710e9b6e2e77afd94dvboxsync * @interface_method_impl{FNDISREADBYTES}
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsyncstatic DECLCALLBACK(int) MyDisasInstrRead(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync RTUINTPTR uSrcAddr = pState->Dis.uInstrAddr + offInstr;
7bf0220c3332700233120b513c9b4ba20a0caa9bvboxsync * Straight forward reading.
10f18618390096a9c968016b6ca94d77b91618fbvboxsync //size_t cbToRead = cbMaxRead;
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync memcpy(&pState->Dis.abInstr[offInstr], pState->pbNext, cbToRead);
10f18618390096a9c968016b6ca94d77b91618fbvboxsync * Reading too much.
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync memcpy(&pState->Dis.abInstr[offInstr], pState->pbNext, pState->cbLeft);
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync memset(&pState->Dis.abInstr[offInstr], 0xcc, cbMinRead);
10f18618390096a9c968016b6ca94d77b91618fbvboxsync * Non-sequential read, that's an error.
10f18618390096a9c968016b6ca94d77b91618fbvboxsync RTStrmPrintf(g_pStdErr, "Reading before current instruction!\n");
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync memset(&pState->Dis.abInstr[offInstr], 0x90, cbMinRead);
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,
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync &State.Dis, &State.cbInstr, State.szLine, sizeof(State.szLine));
8599686860198730ae53d5895386d1b57dbc060evboxsync if (State.fUndefOp && State.enmUndefOp == kUndefOp_DefineByte)
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync RTPrintf(off ? ", %03xh" : " %03xh", State.Dis.abInstr[off]);
8599686860198730ae53d5895386d1b57dbc060evboxsync else if (!State.fUndefOp && State.enmUndefOp == kUndefOp_All)
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync RTPrintf("%s: error at %#RX64: unexpected valid instruction (op=%d)\n", argv0, State.uAddress, State.Dis.pCurInstr->uOpcode);
7748b9362d6a39df9045d5d05ccb57871145a649vboxsync else if (State.fUndefOp && State.enmUndefOp == kUndefOp_Fail)
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync RTPrintf("%s: error at %#RX64: undefined opcode (op=%d)\n", argv0, State.uAddress, State.Dis.pCurInstr->uOpcode);
8599686860198730ae53d5895386d1b57dbc060evboxsync /* Use db for odd encodings that we can't make the assembler use. */
6d51216d13610f142b377a8e15c6c51adb83ba1bvboxsync RTPrintf(off ? ", %03xh" : " %03xh", State.Dis.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)
8ed69d0690530fb94a9d77e642c563a092fe6643vboxsync /* Check that the size-only mode returns the smae size on success. */
8ed69d0690530fb94a9d77e642c563a092fe6643vboxsync memcpy(abInstr, State.Dis.abInstr, sizeof(State.Dis.abInstr));
2e65d8da6ef2ef03491ebb4c3f065f10dd912b36vboxsync int rcOnly = DISInstrWithPrefetchedBytes(State.uAddress, enmCpuMode, 0 /*fFilter - none */,
2e65d8da6ef2ef03491ebb4c3f065f10dd912b36vboxsync abInstr, State.Dis.cbCachedInstr, MyDisasInstrRead, &State,
8ed69d0690530fb94a9d77e642c563a092fe6643vboxsync RTPrintf("; Instruction size only check failed rc=%Rrc cbInstrOnly=%#x exepcted %Rrc and %#x\n",
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 * 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 '>'. */
b3a342577cc87b05977a5bfc90924c9e80e0877bvboxsync while (RT_C_IS_SPACE(ch2 = *psz) || ch2 == '<' || ch2 == '>' || ch2 == ',' || ch2 == ';')
b3a342577cc87b05977a5bfc90924c9e80e0877bvboxsync if (ch2 == '0' && (psz[1] == 'x' || psz[1] == 'X'))
57a94b9fea6a6400f7a80e322e84b5b453c3bff0vboxsync /* one digit followed by a space or EOS, or two digits. */
b3a342577cc87b05977a5bfc90924c9e80e0877bvboxsync if (!RT_C_IS_SPACE(ch2 = *psz) && ch2 != '\0' && ch2 != '>' && ch2 != ',' && 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.