Skip to content

Commit 5055534

Browse files
author
George Rimar
committed
[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
1 parent e573a9c commit 5055534

File tree

2 files changed

+26
-39
lines changed

2 files changed

+26
-39
lines changed

llvm/include/llvm/Object/ELFObjectFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
239239
using Elf_Rela = typename ELFT::Rela;
240240
using Elf_Dyn = typename ELFT::Dyn;
241241

242+
SectionRef toSectionRef(const Elf_Shdr *Sec) const {
243+
return SectionRef(toDRI(Sec), this);
244+
}
245+
242246
private:
243247
ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
244248
const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,11 +4749,14 @@ void DumpStyle<ELFT>::printStackSize(const ELFObjectFile<ELFT> *Obj,
47494749
Data, &Offset);
47504750
}
47514751

4752-
template <class ELFT>
4753-
SectionRef toSectionRef(const ObjectFile *Obj, const typename ELFT::Shdr *Sec) {
4754-
DataRefImpl DRI;
4755-
DRI.p = reinterpret_cast<uintptr_t>(Sec);
4756-
return SectionRef(DRI, Obj);
4752+
// Used for printing section names in places where possible errors can be
4753+
// ignored.
4754+
static StringRef getSectionName(const SectionRef &Sec) {
4755+
Expected<StringRef> NameOrErr = Sec.getName();
4756+
if (NameOrErr)
4757+
return *NameOrErr;
4758+
consumeError(NameOrErr.takeError());
4759+
return "<?>";
47574760
}
47584761

47594762
template <class ELFT>
@@ -4764,16 +4767,11 @@ void DumpStyle<ELFT>::printNonRelocatableStackSizes(
47644767
const ELFFile<ELFT> *EF = Obj->getELFFile();
47654768
StringRef FileStr = Obj->getFileName();
47664769
for (const SectionRef &Sec : Obj->sections()) {
4767-
StringRef SectionName;
4768-
if (Expected<StringRef> NameOrErr = Sec.getName())
4769-
SectionName = *NameOrErr;
4770-
else
4771-
consumeError(NameOrErr.takeError());
4772-
4773-
const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
4770+
StringRef SectionName = getSectionName(Sec);
47744771
if (!SectionName.startswith(".stack_sizes"))
47754772
continue;
47764773
PrintHeader();
4774+
const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
47774775
ArrayRef<uint8_t> Contents =
47784776
unwrapOrError(this->FileName, EF->getSectionContents(ElfSec));
47794777
DataExtractor Data(
@@ -4798,8 +4796,7 @@ void DumpStyle<ELFT>::printNonRelocatableStackSizes(
47984796
FileStr);
47994797
}
48004798
uint64_t SymValue = Data.getAddress(&Offset);
4801-
printFunctionStackSize(Obj, SymValue,
4802-
toSectionRef<ELFT>(Obj, FunctionELFSec),
4799+
printFunctionStackSize(Obj, SymValue, Obj->toSectionRef(FunctionELFSec),
48034800
SectionName, Data, &Offset);
48044801
}
48054802
}
@@ -4848,7 +4845,7 @@ void DumpStyle<ELFT>::printRelocatableStackSizes(
48484845
if (!ContentsSectionNameOrErr->startswith(".stack_sizes"))
48494846
continue;
48504847
// Insert a mapping from the stack sizes section to its relocation section.
4851-
StackSizeRelocMap[toSectionRef<ELFT>(Obj, ContentsSec)] = Sec;
4848+
StackSizeRelocMap[Obj->toSectionRef(ContentsSec)] = Sec;
48524849
}
48534850

48544851
for (const auto &StackSizeMapEntry : StackSizeRelocMap) {
@@ -4857,12 +4854,7 @@ void DumpStyle<ELFT>::printRelocatableStackSizes(
48574854
const SectionRef &RelocSec = StackSizeMapEntry.second;
48584855

48594856
// Warn about stack size sections without a relocation section.
4860-
StringRef StackSizeSectionName;
4861-
if (Expected<StringRef> NameOrErr = StackSizesSec.getName())
4862-
StackSizeSectionName = *NameOrErr;
4863-
else
4864-
consumeError(NameOrErr.takeError());
4865-
4857+
StringRef StackSizeSectionName = getSectionName(StackSizesSec);
48664858
if (RelocSec == NullSection) {
48674859
reportWarning(createError("section " + StackSizeSectionName +
48684860
" does not have a corresponding "
@@ -4876,9 +4868,8 @@ void DumpStyle<ELFT>::printRelocatableStackSizes(
48764868
// described in it.
48774869
const Elf_Shdr *StackSizesELFSec =
48784870
Obj->getSection(StackSizesSec.getRawDataRefImpl());
4879-
const SectionRef FunctionSec = toSectionRef<ELFT>(
4880-
Obj, unwrapOrError(this->FileName,
4881-
EF->getSection(StackSizesELFSec->sh_link)));
4871+
const SectionRef FunctionSec = Obj->toSectionRef(unwrapOrError(
4872+
this->FileName, EF->getSection(StackSizesELFSec->sh_link)));
48824873

48834874
bool (*IsSupportedFn)(uint64_t);
48844875
RelocationResolver Resolver;
@@ -4889,21 +4880,13 @@ void DumpStyle<ELFT>::printRelocatableStackSizes(
48894880
Contents.size()),
48904881
Obj->isLittleEndian(), sizeof(Elf_Addr));
48914882
for (const RelocationRef &Reloc : RelocSec.relocations()) {
4892-
if (!IsSupportedFn(Reloc.getType())) {
4893-
StringRef RelocSectionName;
4894-
Expected<StringRef> NameOrErr = RelocSec.getName();
4895-
if (NameOrErr)
4896-
RelocSectionName = *NameOrErr;
4897-
else
4898-
consumeError(NameOrErr.takeError());
4899-
4900-
StringRef RelocName = EF->getRelocationTypeName(Reloc.getType());
4901-
reportError(
4902-
createStringError(object_error::parse_failed,
4903-
"unsupported relocation type in section %s: %s",
4904-
RelocSectionName.data(), RelocName.data()),
4905-
Obj->getFileName());
4906-
}
4883+
if (!IsSupportedFn(Reloc.getType()))
4884+
reportError(createStringError(
4885+
object_error::parse_failed,
4886+
"unsupported relocation type in section %s: %s",
4887+
getSectionName(RelocSec).data(),
4888+
EF->getRelocationTypeName(Reloc.getType()).data()),
4889+
Obj->getFileName());
49074890
this->printStackSize(Obj, Reloc, FunctionSec, StackSizeSectionName,
49084891
Resolver, Data);
49094892
}

0 commit comments

Comments
 (0)