<div dir="ltr"><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Kaylor, Andrew</b> <span dir="ltr"><<a href="mailto:andrew.kaylor@intel.com">andrew.kaylor@intel.com</a>></span><br>Date: 2014-10-29 1:16 GMT+02:00<br>Subject: RE: [LLVMdev] MCJIT + Win64 SEH handling problem (missing unwinding info)<br>To: Aliaksei Zasenka <<a href="mailto:listhex@gmail.com">listhex@gmail.com</a>><br><br><br>





<div link="blue" vlink="purple" lang="EN-US">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">It sounds like you understand the basic outline pretty well.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Probably the biggest pitfall that we encountered while implementing the existing formats was in understanding the distinction between the address that a section
 has within the object file (ObjAddress) and the address at which it will be loaded (LoadAddress).  The data structures evolved during the implementation to better handle this distinction, and we tried to document it with comments as problems were fixed.  So
 hopefully that part will be easier for you.  <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">I believe that we also encountered some inconsistencies in the way that the ObjectFile interface was implemented by ELFObjectFile and MachOObjectFile.  As you
 begin working with COFFObjectFile, if it appears that something is implemented strangely, it may be that it is incorrect.  Feel free to ask about problems like this before spending excessive effort trying to understand an implementation that may be broken.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Eventually in order to have COFF support at the same level as ELF and MachO support we’ll need it to handle multiple modules and out-of-process execution, but
 I strongly suggest that you just focus on getting it working with a single simple module that has no external dependencies as a first step.  Linking and address remapping can be tricky, but you should be able to get something working without those features
 being implemented.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">The tests under llvm/test/ExecutionEngine use lli to exercise MCJIT, but there are hooks in the mechanism somewhere that currently force these tests to use
 ELF format on Windows.  There is one such hook in the llvm/test/lit.cfg file.  There may be others.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Let me know if you run into any problems.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Good luck!<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">-Andy<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> Aliaksei Zasenka [mailto:<a href="mailto:listhex@gmail.com" target="_blank">listhex@gmail.com</a>]
<br>
<b>Sent:</b> Tuesday, October 28, 2014 3:54 PM<br>
<b>To:</b> Kaylor, Andrew<br>
<b>Subject:</b> Re: [LLVMdev] MCJIT + Win64 SEH handling problem (missing unwinding info)<u></u><u></u></span></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Andrew,<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">First of all thanks for your answer.
<br>
<br>
In general I understand what steps should be done. Firstly something like RyntimeDyldCOFF should be implemented and supported. This class is responsible for object file loading, resolving relocations and EH frames registration. Then FunctionTable and unwind
 data located in COFF should be introduced to system with RtlAddFunctionTable on finalizing.
<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">I suppose lots of details may arise and to make quality product is not that simple like it seems. But
<span lang="EN">I think I'll try to make a prototype. <br>
</span><br>
<span lang="EN">Would appreciate any help or advice.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Regards,<br>
Aliaksei<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">2014-10-28 22:37 GMT+02:00 Kaylor, Andrew <<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>>:<u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Hi Aliaksei,</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">The MCJIT code is structured in such a way as to abstract the details of object file formats.  What
 is primarily lacking is a COFF implementation of the RuntimeDyld interface.  When we first implemented MCJIT on Windows using the ELF object format was sufficient for the applications we were aware of that intended to use MCJIT on Windows, so we used ELF-on-Windows
 as a sort of shortcut.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">I’m not aware of anything that would prevent MCJIT from working with COFF files if someone was willing
 to invest the effort to implement the RuntimeDyld support for COFF.  It’s likely that one or two small issues would arise, but I don’t think there would be any major design issues.  However, even after basic MCJIT execution of COFF images is in place there
 may be additional work to be done to make exception handling work correctly.  I don’t know one way or the other about that.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">If you are interested in trying to implement the RuntimeDyld support for COFF I can provide you with
 some details of what is involved.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">-Andy</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> </span><u></u><u></u></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">
<a href="mailto:llvmdev-bounces@cs.uiuc.edu" target="_blank">llvmdev-bounces@cs.uiuc.edu</a> [mailto:<a href="mailto:llvmdev-bounces@cs.uiuc.edu" target="_blank">llvmdev-bounces@cs.uiuc.edu</a>]
<b>On Behalf Of </b>Aliaksei Zasenka<br>
<b>Sent:</b> Tuesday, October 28, 2014 5:26 AM<br>
<b>To:</b> <a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a><br>
<b>Subject:</b> [LLVMdev] MCJIT + Win64 SEH handling problem (missing unwinding info)</span><u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Hi all,<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
I'm using MCJIT for running the manually generated code under both Win32 and Win64 environments.  Unfortunately catching exceptions under Win64 doesn't work. As I understand the main problem is the lack of function table that is needed to unwind a call stack.<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Microsoft
<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms680595%28v=vs.85%29.aspx" target="_blank">
says </a><br>
<i>Function tables are used on 64-bit Windows to determine how to unwind or walk the stack. These tables are usually generated by the compiler and stored as part of the image. However, applications must provide the function table for dynamically generated code.</i><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">As I can see LLVM usually creates Function Table when generating COFF (Win64EH::UnwindEmitter). But MCJIT can load only ELF files.
<br>
<br>
Is there any way to workaround the problem? Maybe it is already planned to implement COFF loading in MCJIT?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Best regards,<br>
Aliaksei<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div></div></div>
</div>

</div><br></div>