zig icon indicating copy to clipboard operation
zig copied to clipboard

Linking fails when building Zig: LLD dependencies are missing

Open kamidev opened this issue 3 years ago • 30 comments

Zig Version

b88151 (build fails)

Steps to Reproduce

Apple M1 Max, macOS Version 12.4 (Build 21F79). llvm 14.0.6 and zlib installed with homebrew.

Using build instructions for M1 Mac from wiki:

➜  rm -rf build; mkdir build; rm -rf ~/.cache/zig; rm -rf zig-cache; git pull
Already up to date.
➜  cd build; cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DZIG_PREFER_CLANG_CPP_DYLIB=true; make install

Expected Behavior

Successful build of stage1 binary

Actual Behavior

Build fails

[ 99%] Linking CXX executable zig0
Undefined symbols for architecture arm64:
  "_adler32", referenced from:
      void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda'(unsigned long)::operator()(unsigned long) const in liblldELF.a(OutputSections.cpp.o)
      void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda'(unsigned long)::operator()(unsigned long) const in liblldELF.a(OutputSections.cpp.o)
      void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda'(unsigned long)::operator()(unsigned long) const in liblldELF.a(OutputSections.cpp.o)
      void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda'(unsigned long)::operator()(unsigned long) const in liblldELF.a(OutputSections.cpp.o)
  "_adler32_combine", referenced from:
      void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, false> >() in liblldELF.a(OutputSections.cpp.o)
      void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, false> >() in liblldELF.a(OutputSections.cpp.o)
      void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)1, true> >() in liblldELF.a(OutputSections.cpp.o)
      void lld::elf::OutputSection::maybeCompress<llvm::object::ELFType<(llvm::support::endianness)0, true> >() in liblldELF.a(OutputSections.cpp.o)
  "_deflate", referenced from:
      deflateShard(llvm::ArrayRef<unsigned char>, int, int) in liblldELF.a(OutputSections.cpp.o)
  "_deflateEnd", referenced from:
      deflateShard(llvm::ArrayRef<unsigned char>, int, int) in liblldELF.a(OutputSections.cpp.o)
  "_deflateInit2_", referenced from:
      deflateShard(llvm::ArrayRef<unsigned char>, int, int) in liblldELF.a(OutputSections.cpp.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [zig0] Error 1
make[1]: *** [CMakeFiles/zig0.dir/all] Error 2
make: *** [all] Error 2

kamidev avatar Jul 10 '22 15:07 kamidev

Do you have zlib installed and does -DZIG_STATIC_ZLIB=on make any difference?

Vexu avatar Jul 10 '22 16:07 Vexu

Zlib was installed. But it seems that -DZIG_STATIC_ZLIB=on is needed to build sucessfully. Thanks a lot!

kamidev avatar Jul 10 '22 16:07 kamidev

Stage 2 and 3 also build without problems.

Behavior tests pass for stage1 and stage2 and panics for stage3. This happened before the llvm14 upgrade, too.

kamidev avatar Jul 10 '22 16:07 kamidev

I just updated the wiki page for build issues with what currently works for M1 Mac. As @topolarity just wrote on Discord, adding -DZIG_STATIC_ZLIB=ON is just a workaround until llvm-config reports dependencies correctly. But it's still good to know about.

kamidev avatar Jul 10 '22 16:07 kamidev

This also applies to Arch Linux.

Vexu avatar Jul 10 '22 17:07 Vexu

Yes, it's apparently a more general problem. I can't test on Arch Linux right now. But I just checked and the '-DZIG_STATIC_ZLIB=on' workaround is useful for an Intel Mac, too.

On my Intel Mac, I also had to install lxml2 (with homebrew) before it worked. That may be specific to my environment, though.

kamidev avatar Jul 10 '22 17:07 kamidev

Also had this problem on NixOS:

[ 99%] Linking CXX executable zig0
/nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: /nix/store/17il9zx2zkqvmxjkrdszi1m8hvvama6l-lld-14.0.1-lib/lib/liblldELF.a(OutputSections.cpp.o): undefined reference to symbol 'deflate'
/nix/store/rq6bh3qfrqnyqwik0w3q6w180zg3w2pa-binutils-2.38/bin/ld: /nix/store/q7k32ydcqlram7f0l6b1y2c4cs07765y-zlib-1.2.12/lib/libz.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/zig0.dir/build.make:137: zig0] Error 1
make[1]: *** [CMakeFiles/Makefile2:202: CMakeFiles/zig0.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

The -DZIG_STATIC_ZLIB=on fix did work though.

desttinghim avatar Jul 10 '22 17:07 desttinghim

Do we have an upstream bug report to link to? I think this is LLVM not reporting the libraries in llvm-config. This like the third time this same thing has regressed in the exact same way

andrewrk avatar Jul 10 '22 23:07 andrewrk

I couldn't find an open LLVM issue with that problem. But maybe I didn't search for the right thing. @andrewrk, can you remember some of the old issues for this? If this has happened before, it might be good to start there.

kamidev avatar Jul 11 '22 07:07 kamidev

@kamidev

I can't find the very exact open issue, only related ones

    1. https://github.com/llvm/llvm-project/issues/49538
    1. ~~https://github.com/llvm/llvm-project/issues/47189 (not sure if thats the reason)~~
    1. https://reviews.llvm.org/D116492 deprecation of llvm-config for lld

It's likely only getting worse without standalone builds and tests of the build configuration due to higher turnaround times, no artefact caching etc etc. https://discourse.llvm.org/t/standalone-build-for-clang-lld-fix-or-remove/59977/9

matu3ba avatar Jul 12 '22 09:07 matu3ba

@kamidev

I can't find the very exact open issue, only related ones

Thank you @matu3ba! Yes, those sort of stick out among recent llvm-config problems. And missing standalone builds doesn't seem good.

If we had this problem with LLVM releases before, it would be useful to find out how it was fixed then. I tried searching closed issues in the LLVM tracker, but it's hard to narrow down there without knowing more.

kamidev avatar Jul 12 '22 11:07 kamidev

Good news! The PR from @topolarity (https://github.com/ziglang/zig/pull/12136) that merged yesterday apparently fixed the linker issue. Which means the zlib workaround is no longer necessary.

Installing from source on ARM Macs is simple again:

> rm -rf build; mkdir build; rm -rf ~/.cache/zig; rm -rf zig-cache; git pull
> cd build; cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm); make install

I'll leave this issue open until people who had the same problem on other platforms have tested.

kamidev avatar Jul 27 '22 13:07 kamidev

Update. Some good news and some bad news.

Good: recent versions of zig (> 0.10.0-dev.3383) provide an easy way to do static builds. The same way works for both ARM and Intel Macs.

> cd build; cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DZIG_STATIC_LLVM=on; make install

Also good: 'zig version' will now display an error message if the build has an incorrect and dangerous mix of static and dynamic linking.

Bad: building with dynamic linking doesn't work anymore. See session below

kamidev avatar Jul 30 '22 10:07 kamidev

➜  zig git:(master) ✗ cd build; cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DZIG_PREFER_CLANG_CPP_DYLIB=true; make install
-- The C compiler identification is AppleClang 13.1.6.13160021
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring zig version 0.10.0-dev.3383+3cf8f283d
-- Found llvm: -lLLVM-14;-lm;-lz;-lcurses;-lxml2 (Required is at least version "14")
-- Found clang: /opt/homebrew/opt/llvm/lib/libclang-cpp.dylib (Required is at least version "14")
-- Found lld: /opt/homebrew/opt/llvm/lib/liblldMinGW.a;/opt/homebrew/opt/llvm/lib/liblldELF.a;/opt/homebrew/opt/llvm/lib/liblldCOFF.a;/opt/homebrew/opt/llvm/lib/liblldWasm.a;/opt/homebrew/opt/llvm/lib/liblldMachO.a;/opt/homebrew/opt/llvm/lib/liblldCommon.a (Required is at least version "14")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    ZIG_PREFER_CLANG_CPP_DYLIB

... After this, no errors or warnings appear and a new binary is installed.

kamidev avatar Jul 30 '22 10:07 kamidev

But this binary is bad and running it does occasionally produce weird errors. Fortunatately, there is an easy explanation for that.

➜  build git:(master) ✗ /Users/jonas/src/zig/zig/build/stage1/bin/zig version
0.10.0-dev.3383+3cf8f283d
error: Zig was built/linked incorrectly: LLVM and Clang have separate copies of libc++
       If you are dynamically linking LLVM, make sure you dynamically link libc++ too

kamidev avatar Jul 30 '22 10:07 kamidev

Thanks for the detailed report!

Dynamic linking on Mac is known to be broken due to https://github.com/ziglang/zig/issues/12147 - That issue will be updated once a fix lands soon (ish)

BTW, there are some limitations to the fix in 12136. Andrew plans to work with upstream to resolve this problem with LLD eventually, so I think this issue should stay open until then. Perhaps a quick rename?

topolarity avatar Jul 30 '22 14:07 topolarity

Thanks for the update! I have followed your conversation with Andrew, but wanted to make sure this issue reflected the current status.

kamidev avatar Jul 30 '22 14:07 kamidev

Perhaps we should also update the wiki?

kamidev avatar Jul 30 '22 14:07 kamidev

Linking fails for me when trying to build stage2 on M1.

$ ./stage1/bin/zig build -p stage2 -Denable-llvm
MachO Flush... warning(link): library not found for '-lz'
warning(link): library not found for '-lcurses'
warning(link): library not found for '-lxml2'
warning(link): Library search paths:
warning(link):   /opt/homebrew/opt/llvm/lib
warning(link):   /usr/lib

If I pass -DZIG_STATIC_ZLIB=on when building stage1, then stage2 builds fine.

yamashitax avatar Aug 07 '22 00:08 yamashitax

I can still build stage 1, 2 and 3 without doing that. What commit of zig master are you building? Which version of MacOS? Are you using brew-installed LLVM and Clang?

➜  build git:(master) ✗ zig version; sw_vers; clang --version
0.10.0-dev.3475+b3d463c9e
ProductName:	macOS
ProductVersion:	12.5
BuildVersion:	21G72
Homebrew clang version 14.0.6
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

kamidev avatar Aug 07 '22 04:08 kamidev

Looks like we have the same.

➜ build/stage1/bin/zig version; sw_vers; clang --version 
0.10.0-dev.3475+b3d463c9e
ProductName:	macOS
ProductVersion:	12.5
BuildVersion:	21G72
Homebrew clang version 14.0.6
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

yamashitax avatar Aug 07 '22 04:08 yamashitax

Interesting! Do you get any warnings at all for your succesful stage2 build? I don't.

Do you have 'export PATH="/usr/local/bin:$PATH"' in your profile? I do. I also had 'export PATH=/opt/homebrew/opt/llvm/bin:$PATH'. But no longer need that to build stage2, I just tested.

kamidev avatar Aug 07 '22 05:08 kamidev

Did you have to install any libraries before '-DZIG_STATIC_ZLIB=on' made your build work? Or was everything you needed already installed?

kamidev avatar Aug 07 '22 05:08 kamidev

I hava a similar problem on mac os Ventura. I try to build master.

I use this command

cmake .. -DCMAKE_PREFIX_PATH=/Users/iddar/.local/llvm -DZIG_STATIC_ZLIB=on

Only complete a zig1 and zig2 steps but not stage3, I see similar error if use the pre-build version

[100%] Built target zig2
[100%] Building stage3
MachO Flush... error(link): undefined reference to symbol 'dyld_stub_binder'
thread 904705 panic: attempt to unwrap error: UndefinedSymbolReference

version

> sw_vers; clang --version ; llc --version
LLVM (http://llvm.org/):
  LLVM version 15.0.0
  Optimized build.
  Default target: arm64-apple-darwin22.1.0
  Host CPU: apple-m1

ProductName:            macOS
ProductVersion:         13.0
BuildVersion:           22A5342f

clang version 15.0.0
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Users/iddar/.local/llvm/bin

iddar avatar Sep 17 '22 07:09 iddar

I managed to build on Montery using a local llvm15 (downloaded here). Haven't tried Ventura yet.

➜  zig git:(master) sw_vers; clang --version ; llc --version
ProductName:	macOS
ProductVersion:	12.6
BuildVersion:	21G115
clang version 15.0.1 ([email protected]:git/home-dim.git 285d797c78176dffdea707b28a6dafedf5803ce5)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Users/jonas/src/clang+llvm-15.0.1-arm64-apple-darwin21.0/bin
LLVM (http://llvm.org/):
  LLVM version 15.0.1
  Optimized build.
  Default target: arm64-apple-darwin21.6.0
  Host CPU: apple-m1

kamidev avatar Sep 21 '22 15:09 kamidev

On ARM Macs the build might stop with Apple security warnings. This is caused by Apple quarantine xattributes in the downloaded source folder. You can check if they are there with 'ls -l@'. If so, remove them like this:

➜  clang+llvm-15.0.1-arm64-apple-darwin21.0 xattr -d com.apple.quarantine -s -r bin
➜  clang+llvm-15.0.1-arm64-apple-darwin21.0 xattr -d com.apple.quarantine -s -r lib

Now the build should work without warnings.

➜  zig git:(master) rm -rf build; mkdir build; rm -rf ~/.cache/zig; rm -rf zig-cache
➜  zig git:(master) cd build; cmake .. -DCMAKE_PREFIX_PATH=$HOME/src/clang+llvm-15.0.1-arm64-apple-darwin21.0 -DZIG_STATIC_LLVM=on; make install
...
[ 97%] Linking CXX static library zigcpp/libzigstage1.a
[ 97%] Built target zigstage1
[ 98%] Building CXX object CMakeFiles/zig1.dir/src/stage1/zig0.cpp.o
[ 98%] Linking CXX executable zig1
[ 98%] Built target zig1
[ 99%] Building stage2 object /Users/jonas/src/zig/zig/build/zig2.o
[ 99%] Building CXX object CMakeFiles/zig2.dir/src/stage1/empty.cpp.o
[100%] Linking CXX executable zig2
[100%] Built target zig2
[100%] Building stage3
[100%] Built target stage3
Install the project...
-- Install configuration: "Debug"
-- Up-to-date: /Users/jonas/src/zig/zig/build/stage3/lib/zig/compiler_rt.zig

kamidev avatar Sep 21 '22 15:09 kamidev

I hava a similar problem on mac os Ventura. I try to build master.

I use this command

cmake .. -DCMAKE_PREFIX_PATH=/Users/iddar/.local/llvm -DZIG_STATIC_ZLIB=on

Only complete a zig1 and zig2 steps but not stage3, I see similar error if use the pre-build version

[100%] Built target zig2
[100%] Building stage3
MachO Flush... error(link): undefined reference to symbol 'dyld_stub_binder'
thread 904705 panic: attempt to unwrap error: UndefinedSymbolReference

version

> sw_vers; clang --version ; llc --version
LLVM (http://llvm.org/):
  LLVM version 15.0.0
  Optimized build.
  Default target: arm64-apple-darwin22.1.0
  Host CPU: apple-m1

ProductName:            macOS
ProductVersion:         13.0
BuildVersion:           22A5342f

clang version 15.0.0
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Users/iddar/.local/llvm/bin

same here

tetotechy avatar Sep 25 '22 18:09 tetotechy

@kamidev I'm not sure to understand, where are the lib and bin directories you refer to ?

➜  clang+llvm-15.0.1-arm64-apple-darwin21.0 xattr -d com.apple.quarantine -s -r bin
➜  clang+llvm-15.0.1-arm64-apple-darwin21.0 xattr -d com.apple.quarantine -s -r lib

gaetschwartz avatar Oct 12 '22 07:10 gaetschwartz

@kamidev I'm not sure to understand, where are the lib and bin directories you refer to ?

➜  clang+llvm-15.0.1-arm64-apple-darwin21.0 xattr -d com.apple.quarantine -s -r bin
➜  clang+llvm-15.0.1-arm64-apple-darwin21.0 xattr -d com.apple.quarantine -s -r lib

Hi @gaetschwartz! Those directories were in a local build of clang+llvm (from https://github.com/llvm/llvm-project/releases). I am currently back to using homebrew llvm (version 15.0.2) which doesn't have such problems. In general, "fixing" Apple-specific quarantine attributes should be avoided. But for development it can be good to know about this workaround.

kamidev avatar Oct 16 '22 07:10 kamidev