11

I have a C++ project that uses a C++ library that I also wrote. I'm using clang++ 3.3 to build everything. Each file in the library is compiled as

clang++ -c -O -emit-llvm somefile.cpp -o somefile.bc

I'm then using llvm-link to combine all the library *.bc files into a single bit code file like so

llvm-link -o MyLibrary.bc somefile.bc someotherfile.bc etc.bc

I'm conceptualizing this to be similar to creating an archive of object files, but I don't think that's true based on how things are acting.

I then compile the source files of my project using a similar command to the one above. I then use llvm-link (again) to combine these, along with the library bit code file into a single bit code file like this

llvm-link -o app.bc1 main.bc x.bc y.bc path/to/MyLibrary.bc

Next I compile app.bc1 into a native object file

llc -filetype=obj app.bc1 -o app.o

Finally I use clang++ again to link this native object file (and against the other native libraries I need, such as the C++ standard library, etc)

clang++ app.o -o app

However, what appears to be happening is that when I llvm-link the application's bit code the entire contents of MyLibrary.bc seems to be included in the result. Thus the final linking needs to resolve references made by library components that I'm not actually using.

What I would like to do is extract from MyLibrary.bc only the bit code files that my application needs. I see there is an llvm-ar program but in reading about it I don't get the impression that it would help here. I guessed I could combine the library with llvm-ar instead of llvm-link, but I can't figure it out. I'm hoping all I need is a little push :)

7
  • Your link step doesn't include any indication of optimization or dead-stripping that should be performed. Did you try adding a flag to do that? Commented Jul 3, 2013 at 23:39
  • You mean a flag on llvm-link? The documentation here llvm.org/docs/CommandGuide/llvm-link.html doesn't talk about any such flag. I did try using opt with the -dce and -adce flags but there was no effect (on my issue). Commented Jul 4, 2013 at 12:09
  • 1
    No, on the final link. Dead stripping a library doesn't really make sense. Commented Jul 4, 2013 at 13:44
  • Okay, there are three link steps but I understand you to mean the last one where I use the clang++ driver. I looked over the clang and ld manual pages but I'm not seeing any option for dead code elimination or similar. Do you happen to know which flag I should be using? Commented Jul 4, 2013 at 15:14
  • What system/target environment/linker version? Commented Jul 4, 2013 at 17:04

1 Answer 1

4

EDIT: It is actually ar which makes it work.

Bit late but still might be relevant to someone, we use ar and ld.gold with LLVM plugin to link bitcode:

ar r --plugin /usr/lib64/llvm/LLVMgold.so library.a <files...>
ld.gold --plugin /usr/lib64/llvm/LLVMgold.so -plugin-opt emit-llvm  main.bc library.a

Of course the path to LLVMgold.so might be different. This way resulting .bc has just needed symbols.

Sign up to request clarification or add additional context in comments.

2 Comments

Vladimir, could a plugin load of this nature be used in a process such as AOP to instrument the bytecode?
You could do bitcode instrumentation using LLVM's opt, I believe it also has support for plugins (custom passes over LLVM bitcode in this case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.