1RUN: lld-link -lldmingw %S/Inputs/inline-weak.o %S/Inputs/inline-weak2.o -out:%t.exe
2
3When compiling certain forms of templated inline functions, some
4versions of GCC (tested with 5.4) produces a weak symbol for the function.
5Newer versions of GCC don't do this though.
6
7The bundled object files are an example of that, they can be produced
8with test code like this:
9
10$ cat inline-weak.h
11class MyClass {
12public:
13    template<typename... _Args> int get(_Args&&... args) {
14        return a;
15    }
16private:
17    int a;
18};
19
20$ cat inline-weak.cpp
21#include "inline-weak.h"
22
23int get(MyClass& a);
24
25int main(int argc, char* argv[]) {
26    MyClass a;
27    int ret = a.get();
28    ret += get(a);
29    return ret;
30}
31extern "C" void mainCRTStartup(void) {
32    main(0, (char**)0);
33}
34extern "C" void __main(void) {
35}
36
37$ cat inline-weak2.cpp
38#include "inline-weak.h"
39
40int get(MyClass& a) {
41    return a.get();
42}
43
44$ x86_64-w64-mingw32-g++ -std=c++11 -c inline-weak.cpp
45$ x86_64-w64-mingw32-g++ -std=c++11 -c inline-weak2.cpp
46
47$ x86_64-w64-mingw32-nm inline-weak.o | grep MyClass3get
480000000000000000 p .pdata$_ZN7MyClass3getIJEEEiDpOT_
490000000000000000 t .text$_ZN7MyClass3getIJEEEiDpOT_
500000000000000000 T .weak._ZN7MyClass3getIIEEEiDpOT_.main
510000000000000000 r .xdata$_ZN7MyClass3getIJEEEiDpOT_
52                 w _ZN7MyClass3getIIEEEiDpOT_
530000000000000000 T _ZN7MyClass3getIJEEEiDpOT_
54
55$ x86_64-w64-mingw32-nm inline-weak2.o | grep MyClass3get
560000000000000000 p .pdata$_ZN7MyClass3getIJEEEiDpOT_
570000000000000000 t .text$_ZN7MyClass3getIJEEEiDpOT_
580000000000000000 T .weak._ZN7MyClass3getIIEEEiDpOT_._Z3getR7MyClass
590000000000000000 r .xdata$_ZN7MyClass3getIJEEEiDpOT_
60                 w _ZN7MyClass3getIIEEEiDpOT_
610000000000000000 T _ZN7MyClass3getIJEEEiDpOT_
62
63This can't be reproduced by assembling .s files with llvm-mc, since that
64always produces a symbol named .weak.<weaksymbol>.default, therefore
65the test uses prebuilt object files instead.
66
67In these cases, the undefined weak symbol points to the regular symbol
68.weak._ZN7MyClass3getIIEEEiDpOT_.<othersymbol>, where <othersymbol>
69varies among the object files that emit the same function. This regular
70symbol points to the same location as the comdat function
71_ZN7MyClass3getIJEEEiDpOT_.
72
73When linking, the comdat section from the second object file gets
74discarded, as it matches the one that already exists. This means that
75the uniquely named symbol .weak.<weakname>.<othername> points to a
76discarded section chunk.
77
78Previously, this would have triggered adding an Undefined symbol for
79this case, which would later break linking. However, also previously,
80if the second object file is linked in via a static library, this
81leftover symbol is retained as a Lazy symbol, which would make the link
82succeed.
83