clang failed to invoke lld correctly

1.4k Views Asked by At

I prepare two very simple c files, main.c and test.c, and use the below command to compile them:
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, it actually invoke "/usr/bin/gcc" -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, everything 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 can 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.

Best Regards,
Simon

0

There are 0 best solutions below