<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - LLD for MinGW generating false undefined references and segmentation fault binaries"
   href="https://bugs.llvm.org/show_bug.cgi?id=38939">38939</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LLD for MinGW generating false undefined references and segmentation fault binaries
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>COFF
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>notengobattery@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>First of all, I've doing some work in order to bring a fully functional, self
hosted LLVM distro to MinGW. I've made some great advancements (for example,
getting rid of 'undefined reference to typeinfo/vtable') and I built a 1st
stage LLVM/Clang from *Trunk* (or master if using the git mirror) and a 2nd
stage LLVM/Clang that uses the libc++, libc++abi and libunwind from stage 1.

Both stages are working correctly and passing the test-suite tests, both when
using the MinGW's libstdc++ and the just-built libcxx+libcxxabi+libunwind.
AFAIK most features are working, from exceptions to sanitizers, so I'm in a
good road.

As I cannot and won't upload binaries, you have to trust my pastebins.
- In this pastebin you can see the DLL dependencies of a self-hosted LLVM
distro on MinGW using GNU BFD linker
<a href="https://pastebin.com/iUFngMfw">https://pastebin.com/iUFngMfw</a>

However, when using the just-build LLD to build a simple .exe it fails. It also
fails when building the final stage llvm-tblgen.exe
- In this pastebin you can see the Clang log for building a simple a.exe in 4
combinations: lld-libc++, lld-libstdc++, bfd-libc++ and bfd-libstdc++
<a href="https://pastebin.com/vuYRkBwG">https://pastebin.com/vuYRkBwG</a>

If I use LLD and the /force option, a binary is created but it segfaults
- In this pastebin you can see the Clang log for building a simple a.exe with
/force flag that segfaults
<a href="https://pastebin.com/LBgSam9f">https://pastebin.com/LBgSam9f</a>

P.D. Sorry for using pastebin

-- Extra details
1. As long as you use libc++ instead of libstdc++ to build lld, it won't
segfault or deadlock. Using libstdc++ to build ld.lld will deadlock. I'm not
sure but this may be related <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - lld mingw64: invalid memory access leads to deadlock"
   href="show_bug.cgi?id=36785">https://bugs.llvm.org/show_bug.cgi?id=36785</a>
2. I've tracked the problem to a specific function. In particular this one
<a href="https://github.com/llvm-mirror/lld/blob/72803c47f6c7d5ebbcccf44651403654fa60b903/COFF/SymbolTable.cpp#L154">https://github.com/llvm-mirror/lld/blob/72803c47f6c7d5ebbcccf44651403654fa60b903/COFF/SymbolTable.cpp#L154</a>
   This is important because as far as I understand, LLD will mark some symbols
that had an entry in the import library (i.e. dll.a) with the prefix __imp_ but
not the corresponding export without the __imp_ as "Lazy". These "Lazy" symbols
are MinGW automatic imports.
   The mentioned function will convert this Symbol to a DefinedImportData which
contains a reference to the DLL (i.e. the dll, not the dll.a), so the symbol
will not be undefined if it's present in the DLL and LLD will link and
reference the relative address in the DLL (i.e. not the dll.a).
   As long as I tested, this dynamic cast always fails, so LLD cannot resolve
the MinGW automatic import and generates a false positive for the undefined
symbol. And, of course, if you use /force, these symbols will point to
0x00000000, and that is the cause of the segfault.

If I examine the generated .exe (the one linked with bfd) using Dependency
Walker, the symbol is successfully imported, referenced and used. LLD produces
a bad binary even when /force is used.
<a href="https://pastebin.com/GDEESVmi">https://pastebin.com/GDEESVmi</a></pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>