<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;">
<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="}">(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="}">ModuleSetHandleT</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="}">().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="}">::LoadLibraryPermanently(</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=#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="}">Module</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="}">::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="}">Module</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::move(Ms),</font>
<br><font size=1 face="}">             
                     make_unique<</font><font size=1 color=#4181c0 face="}">SectionMemoryManager</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);</font>
<br><font size=1 face="}">  </font><font size=1 color=#4181c0 face="}">Mangler</font><font size=1 face="}">::getNameWithPrefix(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(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="}">);</font>
<br><font size=1 face="}">}</font>
<br>
<br><font size=1 face="}">};</font></table>
<br><font size=2 face="sans-serif"><br>
And calling from main with:<br>
<br>
</font>
<table width=100% style="border-collapse:collapse;">
<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();</font>
<br><font size=1 face="}">        llvm::InitializeNativeTargetAsmPrinter();</font>
<br><font size=1 face="}">        llvm::InitializeNativeTargetAsmParser();</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.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="}">).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></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>