From 1e8ba3fa21326de4ef5bf908f6bfbf2852e04794 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Mon, 17 Oct 2011 17:13:22 +0000 Subject: llvm-objdump: Add -s, which prints the contents of each section. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142199 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Object/objdump-section-content.test | 6 ++-- tools/llvm-objdump/llvm-objdump.cpp | 57 +++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/test/Object/objdump-section-content.test b/test/Object/objdump-section-content.test index 413f74be57..37f53304ef 100644 --- a/test/Object/objdump-section-content.test +++ b/test/Object/objdump-section-content.test @@ -3,9 +3,7 @@ RUN: | FileCheck %s -check-prefix COFF-i386 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: 0020 83c40cc3 .... 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$. diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 4ae13be3cd..34f27e8a63 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -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" @@ -60,6 +61,9 @@ Disassembled("d", cl::desc("Alias for --disassemble"), static cl::opt Relocations("r", cl::desc("Display the relocation entries in the file")); +static cl::opt +SectionContents("s", cl::desc("Display the content of each section")); + static cl::opt MachO("macho", cl::desc("Use MachO specific object file parser")); static cl::alias @@ -158,10 +162,6 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { 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 PrintSectionHeaders(const ObjectFile *o) { } } +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 @@ int main(int argc, char **argv) { if (InputFilenames.size() == 0) InputFilenames.push_back("a.out"); - if (!Disassemble && !Relocations && !SectionHeaders) { + if (!Disassemble && !Relocations && !SectionHeaders && !SectionContents) { cl::PrintHelpMessage(); return 2; } -- cgit v1.2.3