This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c++/8706: gcc cannot generate __cdel functions that can be called by Visual C if that function returns >8 byte strucutres by value


>Number:         8706
>Category:       c++
>Synopsis:       gcc cannot generate __cdel functions that can be called by Visual C if that function returns >8 byte strucutres by value
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Nov 25 15:36:02 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     cheng@powertv.com
>Release:        2.95.3-5 and 3.1
>Organization:
>Environment:
- HP Vectra 750mhz. Pentium III PC with 256 MB of memory running Windows 2000 Professional.
- Cygwin 1.3.12 DLL with gcc 2.95.3-5 (and also tried with gcc 3.1 with same result).
>Description:
I've narrowed down the bug to a simple case.  If a function returns a structure that has a size > 8 bytes, the GCC and Microsoft Visual C compilers create functions that are not compatible.  They work just fine on their own, but if you try calling one from the other, it would corrupt the stack.  Is this a known issue or there is a workaround?

If you compile the following test code:

--test.c begin--
typedef struct { int bar,x,y; } Foo;

Foo ret_test(void)
{
	Foo ret;
	ret.bar = 0;
	return ret;
} 

int main(char* argc, int argv)
{
	Foo ret = ret_test();
	return ret.bar;
}
--test.c end--

You get the following from GCC:

--gcc output begin--
Dump of file test.o by objdump

File Type: COFF OBJECT

_ret_test:
  00000000: 55                 push        ebp
  00000001: 89 E5              mov         ebp,esp
  00000003: 83 EC 18           sub         esp,18h
  00000006: 8B 45 08           mov         eax,dword ptr [ebp+8]
  00000009: C7 45 F0 00 00 00  mov         dword ptr [ebp-10h],offset _ret_test
            00
  00000010: 8B 55 F0           mov         edx,dword ptr [ebp-10h]
  00000013: 89 10              mov         dword ptr [eax],edx
  00000015: 8B 55 F4           mov         edx,dword ptr [ebp-0Ch]
  00000018: 89 50 04           mov         dword ptr [eax+4],edx
  0000001B: 8B 55 F8           mov         edx,dword ptr [ebp-8]
  0000001E: 89 50 08           mov         dword ptr [eax+8],edx
  00000021: EB 00              jmp         00000023
  00000023: 89 C0              mov         eax,eax
  00000025: 89 EC              mov         esp,ebp
  00000027: 5D                 pop         ebp
  00000028: C2 04 00           ret         4
  0000002B: 90                 nop
_main:
  0000002C: 55                 push        ebp
  0000002D: 89 E5              mov         ebp,esp
  0000002F: 83 EC 18           sub         esp,18h
  00000032: E8 00 00 00 00     call        00000037
  00000037: 8D 45 F0           lea         eax,[ebp-10h]
  0000003A: 83 C4 F4           add         esp,0FFFFFFF4h
  0000003D: 50                 push        eax
  0000003E: E8 BD FF FF FF     call        _ret_test
  00000043: 83 C4 0C           add         esp,0Ch
  00000046: 8B 55 F0           mov         edx,dword ptr [ebp-10h]
  00000049: 89 D0              mov         eax,edx
  0000004B: EB 03              jmp         00000050
  0000004D: 8D 76 00           lea         esi,[esi]
  00000050: 89 EC              mov         esp,ebp
  00000052: 5D                 pop         ebp
  00000053: C3                 ret

  Summary

           0 .bss
           0 .data
          54 .text
--gcc output end--

And this from VC:

--vc output start--
Dump of file test.obj by objdump

File Type: COFF OBJECT

_ret_test:
  00000000: 55                 push        ebp
  00000001: 8B EC              mov         ebp,esp
  00000003: 83 EC 0C           sub         esp,0Ch
  00000006: C7 45 F4 00 00 00  mov         dword ptr [ebp-0Ch],offset _ret_test
            00
  0000000D: 8B 45 08           mov         eax,dword ptr [ebp+8]
  00000010: 8B 4D F4           mov         ecx,dword ptr [ebp-0Ch]
  00000013: 89 08              mov         dword ptr [eax],ecx
  00000015: 8B 55 F8           mov         edx,dword ptr [ebp-8]
  00000018: 89 50 04           mov         dword ptr [eax+4],edx
  0000001B: 8B 4D FC           mov         ecx,dword ptr [ebp-4]
  0000001E: 89 48 08           mov         dword ptr [eax+8],ecx
  00000021: 8B 45 08           mov         eax,dword ptr [ebp+8]
  00000024: 8B E5              mov         esp,ebp
  00000026: 5D                 pop         ebp
  00000027: C3                 ret
_main:
  00000028: 55                 push        ebp
  00000029: 8B EC              mov         ebp,esp
  0000002B: 83 EC 18           sub         esp,18h
  0000002E: 8D 45 E8           lea         eax,[ebp-18h]
  00000031: 50                 push        eax
  00000032: E8 00 00 00 00     call        00000037
  00000037: 83 C4 04           add         esp,4
  0000003A: 8B 08              mov         ecx,dword ptr [eax]
  0000003C: 89 4D F4           mov         dword ptr [ebp-0Ch],ecx
  0000003F: 8B 50 04           mov         edx,dword ptr [eax+4]
  00000042: 89 55 F8           mov         dword ptr [ebp-8],edx
  00000045: 8B 40 08           mov         eax,dword ptr [eax+8]
  00000048: 89 45 FC           mov         dword ptr [ebp-4],eax
  0000004B: 8B 45 F4           mov         eax,dword ptr [ebp-0Ch]
  0000004E: 8B E5              mov         esp,ebp
  00000050: 5D                 pop         ebp
  00000051: C3                 ret

  Summary

          26 .drectve
          52 .text
--vc output end--
>How-To-Repeat:
Try to compile the test program above under GCC and Visual C and compare the way the stack is handled in each.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]