[llvm] r372083 - [llvm-readobj] - Refactor the code.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 17 01:53:18 PDT 2019


Author: grimar
Date: Tue Sep 17 01:53:18 2019
New Revision: 372083

URL: http://llvm.org/viewvc/llvm-project?rev=372083&view=rev
Log:
[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

Modified:
    llvm/trunk/include/llvm/Object/ELFObjectFile.h
    llvm/trunk/tools/llvm-readobj/ELFDumper.cpp

Modified: llvm/trunk/include/llvm/Object/ELFObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELFObjectFile.h?rev=372083&r1=372082&r2=372083&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELFObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ELFObjectFile.h Tue Sep 17 01:53:18 2019
@@ -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<ELFT> EF,
                 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,

Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=372083&r1=372082&r2=372083&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Tue Sep 17 01:53:18 2019
@@ -4749,11 +4749,14 @@ void DumpStyle<ELFT>::printStackSize(con
                                Data, &Offset);
 }
 
-template <class ELFT>
-SectionRef toSectionRef(const ObjectFile *Obj, const typename ELFT::Shdr *Sec) {
-  DataRefImpl DRI;
-  DRI.p = reinterpret_cast<uintptr_t>(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<StringRef> NameOrErr = Sec.getName();
+  if (NameOrErr)
+    return *NameOrErr;
+  consumeError(NameOrErr.takeError());
+  return "<?>";
 }
 
 template <class ELFT>
@@ -4764,16 +4767,11 @@ void DumpStyle<ELFT>::printNonRelocatabl
   const ELFFile<ELFT> *EF = Obj->getELFFile();
   StringRef FileStr = Obj->getFileName();
   for (const SectionRef &Sec : Obj->sections()) {
-    StringRef SectionName;
-    if (Expected<StringRef> 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<uint8_t> Contents =
         unwrapOrError(this->FileName, EF->getSectionContents(ElfSec));
     DataExtractor Data(
@@ -4798,8 +4796,7 @@ void DumpStyle<ELFT>::printNonRelocatabl
             FileStr);
       }
       uint64_t SymValue = Data.getAddress(&Offset);
-      printFunctionStackSize(Obj, SymValue,
-                             toSectionRef<ELFT>(Obj, FunctionELFSec),
+      printFunctionStackSize(Obj, SymValue, Obj->toSectionRef(FunctionELFSec),
                              SectionName, Data, &Offset);
     }
   }
@@ -4848,7 +4845,7 @@ void DumpStyle<ELFT>::printRelocatableSt
     if (!ContentsSectionNameOrErr->startswith(".stack_sizes"))
       continue;
     // Insert a mapping from the stack sizes section to its relocation section.
-    StackSizeRelocMap[toSectionRef<ELFT>(Obj, ContentsSec)] = Sec;
+    StackSizeRelocMap[Obj->toSectionRef(ContentsSec)] = Sec;
   }
 
   for (const auto &StackSizeMapEntry : StackSizeRelocMap) {
@@ -4857,12 +4854,7 @@ void DumpStyle<ELFT>::printRelocatableSt
     const SectionRef &RelocSec = StackSizeMapEntry.second;
 
     // Warn about stack size sections without a relocation section.
-    StringRef StackSizeSectionName;
-    if (Expected<StringRef> 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<ELFT>::printRelocatableSt
     // described in it.
     const Elf_Shdr *StackSizesELFSec =
         Obj->getSection(StackSizesSec.getRawDataRefImpl());
-    const SectionRef FunctionSec = toSectionRef<ELFT>(
-        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<ELFT>::printRelocatableSt
                   Contents.size()),
         Obj->isLittleEndian(), sizeof(Elf_Addr));
     for (const RelocationRef &Reloc : RelocSec.relocations()) {
-      if (!IsSupportedFn(Reloc.getType())) {
-        StringRef RelocSectionName;
-        Expected<StringRef> 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);
     }




More information about the llvm-commits mailing list