I'm using gcc on Linux x86. My program exports a pointer to a C function to LLVM JIT functions. The calling convention is cdecl. It runs well on MingW on Windows. But strange things happens on linux x86 platform. The disassembly of the exported C function is like this :
push ebp
mov ebp,esp
push ebx
sub esp,0x34
mov eax,0xfffffffc
mov eax,DWORD PTR gs:[eax]
mov eax,DWORD PTR [eax+0x1c]
mov eax,DWORD PTR [eax]
mov eax,DWORD PTR [eax+0x28]
mov edx,DWORD PTR [ebp+0xc]
shl edx,0x4
add eax,edx
mov DWORD PTR [ebp-0xc],eax
mov edx,DWORD PTR ds:0x8e49940
mov ebx,DWORD PTR [ebp+0x8]
lea eax,[ebp-0x20]
mov ecx,DWORD PTR [ebp-0xc]
mov DWORD PTR [esp+0xc],ecx
mov ecx,DWORD PTR [ebp+0x10]
mov DWORD PTR [esp+0x8],ecx
mov DWORD PTR [esp+0x4],edx
mov DWORD PTR [esp],eax
call 0x8090f6f <SoCreateArray(DVM_VirtualMachine_tag*, int, DVM_TypeSpecifier_tag*)>
sub esp,0x4
mov eax,DWORD PTR [ebp-0x20]
mov edx,DWORD PTR [ebp-0x1c]
mov DWORD PTR [ebx],eax
mov DWORD PTR [ebx+0x4],edx
mov eax,DWORD PTR [ebp+0x8]
mov ebx,DWORD PTR [ebp-0x4]
leave
ret 0x4
And the C source code is here:
DVM_ObjectRef SoNewArray(BINT ty,BINT dim)
{
DVM_TypeSpecifier *type
= &curthread->current_executable->executable->type_specifier[ty];
DVM_ObjectRef barray;
barray = SoCreateArray(curdvm, dim, type);
return barray;
}
Notice that the final instruction of the disassembly code is "ret 0x4", which means the function it self cleans the stack and it is not a cdecl function! What's more, even if I declare the C function like this:
DVM_ObjectRef SoNewArray(BINT ty,BINT dim) attribute((cdecl));
the resule is the same. Maybe GCC optimizes my code, and automatically use stdcall, ignoring the calling convention?
My GCC command is
gcc -Wall -fexceptions -Wfatal-errors -g
__cdecl
? Still unanswered. – Oscar