From e7803bdf72cd4c4d0a77c6864a010d9f1fae0d92 Mon Sep 17 00:00:00 2001 From: Ionel-Cristinel ANICHITEI Date: Tue, 30 Mar 2021 21:58:03 +0300 Subject: [PATCH] Implement nd_vsnprintf_s and nd_memset if possible --- CMakeLists.txt | 25 +++++++++++++++++++++++++ README.md | 8 ++++++++ bddisasm/crt.c | 20 +++++++++++++++++++- disasmtool/disasmtool.c | 5 ++++- disasmtool_lix/disasmtool.cpp | 5 ++++- 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 12ce8bb..441c635 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.12) option(BDD_INCLUDE_TOOL "Include the disasmtool executable" ON) option(BDD_INCLUDE_ISAGENERATOR "Include the isagenerator target (if a python interpreter is found)" ON) +option(BDD_USE_EXTERNAL_VSNPRINTF "Expect nd_vsnprintf_s implementation from the integrator" OFF) +option(BDD_USE_EXTERNAL_MEMSET "Expect nd_memset implementation from the integrator" OFF) set(BDD_VER_FILE ${CMAKE_CURRENT_LIST_DIR}/inc/version.h) @@ -87,6 +89,9 @@ set(BDDISASM_INSTALL_INCLUDE_DIR # -- bddisasm -- +include(CheckFunctionExists) +include(CheckSymbolExists) + add_library( bddisasm STATIC bddisasm/crt.c @@ -103,6 +108,26 @@ add_library( bddisasm/include/tabledefs.h "${BDDISASM_PUBLIC_HEADERS}") +if (NOT BDD_USE_EXTERNAL_VSNPRINTF) + check_function_exists(vsnprintf HAS_VSNPRINTF) + if (HAS_VSNPRINTF) + target_compile_definitions(bddisasm PUBLIC -DBDDISASM_HAS_VSNPRINTF) + else () + # See https://cmake.org/Bug/view.php?id=15659 + check_symbol_exists(vsnprintf stdio.h HAS_VSNPRINTF_SYMBOL) + if (HAS_VSNPRINTF_SYMBOL) + target_compile_definitions(bddisasm PUBLIC -DBDDISASM_HAS_VSNPRINTF) + endif () + endif () +endif () + +if (NOT BDD_USE_EXTERNAL_MEMSET) + check_function_exists(memset HAS_MEMSET) + if (HAS_MEMSET) + target_compile_definitions(bddisasm PUBLIC -DBDDISASM_HAS_MEMSET) + endif () +endif () + set_target_properties( bddisasm PROPERTIES POSITION_INDEPENDENT_CODE ON diff --git a/README.md b/README.md index 6d88a83..c8b9f3c 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,10 @@ target_link_libraries(decoder-tool PRIVATE bddisasm::bddisasm) target_link_libraries(emulator-tool PRIVATE bddisasm::bdshemu) ``` +### nd_vsnprintf_s and nd_memset + +By default, if `vsnprintf` and `memset` functions are available, the `nd_vsnprintf_s` and `nd_memset` functions are implemented directly by `bddisasm`. To signal this, `BDDISASM_HAS_VSNPRINTF` and `BDDISASM_HAS_MEMSET` will be added to the public compile definitions of `bddisasm`. This can be disabled by configuring CMake with `BDD_USE_EXTERNAL_VSNPRINTF=ON` and `BDD_USE_EXTERNAL_MEMSET=ON`. + #### Using as a sub-project The project can be consumed as a sub-project, either by adding it as a git submodule, or by using [CMake's FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html): @@ -127,6 +131,8 @@ To build the project run `make` in the root of the repository. This will build o To install the project run `make install`. Depending on the install location you may need to run the command as root. +[nd_vsnprintf_s and nd_memset](#nd_vsnprintf_s-and-nd_memset) will not be defined by `bddisasm`, integrators must provide these functions. + ### Using MSBuild on Windows In order to build the projects on Windows you need: @@ -146,6 +152,8 @@ Building any of the projects is done directly from Visual Studio. The results will be in the bin directory in the root of the repository. +[nd_vsnprintf_s and nd_memset](#nd_vsnprintf_s-and-nd_memset) will not be defined by `bddisasm`, integrators must provide these functions. + ## Decoding instructions ### Decoding API diff --git a/bddisasm/crt.c b/bddisasm/crt.c index 1aa5889..cb2272d 100644 --- a/bddisasm/crt.c +++ b/bddisasm/crt.c @@ -4,7 +4,6 @@ */ #include "include/nd_crt.h" - // // nd_strcat_s // @@ -42,3 +41,22 @@ nd_strcat_s( return dst; } + +#if !defined(BDDISASM_NO_FORMAT) && defined(BDDISASM_HAS_VSNPRINTF) +#include + +int nd_vsnprintf_s(char *buffer, size_t sizeOfBuffer, size_t count, const char *format, va_list argptr) +{ + UNREFERENCED_PARAMETER(count); + return vsnprintf(buffer, sizeOfBuffer, format, argptr); +} +#endif // !defined(BDDISASM_NO_FORMAT) && defined(BDDISASM_HAS_VSNPRINTF) + +#if !defined(BDDISASM_NO_FORMAT) && defined(BDDISASM_HAS_MEMSET) +#include + +void *nd_memset(void *s, int c, size_t n) +{ + return memset(s, c, n); +} +#endif // !defined(BDDISASM_NO_FORMAT) && defined(BDDISASM_HAS_MEMSET) \ No newline at end of file diff --git a/disasmtool/disasmtool.c b/disasmtool/disasmtool.c index de36840..6e80362 100644 --- a/disasmtool/disasmtool.c +++ b/disasmtool/disasmtool.c @@ -62,6 +62,7 @@ char *gSpaces[16] = }; +#if !defined(BDDISASM_HAS_VSNPRINTF) // // nd_vsnprintf // @@ -75,12 +76,14 @@ int nd_vsnprintf_s( { return _vsnprintf_s(buffer, sizeOfBuffer, count, format, argptr); } +#endif // !defined(BDDISASM_HAS_VSNPRINTF) +#if !defined(BDDISASM_HAS_MEMSET) void* nd_memset(void *s, int c, size_t n) { return memset(s, c, n); } - +#endif // !defined(BDDISASM_HAS_MEMSET) // // set_to_string diff --git a/disasmtool_lix/disasmtool.cpp b/disasmtool_lix/disasmtool.cpp index 21e76bb..0c05271 100644 --- a/disasmtool_lix/disasmtool.cpp +++ b/disasmtool_lix/disasmtool.cpp @@ -77,19 +77,22 @@ struct options { extern "C" { +#if !defined(BDDISASM_HAS_VSNPRINTF) int nd_vsnprintf_s(char *buffer, size_t sizeOfBuffer, [[maybe_unused]] size_t count, const char *format, va_list argptr) { return vsnprintf(buffer, sizeOfBuffer, format, argptr); } +#endif // !defined(BDDISASM_HAS_VSNPRINTF) +#if !defined(BDDISASM_HAS_MEMSET) void * nd_memset(void *s, int c, size_t n) { return memset(s, c, n); } +#endif // !defined(BDDISASM_HAS_MEMSET) } - static bool _hexstring_to_bytes(options &opts) { if (!opts.hex_file.empty()) {