brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · a2b4688 Raw
83 lines · plain
1RUN: lld-link -lldmingw %S/Inputs/inline-weak.o %S/Inputs/inline-weak2.o -out:%t.exe2 3When compiling certain forms of templated inline functions, some4versions 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 produced8with test code like this:9 10$ cat inline-weak.h11class MyClass {12public:13    template<typename... _Args> int get(_Args&&... args) {14        return a;15    }16private:17    int a;18};19 20$ cat inline-weak.cpp21#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.cpp38#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.cpp45$ x86_64-w64-mingw32-g++ -std=c++11 -c inline-weak2.cpp46 47$ x86_64-w64-mingw32-nm inline-weak.o | grep MyClass3get480000000000000000 p .pdata$_ZN7MyClass3getIJEEEiDpOT_490000000000000000 t .text$_ZN7MyClass3getIJEEEiDpOT_500000000000000000 T .weak._ZN7MyClass3getIIEEEiDpOT_.main510000000000000000 r .xdata$_ZN7MyClass3getIJEEEiDpOT_52                 w _ZN7MyClass3getIIEEEiDpOT_530000000000000000 T _ZN7MyClass3getIJEEEiDpOT_54 55$ x86_64-w64-mingw32-nm inline-weak2.o | grep MyClass3get560000000000000000 p .pdata$_ZN7MyClass3getIJEEEiDpOT_570000000000000000 t .text$_ZN7MyClass3getIJEEEiDpOT_580000000000000000 T .weak._ZN7MyClass3getIIEEEiDpOT_._Z3getR7MyClass590000000000000000 r .xdata$_ZN7MyClass3getIJEEEiDpOT_60                 w _ZN7MyClass3getIIEEEiDpOT_610000000000000000 T _ZN7MyClass3getIJEEEiDpOT_62 63This can't be reproduced by assembling .s files with llvm-mc, since that64always produces a symbol named .weak.<weaksymbol>.default, therefore65the test uses prebuilt object files instead.66 67In these cases, the undefined weak symbol points to the regular symbol68.weak._ZN7MyClass3getIIEEEiDpOT_.<othersymbol>, where <othersymbol>69varies among the object files that emit the same function. This regular70symbol points to the same location as the comdat function71_ZN7MyClass3getIJEEEiDpOT_.72 73When linking, the comdat section from the second object file gets74discarded, as it matches the one that already exists. This means that75the uniquely named symbol .weak.<weakname>.<othername> points to a76discarded section chunk.77 78Previously, this would have triggered adding an Undefined symbol for79this case, which would later break linking. However, also previously,80if the second object file is linked in via a static library, this81leftover symbol is retained as a Lazy symbol, which would make the link82succeed.83