Jan Kratochvil
Projects Products GIT Resume Contact
Projects
UNIX UNIX-devel Web Amiga MS-Windows MS-DOS Patches
Captive: The first free NTFS read/write filesystem for GNU/Linux

 

Previous document Parent Next document

API Function Calling Conventions

Standard UNIX code compiled by GCC (GNU C Compiler) running on host GNU/Linux always uses cdecl ABI (Application Binary Interface) calling convention. This calling convention is also the default declaration type of UNIX functions.

W32 uses three different calling conventions in its ABI. They are all described in the Microsoft documentation. There is always necessary to have the proper function declaration (prototype) in the caller scope to prevent all sorts of unexpected crashes.

Unfortunately some non-matching combinations of calling conventions result in hard to debug bugs: the caller gets back an unexpected stack pointer from the callee and upon return it will restore registers from the wrong stack pointer place. Since the caller will finally reclaim its stack frame from its (uncorrupted) EBP stack frame pointer the caller will return to the caller of the caller correctly. Just the registers remain corrupted causing crashes of completely unrelated code executed far, far away...

EDI, ESI and EBX registers are always saved on the stack. They are stored on the stack in this particular order from bottom to top addresses (using the push EBX, push ESI, push EDI sequence). Fortunately GNU/Linux GCC has the same register saving behaviour. If some register corruption occurs the calling type presented between the caller and callee should be checked.

W32 Calling Convention "cdecl"

The only calling convention in the UNIX world. The default one for all the compilers. All the arguments are passed on the stack, no arguments are cleaned by the callee. Possible inconsistencies in the number of function arguments with the function prototype used by the caller is harmless. Variable arguments lists can be passed by this convention.

W32 Calling Convention cdecl Scheme
W32 Calling Convention cdecl Scheme

 

Calling Convention cdecl Characteristics
Arguments freed by caller
Arguments on the stack #0 ... #(n-1)
Arguments in the registers none
GCC attribute __attribute__((__cdecl__)) (default)

W32 Calling Convention "stdcall"

W32 Calling Convention stdcall Scheme
W32 Calling Convention stdcall Scheme

 

Convention never used in the UNIX world. It needs to be specified for W32 compilers. All the arguments are passed on the stack, all the arguments are cleaned by the callee. Possible inconsistencies in the number of function arguments with the function prototype used by the caller will result in fatal crash. Variable arguments lists cannot be passed by this convention – use cdecl instead.

Calling Convention stdcall Characteristics
Arguments freed by callee
Arguments on the stack #0 ... #(n-1)
Arguments in the registers none
GCC attribute __attribute__((__stdcall__))

W32 Calling Convention "fastcall"

Convention never used in the UNIX world. It needs to be specified for W32 compilers. Convention used in the W32 world for its low calling overhead. All but the first two arguments are passed on the stack, such arguments are cleaned by the callee. First two arguments are passed in the registers ECX and EDX respectively. Possible inconsistencies in the number of function arguments with the function prototype used by the caller will result in fatal crash. Variable arguments lists cannot be passed by this convention – use cdecl instead.

GCC (GNU C Compiler) native support for this calling convention is pretty fresh and it is currently present only in the recent CVS versions since 21st December of 2002 which should get released as GCC version 3.4. This project solved the unsupported calling convention by declaration of arguments passed in registers by __attribute__((__regparm__(3))). W32 passes the arguments in registers in the order ECX, EDX but GCC passes them in registers EAX, EDX, ECX. This incompatibility is compensated at C source level in the relaying code generated by captivesym relay generator.

W32 Calling Convention fastcall Scheme
W32 Calling Convention fastcall Scheme

 

Calling Convention fastcall Characteristics
Arguments freed by callee
Arguments on the stack #2 ... #(n-1)
Arguments in the registers ECX=#0, EDX=#1
GCC ≥3.4 attribute __attribute__((__fastcall__))
GCC <3.4 attr. emulation__attribute__((__stdcall__))
__attribute__((__regparm__(3) /* EAX,EDX,ECX */))

 

 

Previous document Next document

EOF