zydis/include/Zydis/Formatter.h

299 lines
10 KiB
C
Raw Normal View History

2016-05-26 03:25:48 +08:00
/***************************************************************************************************
Zyan Disassembler Engine (Zydis)
Original Author : Florian Bernd
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
***************************************************************************************************/
#ifndef ZYDIS_FORMATTER_H
#define ZYDIS_FORMATTER_H
#include <stdint.h>
#include <Zydis/Defines.h>
#include <Zydis/Status.h>
#include <Zydis/InstructionInfo.h>
#include <Zydis/SymbolResolver.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================================== */
/* Enums and types */
2016-11-21 21:55:17 +08:00
/* ============================================================================================== */
2016-05-26 03:25:48 +08:00
/**
2016-11-21 21:55:17 +08:00
* @brief Defines the @c ZydisFormatterStyle datatype.
2016-05-26 03:25:48 +08:00
*/
typedef uint8_t ZydisFormatterStyle;
/**
* @brief Values that represent formatter-styles.
*/
enum ZydisFormatterStyles
{
/**
* @brief Generates intel-style disassembly.
*/
ZYDIS_FORMATTER_STYLE_INTEL
};
/**
2016-11-21 21:55:17 +08:00
* @brief Defines the @c ZydisFormatterFlags datatype.
2016-05-26 03:25:48 +08:00
*/
typedef uint8_t ZydisFormatterFlags;
#define ZYDIS_FORMATTER_FLAG_UPPERCASE 0x01
#define ZYDIS_FORMATTER_FLAG_TAB_AFTER_MNEMONIC 0x02
#define ZYDIS_FORMATTER_FLAG_NO_SPACE_BETWEEN_OPERANDS 0x04
#define ZYDIS_FORMATTER_FLAG_ALWAYS_DISPLAY_MEMORY_SIZE 0x08
#define ZYDIS_FORMATTER_FLAG_ALWAYS_DISPLAY_MEMORY_SEGMENT 0x10
2016-05-26 03:25:48 +08:00
/**
2016-11-21 21:55:17 +08:00
* @brief Defines the @c ZydisFormatterAddressFormat datatype.
2016-05-26 03:25:48 +08:00
*/
typedef uint8_t ZydisFormatterAddressFormat;
/**
* @brief Values that represent address-formats.
*/
enum ZydisFormatterAddressFormat
{
2016-11-21 21:55:17 +08:00
ZYDIS_FORMATTER_ADDR_DEFAULT,
2016-05-26 03:25:48 +08:00
/**
* @brief Displays absolute addresses instead of relative ones.
*/
2016-11-21 21:55:17 +08:00
ZYDIS_FORMATTER_ADDR_ABSOLUTE,
2016-05-26 03:25:48 +08:00
/**
* @brief Uses signed hexadecimal values to display relative addresses.
*
* Examples:
* "JMP 0x20"
* "JMP -0x20"
*/
2016-11-21 21:55:17 +08:00
ZYDIS_FORMATTER_ADDR_RELATIVE_SIGNED,
2016-05-26 03:25:48 +08:00
/**
* @brief Uses unsigned hexadecimal values to display relative addresses.
*
* Examples:
* "JMP 0x20"
* "JMP 0xE0"
*/
2016-11-21 21:55:17 +08:00
ZYDIS_FORMATTER_ADDR_RELATIVE_UNSIGNED,
2016-05-26 03:25:48 +08:00
};
/**
2016-11-21 21:55:17 +08:00
* @brief Defines the @c ZydisFormatterDisplacementFormat datatype.
2016-05-26 03:25:48 +08:00
*/
typedef uint8_t ZydisFormatterDisplacementFormat;
/**
* @brief Values that represent displacement-formats.
*/
enum ZydisFormatterDisplacementFormats
{
2016-11-21 21:55:17 +08:00
ZYDIS_FORMATTER_DISP_DEFAULT,
2016-05-26 03:25:48 +08:00
/**
* @brief Formats displacements as signed hexadecimal values.
*
* Examples:
* "MOV EAX, DWORD PTR SS:[ESP+0x400]"
* "MOV EAX, DWORD PTR SS:[ESP-0x400]"
*/
ZYDIS_FORMATTER_DISP_HEX_SIGNED,
/**
* @brief Formats displacements as unsigned hexadecimal values.
*
* Examples:
* "MOV EAX, DWORD PTR SS:[ESP+0x400]"
* "MOV EAX, DWORD PTR SS:[ESP+0xFFFFFC00]"
*/
ZYDIS_FORMATTER_DISP_HEX_UNSIGNED
};
/**
2016-11-21 21:55:17 +08:00
* @brief Defines the @c ZydisFormatterImmediateFormat datatype.
2016-05-26 03:25:48 +08:00
*/
typedef uint8_t ZydisFormatterImmediateFormat;
/**
* @brief Values that represent formatter immediate-formats.
*/
enum ZydisFormatterImmediateFormats
{
2016-11-21 21:55:17 +08:00
ZYDIS_FORMATTER_IMM_DEFAULT,
2016-05-26 03:25:48 +08:00
/**
* @brief Formats immediates as signed hexadecimal values.
*
* Examples:
* "MOV EAX, 0x400"
* "MOV EAX, -0x400"
*/
ZYDIS_FORMATTER_IMM_HEX_SIGNED,
/**
* @brief Formats immediates as unsigned hexadecimal values.
*
* Examples:
* "MOV EAX, 0x400"
* "MOV EAX, 0xFFFFFC00"
*/
ZYDIS_FORMATTER_IMM_HEX_UNSIGNED
};
/**
2016-11-21 21:55:17 +08:00
* @brief Defines the @c ZydisFormatterHookType datatype.
*/
typedef uint8_t ZydisFormatterHookType;
/**
* @brief Values that represent formatter hook-types.
*/
enum ZydisFormatterHookTypes
{
/**
* @brief This hook is called right
*/
ZYDIS_FORMATTER_HOOK_PRE,
ZYDIS_FORMATTER_HOOK_POST,
ZYDIS_FORMATTER_HOOK_FORMAT_MNEMONIC,
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND,
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_REG,
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_MEM,
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_IMM,
ZYDIS_FORMATTER_HOOK_FORMAT_OPERAND_PTR,
ZYDIS_FORMATTER_HOOK_FORMAT_ADDRESS_ABS,
ZYDIS_FORMATTER_HOOK_FORMAT_ADDRESS_REL,
ZYDIS_FORMATTER_HOOK_FORMAT_DISPLACEMENT,
ZYDIS_FORMATTER_HOOK_FORMAT_IMMEDIATE
};
typedef const char* (*ZydisFormatterHookFormatMnemonicFunc)(void* context,
const ZydisInstructionInfo* info, const ZydisOperandInfo* operand, uint64_t address,
int64_t* offset);
/**
* @brief Defines the @c ZydisInstructionFormatter struct.
2016-05-26 03:25:48 +08:00
*/
typedef struct ZydisInstructionFormatter_
{
ZydisFormatterStyle style;
ZydisFormatterFlags flags;
ZydisFormatterAddressFormat addressFormat;
ZydisFormatterDisplacementFormat displacementFormat;
ZydisFormatterImmediateFormat immediateFormat;
ZydisCustomSymbolResolver* symbolResolver;
} ZydisInstructionFormatter;
2016-11-21 21:55:17 +08:00
/* ============================================================================================== */
2016-05-26 03:25:48 +08:00
/* Exported functions */
2016-11-21 21:55:17 +08:00
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Basic functions */
2016-05-26 03:25:48 +08:00
/* ---------------------------------------------------------------------------------------------- */
/**
* @brief Initializes the given @c ZydisInstructionFormatter instance.
*
* @param formatter A pointer to the instruction-formatter instance.
* @param style The formatter style to use.
*
* @return A zydis status code.
*/
2016-06-20 07:33:29 +08:00
ZYDIS_EXPORT ZydisStatus ZydisFormatterInitInstructionFormatter(
2016-05-26 03:25:48 +08:00
ZydisInstructionFormatter* formatter, ZydisFormatterStyle style);
/**
* @brief Initializes the given @c ZydisInstructionFormatter instance.
*
* @param formatter A pointer to the instruction-formatter instance.
* @param style The formatter style to use.
* @param flags Additional formatter-flags.
*
* @return A zydis status code.
*/
2016-06-20 07:33:29 +08:00
ZYDIS_EXPORT ZydisStatus ZydisFormatterInitInstructionFormatterEx(
2016-05-26 03:25:48 +08:00
ZydisInstructionFormatter* formatter, ZydisFormatterStyle style, ZydisFormatterFlags flags);
/**
* @brief Returns the symbol-resolver assigned to the given instruction-formatter instance.
*
* @param formatter A pointer to the instruction-formatter instance.
* @param symbolResolver A pointer to the memory that receives the current symbol-resolver
* pointer.
*
* @return A zydis status code.
*/
2016-06-20 07:33:29 +08:00
ZYDIS_EXPORT ZydisStatus ZydisFormatterGetSymbolResolver(
2016-05-26 03:25:48 +08:00
const ZydisInstructionFormatter* formatter, ZydisCustomSymbolResolver** symbolResolver);
/**
* @brief Changes the symbol-resolver of the given instruction-formatter instance.
*
* @param formatter A pointer to the instruction-formatter instance.
* @param symbolResolver A pointer to the new symbol-resolver instance.
*
* @return The ZydisStatus.
*/
2016-06-20 07:33:29 +08:00
ZYDIS_EXPORT ZydisStatus ZydisFormatterSetSymbolResolver(
2016-05-26 03:25:48 +08:00
ZydisInstructionFormatter* formatter, ZydisCustomSymbolResolver* symbolResolver);
2016-11-21 21:55:17 +08:00
ZYDIS_EXPORT ZydisStatus ZydisFormatterSetHook(ZydisInstructionFormatter* formatter,
ZydisFormatterHookType hook, const void* callback);
2016-05-26 03:25:48 +08:00
/**
* @brief Formats the given instruction and writes it into the output buffer.
*
* @param formatter A pointer to the instruction-formatter instance.
* @param info A pointer to the instruction-info struct.
* @param buffer A pointer to the output buffer.
* @param bufferLen The length of the output buffer.
*
* @return A zydis status code.
*/
2016-06-20 07:33:29 +08:00
ZYDIS_EXPORT ZydisStatus ZydisFormatterFormatInstruction(
2016-05-26 03:25:48 +08:00
ZydisInstructionFormatter* formatter, const ZydisInstructionInfo* info, char* buffer,
size_t bufferLen);
2016-11-21 21:55:17 +08:00
/* ---------------------------------------------------------------------------------------------- */
/* Formatting functions for custom implementations */
/* ---------------------------------------------------------------------------------------------- */
ZYDIS_EXPORT ZydisStatus ZydisFormatterFormatOperandIntel(
const ZydisInstructionFormatter* formatter, char* buffer, size_t bufferLen, size_t* offset,
const ZydisInstructionInfo* info, const ZydisOperandInfo* operand, uint16_t typecast);
ZYDIS_EXPORT ZydisStatus ZydisFormatterFormatOperandMemIntel(
const ZydisInstructionFormatter* formatter, char* buffer, size_t bufferLen, size_t* offset,
const ZydisInstructionInfo* info, const ZydisOperandInfo* operand, uint16_t typecast);
2016-05-26 03:25:48 +08:00
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
#ifdef __cplusplus
}
#endif
#endif /* ZYDIS_FORMATTER_H */