create position independent object file from LLVM bit code
Asked Answered
C

1

12

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
Chelsiechelsy answered 13/4, 2013 at 7:0 Comment(0)
A
19

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.

Ablution answered 14/4, 2013 at 8:30 Comment(4)
thanks!!!!, is there a tool to build the library directly from the IR bit code, without relying on the assembler?Chelsiechelsy
i've seen that llc support native code output, but it is experimental. Is there a production-quality approach for that?Chelsiechelsy
Hi. Would you tell where option "-relocation-model" is described? I can find it for "lli", but "lli" is not "llc".Silkweed
@Silkweed It is described in the output of llc --help. Sadly the manpages do only contain a mere fraction of llc's options. Took me 1 1/2 days to figure out llc had this flag.Viperine

© 2022 - 2024 — McMap. All rights reserved.