<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 - warning: found local symbol in global part of symbol table with Swift compiled code"
   href="https://bugs.llvm.org/show_bug.cgi?id=36771">36771</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>warning: found local symbol in global part of symbol table with Swift compiled code
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>alblue@apple.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When compiling Swift on Linux with 4.1-DEVELOPMENT-SNAPSHOT-2018-03-11-a and
LLD 6.0, I see a warning generated at link time complaining that a found local
symbol in global part of symbol table:

/tmp/cross-toolchain/swift.xctoolchain/usr/bin/ld.lld: warning: found local
symbol '__stop_swift2_protocol_conformances' in global part of symbol table in
file
/private/tmp/cross-toolchain/swift.xctoolchain/usr/lib/swift/linux/libFoundation.so
/tmp/cross-toolchain/swift.xctoolchain/usr/bin/ld.lld: warning: found local
symbol '__start_swift3_typeref' in global part of symbol table in file
/private/tmp/cross-toolchain/swift.xctoolchain/usr/lib/swift/linux/libFoundation.so
/tmp/cross-toolchain/swift.xctoolchain/usr/bin/ld.lld: warning: found local
symbol '__start_swift3_reflstr' in global part of symbol table in file
/private/tmp/cross-toolchain/swift.xctoolchain/usr/lib/swift/linux/libFoundation.so

This apparently is a new feature added in LLD 6.0 (see
<a href="https://reviews.llvm.org/D41257">https://reviews.llvm.org/D41257</a>). The question is, why is Swift (which uses
LLVM as the back end) writing local symbols in a global symbol table if this is
not allowed as per the ELF spec?

According to the author of the warning:

"
Look at Figure 1-13 on this PDF page 19 of
<a href="http://www.skyfree.org/linux/references/ELF_Format.pdf">http://www.skyfree.org/linux/references/ELF_Format.pdf</a> to see what it is. It
says that sh_info member in a section header for the symbol type (of type
SHT_SYMTAB and SHT_DYNSYM) must have an index one greater than the symbol table
index of the last local symbol (binding STB_LOCAL).
So, in a symbol table, all symbols after sh_info index must be non-local. That
warning message is emitted when lld found a violation of the spec.
If you want to fix it, you probably need to sort a symbol table so that all
local symbols are at beginning of a table, and set its last index + 1 to
sh_info member of the symbol table section header.
"

The question is why isn't LLVM's ELF backend performing this step, and
generating a potentially invalid ELF file?

For cross-reference purposes, the bug at Swift.org is
<a href="https://bugs.swift.org/browse/SR-7189">https://bugs.swift.org/browse/SR-7189</a> and the corresponding location where the
Swift compiler emits the symbols is
<a href="https://github.com/apple/swift/blob/master/stdlib/public/runtime/SwiftRT-ELF.cpp#L21-L37">https://github.com/apple/swift/blob/master/stdlib/public/runtime/SwiftRT-ELF.cpp#L21-L37</a>
- the symbols are marked with an independent section and hidden visibility
symbols:

 __asm__("\t.section " #name ",\"a\"\n");                                     \
  __attribute__((__visibility__("hidden"))) extern const char __start_##name; 
\
  __attribute__((__visibility__("hidden"))) extern const char __stop_##name;

FWIW when LLD 5 was being used to link the project, these same symbols weren't
being added to the dynamic exports automatically (which they were when linked
with Gold). I suspect the same issue exists for both.

The generated so that was causing the complaints is downloadable from the
libFoundation.so in the Swift download, which for the above is at
<a href="https://swift.org/builds/swift-4.1-branch/ubuntu1604/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-11-a/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-11-a-ubuntu16.04.tar.gz">https://swift.org/builds/swift-4.1-branch/ubuntu1604/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-11-a/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-11-a-ubuntu16.04.tar.gz</a>
and the file at
swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-11-a-ubuntu16.04/usr/lib/swift/linux/libFoundation.so</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>