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