Compiler projects using llvm
## Check how the GNU Hash section is dumped with --gnu-hash-table.

# RUN: yaml2obj --docnum=1 -DBITS=64 %s -o %t.x64
# RUN: yaml2obj --docnum=1 -DBITS=32 %s -o %t.x32

# RUN: llvm-readobj --gnu-hash-table %t.x64 | FileCheck %s
# RUN: llvm-readelf --gnu-hash-table %t.x64 | FileCheck %s

# RUN: llvm-readobj --gnu-hash-table %t.x32 | FileCheck %s
# RUN: llvm-readelf --gnu-hash-table %t.x32 | FileCheck %s

# CHECK:      GnuHashTable {
# CHECK-NEXT:   Num Buckets: 3
# CHECK-NEXT:   First Hashed Symbol Index: 1
# CHECK-NEXT:   Num Mask Words: 2
# CHECK-NEXT:   Shift Count: 2
# CHECK-NEXT:   Bloom Filter: [0x3, 0x4]
# CHECK-NEXT:   Buckets: [5, 6, 7]
# CHECK-NEXT:   Values: [0x8, 0x9, 0xA, 0xB]
# CHECK-NEXT: }

--- !ELF
FileHeader:
  Class: ELFCLASS[[BITS]]
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x1
      Shift2: 0x2
## The number of words in the Bloom filter. The value of 2 is no-op.
      MaskWords: [[MASKWORDS=2]]
## The number of hash buckets. The value of 3 is no-op.
      NBuckets:  [[NBUCKETS=3]]
    BloomFilter: [0x3, 0x4]
    HashBuckets: [0x5, 0x6, 0x7]
    HashValues:  [0x8, 0x9, 0xA, 0xB]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Link:  .dynstr
    Entries:
      - Tag:   DT_GNU_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols:
  - Name:    aaa
    Binding: STB_GLOBAL
  - Name:    bbb
    Binding: STB_GLOBAL
  - Name:    ccc
    Binding: STB_GLOBAL
  - Name:    ddd
    Binding: STB_GLOBAL
ProgramHeaders:
  - Type:     PT_LOAD
    Flags:    [ PF_R, PF_X ]
    FirstSec: .gnu.hash
    LastSec:  .dynamic

## Check we report a warning if there is no dynamic symbol section in the object.

# RUN: yaml2obj --docnum=2 %s -o %t.nodynsym
# RUN: llvm-readobj --gnu-hash-table %t.nodynsym 2>&1 | FileCheck %s -DFILE=%t.nodynsym --check-prefix=NODYNSYM
# RUN: llvm-readelf --gnu-hash-table %t.nodynsym 2>&1 | FileCheck %s -DFILE=%t.nodynsym --check-prefix=NODYNSYM

# NODYNSYM:      GnuHashTable {
# NODYNSYM-NEXT:   Num Buckets: 1
# NODYNSYM-NEXT:   First Hashed Symbol Index: 0
# NODYNSYM-NEXT:   Num Mask Words: 1
# NODYNSYM-NEXT:   Shift Count: 0
# NODYNSYM-NEXT:   Bloom Filter: [0x0]
# NODYNSYM-NEXT:   Buckets: [0]
# NODYNSYM-NEXT: warning: '[[FILE]]': unable to dump 'Values' for the SHT_GNU_HASH section: no dynamic symbol table found
# NODYNSYM-NEXT: }

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x0
      Shift2: 0x0
    BloomFilter: [ 0x0 ]
    HashBuckets: [ 0x0 ]
    HashValues:  [ 0x0 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Entries:
      - Tag:   DT_GNU_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
ProgramHeaders:
  - Type:     PT_LOAD
    Flags:    [ PF_R, PF_X ]
    FirstSec: .gnu.hash
    LastSec:  .dynamic

## Check what we do when the index of the first symbol in the dynamic symbol table
## included in the hash table is larger than the number of dynamic symbols.

# RUN: yaml2obj --docnum=3 %s -o %t.brokensymndx
# RUN: llvm-readobj --gnu-hash-table %t.brokensymndx 2>&1 \
# RUN:   | FileCheck %s -DFILE=%t.brokensymndx --check-prefix=SYMNDX
# RUN: llvm-readelf --gnu-hash-table %t.brokensymndx 2>&1 \
# RUN:   | FileCheck %s -DFILE=%t.brokensymndx --check-prefix=SYMNDX

# SYMNDX:      GnuHashTable {
# SYMNDX-NEXT:   Num Buckets: 1
# SYMNDX-NEXT:   First Hashed Symbol Index: 2
# SYMNDX-NEXT:   Num Mask Words: 1
# SYMNDX-NEXT:   Shift Count: 0
# SYMNDX-NEXT:   Bloom Filter: [0x1]
# SYMNDX-NEXT:   Buckets: [2]
# SYMNDX-NEXT: warning: '[[FILE]]': unable to dump 'Values' for the SHT_GNU_HASH section: the first hashed symbol index (2) is greater than or equal to the number of dynamic symbols (2)
# SYMNDX-NEXT: }

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x2
      Shift2: 0x0
    BloomFilter: [ 0x1 ]
    HashBuckets: [ 0x2 ]
    HashValues:  [ 0x3 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Link:  0
    Entries:
      - Tag:   DT_GNU_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols:
  - Name:    aaa
    Binding: STB_GLOBAL
ProgramHeaders:
  - Type:     PT_LOAD
    Flags:    [ PF_R, PF_X ]
    FirstSec: .gnu.hash
    LastSec:  .dynamic

## Check we emit a warning when the dynamic symbol table is empty.
## A valid dynamic symbol table should have at least one symbol: the symbol with index 0.

# RUN: yaml2obj --docnum=4 %s -o %t.emptydynsym
# RUN: llvm-readobj --gnu-hash-table %t.emptydynsym 2>&1 \
# RUN:   | FileCheck %s -DFILE=%t.emptydynsym --check-prefix=EMPTY-DYNSYM
# RUN: llvm-readelf --gnu-hash-table %t.emptydynsym 2>&1 \
# RUN:   | FileCheck %s -DFILE=%t.emptydynsym --check-prefix=EMPTY-DYNSYM

# EMPTY-DYNSYM:      GnuHashTable {
# EMPTY-DYNSYM-NEXT:   Num Buckets: 1
# EMPTY-DYNSYM-NEXT:   First Hashed Symbol Index: 0
# EMPTY-DYNSYM-NEXT:   Num Mask Words: 1
# EMPTY-DYNSYM-NEXT:   Shift Count: 0
# EMPTY-DYNSYM-NEXT:   Bloom Filter: [0x0]
# EMPTY-DYNSYM-NEXT:   Buckets: [0]
# EMPTY-DYNSYM-NEXT: warning: '[[FILE]]': unable to dump 'Values' for the SHT_GNU_HASH section: the dynamic symbol table is empty
# EMPTY-DYNSYM-NEXT: }

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: 0x0
      Shift2: 0x0
    BloomFilter: [ 0x0 ]
    HashBuckets: [ 0x0 ]
    HashValues:  [ 0x0 ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Link:  0
    Entries:
      - Tag:   DT_GNU_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
  - Name: .dynsym
    Type: SHT_DYNSYM
    Size: 0
ProgramHeaders:
  - Type:     PT_LOAD
    Flags:    [ PF_R, PF_X ]
    FirstSec: .gnu.hash
    LastSec:  .dynamic

## Linkers might produce an empty no-op SHT_GNU_HASH section when
## there are no dynamic symbols or when all dynamic symbols are undefined.
## Such sections normally have a single zero entry in the bloom
## filter, a single zero entry in the hash bucket and no values.
##
## The index of the first symbol in the dynamic symbol table
## included in the hash table can be set to the number of dynamic symbols,
## which is one larger than the index of the last dynamic symbol.
## For empty tables however, this value is unimportant and can be ignored.

## Case A: set the index of the first symbol in the dynamic symbol table to
##         the number of dynamic symbols.
# RUN: yaml2obj --docnum=5 -DSYMNDX=0x1 %s -o %t.empty.1
# RUN: llvm-readobj --gnu-hash-table %t.empty.1 2>&1 \
# RUN:   | FileCheck %s -DFILE=%t.empty.1 -DSYMNDX=1 --check-prefix=EMPTY --implicit-check-not="warning:"
# RUN: llvm-readelf --gnu-hash-table %t.empty.1 2>&1 \
# RUN:   | FileCheck %s -DFILE=%t.empty.1 -DSYMNDX=1 --check-prefix=EMPTY --implicit-check-not="warning:"

## Case B: set the index of the first symbol in the dynamic symbol table to
##         an arbitrary value that is larger than the number of dynamic symbols.
# RUN: yaml2obj --docnum=5 -DSYMNDX=0x2 %s -o %t.empty.2
# RUN: llvm-readobj --gnu-hash-table %t.empty.2 2>&1 \
# RUN:   | FileCheck %s -DFILE=%t.empty.2 -DSYMNDX=2 --check-prefix=EMPTY --implicit-check-not="warning:"
# RUN: llvm-readelf --gnu-hash-table %t.empty.2 2>&1 \
# RUN:   | FileCheck %s -DFILE=%t.empty.2 -DSYMNDX=2 --check-prefix=EMPTY --implicit-check-not="warning:"

# EMPTY:      GnuHashTable {
# EMPTY-NEXT:   Num Buckets: 1
# EMPTY-NEXT:   First Hashed Symbol Index: [[SYMNDX]]
# EMPTY-NEXT:   Num Mask Words: 1
# EMPTY-NEXT:   Shift Count: 0
# EMPTY-NEXT:   Bloom Filter: [0x0]
# EMPTY-NEXT:   Buckets: [0]
# EMPTY-NEXT:   Values: []
# EMPTY-NEXT: }

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:  .gnu.hash
    Type:  SHT_GNU_HASH
    Flags: [ SHF_ALLOC ]
    Header:
      SymNdx: [[SYMNDX]]
      Shift2: 0x0
    BloomFilter: [ 0x0 ]
    HashBuckets: [ 0x0 ]
    HashValues:  [ ]
  - Name:  .dynamic
    Type:  SHT_DYNAMIC
    Flags: [ SHF_ALLOC ]
    Link:  0
    Entries:
      - Tag:   DT_GNU_HASH
        Value: 0x0
      - Tag:   DT_NULL
        Value: 0x0
DynamicSymbols: []
ProgramHeaders:
  - Type:     PT_LOAD
    Flags:    [ PF_R, PF_X ]
    FirstSec: .gnu.hash
    LastSec:  .dynamic

## Check we report a proper warning when a hash table goes past the end of the file.

## Case A: the 'maskwords' field is set so that the table goes past the end of the file.
# RUN: yaml2obj --docnum=1 -DBITS=64 -DMACHINE=EM_X86_64 -D MASKWORDS=4294967295 %s -o %t.err.maskwords
# RUN: llvm-readobj --gnu-hash-table %t.err.maskwords 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t.err.maskwords -DMASKWORDS=4294967295 -DNBUCKETS=3 --check-prefix=ERR
# RUN: llvm-readelf --gnu-hash-table %t.err.maskwords 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t.err.maskwords -DMASKWORDS=4294967295 -DNBUCKETS=3 --check-prefix=ERR

## Case B: the 'nbuckets' field is set so that the table goes past the end of the file.
# RUN: yaml2obj --docnum=1 -DBITS=64 -DMACHINE=EM_X86_64 -D NBUCKETS=4294967295 %s -o %t.err.nbuckets
# RUN: llvm-readobj --gnu-hash-table %t.err.nbuckets 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t.err.nbuckets -DMASKWORDS=2 -DNBUCKETS=4294967295 --check-prefix=ERR
# RUN: llvm-readelf --gnu-hash-table %t.err.nbuckets 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t.err.nbuckets -DMASKWORDS=2 -DNBUCKETS=4294967295 --check-prefix=ERR

# ERR:      GnuHashTable {
# ERR-NEXT:   Num Buckets: [[NBUCKETS]]
# ERR-NEXT:   First Hashed Symbol Index: 1
# ERR-NEXT:   Num Mask Words: [[MASKWORDS]]
# ERR-NEXT:   Shift Count: 2
# ERR-NEXT: warning: '[[FILE]]': unable to dump the SHT_GNU_HASH section at 0x78: it goes past the end of the file
# ERR-NEXT: }

## Check we report a single warning about the broken GNU hash table when both
## --gnu-hash-table and --elf-hash-histogram options are requested.
# RUN: llvm-readelf --gnu-hash-table --elf-hash-histogram %t.err.nbuckets 2>&1 | \
# RUN:   FileCheck %s -DFILE=%t.err.nbuckets -DMASKWORDS=2 -DNBUCKETS=4294967295 --check-prefix=ERR --implicit-check-not=warning