[llvm-bugs] [Bug 36537] New: LLD can create GNU hash section with 0 buckets (incompatible with Android)

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Feb 27 05:43:04 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=36537

            Bug ID: 36537
           Summary: LLD can create GNU hash section with 0 buckets
                    (incompatible with Android)
           Product: lld
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: ELF
          Assignee: unassignedbugs at nondot.org
          Reporter: peter.smith at linaro.org
                CC: llvm-bugs at lists.llvm.org

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?)"
https://android.googlesource.com/platform/bionic/+/master/linker/linker.cpp#3469

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180227/d740c5a4/attachment.html>


More information about the llvm-bugs mailing list