NO

Author Topic: MSVC Binary Compatibility  (Read 6137 times)

iZzz32

  • Guest
MSVC Binary Compatibility
« on: July 15, 2007, 01:28:39 PM »
Pelle, is it possible to make Pelles C binary compatible with MSVC (in -Ze mode)? I'm using Pelles C to write a module for MSVC6 application and found some incompatibilities (structures, __fastcall functions; I could explain details if you want).

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: MSVC Binary Compatibility
« Reply #1 on: July 15, 2007, 10:30:16 PM »
Give me an explanation first, and we will see...
/Pelle

iZzz32

  • Guest
Re: MSVC Binary Compatibility
« Reply #2 on: July 16, 2007, 11:04:36 AM »
OK. Pelles C fastcall incompatibilities. Disclaimer: tested with MSVC6 only.

1. Incorrect order of arguments, when non-integer arguments exist.
When calling fastcall function, MSVC searches argument list from left to right for 32-bit (16-bit, 8-bit) integers or pointers, and places the first such argument in ECX (CX, CL), the second in EDX (DX, DL), then it copies remaining arguments to stack from right to left. If called function returns non 8-bit/16-bit/32-bit/64-bit structure, MSVC then PUSHes an address of result buffer (presult in the attached source code). After that MSVC calls function.

2. Passing presult in register.
Pelles C sometimes passes pointer to buffer for returned structure in ECX or EDX. In MSVC it is always the first stack argument (pushed last). Also Pelles C has a bug in __fastcall functions that return structures (see attached example).

3. When returning structures, Pelles C doesn't return pointer-to-result in EAX, but MSVC always does.
The whole process of returning non 8-bit/16-bit/32-bit/64-bit structure in MSVC (you don't need to copy this behavior, just return a pointer in EAX): calling function allocates temporary buffer for result, passes pointer to it as the first STACK argument of a function, function fills that buffer and returns pointer to it in EAX; if calling function wants to store “returned” structure to a variable, it copies structure from the buffer pointed by EAX to that variable.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: MSVC Binary Compatibility
« Reply #3 on: July 21, 2007, 04:50:36 PM »
I will try to fix the fastcall convention, but if it means too much work I rather drop this feature since this convention is pretty useless anyway...
/Pelle

iZzz32

  • Guest
Re: MSVC Binary Compatibility
« Reply #4 on: July 23, 2007, 10:30:06 AM »
Ok. Note that (3) is for __stdcall also.
« Last Edit: July 23, 2007, 06:48:40 PM by iZzz32 »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: MSVC Binary Compatibility
« Reply #5 on: July 23, 2007, 04:49:39 PM »
You mean presult returned in eax?! Yes, it's returned for _stdcall and _cdecl too - but the compiler couldn't care less, so it can only be of interest to assembly programmers. Is this officially documented (I can't find it)? If it's not, it seems like a useless thing to add (and trust to be there in the future)...
/Pelle

iZzz32

  • Guest
Re: MSVC Binary Compatibility
« Reply #6 on: July 23, 2007, 06:39:58 PM »
it can only be of interest to assembly programmers
I'm trying to use DLL compiled with Pelles C in the MSVC application. When the application calls a function, that returns structure and then uses EAX to get result, it crashes. If Pelles C has -Ze option why not to make such small changes? It will not take much time.

Quote
Is this officially documented
As I know, Microsoft never documented calling conventions.
As I know, Microsoft never fully documented calling conventions. :-)
« Last Edit: July 23, 2007, 08:24:26 PM by iZzz32 »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: MSVC Binary Compatibility
« Reply #7 on: July 23, 2007, 07:23:00 PM »
OK, good enough reason. Returning structures should be fairly uncommon anyway...
/Pelle

iZzz32

  • Guest
Re: MSVC Binary Compatibility
« Reply #8 on: July 23, 2007, 08:22:04 PM »
I searched in the MSDN Library that was installed with Visual Studio 2005.

C++ Language Reference: Argument Passing and Naming Conventions says:
“…Larger structures are returned in the EAX register as pointers to hidden return structures…”

There is also MSKB100832, it says nothing about structures, but it says the following about (1):
“…The first two function arguments that require four or fewer bytes are placed into registers. The caller pushes the remainder of the parameters onto the stack from right to left. This behavior may change in future versions.”

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: MSVC Binary Compatibility
« Reply #9 on: July 24, 2007, 12:16:09 AM »
OK, thanks. I will return the pointer in EAX for now - this is obviously what the MSVC 2005 compiler does, so...
/Pelle