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