2015-05-16 11:05:17 +08:00
|
|
|
|
/***************************************************************************************************
|
2015-03-16 23:37:15 +08:00
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
Zyan Disassembler Engine
|
2015-03-16 23:37:15 +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
|
2015-03-16 23:37:15 +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
|
|
|
|
*
|
2015-03-16 23:37:15 +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
|
|
|
|
*
|
2015-03-16 23:37:15 +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
|
|
|
|
***************************************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef _ZYDIS_INSTRUCTIONFORMATTER_HPP_
|
|
|
|
|
#define _ZYDIS_INSTRUCTIONFORMATTER_HPP_
|
2015-03-16 23:37:15 +08:00
|
|
|
|
|
|
|
|
|
#include <vector>
|
2015-05-16 11:05:17 +08:00
|
|
|
|
#include "ZydisTypes.hpp"
|
|
|
|
|
#include "ZydisSymbolResolver.hpp"
|
2015-03-16 23:37:15 +08:00
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
namespace Zydis
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
/* BaseInstructionFormatter ===================================================================== */
|
2015-03-16 23:37:15 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Base class for all instruction formatter implementations.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
class BaseInstructionFormatter
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
private:
|
2015-05-16 11:05:17 +08:00
|
|
|
|
static const char* m_registerStrings[];
|
|
|
|
|
BaseSymbolResolver* m_symbolResolver;
|
|
|
|
|
std::vector<char> m_outputBuffer;
|
|
|
|
|
size_t m_outputStringLen;
|
|
|
|
|
bool m_outputUppercase;
|
2015-03-16 23:37:15 +08:00
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Clears the output string buffer.
|
|
|
|
|
*/
|
|
|
|
|
void outputClear();
|
|
|
|
|
/**
|
|
|
|
|
* @brief Returns the content of the output string buffer.
|
|
|
|
|
* @return Pointer to the content of the ouput string buffer.
|
|
|
|
|
*/
|
|
|
|
|
const char* outputString();
|
|
|
|
|
/**
|
|
|
|
|
* @brief Appends text to the ouput string buffer.
|
|
|
|
|
* @param text The text.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void outputAppend(const char* text);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Appends formatted text to the output string buffer.
|
|
|
|
|
* @param format The format string.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void outputAppendFormatted(const char* format, ...);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Changes automatic conversion of characters to uppercase.
|
|
|
|
|
* @param uppercase Set true to enable automatic uppercase conversion.
|
|
|
|
|
*/
|
|
|
|
|
void outputSetUppercase(bool uppercase);
|
|
|
|
|
/**
|
|
|
|
|
* @brief Appends a formatted address to the output string buffer.
|
|
|
|
|
* @param info The instruction info.
|
|
|
|
|
* @param address The address.
|
|
|
|
|
* @param resolveSymbols If this parameter is true, the method will try to display a
|
|
|
|
|
* smybol name instead of the numeric value.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void outputAppendAddress(const InstructionInfo& info, uint64_t address,
|
2015-03-16 23:37:15 +08:00
|
|
|
|
bool resolveSymbols = true);
|
|
|
|
|
/**
|
|
|
|
|
* @brief Appends a formatted immediate value to the output string buffer.
|
|
|
|
|
* @param info The instruction info.
|
|
|
|
|
* @param operand The immediate operand.
|
|
|
|
|
* @param resolveSymbols If this parameter is true, the method will try to display a
|
|
|
|
|
* smybol name instead of the numeric value.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void outputAppendImmediate(const InstructionInfo& info, const OperandInfo& operand,
|
2015-03-16 23:37:15 +08:00
|
|
|
|
bool resolveSymbols = false);
|
|
|
|
|
/**
|
|
|
|
|
* @brief Appends a formatted memory displacement value to the output string buffer.
|
|
|
|
|
* @param operand The memory operand.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void outputAppendDisplacement(const OperandInfo& operand);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Returns the string representation of a given register.
|
|
|
|
|
* @param reg The register.
|
|
|
|
|
* @return The string representation of the given register.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
const char *registerToString(Register reg) const;
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Resolves a symbol.
|
|
|
|
|
* @param info The instruction info.
|
|
|
|
|
* @param address The address.
|
|
|
|
|
* @param offset Reference to an unsigned 64 bit integer that receives an offset
|
|
|
|
|
* relative to the base address of the symbol.
|
|
|
|
|
* @return The name of the symbol, if the symbol was found, @c NULL if not.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
const char* resolveSymbol(const InstructionInfo& info, uint64_t address,
|
|
|
|
|
uint64_t& offset) const;
|
2015-03-16 23:37:15 +08:00
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Override this method to implement a custom disassembly syntax. Use the
|
|
|
|
|
* @c outputAppend and @c outputAppendFormatted methods to fill the internal
|
|
|
|
|
* string buffer.
|
|
|
|
|
* @param info The instruction info.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
virtual void internalFormatInstruction(const InstructionInfo& info);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Default constructor.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
BaseInstructionFormatter();
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Constructor.
|
|
|
|
|
* @param symbolResolver Pointer to a symbol resolver instance or @c NULL, if no smybol
|
|
|
|
|
* resolver should be used.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
explicit BaseInstructionFormatter(BaseSymbolResolver* symbolResolver);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Destructor.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
virtual ~BaseInstructionFormatter();
|
2015-03-16 23:37:15 +08:00
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Formats a decoded instruction.
|
|
|
|
|
* @param info The instruction info.
|
|
|
|
|
* @return Pointer to the formatted instruction string.
|
|
|
|
|
*/
|
2015-05-18 08:33:04 +08:00
|
|
|
|
const char* formatInstruction(const InstructionInfo& info);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Returns a pointer to the current symbol resolver.
|
|
|
|
|
* @return Pointer to the current symbol resolver or @c NULL, if no symbol resolver is used.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
BaseSymbolResolver* getSymbolResolver() const;
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Sets a new symbol resolver.
|
|
|
|
|
* @param symbolResolver Pointer to a symbol resolver instance or @c NULL, if no smybol
|
|
|
|
|
* resolver should be used.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void setSymbolResolver(BaseSymbolResolver* symbolResolver);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
inline void BaseInstructionFormatter::outputSetUppercase(bool uppercase)
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
m_outputUppercase = uppercase;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 08:33:04 +08:00
|
|
|
|
inline char const* BaseInstructionFormatter::registerToString(Register reg) const
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
2015-05-16 11:05:17 +08:00
|
|
|
|
if (reg == Register::NONE)
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
return "error";
|
|
|
|
|
}
|
|
|
|
|
return m_registerStrings[static_cast<uint16_t>(reg) - 1];
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
inline char const* BaseInstructionFormatter::resolveSymbol(const InstructionInfo& info,
|
|
|
|
|
uint64_t address, uint64_t& offset) const
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
if (m_symbolResolver)
|
|
|
|
|
{
|
|
|
|
|
return m_symbolResolver->resolveSymbol(info, address, offset);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
inline BaseSymbolResolver* BaseInstructionFormatter::getSymbolResolver() const
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
return m_symbolResolver;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
inline void BaseInstructionFormatter::setSymbolResolver(
|
|
|
|
|
BaseSymbolResolver* symbolResolver)
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
m_symbolResolver = symbolResolver;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
/* IntelInstructionFormatter ==================================================================== */
|
2015-03-16 23:37:15 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Intel syntax instruction formatter.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
class IntelInstructionFormatter : public BaseInstructionFormatter
|
2015-03-16 23:37:15 +08:00
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Appends an operand cast to the output string buffer.
|
|
|
|
|
* @param operand The operand.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void outputAppendOperandCast(const OperandInfo& operand);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Formats the specified operand and appends the resulting string to the output
|
|
|
|
|
* buffer.
|
|
|
|
|
* @param info The instruction info.
|
|
|
|
|
* @param operand The operand.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void formatOperand(const InstructionInfo& info, const OperandInfo& operand);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Fills the internal string buffer with an intel style formatted instruction string.
|
|
|
|
|
* @param info The instruction info.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
void internalFormatInstruction(const InstructionInfo& info) override;
|
2015-03-16 23:37:15 +08:00
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Default constructor.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
IntelInstructionFormatter();
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Constructor.
|
|
|
|
|
* @param symbolResolver Pointer to a symbol resolver instance or @c NULL, if no smybol
|
|
|
|
|
* resolver should be used.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
explicit IntelInstructionFormatter(BaseSymbolResolver* symbolResolver);
|
2015-03-16 23:37:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Destructor.
|
|
|
|
|
*/
|
2015-05-16 11:05:17 +08:00
|
|
|
|
~IntelInstructionFormatter() override;
|
2015-03-16 23:37:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
|
/* ============================================================================================== */
|
2015-03-16 23:37:15 +08:00
|
|
|
|
|
|
|
|
|
}
|
2015-05-16 11:05:17 +08:00
|
|
|
|
|
|
|
|
|
#endif /* _ZYDIS_INSTRUCTIONFORMATTER_HPP_ */
|