<div dir="ltr">Hi Bjoern,<div><br></div><div>It looks like you are casting a regular function pointer (returned from getFunctionAddress) to a virtual method pointer. This is undefined behavior.</div><div><br></div><div>The easiest way to achieve the effect you want would be to add a trampoline to your source (or at the IR level):</div><div><br></div><div><font face="monospace, monospace">extern "C" SimpleResult call_Interface_init(Interface *Instance) { return Instance->init(); }</font></div><div><br></div><div>Then you can just use:</div><div><br></div><div><font face="monospace, monospace">auto CallInit = (SimpleResult(*)(Interface*))engine->getFunctionAddress("call_Interface_init");</font></div><div><font face="monospace, monospace">CallInit(inter);</font></div><div><br></div><div>Cheers,</div><div>Lang.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 20, 2018 at 1:42 AM, via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><font size="2" face="sans-serif">Still no progress with this problem. Only
that non-member functions seems to be working...</font>
<br><font size="2" face="sans-serif">Also:</font>
<br><font size="2" face="sans-serif">Even when I set the target triple of
the Module (when the file was parsed at runtime) to "COFF" nothing
changes...</font>
<br>
<br>
<br><font size="1" color="#5f5f5f" face="sans-serif">From:
</font><font size="1" face="sans-serif">Bjoern Gaier/HE/HORIBA</font>
<br><font size="1" color="#5f5f5f" face="sans-serif">To:
</font><font size="1" face="sans-serif"><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a></font>
<br><font size="1" color="#5f5f5f" face="sans-serif">Cc:
</font><font size="1" face="sans-serif"><a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a></font>
<br><font size="1" color="#5f5f5f" face="sans-serif">Date:
</font><font size="1" face="sans-serif">08.02.2018 12:28</font>
<br><font size="1" color="#5f5f5f" face="sans-serif">Subject:
</font><font size="1" face="sans-serif">Re: Calling
virtual elf functions under windows -> Adding ASM code</font>
<br>
<hr noshade><div class="HOEnZb"><div class="h5">
<br>
<br><font size="2" face="sans-serif">Hello everyone,</font>
<br>
<br><font size="2" face="sans-serif">I'm sorry for "flooding" your
inbox, but I wanted to extend my message which is shown below. The problem
is still not solved and I'm more confused then before.</font>
<br><font size="2" face="sans-serif">I compiled the code - the one seen in
the pictures to elf bc and to msvc (pecoff) bc. Then I used llc to
generate assembler output.</font>
<br>
<br><font size="2" face="sans-serif">In my opinion the involved functions
are acting exactly the same. There are only some registers swapped. I should
mention: when using the msvc bc code, then the calls via the interface
are working.</font>
<br>
<br><font size="2" face="sans-serif">I attach the assembly code of the involved
code to. Could please anyone explain me the difference, which could cause
the crash?</font>
<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>
<br><font size="1" color="#5f5f5f" face="sans-serif">From:
</font><font size="1" face="sans-serif">Bjoern Gaier/HE/HORIBA</font>
<br><font size="1" color="#5f5f5f" face="sans-serif">To:
</font><font size="1" face="sans-serif"><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>,
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a></font>
<br><font size="1" color="#5f5f5f" face="sans-serif">Date:
</font><font size="1" face="sans-serif">07.02.2018 11:47</font>
<br><font size="1" color="#5f5f5f" face="sans-serif">Subject:
</font><font size="1" face="sans-serif">Calling virtual
elf functions under windows</font>
<br>
<hr noshade>
<br>
<br><font size="2" face="sans-serif">Hello everyone reading this,</font>
<br>
<br><font size="2" face="sans-serif">I'm totally confused and need an explanation.
I'm also not sure if it's a clang or a LLVM subject. </font>
<br>
<br><font size="2" face="sans-serif">I'm working with Visual Studio 2015,
LLVM 5.0.1 and Windows 7 64bit.</font>
<br>
<br><font size="2" face="sans-serif">I have a simple project. With 2 Header
files and 1 cpp file.</font>
<br><font size="2" face="sans-serif">"Interface.h"<br>
This file defines a simple class with two pure virtual member functions.</font>
<br>
<br><font size="2" face="sans-serif">"SimpleResult.h"</font>
<br><font size="2" face="sans-serif">This file defines a struct holding an
integer</font>
<br>
<br><font size="2" face="sans-serif">"CM_Elf.cpp"</font>
<br><font size="2" face="sans-serif">This file defines a class (CM_Elf) implementing
the interface from Interface.h</font>
<br><font size="2" face="sans-serif">Also there is an instance of the class
(named "elf").</font>
<br>
<br><font size="2" face="sans-serif">I compile the CM_Elf.cpp to a CM_Elf.bc
file via clang-cl but with target "x86_64-pc-windows-elf"</font>
<br>
<br>
<br><font size="2" face="sans-serif">In another application I use LLVM to
parse the BC file and use the ExecutionEngine to get some addresses.<br>
What I want to archive is:</font>
<br><font size="2" face="sans-serif">I want to call the implemented member
functions of "elf".</font>
<br>
<br><font size="2" face="sans-serif">1.) I execute all the global constructors
collected by the ExecutionEngine</font>
<br><font size="2" face="sans-serif">-> The constructor of elf gets called</font>
<br><font size="2" face="sans-serif">2.) I use ExecutionEngine to get the
address of "elf"</font>
<br><font size="2" face="sans-serif">3.) I use the interface to execute Interface::init</font>
<br><font size="2" face="sans-serif">-> Init gets called</font>
<br><font size="2" face="sans-serif">4.) I use the interface to execute Interface::shutdown</font>
<br><font size="2" face="sans-serif">-> CRASH!</font>
<br>
<br><font size="2" face="sans-serif">When I swap the calls to init and shutdown,
then everything works. So the virtual call to init seems to corrupt my
stack - maybe it's the return value. Could it be a calling convention problem?
But my target is still "windows" only msvc changed to elf...</font>
<br><font size="2" face="sans-serif">When I compile CM_Elf for target "x86_64-pc-windows-msvc19.0.<wbr>24215"
then it works.</font>
<br>
<br><font size="2" face="sans-serif">When I get the address of CM_Elf::init
and CM_Elf::shutdown via the ExecutionEngine and call them without using
the virtual table, then again everything works.</font>
<br>
<br><font size="2" face="sans-serif">SO! Is it not possible to call virtual
elf functions via there interface? But why does normal functions work?
Also I reach the functions which are stored in the vTable of CM_Elf so
the vTable is correct.</font>
<br>
<br><font size="2" face="sans-serif">I append some pictures of the code...
I hope someone could help me with this.</font>
<br>
<br><font size="2" face="sans-serif">Kind regards</font>
<br><font size="2" face="sans-serif">Björn</font>
<br><font size="2" face="sans-serif"><br>
<br>
</font>
<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: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko
Lampert, Hiroshi Kawamura, Takashi Nagano, Takeshi Fukushima.<br>
<br>
</font>
<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: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko
Lampert, Hiroshi Kawamura, Takashi Nagano, Takeshi Fukushima.<br>
<br>
</font></div></div><br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>