Unable to perform LLVM LTO using clang

Hi everyone,

I want to perform whole-program analysis for apache httpd using httpd’s original autoconf system so that I need to generate a single bitcode file.

So I follow the instructions in LLVM document on gold plugin http://llvm.org/docs/GoldPlugin.html#lto-how-to-build. However, when running a simple program using LTO, clang fails to link the object file with the LLVM bitcode.
There’re only two c files, a.c and b.c in the source folder.

$ clang -flto a.c -c -o a.o      # <-- a.o is LLVM bitcode file
$ clang b.c -c -o b.o            # <-- b.o is native object file
$ clang -flto a.o b.o -o main    # <-- link with LLVMgold plugin

When executing the last command, it prompts

a.o: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)

At the same time, performing the command of
ld -plugin=/usr/local/lib/LLVMgold.so a.o b.o -o main
it will successfully generate main.

My libLTO build process is as follows,

  1. check out binutils
  2. configure with --enable-gold --enable-plugins
  3. make
  4. make install
  5. replace original ld in /usr/local/bin with ld.gold using ln
  6. replace ar and nm in /usr/local/bin with ar and nm-new in the binutils folder just built using ln
  7. configure LLVM with --with-binutils-include=/path/to/binutils/src/include
  8. execute make again in LLVM root
    It seems the gold linker works well with the LLVMgold.so lib however clang fails to correctly invoke gold linker.

Could anyone tell me where my mistake is?

Regards,
Chunbai Yang

Could anyone tell me where my mistake is?

Try running

clang -flto a.o b.o -o main -v

it should print how clang is running the linker. See if it is running
the linker you expect and if it is passing -plugin to it.

Cheers,
Rafael

Dear Rafael,

Thank you for your answer. The verbose mode -v shows that clang uses the old ld in /usr/bin while I install the gold in /usr/local/bin.

Regards,
Chunbai

Clang should be searching the path. What does

$ which ld

print? Maybe all that you have to do is

$ export PATH=/usr/local/bin:$PATH

Cheers,
Rafael

The problem is solved. Thank you for your time, Rafael.

Previously, my clang uses the ld in /usr/bin. However, I installed the gold plugin to /usr/local/bin. So that clang actually uses the wrong ld.
Now it’s fixed by configuring clang to the ld in /usr/local/bin.

Regards,
Chunbai