Index: llvm/test/tools/llvm-readobj/ELF/hash-table.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/hash-table.test +++ llvm/test/tools/llvm-readobj/ELF/hash-table.test @@ -98,6 +98,8 @@ # RUN: llvm-readobj --hash-table %t4.o 2>&1 | FileCheck %s --check-prefix=ERR1 -DFILE=%t4.o # ERR1: HashTable { +# ERR1-NEXT: Num Buckets: 0 +# ERR1-NEXT: Num Chains: 0 # ERR1-NEXT: warning: '[[FILE]]': the hash table at offset 0x2b1 goes past the end of the file (0x2b8){{$}} # ERR1-NEXT: } @@ -157,6 +159,8 @@ # RUN: FileCheck %s --check-prefix=ERR2 -DFILE=%t5.2.o --implicit-check-not="warning:" # ERR2: HashTable { +# ERR2: Num Buckets: 94 +# ERR2: Num Chains: 1 # ERR2-NEXT: warning: '[[FILE]]': the hash table at offset 0x54 goes past the end of the file (0x1d4), nbucket = 94, nchain = 1{{$}} # ERR2-NEXT: } @@ -188,6 +192,8 @@ # ERR3: warning: '[[FILE]]': hash table nchain (94) differs from symbol count derived from SHT_DYNSYM section header (1) # ERR3: HashTable { +# ERR3-NEXT: Num Buckets: 1 +# ERR3-NEXT: Num Chains: 94 # ERR3-NEXT: warning: '[[FILE]]': the hash table at offset 0x54 goes past the end of the file (0x1d4), nbucket = 1, nchain = 94{{$}} # ERR3-NEXT: } @@ -219,3 +225,16 @@ Sections: - Section: .hash - Section: .dynamic + +## Show we do not duplicate warnings when printing both the hash table and the hash histogram. +## Note that --elf-hash-histogram is only implemented for llvm-readelf currently. +# RUN: yaml2obj --docnum=3 %s -o %t4.o +# RUN: llvm-readelf --hash-table --elf-hash-histogram %t4.o 2>&1 \ +# RUN: | FileCheck %s --check-prefix=SINGLE-WARN -DFILE=%t4.o --implicit-check-not="warning:" + +# SINGLE-WARN: warning: '[[FILE]]': hash table nchain (0) differs from symbol count derived from SHT_DYNSYM section header (1) +# SINGLE-WARN-NEXT: HashTable { +# SINGLE-WARN-NEXT: Num Buckets: 0 +# SINGLE-WARN-NEXT: Num Chains: 0 +# SINGLE-WARN-NEXT: warning: '[[FILE]]': the hash table at offset 0x2b1 goes past the end of the file (0x2b8) +# SINGLE-WARN-NEXT: } Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -2636,14 +2636,14 @@ } template -static bool checkHashTable(const ELFFile *Obj, - const typename ELFT::Hash *H, StringRef FileName) { +static bool checkHashTable(const ELFDumper &Dumper, + const ELFFile *Obj, + const typename ELFT::Hash *H) { auto WarnAndReturn = [&](uint64_t Off, const Twine &Msg = "") { - reportWarning(createError("the hash table at offset 0x" + - Twine::utohexstr(Off) + - " goes past the end of the file (0x" + - Twine::utohexstr(Obj->getBufSize()) + ")" + Msg), - FileName); + Dumper.reportUniqueWarning( + createError("the hash table at offset 0x" + Twine::utohexstr(Off) + + " goes past the end of the file (0x" + + Twine::utohexstr(Obj->getBufSize()) + ")" + Msg)); return false; }; @@ -2662,11 +2662,13 @@ template void ELFDumper::printHashTable() { DictScope D(W, "HashTable"); - if (!HashTable || - !checkHashTable(ObjF->getELFFile(), HashTable, ObjF->getFileName())) + if (!HashTable) return; W.printNumber("Num Buckets", HashTable->nbucket); W.printNumber("Num Chains", HashTable->nchain); + if (!checkHashTable(*this, ObjF->getELFFile(), HashTable)) + return; + W.printList("Buckets", HashTable->buckets()); W.printList("Chains", HashTable->chains()); } @@ -4006,7 +4008,7 @@ if (const Elf_Hash *SysVHash = this->dumper()->getHashTable()) { OS << "\n Symbol table of .hash for image:\n"; - if (checkHashTable(Obj, SysVHash, this->FileName)) + if (checkHashTable(*this->dumper(), Obj, SysVHash)) PrintHashTable(SysVHash); } @@ -4564,7 +4566,7 @@ void GNUStyle::printHashHistogram(const ELFFile *Obj) { // Print histogram for .hash section if (const Elf_Hash *HashTable = this->dumper()->getHashTable()) { - if (!checkHashTable(Obj, HashTable, this->FileName)) + if (!checkHashTable(*this->dumper(), Obj, HashTable)) return; size_t NBucket = HashTable->nbucket;