Index: llvm/trunk/include/llvm/Object/ELFObjectFile.h =================================================================== --- llvm/trunk/include/llvm/Object/ELFObjectFile.h +++ llvm/trunk/include/llvm/Object/ELFObjectFile.h @@ -239,6 +239,10 @@ 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, Index: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp +++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp @@ -4749,11 +4749,14 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ // 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 @@ 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); }