[LLVMdev] Question about usage of LLVMLinkModules()

Duncan P. N. Exon Smith dexonsmith at apple.com
Thu Jun 11 09:41:47 PDT 2015


> On 2015-Jun-08, at 07:11, Lefteris Karapetsas <lefteris at refu.co> wrote:
> 
> Hello all,
> 
> I am using llvm as a backend to a hobby language I am making.  I have experimented with the C bindings since the project is written in C. I had no problems building a single lllvm module so far. I am not using a JIT compiler but at least at the moment am simply generating executables. The workflow I am using is to build a module with LLVMModuleCreateWithName() and populate it with the LLVMBuilder.
> 
> After everything is in the module I simply output it into a file with LLVMPrintModuleToFile(). Then I am calling llc to convert the IR  to native machine code and finally gcc to create an executable out of it. All these from inside my program. It may not be the best way to generate an executable and I would really welcome as much advice as you can give me on that but it works.
> 
> The issue comes when I actually create 2 different modules. One being a "standard" library of sorts and the other the main module which should use it. I create both modules, with no errors appearing if I use LLVMVerifyModule() on them.
> 
> The problem is how do I link them? I tried to do it like this:
> 
>             // if an error occurs LLVMLinkModules() returns true
>             if (true == LLVMLinkModules(main_module, stdlib_module, LLVMLinkerDestroySource, &error)) {
>                 bllvm_error("Could not link LLVM modules", error);
>                 goto end;
>             }
> 
> 
> But this fails (returns true), with no error. Even though both modules are valid. I understand that the above may be a really naive mistake, and that my whole approach may be wrong. This is why I would appreciate any links to tutorials/documents which would show the proper way to link 2 modules or some good old simple advice tips from any of you guys.

Weird that you're not getting an error message.

Best thing is probably to spin up a debugger (or add some printfs)
and see what's going on.

One hypothetical point of failure: module linking requires that both
modules are in the same context.  If that's not the problem (and it
probably isn't, since I'd expect crashes there), my bet would be on
some sort of linkage conflict between the modules.



More information about the llvm-dev mailing list