Creating and using LLVM bitcode libraries

試著忘記壹切 提交于 2019-12-05 17:36:10

问题


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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/17459800/creating-and-using-llvm-bitcode-libraries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!