Menu

#489 Including Windows.h makes compiler to ignore cdecl attribute of a function.

v1.0 (example)
open
nobody
None
5
2015-07-15
2015-07-15
Marcos Diaz
No

Hi,
In Windows 7,

gcc:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/Program\ Files\ (x86)/mingw-w64/i686-4.8.2-win32-dwarf-rt
_v3-rev4/mingw32/bin/../libexec/gcc/i686-w64-mingw32/4.8.2/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: ../../../src/gcc-4.8.2/configure --host=i686-w64-mingw32 --buil
d=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/c
/mingw482/i686-482-win32-dwarf-rt_v3-rev4/mingw32 --with-gxx-include-dir=/mingw3
2/i686-w64-mingw32/include/c++ --enable-shared --enable-static --disable-multili
b --enable-languages=ada,c,c++,fortran,objc,obj-c++,lto --enable-libstdcxx-time=
yes --enable-threads=win32 --enable-libgomp --enable-libatomic --enable-lto --en
able-graphite --enable-checking=release --enable-fully-dynamic-string --enable-v
ersion-specific-runtime-libs --disable-sjlj-exceptions --with-dwarf2 --disable-i
sl-version-check --disable-cloog-version-check --disable-libstdcxx-pch --disable
-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --d
isable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with
-arch=i686 --with-tune=generic --with-libiconv --with-system-zlib --with-gmp=/c/
mingw482/prerequisites/i686-w64-mingw32-static --with-mpfr=/c/mingw482/prerequis
ites/i686-w64-mingw32-static --with-mpc=/c/mingw482/prerequisites/i686-w64-mingw
32-static --with-isl=/c/mingw482/prerequisites/i686-w64-mingw32-static --with-cl
oog=/c/mingw482/prerequisites/i686-w64-mingw32-static --enable-cloog-backend=isl
 --with-pkgversion='i686-win32-dwarf-rev4, Built by MinGW-W64 project' --with-bu
gurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw482/i
686-482-win32-dwarf-rt_v3-rev4/mingw32/opt/include -I/c/mingw482/prerequisites/i
686-zlib-static/include -I/c/mingw482/prerequisites/i686-w64-mingw32-static/incl
ude' CXXFLAGS='-O2 -pipe -I/c/mingw482/i686-482-win32-dwarf-rt_v3-rev4/mingw32/o
pt/include -I/c/mingw482/prerequisites/i686-zlib-static/include -I/c/mingw482/pr
erequisites/i686-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/c/mingw
482/i686-482-win32-dwarf-rt_v3-rev4/mingw32/opt/lib -L/c/mingw482/prerequisites/
i686-zlib-static/lib -L/c/mingw482/prerequisites/i686-w64-mingw32-static/lib -Wl
,--large-address-aware'
Thread model: win32
gcc version 4.8.2 (i686-win32-dwarf-rev4, Built by MinGW-W64 project)

GNU ld (GNU Binutils) 2.24

mingw32.

Having this example code:

#include <Windows.h>

class Test
{
    __attribute__((cdecl)) void functioncdecl();
    void functionno();
    int i;
};

void Test::functioncdecl()
{
    i++;
}

void Test::functionno()
{
    i++;
};

compiling with:

g++ -S pruebacdecl.cpp

I can see the assembler generated for functioncdecl, that is not following the cdecl attribute:

__ZN4Test13functioncdeclEv:
LFB473:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $4, %esp
    movl    %ecx, -4(%ebp)
    movl    -4(%ebp), %eax
    movl    (%eax), %eax
    leal    1(%eax), %edx
    movl    -4(%ebp), %eax
    movl    %edx, (%eax)
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE473:
    .align 2
    .globl  __ZN4Test10functionnoEv
    .def    __ZN4Test10functionnoEv;    .scl    2;  .type   32; .endef
__ZN4Test10functionnoEv:
LFB474:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $4, %esp
    movl    %ecx, -4(%ebp)
    movl    -4(%ebp), %eax
    movl    (%eax), %eax
    leal    1(%eax), %edx
    movl    -4(%ebp), %eax
    movl    %edx, (%eax)
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret

As we can see both functions remains the same,
But, if I comment the #include <windows.h> line, The output assembly is correct:</windows.h>

__ZN4Test13functioncdeclEv:
LFB0:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    movl    8(%ebp), %eax
    movl    (%eax), %eax
    leal    1(%eax), %edx
    movl    8(%ebp), %eax
    movl    %edx, (%eax)
    popl    %ebp
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc

(Note that now follows the cdecl attribute)
So, it is ignoring the cdecl attribute, just by including Windows.h, and no warning is emitted.

Discussion


Log in to post a comment.

Monday.com Logo