I have been doing some x86 programming in Windows with NASM and I have run into some confusion. I am confused as to why I must do this:
extern _ExitProcess@4
Specifically I am confused about the ‘_’ and the ‘@4’. I know that the ‘@4’ is the size of the stack but why is it needed? When I looked in the kernel32.dll with a hex editor I only saw ‘ExitProcess’ not ‘_ExitProcess@4’.
I am also confused as to why C Functions do not need the underscore and the stack size such as this:
extern printf
Why don’t C Functions need decorations?
My third question is “Is this the way I should be using these functions?” Right now I am linking with the actual dll files themselves.
To enable the linker to report a fatal error if your compiler assumed the wrong calling convention for the function (this can happen if you forget to include header files in C and ignore all the compiler warnings or if a declaration doesn’t exactly match the function in the shared library).
Functions that use the
cdeclcalling convention are decorated with a single leading (so it would actually be_printf).The reason why no parameter size is encoded into the decorated name is that the caller is responsible for both setting up and tearing down the stack, so an argument count mismatch will not be fatal for the stack setup (though the calling function might still crash if it isn’t given the right arguments, of course). It might even be possible that the argument count is variable, like in the case of
printf.The mangled names are usually mapped to the actual exported names of the DLL using definition files (
*.def), which then get compiled to*.libimport library files that can be used in your linker invocation. An example of such a definition file forkernel32.dllis this one. The following line defines the mapping forExitProcess:I don’t know NASM very well, but the code I’ve seen so far usually specifies the decorated name, like in your example.
You can find more information on this excellent page about Win32 calling conventions.