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;
const Elf_Sym *FirstSym, StringRef StrTable,
bool IsDynamic) = 0;
virtual void printProgramHeaders(const ELFFile<ELFT> *Obj) = 0;
- virtual void printSectionAsString(const ELFFile<ELFT> *Obj,
- StringRef SectionName) = 0;
virtual void printHashHistogram(const ELFFile<ELFT> *Obj) = 0;
virtual void printCGProfile(const ELFFile<ELFT> *Obj) = 0;
virtual void printAddrsig(const ELFFile<ELFT> *Obj) = 0;
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<ELFT> *Obj) override;
void printCGProfile(const ELFFile<ELFT> *Obj) override;
void printAddrsig(const ELFFile<ELFT> *Obj) override;
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<ELFT> *Obj) override;
void printCGProfile(const ELFFile<ELFT> *Obj) override;
void printAddrsig(const ELFFile<ELFT> *Obj) override;
ELFDumperStyle->printProgramHeaders(Obj);
}
-template <class ELFT>
-void ELFDumper<ELFT>::printSectionAsString(StringRef SectionName) {
- ELFDumperStyle->printSectionAsString(Obj, SectionName);
-}
-
template <class ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
ELFDumperStyle->printDynamicRelocations(Obj);
}
}
template <class ELFT>
-void GNUStyle<ELFT>::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<const char *>(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 <class ELFT>
void GNUStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela R,
bool IsRela) {
SmallString<32> RelocName;
}
template <class ELFT>
-void LLVMStyle<ELFT>::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<const char *>(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 <class ELFT>
void LLVMStyle<ELFT>::printHashHistogram(const ELFFile<ELFT> *Obj) {
W.startLine() << "Hash Histogram not implemented!\n";
}
#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"
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<char>(Start[i]) : '.');
+}
+
+static Expected<object::SectionRef>
+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<StringError>("invalid section reference",
+ object::object_error::parse_failed);
+}
+
+void ObjDumper::printSectionAsString(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() << "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<const char *>(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;