llvm.org GIT mirror llvm / cac968e
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337896 91177308-0d34-0410-b5e6-96231b3b80d8 Paul Semel 2 years ago
7 changed file(s) with 33 addition(s) and 77 deletion(s). Raw diff Collapse all Expand all
1717
1818 MACHO: 0x00000000 50488d3d 00000000 e8000000 00e80000 PH.=............
1919 MACHO: 0x00000010 000031c0 5ac3 ..1.Z.
20
21 RUN: llvm-readobj -x 1 %p/Inputs/trivial.obj.wasm \
22 RUN: | FileCheck %s --check-prefix WASM
23
24 WASM: 0x00000000 03600001 7f60017f 017f6001 7f00 .`...`....`...
8282 void printSymbols() override;
8383 void printDynamicSymbols() override;
8484 void printUnwindInfo() override;
85 void printSectionAsHex(StringRef StringName) override;
8685
8786 void printNeededLibraries() override;
8887
654653 printDOSHeader(DH);
655654 }
656655
657 void COFFDumper::printSectionAsHex(StringRef SectionName) {
658 char *StrPtr;
659 long SectionIndex = strtol(SectionName.data(), &StrPtr, 10);
660 const coff_section *Sec;
661 if (*StrPtr)
662 error(Obj->getSection(SectionName, Sec));
663 else {
664 error(Obj->getSection((int)SectionIndex, Sec));
665 if (!Sec)
666 return error(object_error::parse_failed);
667 }
668
669 StringRef SecName;
670 error(Obj->getSectionName(Sec, SecName));
671
672 ArrayRef Content;
673 error(Obj->getSectionContents(Sec, Content));
674 const uint8_t *SecContent = Content.data();
675
676 SectionHexDump(SecName, SecContent, Content.size());
677 }
678
679656 void COFFDumper::printDOSHeader(const dos_header *DH) {
680657 DictScope D(W, "DOSHeader");
681658 W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic)));
151151 void printDynamicTable() override;
152152 void printNeededLibraries() override;
153153 void printProgramHeaders() override;
154 void printSectionAsHex(StringRef StringName) override;
155154 void printHashTable() override;
156155 void printGnuHashTable() override;
157156 void printLoadName() override;
283282 const Elf_Hash *getHashTable() const { return HashTable; }
284283 const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; }
285284 };
286
287 template
288 void ELFDumper::printSectionAsHex(StringRef SectionName) {
289 char *StrPtr;
290 long SectionIndex = strtol(SectionName.data(), &StrPtr, 10);
291 const Elf_Shdr *Sec;
292 if (*StrPtr)
293 Sec = unwrapOrError(Obj->getSection(SectionName));
294 else
295 Sec = unwrapOrError(Obj->getSection((unsigned int)SectionIndex));
296
297 StringRef SecName = unwrapOrError(Obj->getSectionName(Sec));
298 const uint8_t *SecContent =
299 reinterpret_cast(Obj->base() + Sec->sh_offset);
300
301 SectionHexDump(SecName, SecContent, Sec->sh_size);
302 }
303285
304286 template
305287 void ELFDumper::printSymbolsHelper(bool IsDynamic) const {
3737 void printDynamicSymbols() override;
3838 void printUnwindInfo() override;
3939 void printStackMap() const override;
40 void printSectionAsHex(StringRef SectionName) override;
4140
4241 void printNeededLibraries() override;
4342
676675 StackMapV2Parser(StackMapContentsArray));
677676 }
678677
679 void MachODumper::printSectionAsHex(StringRef SectionName) {
680 char *StrPtr;
681 long SectionIndex = strtol(SectionName.data(), &StrPtr, 10);
682 SectionRef SecTmp;
683 const SectionRef *Sec = &SecTmp;
684 if (*StrPtr)
685 SecTmp = unwrapOrError(Obj->getSection(SectionName));
686 else
687 SecTmp = unwrapOrError(Obj->getSection((unsigned int)SectionIndex));
688
689 StringRef SecName;
690 error(Sec->getName(SecName));
691
692 StringRef Data;
693 error(Sec->getContents(Data));
694 const uint8_t *SecContent = reinterpret_cast(Data.data());
695
696 SectionHexDump(SecName, SecContent, Data.size());
697 }
698
699678 void MachODumper::printNeededLibraries() {
700679 ListScope D(W, "NeededLibraries");
701680
3636 char *StrPtr;
3737 long SectionIndex = strtol(SecName.data(), &StrPtr, 10);
3838 object::SectionRef Section;
39 long SecIndex = 0;
39 long SecIndex;
40 if (Obj->isELF())
41 SecIndex = 0;
42 else
43 SecIndex = 1;
4044 for (object::SectionRef SecRef : Obj->sections()) {
4145 if (*StrPtr) {
4246 StringRef SectionName;
8993 }
9094 }
9195
92 void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section,
93 size_t Size) {
94 const uint8_t *SecContent = Section;
95 const uint8_t *SecEnd = Section + Size;
96 W.startLine() << "Hex dump of section '" << SecName << "':\n";
96 void ObjDumper::printSectionAsHex(const object::ObjectFile *Obj,
97 StringRef SecName) {
98 Expected SectionRefOrError =
99 getSecNameOrIndexAsSecRef(Obj, SecName);
100 if (!SectionRefOrError)
101 error(std::move(SectionRefOrError));
102 object::SectionRef Section = *SectionRefOrError;
103 StringRef SectionName;
104
105 if (std::error_code E = Section.getName(SectionName))
106 error(E);
107 W.startLine() << "Hex dump of section '" << SectionName << "':\n";
108
109 StringRef SectionContent;
110 Section.getContents(SectionContent);
111 const uint8_t *SecContent = SectionContent.bytes_begin();
112 const uint8_t *SecEnd = SecContent + SectionContent.size();
97113
98114 for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
99115 const uint8_t *TmpSecPtr = SecPtr;
120136 ' ');
121137
122138 TmpSecPtr = SecPtr;
123 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) {
124 if (isprint(TmpSecPtr[i]))
125 W.startLine() << TmpSecPtr[i];
126 else
127 W.startLine() << '.';
128 }
139 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
140 W.startLine() << (isprint(TmpSecPtr[i]) ? static_cast(TmpSecPtr[i])
141 : '.');
129142
130143 W.startLine() << '\n';
131144 }
8888 virtual void printStackMap() const = 0;
8989
9090 void printSectionAsString(const object::ObjectFile *Obj, StringRef SecName);
91 void printSectionAsHex(const object::ObjectFile *Obj, StringRef SecName);
9192
9293 protected:
9394 ScopedPrinter &W;
94 void SectionHexDump(StringRef SecName, const uint8_t *Section, size_t Size);
9595 };
9696
9797 std::error_code createCOFFDumper(const object::ObjectFile *Obj,
440440 Dumper->printSectionAsString(Obj, SectionName);
441441 });
442442 if (!opts::HexDump.empty())
443 llvm::for_each(opts::HexDump, [&Dumper](StringRef SectionName) {
444 Dumper->printSectionAsHex(SectionName);
443 llvm::for_each(opts::HexDump, [&Dumper, Obj](StringRef SectionName) {
444 Dumper->printSectionAsHex(Obj, SectionName);
445445 });
446446 if (opts::HashTable)
447447 Dumper->printHashTable();