Creating a shared library from a module

I'd like to create a shared library out of a module in memory. Thus, I have a working module and would like to produce a library similar to what

$(CC) $(CFLAGS) -shared -fPIC

would produce, but I'd like to use the LLVM library (not the tools!). The documentation is very scarce about this.

Where in the code (probably clang?!) is a good starting point to look? 'Shared' and 'position-independent' would be important.

Thanks,
Frank

Hi Frank,

I'd like to create a shared library out of a module in memory. Thus, I have
a working module and would like to produce a library similar to what

That's not something LLVM can do on its own. You need a linker of some
kind to create a shared library (to create appropriate GOT entries,
resolve relocations, find dependent libraries, ...).

LLVM includes a component called MCJIT which does some of this work,
sort of, but it's in no way designed to output another object file at
the end of the process, which would be needed. It's designed to fixup
an object file in mostly ad-hoc ways so that it can be executed
directly in memory.

Other than that, you might have success linking against lld as a
library in addition to base LLVM. The documentation there is likely to
be even more sparse though (lld is reasonably new and embedding
linkers is less popular than embedding compilers).

Where in the code (probably clang?!) is a good starting point to look?
'Shared' and 'position-independent' would be important.

Certainly you'd have to setup your target to compile PIC.

Cheers.

Tim.

Hi Tim,

I am already using MCJIT. However, I am now confronted with architectures
where it's not straight-forward to cross compile LLVM to (and use it's JIT'er).
Thus, I am exploring the detour over a shared lib.

Assuming that clang cooperates with the linker would it be sensible to
track what path is taken when specifying -shared -fPIC? This should give
me hints what is needed.?

Frank

Assuming that clang cooperates with the linker would it be sensible to
track what path is taken when specifying -shared -fPIC? This should give
me hints what is needed.?

What Clang does is gets LLVM to output a temporary .o file and then
constructs a command-line to call the linker as a separate process. If
that's a good enough solution for you then passing clang an extra "-v"
option will get it to show you exactly how it invokes the linker and
you can copy the key bits into your own program.

Cheers.

Tim.