Runtime check error # 0: using C-export from MinGW dll in VC ++ (or: using MinGW libclang assembly in VC ++ application)

This question is related to the use of C-functions from the MinGW dll in a VC ++ project, which does not work with the following error: Runtime check error # 0 .

I successfully create clang and, more importantly, libclang using MinGW (to have libclang.dll that uses the standard MinGW library).

My application previously used VC ++ - the libclang assembly, which I now want to exchange using the MinGW assembly.

To do this, I created a def file and then an import library from the MinGW dll file:

dlltool -z libclang.def --export-all-symbol libclang.dll dlltool -d libclang.def -l libclang.lib 

Before creating the import library, I changed the def file, so it contains only the important clang functions that were declared using extern "C" . Here is a short excerpt:

 LIBRARY libclang.dll EXPORTS clang_CXCursorSet_contains @ 50006 clang_CXCursorSet_insert @ 50007 clang_CXXMethod_isStatic @ 50008 clang_CXXMethod_isVirtual @ 50009 clang_Cursor_getTranslationUnit @ 50010 

Using the MinGW dll and the new import library, I can now successfully compile my application. It works, and I can actually use some functions such as "clang_createIndex", but whenever I get to "clang_getTranslationUnitCursor", I get:

Runtime Check Error # 0 - ESP value was not properly stored during function call. This is usually the result of calling a function declared with one call, with a function pointer declared with another calling convention.

Functions inside Clang Index.h (and therefore beyond my control) are declared as follows:

 #ifdef _MSC_VER #ifdef _CINDEX_LIB_ #define CINDEX_LINKAGE __declspec(dllexport) #else #define CINDEX_LINKAGE __declspec(dllimport) #endif #else #define CINDEX_LINKAGE #endif CINDEX_LINKAGE CXCursor clang_getTranslationUnitCursor(CXTranslationUnit); 

In fact, I have no idea why it works for some functions and not for others!

Thanks a lot!

[Update]

For build enthusiasts, here is an example build that crashes. The clang_getNumDiagnostics call works, the clang_getTranslationUnitCursor call fails on the last line when __RTC_CheckEsp is called - which is a function that validates the ESP

 // call to clang_getNumDiagnostics(TU); - works! 5AF3EFAB mov esi,esp 5AF3EFAD mov eax,dword ptr [ebp-30h] 5AF3EFB0 push eax 5AF3EFB1 call dword ptr [__imp__clang_getNumDiagnostics (5AF977E0h)] 5AF3EFB7 add esp,4 5AF3EFBA cmp esi,esp 5AF3EFBC call @ILT+7135(__RTC_CheckEsp) (5AF16BE4h) // call to clang_getTranslationUnitCursor(TU); - fails! 5AF3EFC1 mov esi,esp 5AF3EFC3 mov eax,dword ptr [ebp-30h] 5AF3EFC6 push eax 5AF3EFC7 lea ecx,[ebp-234h] 5AF3EFCD push ecx 5AF3EFCE call dword ptr [__imp__clang_getTranslationUnitCursor (5AF9780Ch)] 5AF3EFD4 add esp,8 5AF3EFD7 cmp esi,esp 5AF3EFD9 call @ILT+7135(__RTC_CheckEsp) (5AF16BE4h) 

When clang_getTranslationUnitCursor is called, esp will be increased by 4. The big question is: for both function calls that take the same parameter, why does it "add esp, 4" after calling clang_getNumDiagnostics, but "add esp, 8" when calling clang_getTranslationUnitCursor? ?

0
source share

Source: https://habr.com/ru/post/1396253/


All Articles