MinGW 4.7.0-compiled Clang Mis-compiles Simple Programs

When using MinGW 4.7.0 to compile Clang on Windows, the resulting Clang binary does not correctly compile simple C++ code. This is with ToT LLVM/Clang.

Clang build:
$ cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_EXTERNAL_CLANG_SOURCE_DIR= -G Ninja
$ ninja

After building Clang, I tried to compile the following program:

#include
#include

int main(int argc, char** argv) {
std::string Name(“me”);

std::cout << Name << “\n”;

return 0;
}

with

$ bin\clang++.exe test1.cpp -O0 -o test1.exe -v

The resulting executable immediately seg faults in the CRT start-up routines:

Program received signal SIGSEGV, Segmentation fault.
0x6fc65abb in libstdc+±6!_ZNKSs4sizeEv () from c:\MinGW\bin\libstdc+±6.dll
(gdb) bt
#0 0x6fc65abb in libstdc+±6!_ZNKSs4sizeEv () from c:\MinGW\bin\libstdc+±6.dll
#1 0x6fcba6f1 in libstdc+±6!_ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKSbIS4_S5_T1_E ()
from c:\MinGW\bin\libstdc+±6.dll
#2 0x00401453 in _fu0___ZSt4cout ()
#3 0x004010b9 in __mingw_CRTStartup () at …/mingw/crt1.c:244
#4 0x00401284 in mainCRTStartup () at …/mingw/crt1.c:264

Any idea what is going on here? I’m cross-posting to llvm-dev since I’m not sure if this is a Clang or LLVM codegen issue.

Clang verbose output:

clang version 3.2
Target: i686-pc-mingw32
Thread model: posix
“C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin/clang++.exe” -cc1 -triple i686-pc-mingw32 -S -disable-free -main-file-name test1.cpp -mrelocation-model static -mdisable-fp-elim -fmath-errno -mconstructor-aliases -target-cpu pentium4 -momit-leaf-frame-pointer -v -resource-dir “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin\…\lib\clang\3.2” -fmodule-cache-path “C:\Users\JHOLEW~1\AppData\Local\Temp\clang-module-cache” -O0 -fdeprecated-macro -fno-dwarf-directory-asm -ferror-limit 19 -fmessage-length 129 -mstackrealign -fno-use-cxa-atexit -fgnu-runtime -fobjc-runtime-has-arc -fobjc-runtime-has-weak
-fobjc-fragile-abi -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017384.s -x c++ “…\clang-x86-self\test1.cpp”
clang -cc1 version 3.2 based upon LLVM 3.2svn default target i686-pc-mingw32
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.0”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.0/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.0/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.0/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.1”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.1/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.1/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.1/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.2”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.2/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.2/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.2/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.3”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.3/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.3/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.3/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.4”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.4/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.4/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.5.4/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.0”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.0/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.0/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.0/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.1”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.1/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.1/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.1/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.2”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.2/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.2/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.2/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.3”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.3/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.3/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.6.3/backward”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.7.0”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.7.0/x86_64-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.7.0/i686-w64-mingw32”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin…\lib\clang\3.2/…/…/…/include/c++/4.7.0/backward”
ignoring nonexistent directory “/mingw/lib/gcc/mingw32/4.5.2/include/c++”
ignoring nonexistent directory “/mingw/lib/gcc/mingw32/4.5.2/include/c++/mingw32”
ignoring nonexistent directory “/mingw/lib/gcc/mingw32/4.5.2/include/c++/backward”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.3/include/c++”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.3/include/c++/mingw32”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.3/include/c++/backward”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.2/include/c++”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.2/include/c++/mingw32”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.2/include/c++/backward”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.1/include/c++”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.1/include/c++/mingw32”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.6.1/include/c++/backward”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.5.2/include/c++”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.5.2/include/c++/mingw32”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.5.2/include/c++/backward”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.5.0/include/c++”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.5.0/include/c++/mingw32”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.5.0/include/c++/backward”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.4.0/include/c++”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.4.0/include/c++/mingw32”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.4.0/include/c++/backward”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.3.0/include/c++”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.3.0/include/c++/mingw32”
ignoring nonexistent directory “c:/MinGW/lib/gcc/mingw32/4.3.0/include/c++/backward”
ignoring nonexistent directory “/usr/local/include”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin/…/lib/clang/3.2/…/…/…/i686-w64-mingw32/include”
ignoring nonexistent directory “C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin/…/lib/clang/3.2/…/…/…/x86_64-w64-mingw32/include”
ignoring nonexistent directory “/usr/include”
#include “…” search starts here:
#include <…> search starts here:
c:/MinGW/lib/gcc/mingw32/4.7.0/include/c++
c:/MinGW/lib/gcc/mingw32/4.7.0/include/c++/mingw32
c:/MinGW/lib/gcc/mingw32/4.7.0/include/c++/backward
C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin/…/lib/clang/3.2/include
C:/Users/jholewinski/projects/llvm/build/clang-x86-mingw32/bin/…/lib/clang/3.2/…/…/…/include
/mingw/include
c:/mingw/include
End of search list.
“c:/MinGW/bin/g++.exe” -O0 -v -c -o C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017385.o -x assembler C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017384.s
Using built-in specs.
COLLECT_GCC=c:/MinGW/bin/g++.exe
Target: mingw32
Configured with: …/gcc-4.7.0/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1-with-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.7.0 (GCC)
COLLECT_GCC_OPTIONS=’-O0’ ‘-v’ ‘-c’ ‘-o’ ‘C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017385.o’ ‘-shared-libgcc’ ‘-mtune=i386’ ‘-march=i386’
c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/…/mingw32/bin/as.exe -o C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017385.o C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017384.s
COMPILER_PATH=c:/mingw/bin/…/libexec/gcc/mingw32/4.7.0/;c:/mingw/bin/…/libexec/gcc/;c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/…/mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/;c:/mingw/bin/…/lib/gcc/;c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/…/mingw32/lib/;c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/;/mingw/lib/
COLLECT_GCC_OPTIONS=’-O0’ ‘-v’ ‘-c’ ‘-o’ ‘C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017385.o’ ‘-shared-libgcc’ ‘-mtune=i386’ ‘-march=i386’
“c:/MinGW/bin/g++.exe” -O0 -v -o test1.exe C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017385.o
Using built-in specs.
COLLECT_GCC=c:/MinGW/bin/g++.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/…/libexec/gcc/mingw32/4.7.0/lto-wrapper.exe
Target: mingw32
Configured with: …/gcc-4.7.0/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1-with-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.7.0 (GCC)
COMPILER_PATH=c:/mingw/bin/…/libexec/gcc/mingw32/4.7.0/;c:/mingw/bin/…/libexec/gcc/;c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/…/mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/;c:/mingw/bin/…/lib/gcc/;c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/…/mingw32/lib/;c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/;/mingw/lib/
COLLECT_GCC_OPTIONS=’-O0’ ‘-v’ ‘-o’ ‘test1.exe’ ‘-shared-libgcc’ ‘-mtune=i386’ ‘-march=i386’
c:/mingw/bin/…/libexec/gcc/mingw32/4.7.0/collect2.exe -Bdynamic -u ___register_frame_info -u ___deregister_frame_info -o test1.exe c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/crt2.o c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/crtbegin.o -Lc:/mingw/bin/…/lib/gcc/mingw32/4.7.0 -Lc:/mingw/bin/…/lib/gcc -Lc:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/…/…/mingw32/lib -Lc:/mingw/bin/…/lib/gcc/mingw32/4.7.0/…/…/… -L/mingw/lib C:/Users/JHOLEW~1/AppData/Local/Temp/test1-017385.o -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/…/lib/gcc/mingw32/4.7.0/crtend.o

Is this the first time that you've tried 4.7? I saw this thread some
time ago, but it isn't very helpful :wink:

http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-April/021027.html

Under Debian, I had to force the build of clang with gcc 4.6 because gcc 4.7 miscompiles clang

See:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=675056

S

Well, this is not relevant here - it was subtle clang bug which was
triggered by gcc 4.7 only. It was fixed on 3.1 release / mainline, so
all other bugs should be reported to LLVM bugzilla.

Is this the first time that you’ve tried 4.7? I saw this thread some
time ago, but it isn’t very helpful :wink:

http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-April/021027.html

Under Debian, I had to force the build of clang with gcc 4.6 because
gcc 4.7 miscompiles clang

See:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=675056

Well, this is not relevant here - it was subtle clang bug which was
triggered by gcc 4.7 only. It was fixed on 3.1 release / mainline, so
all other bugs should be reported to LLVM bugzilla.

I’m not sure this bug applies here, since it’s the program generated by Clang that is crashing, not Clang itself. This leads me to believe it is a codegen issue somewhere within Clang and not a mis-compile of Clang itself. Or perhaps its a subtle bug in MinGW’s libstdc++ 4.7.0 build.

Everything does work fine if I downgrade to MinGW 4.6.3.

I'm not sure this bug applies here, since it's the program generated *by*
Clang that is crashing, not Clang itself. This leads me to believe it is a
codegen issue somewhere within Clang and not a mis-compile of Clang itself.
Or perhaps its a subtle bug in MinGW's libstdc++ 4.7.0 build.
Everything does work fine if I downgrade to MinGW 4.6.3.

This smells like static ctors problem described by Rafael on
llvm-commits. What if you change the .ctors section to .init_array?

I’m not sure this bug applies here, since it’s the program generated by
Clang that is crashing, not Clang itself. This leads me to believe it is a
codegen issue somewhere within Clang and not a mis-compile of Clang itself.
Or perhaps its a subtle bug in MinGW’s libstdc++ 4.7.0 build.
Everything does work fine if I downgrade to MinGW 4.6.3.

This smells like static ctors problem described by Rafael on
llvm-commits. What if you change the .ctors section to .init_array?

I tried with:

  • .section .ctors,“w”
  • .section .init_array,“w”

and re-assembled (as)/re-linked (collect2) the executable, but the problem still persists.

I’m not sure this bug applies here, since it’s the program generated by
Clang that is crashing, not Clang itself. This leads me to believe it is a
codegen issue somewhere within Clang and not a mis-compile of Clang itself.
Or perhaps its a subtle bug in MinGW’s libstdc++ 4.7.0 build.
Everything does work fine if I downgrade to MinGW 4.6.3.

This smells like static ctors problem described by Rafael on
llvm-commits. What if you change the .ctors section to .init_array?

I tried with:

  • .section .ctors,“w”
  • .section .init_array,“w”

and re-assembled (as)/re-linked (collect2) the executable, but the problem still persists.

Sorry, switch the + and -…