在 2022/10/11 05:20, Martin Storsjö 写道:
On Mon, 10 Oct 2022, Pali Rohár wrote:

Anyway, why exported functions are defined via _CRTIMP? Should not be
also be defined via __MINGW_IMP_SYMBOL/dllimport to avoid generating
unnecessary thunk/wrapper for mingw-w64 runtime code?

I think it's a matter of practical tradeoff - the thunk calls isn't a really big deal here, while having _CRTIMP on declarations and having that expand to an empty string for building the mingw-w64-crt code mostly does the right thing - but I'm not sure. I presume it's possible to shave off a few bytes from a final binary by avoiding that, but it might make things more messy in general.


Typically such thunks exist in import libraries, so first of all, there is no such thunk in the runtime itself.

When calling an imported function that lacks `dllimport`, GCC generates a call to a plain function, which effectively links to the thunk. When calling a `dllimport` function, GCC generates a load from the `__imp_` pointer, followed by an indirect call, so control flow does not go through the thunk.

The latter may appear to be more efficient, but it's actually a tradeoff. Requiring a load + indirect call can cause code to grow by a few bytes on _each_ call site, and sometimes it occupies a (non-volatile) register, which could otherwise be useful.


--
Best regards,
LIU Hao

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to