<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 can create GNU hash section with 0 buckets (incompatible with Android)"
   href="https://bugs.llvm.org/show_bug.cgi?id=36537">36537</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LLD can create GNU hash section with 0 buckets (incompatible with Android)
          </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>peter.smith@linaro.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I've found that LLD cannot link various Android executables due to the dynamic
linker not liking GNU Hash Sections with nbuckets == 0. It gives the error
message: empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
        "(new hash type from the future?)"
<a href="https://android.googlesource.com/platform/bionic/+/master/linker/linker.cpp#3469">https://android.googlesource.com/platform/bionic/+/master/linker/linker.cpp#3469</a>

Tracing this back through the code, this can occur when all the symbols are in
the lower stable partition in GnuHashTableSection::addSymbols() as this will
bail out before NBuckets is set.

When all the symbols in an executable are in the lower partition in:
  std::vector<SymbolTableEntry>::iterator Mid =
      std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
        // Shared symbols that this executable preempts are special. The
dynamic
        // linker has to look them up, so they have to be in the hash table.
        if (auto *SS = dyn_cast<SharedSymbol>(S.Sym))
          return SS->CopyRelSec == nullptr && !SS->NeedsPltAddr;
        return !S.Sym->isDefined();
      });
  if (Mid == V.end())
    return;

  // We chose load factor 4 for the on-disk hash table. For each hash
  // collision, the dynamic linker will compare a uint32_t hash value.
  // Since the integer comparison is quite fast, we believe we can make
  // the load factor even larger. 4 is just a conservative choice.
  NBuckets = std::max<size_t>((V.end() - Mid) / 4, 1);

To reproduce on Linux I've found that I need to compile and link with -fpie
-pie.

For example:
#include <stdio.h>

int main(void) {
    printf("Hello World\n");
    return 0;
}

clang hashtest.c -fpie -Wl,--hash-style=gnu -o hashtest.exe
-fuse-ld=/linaro/upstream/buildclang/bin/ld.lld -pie

llvm-readobj -gnu-hash-table hashtest.exe 

File: hashtest.exe
Format: ELF64-x86-64
Arch: x86_64
AddressSize: 64bit
LoadName: 
GnuHashTable {
  Num Buckets: 0
  First Hashed Symbol Index: 8
  Num Mask Words: 1
  Shift Count: 6
  Bloom Filter: [0x0]
  Buckets: []
  Values: []
}

I'm not convinced that the bionic loader is correct to reject the executable
becasue nbuckets is 0. It could treat that as an empty table and just skip it.
However I think that LLD should probably remove the Hash Table section
completely if it is empty.</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>