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" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/CodeView/Line.h" |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/CodeView/MemoryTypeTableBuilder.h" |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/CodeView/RecordSerialization.h" |
| 29 | #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h" |
| 30 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 31 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Reid Kleckner | 4a14bca | 2016-05-05 00:34:33 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/CodeView/TypeDumper.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 34 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Zachary Turner | 2d02cee | 2016-05-03 22:18:17 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/CodeView/TypeStream.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 | |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 497 | static const EnumEntry<uint8_t> ProcSymFlagNames[] = { |
| 498 | LLVM_READOBJ_ENUM_CLASS_ENT(ProcSymFlags, HasFP), |
| 499 | LLVM_READOBJ_ENUM_CLASS_ENT(ProcSymFlags, HasIRET), |
| 500 | LLVM_READOBJ_ENUM_CLASS_ENT(ProcSymFlags, HasFRET), |
| 501 | LLVM_READOBJ_ENUM_CLASS_ENT(ProcSymFlags, IsNoReturn), |
| 502 | LLVM_READOBJ_ENUM_CLASS_ENT(ProcSymFlags, IsUnreachable), |
| 503 | LLVM_READOBJ_ENUM_CLASS_ENT(ProcSymFlags, HasCustomCallingConv), |
| 504 | LLVM_READOBJ_ENUM_CLASS_ENT(ProcSymFlags, IsNoInline), |
| 505 | LLVM_READOBJ_ENUM_CLASS_ENT(ProcSymFlags, HasOptimizedDebugInfo), |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 506 | }; |
| 507 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 508 | static const EnumEntry<uint32_t> FrameDataFlags[] = { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 509 | LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH), |
| 510 | LLVM_READOBJ_ENUM_ENT(FrameData, HasEH), |
| 511 | LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart), |
| 512 | }; |
| 513 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 514 | static const EnumEntry<uint8_t> FileChecksumKindNames[] = { |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 515 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None), |
| 516 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5), |
| 517 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1), |
| 518 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256), |
| 519 | }; |
| 520 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 521 | template <typename T> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 522 | static std::error_code getSymbolAuxData(const COFFObjectFile *Obj, |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 523 | COFFSymbolRef Symbol, |
| 524 | uint8_t AuxSymbolIdx, const T *&Aux) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 525 | ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 526 | AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 527 | Aux = reinterpret_cast<const T*>(AuxData.data()); |
| 528 | return readobj_error::success; |
| 529 | } |
| 530 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 531 | void COFFDumper::cacheRelocations() { |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 532 | if (RelocCached) |
| 533 | return; |
| 534 | RelocCached = true; |
| 535 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 536 | for (const SectionRef &S : Obj->sections()) { |
| 537 | const coff_section *Section = Obj->getCOFFSection(S); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 538 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 539 | for (const RelocationRef &Reloc : S.relocations()) |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 540 | RelocMap[Section].push_back(Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 541 | |
| 542 | // Sort relocations by address. |
| 543 | std::sort(RelocMap[Section].begin(), RelocMap[Section].end(), |
| 544 | relocAddressLess); |
| 545 | } |
| 546 | } |
| 547 | |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 548 | void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) { |
| 549 | const data_directory *Data; |
| 550 | if (Obj->getDataDirectory(Index, Data)) |
| 551 | return; |
| 552 | W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress); |
| 553 | W.printHex(FieldName + "Size", Data->Size); |
| 554 | } |
| 555 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 556 | void COFFDumper::printFileHeaders() { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 557 | time_t TDS = Obj->getTimeDateStamp(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 558 | char FormattedTime[20] = { }; |
| 559 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); |
| 560 | |
| 561 | { |
| 562 | DictScope D(W, "ImageFileHeader"); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 563 | W.printEnum ("Machine", Obj->getMachine(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 564 | makeArrayRef(ImageFileMachineType)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 565 | W.printNumber("SectionCount", Obj->getNumberOfSections()); |
| 566 | W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp()); |
| 567 | W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable()); |
| 568 | W.printNumber("SymbolCount", Obj->getNumberOfSymbols()); |
| 569 | W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader()); |
| 570 | W.printFlags ("Characteristics", Obj->getCharacteristics(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 571 | makeArrayRef(ImageFileCharacteristics)); |
| 572 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 573 | |
| 574 | // Print PE header. This header does not exist if this is an object file and |
| 575 | // not an executable. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 576 | const pe32_header *PEHeader = nullptr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 577 | error(Obj->getPE32Header(PEHeader)); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 578 | if (PEHeader) |
| 579 | printPEHeader<pe32_header>(PEHeader); |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 580 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 581 | const pe32plus_header *PEPlusHeader = nullptr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 582 | error(Obj->getPE32PlusHeader(PEPlusHeader)); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 583 | if (PEPlusHeader) |
| 584 | printPEHeader<pe32plus_header>(PEPlusHeader); |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 585 | |
| 586 | if (const dos_header *DH = Obj->getDOSHeader()) |
| 587 | printDOSHeader(DH); |
| 588 | } |
| 589 | |
| 590 | void COFFDumper::printDOSHeader(const dos_header *DH) { |
| 591 | DictScope D(W, "DOSHeader"); |
| 592 | W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic))); |
| 593 | W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage); |
| 594 | W.printNumber("FileSizeInPages", DH->FileSizeInPages); |
| 595 | W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems); |
| 596 | W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs); |
| 597 | W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs); |
| 598 | W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs); |
| 599 | W.printNumber("InitialRelativeSS", DH->InitialRelativeSS); |
| 600 | W.printNumber("InitialSP", DH->InitialSP); |
| 601 | W.printNumber("Checksum", DH->Checksum); |
| 602 | W.printNumber("InitialIP", DH->InitialIP); |
| 603 | W.printNumber("InitialRelativeCS", DH->InitialRelativeCS); |
| 604 | W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable); |
| 605 | W.printNumber("OverlayNumber", DH->OverlayNumber); |
| 606 | W.printNumber("OEMid", DH->OEMid); |
| 607 | W.printNumber("OEMinfo", DH->OEMinfo); |
| 608 | W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 609 | } |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 610 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 611 | template <class PEHeader> |
| 612 | void COFFDumper::printPEHeader(const PEHeader *Hdr) { |
| 613 | DictScope D(W, "ImageOptionalHeader"); |
| 614 | W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion); |
| 615 | W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion); |
| 616 | W.printNumber("SizeOfCode", Hdr->SizeOfCode); |
| 617 | W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData); |
| 618 | W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData); |
| 619 | W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint); |
| 620 | W.printHex ("BaseOfCode", Hdr->BaseOfCode); |
| 621 | printBaseOfDataField(Hdr); |
| 622 | W.printHex ("ImageBase", Hdr->ImageBase); |
| 623 | W.printNumber("SectionAlignment", Hdr->SectionAlignment); |
| 624 | W.printNumber("FileAlignment", Hdr->FileAlignment); |
| 625 | W.printNumber("MajorOperatingSystemVersion", |
| 626 | Hdr->MajorOperatingSystemVersion); |
| 627 | W.printNumber("MinorOperatingSystemVersion", |
| 628 | Hdr->MinorOperatingSystemVersion); |
| 629 | W.printNumber("MajorImageVersion", Hdr->MajorImageVersion); |
| 630 | W.printNumber("MinorImageVersion", Hdr->MinorImageVersion); |
| 631 | W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion); |
| 632 | W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion); |
| 633 | W.printNumber("SizeOfImage", Hdr->SizeOfImage); |
| 634 | W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders); |
| 635 | W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem)); |
David Majnemer | 774aadf | 2014-11-18 02:45:28 +0000 | [diff] [blame] | 636 | W.printFlags ("Characteristics", Hdr->DLLCharacteristics, |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 637 | makeArrayRef(PEDLLCharacteristics)); |
| 638 | W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve); |
| 639 | W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit); |
| 640 | W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve); |
| 641 | W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit); |
| 642 | W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 643 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 644 | if (Hdr->NumberOfRvaAndSize > 0) { |
| 645 | DictScope D(W, "DataDirectory"); |
| 646 | static const char * const directory[] = { |
| 647 | "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable", |
| 648 | "CertificateTable", "BaseRelocationTable", "Debug", "Architecture", |
| 649 | "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT", |
| 650 | "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved" |
| 651 | }; |
| 652 | |
| 653 | for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) { |
| 654 | printDataDirectory(i, directory[i]); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 655 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 656 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 659 | void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { |
| 660 | W.printHex("BaseOfData", Hdr->BaseOfData); |
| 661 | } |
| 662 | |
| 663 | void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} |
| 664 | |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 665 | void COFFDumper::printCodeViewDebugInfo() { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 666 | // Print types first to build CVUDTNames, then print symbols. |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 667 | for (const SectionRef &S : Obj->sections()) { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 668 | StringRef SectionName; |
| 669 | error(S.getName(SectionName)); |
| 670 | if (SectionName == ".debug$T") |
| 671 | printCodeViewTypeSection(SectionName, S); |
| 672 | } |
| 673 | for (const SectionRef &S : Obj->sections()) { |
| 674 | StringRef SectionName; |
| 675 | error(S.getName(SectionName)); |
| 676 | if (SectionName == ".debug$S") |
| 677 | printCodeViewSymbolSection(SectionName, S); |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 681 | void COFFDumper::initializeFileAndStringTables(StringRef Data) { |
| 682 | while (!Data.empty() && (CVFileChecksumTable.data() == nullptr || |
| 683 | CVStringTable.data() == nullptr)) { |
| 684 | // The section consists of a number of subsection in the following format: |
| 685 | // |SubSectionType|SubSectionSize|Contents...| |
| 686 | uint32_t SubType, SubSectionSize; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 687 | error(consume(Data, SubType)); |
| 688 | error(consume(Data, SubSectionSize)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 689 | if (SubSectionSize > Data.size()) |
| 690 | return error(object_error::parse_failed); |
| 691 | switch (ModuleSubstreamKind(SubType)) { |
| 692 | case ModuleSubstreamKind::FileChecksums: |
| 693 | CVFileChecksumTable = Data.substr(0, SubSectionSize); |
| 694 | break; |
| 695 | case ModuleSubstreamKind::StringTable: |
| 696 | CVStringTable = Data.substr(0, SubSectionSize); |
| 697 | break; |
| 698 | default: |
| 699 | break; |
| 700 | } |
| 701 | Data = Data.drop_front(alignTo(SubSectionSize, 4)); |
| 702 | } |
| 703 | } |
| 704 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 705 | void COFFDumper::printCodeViewSymbolSection(StringRef SectionName, |
| 706 | const SectionRef &Section) { |
| 707 | StringRef SectionContents; |
| 708 | error(Section.getContents(SectionContents)); |
| 709 | StringRef Data = SectionContents; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 710 | |
| 711 | SmallVector<StringRef, 10> FunctionNames; |
| 712 | StringMap<StringRef> FunctionLineTables; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 713 | |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 714 | ListScope D(W, "CodeViewDebugInfo"); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 715 | // Print the section to allow correlation with printSections. |
| 716 | W.printNumber("Section", SectionName, Obj->getSectionID(Section)); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 717 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 718 | uint32_t Magic; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 719 | error(consume(Data, Magic)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 720 | W.printHex("Magic", Magic); |
| 721 | if (Magic != COFF::DEBUG_SECTION_MAGIC) |
| 722 | return error(object_error::parse_failed); |
| 723 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 724 | initializeFileAndStringTables(Data); |
| 725 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 726 | while (!Data.empty()) { |
| 727 | // The section consists of a number of subsection in the following format: |
| 728 | // |SubSectionType|SubSectionSize|Contents...| |
| 729 | uint32_t SubType, SubSectionSize; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 730 | error(consume(Data, SubType)); |
| 731 | error(consume(Data, SubSectionSize)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 732 | |
| 733 | ListScope S(W, "Subsection"); |
| 734 | W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes)); |
| 735 | W.printHex("SubSectionSize", SubSectionSize); |
| 736 | |
| 737 | // Get the contents of the subsection. |
| 738 | if (SubSectionSize > Data.size()) |
| 739 | return error(object_error::parse_failed); |
| 740 | StringRef Contents = Data.substr(0, SubSectionSize); |
| 741 | |
| 742 | // Add SubSectionSize to the current offset and align that offset to find |
| 743 | // the next subsection. |
| 744 | size_t SectionOffset = Data.data() - SectionContents.data(); |
| 745 | size_t NextOffset = SectionOffset + SubSectionSize; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 746 | NextOffset = alignTo(NextOffset, 4); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 747 | Data = SectionContents.drop_front(NextOffset); |
| 748 | |
| 749 | // Optionally print the subsection bytes in case our parsing gets confused |
| 750 | // later. |
| 751 | if (opts::CodeViewSubsectionBytes) |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 752 | printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents, |
| 753 | Contents); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 754 | |
| 755 | switch (ModuleSubstreamKind(SubType)) { |
| 756 | case ModuleSubstreamKind::Symbols: |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 757 | printCodeViewSymbolsSubsection(Contents, Section, SectionContents); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 758 | break; |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 759 | |
| 760 | case ModuleSubstreamKind::InlineeLines: |
| 761 | printCodeViewInlineeLines(Contents); |
| 762 | break; |
| 763 | |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 764 | case ModuleSubstreamKind::FileChecksums: |
| 765 | printCodeViewFileChecksums(Contents); |
| 766 | break; |
| 767 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 768 | case ModuleSubstreamKind::Lines: { |
| 769 | // Holds a PC to file:line table. Some data to parse this subsection is |
| 770 | // stored in the other subsections, so just check sanity and store the |
| 771 | // pointers for deferred processing. |
| 772 | |
| 773 | if (SubSectionSize < 12) { |
| 774 | // There should be at least three words to store two function |
| 775 | // relocations and size of the code. |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 776 | error(object_error::parse_failed); |
| 777 | return; |
| 778 | } |
| 779 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 780 | StringRef LinkageName; |
| 781 | error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset, |
| 782 | LinkageName)); |
| 783 | W.printString("LinkageName", LinkageName); |
| 784 | if (FunctionLineTables.count(LinkageName) != 0) { |
| 785 | // Saw debug info for this function already? |
| 786 | error(object_error::parse_failed); |
| 787 | return; |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 788 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 789 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 790 | FunctionLineTables[LinkageName] = Contents; |
| 791 | FunctionNames.push_back(LinkageName); |
| 792 | break; |
| 793 | } |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 794 | case ModuleSubstreamKind::FrameData: { |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 795 | // First four bytes is a relocation against the function. |
| 796 | const uint32_t *CodePtr; |
| 797 | error(consumeObject(Contents, CodePtr)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 798 | StringRef LinkageName; |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 799 | error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents, |
| 800 | CodePtr, LinkageName)); |
| 801 | W.printString("LinkageName", LinkageName); |
| 802 | |
| 803 | // To find the active frame description, search this array for the |
| 804 | // smallest PC range that includes the current PC. |
| 805 | while (!Contents.empty()) { |
| 806 | const FrameData *FD; |
| 807 | error(consumeObject(Contents, FD)); |
| 808 | DictScope S(W, "FrameData"); |
| 809 | W.printHex("RvaStart", FD->RvaStart); |
| 810 | W.printHex("CodeSize", FD->CodeSize); |
| 811 | W.printHex("LocalSize", FD->LocalSize); |
| 812 | W.printHex("ParamsSize", FD->ParamsSize); |
| 813 | W.printHex("MaxStackSize", FD->MaxStackSize); |
| 814 | W.printString("FrameFunc", |
| 815 | CVStringTable.drop_front(FD->FrameFunc).split('\0').first); |
| 816 | W.printHex("PrologSize", FD->PrologSize); |
| 817 | W.printHex("SavedRegsSize", FD->SavedRegsSize); |
| 818 | W.printFlags("Flags", FD->Flags, makeArrayRef(FrameDataFlags)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 819 | } |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | // Do nothing for unrecognized subsections. |
| 824 | default: |
| 825 | break; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 826 | } |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 827 | W.flush(); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | // Dump the line tables now that we've read all the subsections and know all |
| 831 | // the required information. |
| 832 | for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) { |
| 833 | StringRef Name = FunctionNames[I]; |
| 834 | ListScope S(W, "FunctionLineTable"); |
Reid Kleckner | 7c0c0c0 | 2015-12-15 01:23:55 +0000 | [diff] [blame] | 835 | W.printString("LinkageName", Name); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 836 | |
| 837 | DataExtractor DE(FunctionLineTables[Name], true, 4); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 838 | uint32_t Offset = 6; // Skip relocations. |
| 839 | uint16_t Flags = DE.getU16(&Offset); |
| 840 | W.printHex("Flags", Flags); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 841 | bool HasColumnInformation = Flags & codeview::LineFlags::HaveColumns; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 842 | uint32_t FunctionSize = DE.getU32(&Offset); |
| 843 | W.printHex("CodeSize", FunctionSize); |
| 844 | while (DE.isValidOffset(Offset)) { |
| 845 | // For each range of lines with the same filename, we have a segment |
| 846 | // in the line table. The filename string is accessed using double |
| 847 | // indirection to the string table subsection using the index subsection. |
| 848 | uint32_t OffsetInIndex = DE.getU32(&Offset), |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 849 | NumLines = DE.getU32(&Offset), |
David Majnemer | 0373d53 | 2015-07-09 18:14:31 +0000 | [diff] [blame] | 850 | FullSegmentSize = DE.getU32(&Offset); |
| 851 | |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 852 | uint32_t ColumnOffset = Offset + 8 * NumLines; |
| 853 | DataExtractor ColumnDE(DE.getData(), true, 4); |
| 854 | |
David Majnemer | 0373d53 | 2015-07-09 18:14:31 +0000 | [diff] [blame] | 855 | if (FullSegmentSize != |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 856 | 12 + 8 * NumLines + (HasColumnInformation ? 4 * NumLines : 0)) { |
David Majnemer | 0373d53 | 2015-07-09 18:14:31 +0000 | [diff] [blame] | 857 | error(object_error::parse_failed); |
| 858 | return; |
| 859 | } |
Justin Bogner | a1413db | 2015-07-09 04:27:36 +0000 | [diff] [blame] | 860 | |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 861 | ListScope S(W, "FilenameSegment"); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 862 | printFileNameForOffset("Filename", OffsetInIndex); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 863 | for (unsigned LineIdx = 0; |
| 864 | LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) { |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 865 | // Then go the (PC, LineNumber) pairs. The line number is stored in the |
| 866 | // least significant 31 bits of the respective word in the table. |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 867 | uint32_t PC = DE.getU32(&Offset), LineData = DE.getU32(&Offset); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 868 | if (PC >= FunctionSize) { |
| 869 | error(object_error::parse_failed); |
| 870 | return; |
| 871 | } |
| 872 | char Buffer[32]; |
| 873 | format("+0x%X", PC).snprint(Buffer, 32); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 874 | ListScope PCScope(W, Buffer); |
David Majnemer | a4859df | 2016-02-04 17:57:12 +0000 | [diff] [blame] | 875 | LineInfo LI(LineData); |
| 876 | if (LI.isAlwaysStepInto()) |
| 877 | W.printString("StepInto", StringRef("Always")); |
| 878 | else if (LI.isNeverStepInto()) |
| 879 | W.printString("StepInto", StringRef("Never")); |
| 880 | else |
| 881 | W.printNumber("LineNumberStart", LI.getStartLine()); |
| 882 | W.printNumber("LineNumberEndDelta", LI.getLineDelta()); |
| 883 | W.printBoolean("IsStatement", LI.isStatement()); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 884 | if (HasColumnInformation && |
| 885 | ColumnDE.isValidOffsetForDataOfSize(ColumnOffset, 4)) { |
| 886 | uint16_t ColStart = ColumnDE.getU16(&ColumnOffset); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 887 | W.printNumber("ColStart", ColStart); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 888 | uint16_t ColEnd = ColumnDE.getU16(&ColumnOffset); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 889 | W.printNumber("ColEnd", ColEnd); |
| 890 | } |
| 891 | } |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 892 | // Skip over the column data. |
| 893 | if (HasColumnInformation) { |
| 894 | for (unsigned LineIdx = 0; |
| 895 | LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) { |
| 896 | DE.getU32(&Offset); |
| 897 | } |
| 898 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 903 | void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection, |
| 904 | const SectionRef &Section, |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 905 | StringRef SectionContents) { |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 906 | ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(), |
| 907 | Subsection.bytes_end()); |
| 908 | auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj, |
| 909 | SectionContents); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 910 | |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 911 | CVSymbolDumper CVSD(W, CVTD, std::move(CODD), opts::CodeViewSubsectionBytes); |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 912 | |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 913 | if (!CVSD.dump(BinaryData)) { |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 914 | W.flush(); |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 915 | error(object_error::parse_failed); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 916 | } |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 917 | W.flush(); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 918 | } |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 919 | |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 920 | void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) { |
| 921 | StringRef Data = Subsection; |
| 922 | while (!Data.empty()) { |
| 923 | DictScope S(W, "FileChecksum"); |
| 924 | const FileChecksum *FC; |
| 925 | error(consumeObject(Data, FC)); |
| 926 | if (FC->FileNameOffset >= CVStringTable.size()) |
| 927 | error(object_error::parse_failed); |
| 928 | StringRef Filename = |
| 929 | CVStringTable.drop_front(FC->FileNameOffset).split('\0').first; |
| 930 | W.printHex("Filename", Filename, FC->FileNameOffset); |
| 931 | W.printHex("ChecksumSize", FC->ChecksumSize); |
| 932 | W.printEnum("ChecksumKind", uint8_t(FC->ChecksumKind), |
| 933 | makeArrayRef(FileChecksumKindNames)); |
| 934 | if (FC->ChecksumSize >= Data.size()) |
| 935 | error(object_error::parse_failed); |
| 936 | StringRef ChecksumBytes = Data.substr(0, FC->ChecksumSize); |
| 937 | W.printBinary("ChecksumBytes", ChecksumBytes); |
Rafael Espindola | a79078c | 2016-01-18 20:55:24 +0000 | [diff] [blame] | 938 | unsigned PaddedSize = alignTo(FC->ChecksumSize + sizeof(FileChecksum), 4) - |
| 939 | sizeof(FileChecksum); |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 940 | Data = Data.drop_front(PaddedSize); |
| 941 | } |
| 942 | } |
| 943 | |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 944 | void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) { |
| 945 | StringRef Data = Subsection; |
| 946 | uint32_t Signature; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 947 | error(consume(Data, Signature)); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 948 | bool HasExtraFiles = Signature == unsigned(InlineeLinesSignature::ExtraFiles); |
| 949 | |
| 950 | while (!Data.empty()) { |
| 951 | const InlineeSourceLine *ISL; |
| 952 | error(consumeObject(Data, ISL)); |
| 953 | DictScope S(W, "InlineeSourceLine"); |
| 954 | printTypeIndex("Inlinee", ISL->Inlinee); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 955 | printFileNameForOffset("FileID", ISL->FileID); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 956 | W.printNumber("SourceLineNum", ISL->SourceLineNum); |
| 957 | |
| 958 | if (HasExtraFiles) { |
| 959 | uint32_t ExtraFileCount; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 960 | error(consume(Data, ExtraFileCount)); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 961 | W.printNumber("ExtraFileCount", ExtraFileCount); |
| 962 | ListScope ExtraFiles(W, "ExtraFiles"); |
| 963 | for (unsigned I = 0; I < ExtraFileCount; ++I) { |
| 964 | uint32_t FileID; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 965 | error(consume(Data, FileID)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 966 | printFileNameForOffset("FileID", FileID); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 972 | StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) { |
| 973 | // The file checksum subsection should precede all references to it. |
| 974 | if (!CVFileChecksumTable.data() || !CVStringTable.data()) |
| 975 | error(object_error::parse_failed); |
| 976 | // Check if the file checksum table offset is valid. |
| 977 | if (FileOffset >= CVFileChecksumTable.size()) |
| 978 | error(object_error::parse_failed); |
| 979 | |
| 980 | // The string table offset comes first before the file checksum. |
| 981 | StringRef Data = CVFileChecksumTable.drop_front(FileOffset); |
| 982 | uint32_t StringOffset; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 983 | error(consume(Data, StringOffset)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 984 | |
| 985 | // Check if the string table offset is valid. |
| 986 | if (StringOffset >= CVStringTable.size()) |
| 987 | error(object_error::parse_failed); |
| 988 | |
| 989 | // Return the null-terminated string. |
| 990 | return CVStringTable.drop_front(StringOffset).split('\0').first; |
| 991 | } |
| 992 | |
| 993 | void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) { |
| 994 | W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset); |
| 995 | } |
| 996 | |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 997 | void COFFDumper::mergeCodeViewTypes(MemoryTypeTableBuilder &CVTypes) { |
| 998 | for (const SectionRef &S : Obj->sections()) { |
| 999 | StringRef SectionName; |
| 1000 | error(S.getName(SectionName)); |
| 1001 | if (SectionName == ".debug$T") { |
| 1002 | StringRef Data; |
| 1003 | error(S.getContents(Data)); |
| 1004 | unsigned Magic = *reinterpret_cast<const ulittle32_t *>(Data.data()); |
| 1005 | if (Magic != 4) |
| 1006 | error(object_error::parse_failed); |
| 1007 | Data = Data.drop_front(4); |
| 1008 | ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(Data.data()), |
| 1009 | Data.size()); |
| 1010 | if (!mergeTypeStreams(CVTypes, Bytes)) |
| 1011 | return error(object_error::parse_failed); |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1016 | void COFFDumper::printCodeViewTypeSection(StringRef SectionName, |
| 1017 | const SectionRef &Section) { |
| 1018 | ListScope D(W, "CodeViewTypes"); |
| 1019 | W.printNumber("Section", SectionName, Obj->getSectionID(Section)); |
Adrian McCarthy | 963a331 | 2016-05-02 23:45:03 +0000 | [diff] [blame] | 1020 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1021 | StringRef Data; |
| 1022 | error(Section.getContents(Data)); |
Reid Kleckner | b2bd281 | 2016-05-02 18:10:00 +0000 | [diff] [blame] | 1023 | if (opts::CodeViewSubsectionBytes) |
| 1024 | W.printBinaryBlock("Data", Data); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1025 | |
| 1026 | uint32_t Magic; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 1027 | error(consume(Data, Magic)); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1028 | W.printHex("Magic", Magic); |
| 1029 | if (Magic != COFF::DEBUG_SECTION_MAGIC) |
| 1030 | return error(object_error::parse_failed); |
| 1031 | |
| 1032 | ArrayRef<uint8_t> BinaryData(reinterpret_cast<const uint8_t *>(Data.data()), |
| 1033 | Data.size()); |
Reid Kleckner | 4a14bca | 2016-05-05 00:34:33 +0000 | [diff] [blame] | 1034 | if (!CVTD.dump(BinaryData)) { |
| 1035 | W.flush(); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1036 | error(object_error::parse_failed); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1037 | } |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1040 | void COFFDumper::printSections() { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1041 | ListScope SectionsD(W, "Sections"); |
| 1042 | int SectionNumber = 0; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1043 | for (const SectionRef &Sec : Obj->sections()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1044 | ++SectionNumber; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1045 | const coff_section *Section = Obj->getCOFFSection(Sec); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1046 | |
| 1047 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1048 | error(Sec.getName(Name)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1049 | |
| 1050 | DictScope D(W, "Section"); |
| 1051 | W.printNumber("Number", SectionNumber); |
| 1052 | W.printBinary("Name", Name, Section->Name); |
| 1053 | W.printHex ("VirtualSize", Section->VirtualSize); |
| 1054 | W.printHex ("VirtualAddress", Section->VirtualAddress); |
| 1055 | W.printNumber("RawDataSize", Section->SizeOfRawData); |
| 1056 | W.printHex ("PointerToRawData", Section->PointerToRawData); |
| 1057 | W.printHex ("PointerToRelocations", Section->PointerToRelocations); |
| 1058 | W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers); |
| 1059 | W.printNumber("RelocationCount", Section->NumberOfRelocations); |
| 1060 | W.printNumber("LineNumberCount", Section->NumberOfLinenumbers); |
| 1061 | W.printFlags ("Characteristics", Section->Characteristics, |
| 1062 | makeArrayRef(ImageSectionCharacteristics), |
| 1063 | COFF::SectionCharacteristics(0x00F00000)); |
| 1064 | |
| 1065 | if (opts::SectionRelocations) { |
| 1066 | ListScope D(W, "Relocations"); |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1067 | for (const RelocationRef &Reloc : Sec.relocations()) |
| 1068 | printRelocation(Sec, Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | if (opts::SectionSymbols) { |
| 1072 | ListScope D(W, "Symbols"); |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1073 | for (const SymbolRef &Symbol : Obj->symbols()) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1074 | if (!Sec.containsSymbol(Symbol)) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1075 | continue; |
| 1076 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1077 | printSymbol(Symbol); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
David Majnemer | dac3985 | 2014-09-26 22:32:16 +0000 | [diff] [blame] | 1081 | if (opts::SectionData && |
| 1082 | !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1083 | StringRef Data; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1084 | error(Sec.getContents(Data)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1085 | |
| 1086 | W.printBinaryBlock("SectionData", Data); |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | void COFFDumper::printRelocations() { |
| 1092 | ListScope D(W, "Relocations"); |
| 1093 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1094 | int SectionNumber = 0; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1095 | for (const SectionRef &Section : Obj->sections()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1096 | ++SectionNumber; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1097 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1098 | error(Section.getName(Name)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1099 | |
| 1100 | bool PrintedGroup = false; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1101 | for (const RelocationRef &Reloc : Section.relocations()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1102 | if (!PrintedGroup) { |
| 1103 | W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; |
| 1104 | W.indent(); |
| 1105 | PrintedGroup = true; |
| 1106 | } |
| 1107 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1108 | printRelocation(Section, Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | if (PrintedGroup) { |
| 1112 | W.unindent(); |
| 1113 | W.startLine() << "}\n"; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1118 | void COFFDumper::printRelocation(const SectionRef &Section, |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 1119 | const RelocationRef &Reloc, uint64_t Bias) { |
| 1120 | uint64_t Offset = Reloc.getOffset() - Bias; |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1121 | uint64_t RelocType = Reloc.getType(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1122 | SmallString<32> RelocName; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1123 | StringRef SymbolName; |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 1124 | Reloc.getTypeName(RelocName); |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1125 | symbol_iterator Symbol = Reloc.getSymbol(); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1126 | if (Symbol != Obj->symbol_end()) { |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1127 | Expected<StringRef> SymbolNameOrErr = Symbol->getName(); |
| 1128 | error(errorToErrorCode(SymbolNameOrErr.takeError())); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1129 | SymbolName = *SymbolNameOrErr; |
| 1130 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1131 | |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1132 | if (opts::ExpandRelocs) { |
| 1133 | DictScope Group(W, "Relocation"); |
| 1134 | W.printHex("Offset", Offset); |
| 1135 | W.printNumber("Type", RelocName, RelocType); |
David Majnemer | 1f80b0a | 2014-11-13 07:42:11 +0000 | [diff] [blame] | 1136 | W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName); |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1137 | } else { |
| 1138 | raw_ostream& OS = W.startLine(); |
| 1139 | OS << W.hex(Offset) |
| 1140 | << " " << RelocName |
David Majnemer | 1f80b0a | 2014-11-13 07:42:11 +0000 | [diff] [blame] | 1141 | << " " << (SymbolName.empty() ? "-" : SymbolName) |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1142 | << "\n"; |
| 1143 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | void COFFDumper::printSymbols() { |
| 1147 | ListScope Group(W, "Symbols"); |
| 1148 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1149 | for (const SymbolRef &Symbol : Obj->symbols()) |
| 1150 | printSymbol(Symbol); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1153 | void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1154 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1155 | static ErrorOr<StringRef> |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1156 | getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber, |
| 1157 | const coff_section *Section) { |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1158 | if (Section) { |
| 1159 | StringRef SectionName; |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1160 | if (std::error_code EC = Obj->getSectionName(Section, SectionName)) |
| 1161 | return EC; |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1162 | return SectionName; |
| 1163 | } |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1164 | if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1165 | return StringRef("IMAGE_SYM_DEBUG"); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1166 | if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1167 | return StringRef("IMAGE_SYM_ABSOLUTE"); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1168 | if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1169 | return StringRef("IMAGE_SYM_UNDEFINED"); |
| 1170 | return StringRef(""); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1173 | void COFFDumper::printSymbol(const SymbolRef &Sym) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1174 | DictScope D(W, "Symbol"); |
| 1175 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1176 | COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1177 | const coff_section *Section; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1178 | if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1179 | W.startLine() << "Invalid section number: " << EC.message() << "\n"; |
| 1180 | W.flush(); |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | StringRef SymbolName; |
| 1185 | if (Obj->getSymbolName(Symbol, SymbolName)) |
| 1186 | SymbolName = ""; |
| 1187 | |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1188 | StringRef SectionName = ""; |
| 1189 | ErrorOr<StringRef> Res = |
| 1190 | getSectionName(Obj, Symbol.getSectionNumber(), Section); |
| 1191 | if (Res) |
| 1192 | SectionName = *Res; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1193 | |
| 1194 | W.printString("Name", SymbolName); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1195 | W.printNumber("Value", Symbol.getValue()); |
| 1196 | W.printNumber("Section", SectionName, Symbol.getSectionNumber()); |
| 1197 | W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType)); |
| 1198 | W.printEnum ("ComplexType", Symbol.getComplexType(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1199 | makeArrayRef(ImageSymDType)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1200 | W.printEnum ("StorageClass", Symbol.getStorageClass(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1201 | makeArrayRef(ImageSymClass)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1202 | W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1203 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1204 | for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) { |
| 1205 | if (Symbol.isFunctionDefinition()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1206 | const coff_aux_function_definition *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1207 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1208 | |
| 1209 | DictScope AS(W, "AuxFunctionDef"); |
| 1210 | W.printNumber("TagIndex", Aux->TagIndex); |
| 1211 | W.printNumber("TotalSize", Aux->TotalSize); |
David Majnemer | f3a2af5 | 2014-03-19 04:33:27 +0000 | [diff] [blame] | 1212 | W.printHex("PointerToLineNumber", Aux->PointerToLinenumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1213 | W.printHex("PointerToNextFunction", Aux->PointerToNextFunction); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1214 | |
David Majnemer | c7d7c6f | 2014-10-31 05:07:00 +0000 | [diff] [blame] | 1215 | } else if (Symbol.isAnyUndefined()) { |
David Majnemer | f3a2af5 | 2014-03-19 04:33:27 +0000 | [diff] [blame] | 1216 | const coff_aux_weak_external *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1217 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1218 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1219 | ErrorOr<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1220 | StringRef LinkedName; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1221 | std::error_code EC = Linked.getError(); |
| 1222 | if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1223 | LinkedName = ""; |
| 1224 | error(EC); |
| 1225 | } |
| 1226 | |
| 1227 | DictScope AS(W, "AuxWeakExternal"); |
| 1228 | W.printNumber("Linked", LinkedName, Aux->TagIndex); |
| 1229 | W.printEnum ("Search", Aux->Characteristics, |
| 1230 | makeArrayRef(WeakExternalCharacteristics)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1231 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1232 | } else if (Symbol.isFileRecord()) { |
| 1233 | const char *FileName; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1234 | error(getSymbolAuxData(Obj, Symbol, I, FileName)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1235 | |
Nico Rieck | 0ab8e60 | 2013-04-22 08:35:11 +0000 | [diff] [blame] | 1236 | DictScope AS(W, "AuxFileRecord"); |
Saleem Abdulrasool | d38c6b1 | 2014-04-14 02:37:23 +0000 | [diff] [blame] | 1237 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1238 | StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() * |
| 1239 | Obj->getSymbolTableEntrySize()); |
Saleem Abdulrasool | d38c6b1 | 2014-04-14 02:37:23 +0000 | [diff] [blame] | 1240 | W.printString("FileName", Name.rtrim(StringRef("\0", 1))); |
Saleem Abdulrasool | 3b5e001 | 2014-04-16 04:15:29 +0000 | [diff] [blame] | 1241 | break; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1242 | } else if (Symbol.isSectionDefinition()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1243 | const coff_aux_section_definition *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1244 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1245 | |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1246 | int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj()); |
| 1247 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1248 | DictScope AS(W, "AuxSectionDef"); |
| 1249 | W.printNumber("Length", Aux->Length); |
| 1250 | W.printNumber("RelocationCount", Aux->NumberOfRelocations); |
| 1251 | W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers); |
| 1252 | W.printHex("Checksum", Aux->CheckSum); |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1253 | W.printNumber("Number", AuxNumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1254 | W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1255 | |
Nico Rieck | a711dee | 2013-04-22 08:34:59 +0000 | [diff] [blame] | 1256 | if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1257 | && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { |
| 1258 | const coff_section *Assoc; |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1259 | StringRef AssocName = ""; |
| 1260 | std::error_code EC = Obj->getSection(AuxNumber, Assoc); |
| 1261 | ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc); |
| 1262 | if (Res) |
| 1263 | AssocName = *Res; |
| 1264 | if (!EC) |
| 1265 | EC = Res.getError(); |
| 1266 | if (EC) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1267 | AssocName = ""; |
| 1268 | error(EC); |
| 1269 | } |
| 1270 | |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1271 | W.printNumber("AssocSection", AssocName, AuxNumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1272 | } |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1273 | } else if (Symbol.isCLRToken()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1274 | const coff_aux_clr_token *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1275 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1276 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1277 | ErrorOr<COFFSymbolRef> ReferredSym = |
| 1278 | Obj->getSymbol(Aux->SymbolTableIndex); |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1279 | StringRef ReferredName; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1280 | std::error_code EC = ReferredSym.getError(); |
| 1281 | if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) { |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1282 | ReferredName = ""; |
| 1283 | error(EC); |
| 1284 | } |
| 1285 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1286 | DictScope AS(W, "AuxCLRToken"); |
| 1287 | W.printNumber("AuxType", Aux->AuxType); |
| 1288 | W.printNumber("Reserved", Aux->Reserved); |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1289 | W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1290 | |
| 1291 | } else { |
| 1292 | W.startLine() << "<unhandled auxiliary record>\n"; |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | void COFFDumper::printUnwindInfo() { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1298 | ListScope D(W, "UnwindInformation"); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1299 | switch (Obj->getMachine()) { |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1300 | case COFF::IMAGE_FILE_MACHINE_AMD64: { |
| 1301 | Win64EH::Dumper Dumper(W); |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1302 | Win64EH::Dumper::SymbolResolver |
| 1303 | Resolver = [](const object::coff_section *Section, uint64_t Offset, |
| 1304 | SymbolRef &Symbol, void *user_data) -> std::error_code { |
| 1305 | COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data); |
| 1306 | return Dumper->resolveSymbol(Section, Offset, Symbol); |
| 1307 | }; |
Saleem Abdulrasool | 65dbbb5 | 2014-05-25 21:37:59 +0000 | [diff] [blame] | 1308 | Win64EH::Dumper::Context Ctx(*Obj, Resolver, this); |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1309 | Dumper.printData(Ctx); |
| 1310 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1311 | } |
Saleem Abdulrasool | e6971ca | 2014-06-04 15:47:15 +0000 | [diff] [blame] | 1312 | case COFF::IMAGE_FILE_MACHINE_ARMNT: { |
| 1313 | ARM::WinEH::Decoder Decoder(W); |
| 1314 | Decoder.dumpProcedureData(*Obj); |
| 1315 | break; |
| 1316 | } |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1317 | default: |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1318 | W.printEnum("unsupported Image Machine", Obj->getMachine(), |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1319 | makeArrayRef(ImageFileMachineType)); |
| 1320 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1324 | void COFFDumper::printImportedSymbols( |
| 1325 | iterator_range<imported_symbol_iterator> Range) { |
| 1326 | for (const ImportedSymbolRef &I : Range) { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1327 | StringRef Sym; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1328 | error(I.getSymbolName(Sym)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1329 | uint16_t Ordinal; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1330 | error(I.getOrdinal(Ordinal)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1331 | W.printNumber("Symbol", Sym, Ordinal); |
| 1332 | } |
| 1333 | } |
| 1334 | |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1335 | void COFFDumper::printDelayImportedSymbols( |
| 1336 | const DelayImportDirectoryEntryRef &I, |
| 1337 | iterator_range<imported_symbol_iterator> Range) { |
| 1338 | int Index = 0; |
| 1339 | for (const ImportedSymbolRef &S : Range) { |
| 1340 | DictScope Import(W, "Import"); |
| 1341 | StringRef Sym; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1342 | error(S.getSymbolName(Sym)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1343 | uint16_t Ordinal; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1344 | error(S.getOrdinal(Ordinal)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1345 | W.printNumber("Symbol", Sym, Ordinal); |
| 1346 | uint64_t Addr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1347 | error(I.getImportAddress(Index++, Addr)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1348 | W.printHex("Address", Addr); |
| 1349 | } |
| 1350 | } |
| 1351 | |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1352 | void COFFDumper::printCOFFImports() { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1353 | // Regular imports |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1354 | for (const ImportDirectoryEntryRef &I : Obj->import_directories()) { |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1355 | DictScope Import(W, "Import"); |
| 1356 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1357 | error(I.getName(Name)); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1358 | W.printString("Name", Name); |
| 1359 | uint32_t Addr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1360 | error(I.getImportLookupTableRVA(Addr)); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1361 | W.printHex("ImportLookupTableRVA", Addr); |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1362 | error(I.getImportAddressTableRVA(Addr)); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1363 | W.printHex("ImportAddressTableRVA", Addr); |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1364 | printImportedSymbols(I.imported_symbols()); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | // Delay imports |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1368 | for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1369 | DictScope Import(W, "DelayImport"); |
| 1370 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1371 | error(I.getName(Name)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1372 | W.printString("Name", Name); |
Rui Ueyama | 1af0865 | 2014-10-03 18:07:18 +0000 | [diff] [blame] | 1373 | const delay_import_directory_table_entry *Table; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1374 | error(I.getDelayImportTable(Table)); |
Rui Ueyama | 1af0865 | 2014-10-03 18:07:18 +0000 | [diff] [blame] | 1375 | W.printHex("Attributes", Table->Attributes); |
| 1376 | W.printHex("ModuleHandle", Table->ModuleHandle); |
| 1377 | W.printHex("ImportAddressTable", Table->DelayImportAddressTable); |
| 1378 | W.printHex("ImportNameTable", Table->DelayImportNameTable); |
| 1379 | W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable); |
| 1380 | W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1381 | printDelayImportedSymbols(I, I.imported_symbols()); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1384 | |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 1385 | void COFFDumper::printCOFFExports() { |
| 1386 | for (const ExportDirectoryEntryRef &E : Obj->export_directories()) { |
| 1387 | DictScope Export(W, "Export"); |
| 1388 | |
| 1389 | StringRef Name; |
| 1390 | uint32_t Ordinal, RVA; |
| 1391 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1392 | error(E.getSymbolName(Name)); |
| 1393 | error(E.getOrdinal(Ordinal)); |
| 1394 | error(E.getExportRVA(RVA)); |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 1395 | |
| 1396 | W.printNumber("Ordinal", Ordinal); |
| 1397 | W.printString("Name", Name); |
| 1398 | W.printHex("RVA", RVA); |
| 1399 | } |
| 1400 | } |
| 1401 | |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1402 | void COFFDumper::printCOFFDirectives() { |
| 1403 | for (const SectionRef &Section : Obj->sections()) { |
| 1404 | StringRef Contents; |
| 1405 | StringRef Name; |
| 1406 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1407 | error(Section.getName(Name)); |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1408 | if (Name != ".drectve") |
| 1409 | continue; |
| 1410 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1411 | error(Section.getContents(Contents)); |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1412 | |
| 1413 | W.printString("Directive(s)", Contents); |
| 1414 | } |
| 1415 | } |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1416 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1417 | static StringRef getBaseRelocTypeName(uint8_t Type) { |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1418 | switch (Type) { |
| 1419 | case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE"; |
| 1420 | case COFF::IMAGE_REL_BASED_HIGH: return "HIGH"; |
| 1421 | case COFF::IMAGE_REL_BASED_LOW: return "LOW"; |
| 1422 | case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW"; |
| 1423 | case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ"; |
Saleem Abdulrasool | 5a41c37 | 2015-01-16 20:16:09 +0000 | [diff] [blame] | 1424 | case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)"; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1425 | case COFF::IMAGE_REL_BASED_DIR64: return "DIR64"; |
Colin LeMahieu | 9fbffee | 2014-11-19 17:10:39 +0000 | [diff] [blame] | 1426 | default: return "unknown (" + llvm::utostr(Type) + ")"; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | void COFFDumper::printCOFFBaseReloc() { |
| 1431 | ListScope D(W, "BaseReloc"); |
| 1432 | for (const BaseRelocRef &I : Obj->base_relocs()) { |
| 1433 | uint8_t Type; |
| 1434 | uint32_t RVA; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1435 | error(I.getRVA(RVA)); |
| 1436 | error(I.getType(Type)); |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1437 | DictScope Import(W, "Entry"); |
| 1438 | W.printString("Type", getBaseRelocTypeName(Type)); |
| 1439 | W.printHex("Address", RVA); |
| 1440 | } |
| 1441 | } |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 1442 | |
| 1443 | void COFFDumper::printStackMap() const { |
| 1444 | object::SectionRef StackMapSection; |
| 1445 | for (auto Sec : Obj->sections()) { |
| 1446 | StringRef Name; |
| 1447 | Sec.getName(Name); |
| 1448 | if (Name == ".llvm_stackmaps") { |
| 1449 | StackMapSection = Sec; |
| 1450 | break; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | if (StackMapSection == object::SectionRef()) |
| 1455 | return; |
| 1456 | |
| 1457 | StringRef StackMapContents; |
| 1458 | StackMapSection.getContents(StackMapContents); |
| 1459 | ArrayRef<uint8_t> StackMapContentsArray( |
| 1460 | reinterpret_cast<const uint8_t*>(StackMapContents.data()), |
| 1461 | StackMapContents.size()); |
| 1462 | |
| 1463 | if (Obj->isLittleEndian()) |
| 1464 | prettyPrintStackMap( |
| 1465 | llvm::outs(), |
| 1466 | StackMapV1Parser<support::little>(StackMapContentsArray)); |
| 1467 | else |
| 1468 | prettyPrintStackMap(llvm::outs(), |
| 1469 | StackMapV1Parser<support::big>(StackMapContentsArray)); |
| 1470 | } |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1471 | |
| 1472 | void llvm::dumpCodeViewMergedTypes( |
| 1473 | ScopedPrinter &Writer, llvm::codeview::MemoryTypeTableBuilder &CVTypes) { |
| 1474 | // Flatten it first, then run our dumper on it. |
| 1475 | ListScope S(Writer, "MergedTypeStream"); |
| 1476 | SmallString<0> Buf; |
| 1477 | CVTypes.ForEachRecord([&](TypeIndex TI, MemoryTypeTableBuilder::Record *R) { |
| 1478 | // The record data doesn't include the 16 bit size. |
| 1479 | Buf.push_back(R->size() & 0xff); |
| 1480 | Buf.push_back((R->size() >> 8) & 0xff); |
| 1481 | Buf.append(R->data(), R->data() + R->size()); |
| 1482 | }); |
| 1483 | CVTypeDumper CVTD(Writer, opts::CodeViewSubsectionBytes); |
| 1484 | ArrayRef<uint8_t> BinaryData(reinterpret_cast<const uint8_t *>(Buf.data()), |
| 1485 | Buf.size()); |
| 1486 | if (!CVTD.dump(BinaryData)) { |
| 1487 | Writer.flush(); |
| 1488 | error(object_error::parse_failed); |
| 1489 | } |
| 1490 | } |