[llvm-commits] [llvm] r142199 - in /llvm/trunk: test/Object/objdump-section-content.test tools/llvm-objdump/llvm-objdump.cpp

Michael J. Spencer bigcheesegs at gmail.com
Mon Oct 17 10:13:22 PDT 2011


Author: mspencer
Date: Mon Oct 17 12:13:22 2011
New Revision: 142199

URL: http://llvm.org/viewvc/llvm-project?rev=142199&view=rev
Log:
llvm-objdump: Add -s, which prints the contents of each section.

Modified:
    llvm/trunk/test/Object/objdump-section-content.test
    llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp

Modified: llvm/trunk/test/Object/objdump-section-content.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/objdump-section-content.test?rev=142199&r1=142198&r2=142199&view=diff
==============================================================================
--- llvm/trunk/test/Object/objdump-section-content.test (original)
+++ llvm/trunk/test/Object/objdump-section-content.test Mon Oct 17 12:13:22 2011
@@ -3,9 +3,7 @@
 RUN: llvm-objdump -s %p/TestObjectFiles/trivial-object-test.elf-i386 \
 RUN:              | FileCheck %s -check-prefix ELF-i386
 
-XFAIL: *
-
-COFF-i386: trivial-object-test.coff-i386:     file format pe-i386
+COFF-i386: trivial-object-test.coff-i386:     file format
 COFF-i386: Contents of section .text:
 COFF-i386:  0000 83ec0cc7 44240800 000000c7 04240000  ....D$.......$..
 COFF-i386:  0010 0000e800 000000e8 00000000 8b442408  .............D$.
@@ -13,7 +11,7 @@
 COFF-i386: Contents of section .data:
 COFF-i386:  0000 48656c6c 6f20576f 726c6421 00        Hello World!.
 
-ELF-i386: trivial-object-test.elf-i386:     file format elf32-i386
+ELF-i386: trivial-object-test.elf-i386:     file format
 ELF-i386: Contents of section .text:
 ELF-i386:  0000 83ec0cc7 44240800 000000c7 04240000  ....D$.......$..
 ELF-i386:  0010 0000e8fc ffffffe8 fcffffff 8b442408  .............D$.

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=142199&r1=142198&r2=142199&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Mon Oct 17 12:13:22 2011
@@ -18,6 +18,7 @@
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/ADT/OwningPtr.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/MC/MCAsmInfo.h"
@@ -61,6 +62,9 @@
 Relocations("r", cl::desc("Display the relocation entries in the file"));
 
 static cl::opt<bool>
+SectionContents("s", cl::desc("Display the content of each section"));
+
+static cl::opt<bool>
 MachO("macho", cl::desc("Use MachO specific object file parser"));
 static cl::alias
 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
@@ -158,10 +162,6 @@
     return;
   }
 
-  outs() << '\n';
-  outs() << Obj->getFileName()
-         << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
-
   error_code ec;
   for (section_iterator i = Obj->begin_sections(),
                         e = Obj->end_sections();
@@ -370,13 +370,60 @@
   }
 }
 
+static void PrintSectionContents(const ObjectFile *o) {
+  error_code ec;
+  for (section_iterator si = o->begin_sections(),
+                        se = o->end_sections();
+                        si != se; si.increment(ec)) {
+    if (error(ec)) return;
+    StringRef Name;
+    StringRef Contents;
+    uint64_t BaseAddr;
+    if (error(si->getName(Name))) continue;
+    if (error(si->getContents(Contents))) continue;
+    if (error(si->getAddress(BaseAddr))) continue;
+
+    outs() << "Contents of section " << Name << ":\n";
+
+    // Dump out the content as hex and printable ascii characters.
+    for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
+      outs() << format(" %04x ", BaseAddr + addr);
+      // Dump line of hex.
+      for (std::size_t i = 0; i < 16; ++i) {
+        if (i != 0 && i % 4 == 0)
+          outs() << ' ';
+        if (addr + i < end)
+          outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
+                 << hexdigit(Contents[addr + i] & 0xF, true);
+        else
+          outs() << "  ";
+      }
+      // Print ascii.
+      outs() << "  ";
+      for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
+        if (std::isprint(Contents[addr + i] & 0xFF))
+          outs() << Contents[addr + i];
+        else
+          outs() << ".";
+      }
+      outs() << "\n";
+    }
+  }
+}
+
 static void DumpObject(const ObjectFile *o) {
+  outs() << '\n';
+  outs() << o->getFileName()
+         << ":\tfile format " << o->getFileFormatName() << "\n\n";
+
   if (Disassemble)
     DisassembleObject(o, Relocations);
   if (Relocations && !Disassemble)
     PrintRelocations(o);
   if (SectionHeaders)
     PrintSectionHeaders(o);
+  if (SectionContents)
+    PrintSectionContents(o);
 }
 
 /// @brief Dump each object file in \a a;
@@ -447,7 +494,7 @@
   if (InputFilenames.size() == 0)
     InputFilenames.push_back("a.out");
 
-  if (!Disassemble && !Relocations && !SectionHeaders) {
+  if (!Disassemble && !Relocations && !SectionHeaders && !SectionContents) {
     cl::PrintHelpMessage();
     return 2;
   }





More information about the llvm-commits mailing list