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 @@ -157,6 +157,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 +190,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 +223,14 @@ 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: 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 @@ -2642,28 +2642,30 @@ } template -static bool checkHashTable(const ELFFile *Obj, - const typename ELFT::Hash *H, StringRef FileName) { - 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); - return false; +static Error checkHashTable(const ELFFile *Obj, + const typename ELFT::Hash *H, + bool *IsHeaderValid = nullptr) { + auto BuildError = [&](uint64_t Off, const Twine &Msg = "") { + return createError("the hash table at offset 0x" + Twine::utohexstr(Off) + + " goes past the end of the file (0x" + + Twine::utohexstr(Obj->getBufSize()) + ")" + Msg); }; // Each SHT_HASH section starts from two 32-bit fields: nbucket and nchain. const unsigned HeaderSize = 2 * sizeof(typename ELFT::Word); const uint64_t SecOffset = (const uint8_t *)H - Obj->base(); + + if (IsHeaderValid) + *IsHeaderValid = Obj->getBufSize() - SecOffset >= HeaderSize; + if (Obj->getBufSize() - SecOffset < HeaderSize) - return WarnAndReturn(SecOffset); + return BuildError(SecOffset); if (Obj->getBufSize() - SecOffset - HeaderSize < ((uint64_t)H->nbucket + H->nchain) * sizeof(typename ELFT::Word)) - return WarnAndReturn(SecOffset, ", nbucket = " + Twine(H->nbucket) + - ", nchain = " + Twine(H->nchain)); - return true; + return BuildError(SecOffset, ", nbucket = " + Twine(H->nbucket) + + ", nchain = " + Twine(H->nchain)); + return Error::success(); } template @@ -2690,11 +2692,21 @@ template void ELFDumper::printHashTable() { DictScope D(W, "HashTable"); - if (!HashTable || - !checkHashTable(ObjF->getELFFile(), HashTable, ObjF->getFileName())) + if (!HashTable) + return; + + bool IsHeaderValid; + Error Err = checkHashTable(ObjF->getELFFile(), HashTable, &IsHeaderValid); + if (IsHeaderValid) { + W.printNumber("Num Buckets", HashTable->nbucket); + W.printNumber("Num Chains", HashTable->nchain); + } + + if (Err) { + reportUniqueWarning(std::move(Err)); return; - W.printNumber("Num Buckets", HashTable->nbucket); - W.printNumber("Num Chains", HashTable->nchain); + } + W.printList("Buckets", HashTable->buckets()); W.printList("Chains", HashTable->chains()); } @@ -4026,7 +4038,9 @@ if (const Elf_Hash *SysVHash = this->dumper()->getHashTable()) { OS << "\n Symbol table of .hash for image:\n"; - if (checkHashTable(Obj, SysVHash, this->FileName)) + if (Error E = checkHashTable(Obj, SysVHash)) + this->reportUniqueWarning(std::move(E)); + else PrintHashTable(SysVHash); } @@ -4689,7 +4703,9 @@ void GNUStyle::printHashHistograms(const ELFFile *Obj) { // Print histogram for the .hash section. if (const Elf_Hash *HashTable = this->dumper()->getHashTable()) - if (checkHashTable(Obj, HashTable, this->FileName)) + if (Error E = checkHashTable(Obj, HashTable)) + this->reportUniqueWarning(std::move(E)); + else printHashHistogram(*HashTable); // Print histogram for the .gnu.hash section.