ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.95k stars 2.55k forks source link

Build failed on macOS Sonoma with Xcode 15: zig2 duplicate symbol `__mh_execute_header` #17050

Closed thelastlin closed 1 year ago

thelastlin commented 1 year ago

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

  1. Install required dependencies.
  2. Configure and build with:
    export SDKROOT=$(xcrun --sdk macosx --show-sdk-path); cd builld;
    cmake .. -DCMAKE_PREFIX_PATH=$PATH_TO_LLVM16_INSTALL_PATH
    make -j`nproc`

    which SDKROOT will be set to /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk

  3. Failed at Linking CXX executable zig2, linker output:
    @(#)PROGRAM:ld  PROJECT:dyld-1015.7
    BUILD 19:43:18 Aug 22 2023
    configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
    will use ld-classic for: armv6 armv7 armv7s arm64_32 i386 armv6m armv7k armv7m armv7em
    LTO support using: LLVM version 15.0.0 (static support for 29, runtime is 29)
    TAPI support using: Apple TAPI version 15.0.0 (tapi-1500.0.12.3)
    Library search paths:
        ... 
    Framework search paths:
    ld: warning: ignoring duplicate libraries: '/path/to/llvm-16/lib/libclangAST.a', '/path/to/llvm-16/lib/libclangASTMatchers.a', '/path/to/llvm-16/lib/libclangAnalysis.a', '/path/to/llvm-16/lib/libclangParse.a', '/path/to/llvm-16/lib/libclangSema.a', '/path/to/llvm-16/lib/libclangStaticAnalyzerCheckers.a', '/path/to/llvm-16/lib/libclangStaticAnalyzerCore.a', '/path/to/llvm-16/lib/libclangStaticAnalyzerFrontend.a'
    duplicate symbol '__mh_execute_header' in:
        /path/to/build/CMakeFiles/zig2.dir/zig2.c.o
        boundary-file
    ld: 1 duplicate symbols

macOS: 23A5337a Xcode: 15A5229m LLVM(llvm-config --version): 16.0.6

Expected Behavior

  1. Succeed in linking zig2.
jedisct1 commented 1 year ago

As a quick workaround, edit zig2.c and remove the definition:

struct mach_header_64__3906 _mh_execute_header = {UINT32_C(0xaaaaaaaa), ... };
mikdusan commented 1 year ago

Further hint, this is definitely triggered when Xcode 15 (Release Candidate) linker is used, even on Ventura. It looks like the latest system linker is more strict.

mikdusan commented 1 year ago

another hint

but what's going on here? If we look as to why the symbol is used... it's used when .zig owns (exports) main. It's not needed when .c owns main. And this is further re-enforced by your workaround.

I'm thinking when backend is .c we have 2 choices:

  1. do not define _mh_execute_header
  2. define _mh_execute_header but it must be weak
jvo203 commented 1 year ago

This problem is happening on the latest macOS Ventura 13.6 too, M1 Ultra chip, even with the latest zig (after "git pull"). Can't compile / build zig.

jvo203 commented 1 year ago

Are you sure this has been fixed? Am still getting an error in Ventura 13.6 (this time on Intel macOS):

[ 94%] Linking CXX executable zig2
ld: warning: ignoring duplicate libraries: '/usr/local/opt/llvm/lib/libclangAST.a', '/usr/local/opt/llvm/lib/libclangASTMatchers.a', '/usr/local/opt/llvm/lib/libclangAnalysis.a', '/usr/local/opt/llvm/lib/libclangParse.a', '/usr/local/opt/llvm/lib/libclangSema.a', '/usr/local/opt/llvm/lib/libclangStaticAnalyzerCheckers.a', '/usr/local/opt/llvm/lib/libclangStaticAnalyzerCore.a', '/usr/local/opt/llvm/lib/libclangStaticAnalyzerFrontend.a'
duplicate symbol '__mh_execute_header' in:
    /Users/chris/projects/zig/build/CMakeFiles/zig2.dir/zig2.c.o
    boundary-file
ld: 1 duplicate symbols
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [zig2] Error 1
make[1]: *** [CMakeFiles/zig2.dir/all] Error 2
make: *** [all] Error 2
mikdusan commented 1 year ago

Are you sure this has been fixed? Am still getting an error in Ventura 13.6 (this time on Intel macOS):

sorry it's a 2-part fix. The first part has been committed. But we have a bootstrap process where stage1/zig1.wasm has to be updated in another commit and that has been delayed; this comment https://github.com/ziglang/zig/pull/17180#issuecomment-1732676362 indicates that when #17253 merges to master (it looks like very soon!) it will have the updated zig1.wasm and you should be able to build.

update: zig1.wasm landed in master 6bd54a1d3ebd8d997158c57057ba742824cf7e0c

jvo203 commented 1 year ago

I see!

wangzhizhou commented 1 year ago

Xcode 15‘s new linker cause this problem, you can add -ld_classic into OTHER_LDFLAGS reference: https://developer.apple.com/forums/thread/736590

jvo203 commented 1 year ago

Thanks, it's good to know it. Other, non-zig related projects have been affected too so it's great that at least there is a temporary workaround.