llvm/llvm/test/tools/llvm-readobj/ELF/versym-invalid.test

## Test how llvm-readobj/llvm-readelf tools handle invalid SHT_GNU_versym sections.

## Check that we report a warning when sh_link references a non-existent section.

# RUN: yaml2obj --docnum=1 -DLINK=0xFF %s -o %t1
# RUN: llvm-readelf -V %t1 2>&1 | FileCheck -DFILE=%t1 %s --check-prefix=GNU-INVALID-LINK
# RUN: llvm-readobj -V %t1 2>&1 | FileCheck -DFILE=%t1 %s --check-prefix=LLVM-INVALID-LINK

# GNU-INVALID-LINK:      Version symbols section '.gnu.version' contains 1 entries:
# GNU-INVALID-LINK-NEXT: warning: '[[FILE]]': invalid section linked to SHT_GNU_versym section with index 1: invalid section index: 255
# GNU-INVALID-LINK-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 255 (<corrupt>)

# LLVM-INVALID-LINK:      VersionSymbols [
# LLVM-INVALID-LINK-NEXT: warning: '[[FILE]]': invalid section linked to SHT_GNU_versym section with index 1: invalid section index: 255
# LLVM-INVALID-LINK-NEXT: ]

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_EXEC
Sections:
  - Name:    .gnu.version
    Type:    SHT_GNU_versym
    Link:    [[LINK]]
    Entries: [ 0 ]

## Check that we report a warning when the sh_link field of a SHT_GNU_versym section does not reference
## a dynamic symbol table section.

# RUN: yaml2obj --docnum=1 -DLINK=0x0 %s -o %t2
# RUN: llvm-readelf -V %t2 2>&1 | FileCheck -DFILE=%t2 %s --check-prefix=INVALID-SYMBOL-TABLE-GNU
# RUN: llvm-readobj -V %t2 2>&1 | FileCheck -DFILE=%t2 %s --check-prefix=INVALID-SYMBOL-TABLE-LLVM

# INVALID-SYMBOL-TABLE-GNU:      Version symbols section '.gnu.version' contains 1 entries:
# INVALID-SYMBOL-TABLE-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 0 ()
# INVALID-SYMBOL-TABLE-GNU-NEXT:  warning: '[[FILE]]': invalid section linked to SHT_GNU_versym section with index 1: expected SHT_DYNSYM, but got SHT_NULL
# INVALID-SYMBOL-TABLE-GNU-NEXT:  000:   0 (*local*)

# INVALID-SYMBOL-TABLE-LLVM:      VersionSymbols [
# INVALID-SYMBOL-TABLE-LLVM-NEXT: warning: '[[FILE]]': invalid section linked to SHT_GNU_versym section with index 1: expected SHT_DYNSYM, but got SHT_NULL
# INVALID-SYMBOL-TABLE-LLVM-NEXT: ]

## Check we report a warning when something is wrong with a string table linked to a symbol table that
## is linked with SHT_GNU_versym. In this case we are unable to produce LLVM style output,
## but GNU style is fine because it does not need that string table.

# RUN: yaml2obj --docnum=2 %s -o %t3
# RUN: llvm-readelf -V %t3 2>&1 | FileCheck -DFILE=%t3 %s --check-prefix=INVALID-STRING-TABLE-GNU
# RUN: llvm-readobj -V %t3 2>&1 | FileCheck -DFILE=%t3 %s --check-prefix=INVALID-STRING-TABLE-LLVM

# INVALID-STRING-TABLE-GNU:      Version symbols section '.gnu.version' contains 1 entries:
# INVALID-STRING-TABLE-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 3 (.dynsym)
# INVALID-STRING-TABLE-GNU-NEXT:  warning: '[[FILE]]': can't get a string table for the symbol table linked to SHT_GNU_versym section with index 1: invalid string table linked to SHT_DYNSYM section with index 3: invalid sh_type for string table section [index 2]: expected SHT_STRTAB, but got SHT_NULL
# INVALID-STRING-TABLE-GNU-NEXT:  000:   0 (*local*)

# INVALID-STRING-TABLE-LLVM:      VersionSymbols [
# INVALID-STRING-TABLE-LLVM-NEXT: warning: '[[FILE]]': can't get a string table for the symbol table linked to SHT_GNU_versym section with index 1: invalid string table linked to SHT_DYNSYM section with index 3: invalid sh_type for string table section [index 2]: expected SHT_STRTAB, but got SHT_NULL
# INVALID-STRING-TABLE-LLVM-NEXT: ]

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_EXEC
Sections:
  - Name:    .gnu.version
    Type:    SHT_GNU_versym
    Entries: [ 0 ]
  - Name:    .dynstr
    Type:    SHT_NULL
DynamicSymbols: []

## Check we report a warning when a SHT_GNU_versym section is not correctly aligned in memory.

# RUN: yaml2obj --docnum=3 %s -DSHOFFSET=0xffff -o %t4
# RUN: llvm-readelf -V %t4 2>&1 | FileCheck -DFILE=%t4 %s --check-prefix=MISALIGNED-GNU
# RUN: llvm-readobj -V %t4 2>&1 | FileCheck -DFILE=%t4 %s --check-prefix=MISALIGNED-LLVM

# MISALIGNED-GNU:      Version symbols section '.gnu.version' contains 1 entries:
# MISALIGNED-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x00ffff  Link: 2 (.dynsym)
# MISALIGNED-GNU-NEXT:  warning: '[[FILE]]': the SHT_GNU_versym section with index 1 is misaligned

# MISALIGNED-LLVM:      VersionSymbols [
# MISALIGNED-LLVM-NEXT:  warning: '[[FILE]]': the SHT_GNU_versym section with index 1 is misaligned
# MISALIGNED-LLVM-NEXT: ]

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_EXEC
Sections:
  - Name:     .gnu.version
    Type:     SHT_GNU_versym
    Entries:  [ 0 ]
    ShOffset: [[SHOFFSET=<none>]]
    ShName:   [[SHNAME=<none>]]
  - Name:   .dynsym
    Type:   SHT_DYNSYM
    ShName: [[DYNSYMNAME=<none>]]
DynamicSymbols: []

## Check that llvm-readelf reports a warning when the name of the SHT_GNU_versym section can't be read.

# RUN: yaml2obj %s --docnum=3 -DSHNAME=0xFF -o %t.invalid.name
# RUN: llvm-readobj -V %t.invalid.name 2>&1 | \
# RUN:   FileCheck %s --check-prefix=INVALID-NAME-LLVM -DFILE=%t.invalid.name --implicit-check-not=warning:
# RUN: llvm-readelf -V %t.invalid.name 2>&1 | \
# RUN:   FileCheck %s --check-prefix=INVALID-NAME-GNU -DFILE=%t.invalid.name --implicit-check-not=warning:

# INVALID-NAME-LLVM:      VersionSymbols [
# INVALID-NAME-LLVM-NEXT:   Symbol {
# INVALID-NAME-LLVM-NEXT:     Version: 0
# INVALID-NAME-LLVM-NEXT:     Name:
# INVALID-NAME-LLVM-NEXT:   }
# INVALID-NAME-LLVM-NEXT: ]

# INVALID-NAME-GNU:      warning: '[[FILE]]': unable to get the name of SHT_GNU_versym section with index 1: a section [index 1] has an invalid sh_name (0xff) offset which goes past the end of the section name string table
# INVALID-NAME-GNU-NEXT: Version symbols section '<?>' contains 1 entries:
# INVALID-NAME-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 2 (.dynsym)
# INVALID-NAME-GNU-NEXT:   000:   0 (*local*)

## Check that llvm-readelf reports a warning when we are unable to read the name of
## the section linked with the SHT_GNU_verneed section (usually ".dynsym").

# RUN: yaml2obj %s --docnum=3 -DDYNSYMNAME=0xFF -o %t.invalid.name2
# RUN: llvm-readobj -V %t.invalid.name2 2>&1 | \
# RUN:   FileCheck %s --check-prefix=INVALID-NAME-LLVM -DFILE=%t.invalid.name2 --implicit-check-not=warning:
# RUN: llvm-readelf -V %t.invalid.name2 2>&1 | \
# RUN:   FileCheck %s --check-prefix=INVALID-NAME2-GNU -DFILE=%t.invalid.name2 --implicit-check-not=warning:

# INVALID-NAME2-GNU:      Version symbols section '.gnu.version' contains 1 entries:
# INVALID-NAME2-GNU-NEXT: warning: '[[FILE]]': unable to get the name of SHT_DYNSYM section with index 2: a section [index 2] has an invalid sh_name (0xff) offset which goes past the end of the section name string table
# INVALID-NAME2-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 2 (<?>)
# INVALID-NAME2-GNU-NEXT:   000:   0 (*local*)

## Check we report a warning when a SHT_GNU_versym section has an invalid entry size.

# RUN: yaml2obj --docnum=4 %s -o %t5
# RUN: llvm-readelf -V --dyn-syms %t5 2>&1 | FileCheck -DFILE=%t5 %s --check-prefix=INVALID-ENT-SIZE-GNU
# RUN: llvm-readobj -V --dyn-syms %t5 2>&1 | FileCheck -DFILE=%t5 %s --check-prefix=INVALID-ENT-SIZE-LLVM

# INVALID-ENT-SIZE-GNU:      Symbol table '.dynsym' contains 2 entries:
# INVALID-ENT-SIZE-GNU-NEXT:    Num:    Value          Size Type    Bind   Vis       Ndx Name
# INVALID-ENT-SIZE-GNU-NEXT: warning: '[[FILE]]': section [index 1] has invalid sh_entsize: expected 2, but got 3
# INVALID-ENT-SIZE-GNU-NEXT:      0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND @<corrupt>
# INVALID-ENT-SIZE-GNU-NEXT:      1: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND foo@<corrupt>
# INVALID-ENT-SIZE-GNU:      Version symbols section '.gnu.version' contains 1 entries:
# INVALID-ENT-SIZE-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 2 (.dynsym)
# INVALID-ENT-SIZE-GNU-NEXT: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has invalid sh_entsize: expected 2, but got 3

# INVALID-ENT-SIZE-LLVM:      DynamicSymbols [
# INVALID-ENT-SIZE-LLVM-NEXT: warning: '[[FILE]]': section [index 1] has invalid sh_entsize: expected 2, but got 3
# INVALID-ENT-SIZE-LLVM-NEXT:   Symbol {
# INVALID-ENT-SIZE-LLVM-NEXT:     Name: @<corrupt> (0)
# INVALID-ENT-SIZE-LLVM-NEXT:     Value: 0x0
# INVALID-ENT-SIZE-LLVM-NEXT:     Size: 0
# INVALID-ENT-SIZE-LLVM-NEXT:     Binding: Local (0x0)
# INVALID-ENT-SIZE-LLVM-NEXT:     Type: None (0x0)
# INVALID-ENT-SIZE-LLVM-NEXT:     Other: 0
# INVALID-ENT-SIZE-LLVM-NEXT:     Section: Undefined (0x0)
# INVALID-ENT-SIZE-LLVM-NEXT:   }
# INVALID-ENT-SIZE-LLVM-NEXT:   Symbol {
# INVALID-ENT-SIZE-LLVM-NEXT:     Name: foo@<corrupt> (1)
# INVALID-ENT-SIZE-LLVM-NEXT:     Value: 0x0
# INVALID-ENT-SIZE-LLVM-NEXT:     Size: 0
# INVALID-ENT-SIZE-LLVM-NEXT:     Binding: Local (0x0)
# INVALID-ENT-SIZE-LLVM-NEXT:     Type: None (0x0)
# INVALID-ENT-SIZE-LLVM-NEXT:     Other: 0
# INVALID-ENT-SIZE-LLVM-NEXT:     Section: Undefined (0x0)
# INVALID-ENT-SIZE-LLVM-NEXT:   }
# INVALID-ENT-SIZE-LLVM-NEXT: ]
# INVALID-ENT-SIZE-LLVM:      VersionSymbols [
# INVALID-ENT-SIZE-LLVM-NEXT: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has invalid sh_entsize: expected 2, but got 3
# INVALID-ENT-SIZE-LLVM-NEXT: ]

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_EXEC
Sections:
  - Name:    .gnu.version
    Type:    SHT_GNU_versym
    Entries: [ 0 ]
    EntSize: 3
DynamicSymbols:
  - Name: foo

## Check we report a warning when the number of version entries does not match the number of symbols in the associated symbol table.

# RUN: yaml2obj --docnum=5 %s -o %t6
# RUN: llvm-readelf -V %t6 2>&1 | FileCheck -DFILE=%t6 %s --check-prefix=SYMBOLS-NUM-MISMATCH-GNU
# RUN: llvm-readobj -V %t6 2>&1 | FileCheck -DFILE=%t6 %s --check-prefix=SYMBOLS-NUM-MISMATCH-LLVM

# SYMBOLS-NUM-MISMATCH-GNU:      Version symbols section '.gnu.version' contains 2 entries:
# SYMBOLS-NUM-MISMATCH-GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 2 (.dynsym)
# SYMBOLS-NUM-MISMATCH-GNU-NEXT:  warning: '[[FILE]]': SHT_GNU_versym section with index 1: the number of entries (2) does not match the number of symbols (3) in the symbol table with index 2
# SYMBOLS-NUM-MISMATCH-GNU-NEXT:   000:   0 (*local*)       1 (*global*)

# SYMBOLS-NUM-MISMATCH-LLVM:      VersionSymbols [
# SYMBOLS-NUM-MISMATCH-LLVM-NEXT: warning: '[[FILE]]': SHT_GNU_versym section with index 1: the number of entries (2) does not match the number of symbols (3) in the symbol table with index 2
# SYMBOLS-NUM-MISMATCH-LLVM-NEXT: ]

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_EXEC
Sections:
  - Name:    .gnu.version
    Type:    SHT_GNU_versym
    Entries: [ 0, 1 ]
DynamicSymbols:
  - Name: foo
  - Name: bar

## Check we can dump a SHT_GNU_versym section when it is linked to a custom dynamic symbol
## table that is not called ".dynsym".

# RUN: yaml2obj --docnum=6 %s -o %t7
# RUN: llvm-readelf -V %t7 2>&1 | FileCheck -DFILE=%t7 %s --check-prefix=CUSTOM-SYMTAB-GNU
# RUN: llvm-readobj -V %t7 2>&1 | FileCheck -DFILE=%t7 %s --check-prefix=CUSTOM-SYMTAB-LLVM

# CUSTOM-SYMTAB-GNU:      Version symbols section '.gnu.version' contains 1 entries:
# CUSTOM-SYMTAB-GNU-NEXT:   Addr: 0000000000000000  Offset: 0x000040  Link: 2 (.foo.dynsym)
# CUSTOM-SYMTAB-GNU-NEXT:    000:   0 (*local*)

# CUSTOM-SYMTAB-LLVM:      VersionSymbols [
# CUSTOM-SYMTAB-LLVM-NEXT:  Symbol {
# CUSTOM-SYMTAB-LLVM-NEXT:    Version: 0
# CUSTOM-SYMTAB-LLVM-NEXT:    Name:
# CUSTOM-SYMTAB-LLVM-NEXT:  }
# CUSTOM-SYMTAB-LLVM-NEXT: ]

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_EXEC
Sections:
  - Name:    .gnu.version
    Type:    SHT_GNU_versym
    Link:    .foo.dynsym
    Entries: [ 0 ]
## A custom empty dynamic symbol table with a null entry.
  - Name:    .foo.dynsym
    Type:    SHT_DYNSYM
    Link:    .dynstr
    EntSize: 24
    Size:    24
DynamicSymbols:
  - Name: foo
  - Name: bar

## Version index in a SHT_GNU_versym section overflows the version map.
## Check we report it when trying to dump dynamic symbols.

# RUN: yaml2obj %s --docnum=7 -o %t8
# RUN: llvm-readobj --dyn-syms %t8 2>&1 \
# RUN:   | FileCheck -DFILE=%t8 --implicit-check-not=warning --check-prefix=VERSION-OVERFLOW-LLVM %s
# RUN: llvm-readelf --dyn-syms %t8 2>&1 \
# RUN:   | FileCheck -DFILE=%t8 --implicit-check-not=warning --check-prefix=VERSION-OVERFLOW-GNU %s

# VERSION-OVERFLOW-LLVM:      DynamicSymbols [
# VERSION-OVERFLOW-LLVM-NEXT: warning: '[[FILE]]': SHT_GNU_versym section refers to a version index 255 which is missing
# VERSION-OVERFLOW-LLVM-NEXT:   Symbol {
# VERSION-OVERFLOW-LLVM-NEXT:     Name: @<corrupt> (0)
# VERSION-OVERFLOW-LLVM:      warning: '[[FILE]]': SHT_GNU_versym section refers to a version index 254 which is missing
# VERSION-OVERFLOW-LLVM-NEXT:   Symbol {
# VERSION-OVERFLOW-LLVM-NEXT:     Name: foo@<corrupt> (5)
# VERSION-OVERFLOW-LLVM:        Symbol {
# VERSION-OVERFLOW-LLVM-NEXT:     Name: bar@<corrupt> (1)

# VERSION-OVERFLOW-GNU:      Symbol table '.dynsym' contains 3 entries:
# VERSION-OVERFLOW-GNU-NEXT:  Num:    Value          Size Type    Bind   Vis       Ndx Name
# VERSION-OVERFLOW-GNU-NEXT: warning: '[[FILE]]': SHT_GNU_versym section refers to a version index 255 which is missing
# VERSION-OVERFLOW-GNU-NEXT:     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND @<corrupt>
# VERSION-OVERFLOW-GNU-NEXT: warning: '[[FILE]]': SHT_GNU_versym section refers to a version index 254 which is missing
# VERSION-OVERFLOW-GNU-NEXT:     1: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND foo@<corrupt>
# VERSION-OVERFLOW-GNU-NEXT:     2: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND bar@<corrupt

--- !ELF
FileHeader:
  Class: ELFCLASS64
  Data:  ELFDATA2LSB
  Type:  ET_DYN
Sections:
  - Name:    .gnu.version
    Type:    SHT_GNU_versym
    Entries: [ 0xFF, 0xFE, 0xFF ]
DynamicSymbols:
  - Name: foo
  - Name: bar