<font size=2 face="sans-serif">Hello Lang,</font>
<br>
<br><font size=2 face="sans-serif">thank you very much! Now I'm understanding
the whole JIT-ting a lot better. I guess, that every problem I had is solved
now. Thank you!</font>
<br>
<br><font size=2 face="sans-serif">Kind regards</font>
<br><font size=2 face="sans-serif">Jö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>,
LLVM Developers Mailing List <llvm-dev@lists.llvm.org></font>
<br><font size=1 color=#5f5f5f face="sans-serif">Date:      
 </font><font size=1 face="sans-serif">10.05.2017 00:50</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 - Resolve obj-Addresses?</font>
<br>
<hr noshade>
<br>
<br>
<br><font size=3>Hi Björn,</font>
<br>
<br><font size=3>But I don't know what to do with "NotifyObjectLoaded".
So I overloaded the function and can now access the sections. Soo... I
allocate memory in my sharedmemory for the sections and use mapSectionAddress
to change the addresses. But how do I copy the memory in the finalizeMemory
method? I don't have access to the section anymore! </font>
<br>
<br><font size=3>You're following the recipe for "the hard way",
so I'll stick to that: Notice that in this scheme the each section is allocated
twice. The first allocation is done by allocateCodeSection/allocateDataSection,
and you have to store a pointer to the memory you allocate (and the section
size, alignment, memory protections, etc) in a collection somewhere inside
the allocator. The second allocation should be done in NotifyObjectLoaded:
You use the stored section information (sizes and alignments) to allocate
the sections at their final addresses inside shared memory, then use the
stored local addresses and newly allocated final addresses to set up the
address mapping. Finally, in finalizeMemory, you copy the relocated bytes
from the local memory allocated by the first allocation to the shared memory
allocated by the second one, then set the appropriate memory protections
on the shared memory. The pseudo-C++ for the whole process looks like this:</font>
<br>
<br><tt><font size=3>RTDyldLinkingLayer::loadObject(Obj) {</font></tt>
<br><tt><font size=3>  for (auto &Section : Obj.sections()) {</font></tt>
<br><tt><font size=3>    uint8_t *LocalAddr = MemMgr.allocateSection(Section.Size,
Section.Align, ...);</font></tt>
<br><tt><font size=3>    copyToLocalMemory(LocalAddr, Section);</font></tt>
<br><tt><font size=3>  }</font></tt>
<br><tt><font size=3>  MemMgr.notifyLoaded(Obj);</font></tt>
<br>
<br><tt><font size=3>  // RTDyldLinkingLayer::finalize() will be called
implicitly when you try to take the address</font></tt>
<br><tt><font size=3>  // of a symbol.</font></tt>
<br><tt><font size=3>}</font></tt>
<br>
<br><tt><font size=3>RTDyldLinkingLayer::finalize() {</font></tt>
<br><tt><font size=3>  // Apply relocations to our local copy of the
sections based on the final addresses set up</font></tt>
<br><tt><font size=3>  // in the address mapping.</font></tt>
<br><tt><font size=3>  applyRelocationsToLocalMemory();</font></tt>
<br>
<br><tt><font size=3>  // Call the memory manager to copy the now-relocated
bytes from local memory to their final</font></tt>
<br><tt><font size=3>  // addresses, then mprotect the memory so that
the code at the final address can be</font></tt>
<br><tt><font size=3>  // executed.</font></tt>
<br><tt><font size=3>  MemMgr.finalizeMemory();</font></tt>
<br><tt><font size=3>}</font></tt>
<br>
<br><tt><font size=3>uint8_t* MyMemoryManager::allocate*Section(Size, Align,
...) {</font></tt>
<br><tt><font size=3>  uint8_t *LocalAddr = allocate_aligned_memory(Size,
Align);</font></tt>
<br><tt><font size=3>  AllocsToRemap.push_back(RemapRecord(LocalAddr,
Size, Align));</font></tt>
<br><tt><font size=3>  return LocalAddr;</font></tt>
<br><tt><font size=3>}</font></tt>
<br>
<br><tt><font size=3>void MyMemoryManager::notifyLoaded(Obj, RTDyld) {</font></tt>
<br><tt><font size=3>  for (auto &AR : AllocsToRemap) {</font></tt>
<br><tt><font size=3>    JITTargetAddress FinalAddr = allocateSharedMemory(AR.Size,
AR.Align);</font></tt>
<br><tt><font size=3>    RTDyld.mapSectionAddress(AR.LocalAddr,
FinalAddr);</font></tt>
<br><tt><font size=3>    AllocsToFinalize.push_back(FinalizeRecord(LocalAddr,
FinalAddr, Size));</font></tt>
<br><tt><font size=3>  }</font></tt>
<br><tt><font size=3>  AllocsToRemap.clear();</font></tt>
<br><tt><font size=3>}</font></tt>
<br>
<br><tt><font size=3>void MyMemoryManager::finalizeMemory() {</font></tt>
<br><tt><font size=3>  for (auto &AR : AllocsToFinalize) {</font></tt>
<br><tt><font size=3>    copyFromLocalToFinalMemory(AR.FinalAddr,
AR.LocalAddr, AR.Size);</font></tt>
<br><tt><font size=3>    setMemoryProtections(AR.FinalAddr, Size);</font></tt>
<br><tt><font size=3>  }</font></tt>
<br><tt><font size=3>  AllocsToFinalize.clear();</font></tt>
<br><tt><font size=3>}</font></tt>
<br>
<br><font size=3>Hope this helps!</font>
<br><font size=3> </font>
<br><font size=3>If you're JITing across processes with ORC it might be
worth looking at OrcRemoteTargetClient.h and OrcRemoteTargetServer.h: They
provide a built-in remote memory manager that can allocate memory in the
JIT target process and convey those allocations back to the JIT compiler
via RPC. There's an example of this in the 5th entry of the Building A
JIT tutorial - </font><a href=http://releases.llvm.org/4.0.0/docs/tutorial/BuildingAJIT5.html><font size=3 color=blue><u>http://releases.llvm.org/4.0.0/docs/tutorial/BuildingAJIT5.html</u></font></a><font size=3> (though
unfortunately I haven't had time to write the tutorial text for it yet).</font>
<br>
<br><font size=3>Cheers,</font>
<br><font size=3>Lang.</font>
<br>
<br>
<br><font size=3>On Tue, May 9, 2017 at 7:24 AM, <</font><a href=mailto:bjoern.gaier@horiba.com target=_blank><font size=3 color=blue><u>bjoern.gaier@horiba.com</u></font></a><font size=3>>
wrote:</font>
<br><font size=2 face="sans-serif">Hello Lang,</font><font size=3> <br>
</font><font size=2 face="sans-serif"><br>
thanks for your answer! With small steps I'm coming to my goal.</font><font size=3>
<br>
</font><font size=2 face="sans-serif"><br>
But I don't know what to do with "NotifyObjectLoaded". So I overloaded
the function and can now access the sections. Soo... I allocate memory
in my sharedmemory for the sections and use mapSectionAddress to change
the addresses. But how do I copy the memory in the finalizeMemory method?
I don't have access to the section anymore!</font><font size=3> </font><font size=2 face="sans-serif"><br>
Plus, is there a way to reallocate global variables too, which aren't declared
as extern - give them another memory location? I see these values with
"symbols" in "NotifyObjectLoaded", but I don't have
an address for them.</font><font size=3> </font><font size=2 face="sans-serif"><br>
And... can I load .obj files, which Visual Studio had generated?</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</font><font size=3> <br>
<br>
<br>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
From:        </font><font size=1 face="sans-serif">Lang
Hames <</font><a href=mailto:lhames@gmail.com target=_blank><font size=1 color=blue face="sans-serif"><u>lhames@gmail.com</u></font></a><font size=1 face="sans-serif">></font><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
To:        </font><a href=mailto:bjoern.gaier@horiba.com target=_blank><font size=1 color=blue face="sans-serif"><u>bjoern.gaier@horiba.com</u></font></a><font size=1 face="sans-serif">,
Clang Dev <</font><a href="mailto:cfe-dev@lists.llvm.org" target=_blank><font size=1 color=blue face="sans-serif"><u>cfe-dev@lists.llvm.org</u></font></a><font size=1 face="sans-serif">>,
LLVM Developers Mailing List <</font><a href="mailto:llvm-dev@lists.llvm.org" target=_blank><font size=1 color=blue face="sans-serif"><u>llvm-dev@lists.llvm.org</u></font></a><font size=1 face="sans-serif">></font><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
Date:        </font><font size=1 face="sans-serif">07.05.2017
04:37</font><font size=3> </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 - Resolve obj-Addresses?</font><font size=3> <br>
</font>
<hr noshade><font size=3><br>
<br>
<br>
Hi Bjoern, <br>
<br>
CCing cfg-dev (since that's where the conversation started) and llvm-dev
(since it's relevant there). <br>
<br>
Do you know if there is a way to obtain the fully resolved obj-code? I
wanted to load the functions into a shared memory, but how? The only thing
I receive is a function pointer, but I don't know how large the function
'behind' is. Even a call to _msize (windows) does only resolve in a crash. 
<br>
<br>
You can access the fully-resolved object code on a per-section basis (there
is no easy way to do it per-function) by using a custom memory manager,
rather than SectionMemoryManager. <br>
<br>
There are two ways to go about this: <br>
<br>
(1) The easy way (only valid for shared-memory on the same machine) is
to allocate the sections directly out of shared memory (by overriding allocateCodeSection/allocateDataSection).
<br>
<br>
(2) The hard way (which is general enough to allow you to copy the relocated
bytes to remote machines) is three part: <br>
(a) Allocate the sections locally (e.g. using malloc, or inheriting from
SectionMemoryManager and capturing the allocation details) <br>
(b) Override the notifyObjectLoaded method to allocate the final memory
locations, then call RTDyldMemoryManager::mapSectionAddress to map each
local allocation to its corresponding final address <br>
(c) Override finalizeMemory to copy the relocated bytes to their final
addresses <br>
<br>
Hope this helps. <br>
<br>
Cheers, <br>
Lang. <br>
<br>
<br>
<br>
<br>
On Tue, May 2, 2017 at 10:47 PM, <</font><a href=mailto:bjoern.gaier@horiba.com target=_blank><font size=3 color=blue><u>bjoern.gaier@horiba.com</u></font></a><font size=3>>
wrote: </font><font size=2 face="sans-serif"><br>
Hello Lang,</font><font size=3> </font><font size=2 face="sans-serif"><br>
<br>
your answer explained a lot to me, thank you for that. Do you know if there
is a way to obtain the fully resolved obj-code? I wanted to load the functions
into a shared memory, but how? The only thing I receive is a function pointer,
but I don't know how large the function 'behind' is. Even a call to _msize
(windows) does only resolve in a crash.</font><font size=3> </font><font size=2 face="sans-serif"><br>
<br>
Kind regards</font><font size=3> </font><font size=2 face="sans-serif"><br>
Björn</font><font size=3> <br>
<br>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
<br>
From:        </font><font size=1 face="sans-serif">Lang
Hames <</font><a href=mailto:lhames@gmail.com target=_blank><font size=1 color=blue face="sans-serif"><u>lhames@gmail.com</u></font></a><font size=1 face="sans-serif">></font><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
To:        </font><a href=mailto:bjoern.gaier@horiba.com target=_blank><font size=1 color=blue face="sans-serif"><u>bjoern.gaier@horiba.com</u></font></a><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
Cc:        </font><a href=mailto:willdtz@gmail.com target=_blank><font size=1 color=blue face="sans-serif"><u>willdtz@gmail.com</u></font></a><font size=1 face="sans-serif">,
Clang Dev <</font><a href="mailto:cfe-dev@lists.llvm.org" target=_blank><font size=1 color=blue face="sans-serif"><u>cfe-dev@lists.llvm.org</u></font></a><font size=1 face="sans-serif">></font><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
Date:        </font><font size=1 face="sans-serif">01.05.2017
20:15</font><font size=3> </font><font size=1 color=#5f5f5f face="sans-serif"><br>
Subject:        </font><font size=1 face="sans-serif">Re:
[cfe-dev] JIT doens't resolve address - Resolve obj-Addresses?</font><font size=3>
<br>
</font>
<hr noshade><font size=3><br>
<br>
<br>
<br>
Hi Björn, <br>
<br>
I tried using your code and now it works! I'm happy and surprised! <br>
<br>
Oh - I missed that Will's code had fixed your issue. That's great. :) <br>
<br>
Do you know if there is a way to directly load .obj-Files and resolve the
references with own addresses? Like when somebody used printf in the generated
obj-File, but I replace the address with my own printf?  <br>
<br>
You can load precompiled object files by calling addObjectSet on the ObjectLayer.
<br>
<br>
All external symbol references are resolved by the JITSymbolResolver that
is created by createLambdaResolver. In your example the resolution process
is two-step: First the JIT searches its own function definitions to see
if there is a function called "printf" defined there (this is
what the call to CompileLayer.findSymbol does): </font><tt><font size=3><br>
<br>
[&](const std::string &Name)  {</font></tt><font size=3> </font><tt><font size=3><br>
  printf("FLUSH :0\n");</font></tt><font size=3> </font><tt><font size=3><br>
  if (auto Sym = CompileLayer.findSymbol(Name, false))</font></tt><font size=3>
</font><tt><font size=3><br>
    return Sym;</font></tt><font size=3> </font><tt><font size=3><br>
  return JITSymbol(nullptr);</font></tt><font size=3> </font><tt><font size=3><br>
}</font></tt><font size=3> <br>
<br>
Then, if it doesn't find a definition there, it falls back to searching
in the current process (this is what RTDyldMemoryManager::getSymbolAddressInProcess
does): </font><tt><font size=3><br>
<br>
[](const std::string &S)  { </font></tt><font size=3> </font><tt><font size=3><br>
  printf("PLUSH :0\n"); </font></tt><font size=3> </font><tt><font size=3><br>
  if (auto SymAddr = </font></tt><font size=3> </font><tt><font size=3><br>
        RTDyldMemoryManager::getSymbolAddressInProcess(S)) </font></tt><font size=3>
</font><tt><font size=3><br>
    return JITSymbol(SymAddr, JITSymbolFlags::Exported); </font></tt><font size=3>
</font><tt><font size=3><br>
  return JITSymbol(nullptr); </font></tt><font size=3> </font><tt><font size=3><br>
}</font></tt><font size=3> <br>
<br>
If, for example, you wanted to redirect calls to printf to your own custom
version (while resolving all other externals by the normal process) you
could change the second lambda like so: </font><tt><font size=3><br>
<br>
[](const std::string &S)  { </font></tt><font size=3> </font><tt><font size=3><br>
  printf("PLUSH :0\n");</font></tt><font size=3> </font><tt><font size=3><br>
  if (S == mangleName("printf"))</font></tt><font size=3>
</font><tt><font size=3><br>
    return JITSymbol((JITSymbolAddress)&myCustomPrintf,</font></tt><font size=3>
</font><tt><font size=3><br>
                     JITSymbolFlags::Exported);</font></tt><font size=3>
</font><tt><font size=3><br>
  if (auto SymAddr = </font></tt><font size=3> </font><tt><font size=3><br>
        RTDyldMemoryManager::getSymbolAddressInProcess(S)) </font></tt><font size=3>
</font><tt><font size=3><br>
    return JITSymbol(SymAddr, JITSymbolFlags::Exported); </font></tt><font size=3>
</font><tt><font size=3><br>
  return JITSymbol(nullptr); </font></tt><font size=3> </font><tt><font size=3><br>
}</font></tt><font size=3> <br>
<br>
(You'll just have to pull the mangling logic from findSymbol out into its
own "mangleName" function for this). <br>
<br>
Hope this helps. <br>
<br>
Cheers, <br>
Lang. <br>
<br>
<br>
On Sun, Apr 23, 2017 at 11:08 PM, <</font><a href=mailto:bjoern.gaier@horiba.com target=_blank><font size=3 color=blue><u>bjoern.gaier@horiba.com</u></font></a><font size=3>>
wrote: </font><font size=2 face="sans-serif"><br>
Hello Mr. Dietz,</font><font size=3> </font><font size=2 face="sans-serif"><br>
<br>
I tried using your code and now it works! I'm happy and surprised! I will
now compare the codes and try to spot the main difference! Thanks for your
help!</font><font size=3> </font><font size=2 face="sans-serif"><br>
Do you know if there is a way to directly load .obj-Files and resolve the
references with own addresses? Like when somebody used printf in the generated
obj-File, but I replace the address with my own printf?</font><font size=3>
</font><font size=2 face="sans-serif"><br>
<br>
Kind regards</font><font size=3> </font><font size=2 face="sans-serif"><br>
Björn</font><font size=3> <br>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
<br>
<br>
From:        </font><font size=1 face="sans-serif">Will
Dietz <</font><a href=mailto:willdtz@gmail.com target=_blank><font size=1 color=blue face="sans-serif"><u>willdtz@gmail.com</u></font></a><font size=1 face="sans-serif">></font><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
To:        </font><a href=mailto:bjoern.gaier@horiba.com target=_blank><font size=1 color=blue face="sans-serif"><u>bjoern.gaier@horiba.com</u></font></a><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
Cc:        </font><font size=1 face="sans-serif">Lang
Hames <</font><a href=mailto:lhames@gmail.com target=_blank><font size=1 color=blue face="sans-serif"><u>lhames@gmail.com</u></font></a><font size=1 face="sans-serif">>,
Clang Dev <</font><a href="mailto:cfe-dev@lists.llvm.org" target=_blank><font size=1 color=blue face="sans-serif"><u>cfe-dev@lists.llvm.org</u></font></a><font size=1 face="sans-serif">></font><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
Date:        </font><font size=1 face="sans-serif">21.04.2017
21:50</font><font size=3> </font><font size=1 color=#5f5f5f face="sans-serif"><br>
Subject:        </font><font size=1 face="sans-serif">Re:
[cfe-dev] JIT doens't resolve address</font><font size=3> </font><font size=1 color=#5f5f5f face="sans-serif"><br>
Sent by:        </font><a href=mailto:w@wdtz.org target=_blank><font size=1 color=blue face="sans-serif"><u>w@wdtz.org</u></font></a><font size=3>
<br>
</font>
<hr noshade><font size=3><br>
<br>
<br>
Hmm, I tried using your code (copy+pasted and then mass-dumped headers
to fix includes), and it seems to work fine for me: </font><font size=3 color=blue><u><br>
<br>
</u></font><a href="https://gist.github.com/dtzWill/df84b64a73001532e3fcfe73a2cffbb9#file-test-log" target=_blank><font size=3 color=blue><u>https://gist.github.com/dtzWill/df84b64a73001532e3fcfe73a2cffbb9#file-test-log</u></font></a><font size=3>
<br>
<br>
Do you get different results when using your LLVM? If not, can you identify
the differences between your code and what I constructed from your snippets?
<br>
<br>
I noticed "PAUSE", are you on Windows? I'm not sure what the
functionality/status of the JIT on Windows is, perhaps someone else can
comment on that. <br>
<br>
~Will <br>
<br>
On Fri, Apr 21, 2017 at 1:03 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><font size=2 face="sans-serif"><br>
Hello Lang,</font><font size=3> </font><font size=2 face="sans-serif"><br>
<br>
the Code for my jit_main is a normal printf-HelloWorld program. I opended
the file with notepad and found the following:</font><font size=3> </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=2 face="sans-serif">;
Function Attrs: noinline norecurse uwtable</font><font size=3> </font><font size=2 face="sans-serif"><br>
define i32 @main() #0 {</font><font size=3> </font><font size=2 face="sans-serif"><br>
entry:</font><font size=3> </font><font size=2 face="sans-serif"><br>
  %retval = alloca i32, align 4</font><font size=3> </font><font size=2 face="sans-serif"><br>
  store i32 0, i32* %retval, align 4</font><font size=3> </font><font size=2 face="sans-serif"><br>
  %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><font size=3> </font><font size=2 face="sans-serif"><br>
  %call1 = call i32 @system(i8* getelementptr inbounds ([6 x i8],
[6 x i8]* @"\01??_C@_05DIAHPDGL@PAUSE?$AA@", i32 0, i32 0))</font><font size=3>
</font><font size=2 face="sans-serif"><br>
  ret i32 0</font><font size=3> </font><font size=2 face="sans-serif"><br>
}</font></table>
<br><font size=3><br>
</font><font size=2 face="sans-serif"><br>
<br>
<br>
<br>
So... there is a main... Anyway! The code for the main is this one:</font><font size=3>
</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=#808080>#include</font><font size=1>
</font><font size=1 color=#a11f12><stdio.h></font><font size=3> </font><font size=1 color=#808080><br>
#include</font><font size=1> </font><font size=1 color=#a11f12><stdlib.h></font><font size=3>
</font><font size=1 color=blue><br>
<br>
int</font><font size=1> main()</font><font size=3> </font><font size=1><br>
{</font><font size=3> </font><font size=1><br>
        printf(</font><font size=1 color=#a11f12>"Planschbeckilein
sagt Halloilein!\n"</font><font size=1>);</font><font size=3> </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>
<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=3><br>
</font><font size=2 face="sans-serif"><br>
<br>
<br>
<br>
Kind regards</font><font size=3> </font><font size=2 face="sans-serif"><br>
Björn</font><font size=3> </font><font size=1 color=#5f5f5f face="sans-serif"><br>
<br>
<br>
<br>
From:        </font><font size=1 face="sans-serif">Lang
Hames <</font><a href=mailto:lhames@gmail.com target=_blank><font size=1 color=blue face="sans-serif"><u>lhames@gmail.com</u></font></a><font size=1 face="sans-serif">></font><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
To:        </font><a href=mailto:bjoern.gaier@horiba.com target=_blank><font size=1 color=blue face="sans-serif"><u>bjoern.gaier@horiba.com</u></font></a><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
Cc:        </font><font size=1 face="sans-serif">Clang
Dev <</font><a href="mailto:cfe-dev@lists.llvm.org" target=_blank><font size=1 color=blue face="sans-serif"><u>cfe-dev@lists.llvm.org</u></font></a><font size=1 face="sans-serif">></font><font size=3>
</font><font size=1 color=#5f5f5f face="sans-serif"><br>
Date:        </font><font size=1 face="sans-serif">20.04.2017
22:54</font><font size=3> </font><font size=1 color=#5f5f5f face="sans-serif"><br>
Subject:        </font><font size=1 face="sans-serif">Re:
[cfe-dev] JIT doens't resolve address</font><font size=3> <br>
</font>
<hr noshade><font size=3><br>
<br>
<br>
<br>
Hi Bjoern, <br>
<br>
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. <br>
<br>
A failure to find main has three likely causes: <br>
<br>
(1) jit_main.ll does not define main, <br>
(2) jit_main.ll defines main with private/internal linkage (the JIT can't
see private/internal symbols in general) <br>
(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. <br>
<br>
Can you share the contents of your jit_main.ll Module? <br>
<br>
Cheers, <br>
Lang. <br>
    <br>
<br>
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: <br>
HI Bjoern, <br>
<br>
There are two kinds of symbol lookup in the JIT: <br>
<br>
(1) You want to find a symbol defined JIT'd code. This is what "findSymbol"
on the JIT class does. <br>
<br>
(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. <br>
<br>
So assuming your main function in main.ll is trivial, e.g. </font><tt><font size=3><br>
<br>
int main() {</font></tt><font size=3> </font><tt><font size=3><br>
  return 0;</font></tt><font size=3> </font><tt><font size=3><br>
}</font></tt><font size=3> <br>
<br>
then your findSymbol call will return the address of the JIT'd main without
ever needing to look anything up in the resolver. <br>
<br>
If, on the other hand, your main function contains an external reference,
e.g. </font><tt><font size=3><br>
<br>
int main() {</font></tt><font size=3> </font><tt><font size=3><br>
  printf("Hello, World!"); // Reference to externally defined
function printf.</font></tt><font size=3> </font><tt><font size=3><br>
  return 0;</font></tt><font size=3> </font><tt><font size=3><br>
}</font></tt><font size=3> <br>
<br>
Then you'll receive a call back on your resolver looking for the address
of printf. <br>
<br>
Hope this helps! <br>
<br>
Cheers, <br>
Lang. <br>
<br>
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><font size=2 face="sans-serif"><br>
Hello LLVM-World,</font><font size=3> </font><font size=2 face="sans-serif"><br>
<br>
I was following the "Building a JIT in LLVM"-Tutorial and tried
to load a normal main. My code is the following:</font><font size=3> </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> </font><font size=1 color=blue><br>
<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>
</font><font size=1><br>
<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> </font><font size=1><br>
<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>
</font><font size=1><br>
<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> </font><font size=1><br>
<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> </font><font size=1><br>
<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> </font><font size=1><br>
<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>
</font><font size=1><br>
<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> </font><font size=1 color=#4181c0><br>
<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> </font><font size=1 color=blue><br>
<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> </font><font size=1><br>
<br>
};</font></table>
<br><font size=2 face="sans-serif"><br>
<br>
<br>
<br>
<br>
<br>
And calling from main with:</font><font size=3> </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>
</font><font size=1><br>
<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> </font><font size=1><br>
<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>
</font><font size=1><br>
<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>
</font><font size=1><br>
<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>
<br>
<br>
<br>
<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> </font><font size=2 face="sans-serif"><br>
<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><font size=3> <br>
_______________________________________________<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><font size=2 face="sans-serif"><br>
<br>
<br>
<br>
<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><font size=3> <br>
<br>
_______________________________________________<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><font size=2 face="sans-serif"><br>
<br>
<br>
<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><font size=3> <br>
</font><font size=2 face="sans-serif"><br>
<br>
<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><font size=3> <br>
<br>
</font><font size=2 face="sans-serif"><br>
<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>
<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>