llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.43k stars 12.16k forks source link

weak symbols fail on Windows #26370

Open llvmbot opened 8 years ago

llvmbot commented 8 years ago
Bugzilla Link 25996
Version 3.7
OS Windows NT
Reporter LLVM Bugzilla Contributor
CC @rnk

Extended Description

Having the code:

$ cat main.cpp
#include <stdio.h>

__attribute__((weak)) int fn()
{
        return 0;
}

int main(int argc, char *argv[])
{
        printf("%d\n", fn());

        return 0;
}
$ cat fn.cpp

int fn()
{
        return 42;
}

fails to compile on Windows for both msvc and gnu toolchains:

$ /c/prog64/LLVM/bin/clang++ -v main.cpp fn.cpp
clang version 3.7.0 (trunk 234109)
Target: x86_64-pc-windows-msvc
Thread model: posix
 "c:\\prog64\\LLVM\\bin\\clang++.exe" -cc1 -triple x86_64-pc-windows-msvc -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mthre
ad-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -momit-leaf-frame-pointer -v -dwarf-column-info -resource-dir "c:\\prog64\\LLVM\\bin\\..\\lib\\clang\
\3.7.0" -internal-isystem "c:\\prog64\\LLVM\\bin\\..\\lib\\clang\\3.7.0\\include" -internal-isystem "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include" -internal-isystem "C:\\Program
Files (x86)\\Windows Kits\\8.1\\include\\shared" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\um" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\win
rt" -std=c++11 -fdeprecated-macro -fdebug-compilation-dir "c:\\tmp" -ferror-limit 19 -fmessage-length 200 -mstackrealign -fms-extensions -fms-compatibility -fms-compatibility-version=18 -fno-threadsaf
e-statics -fdelayed-template-parsing -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o "C:/Users/schuttek/AppData/Local/Temp\\main-2b9d52.o" -x c++ main
.cpp
clang -cc1 version 3.7.0 based upon LLVM 3.7.0svn-r234109 default target x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
 c:\prog64\LLVM\bin\..\lib\clang\3.7.0\include
 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
 C:\Program Files (x86)\Windows Kits\8.1\include\shared
 C:\Program Files (x86)\Windows Kits\8.1\include\um
 C:\Program Files (x86)\Windows Kits\8.1\include\winrt
End of search list.
 "c:\\prog64\\LLVM\\bin\\clang++.exe" -cc1 -triple x86_64-pc-windows-msvc -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name fn.cpp -mrelocation-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-column-info -resource-dir "c:\\prog64\\LLVM\\bin\\..\\lib\\clang\\3
.7.0" -internal-isystem "c:\\prog64\\LLVM\\bin\\..\\lib\\clang\\3.7.0\\include" -internal-isystem "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include" -internal-isystem "C:\\Program Fi
les (x86)\\Windows Kits\\8.1\\include\\shared" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\um" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\winrt
" -std=c++11 -fdeprecated-macro -fdebug-compilation-dir "c:\\tmp" -ferror-limit 19 -fmessage-length 200 -mstackrealign -fms-extensions -fms-compatibility -fms-compatibility-version=18 -fno-threadsafe-
statics -fdelayed-template-parsing -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o "C:/Users/schuttek/AppData/Local/Temp\\fn-f9cbfd.o" -x c++ fn.cpp
clang -cc1 version 3.7.0 based upon LLVM 3.7.0svn-r234109 default target x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
 c:\prog64\LLVM\bin\..\lib\clang\3.7.0\include
 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
 C:\Program Files (x86)\Windows Kits\8.1\include\shared
 C:\Program Files (x86)\Windows Kits\8.1\include\um
 C:\Program Files (x86)\Windows Kits\8.1\include\winrt
End of search list.
 "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin\\amd64\\link.exe" -out:a.exe -defaultlib:libcmt "-libpath:C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\lib\\amd64" "-libp
ath:C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\x64" -nologo "C:/Users/schuttek/AppData/Local/Temp\\main-2b9d52.o" "C:/Users/schuttek/AppData/Local/Temp\\fn-f9cbfd.o"
fn-f9cbfd.o : error LNK2005: "int __cdecl fn(void)" (?fn@@YAHXZ) already defined in main-2b9d52.o
a.exe : fatal error LNK1169: one or more multiply defined symbols found
clang++.exe: error: linker command failed with exit code 1169 (use -v to see invocation)
$ clang++ -m64 -v main.cpp fn.cpp
clang version 3.7.0 (tags/RELEASE_370/final)
Target: x86_64-pc-windows-gnu
Thread model: posix
 "c:\\prog64\\LLVM_mingw\\bin\\clang++.exe" -cc1 -triple x86_64-pc-windows-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-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-column-info -resource-dir "c:\\prog64\\LLVM_mingw\\bin\\..\\
lib\\clang\\3.7.0" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\x86_64-w64-mingw32" -
internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\backward" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\4.8.1" -internal-isystem "c:\\p
rog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\4.8.1\\x86_64-w64-mingw32" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\4.8.1\\backward" -internal-isystem "c
:\\prog64\\mingw64-4.8.1\\include\\c++\\4.8.1" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\include\\c++\\4.8.1\\x86_64-w64-mingw32" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\include\\c++\\4.8.1\
\backward" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include\\c++" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include\\
c++\\x86_64-w64-mingw32" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include\\c++\\backward" -internal-isystem "c:\\prog64\\LLVM_mingw\\bin\\..\\lib\\clang\\3.7.
0\\include" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32/sys-root/mingw/include" -intern
al-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include-fixed" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include" -internal-isystem "c:\\prog64\\min
gw64-4.8.1\\include" -fdeprecated-macro -fdebug-compilation-dir "c:\\tmp" -ferror-limit 19 -fmessage-length 200 -mstackrealign -fno-use-cxa-atexit -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdi
agnostics-show-option -fcolor-diagnostics -o "C:/Users/schuttek/AppData/Local/Temp\\main-3037a0.o" -x c++ main.cpp
clang -cc1 version 3.7.0 based upon LLVM 3.7.0 default target i686-pc-windows-gnu
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\x86_64-w64-mingw32"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\backward"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\4.8.1"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\4.8.1\x86_64-w64-mingw32"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\4.8.1\backward"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\include\c++\4.8.1"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\include\c++\4.8.1\x86_64-w64-mingw32"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\include\c++\4.8.1\backward"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32/sys-root/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\x86_64-w64-mingw32
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\backward
 c:\prog64\LLVM_mingw\bin\..\lib\clang\3.7.0\include
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include-fixed
 c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include
 c:\prog64\mingw64-4.8.1\include
End of search list.
 "c:\\prog64\\LLVM_mingw\\bin\\clang++.exe" -cc1 -triple x86_64-pc-windows-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name fn.cpp -mrelocation-model pic -pic-level 2 -mt
hread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -momit-leaf-frame-pointer -v -dwarf-column-info -resource-dir "c:\\prog64\\LLVM_mingw\\bin\\..\\li
b\\clang\\3.7.0" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\x86_64-w64-mingw32" -in
ternal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\backward" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\4.8.1" -internal-isystem "c:\\pro
g64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\4.8.1\\x86_64-w64-mingw32" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include\\c++\\4.8.1\\backward" -internal-isystem "c:\
\prog64\\mingw64-4.8.1\\include\\c++\\4.8.1" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\include\\c++\\4.8.1\\x86_64-w64-mingw32" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\include\\c++\\4.8.1\\b
ackward" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include\\c++" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include\\c+
+\\x86_64-w64-mingw32" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include\\c++\\backward" -internal-isystem "c:\\prog64\\LLVM_mingw\\bin\\..\\lib\\clang\\3.7.0\
\include" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32/sys-root/mingw/include" -internal
-isystem "c:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1\\include-fixed" -internal-isystem "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\include" -internal-isystem "c:\\prog64\\mingw
64-4.8.1\\include" -fdeprecated-macro -fdebug-compilation-dir "c:\\tmp" -ferror-limit 19 -fmessage-length 200 -mstackrealign -fno-use-cxa-atexit -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiag
nostics-show-option -fcolor-diagnostics -o "C:/Users/schuttek/AppData/Local/Temp\\fn-077acf.o" -x c++ fn.cpp
clang -cc1 version 3.7.0 based upon LLVM 3.7.0 default target i686-pc-windows-gnu
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\x86_64-w64-mingw32"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\backward"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\4.8.1"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\4.8.1\x86_64-w64-mingw32"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include\c++\4.8.1\backward"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\include\c++\4.8.1"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\include\c++\4.8.1\x86_64-w64-mingw32"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\include\c++\4.8.1\backward"
ignoring nonexistent directory "c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32/sys-root/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\x86_64-w64-mingw32
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\backward
 c:\prog64\LLVM_mingw\bin\..\lib\clang\3.7.0\include
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include
 c:\prog64\mingw64-4.8.1\lib\gcc\x86_64-w64-mingw32\4.8.1\include-fixed
 c:\prog64\mingw64-4.8.1\x86_64-w64-mingw32\include
 c:\prog64\mingw64-4.8.1\include
End of search list.
 "c:\\prog64\\mingw64-4.8.1\\bin\\ld.exe" -m i386pep -Bdynamic -o a.exe "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\lib\\crt2.o" "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\lib\\crtbegin.o" "-
Lc:\\prog64\\mingw64-4.8.1\\lib\\gcc\\x86_64-w64-mingw32\\4.8.1" "-Lc:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\lib" "-Lc:\\prog64\\mingw64-4.8.1\\lib" "-Lc:\\prog64\\mingw64-4.8.1\\x86_64-w64-ming
w32/sys-root/mingw/lib" "C:/Users/schuttek/AppData/Local/Temp\\main-3037a0.o" "C:/Users/schuttek/AppData/Local/Temp\\fn-077acf.o" -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvap
i32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt "c:\\prog64\\mingw64-4.8.1\\x86_64-w64-mingw32\\lib\\crtend.o"
C:/Users/schuttek/AppData/Local/Temp\fn-077acf.o:(.text+0x0): multiple definition of `fn()'
C:/Users/schuttek/AppData/Local/Temp\main-3037a0.o:(.text+0x0): first defined here
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)

Note that this compiles and runs OK on windows/g++ 4.8.1, Linux/gcc 4.8 and Linux/clang++-3.5. Result for windows/g++ 4.8.1:
$ g++ -o foo main.cpp fn.cpp

$ ./foo
42
rnk commented 8 years ago

Yep, implementing a mingw-style weak attribute is still TODO.