简体   繁体   中英

Why doesn't __declspec(dllimport) work with memcpy() in Visual C++? How do I make it work?

I've noticed that __declspec(dllimport) doesn't work with memcpy in Visual C++.
ie, the linker introduces a jmp thunk , regardless of the dllimport , so that instead of

    call    QWORD PTR __imp_memcpy

I get

    call    memcpy

What's the reason for this behavior? How do I remove the jump overhead for memcpy ?

This appears to occur when the translation unit contains a function declaration that is not dllimport ed. However, that includes implicit declarations by the compiler under /Oi (which is implied by /O2 ).

Therefore, avoiding this problem requires doing two things:

  1. Modifying all declarations of memcpy to be __declspec(dllimport) .
    (This might require macro hackery via #define memcpy bad_memcpy or such.)

  2. Compiling with /Oi- to prevent the internal declaration from being introduced.

Unfortunately, this is an unsatisfactory workaround as it appears to inhibit compiler optimizations for memset ... and I don't know of any way to work around that simultaneously. Does anyone else know how to avoid that problem as well?

Also note that disabling intrinsics in the project properties in the IDE may not be sufficient, as Visual C++ doesn't necessarily emit /Oi- as a result of that if it thinks the option was already disabled. (That also seems like a bug.)

And indeed, we see the issue is fixed when we do this :

   call    QWORD PTR __imp_memcpy

I would think this is a bug though... the internal declaration by the compiler shouldn't override the user-specified one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM