1 //===-- COFFDump.cpp - COFF-specific dumper ---------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file implements the COFF-specific dumper for llvm-objdump.
11 /// It outputs the Win64 EH data structures as plain text.
12 /// The encoding of the unwind codes is described in MSDN:
13 /// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx
15 //===----------------------------------------------------------------------===//
17 #include "llvm-objdump.h"
18 #include "llvm/Demangle/Demangle.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Object/COFFImportFile.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/Win64EH.h"
24 #include "llvm/Support/WithColor.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace llvm::object
;
28 using namespace llvm::Win64EH
;
31 // Returns the name of the unwind code.
32 static StringRef
getUnwindCodeTypeName(uint8_t Code
) {
34 default: llvm_unreachable("Invalid unwind code");
35 case UOP_PushNonVol
: return "UOP_PushNonVol";
36 case UOP_AllocLarge
: return "UOP_AllocLarge";
37 case UOP_AllocSmall
: return "UOP_AllocSmall";
38 case UOP_SetFPReg
: return "UOP_SetFPReg";
39 case UOP_SaveNonVol
: return "UOP_SaveNonVol";
40 case UOP_SaveNonVolBig
: return "UOP_SaveNonVolBig";
41 case UOP_SaveXMM128
: return "UOP_SaveXMM128";
42 case UOP_SaveXMM128Big
: return "UOP_SaveXMM128Big";
43 case UOP_PushMachFrame
: return "UOP_PushMachFrame";
47 // Returns the name of a referenced register.
48 static StringRef
getUnwindRegisterName(uint8_t Reg
) {
50 default: llvm_unreachable("Invalid register");
61 case 10: return "R10";
62 case 11: return "R11";
63 case 12: return "R12";
64 case 13: return "R13";
65 case 14: return "R14";
66 case 15: return "R15";
70 // Calculates the number of array slots required for the unwind code.
71 static unsigned getNumUsedSlots(const UnwindCode
&UnwindCode
) {
72 switch (UnwindCode
.getUnwindOp()) {
73 default: llvm_unreachable("Invalid unwind code");
77 case UOP_PushMachFrame
:
82 case UOP_SaveNonVolBig
:
83 case UOP_SaveXMM128Big
:
86 return (UnwindCode
.getOpInfo() == 0) ? 2 : 3;
90 // Prints one unwind code. Because an unwind code can occupy up to 3 slots in
91 // the unwind codes array, this function requires that the correct number of
93 static void printUnwindCode(ArrayRef
<UnwindCode
> UCs
) {
94 assert(UCs
.size() >= getNumUsedSlots(UCs
[0]));
95 outs() << format(" 0x%02x: ", unsigned(UCs
[0].u
.CodeOffset
))
96 << getUnwindCodeTypeName(UCs
[0].getUnwindOp());
97 switch (UCs
[0].getUnwindOp()) {
99 outs() << " " << getUnwindRegisterName(UCs
[0].getOpInfo());
102 if (UCs
[0].getOpInfo() == 0) {
103 outs() << " " << UCs
[1].FrameOffset
;
105 outs() << " " << UCs
[1].FrameOffset
106 + (static_cast<uint32_t>(UCs
[2].FrameOffset
) << 16);
110 outs() << " " << ((UCs
[0].getOpInfo() + 1) * 8);
116 outs() << " " << getUnwindRegisterName(UCs
[0].getOpInfo())
117 << format(" [0x%04x]", 8 * UCs
[1].FrameOffset
);
119 case UOP_SaveNonVolBig
:
120 outs() << " " << getUnwindRegisterName(UCs
[0].getOpInfo())
121 << format(" [0x%08x]", UCs
[1].FrameOffset
122 + (static_cast<uint32_t>(UCs
[2].FrameOffset
) << 16));
125 outs() << " XMM" << static_cast<uint32_t>(UCs
[0].getOpInfo())
126 << format(" [0x%04x]", 16 * UCs
[1].FrameOffset
);
128 case UOP_SaveXMM128Big
:
129 outs() << " XMM" << UCs
[0].getOpInfo()
130 << format(" [0x%08x]", UCs
[1].FrameOffset
131 + (static_cast<uint32_t>(UCs
[2].FrameOffset
) << 16));
133 case UOP_PushMachFrame
:
134 outs() << " " << (UCs
[0].getOpInfo() ? "w/o" : "w")
141 static void printAllUnwindCodes(ArrayRef
<UnwindCode
> UCs
) {
142 for (const UnwindCode
*I
= UCs
.begin(), *E
= UCs
.end(); I
< E
; ) {
143 unsigned UsedSlots
= getNumUsedSlots(*I
);
144 if (UsedSlots
> UCs
.size()) {
145 outs() << "Unwind data corrupted: Encountered unwind op "
146 << getUnwindCodeTypeName((*I
).getUnwindOp())
147 << " which requires " << UsedSlots
148 << " slots, but only " << UCs
.size()
149 << " remaining in buffer";
152 printUnwindCode(makeArrayRef(I
, E
));
157 // Given a symbol sym this functions returns the address and section of it.
158 static Error
resolveSectionAndAddress(const COFFObjectFile
*Obj
,
159 const SymbolRef
&Sym
,
160 const coff_section
*&ResolvedSection
,
161 uint64_t &ResolvedAddr
) {
162 Expected
<uint64_t> ResolvedAddrOrErr
= Sym
.getAddress();
163 if (!ResolvedAddrOrErr
)
164 return ResolvedAddrOrErr
.takeError();
165 ResolvedAddr
= *ResolvedAddrOrErr
;
166 Expected
<section_iterator
> Iter
= Sym
.getSection();
168 return Iter
.takeError();
169 ResolvedSection
= Obj
->getCOFFSection(**Iter
);
170 return Error::success();
173 // Given a vector of relocations for a section and an offset into this section
174 // the function returns the symbol used for the relocation at the offset.
175 static Error
resolveSymbol(const std::vector
<RelocationRef
> &Rels
,
176 uint64_t Offset
, SymbolRef
&Sym
) {
177 for (auto &R
: Rels
) {
178 uint64_t Ofs
= R
.getOffset();
180 Sym
= *R
.getSymbol();
181 return Error::success();
184 return make_error
<BinaryError
>();
187 // Given a vector of relocations for a section and an offset into this section
188 // the function resolves the symbol used for the relocation at the offset and
189 // returns the section content and the address inside the content pointed to
192 getSectionContents(const COFFObjectFile
*Obj
,
193 const std::vector
<RelocationRef
> &Rels
, uint64_t Offset
,
194 ArrayRef
<uint8_t> &Contents
, uint64_t &Addr
) {
196 if (Error E
= resolveSymbol(Rels
, Offset
, Sym
))
198 const coff_section
*Section
;
199 if (Error E
= resolveSectionAndAddress(Obj
, Sym
, Section
, Addr
))
201 return Obj
->getSectionContents(Section
, Contents
);
204 // Given a vector of relocations for a section and an offset into this section
205 // the function returns the name of the symbol used for the relocation at the
207 static Error
resolveSymbolName(const std::vector
<RelocationRef
> &Rels
,
208 uint64_t Offset
, StringRef
&Name
) {
210 if (Error EC
= resolveSymbol(Rels
, Offset
, Sym
))
212 Expected
<StringRef
> NameOrErr
= Sym
.getName();
214 return NameOrErr
.takeError();
216 return Error::success();
219 static void printCOFFSymbolAddress(raw_ostream
&Out
,
220 const std::vector
<RelocationRef
> &Rels
,
221 uint64_t Offset
, uint32_t Disp
) {
223 if (!resolveSymbolName(Rels
, Offset
, Sym
)) {
226 Out
<< format(" + 0x%04x", Disp
);
228 Out
<< format("0x%04x", Disp
);
233 printSEHTable(const COFFObjectFile
*Obj
, uint32_t TableVA
, int Count
) {
237 uintptr_t IntPtr
= 0;
238 if (std::error_code EC
= Obj
->getVaPtr(TableVA
, IntPtr
))
239 reportError(errorCodeToError(EC
), Obj
->getFileName());
241 const support::ulittle32_t
*P
= (const support::ulittle32_t
*)IntPtr
;
242 outs() << "SEH Table:";
243 for (int I
= 0; I
< Count
; ++I
)
244 outs() << format(" 0x%x", P
[I
] + Obj
->getPE32Header()->ImageBase
);
248 template <typename T
>
249 static void printTLSDirectoryT(const coff_tls_directory
<T
> *TLSDir
) {
250 size_t FormatWidth
= sizeof(T
) * 2;
251 outs() << "TLS directory:"
252 << "\n StartAddressOfRawData: "
253 << format_hex(TLSDir
->StartAddressOfRawData
, FormatWidth
)
254 << "\n EndAddressOfRawData: "
255 << format_hex(TLSDir
->EndAddressOfRawData
, FormatWidth
)
256 << "\n AddressOfIndex: "
257 << format_hex(TLSDir
->AddressOfIndex
, FormatWidth
)
258 << "\n AddressOfCallBacks: "
259 << format_hex(TLSDir
->AddressOfCallBacks
, FormatWidth
)
260 << "\n SizeOfZeroFill: "
261 << TLSDir
->SizeOfZeroFill
262 << "\n Characteristics: "
263 << TLSDir
->Characteristics
265 << TLSDir
->getAlignment()
269 static void printTLSDirectory(const COFFObjectFile
*Obj
) {
270 const pe32_header
*PE32Header
= Obj
->getPE32Header();
271 const pe32plus_header
*PE32PlusHeader
= Obj
->getPE32PlusHeader();
273 // Skip if it's not executable.
274 if (!PE32Header
&& !PE32PlusHeader
)
277 const data_directory
*DataDir
;
278 if (std::error_code EC
= Obj
->getDataDirectory(COFF::TLS_TABLE
, DataDir
))
279 reportError(errorCodeToError(EC
), Obj
->getFileName());
281 if (DataDir
->RelativeVirtualAddress
== 0)
284 uintptr_t IntPtr
= 0;
285 if (std::error_code EC
=
286 Obj
->getRvaPtr(DataDir
->RelativeVirtualAddress
, IntPtr
))
287 reportError(errorCodeToError(EC
), Obj
->getFileName());
290 auto *TLSDir
= reinterpret_cast<const coff_tls_directory32
*>(IntPtr
);
291 printTLSDirectoryT(TLSDir
);
293 auto *TLSDir
= reinterpret_cast<const coff_tls_directory64
*>(IntPtr
);
294 printTLSDirectoryT(TLSDir
);
300 static void printLoadConfiguration(const COFFObjectFile
*Obj
) {
301 // Skip if it's not executable.
302 if (!Obj
->getPE32Header())
305 // Currently only x86 is supported
306 if (Obj
->getMachine() != COFF::IMAGE_FILE_MACHINE_I386
)
309 const data_directory
*DataDir
;
311 if (std::error_code EC
=
312 Obj
->getDataDirectory(COFF::LOAD_CONFIG_TABLE
, DataDir
))
313 reportError(errorCodeToError(EC
), Obj
->getFileName());
315 uintptr_t IntPtr
= 0;
316 if (DataDir
->RelativeVirtualAddress
== 0)
319 if (std::error_code EC
=
320 Obj
->getRvaPtr(DataDir
->RelativeVirtualAddress
, IntPtr
))
321 reportError(errorCodeToError(EC
), Obj
->getFileName());
323 auto *LoadConf
= reinterpret_cast<const coff_load_configuration32
*>(IntPtr
);
324 outs() << "Load configuration:"
325 << "\n Timestamp: " << LoadConf
->TimeDateStamp
326 << "\n Major Version: " << LoadConf
->MajorVersion
327 << "\n Minor Version: " << LoadConf
->MinorVersion
328 << "\n GlobalFlags Clear: " << LoadConf
->GlobalFlagsClear
329 << "\n GlobalFlags Set: " << LoadConf
->GlobalFlagsSet
330 << "\n Critical Section Default Timeout: " << LoadConf
->CriticalSectionDefaultTimeout
331 << "\n Decommit Free Block Threshold: " << LoadConf
->DeCommitFreeBlockThreshold
332 << "\n Decommit Total Free Threshold: " << LoadConf
->DeCommitTotalFreeThreshold
333 << "\n Lock Prefix Table: " << LoadConf
->LockPrefixTable
334 << "\n Maximum Allocation Size: " << LoadConf
->MaximumAllocationSize
335 << "\n Virtual Memory Threshold: " << LoadConf
->VirtualMemoryThreshold
336 << "\n Process Affinity Mask: " << LoadConf
->ProcessAffinityMask
337 << "\n Process Heap Flags: " << LoadConf
->ProcessHeapFlags
338 << "\n CSD Version: " << LoadConf
->CSDVersion
339 << "\n Security Cookie: " << LoadConf
->SecurityCookie
340 << "\n SEH Table: " << LoadConf
->SEHandlerTable
341 << "\n SEH Count: " << LoadConf
->SEHandlerCount
343 printSEHTable(Obj
, LoadConf
->SEHandlerTable
, LoadConf
->SEHandlerCount
);
347 // Prints import tables. The import table is a table containing the list of
348 // DLL name and symbol names which will be linked by the loader.
349 static void printImportTables(const COFFObjectFile
*Obj
) {
350 import_directory_iterator I
= Obj
->import_directory_begin();
351 import_directory_iterator E
= Obj
->import_directory_end();
354 outs() << "The Import Tables:\n";
355 for (const ImportDirectoryEntryRef
&DirRef
: Obj
->import_directories()) {
356 const coff_import_directory_table_entry
*Dir
;
358 if (DirRef
.getImportTableEntry(Dir
)) return;
359 if (DirRef
.getName(Name
)) return;
361 outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n",
362 static_cast<uint32_t>(Dir
->ImportLookupTableRVA
),
363 static_cast<uint32_t>(Dir
->TimeDateStamp
),
364 static_cast<uint32_t>(Dir
->ForwarderChain
),
365 static_cast<uint32_t>(Dir
->NameRVA
),
366 static_cast<uint32_t>(Dir
->ImportAddressTableRVA
));
367 outs() << " DLL Name: " << Name
<< "\n";
368 outs() << " Hint/Ord Name\n";
369 for (const ImportedSymbolRef
&Entry
: DirRef
.imported_symbols()) {
371 if (Entry
.isOrdinal(IsOrdinal
))
375 if (Entry
.getOrdinal(Ordinal
))
377 outs() << format(" % 6d\n", Ordinal
);
380 uint32_t HintNameRVA
;
381 if (Entry
.getHintNameRVA(HintNameRVA
))
385 if (Obj
->getHintName(HintNameRVA
, Hint
, Name
))
387 outs() << format(" % 6d ", Hint
) << Name
<< "\n";
393 // Prints export tables. The export table is a table containing the list of
394 // exported symbol from the DLL.
395 static void printExportTable(const COFFObjectFile
*Obj
) {
396 outs() << "Export Table:\n";
397 export_directory_iterator I
= Obj
->export_directory_begin();
398 export_directory_iterator E
= Obj
->export_directory_end();
402 uint32_t OrdinalBase
;
403 if (I
->getDllName(DllName
))
405 if (I
->getOrdinalBase(OrdinalBase
))
407 outs() << " DLL name: " << DllName
<< "\n";
408 outs() << " Ordinal base: " << OrdinalBase
<< "\n";
409 outs() << " Ordinal RVA Name\n";
410 for (; I
!= E
; I
= ++I
) {
412 if (I
->getOrdinal(Ordinal
))
415 if (I
->getExportRVA(RVA
))
418 if (I
->isForwarder(IsForwarder
))
422 // Export table entries can be used to re-export symbols that
423 // this COFF file is imported from some DLLs. This is rare.
424 // In most cases IsForwarder is false.
425 outs() << format(" % 4d ", Ordinal
);
427 outs() << format(" % 4d %# 8x", Ordinal
, RVA
);
431 if (I
->getSymbolName(Name
))
434 outs() << " " << Name
;
437 if (I
->getForwardTo(S
))
439 outs() << " (forwarded to " << S
<< ")";
445 // Given the COFF object file, this function returns the relocations for .pdata
446 // and the pointer to "runtime function" structs.
447 static bool getPDataSection(const COFFObjectFile
*Obj
,
448 std::vector
<RelocationRef
> &Rels
,
449 const RuntimeFunction
*&RFStart
, int &NumRFs
) {
450 for (const SectionRef
&Section
: Obj
->sections()) {
451 StringRef Name
= unwrapOrError(Section
.getName(), Obj
->getFileName());
452 if (Name
!= ".pdata")
455 const coff_section
*Pdata
= Obj
->getCOFFSection(Section
);
456 for (const RelocationRef
&Reloc
: Section
.relocations())
457 Rels
.push_back(Reloc
);
459 // Sort relocations by address.
460 llvm::sort(Rels
, isRelocAddressLess
);
462 ArrayRef
<uint8_t> Contents
;
463 if (Error E
= Obj
->getSectionContents(Pdata
, Contents
))
464 reportError(std::move(E
), Obj
->getFileName());
466 if (Contents
.empty())
469 RFStart
= reinterpret_cast<const RuntimeFunction
*>(Contents
.data());
470 NumRFs
= Contents
.size() / sizeof(RuntimeFunction
);
476 Error
getCOFFRelocationValueString(const COFFObjectFile
*Obj
,
477 const RelocationRef
&Rel
,
478 SmallVectorImpl
<char> &Result
) {
479 symbol_iterator SymI
= Rel
.getSymbol();
480 Expected
<StringRef
> SymNameOrErr
= SymI
->getName();
482 return SymNameOrErr
.takeError();
483 StringRef SymName
= *SymNameOrErr
;
484 Result
.append(SymName
.begin(), SymName
.end());
485 return Error::success();
488 static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo
*UI
) {
489 // The casts to int are required in order to output the value as number.
490 // Without the casts the value would be interpreted as char data (which
491 // results in garbage output).
492 outs() << " Version: " << static_cast<int>(UI
->getVersion()) << "\n";
493 outs() << " Flags: " << static_cast<int>(UI
->getFlags());
494 if (UI
->getFlags()) {
495 if (UI
->getFlags() & UNW_ExceptionHandler
)
496 outs() << " UNW_ExceptionHandler";
497 if (UI
->getFlags() & UNW_TerminateHandler
)
498 outs() << " UNW_TerminateHandler";
499 if (UI
->getFlags() & UNW_ChainInfo
)
500 outs() << " UNW_ChainInfo";
503 outs() << " Size of prolog: " << static_cast<int>(UI
->PrologSize
) << "\n";
504 outs() << " Number of Codes: " << static_cast<int>(UI
->NumCodes
) << "\n";
505 // Maybe this should move to output of UOP_SetFPReg?
506 if (UI
->getFrameRegister()) {
507 outs() << " Frame register: "
508 << getUnwindRegisterName(UI
->getFrameRegister()) << "\n";
509 outs() << " Frame offset: " << 16 * UI
->getFrameOffset() << "\n";
511 outs() << " No frame pointer used\n";
513 if (UI
->getFlags() & (UNW_ExceptionHandler
| UNW_TerminateHandler
)) {
514 // FIXME: Output exception handler data
515 } else if (UI
->getFlags() & UNW_ChainInfo
) {
516 // FIXME: Output chained unwind info
520 outs() << " Unwind Codes:\n";
522 printAllUnwindCodes(makeArrayRef(&UI
->UnwindCodes
[0], UI
->NumCodes
));
528 /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
529 /// pointing to an executable file.
530 static void printRuntimeFunction(const COFFObjectFile
*Obj
,
531 const RuntimeFunction
&RF
) {
532 if (!RF
.StartAddress
)
534 outs() << "Function Table:\n"
535 << format(" Start Address: 0x%04x\n",
536 static_cast<uint32_t>(RF
.StartAddress
))
537 << format(" End Address: 0x%04x\n",
538 static_cast<uint32_t>(RF
.EndAddress
))
539 << format(" Unwind Info Address: 0x%04x\n",
540 static_cast<uint32_t>(RF
.UnwindInfoOffset
));
542 if (Obj
->getRvaPtr(RF
.UnwindInfoOffset
, addr
))
544 printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo
*>(addr
));
547 /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
548 /// pointing to an object file. Unlike executable, fields in RuntimeFunction
549 /// struct are filled with zeros, but instead there are relocations pointing to
550 /// them so that the linker will fill targets' RVAs to the fields at link
551 /// time. This function interprets the relocations to find the data to be used
552 /// in the resulting executable.
553 static void printRuntimeFunctionRels(const COFFObjectFile
*Obj
,
554 const RuntimeFunction
&RF
,
555 uint64_t SectionOffset
,
556 const std::vector
<RelocationRef
> &Rels
) {
557 outs() << "Function Table:\n";
558 outs() << " Start Address: ";
559 printCOFFSymbolAddress(outs(), Rels
,
561 /*offsetof(RuntimeFunction, StartAddress)*/ 0,
565 outs() << " End Address: ";
566 printCOFFSymbolAddress(outs(), Rels
,
568 /*offsetof(RuntimeFunction, EndAddress)*/ 4,
572 outs() << " Unwind Info Address: ";
573 printCOFFSymbolAddress(outs(), Rels
,
575 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
576 RF
.UnwindInfoOffset
);
579 ArrayRef
<uint8_t> XContents
;
580 uint64_t UnwindInfoOffset
= 0;
581 if (Error E
= getSectionContents(
584 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
585 XContents
, UnwindInfoOffset
))
586 reportError(std::move(E
), Obj
->getFileName());
587 if (XContents
.empty())
590 UnwindInfoOffset
+= RF
.UnwindInfoOffset
;
591 if (UnwindInfoOffset
> XContents
.size())
594 auto *UI
= reinterpret_cast<const Win64EH::UnwindInfo
*>(XContents
.data() +
596 printWin64EHUnwindInfo(UI
);
599 void printCOFFUnwindInfo(const COFFObjectFile
*Obj
) {
600 if (Obj
->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64
) {
601 WithColor::error(errs(), "llvm-objdump")
602 << "unsupported image machine type "
603 "(currently only AMD64 is supported).\n";
607 std::vector
<RelocationRef
> Rels
;
608 const RuntimeFunction
*RFStart
;
610 if (!getPDataSection(Obj
, Rels
, RFStart
, NumRFs
))
612 ArrayRef
<RuntimeFunction
> RFs(RFStart
, NumRFs
);
614 bool IsExecutable
= Rels
.empty();
616 for (const RuntimeFunction
&RF
: RFs
)
617 printRuntimeFunction(Obj
, RF
);
621 for (const RuntimeFunction
&RF
: RFs
) {
622 uint64_t SectionOffset
=
623 std::distance(RFs
.begin(), &RF
) * sizeof(RuntimeFunction
);
624 printRuntimeFunctionRels(Obj
, RF
, SectionOffset
, Rels
);
628 void printCOFFFileHeader(const object::ObjectFile
*Obj
) {
629 const COFFObjectFile
*file
= dyn_cast
<const COFFObjectFile
>(Obj
);
630 printTLSDirectory(file
);
631 printLoadConfiguration(file
);
632 printImportTables(file
);
633 printExportTable(file
);
636 void printCOFFSymbolTable(const object::COFFImportFile
*i
) {
638 bool IsCode
= i
->getCOFFImportHeader()->getType() == COFF::IMPORT_CODE
;
640 for (const object::BasicSymbolRef
&Sym
: i
->symbols()) {
642 raw_string_ostream
NS(Name
);
644 cantFail(Sym
.printName(NS
));
647 outs() << "[" << format("%2d", Index
) << "]"
648 << "(sec " << format("%2d", 0) << ")"
649 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
650 << "(ty " << format("%3x", (IsCode
&& Index
) ? 32 : 0) << ")"
651 << "(scl " << format("%3x", 0) << ") "
652 << "(nx " << 0 << ") "
653 << "0x" << format("%08x", 0) << " " << Name
<< '\n';
659 void printCOFFSymbolTable(const COFFObjectFile
*coff
) {
660 for (unsigned SI
= 0, SE
= coff
->getNumberOfSymbols(); SI
!= SE
; ++SI
) {
661 Expected
<COFFSymbolRef
> Symbol
= coff
->getSymbol(SI
);
663 reportError(Symbol
.takeError(), coff
->getFileName());
666 if (std::error_code EC
= coff
->getSymbolName(*Symbol
, Name
))
667 reportError(errorCodeToError(EC
), coff
->getFileName());
669 outs() << "[" << format("%2d", SI
) << "]"
670 << "(sec " << format("%2d", int(Symbol
->getSectionNumber())) << ")"
671 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
672 << "(ty " << format("%3x", unsigned(Symbol
->getType())) << ")"
673 << "(scl " << format("%3x", unsigned(Symbol
->getStorageClass()))
675 << "(nx " << unsigned(Symbol
->getNumberOfAuxSymbols()) << ") "
676 << "0x" << format("%08x", unsigned(Symbol
->getValue())) << " "
678 if (Demangle
&& Name
.startswith("?")) {
679 char *DemangledSymbol
= nullptr;
683 microsoftDemangle(Name
.data(), DemangledSymbol
, &Size
, &Status
);
685 if (Status
== 0 && DemangledSymbol
) {
686 outs() << " (" << StringRef(DemangledSymbol
) << ")";
687 std::free(DemangledSymbol
);
689 outs() << " (invalid mangled name)";
694 for (unsigned AI
= 0, AE
= Symbol
->getNumberOfAuxSymbols(); AI
< AE
; ++AI
, ++SI
) {
695 if (Symbol
->isSectionDefinition()) {
696 const coff_aux_section_definition
*asd
;
697 if (std::error_code EC
=
698 coff
->getAuxSymbol
<coff_aux_section_definition
>(SI
+ 1, asd
))
699 reportError(errorCodeToError(EC
), coff
->getFileName());
701 int32_t AuxNumber
= asd
->getNumber(Symbol
->isBigObj());
704 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
705 , unsigned(asd
->Length
)
706 , unsigned(asd
->NumberOfRelocations
)
707 , unsigned(asd
->NumberOfLinenumbers
)
708 , unsigned(asd
->CheckSum
))
709 << format("assoc %d comdat %d\n"
710 , unsigned(AuxNumber
)
711 , unsigned(asd
->Selection
));
712 } else if (Symbol
->isFileRecord()) {
713 const char *FileName
;
714 if (std::error_code EC
= coff
->getAuxSymbol
<char>(SI
+ 1, FileName
))
715 reportError(errorCodeToError(EC
), coff
->getFileName());
717 StringRef
Name(FileName
, Symbol
->getNumberOfAuxSymbols() *
718 coff
->getSymbolTableEntrySize());
719 outs() << "AUX " << Name
.rtrim(StringRef("\0", 1)) << '\n';
721 SI
= SI
+ Symbol
->getNumberOfAuxSymbols();
723 } else if (Symbol
->isWeakExternal()) {
724 const coff_aux_weak_external
*awe
;
725 if (std::error_code EC
=
726 coff
->getAuxSymbol
<coff_aux_weak_external
>(SI
+ 1, awe
))
727 reportError(errorCodeToError(EC
), coff
->getFileName());
729 outs() << "AUX " << format("indx %d srch %d\n",
730 static_cast<uint32_t>(awe
->TagIndex
),
731 static_cast<uint32_t>(awe
->Characteristics
));
733 outs() << "AUX Unknown\n";