<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        color:black;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";
        color:black;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        color:black;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;
        color:black;}
span.EmailStyle21
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;}
span.EmailStyle22
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body bgcolor="white" lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hey Stefan,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I provided the .cpp file, the .obj file and the .cod file in the attachments at the bug report (<a href="https://bugs.llvm.org/show_bug.cgi?id=39447">https://bugs.llvm.org/show_bug.cgi?id=39447</a>)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The .cod file is what you are looking for. I remember talking with you about that bug :’D<o:p></o:p></p>
<p class="MsoNormal">But I’m just a beginner >o<<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Kind greetings<o:p></o:p></p>
<p class="MsoNormal">Björn<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b>From:</b> Stefan Gränitz <stefan.graenitz@gmail.com> <br>
<b>Sent:</b> Samstag, 15. Dezember 2018 18:45<br>
<b>To:</b> Gaier, Bjoern <Bjoern.Gaier@horiba.com>; LLVM Developers Mailing List <llvm-dev@lists.llvm.org><br>
<b>Cc:</b> andya@microsoft.com; Zachary Turner <zturner@google.com><br>
<b>Subject:</b> Re: [llvm-dev] Crashes when adding VisualStduio generated object files to the JIT process<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Hi Björn<br>
<br>
I think it would be helpful to provide some more of the assembly you are ending up with. In the example below: Which source code are you compiling? What does it try to achieve? What happens to eax, ebx, rdi after the xor?<br>
<br>
+Andy,Zachary: Maybe you can correct, verify or explain in more detail my analysis in the end of this mail. I have no internal knowledge whatsoever, I just happened to reverse engineer this some time ago.<br>
<br>
>From what I found in the old thread and in your bug report, I can make a guess.<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre>I show you a short snippet of the assembly output.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>mov      eax, DWORD PTR ?myInt@@3HA ; myInt<o:p></o:p></pre>
<pre>lea      rdi, OFFSET FLAT:__ImageBase<o:p></o:p></pre>
<pre>xor      ebx, ebx<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Then these offset is used to jump to some labels like "$<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">LL4 at execute</a>:".<o:p></o:p></pre>
</blockquote>
<p class="MsoNormal" style="margin-bottom:12.0pt">The $LL4 looks like a symbol name in a jump table. There are a few examples (in the context of an unrelated bug report), that show how such tables look like:
<a href="https://gist.github.com/rygorous/6790191">https://gist.github.com/rygorous/6790191</a><br>
<br>
Here is an explanation of LEA: <a href="https://stackoverflow.com/questions/1658294">
https://stackoverflow.com/questions/1658294</a><br>
<br>
<o:p></o:p></p>
<div>
<p class="MsoNormal">Am 22.03.18 um 18:43 schrieb Reid Kleckner via llvm-dev:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">I wouldn't be surprised if JITing COFF files on Windows doesn't work so well, since the object file format assumes most symbols are dllimport or within the local 2GB module address range.<o:p></o:p></p>
</div>
</blockquote>
<p class="MsoNormal">Reid gave an important hint here already: dllimport symbols. MSVC apparently still assumes a small code model and uses 32 bit relocations. (Maybe you could change that nowadays with a compiler flag or so?)<br>
<br>
Have a look at this thread from 2015: <a href="http://lists.llvm.org/pipermail/llvm-dev/2015-November/092727.html">
http://lists.llvm.org/pipermail/llvm-dev/2015-November/092727.html</a><br>
Quoting Andy Ayers: <o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre>If at link time it turns out your export is from a DLL the linker will insert a jump stub / dllimport into the image for you which can handle larger distances.<o:p></o:p></pre>
</blockquote>
<p class="MsoNormal" style="margin-bottom:12.0pt">I guess this is the missing piece in your case. You have to do this patching step yourself, that would otherwise happen magically in the Microsoft linker (resolve relocation to a load-time jump stub) and loader
 (create/fill jump stub with the actual DLL symbol address), because the MSVC compiler assumes it when creating your obj file (creates the relocation for a base address, adds the offset and calls it). If this is the case, you should encounter __imp_foo symbol
 relocations. I don't remember the exact details, but this is what you want to watch out for.<br>
<br>
What needs to happen, conceptually, is something like this: __ImageBase is the address of a table that you create and OFFSET FLAT is a relative offset to the entry that you need to prepare. The entry must NOT store the target addresses (to be loaded and called),
 BUT a jump stub/trampoline like "jmp 0x00000000" (which forwards execution to the actual call target).<br>
<br>
When you resolve the relocation for the import symbol (__imp_foo) to the jump stub, you probably don't know the jmp target right away. So, you can leave it nulled and emit a new relocation to fill out the address (symbol foo). Depending on how your stub works,
 it may be a relative offset or a 64-bit absolute address. In the end your code calls the jump stub address as if it was the actual target function. IIRC you cannot simply resolve the original relocation to the target, even if you know it already, because the
 code emitted by MSVC has the base+offset calculation already built in.<br>
<br>
There are some pretty useful tools for all that in LLVM Orc:<br>
<a href="https://github.com/llvm-mirror/llvm/blob/master/lib/ExecutionEngine/Orc/IndirectionUtils.cpp">https://github.com/llvm-mirror/llvm/blob/master/lib/ExecutionEngine/Orc/IndirectionUtils.cpp</a><br>
<br>
Cheers<br>
Stefan<o:p></o:p></p>
<div>
<p class="MsoNormal">Am 13.12.18 um 07:59 schrieb Gaier, Bjoern via llvm-dev:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal">Hello everyone,<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">I’m using the LLVM for a JIT-Client under Windows 64bit. I tried various stuff with it and noticed, that object files or libraries which are generated by the Visual Studio Compiler 2015 or 2017 will break the jitted code, when they are
 added to the process.<o:p></o:p></p>
<p class="MsoNormal">I opened a bug for this a while ago (<a href="https://bugs.llvm.org/show_bug.cgi?id=39447">https://bugs.llvm.org/show_bug.cgi?id=39447</a>) – sadly this bug will become a stopper for me… So I wanted to ask for advice:<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">Does anyone know anything about this bug or a similar one? Does anyone have an idea about this? Is this bug maybe a duplication with another bug?<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">I’m still a beginner, so I hope I did everything right >o<<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">Kind greetings<o:p></o:p></p>
<p class="MsoNormal">Björn<o:p></o:p></p>
<p class="MsoNormal">Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika
<br>
<br>
<o:p></o:p></p>
<pre>_______________________________________________<o:p></o:p></pre>
<pre>LLVM Developers mailing list<o:p></o:p></pre>
<pre><a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><o:p></o:p></pre>
<pre><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><o:p></o:p></pre>
</blockquote>
<pre>-- <o:p></o:p></pre>
<pre><a href="https://weliveindetail.github.io/blog/">https://weliveindetail.github.io/blog/</a><o:p></o:p></pre>
<pre><a href="https://cryptup.org/pub/stefan.graenitz@gmail.com">https://cryptup.org/pub/stefan.graenitz@gmail.com</a><o:p></o:p></pre>
</div>
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika
</body>
</html>