1 //===-- COFFDumper.cpp - COFF-specific dumper -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements the COFF-specific dumper for llvm-readobj.
13 //===----------------------------------------------------------------------===//
15 #include "ARMWinEHPrinter.h"
18 #include "ObjDumper.h"
19 #include "StackMapPrinter.h"
20 #include "Win64EHDumper.h"
21 #include "llvm-readobj.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/DebugInfo/CodeView/CVTypeDumper.h"
26 #include "llvm/DebugInfo/CodeView/CodeView.h"
27 #include "llvm/DebugInfo/CodeView/Line.h"
28 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
29 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
30 #include "llvm/DebugInfo/CodeView/SymbolDumpDelegate.h"
31 #include "llvm/DebugInfo/CodeView/SymbolDumper.h"
32 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
33 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
34 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
35 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
36 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
37 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
38 #include "llvm/Object/COFF.h"
39 #include "llvm/Object/ObjectFile.h"
40 #include "llvm/Support/BinaryByteStream.h"
41 #include "llvm/Support/COFF.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/Compiler.h"
44 #include "llvm/Support/DataExtractor.h"
45 #include "llvm/Support/Format.h"
46 #include "llvm/Support/Path.h"
47 #include "llvm/Support/ScopedPrinter.h"
48 #include "llvm/Support/SourceMgr.h"
49 #include "llvm/Support/Win64EH.h"
50 #include "llvm/Support/raw_ostream.h"
53 #include <system_error>
57 using namespace llvm::object;
58 using namespace llvm::codeview;
59 using namespace llvm::support;
60 using namespace llvm::Win64EH;
64 class COFFDumper : public ObjDumper {
66 friend class COFFObjectDumpDelegate;
67 COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer)
68 : ObjDumper(Writer), Obj(Obj), Writer(Writer) {}
70 void printFileHeaders() override;
71 void printSections() override;
72 void printRelocations() override;
73 void printSymbols() override;
74 void printDynamicSymbols() override;
75 void printUnwindInfo() override;
76 void printCOFFImports() override;
77 void printCOFFExports() override;
78 void printCOFFDirectives() override;
79 void printCOFFBaseReloc() override;
80 void printCOFFDebugDirectory() override;
81 void printCodeViewDebugInfo() override;
82 void mergeCodeViewTypes(llvm::codeview::TypeTableBuilder &CVIDs,
83 llvm::codeview::TypeTableBuilder &CVTypes) override;
84 void printStackMap() const override;
86 void printSymbol(const SymbolRef &Sym);
87 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc,
89 void printDataDirectory(uint32_t Index, const std::string &FieldName);
91 void printDOSHeader(const dos_header *DH);
92 template <class PEHeader> void printPEHeader(const PEHeader *Hdr);
93 void printBaseOfDataField(const pe32_header *Hdr);
94 void printBaseOfDataField(const pe32plus_header *Hdr);
96 void printCodeViewSymbolSection(StringRef SectionName, const SectionRef &Section);
97 void printCodeViewTypeSection(StringRef SectionName, const SectionRef &Section);
98 StringRef getTypeName(TypeIndex Ty);
99 StringRef getFileNameForFileOffset(uint32_t FileOffset);
100 void printFileNameForOffset(StringRef Label, uint32_t FileOffset);
101 void printTypeIndex(StringRef FieldName, TypeIndex TI) {
102 // Forward to CVTypeDumper for simplicity.
103 CVTypeDumper::printTypeIndex(Writer, FieldName, TI, TypeDB);
106 void printCodeViewSymbolsSubsection(StringRef Subsection,
107 const SectionRef &Section,
108 StringRef SectionContents);
110 void printCodeViewFileChecksums(StringRef Subsection);
112 void printCodeViewInlineeLines(StringRef Subsection);
114 void printRelocatedField(StringRef Label, const coff_section *Sec,
115 uint32_t RelocOffset, uint32_t Offset,
116 StringRef *RelocSym = nullptr);
118 void printBinaryBlockWithRelocs(StringRef Label, const SectionRef &Sec,
119 StringRef SectionContents, StringRef Block);
121 /// Given a .debug$S section, find the string table and file checksum table.
122 void initializeFileAndStringTables(StringRef Data);
124 void cacheRelocations();
126 std::error_code resolveSymbol(const coff_section *Section, uint64_t Offset,
128 std::error_code resolveSymbolName(const coff_section *Section,
129 uint64_t Offset, StringRef &Name);
130 std::error_code resolveSymbolName(const coff_section *Section,
131 StringRef SectionContents,
132 const void *RelocPtr, StringRef &Name);
133 void printImportedSymbols(iterator_range<imported_symbol_iterator> Range);
134 void printDelayImportedSymbols(
135 const DelayImportDirectoryEntryRef &I,
136 iterator_range<imported_symbol_iterator> Range);
138 typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy;
140 const llvm::object::COFFObjectFile *Obj;
141 bool RelocCached = false;
143 StringRef CVFileChecksumTable;
144 StringRef CVStringTable;
146 ScopedPrinter &Writer;
150 class COFFObjectDumpDelegate : public SymbolDumpDelegate {
152 COFFObjectDumpDelegate(COFFDumper &CD, const SectionRef &SR,
153 const COFFObjectFile *Obj, StringRef SectionContents)
154 : CD(CD), SR(SR), SectionContents(SectionContents) {
155 Sec = Obj->getCOFFSection(SR);
158 uint32_t getRecordOffset(BinaryStreamReader Reader) override {
159 ArrayRef<uint8_t> Data;
160 if (auto EC = Reader.readLongestContiguousChunk(Data)) {
161 llvm::consumeError(std::move(EC));
164 return Data.data() - SectionContents.bytes_begin();
167 void printRelocatedField(StringRef Label, uint32_t RelocOffset,
168 uint32_t Offset, StringRef *RelocSym) override {
169 CD.printRelocatedField(Label, Sec, RelocOffset, Offset, RelocSym);
172 void printBinaryBlockWithRelocs(StringRef Label,
173 ArrayRef<uint8_t> Block) override {
174 StringRef SBlock(reinterpret_cast<const char *>(Block.data()),
176 if (opts::CodeViewSubsectionBytes)
177 CD.printBinaryBlockWithRelocs(Label, SR, SectionContents, SBlock);
180 StringRef getFileNameForFileOffset(uint32_t FileOffset) override {
181 return CD.getFileNameForFileOffset(FileOffset);
184 StringRef getStringTable() override { return CD.CVStringTable; }
188 const SectionRef &SR;
189 const coff_section *Sec;
190 StringRef SectionContents;
197 std::error_code createCOFFDumper(const object::ObjectFile *Obj,
198 ScopedPrinter &Writer,
199 std::unique_ptr<ObjDumper> &Result) {
200 const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj);
202 return readobj_error::unsupported_obj_file_format;
204 Result.reset(new COFFDumper(COFFObj, Writer));
205 return readobj_error::success;
210 // Given a a section and an offset into this section the function returns the
211 // symbol used for the relocation at the offset.
212 std::error_code COFFDumper::resolveSymbol(const coff_section *Section,
213 uint64_t Offset, SymbolRef &Sym) {
215 const auto &Relocations = RelocMap[Section];
216 auto SymI = Obj->symbol_end();
217 for (const auto &Relocation : Relocations) {
218 uint64_t RelocationOffset = Relocation.getOffset();
220 if (RelocationOffset == Offset) {
221 SymI = Relocation.getSymbol();
225 if (SymI == Obj->symbol_end())
226 return readobj_error::unknown_symbol;
228 return readobj_error::success;
231 // Given a section and an offset into this section the function returns the name
232 // of the symbol used for the relocation at the offset.
233 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
237 if (std::error_code EC = resolveSymbol(Section, Offset, Symbol))
239 Expected<StringRef> NameOrErr = Symbol.getName();
241 return errorToErrorCode(NameOrErr.takeError());
243 return std::error_code();
246 // Helper for when you have a pointer to real data and you want to know about
247 // relocations against it.
248 std::error_code COFFDumper::resolveSymbolName(const coff_section *Section,
249 StringRef SectionContents,
250 const void *RelocPtr,
252 assert(SectionContents.data() < RelocPtr &&
253 RelocPtr < SectionContents.data() + SectionContents.size() &&
254 "pointer to relocated object is not in section");
255 uint64_t Offset = ptrdiff_t(reinterpret_cast<const char *>(RelocPtr) -
256 SectionContents.data());
257 return resolveSymbolName(Section, Offset, Name);
260 void COFFDumper::printRelocatedField(StringRef Label, const coff_section *Sec,
261 uint32_t RelocOffset, uint32_t Offset,
262 StringRef *RelocSym) {
263 StringRef SymStorage;
264 StringRef &Symbol = RelocSym ? *RelocSym : SymStorage;
265 if (!resolveSymbolName(Sec, RelocOffset, Symbol))
266 W.printSymbolOffset(Label, Symbol, Offset);
268 W.printHex(Label, RelocOffset);
271 void COFFDumper::printBinaryBlockWithRelocs(StringRef Label,
272 const SectionRef &Sec,
273 StringRef SectionContents,
275 W.printBinaryBlock(Label, Block);
277 assert(SectionContents.begin() < Block.begin() &&
278 SectionContents.end() >= Block.end() &&
279 "Block is not contained in SectionContents");
280 uint64_t OffsetStart = Block.data() - SectionContents.data();
281 uint64_t OffsetEnd = OffsetStart + Block.size();
285 ListScope D(W, "BlockRelocations");
286 const coff_section *Section = Obj->getCOFFSection(Sec);
287 const auto &Relocations = RelocMap[Section];
288 for (const auto &Relocation : Relocations) {
289 uint64_t RelocationOffset = Relocation.getOffset();
290 if (OffsetStart <= RelocationOffset && RelocationOffset < OffsetEnd)
291 printRelocation(Sec, Relocation, OffsetStart);
295 static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = {
296 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ),
297 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ),
298 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ),
299 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ),
300 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ),
301 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ),
302 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ),
303 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ),
304 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ),
305 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ),
306 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ),
307 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16),
308 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ),
309 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP),
310 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ),
311 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ),
312 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ),
313 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ),
314 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ),
315 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ),
316 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2)
319 static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = {
320 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ),
321 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ),
322 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ),
323 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ),
324 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ),
325 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ),
326 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ),
327 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ),
328 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ),
329 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP),
330 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ),
331 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ),
332 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ),
333 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ),
334 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI )
337 static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = {
338 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ),
339 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ),
340 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ),
341 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ),
342 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ),
343 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ),
344 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ),
345 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER),
346 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ),
347 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ),
348 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ),
351 static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = {
352 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ),
353 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ),
354 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ),
355 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ),
356 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ),
357 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ),
358 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ),
359 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_APPCONTAINER ),
360 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ),
361 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_GUARD_CF ),
362 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE),
365 static const EnumEntry<COFF::SectionCharacteristics>
366 ImageSectionCharacteristics[] = {
367 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NOLOAD ),
368 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ),
369 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ),
370 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ),
371 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA),
372 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ),
373 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ),
374 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ),
375 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ),
376 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ),
377 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ),
378 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ),
379 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ),
380 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ),
381 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ),
382 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ),
383 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ),
384 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ),
385 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ),
386 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ),
387 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ),
388 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ),
389 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ),
390 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ),
391 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ),
392 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ),
393 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ),
394 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ),
395 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ),
396 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ),
397 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ),
398 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ),
399 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ),
400 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ),
401 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ),
402 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE )
405 static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = {
406 { "Null" , COFF::IMAGE_SYM_TYPE_NULL },
407 { "Void" , COFF::IMAGE_SYM_TYPE_VOID },
408 { "Char" , COFF::IMAGE_SYM_TYPE_CHAR },
409 { "Short" , COFF::IMAGE_SYM_TYPE_SHORT },
410 { "Int" , COFF::IMAGE_SYM_TYPE_INT },
411 { "Long" , COFF::IMAGE_SYM_TYPE_LONG },
412 { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT },
413 { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE },
414 { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT },
415 { "Union" , COFF::IMAGE_SYM_TYPE_UNION },
416 { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM },
417 { "MOE" , COFF::IMAGE_SYM_TYPE_MOE },
418 { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE },
419 { "Word" , COFF::IMAGE_SYM_TYPE_WORD },
420 { "UInt" , COFF::IMAGE_SYM_TYPE_UINT },
421 { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD }
424 static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = {
425 { "Null" , COFF::IMAGE_SYM_DTYPE_NULL },
426 { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER },
427 { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION },
428 { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY }
431 static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = {
432 { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION },
433 { "Null" , COFF::IMAGE_SYM_CLASS_NULL },
434 { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC },
435 { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL },
436 { "Static" , COFF::IMAGE_SYM_CLASS_STATIC },
437 { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER },
438 { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF },
439 { "Label" , COFF::IMAGE_SYM_CLASS_LABEL },
440 { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL },
441 { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT },
442 { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT },
443 { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG },
444 { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION },
445 { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG },
446 { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION },
447 { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC },
448 { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG },
449 { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM },
450 { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM },
451 { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD },
452 { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK },
453 { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION },
454 { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT },
455 { "File" , COFF::IMAGE_SYM_CLASS_FILE },
456 { "Section" , COFF::IMAGE_SYM_CLASS_SECTION },
457 { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL },
458 { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN }
461 static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = {
462 { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES },
463 { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY },
464 { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE },
465 { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH },
466 { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE },
467 { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST },
468 { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST }
471 static const EnumEntry<COFF::DebugType> ImageDebugType[] = {
472 { "Unknown" , COFF::IMAGE_DEBUG_TYPE_UNKNOWN },
473 { "COFF" , COFF::IMAGE_DEBUG_TYPE_COFF },
474 { "CodeView" , COFF::IMAGE_DEBUG_TYPE_CODEVIEW },
475 { "FPO" , COFF::IMAGE_DEBUG_TYPE_FPO },
476 { "Misc" , COFF::IMAGE_DEBUG_TYPE_MISC },
477 { "Exception" , COFF::IMAGE_DEBUG_TYPE_EXCEPTION },
478 { "Fixup" , COFF::IMAGE_DEBUG_TYPE_FIXUP },
479 { "OmapToSrc" , COFF::IMAGE_DEBUG_TYPE_OMAP_TO_SRC },
480 { "OmapFromSrc", COFF::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC },
481 { "Borland" , COFF::IMAGE_DEBUG_TYPE_BORLAND },
482 { "Reserved10" , COFF::IMAGE_DEBUG_TYPE_RESERVED10 },
483 { "CLSID" , COFF::IMAGE_DEBUG_TYPE_CLSID },
484 { "VCFeature" , COFF::IMAGE_DEBUG_TYPE_VC_FEATURE },
485 { "POGO" , COFF::IMAGE_DEBUG_TYPE_POGO },
486 { "ILTCG" , COFF::IMAGE_DEBUG_TYPE_ILTCG },
487 { "MPX" , COFF::IMAGE_DEBUG_TYPE_MPX },
488 { "Repro" , COFF::IMAGE_DEBUG_TYPE_REPRO },
491 static const EnumEntry<COFF::WeakExternalCharacteristics>
492 WeakExternalCharacteristics[] = {
493 { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY },
494 { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY },
495 { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS }
498 static const EnumEntry<uint32_t> SubSectionTypes[] = {
499 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, Symbols),
500 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, Lines),
501 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, StringTable),
502 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FileChecksums),
503 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FrameData),
504 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, InlineeLines),
505 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CrossScopeImports),
506 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CrossScopeExports),
507 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, ILLines),
508 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, FuncMDTokenMap),
509 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, TypeMDTokenMap),
510 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, MergedAssemblyInput),
511 LLVM_READOBJ_ENUM_CLASS_ENT(ModuleSubstreamKind, CoffSymbolRVA),
514 static const EnumEntry<uint32_t> FrameDataFlags[] = {
515 LLVM_READOBJ_ENUM_ENT(FrameData, HasSEH),
516 LLVM_READOBJ_ENUM_ENT(FrameData, HasEH),
517 LLVM_READOBJ_ENUM_ENT(FrameData, IsFunctionStart),
520 static const EnumEntry<uint8_t> FileChecksumKindNames[] = {
521 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, None),
522 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, MD5),
523 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA1),
524 LLVM_READOBJ_ENUM_CLASS_ENT(FileChecksumKind, SHA256),
527 template <typename T>
528 static std::error_code getSymbolAuxData(const COFFObjectFile *Obj,
529 COFFSymbolRef Symbol,
530 uint8_t AuxSymbolIdx, const T *&Aux) {
531 ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol);
532 AuxData = AuxData.slice(AuxSymbolIdx * Obj->getSymbolTableEntrySize());
533 Aux = reinterpret_cast<const T*>(AuxData.data());
534 return readobj_error::success;
537 void COFFDumper::cacheRelocations() {
542 for (const SectionRef &S : Obj->sections()) {
543 const coff_section *Section = Obj->getCOFFSection(S);
545 for (const RelocationRef &Reloc : S.relocations())
546 RelocMap[Section].push_back(Reloc);
548 // Sort relocations by address.
549 std::sort(RelocMap[Section].begin(), RelocMap[Section].end(),
554 void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) {
555 const data_directory *Data;
556 if (Obj->getDataDirectory(Index, Data))
558 W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress);
559 W.printHex(FieldName + "Size", Data->Size);
562 void COFFDumper::printFileHeaders() {
563 time_t TDS = Obj->getTimeDateStamp();
564 char FormattedTime[20] = { };
565 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
568 DictScope D(W, "ImageFileHeader");
569 W.printEnum ("Machine", Obj->getMachine(),
570 makeArrayRef(ImageFileMachineType));
571 W.printNumber("SectionCount", Obj->getNumberOfSections());
572 W.printHex ("TimeDateStamp", FormattedTime, Obj->getTimeDateStamp());
573 W.printHex ("PointerToSymbolTable", Obj->getPointerToSymbolTable());
574 W.printNumber("SymbolCount", Obj->getNumberOfSymbols());
575 W.printNumber("OptionalHeaderSize", Obj->getSizeOfOptionalHeader());
576 W.printFlags ("Characteristics", Obj->getCharacteristics(),
577 makeArrayRef(ImageFileCharacteristics));
580 // Print PE header. This header does not exist if this is an object file and
581 // not an executable.
582 const pe32_header *PEHeader = nullptr;
583 error(Obj->getPE32Header(PEHeader));
585 printPEHeader<pe32_header>(PEHeader);
587 const pe32plus_header *PEPlusHeader = nullptr;
588 error(Obj->getPE32PlusHeader(PEPlusHeader));
590 printPEHeader<pe32plus_header>(PEPlusHeader);
592 if (const dos_header *DH = Obj->getDOSHeader())
596 void COFFDumper::printDOSHeader(const dos_header *DH) {
597 DictScope D(W, "DOSHeader");
598 W.printString("Magic", StringRef(DH->Magic, sizeof(DH->Magic)));
599 W.printNumber("UsedBytesInTheLastPage", DH->UsedBytesInTheLastPage);
600 W.printNumber("FileSizeInPages", DH->FileSizeInPages);
601 W.printNumber("NumberOfRelocationItems", DH->NumberOfRelocationItems);
602 W.printNumber("HeaderSizeInParagraphs", DH->HeaderSizeInParagraphs);
603 W.printNumber("MinimumExtraParagraphs", DH->MinimumExtraParagraphs);
604 W.printNumber("MaximumExtraParagraphs", DH->MaximumExtraParagraphs);
605 W.printNumber("InitialRelativeSS", DH->InitialRelativeSS);
606 W.printNumber("InitialSP", DH->InitialSP);
607 W.printNumber("Checksum", DH->Checksum);
608 W.printNumber("InitialIP", DH->InitialIP);
609 W.printNumber("InitialRelativeCS", DH->InitialRelativeCS);
610 W.printNumber("AddressOfRelocationTable", DH->AddressOfRelocationTable);
611 W.printNumber("OverlayNumber", DH->OverlayNumber);
612 W.printNumber("OEMid", DH->OEMid);
613 W.printNumber("OEMinfo", DH->OEMinfo);
614 W.printNumber("AddressOfNewExeHeader", DH->AddressOfNewExeHeader);
617 template <class PEHeader>
618 void COFFDumper::printPEHeader(const PEHeader *Hdr) {
619 DictScope D(W, "ImageOptionalHeader");
620 W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion);
621 W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion);
622 W.printNumber("SizeOfCode", Hdr->SizeOfCode);
623 W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData);
624 W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData);
625 W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint);
626 W.printHex ("BaseOfCode", Hdr->BaseOfCode);
627 printBaseOfDataField(Hdr);
628 W.printHex ("ImageBase", Hdr->ImageBase);
629 W.printNumber("SectionAlignment", Hdr->SectionAlignment);
630 W.printNumber("FileAlignment", Hdr->FileAlignment);
631 W.printNumber("MajorOperatingSystemVersion",
632 Hdr->MajorOperatingSystemVersion);
633 W.printNumber("MinorOperatingSystemVersion",
634 Hdr->MinorOperatingSystemVersion);
635 W.printNumber("MajorImageVersion", Hdr->MajorImageVersion);
636 W.printNumber("MinorImageVersion", Hdr->MinorImageVersion);
637 W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion);
638 W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion);
639 W.printNumber("SizeOfImage", Hdr->SizeOfImage);
640 W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders);
641 W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem));
642 W.printFlags ("Characteristics", Hdr->DLLCharacteristics,
643 makeArrayRef(PEDLLCharacteristics));
644 W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve);
645 W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit);
646 W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve);
647 W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit);
648 W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize);
650 if (Hdr->NumberOfRvaAndSize > 0) {
651 DictScope D(W, "DataDirectory");
652 static const char * const directory[] = {
653 "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable",
654 "CertificateTable", "BaseRelocationTable", "Debug", "Architecture",
655 "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT",
656 "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved"
659 for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i)
660 printDataDirectory(i, directory[i]);
664 void COFFDumper::printCOFFDebugDirectory() {
665 ListScope LS(W, "DebugDirectory");
666 for (const debug_directory &D : Obj->debug_directories()) {
667 char FormattedTime[20] = {};
668 time_t TDS = D.TimeDateStamp;
669 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
670 DictScope S(W, "DebugEntry");
671 W.printHex("Characteristics", D.Characteristics);
672 W.printHex("TimeDateStamp", FormattedTime, D.TimeDateStamp);
673 W.printHex("MajorVersion", D.MajorVersion);
674 W.printHex("MinorVersion", D.MinorVersion);
675 W.printEnum("Type", D.Type, makeArrayRef(ImageDebugType));
676 W.printHex("SizeOfData", D.SizeOfData);
677 W.printHex("AddressOfRawData", D.AddressOfRawData);
678 W.printHex("PointerToRawData", D.PointerToRawData);
679 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) {
680 const codeview::DebugInfo *DebugInfo;
681 StringRef PDBFileName;
682 error(Obj->getDebugPDBInfo(&D, DebugInfo, PDBFileName));
683 DictScope PDBScope(W, "PDBInfo");
684 W.printHex("PDBSignature", DebugInfo->Signature.CVSignature);
685 if (DebugInfo->Signature.CVSignature == OMF::Signature::PDB70) {
686 W.printBinary("PDBGUID", makeArrayRef(DebugInfo->PDB70.Signature));
687 W.printNumber("PDBAge", DebugInfo->PDB70.Age);
688 W.printString("PDBFileName", PDBFileName);
691 // FIXME: Type values of 12 and 13 are commonly observed but are not in
692 // the documented type enum. Figure out what they mean.
693 ArrayRef<uint8_t> RawData;
695 Obj->getRvaAndSizeAsBytes(D.AddressOfRawData, D.SizeOfData, RawData));
696 W.printBinaryBlock("RawData", RawData);
701 void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) {
702 W.printHex("BaseOfData", Hdr->BaseOfData);
705 void COFFDumper::printBaseOfDataField(const pe32plus_header *) {}
707 void COFFDumper::printCodeViewDebugInfo() {
708 // Print types first to build CVUDTNames, then print symbols.
709 for (const SectionRef &S : Obj->sections()) {
710 StringRef SectionName;
711 error(S.getName(SectionName));
712 if (SectionName == ".debug$T")
713 printCodeViewTypeSection(SectionName, S);
715 for (const SectionRef &S : Obj->sections()) {
716 StringRef SectionName;
717 error(S.getName(SectionName));
718 if (SectionName == ".debug$S")
719 printCodeViewSymbolSection(SectionName, S);
723 void COFFDumper::initializeFileAndStringTables(StringRef Data) {
724 while (!Data.empty() && (CVFileChecksumTable.data() == nullptr ||
725 CVStringTable.data() == nullptr)) {
726 // The section consists of a number of subsection in the following format:
727 // |SubSectionType|SubSectionSize|Contents...|
728 uint32_t SubType, SubSectionSize;
729 error(consume(Data, SubType));
730 error(consume(Data, SubSectionSize));
731 if (SubSectionSize > Data.size())
732 return error(object_error::parse_failed);
733 switch (ModuleSubstreamKind(SubType)) {
734 case ModuleSubstreamKind::FileChecksums:
735 CVFileChecksumTable = Data.substr(0, SubSectionSize);
737 case ModuleSubstreamKind::StringTable:
738 CVStringTable = Data.substr(0, SubSectionSize);
743 uint32_t PaddedSize = alignTo(SubSectionSize, 4);
744 if (PaddedSize > Data.size())
745 error(object_error::parse_failed);
746 Data = Data.drop_front(PaddedSize);
750 void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
751 const SectionRef &Section) {
752 StringRef SectionContents;
753 error(Section.getContents(SectionContents));
754 StringRef Data = SectionContents;
756 SmallVector<StringRef, 10> FunctionNames;
757 StringMap<StringRef> FunctionLineTables;
759 ListScope D(W, "CodeViewDebugInfo");
760 // Print the section to allow correlation with printSections.
761 W.printNumber("Section", SectionName, Obj->getSectionID(Section));
764 error(consume(Data, Magic));
765 W.printHex("Magic", Magic);
766 if (Magic != COFF::DEBUG_SECTION_MAGIC)
767 return error(object_error::parse_failed);
769 initializeFileAndStringTables(Data);
771 // TODO: Convert this over to using ModuleSubstreamVisitor.
772 while (!Data.empty()) {
773 // The section consists of a number of subsection in the following format:
774 // |SubSectionType|SubSectionSize|Contents...|
775 uint32_t SubType, SubSectionSize;
776 error(consume(Data, SubType));
777 error(consume(Data, SubSectionSize));
779 ListScope S(W, "Subsection");
780 W.printEnum("SubSectionType", SubType, makeArrayRef(SubSectionTypes));
781 W.printHex("SubSectionSize", SubSectionSize);
783 // Get the contents of the subsection.
784 if (SubSectionSize > Data.size())
785 return error(object_error::parse_failed);
786 StringRef Contents = Data.substr(0, SubSectionSize);
788 // Add SubSectionSize to the current offset and align that offset to find
789 // the next subsection.
790 size_t SectionOffset = Data.data() - SectionContents.data();
791 size_t NextOffset = SectionOffset + SubSectionSize;
792 NextOffset = alignTo(NextOffset, 4);
793 if (NextOffset > SectionContents.size())
794 return error(object_error::parse_failed);
795 Data = SectionContents.drop_front(NextOffset);
797 // Optionally print the subsection bytes in case our parsing gets confused
799 if (opts::CodeViewSubsectionBytes)
800 printBinaryBlockWithRelocs("SubSectionContents", Section, SectionContents,
803 switch (ModuleSubstreamKind(SubType)) {
804 case ModuleSubstreamKind::Symbols:
805 printCodeViewSymbolsSubsection(Contents, Section, SectionContents);
808 case ModuleSubstreamKind::InlineeLines:
809 printCodeViewInlineeLines(Contents);
812 case ModuleSubstreamKind::FileChecksums:
813 printCodeViewFileChecksums(Contents);
816 case ModuleSubstreamKind::Lines: {
817 // Holds a PC to file:line table. Some data to parse this subsection is
818 // stored in the other subsections, so just check sanity and store the
819 // pointers for deferred processing.
821 if (SubSectionSize < 12) {
822 // There should be at least three words to store two function
823 // relocations and size of the code.
824 error(object_error::parse_failed);
828 StringRef LinkageName;
829 error(resolveSymbolName(Obj->getCOFFSection(Section), SectionOffset,
831 W.printString("LinkageName", LinkageName);
832 if (FunctionLineTables.count(LinkageName) != 0) {
833 // Saw debug info for this function already?
834 error(object_error::parse_failed);
838 FunctionLineTables[LinkageName] = Contents;
839 FunctionNames.push_back(LinkageName);
842 case ModuleSubstreamKind::FrameData: {
843 // First four bytes is a relocation against the function.
844 BinaryByteStream S(Contents, llvm::support::little);
845 BinaryStreamReader SR(S);
846 const uint32_t *CodePtr;
847 error(SR.readObject(CodePtr));
848 StringRef LinkageName;
849 error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents,
850 CodePtr, LinkageName));
851 W.printString("LinkageName", LinkageName);
853 // To find the active frame description, search this array for the
854 // smallest PC range that includes the current PC.
855 while (!SR.empty()) {
857 error(SR.readObject(FD));
859 if (FD->FrameFunc >= CVStringTable.size())
860 error(object_error::parse_failed);
862 StringRef FrameFunc =
863 CVStringTable.drop_front(FD->FrameFunc).split('\0').first;
865 DictScope S(W, "FrameData");
866 W.printHex("RvaStart", FD->RvaStart);
867 W.printHex("CodeSize", FD->CodeSize);
868 W.printHex("LocalSize", FD->LocalSize);
869 W.printHex("ParamsSize", FD->ParamsSize);
870 W.printHex("MaxStackSize", FD->MaxStackSize);
871 W.printString("FrameFunc", FrameFunc);
872 W.printHex("PrologSize", FD->PrologSize);
873 W.printHex("SavedRegsSize", FD->SavedRegsSize);
874 W.printFlags("Flags", FD->Flags, makeArrayRef(FrameDataFlags));
879 // Do nothing for unrecognized subsections.
886 // Dump the line tables now that we've read all the subsections and know all
887 // the required information.
888 for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) {
889 StringRef Name = FunctionNames[I];
890 ListScope S(W, "FunctionLineTable");
891 W.printString("LinkageName", Name);
893 DataExtractor DE(FunctionLineTables[Name], true, 4);
894 uint32_t Offset = 6; // Skip relocations.
895 uint16_t Flags = DE.getU16(&Offset);
896 W.printHex("Flags", Flags);
897 bool HasColumnInformation = Flags & codeview::LineFlags::HaveColumns;
898 uint32_t FunctionSize = DE.getU32(&Offset);
899 W.printHex("CodeSize", FunctionSize);
900 while (DE.isValidOffset(Offset)) {
901 // For each range of lines with the same filename, we have a segment
902 // in the line table. The filename string is accessed using double
903 // indirection to the string table subsection using the index subsection.
904 uint32_t OffsetInIndex = DE.getU32(&Offset),
905 NumLines = DE.getU32(&Offset),
906 FullSegmentSize = DE.getU32(&Offset);
908 uint32_t ColumnOffset = Offset + 8 * NumLines;
909 DataExtractor ColumnDE(DE.getData(), true, 4);
911 if (FullSegmentSize !=
912 12 + 8 * NumLines + (HasColumnInformation ? 4 * NumLines : 0)) {
913 error(object_error::parse_failed);
917 ListScope S(W, "FilenameSegment");
918 printFileNameForOffset("Filename", OffsetInIndex);
919 for (unsigned LineIdx = 0;
920 LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) {
921 // Then go the (PC, LineNumber) pairs. The line number is stored in the
922 // least significant 31 bits of the respective word in the table.
923 uint32_t PC = DE.getU32(&Offset), LineData = DE.getU32(&Offset);
924 if (PC >= FunctionSize) {
925 error(object_error::parse_failed);
929 format("+0x%X", PC).snprint(Buffer, 32);
930 ListScope PCScope(W, Buffer);
931 LineInfo LI(LineData);
932 if (LI.isAlwaysStepInto())
933 W.printString("StepInto", StringRef("Always"));
934 else if (LI.isNeverStepInto())
935 W.printString("StepInto", StringRef("Never"));
937 W.printNumber("LineNumberStart", LI.getStartLine());
938 W.printNumber("LineNumberEndDelta", LI.getLineDelta());
939 W.printBoolean("IsStatement", LI.isStatement());
940 if (HasColumnInformation &&
941 ColumnDE.isValidOffsetForDataOfSize(ColumnOffset, 4)) {
942 uint16_t ColStart = ColumnDE.getU16(&ColumnOffset);
943 W.printNumber("ColStart", ColStart);
944 uint16_t ColEnd = ColumnDE.getU16(&ColumnOffset);
945 W.printNumber("ColEnd", ColEnd);
948 // Skip over the column data.
949 if (HasColumnInformation) {
950 for (unsigned LineIdx = 0;
951 LineIdx != NumLines && DE.isValidOffset(Offset); ++LineIdx) {
959 void COFFDumper::printCodeViewSymbolsSubsection(StringRef Subsection,
960 const SectionRef &Section,
961 StringRef SectionContents) {
962 ArrayRef<uint8_t> BinaryData(Subsection.bytes_begin(),
963 Subsection.bytes_end());
964 auto CODD = llvm::make_unique<COFFObjectDumpDelegate>(*this, Section, Obj,
967 CVSymbolDumper CVSD(W, TypeDB, std::move(CODD),
968 opts::CodeViewSubsectionBytes);
969 BinaryByteStream Stream(BinaryData, llvm::support::little);
970 CVSymbolArray Symbols;
971 BinaryStreamReader Reader(Stream);
972 if (auto EC = Reader.readArray(Symbols, Reader.getLength())) {
973 consumeError(std::move(EC));
975 error(object_error::parse_failed);
978 if (auto EC = CVSD.dump(Symbols)) {
980 error(std::move(EC));
985 void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) {
986 BinaryByteStream S(Subsection, llvm::support::little);
987 BinaryStreamReader SR(S);
988 while (!SR.empty()) {
989 DictScope S(W, "FileChecksum");
990 const FileChecksum *FC;
991 error(SR.readObject(FC));
992 if (FC->FileNameOffset >= CVStringTable.size())
993 error(object_error::parse_failed);
995 CVStringTable.drop_front(FC->FileNameOffset).split('\0').first;
996 W.printHex("Filename", Filename, FC->FileNameOffset);
997 W.printHex("ChecksumSize", FC->ChecksumSize);
998 W.printEnum("ChecksumKind", uint8_t(FC->ChecksumKind),
999 makeArrayRef(FileChecksumKindNames));
1000 if (FC->ChecksumSize >= SR.bytesRemaining())
1001 error(object_error::parse_failed);
1002 ArrayRef<uint8_t> ChecksumBytes;
1003 error(SR.readBytes(ChecksumBytes, FC->ChecksumSize));
1004 W.printBinary("ChecksumBytes", ChecksumBytes);
1005 unsigned PaddedSize = alignTo(FC->ChecksumSize + sizeof(FileChecksum), 4) -
1006 sizeof(FileChecksum);
1007 PaddedSize -= ChecksumBytes.size();
1008 if (PaddedSize > SR.bytesRemaining())
1009 error(object_error::parse_failed);
1010 error(SR.skip(PaddedSize));
1014 void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) {
1015 BinaryByteStream S(Subsection, llvm::support::little);
1016 BinaryStreamReader SR(S);
1018 error(SR.readInteger(Signature));
1019 bool HasExtraFiles = Signature == unsigned(InlineeLinesSignature::ExtraFiles);
1021 while (!SR.empty()) {
1022 const InlineeSourceLine *ISL;
1023 error(SR.readObject(ISL));
1024 DictScope S(W, "InlineeSourceLine");
1025 printTypeIndex("Inlinee", ISL->Inlinee);
1026 printFileNameForOffset("FileID", ISL->FileID);
1027 W.printNumber("SourceLineNum", ISL->SourceLineNum);
1029 if (HasExtraFiles) {
1030 uint32_t ExtraFileCount;
1031 error(SR.readInteger(ExtraFileCount));
1032 W.printNumber("ExtraFileCount", ExtraFileCount);
1033 ListScope ExtraFiles(W, "ExtraFiles");
1034 for (unsigned I = 0; I < ExtraFileCount; ++I) {
1036 error(SR.readInteger(FileID));
1037 printFileNameForOffset("FileID", FileID);
1043 StringRef COFFDumper::getFileNameForFileOffset(uint32_t FileOffset) {
1044 // The file checksum subsection should precede all references to it.
1045 if (!CVFileChecksumTable.data() || !CVStringTable.data())
1046 error(object_error::parse_failed);
1047 // Check if the file checksum table offset is valid.
1048 if (FileOffset >= CVFileChecksumTable.size())
1049 error(object_error::parse_failed);
1051 // The string table offset comes first before the file checksum.
1052 StringRef Data = CVFileChecksumTable.drop_front(FileOffset);
1053 uint32_t StringOffset;
1054 error(consume(Data, StringOffset));
1056 // Check if the string table offset is valid.
1057 if (StringOffset >= CVStringTable.size())
1058 error(object_error::parse_failed);
1060 // Return the null-terminated string.
1061 return CVStringTable.drop_front(StringOffset).split('\0').first;
1064 void COFFDumper::printFileNameForOffset(StringRef Label, uint32_t FileOffset) {
1065 W.printHex(Label, getFileNameForFileOffset(FileOffset), FileOffset);
1068 void COFFDumper::mergeCodeViewTypes(TypeTableBuilder &CVIDs,
1069 TypeTableBuilder &CVTypes) {
1070 for (const SectionRef &S : Obj->sections()) {
1071 StringRef SectionName;
1072 error(S.getName(SectionName));
1073 if (SectionName == ".debug$T") {
1075 error(S.getContents(Data));
1077 error(consume(Data, Magic));
1079 error(object_error::parse_failed);
1080 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(Data.data()),
1082 BinaryByteStream Stream(Bytes, llvm::support::little);
1084 BinaryStreamReader Reader(Stream);
1085 if (auto EC = Reader.readArray(Types, Reader.getLength())) {
1086 consumeError(std::move(EC));
1088 error(object_error::parse_failed);
1091 if (auto EC = mergeTypeStreams(CVIDs, CVTypes, nullptr, Types))
1092 return error(std::move(EC));
1097 void COFFDumper::printCodeViewTypeSection(StringRef SectionName,
1098 const SectionRef &Section) {
1099 ListScope D(W, "CodeViewTypes");
1100 W.printNumber("Section", SectionName, Obj->getSectionID(Section));
1103 error(Section.getContents(Data));
1104 if (opts::CodeViewSubsectionBytes)
1105 W.printBinaryBlock("Data", Data);
1108 error(consume(Data, Magic));
1109 W.printHex("Magic", Magic);
1110 if (Magic != COFF::DEBUG_SECTION_MAGIC)
1111 return error(object_error::parse_failed);
1113 CVTypeDumper CVTD(TypeDB);
1114 TypeDumpVisitor TDV(TypeDB, &W, opts::CodeViewSubsectionBytes);
1115 if (auto EC = CVTD.dump({Data.bytes_begin(), Data.bytes_end()}, TDV)) {
1117 error(llvm::errorToErrorCode(std::move(EC)));
1121 void COFFDumper::printSections() {
1122 ListScope SectionsD(W, "Sections");
1123 int SectionNumber = 0;
1124 for (const SectionRef &Sec : Obj->sections()) {
1126 const coff_section *Section = Obj->getCOFFSection(Sec);
1129 error(Sec.getName(Name));
1131 DictScope D(W, "Section");
1132 W.printNumber("Number", SectionNumber);
1133 W.printBinary("Name", Name, Section->Name);
1134 W.printHex ("VirtualSize", Section->VirtualSize);
1135 W.printHex ("VirtualAddress", Section->VirtualAddress);
1136 W.printNumber("RawDataSize", Section->SizeOfRawData);
1137 W.printHex ("PointerToRawData", Section->PointerToRawData);
1138 W.printHex ("PointerToRelocations", Section->PointerToRelocations);
1139 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers);
1140 W.printNumber("RelocationCount", Section->NumberOfRelocations);
1141 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers);
1142 W.printFlags ("Characteristics", Section->Characteristics,
1143 makeArrayRef(ImageSectionCharacteristics),
1144 COFF::SectionCharacteristics(0x00F00000));
1146 if (opts::SectionRelocations) {
1147 ListScope D(W, "Relocations");
1148 for (const RelocationRef &Reloc : Sec.relocations())
1149 printRelocation(Sec, Reloc);
1152 if (opts::SectionSymbols) {
1153 ListScope D(W, "Symbols");
1154 for (const SymbolRef &Symbol : Obj->symbols()) {
1155 if (!Sec.containsSymbol(Symbol))
1158 printSymbol(Symbol);
1162 if (opts::SectionData &&
1163 !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
1165 error(Sec.getContents(Data));
1167 W.printBinaryBlock("SectionData", Data);
1172 void COFFDumper::printRelocations() {
1173 ListScope D(W, "Relocations");
1175 int SectionNumber = 0;
1176 for (const SectionRef &Section : Obj->sections()) {
1179 error(Section.getName(Name));
1181 bool PrintedGroup = false;
1182 for (const RelocationRef &Reloc : Section.relocations()) {
1183 if (!PrintedGroup) {
1184 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
1186 PrintedGroup = true;
1189 printRelocation(Section, Reloc);
1194 W.startLine() << "}\n";
1199 void COFFDumper::printRelocation(const SectionRef &Section,
1200 const RelocationRef &Reloc, uint64_t Bias) {
1201 uint64_t Offset = Reloc.getOffset() - Bias;
1202 uint64_t RelocType = Reloc.getType();
1203 SmallString<32> RelocName;
1204 StringRef SymbolName;
1205 Reloc.getTypeName(RelocName);
1206 symbol_iterator Symbol = Reloc.getSymbol();
1207 if (Symbol != Obj->symbol_end()) {
1208 Expected<StringRef> SymbolNameOrErr = Symbol->getName();
1209 error(errorToErrorCode(SymbolNameOrErr.takeError()));
1210 SymbolName = *SymbolNameOrErr;
1213 if (opts::ExpandRelocs) {
1214 DictScope Group(W, "Relocation");
1215 W.printHex("Offset", Offset);
1216 W.printNumber("Type", RelocName, RelocType);
1217 W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName);
1219 raw_ostream& OS = W.startLine();
1222 << " " << (SymbolName.empty() ? "-" : SymbolName)
1227 void COFFDumper::printSymbols() {
1228 ListScope Group(W, "Symbols");
1230 for (const SymbolRef &Symbol : Obj->symbols())
1231 printSymbol(Symbol);
1234 void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); }
1236 static ErrorOr<StringRef>
1237 getSectionName(const llvm::object::COFFObjectFile *Obj, int32_t SectionNumber,
1238 const coff_section *Section) {
1240 StringRef SectionName;
1241 if (std::error_code EC = Obj->getSectionName(Section, SectionName))
1245 if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
1246 return StringRef("IMAGE_SYM_DEBUG");
1247 if (SectionNumber == llvm::COFF::IMAGE_SYM_ABSOLUTE)
1248 return StringRef("IMAGE_SYM_ABSOLUTE");
1249 if (SectionNumber == llvm::COFF::IMAGE_SYM_UNDEFINED)
1250 return StringRef("IMAGE_SYM_UNDEFINED");
1251 return StringRef("");
1254 void COFFDumper::printSymbol(const SymbolRef &Sym) {
1255 DictScope D(W, "Symbol");
1257 COFFSymbolRef Symbol = Obj->getCOFFSymbol(Sym);
1258 const coff_section *Section;
1259 if (std::error_code EC = Obj->getSection(Symbol.getSectionNumber(), Section)) {
1260 W.startLine() << "Invalid section number: " << EC.message() << "\n";
1265 StringRef SymbolName;
1266 if (Obj->getSymbolName(Symbol, SymbolName))
1269 StringRef SectionName = "";
1270 ErrorOr<StringRef> Res =
1271 getSectionName(Obj, Symbol.getSectionNumber(), Section);
1275 W.printString("Name", SymbolName);
1276 W.printNumber("Value", Symbol.getValue());
1277 W.printNumber("Section", SectionName, Symbol.getSectionNumber());
1278 W.printEnum ("BaseType", Symbol.getBaseType(), makeArrayRef(ImageSymType));
1279 W.printEnum ("ComplexType", Symbol.getComplexType(),
1280 makeArrayRef(ImageSymDType));
1281 W.printEnum ("StorageClass", Symbol.getStorageClass(),
1282 makeArrayRef(ImageSymClass));
1283 W.printNumber("AuxSymbolCount", Symbol.getNumberOfAuxSymbols());
1285 for (uint8_t I = 0; I < Symbol.getNumberOfAuxSymbols(); ++I) {
1286 if (Symbol.isFunctionDefinition()) {
1287 const coff_aux_function_definition *Aux;
1288 error(getSymbolAuxData(Obj, Symbol, I, Aux));
1290 DictScope AS(W, "AuxFunctionDef");
1291 W.printNumber("TagIndex", Aux->TagIndex);
1292 W.printNumber("TotalSize", Aux->TotalSize);
1293 W.printHex("PointerToLineNumber", Aux->PointerToLinenumber);
1294 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction);
1296 } else if (Symbol.isAnyUndefined()) {
1297 const coff_aux_weak_external *Aux;
1298 error(getSymbolAuxData(Obj, Symbol, I, Aux));
1300 ErrorOr<COFFSymbolRef> Linked = Obj->getSymbol(Aux->TagIndex);
1301 StringRef LinkedName;
1302 std::error_code EC = Linked.getError();
1303 if (EC || (EC = Obj->getSymbolName(*Linked, LinkedName))) {
1308 DictScope AS(W, "AuxWeakExternal");
1309 W.printNumber("Linked", LinkedName, Aux->TagIndex);
1310 W.printEnum ("Search", Aux->Characteristics,
1311 makeArrayRef(WeakExternalCharacteristics));
1313 } else if (Symbol.isFileRecord()) {
1314 const char *FileName;
1315 error(getSymbolAuxData(Obj, Symbol, I, FileName));
1317 DictScope AS(W, "AuxFileRecord");
1319 StringRef Name(FileName, Symbol.getNumberOfAuxSymbols() *
1320 Obj->getSymbolTableEntrySize());
1321 W.printString("FileName", Name.rtrim(StringRef("\0", 1)));
1323 } else if (Symbol.isSectionDefinition()) {
1324 const coff_aux_section_definition *Aux;
1325 error(getSymbolAuxData(Obj, Symbol, I, Aux));
1327 int32_t AuxNumber = Aux->getNumber(Symbol.isBigObj());
1329 DictScope AS(W, "AuxSectionDef");
1330 W.printNumber("Length", Aux->Length);
1331 W.printNumber("RelocationCount", Aux->NumberOfRelocations);
1332 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers);
1333 W.printHex("Checksum", Aux->CheckSum);
1334 W.printNumber("Number", AuxNumber);
1335 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect));
1337 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT
1338 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
1339 const coff_section *Assoc;
1340 StringRef AssocName = "";
1341 std::error_code EC = Obj->getSection(AuxNumber, Assoc);
1342 ErrorOr<StringRef> Res = getSectionName(Obj, AuxNumber, Assoc);
1346 EC = Res.getError();
1352 W.printNumber("AssocSection", AssocName, AuxNumber);
1354 } else if (Symbol.isCLRToken()) {
1355 const coff_aux_clr_token *Aux;
1356 error(getSymbolAuxData(Obj, Symbol, I, Aux));
1358 ErrorOr<COFFSymbolRef> ReferredSym =
1359 Obj->getSymbol(Aux->SymbolTableIndex);
1360 StringRef ReferredName;
1361 std::error_code EC = ReferredSym.getError();
1362 if (EC || (EC = Obj->getSymbolName(*ReferredSym, ReferredName))) {
1367 DictScope AS(W, "AuxCLRToken");
1368 W.printNumber("AuxType", Aux->AuxType);
1369 W.printNumber("Reserved", Aux->Reserved);
1370 W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex);
1373 W.startLine() << "<unhandled auxiliary record>\n";
1378 void COFFDumper::printUnwindInfo() {
1379 ListScope D(W, "UnwindInformation");
1380 switch (Obj->getMachine()) {
1381 case COFF::IMAGE_FILE_MACHINE_AMD64: {
1382 Win64EH::Dumper Dumper(W);
1383 Win64EH::Dumper::SymbolResolver
1384 Resolver = [](const object::coff_section *Section, uint64_t Offset,
1385 SymbolRef &Symbol, void *user_data) -> std::error_code {
1386 COFFDumper *Dumper = reinterpret_cast<COFFDumper *>(user_data);
1387 return Dumper->resolveSymbol(Section, Offset, Symbol);
1389 Win64EH::Dumper::Context Ctx(*Obj, Resolver, this);
1390 Dumper.printData(Ctx);
1393 case COFF::IMAGE_FILE_MACHINE_ARMNT: {
1394 ARM::WinEH::Decoder Decoder(W);
1395 Decoder.dumpProcedureData(*Obj);
1399 W.printEnum("unsupported Image Machine", Obj->getMachine(),
1400 makeArrayRef(ImageFileMachineType));
1405 void COFFDumper::printImportedSymbols(
1406 iterator_range<imported_symbol_iterator> Range) {
1407 for (const ImportedSymbolRef &I : Range) {
1409 error(I.getSymbolName(Sym));
1411 error(I.getOrdinal(Ordinal));
1412 W.printNumber("Symbol", Sym, Ordinal);
1416 void COFFDumper::printDelayImportedSymbols(
1417 const DelayImportDirectoryEntryRef &I,
1418 iterator_range<imported_symbol_iterator> Range) {
1420 for (const ImportedSymbolRef &S : Range) {
1421 DictScope Import(W, "Import");
1423 error(S.getSymbolName(Sym));
1425 error(S.getOrdinal(Ordinal));
1426 W.printNumber("Symbol", Sym, Ordinal);
1428 error(I.getImportAddress(Index++, Addr));
1429 W.printHex("Address", Addr);
1433 void COFFDumper::printCOFFImports() {
1435 for (const ImportDirectoryEntryRef &I : Obj->import_directories()) {
1436 DictScope Import(W, "Import");
1438 error(I.getName(Name));
1439 W.printString("Name", Name);
1441 error(I.getImportLookupTableRVA(ILTAddr));
1442 W.printHex("ImportLookupTableRVA", ILTAddr);
1444 error(I.getImportAddressTableRVA(IATAddr));
1445 W.printHex("ImportAddressTableRVA", IATAddr);
1446 // The import lookup table can be missing with certain older linkers, so
1447 // fall back to the import address table in that case.
1449 printImportedSymbols(I.lookup_table_symbols());
1451 printImportedSymbols(I.imported_symbols());
1455 for (const DelayImportDirectoryEntryRef &I : Obj->delay_import_directories()) {
1456 DictScope Import(W, "DelayImport");
1458 error(I.getName(Name));
1459 W.printString("Name", Name);
1460 const delay_import_directory_table_entry *Table;
1461 error(I.getDelayImportTable(Table));
1462 W.printHex("Attributes", Table->Attributes);
1463 W.printHex("ModuleHandle", Table->ModuleHandle);
1464 W.printHex("ImportAddressTable", Table->DelayImportAddressTable);
1465 W.printHex("ImportNameTable", Table->DelayImportNameTable);
1466 W.printHex("BoundDelayImportTable", Table->BoundDelayImportTable);
1467 W.printHex("UnloadDelayImportTable", Table->UnloadDelayImportTable);
1468 printDelayImportedSymbols(I, I.imported_symbols());
1472 void COFFDumper::printCOFFExports() {
1473 for (const ExportDirectoryEntryRef &E : Obj->export_directories()) {
1474 DictScope Export(W, "Export");
1477 uint32_t Ordinal, RVA;
1479 error(E.getSymbolName(Name));
1480 error(E.getOrdinal(Ordinal));
1481 error(E.getExportRVA(RVA));
1483 W.printNumber("Ordinal", Ordinal);
1484 W.printString("Name", Name);
1485 W.printHex("RVA", RVA);
1489 void COFFDumper::printCOFFDirectives() {
1490 for (const SectionRef &Section : Obj->sections()) {
1494 error(Section.getName(Name));
1495 if (Name != ".drectve")
1498 error(Section.getContents(Contents));
1500 W.printString("Directive(s)", Contents);
1504 static StringRef getBaseRelocTypeName(uint8_t Type) {
1506 case COFF::IMAGE_REL_BASED_ABSOLUTE: return "ABSOLUTE";
1507 case COFF::IMAGE_REL_BASED_HIGH: return "HIGH";
1508 case COFF::IMAGE_REL_BASED_LOW: return "LOW";
1509 case COFF::IMAGE_REL_BASED_HIGHLOW: return "HIGHLOW";
1510 case COFF::IMAGE_REL_BASED_HIGHADJ: return "HIGHADJ";
1511 case COFF::IMAGE_REL_BASED_ARM_MOV32T: return "ARM_MOV32(T)";
1512 case COFF::IMAGE_REL_BASED_DIR64: return "DIR64";
1513 default: return "unknown (" + llvm::utostr(Type) + ")";
1517 void COFFDumper::printCOFFBaseReloc() {
1518 ListScope D(W, "BaseReloc");
1519 for (const BaseRelocRef &I : Obj->base_relocs()) {
1522 error(I.getRVA(RVA));
1523 error(I.getType(Type));
1524 DictScope Import(W, "Entry");
1525 W.printString("Type", getBaseRelocTypeName(Type));
1526 W.printHex("Address", RVA);
1530 void COFFDumper::printStackMap() const {
1531 object::SectionRef StackMapSection;
1532 for (auto Sec : Obj->sections()) {
1535 if (Name == ".llvm_stackmaps") {
1536 StackMapSection = Sec;
1541 if (StackMapSection == object::SectionRef())
1544 StringRef StackMapContents;
1545 StackMapSection.getContents(StackMapContents);
1546 ArrayRef<uint8_t> StackMapContentsArray(
1547 reinterpret_cast<const uint8_t*>(StackMapContents.data()),
1548 StackMapContents.size());
1550 if (Obj->isLittleEndian())
1551 prettyPrintStackMap(
1553 StackMapV2Parser<support::little>(StackMapContentsArray));
1555 prettyPrintStackMap(llvm::outs(),
1556 StackMapV2Parser<support::big>(StackMapContentsArray));
1559 void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer,
1560 llvm::codeview::TypeTableBuilder &IDTable,
1561 llvm::codeview::TypeTableBuilder &CVTypes) {
1562 // Flatten it first, then run our dumper on it.
1563 SmallString<0> TypeBuf;
1564 CVTypes.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Record) {
1565 TypeBuf.append(Record.begin(), Record.end());
1568 TypeDatabase TypeDB;
1570 ListScope S(Writer, "MergedTypeStream");
1571 CVTypeDumper CVTD(TypeDB);
1572 TypeDumpVisitor TDV(TypeDB, &Writer, opts::CodeViewSubsectionBytes);
1573 if (auto EC = CVTD.dump(
1574 {TypeBuf.str().bytes_begin(), TypeBuf.str().bytes_end()}, TDV)) {
1576 error(std::move(EC));
1580 // Flatten the id stream and print it next. The ID stream refers to names from
1582 SmallString<0> IDBuf;
1583 IDTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Record) {
1584 IDBuf.append(Record.begin(), Record.end());
1588 ListScope S(Writer, "MergedIDStream");
1590 CVTypeDumper CVTD(IDDB);
1591 TypeDumpVisitor TDV(TypeDB, &Writer, opts::CodeViewSubsectionBytes);
1592 TDV.setItemDB(IDDB);
1593 if (auto EC = CVTD.dump(
1594 {IDBuf.str().bytes_begin(), IDBuf.str().bytes_end()}, TDV)) {
1596 error(std::move(EC));