clang on cygwin fails to produce executable from c++ source

Hi,

I’m trying to use clang on cygwin to compile C++ files but the executable it produces fails to run. I get runtime failures from cygwin:

Cygwin runtime failure: a.exe: Invalid relocation. Offset 0x2fd4bce73 at address 0x1004010e9 doesn’t fit into 32 bits

sample code - proba.cpp:

#include
using namespace std;

int main(void)
{
cout << “Hello, World!”;
return 0;
}

compiled with:

clang++ proba.cpp

Compilation finishes without any error or warning.

clang was compiled from sources. I tried the latest release (8.0.0) as well as git version (26.03.2019). Both produces the same error when running its output.

clang installation procedure was the following:

git clone https://github.com/llvm/llvm-project.git
mkdir build
cd build
cmake -G “Unix Makefiles” -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=“clang;compiler-rt;lld;” -DLLVM_TARGETS_TO_BUILD=X86 …/llvm/
make -j4
make install

cygwin also has a clang package (version 5.0.1) that compiles proba.cpp to a working executable. The reason of not using that is it does not compile SystemC libraries (https://www.accellera.org/downloads/standards/systemc) correctly. There are few tests that fail during ‘make check’ in contrast when compiling with gcc (7.4.0) all the tests pass.

I also noticed that clang drivers that compiled from sources use g++ as the linker while the one comes with the cygwin package uses ld. I also did not manage to force clang 9 to use lld instead of g++. (-fuse-ld=lld is not recognized by g++)

clang comes with cygwin:

$ clang++ -v proba.cpp -o a_clang5.exe
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/bin
“/usr/bin/clang-5.0” -cc1 -triple x86_64-unknown-windows-cygnus -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name proba.cpp -mre
location-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -momit-leaf-frame-pointer -v -dwarf-co
lumn-info -debugger-tuning=gdb -resource-dir /usr/lib/clang/5.0.1 -internal-isystem /usr/x86_64-pc-cygwin/include/c++ -internal-isystem /usr/x86_64-pc-cygwin/include/c++/x86_6
4-pc-cygwin -internal-isystem /usr/x86_64-pc-cygwin/include/c++/backward -internal-isystem /usr/x86_64-pc-cygwin/include/c++/7.4.0 -internal-isystem /usr/x86_64-pc-cygwin/incl
ude/c++/7.4.0/x86_64-pc-cygwin -internal-isystem /usr/x86_64-pc-cygwin/include/c++/7.4.0/backward -internal-isystem /usr/include/c++/7.4.0 -internal-isystem /usr/include/c++/7
.4.0/x86_64-pc-cygwin -internal-isystem /usr/include/c++/7.4.0/backward -internal-isystem /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++ -internal-isystem /usr/lib/gcc/x86_64
-pc-cygwin/7.4.0/include/c++/x86_64-pc-cygwin -internal-isystem /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/backward -internal-isystem /usr/local/include -internal-isystem
/usr/lib/clang/5.0.1/include -internal-isystem /usr/x86_64-pc-cygwin/include -internal-isystem /usr/include -internal-isystem /usr/include/w32api -fdeprecated-macro -fdebug-c
ompilation-dir /cygdrive/c/home/peter.gerst/tmp/clang_proba -ferror-limit 19 -fmessage-length 175 -femulated-tls -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics
-show-option -fcolor-diagnostics -o /tmp/proba-e855cb.o -x c++ proba.cpp
clang -cc1 version 5.0.1 based upon LLVM 5.0.1 default target x86_64-unknown-windows-cygnus
ignoring nonexistent directory “/usr/x86_64-pc-cygwin/include/c++”
ignoring nonexistent directory “/usr/x86_64-pc-cygwin/include/c++/x86_64-pc-cygwin”
ignoring nonexistent directory “/usr/x86_64-pc-cygwin/include/c++/backward”
ignoring nonexistent directory “/usr/x86_64-pc-cygwin/include/c++/7.4.0”
ignoring nonexistent directory “/usr/x86_64-pc-cygwin/include/c++/7.4.0/x86_64-pc-cygwin”
ignoring nonexistent directory “/usr/x86_64-pc-cygwin/include/c++/7.4.0/backward”
ignoring nonexistent directory “/usr/include/c++/7.4.0”
ignoring nonexistent directory “/usr/include/c++/7.4.0/x86_64-pc-cygwin”
ignoring nonexistent directory “/usr/include/c++/7.4.0/backward”
ignoring nonexistent directory “/usr/local/include”
ignoring nonexistent directory “/usr/x86_64-pc-cygwin/include”
#include “…” search starts here:
#include <…> search starts here:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/x86_64-pc-cygwin
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/backward
/usr/lib/clang/5.0.1/include
/usr/include
/usr/include/w32api
End of search list.
“/usr/bin/ld” -m i386pep --wrap _Znwm --wrap _Znam --wrap _ZnwmRKSt9nothrow_t --wrap _ZnamRKSt9nothrow_t --wrap _ZdlPv --wrap _ZdaPv --wrap _ZdlPvRKSt9nothrow_t --wrap _ZdaPv
KSt9nothrow_t -Bdynamic --tsaware -o a_clang5.exe /usr/lib/crt0.o /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/crtbegin.o -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0 -L/usr/x86_64-pc-cygwin/
lib -L/usr/lib /tmp/proba-e855cb.o -lstdc++ -liconv -lgcc_s -lgcc -lcygwin -ladvapi32 -lshell32 -luser32 -lkernel32 -lgcc_s -lgcc -lcygwin /usr/lib/default-manifest.o /usr/lib
/gcc/x86_64-pc-cygwin/7.4.0/crtend.o

clang 9 compiled from sources:

$ clang++ -v proba.cpp -o a.exe
clang version 9.0.0 (https://github.com/llvm/llvm-project.git e24441aab03e393d591d9252612e8c0ac557b1c3)
Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/local/bin
“/usr/local/bin/clang-9” -cc1 -triple x86_64-unknown-windows-cygnus -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name proba.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir /usr/local/lib/clang/9.0.0 -fdeprecated-macro -fdebug-compilation-dir /cygdrive/c/home/peter.gerst/tmp/clang_proba -ferror-limit 19 -fmessage-length 135 -fno-use-cxa-atexit -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/proba-b6bf15.o -x c++ proba.cpp -faddrsig
clang -cc1 version 9.0.0 based upon LLVM 9.0.0svn default target x86_64-unknown-cygwin
#include “…” search starts here:
#include <…> search starts here:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/x86_64-pc-cygwin
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/backward
/usr/local/include
/usr/local/lib/clang/9.0.0/include
/usr/include/w32api
/usr/include
End of search list.
“/usr/bin/g++” -v -m64 -o a.exe /tmp/proba-b6bf15.o
Using built-in specs.
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libcilkrts --enable-libgomp --enable-libitm --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 7.4.0 (GCC)
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/:/usr/lib/gcc/x86_64-pc-cygwin/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/:/usr/lib/gcc/x86_64-pc-cygwin/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/x86_64-pc-cygwin/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/x86_64-pc-cygwin/lib/…/lib/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/lib/:/lib/…/lib/:/usr/lib/…/lib/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/x86_64-pc-cygwin/lib/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS=’-v’ ‘-m64’ ‘-o’ ‘a.exe’ ‘-shared-libgcc’ ‘-mtune=generic’ ‘-march=x86-64’
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/collect2.exe -plugin /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/cyglto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/lto-wrapper.exe -plugin-opt=-fresolution=/tmp/ccM89KcB.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lcygwin -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id -m i386pep --wrap _Znwm --wrap _Znam --wrap _ZdlPv --wrap _ZdaPv --wrap _ZnwmRKSt9nothrow_t --wrap _ZnamRKSt9nothrow_t --wrap _ZdlPvRKSt9nothrow_t --wrap _ZdaPvRKSt9nothrow_t -Bdynamic --dll-search-prefix=cyg --tsaware -o a.exe /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/lib/crt0.o /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/crtbegin.o -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0 -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/x86_64-pc-cygwin/lib/…/lib -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/lib -L/lib/…/lib -L/usr/lib/…/lib -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/x86_64-pc-cygwin/lib -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/… /tmp/proba-b6bf15.o -lstdc++ -lgcc_s -lgcc -lcygwin -ladvapi32 -lshell32 -luser32 -lkernel32 -lgcc_s -lgcc /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/…/…/…/…/lib/default-manifest.o /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/crtend.o
COLLECT_GCC_OPTIONS=’-v’ ‘-m64’ ‘-o’ ‘a.exe’ ‘-shared-libgcc’ ‘-mtune=generic’ ‘-march=x86-64’

Compiling and executing the C equivalent of proba.cpp works fine with clang 9. I tried the tests suggested by the documentation (https://clang.llvm.org/get_started.html):

clang --help
clang file.c -fsyntax-only
clang file.c -S -emit-llvm -o -
clang file.c -S -emit-llvm -o - -O3
clang file.c -S -O3 -o -

They seemed to work correctly to me.

I also tried the following without success

clang++ -c proba.cpp -o proba.o
ld.lld proba.o -o proba.exe
ld.lld: error: proba.o: unknown file type

Does anybody have any idea what could go wrong with the C++ compilation using clang 9 on cygwin?

Did anybody manage to use lld linker along with clang under cygwin?

Any hint on how can clang + lld toolchain setup correctly under cygwin will be also appreciated. Thank you!

Note:

This issue is also reported on superuser:
https://superuser.com/questions/1421240/clang-on-cygwin-fails-to-produce-executable-from-c-source