[LLVMdev] Question about usage of LLVMLinkModules()

Lefteris Karapetsas lefteris at refu.co
Thu Jun 11 12:30:58 PDT 2015


Hello Duncan,

I have only been experimenting with LLVM, never used the develop version
and I initially joined the mailing list hoping to get some help with my
particular issue. But it would be my pleasure to help root out this
potential bug. I have not tried with the latest llvm trunk but `llvm-link`
succesfully gives an error when trying to link the 2 modules together.

llvm-link llvm_module1.ll llvm_module2.ll
ERROR: Linking globals named 'gstr_706834940': symbol multiply defined!

It must be an error in the C api wrapper. I am not familiar enough with the
codebase to spot it fast though. As I said above the LLVM version is 3.6.1.

The C-wrapper of LLVMLinkModules seem to be using a DiagnosticInfo Handler
to handle errors. The error is omitted in the core of LinkModules.cpp but
for some reason does not appear outside when consumed by the C-API.


Lefteris(Eleftherios) Karapetsas  Click here <http://lefteris.refu.co> for
my CV/portfolio website
<http://lefteris.refu.co>   Also maintain a blog <http://blog.refu.co>on
various topics, mostly engineering


On Thu, Jun 11, 2015 at 8:11 PM, Duncan P. N. Exon Smith <
dexonsmith at apple.com> wrote:

>
> > On 2015-Jun-11, at 10:54, Lefteris Karapetsas <lefteris at refu.co> wrote:
> >
> > Hello Duncan,
> >
> > Thank you for your reply. I did figure it out in the end. It was a
> linkage conflict between the modules. There were some global types that I
> was declaring in both modules. After removing them, linking worked
> perfectly.
> >
> > Still it's a mystery why I got no error string pointing me to the reason
> the linking failed in the first place.
>
> Does it (the missing error message) reproduce on trunk?
>
> Does the error message get skipped just with the C API, or does
> something funny also happen for `llvm-link`?
>
> I'm glad you sorted out your problem :), but that missing error
> message sounds like a real bug that it would be nice to root out.
>
>
>
> >
> > Best Regards,
> >
> > Lefteris(Eleftherios) Karapetsas
> > Click here for my CV/portfolio website
> > Also maintain a blog on various topics, mostly engineering
> >
> >
> > On Thu, Jun 11, 2015 at 6:41 PM, Duncan P. N. Exon Smith <
> dexonsmith at apple.com> wrote:
> >
> > > 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.
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150611/678b81ea/attachment.html>


More information about the llvm-dev mailing list