Index: test/tools/llvm-readobj/print-section.test =================================================================== --- /dev/null +++ test/tools/llvm-readobj/print-section.test @@ -0,0 +1,8 @@ +RUN: llvm-readobj -p .text %p/Inputs/elf-groups.x86_64 \ +RUN: | FileCheck %s + +CHECK: [ 0] UH..H....E. +CHECK: [ f] .E.x.E.. +CHECK: [ 1a] ..}.. +CHECK: [ 23] .}.. +CHECK: [ 2b] 1.H...]. Index: tools/llvm-readobj/ELFDumper.cpp =================================================================== --- tools/llvm-readobj/ELFDumper.cpp +++ tools/llvm-readobj/ELFDumper.cpp @@ -151,7 +151,6 @@ void printDynamicTable() override; void printNeededLibraries() override; void printProgramHeaders() override; - void printSectionAsString(StringRef StringName) override; void printSectionAsHex(StringRef StringName) override; void printHashTable() override; void printGnuHashTable() override; @@ -348,8 +347,6 @@ const Elf_Sym *FirstSym, StringRef StrTable, bool IsDynamic) = 0; virtual void printProgramHeaders(const ELFFile *Obj) = 0; - virtual void printSectionAsString(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; @@ -381,7 +378,6 @@ void printSymtabMessage(const ELFO *Obj, StringRef Name, size_t Offset) override; void printProgramHeaders(const ELFO *Obj) override; - void printSectionAsString(const ELFO *Obj, StringRef SectionName) override; void printHashHistogram(const ELFFile *Obj) override; void printCGProfile(const ELFFile *Obj) override; void printNotes(const ELFFile *Obj) override; @@ -445,7 +441,6 @@ void printDynamicSymbols(const ELFO *Obj) override; void printDynamicRelocations(const ELFO *Obj) override; void printProgramHeaders(const ELFO *Obj) override; - void printSectionAsString(const ELFO *Obj, StringRef SectionName) override; void printHashHistogram(const ELFFile *Obj) override; void printCGProfile(const ELFFile *Obj) override; void printNotes(const ELFFile *Obj) override; @@ -1585,11 +1580,6 @@ ELFDumperStyle->printProgramHeaders(Obj); } -template -void ELFDumper::printSectionAsString(StringRef SectionName) { - ELFDumperStyle->printSectionAsString(Obj, SectionName); -} - template void ELFDumper::printDynamicRelocations() { ELFDumperStyle->printDynamicRelocations(Obj); } @@ -3293,36 +3283,6 @@ } } -template -void GNUStyle::printSectionAsString(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 << "String dump of section '" << SecName << "':\n"; - const char *SecContent = - reinterpret_cast(Obj->base() + Sec->sh_offset); - const char *CurrentWord = SecContent; - const char *SecEnd = SecContent + Sec->sh_size; - while (CurrentWord <= SecEnd) { - size_t WordSize = strnlen(CurrentWord, SecEnd - CurrentWord); - if (!WordSize) { - CurrentWord++; - continue; - } - OS << format("[%6tx]", CurrentWord - SecContent); - OS << format(" %.*s\n", WordSize, CurrentWord); - CurrentWord += WordSize + 1; - } - OS.flush(); -} - template void GNUStyle::printDynamicRelocation(const ELFO *Obj, Elf_Rela R, bool IsRela) { @@ -4353,38 +4313,6 @@ } } -template -void LLVMStyle::printSectionAsString(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() << "String dump of section '" << SecName << "':\n"; - const char *SecContent = - reinterpret_cast(Obj->base() + Sec->sh_offset); - const char *CurrentWord = SecContent; - const char *SecEnd = SecContent + Sec->sh_size; - while (CurrentWord <= SecEnd) { - size_t WordSize = strnlen(CurrentWord, SecEnd - CurrentWord); - if (!WordSize) { - CurrentWord++; - continue; - } - W.startLine() << "[" - << to_string( - format_hex_no_prefix((CurrentWord - SecContent), 6)) - << "]"; - W.startLine() << format(" %.*s\n", WordSize, CurrentWord); - CurrentWord += WordSize + 1; - } -} - 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 @@ -14,6 +14,7 @@ #include #include "llvm/ADT/StringRef.h" +#include "llvm/Object/ObjectFile.h" namespace llvm { namespace object { @@ -43,7 +44,6 @@ virtual void printDynamicTable() { } virtual void printNeededLibraries() { } virtual void printProgramHeaders() { } - virtual void printSectionAsString(StringRef SectionName) {} virtual void printSectionAsHex(StringRef SectionName) {} virtual void printHashTable() { } virtual void printGnuHashTable() { } @@ -87,6 +87,8 @@ virtual void printStackMap() const = 0; + void printSectionAsString(const object::ObjectFile *Obj, StringRef SecName); + protected: ScopedPrinter &W; void SectionHexDump(StringRef SecName, const uint8_t *Section, size_t Size); Index: tools/llvm-readobj/ObjDumper.cpp =================================================================== --- tools/llvm-readobj/ObjDumper.cpp +++ tools/llvm-readobj/ObjDumper.cpp @@ -14,7 +14,9 @@ #include "ObjDumper.h" #include "Error.h" +#include "llvm-readobj.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Error.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" @@ -25,6 +27,69 @@ ObjDumper::~ObjDumper() { } +static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) { + for (size_t i = 0; i < Len; i++) + W << (isprint(Start[i]) ? static_cast(Start[i]) : '.'); +} + +static Expected +getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) { + char *StrPtr; + long SectionIndex = strtol(SecName.data(), &StrPtr, 10); + object::SectionRef Section; + unsigned SecIndex = 0; + for (object::SectionRef SecRef : Obj->sections()) { + if (*StrPtr) { + StringRef SectionName; + + if (std::error_code E = SecRef.getName(SectionName)) + return errorCodeToError(E); + + if (SectionName == SecName) + return SecRef; + } else if (SecIndex == SectionIndex) + return SecRef; + + SecIndex++; + } + return make_error("invalid section reference", + object::object_error::parse_failed); +} + +void ObjDumper::printSectionAsString(const object::ObjectFile *Obj, + StringRef SecName) { + Expected 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() << "String dump of section '" << SectionName << "':\n"; + + StringRef SectionContent; + Section.getContents(SectionContent); + + const uint8_t *SecContent = SectionContent.bytes_begin(); + const uint8_t *CurrentWord = SecContent; + const uint8_t *SecEnd = SectionContent.bytes_end(); + + while (CurrentWord <= SecEnd) { + size_t WordSize = strnlen(reinterpret_cast(CurrentWord), + SecEnd - CurrentWord); + if (!WordSize) { + CurrentWord++; + continue; + } + W.startLine() << format("[%6tx] ", CurrentWord - SecContent); + printAsPrintable(W.startLine(), CurrentWord, WordSize); + W.startLine() << '\n'; + CurrentWord += WordSize + 1; + } +} + void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section, size_t Size) { const uint8_t *SecContent = Section; Index: tools/llvm-readobj/llvm-readobj.cpp =================================================================== --- tools/llvm-readobj/llvm-readobj.cpp +++ tools/llvm-readobj/llvm-readobj.cpp @@ -434,8 +434,8 @@ if (opts::ProgramHeaders) Dumper->printProgramHeaders(); if (!opts::StringDump.empty()) - llvm::for_each(opts::StringDump, [&Dumper](StringRef SectionName) { - Dumper->printSectionAsString(SectionName); + llvm::for_each(opts::StringDump, [&Dumper, Obj](StringRef SectionName) { + Dumper->printSectionAsString(Obj, SectionName); }); if (!opts::HexDump.empty()) llvm::for_each(opts::HexDump, [&Dumper](StringRef SectionName) {