fortran function calling C function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi everyone,

I have a fortran main program test with a function linked to a C function. Build commands are as follows:
gcc -ggdb3 -c t.c -otc.o -Wall
gfortran -ggdb3 -c t.f -otf.o -Wall
gfortran -Wl,--enable-stdcall-fixup -ggdb3 tf.o tc.o -ot.exe -Wall
Executable is created but crashes when I use following C code (with stdcall)
The same executable runs without problems (apparently) when using C code with cdecl but I don't understand why and suspect it's not a normal behaviour.
I use gcc, gfortran (4.4.0) MinGW (windows vista).

Could you please tell me which gcc options are necessary to make things work properly or if gcc is not involved here what is the right list to post?

Best regards.

PS: I have included disassembled code for fortran and the 2 C versions for completeness but I was not yet able to interpret the difference
(mainly ret instruction is different?)

Fortran code:
------------
     PROGRAM T

IMPLICIT NONE INTEGER KR , KF , KI
C
     DO 21 KI = 1,32
        KR = KF(KI)
21   CONTINUE
     END

C code: (stdcall)
------
#include <stdio.h>
#include <stdlib.h>
#define INTEGER long

INTEGER __stdcall kf_(INTEGER *ki) {
//INTEGER __cdecl kf_(INTEGER *ki) {
   INTEGER kr;
   kr = *ki-1;
   return kr;
}

C code: (cdecl)
------
#include <stdio.h>
#include <stdlib.h>
#define INTEGER long

//INTEGER __stdcall kf_(INTEGER *ki) {
INTEGER __cdecl kf_(INTEGER *ki) {
   INTEGER kr;
   kr = *ki-1;
   return kr;
}

Fortran assembler (gdb disassemble)
-----------------
Dump of assembler code for function t:
0x00401390 <t+0>:    push   %ebp
0x00401391 <t+1>:    mov    %esp,%ebp
0x00401393 <t+3>:    sub    $0x28,%esp
0x00401396 <t+6>:    movl   $0x403080,0x4(%esp)
0x0040139e <t+14>:    movl   $0x8,(%esp)
0x004013a5 <t+21>:    call   0x401434 <_gfortran_set_options>
0x004013aa <t+26>:    movl   $0x1,0xfffffff0(%ebp)
0x004013b1 <t+33>:    mov    0xfffffff0(%ebp),%eax
0x004013b4 <t+36>:    cmp    $0x20,%eax
0x004013b7 <t+39>:    jg     0x4013e1 <t+81>
0x004013b9 <t+41>:    jmp    0x4013bc <t+44>
0x004013bb <t+43>: nop 0x004013bc <t+44>: lea 0xfffffff0(%ebp),%eax
0x004013bf <t+47>:    mov    %eax,(%esp)
0x004013c2 <t+50>:    call   0x4013e4 <kf_>
0x004013c7 <t+55>:    mov    %eax,0xfffffff4(%ebp)
0x004013ca <t+58>:    mov    0xfffffff0(%ebp),%eax
0x004013cd <t+61>:    cmp    $0x20,%eax
0x004013d0 <t+64>:    sete   %al
0x004013d3 <t+67>:    movzbl %al,%eax
0x004013d6 <t+70>:    mov    0xfffffff0(%ebp),%edx
0x004013d9 <t+73>:    inc    %edx
0x004013da <t+74>:    mov    %edx,0xfffffff0(%ebp)
0x004013dd <t+77>:    test   %eax,%eax
0x004013df <t+79>:    je     0x4013bb <t+43>
0x004013e1 <t+81>: leave 0x004013e2 <t+82>: ret End of assembler dump.

C assembler with stdcall:
----------
Dump of assembler code for function kf_:
0x004013e4 <kf_+0>:    push   %ebp
0x004013e5 <kf_+1>:    mov    %esp,%ebp
0x004013e7 <kf_+3>:    sub    $0x10,%esp
0x004013ea <kf_+6>:    mov    0x8(%ebp),%eax
0x004013ed <kf_+9>:    mov    (%eax),%eax
0x004013ef <kf_+11>:    dec    %eax
0x004013f0 <kf_+12>:    mov    %eax,0xfffffffc(%ebp)
0x004013f3 <kf_+15>:    mov    0xfffffffc(%ebp),%eax
0x004013f6 <kf_+18>: leave 0x004013f7 <kf_+19>: ret $0x4
End of assembler dump.

C assembler with cdecl:
------------
Dump of assembler code for function kf_:
0x004013e4 <kf_+0>:    push   %ebp
0x004013e5 <kf_+1>:    mov    %esp,%ebp
0x004013e7 <kf_+3>:    sub    $0x10,%esp
0x004013ea <kf_+6>:    mov    0x8(%ebp),%eax
0x004013ed <kf_+9>:    mov    (%eax),%eax
0x004013ef <kf_+11>:    dec    %eax
0x004013f0 <kf_+12>:    mov    %eax,0xfffffffc(%ebp)
0x004013f3 <kf_+15>:    mov    0xfffffffc(%ebp),%eax
0x004013f6 <kf_+18>: leave 0x004013f7 <kf_+19>: ret End of assembler dump.


--
Jean-François MAUREL
PI Méca
4 rue de l'abreuvoir
92400 Courbevoie France
phone: 33(0)147176513




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux