<font size=2 face="sans-serif">Hello Lang,</font>
<br>
<br><font size=2 face="sans-serif">the Code for my jit_main is a normal
printf-HelloWorld program. I opended the file with notepad and found the
following:</font>
<br>
<br>
<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=2 face="sans-serif">;
Function Attrs: noinline norecurse uwtable</font>
<br><font size=2 face="sans-serif">define i32 @main() #0 {</font>
<br><font size=2 face="sans-serif">entry:</font>
<br><font size=2 face="sans-serif">  %retval = alloca i32, align 4</font>
<br><font size=2 face="sans-serif">  store i32 0, i32* %retval, align
4</font>
<br><font size=2 face="sans-serif">  %call = call i32 (i8*, ...) @printf(i8*
getelementptr inbounds ([35 x i8], [35 x i8]* @"\01??_C@_0CD@CMJEAMCD@Planschbeckilein?5sagt?5Halloilein@",
i32 0, i32 0))</font>
<br><font size=2 face="sans-serif">  %call1 = call i32 @system(i8*
getelementptr inbounds ([6 x i8], [6 x i8]* @"\01??_C@_05DIAHPDGL@PAUSE?$AA@",
i32 0, i32 0))</font>
<br><font size=2 face="sans-serif">  ret i32 0</font>
<br><font size=2 face="sans-serif">}</font></table>
<br>
<br><font size=2 face="sans-serif">So... there is a main... Anyway! The
code for the main is this one:</font>
<br>
<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=#808080 face="}">#include</font><font size=1 face="}">
</font><font size=1 color=#a11f12 face="}"><stdio.h></font>
<br><font size=1 color=#808080 face="}">#include</font><font size=1 face="}">
</font><font size=1 color=#a11f12 face="}"><stdlib.h></font>
<br>
<br><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="}">        printf(</font><font size=1 color=#a11f12 face="}">"Planschbeckilein
sagt Halloilein!\n"</font><font size=1 face="}">);</font>
<br><font size=1 face="}">        system(</font><font size=1 color=#a11f12 face="}">"PAUSE"</font><font size=1 face="}">);</font>
<br>
<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>
<br><font size=2 face="sans-serif">Kind regards</font>
<br><font size=2 face="sans-serif">Björn</font>
<br>
<br>
<br>
<br><font size=1 color=#5f5f5f face="sans-serif">From:      
 </font><font size=1 face="sans-serif">Lang Hames <lhames@gmail.com></font>
<br><font size=1 color=#5f5f5f face="sans-serif">To:      
 </font><font size=1 face="sans-serif">bjoern.gaier@horiba.com</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Cc:      
 </font><font size=1 face="sans-serif">Clang Dev <cfe-dev@lists.llvm.org></font>
<br><font size=1 color=#5f5f5f face="sans-serif">Date:      
 </font><font size=1 face="sans-serif">20.04.2017 22:54</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Subject:    
   </font><font size=1 face="sans-serif">Re: [cfe-dev]
JIT doens't resolve address</font>
<br>
<hr noshade>
<br>
<br>
<br><font size=3>Hi Bjoern,</font>
<br>
<br><font size=3>Sorry - I just noticed that the address for your "main"
function has come back as '0'. In this case the answer is even simpler:
The JIT never found the function at all, and so definitely didn't have
any external symbols to resolve, so it never called the resolver.</font>
<br>
<br><font size=3>A failure to find main has three likely causes:</font>
<br>
<br><font size=3>(1) jit_main.ll does not define main,</font>
<br><font size=3>(2) jit_main.ll defines main with private/internal linkage
(the JIT can't see private/internal symbols in general)</font>
<br><font size=3>(3) jit_main.ll defines main with external linkage, but
a system mangling is applied (e.g. on MacOSX 'main' is mangled to '_main').
The mangler code in your findSymbol function *should* correct
for this, but this may fail if the default data layout for your TargetMachine
varies from the Module's DataLayout.</font>
<br>
<br><font size=3>Can you share the contents of your jit_main.ll Module?</font>
<br>
<br><font size=3>Cheers,</font>
<br><font size=3>Lang.</font>
<br><font size=3>   </font>
<br>
<br><font size=3>On Thu, Apr 20, 2017 at 1:35 PM, Lang Hames <</font><a href=mailto:lhames@gmail.com target=_blank><font size=3 color=blue><u>lhames@gmail.com</u></font></a><font size=3>>
wrote:</font>
<br><font size=3>HI Bjoern,</font>
<br>
<br><font size=3>There are two kinds of symbol lookup in the JIT:</font>
<br>
<br><font size=3>(1) You want to find a symbol defined JIT'd code. This
is what "findSymbol" on the JIT class does.</font>
<br>
<br><font size=3>(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.</font>
<br>
<br><font size=3>So assuming your main function in main.ll is trivial,
e.g.</font>
<br>
<br><tt><font size=3>int main() {</font></tt>
<br><tt><font size=3>  return 0;</font></tt>
<br><tt><font size=3>}</font></tt>
<br>
<br><font size=3>then your findSymbol call will return the address of the
JIT'd main without ever needing to look anything up in the resolver.</font>
<br>
<br><font size=3>If, on the other hand, your main function contains an
external reference, e.g.</font>
<br>
<br><tt><font size=3>int main() {</font></tt>
<br><tt><font size=3>  printf("Hello, World!"); // Reference
to externally defined function printf.</font></tt>
<br><tt><font size=3>  return 0;</font></tt>
<br><tt><font size=3>}</font></tt>
<br>
<br><font size=3>Then you'll receive a call back on your resolver looking
for the address of printf.</font>
<br>
<br><font size=3>Hope this helps!</font>
<br>
<br><font size=3>Cheers,</font>
<br><font size=3>Lang.</font>
<br>
<br><font size=3>On Thu, Apr 20, 2017 at 6:43 AM, via cfe-dev <</font><a href="mailto:cfe-dev@lists.llvm.org" target=_blank><font size=3 color=blue><u>cfe-dev@lists.llvm.org</u></font></a><font size=3>>
wrote:</font>
<br><font size=2 face="sans-serif">Hello LLVM-World,</font><font size=3>
<br>
</font><font size=2 face="sans-serif"><br>
I was following the "Building a JIT in LLVM"-Tutorial and tried
to load a normal main. My code is the following:</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:1px 1px;"><font size=1 color=blue>class</font><font size=1>
</font><font size=1 color=#4181c0>Jitter</font><font size=3> </font><font size=1><br>
{</font><font size=3> </font><font size=1 color=blue><br>
private</font><font size=1>:</font><font size=3> </font><font size=1><br>
  std::</font><font size=1 color=#4181c0>unique_ptr</font><font size=1><</font><font size=1 color=#4181c0>TargetMachine</font><font size=1>>
TM;</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=blue>const</font><font size=1> </font><font size=1 color=#4181c0>DataLayout</font><font size=1>
DL;</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=#4181c0>ObjectLinkingLayer</font><font size=1><>
ObjectLayer;</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=#4181c0>IRCompileLayer</font><font size=1><</font><font size=1 color=blue>decltype</font><font size=1>(ObjectLayer)>
CompileLayer;</font><font size=3> <br>
</font><font size=1 color=blue><br>
public</font><font size=1>:</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=blue>typedef</font><font size=1> </font><font size=1 color=blue>decltype</font><font size=1>(CompileLayer)::</font><font size=1 color=#4181c0>ModuleSetHandleT</font><font size=1>
</font><font size=1 color=#4181c0>ModuleHandle</font><font size=1>;</font><font size=3>
<br>
</font><font size=1><br>
  Jitter() : TM(</font><font size=1 color=#4181c0>EngineBuilder</font><font size=1>().selectTarget()),
DL(TM</font><font size=1 color=#008080>-></font><font size=1>createDataLayout()),</font><font size=3>
</font><font size=1><br>
      CompileLayer(ObjectLayer, </font><font size=1 color=#4181c0>SimpleCompiler</font><font size=1>(</font><font size=1 color=#008080>*</font><font size=1>TM))
<br>
  {printf(</font><font size=1 color=#a11f12>"!"</font><font size=1>);</font><font size=3>
</font><font size=1><br>
        llvm::sys::</font><font size=1 color=#4181c0>DynamicLibrary</font><font size=1>::LoadLibraryPermanently(</font><font size=1 color=blue>nullptr</font><font size=1>);</font><font size=3>
</font><font size=1><br>
  }</font><font size=3> <br>
</font><font size=1><br>
  </font><font size=1 color=#4181c0>TargetMachine</font><font size=1>
&getTargetMachine() { </font><font size=1 color=blue>return</font><font size=1>
</font><font size=1 color=#008080>*</font><font size=1>TM; }</font><font size=3>
<br>
</font><font size=1><br>
  </font><font size=1 color=#4181c0>ModuleHandle</font><font size=1>
addModule(std::</font><font size=1 color=#4181c0>unique_ptr</font><font size=1><</font><font size=1 color=#4181c0>Module</font><font size=1>>
&&</font><font size=1 color=#808080>M</font><font size=1>) {</font><font size=3>
</font><font size=1><br>
  </font><font size=1 color=#008000>// Build our symbol resolver:</font><font size=3>
</font><font size=1><br>
  </font><font size=1 color=#008000>// Lambda 1: Look back into the
JIT itself to find symbols that are part of</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=#008000>//        
  the same "logical dylib".</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=#008000>// Lambda 2: Search for external
symbols in the host process.</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=blue>auto</font><font size=1> Resolver
= createLambdaResolver(</font><font size=3> </font><font size=1><br>
      [&](</font><font size=1 color=blue>const</font><font size=1>
std::</font><font size=1 color=#4181c0>string</font><font size=1> &</font><font size=1 color=#808080>Name</font><font size=1>)
<br>
      {</font><font size=3> </font><font size=1><br>
                printf(</font><font size=1 color=#a11f12>"FLUSH
:0\n"</font><font size=1>);</font><font size=3> <br>
</font><font size=1><br>
        </font><font size=1 color=blue>if</font><font size=1>
(</font><font size=1 color=blue>auto</font><font size=1> Sym = CompileLayer.findSymbol(</font><font size=1 color=#808080>Name</font><font size=1>,
</font><font size=1 color=blue>false</font><font size=1>))</font><font size=3>
</font><font size=1><br>
          </font><font size=1 color=blue>return</font><font size=1>
Sym;</font><font size=3> </font><font size=1><br>
        </font><font size=1 color=blue>return</font><font size=1>
</font><font size=1 color=#4181c0>JITSymbol</font><font size=1>(</font><font size=1 color=blue>nullptr</font><font size=1>);</font><font size=3>
</font><font size=1><br>
      },</font><font size=3> </font><font size=1><br>
      [](</font><font size=1 color=blue>const</font><font size=1>
std::</font><font size=1 color=#4181c0>string</font><font size=1> &</font><font size=1 color=#808080>S</font><font size=1>)
<br>
          {</font><font size=3> </font><font size=1><br>
                  printf(</font><font size=1 color=#a11f12>"PLUSH
:0\n"</font><font size=1>);</font><font size=3> <br>
</font><font size=1><br>
        </font><font size=1 color=blue>if</font><font size=1>
(</font><font size=1 color=blue>auto</font><font size=1> SymAddr =</font><font size=3>
</font><font size=1><br>
              </font><font size=1 color=#4181c0>RTDyldMemoryManager</font><font size=1>::getSymbolAddressInProcess(</font><font size=1 color=#808080>S</font><font size=1>))</font><font size=3>
</font><font size=1><br>
          </font><font size=1 color=blue>return</font><font size=1>
</font><font size=1 color=#4181c0>JITSymbol</font><font size=1>(SymAddr,
</font><font size=1 color=#4181c0>JITSymbolFlags</font><font size=1>::</font><font size=1 color=#37605e>Exported</font><font size=1>);</font><font size=3>
</font><font size=1><br>
        </font><font size=1 color=blue>return</font><font size=1>
</font><font size=1 color=#4181c0>JITSymbol</font><font size=1>(</font><font size=1 color=blue>nullptr</font><font size=1>);</font><font size=3>
</font><font size=1><br>
      });</font><font size=3> <br>
</font><font size=1><br>
  </font><font size=1 color=#008000>// Build a singleton module set
to hold our module.</font><font size=3> </font><font size=1><br>
  std::</font><font size=1 color=#4181c0>vector</font><font size=1><std::</font><font size=1 color=#4181c0>unique_ptr</font><font size=1><</font><font size=1 color=#4181c0>Module</font><font size=1>>>
Ms;</font><font size=3> </font><font size=1><br>
  Ms.push_back(std::move(</font><font size=1 color=#808080>M</font><font size=1>));</font><font size=3>
<br>
</font><font size=1><br>
  </font><font size=1 color=#008000>// Add the set to the JIT with
the resolver we created above and a newly</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=#008000>// created SectionMemoryManager.</font><font size=3>
</font><font size=1><br>
  </font><font size=1 color=blue>return</font><font size=1> CompileLayer.addModuleSet(std::move(Ms),</font><font size=3>
</font><font size=1><br>
                     
             make_unique<</font><font size=1 color=#4181c0>SectionMemoryManager</font><font size=1>>(),</font><font size=3>
</font><font size=1><br>
                     
             std::move(Resolver));</font><font size=3>
</font><font size=1><br>
}</font><font size=3> <br>
</font><font size=1 color=#4181c0><br>
JITSymbol</font><font size=1> findSymbol(</font><font size=1 color=blue>const</font><font size=1>
std::</font><font size=1 color=#4181c0>string</font><font size=1> </font><font size=1 color=#808080>Name</font><font size=1>)
{</font><font size=3> </font><font size=1><br>
  std::</font><font size=1 color=#4181c0>string</font><font size=1>
MangledName;</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=#4181c0>raw_string_ostream</font><font size=1>
MangledNameStream(MangledName);</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=#4181c0>Mangler</font><font size=1>::getNameWithPrefix(MangledNameStream,
</font><font size=1 color=#808080>Name</font><font size=1>, DL);</font><font size=3>
</font><font size=1><br>
  printf(</font><font size=1 color=#a11f12>"Tzearch for: %s\n\n"</font><font size=1>,
MangledNameStream.str());</font><font size=3> </font><font size=1><br>
  </font><font size=1 color=blue>return</font><font size=1> CompileLayer.findSymbol(MangledNameStream.str(),
</font><font size=1 color=blue>false</font><font size=1>);</font><font size=3>
</font><font size=1><br>
}</font><font size=3> <br>
</font><font size=1 color=blue><br>
void</font><font size=1> removeModule(</font><font size=1 color=#4181c0>ModuleHandle</font><font size=1>
</font><font size=1 color=#808080>H</font><font size=1>) {</font><font size=3>
</font><font size=1><br>
  CompileLayer.removeModuleSet(</font><font size=1 color=#808080>H</font><font size=1>);</font><font size=3>
</font><font size=1><br>
}</font><font size=3> <br>
</font><font size=1><br>
};</font></table>
<br><font size=2 face="sans-serif"><br>
<br>
And calling from main with:<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:1px 1px;"><font size=1 color=blue>int</font><font size=1>
main()</font><font size=3> </font><font size=1><br>
{</font><font size=3> </font><font size=1><br>
        llvm::InitializeNativeTarget();</font><font size=3>
</font><font size=1><br>
        llvm::InitializeNativeTargetAsmPrinter();</font><font size=3>
</font><font size=1><br>
        llvm::InitializeNativeTargetAsmParser();</font><font size=3>
<br>
</font><font size=1><br>
        llvm::</font><font size=1 color=#4181c0>LLVMContext</font><font size=1>
context;</font><font size=3> </font><font size=1><br>
        llvm::</font><font size=1 color=#4181c0>SMDiagnostic</font><font size=1>
dia;</font><font size=3> <br>
</font><font size=1><br>
        std::</font><font size=1 color=#4181c0>unique_ptr</font><font size=1><llvm::</font><font size=1 color=#4181c0>Module</font><font size=1>>
M = llvm::parseIRFile(</font><font size=1 color=#a11f12>"./jit_main.ll"</font><font size=1>,
dia, context);</font><font size=3> </font><font size=1><br>
        </font><font size=1 color=#4181c0>Jitter</font><font size=1>
jit;</font><font size=3> </font><font size=1><br>
        printf(</font><font size=1 color=#a11f12>"Wuff?"</font><font size=1>);</font><font size=3>
</font><font size=1><br>
        </font><font size=1 color=#4181c0>Jitter</font><font size=1>::</font><font size=1 color=#4181c0>ModuleHandle</font><font size=1>
h = jit.addModule(std::move(M));</font><font size=3> </font><font size=1><br>
        printf(</font><font size=1 color=#a11f12>"KNUFF!\n"</font><font size=1>);</font><font size=3>
<br>
</font><font size=1><br>
        printf(</font><font size=1 color=#a11f12>"Kuchen!
0x%p\n"</font><font size=1>, jit.findSymbol(</font><font size=1 color=#a11f12>"main"</font><font size=1>).getAddress());</font><font size=3>
<br>
</font><font size=1><br>
        system(</font><font size=1 color=#a11f12>"PAUSE"</font><font size=1>);</font><font size=3>
</font><font size=1><br>
        </font><font size=1 color=blue>return</font><font size=1>
0;</font><font size=3> </font><font size=1><br>
}</font></table>
<br><font size=2 face="sans-serif"><br>
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>FLUSH
:0\n</font><font size=2 face="sans-serif">" and "</font><font size=1 color=#a11f12>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><font size=3> <br>
</font><font size=2 face="sans-serif"><br>
Kind regards</font><font size=3> </font><font size=2 face="sans-serif"><br>
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><font size=3>_______________________________________________<br>
cfe-dev mailing list</font><font size=3 color=blue><u><br>
</u></font><a href="mailto:cfe-dev@lists.llvm.org" target=_blank><font size=3 color=blue><u>cfe-dev@lists.llvm.org</u></font></a><font size=3 color=blue><u><br>
</u></font><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target=_blank><font size=3 color=blue><u>http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</u></font></a><font size=3><br>
</font>
<br>
<br>
<br>
<br><font size=2 face="sans-serif"><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>