blob: 08031ae8f49c86631a94875474311b4c94309933 [file] [log] [blame]
Eric Christopher9cad53c2013-04-03 18:31:38 +00001//===-- COFFDumper.cpp - COFF-specific dumper -------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Christopher9cad53c2013-04-03 18:31:38 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file implements the COFF-specific dumper for llvm-readobj.
Eric Christopher9cad53c2013-04-03 18:31:38 +000011///
12//===----------------------------------------------------------------------===//
13
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000014#include "ARMWinEHPrinter.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000015#include "Error.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000016#include "ObjDumper.h"
Lang Hames0000afd2015-06-26 23:56:53 +000017#include "StackMapPrinter.h"
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +000018#include "Win64EHDumper.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000019#include "llvm-readobj.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallString.h"
Colin LeMahieu9fbffee2014-11-19 17:10:39 +000022#include "llvm/ADT/StringExtras.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000023#include "llvm/BinaryFormat/COFF.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000024#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Reid Kleckner6b3faef2016-01-13 23:44:57 +000025#include "llvm/DebugInfo/CodeView/CodeView.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000026#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
Zachary Turner591312c2017-05-30 17:13:33 +000027#include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000028#include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
29#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
Zachary Turner591312c2017-05-30 17:13:33 +000030#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000031#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Reid Klecknere9ab3492016-01-14 19:20:17 +000032#include "llvm/DebugInfo/CodeView/Line.h"
Zachary Turnerca6dbf12017-11-30 18:39:50 +000033#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
Zachary Turneraaad5742016-05-23 23:41:13 +000034#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
35#include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h"
36#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000037#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000038#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Zachary Turner376d4372017-12-05 23:58:18 +000039#include "llvm/DebugInfo/CodeView/TypeHashing.h"
Reid Kleckner6b3faef2016-01-13 23:44:57 +000040#include "llvm/DebugInfo/CodeView/TypeIndex.h"
41#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Reid Kleckner0b269742016-05-14 00:02:53 +000042#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000043#include "llvm/DebugInfo/CodeView/TypeTableCollection.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000044#include "llvm/Object/COFF.h"
45#include "llvm/Object/ObjectFile.h"
Nico Weber23cb79f2019-04-24 23:26:30 +000046#include "llvm/Object/WindowsResource.h"
Zachary Turner05bd9f32017-04-28 23:41:36 +000047#include "llvm/Support/BinaryStreamReader.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000048#include "llvm/Support/Casting.h"
49#include "llvm/Support/Compiler.h"
Zachary Turner0c60f262017-05-18 23:03:06 +000050#include "llvm/Support/ConvertUTF.h"
Zachary Turner05bd9f32017-04-28 23:41:36 +000051#include "llvm/Support/FormatVariadic.h"
Peter Collingbournebc3089f2018-08-22 23:58:16 +000052#include "llvm/Support/LEB128.h"
Fangrui Songef598752019-02-21 07:42:31 +000053#include "llvm/Support/ScopedPrinter.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000054#include "llvm/Support/Win64EH.h"
55#include "llvm/Support/raw_ostream.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000056
57using namespace llvm;
58using namespace llvm::object;
Reid Kleckner72e2ba72016-01-13 19:32:35 +000059using namespace llvm::codeview;
Reid Kleckner6b3faef2016-01-13 23:44:57 +000060using namespace llvm::support;
Eric Christopher9cad53c2013-04-03 18:31:38 +000061using namespace llvm::Win64EH;
62
George Rimar9693d282019-08-09 08:29:26 +000063static inline Error createError(const Twine &Err) {
64 return make_error<StringError>(Err, object_error::parse_failed);
65}
66
Eric Christopher9cad53c2013-04-03 18:31:38 +000067namespace {
68
Reid Klecknerb7d716c2017-06-22 01:10:29 +000069struct LoadConfigTables {
70 uint64_t SEHTableVA = 0;
71 uint64_t SEHTableCount = 0;
72 uint32_t GuardFlags = 0;
73 uint64_t GuardFidTableVA = 0;
74 uint64_t GuardFidTableCount = 0;
Reid Klecknerfd520962018-02-13 20:32:53 +000075 uint64_t GuardLJmpTableVA = 0;
76 uint64_t GuardLJmpTableCount = 0;
Reid Klecknerb7d716c2017-06-22 01:10:29 +000077};
78
Eric Christopher9cad53c2013-04-03 18:31:38 +000079class COFFDumper : public ObjDumper {
80public:
Zachary Turneraaad5742016-05-23 23:41:13 +000081 friend class COFFObjectDumpDelegate;
Zachary Turner88bb1632016-05-03 00:28:04 +000082 COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer)
Zachary Turner526f4f22017-05-19 19:26:58 +000083 : ObjDumper(Writer), Obj(Obj), Writer(Writer), Types(100) {}
Eric Christopher9cad53c2013-04-03 18:31:38 +000084
Craig Topperfd38cbe2014-08-30 16:48:34 +000085 void printFileHeaders() override;
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +000086 void printSectionHeaders() override;
Craig Topperfd38cbe2014-08-30 16:48:34 +000087 void printRelocations() override;
Craig Topperfd38cbe2014-08-30 16:48:34 +000088 void printUnwindInfo() override;
Petr Hosekad6f4572017-12-27 19:59:56 +000089
90 void printNeededLibraries() override;
91
Rui Ueyama1e152d52014-10-02 17:02:18 +000092 void printCOFFImports() override;
Saleem Abdulrasoolddd92642015-01-03 21:35:09 +000093 void printCOFFExports() override;
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +000094 void printCOFFDirectives() override;
Rui Ueyama74e85132014-11-19 00:18:07 +000095 void printCOFFBaseReloc() override;
Reid Kleckner2da433e2016-06-02 17:10:43 +000096 void printCOFFDebugDirectory() override;
Zachary Turner8d6396d2017-04-27 19:38:38 +000097 void printCOFFResources() override;
Reid Klecknerb7d716c2017-06-22 01:10:29 +000098 void printCOFFLoadConfig() override;
Reid Kleckner83ebad32015-12-16 18:28:12 +000099 void printCodeViewDebugInfo() override;
Alexandre Ganea120366e2019-02-07 15:24:18 +0000100 void mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
101 llvm::codeview::MergingTypeTableBuilder &CVTypes,
102 llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
103 llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
104 bool GHash) override;
Lang Hames0000afd2015-06-26 23:56:53 +0000105 void printStackMap() const override;
Peter Collingbournebc3089f2018-08-22 23:58:16 +0000106 void printAddrsig() override;
Rui Ueyamaa2923b22020-03-13 19:41:18 +0900107
Eric Christopher9cad53c2013-04-03 18:31:38 +0000108private:
James Henderson21ed8682019-01-23 16:15:39 +0000109 void printSymbols() override;
110 void printDynamicSymbols() override;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000111 void printSymbol(const SymbolRef &Sym);
Reid Kleckner1c7a9cd2016-01-14 17:51:57 +0000112 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc,
113 uint64_t Bias = 0);
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000114 void printDataDirectory(uint32_t Index, const std::string &FieldName);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000115
David Majnemer50267222014-11-05 06:24:35 +0000116 void printDOSHeader(const dos_header *DH);
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000117 template <class PEHeader> void printPEHeader(const PEHeader *Hdr);
118 void printBaseOfDataField(const pe32_header *Hdr);
119 void printBaseOfDataField(const pe32plus_header *Hdr);
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000120 template <typename T>
121 void printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables);
122 typedef void (*PrintExtraCB)(raw_ostream &, const uint8_t *);
123 void printRVATable(uint64_t TableVA, uint64_t Count, uint64_t EntrySize,
124 PrintExtraCB PrintExtra = 0);
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000125
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000126 void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section);
127 void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section);
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000128 StringRef getTypeName(TypeIndex Ty);
Reid Kleckner8e7275c2016-01-15 00:11:21 +0000129 StringRef getFileNameForFileOffset(uint32_t FileOffset);
130 void printFileNameForOffset(StringRef Label, uint32_t FileOffset);
Reid Kleckner5cb2b6b2016-05-02 20:30:47 +0000131 void printTypeIndex(StringRef FieldName, TypeIndex TI) {
132 // Forward to CVTypeDumper for simplicity.
Zachary Turner526f4f22017-05-19 19:26:58 +0000133 codeview::printTypeIndex(Writer, FieldName, TI, Types);
Reid Kleckner5cb2b6b2016-05-02 20:30:47 +0000134 }
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000135
Timur Iskhodzhanov56af52f2014-10-23 22:25:31 +0000136 void printCodeViewSymbolsSubsection(StringRef Subsection,
137 const SectionRef &Section,
Reid Kleckner2893fd12016-01-14 17:51:54 +0000138 StringRef SectionContents);
Timur Iskhodzhanov56af52f2014-10-23 22:25:31 +0000139
Reid Klecknerc31f5302016-01-15 18:06:25 +0000140 void printCodeViewFileChecksums(StringRef Subsection);
141
Reid Klecknere9ab3492016-01-14 19:20:17 +0000142 void printCodeViewInlineeLines(StringRef Subsection);
143
Reid Kleckner2893fd12016-01-14 17:51:54 +0000144 void printRelocatedField(StringRef Label, const coff_section *Sec,
Zachary Turnera78ecd12016-05-23 18:49:06 +0000145 uint32_t RelocOffset, uint32_t Offset,
146 StringRef *RelocSym = nullptr);
147
Eric Beckmann13017592017-06-13 00:16:32 +0000148 uint32_t countTotalTableEntries(ResourceSectionRef RSF,
149 const coff_resource_dir_table &Table,
150 StringRef Level);
151
Eric Beckmannefef15a2017-05-08 02:47:07 +0000152 void printResourceDirectoryTable(ResourceSectionRef RSF,
153 const coff_resource_dir_table &Table,
154 StringRef Level);
155
Reid Kleckner1c7a9cd2016-01-14 17:51:57 +0000156 void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec,
157 StringRef SectionContents, StringRef Block);
158
Reid Kleckner8e7275c2016-01-15 00:11:21 +0000159 /// Given a .debug$S section, find the string table and file checksum table.
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000160 void initializeFileAndStringTables(BinaryStreamReader &Reader);
Reid Kleckner8e7275c2016-01-15 00:11:21 +0000161
Eric Christopher9cad53c2013-04-03 18:31:38 +0000162 void cacheRelocations();
163
Rafael Espindola4453e42942014-06-13 03:07:50 +0000164 std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset,
165 SymbolRef &Sym);
166 std::error_code resolveSymbolName(const coff_section *Section,
167 uint64_t Offset, StringRef &Name);
Reid Kleckner2893fd12016-01-14 17:51:54 +0000168 std::error_code resolveSymbolName(const coff_section *Section,
169 StringRef SectionContents,
170 const void *RelocPtr, StringRef &Name);
Rui Ueyama979fb402014-10-09 02:16:38 +0000171 void printImportedSymbols(iterator_range<imported_symbol_iterator> Range);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +0000172 void printDelayImportedSymbols(
173 const DelayImportDirectoryEntryRef &I,
174 iterator_range<imported_symbol_iterator> Range);
Rui Ueyama15d99352014-10-03 00:41:58 +0000175
Eric Christopher9cad53c2013-04-03 18:31:38 +0000176 typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy;
177
178 const llvm::object::COFFObjectFile *Obj;
Rafael Espindola76d650e2015-07-06 14:26:07 +0000179 bool RelocCached = false;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000180 RelocMapTy RelocMap;
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000181
Zachary Turner591312c2017-05-30 17:13:33 +0000182 DebugChecksumsSubsectionRef CVFileChecksumTable;
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000183
Zachary Turner591312c2017-05-30 17:13:33 +0000184 DebugStringTableSubsectionRef CVStringTable;
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000185
Reid Klecknera6f64262018-09-11 22:00:50 +0000186 /// Track the compilation CPU type. S_COMPILE3 symbol records typically come
187 /// first, but if we don't see one, just assume an X64 CPU type. It is common.
188 CPUType CompilationCPUType = CPUType::X64;
189
Zachary Turner629cb7d2017-01-11 23:24:22 +0000190 ScopedPrinter &Writer;
Zachary Turner526f4f22017-05-19 19:26:58 +0000191 BinaryByteStream TypeContents;
192 LazyRandomTypeCollection Types;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000193};
194
Zachary Turneraaad5742016-05-23 23:41:13 +0000195class COFFObjectDumpDelegate : public SymbolDumpDelegate {
196public:
197 COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR,
198 const COFFObjectFile *Obj, StringRef SectionContents)
Zachary Turner537014c2016-05-24 03:32:34 +0000199 : CD(CD), SR(SR), SectionContents(SectionContents) {
Zachary Turneraaad5742016-05-23 23:41:13 +0000200 Sec = Obj->getCOFFSection(SR);
201 }
202
Zachary Turner120faca2017-02-27 22:11:43 +0000203 uint32_t getRecordOffset(BinaryStreamReader Reader) override {
Zachary Turner4d49eb92016-10-20 18:31:19 +0000204 ArrayRef<uint8_t> Data;
205 if (auto EC = Reader.readLongestContiguousChunk(Data)) {
206 llvm::consumeError(std::move(EC));
207 return 0;
208 }
209 return Data.data() - SectionContents.bytes_begin();
Zachary Turneraaad5742016-05-23 23:41:13 +0000210 }
211
212 void printRelocatedField(StringRef Label, uint32_t RelocOffset,
213 uint32_t Offset, StringRef *RelocSym) override {
214 CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym);
215 }
216
217 void printBinaryBlockWithRelocs(StringRef Label,
218 ArrayRef<uint8_t> Block) override {
219 StringRef SBlock(reinterpret_cast<const char *>(Block.data()),
220 Block.size());
221 if (opts::CodeViewSubsectionBytes)
222 CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock);
223 }
224
225 StringRef getFileNameForFileOffset(uint32_t FileOffset) override {
226 return CD.getFileNameForFileOffset(FileOffset);
227 }
228
Zachary Turner591312c2017-05-30 17:13:33 +0000229 DebugStringTableSubsectionRef getStringTable() override {
230 return CD.CVStringTable;
231 }
Zachary Turneraaad5742016-05-23 23:41:13 +0000232
233private:
234 COFFDumper &CD;
235 const SectionRef &SR;
Zachary Turneraaad5742016-05-23 23:41:13 +0000236 const coff_section *Sec;
237 StringRef SectionContents;
238};
239
Reid Kleckner4a14bca2016-05-05 00:34:33 +0000240} // end namespace
Eric Christopher9cad53c2013-04-03 18:31:38 +0000241
242namespace llvm {
243
Rafael Espindola4453e42942014-06-13 03:07:50 +0000244std::error_code createCOFFDumper(const object::ObjectFile *Obj,
Zachary Turner88bb1632016-05-03 00:28:04 +0000245 ScopedPrinter &Writer,
Rafael Espindola4453e42942014-06-13 03:07:50 +0000246 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000247 const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj);
248 if (!COFFObj)
249 return readobj_error::unsupported_obj_file_format;
250
251 Result.reset(new COFFDumper(COFFObj, Writer));
252 return readobj_error::success;
253}
254
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000255} // namespace llvm
Eric Christopher9cad53c2013-04-03 18:31:38 +0000256
Hiroshi Inoue7f9f92f2018-02-22 07:48:29 +0000257// Given a section and an offset into this section the function returns the
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000258// symbol used for the relocation at the offset.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000259std::error_code COFFDumper::resolveSymbol(const coff_section *Section,
260 uint64_t Offset, SymbolRef &Sym) {
Rafael Espindola76d650e2015-07-06 14:26:07 +0000261 cacheRelocations();
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000262 const auto &Relocations = RelocMap[Section];
David Majnemer61f17982016-05-29 06:18:08 +0000263 auto SymI = Obj->symbol_end();
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000264 for (const auto &Relocation : Relocations) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000265 uint64_t RelocationOffset = Relocation.getOffset();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000266
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000267 if (RelocationOffset == Offset) {
David Majnemer61f17982016-05-29 06:18:08 +0000268 SymI = Relocation.getSymbol();
269 break;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000270 }
271 }
David Majnemer61f17982016-05-29 06:18:08 +0000272 if (SymI == Obj->symbol_end())
273 return readobj_error::unknown_symbol;
274 Sym = *SymI;
275 return readobj_error::success;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000276}
277
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000278// Given a section and an offset into this section the function returns the name
279// of the symbol used for the relocation at the offset.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000280std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
281 uint64_t Offset,
282 StringRef &Name) {
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000283 SymbolRef Symbol;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000284 if (std::error_code EC = resolveSymbol(Section, Offset, Symbol))
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000285 return EC;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000286 Expected<StringRef> NameOrErr = Symbol.getName();
287 if (!NameOrErr)
288 return errorToErrorCode(NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000289 Name = *NameOrErr;
Rui Ueyama7d099192015-06-09 15:20:42 +0000290 return std::error_code();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000291}
292
Reid Kleckner2893fd12016-01-14 17:51:54 +0000293// Helper for when you have a pointer to real data and you want to know about
294// relocations against it.
295std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
296 StringRef SectionContents,
297 const void *RelocPtr,
298 StringRef &Name) {
299 assert(SectionContents.data() < RelocPtr &&
300 RelocPtr < SectionContents.data() + SectionContents.size() &&
301 "pointer to relocated object is not in section");
302 uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) -
303 SectionContents.data());
304 return resolveSymbolName(Section, Offset, Name);
305}
306
307void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec,
Zachary Turnera78ecd12016-05-23 18:49:06 +0000308 uint32_t RelocOffset, uint32_t Offset,
309 StringRef *RelocSym) {
310 StringRef SymStorage;
311 StringRef &Symbol = RelocSym ? *RelocSym : SymStorage;
312 if (!resolveSymbolName(Sec, RelocOffset, Symbol))
313 W.printSymbolOffset(Label, Symbol, Offset);
314 else
315 W.printHex(Label, RelocOffset);
316}
317
Reid Kleckner1c7a9cd2016-01-14 17:51:57 +0000318void COFFDumper::printBinaryBlockWithRelocs(StringRef Label,
319 const SectionRef &Sec,
320 StringRef SectionContents,
321 StringRef Block) {
322 W.printBinaryBlock(Label, Block);
323
324 assert(SectionContents.begin() < Block.begin() &&
325 SectionContents.end() >= Block.end() &&
326 "Block is not contained in SectionContents");
327 uint64_t OffsetStart = Block.data() - SectionContents.data();
328 uint64_t OffsetEnd = OffsetStart + Block.size();
329
Zachary Turneraaad5742016-05-23 23:41:13 +0000330 W.flush();
Reid Kleckner1c7a9cd2016-01-14 17:51:57 +0000331 cacheRelocations();
332 ListScope D(W, "BlockRelocations");
333 const coff_section *Section = Obj->getCOFFSection(Sec);
334 const auto &Relocations = RelocMap[Section];
335 for (const auto &Relocation : Relocations) {
336 uint64_t RelocationOffset = Relocation.getOffset();
337 if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd)
338 printRelocation(Sec, Relocation, OffsetStart);
339 }
340}
341
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000342static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000343 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ),
344 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ),
345 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ),
346 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ),
Martin Storsjo43c85452017-06-30 07:02:13 +0000347 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM64 ),
Saleem Abdulrasool5e1780e2014-03-11 03:08:37 +0000348 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ),
Eric Christopher9cad53c2013-04-03 18:31:38 +0000349 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ),
350 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ),
351 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ),
352 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ),
353 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ),
354 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ),
355 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16),
356 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ),
357 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP),
358 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ),
359 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ),
360 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ),
361 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ),
362 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ),
363 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ),
364 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2)
365};
366
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000367static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000368 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ),
369 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ),
370 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ),
371 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ),
372 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ),
373 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ),
374 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ),
375 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ),
376 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ),
377 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP),
378 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ),
379 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ),
380 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ),
381 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ),
382 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI )
383};
384
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000385static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000386 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ),
387 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ),
388 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ),
389 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ),
390 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ),
391 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ),
392 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ),
393 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER),
394 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ),
395 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ),
396 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ),
397};
398
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000399static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = {
Rui Ueyama06dc5e72014-01-27 04:22:24 +0000400 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ),
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000401 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ),
402 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ),
403 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ),
404 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ),
405 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ),
406 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ),
Saleem Abdulrasoold90f86d2014-06-27 03:11:18 +0000407 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ),
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000408 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ),
Saleem Abdulrasoold90f86d2014-06-27 03:11:18 +0000409 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ),
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000410 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE),
411};
412
Rui Ueyamaa2923b22020-03-13 19:41:18 +0900413static const EnumEntry<COFF::ExtendedDLLCharacteristics>
414 PEExtendedDLLCharacteristics[] = {
415 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT),
416};
417
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000418static const EnumEntry<COFF::SectionCharacteristics>
Eric Christopher9cad53c2013-04-03 18:31:38 +0000419ImageSectionCharacteristics[] = {
David Majnemerd3238882015-07-30 16:47:56 +0000420 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD ),
Eric Christopher9cad53c2013-04-03 18:31:38 +0000421 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ),
422 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ),
423 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ),
424 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA),
425 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ),
426 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ),
427 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ),
428 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ),
429 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ),
430 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ),
431 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ),
432 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ),
433 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ),
434 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ),
435 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ),
436 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ),
437 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ),
438 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ),
439 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ),
440 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ),
441 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ),
442 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ),
443 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ),
444 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ),
445 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ),
446 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ),
447 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ),
448 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ),
449 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ),
450 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ),
451 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ),
452 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ),
453 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ),
454 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ),
455 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE )
456};
457
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000458static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000459 { "Null" , COFF::IMAGE_SYM_TYPE_NULL },
460 { "Void" , COFF::IMAGE_SYM_TYPE_VOID },
461 { "Char" , COFF::IMAGE_SYM_TYPE_CHAR },
462 { "Short" , COFF::IMAGE_SYM_TYPE_SHORT },
463 { "Int" , COFF::IMAGE_SYM_TYPE_INT },
464 { "Long" , COFF::IMAGE_SYM_TYPE_LONG },
465 { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT },
466 { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE },
467 { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT },
468 { "Union" , COFF::IMAGE_SYM_TYPE_UNION },
469 { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM },
470 { "MOE" , COFF::IMAGE_SYM_TYPE_MOE },
471 { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE },
472 { "Word" , COFF::IMAGE_SYM_TYPE_WORD },
473 { "UInt" , COFF::IMAGE_SYM_TYPE_UINT },
474 { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD }
475};
476
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000477static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000478 { "Null" , COFF::IMAGE_SYM_DTYPE_NULL },
479 { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER },
480 { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION },
481 { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY }
482};
483
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000484static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000485 { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION },
486 { "Null" , COFF::IMAGE_SYM_CLASS_NULL },
487 { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC },
488 { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL },
489 { "Static" , COFF::IMAGE_SYM_CLASS_STATIC },
490 { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER },
491 { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF },
492 { "Label" , COFF::IMAGE_SYM_CLASS_LABEL },
493 { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL },
494 { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT },
495 { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT },
496 { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG },
497 { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION },
498 { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG },
499 { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION },
500 { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC },
501 { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG },
502 { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM },
503 { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM },
504 { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD },
505 { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK },
506 { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION },
507 { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT },
508 { "File" , COFF::IMAGE_SYM_CLASS_FILE },
509 { "Section" , COFF::IMAGE_SYM_CLASS_SECTION },
510 { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL },
511 { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN }
512};
513
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000514static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000515 { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES },
516 { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY },
517 { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE },
518 { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH },
519 { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE },
520 { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST },
521 { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST }
522};
523
Reid Kleckner2da433e2016-06-02 17:10:43 +0000524static const EnumEntry<COFF::DebugType> ImageDebugType[] = {
Rui Ueyamaa2923b22020-03-13 19:41:18 +0900525 {"Unknown", COFF::IMAGE_DEBUG_TYPE_UNKNOWN},
526 {"COFF", COFF::IMAGE_DEBUG_TYPE_COFF},
527 {"CodeView", COFF::IMAGE_DEBUG_TYPE_CODEVIEW},
528 {"FPO", COFF::IMAGE_DEBUG_TYPE_FPO},
529 {"Misc", COFF::IMAGE_DEBUG_TYPE_MISC},
530 {"Exception", COFF::IMAGE_DEBUG_TYPE_EXCEPTION},
531 {"Fixup", COFF::IMAGE_DEBUG_TYPE_FIXUP},
532 {"OmapToSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_TO_SRC},
533 {"OmapFromSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC},
534 {"Borland", COFF::IMAGE_DEBUG_TYPE_BORLAND},
535 {"Reserved10", COFF::IMAGE_DEBUG_TYPE_RESERVED10},
536 {"CLSID", COFF::IMAGE_DEBUG_TYPE_CLSID},
537 {"VCFeature", COFF::IMAGE_DEBUG_TYPE_VC_FEATURE},
538 {"POGO", COFF::IMAGE_DEBUG_TYPE_POGO},
539 {"ILTCG", COFF::IMAGE_DEBUG_TYPE_ILTCG},
540 {"MPX", COFF::IMAGE_DEBUG_TYPE_MPX},
541 {"Repro", COFF::IMAGE_DEBUG_TYPE_REPRO},
542 {"ExtendedDLLCharacteristics",
543 COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS},
Reid Kleckner2da433e2016-06-02 17:10:43 +0000544};
545
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000546static const EnumEntry<COFF::WeakExternalCharacteristics>
Eric Christopher9cad53c2013-04-03 18:31:38 +0000547WeakExternalCharacteristics[] = {
548 { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY },
549 { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY },
550 { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS }
551};
552
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000553static const EnumEntry<uint32_t> SubSectionTypes[] = {
Zachary Turner8c099fe2017-05-30 16:36:15 +0000554 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Symbols),
555 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, Lines),
556 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, StringTable),
557 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FileChecksums),
558 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FrameData),
559 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, InlineeLines),
560 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeImports),
561 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CrossScopeExports),
562 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, ILLines),
563 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, FuncMDTokenMap),
564 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, TypeMDTokenMap),
565 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, MergedAssemblyInput),
566 LLVM_READOBJ_ENUM_CLASS_ENT(DebugSubsectionKind, CoffSymbolRVA),
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000567};
568
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000569static const EnumEntry<uint32_t> FrameDataFlags[] = {
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000570 LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH),
571 LLVM_READOBJ_ENUM_ENT(FrameData, HasEH),
572 LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart),
573};
574
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000575static const EnumEntry<uint8_t> FileChecksumKindNames[] = {
Reid Klecknerc31f5302016-01-15 18:06:25 +0000576 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None),
577 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5),
578 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1),
579 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256),
580};
581
Rafael Espindola4453e42942014-06-13 03:07:50 +0000582template <typename T>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000583static std::error_code getSymbolAuxData(const COFFObjectFile *Obj,
David Majnemer44f51e52014-09-10 12:51:52 +0000584 COFFSymbolRef Symbol,
585 uint8_t AuxSymbolIdx, const T *&Aux) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000586 ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol);
David Majnemer44f51e52014-09-10 12:51:52 +0000587 AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize());
Eric Christopher9cad53c2013-04-03 18:31:38 +0000588 Aux = reinterpret_cast<const T*>(AuxData.data());
589 return readobj_error::success;
590}
591
Eric Christopher9cad53c2013-04-03 18:31:38 +0000592void COFFDumper::cacheRelocations() {
Rafael Espindola76d650e2015-07-06 14:26:07 +0000593 if (RelocCached)
594 return;
595 RelocCached = true;
596
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000597 for (const SectionRef &S : Obj->sections()) {
598 const coff_section *Section = Obj->getCOFFSection(S);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000599
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000600 for (const RelocationRef &Reloc : S.relocations())
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000601 RelocMap[Section].push_back(Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000602
603 // Sort relocations by address.
Fangrui Songfacbfe02019-05-02 10:49:27 +0000604 llvm::sort(RelocMap[Section], [](RelocationRef L, RelocationRef R) {
605 return L.getOffset() < R.getOffset();
606 });
Eric Christopher9cad53c2013-04-03 18:31:38 +0000607 }
608}
609
Nico Weberae73e1f2019-04-26 11:44:10 +0000610void COFFDumper::printDataDirectory(uint32_t Index,
611 const std::string &FieldName) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000612 const data_directory *Data;
613 if (Obj->getDataDirectory(Index, Data))
614 return;
615 W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress);
616 W.printHex(FieldName + "Size", Data->Size);
617}
618
Eric Christopher9cad53c2013-04-03 18:31:38 +0000619void COFFDumper::printFileHeaders() {
David Majnemer44f51e52014-09-10 12:51:52 +0000620 time_t TDS = Obj->getTimeDateStamp();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000621 char FormattedTime[20] = { };
622 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
623
624 {
625 DictScope D(W, "ImageFileHeader");
David Majnemer44f51e52014-09-10 12:51:52 +0000626 W.printEnum ("Machine", Obj->getMachine(),
Eric Christopher9cad53c2013-04-03 18:31:38 +0000627 makeArrayRef(ImageFileMachineType));
David Majnemer44f51e52014-09-10 12:51:52 +0000628 W.printNumber("SectionCount", Obj->getNumberOfSections());
629 W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp());
630 W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable());
631 W.printNumber("SymbolCount", Obj->getNumberOfSymbols());
632 W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader());
633 W.printFlags ("Characteristics", Obj->getCharacteristics(),
Eric Christopher9cad53c2013-04-03 18:31:38 +0000634 makeArrayRef(ImageFileCharacteristics));
635 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000636
637 // Print PE header. This header does not exist if this is an object file and
638 // not an executable.
George Rimar9d5e8a42019-08-19 14:32:23 +0000639 if (const pe32_header *PEHeader = Obj->getPE32Header())
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000640 printPEHeader<pe32_header>(PEHeader);
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000641
George Rimar9d5e8a42019-08-19 14:32:23 +0000642 if (const pe32plus_header *PEPlusHeader = Obj->getPE32PlusHeader())
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000643 printPEHeader<pe32plus_header>(PEPlusHeader);
David Majnemer50267222014-11-05 06:24:35 +0000644
645 if (const dos_header *DH = Obj->getDOSHeader())
646 printDOSHeader(DH);
647}
648
649void COFFDumper::printDOSHeader(const dos_header *DH) {
650 DictScope D(W, "DOSHeader");
651 W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic)));
652 W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage);
653 W.printNumber("FileSizeInPages", DH->FileSizeInPages);
654 W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems);
655 W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs);
656 W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs);
657 W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs);
658 W.printNumber("InitialRelativeSS", DH->InitialRelativeSS);
659 W.printNumber("InitialSP", DH->InitialSP);
660 W.printNumber("Checksum", DH->Checksum);
661 W.printNumber("InitialIP", DH->InitialIP);
662 W.printNumber("InitialRelativeCS", DH->InitialRelativeCS);
663 W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable);
664 W.printNumber("OverlayNumber", DH->OverlayNumber);
665 W.printNumber("OEMid", DH->OEMid);
666 W.printNumber("OEMinfo", DH->OEMinfo);
667 W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader);
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000668}
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000669
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000670template <class PEHeader>
671void COFFDumper::printPEHeader(const PEHeader *Hdr) {
672 DictScope D(W, "ImageOptionalHeader");
Martin Storsjo8ae07ac2017-06-30 07:02:04 +0000673 W.printHex ("Magic", Hdr->Magic);
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000674 W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion);
675 W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion);
676 W.printNumber("SizeOfCode", Hdr->SizeOfCode);
677 W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData);
678 W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData);
679 W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint);
680 W.printHex ("BaseOfCode", Hdr->BaseOfCode);
681 printBaseOfDataField(Hdr);
682 W.printHex ("ImageBase", Hdr->ImageBase);
683 W.printNumber("SectionAlignment", Hdr->SectionAlignment);
684 W.printNumber("FileAlignment", Hdr->FileAlignment);
685 W.printNumber("MajorOperatingSystemVersion",
686 Hdr->MajorOperatingSystemVersion);
687 W.printNumber("MinorOperatingSystemVersion",
688 Hdr->MinorOperatingSystemVersion);
689 W.printNumber("MajorImageVersion", Hdr->MajorImageVersion);
690 W.printNumber("MinorImageVersion", Hdr->MinorImageVersion);
691 W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion);
692 W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion);
693 W.printNumber("SizeOfImage", Hdr->SizeOfImage);
694 W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders);
695 W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem));
David Majnemer774aadf2014-11-18 02:45:28 +0000696 W.printFlags ("Characteristics", Hdr->DLLCharacteristics,
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000697 makeArrayRef(PEDLLCharacteristics));
698 W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve);
699 W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit);
700 W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve);
701 W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit);
702 W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize);
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000703
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000704 if (Hdr->NumberOfRvaAndSize > 0) {
705 DictScope D(W, "DataDirectory");
706 static const char * const directory[] = {
707 "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable",
708 "CertificateTable", "BaseRelocationTable", "Debug", "Architecture",
709 "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT",
710 "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved"
711 };
712
Reid Kleckner2da433e2016-06-02 17:10:43 +0000713 for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i)
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000714 printDataDirectory(i, directory[i]);
Reid Kleckner2da433e2016-06-02 17:10:43 +0000715 }
716}
717
718void COFFDumper::printCOFFDebugDirectory() {
719 ListScope LS(W, "DebugDirectory");
720 for (const debug_directory &D : Obj->debug_directories()) {
721 char FormattedTime[20] = {};
722 time_t TDS = D.TimeDateStamp;
723 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
724 DictScope S(W, "DebugEntry");
725 W.printHex("Characteristics", D.Characteristics);
726 W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp);
727 W.printHex("MajorVersion", D.MajorVersion);
728 W.printHex("MinorVersion", D.MinorVersion);
729 W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType));
730 W.printHex("SizeOfData", D.SizeOfData);
731 W.printHex("AddressOfRawData", D.AddressOfRawData);
732 W.printHex("PointerToRawData", D.PointerToRawData);
Martin Storsjö239fcda2020-04-27 14:55:31 +0300733 // Ideally, if D.AddressOfRawData == 0, we should try to load the payload
734 // using D.PointerToRawData instead.
735 if (D.AddressOfRawData == 0)
736 continue;
Reid Kleckner2da433e2016-06-02 17:10:43 +0000737 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) {
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000738 const codeview::DebugInfo *DebugInfo;
Reid Kleckner2da433e2016-06-02 17:10:43 +0000739 StringRef PDBFileName;
George Rimare3fb2d52019-08-17 16:07:18 +0000740 if (std::error_code EC = Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName))
George Rimare54d3712019-08-22 08:56:24 +0000741 reportError(errorCodeToError(EC), Obj->getFileName());
George Rimare3fb2d52019-08-17 16:07:18 +0000742
Reid Kleckner2da433e2016-06-02 17:10:43 +0000743 DictScope PDBScope(W, "PDBInfo");
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000744 W.printHex("PDBSignature", DebugInfo->Signature.CVSignature);
745 if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) {
746 W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature));
747 W.printNumber("PDBAge", DebugInfo->PDB70.Age);
748 W.printString("PDBFileName", PDBFileName);
749 }
Nico Weber893c6462018-09-05 18:01:04 +0000750 } else if (D.SizeOfData != 0) {
Rui Ueyamaa2923b22020-03-13 19:41:18 +0900751 // FIXME: Data visualization for IMAGE_DEBUG_TYPE_VC_FEATURE and
752 // IMAGE_DEBUG_TYPE_POGO?
Reid Kleckner2da433e2016-06-02 17:10:43 +0000753 ArrayRef<uint8_t> RawData;
George Rimare3fb2d52019-08-17 16:07:18 +0000754 if (std::error_code EC = Obj->getRvaAndSizeAsBytes(D.AddressOfRawData,
755 D.SizeOfData, RawData))
George Rimare54d3712019-08-22 08:56:24 +0000756 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyamaa2923b22020-03-13 19:41:18 +0900757 if (D.Type == COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS) {
758 // FIXME right now the only possible value would fit in 8 bits,
759 // but that might change in the future
760 uint16_t Characteristics = RawData[0];
761 W.printFlags("ExtendedCharacteristics", Characteristics,
762 makeArrayRef(PEExtendedDLLCharacteristics));
763 }
Reid Kleckner2da433e2016-06-02 17:10:43 +0000764 W.printBinaryBlock("RawData", RawData);
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000765 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000766 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000767}
768
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000769void COFFDumper::printRVATable(uint64_t TableVA, uint64_t Count,
770 uint64_t EntrySize, PrintExtraCB PrintExtra) {
771 uintptr_t TableStart, TableEnd;
George Rimare3fb2d52019-08-17 16:07:18 +0000772 if (std::error_code EC = Obj->getVaPtr(TableVA, TableStart))
George Rimare54d3712019-08-22 08:56:24 +0000773 reportError(errorCodeToError(EC), Obj->getFileName());
George Rimare3fb2d52019-08-17 16:07:18 +0000774 if (std::error_code EC =
775 Obj->getVaPtr(TableVA + Count * EntrySize - 1, TableEnd))
George Rimare54d3712019-08-22 08:56:24 +0000776 reportError(errorCodeToError(EC), Obj->getFileName());
Reid Kleckner45cb4fe2017-06-23 22:12:11 +0000777 TableEnd++;
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000778 for (uintptr_t I = TableStart; I < TableEnd; I += EntrySize) {
779 uint32_t RVA = *reinterpret_cast<const ulittle32_t *>(I);
780 raw_ostream &OS = W.startLine();
Reid Kleckner4386bc52018-01-23 23:17:06 +0000781 OS << W.hex(Obj->getImageBase() + RVA);
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000782 if (PrintExtra)
783 PrintExtra(OS, reinterpret_cast<const uint8_t *>(I));
784 OS << '\n';
785 }
786}
787
788void COFFDumper::printCOFFLoadConfig() {
789 LoadConfigTables Tables;
790 if (Obj->is64())
791 printCOFFLoadConfig(Obj->getLoadConfig64(), Tables);
792 else
793 printCOFFLoadConfig(Obj->getLoadConfig32(), Tables);
794
795 if (Tables.SEHTableVA) {
796 ListScope LS(W, "SEHTable");
797 printRVATable(Tables.SEHTableVA, Tables.SEHTableCount, 4);
798 }
799
800 if (Tables.GuardFidTableVA) {
801 ListScope LS(W, "GuardFidTable");
802 if (Tables.GuardFlags & uint32_t(coff_guard_flags::FidTableHasFlags)) {
803 auto PrintGuardFlags = [](raw_ostream &OS, const uint8_t *Entry) {
804 uint8_t Flags = *reinterpret_cast<const uint8_t *>(Entry + 4);
805 if (Flags)
806 OS << " flags " << utohexstr(Flags);
807 };
808 printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 5,
809 PrintGuardFlags);
810 } else {
811 printRVATable(Tables.GuardFidTableVA, Tables.GuardFidTableCount, 4);
812 }
813 }
Reid Klecknerfd520962018-02-13 20:32:53 +0000814
815 if (Tables.GuardLJmpTableVA) {
816 ListScope LS(W, "GuardLJmpTable");
817 printRVATable(Tables.GuardLJmpTableVA, Tables.GuardLJmpTableCount, 4);
818 }
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000819}
820
821template <typename T>
822void COFFDumper::printCOFFLoadConfig(const T *Conf, LoadConfigTables &Tables) {
Reid Kleckner45cb4fe2017-06-23 22:12:11 +0000823 if (!Conf)
824 return;
825
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000826 ListScope LS(W, "LoadConfig");
827 char FormattedTime[20] = {};
828 time_t TDS = Conf->TimeDateStamp;
829 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
830 W.printHex("Size", Conf->Size);
831
832 // Print everything before SecurityCookie. The vast majority of images today
833 // have all these fields.
834 if (Conf->Size < offsetof(T, SEHandlerTable))
835 return;
836 W.printHex("TimeDateStamp", FormattedTime, TDS);
837 W.printHex("MajorVersion", Conf->MajorVersion);
838 W.printHex("MinorVersion", Conf->MinorVersion);
839 W.printHex("GlobalFlagsClear", Conf->GlobalFlagsClear);
840 W.printHex("GlobalFlagsSet", Conf->GlobalFlagsSet);
841 W.printHex("CriticalSectionDefaultTimeout",
842 Conf->CriticalSectionDefaultTimeout);
843 W.printHex("DeCommitFreeBlockThreshold", Conf->DeCommitFreeBlockThreshold);
844 W.printHex("DeCommitTotalFreeThreshold", Conf->DeCommitTotalFreeThreshold);
845 W.printHex("LockPrefixTable", Conf->LockPrefixTable);
846 W.printHex("MaximumAllocationSize", Conf->MaximumAllocationSize);
847 W.printHex("VirtualMemoryThreshold", Conf->VirtualMemoryThreshold);
848 W.printHex("ProcessHeapFlags", Conf->ProcessHeapFlags);
849 W.printHex("ProcessAffinityMask", Conf->ProcessAffinityMask);
850 W.printHex("CSDVersion", Conf->CSDVersion);
851 W.printHex("DependentLoadFlags", Conf->DependentLoadFlags);
852 W.printHex("EditList", Conf->EditList);
853 W.printHex("SecurityCookie", Conf->SecurityCookie);
854
855 // Print the safe SEH table if present.
856 if (Conf->Size < offsetof(coff_load_configuration32, GuardCFCheckFunction))
857 return;
858 W.printHex("SEHandlerTable", Conf->SEHandlerTable);
859 W.printNumber("SEHandlerCount", Conf->SEHandlerCount);
860
861 Tables.SEHTableVA = Conf->SEHandlerTable;
862 Tables.SEHTableCount = Conf->SEHandlerCount;
863
864 // Print everything before CodeIntegrity. (2015)
865 if (Conf->Size < offsetof(T, CodeIntegrity))
866 return;
867 W.printHex("GuardCFCheckFunction", Conf->GuardCFCheckFunction);
868 W.printHex("GuardCFCheckDispatch", Conf->GuardCFCheckDispatch);
869 W.printHex("GuardCFFunctionTable", Conf->GuardCFFunctionTable);
870 W.printNumber("GuardCFFunctionCount", Conf->GuardCFFunctionCount);
871 W.printHex("GuardFlags", Conf->GuardFlags);
872
873 Tables.GuardFidTableVA = Conf->GuardCFFunctionTable;
874 Tables.GuardFidTableCount = Conf->GuardCFFunctionCount;
875 Tables.GuardFlags = Conf->GuardFlags;
876
877 // Print the rest. (2017)
878 if (Conf->Size < sizeof(T))
879 return;
880 W.printHex("GuardAddressTakenIatEntryTable",
881 Conf->GuardAddressTakenIatEntryTable);
882 W.printNumber("GuardAddressTakenIatEntryCount",
883 Conf->GuardAddressTakenIatEntryCount);
884 W.printHex("GuardLongJumpTargetTable", Conf->GuardLongJumpTargetTable);
885 W.printNumber("GuardLongJumpTargetCount", Conf->GuardLongJumpTargetCount);
886 W.printHex("DynamicValueRelocTable", Conf->DynamicValueRelocTable);
887 W.printHex("CHPEMetadataPointer", Conf->CHPEMetadataPointer);
888 W.printHex("GuardRFFailureRoutine", Conf->GuardRFFailureRoutine);
889 W.printHex("GuardRFFailureRoutineFunctionPointer",
890 Conf->GuardRFFailureRoutineFunctionPointer);
891 W.printHex("DynamicValueRelocTableOffset",
892 Conf->DynamicValueRelocTableOffset);
893 W.printNumber("DynamicValueRelocTableSection",
894 Conf->DynamicValueRelocTableSection);
895 W.printHex("GuardRFVerifyStackPointerFunctionPointer",
896 Conf->GuardRFVerifyStackPointerFunctionPointer);
897 W.printHex("HotPatchTableOffset", Conf->HotPatchTableOffset);
Reid Klecknerfd520962018-02-13 20:32:53 +0000898
899 Tables.GuardLJmpTableVA = Conf->GuardLongJumpTargetTable;
900 Tables.GuardLJmpTableCount = Conf->GuardLongJumpTargetCount;
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000901}
902
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000903void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) {
904 W.printHex("BaseOfData", Hdr->BaseOfData);
905}
906
907void COFFDumper::printBaseOfDataField(const pe32plus_header *) {}
908
Reid Kleckner83ebad32015-12-16 18:28:12 +0000909void COFFDumper::printCodeViewDebugInfo() {
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000910 // Print types first to build CVUDTNames, then print symbols.
Reid Kleckner83ebad32015-12-16 18:28:12 +0000911 for (const SectionRef &S : Obj->sections()) {
George Rimarbcc00e12019-08-14 11:10:11 +0000912 StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName());
Zachary Turner5ca44e32018-04-05 18:18:12 +0000913 // .debug$T is a standard CodeView type section, while .debug$P is the same
914 // format but used for MSVC precompiled header object files.
915 if (SectionName == ".debug$T" || SectionName == ".debug$P")
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000916 printCodeViewTypeSection(SectionName, S);
917 }
918 for (const SectionRef &S : Obj->sections()) {
George Rimarbcc00e12019-08-14 11:10:11 +0000919 StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName());
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000920 if (SectionName == ".debug$S")
921 printCodeViewSymbolSection(SectionName, S);
Reid Kleckner83ebad32015-12-16 18:28:12 +0000922 }
923}
924
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000925void COFFDumper::initializeFileAndStringTables(BinaryStreamReader &Reader) {
926 while (Reader.bytesRemaining() > 0 &&
927 (!CVFileChecksumTable.valid() || !CVStringTable.valid())) {
Reid Kleckner8e7275c2016-01-15 00:11:21 +0000928 // The section consists of a number of subsection in the following format:
929 // |SubSectionType|SubSectionSize|Contents...|
930 uint32_t SubType, SubSectionSize;
George Rimar36f23182019-08-13 12:07:41 +0000931
932 if (Error E = Reader.readInteger(SubType))
933 reportError(std::move(E), Obj->getFileName());
934 if (Error E = Reader.readInteger(SubSectionSize))
935 reportError(std::move(E), Obj->getFileName());
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000936
937 StringRef Contents;
George Rimar36f23182019-08-13 12:07:41 +0000938 if (Error E = Reader.readFixedString(Contents, SubSectionSize))
939 reportError(std::move(E), Obj->getFileName());
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000940
Zachary Turner591312c2017-05-30 17:13:33 +0000941 BinaryStreamRef ST(Contents, support::little);
Zachary Turner8c099fe2017-05-30 16:36:15 +0000942 switch (DebugSubsectionKind(SubType)) {
Zachary Turner591312c2017-05-30 17:13:33 +0000943 case DebugSubsectionKind::FileChecksums:
George Rimar36f23182019-08-13 12:07:41 +0000944 if (Error E = CVFileChecksumTable.initialize(ST))
945 reportError(std::move(E), Obj->getFileName());
Reid Kleckner8e7275c2016-01-15 00:11:21 +0000946 break;
Zachary Turner591312c2017-05-30 17:13:33 +0000947 case DebugSubsectionKind::StringTable:
George Rimar36f23182019-08-13 12:07:41 +0000948 if (Error E = CVStringTable.initialize(ST))
949 reportError(std::move(E), Obj->getFileName());
Zachary Turner591312c2017-05-30 17:13:33 +0000950 break;
Reid Kleckner8e7275c2016-01-15 00:11:21 +0000951 default:
952 break;
953 }
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000954
David Majnemerefaaf412016-05-28 20:04:48 +0000955 uint32_t PaddedSize = alignTo(SubSectionSize, 4);
George Rimar36f23182019-08-13 12:07:41 +0000956 if (Error E = Reader.skip(PaddedSize - SubSectionSize))
957 reportError(std::move(E), Obj->getFileName());
Reid Kleckner8e7275c2016-01-15 00:11:21 +0000958 }
959}
960
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000961void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
962 const SectionRef &Section) {
George Rimare3d81fd2019-08-09 10:53:12 +0000963 StringRef SectionContents =
964 unwrapOrError(Obj->getFileName(), Section.getContents());
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000965 StringRef Data = SectionContents;
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000966
967 SmallVector<StringRef, 10> FunctionNames;
968 StringMap<StringRef> FunctionLineTables;
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000969
Zachary Turner99f02152015-02-18 19:32:05 +0000970 ListScope D(W, "CodeViewDebugInfo");
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000971 // Print the section to allow correlation with printSectionHeaders.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000972 W.printNumber("Section", SectionName, Obj->getSectionID(Section));
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000973
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000974 uint32_t Magic;
George Rimar36f23182019-08-13 12:07:41 +0000975 if (Error E = consume(Data, Magic))
976 reportError(std::move(E), Obj->getFileName());
977
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000978 W.printHex("Magic", Magic);
979 if (Magic != COFF::DEBUG_SECTION_MAGIC)
George Rimare54d3712019-08-22 08:56:24 +0000980 reportError(errorCodeToError(object_error::parse_failed),
981 Obj->getFileName());
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000982
Zachary Turnerd9a62632017-05-17 20:23:31 +0000983 BinaryStreamReader FSReader(Data, support::little);
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000984 initializeFileAndStringTables(FSReader);
Reid Kleckner8e7275c2016-01-15 00:11:21 +0000985
Zachary Turnera96cce62016-06-03 03:25:59 +0000986 // TODO: Convert this over to using ModuleSubstreamVisitor.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000987 while (!Data.empty()) {
988 // The section consists of a number of subsection in the following format:
989 // |SubSectionType|SubSectionSize|Contents...|
990 uint32_t SubType, SubSectionSize;
George Rimar36f23182019-08-13 12:07:41 +0000991 if (Error E = consume(Data, SubType))
992 reportError(std::move(E), Obj->getFileName());
993 if (Error E = consume(Data, SubSectionSize))
994 reportError(std::move(E), Obj->getFileName());
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000995
996 ListScope S(W, "Subsection");
Reid Klecknera702f072019-06-18 19:41:25 +0000997 // Dump the subsection as normal even if the ignore bit is set.
998 if (SubType & SubsectionIgnoreFlag) {
999 W.printHex("IgnoredSubsectionKind", SubType);
1000 SubType &= ~SubsectionIgnoreFlag;
1001 }
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001002 W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes));
1003 W.printHex("SubSectionSize", SubSectionSize);
1004
1005 // Get the contents of the subsection.
1006 if (SubSectionSize > Data.size())
George Rimare54d3712019-08-22 08:56:24 +00001007 return reportError(errorCodeToError(object_error::parse_failed),
1008 Obj->getFileName());
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001009 StringRef Contents = Data.substr(0, SubSectionSize);
1010
1011 // Add SubSectionSize to the current offset and align that offset to find
1012 // the next subsection.
1013 size_t SectionOffset = Data.data() - SectionContents.data();
1014 size_t NextOffset = SectionOffset + SubSectionSize;
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001015 NextOffset = alignTo(NextOffset, 4);
David Majnemercab2b462016-05-28 20:04:50 +00001016 if (NextOffset > SectionContents.size())
George Rimare54d3712019-08-22 08:56:24 +00001017 return reportError(errorCodeToError(object_error::parse_failed),
1018 Obj->getFileName());
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001019 Data = SectionContents.drop_front(NextOffset);
1020
1021 // Optionally print the subsection bytes in case our parsing gets confused
1022 // later.
1023 if (opts::CodeViewSubsectionBytes)
Reid Kleckneraf49ecb2016-01-15 22:09:13 +00001024 printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents,
1025 Contents);
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001026
Zachary Turner8c099fe2017-05-30 16:36:15 +00001027 switch (DebugSubsectionKind(SubType)) {
1028 case DebugSubsectionKind::Symbols:
Reid Kleckner2893fd12016-01-14 17:51:54 +00001029 printCodeViewSymbolsSubsection(Contents, Section, SectionContents);
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001030 break;
Reid Klecknere9ab3492016-01-14 19:20:17 +00001031
Zachary Turner8c099fe2017-05-30 16:36:15 +00001032 case DebugSubsectionKind::InlineeLines:
Reid Klecknere9ab3492016-01-14 19:20:17 +00001033 printCodeViewInlineeLines(Contents);
1034 break;
1035
Zachary Turner8c099fe2017-05-30 16:36:15 +00001036 case DebugSubsectionKind::FileChecksums:
Reid Klecknerc31f5302016-01-15 18:06:25 +00001037 printCodeViewFileChecksums(Contents);
1038 break;
1039
Zachary Turner8c099fe2017-05-30 16:36:15 +00001040 case DebugSubsectionKind::Lines: {
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001041 // Holds a PC to file:line table. Some data to parse this subsection is
1042 // stored in the other subsections, so just check sanity and store the
1043 // pointers for deferred processing.
1044
1045 if (SubSectionSize < 12) {
1046 // There should be at least three words to store two function
1047 // relocations and size of the code.
George Rimare54d3712019-08-22 08:56:24 +00001048 reportError(errorCodeToError(object_error::parse_failed),
1049 Obj->getFileName());
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +00001050 return;
1051 }
1052
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001053 StringRef LinkageName;
George Rimare3fb2d52019-08-17 16:07:18 +00001054 if (std::error_code EC = resolveSymbolName(Obj->getCOFFSection(Section),
1055 SectionOffset, LinkageName))
George Rimare54d3712019-08-22 08:56:24 +00001056 reportError(errorCodeToError(EC), Obj->getFileName());
George Rimare3fb2d52019-08-17 16:07:18 +00001057
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001058 W.printString("LinkageName", LinkageName);
1059 if (FunctionLineTables.count(LinkageName) != 0) {
1060 // Saw debug info for this function already?
George Rimare54d3712019-08-22 08:56:24 +00001061 reportError(errorCodeToError(object_error::parse_failed),
1062 Obj->getFileName());
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001063 return;
Zachary Turner99f02152015-02-18 19:32:05 +00001064 }
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +00001065
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001066 FunctionLineTables[LinkageName] = Contents;
1067 FunctionNames.push_back(LinkageName);
1068 break;
1069 }
Zachary Turner8c099fe2017-05-30 16:36:15 +00001070 case DebugSubsectionKind::FrameData: {
Reid Kleckneraf49ecb2016-01-15 22:09:13 +00001071 // First four bytes is a relocation against the function.
Zachary Turnerd9a62632017-05-17 20:23:31 +00001072 BinaryStreamReader SR(Contents, llvm::support::little);
Zachary Turner591312c2017-05-30 17:13:33 +00001073
1074 DebugFrameDataSubsectionRef FrameData;
George Rimar36f23182019-08-13 12:07:41 +00001075 if (Error E = FrameData.initialize(SR))
1076 reportError(std::move(E), Obj->getFileName());
Zachary Turner591312c2017-05-30 17:13:33 +00001077
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001078 StringRef LinkageName;
George Rimare3fb2d52019-08-17 16:07:18 +00001079 if (std::error_code EC =
1080 resolveSymbolName(Obj->getCOFFSection(Section), SectionContents,
1081 FrameData.getRelocPtr(), LinkageName))
George Rimare54d3712019-08-22 08:56:24 +00001082 reportError(errorCodeToError(EC), Obj->getFileName());
Reid Kleckneraf49ecb2016-01-15 22:09:13 +00001083 W.printString("LinkageName", LinkageName);
1084
1085 // To find the active frame description, search this array for the
1086 // smallest PC range that includes the current PC.
Zachary Turner591312c2017-05-30 17:13:33 +00001087 for (const auto &FD : FrameData) {
George Rimar56408602019-08-09 11:03:21 +00001088 StringRef FrameFunc = unwrapOrError(
1089 Obj->getFileName(), CVStringTable.getString(FD.FrameFunc));
David Majnemera6d93fd2016-05-28 19:45:49 +00001090
Reid Kleckneraf49ecb2016-01-15 22:09:13 +00001091 DictScope S(W, "FrameData");
Zachary Turner591312c2017-05-30 17:13:33 +00001092 W.printHex("RvaStart", FD.RvaStart);
1093 W.printHex("CodeSize", FD.CodeSize);
1094 W.printHex("LocalSize", FD.LocalSize);
1095 W.printHex("ParamsSize", FD.ParamsSize);
1096 W.printHex("MaxStackSize", FD.MaxStackSize);
Zachary Turner591312c2017-05-30 17:13:33 +00001097 W.printHex("PrologSize", FD.PrologSize);
1098 W.printHex("SavedRegsSize", FD.SavedRegsSize);
1099 W.printFlags("Flags", FD.Flags, makeArrayRef(FrameDataFlags));
Reid Kleckneree0e8ba2018-09-07 18:48:27 +00001100
1101 // The FrameFunc string is a small RPN program. It can be broken up into
1102 // statements that end in the '=' operator, which assigns the value on
1103 // the top of the stack to the previously pushed variable. Variables can
1104 // be temporary values ($T0) or physical registers ($esp). Print each
1105 // assignment on its own line to make these programs easier to read.
1106 {
1107 ListScope FFS(W, "FrameFunc");
1108 while (!FrameFunc.empty()) {
1109 size_t EqOrEnd = FrameFunc.find('=');
1110 if (EqOrEnd == StringRef::npos)
1111 EqOrEnd = FrameFunc.size();
1112 else
1113 ++EqOrEnd;
1114 StringRef Stmt = FrameFunc.substr(0, EqOrEnd);
1115 W.printString(Stmt);
1116 FrameFunc = FrameFunc.drop_front(EqOrEnd).trim();
1117 }
1118 }
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001119 }
1120 break;
1121 }
1122
1123 // Do nothing for unrecognized subsections.
1124 default:
1125 break;
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +00001126 }
Reid Kleckneraf49ecb2016-01-15 22:09:13 +00001127 W.flush();
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +00001128 }
1129
1130 // Dump the line tables now that we've read all the subsections and know all
1131 // the required information.
1132 for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) {
1133 StringRef Name = FunctionNames[I];
1134 ListScope S(W, "FunctionLineTable");
Reid Kleckner7c0c0c02015-12-15 01:23:55 +00001135 W.printString("LinkageName", Name);
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +00001136
Zachary Turnerd9a62632017-05-17 20:23:31 +00001137 BinaryStreamReader Reader(FunctionLineTables[Name], support::little);
David Majnemer0373d532015-07-09 18:14:31 +00001138
Zachary Turner8c099fe2017-05-30 16:36:15 +00001139 DebugLinesSubsectionRef LineInfo;
George Rimar36f23182019-08-13 12:07:41 +00001140 if (Error E = LineInfo.initialize(Reader))
1141 reportError(std::move(E), Obj->getFileName());
David Majnemer04948362016-01-13 01:05:16 +00001142
Zachary Turner05bd9f32017-04-28 23:41:36 +00001143 W.printHex("Flags", LineInfo.header()->Flags);
1144 W.printHex("CodeSize", LineInfo.header()->CodeSize);
1145 for (const auto &Entry : LineInfo) {
Justin Bognera1413db2015-07-09 04:27:36 +00001146
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +00001147 ListScope S(W, "FilenameSegment");
Zachary Turner05bd9f32017-04-28 23:41:36 +00001148 printFileNameForOffset("Filename", Entry.NameIndex);
Zachary Turner4346ae12017-04-29 00:03:32 +00001149 uint32_t ColumnIndex = 0;
Zachary Turner05bd9f32017-04-28 23:41:36 +00001150 for (const auto &Line : Entry.LineNumbers) {
1151 if (Line.Offset >= LineInfo.header()->CodeSize) {
George Rimare54d3712019-08-22 08:56:24 +00001152 reportError(errorCodeToError(object_error::parse_failed),
1153 Obj->getFileName());
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +00001154 return;
1155 }
Zachary Turner05bd9f32017-04-28 23:41:36 +00001156
Benjamin Krameradcd0262020-01-28 20:23:46 +01001157 std::string PC = std::string(formatv("+{0:X}", uint32_t(Line.Offset)));
Zachary Turner05bd9f32017-04-28 23:41:36 +00001158 ListScope PCScope(W, PC);
1159 codeview::LineInfo LI(Line.Flags);
1160
David Majnemera4859df2016-02-04 17:57:12 +00001161 if (LI.isAlwaysStepInto())
1162 W.printString("StepInto", StringRef("Always"));
1163 else if (LI.isNeverStepInto())
1164 W.printString("StepInto", StringRef("Never"));
1165 else
1166 W.printNumber("LineNumberStart", LI.getStartLine());
1167 W.printNumber("LineNumberEndDelta", LI.getLineDelta());
1168 W.printBoolean("IsStatement", LI.isStatement());
Zachary Turner5b6e4e02017-04-29 01:13:21 +00001169 if (LineInfo.hasColumnInfo()) {
Zachary Turner4346ae12017-04-29 00:03:32 +00001170 W.printNumber("ColStart", Entry.Columns[ColumnIndex].StartColumn);
1171 W.printNumber("ColEnd", Entry.Columns[ColumnIndex].EndColumn);
1172 ++ColumnIndex;
David Majnemer04948362016-01-13 01:05:16 +00001173 }
1174 }
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +00001175 }
1176 }
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001177}
1178
Timur Iskhodzhanov56af52f2014-10-23 22:25:31 +00001179void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection,
1180 const SectionRef &Section,
Reid Kleckner2893fd12016-01-14 17:51:54 +00001181 StringRef SectionContents) {
Zachary Turneraaad5742016-05-23 23:41:13 +00001182 ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(),
1183 Subsection.bytes_end());
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001184 auto CODD = std::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj,
Zachary Turneraaad5742016-05-23 23:41:13 +00001185 SectionContents);
Zachary Turnerebd3ae82017-06-01 21:52:41 +00001186 CVSymbolDumper CVSD(W, Types, CodeViewContainer::ObjectFile, std::move(CODD),
Reid Klecknera6f64262018-09-11 22:00:50 +00001187 CompilationCPUType, opts::CodeViewSubsectionBytes);
Zachary Turner0d43c1c2016-05-28 05:21:57 +00001188 CVSymbolArray Symbols;
Zachary Turnerd9a62632017-05-17 20:23:31 +00001189 BinaryStreamReader Reader(BinaryData, llvm::support::little);
George Rimare3fb2d52019-08-17 16:07:18 +00001190 if (Error E = Reader.readArray(Symbols, Reader.getLength())) {
Zachary Turner0d43c1c2016-05-28 05:21:57 +00001191 W.flush();
George Rimare3fb2d52019-08-17 16:07:18 +00001192 reportError(std::move(E), Obj->getFileName());
Zachary Turner0d43c1c2016-05-28 05:21:57 +00001193 }
Reid Kleckner2893fd12016-01-14 17:51:54 +00001194
George Rimar36f23182019-08-13 12:07:41 +00001195 if (Error E = CVSD.dump(Symbols)) {
Reid Kleckneraf49ecb2016-01-15 22:09:13 +00001196 W.flush();
George Rimar36f23182019-08-13 12:07:41 +00001197 reportError(std::move(E), Obj->getFileName());
Timur Iskhodzhanov56af52f2014-10-23 22:25:31 +00001198 }
Reid Klecknera6f64262018-09-11 22:00:50 +00001199 CompilationCPUType = CVSD.getCompilationCPUType();
Reid Kleckneraf49ecb2016-01-15 22:09:13 +00001200 W.flush();
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001201}
Timur Iskhodzhanov56af52f2014-10-23 22:25:31 +00001202
Reid Klecknerc31f5302016-01-15 18:06:25 +00001203void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) {
Zachary Turner591312c2017-05-30 17:13:33 +00001204 BinaryStreamRef Stream(Subsection, llvm::support::little);
Zachary Turner8c099fe2017-05-30 16:36:15 +00001205 DebugChecksumsSubsectionRef Checksums;
George Rimar36f23182019-08-13 12:07:41 +00001206 if (Error E = Checksums.initialize(Stream))
1207 reportError(std::move(E), Obj->getFileName());
Zachary Turnerc37cb0c2017-04-27 16:12:16 +00001208
1209 for (auto &FC : Checksums) {
Reid Klecknerc31f5302016-01-15 18:06:25 +00001210 DictScope S(W, "FileChecksum");
Zachary Turnerc37cb0c2017-04-27 16:12:16 +00001211
George Rimar56408602019-08-09 11:03:21 +00001212 StringRef Filename = unwrapOrError(
1213 Obj->getFileName(), CVStringTable.getString(FC.FileNameOffset));
Zachary Turnerc37cb0c2017-04-27 16:12:16 +00001214 W.printHex("Filename", Filename, FC.FileNameOffset);
1215 W.printHex("ChecksumSize", FC.Checksum.size());
1216 W.printEnum("ChecksumKind", uint8_t(FC.Kind),
Reid Klecknerc31f5302016-01-15 18:06:25 +00001217 makeArrayRef(FileChecksumKindNames));
Zachary Turnerc37cb0c2017-04-27 16:12:16 +00001218
1219 W.printBinary("ChecksumBytes", FC.Checksum);
Reid Klecknerc31f5302016-01-15 18:06:25 +00001220 }
1221}
1222
Reid Klecknere9ab3492016-01-14 19:20:17 +00001223void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) {
Zachary Turnerd9a62632017-05-17 20:23:31 +00001224 BinaryStreamReader SR(Subsection, llvm::support::little);
Zachary Turner8c099fe2017-05-30 16:36:15 +00001225 DebugInlineeLinesSubsectionRef Lines;
George Rimar36f23182019-08-13 12:07:41 +00001226 if (Error E = Lines.initialize(SR))
1227 reportError(std::move(E), Obj->getFileName());
Reid Klecknere9ab3492016-01-14 19:20:17 +00001228
Zachary Turneredef1452017-05-02 16:56:09 +00001229 for (auto &Line : Lines) {
Reid Klecknere9ab3492016-01-14 19:20:17 +00001230 DictScope S(W, "InlineeSourceLine");
Zachary Turneredef1452017-05-02 16:56:09 +00001231 printTypeIndex("Inlinee", Line.Header->Inlinee);
1232 printFileNameForOffset("FileID", Line.Header->FileID);
1233 W.printNumber("SourceLineNum", Line.Header->SourceLineNum);
Reid Klecknere9ab3492016-01-14 19:20:17 +00001234
Zachary Turneredef1452017-05-02 16:56:09 +00001235 if (Lines.hasExtraFiles()) {
1236 W.printNumber("ExtraFileCount", Line.ExtraFiles.size());
Reid Klecknere9ab3492016-01-14 19:20:17 +00001237 ListScope ExtraFiles(W, "ExtraFiles");
Zachary Turneredef1452017-05-02 16:56:09 +00001238 for (const auto &FID : Line.ExtraFiles) {
1239 printFileNameForOffset("FileID", FID);
Reid Klecknere9ab3492016-01-14 19:20:17 +00001240 }
1241 }
1242 }
1243}
1244
Reid Kleckner8e7275c2016-01-15 00:11:21 +00001245StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) {
1246 // The file checksum subsection should precede all references to it.
Zachary Turner2d5c2cd2017-05-03 17:11:11 +00001247 if (!CVFileChecksumTable.valid() || !CVStringTable.valid())
George Rimare54d3712019-08-22 08:56:24 +00001248 reportError(errorCodeToError(object_error::parse_failed),
1249 Obj->getFileName());
Zachary Turner2d5c2cd2017-05-03 17:11:11 +00001250
Zachary Turner591312c2017-05-30 17:13:33 +00001251 auto Iter = CVFileChecksumTable.getArray().at(FileOffset);
Zachary Turner2d5c2cd2017-05-03 17:11:11 +00001252
Reid Kleckner8e7275c2016-01-15 00:11:21 +00001253 // Check if the file checksum table offset is valid.
Zachary Turner2d5c2cd2017-05-03 17:11:11 +00001254 if (Iter == CVFileChecksumTable.end())
George Rimare54d3712019-08-22 08:56:24 +00001255 reportError(errorCodeToError(object_error::parse_failed),
1256 Obj->getFileName());
Reid Kleckner8e7275c2016-01-15 00:11:21 +00001257
George Rimar56408602019-08-09 11:03:21 +00001258 return unwrapOrError(Obj->getFileName(),
1259 CVStringTable.getString(Iter->FileNameOffset));
Reid Kleckner8e7275c2016-01-15 00:11:21 +00001260}
1261
1262void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) {
1263 W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset);
1264}
1265
Zachary Turnerca6dbf12017-11-30 18:39:50 +00001266void COFFDumper::mergeCodeViewTypes(MergingTypeTableBuilder &CVIDs,
Alexandre Ganea120366e2019-02-07 15:24:18 +00001267 MergingTypeTableBuilder &CVTypes,
1268 GlobalTypeTableBuilder &GlobalCVIDs,
1269 GlobalTypeTableBuilder &GlobalCVTypes,
1270 bool GHash) {
Reid Kleckner0b269742016-05-14 00:02:53 +00001271 for (const SectionRef &S : Obj->sections()) {
George Rimarbcc00e12019-08-14 11:10:11 +00001272 StringRef SectionName = unwrapOrError(Obj->getFileName(), S.getName());
Reid Kleckner0b269742016-05-14 00:02:53 +00001273 if (SectionName == ".debug$T") {
George Rimare3d81fd2019-08-09 10:53:12 +00001274 StringRef Data = unwrapOrError(Obj->getFileName(), S.getContents());
David Majnemer78b0d722016-05-28 19:17:48 +00001275 uint32_t Magic;
George Rimar36f23182019-08-13 12:07:41 +00001276 if (Error E = consume(Data, Magic))
1277 reportError(std::move(E), Obj->getFileName());
1278
Reid Kleckner0b269742016-05-14 00:02:53 +00001279 if (Magic != 4)
George Rimare54d3712019-08-22 08:56:24 +00001280 reportError(errorCodeToError(object_error::parse_failed),
1281 Obj->getFileName());
Zachary Turnerd9a62632017-05-17 20:23:31 +00001282
Zachary Turner0d43c1c2016-05-28 05:21:57 +00001283 CVTypeArray Types;
Zachary Turnerd9a62632017-05-17 20:23:31 +00001284 BinaryStreamReader Reader(Data, llvm::support::little);
Zachary Turner0d43c1c2016-05-28 05:21:57 +00001285 if (auto EC = Reader.readArray(Types, Reader.getLength())) {
1286 consumeError(std::move(EC));
1287 W.flush();
George Rimare54d3712019-08-22 08:56:24 +00001288 reportError(errorCodeToError(object_error::parse_failed),
1289 Obj->getFileName());
Zachary Turner0d43c1c2016-05-28 05:21:57 +00001290 }
Zachary Turnerb32ec022017-05-18 23:04:08 +00001291 SmallVector<TypeIndex, 128> SourceToDest;
Alexandre Ganea90f4b942019-01-07 13:53:16 +00001292 Optional<uint32_t> PCHSignature;
Alexandre Ganea120366e2019-02-07 15:24:18 +00001293 if (GHash) {
1294 std::vector<GloballyHashedType> Hashes =
1295 GloballyHashedType::hashTypes(Types);
George Rimar36f23182019-08-13 12:07:41 +00001296 if (Error E =
Nico Weberae73e1f2019-04-26 11:44:10 +00001297 mergeTypeAndIdRecords(GlobalCVIDs, GlobalCVTypes, SourceToDest,
1298 Types, Hashes, PCHSignature))
George Rimar36f23182019-08-13 12:07:41 +00001299 return reportError(std::move(E), Obj->getFileName());
Alexandre Ganea120366e2019-02-07 15:24:18 +00001300 } else {
George Rimar36f23182019-08-13 12:07:41 +00001301 if (Error E = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types,
Alexandre Ganea120366e2019-02-07 15:24:18 +00001302 PCHSignature))
George Rimar36f23182019-08-13 12:07:41 +00001303 return reportError(std::move(E), Obj->getFileName());
Alexandre Ganea120366e2019-02-07 15:24:18 +00001304 }
Reid Kleckner0b269742016-05-14 00:02:53 +00001305 }
1306 }
1307}
1308
Reid Kleckner72e2ba72016-01-13 19:32:35 +00001309void COFFDumper::printCodeViewTypeSection(StringRef SectionName,
1310 const SectionRef &Section) {
1311 ListScope D(W, "CodeViewTypes");
1312 W.printNumber("Section", SectionName, Obj->getSectionID(Section));
Adrian McCarthy963a3312016-05-02 23:45:03 +00001313
George Rimare3d81fd2019-08-09 10:53:12 +00001314 StringRef Data = unwrapOrError(Obj->getFileName(), Section.getContents());
Reid Klecknerb2bd2812016-05-02 18:10:00 +00001315 if (opts::CodeViewSubsectionBytes)
1316 W.printBinaryBlock("Data", Data);
Reid Kleckner7960de92016-05-04 19:39:28 +00001317
1318 uint32_t Magic;
George Rimar36f23182019-08-13 12:07:41 +00001319 if (Error E = consume(Data, Magic))
1320 reportError(std::move(E), Obj->getFileName());
1321
Reid Kleckner7960de92016-05-04 19:39:28 +00001322 W.printHex("Magic", Magic);
1323 if (Magic != COFF::DEBUG_SECTION_MAGIC)
George Rimare54d3712019-08-22 08:56:24 +00001324 reportError(errorCodeToError(object_error::parse_failed),
1325 Obj->getFileName());
Reid Kleckner7960de92016-05-04 19:39:28 +00001326
Zachary Turner26dbc542017-06-18 20:52:45 +00001327 Types.reset(Data, 100);
Zachary Turner526f4f22017-05-19 19:26:58 +00001328
1329 TypeDumpVisitor TDV(Types, &W, opts::CodeViewSubsectionBytes);
George Rimar36f23182019-08-13 12:07:41 +00001330 if (Error E = codeview::visitTypeStream(Types, TDV))
1331 reportError(std::move(E), Obj->getFileName());
1332
Zachary Turner526f4f22017-05-19 19:26:58 +00001333 W.flush();
Timur Iskhodzhanov56af52f2014-10-23 22:25:31 +00001334}
1335
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +00001336void COFFDumper::printSectionHeaders() {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001337 ListScope SectionsD(W, "Sections");
1338 int SectionNumber = 0;
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001339 for (const SectionRef &Sec : Obj->sections()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001340 ++SectionNumber;
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001341 const coff_section *Section = Obj->getCOFFSection(Sec);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001342
George Rimarbcc00e12019-08-14 11:10:11 +00001343 StringRef Name = unwrapOrError(Obj->getFileName(), Sec.getName());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001344
1345 DictScope D(W, "Section");
1346 W.printNumber("Number", SectionNumber);
1347 W.printBinary("Name", Name, Section->Name);
1348 W.printHex ("VirtualSize", Section->VirtualSize);
1349 W.printHex ("VirtualAddress", Section->VirtualAddress);
1350 W.printNumber("RawDataSize", Section->SizeOfRawData);
1351 W.printHex ("PointerToRawData", Section->PointerToRawData);
1352 W.printHex ("PointerToRelocations", Section->PointerToRelocations);
1353 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers);
1354 W.printNumber("RelocationCount", Section->NumberOfRelocations);
1355 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers);
1356 W.printFlags ("Characteristics", Section->Characteristics,
1357 makeArrayRef(ImageSectionCharacteristics),
1358 COFF::SectionCharacteristics(0x00F00000));
1359
1360 if (opts::SectionRelocations) {
1361 ListScope D(W, "Relocations");
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001362 for (const RelocationRef &Reloc : Sec.relocations())
1363 printRelocation(Sec, Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001364 }
1365
1366 if (opts::SectionSymbols) {
1367 ListScope D(W, "Symbols");
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001368 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +00001369 if (!Sec.containsSymbol(Symbol))
Eric Christopher9cad53c2013-04-03 18:31:38 +00001370 continue;
1371
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001372 printSymbol(Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001373 }
1374 }
1375
David Majnemerdac39852014-09-26 22:32:16 +00001376 if (opts::SectionData &&
1377 !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
George Rimare3d81fd2019-08-09 10:53:12 +00001378 StringRef Data = unwrapOrError(Obj->getFileName(), Sec.getContents());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001379 W.printBinaryBlock("SectionData", Data);
1380 }
1381 }
1382}
1383
1384void COFFDumper::printRelocations() {
1385 ListScope D(W, "Relocations");
1386
Eric Christopher9cad53c2013-04-03 18:31:38 +00001387 int SectionNumber = 0;
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001388 for (const SectionRef &Section : Obj->sections()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001389 ++SectionNumber;
George Rimarbcc00e12019-08-14 11:10:11 +00001390 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001391
1392 bool PrintedGroup = false;
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001393 for (const RelocationRef &Reloc : Section.relocations()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001394 if (!PrintedGroup) {
1395 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
1396 W.indent();
1397 PrintedGroup = true;
1398 }
1399
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001400 printRelocation(Section, Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001401 }
1402
1403 if (PrintedGroup) {
1404 W.unindent();
1405 W.startLine() << "}\n";
1406 }
1407 }
1408}
1409
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001410void COFFDumper::printRelocation(const SectionRef &Section,
Reid Kleckner1c7a9cd2016-01-14 17:51:57 +00001411 const RelocationRef &Reloc, uint64_t Bias) {
1412 uint64_t Offset = Reloc.getOffset() - Bias;
Rafael Espindola99c041b2015-06-30 01:53:01 +00001413 uint64_t RelocType = Reloc.getType();
Eric Christopher9cad53c2013-04-03 18:31:38 +00001414 SmallString<32> RelocName;
Eric Christopher9cad53c2013-04-03 18:31:38 +00001415 StringRef SymbolName;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001416 Reloc.getTypeName(RelocName);
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001417 symbol_iterator Symbol = Reloc.getSymbol();
Martin Storsjo74e7d262019-01-03 08:08:23 +00001418 int64_t SymbolIndex = -1;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001419 if (Symbol != Obj->symbol_end()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001420 Expected<StringRef> SymbolNameOrErr = Symbol->getName();
George Rimare3fb2d52019-08-17 16:07:18 +00001421 if (!SymbolNameOrErr)
1422 reportError(SymbolNameOrErr.takeError(), Obj->getFileName());
1423
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001424 SymbolName = *SymbolNameOrErr;
Martin Storsjo74e7d262019-01-03 08:08:23 +00001425 SymbolIndex = Obj->getSymbolIndex(Obj->getCOFFSymbol(*Symbol));
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001426 }
Eric Christopher9cad53c2013-04-03 18:31:38 +00001427
Nico Rieckf3f0b792013-04-12 04:01:52 +00001428 if (opts::ExpandRelocs) {
1429 DictScope Group(W, "Relocation");
1430 W.printHex("Offset", Offset);
1431 W.printNumber("Type", RelocName, RelocType);
David Majnemer1f80b0a2014-11-13 07:42:11 +00001432 W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName);
Martin Storsjo74e7d262019-01-03 08:08:23 +00001433 W.printNumber("SymbolIndex", SymbolIndex);
Nico Rieckf3f0b792013-04-12 04:01:52 +00001434 } else {
1435 raw_ostream& OS = W.startLine();
1436 OS << W.hex(Offset)
1437 << " " << RelocName
David Majnemer1f80b0a2014-11-13 07:42:11 +00001438 << " " << (SymbolName.empty() ? "-" : SymbolName)
Martin Storsjo74e7d262019-01-03 08:08:23 +00001439 << " (" << SymbolIndex << ")"
Nico Rieckf3f0b792013-04-12 04:01:52 +00001440 << "\n";
1441 }
Eric Christopher9cad53c2013-04-03 18:31:38 +00001442}
1443
1444void COFFDumper::printSymbols() {
1445 ListScope Group(W, "Symbols");
1446
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001447 for (const SymbolRef &Symbol : Obj->symbols())
1448 printSymbol(Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001449}
1450
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001451void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); }
Eric Christopher9cad53c2013-04-03 18:31:38 +00001452
Fangrui Song8be28cd2019-05-02 10:32:03 +00001453static Expected<StringRef>
David Majnemer236b0ca2014-11-17 11:17:17 +00001454getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber,
1455 const coff_section *Section) {
Fangrui Song8be28cd2019-05-02 10:32:03 +00001456 if (Section)
1457 return Obj->getSectionName(Section);
David Majnemerf4dc4562014-09-20 00:25:06 +00001458 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
David Majnemer236b0ca2014-11-17 11:17:17 +00001459 return StringRef("IMAGE_SYM_DEBUG");
David Majnemerf4dc4562014-09-20 00:25:06 +00001460 if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE)
David Majnemer236b0ca2014-11-17 11:17:17 +00001461 return StringRef("IMAGE_SYM_ABSOLUTE");
David Majnemerf4dc4562014-09-20 00:25:06 +00001462 if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED)
David Majnemer236b0ca2014-11-17 11:17:17 +00001463 return StringRef("IMAGE_SYM_UNDEFINED");
1464 return StringRef("");
David Majnemerf4dc4562014-09-20 00:25:06 +00001465}
1466
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001467void COFFDumper::printSymbol(const SymbolRef &Sym) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001468 DictScope D(W, "Symbol");
1469
David Majnemer44f51e52014-09-10 12:51:52 +00001470 COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001471 const coff_section *Section;
David Majnemer44f51e52014-09-10 12:51:52 +00001472 if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001473 W.startLine() << "Invalid section number: " << EC.message() << "\n";
1474 W.flush();
1475 return;
1476 }
1477
1478 StringRef SymbolName;
1479 if (Obj->getSymbolName(Symbol, SymbolName))
1480 SymbolName = "";
1481
Fangrui Song8be28cd2019-05-02 10:32:03 +00001482 StringRef SectionName;
1483 if (Expected<StringRef> NameOrErr =
1484 getSectionName(Obj, Symbol.getSectionNumber(), Section))
1485 SectionName = *NameOrErr;
Eric Christopher9cad53c2013-04-03 18:31:38 +00001486
1487 W.printString("Name", SymbolName);
David Majnemer44f51e52014-09-10 12:51:52 +00001488 W.printNumber("Value", Symbol.getValue());
1489 W.printNumber("Section", SectionName, Symbol.getSectionNumber());
1490 W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType));
1491 W.printEnum ("ComplexType", Symbol.getComplexType(),
Eric Christopher9cad53c2013-04-03 18:31:38 +00001492 makeArrayRef(ImageSymDType));
David Majnemer44f51e52014-09-10 12:51:52 +00001493 W.printEnum ("StorageClass", Symbol.getStorageClass(),
Eric Christopher9cad53c2013-04-03 18:31:38 +00001494 makeArrayRef(ImageSymClass));
David Majnemer44f51e52014-09-10 12:51:52 +00001495 W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001496
David Majnemer44f51e52014-09-10 12:51:52 +00001497 for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) {
1498 if (Symbol.isFunctionDefinition()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001499 const coff_aux_function_definition *Aux;
George Rimare3fb2d52019-08-17 16:07:18 +00001500 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, Aux))
George Rimare54d3712019-08-22 08:56:24 +00001501 reportError(errorCodeToError(EC), Obj->getFileName());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001502
1503 DictScope AS(W, "AuxFunctionDef");
1504 W.printNumber("TagIndex", Aux->TagIndex);
1505 W.printNumber("TotalSize", Aux->TotalSize);
David Majnemerf3a2af52014-03-19 04:33:27 +00001506 W.printHex("PointerToLineNumber", Aux->PointerToLinenumber);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001507 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001508
David Majnemerc7d7c6f2014-10-31 05:07:00 +00001509 } else if (Symbol.isAnyUndefined()) {
David Majnemerf3a2af52014-03-19 04:33:27 +00001510 const coff_aux_weak_external *Aux;
George Rimare3fb2d52019-08-17 16:07:18 +00001511 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, Aux))
George Rimare54d3712019-08-22 08:56:24 +00001512 reportError(errorCodeToError(EC), Obj->getFileName());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001513
Rafael Espindola74894922017-10-11 17:23:15 +00001514 Expected<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex);
George Rimare54d3712019-08-22 08:56:24 +00001515 if (!Linked)
1516 reportError(Linked.takeError(), Obj->getFileName());
1517
Eric Christopher9cad53c2013-04-03 18:31:38 +00001518 StringRef LinkedName;
George Rimare54d3712019-08-22 08:56:24 +00001519 if (std::error_code EC = Obj->getSymbolName(*Linked, LinkedName))
1520 reportError(errorCodeToError(EC), Obj->getFileName());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001521
1522 DictScope AS(W, "AuxWeakExternal");
1523 W.printNumber("Linked", LinkedName, Aux->TagIndex);
1524 W.printEnum ("Search", Aux->Characteristics,
1525 makeArrayRef(WeakExternalCharacteristics));
Eric Christopher9cad53c2013-04-03 18:31:38 +00001526
David Majnemer44f51e52014-09-10 12:51:52 +00001527 } else if (Symbol.isFileRecord()) {
1528 const char *FileName;
George Rimare3fb2d52019-08-17 16:07:18 +00001529 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, FileName))
George Rimare54d3712019-08-22 08:56:24 +00001530 reportError(errorCodeToError(EC), Obj->getFileName());
Nico Rieck0ab8e602013-04-22 08:35:11 +00001531 DictScope AS(W, "AuxFileRecord");
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +00001532
David Majnemer44f51e52014-09-10 12:51:52 +00001533 StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() *
1534 Obj->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +00001535 W.printString("FileName", Name.rtrim(StringRef("\0", 1)));
Saleem Abdulrasool3b5e0012014-04-16 04:15:29 +00001536 break;
David Majnemer44f51e52014-09-10 12:51:52 +00001537 } else if (Symbol.isSectionDefinition()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001538 const coff_aux_section_definition *Aux;
George Rimare3fb2d52019-08-17 16:07:18 +00001539 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, Aux))
George Rimare54d3712019-08-22 08:56:24 +00001540 reportError(errorCodeToError(EC), Obj->getFileName());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001541
David Majnemer4d571592014-09-15 19:42:42 +00001542 int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj());
1543
Eric Christopher9cad53c2013-04-03 18:31:38 +00001544 DictScope AS(W, "AuxSectionDef");
1545 W.printNumber("Length", Aux->Length);
1546 W.printNumber("RelocationCount", Aux->NumberOfRelocations);
1547 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers);
1548 W.printHex("Checksum", Aux->CheckSum);
David Majnemer4d571592014-09-15 19:42:42 +00001549 W.printNumber("Number", AuxNumber);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001550 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect));
Eric Christopher9cad53c2013-04-03 18:31:38 +00001551
Nico Riecka711dee2013-04-22 08:34:59 +00001552 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT
Eric Christopher9cad53c2013-04-03 18:31:38 +00001553 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
1554 const coff_section *Assoc;
David Majnemer236b0ca2014-11-17 11:17:17 +00001555 StringRef AssocName = "";
Fangrui Song8be28cd2019-05-02 10:32:03 +00001556 if (std::error_code EC = Obj->getSection(AuxNumber, Assoc))
George Rimare54d3712019-08-22 08:56:24 +00001557 reportError(errorCodeToError(EC), Obj->getFileName());
Fangrui Song8be28cd2019-05-02 10:32:03 +00001558 Expected<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc);
1559 if (!Res)
George Rimar36f23182019-08-13 12:07:41 +00001560 reportError(Res.takeError(), Obj->getFileName());
Fangrui Song8be28cd2019-05-02 10:32:03 +00001561 AssocName = *Res;
Eric Christopher9cad53c2013-04-03 18:31:38 +00001562
David Majnemer4d571592014-09-15 19:42:42 +00001563 W.printNumber("AssocSection", AssocName, AuxNumber);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001564 }
David Majnemer44f51e52014-09-10 12:51:52 +00001565 } else if (Symbol.isCLRToken()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001566 const coff_aux_clr_token *Aux;
George Rimare3fb2d52019-08-17 16:07:18 +00001567 if (std::error_code EC = getSymbolAuxData(Obj, Symbol, I, Aux))
George Rimare54d3712019-08-22 08:56:24 +00001568 reportError(errorCodeToError(EC), Obj->getFileName());
Eric Christopher9cad53c2013-04-03 18:31:38 +00001569
Rafael Espindola74894922017-10-11 17:23:15 +00001570 Expected<COFFSymbolRef> ReferredSym =
David Majnemer44f51e52014-09-10 12:51:52 +00001571 Obj->getSymbol(Aux->SymbolTableIndex);
George Rimare54d3712019-08-22 08:56:24 +00001572 if (!ReferredSym)
1573 reportError(ReferredSym.takeError(), Obj->getFileName());
1574
Nico Rieck8678acd2014-03-17 01:46:52 +00001575 StringRef ReferredName;
George Rimare54d3712019-08-22 08:56:24 +00001576 if (std::error_code EC = Obj->getSymbolName(*ReferredSym, ReferredName))
1577 reportError(errorCodeToError(EC), Obj->getFileName());
Nico Rieck8678acd2014-03-17 01:46:52 +00001578
Eric Christopher9cad53c2013-04-03 18:31:38 +00001579 DictScope AS(W, "AuxCLRToken");
1580 W.printNumber("AuxType", Aux->AuxType);
1581 W.printNumber("Reserved", Aux->Reserved);
Nico Rieck8678acd2014-03-17 01:46:52 +00001582 W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001583
1584 } else {
1585 W.startLine() << "<unhandled auxiliary record>\n";
1586 }
1587 }
1588}
1589
1590void COFFDumper::printUnwindInfo() {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001591 ListScope D(W, "UnwindInformation");
David Majnemer44f51e52014-09-10 12:51:52 +00001592 switch (Obj->getMachine()) {
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +00001593 case COFF::IMAGE_FILE_MACHINE_AMD64: {
1594 Win64EH::Dumper Dumper(W);
Rafael Espindola4453e42942014-06-13 03:07:50 +00001595 Win64EH::Dumper::SymbolResolver
1596 Resolver = [](const object::coff_section *Section, uint64_t Offset,
1597 SymbolRef &Symbol, void *user_data) -> std::error_code {
1598 COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data);
1599 return Dumper->resolveSymbol(Section, Offset, Symbol);
1600 };
Saleem Abdulrasool65dbbb52014-05-25 21:37:59 +00001601 Win64EH::Dumper::Context Ctx(*Obj, Resolver, this);
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +00001602 Dumper.printData(Ctx);
1603 break;
Eric Christopher9cad53c2013-04-03 18:31:38 +00001604 }
Sanjin Sijariccd416382018-10-24 00:03:34 +00001605 case COFF::IMAGE_FILE_MACHINE_ARM64:
Saleem Abdulrasoole6971ca2014-06-04 15:47:15 +00001606 case COFF::IMAGE_FILE_MACHINE_ARMNT: {
Sanjin Sijariccd416382018-10-24 00:03:34 +00001607 ARM::WinEH::Decoder Decoder(W, Obj->getMachine() ==
1608 COFF::IMAGE_FILE_MACHINE_ARM64);
Fangrui Song8be28cd2019-05-02 10:32:03 +00001609 // TODO Propagate the error.
1610 consumeError(Decoder.dumpProcedureData(*Obj));
Saleem Abdulrasoole6971ca2014-06-04 15:47:15 +00001611 break;
1612 }
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +00001613 default:
David Majnemer44f51e52014-09-10 12:51:52 +00001614 W.printEnum("unsupported Image Machine", Obj->getMachine(),
Saleem Abdulrasoole8839a72014-05-25 20:26:45 +00001615 makeArrayRef(ImageFileMachineType));
1616 break;
Eric Christopher9cad53c2013-04-03 18:31:38 +00001617 }
1618}
1619
Petr Hosekad6f4572017-12-27 19:59:56 +00001620void COFFDumper::printNeededLibraries() {
1621 ListScope D(W, "NeededLibraries");
1622
1623 using LibsTy = std::vector<StringRef>;
1624 LibsTy Libs;
1625
1626 for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) {
1627 StringRef Name;
1628 if (!DirRef.getName(Name))
1629 Libs.push_back(Name);
1630 }
1631
Fangrui Songa5355a52019-04-22 15:53:43 +00001632 llvm::stable_sort(Libs);
Petr Hosekad6f4572017-12-27 19:59:56 +00001633
1634 for (const auto &L : Libs) {
Jordan Rupprecht565f1e22019-06-12 20:16:22 +00001635 W.startLine() << L << "\n";
Petr Hosekad6f4572017-12-27 19:59:56 +00001636 }
1637}
1638
Rui Ueyama979fb402014-10-09 02:16:38 +00001639void COFFDumper::printImportedSymbols(
1640 iterator_range<imported_symbol_iterator> Range) {
1641 for (const ImportedSymbolRef &I : Range) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001642 StringRef Sym;
George Rimare3fb2d52019-08-17 16:07:18 +00001643 if (std::error_code EC = I.getSymbolName(Sym))
George Rimare54d3712019-08-22 08:56:24 +00001644 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyama15d99352014-10-03 00:41:58 +00001645 uint16_t Ordinal;
George Rimare3fb2d52019-08-17 16:07:18 +00001646 if (std::error_code EC = I.getOrdinal(Ordinal))
George Rimare54d3712019-08-22 08:56:24 +00001647 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyama15d99352014-10-03 00:41:58 +00001648 W.printNumber("Symbol", Sym, Ordinal);
1649 }
1650}
1651
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001652void COFFDumper::printDelayImportedSymbols(
1653 const DelayImportDirectoryEntryRef &I,
1654 iterator_range<imported_symbol_iterator> Range) {
1655 int Index = 0;
1656 for (const ImportedSymbolRef &S : Range) {
1657 DictScope Import(W, "Import");
1658 StringRef Sym;
George Rimare3fb2d52019-08-17 16:07:18 +00001659 if (std::error_code EC = S.getSymbolName(Sym))
George Rimare54d3712019-08-22 08:56:24 +00001660 reportError(errorCodeToError(EC), Obj->getFileName());
George Rimare3fb2d52019-08-17 16:07:18 +00001661
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001662 uint16_t Ordinal;
George Rimare3fb2d52019-08-17 16:07:18 +00001663 if (std::error_code EC = S.getOrdinal(Ordinal))
George Rimare54d3712019-08-22 08:56:24 +00001664 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001665 W.printNumber("Symbol", Sym, Ordinal);
George Rimare3fb2d52019-08-17 16:07:18 +00001666
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001667 uint64_t Addr;
George Rimare3fb2d52019-08-17 16:07:18 +00001668 if (std::error_code EC = I.getImportAddress(Index++, Addr))
George Rimare54d3712019-08-22 08:56:24 +00001669 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001670 W.printHex("Address", Addr);
1671 }
1672}
1673
Rui Ueyama1e152d52014-10-02 17:02:18 +00001674void COFFDumper::printCOFFImports() {
Rui Ueyama15d99352014-10-03 00:41:58 +00001675 // Regular imports
Rui Ueyama979fb402014-10-09 02:16:38 +00001676 for (const ImportDirectoryEntryRef &I : Obj->import_directories()) {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001677 DictScope Import(W, "Import");
1678 StringRef Name;
George Rimare3fb2d52019-08-17 16:07:18 +00001679 if (std::error_code EC = I.getName(Name))
George Rimare54d3712019-08-22 08:56:24 +00001680 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyama1e152d52014-10-02 17:02:18 +00001681 W.printString("Name", Name);
Shoaib Meenai73754482017-03-26 17:10:11 +00001682 uint32_t ILTAddr;
George Rimare3fb2d52019-08-17 16:07:18 +00001683 if (std::error_code EC = I.getImportLookupTableRVA(ILTAddr))
George Rimare54d3712019-08-22 08:56:24 +00001684 reportError(errorCodeToError(EC), Obj->getFileName());
Shoaib Meenai73754482017-03-26 17:10:11 +00001685 W.printHex("ImportLookupTableRVA", ILTAddr);
1686 uint32_t IATAddr;
George Rimare3fb2d52019-08-17 16:07:18 +00001687 if (std::error_code EC = I.getImportAddressTableRVA(IATAddr))
George Rimare54d3712019-08-22 08:56:24 +00001688 reportError(errorCodeToError(EC), Obj->getFileName());
Shoaib Meenai73754482017-03-26 17:10:11 +00001689 W.printHex("ImportAddressTableRVA", IATAddr);
1690 // The import lookup table can be missing with certain older linkers, so
1691 // fall back to the import address table in that case.
1692 if (ILTAddr)
1693 printImportedSymbols(I.lookup_table_symbols());
1694 else
1695 printImportedSymbols(I.imported_symbols());
Rui Ueyama15d99352014-10-03 00:41:58 +00001696 }
1697
1698 // Delay imports
Rui Ueyama979fb402014-10-09 02:16:38 +00001699 for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001700 DictScope Import(W, "DelayImport");
1701 StringRef Name;
George Rimare3fb2d52019-08-17 16:07:18 +00001702 if (std::error_code EC = I.getName(Name))
George Rimare54d3712019-08-22 08:56:24 +00001703 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyama15d99352014-10-03 00:41:58 +00001704 W.printString("Name", Name);
Rui Ueyama1af08652014-10-03 18:07:18 +00001705 const delay_import_directory_table_entry *Table;
George Rimare3fb2d52019-08-17 16:07:18 +00001706 if (std::error_code EC = I.getDelayImportTable(Table))
George Rimare54d3712019-08-22 08:56:24 +00001707 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyama1af08652014-10-03 18:07:18 +00001708 W.printHex("Attributes", Table->Attributes);
1709 W.printHex("ModuleHandle", Table->ModuleHandle);
1710 W.printHex("ImportAddressTable", Table->DelayImportAddressTable);
1711 W.printHex("ImportNameTable", Table->DelayImportNameTable);
1712 W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable);
1713 W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001714 printDelayImportedSymbols(I, I.imported_symbols());
Rui Ueyama1e152d52014-10-02 17:02:18 +00001715 }
1716}
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +00001717
Saleem Abdulrasoolddd92642015-01-03 21:35:09 +00001718void COFFDumper::printCOFFExports() {
1719 for (const ExportDirectoryEntryRef &E : Obj->export_directories()) {
1720 DictScope Export(W, "Export");
1721
1722 StringRef Name;
1723 uint32_t Ordinal, RVA;
1724
George Rimare3fb2d52019-08-17 16:07:18 +00001725 if (std::error_code EC = E.getSymbolName(Name))
George Rimare54d3712019-08-22 08:56:24 +00001726 reportError(errorCodeToError(EC), Obj->getFileName());
George Rimare3fb2d52019-08-17 16:07:18 +00001727 if (std::error_code EC = E.getOrdinal(Ordinal))
George Rimare54d3712019-08-22 08:56:24 +00001728 reportError(errorCodeToError(EC), Obj->getFileName());
George Rimare3fb2d52019-08-17 16:07:18 +00001729 if (std::error_code EC = E.getExportRVA(RVA))
George Rimare54d3712019-08-22 08:56:24 +00001730 reportError(errorCodeToError(EC), Obj->getFileName());
Saleem Abdulrasoolddd92642015-01-03 21:35:09 +00001731
1732 W.printNumber("Ordinal", Ordinal);
1733 W.printString("Name", Name);
1734 W.printHex("RVA", RVA);
1735 }
1736}
1737
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +00001738void COFFDumper::printCOFFDirectives() {
1739 for (const SectionRef &Section : Obj->sections()) {
George Rimarbcc00e12019-08-14 11:10:11 +00001740 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName());
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +00001741 if (Name != ".drectve")
1742 continue;
1743
George Rimare3d81fd2019-08-09 10:53:12 +00001744 StringRef Contents =
1745 unwrapOrError(Obj->getFileName(), Section.getContents());
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +00001746 W.printString("Directive(s)", Contents);
1747 }
1748}
Rui Ueyama74e85132014-11-19 00:18:07 +00001749
Alexander Kornienkoa45648b2017-07-26 10:14:55 +00001750static std::string getBaseRelocTypeName(uint8_t Type) {
Rui Ueyama74e85132014-11-19 00:18:07 +00001751 switch (Type) {
1752 case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE";
1753 case COFF::IMAGE_REL_BASED_HIGH: return "HIGH";
1754 case COFF::IMAGE_REL_BASED_LOW: return "LOW";
1755 case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW";
1756 case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ";
Saleem Abdulrasool5a41c372015-01-16 20:16:09 +00001757 case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)";
Rui Ueyama74e85132014-11-19 00:18:07 +00001758 case COFF::IMAGE_REL_BASED_DIR64: return "DIR64";
Alexander Kornienkoa45648b2017-07-26 10:14:55 +00001759 default: return "unknown (" + llvm::utostr(Type) + ")";
Rui Ueyama74e85132014-11-19 00:18:07 +00001760 }
1761}
1762
1763void COFFDumper::printCOFFBaseReloc() {
1764 ListScope D(W, "BaseReloc");
1765 for (const BaseRelocRef &I : Obj->base_relocs()) {
1766 uint8_t Type;
1767 uint32_t RVA;
George Rimare3fb2d52019-08-17 16:07:18 +00001768 if (std::error_code EC = I.getRVA(RVA))
George Rimare54d3712019-08-22 08:56:24 +00001769 reportError(errorCodeToError(EC), Obj->getFileName());
George Rimare3fb2d52019-08-17 16:07:18 +00001770 if (std::error_code EC = I.getType(Type))
George Rimare54d3712019-08-22 08:56:24 +00001771 reportError(errorCodeToError(EC), Obj->getFileName());
Rui Ueyama74e85132014-11-19 00:18:07 +00001772 DictScope Import(W, "Entry");
1773 W.printString("Type", getBaseRelocTypeName(Type));
1774 W.printHex("Address", RVA);
1775 }
1776}
Lang Hames0000afd2015-06-26 23:56:53 +00001777
Zachary Turner8d6396d2017-04-27 19:38:38 +00001778void COFFDumper::printCOFFResources() {
1779 ListScope ResourcesD(W, "Resources");
1780 for (const SectionRef &S : Obj->sections()) {
George Rimarbcc00e12019-08-14 11:10:11 +00001781 StringRef Name = unwrapOrError(Obj->getFileName(), S.getName());
Zachary Turner8d6396d2017-04-27 19:38:38 +00001782 if (!Name.startswith(".rsrc"))
1783 continue;
1784
George Rimare3d81fd2019-08-09 10:53:12 +00001785 StringRef Ref = unwrapOrError(Obj->getFileName(), S.getContents());
Zachary Turner8d6396d2017-04-27 19:38:38 +00001786
1787 if ((Name == ".rsrc") || (Name == ".rsrc$01")) {
Martin Storsjo94382212019-08-30 06:55:49 +00001788 ResourceSectionRef RSF;
1789 Error E = RSF.load(Obj, S);
1790 if (E)
1791 reportError(std::move(E), Obj->getFileName());
George Rimare3d81fd2019-08-09 10:53:12 +00001792 auto &BaseTable = unwrapOrError(Obj->getFileName(), RSF.getBaseTable());
Eric Beckmann13017592017-06-13 00:16:32 +00001793 W.printNumber("Total Number of Resources",
1794 countTotalTableEntries(RSF, BaseTable, "Type"));
1795 W.printHex("Base Table Address",
1796 Obj->getCOFFSection(S)->PointerToRawData);
1797 W.startLine() << "\n";
Eric Beckmannefef15a2017-05-08 02:47:07 +00001798 printResourceDirectoryTable(RSF, BaseTable, "Type");
Zachary Turner8d6396d2017-04-27 19:38:38 +00001799 }
Eric Beckmannefef15a2017-05-08 02:47:07 +00001800 if (opts::SectionData)
1801 W.printBinaryBlock(Name.str() + " Data", Ref);
Zachary Turner8d6396d2017-04-27 19:38:38 +00001802 }
1803}
1804
Eric Beckmann13017592017-06-13 00:16:32 +00001805uint32_t
1806COFFDumper::countTotalTableEntries(ResourceSectionRef RSF,
1807 const coff_resource_dir_table &Table,
1808 StringRef Level) {
1809 uint32_t TotalEntries = 0;
1810 for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries;
1811 i++) {
Martin Storsjoedb6ab92019-08-29 08:59:56 +00001812 auto Entry = unwrapOrError(Obj->getFileName(), RSF.getTableEntry(Table, i));
Eric Beckmann13017592017-06-13 00:16:32 +00001813 if (Entry.Offset.isSubDir()) {
1814 StringRef NextLevel;
1815 if (Level == "Name")
1816 NextLevel = "Language";
1817 else
1818 NextLevel = "Name";
George Rimare3d81fd2019-08-09 10:53:12 +00001819 auto &NextTable =
1820 unwrapOrError(Obj->getFileName(), RSF.getEntrySubDir(Entry));
Eric Beckmann13017592017-06-13 00:16:32 +00001821 TotalEntries += countTotalTableEntries(RSF, NextTable, NextLevel);
1822 } else {
1823 TotalEntries += 1;
1824 }
1825 }
1826 return TotalEntries;
1827}
1828
Eric Beckmannefef15a2017-05-08 02:47:07 +00001829void COFFDumper::printResourceDirectoryTable(
1830 ResourceSectionRef RSF, const coff_resource_dir_table &Table,
1831 StringRef Level) {
Eric Beckmannefef15a2017-05-08 02:47:07 +00001832
Eric Beckmann13017592017-06-13 00:16:32 +00001833 W.printNumber("Number of String Entries", Table.NumberOfNameEntries);
1834 W.printNumber("Number of ID Entries", Table.NumberOfIDEntries);
Eric Beckmannefef15a2017-05-08 02:47:07 +00001835
1836 // Iterate through level in resource directory tree.
1837 for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries;
1838 i++) {
Martin Storsjoedb6ab92019-08-29 08:59:56 +00001839 auto Entry = unwrapOrError(Obj->getFileName(), RSF.getTableEntry(Table, i));
Eric Beckmannefef15a2017-05-08 02:47:07 +00001840 StringRef Name;
1841 SmallString<20> IDStr;
1842 raw_svector_ostream OS(IDStr);
1843 if (i < Table.NumberOfNameEntries) {
Nico Weberae73e1f2019-04-26 11:44:10 +00001844 ArrayRef<UTF16> RawEntryNameString =
George Rimare3d81fd2019-08-09 10:53:12 +00001845 unwrapOrError(Obj->getFileName(), RSF.getEntryNameString(Entry));
Eric Beckmann674deed2017-05-09 19:35:45 +00001846 std::vector<UTF16> EndianCorrectedNameString;
1847 if (llvm::sys::IsBigEndianHost) {
1848 EndianCorrectedNameString.resize(RawEntryNameString.size() + 1);
1849 std::copy(RawEntryNameString.begin(), RawEntryNameString.end(),
1850 EndianCorrectedNameString.begin() + 1);
1851 EndianCorrectedNameString[0] = UNI_UTF16_BYTE_ORDER_MARK_SWAPPED;
1852 RawEntryNameString = makeArrayRef(EndianCorrectedNameString);
1853 }
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001854 std::string EntryNameString;
1855 if (!llvm::convertUTF16ToUTF8String(RawEntryNameString, EntryNameString))
George Rimare54d3712019-08-22 08:56:24 +00001856 reportError(errorCodeToError(object_error::parse_failed),
1857 Obj->getFileName());
Eric Beckmannefef15a2017-05-08 02:47:07 +00001858 OS << ": ";
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001859 OS << EntryNameString;
Eric Beckmannefef15a2017-05-08 02:47:07 +00001860 } else {
1861 if (Level == "Type") {
Nico Weber23cb79f2019-04-24 23:26:30 +00001862 OS << ": ";
1863 printResourceTypeName(Entry.Identifier.ID, OS);
Eric Beckmannefef15a2017-05-08 02:47:07 +00001864 } else {
1865 OS << ": (ID " << Entry.Identifier.ID << ")";
1866 }
1867 }
1868 Name = StringRef(IDStr);
1869 ListScope ResourceType(W, Level.str() + Name.str());
1870 if (Entry.Offset.isSubDir()) {
Eric Beckmann13017592017-06-13 00:16:32 +00001871 W.printHex("Table Offset", Entry.Offset.value());
Eric Beckmannefef15a2017-05-08 02:47:07 +00001872 StringRef NextLevel;
1873 if (Level == "Name")
1874 NextLevel = "Language";
1875 else
1876 NextLevel = "Name";
George Rimare3d81fd2019-08-09 10:53:12 +00001877 auto &NextTable =
1878 unwrapOrError(Obj->getFileName(), RSF.getEntrySubDir(Entry));
Eric Beckmannefef15a2017-05-08 02:47:07 +00001879 printResourceDirectoryTable(RSF, NextTable, NextLevel);
1880 } else {
Eric Beckmann13017592017-06-13 00:16:32 +00001881 W.printHex("Entry Offset", Entry.Offset.value());
1882 char FormattedTime[20] = {};
1883 time_t TDS = time_t(Table.TimeDateStamp);
1884 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
Eric Beckmannefef15a2017-05-08 02:47:07 +00001885 W.printHex("Time/Date Stamp", FormattedTime, Table.TimeDateStamp);
1886 W.printNumber("Major Version", Table.MajorVersion);
1887 W.printNumber("Minor Version", Table.MinorVersion);
Eric Beckmann13017592017-06-13 00:16:32 +00001888 W.printNumber("Characteristics", Table.Characteristics);
Martin Storsjo7ba81d92019-08-29 09:00:14 +00001889 ListScope DataScope(W, "Data");
1890 auto &DataEntry =
1891 unwrapOrError(Obj->getFileName(), RSF.getEntryData(Entry));
1892 W.printHex("DataRVA", DataEntry.DataRVA);
1893 W.printNumber("DataSize", DataEntry.DataSize);
1894 W.printNumber("Codepage", DataEntry.Codepage);
1895 W.printNumber("Reserved", DataEntry.Reserved);
Martin Storsjo94382212019-08-30 06:55:49 +00001896 StringRef Contents =
1897 unwrapOrError(Obj->getFileName(), RSF.getContents(DataEntry));
1898 W.printBinaryBlock("Data", Contents);
Eric Beckmannefef15a2017-05-08 02:47:07 +00001899 }
1900 }
1901}
1902
Lang Hames0000afd2015-06-26 23:56:53 +00001903void COFFDumper::printStackMap() const {
1904 object::SectionRef StackMapSection;
1905 for (auto Sec : Obj->sections()) {
1906 StringRef Name;
George Rimarbcc00e12019-08-14 11:10:11 +00001907 if (Expected<StringRef> NameOrErr = Sec.getName())
1908 Name = *NameOrErr;
1909 else
1910 consumeError(NameOrErr.takeError());
1911
Lang Hames0000afd2015-06-26 23:56:53 +00001912 if (Name == ".llvm_stackmaps") {
1913 StackMapSection = Sec;
1914 break;
1915 }
1916 }
1917
1918 if (StackMapSection == object::SectionRef())
1919 return;
1920
George Rimare3d81fd2019-08-09 10:53:12 +00001921 StringRef StackMapContents =
1922 unwrapOrError(Obj->getFileName(), StackMapSection.getContents());
Fangrui Song6a0746a2019-04-07 03:58:42 +00001923 ArrayRef<uint8_t> StackMapContentsArray =
1924 arrayRefFromStringRef(StackMapContents);
Lang Hames0000afd2015-06-26 23:56:53 +00001925
1926 if (Obj->isLittleEndian())
1927 prettyPrintStackMap(
Philip Reames377f5072019-04-13 02:02:56 +00001928 W, StackMapParser<support::little>(StackMapContentsArray));
Lang Hames0000afd2015-06-26 23:56:53 +00001929 else
Philip Reamesf7acef92019-04-13 02:23:08 +00001930 prettyPrintStackMap(
1931 W, StackMapParser<support::big>(StackMapContentsArray));
Lang Hames0000afd2015-06-26 23:56:53 +00001932}
Reid Kleckner0b269742016-05-14 00:02:53 +00001933
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001934void COFFDumper::printAddrsig() {
1935 object::SectionRef AddrsigSection;
1936 for (auto Sec : Obj->sections()) {
1937 StringRef Name;
George Rimarbcc00e12019-08-14 11:10:11 +00001938 if (Expected<StringRef> NameOrErr = Sec.getName())
1939 Name = *NameOrErr;
1940 else
1941 consumeError(NameOrErr.takeError());
1942
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001943 if (Name == ".llvm_addrsig") {
1944 AddrsigSection = Sec;
1945 break;
1946 }
1947 }
1948
1949 if (AddrsigSection == object::SectionRef())
1950 return;
1951
George Rimare3d81fd2019-08-09 10:53:12 +00001952 StringRef AddrsigContents =
1953 unwrapOrError(Obj->getFileName(), AddrsigSection.getContents());
Fangrui Song6a0746a2019-04-07 03:58:42 +00001954 ArrayRef<uint8_t> AddrsigContentsArray(AddrsigContents.bytes_begin(),
1955 AddrsigContents.size());
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001956
1957 ListScope L(W, "Addrsig");
Fangrui Song6a0746a2019-04-07 03:58:42 +00001958 const uint8_t *Cur = AddrsigContents.bytes_begin();
1959 const uint8_t *End = AddrsigContents.bytes_end();
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001960 while (Cur != End) {
1961 unsigned Size;
1962 const char *Err;
1963 uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err);
1964 if (Err)
George Rimare3fb2d52019-08-17 16:07:18 +00001965 reportError(createError(Err), Obj->getFileName());
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001966
1967 Expected<COFFSymbolRef> Sym = Obj->getSymbol(SymIndex);
George Rimare54d3712019-08-22 08:56:24 +00001968 if (!Sym)
1969 reportError(Sym.takeError(), Obj->getFileName());
1970
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001971 StringRef SymName;
George Rimare54d3712019-08-22 08:56:24 +00001972 if (std::error_code EC = Obj->getSymbolName(*Sym, SymName))
1973 reportError(errorCodeToError(EC), Obj->getFileName());
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001974
1975 W.printNumber("Sym", SymName, SymIndex);
1976 Cur += Size;
1977 }
1978}
1979
Alexandre Ganea120366e2019-02-07 15:24:18 +00001980void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer,
1981 ArrayRef<ArrayRef<uint8_t>> IpiRecords,
1982 ArrayRef<ArrayRef<uint8_t>> TpiRecords) {
1983 TypeTableCollection TpiTypes(TpiRecords);
Reid Kleckner5d577522017-03-24 17:26:38 +00001984 {
1985 ListScope S(Writer, "MergedTypeStream");
Zachary Turner526f4f22017-05-19 19:26:58 +00001986 TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes);
George Rimar36f23182019-08-13 12:07:41 +00001987 if (Error Err = codeview::visitTypeStream(TpiTypes, TDV))
1988 reportError(std::move(Err), "<?>");
Zachary Turner526f4f22017-05-19 19:26:58 +00001989 Writer.flush();
Reid Kleckner5d577522017-03-24 17:26:38 +00001990 }
1991
1992 // Flatten the id stream and print it next. The ID stream refers to names from
1993 // the type stream.
Alexandre Ganea120366e2019-02-07 15:24:18 +00001994 TypeTableCollection IpiTypes(IpiRecords);
Reid Kleckner5d577522017-03-24 17:26:38 +00001995 {
1996 ListScope S(Writer, "MergedIDStream");
Zachary Turner526f4f22017-05-19 19:26:58 +00001997 TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes);
1998 TDV.setIpiTypes(IpiTypes);
George Rimar36f23182019-08-13 12:07:41 +00001999 if (Error Err = codeview::visitTypeStream(IpiTypes, TDV))
2000 reportError(std::move(Err), "<?>");
Zachary Turner526f4f22017-05-19 19:26:58 +00002001 Writer.flush();
Reid Kleckner0b269742016-05-14 00:02:53 +00002002 }
2003}