<div dir="ltr">HI Bjoern,<div><br></div><div>There are two kinds of symbol lookup in the JIT:</div><div><br></div><div>(1) You want to find a symbol defined JIT'd code. This is what "findSymbol" on the JIT class does.</div><div><br></div><div>(2) The JIT wants to fix up a module that contains references to symbols defined elsewhere (either in your program, or in other JIT'd code). This is the SymbolResolver's job.</div><div><br></div><div>So assuming your main function in main.ll is trivial, e.g.</div><div><br></div><div><font face="monospace, monospace">int main() {</font></div><div><font face="monospace, monospace">  return 0;</font></div><div><font face="monospace, monospace">}</font></div><div><br></div><div>then your findSymbol call will return the address of the JIT'd main without ever needing to look anything up in the resolver.</div><div><br></div><div>If, on the other hand, your main function contains an external reference, e.g.</div><div><br></div><div><font face="monospace, monospace">int main() {</font></div><div><font face="monospace, monospace">  printf("Hello, World!"); // Reference to externally defined function printf.</font></div><div><font face="monospace, monospace">  return 0;</font></div><div><font face="monospace, monospace">}</font></div><div><br></div><div>Then you'll receive a call back on your resolver looking for the address of printf.</div><div><br></div><div>Hope this helps!</div><div><br></div><div>Cheers,</div><div>Lang.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 20, 2017 at 6:43 AM, via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><font size="2" face="sans-serif">Hello LLVM-World,</font>
<br>
<br><font size="2" face="sans-serif">I was following the "Building a
JIT in LLVM"-Tutorial and tried to load a normal main. My code is
the following:<br>
</font>
<table width="100%" style="border-collapse:collapse">
<tbody><tr valign="top" height="8">
<td width="100%" style="border-style:solid solid solid solid;border-color:#000000;border-width:1px 1px 1px 1px;padding:0px 0px"><font size="1" color="blue" face="}">class</font><font size="1" face="}">
</font><font size="1" color="#4181c0" face="}">Jitter</font>
<br><font size="1" face="}">{</font>
<br><font size="1" color="blue" face="}">private</font><font size="1" face="}">:</font>
<br><font size="1" face="}">  std::</font><font size="1" color="#4181c0" face="}">unique_ptr</font><font size="1" face="}"><</font><font size="1" color="#4181c0" face="}">TargetMachine</font><font size="1" face="}">>
TM;</font>
<br><font size="1" face="}">  </font><font size="1" color="blue" face="}">const</font><font size="1" face="}">
</font><font size="1" color="#4181c0" face="}">DataLayout</font><font size="1" face="}">
DL;</font>
<br><font size="1" face="}">  </font><font size="1" color="#4181c0" face="}">ObjectLinkingLayer</font><font size="1" face="}"><>
ObjectLayer;</font>
<br><font size="1" face="}">  </font><font size="1" color="#4181c0" face="}">IRCompileLayer</font><font size="1" face="}"><</font><font size="1" color="blue" face="}">decltype</font><font size="1" face="}">(<wbr>ObjectLayer)>
CompileLayer;</font>
<br>
<br><font size="1" color="blue" face="}">public</font><font size="1" face="}">:</font>
<br><font size="1" face="}">  </font><font size="1" color="blue" face="}">typedef</font><font size="1" face="}">
</font><font size="1" color="blue" face="}">decltype</font><font size="1" face="}">(CompileLayer)::</font><font size="1" color="#4181c0" face="}">Module<wbr>SetHandleT</font><font size="1" face="}">
</font><font size="1" color="#4181c0" face="}">ModuleHandle</font><font size="1" face="}">;</font>
<br>
<br><font size="1" face="}">  Jitter() : TM(</font><font size="1" color="#4181c0" face="}">EngineBuilder</font><font size="1" face="}">().<wbr>selectTarget()),
DL(TM</font><font size="1" color="#008080" face="}">-></font><font size="1" face="}">createDataLayout()),</font>
<br><font size="1" face="}">      CompileLayer(ObjectLayer,
</font><font size="1" color="#4181c0" face="}">SimpleCompiler</font><font size="1" face="}">(</font><font size="1" color="#008080" face="}">*</font><font size="1" face="}">TM))
</font>
<br><font size="1" face="}">  {printf(</font><font size="1" color="#a11f12" face="}">"!"</font><font size="1" face="}">);</font>
<br><font size="1" face="}">        llvm::sys::</font><font size="1" color="#4181c0" face="}">DynamicLibrary</font><font size="1" face="}">::<wbr>LoadLibraryPermanently(</font><font size="1" color="blue" face="}">nullptr</font><font size="1" face="}"><wbr>);</font>
<br><font size="1" face="}">  }</font>
<br>
<br><font size="1" face="}">  </font><font size="1" color="#4181c0" face="}">TargetMachine</font><font size="1" face="}">
&getTargetMachine() { </font><font size="1" color="blue" face="}">return</font><font size="1" face="}">
</font><font size="1" color="#008080" face="}">*</font><font size="1" face="}">TM;
}</font>
<br>
<br><font size="1" face="}">  </font><font size="1" color="#4181c0" face="}">ModuleHandle</font><font size="1" face="}">
addModule(std::</font><font size="1" color="#4181c0" face="}">unique_ptr</font><font size="1" face="}"><</font><font size="1" color="#4181c0" face="}">Modu<wbr>le</font><font size="1" face="}">>
&&</font><font size="1" color="#808080" face="}">M</font><font size="1" face="}">)
{</font>
<br><font size="1" face="}">  </font><font size="1" color="#008000" face="}">//
Build our symbol resolver:</font>
<br><font size="1" face="}">  </font><font size="1" color="#008000" face="}">//
Lambda 1: Look back into the JIT itself to find symbols that are part of</font>
<br><font size="1" face="}">  </font><font size="1" color="#008000" face="}">//
          the same "logical dylib".</font>
<br><font size="1" face="}">  </font><font size="1" color="#008000" face="}">//
Lambda 2: Search for external symbols in the host process.</font>
<br><font size="1" face="}">  </font><font size="1" color="blue" face="}">auto</font><font size="1" face="}">
Resolver = createLambdaResolver(</font>
<br><font size="1" face="}">      [&](</font><font size="1" color="blue" face="}">const</font><font size="1" face="}">
std::</font><font size="1" color="#4181c0" face="}">string</font><font size="1" face="}">
&</font><font size="1" color="#808080" face="}">Name</font><font size="1" face="}">)
</font>
<br><font size="1" face="}">      {</font>
<br><font size="1" face="}">           
    printf(</font><font size="1" color="#a11f12" face="}">"FLUSH
:0\n"</font><font size="1" face="}">);</font>
<br>
<br><font size="1" face="}">        </font><font size="1" color="blue" face="}">if</font><font size="1" face="}">
(</font><font size="1" color="blue" face="}">auto</font><font size="1" face="}">
Sym = CompileLayer.findSymbol(</font><font size="1" color="#808080" face="}">Name</font><font size="1" face="}">,
</font><font size="1" color="blue" face="}">false</font><font size="1" face="}">))</font>
<br><font size="1" face="}">          </font><font size="1" color="blue" face="}">return</font><font size="1" face="}">
Sym;</font>
<br><font size="1" face="}">        </font><font size="1" color="blue" face="}">return</font><font size="1" face="}">
</font><font size="1" color="#4181c0" face="}">JITSymbol</font><font size="1" face="}">(</font><font size="1" color="blue" face="}">nullptr</font><font size="1" face="}">);</font>
<br><font size="1" face="}">      },</font>
<br><font size="1" face="}">      [](</font><font size="1" color="blue" face="}">const</font><font size="1" face="}">
std::</font><font size="1" color="#4181c0" face="}">string</font><font size="1" face="}">
&</font><font size="1" color="#808080" face="}">S</font><font size="1" face="}">)
</font>
<br><font size="1" face="}">          {</font>
<br><font size="1" face="}">           
      printf(</font><font size="1" color="#a11f12" face="}">"PLUSH
:0\n"</font><font size="1" face="}">);</font>
<br>
<br><font size="1" face="}">        </font><font size="1" color="blue" face="}">if</font><font size="1" face="}">
(</font><font size="1" color="blue" face="}">auto</font><font size="1" face="}">
SymAddr =</font>
<br><font size="1" face="}">             
</font><font size="1" color="#4181c0" face="}">RTDyldMemoryManager</font><font size="1" face="}">::<wbr>getSymbolAddressInProcess(</font><font size="1" color="#808080" face="}">S</font><font size="1" face="}">))</font>
<br><font size="1" face="}">          </font><font size="1" color="blue" face="}">return</font><font size="1" face="}">
</font><font size="1" color="#4181c0" face="}">JITSymbol</font><font size="1" face="}">(SymAddr,
</font><font size="1" color="#4181c0" face="}">JITSymbolFlags</font><font size="1" face="}">::</font><font size="1" color="#37605e" face="}">Exported</font><font size="1" face="}">);</font>
<br><font size="1" face="}">        </font><font size="1" color="blue" face="}">return</font><font size="1" face="}">
</font><font size="1" color="#4181c0" face="}">JITSymbol</font><font size="1" face="}">(</font><font size="1" color="blue" face="}">nullptr</font><font size="1" face="}">);</font>
<br><font size="1" face="}">      });</font>
<br>
<br><font size="1" face="}">  </font><font size="1" color="#008000" face="}">//
Build a singleton module set to hold our module.</font>
<br><font size="1" face="}">  std::</font><font size="1" color="#4181c0" face="}">vector</font><font size="1" face="}"><std::</font><font size="1" color="#4181c0" face="}">unique_ptr</font><font size="1" face="}"><</font><font size="1" color="#4181c0" face="}">Mo<wbr>dule</font><font size="1" face="}">>>
Ms;</font>
<br><font size="1" face="}">  Ms.push_back(std::move(</font><font size="1" color="#808080" face="}">M</font><font size="1" face="}">));</font>
<br>
<br><font size="1" face="}">  </font><font size="1" color="#008000" face="}">//
Add the set to the JIT with the resolver we created above and a newly</font>
<br><font size="1" face="}">  </font><font size="1" color="#008000" face="}">//
created SectionMemoryManager.</font>
<br><font size="1" face="}">  </font><font size="1" color="blue" face="}">return</font><font size="1" face="}">
CompileLayer.addModuleSet(std:<wbr>:move(Ms),</font>
<br><font size="1" face="}">             
                     make_unique<</font><font size="1" color="#4181c0" face="}">SectionMemoryMana<wbr>ger</font><font size="1" face="}">>(),</font>
<br><font size="1" face="}">             
                     std::move(Resolver));</font>
<br><font size="1" face="}">}</font>
<br>
<br><font size="1" color="#4181c0" face="}">JITSymbol</font><font size="1" face="}">
findSymbol(</font><font size="1" color="blue" face="}">const</font><font size="1" face="}">
std::</font><font size="1" color="#4181c0" face="}">string</font><font size="1" face="}">
</font><font size="1" color="#808080" face="}">Name</font><font size="1" face="}">)
{</font>
<br><font size="1" face="}">  std::</font><font size="1" color="#4181c0" face="}">string</font><font size="1" face="}">
MangledName;</font>
<br><font size="1" face="}">  </font><font size="1" color="#4181c0" face="}">raw_string_ostream</font><font size="1" face="}">
MangledNameStream(MangledName)<wbr>;</font>
<br><font size="1" face="}">  </font><font size="1" color="#4181c0" face="}">Mangler</font><font size="1" face="}">::getNameWithPrefix(<wbr>MangledNameStream,
</font><font size="1" color="#808080" face="}">Name</font><font size="1" face="}">,
DL);</font>
<br><font size="1" face="}">  printf(</font><font size="1" color="#a11f12" face="}">"Tzearch
for: %s\n\n"</font><font size="1" face="}">, MangledNameStream.str());</font>
<br><font size="1" face="}">  </font><font size="1" color="blue" face="}">return</font><font size="1" face="}">
CompileLayer.findSymbol(<wbr>MangledNameStream.str(), </font><font size="1" color="blue" face="}">false</font><font size="1" face="}">);</font>
<br><font size="1" face="}">}</font>
<br>
<br><font size="1" color="blue" face="}">void</font><font size="1" face="}">
removeModule(</font><font size="1" color="#4181c0" face="}">ModuleHandle</font><font size="1" face="}">
</font><font size="1" color="#808080" face="}">H</font><font size="1" face="}">)
{</font>
<br><font size="1" face="}">  CompileLayer.removeModuleSet(</font><font size="1" color="#808080" face="}">H</font><font size="1" face="}"><wbr>);</font>
<br><font size="1" face="}">}</font>
<br>
<br><font size="1" face="}">};</font></td></tr></tbody></table>
<br><font size="2" face="sans-serif"><br>
And calling from main with:<br>
<br>
</font>
<table width="100%" style="border-collapse:collapse">
<tbody><tr valign="top" height="8">
<td width="100%" style="border-style:solid solid solid solid;border-color:#000000;border-width:1px 1px 1px 1px;padding:0px 0px"><font size="1" color="blue" face="}">int</font><font size="1" face="}">
main()</font>
<br><font size="1" face="}">{</font>
<br><font size="1" face="}">        llvm::InitializeNativeTarget()<wbr>;</font>
<br><font size="1" face="}">        llvm::<wbr>InitializeNativeTargetAsmPrint<wbr>er();</font>
<br><font size="1" face="}">        llvm::<wbr>InitializeNativeTargetAsmParse<wbr>r();</font>
<br>
<br><font size="1" face="}">        llvm::</font><font size="1" color="#4181c0" face="}">LLVMContext</font><font size="1" face="}">
context;</font>
<br><font size="1" face="}">        llvm::</font><font size="1" color="#4181c0" face="}">SMDiagnostic</font><font size="1" face="}">
dia;</font>
<br>
<br><font size="1" face="}">        std::</font><font size="1" color="#4181c0" face="}">unique_ptr</font><font size="1" face="}"><llvm::</font><font size="1" color="#4181c0" face="}">Module</font><font size="1" face="}">>
M = llvm::parseIRFile(</font><font size="1" color="#a11f12" face="}">"./jit_main.<wbr>ll"</font><font size="1" face="}">,
dia, context);</font>
<br><font size="1" face="}">        </font><font size="1" color="#4181c0" face="}">Jitter</font><font size="1" face="}">
jit;</font>
<br><font size="1" face="}">        printf(</font><font size="1" color="#a11f12" face="}">"Wuff?"</font><font size="1" face="}">);</font>
<br><font size="1" face="}">        </font><font size="1" color="#4181c0" face="}">Jitter</font><font size="1" face="}">::</font><font size="1" color="#4181c0" face="}">ModuleHandle</font><font size="1" face="}">
h = jit.addModule(std::move(M));</font>
<br><font size="1" face="}">        printf(</font><font size="1" color="#a11f12" face="}">"KNUFF!\n"</font><font size="1" face="}">);</font>
<br>
<br><font size="1" face="}">        printf(</font><font size="1" color="#a11f12" face="}">"Kuchen!
0x%p\n"</font><font size="1" face="}">, jit.findSymbol(</font><font size="1" color="#a11f12" face="}">"main"</font><font size="1" face="}">).<wbr>getAddress());</font>
<br>
<br><font size="1" face="}">        system(</font><font size="1" color="#a11f12" face="}">"PAUSE"</font><font size="1" face="}">);</font>
<br><font size="1" face="}">        </font><font size="1" color="blue" face="}">return</font><font size="1" face="}">
0;</font>
<br><font size="1" face="}">}</font></td></tr></tbody></table>
<br><font size="2" face="sans-serif">The Code runs without a fail, but when
the programm tries to resolve "main" the address is 0. The strange
thing: the printf "</font><font size="1" color="#a11f12" face="}">FLUSH
:0\n</font><font size="2" face="sans-serif">" and "</font><font size="1" color="#a11f12" face="}">PLUSH
:0\n</font><font size="2" face="sans-serif">" are never called, so did
the code never compiled? What I'm doing wrong?</font>
<br>
<br><font size="2" face="sans-serif">Kind regards</font>
<br><font size="2" face="sans-serif">Björn<br>
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr.
DE 114 165 789<br>
Geschäftsführer: Hiroshi Kawamura, Dr Hiroshi Nakamura, Markus Bode, Heiko
Lampert, Takashi Nagano, Takeshi Fukushima.</font><br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>