[llvm-dev] Calling virtual elf functions under windows

via llvm-dev llvm-dev at lists.llvm.org
Wed Feb 7 02:47:03 PST 2018


Hello everyone reading this,

I'm totally confused and need an explanation. I'm also not sure if it's a 
clang or a LLVM subject. 

I'm working with Visual Studio 2015, LLVM 5.0.1 and Windows 7 64bit.

I have a simple project. With 2 Header files and 1 cpp file.
"Interface.h"
This file defines a simple class with two pure virtual member functions.

"SimpleResult.h"
This file defines a struct holding an integer

"CM_Elf.cpp"
This file defines a class (CM_Elf) implementing the interface from 
Interface.h
Also there is an instance of the class (named "elf").

I compile the CM_Elf.cpp to a CM_Elf.bc file via clang-cl but with target 
"x86_64-pc-windows-elf"


In another application I use LLVM to parse the BC file and use the 
ExecutionEngine to get some addresses.
What I want to archive is:
I want to call the implemented member functions of "elf".

1.) I execute all the global constructors collected by the ExecutionEngine
-> The constructor of elf gets called
2.) I use ExecutionEngine to get the address of "elf"
3.) I use the interface to execute Interface::init
-> Init gets called
4.) I use the interface to execute Interface::shutdown
-> CRASH!

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...
When I compile CM_Elf for target "x86_64-pc-windows-msvc19.0.24215" then 
it works.

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.

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.

I append some pictures of the code... I hope someone could help me with 
this.

Kind regards
Björn


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, Hiroshi Kawamura, Takashi Nagano, Takeshi Fukushima.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180207/8dee1a99/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Calling.PNG
Type: image/png
Size: 14041 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180207/8dee1a99/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CM_Elf_cpp.PNG
Type: image/png
Size: 12189 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180207/8dee1a99/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Interface_H.PNG
Type: image/png
Size: 2895 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180207/8dee1a99/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SimpleResult_H.PNG
Type: image/png
Size: 5952 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180207/8dee1a99/attachment-0003.png>


More information about the llvm-dev mailing list