<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 - Missing SHF_TLS section error with ELF TLS and --pack-dyn-relocs=android"
   href="https://bugs.llvm.org/show_bug.cgi?id=37841">37841</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missing SHF_TLS section error with ELF TLS and --pack-dyn-relocs=android
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>Other
          </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
          </td>
        </tr></table>
      <p>
        <div>
        <pre>lld prints a missing SHF_TLS section error given the combination of arm64, a
static ELF TLS variable, and Android-style packed relocations. Test case:

    static __thread char tlsvar;
    char bump() {
      return ++tlsvar;
    }

$ /x/clang6/bin/clang test.c -nostdlib -shared -fpic -target
aarch64-linux-android -fuse-ld=lld -Wl,--pack-dyn-relocs=android
-fno-emulated-tls

Output:

/x/clang6/bin/ld.lld: error: /tmp/test-bca6ac.o has an STT_TLS symbol but
doesn't have an SHF_TLS section
clang-6.0: error: linker command failed with exit code 1 (use -v to see
invocation)

There is an SHF_TLS section in the object file. It links fine if I omit
-Wl,--pack-dyn-relocs=android, and it also works on other architectures (e.g.
arm-linux-androideabi).

I think the problem is that the Android relocation packing code is using the
global Out::TlsPhdr variable before it's been initialized. The Android packing
code (updateAllocSize/encodeDynamicReloc) needs to get the addend of a TLS
relocation, and for that, it calls getSymVA on a TLS symbol, which reads the
global Out::TlsPhdr pointer. Out::TlsPhdr isn't initialized until setPhdrs is
called, though, and the packing code is called from finalizeSections.

Backtrace from the error:

#0  getSymVA (Sym=..., Addend=@0x7fffffffbcd0: 0) at
/x/llvm-upstream/llvm/tools/lld/ELF/Symbols.cpp:94
#1  0x00007ffff6c74c70 in lld::elf::Symbol::getVA (this=0x65d630, Addend=0) at
/x/llvm-upstream/llvm/tools/lld/ELF/Symbols.cpp:111
#2  0x00007ffff6c7a8ff in lld::elf::DynamicReloc::computeAddend (this=0x680a50)
at /x/llvm-upstream/llvm/tools/lld/ELF/SyntheticSections.cpp:1399
#3  0x00007ffff6c7fa50 in
encodeDynamicReloc<llvm::object::ELFType<(llvm::support::endianness)1, true> >
(P=0x7fffffffbef0, Rel=...) at
/x/llvm-upstream/llvm/tools/lld/ELF/SyntheticSections.cpp:1455
#4  0x00007ffff6ca6660 in
lld::elf::AndroidPackedRelocationSection<llvm::object::ELFType<(llvm::support::endianness)1,
true> >::updateAllocSize (this=0x6670d0)
    at /x/llvm-upstream/llvm/tools/lld/ELF/SyntheticSections.cpp:1564
#5  0x00007ffff6d37429 in (anonymous
namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true>
<span class="quote">>::finalizeSections (this=0x7fffffffc728)</span >
    at /x/llvm-upstream/llvm/tools/lld/ELF/Writer.cpp:1697
#6  0x00007ffff6d128eb in (anonymous
namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true>
<span class="quote">>::run (this=0x7fffffffc728) at</span >
/x/llvm-upstream/llvm/tools/lld/ELF/Writer.cpp:447
#7  0x00007ffff6d4ba5a in
lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)1, true>
<span class="quote">> () at /x/llvm-upstream/llvm/tools/lld/ELF/Writer.cpp:145</span >
#8  0x00007ffff6af7f7d in
lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::support::endianness)1,
true> > (this=0x647bc0, Args=...) at
/x/llvm-upstream/llvm/tools/lld/ELF/Driver.cpp:1383
#9  0x00007ffff6adfed8 in lld::elf::LinkerDriver::main (this=0x647bc0,
ArgsArr=...) at /x/llvm-upstream/llvm/tools/lld/ELF/Driver.cpp:402
#10 0x00007ffff6adf6ab in lld::elf::link (Args=..., CanExitEarly=true,
Error=...) at /x/llvm-upstream/llvm/tools/lld/ELF/Driver.cpp:98
#11 0x0000000000401946 in main (Argc=16, Argv=0x7fffffffd688) at
/x/llvm-upstream/llvm/tools/lld/tools/lld/lld.cpp:132

Backtrace from Out::TlsPhdr initialization (w/o packed relocations):

#0  (anonymous
namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true>
<span class="quote">>::setPhdrs (this=0x7fffffffc758) at</span >
/x/llvm-upstream/llvm/tools/lld/ELF/Writer.cpp:2087
#1  0x00007ffff6d129e6 in (anonymous
namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true>
<span class="quote">>::run (this=0x7fffffffc758) at</span >
/x/llvm-upstream/llvm/tools/lld/ELF/Writer.cpp:471
#2  0x00007ffff6d4ba5a in
lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)1, true>
<span class="quote">> () at /x/llvm-upstream/llvm/tools/lld/ELF/Writer.cpp:145</span >
#3  0x00007ffff6af7f7d in
lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::support::endianness)1,
true> > (this=0x647bb0, Args=...) at
/x/llvm-upstream/llvm/tools/lld/ELF/Driver.cpp:1383
#4  0x00007ffff6adfed8 in lld::elf::LinkerDriver::main (this=0x647bb0,
ArgsArr=...) at /x/llvm-upstream/llvm/tools/lld/ELF/Driver.cpp:402
#5  0x00007ffff6adf6ab in lld::elf::link (Args=..., CanExitEarly=true,
Error=...) at /x/llvm-upstream/llvm/tools/lld/ELF/Driver.cpp:98
#6  0x0000000000401946 in main (Argc=15, Argv=0x7fffffffd6b8) at
/x/llvm-upstream/llvm/tools/lld/tools/lld/lld.cpp:132</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>