[PATCH] D87089: [llvm-readobj] - Remove code duplication when printing dynamic relocations. NFCI.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 3 06:39:11 PDT 2020


grimar created this revision.
grimar added reviewers: jhenderson, MaskRay.
Herald added a subscriber: rupprecht.
Herald added a project: LLVM.
grimar requested review of this revision.

LLVM style code can be simplified to avoid the duplication of logic
related to printing dynamic relocations.

Depends on D87087 <https://reviews.llvm.org/D87087>


https://reviews.llvm.org/D87089

Files:
  llvm/tools/llvm-readobj/ELFDumper.cpp


Index: llvm/tools/llvm-readobj/ELFDumper.cpp
===================================================================
--- llvm/tools/llvm-readobj/ELFDumper.cpp
+++ llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -945,6 +945,9 @@
   template <class RelTy>
   void printRelRelaReloc(const RelTy &R, unsigned RelIndex, const Elf_Shdr &Sec,
                          const Elf_Shdr *SymTab);
+  template <class RelTy>
+  void printRelRelaReloc(const RelTy &Rel, StringRef SymbolName);
+
   template <class RelTy> void printDynamicRelocation(const RelTy &Rel);
 
   void printSymbols();
@@ -6198,23 +6201,28 @@
     return;
   }
 
-  std::string TargetName = Target->second;
+  printRelRelaReloc(Rel, Target->second);
+}
+
+template <class ELFT>
+template <class RelTy>
+void LLVMStyle<ELFT>::printRelRelaReloc(const RelTy &Rel,
+                                        StringRef SymbolName) {
   SmallString<32> RelocName;
   this->Obj.getRelocationTypeName(Rel.getType(this->Obj.isMips64EL()),
                                   RelocName);
-
   uintX_t Addend = getAddend<ELFT>(Rel).getValueOr(0);
   if (opts::ExpandRelocs) {
     DictScope Group(W, "Relocation");
     W.printHex("Offset", Rel.r_offset);
     W.printNumber("Type", RelocName, (int)Rel.getType(this->Obj.isMips64EL()));
-    W.printNumber("Symbol", !TargetName.empty() ? TargetName : "-",
+    W.printNumber("Symbol", !SymbolName.empty() ? SymbolName : "-",
                   Rel.getSymbol(this->Obj.isMips64EL()));
     W.printHex("Addend", Addend);
   } else {
     raw_ostream &OS = W.startLine();
     OS << W.hex(Rel.r_offset) << " " << RelocName << " "
-       << (!TargetName.empty() ? TargetName : "-") << " " << W.hex(Addend)
+       << (!SymbolName.empty() ? SymbolName : "-") << " " << W.hex(Addend)
        << "\n";
   }
 }
@@ -6407,25 +6415,9 @@
 template <class ELFT>
 template <class RelTy>
 void LLVMStyle<ELFT>::printDynamicRelocation(const RelTy &Rel) {
-  SmallString<32> RelocName;
-  this->Obj.getRelocationTypeName(Rel.getType(this->Obj.isMips64EL()),
-                                  RelocName);
-  std::string SymbolName =
-      getSymbolForReloc(this->Obj, this->FileName, this->dumper(), Rel).Name;
-
-  uintX_t Addend = getAddend<ELFT>(Rel).getValueOr(0);
-  if (opts::ExpandRelocs) {
-    DictScope Group(W, "Relocation");
-    W.printHex("Offset", Rel.r_offset);
-    W.printNumber("Type", RelocName, (int)Rel.getType(this->Obj.isMips64EL()));
-    W.printString("Symbol", !SymbolName.empty() ? SymbolName : "-");
-    W.printHex("Addend", Addend);
-  } else {
-    raw_ostream &OS = W.startLine();
-    OS << W.hex(Rel.r_offset) << " " << RelocName << " "
-       << (!SymbolName.empty() ? SymbolName : "-") << " " << W.hex(Addend)
-       << "\n";
-  }
+  RelSymbol<ELFT> S =
+      getSymbolForReloc(this->Obj, this->FileName, this->dumper(), Rel);
+  printRelRelaReloc(Rel, S.Name);
 }
 
 template <class ELFT>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87089.289705.patch
Type: text/x-patch
Size: 2902 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200903/dca5a1c2/attachment.bin>


More information about the llvm-commits mailing list