'create position independent object file from LLVM bit code

I have a llvm module that i've dumped as bitcode file with llvm::WriteBitcodeToFile. I want to turn this bitcode file into an native dynamically loadable library that contains the functions in the module.

How do i do this? i tried using llc for this, but this produces code that apparently is not relocatable, since after doing the following steps:

llc -enable-pie -cppgen=functions -filetype=asm executableModule -o em.s

then, assemblying with gnu as into an object file:

as -o mylib.o em.s

finally, trying to produce a shared library with:

gcc -shared -o libmyfile.so -fPIC mylib.o

fails with the error:

/usr/bin/ld: error: mylib.o: requires dynamic R_X86_64_PC32 reloc against 'X.foo' which may overflow at runtime; recompile with -fPIC
collect2: ld returned 1 exit status


Solution 1:[1]

You need to setup relocation model. Something like -llc -relocation-model=pic. Do not use PIE, because it's for executables, not for libraries. Also, -cppgen does not make any sense here, it's for cpp backend only.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anton Korobeynikov