Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

clang passes meaningless flags to lld when targeting MacOS #33764

Open Quuxplusone opened 7 years ago

Quuxplusone commented 7 years ago
Bugzilla Link PR34792
Status NEW
Importance P enhancement
Reported by Richard Smith (richard-llvm@metafoo.co.uk)
Reported on 2017-09-30 23:44:20 -0700
Last modified on 2017-10-20 19:19:14 -0700
Version 5.0
Hardware PC All
CC llvm-bugs@lists.llvm.org, ngolovliov@gmail.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
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.
Quuxplusone commented 7 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] <inputs>

BUNDLE EXECUTABLE OPTIONS:
  -bundle_loader <path> 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.

(In reply to Richard Smith from comment #0)
> 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.