<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 resolves reloc to sym in discarded section to 0 if sym_name < originating_sym_name"
   href="https://bugs.llvm.org/show_bug.cgi?id=45594">45594</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>lld resolves reloc to sym in discarded section to 0 if sym_name < originating_sym_name
          </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>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>ELF
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>rprichard@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, smithp352@googlemail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=23386" name="attach_23386" title="lld-comdat-missing-diag.sh">attachment 23386</a> <a href="attachment.cgi?id=23386&action=edit" title="lld-comdat-missing-diag.sh">[details]</a></span>
lld-comdat-missing-diag.sh

If an object file has a relocation to a symbol from a discarded section, lld
normally issues an error:

    ld.lld: error: relocation refers to a symbol in a discarded section: zzz

I've found a case where ld.lld doesn't issue the error, though, and instead
resolves the relocation to 0:
 - the object file is in an archive
 - the relocation target symbol's name comes before the name of the function
containing the relocation (e.g. in ASCII order).

e.g. If "mmm2" calls "aaa", there is no diagnostic, and the program crashes,
but if it calls "zzz", there is an linker error.

I tried ld.bfd and ld.gold, and both issue a linker error even when "mmm2"
calls "aaa".

The diagnostic is issued if the archive is surrounded with --whole-archive /
--no-whole-archive on the linker command-line.

I tried debugging it a little, and it seemed that the discarded symbol was not
of kind Symbol::UndefinedKind but of kind Symbol::LazyArchiveKind.

I originally noticed this problem in the Android platform, on 32-bit x86, where
we have various copies of the __x86.get_pc_thunk.bx function floating around,
some in .gnu.linkonce.t.__x86.get_pc_thunk.bx sections and others in
.text.__x86.get_pc_thunk.bx sections. lld drops the .gnu.linkonce.* copy (which
is fine) but then produces an executable without the function, with calls to
address "0". I'd expect lld to either use the .text.* copy or issue an error.
Either is OK -- Android should switch over to comdat. I opened
<a href="http://b/154376560">http://b/154376560</a> in Google's tracker for that.)

I'll attach scripts for reproducing:
 - the problem with comdat section groups
 - the problem with .gnu.linkonce.t.__x86.get_pc_thunk.bx</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>