zyantific / zydis

Fast and lightweight x86/x86-64 disassembler and code generation library
https://zydis.re
MIT License
3.3k stars 427 forks source link

[vcpkg] Support for update version v4.1.0 #506

Closed FrankXie05 closed 1 month ago

FrankXie05 commented 1 month ago

Hi, all. I am the maintainer of vcpkg.

Recently I get an error when updating version to v4.1.0 on x64-windows:

LINK Pass 1: command "C:\PROGRA~1\MICROS~3\2022\COMMUN~1\VC\Tools\MSVC\1439~1.335\bin\Hostx64\x64\link.exe CMakeFiles\Zydis.dir\src\MetaInfo.c.obj CMakeFiles\Zydis.dir\src\Mnemonic.c.obj CMakeFiles\Zydis.dir\src\Register.c.obj CMakeFiles\Zydis.dir\src\Segment.c.obj CMakeFiles\Zydis.dir\src\SharedData.c.obj CMakeFiles\Zydis.dir\src\String.c.obj CMakeFiles\Zydis.dir\src\Utils.c.obj CMakeFiles\Zydis.dir\src\Zydis.c.obj CMakeFiles\Zydis.dir\src\Decoder.c.obj CMakeFiles\Zydis.dir\src\DecoderData.c.obj CMakeFiles\Zydis.dir\src\Encoder.c.obj CMakeFiles\Zydis.dir\src\EncoderData.c.obj CMakeFiles\Zydis.dir\src\Disassembler.c.obj CMakeFiles\Zydis.dir\src\Formatter.c.obj CMakeFiles\Zydis.dir\src\FormatterBuffer.c.obj CMakeFiles\Zydis.dir\src\FormatterATT.c.obj CMakeFiles\Zydis.dir\src\FormatterBase.c.obj CMakeFiles\Zydis.dir\src\FormatterIntel.c.obj CMakeFiles\Zydis.dir\resources\VersionInfo.rc.res /out:Zydis.dll /implib:Zydis.lib /pdb:Zydis.pdb /dll /version:4.1 /machine:x64 /nologo /debug /INCREMENTAL F:\vcpkg\installed\x64-windows\debug\lib\Zycore.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\Zydis.dir/intermediate.manifest CMakeFiles\Zydis.dir/manifest.res" failed (exit code 1120) with the following output:
   Creating library Zydis.lib and object Zydis.exp

String.c.obj : error LNK2019: unresolved external symbol ZYAN_DIV64 referenced in function ZydisStringAppendDecU64

Zydis.dll : fatal error LNK1120: 1 unresolved externals

Note I use zycore provided by vcpkg.

I see that the function definition of zycore is designed to work on Linux.

Request: Will our current support triplets for ZYAN_DIV64 change due to the above reasons? Or do we need to make any adjustments to support windows

Related PR: https://github.com/zyantific/zydis/pull/424 https://github.com/zyantific/zycore-c/pull/62

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 62d5de6..2588e53 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -138,7 +138,7 @@ function (locate_zycore)
     )
 endfunction ()

-locate_zycore()
+find_package(zycore CONFIG REQUIRED)

 # =============================================================================================== #
 # Library configuration                                                                           #
@@ -151,7 +151,7 @@ else ()
     target_compile_definitions("Zydis" PUBLIC "ZYDIS_STATIC_BUILD")
 endif ()

-target_link_libraries("Zydis" PUBLIC "Zycore")
+target_link_libraries("Zydis" PUBLIC Zycore::Zycore)
 target_include_directories("Zydis"
     PUBLIC
         $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
mappzor commented 1 month ago

I use zycore provided by vcpkg.

I'm not actively using vcpkg but according to this site zycore 1.5.0 should be available via vcpkg. This version contains ZYAN_DIV64 changes and should work fine, so please check if that's the version you have.

FrankXie05 commented 1 month ago

@mappzor Thanks for your Reply. :)

I don't see the exported symbol for ZYAN_DIV64 in local Zycore.lib. Also I don't see ZYCORE_EXPORT in the ZYAN_DIV64 code section.

Zycore:

#if defined(ZYAN_LINUX) && defined(ZYAN_KERNEL)
#   include <asm/div64.h> /* do_div */
#   define ZYAN_DIV64(n, divisor) do_div(n, divisor)
#else
#   define ZYAN_DIV64(n, divisor) (n /= divisor)
#endif

https://github.com/zyantific/zycore-c/blob/74620eefd233bec20daeb66e78e744ff06e273b7/include/Zycore/Defines.h#L489

This version contains ZYAN_DIV64 changes and should work fine

I think it is because zydis will build or deploy zycore by itself, and will not use cmake to find zycore.lib. https://github.com/zyantific/zydis/blob/569320ad3c4856da13b9dbf1f0d9e20bda63870e/CMakeLists.txt#L98

mappzor commented 1 month ago

ZYAN_DIV64 is a macro that on Windows will simply resolve to n /= divisor. It will never be exported as a symbol. Also Zydis doesn't need zycore.lib at all, it only uses its headers.

Sounds like vcpkg is pulling old headers from somewhere and those don't have ZYAN_DIV64 defined at all. In that case linker tries to resolve it as external symbol and obviously fails. Unfortunately I have no idea how vcpkg deals with dependencies and how it will work with CMake logic you've referenced.

FrankXie05 commented 1 month ago

@mappzor It seems that the core of the problem may be that the version of vcpkg's zycore is too low.

Thank you for your patience in explaining. I will try to update zycore to v1.5.0 and debug again. :)