Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1 | //===-- COFFDumper.cpp - COFF-specific dumper -------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements the COFF-specific dumper for llvm-readobj. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 15 | #include "ARMWinEHPrinter.h" |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 16 | #include "CodeView.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 17 | #include "Error.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 18 | #include "ObjDumper.h" |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 19 | #include "StackMapPrinter.h" |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 20 | #include "Win64EHDumper.h" |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 21 | #include "llvm-readobj.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseMap.h" |
| 23 | #include "llvm/ADT/SmallString.h" |
Colin LeMahieu | 9fbffee | 2014-11-19 17:10:39 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/CodeView/ByteStream.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/CodeView/Line.h" |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/CodeView/MemoryTypeTableBuilder.h" |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/CodeView/RecordSerialization.h" |
| 30 | #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h" |
| 31 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Reid Kleckner | 4a14bca | 2016-05-05 00:34:33 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/CodeView/TypeDumper.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 34 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 35 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 37 | #include "llvm/Object/COFF.h" |
| 38 | #include "llvm/Object/ObjectFile.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 39 | #include "llvm/Support/COFF.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Casting.h" |
| 41 | #include "llvm/Support/Compiler.h" |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 42 | #include "llvm/Support/DataExtractor.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Format.h" |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 44 | #include "llvm/Support/ScopedPrinter.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 45 | #include "llvm/Support/SourceMgr.h" |
| 46 | #include "llvm/Support/Win64EH.h" |
| 47 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 48 | #include <algorithm> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 49 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 50 | #include <system_error> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 51 | #include <time.h> |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 52 | |
| 53 | using namespace llvm; |
| 54 | using namespace llvm::object; |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 55 | using namespace llvm::codeview; |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 56 | using namespace llvm::support; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 57 | using namespace llvm::Win64EH; |
| 58 | |
| 59 | namespace { |
| 60 | |
| 61 | class COFFDumper : public ObjDumper { |
| 62 | public: |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 63 | friend class COFFObjectDumpDelegate; |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 64 | COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer) |
Reid Kleckner | 4a14bca | 2016-05-05 00:34:33 +0000 | [diff] [blame] | 65 | : ObjDumper(Writer), Obj(Obj), |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 66 | CVTD(&Writer, opts::CodeViewSubsectionBytes) {} |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 67 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 68 | void printFileHeaders() override; |
| 69 | void printSections() override; |
| 70 | void printRelocations() override; |
| 71 | void printSymbols() override; |
| 72 | void printDynamicSymbols() override; |
| 73 | void printUnwindInfo() override; |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 74 | void printCOFFImports() override; |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 75 | void printCOFFExports() override; |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 76 | void printCOFFDirectives() override; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 77 | void printCOFFBaseReloc() override; |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 78 | void printCodeViewDebugInfo() override; |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 79 | void |
| 80 | mergeCodeViewTypes(llvm::codeview::MemoryTypeTableBuilder &CVTypes) override; |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 81 | void printStackMap() const override; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 82 | private: |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 83 | void printSymbol(const SymbolRef &Sym); |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 84 | void printRelocation(const SectionRef &Section, const RelocationRef &Reloc, |
| 85 | uint64_t Bias = 0); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 86 | void printDataDirectory(uint32_t Index, const std::string &FieldName); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 87 | |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 88 | void printDOSHeader(const dos_header *DH); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 89 | template <class PEHeader> void printPEHeader(const PEHeader *Hdr); |
| 90 | void printBaseOfDataField(const pe32_header *Hdr); |
| 91 | void printBaseOfDataField(const pe32plus_header *Hdr); |
| 92 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 93 | void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section); |
| 94 | void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 95 | StringRef getTypeName(TypeIndex Ty); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 96 | StringRef getFileNameForFileOffset(uint32_t FileOffset); |
| 97 | void printFileNameForOffset(StringRef Label, uint32_t FileOffset); |
Reid Kleckner | 5cb2b6b | 2016-05-02 20:30:47 +0000 | [diff] [blame] | 98 | void printTypeIndex(StringRef FieldName, TypeIndex TI) { |
| 99 | // Forward to CVTypeDumper for simplicity. |
| 100 | CVTD.printTypeIndex(FieldName, TI); |
| 101 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 102 | |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 103 | void printCodeViewSymbolsSubsection(StringRef Subsection, |
| 104 | const SectionRef &Section, |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 105 | StringRef SectionContents); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 106 | |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 107 | void printCodeViewFileChecksums(StringRef Subsection); |
| 108 | |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 109 | void printCodeViewInlineeLines(StringRef Subsection); |
| 110 | |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 111 | void printRelocatedField(StringRef Label, const coff_section *Sec, |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 112 | uint32_t RelocOffset, uint32_t Offset, |
| 113 | StringRef *RelocSym = nullptr); |
| 114 | |
| 115 | void printRelocatedField(StringRef Label, const coff_section *Sec, |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 116 | StringRef SectionContents, const ulittle32_t *Field, |
| 117 | StringRef *RelocSym = nullptr); |
| 118 | |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 119 | void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec, |
| 120 | StringRef SectionContents, StringRef Block); |
| 121 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 122 | /// Given a .debug$S section, find the string table and file checksum table. |
| 123 | void initializeFileAndStringTables(StringRef Data); |
| 124 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 125 | void cacheRelocations(); |
| 126 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 127 | std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset, |
| 128 | SymbolRef &Sym); |
| 129 | std::error_code resolveSymbolName(const coff_section *Section, |
| 130 | uint64_t Offset, StringRef &Name); |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 131 | std::error_code resolveSymbolName(const coff_section *Section, |
| 132 | StringRef SectionContents, |
| 133 | const void *RelocPtr, StringRef &Name); |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 134 | void printImportedSymbols(iterator_range<imported_symbol_iterator> Range); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 135 | void printDelayImportedSymbols( |
| 136 | const DelayImportDirectoryEntryRef &I, |
| 137 | iterator_range<imported_symbol_iterator> Range); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 138 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 139 | typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy; |
| 140 | |
| 141 | const llvm::object::COFFObjectFile *Obj; |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 142 | bool RelocCached = false; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 143 | RelocMapTy RelocMap; |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 144 | StringRef CVFileChecksumTable; |
Timur Iskhodzhanov | 1160333 | 2014-10-06 16:59:44 +0000 | [diff] [blame] | 145 | StringRef CVStringTable; |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 146 | |
Reid Kleckner | 5cb2b6b | 2016-05-02 20:30:47 +0000 | [diff] [blame] | 147 | CVTypeDumper CVTD; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 150 | class COFFObjectDumpDelegate : public SymbolDumpDelegate { |
| 151 | public: |
| 152 | COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR, |
| 153 | const COFFObjectFile *Obj, StringRef SectionContents) |
Zachary Turner | 537014c | 2016-05-24 03:32:34 +0000 | [diff] [blame] | 154 | : CD(CD), SR(SR), SectionContents(SectionContents) { |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 155 | Sec = Obj->getCOFFSection(SR); |
| 156 | } |
| 157 | |
| 158 | uint32_t getRecordOffset(ArrayRef<uint8_t> Record) override { |
| 159 | return Record.data() - SectionContents.bytes_begin(); |
| 160 | } |
| 161 | |
| 162 | void printRelocatedField(StringRef Label, uint32_t RelocOffset, |
| 163 | uint32_t Offset, StringRef *RelocSym) override { |
| 164 | CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym); |
| 165 | } |
| 166 | |
| 167 | void printBinaryBlockWithRelocs(StringRef Label, |
| 168 | ArrayRef<uint8_t> Block) override { |
| 169 | StringRef SBlock(reinterpret_cast<const char *>(Block.data()), |
| 170 | Block.size()); |
| 171 | if (opts::CodeViewSubsectionBytes) |
| 172 | CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock); |
| 173 | } |
| 174 | |
| 175 | StringRef getFileNameForFileOffset(uint32_t FileOffset) override { |
| 176 | return CD.getFileNameForFileOffset(FileOffset); |
| 177 | } |
| 178 | |
| 179 | StringRef getStringTable() override { return CD.CVStringTable; } |
| 180 | |
| 181 | private: |
| 182 | COFFDumper &CD; |
| 183 | const SectionRef &SR; |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 184 | const coff_section *Sec; |
| 185 | StringRef SectionContents; |
| 186 | }; |
| 187 | |
Reid Kleckner | 4a14bca | 2016-05-05 00:34:33 +0000 | [diff] [blame] | 188 | } // end namespace |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 189 | |
| 190 | namespace llvm { |
| 191 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 192 | std::error_code createCOFFDumper(const object::ObjectFile *Obj, |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 193 | ScopedPrinter &Writer, |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 194 | std::unique_ptr<ObjDumper> &Result) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 195 | const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj); |
| 196 | if (!COFFObj) |
| 197 | return readobj_error::unsupported_obj_file_format; |
| 198 | |
| 199 | Result.reset(new COFFDumper(COFFObj, Writer)); |
| 200 | return readobj_error::success; |
| 201 | } |
| 202 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 203 | } // namespace llvm |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 204 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 205 | // Given a a section and an offset into this section the function returns the |
| 206 | // symbol used for the relocation at the offset. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 207 | std::error_code COFFDumper::resolveSymbol(const coff_section *Section, |
| 208 | uint64_t Offset, SymbolRef &Sym) { |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 209 | cacheRelocations(); |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 210 | const auto &Relocations = RelocMap[Section]; |
David Majnemer | 61f1798 | 2016-05-29 06:18:08 +0000 | [diff] [blame] | 211 | auto SymI = Obj->symbol_end(); |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 212 | for (const auto &Relocation : Relocations) { |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 213 | uint64_t RelocationOffset = Relocation.getOffset(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 214 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 215 | if (RelocationOffset == Offset) { |
David Majnemer | 61f1798 | 2016-05-29 06:18:08 +0000 | [diff] [blame] | 216 | SymI = Relocation.getSymbol(); |
| 217 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
David Majnemer | 61f1798 | 2016-05-29 06:18:08 +0000 | [diff] [blame] | 220 | if (SymI == Obj->symbol_end()) |
| 221 | return readobj_error::unknown_symbol; |
| 222 | Sym = *SymI; |
| 223 | return readobj_error::success; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 226 | // Given a section and an offset into this section the function returns the name |
| 227 | // of the symbol used for the relocation at the offset. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 228 | std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, |
| 229 | uint64_t Offset, |
| 230 | StringRef &Name) { |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 231 | SymbolRef Symbol; |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 232 | if (std::error_code EC = resolveSymbol(Section, Offset, Symbol)) |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 233 | return EC; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 234 | Expected<StringRef> NameOrErr = Symbol.getName(); |
| 235 | if (!NameOrErr) |
| 236 | return errorToErrorCode(NameOrErr.takeError()); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 237 | Name = *NameOrErr; |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 238 | return std::error_code(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 241 | // Helper for when you have a pointer to real data and you want to know about |
| 242 | // relocations against it. |
| 243 | std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, |
| 244 | StringRef SectionContents, |
| 245 | const void *RelocPtr, |
| 246 | StringRef &Name) { |
| 247 | assert(SectionContents.data() < RelocPtr && |
| 248 | RelocPtr < SectionContents.data() + SectionContents.size() && |
| 249 | "pointer to relocated object is not in section"); |
| 250 | uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) - |
| 251 | SectionContents.data()); |
| 252 | return resolveSymbolName(Section, Offset, Name); |
| 253 | } |
| 254 | |
| 255 | void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec, |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 256 | uint32_t RelocOffset, uint32_t Offset, |
| 257 | StringRef *RelocSym) { |
| 258 | StringRef SymStorage; |
| 259 | StringRef &Symbol = RelocSym ? *RelocSym : SymStorage; |
| 260 | if (!resolveSymbolName(Sec, RelocOffset, Symbol)) |
| 261 | W.printSymbolOffset(Label, Symbol, Offset); |
| 262 | else |
| 263 | W.printHex(Label, RelocOffset); |
| 264 | } |
| 265 | |
| 266 | void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec, |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 267 | StringRef SectionContents, |
| 268 | const ulittle32_t *Field, |
| 269 | StringRef *RelocSym) { |
| 270 | StringRef SymStorage; |
| 271 | StringRef &Symbol = RelocSym ? *RelocSym : SymStorage; |
| 272 | if (!resolveSymbolName(Sec, SectionContents, Field, Symbol)) |
| 273 | W.printSymbolOffset(Label, Symbol, *Field); |
| 274 | else |
| 275 | W.printHex(Label, *Field); |
| 276 | } |
| 277 | |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 278 | void COFFDumper::printBinaryBlockWithRelocs(StringRef Label, |
| 279 | const SectionRef &Sec, |
| 280 | StringRef SectionContents, |
| 281 | StringRef Block) { |
| 282 | W.printBinaryBlock(Label, Block); |
| 283 | |
| 284 | assert(SectionContents.begin() < Block.begin() && |
| 285 | SectionContents.end() >= Block.end() && |
| 286 | "Block is not contained in SectionContents"); |
| 287 | uint64_t OffsetStart = Block.data() - SectionContents.data(); |
| 288 | uint64_t OffsetEnd = OffsetStart + Block.size(); |
| 289 | |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 290 | W.flush(); |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 291 | cacheRelocations(); |
| 292 | ListScope D(W, "BlockRelocations"); |
| 293 | const coff_section *Section = Obj->getCOFFSection(Sec); |
| 294 | const auto &Relocations = RelocMap[Section]; |
| 295 | for (const auto &Relocation : Relocations) { |
| 296 | uint64_t RelocationOffset = Relocation.getOffset(); |
| 297 | if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd) |
| 298 | printRelocation(Sec, Relocation, OffsetStart); |
| 299 | } |
| 300 | } |
| 301 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 302 | static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 303 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ), |
| 304 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ), |
| 305 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ), |
| 306 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ), |
Saleem Abdulrasool | 5e1780e | 2014-03-11 03:08:37 +0000 | [diff] [blame] | 307 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 308 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ), |
| 309 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ), |
| 310 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ), |
| 311 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ), |
| 312 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ), |
| 313 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ), |
| 314 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16), |
| 315 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ), |
| 316 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP), |
| 317 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ), |
| 318 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ), |
| 319 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ), |
| 320 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ), |
| 321 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ), |
| 322 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ), |
| 323 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2) |
| 324 | }; |
| 325 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 326 | static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 327 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ), |
| 328 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ), |
| 329 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ), |
| 330 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ), |
| 331 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ), |
| 332 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ), |
| 333 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ), |
| 334 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ), |
| 335 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ), |
| 336 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP), |
| 337 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ), |
| 338 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ), |
| 339 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ), |
| 340 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ), |
| 341 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI ) |
| 342 | }; |
| 343 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 344 | static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = { |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 345 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ), |
| 346 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ), |
| 347 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ), |
| 348 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ), |
| 349 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ), |
| 350 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ), |
| 351 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ), |
| 352 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER), |
| 353 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ), |
| 354 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ), |
| 355 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ), |
| 356 | }; |
| 357 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 358 | static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = { |
Rui Ueyama | 06dc5e7 | 2014-01-27 04:22:24 +0000 | [diff] [blame] | 359 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 360 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ), |
| 361 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ), |
| 362 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ), |
| 363 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ), |
| 364 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ), |
| 365 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ), |
Saleem Abdulrasool | d90f86d | 2014-06-27 03:11:18 +0000 | [diff] [blame] | 366 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 367 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ), |
Saleem Abdulrasool | d90f86d | 2014-06-27 03:11:18 +0000 | [diff] [blame] | 368 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 369 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE), |
| 370 | }; |
| 371 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 372 | static const EnumEntry<COFF::SectionCharacteristics> |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 373 | ImageSectionCharacteristics[] = { |
David Majnemer | d323888 | 2015-07-30 16:47:56 +0000 | [diff] [blame] | 374 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD ), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 375 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ), |
| 376 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ), |
| 377 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ), |
| 378 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA), |
| 379 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ), |
| 380 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ), |
| 381 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ), |
| 382 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ), |
| 383 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ), |
| 384 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ), |
| 385 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ), |
| 386 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ), |
| 387 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ), |
| 388 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ), |
| 389 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ), |
| 390 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ), |
| 391 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ), |
| 392 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ), |
| 393 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ), |
| 394 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ), |
| 395 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ), |
| 396 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ), |
| 397 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ), |
| 398 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ), |
| 399 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ), |
| 400 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ), |
| 401 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ), |
| 402 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ), |
| 403 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ), |
| 404 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ), |
| 405 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ), |
| 406 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ), |
| 407 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ), |
| 408 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ), |
| 409 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE ) |
| 410 | }; |
| 411 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 412 | static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 413 | { "Null" , COFF::IMAGE_SYM_TYPE_NULL }, |
| 414 | { "Void" , COFF::IMAGE_SYM_TYPE_VOID }, |
| 415 | { "Char" , COFF::IMAGE_SYM_TYPE_CHAR }, |
| 416 | { "Short" , COFF::IMAGE_SYM_TYPE_SHORT }, |
| 417 | { "Int" , COFF::IMAGE_SYM_TYPE_INT }, |
| 418 | { "Long" , COFF::IMAGE_SYM_TYPE_LONG }, |
| 419 | { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT }, |
| 420 | { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE }, |
| 421 | { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT }, |
| 422 | { "Union" , COFF::IMAGE_SYM_TYPE_UNION }, |
| 423 | { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM }, |
| 424 | { "MOE" , COFF::IMAGE_SYM_TYPE_MOE }, |
| 425 | { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE }, |
| 426 | { "Word" , COFF::IMAGE_SYM_TYPE_WORD }, |
| 427 | { "UInt" , COFF::IMAGE_SYM_TYPE_UINT }, |
| 428 | { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD } |
| 429 | }; |
| 430 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 431 | static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 432 | { "Null" , COFF::IMAGE_SYM_DTYPE_NULL }, |
| 433 | { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER }, |
| 434 | { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION }, |
| 435 | { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY } |
| 436 | }; |
| 437 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 438 | static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 439 | { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION }, |
| 440 | { "Null" , COFF::IMAGE_SYM_CLASS_NULL }, |
| 441 | { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC }, |
| 442 | { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL }, |
| 443 | { "Static" , COFF::IMAGE_SYM_CLASS_STATIC }, |
| 444 | { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER }, |
| 445 | { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF }, |
| 446 | { "Label" , COFF::IMAGE_SYM_CLASS_LABEL }, |
| 447 | { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL }, |
| 448 | { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT }, |
| 449 | { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT }, |
| 450 | { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG }, |
| 451 | { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION }, |
| 452 | { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG }, |
| 453 | { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION }, |
| 454 | { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC }, |
| 455 | { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG }, |
| 456 | { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM }, |
| 457 | { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM }, |
| 458 | { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD }, |
| 459 | { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK }, |
| 460 | { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION }, |
| 461 | { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT }, |
| 462 | { "File" , COFF::IMAGE_SYM_CLASS_FILE }, |
| 463 | { "Section" , COFF::IMAGE_SYM_CLASS_SECTION }, |
| 464 | { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL }, |
| 465 | { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN } |
| 466 | }; |
| 467 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 468 | static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 469 | { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES }, |
| 470 | { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY }, |
| 471 | { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE }, |
| 472 | { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH }, |
| 473 | { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE }, |
| 474 | { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST }, |
| 475 | { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST } |
| 476 | }; |
| 477 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 478 | static const EnumEntry<COFF::WeakExternalCharacteristics> |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 479 | WeakExternalCharacteristics[] = { |
| 480 | { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY }, |
| 481 | { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY }, |
| 482 | { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS } |
| 483 | }; |
| 484 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 485 | static const EnumEntry<uint32_t> SubSectionTypes[] = { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 486 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, Symbols), |
| 487 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, Lines), |
| 488 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, StringTable), |
| 489 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FileChecksums), |
| 490 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FrameData), |
| 491 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, InlineeLines), |
| 492 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CrossScopeImports), |
| 493 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CrossScopeExports), |
| 494 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, ILLines), |
| 495 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FuncMDTokenMap), |
| 496 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, TypeMDTokenMap), |
| 497 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, MergedAssemblyInput), |
| 498 | LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CoffSymbolRVA), |
| 499 | }; |
| 500 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 501 | static const EnumEntry<uint32_t> FrameDataFlags[] = { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 502 | LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH), |
| 503 | LLVM_READOBJ_ENUM_ENT(FrameData, HasEH), |
| 504 | LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart), |
| 505 | }; |
| 506 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 507 | static const EnumEntry<uint8_t> FileChecksumKindNames[] = { |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 508 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None), |
| 509 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5), |
| 510 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1), |
| 511 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256), |
| 512 | }; |
| 513 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 514 | template <typename T> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 515 | static std::error_code getSymbolAuxData(const COFFObjectFile *Obj, |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 516 | COFFSymbolRef Symbol, |
| 517 | uint8_t AuxSymbolIdx, const T *&Aux) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 518 | ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 519 | AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 520 | Aux = reinterpret_cast<const T*>(AuxData.data()); |
| 521 | return readobj_error::success; |
| 522 | } |
| 523 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 524 | void COFFDumper::cacheRelocations() { |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 525 | if (RelocCached) |
| 526 | return; |
| 527 | RelocCached = true; |
| 528 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 529 | for (const SectionRef &S : Obj->sections()) { |
| 530 | const coff_section *Section = Obj->getCOFFSection(S); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 531 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 532 | for (const RelocationRef &Reloc : S.relocations()) |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 533 | RelocMap[Section].push_back(Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 534 | |
| 535 | // Sort relocations by address. |
| 536 | std::sort(RelocMap[Section].begin(), RelocMap[Section].end(), |
| 537 | relocAddressLess); |
| 538 | } |
| 539 | } |
| 540 | |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 541 | void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) { |
| 542 | const data_directory *Data; |
| 543 | if (Obj->getDataDirectory(Index, Data)) |
| 544 | return; |
| 545 | W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress); |
| 546 | W.printHex(FieldName + "Size", Data->Size); |
| 547 | } |
| 548 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 549 | void COFFDumper::printFileHeaders() { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 550 | time_t TDS = Obj->getTimeDateStamp(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 551 | char FormattedTime[20] = { }; |
| 552 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); |
| 553 | |
| 554 | { |
| 555 | DictScope D(W, "ImageFileHeader"); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 556 | W.printEnum ("Machine", Obj->getMachine(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 557 | makeArrayRef(ImageFileMachineType)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 558 | W.printNumber("SectionCount", Obj->getNumberOfSections()); |
| 559 | W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp()); |
| 560 | W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable()); |
| 561 | W.printNumber("SymbolCount", Obj->getNumberOfSymbols()); |
| 562 | W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader()); |
| 563 | W.printFlags ("Characteristics", Obj->getCharacteristics(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 564 | makeArrayRef(ImageFileCharacteristics)); |
| 565 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 566 | |
| 567 | // Print PE header. This header does not exist if this is an object file and |
| 568 | // not an executable. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 569 | const pe32_header *PEHeader = nullptr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 570 | error(Obj->getPE32Header(PEHeader)); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 571 | if (PEHeader) |
| 572 | printPEHeader<pe32_header>(PEHeader); |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 573 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 574 | const pe32plus_header *PEPlusHeader = nullptr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 575 | error(Obj->getPE32PlusHeader(PEPlusHeader)); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 576 | if (PEPlusHeader) |
| 577 | printPEHeader<pe32plus_header>(PEPlusHeader); |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 578 | |
| 579 | if (const dos_header *DH = Obj->getDOSHeader()) |
| 580 | printDOSHeader(DH); |
| 581 | } |
| 582 | |
| 583 | void COFFDumper::printDOSHeader(const dos_header *DH) { |
| 584 | DictScope D(W, "DOSHeader"); |
| 585 | W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic))); |
| 586 | W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage); |
| 587 | W.printNumber("FileSizeInPages", DH->FileSizeInPages); |
| 588 | W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems); |
| 589 | W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs); |
| 590 | W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs); |
| 591 | W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs); |
| 592 | W.printNumber("InitialRelativeSS", DH->InitialRelativeSS); |
| 593 | W.printNumber("InitialSP", DH->InitialSP); |
| 594 | W.printNumber("Checksum", DH->Checksum); |
| 595 | W.printNumber("InitialIP", DH->InitialIP); |
| 596 | W.printNumber("InitialRelativeCS", DH->InitialRelativeCS); |
| 597 | W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable); |
| 598 | W.printNumber("OverlayNumber", DH->OverlayNumber); |
| 599 | W.printNumber("OEMid", DH->OEMid); |
| 600 | W.printNumber("OEMinfo", DH->OEMinfo); |
| 601 | W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 602 | } |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 603 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 604 | template <class PEHeader> |
| 605 | void COFFDumper::printPEHeader(const PEHeader *Hdr) { |
| 606 | DictScope D(W, "ImageOptionalHeader"); |
| 607 | W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion); |
| 608 | W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion); |
| 609 | W.printNumber("SizeOfCode", Hdr->SizeOfCode); |
| 610 | W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData); |
| 611 | W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData); |
| 612 | W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint); |
| 613 | W.printHex ("BaseOfCode", Hdr->BaseOfCode); |
| 614 | printBaseOfDataField(Hdr); |
| 615 | W.printHex ("ImageBase", Hdr->ImageBase); |
| 616 | W.printNumber("SectionAlignment", Hdr->SectionAlignment); |
| 617 | W.printNumber("FileAlignment", Hdr->FileAlignment); |
| 618 | W.printNumber("MajorOperatingSystemVersion", |
| 619 | Hdr->MajorOperatingSystemVersion); |
| 620 | W.printNumber("MinorOperatingSystemVersion", |
| 621 | Hdr->MinorOperatingSystemVersion); |
| 622 | W.printNumber("MajorImageVersion", Hdr->MajorImageVersion); |
| 623 | W.printNumber("MinorImageVersion", Hdr->MinorImageVersion); |
| 624 | W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion); |
| 625 | W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion); |
| 626 | W.printNumber("SizeOfImage", Hdr->SizeOfImage); |
| 627 | W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders); |
| 628 | W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem)); |
David Majnemer | 774aadf | 2014-11-18 02:45:28 +0000 | [diff] [blame] | 629 | W.printFlags ("Characteristics", Hdr->DLLCharacteristics, |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 630 | makeArrayRef(PEDLLCharacteristics)); |
| 631 | W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve); |
| 632 | W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit); |
| 633 | W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve); |
| 634 | W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit); |
| 635 | W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 636 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 637 | if (Hdr->NumberOfRvaAndSize > 0) { |
| 638 | DictScope D(W, "DataDirectory"); |
| 639 | static const char * const directory[] = { |
| 640 | "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable", |
| 641 | "CertificateTable", "BaseRelocationTable", "Debug", "Architecture", |
| 642 | "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT", |
| 643 | "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved" |
| 644 | }; |
| 645 | |
| 646 | for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) { |
| 647 | printDataDirectory(i, directory[i]); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 648 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 649 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 652 | void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { |
| 653 | W.printHex("BaseOfData", Hdr->BaseOfData); |
| 654 | } |
| 655 | |
| 656 | void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} |
| 657 | |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 658 | void COFFDumper::printCodeViewDebugInfo() { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 659 | // Print types first to build CVUDTNames, then print symbols. |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 660 | for (const SectionRef &S : Obj->sections()) { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 661 | StringRef SectionName; |
| 662 | error(S.getName(SectionName)); |
| 663 | if (SectionName == ".debug$T") |
| 664 | printCodeViewTypeSection(SectionName, S); |
| 665 | } |
| 666 | for (const SectionRef &S : Obj->sections()) { |
| 667 | StringRef SectionName; |
| 668 | error(S.getName(SectionName)); |
| 669 | if (SectionName == ".debug$S") |
| 670 | printCodeViewSymbolSection(SectionName, S); |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 674 | void COFFDumper::initializeFileAndStringTables(StringRef Data) { |
| 675 | while (!Data.empty() && (CVFileChecksumTable.data() == nullptr || |
| 676 | CVStringTable.data() == nullptr)) { |
| 677 | // The section consists of a number of subsection in the following format: |
| 678 | // |SubSectionType|SubSectionSize|Contents...| |
| 679 | uint32_t SubType, SubSectionSize; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 680 | error(consume(Data, SubType)); |
| 681 | error(consume(Data, SubSectionSize)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 682 | if (SubSectionSize > Data.size()) |
| 683 | return error(object_error::parse_failed); |
| 684 | switch (ModuleSubstreamKind(SubType)) { |
| 685 | case ModuleSubstreamKind::FileChecksums: |
| 686 | CVFileChecksumTable = Data.substr(0, SubSectionSize); |
| 687 | break; |
| 688 | case ModuleSubstreamKind::StringTable: |
| 689 | CVStringTable = Data.substr(0, SubSectionSize); |
| 690 | break; |
| 691 | default: |
| 692 | break; |
| 693 | } |
David Majnemer | efaaf41 | 2016-05-28 20:04:48 +0000 | [diff] [blame] | 694 | uint32_t PaddedSize = alignTo(SubSectionSize, 4); |
| 695 | if (PaddedSize > Data.size()) |
| 696 | error(object_error::parse_failed); |
| 697 | Data = Data.drop_front(PaddedSize); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 701 | void COFFDumper::printCodeViewSymbolSection(StringRef SectionName, |
| 702 | const SectionRef &Section) { |
| 703 | StringRef SectionContents; |
| 704 | error(Section.getContents(SectionContents)); |
| 705 | StringRef Data = SectionContents; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 706 | |
| 707 | SmallVector<StringRef, 10> FunctionNames; |
| 708 | StringMap<StringRef> FunctionLineTables; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 709 | |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 710 | ListScope D(W, "CodeViewDebugInfo"); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 711 | // Print the section to allow correlation with printSections. |
| 712 | W.printNumber("Section", SectionName, Obj->getSectionID(Section)); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 713 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 714 | uint32_t Magic; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 715 | error(consume(Data, Magic)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 716 | W.printHex("Magic", Magic); |
| 717 | if (Magic != COFF::DEBUG_SECTION_MAGIC) |
| 718 | return error(object_error::parse_failed); |
| 719 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 720 | initializeFileAndStringTables(Data); |
| 721 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 722 | while (!Data.empty()) { |
| 723 | // The section consists of a number of subsection in the following format: |
| 724 | // |SubSectionType|SubSectionSize|Contents...| |
| 725 | uint32_t SubType, SubSectionSize; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 726 | error(consume(Data, SubType)); |
| 727 | error(consume(Data, SubSectionSize)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 728 | |
| 729 | ListScope S(W, "Subsection"); |
| 730 | W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes)); |
| 731 | W.printHex("SubSectionSize", SubSectionSize); |
| 732 | |
| 733 | // Get the contents of the subsection. |
| 734 | if (SubSectionSize > Data.size()) |
| 735 | return error(object_error::parse_failed); |
| 736 | StringRef Contents = Data.substr(0, SubSectionSize); |
| 737 | |
| 738 | // Add SubSectionSize to the current offset and align that offset to find |
| 739 | // the next subsection. |
| 740 | size_t SectionOffset = Data.data() - SectionContents.data(); |
| 741 | size_t NextOffset = SectionOffset + SubSectionSize; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 742 | NextOffset = alignTo(NextOffset, 4); |
David Majnemer | cab2b46 | 2016-05-28 20:04:50 +0000 | [diff] [blame] | 743 | if (NextOffset > SectionContents.size()) |
| 744 | return error(object_error::parse_failed); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 745 | Data = SectionContents.drop_front(NextOffset); |
| 746 | |
| 747 | // Optionally print the subsection bytes in case our parsing gets confused |
| 748 | // later. |
| 749 | if (opts::CodeViewSubsectionBytes) |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 750 | printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents, |
| 751 | Contents); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 752 | |
| 753 | switch (ModuleSubstreamKind(SubType)) { |
| 754 | case ModuleSubstreamKind::Symbols: |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 755 | printCodeViewSymbolsSubsection(Contents, Section, SectionContents); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 756 | break; |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 757 | |
| 758 | case ModuleSubstreamKind::InlineeLines: |
| 759 | printCodeViewInlineeLines(Contents); |
| 760 | break; |
| 761 | |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 762 | case ModuleSubstreamKind::FileChecksums: |
| 763 | printCodeViewFileChecksums(Contents); |
| 764 | break; |
| 765 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 766 | case ModuleSubstreamKind::Lines: { |
| 767 | // Holds a PC to file:line table. Some data to parse this subsection is |
| 768 | // stored in the other subsections, so just check sanity and store the |
| 769 | // pointers for deferred processing. |
| 770 | |
| 771 | if (SubSectionSize < 12) { |
| 772 | // There should be at least three words to store two function |
| 773 | // relocations and size of the code. |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 774 | error(object_error::parse_failed); |
| 775 | return; |
| 776 | } |
| 777 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 778 | StringRef LinkageName; |
| 779 | error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset, |
| 780 | LinkageName)); |
| 781 | W.printString("LinkageName", LinkageName); |
| 782 | if (FunctionLineTables.count(LinkageName) != 0) { |
| 783 | // Saw debug info for this function already? |
| 784 | error(object_error::parse_failed); |
| 785 | return; |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 786 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 787 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 788 | FunctionLineTables[LinkageName] = Contents; |
| 789 | FunctionNames.push_back(LinkageName); |
| 790 | break; |
| 791 | } |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 792 | case ModuleSubstreamKind::FrameData: { |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 793 | // First four bytes is a relocation against the function. |
| 794 | const uint32_t *CodePtr; |
| 795 | error(consumeObject(Contents, CodePtr)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 796 | StringRef LinkageName; |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 797 | error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents, |
| 798 | CodePtr, LinkageName)); |
| 799 | W.printString("LinkageName", LinkageName); |
| 800 | |
| 801 | // To find the active frame description, search this array for the |
| 802 | // smallest PC range that includes the current PC. |
| 803 | while (!Contents.empty()) { |
| 804 | const FrameData *FD; |
| 805 | error(consumeObject(Contents, FD)); |
David Majnemer | a6d93fd | 2016-05-28 19:45:49 +0000 | [diff] [blame] | 806 | |
| 807 | if (FD->FrameFunc >= CVStringTable.size()) |
| 808 | error(object_error::parse_failed); |
| 809 | |
| 810 | StringRef FrameFunc = |
| 811 | CVStringTable.drop_front(FD->FrameFunc).split('\0').first; |
| 812 | |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 813 | DictScope S(W, "FrameData"); |
| 814 | W.printHex("RvaStart", FD->RvaStart); |
| 815 | W.printHex("CodeSize", FD->CodeSize); |
| 816 | W.printHex("LocalSize", FD->LocalSize); |
| 817 | W.printHex("ParamsSize", FD->ParamsSize); |
| 818 | W.printHex("MaxStackSize", FD->MaxStackSize); |
David Majnemer | a6d93fd | 2016-05-28 19:45:49 +0000 | [diff] [blame] | 819 | W.printString("FrameFunc", FrameFunc); |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 820 | W.printHex("PrologSize", FD->PrologSize); |
| 821 | W.printHex("SavedRegsSize", FD->SavedRegsSize); |
| 822 | W.printFlags("Flags", FD->Flags, makeArrayRef(FrameDataFlags)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 823 | } |
| 824 | break; |
| 825 | } |
| 826 | |
| 827 | // Do nothing for unrecognized subsections. |
| 828 | default: |
| 829 | break; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 830 | } |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 831 | W.flush(); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | // Dump the line tables now that we've read all the subsections and know all |
| 835 | // the required information. |
| 836 | for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) { |
| 837 | StringRef Name = FunctionNames[I]; |
| 838 | ListScope S(W, "FunctionLineTable"); |
Reid Kleckner | 7c0c0c0 | 2015-12-15 01:23:55 +0000 | [diff] [blame] | 839 | W.printString("LinkageName", Name); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 840 | |
| 841 | DataExtractor DE(FunctionLineTables[Name], true, 4); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 842 | uint32_t Offset = 6; // Skip relocations. |
| 843 | uint16_t Flags = DE.getU16(&Offset); |
| 844 | W.printHex("Flags", Flags); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 845 | bool HasColumnInformation = Flags & codeview::LineFlags::HaveColumns; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 846 | uint32_t FunctionSize = DE.getU32(&Offset); |
| 847 | W.printHex("CodeSize", FunctionSize); |
| 848 | while (DE.isValidOffset(Offset)) { |
| 849 | // For each range of lines with the same filename, we have a segment |
| 850 | // in the line table. The filename string is accessed using double |
| 851 | // indirection to the string table subsection using the index subsection. |
| 852 | uint32_t OffsetInIndex = DE.getU32(&Offset), |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 853 | NumLines = DE.getU32(&Offset), |
David Majnemer | 0373d53 | 2015-07-09 18:14:31 +0000 | [diff] [blame] | 854 | FullSegmentSize = DE.getU32(&Offset); |
| 855 | |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 856 | uint32_t ColumnOffset = Offset + 8 * NumLines; |
| 857 | DataExtractor ColumnDE(DE.getData(), true, 4); |
| 858 | |
David Majnemer | 0373d53 | 2015-07-09 18:14:31 +0000 | [diff] [blame] | 859 | if (FullSegmentSize != |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 860 | 12 + 8 * NumLines + (HasColumnInformation ? 4 * NumLines : 0)) { |
David Majnemer | 0373d53 | 2015-07-09 18:14:31 +0000 | [diff] [blame] | 861 | error(object_error::parse_failed); |
| 862 | return; |
| 863 | } |
Justin Bogner | a1413db | 2015-07-09 04:27:36 +0000 | [diff] [blame] | 864 | |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 865 | ListScope S(W, "FilenameSegment"); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 866 | printFileNameForOffset("Filename", OffsetInIndex); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 867 | for (unsigned LineIdx = 0; |
| 868 | LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) { |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 869 | // Then go the (PC, LineNumber) pairs. The line number is stored in the |
| 870 | // least significant 31 bits of the respective word in the table. |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 871 | uint32_t PC = DE.getU32(&Offset), LineData = DE.getU32(&Offset); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 872 | if (PC >= FunctionSize) { |
| 873 | error(object_error::parse_failed); |
| 874 | return; |
| 875 | } |
| 876 | char Buffer[32]; |
| 877 | format("+0x%X", PC).snprint(Buffer, 32); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 878 | ListScope PCScope(W, Buffer); |
David Majnemer | a4859df | 2016-02-04 17:57:12 +0000 | [diff] [blame] | 879 | LineInfo LI(LineData); |
| 880 | if (LI.isAlwaysStepInto()) |
| 881 | W.printString("StepInto", StringRef("Always")); |
| 882 | else if (LI.isNeverStepInto()) |
| 883 | W.printString("StepInto", StringRef("Never")); |
| 884 | else |
| 885 | W.printNumber("LineNumberStart", LI.getStartLine()); |
| 886 | W.printNumber("LineNumberEndDelta", LI.getLineDelta()); |
| 887 | W.printBoolean("IsStatement", LI.isStatement()); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 888 | if (HasColumnInformation && |
| 889 | ColumnDE.isValidOffsetForDataOfSize(ColumnOffset, 4)) { |
| 890 | uint16_t ColStart = ColumnDE.getU16(&ColumnOffset); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 891 | W.printNumber("ColStart", ColStart); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 892 | uint16_t ColEnd = ColumnDE.getU16(&ColumnOffset); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 893 | W.printNumber("ColEnd", ColEnd); |
| 894 | } |
| 895 | } |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 896 | // Skip over the column data. |
| 897 | if (HasColumnInformation) { |
| 898 | for (unsigned LineIdx = 0; |
| 899 | LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) { |
| 900 | DE.getU32(&Offset); |
| 901 | } |
| 902 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 907 | void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection, |
| 908 | const SectionRef &Section, |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 909 | StringRef SectionContents) { |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 910 | ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(), |
| 911 | Subsection.bytes_end()); |
| 912 | auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj, |
| 913 | SectionContents); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 914 | |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 915 | CVSymbolDumper CVSD(W, CVTD, std::move(CODD), opts::CodeViewSubsectionBytes); |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 916 | ByteStream Stream(BinaryData); |
| 917 | CVSymbolArray Symbols; |
| 918 | StreamReader Reader(Stream); |
| 919 | if (auto EC = Reader.readArray(Symbols, Reader.getLength())) { |
| 920 | consumeError(std::move(EC)); |
| 921 | W.flush(); |
| 922 | error(object_error::parse_failed); |
| 923 | } |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 924 | |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 925 | if (!CVSD.dump(Symbols)) { |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 926 | W.flush(); |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 927 | error(object_error::parse_failed); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 928 | } |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 929 | W.flush(); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 930 | } |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 931 | |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 932 | void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) { |
| 933 | StringRef Data = Subsection; |
| 934 | while (!Data.empty()) { |
| 935 | DictScope S(W, "FileChecksum"); |
| 936 | const FileChecksum *FC; |
| 937 | error(consumeObject(Data, FC)); |
| 938 | if (FC->FileNameOffset >= CVStringTable.size()) |
| 939 | error(object_error::parse_failed); |
| 940 | StringRef Filename = |
| 941 | CVStringTable.drop_front(FC->FileNameOffset).split('\0').first; |
| 942 | W.printHex("Filename", Filename, FC->FileNameOffset); |
| 943 | W.printHex("ChecksumSize", FC->ChecksumSize); |
| 944 | W.printEnum("ChecksumKind", uint8_t(FC->ChecksumKind), |
| 945 | makeArrayRef(FileChecksumKindNames)); |
| 946 | if (FC->ChecksumSize >= Data.size()) |
| 947 | error(object_error::parse_failed); |
| 948 | StringRef ChecksumBytes = Data.substr(0, FC->ChecksumSize); |
| 949 | W.printBinary("ChecksumBytes", ChecksumBytes); |
Rafael Espindola | a79078c | 2016-01-18 20:55:24 +0000 | [diff] [blame] | 950 | unsigned PaddedSize = alignTo(FC->ChecksumSize + sizeof(FileChecksum), 4) - |
| 951 | sizeof(FileChecksum); |
David Majnemer | 37dafba | 2016-05-28 19:45:58 +0000 | [diff] [blame] | 952 | if (PaddedSize > Data.size()) |
| 953 | error(object_error::parse_failed); |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 954 | Data = Data.drop_front(PaddedSize); |
| 955 | } |
| 956 | } |
| 957 | |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 958 | void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) { |
| 959 | StringRef Data = Subsection; |
| 960 | uint32_t Signature; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 961 | error(consume(Data, Signature)); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 962 | bool HasExtraFiles = Signature == unsigned(InlineeLinesSignature::ExtraFiles); |
| 963 | |
| 964 | while (!Data.empty()) { |
| 965 | const InlineeSourceLine *ISL; |
| 966 | error(consumeObject(Data, ISL)); |
| 967 | DictScope S(W, "InlineeSourceLine"); |
| 968 | printTypeIndex("Inlinee", ISL->Inlinee); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 969 | printFileNameForOffset("FileID", ISL->FileID); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 970 | W.printNumber("SourceLineNum", ISL->SourceLineNum); |
| 971 | |
| 972 | if (HasExtraFiles) { |
| 973 | uint32_t ExtraFileCount; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 974 | error(consume(Data, ExtraFileCount)); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 975 | W.printNumber("ExtraFileCount", ExtraFileCount); |
| 976 | ListScope ExtraFiles(W, "ExtraFiles"); |
| 977 | for (unsigned I = 0; I < ExtraFileCount; ++I) { |
| 978 | uint32_t FileID; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 979 | error(consume(Data, FileID)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 980 | printFileNameForOffset("FileID", FileID); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 986 | StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) { |
| 987 | // The file checksum subsection should precede all references to it. |
| 988 | if (!CVFileChecksumTable.data() || !CVStringTable.data()) |
| 989 | error(object_error::parse_failed); |
| 990 | // Check if the file checksum table offset is valid. |
| 991 | if (FileOffset >= CVFileChecksumTable.size()) |
| 992 | error(object_error::parse_failed); |
| 993 | |
| 994 | // The string table offset comes first before the file checksum. |
| 995 | StringRef Data = CVFileChecksumTable.drop_front(FileOffset); |
| 996 | uint32_t StringOffset; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 997 | error(consume(Data, StringOffset)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 998 | |
| 999 | // Check if the string table offset is valid. |
| 1000 | if (StringOffset >= CVStringTable.size()) |
| 1001 | error(object_error::parse_failed); |
| 1002 | |
| 1003 | // Return the null-terminated string. |
| 1004 | return CVStringTable.drop_front(StringOffset).split('\0').first; |
| 1005 | } |
| 1006 | |
| 1007 | void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) { |
| 1008 | W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset); |
| 1009 | } |
| 1010 | |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1011 | void COFFDumper::mergeCodeViewTypes(MemoryTypeTableBuilder &CVTypes) { |
| 1012 | for (const SectionRef &S : Obj->sections()) { |
| 1013 | StringRef SectionName; |
| 1014 | error(S.getName(SectionName)); |
| 1015 | if (SectionName == ".debug$T") { |
| 1016 | StringRef Data; |
| 1017 | error(S.getContents(Data)); |
David Majnemer | 78b0d72 | 2016-05-28 19:17:48 +0000 | [diff] [blame] | 1018 | uint32_t Magic; |
| 1019 | error(consume(Data, Magic)); |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1020 | if (Magic != 4) |
| 1021 | error(object_error::parse_failed); |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1022 | ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(Data.data()), |
| 1023 | Data.size()); |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 1024 | ByteStream Stream(Bytes); |
| 1025 | CVTypeArray Types; |
| 1026 | StreamReader Reader(Stream); |
| 1027 | if (auto EC = Reader.readArray(Types, Reader.getLength())) { |
| 1028 | consumeError(std::move(EC)); |
| 1029 | W.flush(); |
| 1030 | error(object_error::parse_failed); |
| 1031 | } |
| 1032 | |
| 1033 | if (!mergeTypeStreams(CVTypes, Types)) |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1034 | return error(object_error::parse_failed); |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1039 | void COFFDumper::printCodeViewTypeSection(StringRef SectionName, |
| 1040 | const SectionRef &Section) { |
| 1041 | ListScope D(W, "CodeViewTypes"); |
| 1042 | W.printNumber("Section", SectionName, Obj->getSectionID(Section)); |
Adrian McCarthy | 963a331 | 2016-05-02 23:45:03 +0000 | [diff] [blame] | 1043 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1044 | StringRef Data; |
| 1045 | error(Section.getContents(Data)); |
Reid Kleckner | b2bd281 | 2016-05-02 18:10:00 +0000 | [diff] [blame] | 1046 | if (opts::CodeViewSubsectionBytes) |
| 1047 | W.printBinaryBlock("Data", Data); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1048 | |
| 1049 | uint32_t Magic; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 1050 | error(consume(Data, Magic)); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1051 | W.printHex("Magic", Magic); |
| 1052 | if (Magic != COFF::DEBUG_SECTION_MAGIC) |
| 1053 | return error(object_error::parse_failed); |
| 1054 | |
Reid Kleckner | 3b3f490 | 2016-05-31 18:15:23 +0000 | [diff] [blame] | 1055 | if (!CVTD.dump({Data.bytes_begin(), Data.bytes_end()})) { |
Reid Kleckner | 4a14bca | 2016-05-05 00:34:33 +0000 | [diff] [blame] | 1056 | W.flush(); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1057 | error(object_error::parse_failed); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1058 | } |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1061 | void COFFDumper::printSections() { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1062 | ListScope SectionsD(W, "Sections"); |
| 1063 | int SectionNumber = 0; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1064 | for (const SectionRef &Sec : Obj->sections()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1065 | ++SectionNumber; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1066 | const coff_section *Section = Obj->getCOFFSection(Sec); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1067 | |
| 1068 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1069 | error(Sec.getName(Name)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1070 | |
| 1071 | DictScope D(W, "Section"); |
| 1072 | W.printNumber("Number", SectionNumber); |
| 1073 | W.printBinary("Name", Name, Section->Name); |
| 1074 | W.printHex ("VirtualSize", Section->VirtualSize); |
| 1075 | W.printHex ("VirtualAddress", Section->VirtualAddress); |
| 1076 | W.printNumber("RawDataSize", Section->SizeOfRawData); |
| 1077 | W.printHex ("PointerToRawData", Section->PointerToRawData); |
| 1078 | W.printHex ("PointerToRelocations", Section->PointerToRelocations); |
| 1079 | W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers); |
| 1080 | W.printNumber("RelocationCount", Section->NumberOfRelocations); |
| 1081 | W.printNumber("LineNumberCount", Section->NumberOfLinenumbers); |
| 1082 | W.printFlags ("Characteristics", Section->Characteristics, |
| 1083 | makeArrayRef(ImageSectionCharacteristics), |
| 1084 | COFF::SectionCharacteristics(0x00F00000)); |
| 1085 | |
| 1086 | if (opts::SectionRelocations) { |
| 1087 | ListScope D(W, "Relocations"); |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1088 | for (const RelocationRef &Reloc : Sec.relocations()) |
| 1089 | printRelocation(Sec, Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | if (opts::SectionSymbols) { |
| 1093 | ListScope D(W, "Symbols"); |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1094 | for (const SymbolRef &Symbol : Obj->symbols()) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1095 | if (!Sec.containsSymbol(Symbol)) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1096 | continue; |
| 1097 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1098 | printSymbol(Symbol); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | |
David Majnemer | dac3985 | 2014-09-26 22:32:16 +0000 | [diff] [blame] | 1102 | if (opts::SectionData && |
| 1103 | !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1104 | StringRef Data; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1105 | error(Sec.getContents(Data)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1106 | |
| 1107 | W.printBinaryBlock("SectionData", Data); |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | void COFFDumper::printRelocations() { |
| 1113 | ListScope D(W, "Relocations"); |
| 1114 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1115 | int SectionNumber = 0; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1116 | for (const SectionRef &Section : Obj->sections()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1117 | ++SectionNumber; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1118 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1119 | error(Section.getName(Name)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1120 | |
| 1121 | bool PrintedGroup = false; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1122 | for (const RelocationRef &Reloc : Section.relocations()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1123 | if (!PrintedGroup) { |
| 1124 | W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; |
| 1125 | W.indent(); |
| 1126 | PrintedGroup = true; |
| 1127 | } |
| 1128 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1129 | printRelocation(Section, Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | if (PrintedGroup) { |
| 1133 | W.unindent(); |
| 1134 | W.startLine() << "}\n"; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1139 | void COFFDumper::printRelocation(const SectionRef &Section, |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 1140 | const RelocationRef &Reloc, uint64_t Bias) { |
| 1141 | uint64_t Offset = Reloc.getOffset() - Bias; |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1142 | uint64_t RelocType = Reloc.getType(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1143 | SmallString<32> RelocName; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1144 | StringRef SymbolName; |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 1145 | Reloc.getTypeName(RelocName); |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1146 | symbol_iterator Symbol = Reloc.getSymbol(); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1147 | if (Symbol != Obj->symbol_end()) { |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1148 | Expected<StringRef> SymbolNameOrErr = Symbol->getName(); |
| 1149 | error(errorToErrorCode(SymbolNameOrErr.takeError())); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1150 | SymbolName = *SymbolNameOrErr; |
| 1151 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1152 | |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1153 | if (opts::ExpandRelocs) { |
| 1154 | DictScope Group(W, "Relocation"); |
| 1155 | W.printHex("Offset", Offset); |
| 1156 | W.printNumber("Type", RelocName, RelocType); |
David Majnemer | 1f80b0a | 2014-11-13 07:42:11 +0000 | [diff] [blame] | 1157 | W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName); |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1158 | } else { |
| 1159 | raw_ostream& OS = W.startLine(); |
| 1160 | OS << W.hex(Offset) |
| 1161 | << " " << RelocName |
David Majnemer | 1f80b0a | 2014-11-13 07:42:11 +0000 | [diff] [blame] | 1162 | << " " << (SymbolName.empty() ? "-" : SymbolName) |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1163 | << "\n"; |
| 1164 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | void COFFDumper::printSymbols() { |
| 1168 | ListScope Group(W, "Symbols"); |
| 1169 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1170 | for (const SymbolRef &Symbol : Obj->symbols()) |
| 1171 | printSymbol(Symbol); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1174 | void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1175 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1176 | static ErrorOr<StringRef> |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1177 | getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber, |
| 1178 | const coff_section *Section) { |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1179 | if (Section) { |
| 1180 | StringRef SectionName; |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1181 | if (std::error_code EC = Obj->getSectionName(Section, SectionName)) |
| 1182 | return EC; |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1183 | return SectionName; |
| 1184 | } |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1185 | if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1186 | return StringRef("IMAGE_SYM_DEBUG"); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1187 | if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1188 | return StringRef("IMAGE_SYM_ABSOLUTE"); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1189 | if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1190 | return StringRef("IMAGE_SYM_UNDEFINED"); |
| 1191 | return StringRef(""); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1194 | void COFFDumper::printSymbol(const SymbolRef &Sym) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1195 | DictScope D(W, "Symbol"); |
| 1196 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1197 | COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1198 | const coff_section *Section; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1199 | if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1200 | W.startLine() << "Invalid section number: " << EC.message() << "\n"; |
| 1201 | W.flush(); |
| 1202 | return; |
| 1203 | } |
| 1204 | |
| 1205 | StringRef SymbolName; |
| 1206 | if (Obj->getSymbolName(Symbol, SymbolName)) |
| 1207 | SymbolName = ""; |
| 1208 | |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1209 | StringRef SectionName = ""; |
| 1210 | ErrorOr<StringRef> Res = |
| 1211 | getSectionName(Obj, Symbol.getSectionNumber(), Section); |
| 1212 | if (Res) |
| 1213 | SectionName = *Res; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1214 | |
| 1215 | W.printString("Name", SymbolName); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1216 | W.printNumber("Value", Symbol.getValue()); |
| 1217 | W.printNumber("Section", SectionName, Symbol.getSectionNumber()); |
| 1218 | W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType)); |
| 1219 | W.printEnum ("ComplexType", Symbol.getComplexType(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1220 | makeArrayRef(ImageSymDType)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1221 | W.printEnum ("StorageClass", Symbol.getStorageClass(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1222 | makeArrayRef(ImageSymClass)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1223 | W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1224 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1225 | for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) { |
| 1226 | if (Symbol.isFunctionDefinition()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1227 | const coff_aux_function_definition *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1228 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1229 | |
| 1230 | DictScope AS(W, "AuxFunctionDef"); |
| 1231 | W.printNumber("TagIndex", Aux->TagIndex); |
| 1232 | W.printNumber("TotalSize", Aux->TotalSize); |
David Majnemer | f3a2af5 | 2014-03-19 04:33:27 +0000 | [diff] [blame] | 1233 | W.printHex("PointerToLineNumber", Aux->PointerToLinenumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1234 | W.printHex("PointerToNextFunction", Aux->PointerToNextFunction); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1235 | |
David Majnemer | c7d7c6f | 2014-10-31 05:07:00 +0000 | [diff] [blame] | 1236 | } else if (Symbol.isAnyUndefined()) { |
David Majnemer | f3a2af5 | 2014-03-19 04:33:27 +0000 | [diff] [blame] | 1237 | const coff_aux_weak_external *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1238 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1239 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1240 | ErrorOr<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1241 | StringRef LinkedName; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1242 | std::error_code EC = Linked.getError(); |
| 1243 | if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1244 | LinkedName = ""; |
| 1245 | error(EC); |
| 1246 | } |
| 1247 | |
| 1248 | DictScope AS(W, "AuxWeakExternal"); |
| 1249 | W.printNumber("Linked", LinkedName, Aux->TagIndex); |
| 1250 | W.printEnum ("Search", Aux->Characteristics, |
| 1251 | makeArrayRef(WeakExternalCharacteristics)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1252 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1253 | } else if (Symbol.isFileRecord()) { |
| 1254 | const char *FileName; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1255 | error(getSymbolAuxData(Obj, Symbol, I, FileName)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1256 | |
Nico Rieck | 0ab8e60 | 2013-04-22 08:35:11 +0000 | [diff] [blame] | 1257 | DictScope AS(W, "AuxFileRecord"); |
Saleem Abdulrasool | d38c6b1 | 2014-04-14 02:37:23 +0000 | [diff] [blame] | 1258 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1259 | StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() * |
| 1260 | Obj->getSymbolTableEntrySize()); |
Saleem Abdulrasool | d38c6b1 | 2014-04-14 02:37:23 +0000 | [diff] [blame] | 1261 | W.printString("FileName", Name.rtrim(StringRef("\0", 1))); |
Saleem Abdulrasool | 3b5e001 | 2014-04-16 04:15:29 +0000 | [diff] [blame] | 1262 | break; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1263 | } else if (Symbol.isSectionDefinition()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1264 | const coff_aux_section_definition *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1265 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1266 | |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1267 | int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj()); |
| 1268 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1269 | DictScope AS(W, "AuxSectionDef"); |
| 1270 | W.printNumber("Length", Aux->Length); |
| 1271 | W.printNumber("RelocationCount", Aux->NumberOfRelocations); |
| 1272 | W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers); |
| 1273 | W.printHex("Checksum", Aux->CheckSum); |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1274 | W.printNumber("Number", AuxNumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1275 | W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1276 | |
Nico Rieck | a711dee | 2013-04-22 08:34:59 +0000 | [diff] [blame] | 1277 | if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1278 | && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { |
| 1279 | const coff_section *Assoc; |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1280 | StringRef AssocName = ""; |
| 1281 | std::error_code EC = Obj->getSection(AuxNumber, Assoc); |
| 1282 | ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc); |
| 1283 | if (Res) |
| 1284 | AssocName = *Res; |
| 1285 | if (!EC) |
| 1286 | EC = Res.getError(); |
| 1287 | if (EC) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1288 | AssocName = ""; |
| 1289 | error(EC); |
| 1290 | } |
| 1291 | |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1292 | W.printNumber("AssocSection", AssocName, AuxNumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1293 | } |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1294 | } else if (Symbol.isCLRToken()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1295 | const coff_aux_clr_token *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1296 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1297 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1298 | ErrorOr<COFFSymbolRef> ReferredSym = |
| 1299 | Obj->getSymbol(Aux->SymbolTableIndex); |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1300 | StringRef ReferredName; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1301 | std::error_code EC = ReferredSym.getError(); |
| 1302 | if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) { |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1303 | ReferredName = ""; |
| 1304 | error(EC); |
| 1305 | } |
| 1306 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1307 | DictScope AS(W, "AuxCLRToken"); |
| 1308 | W.printNumber("AuxType", Aux->AuxType); |
| 1309 | W.printNumber("Reserved", Aux->Reserved); |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1310 | W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1311 | |
| 1312 | } else { |
| 1313 | W.startLine() << "<unhandled auxiliary record>\n"; |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | void COFFDumper::printUnwindInfo() { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1319 | ListScope D(W, "UnwindInformation"); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1320 | switch (Obj->getMachine()) { |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1321 | case COFF::IMAGE_FILE_MACHINE_AMD64: { |
| 1322 | Win64EH::Dumper Dumper(W); |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1323 | Win64EH::Dumper::SymbolResolver |
| 1324 | Resolver = [](const object::coff_section *Section, uint64_t Offset, |
| 1325 | SymbolRef &Symbol, void *user_data) -> std::error_code { |
| 1326 | COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data); |
| 1327 | return Dumper->resolveSymbol(Section, Offset, Symbol); |
| 1328 | }; |
Saleem Abdulrasool | 65dbbb5 | 2014-05-25 21:37:59 +0000 | [diff] [blame] | 1329 | Win64EH::Dumper::Context Ctx(*Obj, Resolver, this); |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1330 | Dumper.printData(Ctx); |
| 1331 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1332 | } |
Saleem Abdulrasool | e6971ca | 2014-06-04 15:47:15 +0000 | [diff] [blame] | 1333 | case COFF::IMAGE_FILE_MACHINE_ARMNT: { |
| 1334 | ARM::WinEH::Decoder Decoder(W); |
| 1335 | Decoder.dumpProcedureData(*Obj); |
| 1336 | break; |
| 1337 | } |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1338 | default: |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1339 | W.printEnum("unsupported Image Machine", Obj->getMachine(), |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1340 | makeArrayRef(ImageFileMachineType)); |
| 1341 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1345 | void COFFDumper::printImportedSymbols( |
| 1346 | iterator_range<imported_symbol_iterator> Range) { |
| 1347 | for (const ImportedSymbolRef &I : Range) { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1348 | StringRef Sym; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1349 | error(I.getSymbolName(Sym)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1350 | uint16_t Ordinal; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1351 | error(I.getOrdinal(Ordinal)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1352 | W.printNumber("Symbol", Sym, Ordinal); |
| 1353 | } |
| 1354 | } |
| 1355 | |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1356 | void COFFDumper::printDelayImportedSymbols( |
| 1357 | const DelayImportDirectoryEntryRef &I, |
| 1358 | iterator_range<imported_symbol_iterator> Range) { |
| 1359 | int Index = 0; |
| 1360 | for (const ImportedSymbolRef &S : Range) { |
| 1361 | DictScope Import(W, "Import"); |
| 1362 | StringRef Sym; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1363 | error(S.getSymbolName(Sym)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1364 | uint16_t Ordinal; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1365 | error(S.getOrdinal(Ordinal)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1366 | W.printNumber("Symbol", Sym, Ordinal); |
| 1367 | uint64_t Addr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1368 | error(I.getImportAddress(Index++, Addr)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1369 | W.printHex("Address", Addr); |
| 1370 | } |
| 1371 | } |
| 1372 | |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1373 | void COFFDumper::printCOFFImports() { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1374 | // Regular imports |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1375 | for (const ImportDirectoryEntryRef &I : Obj->import_directories()) { |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1376 | DictScope Import(W, "Import"); |
| 1377 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1378 | error(I.getName(Name)); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1379 | W.printString("Name", Name); |
| 1380 | uint32_t Addr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1381 | error(I.getImportLookupTableRVA(Addr)); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1382 | W.printHex("ImportLookupTableRVA", Addr); |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1383 | error(I.getImportAddressTableRVA(Addr)); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1384 | W.printHex("ImportAddressTableRVA", Addr); |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1385 | printImportedSymbols(I.imported_symbols()); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | // Delay imports |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1389 | for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1390 | DictScope Import(W, "DelayImport"); |
| 1391 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1392 | error(I.getName(Name)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1393 | W.printString("Name", Name); |
Rui Ueyama | 1af0865 | 2014-10-03 18:07:18 +0000 | [diff] [blame] | 1394 | const delay_import_directory_table_entry *Table; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1395 | error(I.getDelayImportTable(Table)); |
Rui Ueyama | 1af0865 | 2014-10-03 18:07:18 +0000 | [diff] [blame] | 1396 | W.printHex("Attributes", Table->Attributes); |
| 1397 | W.printHex("ModuleHandle", Table->ModuleHandle); |
| 1398 | W.printHex("ImportAddressTable", Table->DelayImportAddressTable); |
| 1399 | W.printHex("ImportNameTable", Table->DelayImportNameTable); |
| 1400 | W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable); |
| 1401 | W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1402 | printDelayImportedSymbols(I, I.imported_symbols()); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1403 | } |
| 1404 | } |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1405 | |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 1406 | void COFFDumper::printCOFFExports() { |
| 1407 | for (const ExportDirectoryEntryRef &E : Obj->export_directories()) { |
| 1408 | DictScope Export(W, "Export"); |
| 1409 | |
| 1410 | StringRef Name; |
| 1411 | uint32_t Ordinal, RVA; |
| 1412 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1413 | error(E.getSymbolName(Name)); |
| 1414 | error(E.getOrdinal(Ordinal)); |
| 1415 | error(E.getExportRVA(RVA)); |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 1416 | |
| 1417 | W.printNumber("Ordinal", Ordinal); |
| 1418 | W.printString("Name", Name); |
| 1419 | W.printHex("RVA", RVA); |
| 1420 | } |
| 1421 | } |
| 1422 | |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1423 | void COFFDumper::printCOFFDirectives() { |
| 1424 | for (const SectionRef &Section : Obj->sections()) { |
| 1425 | StringRef Contents; |
| 1426 | StringRef Name; |
| 1427 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1428 | error(Section.getName(Name)); |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1429 | if (Name != ".drectve") |
| 1430 | continue; |
| 1431 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1432 | error(Section.getContents(Contents)); |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1433 | |
| 1434 | W.printString("Directive(s)", Contents); |
| 1435 | } |
| 1436 | } |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1437 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1438 | static StringRef getBaseRelocTypeName(uint8_t Type) { |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1439 | switch (Type) { |
| 1440 | case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE"; |
| 1441 | case COFF::IMAGE_REL_BASED_HIGH: return "HIGH"; |
| 1442 | case COFF::IMAGE_REL_BASED_LOW: return "LOW"; |
| 1443 | case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW"; |
| 1444 | case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ"; |
Saleem Abdulrasool | 5a41c37 | 2015-01-16 20:16:09 +0000 | [diff] [blame] | 1445 | case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)"; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1446 | case COFF::IMAGE_REL_BASED_DIR64: return "DIR64"; |
Colin LeMahieu | 9fbffee | 2014-11-19 17:10:39 +0000 | [diff] [blame] | 1447 | default: return "unknown (" + llvm::utostr(Type) + ")"; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | void COFFDumper::printCOFFBaseReloc() { |
| 1452 | ListScope D(W, "BaseReloc"); |
| 1453 | for (const BaseRelocRef &I : Obj->base_relocs()) { |
| 1454 | uint8_t Type; |
| 1455 | uint32_t RVA; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1456 | error(I.getRVA(RVA)); |
| 1457 | error(I.getType(Type)); |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1458 | DictScope Import(W, "Entry"); |
| 1459 | W.printString("Type", getBaseRelocTypeName(Type)); |
| 1460 | W.printHex("Address", RVA); |
| 1461 | } |
| 1462 | } |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 1463 | |
| 1464 | void COFFDumper::printStackMap() const { |
| 1465 | object::SectionRef StackMapSection; |
| 1466 | for (auto Sec : Obj->sections()) { |
| 1467 | StringRef Name; |
| 1468 | Sec.getName(Name); |
| 1469 | if (Name == ".llvm_stackmaps") { |
| 1470 | StackMapSection = Sec; |
| 1471 | break; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | if (StackMapSection == object::SectionRef()) |
| 1476 | return; |
| 1477 | |
| 1478 | StringRef StackMapContents; |
| 1479 | StackMapSection.getContents(StackMapContents); |
| 1480 | ArrayRef<uint8_t> StackMapContentsArray( |
| 1481 | reinterpret_cast<const uint8_t*>(StackMapContents.data()), |
| 1482 | StackMapContents.size()); |
| 1483 | |
| 1484 | if (Obj->isLittleEndian()) |
| 1485 | prettyPrintStackMap( |
| 1486 | llvm::outs(), |
| 1487 | StackMapV1Parser<support::little>(StackMapContentsArray)); |
| 1488 | else |
| 1489 | prettyPrintStackMap(llvm::outs(), |
| 1490 | StackMapV1Parser<support::big>(StackMapContentsArray)); |
| 1491 | } |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1492 | |
| 1493 | void llvm::dumpCodeViewMergedTypes( |
| 1494 | ScopedPrinter &Writer, llvm::codeview::MemoryTypeTableBuilder &CVTypes) { |
| 1495 | // Flatten it first, then run our dumper on it. |
| 1496 | ListScope S(Writer, "MergedTypeStream"); |
| 1497 | SmallString<0> Buf; |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 1498 | CVTypes.ForEachRecord([&](TypeIndex TI, StringRef Record) { |
| 1499 | Buf.append(Record.begin(), Record.end()); |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1500 | }); |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 1501 | CVTypeDumper CVTD(&Writer, opts::CodeViewSubsectionBytes); |
Reid Kleckner | 3b3f490 | 2016-05-31 18:15:23 +0000 | [diff] [blame] | 1502 | if (!CVTD.dump({Buf.str().bytes_begin(), Buf.str().bytes_end()})) { |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1503 | Writer.flush(); |
| 1504 | error(object_error::parse_failed); |
| 1505 | } |
| 1506 | } |