2015-05-16 11:05:17 +08:00
|
|
|
|
/***************************************************************************************************
|
2014-10-25 05:11:16 +08:00
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
Zyan Disassembler Engine
|
2014-10-25 05:11:16 +08:00
|
|
|
|
Version 1.0
|
|
|
|
|
|
|
|
|
|
Remarks : Freeware, Copyright must be included
|
|
|
|
|
|
|
|
|
|
Original Author : Florian Bernd
|
2015-05-16 11:05:17 +08:00
|
|
|
|
Modifications : Joel H<EFBFBD>ner
|
2014-10-25 05:11:16 +08:00
|
|
|
|
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2015-05-16 11:05:17 +08:00
|
|
|
|
*
|
2014-10-25 05:11:16 +08:00
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2015-05-16 11:05:17 +08:00
|
|
|
|
*
|
2014-10-25 05:11:16 +08:00
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
***************************************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "ZydisInstructionFormatter.hpp"
|
|
|
|
|
#include "ZydisUtils.hpp"
|
2014-10-25 05:11:16 +08:00
|
|
|
|
#include <cstdarg>
|
2014-10-30 23:36:25 +08:00
|
|
|
|
#include <cctype>
|
2015-02-05 19:39:16 +08:00
|
|
|
|
#include <cstdio>
|
2015-05-21 23:01:07 +08:00
|
|
|
|
#include <cstring>
|
2014-10-25 05:11:16 +08:00
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
namespace Zydis
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
/* BaseInstructionFormatter ================================================================ */
|
2014-11-03 22:15:48 +08:00
|
|
|
|
|
2015-05-22 03:42:06 +08:00
|
|
|
|
const char* BaseInstructionFormatter::m_registerStrings[] =
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
/* 8 bit general purpose registers */
|
|
|
|
|
"al", "cl", "dl", "bl",
|
|
|
|
|
"ah", "ch", "dh", "bh",
|
|
|
|
|
"spl", "bpl", "sil", "dil",
|
|
|
|
|
"r8b", "r9b", "r10b", "r11b",
|
|
|
|
|
"r12b", "r13b", "r14b", "r15b",
|
|
|
|
|
/* 16 bit general purpose registers */
|
|
|
|
|
"ax", "cx", "dx", "bx",
|
|
|
|
|
"sp", "bp", "si", "di",
|
|
|
|
|
"r8w", "r9w", "r10w", "r11w",
|
|
|
|
|
"r12w", "r13w", "r14w", "r15w",
|
|
|
|
|
/* 32 bit general purpose registers */
|
|
|
|
|
"eax", "ecx", "edx", "ebx",
|
|
|
|
|
"esp", "ebp", "esi", "edi",
|
|
|
|
|
"r8d", "r9d", "r10d", "r11d",
|
|
|
|
|
"r12d", "r13d", "r14d", "r15d",
|
|
|
|
|
/* 64 bit general purpose registers */
|
|
|
|
|
"rax", "rcx", "rdx", "rbx",
|
|
|
|
|
"rsp", "rbp", "rsi", "rdi",
|
|
|
|
|
"r8", "r9", "r10", "r11",
|
|
|
|
|
"r12", "r13", "r14", "r15",
|
|
|
|
|
/* segment registers */
|
|
|
|
|
"es", "cs", "ss",
|
|
|
|
|
"ds", "fs", "gs",
|
|
|
|
|
/* control registers */
|
|
|
|
|
"cr0", "cr1", "cr2", "cr3",
|
|
|
|
|
"cr4", "cr5", "cr6", "cr7",
|
|
|
|
|
"cr8", "cr9", "cr10", "cr11",
|
|
|
|
|
"cr12", "cr13", "cr14", "cr15",
|
|
|
|
|
/* debug registers */
|
|
|
|
|
"dr0", "dr1", "dr2", "dr3",
|
|
|
|
|
"dr4", "dr5", "dr6", "dr7",
|
|
|
|
|
"dr8", "dr9", "dr10", "dr11",
|
|
|
|
|
"dr12", "dr13", "dr14", "dr15",
|
|
|
|
|
/* mmx registers */
|
|
|
|
|
"mm0", "mm1", "mm2", "mm3",
|
|
|
|
|
"mm4", "mm5", "mm6", "mm7",
|
|
|
|
|
/* x87 registers */
|
|
|
|
|
"st0", "st1", "st2", "st3",
|
|
|
|
|
"st4", "st5", "st6", "st7",
|
|
|
|
|
/* extended multimedia registers */
|
|
|
|
|
"xmm0", "xmm1", "xmm2", "xmm3",
|
|
|
|
|
"xmm4", "xmm5", "xmm6", "xmm7",
|
|
|
|
|
"xmm8", "xmm9", "xmm10", "xmm11",
|
|
|
|
|
"xmm12", "xmm13", "xmm14", "xmm15",
|
|
|
|
|
/* 256 bit multimedia registers */
|
|
|
|
|
"ymm0", "ymm1", "ymm2", "ymm3",
|
|
|
|
|
"ymm4", "ymm5", "ymm6", "ymm7",
|
|
|
|
|
"ymm8", "ymm9", "ymm10", "ymm11",
|
|
|
|
|
"ymm12", "ymm13", "ymm14", "ymm15",
|
|
|
|
|
/* instruction pointer register */
|
|
|
|
|
"rip"
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void BaseInstructionFormatter::internalFormatInstruction(const InstructionInfo& /*info*/)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
// Nothing to do here
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
BaseInstructionFormatter::BaseInstructionFormatter()
|
2014-10-25 05:11:16 +08:00
|
|
|
|
: m_symbolResolver(nullptr)
|
2014-11-01 10:42:37 +08:00
|
|
|
|
, m_outputStringLen(0)
|
2014-11-03 22:15:48 +08:00
|
|
|
|
, m_outputUppercase(false)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
BaseInstructionFormatter::BaseInstructionFormatter(
|
|
|
|
|
BaseSymbolResolver *symbolResolver)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
: m_symbolResolver(symbolResolver)
|
2014-11-01 10:42:37 +08:00
|
|
|
|
, m_outputStringLen(0)
|
2014-11-03 22:15:48 +08:00
|
|
|
|
, m_outputUppercase(false)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 08:26:18 +08:00
|
|
|
|
const char* BaseInstructionFormatter::formatInstruction(const InstructionInfo& info)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
// Clears the internal string buffer
|
|
|
|
|
outputClear();
|
|
|
|
|
// Calls the virtual format method that actually formats the instruction
|
|
|
|
|
internalFormatInstruction(info);
|
|
|
|
|
if (m_outputBuffer.size() == 0)
|
|
|
|
|
{
|
|
|
|
|
// The basic instruction formatter only returns the instruction menmonic.
|
2015-05-16 11:05:17 +08:00
|
|
|
|
return Internal::GetInstructionMnemonicString(info.mnemonic);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
// Return the formatted instruction string
|
|
|
|
|
return outputString();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
BaseInstructionFormatter::~BaseInstructionFormatter()
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void BaseInstructionFormatter::outputClear()
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2014-11-01 10:42:37 +08:00
|
|
|
|
m_outputStringLen = 0;
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
char const *BaseInstructionFormatter::outputString()
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
return& m_outputBuffer[0];
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 08:33:04 +08:00
|
|
|
|
void BaseInstructionFormatter::outputAppend(char const* text)
|
2014-11-01 10:42:37 +08:00
|
|
|
|
{
|
2014-10-25 05:11:16 +08:00
|
|
|
|
// Get the string length including the null-terminator char
|
|
|
|
|
size_t strLen = strlen(text) + 1;
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// Get the buffer size
|
2014-10-25 05:11:16 +08:00
|
|
|
|
size_t bufLen = m_outputBuffer.size();
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// Decrease the offset by one, to exclude already existing null-terminator chars in the
|
2014-10-25 05:11:16 +08:00
|
|
|
|
// output buffer
|
2014-11-01 10:42:37 +08:00
|
|
|
|
size_t offset = (m_outputStringLen) ? m_outputStringLen - 1 : 0;
|
2014-10-25 05:11:16 +08:00
|
|
|
|
// Resize capacity of the output buffer on demand and add some extra space to improve the
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// performance
|
|
|
|
|
if (bufLen <= (m_outputStringLen + strLen))
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2014-11-01 10:42:37 +08:00
|
|
|
|
m_outputBuffer.resize(bufLen + strLen + 512);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// Write the text to the output buffer
|
2014-10-25 05:11:16 +08:00
|
|
|
|
memcpy(&m_outputBuffer[offset], text, strLen);
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// Increase the string length
|
|
|
|
|
m_outputStringLen = offset + strLen;
|
2014-10-30 23:36:25 +08:00
|
|
|
|
// Convert to uppercase
|
2014-11-03 22:15:48 +08:00
|
|
|
|
if (m_outputUppercase)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
2014-11-01 10:42:37 +08:00
|
|
|
|
for (size_t i = offset; i < m_outputStringLen - 1; ++i)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
2015-05-16 11:25:11 +08:00
|
|
|
|
m_outputBuffer[i] = static_cast<char>(toupper(m_outputBuffer[i]));
|
2014-10-30 23:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-01 10:42:37 +08:00
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
|
|
2015-05-18 08:33:04 +08:00
|
|
|
|
void BaseInstructionFormatter::outputAppendFormatted(char const* format, ...)
|
2014-11-01 10:42:37 +08:00
|
|
|
|
{
|
2014-10-25 05:11:16 +08:00
|
|
|
|
va_list arguments;
|
|
|
|
|
va_start(arguments, format);
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// Get the buffer size
|
2014-10-25 05:11:16 +08:00
|
|
|
|
size_t bufLen = m_outputBuffer.size();
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// Decrease the offset by one, to exclude already existing null-terminator chars in the
|
2014-10-25 05:11:16 +08:00
|
|
|
|
// output buffer
|
2014-11-01 10:42:37 +08:00
|
|
|
|
size_t offset = (m_outputStringLen) ? m_outputStringLen - 1 : 0;
|
|
|
|
|
// Resize the output buffer on demand and add some extra space to improve the performance
|
|
|
|
|
if ((bufLen - m_outputStringLen) < 256)
|
|
|
|
|
{
|
|
|
|
|
bufLen = bufLen + 512;
|
|
|
|
|
m_outputBuffer.resize(bufLen);
|
|
|
|
|
}
|
|
|
|
|
int strLen = 0;
|
|
|
|
|
do
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// If the formatted text did not fit in the output buffer, resize it, and try again
|
|
|
|
|
if (strLen < 0)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2014-11-01 10:42:37 +08:00
|
|
|
|
m_outputBuffer.resize(bufLen + 512);
|
|
|
|
|
return outputAppendFormatted(format, arguments);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
2014-11-01 10:42:37 +08:00
|
|
|
|
// Write the formatted text to the output buffer
|
|
|
|
|
assert((bufLen - offset) > 0);
|
2015-05-18 08:26:18 +08:00
|
|
|
|
strLen = std::vsnprintf(&m_outputBuffer[offset], bufLen - offset, format, arguments);
|
2014-11-01 10:42:37 +08:00
|
|
|
|
} while (strLen < 0);
|
|
|
|
|
// Increase the string length
|
|
|
|
|
m_outputStringLen = offset + strLen + 1;
|
|
|
|
|
// Convert to uppercase
|
2014-11-03 22:15:48 +08:00
|
|
|
|
if (m_outputUppercase)
|
2014-11-01 10:42:37 +08:00
|
|
|
|
{
|
|
|
|
|
for (size_t i = offset; i < m_outputStringLen - 1; ++i)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
2015-05-16 11:25:11 +08:00
|
|
|
|
m_outputBuffer[i] = static_cast<char>(toupper(m_outputBuffer[i]));
|
2014-10-30 23:36:25 +08:00
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
va_end(arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void BaseInstructionFormatter::outputAppendAddress(const InstructionInfo& info,
|
2014-10-30 23:36:25 +08:00
|
|
|
|
uint64_t address, bool resolveSymbols)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2014-10-30 23:36:25 +08:00
|
|
|
|
uint64_t offset = 0;
|
2015-05-16 11:05:17 +08:00
|
|
|
|
const char *name = nullptr;
|
2014-10-30 23:36:25 +08:00
|
|
|
|
if (resolveSymbols)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2014-10-30 23:36:25 +08:00
|
|
|
|
name = resolveSymbol(info, address, offset);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
if (name)
|
|
|
|
|
{
|
|
|
|
|
if (offset)
|
|
|
|
|
{
|
|
|
|
|
outputAppendFormatted("%s+%.2llX", name, offset);
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
outputAppend(name);
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
{
|
2015-05-22 03:42:06 +08:00
|
|
|
|
if (info.flags & IF_DISASSEMBLER_MODE_16)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppendFormatted("%.4X", address);
|
2015-05-22 03:42:06 +08:00
|
|
|
|
} else if (info.flags & IF_DISASSEMBLER_MODE_32)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppendFormatted("%.8lX", address);
|
2015-05-22 03:42:06 +08:00
|
|
|
|
} else if (info.flags & IF_DISASSEMBLER_MODE_64)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppendFormatted("%.16llX", address);
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void BaseInstructionFormatter::outputAppendImmediate(const InstructionInfo& info,
|
|
|
|
|
const OperandInfo& operand, bool resolveSymbols)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
assert(operand.type == OperandType::IMMEDIATE);
|
2014-10-30 23:36:25 +08:00
|
|
|
|
uint64_t value = 0;
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (operand.signed_lval&& (operand.size != info.operand_mode))
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
|
|
|
|
if (operand.size == 8)
|
|
|
|
|
{
|
|
|
|
|
value = static_cast<int64_t>(operand.lval.sbyte);
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
assert(operand.size == 32);
|
|
|
|
|
value = static_cast<int64_t>(operand.lval.sdword);
|
|
|
|
|
}
|
|
|
|
|
if (info.operand_mode < 64)
|
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
value = value& ((1ull << info.operand_mode) - 1ull);
|
2014-10-30 23:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
switch (operand.size)
|
|
|
|
|
{
|
|
|
|
|
case 8:
|
|
|
|
|
value = operand.lval.ubyte;
|
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
value = operand.lval.uword;
|
|
|
|
|
break;
|
|
|
|
|
case 32:
|
|
|
|
|
value = operand.lval.udword;
|
|
|
|
|
break;
|
|
|
|
|
case 64:
|
|
|
|
|
value = operand.lval.uqword;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uint64_t offset = 0;
|
2015-05-22 03:42:06 +08:00
|
|
|
|
const char* name = nullptr;
|
2014-10-30 23:36:25 +08:00
|
|
|
|
if (resolveSymbols)
|
|
|
|
|
{
|
|
|
|
|
name = resolveSymbol(info, value, offset);
|
|
|
|
|
}
|
|
|
|
|
if (name)
|
|
|
|
|
{
|
|
|
|
|
if (offset)
|
|
|
|
|
{
|
|
|
|
|
outputAppendFormatted("%s+%.2llX", name, offset);
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
outputAppend(name);
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
outputAppendFormatted("%.2llX", value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void BaseInstructionFormatter::outputAppendDisplacement(const OperandInfo& operand)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
|
|
|
|
assert(operand.offset > 0);
|
2015-05-22 03:42:06 +08:00
|
|
|
|
if ((operand.base == Register::NONE) && (operand.index == Register::NONE))
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
|
|
|
|
// Assume the displacement value is unsigned
|
|
|
|
|
assert(operand.scale == 0);
|
|
|
|
|
assert(operand.offset != 8);
|
|
|
|
|
uint64_t value = 0;
|
|
|
|
|
switch (operand.offset)
|
|
|
|
|
{
|
|
|
|
|
case 16:
|
|
|
|
|
value = operand.lval.uword;
|
|
|
|
|
break;
|
|
|
|
|
case 32:
|
|
|
|
|
value = operand.lval.udword;
|
|
|
|
|
break;
|
|
|
|
|
case 64:
|
|
|
|
|
value = operand.lval.uqword;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
outputAppendFormatted("%.2llX", value);
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
// The displacement value might be negative
|
|
|
|
|
assert(operand.offset != 64);
|
|
|
|
|
int64_t value = 0;
|
|
|
|
|
switch (operand.offset)
|
|
|
|
|
{
|
|
|
|
|
case 8:
|
|
|
|
|
value = operand.lval.sbyte;
|
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
value = operand.lval.sword;
|
|
|
|
|
break;
|
|
|
|
|
case 32:
|
|
|
|
|
value = operand.lval.sdword;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
if (value < 0)
|
|
|
|
|
{
|
|
|
|
|
outputAppendFormatted("-%.2lX", -value);
|
|
|
|
|
} else
|
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppendFormatted("%s%.2lX", (operand.base != Register::NONE ||
|
|
|
|
|
operand.index != Register::NONE) ? "+" : "", value);
|
2014-10-30 23:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
/* IntelInstructionFormatter =============================================================== */
|
2014-10-30 23:36:25 +08:00
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void IntelInstructionFormatter::outputAppendOperandCast(const OperandInfo& operand)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
|
|
|
|
switch(operand.size)
|
|
|
|
|
{
|
|
|
|
|
case 8:
|
|
|
|
|
outputAppend("byte ptr " );
|
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
outputAppend("word ptr " );
|
|
|
|
|
break;
|
|
|
|
|
case 32:
|
|
|
|
|
outputAppend("dword ptr ");
|
|
|
|
|
break;
|
|
|
|
|
case 64:
|
|
|
|
|
outputAppend("qword ptr ");
|
|
|
|
|
break;
|
|
|
|
|
case 80:
|
|
|
|
|
outputAppend("tword ptr ");
|
|
|
|
|
break;
|
|
|
|
|
case 128:
|
|
|
|
|
outputAppend("oword ptr ");
|
|
|
|
|
break;
|
|
|
|
|
case 256:
|
|
|
|
|
outputAppend("yword ptr ");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void IntelInstructionFormatter::formatOperand(const InstructionInfo& info,
|
|
|
|
|
const OperandInfo& operand)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
switch (operand.type)
|
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
case OperandType::REGISTER:
|
2014-10-25 05:11:16 +08:00
|
|
|
|
outputAppend(registerToString(operand.base));
|
|
|
|
|
break;
|
2015-05-16 11:05:17 +08:00
|
|
|
|
case OperandType::MEMORY:
|
2015-05-22 03:42:06 +08:00
|
|
|
|
if (info.flags & IF_PREFIX_SEGMENT)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2014-10-27 21:10:22 +08:00
|
|
|
|
outputAppendFormatted("%s:", registerToString(info.segment));
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
outputAppend("[");
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (operand.base == Register::RIP)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
|
|
|
|
// TODO: Add option
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppendAddress(info, CalcAbsoluteTarget(info, operand), true);
|
2014-10-30 23:36:25 +08:00
|
|
|
|
} else
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (operand.base != Register::NONE)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppend(registerToString(operand.base));
|
|
|
|
|
}
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (operand.index != Register::NONE)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppendFormatted("%s%s", operand.base != Register::NONE ? "+" : "",
|
2014-10-25 05:11:16 +08:00
|
|
|
|
registerToString(operand.index));
|
|
|
|
|
if (operand.scale)
|
|
|
|
|
{
|
|
|
|
|
outputAppendFormatted("*%d", operand.scale);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-30 23:36:25 +08:00
|
|
|
|
if (operand.offset)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppendDisplacement(operand);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
outputAppend("]");
|
|
|
|
|
break;
|
2015-05-16 11:05:17 +08:00
|
|
|
|
case OperandType::POINTER:
|
2014-10-25 05:11:16 +08:00
|
|
|
|
// TODO: resolve symbols
|
|
|
|
|
switch (operand.size)
|
|
|
|
|
{
|
|
|
|
|
case 32:
|
|
|
|
|
outputAppendFormatted("word %.4X:%.4X", operand.lval.ptr.seg,
|
2015-05-16 11:05:17 +08:00
|
|
|
|
operand.lval.ptr.off& 0xFFFF);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
break;
|
|
|
|
|
case 48:
|
|
|
|
|
outputAppendFormatted("dword %.4X:%.8lX", operand.lval.ptr.seg, operand.lval.ptr.off);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2015-05-16 11:05:17 +08:00
|
|
|
|
case OperandType::IMMEDIATE:
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
2014-10-30 23:36:25 +08:00
|
|
|
|
outputAppendImmediate(info, operand, true);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
2015-05-16 11:05:17 +08:00
|
|
|
|
case OperandType::REL_IMMEDIATE:
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
if (operand.size == 8)
|
|
|
|
|
{
|
|
|
|
|
outputAppend("short ");
|
|
|
|
|
}
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppendAddress(info, CalcAbsoluteTarget(info, operand), true);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
2015-05-16 11:05:17 +08:00
|
|
|
|
case OperandType::CONSTANT:
|
2014-10-30 23:36:25 +08:00
|
|
|
|
outputAppendFormatted("%.2X", operand.lval.udword);
|
2014-10-25 05:11:16 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void IntelInstructionFormatter::internalFormatInstruction(const InstructionInfo& info)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
// Append string prefixes
|
2015-05-22 03:42:06 +08:00
|
|
|
|
if (info.flags & IF_PREFIX_LOCK)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppend("lock ");
|
|
|
|
|
}
|
2015-05-22 03:42:06 +08:00
|
|
|
|
if (info.flags & IF_PREFIX_REP)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppend("rep ");
|
2015-05-22 03:42:06 +08:00
|
|
|
|
} else if (info.flags & IF_PREFIX_REPNE)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppend("repne ");
|
|
|
|
|
}
|
|
|
|
|
// Append the instruction mnemonic
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppend(Internal::GetInstructionMnemonicString(info.mnemonic));
|
2014-10-25 05:11:16 +08:00
|
|
|
|
// Append the first operand
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (info.operand[0].type != OperandType::NONE)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppend(" ");
|
2014-10-30 23:36:25 +08:00
|
|
|
|
bool cast = false;
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (info.operand[0].type == OperandType::MEMORY)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (info.operand[1].type == OperandType::IMMEDIATE ||
|
|
|
|
|
info.operand[1].type == OperandType::CONSTANT ||
|
|
|
|
|
info.operand[1].type == OperandType::NONE ||
|
2014-10-30 23:36:25 +08:00
|
|
|
|
(info.operand[0].size != info.operand[1].size))
|
|
|
|
|
{
|
|
|
|
|
cast = true;
|
2015-05-22 03:42:06 +08:00
|
|
|
|
} else if (info.operand[1].type == OperandType::REGISTER &&
|
2015-05-16 11:05:17 +08:00
|
|
|
|
info.operand[1].base == Register::CL)
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
|
|
|
|
switch (info.mnemonic)
|
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
case InstructionMnemonic::RCL:
|
|
|
|
|
case InstructionMnemonic::ROL:
|
|
|
|
|
case InstructionMnemonic::ROR:
|
|
|
|
|
case InstructionMnemonic::RCR:
|
|
|
|
|
case InstructionMnemonic::SHL:
|
|
|
|
|
case InstructionMnemonic::SHR:
|
|
|
|
|
case InstructionMnemonic::SAR:
|
2014-10-30 23:36:25 +08:00
|
|
|
|
cast = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (cast)
|
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppendOperandCast(info.operand[0]);
|
2014-10-30 23:36:25 +08:00
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
|
formatOperand(info, info.operand[0]);
|
|
|
|
|
}
|
|
|
|
|
// Append the second operand
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (info.operand[1].type != OperandType::NONE)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppend(", ");
|
2014-10-30 23:36:25 +08:00
|
|
|
|
bool cast = false;
|
2015-05-22 03:42:06 +08:00
|
|
|
|
if (info.operand[1].type == OperandType::MEMORY &&
|
|
|
|
|
info.operand[0].size != info.operand[1].size &&
|
2015-05-16 11:05:17 +08:00
|
|
|
|
((info.operand[0].type != OperandType::REGISTER) ||
|
2015-05-22 03:42:06 +08:00
|
|
|
|
((info.operand[0].base != Register::ES) &&
|
|
|
|
|
(info.operand[0].base != Register::CS) &&
|
|
|
|
|
(info.operand[0].base != Register::SS) &&
|
|
|
|
|
(info.operand[0].base != Register::DS) &&
|
|
|
|
|
(info.operand[0].base != Register::FS) &&
|
2015-05-16 11:05:17 +08:00
|
|
|
|
(info.operand[0].base != Register::GS))))
|
2014-10-30 23:36:25 +08:00
|
|
|
|
{
|
|
|
|
|
cast = true;
|
|
|
|
|
}
|
|
|
|
|
if (cast)
|
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppendOperandCast(info.operand[1]);
|
2014-10-30 23:36:25 +08:00
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
|
formatOperand(info, info.operand[1]);
|
|
|
|
|
}
|
|
|
|
|
// Append the third operand
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (info.operand[2].type != OperandType::NONE)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppend(", ");
|
2014-10-30 23:36:25 +08:00
|
|
|
|
bool cast = false;
|
2015-05-22 03:42:06 +08:00
|
|
|
|
if (info.operand[2].type == OperandType::MEMORY &&
|
2014-10-30 23:36:25 +08:00
|
|
|
|
(info.operand[2].size != info.operand[1].size))
|
|
|
|
|
{
|
|
|
|
|
cast = true;
|
|
|
|
|
}
|
|
|
|
|
if (cast)
|
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
outputAppendOperandCast(info.operand[2]);
|
2014-10-30 23:36:25 +08:00
|
|
|
|
}
|
2014-10-25 05:11:16 +08:00
|
|
|
|
formatOperand(info, info.operand[2]);
|
|
|
|
|
}
|
|
|
|
|
// Append the fourth operand
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (info.operand[3].type != OperandType::NONE)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
outputAppend(", ");
|
|
|
|
|
formatOperand(info, info.operand[3]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
IntelInstructionFormatter::IntelInstructionFormatter()
|
|
|
|
|
: BaseInstructionFormatter()
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
IntelInstructionFormatter::IntelInstructionFormatter(
|
|
|
|
|
BaseSymbolResolver *symbolResolver)
|
|
|
|
|
: BaseInstructionFormatter(symbolResolver)
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
IntelInstructionFormatter::~IntelInstructionFormatter()
|
2014-10-25 05:11:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
/* ============================================================================================== */
|
2014-11-03 22:15:48 +08:00
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
}
|