Add -expand-relocs to llvm-readobj
authorNico Rieck <nico.rieck@gmail.com>
Fri, 12 Apr 2013 04:01:52 +0000 (04:01 +0000)
committerNico Rieck <nico.rieck@gmail.com>
Fri, 12 Apr 2013 04:01:52 +0000 (04:01 +0000)
This option expands shown relocations from single line to a dictionary
format:

  Relocation {
    Offset: 0x4
    Type: R_386_32 (1)
    Symbol: sym
    Info: 0x0
  }

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179359 91177308-0d34-0410-b5e6-96231b3b80d8

docs/CommandGuide/llvm-readobj.rst
tools/llvm-readobj/COFFDumper.cpp
tools/llvm-readobj/ELFDumper.cpp
tools/llvm-readobj/MachODumper.cpp
tools/llvm-readobj/llvm-readobj.cpp
tools/llvm-readobj/llvm-readobj.h

index 3ede21d5461c39df2aac123d319c75afc9182671..1ca0b19a42546c99d3ff40f17db20e3a9dc350ac 100644 (file)
@@ -63,6 +63,11 @@ input. Otherwise, it will read from the specified ``filenames``.
 
  Display unwind information.
 
+.. option:: -expand-relocs
+
+ When used with ``-relocations``, display each relocation in an expanded
+ multi-line format.
+
 .. option:: -dynamic-table
 
  Display the ELF .dynamic section table (only for ELF object files).
index be4e76cc634f0d6c2c8d7c30376e8dd5e2579d48..d600bc3fd2f6c855a800828acc7823d09798fc94 100644 (file)
@@ -680,11 +680,18 @@ void COFFDumper::printRelocation(section_iterator SecI,
   if (error(Symbol.getName(SymbolName))) return;
   if (error(SecI->getContents(Contents))) return;
 
-  raw_ostream& OS = W.startLine();
-  OS << W.hex(Offset)
-     << " " << RelocName
-     << " " << (SymbolName.size() > 0 ? SymbolName : "-")
-     << "\n";
+  if (opts::ExpandRelocs) {
+    DictScope Group(W, "Relocation");
+    W.printHex("Offset", Offset);
+    W.printNumber("Type", RelocName, RelocType);
+    W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
+  } else {
+    raw_ostream& OS = W.startLine();
+    OS << W.hex(Offset)
+       << " " << RelocName
+       << " " << (SymbolName.size() > 0 ? SymbolName : "-")
+       << "\n";
+  }
 }
 
 void COFFDumper::printSymbols() {
index 9e111dd905ca625d620cb756f3f4dfeb80acf80b..b0e2734ac937344dd5309c32b4801fcb65afa1da 100644 (file)
@@ -549,22 +549,32 @@ template<class ELFT>
 void ELFDumper<ELFT>::printRelocation(section_iterator Sec,
                                       relocation_iterator RelI) {
   uint64_t Offset;
+  uint64_t RelocType;
   SmallString<32> RelocName;
   int64_t Info;
   StringRef SymbolName;
   SymbolRef Symbol;
   if (error(RelI->getOffset(Offset))) return;
+  if (error(RelI->getType(RelocType))) return;
   if (error(RelI->getTypeName(RelocName))) return;
   if (error(RelI->getAdditionalInfo(Info))) return;
   if (error(RelI->getSymbol(Symbol))) return;
   if (error(Symbol.getName(SymbolName))) return;
 
-  raw_ostream& OS = W.startLine();
-  OS << W.hex(Offset)
-     << " " << RelocName
-     << " " << (SymbolName.size() > 0 ? SymbolName : "-")
-     << " " << W.hex(Info)
-     << "\n";
+  if (opts::ExpandRelocs) {
+    DictScope Group(W, "Relocation");
+    W.printHex("Offset", Offset);
+    W.printNumber("Type", RelocName, RelocType);
+    W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
+    W.printHex("Info", Info);
+  } else {
+    raw_ostream& OS = W.startLine();
+    OS << W.hex(Offset)
+       << " " << RelocName
+       << " " << (SymbolName.size() > 0 ? SymbolName : "-")
+       << " " << W.hex(Info)
+       << "\n";
+  }
 }
 
 template<class ELFT>
index a13593b44836803db45adeb33e92fbfd4af57ccc..69f1d63b12f1de9ec9b06a5d704d4f24ef728a33 100644 (file)
@@ -341,18 +341,32 @@ void MachODumper::printRelocation(section_iterator SecI,
   const MachOObjectFileBase::RelocationEntry *RE = Obj->getRelocation(DR);
   bool IsScattered = Obj->isScattered(RE);
 
-  raw_ostream& OS = W.startLine();
-  OS << W.hex(Offset)
-     << " " << Obj->isPCRel(RE)
-     << " " << Obj->getLength(RE);
-  if (IsScattered)
-    OS << " n/a";
-  else
-    OS << " " << RE->getExternal();
-  OS << " " << RelocName
-     << " " << IsScattered
-     << " " << (SymbolName.size() > 0 ? SymbolName : "-")
-     << "\n";
+  if (opts::ExpandRelocs) {
+    DictScope Group(W, "Relocation");
+    W.printHex("Offset", Offset);
+    W.printNumber("PCRel", Obj->isPCRel(RE));
+    W.printNumber("Length", Obj->getLength(RE));
+    if (IsScattered)
+      W.printString("Extern", StringRef("N/A"));
+    else
+      W.printNumber("Extern", RE->getExternal());
+    W.printNumber("Type", RelocName, RE->getType());
+    W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
+    W.printNumber("Scattered", IsScattered);
+  } else {
+    raw_ostream& OS = W.startLine();
+    OS << W.hex(Offset)
+       << " " << Obj->isPCRel(RE)
+       << " " << Obj->getLength(RE);
+    if (IsScattered)
+      OS << " n/a";
+    else
+      OS << " " << RE->getExternal();
+    OS << " " << RelocName
+       << " " << IsScattered
+       << " " << (SymbolName.size() > 0 ? SymbolName : "-")
+       << "\n";
+  }
 }
 
 void MachODumper::printSymbols() {
index 67c9a98f40f3ae5c1ecb14047c8d8fce1dd80562..9d5cfcbd6a233ca6d2d2d6c98a8095b96c4ba561 100644 (file)
@@ -120,6 +120,10 @@ namespace opts {
   // -needed-libs
   cl::opt<bool> NeededLibraries("needed-libs",
     cl::desc("Display the needed libraries"));
+
+  // -expand-relocs
+  cl::opt<bool> ExpandRelocs("expand-relocs",
+    cl::desc("Expand each shown relocation to multiple lines"));
 } // namespace opts
 
 namespace llvm {
index be18268a7f64b887210f844d7b24bcaec41eea9a..3f756106c978f97fcb26a65ad6c62efacc98aefd 100644 (file)
@@ -37,6 +37,7 @@ namespace opts {
   extern llvm::cl::opt<bool> Symbols;
   extern llvm::cl::opt<bool> DynamicSymbols;
   extern llvm::cl::opt<bool> UnwindInfo;
+  extern llvm::cl::opt<bool> ExpandRelocs;
 } // namespace opts
 
 #define LLVM_READOBJ_ENUM_ENT(ns, enum) \