[LLVMdev] How to link code into EXE, or, am I doing this wrong?

Michael Spencer
Wed, 07 Nov 2012 18:46:20 -0000

On Wed, Nov 7, 2012 at 8:28 AM, John Slagel <john.slagel at gmail.com>
wrote:
> Hi,
>
> I've been writing a 80's era-flavor of QuickBASIC compiler based on
LLVM,
> and it's come surprisingly far very quickly, LLVM is working great.
GOSUB's,
> GOTO's, FIELD statements.
> I'm using Visual Studio 2010.
> Here's my setup: I use LLVM to turn my AST into code, which, during
> debugging I then execute natively using "getPointerToFunction"
and the
> runtime library it uses is a .lib in my VS2010 project that it calls into.
> Then, to produce an EXE, I use addPassesToEmitFile to produce an .OBJ file
> for the native machine (x86 windows), which I then link with my runtime
> library using the Visual Studio linker:
>
>   link.exe test.bas.obj debug\BPRuntime.lib kernel32.lib /subsystem:console
>
> Which produces a native executable that executes just fine under Windows.
>
> All of that works great.
>
> What I can't figure out, even after LOTS of searching, is how to
replace
> that with a LLVM linker or some other linker so that I can give out my
> compiler and people could use it to make EXE files.  I found similar posts
> and questions but no good answers, and it seems that some of this (an LLVM
> linker) is in flux.
>
> Or, am I going about this wrong?   Should I be generating bitcode and
making
> my runtime lib be bitcode and then link them using LLVM tools?
>
> Thanks!

llvm currently has no way to generate PECOFF executables except to
rely on the system linker. Your current options are:

* Include the MinGW ld in your distribution.
* Always use the llvm jit (with or without compiling your runtime to bitcode).
* Wait for http://lld.llvm.org/ to become feature complete.

- Michael Spencer