Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 351
lurscher Avatar asked Apr 13 '13 07:04

lurscher


People also ask

What is LLi LLVM?

DESCRIPTION. lli directly executes programs in LLVM bitcode format. It takes a program in LLVM bitcode format and executes it using a just-in-time compiler or an interpreter. lli is not an emulator. It will not execute IR of different architectures and it can only interpret (or JIT-compile) for the host architecture.

What is LLVM IR Bitcode?

What is commonly known as the LLVM bitcode file format (also, sometimes anachronistically known as bytecode) is actually two things: a bitstream container format and an encoding of LLVM IR into the container format. The bitstream format is an abstract encoding of structured data, very similar to XML in some ways.

Does LLVM compile to machine code?

LLVM is a language-agnostic compiler toolchain that handles program optimization and code generation. It is based on its own internal representation, called LLVM IR, which is then transformed into machine code.

How big is LLVM?

An LLVM-only build will need about 1-3 GB of space. A full build of LLVM and Clang will need around 15-20 GB of disk space. The exact space requirements will vary by system.


1 Answers

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.

like image 175
Anton Korobeynikov Avatar answered Oct 12 '22 22:10

Anton Korobeynikov