How to create shared library using llvm-ld

Hi, all. I am using llvm-gcc to compile some softwares, and compile them with CFLAGS=“-emit-llvm” to gernerate bitcode IR. Then I have to replace normal ld with llvm-ld. Unfortunately, llvm-ld doesn’t support -shared option which is used to create a shared library. I’ve also noticed that llvm-ld supports an option ‘-link-as-library’ which is used to generate static library file. It is helpless. Any ideas? Thanks a lot. Gauss, 2009-09-23

gauss wrote:

Hi, all. I am using llvm-gcc to compile some softwares, and compile them with CFLAGS="-emit-llvm" to gernerate bitcode IR. Then I have to replace normal */ld/* with */llvm-ld/*. Unfortunately, */llvm-ld/* doesn't support */-shared/* option which is used to create a shared library. I've also noticed that llvm-ld supports an option '-link-as-library' which is used to generate static library file. It is helpless. Any ideas? Thanks a lot. Gauss, 2009-09-23

Use the gold plugin: The LLVM gold plugin — LLVM 18.0.0git documentation . Then 'ld -shared foo.bc' will work just as if foo.bc were a .o file.

If you don't want to (or can't) use gold for some reason, you'll need to a) run llvm-ld -link-as-library to produce a merged .bc
b) run llc -relocation-model=pic to produce a .s file
c) assemble and link it into a .so file (gcc -shared foo.s will work fine, or you can run 'as' and 'ld' yourself, though figuring out the options is a pain.)

but this route may give you slightly less optimization than using gold directly, for the reasons explained in llvm.org/docs/LinkTimeOptimization.html .

Alternately, you can implement the -shared flag for llvm-ld yourself, which does the same operations as the above list but without the need to run the individual commands.

Nick