diff options
author | Paul Semel <semelpaul@gmail.com> | 2018-07-25 10:04:37 +0000 |
---|---|---|
committer | Paul Semel <semelpaul@gmail.com> | 2018-07-25 10:04:37 +0000 |
commit | 5ce8f1598c450bcdadcca34000428300c903a544 (patch) | |
tree | d84158cfaac54400acd1a274e0f978857593391f /llvm/tools/llvm-readobj/ObjDumper.cpp | |
parent | 4f6481dc811250c55cda9f647b1e00331eec71cc (diff) | |
download | bcm5719-llvm-5ce8f1598c450bcdadcca34000428300c903a544.tar.gz bcm5719-llvm-5ce8f1598c450bcdadcca34000428300c903a544.zip |
[llvm-readobj] Generic hex-dump option
Helpers are available to make this option file format independant. This
patch adds the feature for Wasm file format. It doesn't change the
behavior of the other file format handling.
Differential Revision: https://reviews.llvm.org/D49545
llvm-svn: 337896
Diffstat (limited to 'llvm/tools/llvm-readobj/ObjDumper.cpp')
-rw-r--r-- | llvm/tools/llvm-readobj/ObjDumper.cpp | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/llvm/tools/llvm-readobj/ObjDumper.cpp b/llvm/tools/llvm-readobj/ObjDumper.cpp index 4677a378382..17675fd8f27 100644 --- a/llvm/tools/llvm-readobj/ObjDumper.cpp +++ b/llvm/tools/llvm-readobj/ObjDumper.cpp @@ -37,7 +37,11 @@ getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) { char *StrPtr; long SectionIndex = strtol(SecName.data(), &StrPtr, 10); object::SectionRef Section; - long SecIndex = 0; + long SecIndex; + if (Obj->isELF()) + SecIndex = 0; + else + SecIndex = 1; for (object::SectionRef SecRef : Obj->sections()) { if (*StrPtr) { StringRef SectionName; @@ -90,11 +94,23 @@ void ObjDumper::printSectionAsString(const object::ObjectFile *Obj, } } -void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section, - size_t Size) { - const uint8_t *SecContent = Section; - const uint8_t *SecEnd = Section + Size; - W.startLine() << "Hex dump of section '" << SecName << "':\n"; +void ObjDumper::printSectionAsHex(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() << "Hex dump of section '" << SectionName << "':\n"; + + StringRef SectionContent; + Section.getContents(SectionContent); + const uint8_t *SecContent = SectionContent.bytes_begin(); + const uint8_t *SecEnd = SecContent + SectionContent.size(); for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) { const uint8_t *TmpSecPtr = SecPtr; @@ -121,12 +137,9 @@ void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section, ' '); TmpSecPtr = SecPtr; - for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) { - if (isprint(TmpSecPtr[i])) - W.startLine() << TmpSecPtr[i]; - else - W.startLine() << '.'; - } + for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) + W.startLine() << (isprint(TmpSecPtr[i]) ? static_cast<char>(TmpSecPtr[i]) + : '.'); W.startLine() << '\n'; } |