<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Eli,<div><br></div><div>You will need to add the process symbols explicitly to the search space using:</div><div><br></div><div><font face="monospace, monospace">  sys::DynamicLibrary::LoadLibraryPermanently(nullptr);</font></div><div><br></div><div>If you are on linux, you will also need to make sure that process symbols are exported by passing <font face="monospace, monospace">-Wl,--export-dynamic</font> to clang, and ensure that any symbols that you want to call are not dead-stripped by the linker.<br></div><div><br></div><div>Hope that helps!</div><div><br></div><div>Cheers,</div><div>Lang.</div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 11, 2019 at 8:16 PM Eli Baum via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi!<div><br></div><div>I am using LLVM to create a JIT – I have followed the excellent Kaleidoscope tutorial, and am now starting to build off of it.</div><div><br></div><div>Down the line, I plan to use this JIT for my research into computer architecture, which means that I will need to run the JIT itself within a simulated CPU and bare-bones linux system. To make that research easier, I am trying to statically compile my JIT. Everything works with the normal compilation (clang ... -rdynamic) but when switched to static (clang ... -static), <i>I can no longer execute externally defined C functions</i>. I suspect this is something to do with the symbol loader not being properly set up for static linking, but I don't know much about that.</div><div><br></div><div>Here are some more details:</div><div><br></div><div><b>In the normally-compiled kaleidoscope JIT, </b>I can run something like this:</div><div><div><font face="monospace, monospace">kld> extern printd(x);</font></div><div><font face="monospace, monospace">kld> printd(10);</font></div><div><font face="monospace, monospace">10.000000</font></div></div><div><br></div><div>printd was one of the C functions defined within the main program:</div><div><div><font face="monospace, monospace">/// printd - printf that takes a double prints it as "%f\n", returning 0.</font></div><div><font face="monospace, monospace">extern "C" DLLEXPORT double printd(double X) {</font></div><div><font face="monospace, monospace">  fprintf(stderr, "%f\n", X);</font></div><div><font face="monospace, monospace">  return X;</font></div><div><font face="monospace, monospace">}</font></div></div><div><br></div><div>However, in the static linked version, the JIT cannot find the symbol:</div><div><div><font face="monospace, monospace">kld> extern printd(x);</font></div><div><font face="monospace, monospace">kld> printd(1);</font></div><div><font face="monospace, monospace">kaleidoscope error: Symbols not found: { printd }</font></div></div><div><br></div><div>But the symbol is there:</div><div><div><font face="monospace, monospace">$ llvm-nm kld_s | grep printd</font></div><div><font face="monospace, monospace">0000000000497e30 T printd</font></div><div><font face="monospace, monospace">$ llvm-nm kld | grep printd<br></font></div><div><font face="monospace, monospace">00000000006b1820 T printd</font></div><div><br></div></div><div>(kld_s is the static version)</div><div><br></div><div>How can I begin to debug this issue? Does something different happen to the symbol loader when I compile statically?</div><div><br></div><div>My two make scripts look like</div><div><div><font face="monospace, monospace">$ make kld</font></div><div><font face="monospace, monospace">clang++ -v  kld.cpp libkld.o /home/eli/sproj-git/passes/test/libTestPass.o `llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native ` -fuse-ld=gold -g3 -o kld <b>-rdynamic</b></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">$ make kld_s</font></div><div><font face="monospace, monospace">clang++ -v  kld.cpp libkld.o /home/eli/sproj-git/passes/test/libTestPass.o `llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native ` -fuse-ld=gold -g3 -o kld_s <b>-static</b><br></font></div></div><div><br></div><div>The only difference is in the final flag (bold). libkld.o and libTestPass.o are unrelated object files I am linking from other parts of my project.</div><div><br></div><div>If the clang option -static behaves like gcc's, the man page does say that it "... prevents linking with the shared libraries", which I suppose is not precisely what I want. </div><div><br></div><div>I previously emailed llvm-dev a few months ago about a similar issue, where extern functions were not working at all (even dynamically). I resolved<i> that </i>issue by downgrading to LLVM 6, and recently, by upgrading to LLVM 9. Now the issue has returned, I suppose, in slightly different form.</div><div><br></div><div>If anyone has any ideas, please let me know!</div><div><br></div><div>Thank you so much,</div><div>Eli Baum</div></div></div></div></div></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>