Skip to content

Commit 2852492

Browse files
committed
Merge branch 'develop'
2 parents 9ec1e0c + 778401a commit 2852492

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+13024
-12099
lines changed

CMakeLists.txt

+61-25
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,32 @@ option(ZYDIS_FEATURE_DECODER
1515
option(ZYDIS_FEATURE_FORMATTER
1616
"Enable instruction formatting functionality"
1717
ON)
18-
option(ZYDIS_FEATURE_EVEX
19-
"Enable support for EVEX instructions"
18+
option(ZYDIS_FEATURE_EVEX
19+
"Enable support for EVEX instructions"
2020
ON)
21-
option(ZYDIS_FEATURE_MVEX
22-
"Enable support for MVEX instructions"
21+
option(ZYDIS_FEATURE_MVEX
22+
"Enable support for MVEX instructions"
2323
ON)
2424

2525
# Build configuration
2626
option(BUILD_SHARED_LIBS
2727
"Build shared libraries"
2828
OFF)
29+
option(ZYDIS_WHOLE_PROGRAM_OPTIMIZATION
30+
"Enable whole program optimization"
31+
OFF)
2932
option(ZYDIS_NO_LIBC
3033
"Don't use any C standard library functions (for exotic build-envs like kernel drivers)"
3134
OFF)
32-
option(ZYDIS_BUILD_EXAMPLES
33-
"Build examples"
35+
option(ZYDIS_BUILD_EXAMPLES
36+
"Build examples"
3437
ON)
35-
option(ZYDIS_BUILD_TOOLS
36-
"Build tools"
38+
option(ZYDIS_BUILD_TOOLS
39+
"Build tools"
3740
ON)
3841
option(ZYDIS_FUZZ_AFL_FAST
39-
"Enables AFL persistent mode and reduces prints in ZydisFuzzIn"
40-
OFF)
42+
"Enables AFL persistent mode and reduces prints in ZydisFuzzIn"
43+
OFF)
4144
option(ZYDIS_DEV_MODE
4245
"Enable developer mode (-Wall, -Werror, ...)"
4346
OFF)
@@ -66,10 +69,15 @@ endif ()
6669

6770
add_library("Zydis")
6871

69-
target_include_directories("Zydis"
72+
target_include_directories("Zydis"
7073
PUBLIC "include" ${PROJECT_BINARY_DIR}
7174
PRIVATE "src")
7275
target_compile_definitions("Zydis" PRIVATE "_CRT_SECURE_NO_WARNINGS" "ZYDIS_EXPORTS")
76+
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
77+
set_target_properties("Zydis" PROPERTIES COMPILE_FLAGS "/GL")
78+
set_target_properties("Zydis" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
79+
set_target_properties("Zydis" PROPERTIES STATIC_LIBRARY_FLAGS_RELEASE "/LTCG")
80+
endif ()
7381
generate_export_header("Zydis" BASE_NAME "ZYDIS" EXPORT_FILE_NAME "ZydisExportConfig.h")
7482

7583
if (NOT ZYDIS_FEATURE_ENCODER AND NOT ZYDIS_FEATURE_DECODER)
@@ -80,6 +88,13 @@ if (NOT ZYDIS_FEATURE_ENCODER AND NOT ZYDIS_FEATURE_DECODER)
8088
)
8189
endif ()
8290

91+
if (ZYDIS_FEATURE_FORMATTER AND NOT ZYDIS_FEATURE_DECODER)
92+
message(
93+
FATAL_ERROR
94+
"\nZYDIS_FEATURE_FORMATTER requires ZYDIS_FEATURE_DECODER to be enabled"
95+
)
96+
endif ()
97+
8398
if (NOT ZYDIS_FEATURE_DECODER)
8499
target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_DECODER")
85100
endif ()
@@ -97,7 +112,7 @@ if (ZYDIS_NO_LIBC)
97112
endif ()
98113

99114
target_sources("Zydis"
100-
PUBLIC
115+
PRIVATE
101116
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/CommonTypes.h"
102117
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Defines.h"
103118
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/MetaInfo.h"
@@ -110,7 +125,6 @@ target_sources("Zydis"
110125
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Zydis.h"
111126
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/LibC.h"
112127
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/SharedData.h"
113-
PRIVATE
114128
"src/MetaInfo.c"
115129
"src/Mnemonic.c"
116130
"src/Register.c"
@@ -120,16 +134,19 @@ target_sources("Zydis"
120134
"src/Zydis.c")
121135

122136
if (ZYDIS_FEATURE_DECODER)
123-
target_sources("Zydis"
124-
PUBLIC
137+
target_sources("Zydis"
138+
PRIVATE
125139
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Decoder.h"
126140
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/DecoderTypes.h"
127-
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Formatter.h"
128141
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/DecoderData.h"
129-
PRIVATE
130142
"src/Decoder.c"
131-
"src/DecoderData.c"
132-
"src/Formatter.c")
143+
"src/DecoderData.c")
144+
if (ZYDIS_FEATURE_FORMATTER)
145+
target_sources("Zydis"
146+
PRIVATE
147+
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Formatter.h"
148+
"src/Formatter.c")
149+
endif ()
133150
endif ()
134151

135152
if (BUILD_SHARED_LIBS AND WIN32)
@@ -141,14 +158,17 @@ install(TARGETS "Zydis"
141158
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
142159
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
143160
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
144-
install(DIRECTORY "include" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
161+
install(FILES
162+
"${PROJECT_BINARY_DIR}/ZydisExportConfig.h"
163+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
164+
install(DIRECTORY "include/" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
145165

146166
# =============================================================================================== #
147167
# Examples #
148168
# =============================================================================================== #
149169

150170
if (ZYDIS_BUILD_EXAMPLES)
151-
if (ZYDIS_FEATURE_DECODER)
171+
if (ZYDIS_FEATURE_DECODER AND ZYDIS_FEATURE_FORMATTER)
152172
add_executable("FormatterHooks" "examples/FormatterHooks.c")
153173
target_link_libraries("FormatterHooks" "Zydis")
154174
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter")
@@ -158,14 +178,22 @@ if (ZYDIS_BUILD_EXAMPLES)
158178
target_link_libraries("ZydisFuzzIn" "Zydis")
159179
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
160180
target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS")
161-
if (ZYDIS_FUZZ_AFL_FAST)
162-
target_compile_definitions("ZydisFuzzIn" PRIVATE "ZYDIS_FUZZ_AFL_FAST")
163-
endif ()
181+
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
182+
set_target_properties("ZydisFuzzIn" PROPERTIES COMPILE_FLAGS "/GL")
183+
set_target_properties("ZydisFuzzIn" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
184+
endif ()
185+
if (ZYDIS_FUZZ_AFL_FAST)
186+
target_compile_definitions("ZydisFuzzIn" PRIVATE "ZYDIS_FUZZ_AFL_FAST")
187+
endif ()
164188

165189
add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c")
166190
target_link_libraries("ZydisPerfTest" "Zydis")
167191
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
168192
target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS")
193+
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
194+
set_target_properties("ZydisPerfTest" PROPERTIES COMPILE_FLAGS "/GL")
195+
set_target_properties("ZydisPerfTest" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
196+
endif ()
169197
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
170198
target_compile_definitions("ZydisPerfTest" PRIVATE "_GNU_SOURCE")
171199
find_package(Threads REQUIRED)
@@ -179,15 +207,23 @@ endif ()
179207
# =============================================================================================== #
180208

181209
if (ZYDIS_BUILD_TOOLS)
182-
if (ZYDIS_FEATURE_DECODER)
210+
if (ZYDIS_FEATURE_DECODER AND ZYDIS_FEATURE_FORMATTER)
183211
add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
184212
target_link_libraries("ZydisDisasm" "Zydis")
185213
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
186214
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")
215+
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
216+
set_target_properties("ZydisDisasm" PROPERTIES COMPILE_FLAGS "/GL")
217+
set_target_properties("ZydisDisasm" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
218+
endif ()
187219

188220
add_executable("ZydisInfo" "tools/ZydisInfo.c")
189221
target_link_libraries("ZydisInfo" "Zydis")
190222
set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools")
191223
target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS")
224+
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
225+
set_target_properties("ZydisInfo" PROPERTIES COMPILE_FLAGS "/GL")
226+
set_target_properties("ZydisInfo" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
227+
endif ()
192228
endif ()
193229
endif ()

LICENSE

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2017 Florian Bernd
4-
Copyright (c) 2017 Joel Höner
3+
Copyright (c) 2018 Florian Bernd
4+
Copyright (c) 2018 Joel Höner
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

README.md

+27-31
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,72 @@
11
![zydis logo](https://mainframe.pw/u/P94JAqY9XSDdPedv.svg?x)
2-
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) [![Gitter](https://badges.gitter.im/zyantific/zyan-disassembler-engine.svg)](https://gitter.im/zyantific/zyan-disassembler-engine?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge) [![Build status](https://ci.appveyor.com/api/projects/status/2tad27q0b9v6qtga/branch/master?svg=true)](https://ci.appveyor.com/project/athre0z/zydis/branch/master)
2+
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) [![Gitter](https://badges.gitter.im/zyantific/zyan-disassembler-engine.svg)](https://gitter.im/zyantific/zydis?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge) [![Discord](https://img.shields.io/discord/390136917779415060.svg)](https://discordapp.com/channels/390136917779415060/390138781313007626) [![Build status](https://ci.appveyor.com/api/projects/status/2tad27q0b9v6qtga/branch/master?svg=true)](https://ci.appveyor.com/project/athre0z/zydis/branch/master)
33

44
Fast and lightweight x86/x86-64 disassembler library.
55

66
## Features
77

8-
- Supports all x86 and x86-64 (AMD64) instructions.
9-
- Supports pretty much all ISA extensions (list incomplete):
10-
- FPU (x87), MMX
11-
- SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, SSE4A, AESNI
12-
- AVX, AVX2, AVX512BW, AVX512CD, AVX512DQ, AVX512ER, AVX512F, AVX512PF, AVX512VL
13-
- ADX, BMI1, BMI2, FMA, FMA4
8+
- Supports all x86 and x86-64 (AMD64) instructions and [extensions](https://github.com/zyantific/zydis/blob/master/include/Zydis/Generated/EnumISAExt.h)
149
- Optimized for high performance
1510
- No dynamic memory allocation ("malloc")
11+
- Thread-safe by design
1612
- Very small file-size overhead compared to other common disassembler libraries
1713
- [Complete doxygen documentation](https://www.zyantific.com/doc/zydis/index.html)
18-
- No dependencies on platform specific APIs
19-
- Should compile on any platform with a complete libc and CMake
20-
- Tested on Windows, macOS and Linux
21-
22-
## Roadmap
23-
24-
- Language bindings [v2.0 final]
25-
- Tests [v2.0 final]
26-
- Graphical editor for the instruction-database [v2.0 final]
27-
- Implement CMake feature gates. Currently, everything is always included. [v2.0 final]
28-
- Encoding support [v2.1]
14+
- Absolutely no dependencies — [not even libc](https://github.com/zyantific/zydis/blob/develop/CMakeLists.txt#L32)
15+
- Should compile on any platform with a working C99 compiler
16+
- Tested on Windows, macOS, FreeBSD and Linux, both user and kernel mode
2917

3018
## Quick Example
3119

3220
The following example program uses Zydis to disassemble a given memory buffer and prints the output to the console.
3321

3422
```C
3523
#include <stdio.h>
24+
#include <inttypes.h>
3625
#include <Zydis/Zydis.h>
3726

3827
int main()
3928
{
4029
uint8_t data[] =
4130
{
42-
0x51, 0x8D, 0x45, 0xFF, 0x50, 0xFF, 0x75, 0x0C, 0xFF, 0x75,
43-
0x08, 0xFF, 0x15, 0xA0, 0xA5, 0x48, 0x76, 0x85, 0xC0, 0x0F,
31+
0x51, 0x8D, 0x45, 0xFF, 0x50, 0xFF, 0x75, 0x0C, 0xFF, 0x75,
32+
0x08, 0xFF, 0x15, 0xA0, 0xA5, 0x48, 0x76, 0x85, 0xC0, 0x0F,
4433
0x88, 0xFC, 0xDA, 0x02, 0x00
4534
};
4635

4736
// Initialize decoder context.
4837
ZydisDecoder decoder;
4938
ZydisDecoderInit(
50-
&decoder,
51-
ZYDIS_MACHINE_MODE_LONG_64,
39+
&decoder,
40+
ZYDIS_MACHINE_MODE_LONG_64,
5241
ZYDIS_ADDRESS_WIDTH_64);
5342

5443
// Initialize formatter. Only required when you actually plan to
5544
// do instruction formatting ("disassembling"), like we do here.
5645
ZydisFormatter formatter;
5746
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
58-
47+
5948
// Loop over the instructions in our buffer.
49+
// The IP is chosen arbitrary here in order to better visualize
50+
// relative addressing.
6051
uint64_t instructionPointer = 0x007FFFFFFF400000;
61-
uint8_t* readPointer = data;
52+
size_t offset = 0;
6253
size_t length = sizeof(data);
6354
ZydisDecodedInstruction instruction;
6455
while (ZYDIS_SUCCESS(ZydisDecoderDecodeBuffer(
65-
&decoder, readPointer, length, instructionPointer, &instruction)))
56+
&decoder, data + offset, length - offset,
57+
instructionPointer, &instruction)))
6658
{
6759
// Print current instruction pointer.
6860
printf("%016" PRIX64 " ", instructionPointer);
6961

70-
// Format & print the binary instruction
62+
// Format & print the binary instruction
7163
// structure to human readable format.
7264
char buffer[256];
7365
ZydisFormatterFormatInstruction(
7466
&formatter, &instruction, buffer, sizeof(buffer));
7567
puts(buffer);
7668

77-
readPointer += instruction.length;
78-
length -= instruction.length;
69+
offset += instruction.length;
7970
instructionPointer += instruction.length;
8071
}
8172
}
@@ -96,25 +87,30 @@ The above example program generates the following output:
9687
007FFFFFFF400013 js 0x007FFFFFFF42DB15
9788
```
9889

99-
## Compilation
90+
## Build
91+
92+
#### Unix
10093

10194
Zydis builds cleanly on most platforms without any external dependencies. You can use CMake to generate project files for your favorite C99 compiler.
10295

10396
```bash
104-
# Linux and OS X
10597
git clone 'https://github.com/zyantific/zydis.git'
10698
cd zydis
10799
mkdir build && cd build
108100
cmake ..
109101
make
110102
```
111103

104+
#### Windows
105+
106+
Either use the [Visual Studio 2017 project](https://github.com/zyantific/zydis/tree/master/msvc) or build Zydis using [CMake](https://cmake.org/download/) ([video guide](https://www.youtube.com/watch?v=fywLDK1OAtQ)).
107+
112108
## `ZydisInfo` tool
113109
![ZydisInfo](https://raw.githubusercontent.com/zyantific/zydis/master/assets/screenshots/ZydisInfo.png)
114110

115111
## Credits
116112
- Intel (for open-sourcing [XED](https://github.com/intelxed/xed), allowing for automatic comparision of our tables against theirs, improving both)
117-
- LLVM (for providing pretty solid instruction data as well)
113+
- [LLVM](https://llvm.org) (for providing pretty solid instruction data as well)
118114
- Christian Ludloff (http://sandpile.org, insanely helpful)
119115
- [LekoArts](https://www.lekoarts.de/) (for creating the project logo)
120116
- Our [contributors on GitHub](https://github.com/zyantific/zydis/graphs/contributors)

0 commit comments

Comments
 (0)