From 505553495c43b963512d7ae33a2aa9fe6769a15f Mon Sep 17 00:00:00 2001 From: George Rimar Date: Tue, 17 Sep 2019 08:53:18 +0000 Subject: [PATCH] [llvm-readobj] - Refactor the code. It's a straightforward refactoring that allows to simplify and encapsulate the code. Differential revision: https://reviews.llvm.org/D67624 llvm-svn: 372083 --- llvm/include/llvm/Object/ELFObjectFile.h | 4 +++ llvm/tools/llvm-readobj/ELFDumper.cpp | 61 ++++++++++++-------------------- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h index 8dac02c..774d8c3 100644 --- a/llvm/include/llvm/Object/ELFObjectFile.h +++ b/llvm/include/llvm/Object/ELFObjectFile.h @@ -239,6 +239,10 @@ public: using Elf_Rela = typename ELFT::Rela; using Elf_Dyn = typename ELFT::Dyn; + SectionRef toSectionRef(const Elf_Shdr *Sec) const { + return SectionRef(toDRI(Sec), this); + } + private: ELFObjectFile(MemoryBufferRef Object, ELFFile EF, const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec, diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 3631efa..7876f41 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -4749,11 +4749,14 @@ void DumpStyle::printStackSize(const ELFObjectFile *Obj, Data, &Offset); } -template -SectionRef toSectionRef(const ObjectFile *Obj, const typename ELFT::Shdr *Sec) { - DataRefImpl DRI; - DRI.p = reinterpret_cast(Sec); - return SectionRef(DRI, Obj); +// Used for printing section names in places where possible errors can be +// ignored. +static StringRef getSectionName(const SectionRef &Sec) { + Expected NameOrErr = Sec.getName(); + if (NameOrErr) + return *NameOrErr; + consumeError(NameOrErr.takeError()); + return ""; } template @@ -4764,16 +4767,11 @@ void DumpStyle::printNonRelocatableStackSizes( const ELFFile *EF = Obj->getELFFile(); StringRef FileStr = Obj->getFileName(); for (const SectionRef &Sec : Obj->sections()) { - StringRef SectionName; - if (Expected NameOrErr = Sec.getName()) - SectionName = *NameOrErr; - else - consumeError(NameOrErr.takeError()); - - const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl()); + StringRef SectionName = getSectionName(Sec); if (!SectionName.startswith(".stack_sizes")) continue; PrintHeader(); + const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl()); ArrayRef Contents = unwrapOrError(this->FileName, EF->getSectionContents(ElfSec)); DataExtractor Data( @@ -4798,8 +4796,7 @@ void DumpStyle::printNonRelocatableStackSizes( FileStr); } uint64_t SymValue = Data.getAddress(&Offset); - printFunctionStackSize(Obj, SymValue, - toSectionRef(Obj, FunctionELFSec), + printFunctionStackSize(Obj, SymValue, Obj->toSectionRef(FunctionELFSec), SectionName, Data, &Offset); } } @@ -4848,7 +4845,7 @@ void DumpStyle::printRelocatableStackSizes( if (!ContentsSectionNameOrErr->startswith(".stack_sizes")) continue; // Insert a mapping from the stack sizes section to its relocation section. - StackSizeRelocMap[toSectionRef(Obj, ContentsSec)] = Sec; + StackSizeRelocMap[Obj->toSectionRef(ContentsSec)] = Sec; } for (const auto &StackSizeMapEntry : StackSizeRelocMap) { @@ -4857,12 +4854,7 @@ void DumpStyle::printRelocatableStackSizes( const SectionRef &RelocSec = StackSizeMapEntry.second; // Warn about stack size sections without a relocation section. - StringRef StackSizeSectionName; - if (Expected NameOrErr = StackSizesSec.getName()) - StackSizeSectionName = *NameOrErr; - else - consumeError(NameOrErr.takeError()); - + StringRef StackSizeSectionName = getSectionName(StackSizesSec); if (RelocSec == NullSection) { reportWarning(createError("section " + StackSizeSectionName + " does not have a corresponding " @@ -4876,9 +4868,8 @@ void DumpStyle::printRelocatableStackSizes( // described in it. const Elf_Shdr *StackSizesELFSec = Obj->getSection(StackSizesSec.getRawDataRefImpl()); - const SectionRef FunctionSec = toSectionRef( - Obj, unwrapOrError(this->FileName, - EF->getSection(StackSizesELFSec->sh_link))); + const SectionRef FunctionSec = Obj->toSectionRef(unwrapOrError( + this->FileName, EF->getSection(StackSizesELFSec->sh_link))); bool (*IsSupportedFn)(uint64_t); RelocationResolver Resolver; @@ -4889,21 +4880,13 @@ void DumpStyle::printRelocatableStackSizes( Contents.size()), Obj->isLittleEndian(), sizeof(Elf_Addr)); for (const RelocationRef &Reloc : RelocSec.relocations()) { - if (!IsSupportedFn(Reloc.getType())) { - StringRef RelocSectionName; - Expected NameOrErr = RelocSec.getName(); - if (NameOrErr) - RelocSectionName = *NameOrErr; - else - consumeError(NameOrErr.takeError()); - - StringRef RelocName = EF->getRelocationTypeName(Reloc.getType()); - reportError( - createStringError(object_error::parse_failed, - "unsupported relocation type in section %s: %s", - RelocSectionName.data(), RelocName.data()), - Obj->getFileName()); - } + if (!IsSupportedFn(Reloc.getType())) + reportError(createStringError( + object_error::parse_failed, + "unsupported relocation type in section %s: %s", + getSectionName(RelocSec).data(), + EF->getRelocationTypeName(Reloc.getType()).data()), + Obj->getFileName()); this->printStackSize(Obj, Reloc, FunctionSec, StackSizeSectionName, Resolver, Data); } -- 2.7.4