Zydis Documentation
repository·master·Indexed 26 days ago
https://github.com/zyantific/zydisA fast, lightweight, and high-performance x86/x86-64 (AMD64) disassembler and code generation (encoder) library. Zydis is thread-safe, requires no dynamic memory allocation, and has no third-party dependencies. It includes features for custom symbol resolution, instruction tokenization, and support for Windows kernel-mode drivers. The library provides official bindings for Rust and Python 3, and can be built via CMake or MSVC.
What's inside Zydis
Configure Zydis for Kernel Mode development
masterTo build the
Kernel modeconfiguration, you must have the Microsoft Windows Driver Kit (WDK) installed.Requirements:
- Install the WDK from: https://developer.microsoft.com/en-us/windows/hardware/windows-driver-kit
- Important: If you have an existing WDK installation, ensure it is updated to at least Windows 10 version 1709 (10.0.16299.0) or newer to avoid bugs when opening the solution file.
Use the Amalgamated Distribution
masterFor projects that want to avoid complex build systems, Zydis provides an auto-generated single header and single source file variant. To use it, simply copy these two files into your project. You can find these on the release page aszydis-amalgamated.tar.gz.Port Zydis Decoder usage from v4 to v5
masterWhen upgrading the Decoder from v4 to v5, the following struct changes must be addressed:
ZydisDecodedOperandImmstruct- New field
offset: Contains the offset of the immediate data, relative to the beginning of the instruction, in bytes. - New field
size: Contains the physical immediate size, in bits.
ZydisDecodedOperandMemDisp_struct- New field
offset: Contains the offset of the immediate data, relative to the beginning of the instruction, in bytes. - New field
size: Contains the physical displacement size, in bits. - Removed field
has_displacement: This field has been removed. To check if a displacement exists, check ifsizeis non-zero. Asizeof 0 indicates there is no displacement.
- New field
Port Zydis Encoder usage from v4 to v5
masterWhen upgrading the Encoder from v4 to v5, note the following changes to function behavior and requirements:
ZydisRegisterGetLargestEnclosing: For registers that do not have an enclosing register, this function now returns the register itself. In v4, it returnedZYDIS_REGISTER_NONEfor these cases.ZydisEncoderDecodedInstructionToEncoderRequest: This function now requires that the operand count passed is exactly equal toinstruction->operand_count_visible. In v4, passing a value lower than the maximum visible operand count was permitted, but this is no longer allowed in v5.
Build Zydis using MSVC project files
masterYou can build Zydis and its included tools/examples using the MSVC project files located in the
msvc/directory. The build system provides five distinct configurations, each supporting both 32/64-bit and Debug/Release modes:- Static with dynamic run-time library (MD)
- Static with static run-time library (MT)
- Dynamic (DLL) with dynamic run-time library (MD)
- Dynamic (DLL) with static run-time library (MT)
- Kernel mode
Note that the
Kernel modeconfiguration only builds theZydisproject and theZydisWinKerneldriver sample. All other configurations build all projects except forZydisWinKernel.Install Zydis via Package Managers
masterPre-built headers, shared libraries, and executables are available through several package managers depending on your operating system.
# Arch Linux pacman -S zydis # Debian / Ubuntu apt-get install libzydis-dev zydis-tools # Homebrew (macOS) brew install zydis # NixOS nix-shell -p zydis # vcpkg vcpkg install zydisMigrate from Zydis v3 to v4
masterWhen upgrading from version 3 to version 4, note the following general breaking changes:
- Compiler Requirement: Zydis now requires a C11 capable compiler.
- Type Renaming:
ZydisAddressWidthhas been renamed toZydisStackWidth. - Constant Renaming:
ZYDIS_STATIC_DEFINE$\rightarrow$ZYDIS_STATIC_BUILDZydis_EXPORTS$\rightarrow$ZYDIS_SHOULD_EXPORTZYDIS_ADDRESS_WIDTH_XXX$\rightarrow$ZYDIS_STACK_WIDTH_XXX
- Enum Changes:
ZydisMemoryOperandTypenow includesZYDIS_MEMOP_TYPE_VSIB. - Flag API Changes:
ZydisCPUFlagActionis replaced byZydisAccessedFlagsMask.ZydisAccessedFlagsis the new replacement for CPU flag arrays.ZYDIS_CPUFLAG_C[0-3]are replaced withZYDIS_FPUFLAG_C[0-3].
- Segment API:
ZydisGetInstructionSegmentsand its related types have been moved to a separate header file.
Use Zydis in a CMake Project
masterIf you are managing your own project with CMake, you can integrate Zydis as a submodule or external dependency. A complete example of how to set this up is available in thezyantific/zydis-submodule-examplerepository.Build Zydis using CMake
masterYou can use CMake to build Zydis on Windows, macOS, Linux, and BSDs. This is the recommended method for most platforms.
git clone --recursive 'https://github.com/zyantific/zydis.git' cd zydis cmake -B build cmake --build build -j4Disable specific Zydis features via preprocessor directives
masterAll Zydis features are enabled by default. To reduce the footprint or disable specific functionality, you can define preprocessor directives. For example, to disable the formatter, define
ZYDIS_DISABLE_FORMATTER.For a complete list of available feature switches, refer to the
CMakeLists.txtfile in the repository.Troubleshoot shared library relocation errors
masterIf you encounter relocation errors like
/usr/bin/ld: ... relocation R_X86_64_PC32 ... can not be used when making a shared object; recompile with -fPICwhen building Zydis as a static library to be linked into a shared library, force position-independent code by passing the following flag to your CMake invocation:-DCMAKE_POSITION_INDEPENDENT_CODE=ON