diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 92df289..10e9079 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,12 @@ jobs: steps: - uses: actions/checkout@v2 - name: Build bddisasm and bdshemu - run: make -j$(nproc) + run: | + mkdir build + cd build + cmake .. + make + cd - - name: Install rapidjson uses: actions/checkout@master with: @@ -88,5 +93,5 @@ jobs: - name: Run cppcheck run: cppcheck --error-exitcode=1 --language=c \ --enable=all --suppress=missingIncludeSystem --suppress=unusedStructMember --suppress=unusedFunction \ - -I inc/ -I inc/bdshemu -I bddisasm/include bddisasm/ bdshemu/ \ + -I inc/ -I bddisasm/include bddisasm/ bdshemu/ \ / diff --git a/.gitignore b/.gitignore index 587ccdd..9adf611 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,7 @@ bdshemu_fuzz/shfuzz bdshemu_fuzz/out-32 bdshemu_fuzz/out-64 docs/build +libbddisasm.pc +build +.vscode +disasmtool_lix/_build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ddc19ce --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,135 @@ +cmake_minimum_required(VERSION 3.12) + +include("${CMAKE_CURRENT_LIST_DIR}/project-meta-info.in") + +set(disasm_version_file ${CMAKE_CURRENT_LIST_DIR}/inc/version.h) + +file(STRINGS ${disasm_version_file} disasm_ver_major REGEX "DISASM_VERSION_MAJOR") +file(STRINGS ${disasm_version_file} disasm_ver_minor REGEX "DISASM_VERSION_MINOR") +file(STRINGS ${disasm_version_file} disasm_ver_patch REGEX "DISASM_VERSION_REVISION") + +string(REGEX REPLACE "#define DISASM_VERSION_MAJOR[ \t\r\n]*" "" disasm_ver_major ${disasm_ver_major}) +string(REGEX REPLACE "#define DISASM_VERSION_MINOR[ \t\r\n]*" "" disasm_ver_minor ${disasm_ver_minor}) +string(REGEX REPLACE "#define DISASM_VERSION_REVISION[ \t\r\n]*" "" disasm_ver_patch ${disasm_ver_patch}) + +message(STATUS "Extracted version from ${disasm_version_file}: ${disasm_ver_major}.${disasm_ver_minor}.${disasm_ver_patch}") + +project(bddisasm + VERSION ${disasm_ver_major}.${disasm_ver_minor}.${disasm_ver_patch} + DESCRIPTION ${project_description} + LANGUAGES C + ) + +if (NOT CMAKE_BUILD_TYPE) + message(STATUS "No build type given. Will use 'Release'") + set(CMAKE_BUILD_TYPE Release) +endif () + +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/x64/${CMAKE_BUILD_TYPE}) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/x64/${CMAKE_BUILD_TYPE}) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/x64/${CMAKE_BUILD_TYPE}) + +message(STATUS "Output directory set to: ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") + +set(bddisasm_src + bddisasm/crt.c + bddisasm/bddisasm.c + ) +add_library(bddisasm STATIC ${bddisasm_src}) +set_target_properties(bddisasm PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_STANDARD 11 + VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} + ) + +set(bdshemu_src + bdshemu/bdshemu.c + ) +add_library(bdshemu STATIC ${bdshemu_src}) +set_target_properties(bdshemu PROPERTIES + POSITION_INDEPENDENT_CODE ON + C_STANDARD 11 + VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} + ) +add_dependencies(bdshemu bddisasm) + +include_directories( + inc + inc/bdshemu + bddisasm/include + ) + +if (CMAKE_BUILD_TYPE EQUAL "Release") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Ofast -DNDEBUG") +else () + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -D_DEBUG -DDEBUG") +endif () + +add_compile_options( + "$<$:-U_FORTIFY_SOURCE>" + "$<$:-D_FORTIFY_SOURCE=2>" + -Wall + -Wno-unknown-pragmas + -Wextra + -Wshadow + -Wformat-security + -Wstrict-overflow=2 + -Wstrict-prototypes + -Wwrite-strings + -Wshadow + -Winit-self + -Wswitch-default + -Wmissing-variable-declarations + -Wused-but-marked-unused + -Wswitch-enum + -Wno-unused-function + -Wno-multichar + -Wno-incompatible-pointer-types + -Wcast-qual + -Wnull-dereference + -Wduplicated-cond + -Werror=format-security + -Werror=implicit-function-declaration + -pipe + -fwrapv + -fno-strict-aliasing + -fstack-protector-strong + -fno-omit-frame-pointer + -ffunction-sections + -fdata-sections + -g3 + -gdwarf-4 + -grecord-gcc-switches + -march=nehalem + ) + +include(GNUInstallDirs) + +set(CMAKE_SKIP_BUILD_RPATH TRUE) +set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) +set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") +set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + +get_filename_component(public_header_path "${PROJECT_SOURCE_DIR}/inc" REALPATH) +file(GLOB_RECURSE public_headers "${public_header_path}/*.h") +set_target_properties(bddisasm PROPERTIES PUBLIC_HEADER "${public_headers}") + +set(DEST_DIR "${CMAKE_INSTALL_PREFIX}") +set(LIB_DIR "${CMAKE_INSTALL_LIBDIR}") +set(INC_DIR "${CMAKE_INSTALL_INCLUDEDIR}") +set(PRIVATE_LIBS "-lbdshemu -lbddisasm") +set(DATA_DIR "${CMAKE_INSTALL_DATADIR}") + +CONFIGURE_FILE("${CMAKE_STATIC_LIBRARY_PREFIX}bddisasm.pc.in" + "${PROJECT_SOURCE_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}bddisasm.pc" + @ONLY + ) + +INSTALL(TARGETS bddisasm bdshemu + LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" + PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/bddisasm" + ) + +INSTALL(FILES "${PROJECT_SOURCE_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}bddisasm.pc" + DESTINATION "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" + ) diff --git a/README.md b/README.md index a31cd16..9087621 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ The main objectives of this disassembler are: 4. Easy to work with - just include the main header file, bddisasm.h, link with the bddisasm library, and call the NdDecode API! 5. Complete - support every x86 instruction to date, and provide as much information as possible. -## Build +## Build and install ### Windows @@ -54,6 +54,31 @@ In order to build the projects on Linux you need: * gcc * make +* cmake 3.12 or newer (optional) + +#### Building and installing with make + +In order to build bddisasm and bdshemu run `make` in the root of the repository. The results will be placed in the bin directory. + +In order to install bddisasm and bdshemu run `make install`. + +#### Building and installing with cmake + +```console +mkdir build +cd build + +cmake .. + +make +make install +``` + +The default build type is Release. + +Note that the current install behavior differs based on how the installation is done. When using make, the `bdshemu.h` header is placed by default in `include/bddisasm/bdshemu` in your install location, while the cmake method will place it in `include/bddisasm`. Using cmake also provides support for pkg-config. + +### Building disasmtool_lix For disasmtool_lix you also need: @@ -61,8 +86,6 @@ For disasmtool_lix you also need: * cmake 3.12 or newer * [RapidJSON](https://github.com/Tencent/rapidjson/) -In order to build bddisasm and bdshemu run `make` in the root of the repository. The results will be placed in the bin directory. - In order to build disasmtool_lix go to the disasmtool_lix directory and run `make`. The results will be in the bin directory in the disasmtool_lix/build directory. ## Decoding instructions diff --git a/bdshemu/Makefile b/bdshemu/Makefile index 5fe016f..0564447 100644 --- a/bdshemu/Makefile +++ b/bdshemu/Makefile @@ -4,7 +4,7 @@ SRC_FILES := bdshemu.c OBJECTS := $(SRC_FILES:.c=.o) -INCLUDES := -I. -I../inc -I../bddisasm/include -I../inc/bdshemu +INCLUDES := -I. -I../inc -I../bddisasm/include ifeq ($(PLATFORM),) PLATFORM := x64 @@ -119,5 +119,5 @@ clean: clean_lib_file clean_int_dir clean_dep_dir install: all install -d $(DESTDIR)$(PREFIX)/lib/ install -m 644 $(OUT_DIR)/$(LIB_NAME) $(DESTDIR)$(PREFIX)/lib/ - install -d $(DESTDIR)$(PREFIX)/include/bddisasm/bdshemu/ - cp -r ../inc/bdshemu/* $(DESTDIR)$(PREFIX)/include/bddisasm/bdshemu/ \ No newline at end of file + install -d $(DESTDIR)$(PREFIX)/include/bddisasm/ + cp -r ../inc/bdshemu.h $(DESTDIR)$(PREFIX)/include/bddisasm/ \ No newline at end of file diff --git a/bdshemu/bdshemu.c b/bdshemu/bdshemu.c index 991ac5a..3317cea 100644 --- a/bdshemu/bdshemu.c +++ b/bdshemu/bdshemu.c @@ -9,7 +9,7 @@ #include "nd_crt.h" #include "bddisasm.h" -#include "bdshemu/bdshemu.h" +#include "bdshemu.h" // diff --git a/bdshemu/bdshemu.vcxproj b/bdshemu/bdshemu.vcxproj index 8b8e939..d2b40e0 100644 --- a/bdshemu/bdshemu.vcxproj +++ b/bdshemu/bdshemu.vcxproj @@ -405,7 +405,7 @@ - + diff --git a/bdshemu/bdshemu.vcxproj.filters b/bdshemu/bdshemu.vcxproj.filters index 626a3f0..ba58200 100644 --- a/bdshemu/bdshemu.vcxproj.filters +++ b/bdshemu/bdshemu.vcxproj.filters @@ -23,7 +23,7 @@ - + Header Files\public diff --git a/disasmtool/disasmtool.c b/disasmtool/disasmtool.c index 4472f27..e15abcf 100644 --- a/disasmtool/disasmtool.c +++ b/disasmtool/disasmtool.c @@ -11,7 +11,7 @@ typedef uint64_t QWORD, *PQWORD; // Main disasm header file. -#include "bdshemu\bdshemu.h" +#include "bdshemu.h" #include "bddisasm.h" diff --git a/disasmtool_lix/CMakeLists.txt b/disasmtool_lix/CMakeLists.txt index 17ea8d8..b937996 100644 --- a/disasmtool_lix/CMakeLists.txt +++ b/disasmtool_lix/CMakeLists.txt @@ -253,7 +253,6 @@ endif() include_directories( ${DISASM_DIRECTORY}/inc - ${DISASM_DIRECTORY}/inc/bdshemu ${DISASM_DIRECTORY}/bddisasm/include ) diff --git a/inc/bdshemu/bdshemu.h b/inc/bdshemu.h similarity index 99% rename from inc/bdshemu/bdshemu.h rename to inc/bdshemu.h index 06eea39..31080af 100644 --- a/inc/bdshemu/bdshemu.h +++ b/inc/bdshemu.h @@ -6,7 +6,7 @@ #define BDSHEMU_H -#include "../bddisasm.h" +#include "bddisasm.h" // diff --git a/libbddisasm.pc.in b/libbddisasm.pc.in new file mode 100644 index 0000000..9a0ff8c --- /dev/null +++ b/libbddisasm.pc.in @@ -0,0 +1,19 @@ +dir_prefix=@DEST_DIR@ +lib=@LIB_DIR@ +include=@INC_DIR@ +data_dir=@DATA_DIR@ + +prefix=${dir_prefix} +lib_dir=${dir_prefix}/${lib} +include_dir=${dir_prefix}/${include} + +datarootdir=${dir_prefix}/${data_dir} +pkgdatadir=${datarootdir}/bddisasm + +Name: bddisasm +Description: "Bitdefender x86 instruction decoder and shellcode emulator" +URL: https://github.com/bitdefender/bddisasm +Version: @PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@ + +Libs: -L${lib_dir} @PRIVATE_LIBS@ +Cflags: -I${include_dir} diff --git a/project-meta-info.in b/project-meta-info.in new file mode 100644 index 0000000..a56c711 --- /dev/null +++ b/project-meta-info.in @@ -0,0 +1,3 @@ +# project-meta-info.in + +set(project_description "Bitdefender x86 instruction decoder and emulator")