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