zyantific / zydis

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

Build failed: build zydis as a dynamic link library on MacOS #474

Closed JamesAldridge closed 10 months ago

JamesAldridge commented 10 months ago

Seems to be the same issue as https://github.com/zyantific/zydis/issues/350

System Specifications:

Operating System: macOS Sonoma 14.1 (23B74) CPU: 2.7 GHz Quad-Core Intel Core i7 Compiler Info: Apple clang version 15.0.0 (clang-1500.0.40.1) Target: x86_64-apple-darwin23.1.0 Thread model: posix

When i build zydis with shared flag, the problem I've encountered is as follows.

Jamess-MacBook-Pro-4:jak-project-v-update-zydis jamesaldridge$ cmake --build build --parallel $((sysctl -n hw.logicalcpu`)) [0/2] Re-checking globbed directories... [2/11] Linking C shared library third-party/zydis/libZydis.4.0.0.0.dylib FAILED: third-party/zydis/libZydis.4.0.0.0.dylib : && /usr/bin/clang -O3 -DNDEBUG -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk -dynamiclib -Wl,-headerpad_max_install_names -compatibility_version 4.0.0 -current_version 4.0.0 -o third-party/zydis/libZydis.4.0.0.0.dylib -install_name @rpath/libZydis.4.0.dylib third-party/zydis/CMakeFiles/Zydis.dir/src/MetaInfo.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Register.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Segment.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/SharedData.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/String.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Utils.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Zydis.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Decoder.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/DecoderData.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Encoder.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/EncoderData.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Disassembler.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/Formatter.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/FormatterBuffer.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/FormatterATT.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/FormatterBase.c.o third-party/zydis/CMakeFiles/Zydis.dir/src/FormatterIntel.c.o third-party/zydis/zycore/libZycore.a && : ld: warning: pointer not aligned at _STR_MNEMONIC+0x9 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x12 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x1B from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x24 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x2D from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x36 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x3F from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x51 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x5A from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x63 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x6C from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x75 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x7E from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x87 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0x99 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0xA2 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0xAB from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0xB4 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o ld: warning: pointer not aligned at _STR_MNEMONIC+0xBD from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/Mnemonic.c.o

ld: warning: pointer not aligned at _STR_PREF_REX_4E+0x0 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/FormatterBase.c.o ld: warning: pointer not aligned at _STR_PREF_REX_4F+0x0 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/FormatterBase.c.o ld: warning: pointer not aligned at _STR_ADD+0x0 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/FormatterIntel.c.o ld: warning: pointer not aligned at _STR_MUL+0x0 from /Users/jamesaldridge/dev/jak-project-v-update-zydis/build/third-party/zydis/CMakeFiles/Zydis.dir/src/FormatterIntel.c.o ld: pointer not 4-byte aligned at __DATA_CONST+0x39, fix alignment or disable chained fixups clang: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.`

Originally posted by @JamesAldridge in https://github.com/zyantific/zydis/issues/350#issuecomment-1893292945

flobernd commented 10 months ago

Hi @JamesAldridge , like @athre0z mentioned in the original issue, we don't have Intel based Mac hardware available to test this. Could you please help us debugging?

The relevant code is located here: https://github.com/zyantific/zydis/blob/03b7723770e0006a78b1b77f5c9b10d7af0237c9/include/Zydis/ShortString.h#L47-L73

As you can see, we are already using a guard to disable packing for AARCH64 on APPLE:

#if !(defined(ZYAN_AARCH64) && defined(ZYAN_APPLE))
#   pragma pack(push, 1)
#endif

Are these macros defined for you when you are building the library?

This is how we define these macros:

athre0z commented 10 months ago

I think we just have to get rid of the ARM64 portion of the check and simply do:

#if !defined(ZYAN_APPLE)

I originally assumed that this was only an issue on aarch64 targets where Apple does pointer authentication, but it looks like the toolchain also complains about it on Intel.

JamesAldridge commented 10 months ago

Only ZYAN_APPLE and ZYAN_X64 are defined not ZYAN_AARCH64 when i build

JamesAldridge commented 10 months ago

I think we just have to get rid of the ARM64 portion of the check and simply do:

#if !defined(ZYAN_APPLE)

I originally assumed that this was only an issue on aarch64 targets where Apple does pointer authentication, but it looks like the toolchain also complains about it on Intel.

That works, just tested