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" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 16 | #include "Error.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 17 | #include "ObjDumper.h" |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 18 | #include "StackMapPrinter.h" |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 19 | #include "Win64EHDumper.h" |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 20 | #include "llvm-readobj.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/SmallString.h" |
Colin LeMahieu | 9fbffee | 2014-11-19 17:10:39 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 24 | #include "llvm/BinaryFormat/COFF.h" |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" |
| 30 | #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h" |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 31 | #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/CodeView/Line.h" |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 34 | #include "llvm/DebugInfo/CodeView/RecordSerialization.h" |
Zachary Turner | 0d84074 | 2016-10-07 21:34:46 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h" |
| 37 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 38 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 39 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 40 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 41 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 42 | #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 43 | #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 44 | #include "llvm/DebugInfo/CodeView/TypeTableCollection.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 45 | #include "llvm/Object/COFF.h" |
| 46 | #include "llvm/Object/ObjectFile.h" |
Zachary Turner | 05bd9f3 | 2017-04-28 23:41:36 +0000 | [diff] [blame] | 47 | #include "llvm/Support/BinaryStreamReader.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Casting.h" |
| 49 | #include "llvm/Support/Compiler.h" |
Zachary Turner | 0c60f26 | 2017-05-18 23:03:06 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ConvertUTF.h" |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 51 | #include "llvm/Support/DataExtractor.h" |
Zachary Turner | 05bd9f3 | 2017-04-28 23:41:36 +0000 | [diff] [blame] | 52 | #include "llvm/Support/FormatVariadic.h" |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Path.h" |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 54 | #include "llvm/Support/ScopedPrinter.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 55 | #include "llvm/Support/SourceMgr.h" |
| 56 | #include "llvm/Support/Win64EH.h" |
| 57 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 58 | #include <algorithm> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 59 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 60 | #include <system_error> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 61 | #include <time.h> |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 62 | |
| 63 | using namespace llvm; |
| 64 | using namespace llvm::object; |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 65 | using namespace llvm::codeview; |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 66 | using namespace llvm::support; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 67 | using namespace llvm::Win64EH; |
| 68 | |
| 69 | namespace { |
| 70 | |
Reid Kleckner | b7d716c | 2017-06-22 01:10:29 +0000 | [diff] [blame] | 71 | struct LoadConfigTables { |
| 72 | uint64_t SEHTableVA = 0; |
| 73 | uint64_t SEHTableCount = 0; |
| 74 | uint32_t GuardFlags = 0; |
| 75 | uint64_t GuardFidTableVA = 0; |
| 76 | uint64_t GuardFidTableCount = 0; |
| 77 | }; |
| 78 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 79 | class COFFDumper : public ObjDumper { |
| 80 | public: |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 81 | friend class COFFObjectDumpDelegate; |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 82 | COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer) |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 83 | : ObjDumper(Writer), Obj(Obj), Writer(Writer), Types(100) {} |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 84 | |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 85 | void printFileHeaders() override; |
| 86 | void printSections() override; |
| 87 | void printRelocations() override; |
| 88 | void printSymbols() override; |
| 89 | void printDynamicSymbols() override; |
| 90 | void printUnwindInfo() override; |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 91 | void printCOFFImports() override; |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 92 | void printCOFFExports() override; |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 93 | void printCOFFDirectives() override; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 94 | void printCOFFBaseReloc() override; |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 95 | void printCOFFDebugDirectory() override; |
Zachary Turner | 8d6396d | 2017-04-27 19:38:38 +0000 | [diff] [blame] | 96 | void printCOFFResources() override; |
Reid Kleckner | b7d716c | 2017-06-22 01:10:29 +0000 | [diff] [blame] | 97 | void printCOFFLoadConfig() override; |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 98 | void printCodeViewDebugInfo() override; |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 99 | void mergeCodeViewTypes(llvm::codeview::TypeTableBuilder &CVIDs, |
| 100 | llvm::codeview::TypeTableBuilder &CVTypes) override; |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 101 | void printStackMap() const override; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 102 | private: |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 103 | void printSymbol(const SymbolRef &Sym); |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 104 | void printRelocation(const SectionRef &Section, const RelocationRef &Reloc, |
| 105 | uint64_t Bias = 0); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 106 | void printDataDirectory(uint32_t Index, const std::string &FieldName); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 107 | |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 108 | void printDOSHeader(const dos_header *DH); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 109 | template <class PEHeader> void printPEHeader(const PEHeader *Hdr); |
| 110 | void printBaseOfDataField(const pe32_header *Hdr); |
| 111 | void printBaseOfDataField(const pe32plus_header *Hdr); |
Reid Kleckner | b7d716c | 2017-06-22 01:10:29 +0000 | [diff] [blame] | 112 | template <typename T> |
| 113 | void printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables); |
| 114 | typedef void (*PrintExtraCB)(raw_ostream &, const uint8_t *); |
| 115 | void printRVATable(uint64_t TableVA, uint64_t Count, uint64_t EntrySize, |
| 116 | PrintExtraCB PrintExtra = 0); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 117 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 118 | void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section); |
| 119 | void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 120 | StringRef getTypeName(TypeIndex Ty); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 121 | StringRef getFileNameForFileOffset(uint32_t FileOffset); |
| 122 | void printFileNameForOffset(StringRef Label, uint32_t FileOffset); |
Reid Kleckner | 5cb2b6b | 2016-05-02 20:30:47 +0000 | [diff] [blame] | 123 | void printTypeIndex(StringRef FieldName, TypeIndex TI) { |
| 124 | // Forward to CVTypeDumper for simplicity. |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 125 | codeview::printTypeIndex(Writer, FieldName, TI, Types); |
Reid Kleckner | 5cb2b6b | 2016-05-02 20:30:47 +0000 | [diff] [blame] | 126 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 127 | |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 128 | void printCodeViewSymbolsSubsection(StringRef Subsection, |
| 129 | const SectionRef &Section, |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 130 | StringRef SectionContents); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 131 | |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 132 | void printCodeViewFileChecksums(StringRef Subsection); |
| 133 | |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 134 | void printCodeViewInlineeLines(StringRef Subsection); |
| 135 | |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 136 | void printRelocatedField(StringRef Label, const coff_section *Sec, |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 137 | uint32_t RelocOffset, uint32_t Offset, |
| 138 | StringRef *RelocSym = nullptr); |
| 139 | |
Eric Beckmann | 1301759 | 2017-06-13 00:16:32 +0000 | [diff] [blame] | 140 | uint32_t countTotalTableEntries(ResourceSectionRef RSF, |
| 141 | const coff_resource_dir_table &Table, |
| 142 | StringRef Level); |
| 143 | |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 144 | void printResourceDirectoryTable(ResourceSectionRef RSF, |
| 145 | const coff_resource_dir_table &Table, |
| 146 | StringRef Level); |
| 147 | |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 148 | void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec, |
| 149 | StringRef SectionContents, StringRef Block); |
| 150 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 151 | /// Given a .debug$S section, find the string table and file checksum table. |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 152 | void initializeFileAndStringTables(BinaryStreamReader &Reader); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 153 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 154 | void cacheRelocations(); |
| 155 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 156 | std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset, |
| 157 | SymbolRef &Sym); |
| 158 | std::error_code resolveSymbolName(const coff_section *Section, |
| 159 | uint64_t Offset, StringRef &Name); |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 160 | std::error_code resolveSymbolName(const coff_section *Section, |
| 161 | StringRef SectionContents, |
| 162 | const void *RelocPtr, StringRef &Name); |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 163 | void printImportedSymbols(iterator_range<imported_symbol_iterator> Range); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 164 | void printDelayImportedSymbols( |
| 165 | const DelayImportDirectoryEntryRef &I, |
| 166 | iterator_range<imported_symbol_iterator> Range); |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 167 | ErrorOr<const coff_resource_dir_entry &> |
| 168 | getResourceDirectoryTableEntry(const coff_resource_dir_table &Table, |
| 169 | uint32_t Index); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 170 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 171 | typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy; |
| 172 | |
| 173 | const llvm::object::COFFObjectFile *Obj; |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 174 | bool RelocCached = false; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 175 | RelocMapTy RelocMap; |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 176 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 177 | DebugChecksumsSubsectionRef CVFileChecksumTable; |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 178 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 179 | DebugStringTableSubsectionRef CVStringTable; |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 180 | |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 181 | ScopedPrinter &Writer; |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 182 | BinaryByteStream TypeContents; |
| 183 | LazyRandomTypeCollection Types; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 186 | class COFFObjectDumpDelegate : public SymbolDumpDelegate { |
| 187 | public: |
| 188 | COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR, |
| 189 | const COFFObjectFile *Obj, StringRef SectionContents) |
Zachary Turner | 537014c | 2016-05-24 03:32:34 +0000 | [diff] [blame] | 190 | : CD(CD), SR(SR), SectionContents(SectionContents) { |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 191 | Sec = Obj->getCOFFSection(SR); |
| 192 | } |
| 193 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 194 | uint32_t getRecordOffset(BinaryStreamReader Reader) override { |
Zachary Turner | 4d49eb9 | 2016-10-20 18:31:19 +0000 | [diff] [blame] | 195 | ArrayRef<uint8_t> Data; |
| 196 | if (auto EC = Reader.readLongestContiguousChunk(Data)) { |
| 197 | llvm::consumeError(std::move(EC)); |
| 198 | return 0; |
| 199 | } |
| 200 | return Data.data() - SectionContents.bytes_begin(); |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void printRelocatedField(StringRef Label, uint32_t RelocOffset, |
| 204 | uint32_t Offset, StringRef *RelocSym) override { |
| 205 | CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym); |
| 206 | } |
| 207 | |
| 208 | void printBinaryBlockWithRelocs(StringRef Label, |
| 209 | ArrayRef<uint8_t> Block) override { |
| 210 | StringRef SBlock(reinterpret_cast<const char *>(Block.data()), |
| 211 | Block.size()); |
| 212 | if (opts::CodeViewSubsectionBytes) |
| 213 | CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock); |
| 214 | } |
| 215 | |
| 216 | StringRef getFileNameForFileOffset(uint32_t FileOffset) override { |
| 217 | return CD.getFileNameForFileOffset(FileOffset); |
| 218 | } |
| 219 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 220 | DebugStringTableSubsectionRef getStringTable() override { |
| 221 | return CD.CVStringTable; |
| 222 | } |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 223 | |
| 224 | private: |
| 225 | COFFDumper &CD; |
| 226 | const SectionRef &SR; |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 227 | const coff_section *Sec; |
| 228 | StringRef SectionContents; |
| 229 | }; |
| 230 | |
Reid Kleckner | 4a14bca | 2016-05-05 00:34:33 +0000 | [diff] [blame] | 231 | } // end namespace |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 232 | |
| 233 | namespace llvm { |
| 234 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 235 | std::error_code createCOFFDumper(const object::ObjectFile *Obj, |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 236 | ScopedPrinter &Writer, |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 237 | std::unique_ptr<ObjDumper> &Result) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 238 | const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj); |
| 239 | if (!COFFObj) |
| 240 | return readobj_error::unsupported_obj_file_format; |
| 241 | |
| 242 | Result.reset(new COFFDumper(COFFObj, Writer)); |
| 243 | return readobj_error::success; |
| 244 | } |
| 245 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 246 | } // namespace llvm |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 247 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 248 | // Given a a section and an offset into this section the function returns the |
| 249 | // symbol used for the relocation at the offset. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 250 | std::error_code COFFDumper::resolveSymbol(const coff_section *Section, |
| 251 | uint64_t Offset, SymbolRef &Sym) { |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 252 | cacheRelocations(); |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 253 | const auto &Relocations = RelocMap[Section]; |
David Majnemer | 61f1798 | 2016-05-29 06:18:08 +0000 | [diff] [blame] | 254 | auto SymI = Obj->symbol_end(); |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 255 | for (const auto &Relocation : Relocations) { |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 256 | uint64_t RelocationOffset = Relocation.getOffset(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 257 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 258 | if (RelocationOffset == Offset) { |
David Majnemer | 61f1798 | 2016-05-29 06:18:08 +0000 | [diff] [blame] | 259 | SymI = Relocation.getSymbol(); |
| 260 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
David Majnemer | 61f1798 | 2016-05-29 06:18:08 +0000 | [diff] [blame] | 263 | if (SymI == Obj->symbol_end()) |
| 264 | return readobj_error::unknown_symbol; |
| 265 | Sym = *SymI; |
| 266 | return readobj_error::success; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 269 | // Given a section and an offset into this section the function returns the name |
| 270 | // of the symbol used for the relocation at the offset. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 271 | std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, |
| 272 | uint64_t Offset, |
| 273 | StringRef &Name) { |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 274 | SymbolRef Symbol; |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 275 | if (std::error_code EC = resolveSymbol(Section, Offset, Symbol)) |
Saleem Abdulrasool | 5dd27f4 | 2014-05-25 20:26:37 +0000 | [diff] [blame] | 276 | return EC; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 277 | Expected<StringRef> NameOrErr = Symbol.getName(); |
| 278 | if (!NameOrErr) |
| 279 | return errorToErrorCode(NameOrErr.takeError()); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 280 | Name = *NameOrErr; |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 281 | return std::error_code(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 284 | // Helper for when you have a pointer to real data and you want to know about |
| 285 | // relocations against it. |
| 286 | std::error_code COFFDumper::resolveSymbolName(const coff_section *Section, |
| 287 | StringRef SectionContents, |
| 288 | const void *RelocPtr, |
| 289 | StringRef &Name) { |
| 290 | assert(SectionContents.data() < RelocPtr && |
| 291 | RelocPtr < SectionContents.data() + SectionContents.size() && |
| 292 | "pointer to relocated object is not in section"); |
| 293 | uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) - |
| 294 | SectionContents.data()); |
| 295 | return resolveSymbolName(Section, Offset, Name); |
| 296 | } |
| 297 | |
| 298 | void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec, |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 299 | uint32_t RelocOffset, uint32_t Offset, |
| 300 | StringRef *RelocSym) { |
| 301 | StringRef SymStorage; |
| 302 | StringRef &Symbol = RelocSym ? *RelocSym : SymStorage; |
| 303 | if (!resolveSymbolName(Sec, RelocOffset, Symbol)) |
| 304 | W.printSymbolOffset(Label, Symbol, Offset); |
| 305 | else |
| 306 | W.printHex(Label, RelocOffset); |
| 307 | } |
| 308 | |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 309 | void COFFDumper::printBinaryBlockWithRelocs(StringRef Label, |
| 310 | const SectionRef &Sec, |
| 311 | StringRef SectionContents, |
| 312 | StringRef Block) { |
| 313 | W.printBinaryBlock(Label, Block); |
| 314 | |
| 315 | assert(SectionContents.begin() < Block.begin() && |
| 316 | SectionContents.end() >= Block.end() && |
| 317 | "Block is not contained in SectionContents"); |
| 318 | uint64_t OffsetStart = Block.data() - SectionContents.data(); |
| 319 | uint64_t OffsetEnd = OffsetStart + Block.size(); |
| 320 | |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 321 | W.flush(); |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 322 | cacheRelocations(); |
| 323 | ListScope D(W, "BlockRelocations"); |
| 324 | const coff_section *Section = Obj->getCOFFSection(Sec); |
| 325 | const auto &Relocations = RelocMap[Section]; |
| 326 | for (const auto &Relocation : Relocations) { |
| 327 | uint64_t RelocationOffset = Relocation.getOffset(); |
| 328 | if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd) |
| 329 | printRelocation(Sec, Relocation, OffsetStart); |
| 330 | } |
| 331 | } |
| 332 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 333 | static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 334 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ), |
| 335 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ), |
| 336 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ), |
| 337 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ), |
Martin Storsjo | 43c8545 | 2017-06-30 07:02:13 +0000 | [diff] [blame] | 338 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM64 ), |
Saleem Abdulrasool | 5e1780e | 2014-03-11 03:08:37 +0000 | [diff] [blame] | 339 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 340 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ), |
| 341 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ), |
| 342 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ), |
| 343 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ), |
| 344 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ), |
| 345 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ), |
| 346 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16), |
| 347 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ), |
| 348 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP), |
| 349 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ), |
| 350 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ), |
| 351 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ), |
| 352 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ), |
| 353 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ), |
| 354 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ), |
| 355 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2) |
| 356 | }; |
| 357 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 358 | static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 359 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ), |
| 360 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ), |
| 361 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ), |
| 362 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ), |
| 363 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ), |
| 364 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ), |
| 365 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ), |
| 366 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ), |
| 367 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ), |
| 368 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP), |
| 369 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ), |
| 370 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ), |
| 371 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ), |
| 372 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ), |
| 373 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI ) |
| 374 | }; |
| 375 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 376 | static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = { |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 377 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ), |
| 378 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ), |
| 379 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ), |
| 380 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ), |
| 381 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ), |
| 382 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ), |
| 383 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ), |
| 384 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER), |
| 385 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ), |
| 386 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ), |
| 387 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ), |
| 388 | }; |
| 389 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 390 | static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = { |
Rui Ueyama | 06dc5e7 | 2014-01-27 04:22:24 +0000 | [diff] [blame] | 391 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 392 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ), |
| 393 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ), |
| 394 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ), |
| 395 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ), |
| 396 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ), |
| 397 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ), |
Saleem Abdulrasool | d90f86d | 2014-06-27 03:11:18 +0000 | [diff] [blame] | 398 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 399 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ), |
Saleem Abdulrasool | d90f86d | 2014-06-27 03:11:18 +0000 | [diff] [blame] | 400 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ), |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 401 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE), |
| 402 | }; |
| 403 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 404 | static const EnumEntry<COFF::SectionCharacteristics> |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 405 | ImageSectionCharacteristics[] = { |
David Majnemer | d323888 | 2015-07-30 16:47:56 +0000 | [diff] [blame] | 406 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD ), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 407 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ), |
| 408 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ), |
| 409 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ), |
| 410 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA), |
| 411 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ), |
| 412 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ), |
| 413 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ), |
| 414 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ), |
| 415 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ), |
| 416 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ), |
| 417 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ), |
| 418 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ), |
| 419 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ), |
| 420 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ), |
| 421 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ), |
| 422 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ), |
| 423 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ), |
| 424 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ), |
| 425 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ), |
| 426 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ), |
| 427 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ), |
| 428 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ), |
| 429 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ), |
| 430 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ), |
| 431 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ), |
| 432 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ), |
| 433 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ), |
| 434 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ), |
| 435 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ), |
| 436 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ), |
| 437 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ), |
| 438 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ), |
| 439 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ), |
| 440 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ), |
| 441 | LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE ) |
| 442 | }; |
| 443 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 444 | static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 445 | { "Null" , COFF::IMAGE_SYM_TYPE_NULL }, |
| 446 | { "Void" , COFF::IMAGE_SYM_TYPE_VOID }, |
| 447 | { "Char" , COFF::IMAGE_SYM_TYPE_CHAR }, |
| 448 | { "Short" , COFF::IMAGE_SYM_TYPE_SHORT }, |
| 449 | { "Int" , COFF::IMAGE_SYM_TYPE_INT }, |
| 450 | { "Long" , COFF::IMAGE_SYM_TYPE_LONG }, |
| 451 | { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT }, |
| 452 | { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE }, |
| 453 | { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT }, |
| 454 | { "Union" , COFF::IMAGE_SYM_TYPE_UNION }, |
| 455 | { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM }, |
| 456 | { "MOE" , COFF::IMAGE_SYM_TYPE_MOE }, |
| 457 | { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE }, |
| 458 | { "Word" , COFF::IMAGE_SYM_TYPE_WORD }, |
| 459 | { "UInt" , COFF::IMAGE_SYM_TYPE_UINT }, |
| 460 | { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD } |
| 461 | }; |
| 462 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 463 | static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 464 | { "Null" , COFF::IMAGE_SYM_DTYPE_NULL }, |
| 465 | { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER }, |
| 466 | { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION }, |
| 467 | { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY } |
| 468 | }; |
| 469 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 470 | static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 471 | { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION }, |
| 472 | { "Null" , COFF::IMAGE_SYM_CLASS_NULL }, |
| 473 | { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC }, |
| 474 | { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL }, |
| 475 | { "Static" , COFF::IMAGE_SYM_CLASS_STATIC }, |
| 476 | { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER }, |
| 477 | { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF }, |
| 478 | { "Label" , COFF::IMAGE_SYM_CLASS_LABEL }, |
| 479 | { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL }, |
| 480 | { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT }, |
| 481 | { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT }, |
| 482 | { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG }, |
| 483 | { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION }, |
| 484 | { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG }, |
| 485 | { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION }, |
| 486 | { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC }, |
| 487 | { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG }, |
| 488 | { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM }, |
| 489 | { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM }, |
| 490 | { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD }, |
| 491 | { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK }, |
| 492 | { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION }, |
| 493 | { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT }, |
| 494 | { "File" , COFF::IMAGE_SYM_CLASS_FILE }, |
| 495 | { "Section" , COFF::IMAGE_SYM_CLASS_SECTION }, |
| 496 | { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL }, |
| 497 | { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN } |
| 498 | }; |
| 499 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 500 | static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 501 | { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES }, |
| 502 | { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY }, |
| 503 | { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE }, |
| 504 | { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH }, |
| 505 | { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE }, |
| 506 | { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST }, |
| 507 | { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST } |
| 508 | }; |
| 509 | |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 510 | static const EnumEntry<COFF::DebugType> ImageDebugType[] = { |
| 511 | { "Unknown" , COFF::IMAGE_DEBUG_TYPE_UNKNOWN }, |
| 512 | { "COFF" , COFF::IMAGE_DEBUG_TYPE_COFF }, |
| 513 | { "CodeView" , COFF::IMAGE_DEBUG_TYPE_CODEVIEW }, |
| 514 | { "FPO" , COFF::IMAGE_DEBUG_TYPE_FPO }, |
| 515 | { "Misc" , COFF::IMAGE_DEBUG_TYPE_MISC }, |
| 516 | { "Exception" , COFF::IMAGE_DEBUG_TYPE_EXCEPTION }, |
| 517 | { "Fixup" , COFF::IMAGE_DEBUG_TYPE_FIXUP }, |
| 518 | { "OmapToSrc" , COFF::IMAGE_DEBUG_TYPE_OMAP_TO_SRC }, |
| 519 | { "OmapFromSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC }, |
| 520 | { "Borland" , COFF::IMAGE_DEBUG_TYPE_BORLAND }, |
David Majnemer | a2a2a73 | 2016-06-02 17:32:11 +0000 | [diff] [blame] | 521 | { "Reserved10" , COFF::IMAGE_DEBUG_TYPE_RESERVED10 }, |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 522 | { "CLSID" , COFF::IMAGE_DEBUG_TYPE_CLSID }, |
| 523 | { "VCFeature" , COFF::IMAGE_DEBUG_TYPE_VC_FEATURE }, |
| 524 | { "POGO" , COFF::IMAGE_DEBUG_TYPE_POGO }, |
| 525 | { "ILTCG" , COFF::IMAGE_DEBUG_TYPE_ILTCG }, |
| 526 | { "MPX" , COFF::IMAGE_DEBUG_TYPE_MPX }, |
David Majnemer | a2a2a73 | 2016-06-02 17:32:11 +0000 | [diff] [blame] | 527 | { "Repro" , COFF::IMAGE_DEBUG_TYPE_REPRO }, |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 528 | }; |
| 529 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 530 | static const EnumEntry<COFF::WeakExternalCharacteristics> |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 531 | WeakExternalCharacteristics[] = { |
| 532 | { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY }, |
| 533 | { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY }, |
| 534 | { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS } |
| 535 | }; |
| 536 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 537 | static const EnumEntry<uint32_t> SubSectionTypes[] = { |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 538 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Symbols), |
| 539 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Lines), |
| 540 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, StringTable), |
| 541 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FileChecksums), |
| 542 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FrameData), |
| 543 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, InlineeLines), |
| 544 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeImports), |
| 545 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeExports), |
| 546 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, ILLines), |
| 547 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FuncMDTokenMap), |
| 548 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, TypeMDTokenMap), |
| 549 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, MergedAssemblyInput), |
| 550 | LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CoffSymbolRVA), |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 551 | }; |
| 552 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 553 | static const EnumEntry<uint32_t> FrameDataFlags[] = { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 554 | LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH), |
| 555 | LLVM_READOBJ_ENUM_ENT(FrameData, HasEH), |
| 556 | LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart), |
| 557 | }; |
| 558 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 559 | static const EnumEntry<uint8_t> FileChecksumKindNames[] = { |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 560 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None), |
| 561 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5), |
| 562 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1), |
| 563 | LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256), |
| 564 | }; |
| 565 | |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 566 | static const EnumEntry<COFF::ResourceTypeID> ResourceTypeNames[]{ |
| 567 | {"kRT_CURSOR (ID 1)", COFF::RID_Cursor}, |
| 568 | {"kRT_BITMAP (ID 2)", COFF::RID_Bitmap}, |
| 569 | {"kRT_ICON (ID 3)", COFF::RID_Icon}, |
| 570 | {"kRT_MENU (ID 4)", COFF::RID_Menu}, |
| 571 | {"kRT_DIALOG (ID 5)", COFF::RID_Dialog}, |
| 572 | {"kRT_STRING (ID 6)", COFF::RID_String}, |
| 573 | {"kRT_FONTDIR (ID 7)", COFF::RID_FontDir}, |
| 574 | {"kRT_FONT (ID 8)", COFF::RID_Font}, |
| 575 | {"kRT_ACCELERATOR (ID 9)", COFF::RID_Accelerator}, |
| 576 | {"kRT_RCDATA (ID 10)", COFF::RID_RCData}, |
| 577 | {"kRT_MESSAGETABLE (ID 11)", COFF::RID_MessageTable}, |
| 578 | {"kRT_GROUP_CURSOR (ID 12)", COFF::RID_Group_Cursor}, |
| 579 | {"kRT_GROUP_ICON (ID 14)", COFF::RID_Group_Icon}, |
| 580 | {"kRT_VERSION (ID 16)", COFF::RID_Version}, |
| 581 | {"kRT_DLGINCLUDE (ID 17)", COFF::RID_DLGInclude}, |
| 582 | {"kRT_PLUGPLAY (ID 19)", COFF::RID_PlugPlay}, |
| 583 | {"kRT_VXD (ID 20)", COFF::RID_VXD}, |
| 584 | {"kRT_ANICURSOR (ID 21)", COFF::RID_AniCursor}, |
| 585 | {"kRT_ANIICON (ID 22)", COFF::RID_AniIcon}, |
| 586 | {"kRT_HTML (ID 23)", COFF::RID_HTML}, |
| 587 | {"kRT_MANIFEST (ID 24)", COFF::RID_Manifest}}; |
| 588 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 589 | template <typename T> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 590 | static std::error_code getSymbolAuxData(const COFFObjectFile *Obj, |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 591 | COFFSymbolRef Symbol, |
| 592 | uint8_t AuxSymbolIdx, const T *&Aux) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 593 | ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 594 | AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 595 | Aux = reinterpret_cast<const T*>(AuxData.data()); |
| 596 | return readobj_error::success; |
| 597 | } |
| 598 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 599 | void COFFDumper::cacheRelocations() { |
Rafael Espindola | 76d650e | 2015-07-06 14:26:07 +0000 | [diff] [blame] | 600 | if (RelocCached) |
| 601 | return; |
| 602 | RelocCached = true; |
| 603 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 604 | for (const SectionRef &S : Obj->sections()) { |
| 605 | const coff_section *Section = Obj->getCOFFSection(S); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 606 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 607 | for (const RelocationRef &Reloc : S.relocations()) |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 608 | RelocMap[Section].push_back(Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 609 | |
| 610 | // Sort relocations by address. |
| 611 | std::sort(RelocMap[Section].begin(), RelocMap[Section].end(), |
| 612 | relocAddressLess); |
| 613 | } |
| 614 | } |
| 615 | |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 616 | void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) { |
| 617 | const data_directory *Data; |
| 618 | if (Obj->getDataDirectory(Index, Data)) |
| 619 | return; |
| 620 | W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress); |
| 621 | W.printHex(FieldName + "Size", Data->Size); |
| 622 | } |
| 623 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 624 | void COFFDumper::printFileHeaders() { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 625 | time_t TDS = Obj->getTimeDateStamp(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 626 | char FormattedTime[20] = { }; |
| 627 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); |
| 628 | |
| 629 | { |
| 630 | DictScope D(W, "ImageFileHeader"); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 631 | W.printEnum ("Machine", Obj->getMachine(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 632 | makeArrayRef(ImageFileMachineType)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 633 | W.printNumber("SectionCount", Obj->getNumberOfSections()); |
| 634 | W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp()); |
| 635 | W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable()); |
| 636 | W.printNumber("SymbolCount", Obj->getNumberOfSymbols()); |
| 637 | W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader()); |
| 638 | W.printFlags ("Characteristics", Obj->getCharacteristics(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 639 | makeArrayRef(ImageFileCharacteristics)); |
| 640 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 641 | |
| 642 | // Print PE header. This header does not exist if this is an object file and |
| 643 | // not an executable. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 644 | const pe32_header *PEHeader = nullptr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 645 | error(Obj->getPE32Header(PEHeader)); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 646 | if (PEHeader) |
| 647 | printPEHeader<pe32_header>(PEHeader); |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 648 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 649 | const pe32plus_header *PEPlusHeader = nullptr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 650 | error(Obj->getPE32PlusHeader(PEPlusHeader)); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 651 | if (PEPlusHeader) |
| 652 | printPEHeader<pe32plus_header>(PEPlusHeader); |
David Majnemer | 5026722 | 2014-11-05 06:24:35 +0000 | [diff] [blame] | 653 | |
| 654 | if (const dos_header *DH = Obj->getDOSHeader()) |
| 655 | printDOSHeader(DH); |
| 656 | } |
| 657 | |
| 658 | void COFFDumper::printDOSHeader(const dos_header *DH) { |
| 659 | DictScope D(W, "DOSHeader"); |
| 660 | W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic))); |
| 661 | W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage); |
| 662 | W.printNumber("FileSizeInPages", DH->FileSizeInPages); |
| 663 | W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems); |
| 664 | W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs); |
| 665 | W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs); |
| 666 | W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs); |
| 667 | W.printNumber("InitialRelativeSS", DH->InitialRelativeSS); |
| 668 | W.printNumber("InitialSP", DH->InitialSP); |
| 669 | W.printNumber("Checksum", DH->Checksum); |
| 670 | W.printNumber("InitialIP", DH->InitialIP); |
| 671 | W.printNumber("InitialRelativeCS", DH->InitialRelativeCS); |
| 672 | W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable); |
| 673 | W.printNumber("OverlayNumber", DH->OverlayNumber); |
| 674 | W.printNumber("OEMid", DH->OEMid); |
| 675 | W.printNumber("OEMinfo", DH->OEMinfo); |
| 676 | W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 677 | } |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 678 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 679 | template <class PEHeader> |
| 680 | void COFFDumper::printPEHeader(const PEHeader *Hdr) { |
| 681 | DictScope D(W, "ImageOptionalHeader"); |
Martin Storsjo | 8ae07ac | 2017-06-30 07:02:04 +0000 | [diff] [blame] | 682 | W.printHex ("Magic", Hdr->Magic); |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 683 | W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion); |
| 684 | W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion); |
| 685 | W.printNumber("SizeOfCode", Hdr->SizeOfCode); |
| 686 | W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData); |
| 687 | W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData); |
| 688 | W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint); |
| 689 | W.printHex ("BaseOfCode", Hdr->BaseOfCode); |
| 690 | printBaseOfDataField(Hdr); |
| 691 | W.printHex ("ImageBase", Hdr->ImageBase); |
| 692 | W.printNumber("SectionAlignment", Hdr->SectionAlignment); |
| 693 | W.printNumber("FileAlignment", Hdr->FileAlignment); |
| 694 | W.printNumber("MajorOperatingSystemVersion", |
| 695 | Hdr->MajorOperatingSystemVersion); |
| 696 | W.printNumber("MinorOperatingSystemVersion", |
| 697 | Hdr->MinorOperatingSystemVersion); |
| 698 | W.printNumber("MajorImageVersion", Hdr->MajorImageVersion); |
| 699 | W.printNumber("MinorImageVersion", Hdr->MinorImageVersion); |
| 700 | W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion); |
| 701 | W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion); |
| 702 | W.printNumber("SizeOfImage", Hdr->SizeOfImage); |
| 703 | W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders); |
| 704 | W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem)); |
David Majnemer | 774aadf | 2014-11-18 02:45:28 +0000 | [diff] [blame] | 705 | W.printFlags ("Characteristics", Hdr->DLLCharacteristics, |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 706 | makeArrayRef(PEDLLCharacteristics)); |
| 707 | W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve); |
| 708 | W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit); |
| 709 | W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve); |
| 710 | W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit); |
| 711 | W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 712 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 713 | if (Hdr->NumberOfRvaAndSize > 0) { |
| 714 | DictScope D(W, "DataDirectory"); |
| 715 | static const char * const directory[] = { |
| 716 | "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable", |
| 717 | "CertificateTable", "BaseRelocationTable", "Debug", "Architecture", |
| 718 | "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT", |
| 719 | "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved" |
| 720 | }; |
| 721 | |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 722 | for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 723 | printDataDirectory(i, directory[i]); |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
| 727 | void COFFDumper::printCOFFDebugDirectory() { |
| 728 | ListScope LS(W, "DebugDirectory"); |
| 729 | for (const debug_directory &D : Obj->debug_directories()) { |
| 730 | char FormattedTime[20] = {}; |
| 731 | time_t TDS = D.TimeDateStamp; |
| 732 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); |
| 733 | DictScope S(W, "DebugEntry"); |
| 734 | W.printHex("Characteristics", D.Characteristics); |
| 735 | W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp); |
| 736 | W.printHex("MajorVersion", D.MajorVersion); |
| 737 | W.printHex("MinorVersion", D.MinorVersion); |
| 738 | W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType)); |
| 739 | W.printHex("SizeOfData", D.SizeOfData); |
| 740 | W.printHex("AddressOfRawData", D.AddressOfRawData); |
| 741 | W.printHex("PointerToRawData", D.PointerToRawData); |
| 742 | if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) { |
Saleem Abdulrasool | 0152802 | 2016-08-09 00:25:12 +0000 | [diff] [blame] | 743 | const codeview::DebugInfo *DebugInfo; |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 744 | StringRef PDBFileName; |
Saleem Abdulrasool | 0152802 | 2016-08-09 00:25:12 +0000 | [diff] [blame] | 745 | error(Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName)); |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 746 | DictScope PDBScope(W, "PDBInfo"); |
Saleem Abdulrasool | 0152802 | 2016-08-09 00:25:12 +0000 | [diff] [blame] | 747 | W.printHex("PDBSignature", DebugInfo->Signature.CVSignature); |
| 748 | if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) { |
| 749 | W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature)); |
| 750 | W.printNumber("PDBAge", DebugInfo->PDB70.Age); |
| 751 | W.printString("PDBFileName", PDBFileName); |
| 752 | } |
Reid Kleckner | 2da433e | 2016-06-02 17:10:43 +0000 | [diff] [blame] | 753 | } else { |
| 754 | // FIXME: Type values of 12 and 13 are commonly observed but are not in |
| 755 | // the documented type enum. Figure out what they mean. |
| 756 | ArrayRef<uint8_t> RawData; |
| 757 | error( |
| 758 | Obj->getRvaAndSizeAsBytes(D.AddressOfRawData, D.SizeOfData, RawData)); |
| 759 | W.printBinaryBlock("RawData", RawData); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 760 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 761 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Reid Kleckner | b7d716c | 2017-06-22 01:10:29 +0000 | [diff] [blame] | 764 | void COFFDumper::printRVATable(uint64_t TableVA, uint64_t Count, |
| 765 | uint64_t EntrySize, PrintExtraCB PrintExtra) { |
| 766 | uintptr_t TableStart, TableEnd; |
| 767 | error(Obj->getVaPtr(TableVA, TableStart)); |
Reid Kleckner | 45cb4fe | 2017-06-23 22:12:11 +0000 | [diff] [blame] | 768 | error(Obj->getVaPtr(TableVA + Count * EntrySize - 1, TableEnd)); |
| 769 | TableEnd++; |
Reid Kleckner | b7d716c | 2017-06-22 01:10:29 +0000 | [diff] [blame] | 770 | for (uintptr_t I = TableStart; I < TableEnd; I += EntrySize) { |
| 771 | uint32_t RVA = *reinterpret_cast<const ulittle32_t *>(I); |
| 772 | raw_ostream &OS = W.startLine(); |
| 773 | OS << "0x" << utohexstr(Obj->getImageBase() + RVA); |
| 774 | if (PrintExtra) |
| 775 | PrintExtra(OS, reinterpret_cast<const uint8_t *>(I)); |
| 776 | OS << '\n'; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | void COFFDumper::printCOFFLoadConfig() { |
| 781 | LoadConfigTables Tables; |
| 782 | if (Obj->is64()) |
| 783 | printCOFFLoadConfig(Obj->getLoadConfig64(), Tables); |
| 784 | else |
| 785 | printCOFFLoadConfig(Obj->getLoadConfig32(), Tables); |
| 786 | |
| 787 | if (Tables.SEHTableVA) { |
| 788 | ListScope LS(W, "SEHTable"); |
| 789 | printRVATable(Tables.SEHTableVA, Tables.SEHTableCount, 4); |
| 790 | } |
| 791 | |
| 792 | if (Tables.GuardFidTableVA) { |
| 793 | ListScope LS(W, "GuardFidTable"); |
| 794 | if (Tables.GuardFlags & uint32_t(coff_guard_flags::FidTableHasFlags)) { |
| 795 | auto PrintGuardFlags = [](raw_ostream &OS, const uint8_t *Entry) { |
| 796 | uint8_t Flags = *reinterpret_cast<const uint8_t *>(Entry + 4); |
| 797 | if (Flags) |
| 798 | OS << " flags " << utohexstr(Flags); |
| 799 | }; |
| 800 | printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 5, |
| 801 | PrintGuardFlags); |
| 802 | } else { |
| 803 | printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 4); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | template <typename T> |
| 809 | void COFFDumper::printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables) { |
Reid Kleckner | 45cb4fe | 2017-06-23 22:12:11 +0000 | [diff] [blame] | 810 | if (!Conf) |
| 811 | return; |
| 812 | |
Reid Kleckner | b7d716c | 2017-06-22 01:10:29 +0000 | [diff] [blame] | 813 | ListScope LS(W, "LoadConfig"); |
| 814 | char FormattedTime[20] = {}; |
| 815 | time_t TDS = Conf->TimeDateStamp; |
| 816 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); |
| 817 | W.printHex("Size", Conf->Size); |
| 818 | |
| 819 | // Print everything before SecurityCookie. The vast majority of images today |
| 820 | // have all these fields. |
| 821 | if (Conf->Size < offsetof(T, SEHandlerTable)) |
| 822 | return; |
| 823 | W.printHex("TimeDateStamp", FormattedTime, TDS); |
| 824 | W.printHex("MajorVersion", Conf->MajorVersion); |
| 825 | W.printHex("MinorVersion", Conf->MinorVersion); |
| 826 | W.printHex("GlobalFlagsClear", Conf->GlobalFlagsClear); |
| 827 | W.printHex("GlobalFlagsSet", Conf->GlobalFlagsSet); |
| 828 | W.printHex("CriticalSectionDefaultTimeout", |
| 829 | Conf->CriticalSectionDefaultTimeout); |
| 830 | W.printHex("DeCommitFreeBlockThreshold", Conf->DeCommitFreeBlockThreshold); |
| 831 | W.printHex("DeCommitTotalFreeThreshold", Conf->DeCommitTotalFreeThreshold); |
| 832 | W.printHex("LockPrefixTable", Conf->LockPrefixTable); |
| 833 | W.printHex("MaximumAllocationSize", Conf->MaximumAllocationSize); |
| 834 | W.printHex("VirtualMemoryThreshold", Conf->VirtualMemoryThreshold); |
| 835 | W.printHex("ProcessHeapFlags", Conf->ProcessHeapFlags); |
| 836 | W.printHex("ProcessAffinityMask", Conf->ProcessAffinityMask); |
| 837 | W.printHex("CSDVersion", Conf->CSDVersion); |
| 838 | W.printHex("DependentLoadFlags", Conf->DependentLoadFlags); |
| 839 | W.printHex("EditList", Conf->EditList); |
| 840 | W.printHex("SecurityCookie", Conf->SecurityCookie); |
| 841 | |
| 842 | // Print the safe SEH table if present. |
| 843 | if (Conf->Size < offsetof(coff_load_configuration32, GuardCFCheckFunction)) |
| 844 | return; |
| 845 | W.printHex("SEHandlerTable", Conf->SEHandlerTable); |
| 846 | W.printNumber("SEHandlerCount", Conf->SEHandlerCount); |
| 847 | |
| 848 | Tables.SEHTableVA = Conf->SEHandlerTable; |
| 849 | Tables.SEHTableCount = Conf->SEHandlerCount; |
| 850 | |
| 851 | // Print everything before CodeIntegrity. (2015) |
| 852 | if (Conf->Size < offsetof(T, CodeIntegrity)) |
| 853 | return; |
| 854 | W.printHex("GuardCFCheckFunction", Conf->GuardCFCheckFunction); |
| 855 | W.printHex("GuardCFCheckDispatch", Conf->GuardCFCheckDispatch); |
| 856 | W.printHex("GuardCFFunctionTable", Conf->GuardCFFunctionTable); |
| 857 | W.printNumber("GuardCFFunctionCount", Conf->GuardCFFunctionCount); |
| 858 | W.printHex("GuardFlags", Conf->GuardFlags); |
| 859 | |
| 860 | Tables.GuardFidTableVA = Conf->GuardCFFunctionTable; |
| 861 | Tables.GuardFidTableCount = Conf->GuardCFFunctionCount; |
| 862 | Tables.GuardFlags = Conf->GuardFlags; |
| 863 | |
| 864 | // Print the rest. (2017) |
| 865 | if (Conf->Size < sizeof(T)) |
| 866 | return; |
| 867 | W.printHex("GuardAddressTakenIatEntryTable", |
| 868 | Conf->GuardAddressTakenIatEntryTable); |
| 869 | W.printNumber("GuardAddressTakenIatEntryCount", |
| 870 | Conf->GuardAddressTakenIatEntryCount); |
| 871 | W.printHex("GuardLongJumpTargetTable", Conf->GuardLongJumpTargetTable); |
| 872 | W.printNumber("GuardLongJumpTargetCount", Conf->GuardLongJumpTargetCount); |
| 873 | W.printHex("DynamicValueRelocTable", Conf->DynamicValueRelocTable); |
| 874 | W.printHex("CHPEMetadataPointer", Conf->CHPEMetadataPointer); |
| 875 | W.printHex("GuardRFFailureRoutine", Conf->GuardRFFailureRoutine); |
| 876 | W.printHex("GuardRFFailureRoutineFunctionPointer", |
| 877 | Conf->GuardRFFailureRoutineFunctionPointer); |
| 878 | W.printHex("DynamicValueRelocTableOffset", |
| 879 | Conf->DynamicValueRelocTableOffset); |
| 880 | W.printNumber("DynamicValueRelocTableSection", |
| 881 | Conf->DynamicValueRelocTableSection); |
| 882 | W.printHex("GuardRFVerifyStackPointerFunctionPointer", |
| 883 | Conf->GuardRFVerifyStackPointerFunctionPointer); |
| 884 | W.printHex("HotPatchTableOffset", Conf->HotPatchTableOffset); |
| 885 | } |
| 886 | |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 887 | void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) { |
| 888 | W.printHex("BaseOfData", Hdr->BaseOfData); |
| 889 | } |
| 890 | |
| 891 | void COFFDumper::printBaseOfDataField(const pe32plus_header *) {} |
| 892 | |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 893 | void COFFDumper::printCodeViewDebugInfo() { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 894 | // Print types first to build CVUDTNames, then print symbols. |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 895 | for (const SectionRef &S : Obj->sections()) { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 896 | StringRef SectionName; |
| 897 | error(S.getName(SectionName)); |
| 898 | if (SectionName == ".debug$T") |
| 899 | printCodeViewTypeSection(SectionName, S); |
| 900 | } |
| 901 | for (const SectionRef &S : Obj->sections()) { |
| 902 | StringRef SectionName; |
| 903 | error(S.getName(SectionName)); |
| 904 | if (SectionName == ".debug$S") |
| 905 | printCodeViewSymbolSection(SectionName, S); |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 909 | void COFFDumper::initializeFileAndStringTables(BinaryStreamReader &Reader) { |
| 910 | while (Reader.bytesRemaining() > 0 && |
| 911 | (!CVFileChecksumTable.valid() || !CVStringTable.valid())) { |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 912 | // The section consists of a number of subsection in the following format: |
| 913 | // |SubSectionType|SubSectionSize|Contents...| |
| 914 | uint32_t SubType, SubSectionSize; |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 915 | error(Reader.readInteger(SubType)); |
| 916 | error(Reader.readInteger(SubSectionSize)); |
| 917 | |
| 918 | StringRef Contents; |
| 919 | error(Reader.readFixedString(Contents, SubSectionSize)); |
| 920 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 921 | BinaryStreamRef ST(Contents, support::little); |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 922 | switch (DebugSubsectionKind(SubType)) { |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 923 | case DebugSubsectionKind::FileChecksums: |
| 924 | error(CVFileChecksumTable.initialize(ST)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 925 | break; |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 926 | case DebugSubsectionKind::StringTable: |
Zachary Turner | d9a6263 | 2017-05-17 20:23:31 +0000 | [diff] [blame] | 927 | error(CVStringTable.initialize(ST)); |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 928 | break; |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 929 | default: |
| 930 | break; |
| 931 | } |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 932 | |
David Majnemer | efaaf41 | 2016-05-28 20:04:48 +0000 | [diff] [blame] | 933 | uint32_t PaddedSize = alignTo(SubSectionSize, 4); |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 934 | error(Reader.skip(PaddedSize - SubSectionSize)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 938 | void COFFDumper::printCodeViewSymbolSection(StringRef SectionName, |
| 939 | const SectionRef &Section) { |
| 940 | StringRef SectionContents; |
| 941 | error(Section.getContents(SectionContents)); |
| 942 | StringRef Data = SectionContents; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 943 | |
| 944 | SmallVector<StringRef, 10> FunctionNames; |
| 945 | StringMap<StringRef> FunctionLineTables; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 946 | |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 947 | ListScope D(W, "CodeViewDebugInfo"); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 948 | // Print the section to allow correlation with printSections. |
| 949 | W.printNumber("Section", SectionName, Obj->getSectionID(Section)); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 950 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 951 | uint32_t Magic; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 952 | error(consume(Data, Magic)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 953 | W.printHex("Magic", Magic); |
| 954 | if (Magic != COFF::DEBUG_SECTION_MAGIC) |
| 955 | return error(object_error::parse_failed); |
| 956 | |
Zachary Turner | d9a6263 | 2017-05-17 20:23:31 +0000 | [diff] [blame] | 957 | BinaryStreamReader FSReader(Data, support::little); |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 958 | initializeFileAndStringTables(FSReader); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 959 | |
Zachary Turner | a96cce6 | 2016-06-03 03:25:59 +0000 | [diff] [blame] | 960 | // TODO: Convert this over to using ModuleSubstreamVisitor. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 961 | while (!Data.empty()) { |
| 962 | // The section consists of a number of subsection in the following format: |
| 963 | // |SubSectionType|SubSectionSize|Contents...| |
| 964 | uint32_t SubType, SubSectionSize; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 965 | error(consume(Data, SubType)); |
| 966 | error(consume(Data, SubSectionSize)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 967 | |
| 968 | ListScope S(W, "Subsection"); |
| 969 | W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes)); |
| 970 | W.printHex("SubSectionSize", SubSectionSize); |
| 971 | |
| 972 | // Get the contents of the subsection. |
| 973 | if (SubSectionSize > Data.size()) |
| 974 | return error(object_error::parse_failed); |
| 975 | StringRef Contents = Data.substr(0, SubSectionSize); |
| 976 | |
| 977 | // Add SubSectionSize to the current offset and align that offset to find |
| 978 | // the next subsection. |
| 979 | size_t SectionOffset = Data.data() - SectionContents.data(); |
| 980 | size_t NextOffset = SectionOffset + SubSectionSize; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 981 | NextOffset = alignTo(NextOffset, 4); |
David Majnemer | cab2b46 | 2016-05-28 20:04:50 +0000 | [diff] [blame] | 982 | if (NextOffset > SectionContents.size()) |
| 983 | return error(object_error::parse_failed); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 984 | Data = SectionContents.drop_front(NextOffset); |
| 985 | |
| 986 | // Optionally print the subsection bytes in case our parsing gets confused |
| 987 | // later. |
| 988 | if (opts::CodeViewSubsectionBytes) |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 989 | printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents, |
| 990 | Contents); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 991 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 992 | switch (DebugSubsectionKind(SubType)) { |
| 993 | case DebugSubsectionKind::Symbols: |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 994 | printCodeViewSymbolsSubsection(Contents, Section, SectionContents); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 995 | break; |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 996 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 997 | case DebugSubsectionKind::InlineeLines: |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 998 | printCodeViewInlineeLines(Contents); |
| 999 | break; |
| 1000 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 1001 | case DebugSubsectionKind::FileChecksums: |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 1002 | printCodeViewFileChecksums(Contents); |
| 1003 | break; |
| 1004 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 1005 | case DebugSubsectionKind::Lines: { |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1006 | // Holds a PC to file:line table. Some data to parse this subsection is |
| 1007 | // stored in the other subsections, so just check sanity and store the |
| 1008 | // pointers for deferred processing. |
| 1009 | |
| 1010 | if (SubSectionSize < 12) { |
| 1011 | // There should be at least three words to store two function |
| 1012 | // relocations and size of the code. |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 1013 | error(object_error::parse_failed); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1017 | StringRef LinkageName; |
| 1018 | error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset, |
| 1019 | LinkageName)); |
| 1020 | W.printString("LinkageName", LinkageName); |
| 1021 | if (FunctionLineTables.count(LinkageName) != 0) { |
| 1022 | // Saw debug info for this function already? |
| 1023 | error(object_error::parse_failed); |
| 1024 | return; |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 1025 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 1026 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1027 | FunctionLineTables[LinkageName] = Contents; |
| 1028 | FunctionNames.push_back(LinkageName); |
| 1029 | break; |
| 1030 | } |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 1031 | case DebugSubsectionKind::FrameData: { |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 1032 | // First four bytes is a relocation against the function. |
Zachary Turner | d9a6263 | 2017-05-17 20:23:31 +0000 | [diff] [blame] | 1033 | BinaryStreamReader SR(Contents, llvm::support::little); |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1034 | |
| 1035 | DebugFrameDataSubsectionRef FrameData; |
| 1036 | error(FrameData.initialize(SR)); |
| 1037 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1038 | StringRef LinkageName; |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 1039 | error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents, |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1040 | FrameData.getRelocPtr(), LinkageName)); |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 1041 | W.printString("LinkageName", LinkageName); |
| 1042 | |
| 1043 | // To find the active frame description, search this array for the |
| 1044 | // smallest PC range that includes the current PC. |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1045 | for (const auto &FD : FrameData) { |
| 1046 | StringRef FrameFunc = error(CVStringTable.getString(FD.FrameFunc)); |
David Majnemer | a6d93fd | 2016-05-28 19:45:49 +0000 | [diff] [blame] | 1047 | |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 1048 | DictScope S(W, "FrameData"); |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1049 | W.printHex("RvaStart", FD.RvaStart); |
| 1050 | W.printHex("CodeSize", FD.CodeSize); |
| 1051 | W.printHex("LocalSize", FD.LocalSize); |
| 1052 | W.printHex("ParamsSize", FD.ParamsSize); |
| 1053 | W.printHex("MaxStackSize", FD.MaxStackSize); |
David Majnemer | a6d93fd | 2016-05-28 19:45:49 +0000 | [diff] [blame] | 1054 | W.printString("FrameFunc", FrameFunc); |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1055 | W.printHex("PrologSize", FD.PrologSize); |
| 1056 | W.printHex("SavedRegsSize", FD.SavedRegsSize); |
| 1057 | W.printFlags("Flags", FD.Flags, makeArrayRef(FrameDataFlags)); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1058 | } |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | // Do nothing for unrecognized subsections. |
| 1063 | default: |
| 1064 | break; |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 1065 | } |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 1066 | W.flush(); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | // Dump the line tables now that we've read all the subsections and know all |
| 1070 | // the required information. |
| 1071 | for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) { |
| 1072 | StringRef Name = FunctionNames[I]; |
| 1073 | ListScope S(W, "FunctionLineTable"); |
Reid Kleckner | 7c0c0c0 | 2015-12-15 01:23:55 +0000 | [diff] [blame] | 1074 | W.printString("LinkageName", Name); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 1075 | |
Zachary Turner | d9a6263 | 2017-05-17 20:23:31 +0000 | [diff] [blame] | 1076 | BinaryStreamReader Reader(FunctionLineTables[Name], support::little); |
David Majnemer | 0373d53 | 2015-07-09 18:14:31 +0000 | [diff] [blame] | 1077 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 1078 | DebugLinesSubsectionRef LineInfo; |
Zachary Turner | 05bd9f3 | 2017-04-28 23:41:36 +0000 | [diff] [blame] | 1079 | error(LineInfo.initialize(Reader)); |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 1080 | |
Zachary Turner | 05bd9f3 | 2017-04-28 23:41:36 +0000 | [diff] [blame] | 1081 | W.printHex("Flags", LineInfo.header()->Flags); |
| 1082 | W.printHex("CodeSize", LineInfo.header()->CodeSize); |
| 1083 | for (const auto &Entry : LineInfo) { |
Justin Bogner | a1413db | 2015-07-09 04:27:36 +0000 | [diff] [blame] | 1084 | |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 1085 | ListScope S(W, "FilenameSegment"); |
Zachary Turner | 05bd9f3 | 2017-04-28 23:41:36 +0000 | [diff] [blame] | 1086 | printFileNameForOffset("Filename", Entry.NameIndex); |
Zachary Turner | 4346ae1 | 2017-04-29 00:03:32 +0000 | [diff] [blame] | 1087 | uint32_t ColumnIndex = 0; |
Zachary Turner | 05bd9f3 | 2017-04-28 23:41:36 +0000 | [diff] [blame] | 1088 | for (const auto &Line : Entry.LineNumbers) { |
| 1089 | if (Line.Offset >= LineInfo.header()->CodeSize) { |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 1090 | error(object_error::parse_failed); |
| 1091 | return; |
| 1092 | } |
Zachary Turner | 05bd9f3 | 2017-04-28 23:41:36 +0000 | [diff] [blame] | 1093 | |
| 1094 | std::string PC = formatv("+{0:X}", uint32_t(Line.Offset)); |
| 1095 | ListScope PCScope(W, PC); |
| 1096 | codeview::LineInfo LI(Line.Flags); |
| 1097 | |
David Majnemer | a4859df | 2016-02-04 17:57:12 +0000 | [diff] [blame] | 1098 | if (LI.isAlwaysStepInto()) |
| 1099 | W.printString("StepInto", StringRef("Always")); |
| 1100 | else if (LI.isNeverStepInto()) |
| 1101 | W.printString("StepInto", StringRef("Never")); |
| 1102 | else |
| 1103 | W.printNumber("LineNumberStart", LI.getStartLine()); |
| 1104 | W.printNumber("LineNumberEndDelta", LI.getLineDelta()); |
| 1105 | W.printBoolean("IsStatement", LI.isStatement()); |
Zachary Turner | 5b6e4e0 | 2017-04-29 01:13:21 +0000 | [diff] [blame] | 1106 | if (LineInfo.hasColumnInfo()) { |
Zachary Turner | 4346ae1 | 2017-04-29 00:03:32 +0000 | [diff] [blame] | 1107 | W.printNumber("ColStart", Entry.Columns[ColumnIndex].StartColumn); |
| 1108 | W.printNumber("ColEnd", Entry.Columns[ColumnIndex].EndColumn); |
| 1109 | ++ColumnIndex; |
David Majnemer | 0494836 | 2016-01-13 01:05:16 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 1116 | void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection, |
| 1117 | const SectionRef &Section, |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 1118 | StringRef SectionContents) { |
Zachary Turner | aaad574 | 2016-05-23 23:41:13 +0000 | [diff] [blame] | 1119 | ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(), |
| 1120 | Subsection.bytes_end()); |
| 1121 | auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj, |
| 1122 | SectionContents); |
Zachary Turner | ebd3ae8 | 2017-06-01 21:52:41 +0000 | [diff] [blame] | 1123 | CVSymbolDumper CVSD(W, Types, CodeViewContainer::ObjectFile, std::move(CODD), |
| 1124 | opts::CodeViewSubsectionBytes); |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 1125 | CVSymbolArray Symbols; |
Zachary Turner | d9a6263 | 2017-05-17 20:23:31 +0000 | [diff] [blame] | 1126 | BinaryStreamReader Reader(BinaryData, llvm::support::little); |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 1127 | if (auto EC = Reader.readArray(Symbols, Reader.getLength())) { |
| 1128 | consumeError(std::move(EC)); |
| 1129 | W.flush(); |
| 1130 | error(object_error::parse_failed); |
| 1131 | } |
Reid Kleckner | 2893fd1 | 2016-01-14 17:51:54 +0000 | [diff] [blame] | 1132 | |
Zachary Turner | 0d84074 | 2016-10-07 21:34:46 +0000 | [diff] [blame] | 1133 | if (auto EC = CVSD.dump(Symbols)) { |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 1134 | W.flush(); |
Zachary Turner | 0d84074 | 2016-10-07 21:34:46 +0000 | [diff] [blame] | 1135 | error(std::move(EC)); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 1136 | } |
Reid Kleckner | af49ecb | 2016-01-15 22:09:13 +0000 | [diff] [blame] | 1137 | W.flush(); |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1138 | } |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 1139 | |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 1140 | void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) { |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1141 | BinaryStreamRef Stream(Subsection, llvm::support::little); |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 1142 | DebugChecksumsSubsectionRef Checksums; |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1143 | error(Checksums.initialize(Stream)); |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 1144 | |
| 1145 | for (auto &FC : Checksums) { |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 1146 | DictScope S(W, "FileChecksum"); |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 1147 | |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 1148 | StringRef Filename = error(CVStringTable.getString(FC.FileNameOffset)); |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 1149 | W.printHex("Filename", Filename, FC.FileNameOffset); |
| 1150 | W.printHex("ChecksumSize", FC.Checksum.size()); |
| 1151 | W.printEnum("ChecksumKind", uint8_t(FC.Kind), |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 1152 | makeArrayRef(FileChecksumKindNames)); |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 1153 | |
| 1154 | W.printBinary("ChecksumBytes", FC.Checksum); |
Reid Kleckner | c31f530 | 2016-01-15 18:06:25 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 1158 | void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) { |
Zachary Turner | d9a6263 | 2017-05-17 20:23:31 +0000 | [diff] [blame] | 1159 | BinaryStreamReader SR(Subsection, llvm::support::little); |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 1160 | DebugInlineeLinesSubsectionRef Lines; |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 1161 | error(Lines.initialize(SR)); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 1162 | |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 1163 | for (auto &Line : Lines) { |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 1164 | DictScope S(W, "InlineeSourceLine"); |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 1165 | printTypeIndex("Inlinee", Line.Header->Inlinee); |
| 1166 | printFileNameForOffset("FileID", Line.Header->FileID); |
| 1167 | W.printNumber("SourceLineNum", Line.Header->SourceLineNum); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 1168 | |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 1169 | if (Lines.hasExtraFiles()) { |
| 1170 | W.printNumber("ExtraFileCount", Line.ExtraFiles.size()); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 1171 | ListScope ExtraFiles(W, "ExtraFiles"); |
Zachary Turner | edef145 | 2017-05-02 16:56:09 +0000 | [diff] [blame] | 1172 | for (const auto &FID : Line.ExtraFiles) { |
| 1173 | printFileNameForOffset("FileID", FID); |
Reid Kleckner | e9ab349 | 2016-01-14 19:20:17 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 1179 | StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) { |
| 1180 | // The file checksum subsection should precede all references to it. |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 1181 | if (!CVFileChecksumTable.valid() || !CVStringTable.valid()) |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 1182 | error(object_error::parse_failed); |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 1183 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1184 | auto Iter = CVFileChecksumTable.getArray().at(FileOffset); |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 1185 | |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 1186 | // Check if the file checksum table offset is valid. |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 1187 | if (Iter == CVFileChecksumTable.end()) |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 1188 | error(object_error::parse_failed); |
| 1189 | |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 1190 | return error(CVStringTable.getString(Iter->FileNameOffset)); |
Reid Kleckner | 8e7275c | 2016-01-15 00:11:21 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) { |
| 1194 | W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset); |
| 1195 | } |
| 1196 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 1197 | void COFFDumper::mergeCodeViewTypes(TypeTableBuilder &CVIDs, |
| 1198 | TypeTableBuilder &CVTypes) { |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1199 | for (const SectionRef &S : Obj->sections()) { |
| 1200 | StringRef SectionName; |
| 1201 | error(S.getName(SectionName)); |
| 1202 | if (SectionName == ".debug$T") { |
| 1203 | StringRef Data; |
| 1204 | error(S.getContents(Data)); |
David Majnemer | 78b0d72 | 2016-05-28 19:17:48 +0000 | [diff] [blame] | 1205 | uint32_t Magic; |
| 1206 | error(consume(Data, Magic)); |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1207 | if (Magic != 4) |
| 1208 | error(object_error::parse_failed); |
Zachary Turner | d9a6263 | 2017-05-17 20:23:31 +0000 | [diff] [blame] | 1209 | |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 1210 | CVTypeArray Types; |
Zachary Turner | d9a6263 | 2017-05-17 20:23:31 +0000 | [diff] [blame] | 1211 | BinaryStreamReader Reader(Data, llvm::support::little); |
Zachary Turner | 0d43c1c | 2016-05-28 05:21:57 +0000 | [diff] [blame] | 1212 | if (auto EC = Reader.readArray(Types, Reader.getLength())) { |
| 1213 | consumeError(std::move(EC)); |
| 1214 | W.flush(); |
| 1215 | error(object_error::parse_failed); |
| 1216 | } |
Zachary Turner | b32ec02 | 2017-05-18 23:04:08 +0000 | [diff] [blame] | 1217 | SmallVector<TypeIndex, 128> SourceToDest; |
Reid Kleckner | a842cd7 | 2017-07-17 20:28:06 +0000 | [diff] [blame] | 1218 | if (auto EC = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types)) |
Reid Kleckner | c4b5d79 | 2017-04-03 23:58:15 +0000 | [diff] [blame] | 1219 | return error(std::move(EC)); |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1224 | void COFFDumper::printCodeViewTypeSection(StringRef SectionName, |
| 1225 | const SectionRef &Section) { |
| 1226 | ListScope D(W, "CodeViewTypes"); |
| 1227 | W.printNumber("Section", SectionName, Obj->getSectionID(Section)); |
Adrian McCarthy | 963a331 | 2016-05-02 23:45:03 +0000 | [diff] [blame] | 1228 | |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 1229 | StringRef Data; |
| 1230 | error(Section.getContents(Data)); |
Reid Kleckner | b2bd281 | 2016-05-02 18:10:00 +0000 | [diff] [blame] | 1231 | if (opts::CodeViewSubsectionBytes) |
| 1232 | W.printBinaryBlock("Data", Data); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1233 | |
| 1234 | uint32_t Magic; |
Zachary Turner | 38cc8b3 | 2016-05-12 17:45:44 +0000 | [diff] [blame] | 1235 | error(consume(Data, Magic)); |
Reid Kleckner | 7960de9 | 2016-05-04 19:39:28 +0000 | [diff] [blame] | 1236 | W.printHex("Magic", Magic); |
| 1237 | if (Magic != COFF::DEBUG_SECTION_MAGIC) |
| 1238 | return error(object_error::parse_failed); |
| 1239 | |
Zachary Turner | 26dbc54 | 2017-06-18 20:52:45 +0000 | [diff] [blame] | 1240 | Types.reset(Data, 100); |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 1241 | |
| 1242 | TypeDumpVisitor TDV(Types, &W, opts::CodeViewSubsectionBytes); |
| 1243 | error(codeview::visitTypeStream(Types, TDV)); |
| 1244 | W.flush(); |
Timur Iskhodzhanov | 56af52f | 2014-10-23 22:25:31 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1247 | void COFFDumper::printSections() { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1248 | ListScope SectionsD(W, "Sections"); |
| 1249 | int SectionNumber = 0; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1250 | for (const SectionRef &Sec : Obj->sections()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1251 | ++SectionNumber; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1252 | const coff_section *Section = Obj->getCOFFSection(Sec); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1253 | |
| 1254 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1255 | error(Sec.getName(Name)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1256 | |
| 1257 | DictScope D(W, "Section"); |
| 1258 | W.printNumber("Number", SectionNumber); |
| 1259 | W.printBinary("Name", Name, Section->Name); |
| 1260 | W.printHex ("VirtualSize", Section->VirtualSize); |
| 1261 | W.printHex ("VirtualAddress", Section->VirtualAddress); |
| 1262 | W.printNumber("RawDataSize", Section->SizeOfRawData); |
| 1263 | W.printHex ("PointerToRawData", Section->PointerToRawData); |
| 1264 | W.printHex ("PointerToRelocations", Section->PointerToRelocations); |
| 1265 | W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers); |
| 1266 | W.printNumber("RelocationCount", Section->NumberOfRelocations); |
| 1267 | W.printNumber("LineNumberCount", Section->NumberOfLinenumbers); |
| 1268 | W.printFlags ("Characteristics", Section->Characteristics, |
| 1269 | makeArrayRef(ImageSectionCharacteristics), |
| 1270 | COFF::SectionCharacteristics(0x00F00000)); |
| 1271 | |
| 1272 | if (opts::SectionRelocations) { |
| 1273 | ListScope D(W, "Relocations"); |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1274 | for (const RelocationRef &Reloc : Sec.relocations()) |
| 1275 | printRelocation(Sec, Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | if (opts::SectionSymbols) { |
| 1279 | ListScope D(W, "Symbols"); |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1280 | for (const SymbolRef &Symbol : Obj->symbols()) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1281 | if (!Sec.containsSymbol(Symbol)) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1282 | continue; |
| 1283 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1284 | printSymbol(Symbol); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
David Majnemer | dac3985 | 2014-09-26 22:32:16 +0000 | [diff] [blame] | 1288 | if (opts::SectionData && |
| 1289 | !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1290 | StringRef Data; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1291 | error(Sec.getContents(Data)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1292 | |
| 1293 | W.printBinaryBlock("SectionData", Data); |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | void COFFDumper::printRelocations() { |
| 1299 | ListScope D(W, "Relocations"); |
| 1300 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1301 | int SectionNumber = 0; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1302 | for (const SectionRef &Section : Obj->sections()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1303 | ++SectionNumber; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1304 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1305 | error(Section.getName(Name)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1306 | |
| 1307 | bool PrintedGroup = false; |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1308 | for (const RelocationRef &Reloc : Section.relocations()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1309 | if (!PrintedGroup) { |
| 1310 | W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; |
| 1311 | W.indent(); |
| 1312 | PrintedGroup = true; |
| 1313 | } |
| 1314 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1315 | printRelocation(Section, Reloc); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | if (PrintedGroup) { |
| 1319 | W.unindent(); |
| 1320 | W.startLine() << "}\n"; |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1325 | void COFFDumper::printRelocation(const SectionRef &Section, |
Reid Kleckner | 1c7a9cd | 2016-01-14 17:51:57 +0000 | [diff] [blame] | 1326 | const RelocationRef &Reloc, uint64_t Bias) { |
| 1327 | uint64_t Offset = Reloc.getOffset() - Bias; |
Rafael Espindola | 99c041b | 2015-06-30 01:53:01 +0000 | [diff] [blame] | 1328 | uint64_t RelocType = Reloc.getType(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1329 | SmallString<32> RelocName; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1330 | StringRef SymbolName; |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 1331 | Reloc.getTypeName(RelocName); |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1332 | symbol_iterator Symbol = Reloc.getSymbol(); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1333 | if (Symbol != Obj->symbol_end()) { |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1334 | Expected<StringRef> SymbolNameOrErr = Symbol->getName(); |
| 1335 | error(errorToErrorCode(SymbolNameOrErr.takeError())); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1336 | SymbolName = *SymbolNameOrErr; |
| 1337 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1338 | |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1339 | if (opts::ExpandRelocs) { |
| 1340 | DictScope Group(W, "Relocation"); |
| 1341 | W.printHex("Offset", Offset); |
| 1342 | W.printNumber("Type", RelocName, RelocType); |
David Majnemer | 1f80b0a | 2014-11-13 07:42:11 +0000 | [diff] [blame] | 1343 | W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName); |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1344 | } else { |
| 1345 | raw_ostream& OS = W.startLine(); |
| 1346 | OS << W.hex(Offset) |
| 1347 | << " " << RelocName |
David Majnemer | 1f80b0a | 2014-11-13 07:42:11 +0000 | [diff] [blame] | 1348 | << " " << (SymbolName.empty() ? "-" : SymbolName) |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 1349 | << "\n"; |
| 1350 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | void COFFDumper::printSymbols() { |
| 1354 | ListScope Group(W, "Symbols"); |
| 1355 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1356 | for (const SymbolRef &Symbol : Obj->symbols()) |
| 1357 | printSymbol(Symbol); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1360 | void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1361 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 1362 | static ErrorOr<StringRef> |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1363 | getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber, |
| 1364 | const coff_section *Section) { |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1365 | if (Section) { |
| 1366 | StringRef SectionName; |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1367 | if (std::error_code EC = Obj->getSectionName(Section, SectionName)) |
| 1368 | return EC; |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1369 | return SectionName; |
| 1370 | } |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1371 | if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1372 | return StringRef("IMAGE_SYM_DEBUG"); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1373 | if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1374 | return StringRef("IMAGE_SYM_ABSOLUTE"); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1375 | if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED) |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1376 | return StringRef("IMAGE_SYM_UNDEFINED"); |
| 1377 | return StringRef(""); |
David Majnemer | f4dc456 | 2014-09-20 00:25:06 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 1380 | void COFFDumper::printSymbol(const SymbolRef &Sym) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1381 | DictScope D(W, "Symbol"); |
| 1382 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1383 | COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1384 | const coff_section *Section; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1385 | if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1386 | W.startLine() << "Invalid section number: " << EC.message() << "\n"; |
| 1387 | W.flush(); |
| 1388 | return; |
| 1389 | } |
| 1390 | |
| 1391 | StringRef SymbolName; |
| 1392 | if (Obj->getSymbolName(Symbol, SymbolName)) |
| 1393 | SymbolName = ""; |
| 1394 | |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1395 | StringRef SectionName = ""; |
| 1396 | ErrorOr<StringRef> Res = |
| 1397 | getSectionName(Obj, Symbol.getSectionNumber(), Section); |
| 1398 | if (Res) |
| 1399 | SectionName = *Res; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1400 | |
| 1401 | W.printString("Name", SymbolName); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1402 | W.printNumber("Value", Symbol.getValue()); |
| 1403 | W.printNumber("Section", SectionName, Symbol.getSectionNumber()); |
| 1404 | W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType)); |
| 1405 | W.printEnum ("ComplexType", Symbol.getComplexType(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1406 | makeArrayRef(ImageSymDType)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1407 | W.printEnum ("StorageClass", Symbol.getStorageClass(), |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1408 | makeArrayRef(ImageSymClass)); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1409 | W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1410 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1411 | for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) { |
| 1412 | if (Symbol.isFunctionDefinition()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1413 | const coff_aux_function_definition *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1414 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1415 | |
| 1416 | DictScope AS(W, "AuxFunctionDef"); |
| 1417 | W.printNumber("TagIndex", Aux->TagIndex); |
| 1418 | W.printNumber("TotalSize", Aux->TotalSize); |
David Majnemer | f3a2af5 | 2014-03-19 04:33:27 +0000 | [diff] [blame] | 1419 | W.printHex("PointerToLineNumber", Aux->PointerToLinenumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1420 | W.printHex("PointerToNextFunction", Aux->PointerToNextFunction); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1421 | |
David Majnemer | c7d7c6f | 2014-10-31 05:07:00 +0000 | [diff] [blame] | 1422 | } else if (Symbol.isAnyUndefined()) { |
David Majnemer | f3a2af5 | 2014-03-19 04:33:27 +0000 | [diff] [blame] | 1423 | const coff_aux_weak_external *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1424 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1425 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1426 | ErrorOr<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1427 | StringRef LinkedName; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1428 | std::error_code EC = Linked.getError(); |
| 1429 | if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1430 | LinkedName = ""; |
| 1431 | error(EC); |
| 1432 | } |
| 1433 | |
| 1434 | DictScope AS(W, "AuxWeakExternal"); |
| 1435 | W.printNumber("Linked", LinkedName, Aux->TagIndex); |
| 1436 | W.printEnum ("Search", Aux->Characteristics, |
| 1437 | makeArrayRef(WeakExternalCharacteristics)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1438 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1439 | } else if (Symbol.isFileRecord()) { |
| 1440 | const char *FileName; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1441 | error(getSymbolAuxData(Obj, Symbol, I, FileName)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1442 | |
Nico Rieck | 0ab8e60 | 2013-04-22 08:35:11 +0000 | [diff] [blame] | 1443 | DictScope AS(W, "AuxFileRecord"); |
Saleem Abdulrasool | d38c6b1 | 2014-04-14 02:37:23 +0000 | [diff] [blame] | 1444 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1445 | StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() * |
| 1446 | Obj->getSymbolTableEntrySize()); |
Saleem Abdulrasool | d38c6b1 | 2014-04-14 02:37:23 +0000 | [diff] [blame] | 1447 | W.printString("FileName", Name.rtrim(StringRef("\0", 1))); |
Saleem Abdulrasool | 3b5e001 | 2014-04-16 04:15:29 +0000 | [diff] [blame] | 1448 | break; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1449 | } else if (Symbol.isSectionDefinition()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1450 | const coff_aux_section_definition *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1451 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1452 | |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1453 | int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj()); |
| 1454 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1455 | DictScope AS(W, "AuxSectionDef"); |
| 1456 | W.printNumber("Length", Aux->Length); |
| 1457 | W.printNumber("RelocationCount", Aux->NumberOfRelocations); |
| 1458 | W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers); |
| 1459 | W.printHex("Checksum", Aux->CheckSum); |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1460 | W.printNumber("Number", AuxNumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1461 | W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1462 | |
Nico Rieck | a711dee | 2013-04-22 08:34:59 +0000 | [diff] [blame] | 1463 | if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1464 | && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { |
| 1465 | const coff_section *Assoc; |
David Majnemer | 236b0ca | 2014-11-17 11:17:17 +0000 | [diff] [blame] | 1466 | StringRef AssocName = ""; |
| 1467 | std::error_code EC = Obj->getSection(AuxNumber, Assoc); |
| 1468 | ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc); |
| 1469 | if (Res) |
| 1470 | AssocName = *Res; |
| 1471 | if (!EC) |
| 1472 | EC = Res.getError(); |
| 1473 | if (EC) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1474 | AssocName = ""; |
| 1475 | error(EC); |
| 1476 | } |
| 1477 | |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 1478 | W.printNumber("AssocSection", AssocName, AuxNumber); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1479 | } |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1480 | } else if (Symbol.isCLRToken()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1481 | const coff_aux_clr_token *Aux; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1482 | error(getSymbolAuxData(Obj, Symbol, I, Aux)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1483 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1484 | ErrorOr<COFFSymbolRef> ReferredSym = |
| 1485 | Obj->getSymbol(Aux->SymbolTableIndex); |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1486 | StringRef ReferredName; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1487 | std::error_code EC = ReferredSym.getError(); |
| 1488 | if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) { |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1489 | ReferredName = ""; |
| 1490 | error(EC); |
| 1491 | } |
| 1492 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1493 | DictScope AS(W, "AuxCLRToken"); |
| 1494 | W.printNumber("AuxType", Aux->AuxType); |
| 1495 | W.printNumber("Reserved", Aux->Reserved); |
Nico Rieck | 8678acd | 2014-03-17 01:46:52 +0000 | [diff] [blame] | 1496 | W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1497 | |
| 1498 | } else { |
| 1499 | W.startLine() << "<unhandled auxiliary record>\n"; |
| 1500 | } |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | void COFFDumper::printUnwindInfo() { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1505 | ListScope D(W, "UnwindInformation"); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1506 | switch (Obj->getMachine()) { |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1507 | case COFF::IMAGE_FILE_MACHINE_AMD64: { |
| 1508 | Win64EH::Dumper Dumper(W); |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1509 | Win64EH::Dumper::SymbolResolver |
| 1510 | Resolver = [](const object::coff_section *Section, uint64_t Offset, |
| 1511 | SymbolRef &Symbol, void *user_data) -> std::error_code { |
| 1512 | COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data); |
| 1513 | return Dumper->resolveSymbol(Section, Offset, Symbol); |
| 1514 | }; |
Saleem Abdulrasool | 65dbbb5 | 2014-05-25 21:37:59 +0000 | [diff] [blame] | 1515 | Win64EH::Dumper::Context Ctx(*Obj, Resolver, this); |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1516 | Dumper.printData(Ctx); |
| 1517 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1518 | } |
Saleem Abdulrasool | e6971ca | 2014-06-04 15:47:15 +0000 | [diff] [blame] | 1519 | case COFF::IMAGE_FILE_MACHINE_ARMNT: { |
| 1520 | ARM::WinEH::Decoder Decoder(W); |
| 1521 | Decoder.dumpProcedureData(*Obj); |
| 1522 | break; |
| 1523 | } |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1524 | default: |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1525 | W.printEnum("unsupported Image Machine", Obj->getMachine(), |
Saleem Abdulrasool | e8839a7 | 2014-05-25 20:26:45 +0000 | [diff] [blame] | 1526 | makeArrayRef(ImageFileMachineType)); |
| 1527 | break; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1531 | void COFFDumper::printImportedSymbols( |
| 1532 | iterator_range<imported_symbol_iterator> Range) { |
| 1533 | for (const ImportedSymbolRef &I : Range) { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1534 | StringRef Sym; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1535 | error(I.getSymbolName(Sym)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1536 | uint16_t Ordinal; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1537 | error(I.getOrdinal(Ordinal)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1538 | W.printNumber("Symbol", Sym, Ordinal); |
| 1539 | } |
| 1540 | } |
| 1541 | |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1542 | void COFFDumper::printDelayImportedSymbols( |
| 1543 | const DelayImportDirectoryEntryRef &I, |
| 1544 | iterator_range<imported_symbol_iterator> Range) { |
| 1545 | int Index = 0; |
| 1546 | for (const ImportedSymbolRef &S : Range) { |
| 1547 | DictScope Import(W, "Import"); |
| 1548 | StringRef Sym; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1549 | error(S.getSymbolName(Sym)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1550 | uint16_t Ordinal; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1551 | error(S.getOrdinal(Ordinal)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1552 | W.printNumber("Symbol", Sym, Ordinal); |
| 1553 | uint64_t Addr; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1554 | error(I.getImportAddress(Index++, Addr)); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1555 | W.printHex("Address", Addr); |
| 1556 | } |
| 1557 | } |
| 1558 | |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1559 | void COFFDumper::printCOFFImports() { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1560 | // Regular imports |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1561 | for (const ImportDirectoryEntryRef &I : Obj->import_directories()) { |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1562 | DictScope Import(W, "Import"); |
| 1563 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1564 | error(I.getName(Name)); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1565 | W.printString("Name", Name); |
Shoaib Meenai | 7375448 | 2017-03-26 17:10:11 +0000 | [diff] [blame] | 1566 | uint32_t ILTAddr; |
| 1567 | error(I.getImportLookupTableRVA(ILTAddr)); |
| 1568 | W.printHex("ImportLookupTableRVA", ILTAddr); |
| 1569 | uint32_t IATAddr; |
| 1570 | error(I.getImportAddressTableRVA(IATAddr)); |
| 1571 | W.printHex("ImportAddressTableRVA", IATAddr); |
| 1572 | // The import lookup table can be missing with certain older linkers, so |
| 1573 | // fall back to the import address table in that case. |
| 1574 | if (ILTAddr) |
| 1575 | printImportedSymbols(I.lookup_table_symbols()); |
| 1576 | else |
| 1577 | printImportedSymbols(I.imported_symbols()); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | // Delay imports |
Rui Ueyama | 979fb40 | 2014-10-09 02:16:38 +0000 | [diff] [blame] | 1581 | for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) { |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1582 | DictScope Import(W, "DelayImport"); |
| 1583 | StringRef Name; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1584 | error(I.getName(Name)); |
Rui Ueyama | 15d9935 | 2014-10-03 00:41:58 +0000 | [diff] [blame] | 1585 | W.printString("Name", Name); |
Rui Ueyama | 1af0865 | 2014-10-03 18:07:18 +0000 | [diff] [blame] | 1586 | const delay_import_directory_table_entry *Table; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1587 | error(I.getDelayImportTable(Table)); |
Rui Ueyama | 1af0865 | 2014-10-03 18:07:18 +0000 | [diff] [blame] | 1588 | W.printHex("Attributes", Table->Attributes); |
| 1589 | W.printHex("ModuleHandle", Table->ModuleHandle); |
| 1590 | W.printHex("ImportAddressTable", Table->DelayImportAddressTable); |
| 1591 | W.printHex("ImportNameTable", Table->DelayImportNameTable); |
| 1592 | W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable); |
| 1593 | W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable); |
Rui Ueyama | ffa4ceb | 2014-11-13 03:22:54 +0000 | [diff] [blame] | 1594 | printDelayImportedSymbols(I, I.imported_symbols()); |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1597 | |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 1598 | void COFFDumper::printCOFFExports() { |
| 1599 | for (const ExportDirectoryEntryRef &E : Obj->export_directories()) { |
| 1600 | DictScope Export(W, "Export"); |
| 1601 | |
| 1602 | StringRef Name; |
| 1603 | uint32_t Ordinal, RVA; |
| 1604 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1605 | error(E.getSymbolName(Name)); |
| 1606 | error(E.getOrdinal(Ordinal)); |
| 1607 | error(E.getExportRVA(RVA)); |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 1608 | |
| 1609 | W.printNumber("Ordinal", Ordinal); |
| 1610 | W.printString("Name", Name); |
| 1611 | W.printHex("RVA", RVA); |
| 1612 | } |
| 1613 | } |
| 1614 | |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1615 | void COFFDumper::printCOFFDirectives() { |
| 1616 | for (const SectionRef &Section : Obj->sections()) { |
| 1617 | StringRef Contents; |
| 1618 | StringRef Name; |
| 1619 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1620 | error(Section.getName(Name)); |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1621 | if (Name != ".drectve") |
| 1622 | continue; |
| 1623 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1624 | error(Section.getContents(Contents)); |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 1625 | |
| 1626 | W.printString("Directive(s)", Contents); |
| 1627 | } |
| 1628 | } |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1629 | |
Alexander Kornienko | a45648b | 2017-07-26 10:14:55 +0000 | [diff] [blame] | 1630 | static std::string getBaseRelocTypeName(uint8_t Type) { |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1631 | switch (Type) { |
| 1632 | case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE"; |
| 1633 | case COFF::IMAGE_REL_BASED_HIGH: return "HIGH"; |
| 1634 | case COFF::IMAGE_REL_BASED_LOW: return "LOW"; |
| 1635 | case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW"; |
| 1636 | case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ"; |
Saleem Abdulrasool | 5a41c37 | 2015-01-16 20:16:09 +0000 | [diff] [blame] | 1637 | case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)"; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1638 | case COFF::IMAGE_REL_BASED_DIR64: return "DIR64"; |
Alexander Kornienko | a45648b | 2017-07-26 10:14:55 +0000 | [diff] [blame] | 1639 | default: return "unknown (" + llvm::utostr(Type) + ")"; |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | void COFFDumper::printCOFFBaseReloc() { |
| 1644 | ListScope D(W, "BaseReloc"); |
| 1645 | for (const BaseRelocRef &I : Obj->base_relocs()) { |
| 1646 | uint8_t Type; |
| 1647 | uint32_t RVA; |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 1648 | error(I.getRVA(RVA)); |
| 1649 | error(I.getType(Type)); |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 1650 | DictScope Import(W, "Entry"); |
| 1651 | W.printString("Type", getBaseRelocTypeName(Type)); |
| 1652 | W.printHex("Address", RVA); |
| 1653 | } |
| 1654 | } |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 1655 | |
Zachary Turner | 8d6396d | 2017-04-27 19:38:38 +0000 | [diff] [blame] | 1656 | void COFFDumper::printCOFFResources() { |
| 1657 | ListScope ResourcesD(W, "Resources"); |
| 1658 | for (const SectionRef &S : Obj->sections()) { |
| 1659 | StringRef Name; |
| 1660 | error(S.getName(Name)); |
| 1661 | if (!Name.startswith(".rsrc")) |
| 1662 | continue; |
| 1663 | |
| 1664 | StringRef Ref; |
| 1665 | error(S.getContents(Ref)); |
| 1666 | |
| 1667 | if ((Name == ".rsrc") || (Name == ".rsrc$01")) { |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1668 | ResourceSectionRef RSF(Ref); |
| 1669 | auto &BaseTable = unwrapOrError(RSF.getBaseTable()); |
Eric Beckmann | 1301759 | 2017-06-13 00:16:32 +0000 | [diff] [blame] | 1670 | W.printNumber("Total Number of Resources", |
| 1671 | countTotalTableEntries(RSF, BaseTable, "Type")); |
| 1672 | W.printHex("Base Table Address", |
| 1673 | Obj->getCOFFSection(S)->PointerToRawData); |
| 1674 | W.startLine() << "\n"; |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1675 | printResourceDirectoryTable(RSF, BaseTable, "Type"); |
Zachary Turner | 8d6396d | 2017-04-27 19:38:38 +0000 | [diff] [blame] | 1676 | } |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1677 | if (opts::SectionData) |
| 1678 | W.printBinaryBlock(Name.str() + " Data", Ref); |
Zachary Turner | 8d6396d | 2017-04-27 19:38:38 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
Eric Beckmann | 1301759 | 2017-06-13 00:16:32 +0000 | [diff] [blame] | 1682 | uint32_t |
| 1683 | COFFDumper::countTotalTableEntries(ResourceSectionRef RSF, |
| 1684 | const coff_resource_dir_table &Table, |
| 1685 | StringRef Level) { |
| 1686 | uint32_t TotalEntries = 0; |
| 1687 | for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries; |
| 1688 | i++) { |
| 1689 | auto Entry = unwrapOrError(getResourceDirectoryTableEntry(Table, i)); |
| 1690 | if (Entry.Offset.isSubDir()) { |
| 1691 | StringRef NextLevel; |
| 1692 | if (Level == "Name") |
| 1693 | NextLevel = "Language"; |
| 1694 | else |
| 1695 | NextLevel = "Name"; |
| 1696 | auto &NextTable = unwrapOrError(RSF.getEntrySubDir(Entry)); |
| 1697 | TotalEntries += countTotalTableEntries(RSF, NextTable, NextLevel); |
| 1698 | } else { |
| 1699 | TotalEntries += 1; |
| 1700 | } |
| 1701 | } |
| 1702 | return TotalEntries; |
| 1703 | } |
| 1704 | |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1705 | void COFFDumper::printResourceDirectoryTable( |
| 1706 | ResourceSectionRef RSF, const coff_resource_dir_table &Table, |
| 1707 | StringRef Level) { |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1708 | |
Eric Beckmann | 1301759 | 2017-06-13 00:16:32 +0000 | [diff] [blame] | 1709 | W.printNumber("Number of String Entries", Table.NumberOfNameEntries); |
| 1710 | W.printNumber("Number of ID Entries", Table.NumberOfIDEntries); |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1711 | |
| 1712 | // Iterate through level in resource directory tree. |
| 1713 | for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries; |
| 1714 | i++) { |
| 1715 | auto Entry = unwrapOrError(getResourceDirectoryTableEntry(Table, i)); |
| 1716 | StringRef Name; |
| 1717 | SmallString<20> IDStr; |
| 1718 | raw_svector_ostream OS(IDStr); |
| 1719 | if (i < Table.NumberOfNameEntries) { |
Eric Beckmann | cd704cb | 2017-05-08 02:47:42 +0000 | [diff] [blame] | 1720 | ArrayRef<UTF16> RawEntryNameString = unwrapOrError(RSF.getEntryNameString(Entry)); |
Eric Beckmann | 674deed | 2017-05-09 19:35:45 +0000 | [diff] [blame] | 1721 | std::vector<UTF16> EndianCorrectedNameString; |
| 1722 | if (llvm::sys::IsBigEndianHost) { |
| 1723 | EndianCorrectedNameString.resize(RawEntryNameString.size() + 1); |
| 1724 | std::copy(RawEntryNameString.begin(), RawEntryNameString.end(), |
| 1725 | EndianCorrectedNameString.begin() + 1); |
| 1726 | EndianCorrectedNameString[0] = UNI_UTF16_BYTE_ORDER_MARK_SWAPPED; |
| 1727 | RawEntryNameString = makeArrayRef(EndianCorrectedNameString); |
| 1728 | } |
Eric Beckmann | cd704cb | 2017-05-08 02:47:42 +0000 | [diff] [blame] | 1729 | std::string EntryNameString; |
| 1730 | if (!llvm::convertUTF16ToUTF8String(RawEntryNameString, EntryNameString)) |
| 1731 | error(object_error::parse_failed); |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1732 | OS << ": "; |
Eric Beckmann | cd704cb | 2017-05-08 02:47:42 +0000 | [diff] [blame] | 1733 | OS << EntryNameString; |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1734 | } else { |
| 1735 | if (Level == "Type") { |
| 1736 | ScopedPrinter Printer(OS); |
| 1737 | Printer.printEnum("", Entry.Identifier.ID, |
| 1738 | makeArrayRef(ResourceTypeNames)); |
| 1739 | IDStr = IDStr.slice(0, IDStr.find_first_of(")", 0) + 1); |
| 1740 | } else { |
| 1741 | OS << ": (ID " << Entry.Identifier.ID << ")"; |
| 1742 | } |
| 1743 | } |
| 1744 | Name = StringRef(IDStr); |
| 1745 | ListScope ResourceType(W, Level.str() + Name.str()); |
| 1746 | if (Entry.Offset.isSubDir()) { |
Eric Beckmann | 1301759 | 2017-06-13 00:16:32 +0000 | [diff] [blame] | 1747 | W.printHex("Table Offset", Entry.Offset.value()); |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1748 | StringRef NextLevel; |
| 1749 | if (Level == "Name") |
| 1750 | NextLevel = "Language"; |
| 1751 | else |
| 1752 | NextLevel = "Name"; |
| 1753 | auto &NextTable = unwrapOrError(RSF.getEntrySubDir(Entry)); |
| 1754 | printResourceDirectoryTable(RSF, NextTable, NextLevel); |
| 1755 | } else { |
Eric Beckmann | 1301759 | 2017-06-13 00:16:32 +0000 | [diff] [blame] | 1756 | W.printHex("Entry Offset", Entry.Offset.value()); |
| 1757 | char FormattedTime[20] = {}; |
| 1758 | time_t TDS = time_t(Table.TimeDateStamp); |
| 1759 | strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS)); |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1760 | W.printHex("Time/Date Stamp", FormattedTime, Table.TimeDateStamp); |
| 1761 | W.printNumber("Major Version", Table.MajorVersion); |
| 1762 | W.printNumber("Minor Version", Table.MinorVersion); |
Eric Beckmann | 1301759 | 2017-06-13 00:16:32 +0000 | [diff] [blame] | 1763 | W.printNumber("Characteristics", Table.Characteristics); |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | ErrorOr<const coff_resource_dir_entry &> |
| 1769 | COFFDumper::getResourceDirectoryTableEntry(const coff_resource_dir_table &Table, |
| 1770 | uint32_t Index) { |
Eric Beckmann | cd704cb | 2017-05-08 02:47:42 +0000 | [diff] [blame] | 1771 | if (Index >= (uint32_t)(Table.NumberOfNameEntries + Table.NumberOfIDEntries)) |
Eric Beckmann | efef15a | 2017-05-08 02:47:07 +0000 | [diff] [blame] | 1772 | return object_error::parse_failed; |
| 1773 | auto TablePtr = reinterpret_cast<const coff_resource_dir_entry *>(&Table + 1); |
| 1774 | return TablePtr[Index]; |
| 1775 | } |
| 1776 | |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 1777 | void COFFDumper::printStackMap() const { |
| 1778 | object::SectionRef StackMapSection; |
| 1779 | for (auto Sec : Obj->sections()) { |
| 1780 | StringRef Name; |
| 1781 | Sec.getName(Name); |
| 1782 | if (Name == ".llvm_stackmaps") { |
| 1783 | StackMapSection = Sec; |
| 1784 | break; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | if (StackMapSection == object::SectionRef()) |
| 1789 | return; |
| 1790 | |
| 1791 | StringRef StackMapContents; |
| 1792 | StackMapSection.getContents(StackMapContents); |
| 1793 | ArrayRef<uint8_t> StackMapContentsArray( |
| 1794 | reinterpret_cast<const uint8_t*>(StackMapContents.data()), |
| 1795 | StackMapContents.size()); |
| 1796 | |
| 1797 | if (Obj->isLittleEndian()) |
| 1798 | prettyPrintStackMap( |
| 1799 | llvm::outs(), |
Sanjoy Das | 23f06e5 | 2016-09-14 20:22:03 +0000 | [diff] [blame] | 1800 | StackMapV2Parser<support::little>(StackMapContentsArray)); |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 1801 | else |
| 1802 | prettyPrintStackMap(llvm::outs(), |
Sanjoy Das | 23f06e5 | 2016-09-14 20:22:03 +0000 | [diff] [blame] | 1803 | StackMapV2Parser<support::big>(StackMapContentsArray)); |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 1804 | } |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1805 | |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1806 | void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer, |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 1807 | llvm::codeview::TypeTableBuilder &IDTable, |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1808 | llvm::codeview::TypeTableBuilder &CVTypes) { |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1809 | // Flatten it first, then run our dumper on it. |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 1810 | SmallString<0> TypeBuf; |
Zachary Turner | 4efa0a4 | 2016-11-08 22:24:53 +0000 | [diff] [blame] | 1811 | CVTypes.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Record) { |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 1812 | TypeBuf.append(Record.begin(), Record.end()); |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1813 | }); |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 1814 | |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 1815 | TypeTableCollection TpiTypes(CVTypes.records()); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 1816 | { |
| 1817 | ListScope S(Writer, "MergedTypeStream"); |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 1818 | TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); |
| 1819 | error(codeview::visitTypeStream(TpiTypes, TDV)); |
| 1820 | Writer.flush(); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | // Flatten the id stream and print it next. The ID stream refers to names from |
| 1824 | // the type stream. |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 1825 | TypeTableCollection IpiTypes(IDTable.records()); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 1826 | { |
| 1827 | ListScope S(Writer, "MergedIDStream"); |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 1828 | TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); |
| 1829 | TDV.setIpiTypes(IpiTypes); |
| 1830 | error(codeview::visitTypeStream(IpiTypes, TDV)); |
| 1831 | Writer.flush(); |
Reid Kleckner | 0b26974 | 2016-05-14 00:02:53 +0000 | [diff] [blame] | 1832 | } |
| 1833 | } |