llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.05k stars 11.58k forks source link

clang passes meaningless flags to lld when targeting MacOS #34140

Open ec04fc15-fa35-46f2-80e1-5d271f2ef708 opened 6 years ago

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 6 years ago
Bugzilla Link 34792
Version 5.0
OS All

Extended Description

Unpack the official Clang 5 .tar.xz from http://releases.llvm.org/download.html and try to use it with the bundled copy of LLD:

$ ~/clang+llvm-5.0.0-x86_64-apple-darwin/bin/clang++ numbers.cpp -fuse-ld=lld
/Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unknown argument: -no_deduplicate /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unknown argument: -dynamic /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unknown argument: -arch /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unknown emulation: acosx_version_min /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unable to find library -lto_library /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/lib/libLTO.dylib: invalid data encoding clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

Oops.

From clang -v, we see that Clang is passing Apple-linker-specific flags to ld.lld:

"/Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld" -demangle -lto_library /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out /var/folders/00/14vvr000h01000cxqpysvccm004lgg/T/numbers-0d20ee.o -lc++ -lSystem /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/lib/clang/5.0.0/lib/darwin/libclang_rt.osx.a

Either -fuse-ld=lld should not be supported or it should pass meaningful flags.

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 2 years ago

mentioned in issue llvm/llvm-bugzilla-archive#34793

llvmbot commented 6 years ago

I believe the actual problem is that lld for Mach-O is not getting installed (or invoked) correctly:

$ ld.lld --help ... ld.lld: supported targets: elf32-i386 elf32-iamcu elf32-littlearm elf32-ntradbigmips elf32-ntradlittlemips elf32-powerpc elf32-tradbigmips elf32-tradlittlemips elf32-x86-64 elf64-amdgpu elf64-littleaarch64 elf64-powerpc elf64-tradbigmips elf64-tradlittlemips elf64-x86-64

$ ld.lld -flavor darwin OVERVIEW: LLVM Linker

USAGE: ld.lld [options]

BUNDLE EXECUTABLE OPTIONS: -bundle_loader The executable that will be loading this Mach-O bundle ...

So actually `ld.lld -flavor darwin' is the way lld is expected to be called. However, clang does the following instead:

$ clang-5.0 -Xlinker -help -fuse-ld=lld t.c -v ... "/opt/llvm/bin/ld.lld" -demangle -lto_library /opt/llvm/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out -help .../t-4a97e7.o -lSystem /opt/llvm/lib/clang/5.0.0/lib/darwin/libclang_rt.osx.a ... /opt/llvm/bin/ld.lld: supported targets: elf32-i386 elf32-iamcu elf32-littlearm elf32-ntradbigmips elf32-ntradlittlemips elf32-powerpc elf32-tradbigmips elf32-tradlittlemips elf32-x86-64 elf64-amdgpu elf64-littleaarch64 elf64-powerpc elf64-tradbigmips elf64-tradlittlemips elf64-x86-64

In fact, most of the arguments passed to ld64 by the driver are also accepted by Mach-O lld. For example,

$ ld.lld -flavor darwin -demangle -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out t.o -lSystem /opt/llvm/lib/clang/5.0.0/lib/darwin/libclang_rt.osx.a -sdk_version 10.13

will succeed.

I guess this can be fixed by checking whether `-fuse-ld=lld' is set and the target is a Mach-O binary in driver code, so the correct flavour is passed to lld.

Unpack the official Clang 5 .tar.xz from http://releases.llvm.org/download.html and try to use it with the bundled copy of LLD:

$ ~/clang+llvm-5.0.0-x86_64-apple-darwin/bin/clang++ numbers.cpp -fuse-ld=lld
/Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unknown argument: -no_deduplicate /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unknown argument: -dynamic /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unknown argument: -arch /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unknown emulation: acosx_version_min /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: unable to find library -lto_library /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld: error: /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/lib/libLTO.dylib: invalid data encoding clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

Oops.

From clang -v, we see that Clang is passing Apple-linker-specific flags to ld.lld:

"/Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/bin/ld.lld" -demangle -lto_library /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out /var/folders/00/14vvr000h01000cxqpysvccm004lgg/T/numbers-0d20ee.o -lc++ -lSystem /Users/zygoloid/clang+llvm-5.0.0-x86_64-apple-darwin/lib/clang/5.0.0/lib/ darwin/libclang_rt.osx.a

Either -fuse-ld=lld should not be supported or it should pass meaningful flags.