From a6e76d81b4f32aaa9d677da0fdac846200b73887 Mon Sep 17 00:00:00 2001 From: flobernd Date: Tue, 23 Aug 2016 15:57:38 +0200 Subject: [PATCH] Preparations for optional feature support --- CMakeLists.txt | 19 ++++++++++++++++--- include/Zydis/Utils.h | 10 ++-------- include/Zydis/Zydis.h | 28 ++++++++++++++++++++++++++++ src/Zydis.c | 26 ++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ea45cb..e2eb2d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,12 +5,23 @@ include(GenerateExportHeader) project(Zydis) set_property(GLOBAL PROPERTY USE_FOLDERS ON) -option(BUILD_SHARED_LIBS "Build shared libraries rather than static ones" FALSE) +option(BUILD_SHARED_LIBS + "Build shared libraries rather than static ones" + FALSE) option(FORCE_SHARED_CRT "Forces shared linkage against the CRT even when building a static library" FALSE) +option(FEATURE_IMPLICITLY_USED_REGISTERS + "Include information about implicitly used registers" + TRUE) +option(FEATURE_AFFECTED_FLAGS + "Include information about affected flags" + TRUE) +option(FEATURE_CPUID + "Include information about CPUID feature-flags" + FALSE) option(BUILD_EXAMPLES "Build examples" TRUE) -option(BUILD_TOOLS "Build tools") +option(BUILD_TOOLS "Build tools" TRUE) if (NOT CONFIGURED_ONCE) if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR @@ -32,7 +43,7 @@ if (("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") AND NOT FORCE_SHARED_CRT) if(${flag_var} MATCHES "/MD") string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") endif () - endforeach(flag_var) + endforeach () endif () # Library @@ -41,6 +52,7 @@ set(headers "include/Zydis/Defines.h" "include/Zydis/Formatter.h" "include/Zydis/Input.h" + "include/Zydis/InstructionDetails.h" "include/Zydis/InstructionInfo.h" "include/Zydis/Mnemonic.h" "include/Zydis/Register.h" @@ -53,6 +65,7 @@ set(sources "src/Decoder.c" "src/Formatter.c" "src/Input.c" + "src/InstructionDetails.c" "src/InstructionTable.c" "src/Mnemonic.c" "src/Register.c" diff --git a/include/Zydis/Utils.h b/include/Zydis/Utils.h index 29a14b2..bdd0638 100644 --- a/include/Zydis/Utils.h +++ b/include/Zydis/Utils.h @@ -37,27 +37,21 @@ extern "C" { #endif /* ============================================================================================== */ -/* Operand utils */ +/* Utils */ /* ============================================================================================== */ -/* ---------------------------------------------------------------------------------------------- */ -/* Exported functions */ -/* ---------------------------------------------------------------------------------------------- */ - /** * @brief Calculates the absolute target-address of an relative instruction operand. * * @param info A pointer to the instruction-info struct. * @param operand A pointer to the operand-info struct. - * @param address A pointer to the memory address that receives the absolute target-address. + * @param address A pointer to the memory that receives the absolute target-address. * * @return A zydis status code */ ZYDIS_EXPORT ZydisStatus ZydisUtilsCalcAbsoluteTargetAddress(const ZydisInstructionInfo* info, const ZydisOperandInfo* operand, uint64_t* address); -/* ---------------------------------------------------------------------------------------------- */ - /* ============================================================================================== */ #ifdef __cplusplus diff --git a/include/Zydis/Zydis.h b/include/Zydis/Zydis.h index 62da695..b2ec3ac 100644 --- a/include/Zydis/Zydis.h +++ b/include/Zydis/Zydis.h @@ -87,6 +87,25 @@ extern "C" { /* ---------------------------------------------------------------------------------------------- */ +/* ============================================================================================== */ +/* Enums and types */ +/* ============================================================================================== */ + +/** + * @brief Defines the zydis feature datatype. + */ +typedef uint8_t ZydisFeature; + +/** + * @brief Values that represent zydis features. + */ +enum ZydisFeatures +{ + ZYDIS_FEATURE_IMPLICITLY_USED_REGISTERS, + ZYDIS_FEATURE_AFFECTED_FLAGS, + ZYDIS_FEATURE_CPUID +}; + /* ============================================================================================== */ /* Exported functions */ /* ============================================================================================== */ @@ -101,6 +120,15 @@ extern "C" { */ ZYDIS_EXPORT uint64_t ZydisGetVersion(); +/** + * @brief Checks, if the specified feature is enabled in the current zydis library instance. + * + * @param feature The feature. + * + * @return @c True if the feature is enabled, @c false if not. + */ +ZYDIS_EXPORT bool ZydisIsFeatureEnabled(ZydisFeature feature); + /* ============================================================================================== */ #ifdef __cplusplus diff --git a/src/Zydis.c b/src/Zydis.c index 8d88c3f..ce88ef6 100644 --- a/src/Zydis.c +++ b/src/Zydis.c @@ -35,4 +35,30 @@ uint64_t ZydisGetVersion() return ZYDIS_VERSION; } +bool ZydisIsFeatureEnabled(ZydisFeature feature) +{ + switch (feature) + { + case ZYDIS_FEATURE_IMPLICITLY_USED_REGISTERS: +#ifdef ZYDIS_ENABLE_FEATURE_IMPLICITLY_USED_REGISTERS + return true; +#else + return false; +#endif + case ZYDIS_FEATURE_AFFECTED_FLAGS: +#ifdef ZYDIS_ENABLE_FEATURE_AFFECTED_FLAGS + return true; +#else + return false; +#endif + case ZYDIS_FEATURE_CPUID: +#ifdef ZYDIS_ENABLE_FEATURE_CPUID + return true; +#else + return false; +#endif + } + return false; +} + /* ============================================================================================== */