Bug#767323: LTO not working: File format not recognized
Benjamin Kay
benjamin at benkay.net
Thu Dec 25 21:42:58 UTC 2014
TL;DR The problem appears to be related to binutils and not clang.
I am having the same problem as the submitter with Clang 3.5.0-9. Compiling the following simple hello world program:
#include <iostream>
int main()
{
std::cout << "Hello World\n";
return 0;
}
$ clang++ -flto -c hello.cpp
$ clang++ -v -flto hello.o -o hello
Debian clang version 3.5.0-9 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/i586-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/i586-linux-gnu/4.8.4
Found candidate GCC installation: /usr/bin/../lib/gcc/i586-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/i586-linux-gnu/4.9.2
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7.4
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8.4
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/i586-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/i586-linux-gnu/4.8.4
Found candidate GCC installation: /usr/lib/gcc/i586-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/i586-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Candidate multilib: .;@m64
Selected multilib: .;@m64
"/usr/bin/ld" --hash-style=both --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../.. -L/usr/lib/llvm-3.5/bin/../lib -L/lib -L/usr/lib -plugin /usr/lib/llvm-3.5/bin/../lib/LLVMgold.so -plugin-opt=mcpu=x86-64 hello.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crtn.o
hello.o: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It appears that the default linker is not the gold linker.
$ ls -l /usr/bin/ld*
lrwxrwxrwx 1 root root 6 Dec 25 16:33 /usr/bin/ld -> ld.bfd
-rwxr-xr-x 1 root root 1076192 Dec 19 13:30 /usr/bin/ld.bfd
-rwxr-xr-x 1 root root 5388 Nov 6 15:12 /usr/bin/ldd
-rwxr-xr-x 1 root root 2642264 Dec 19 13:30 /usr/bin/ld.gold
The following workaround fixed the problem for me.
$ sudo rm /usr/bin/ld
$ sudo ln -s ld.gold /usr/bin/ld
$ clang++ -flto hello.o -o hello
$ ./hello
Hello World
Perhaps this bug should be reassigned to binutils.
$ dpkg -S /usr/bin/ld
binutils: /usr/bin/ld
$ dpkg -S /usr/bin/ld.gold
binutils: /usr/bin/ld.gold
More information about the Pkg-llvm-team
mailing list