Index: tools/llvm-readobj/ELFDumper.cpp =================================================================== --- tools/llvm-readobj/ELFDumper.cpp +++ tools/llvm-readobj/ELFDumper.cpp @@ -150,6 +150,7 @@ void printNeededLibraries() override; void printProgramHeaders() override; void printSectionAsString(StringRef StringName) override; + void printSectionAsHex(StringRef StringName) override; void printHashTable() override; void printGnuHashTable() override; void printLoadName() override; @@ -327,6 +328,8 @@ virtual void printProgramHeaders(const ELFFile *Obj) = 0; virtual void printSectionAsString(const ELFFile *Obj, StringRef SectionName) = 0; + virtual void printSectionAsHex(const ELFFile *Obj, + StringRef SectionName) = 0; virtual void printHashHistogram(const ELFFile *Obj) = 0; virtual void printCGProfile(const ELFFile *Obj) = 0; virtual void printNotes(const ELFFile *Obj) = 0; @@ -359,6 +362,7 @@ size_t Offset) override; void printProgramHeaders(const ELFO *Obj) override; void printSectionAsString(const ELFO *Obj, StringRef SectionName) override; + void printSectionAsHex(const ELFO *Obj, StringRef SectionName) override; void printHashHistogram(const ELFFile *Obj) override; void printCGProfile(const ELFFile *Obj) override; void printNotes(const ELFFile *Obj) override; @@ -422,6 +426,7 @@ void printDynamicRelocations(const ELFO *Obj) override; void printProgramHeaders(const ELFO *Obj) override; void printSectionAsString(const ELFO *Obj, StringRef SectionName) override; + void printSectionAsHex(const ELFO *Obj, StringRef SectionName) override; void printHashHistogram(const ELFFile *Obj) override; void printCGProfile(const ELFFile *Obj) override; void printNotes(const ELFFile *Obj) override; @@ -1549,6 +1554,11 @@ ELFDumperStyle->printSectionAsString(Obj, SectionName); } +template +void ELFDumper::printSectionAsHex(StringRef SectionName) { + ELFDumperStyle->printSectionAsHex(Obj, SectionName); +} + template void ELFDumper::printDynamicRelocations() { ELFDumperStyle->printDynamicRelocations(Obj); } @@ -3250,6 +3260,51 @@ OS.flush(); } +template +void GNUStyle::printSectionAsHex(const ELFO *Obj, 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)); + OS << "Hex dump of section '" << SecName << "':\n"; + const uint8_t *SecContent = + reinterpret_cast(Obj->base() + Sec->sh_offset); + const uint8_t *SecEnd = SecContent + Sec->sh_size; + + for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) { + const uint8_t *TmpSecPtr = SecPtr; + uint8_t i; + + OS << format_hex(SecPtr - SecContent, 10); + OS << ' '; + for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i, TmpSecPtr += 4) { + uint32_t Val = + __bswap_32(*(reinterpret_cast(TmpSecPtr))); + OS << format_hex_no_prefix(Val, 8); + OS << ' '; + } + + if (i < 4) + OS << format("%*c", (4 - i) * 8 + (4 - i), ' '); + + TmpSecPtr = SecPtr; + for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) { + if (isprint(TmpSecPtr[i])) + OS << TmpSecPtr[i]; + else + OS << '.'; + } + + OS << '\n'; + } + OS.flush(); +} + template void GNUStyle::printDynamicRelocation(const ELFO *Obj, Elf_Rela R, bool IsRela) { @@ -4276,6 +4331,51 @@ } } +template +void LLVMStyle::printSectionAsHex(const ELFO *Obj, + 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)); + W.startLine() << "Hex dump of section '" << SecName << "':\n"; + const uint8_t *SecContent = + reinterpret_cast(Obj->base() + Sec->sh_offset); + const uint8_t *SecEnd = SecContent + Sec->sh_size; + + for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) { + const uint8_t *TmpSecPtr = SecPtr; + uint8_t i; + + W.startLine() << format_hex(SecPtr - SecContent, 10); + W.startLine() << ' '; + for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i, TmpSecPtr += 4) { + uint32_t Val = + __bswap_32(*(reinterpret_cast(TmpSecPtr))); + W.startLine() << format_hex_no_prefix(Val, 8); + W.startLine() << ' '; + } + + if (i < 4) + W.startLine() << format("%*c", (4 - i) * 8 + (4 - i), ' '); + + TmpSecPtr = SecPtr; + for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) { + if (isprint(TmpSecPtr[i])) + W.startLine() << TmpSecPtr[i]; + else + W.startLine() << '.'; + } + + W.startLine() << '\n'; + } +} + template void LLVMStyle::printHashHistogram(const ELFFile *Obj) { W.startLine() << "Hash Histogram not implemented!\n"; Index: tools/llvm-readobj/ObjDumper.h =================================================================== --- tools/llvm-readobj/ObjDumper.h +++ tools/llvm-readobj/ObjDumper.h @@ -44,6 +44,7 @@ virtual void printNeededLibraries() { } virtual void printProgramHeaders() { } virtual void printSectionAsString(StringRef SectionName) {} + virtual void printSectionAsHex(StringRef SectionName) {} virtual void printHashTable() { } virtual void printGnuHashTable() { } virtual void printLoadName() {} Index: tools/llvm-readobj/llvm-readobj.cpp =================================================================== --- tools/llvm-readobj/llvm-readobj.cpp +++ tools/llvm-readobj/llvm-readobj.cpp @@ -152,6 +152,12 @@ cl::alias StringDumpShort("p", cl::desc("Alias for --string-dump"), cl::aliasopt(StringDump)); + // -hex-dump + cl::list HexDump("hex-dump", cl::desc(""), + cl::ZeroOrMore); + cl::alias HexDumpShort("x", cl::desc("Alias for --hex-dump"), + cl::aliasopt(HexDump)); + // -hash-table cl::opt HashTable("hash-table", cl::desc("Display ELF hash table")); @@ -427,6 +433,10 @@ llvm::for_each(opts::StringDump, [&Dumper](StringRef SectionName) { Dumper->printSectionAsString(SectionName); }); + if (!opts::HexDump.empty()) + llvm::for_each(opts::HexDump, [&Dumper](StringRef SectionName) { + Dumper->printSectionAsHex(SectionName); + }); if (opts::HashTable) Dumper->printHashTable(); if (opts::GnuHashTable)