[llvm-objcopy] [COFF] Fix warnings abuilt missing field initialization. NFC.
[llvm-complete.git] / tools / llvm-objdump / COFFDump.cpp
bloba7ee49535d6544982146a7ac20322b99db732794
1 //===-- COFFDump.cpp - COFF-specific dumper ---------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements the COFF-specific dumper for llvm-objdump.
12 /// It outputs the Win64 EH data structures as plain text.
13 /// The encoding of the unwind codes is described in MSDN:
14 /// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx
15 ///
16 //===----------------------------------------------------------------------===//
18 #include "llvm-objdump.h"
19 #include "llvm/Demangle/Demangle.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Object/COFFImportFile.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/Win64EH.h"
25 #include "llvm/Support/WithColor.h"
26 #include "llvm/Support/raw_ostream.h"
28 using namespace llvm;
29 using namespace object;
30 using namespace llvm::Win64EH;
32 // Returns the name of the unwind code.
33 static StringRef getUnwindCodeTypeName(uint8_t Code) {
34 switch(Code) {
35 default: llvm_unreachable("Invalid unwind code");
36 case UOP_PushNonVol: return "UOP_PushNonVol";
37 case UOP_AllocLarge: return "UOP_AllocLarge";
38 case UOP_AllocSmall: return "UOP_AllocSmall";
39 case UOP_SetFPReg: return "UOP_SetFPReg";
40 case UOP_SaveNonVol: return "UOP_SaveNonVol";
41 case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig";
42 case UOP_SaveXMM128: return "UOP_SaveXMM128";
43 case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big";
44 case UOP_PushMachFrame: return "UOP_PushMachFrame";
48 // Returns the name of a referenced register.
49 static StringRef getUnwindRegisterName(uint8_t Reg) {
50 switch(Reg) {
51 default: llvm_unreachable("Invalid register");
52 case 0: return "RAX";
53 case 1: return "RCX";
54 case 2: return "RDX";
55 case 3: return "RBX";
56 case 4: return "RSP";
57 case 5: return "RBP";
58 case 6: return "RSI";
59 case 7: return "RDI";
60 case 8: return "R8";
61 case 9: return "R9";
62 case 10: return "R10";
63 case 11: return "R11";
64 case 12: return "R12";
65 case 13: return "R13";
66 case 14: return "R14";
67 case 15: return "R15";
71 // Calculates the number of array slots required for the unwind code.
72 static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
73 switch (UnwindCode.getUnwindOp()) {
74 default: llvm_unreachable("Invalid unwind code");
75 case UOP_PushNonVol:
76 case UOP_AllocSmall:
77 case UOP_SetFPReg:
78 case UOP_PushMachFrame:
79 return 1;
80 case UOP_SaveNonVol:
81 case UOP_SaveXMM128:
82 return 2;
83 case UOP_SaveNonVolBig:
84 case UOP_SaveXMM128Big:
85 return 3;
86 case UOP_AllocLarge:
87 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
91 // Prints one unwind code. Because an unwind code can occupy up to 3 slots in
92 // the unwind codes array, this function requires that the correct number of
93 // slots is provided.
94 static void printUnwindCode(ArrayRef<UnwindCode> UCs) {
95 assert(UCs.size() >= getNumUsedSlots(UCs[0]));
96 outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset))
97 << getUnwindCodeTypeName(UCs[0].getUnwindOp());
98 switch (UCs[0].getUnwindOp()) {
99 case UOP_PushNonVol:
100 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo());
101 break;
102 case UOP_AllocLarge:
103 if (UCs[0].getOpInfo() == 0) {
104 outs() << " " << UCs[1].FrameOffset;
105 } else {
106 outs() << " " << UCs[1].FrameOffset
107 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
109 break;
110 case UOP_AllocSmall:
111 outs() << " " << ((UCs[0].getOpInfo() + 1) * 8);
112 break;
113 case UOP_SetFPReg:
114 outs() << " ";
115 break;
116 case UOP_SaveNonVol:
117 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
118 << format(" [0x%04x]", 8 * UCs[1].FrameOffset);
119 break;
120 case UOP_SaveNonVolBig:
121 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
122 << format(" [0x%08x]", UCs[1].FrameOffset
123 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
124 break;
125 case UOP_SaveXMM128:
126 outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
127 << format(" [0x%04x]", 16 * UCs[1].FrameOffset);
128 break;
129 case UOP_SaveXMM128Big:
130 outs() << " XMM" << UCs[0].getOpInfo()
131 << format(" [0x%08x]", UCs[1].FrameOffset
132 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
133 break;
134 case UOP_PushMachFrame:
135 outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w")
136 << " error code";
137 break;
139 outs() << "\n";
142 static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) {
143 for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) {
144 unsigned UsedSlots = getNumUsedSlots(*I);
145 if (UsedSlots > UCs.size()) {
146 outs() << "Unwind data corrupted: Encountered unwind op "
147 << getUnwindCodeTypeName((*I).getUnwindOp())
148 << " which requires " << UsedSlots
149 << " slots, but only " << UCs.size()
150 << " remaining in buffer";
151 return ;
153 printUnwindCode(makeArrayRef(I, E));
154 I += UsedSlots;
158 // Given a symbol sym this functions returns the address and section of it.
159 static std::error_code
160 resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
161 const coff_section *&ResolvedSection,
162 uint64_t &ResolvedAddr) {
163 Expected<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
164 if (!ResolvedAddrOrErr)
165 return errorToErrorCode(ResolvedAddrOrErr.takeError());
166 ResolvedAddr = *ResolvedAddrOrErr;
167 Expected<section_iterator> Iter = Sym.getSection();
168 if (!Iter)
169 return errorToErrorCode(Iter.takeError());
170 ResolvedSection = Obj->getCOFFSection(**Iter);
171 return std::error_code();
174 // Given a vector of relocations for a section and an offset into this section
175 // the function returns the symbol used for the relocation at the offset.
176 static std::error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
177 uint64_t Offset, SymbolRef &Sym) {
178 for (auto &R : Rels) {
179 uint64_t Ofs = R.getOffset();
180 if (Ofs == Offset) {
181 Sym = *R.getSymbol();
182 return std::error_code();
185 return object_error::parse_failed;
188 // Given a vector of relocations for a section and an offset into this section
189 // the function resolves the symbol used for the relocation at the offset and
190 // returns the section content and the address inside the content pointed to
191 // by the symbol.
192 static std::error_code
193 getSectionContents(const COFFObjectFile *Obj,
194 const std::vector<RelocationRef> &Rels, uint64_t Offset,
195 ArrayRef<uint8_t> &Contents, uint64_t &Addr) {
196 SymbolRef Sym;
197 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
198 return EC;
199 const coff_section *Section;
200 if (std::error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
201 return EC;
202 if (std::error_code EC = Obj->getSectionContents(Section, Contents))
203 return EC;
204 return std::error_code();
207 // Given a vector of relocations for a section and an offset into this section
208 // the function returns the name of the symbol used for the relocation at the
209 // offset.
210 static std::error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
211 uint64_t Offset, StringRef &Name) {
212 SymbolRef Sym;
213 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
214 return EC;
215 Expected<StringRef> NameOrErr = Sym.getName();
216 if (!NameOrErr)
217 return errorToErrorCode(NameOrErr.takeError());
218 Name = *NameOrErr;
219 return std::error_code();
222 static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
223 const std::vector<RelocationRef> &Rels,
224 uint64_t Offset, uint32_t Disp) {
225 StringRef Sym;
226 if (!resolveSymbolName(Rels, Offset, Sym)) {
227 Out << Sym;
228 if (Disp > 0)
229 Out << format(" + 0x%04x", Disp);
230 } else {
231 Out << format("0x%04x", Disp);
235 static void
236 printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) {
237 if (Count == 0)
238 return;
240 const pe32_header *PE32Header;
241 error(Obj->getPE32Header(PE32Header));
242 uint32_t ImageBase = PE32Header->ImageBase;
243 uintptr_t IntPtr = 0;
244 error(Obj->getVaPtr(TableVA, IntPtr));
245 const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr;
246 outs() << "SEH Table:";
247 for (int I = 0; I < Count; ++I)
248 outs() << format(" 0x%x", P[I] + ImageBase);
249 outs() << "\n\n";
252 template <typename T>
253 static void printTLSDirectoryT(const coff_tls_directory<T> *TLSDir) {
254 size_t FormatWidth = sizeof(T) * 2;
255 outs() << "TLS directory:"
256 << "\n StartAddressOfRawData: "
257 << format_hex(TLSDir->StartAddressOfRawData, FormatWidth)
258 << "\n EndAddressOfRawData: "
259 << format_hex(TLSDir->EndAddressOfRawData, FormatWidth)
260 << "\n AddressOfIndex: "
261 << format_hex(TLSDir->AddressOfIndex, FormatWidth)
262 << "\n AddressOfCallBacks: "
263 << format_hex(TLSDir->AddressOfCallBacks, FormatWidth)
264 << "\n SizeOfZeroFill: "
265 << TLSDir->SizeOfZeroFill
266 << "\n Characteristics: "
267 << TLSDir->Characteristics
268 << "\n Alignment: "
269 << TLSDir->getAlignment()
270 << "\n\n";
273 static void printTLSDirectory(const COFFObjectFile *Obj) {
274 const pe32_header *PE32Header;
275 error(Obj->getPE32Header(PE32Header));
277 const pe32plus_header *PE32PlusHeader;
278 error(Obj->getPE32PlusHeader(PE32PlusHeader));
280 // Skip if it's not executable.
281 if (!PE32Header && !PE32PlusHeader)
282 return;
284 const data_directory *DataDir;
285 error(Obj->getDataDirectory(COFF::TLS_TABLE, DataDir));
286 uintptr_t IntPtr = 0;
287 if (DataDir->RelativeVirtualAddress == 0)
288 return;
289 error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
291 if (PE32Header) {
292 auto *TLSDir = reinterpret_cast<const coff_tls_directory32 *>(IntPtr);
293 printTLSDirectoryT(TLSDir);
294 } else {
295 auto *TLSDir = reinterpret_cast<const coff_tls_directory64 *>(IntPtr);
296 printTLSDirectoryT(TLSDir);
299 outs() << "\n";
302 static void printLoadConfiguration(const COFFObjectFile *Obj) {
303 // Skip if it's not executable.
304 const pe32_header *PE32Header;
305 error(Obj->getPE32Header(PE32Header));
306 if (!PE32Header)
307 return;
309 // Currently only x86 is supported
310 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_I386)
311 return;
313 const data_directory *DataDir;
314 error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir));
315 uintptr_t IntPtr = 0;
316 if (DataDir->RelativeVirtualAddress == 0)
317 return;
318 error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
320 auto *LoadConf = reinterpret_cast<const coff_load_configuration32 *>(IntPtr);
321 outs() << "Load configuration:"
322 << "\n Timestamp: " << LoadConf->TimeDateStamp
323 << "\n Major Version: " << LoadConf->MajorVersion
324 << "\n Minor Version: " << LoadConf->MinorVersion
325 << "\n GlobalFlags Clear: " << LoadConf->GlobalFlagsClear
326 << "\n GlobalFlags Set: " << LoadConf->GlobalFlagsSet
327 << "\n Critical Section Default Timeout: " << LoadConf->CriticalSectionDefaultTimeout
328 << "\n Decommit Free Block Threshold: " << LoadConf->DeCommitFreeBlockThreshold
329 << "\n Decommit Total Free Threshold: " << LoadConf->DeCommitTotalFreeThreshold
330 << "\n Lock Prefix Table: " << LoadConf->LockPrefixTable
331 << "\n Maximum Allocation Size: " << LoadConf->MaximumAllocationSize
332 << "\n Virtual Memory Threshold: " << LoadConf->VirtualMemoryThreshold
333 << "\n Process Affinity Mask: " << LoadConf->ProcessAffinityMask
334 << "\n Process Heap Flags: " << LoadConf->ProcessHeapFlags
335 << "\n CSD Version: " << LoadConf->CSDVersion
336 << "\n Security Cookie: " << LoadConf->SecurityCookie
337 << "\n SEH Table: " << LoadConf->SEHandlerTable
338 << "\n SEH Count: " << LoadConf->SEHandlerCount
339 << "\n\n";
340 printSEHTable(Obj, LoadConf->SEHandlerTable, LoadConf->SEHandlerCount);
341 outs() << "\n";
344 // Prints import tables. The import table is a table containing the list of
345 // DLL name and symbol names which will be linked by the loader.
346 static void printImportTables(const COFFObjectFile *Obj) {
347 import_directory_iterator I = Obj->import_directory_begin();
348 import_directory_iterator E = Obj->import_directory_end();
349 if (I == E)
350 return;
351 outs() << "The Import Tables:\n";
352 for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) {
353 const coff_import_directory_table_entry *Dir;
354 StringRef Name;
355 if (DirRef.getImportTableEntry(Dir)) return;
356 if (DirRef.getName(Name)) return;
358 outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n",
359 static_cast<uint32_t>(Dir->ImportLookupTableRVA),
360 static_cast<uint32_t>(Dir->TimeDateStamp),
361 static_cast<uint32_t>(Dir->ForwarderChain),
362 static_cast<uint32_t>(Dir->NameRVA),
363 static_cast<uint32_t>(Dir->ImportAddressTableRVA));
364 outs() << " DLL Name: " << Name << "\n";
365 outs() << " Hint/Ord Name\n";
366 for (const ImportedSymbolRef &Entry : DirRef.imported_symbols()) {
367 bool IsOrdinal;
368 if (Entry.isOrdinal(IsOrdinal))
369 return;
370 if (IsOrdinal) {
371 uint16_t Ordinal;
372 if (Entry.getOrdinal(Ordinal))
373 return;
374 outs() << format(" % 6d\n", Ordinal);
375 continue;
377 uint32_t HintNameRVA;
378 if (Entry.getHintNameRVA(HintNameRVA))
379 return;
380 uint16_t Hint;
381 StringRef Name;
382 if (Obj->getHintName(HintNameRVA, Hint, Name))
383 return;
384 outs() << format(" % 6d ", Hint) << Name << "\n";
386 outs() << "\n";
390 // Prints export tables. The export table is a table containing the list of
391 // exported symbol from the DLL.
392 static void printExportTable(const COFFObjectFile *Obj) {
393 outs() << "Export Table:\n";
394 export_directory_iterator I = Obj->export_directory_begin();
395 export_directory_iterator E = Obj->export_directory_end();
396 if (I == E)
397 return;
398 StringRef DllName;
399 uint32_t OrdinalBase;
400 if (I->getDllName(DllName))
401 return;
402 if (I->getOrdinalBase(OrdinalBase))
403 return;
404 outs() << " DLL name: " << DllName << "\n";
405 outs() << " Ordinal base: " << OrdinalBase << "\n";
406 outs() << " Ordinal RVA Name\n";
407 for (; I != E; I = ++I) {
408 uint32_t Ordinal;
409 if (I->getOrdinal(Ordinal))
410 return;
411 uint32_t RVA;
412 if (I->getExportRVA(RVA))
413 return;
414 bool IsForwarder;
415 if (I->isForwarder(IsForwarder))
416 return;
418 if (IsForwarder) {
419 // Export table entries can be used to re-export symbols that
420 // this COFF file is imported from some DLLs. This is rare.
421 // In most cases IsForwarder is false.
422 outs() << format(" % 4d ", Ordinal);
423 } else {
424 outs() << format(" % 4d %# 8x", Ordinal, RVA);
427 StringRef Name;
428 if (I->getSymbolName(Name))
429 continue;
430 if (!Name.empty())
431 outs() << " " << Name;
432 if (IsForwarder) {
433 StringRef S;
434 if (I->getForwardTo(S))
435 return;
436 outs() << " (forwarded to " << S << ")";
438 outs() << "\n";
442 // Given the COFF object file, this function returns the relocations for .pdata
443 // and the pointer to "runtime function" structs.
444 static bool getPDataSection(const COFFObjectFile *Obj,
445 std::vector<RelocationRef> &Rels,
446 const RuntimeFunction *&RFStart, int &NumRFs) {
447 for (const SectionRef &Section : Obj->sections()) {
448 StringRef Name;
449 error(Section.getName(Name));
450 if (Name != ".pdata")
451 continue;
453 const coff_section *Pdata = Obj->getCOFFSection(Section);
454 for (const RelocationRef &Reloc : Section.relocations())
455 Rels.push_back(Reloc);
457 // Sort relocations by address.
458 llvm::sort(Rels, RelocAddressLess);
460 ArrayRef<uint8_t> Contents;
461 error(Obj->getSectionContents(Pdata, Contents));
462 if (Contents.empty())
463 continue;
465 RFStart = reinterpret_cast<const RuntimeFunction *>(Contents.data());
466 NumRFs = Contents.size() / sizeof(RuntimeFunction);
467 return true;
469 return false;
472 static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) {
473 // The casts to int are required in order to output the value as number.
474 // Without the casts the value would be interpreted as char data (which
475 // results in garbage output).
476 outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n";
477 outs() << " Flags: " << static_cast<int>(UI->getFlags());
478 if (UI->getFlags()) {
479 if (UI->getFlags() & UNW_ExceptionHandler)
480 outs() << " UNW_ExceptionHandler";
481 if (UI->getFlags() & UNW_TerminateHandler)
482 outs() << " UNW_TerminateHandler";
483 if (UI->getFlags() & UNW_ChainInfo)
484 outs() << " UNW_ChainInfo";
486 outs() << "\n";
487 outs() << " Size of prolog: " << static_cast<int>(UI->PrologSize) << "\n";
488 outs() << " Number of Codes: " << static_cast<int>(UI->NumCodes) << "\n";
489 // Maybe this should move to output of UOP_SetFPReg?
490 if (UI->getFrameRegister()) {
491 outs() << " Frame register: "
492 << getUnwindRegisterName(UI->getFrameRegister()) << "\n";
493 outs() << " Frame offset: " << 16 * UI->getFrameOffset() << "\n";
494 } else {
495 outs() << " No frame pointer used\n";
497 if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
498 // FIXME: Output exception handler data
499 } else if (UI->getFlags() & UNW_ChainInfo) {
500 // FIXME: Output chained unwind info
503 if (UI->NumCodes)
504 outs() << " Unwind Codes:\n";
506 printAllUnwindCodes(makeArrayRef(&UI->UnwindCodes[0], UI->NumCodes));
508 outs() << "\n";
509 outs().flush();
512 /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
513 /// pointing to an executable file.
514 static void printRuntimeFunction(const COFFObjectFile *Obj,
515 const RuntimeFunction &RF) {
516 if (!RF.StartAddress)
517 return;
518 outs() << "Function Table:\n"
519 << format(" Start Address: 0x%04x\n",
520 static_cast<uint32_t>(RF.StartAddress))
521 << format(" End Address: 0x%04x\n",
522 static_cast<uint32_t>(RF.EndAddress))
523 << format(" Unwind Info Address: 0x%04x\n",
524 static_cast<uint32_t>(RF.UnwindInfoOffset));
525 uintptr_t addr;
526 if (Obj->getRvaPtr(RF.UnwindInfoOffset, addr))
527 return;
528 printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo *>(addr));
531 /// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
532 /// pointing to an object file. Unlike executable, fields in RuntimeFunction
533 /// struct are filled with zeros, but instead there are relocations pointing to
534 /// them so that the linker will fill targets' RVAs to the fields at link
535 /// time. This function interprets the relocations to find the data to be used
536 /// in the resulting executable.
537 static void printRuntimeFunctionRels(const COFFObjectFile *Obj,
538 const RuntimeFunction &RF,
539 uint64_t SectionOffset,
540 const std::vector<RelocationRef> &Rels) {
541 outs() << "Function Table:\n";
542 outs() << " Start Address: ";
543 printCOFFSymbolAddress(outs(), Rels,
544 SectionOffset +
545 /*offsetof(RuntimeFunction, StartAddress)*/ 0,
546 RF.StartAddress);
547 outs() << "\n";
549 outs() << " End Address: ";
550 printCOFFSymbolAddress(outs(), Rels,
551 SectionOffset +
552 /*offsetof(RuntimeFunction, EndAddress)*/ 4,
553 RF.EndAddress);
554 outs() << "\n";
556 outs() << " Unwind Info Address: ";
557 printCOFFSymbolAddress(outs(), Rels,
558 SectionOffset +
559 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
560 RF.UnwindInfoOffset);
561 outs() << "\n";
563 ArrayRef<uint8_t> XContents;
564 uint64_t UnwindInfoOffset = 0;
565 error(getSectionContents(
566 Obj, Rels, SectionOffset +
567 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
568 XContents, UnwindInfoOffset));
569 if (XContents.empty())
570 return;
572 UnwindInfoOffset += RF.UnwindInfoOffset;
573 if (UnwindInfoOffset > XContents.size())
574 return;
576 auto *UI = reinterpret_cast<const Win64EH::UnwindInfo *>(XContents.data() +
577 UnwindInfoOffset);
578 printWin64EHUnwindInfo(UI);
581 void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
582 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64) {
583 WithColor::error(errs(), "llvm-objdump")
584 << "unsupported image machine type "
585 "(currently only AMD64 is supported).\n";
586 return;
589 std::vector<RelocationRef> Rels;
590 const RuntimeFunction *RFStart;
591 int NumRFs;
592 if (!getPDataSection(Obj, Rels, RFStart, NumRFs))
593 return;
594 ArrayRef<RuntimeFunction> RFs(RFStart, NumRFs);
596 bool IsExecutable = Rels.empty();
597 if (IsExecutable) {
598 for (const RuntimeFunction &RF : RFs)
599 printRuntimeFunction(Obj, RF);
600 return;
603 for (const RuntimeFunction &RF : RFs) {
604 uint64_t SectionOffset =
605 std::distance(RFs.begin(), &RF) * sizeof(RuntimeFunction);
606 printRuntimeFunctionRels(Obj, RF, SectionOffset, Rels);
610 void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) {
611 const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj);
612 printTLSDirectory(file);
613 printLoadConfiguration(file);
614 printImportTables(file);
615 printExportTable(file);
618 void llvm::printCOFFSymbolTable(const object::COFFImportFile *i) {
619 unsigned Index = 0;
620 bool IsCode = i->getCOFFImportHeader()->getType() == COFF::IMPORT_CODE;
622 for (const object::BasicSymbolRef &Sym : i->symbols()) {
623 std::string Name;
624 raw_string_ostream NS(Name);
626 Sym.printName(NS);
627 NS.flush();
629 outs() << "[" << format("%2d", Index) << "]"
630 << "(sec " << format("%2d", 0) << ")"
631 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
632 << "(ty " << format("%3x", (IsCode && Index) ? 32 : 0) << ")"
633 << "(scl " << format("%3x", 0) << ") "
634 << "(nx " << 0 << ") "
635 << "0x" << format("%08x", 0) << " " << Name << '\n';
637 ++Index;
641 void llvm::printCOFFSymbolTable(const COFFObjectFile *coff) {
642 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
643 Expected<COFFSymbolRef> Symbol = coff->getSymbol(SI);
644 StringRef Name;
645 error(errorToErrorCode(Symbol.takeError()));
646 error(coff->getSymbolName(*Symbol, Name));
648 outs() << "[" << format("%2d", SI) << "]"
649 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
650 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
651 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
652 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass()))
653 << ") "
654 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
655 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
656 << Name;
657 if (Demangle && Name.startswith("?")) {
658 char *DemangledSymbol = nullptr;
659 size_t Size = 0;
660 int Status = -1;
661 DemangledSymbol =
662 microsoftDemangle(Name.data(), DemangledSymbol, &Size, &Status);
664 if (Status == 0 && DemangledSymbol) {
665 outs() << " (" << StringRef(DemangledSymbol) << ")";
666 std::free(DemangledSymbol);
667 } else {
668 outs() << " (invalid mangled name)";
671 outs() << "\n";
673 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
674 if (Symbol->isSectionDefinition()) {
675 const coff_aux_section_definition *asd;
676 error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd));
678 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
680 outs() << "AUX "
681 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
682 , unsigned(asd->Length)
683 , unsigned(asd->NumberOfRelocations)
684 , unsigned(asd->NumberOfLinenumbers)
685 , unsigned(asd->CheckSum))
686 << format("assoc %d comdat %d\n"
687 , unsigned(AuxNumber)
688 , unsigned(asd->Selection));
689 } else if (Symbol->isFileRecord()) {
690 const char *FileName;
691 error(coff->getAuxSymbol<char>(SI + 1, FileName));
693 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
694 coff->getSymbolTableEntrySize());
695 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
697 SI = SI + Symbol->getNumberOfAuxSymbols();
698 break;
699 } else if (Symbol->isWeakExternal()) {
700 const coff_aux_weak_external *awe;
701 error(coff->getAuxSymbol<coff_aux_weak_external>(SI + 1, awe));
703 outs() << "AUX " << format("indx %d srch %d\n",
704 static_cast<uint32_t>(awe->TagIndex),
705 static_cast<uint32_t>(awe->Characteristics));
706 } else {
707 outs() << "AUX Unknown\n";