Clang failed to invoke lld correctly

Hello,

I have a question about the invocation of lld by using the flag “-fuse-ld=lld”.

First, I prepare two very simple c files, main.c and test.c, and use the below command to compile them, everything is OK, main.o and test.o are generated:

clang --target=aarch64 -c main.c
clang --target=aarch64 -c test.c

But when I use the command clang --target=aarch64 -fuse-ld=lld -v main.o test.o to link them, it fails and according to the below log it shows that it actually invoke “/usr/bin/gcc” -fuse-ld=lld instead of /usr/bin/clang -fuse-ld=lld

root@6ab3fca322b6:/home/example# clang --target=aarch64 -fuse-ld=lld -v main.o test.o
clang version 13.0.0
Target: aarch64
Thread model: posix
InstalledDir: /usr/local/bin
**"/usr/bin/gcc" -fuse-ld=lld** -o a.out main.o test.o
ld.lld: error: main.o is incompatible with elf64-x86-64
ld.lld: error: test.o is incompatible with elf64-x86-64
collect2: error: ld returned 1 exit status
clang-13: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

But if I manually invoke ld.lld, there is no problem just a warning about “cannot find entry symbol _start;”.
ld.lld main.o test.o

Also I try to remove the flag --target=aarch64, it still failed: root@6ab3fca322b6:/home/example# clang -v main.o test.o

clang version 13.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
"/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/usr/local/bin/../lib -L/lib -L/usr/lib main.o test.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o /lib/x86_64-linux-gnu/crtn.o
ld: error: main.o is incompatible with elf64-x86-64
ld: error: test.o is incompatible with elf64-x86-64
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)

I have soft linked the /usr/bin/ld to /usr/bin/ld.lld, but I don’t know why it adds some many flags which I don’t specify, e.g., -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2

So my questions are:

  1. why clang --target=aarch64 -fuse-ld=lld can’t invoke ld.lld?
  2. why clang invoke /usr/bin/ld but add some many unspecified flags?
  3. For aarch64, which is the correct way to make clang correctly invoke ld.lld?

I am using clang and lld 13.0.0. Thanks!

Best Regards,
Simon

Hi,

This is because aarch64 is not a valid target, and when clang get’s passed a target it doesn’t recognized it will “fallback” to gcc. If you are targeting a aarch64 linux build the target you should use is: aarch64-unknown-linux-gnu.

Hello,

Thanks for your help, I believe it’s the root cause, but I tried with aarch64-unknown-linux-gnu and it still report error, do I need to try other Target Triple:

simon@LAPTOP-JH2M71LG:/mnt/c/tmp/temp$ clang --target=aarch64-unknown-linux-gnu -c main.c
simon@LAPTOP-JH2M71LG:/mnt/c/tmp/temp$ clang --target=aarch64-unknown-linux-gnu -c test.c
simon@LAPTOP-JH2M71LG:/mnt/c/tmp/temp$ clang --target=aarch64-unknown-linux-gnu -fuse-ld=lld -v main.o test.o
Ubuntu clang version 11.0.0-2~ubuntu20.04.1
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-11/bin
Found candidate GCC installation: /usr/lib/gcc-cross/aarch64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc-cross/aarch64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
clang: error: invalid linker name in argument '-fuse-ld=lld'

Best Regards,
simon

Do you have ld.lld in your path?

Hello,

Yes, I can directly run ld.lld in the shell.

simon@LAPTOP-JH2M71LG:/mnt/c/tmp/temp$ clang --target=aarch64-unknow-linux-gn -c main.c
simon@LAPTOP-JH2M71LG:/mnt/c/tmp/temp$ clang --target=aarch64-unknow-linux-gn -c test.c
simon@LAPTOP-JH2M71LG:/mnt/c/tmp/temp$ clang --target=aarch64-unknown-linux-gnu -fuse-ld=lld -v main.o test.o
Ubuntu clang version 11.0.0-2~ubuntu20.04.1
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-11/bin
Found candidate GCC installation: /usr/lib/gcc-cross/aarch64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc-cross/aarch64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/usr/bin/ld.lld" -EL -z relro --hash-style=gnu --build-id --eh-frame-hdr -m aarch64linux -dynamic-linker /lib/ld-linux-aarch64.so.1 -o a.out /usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/lib/crt1.o /usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/lib/crti.o /usr/lib/gcc-cross/aarch64-linux-gnu/9/crtbegin.o -L/usr/lib/gcc-cross/aarch64-linux-gnu/9 -L/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../aarch64-linux-gnu -L/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../lib64 -L/lib/aarch64-linux-gnu -L/lib/../lib64 -L/usr/lib/aarch64-linux-gnu -L/usr/lib/../lib64 -L/usr/lib/aarch64-linux-gnu/../../lib64 -L/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/lib -L/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../.. -L/usr/lib/llvm-11/bin/../lib -L/lib -L/usr/lib main.o test.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc-cross/aarch64-linux-gnu/9/crtend.o /usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/lib/crtn.o
**simon@LAPTOP-JH2M71LG:/mnt/c/tmp/temp$ ld.lld**
ld.lld: error: no input files

Thanks,
Simon

Seems like it’s working now looking at that first log entry?

Hello,

Yes, it’s working, sorry that I didn’t notice it, thanks a lot!

Best Regards,
Simon