public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* DLL function importing "bugs"?
@ 2017-01-16 10:59 karol82
  0 siblings, 0 replies; only message in thread
From: karol82 @ 2017-01-16 10:59 UTC (permalink / raw)
  To: gcc

Hi,
I found out some small bugs when function is imported from DLL

Test library:
--------------------------------------------------------------------------------
#include <Windows.h>

__declspec(dllexport)
bool test() {
    return true;
}

extern "C"
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
    return TRUE;
}
--------------------------------------------------------------------------------
Build with:
  g++ -shared -o dlltest.dll dlltest.cpp -Wl,--out-implib,libdlltest.a -O3 \
  -flto -s
And small test program:
--------------------------------------------------------------------------------
#ifdef USE_DLL_IMPORT
__declspec(dllimport)
#endif
bool test();

int main() {
    return test() ? 0 : -1;
}
--------------------------------------------------------------------------------
Build with:
g++ apptest.cpp -o apptest1.exe -O3 -flto -ldlltest -L. -s
g++ apptest.cpp -o apptest2.exe -O3 -flto dlltest.dll -s
g++ apptest.cpp -o apptest3.exe -O3 -flto -ldlltest -L. -DUSE_DLL_IMPORT -s

And now, generated code for calling `test()` function:

For apptest1, apptest2:
  in main:     call    _Z4testv
  in _Z4testv: jmp     cs:__imp__Z4testv
For apptest3:
  in main:        call    cs:_Z4testv
  not-referenced: jmp     cs:_Z4testv
================================================================================
Some other test for __builtin functions that full versions are in MSVCRT.dll:
--------------------------------------------------------------------------------
__declspec(dllimport)
void * memcpy(void * destination, const void * source, size_t num);

void * memcpy_wrapper(size_t num, void * destination, const void * source) {
    return memcpy_wrapper(destination, source, num);
}
--------------------------------------------------------------------------------
In this case memcpy_wrapper uses wrapper (as apptest1 and apptest2 above) for
calling memcpy from MSVCRT.dll. I don't really know if this is a gcc or mingw
problem.
================================================================================
1. LTO should find out that `test()` function is really DLL function and do not
   use wrapper.
2. Optimizer should know that wrapper isn't referenced and remove it.
3. Wrappers shouldn't be used for builtin functions.

I'm not experienced enough to even build gcc by myself so I have no idea how to
fix it. I hope that someone smarter will do this (or tell me if it is
impossible).
================================================================================
I'm using: g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 6.3.0

Sorry for my English, but I'm still learning :)
Regards, Karol Rudnik.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-01-16 10:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-16 10:59 DLL function importing "bugs"? karol82

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).