]> granicus.if.org Git - llvm/commitdiff
[llvm-readobj] Generic hex-dump option
authorPaul Semel <semelpaul@gmail.com>
Wed, 25 Jul 2018 10:04:37 +0000 (10:04 +0000)
committerPaul Semel <semelpaul@gmail.com>
Wed, 25 Jul 2018 10:04:37 +0000 (10:04 +0000)
Helpers are available to make this option file format independant. This
patch adds the feature for Wasm file format. It doesn't change the
behavior of the other file format handling.

Differential Revision: https://reviews.llvm.org/D49545

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

test/tools/llvm-readobj/print-hex.test
tools/llvm-readobj/COFFDumper.cpp
tools/llvm-readobj/ELFDumper.cpp
tools/llvm-readobj/MachODumper.cpp
tools/llvm-readobj/ObjDumper.cpp
tools/llvm-readobj/ObjDumper.h
tools/llvm-readobj/llvm-readobj.cpp

index 754c87b0789eb6ae56cffc653ceac28595873e78..c220eb3739d2abfc9b9a795825be89c47d196353 100644 (file)
@@ -18,3 +18,8 @@ RUN:     | FileCheck %s --check-prefix MACHO
 
 MACHO: 0x00000000 50488d3d 00000000 e8000000 00e80000 PH.=............
 MACHO: 0x00000010 000031c0 5ac3                       ..1.Z.
+
+RUN: llvm-readobj -x 1 %p/Inputs/trivial.obj.wasm \
+RUN:     | FileCheck %s --check-prefix WASM
+
+WASM: 0x00000000 03600001 7f60017f 017f6001 7f00 .`...`....`...
index c840d7efb840ec24fc36f89f97e76cce9f8aec81..0ed4ccd09f6f99889f3c674040ce0511181ae780 100644 (file)
@@ -83,7 +83,6 @@ public:
   void printSymbols() override;
   void printDynamicSymbols() override;
   void printUnwindInfo() override;
-  void printSectionAsHex(StringRef StringName) override;
 
   void printNeededLibraries() override;
 
@@ -655,28 +654,6 @@ void COFFDumper::printFileHeaders() {
     printDOSHeader(DH);
 }
 
-void COFFDumper::printSectionAsHex(StringRef SectionName) {
-  char *StrPtr;
-  long SectionIndex = strtol(SectionName.data(), &StrPtr, 10);
-  const coff_section *Sec;
-  if (*StrPtr)
-    error(Obj->getSection(SectionName, Sec));
-  else {
-    error(Obj->getSection((int)SectionIndex, Sec));
-    if (!Sec)
-      return error(object_error::parse_failed);
-  }
-
-  StringRef SecName;
-  error(Obj->getSectionName(Sec, SecName));
-
-  ArrayRef<uint8_t> Content;
-  error(Obj->getSectionContents(Sec, Content));
-  const uint8_t *SecContent = Content.data();
-
-  SectionHexDump(SecName, SecContent, Content.size());
-}
-
 void COFFDumper::printDOSHeader(const dos_header *DH) {
   DictScope D(W, "DOSHeader");
   W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic)));
index 5da66c4a6d86a33f4fdf16cc3d058b041392dad7..645ec2d7e04b14805b934c23aeaddb4088007651 100644 (file)
@@ -152,7 +152,6 @@ public:
   void printDynamicTable() override;
   void printNeededLibraries() override;
   void printProgramHeaders() override;
-  void printSectionAsHex(StringRef StringName) override;
   void printHashTable() override;
   void printGnuHashTable() override;
   void printLoadName() override;
@@ -285,23 +284,6 @@ public:
   const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; }
 };
 
-template <class ELFT>
-void ELFDumper<ELFT>::printSectionAsHex(StringRef SectionName) {
-  char *StrPtr;
-  long SectionIndex = strtol(SectionName.data(), &StrPtr, 10);
-  const Elf_Shdr *Sec;
-  if (*StrPtr)
-    Sec = unwrapOrError(Obj->getSection(SectionName));
-  else
-    Sec = unwrapOrError(Obj->getSection((unsigned int)SectionIndex));
-
-  StringRef SecName = unwrapOrError(Obj->getSectionName(Sec));
-  const uint8_t *SecContent =
-      reinterpret_cast<const uint8_t *>(Obj->base() + Sec->sh_offset);
-
-  SectionHexDump(SecName, SecContent, Sec->sh_size);
-}
-
 template <class ELFT>
 void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
   StringRef StrTable, SymtabName;
index c97f598bdd7e4ed1f1b88562ec418b64a061a74b..69ef1556f78d4d44bfb600518deb6d5a41319dee 100644 (file)
@@ -38,7 +38,6 @@ public:
   void printDynamicSymbols() override;
   void printUnwindInfo() override;
   void printStackMap() const override;
-  void printSectionAsHex(StringRef SectionName) override;
 
   void printNeededLibraries() override;
 
@@ -677,26 +676,6 @@ void MachODumper::printStackMap() const {
                         StackMapV2Parser<support::big>(StackMapContentsArray));
 }
 
-void MachODumper::printSectionAsHex(StringRef SectionName) {
-  char *StrPtr;
-  long SectionIndex = strtol(SectionName.data(), &StrPtr, 10);
-  SectionRef SecTmp;
-  const SectionRef *Sec = &SecTmp;
-  if (*StrPtr)
-    SecTmp = unwrapOrError(Obj->getSection(SectionName));
-  else
-    SecTmp = unwrapOrError(Obj->getSection((unsigned int)SectionIndex));
-
-  StringRef SecName;
-  error(Sec->getName(SecName));
-
-  StringRef Data;
-  error(Sec->getContents(Data));
-  const uint8_t *SecContent = reinterpret_cast<const uint8_t *>(Data.data());
-
-  SectionHexDump(SecName, SecContent, Data.size());
-}
-
 void MachODumper::printNeededLibraries() {
   ListScope D(W, "NeededLibraries");
 
index 4677a378382257b617ae206df1dbb58aa6b926aa..17675fd8f279dbd60011c9ae9a447dfb9e748dac 100644 (file)
@@ -37,7 +37,11 @@ getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) {
   char *StrPtr;
   long SectionIndex = strtol(SecName.data(), &StrPtr, 10);
   object::SectionRef Section;
-  long SecIndex = 0;
+  long SecIndex;
+  if (Obj->isELF())
+    SecIndex = 0;
+  else
+    SecIndex = 1;
   for (object::SectionRef SecRef : Obj->sections()) {
     if (*StrPtr) {
       StringRef SectionName;
@@ -90,11 +94,23 @@ void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
   }
 }
 
-void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section,
-                               size_t Size) {
-  const uint8_t *SecContent = Section;
-  const uint8_t *SecEnd = Section + Size;
-  W.startLine() << "Hex dump of section '" << SecName << "':\n";
+void ObjDumper::printSectionAsHex(const object::ObjectFile *Obj,
+                                  StringRef SecName) {
+  Expected<object::SectionRef> SectionRefOrError =
+      getSecNameOrIndexAsSecRef(Obj, SecName);
+  if (!SectionRefOrError)
+    error(std::move(SectionRefOrError));
+  object::SectionRef Section = *SectionRefOrError;
+  StringRef SectionName;
+
+  if (std::error_code E = Section.getName(SectionName))
+    error(E);
+  W.startLine() << "Hex dump of section '" << SectionName << "':\n";
+
+  StringRef SectionContent;
+  Section.getContents(SectionContent);
+  const uint8_t *SecContent = SectionContent.bytes_begin();
+  const uint8_t *SecEnd = SecContent + SectionContent.size();
 
   for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
     const uint8_t *TmpSecPtr = SecPtr;
@@ -121,12 +137,9 @@ void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section,
                               ' ');
 
     TmpSecPtr = SecPtr;
-    for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) {
-      if (isprint(TmpSecPtr[i]))
-        W.startLine() << TmpSecPtr[i];
-      else
-        W.startLine() << '.';
-    }
+    for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
+      W.startLine() << (isprint(TmpSecPtr[i]) ? static_cast<char>(TmpSecPtr[i])
+                                              : '.');
 
     W.startLine() << '\n';
   }
index 786e1d47d71a10a2266961bd259a4985cfc78a62..8c3a7bec73be4c210c984c7c490c0dfcea6c4703 100644 (file)
@@ -89,10 +89,10 @@ public:
   virtual void printStackMap() const = 0;
 
   void printSectionAsString(const object::ObjectFile *Obj, StringRef SecName);
+  void printSectionAsHex(const object::ObjectFile *Obj, StringRef SecName);
 
 protected:
   ScopedPrinter &W;
-  void SectionHexDump(StringRef SecName, const uint8_t *Section, size_t Size);
 };
 
 std::error_code createCOFFDumper(const object::ObjectFile *Obj,
index 389053d5499d837ab5c38e26f3bfb9185d703278..a7236c02b8ae692e292573dca3e109800f385e16 100644 (file)
@@ -441,8 +441,8 @@ static void dumpObject(const ObjectFile *Obj, ScopedPrinter &Writer) {
       Dumper->printSectionAsString(Obj, SectionName);
     });
   if (!opts::HexDump.empty())
-    llvm::for_each(opts::HexDump, [&Dumper](StringRef SectionName) {
-      Dumper->printSectionAsHex(SectionName);
+    llvm::for_each(opts::HexDump, [&Dumper, Obj](StringRef SectionName) {
+      Dumper->printSectionAsHex(Obj, SectionName);
     });
   if (opts::HashTable)
     Dumper->printHashTable();