From 87481068fddf29e913b129b9c962ba761ae478c8 Mon Sep 17 00:00:00 2001 From: Georgii Rymar Date: Fri, 27 Nov 2020 11:57:03 +0300 Subject: [PATCH] [llvm-readelf] - Switch to using from `reportWarning` to `reportUniqueWarning` in `DynRegionInfo`. This is a part of the plan we had previously to convert all calls to `reportUniqueWarning` and then rename it to just `reportWarning`. I was a bit unsure about this particular change at first, because it doesn't add a new functionality: seems it is impossible to trigger a warning duplication currently. At the same time I find the idea of the plan mentioned very reasonable. And with that we will be sure that `DynRegionInfo` can't report duplicate warnings, what looks like a nice feature for possible refactorings and further tool development. Differential revision: https://reviews.llvm.org/D92224 --- llvm/tools/llvm-readobj/ELFDumper.cpp | 36 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index f89d887..d4f716a 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -125,9 +125,11 @@ template struct RelSymbol { /// the size, entity size and virtual address are different entries in arbitrary /// order (DT_REL, DT_RELSZ, DT_RELENT for example). struct DynRegionInfo { - DynRegionInfo(const Binary &Owner) : Obj(&Owner) {} - DynRegionInfo(const Binary &Owner, const uint8_t *A, uint64_t S, uint64_t ES) - : Addr(A), Size(S), EntSize(ES), Obj(&Owner) {} + DynRegionInfo(const Binary &Owner, const ObjDumper &D) + : Obj(&Owner), Dumper(&D) {} + DynRegionInfo(const Binary &Owner, const ObjDumper &D, const uint8_t *A, + uint64_t S, uint64_t ES) + : Addr(A), Size(S), EntSize(ES), Obj(&Owner), Dumper(&D) {} /// Address in current address space. const uint8_t *Addr = nullptr; @@ -138,6 +140,8 @@ struct DynRegionInfo { /// Owner object. Used for error reporting. const Binary *Obj; + /// Dumper used for error reporting. + const ObjDumper *Dumper; /// Error prefix. Used for error reporting to provide more information. std::string Context; /// Region size name. Used for error reporting. @@ -156,13 +160,11 @@ struct DynRegionInfo { const uint64_t ObjSize = Obj->getMemoryBufferRef().getBufferSize(); if (Size > ObjSize - Offset) { - reportWarning( - createError("unable to read data at 0x" + Twine::utohexstr(Offset) + - " of size 0x" + Twine::utohexstr(Size) + " (" + - SizePrintName + - "): it goes past the end of the file of size 0x" + - Twine::utohexstr(ObjSize)), - Obj->getFileName()); + Dumper->reportUniqueWarning(createError( + "unable to read data at 0x" + Twine::utohexstr(Offset) + + " of size 0x" + Twine::utohexstr(Size) + " (" + SizePrintName + + "): it goes past the end of the file of size 0x" + + Twine::utohexstr(ObjSize))); return {Start, Start}; } @@ -180,7 +182,7 @@ struct DynRegionInfo { (" or " + EntSizePrintName + " (0x" + Twine::utohexstr(EntSize) + ")") .str(); - reportWarning(createError(Msg.c_str()), Obj->getFileName()); + Dumper->reportUniqueWarning(createError(Msg.c_str())); return {Start, Start}; } }; @@ -311,7 +313,7 @@ private: ") + size (0x" + Twine::utohexstr(Size) + ") is greater than the file size (0x" + Twine::utohexstr(Obj.getBufSize()) + ")"); - return DynRegionInfo(ObjF, Obj.base() + Offset, Size, EntSize); + return DynRegionInfo(ObjF, *this, Obj.base() + Offset, Size, EntSize); } void printAttributes(); @@ -1928,7 +1930,7 @@ void ELFDumper::loadDynamicTable() { if (!DynamicPhdr && !DynamicSec) return; - DynRegionInfo FromPhdr(ObjF); + DynRegionInfo FromPhdr(ObjF, *this); bool IsPhdrTableValid = false; if (DynamicPhdr) { // Use cantFail(), because p_offset/p_filesz fields of a PT_DYNAMIC are @@ -1944,7 +1946,7 @@ void ELFDumper::loadDynamicTable() { // Ignore sh_entsize and use the expected value for entry size explicitly. // This allows us to dump dynamic sections with a broken sh_entsize // field. - DynRegionInfo FromSec(ObjF); + DynRegionInfo FromSec(ObjF, *this); bool IsSecTableValid = false; if (DynamicSec) { Expected RegOrErr = @@ -2007,8 +2009,8 @@ template ELFDumper::ELFDumper(const object::ELFObjectFile &O, ScopedPrinter &Writer) : ObjDumper(Writer, O.getFileName()), ObjF(O), Obj(*O.getELFFile()), - DynRelRegion(O), DynRelaRegion(O), DynRelrRegion(O), DynPLTRelRegion(O), - DynamicTable(O) { + DynRelRegion(O, *this), DynRelaRegion(O, *this), DynRelrRegion(O, *this), + DynPLTRelRegion(O, *this), DynamicTable(O, *this) { if (opts::Output == opts::GNU) ELFDumperStyle.reset(new GNUStyle(Writer, *this)); else @@ -2117,7 +2119,7 @@ void ELFDumper::parseDynamicTable() { // If we can't map the DT_SYMTAB value to an address (e.g. when there are // no program headers), we ignore its value. if (const uint8_t *VA = toMappedAddr(Dyn.getTag(), Dyn.getPtr())) { - DynSymFromTable.emplace(ObjF); + DynSymFromTable.emplace(ObjF, *this); DynSymFromTable->Addr = VA; DynSymFromTable->EntSize = sizeof(Elf_Sym); DynSymFromTable->EntSizePrintName = ""; -- 2.7.4