blob: d84f768bf818b2aa225d73a79e83ef1433e91ce6 [file] [log] [blame]
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001//===-- 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/// \brief This file implements the COFF-specific dumper for llvm-objdump.
12/// It outputs the Win64 EH data structures as plain text.
Alp Tokercb402912014-01-24 17:20:08 +000013/// The encoding of the unwind codes is described in MSDN:
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000014/// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx
15///
16//===----------------------------------------------------------------------===//
17
18#include "llvm-objdump.h"
19#include "llvm/Object/COFF.h"
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +000020#include "llvm/Object/COFFImportFile.h"
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000021#include "llvm/Object/ObjectFile.h"
22#include "llvm/Support/Format.h"
Chandler Carruthb034cb72013-01-02 10:26:28 +000023#include "llvm/Support/Win64EH.h"
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000024#include "llvm/Support/raw_ostream.h"
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000025
26using namespace llvm;
27using namespace object;
28using namespace llvm::Win64EH;
29
30// Returns the name of the unwind code.
31static StringRef getUnwindCodeTypeName(uint8_t Code) {
32 switch(Code) {
33 default: llvm_unreachable("Invalid unwind code");
34 case UOP_PushNonVol: return "UOP_PushNonVol";
35 case UOP_AllocLarge: return "UOP_AllocLarge";
36 case UOP_AllocSmall: return "UOP_AllocSmall";
37 case UOP_SetFPReg: return "UOP_SetFPReg";
38 case UOP_SaveNonVol: return "UOP_SaveNonVol";
39 case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig";
40 case UOP_SaveXMM128: return "UOP_SaveXMM128";
41 case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big";
42 case UOP_PushMachFrame: return "UOP_PushMachFrame";
43 }
44}
45
46// Returns the name of a referenced register.
47static StringRef getUnwindRegisterName(uint8_t Reg) {
48 switch(Reg) {
49 default: llvm_unreachable("Invalid register");
50 case 0: return "RAX";
51 case 1: return "RCX";
52 case 2: return "RDX";
53 case 3: return "RBX";
54 case 4: return "RSP";
55 case 5: return "RBP";
56 case 6: return "RSI";
57 case 7: return "RDI";
58 case 8: return "R8";
59 case 9: return "R9";
60 case 10: return "R10";
61 case 11: return "R11";
62 case 12: return "R12";
63 case 13: return "R13";
64 case 14: return "R14";
65 case 15: return "R15";
66 }
67}
68
69// Calculates the number of array slots required for the unwind code.
70static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
71 switch (UnwindCode.getUnwindOp()) {
72 default: llvm_unreachable("Invalid unwind code");
73 case UOP_PushNonVol:
74 case UOP_AllocSmall:
75 case UOP_SetFPReg:
76 case UOP_PushMachFrame:
77 return 1;
78 case UOP_SaveNonVol:
79 case UOP_SaveXMM128:
80 return 2;
81 case UOP_SaveNonVolBig:
82 case UOP_SaveXMM128Big:
83 return 3;
84 case UOP_AllocLarge:
85 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
86 }
87}
88
89// Prints one unwind code. Because an unwind code can occupy up to 3 slots in
90// the unwind codes array, this function requires that the correct number of
91// slots is provided.
92static void printUnwindCode(ArrayRef<UnwindCode> UCs) {
93 assert(UCs.size() >= getNumUsedSlots(UCs[0]));
Rui Ueyama595932f2014-03-04 19:23:56 +000094 outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset))
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000095 << getUnwindCodeTypeName(UCs[0].getUnwindOp());
96 switch (UCs[0].getUnwindOp()) {
97 case UOP_PushNonVol:
98 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo());
99 break;
100 case UOP_AllocLarge:
101 if (UCs[0].getOpInfo() == 0) {
102 outs() << " " << UCs[1].FrameOffset;
103 } else {
104 outs() << " " << UCs[1].FrameOffset
105 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
106 }
107 break;
108 case UOP_AllocSmall:
109 outs() << " " << ((UCs[0].getOpInfo() + 1) * 8);
110 break;
111 case UOP_SetFPReg:
112 outs() << " ";
113 break;
114 case UOP_SaveNonVol:
115 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
116 << format(" [0x%04x]", 8 * UCs[1].FrameOffset);
117 break;
118 case UOP_SaveNonVolBig:
119 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
120 << format(" [0x%08x]", UCs[1].FrameOffset
121 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
122 break;
123 case UOP_SaveXMM128:
124 outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
125 << format(" [0x%04x]", 16 * UCs[1].FrameOffset);
126 break;
127 case UOP_SaveXMM128Big:
128 outs() << " XMM" << UCs[0].getOpInfo()
129 << format(" [0x%08x]", UCs[1].FrameOffset
130 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
131 break;
132 case UOP_PushMachFrame:
133 outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w")
134 << " error code";
135 break;
136 }
137 outs() << "\n";
138}
139
140static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) {
141 for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) {
142 unsigned UsedSlots = getNumUsedSlots(*I);
143 if (UsedSlots > UCs.size()) {
144 outs() << "Unwind data corrupted: Encountered unwind op "
145 << getUnwindCodeTypeName((*I).getUnwindOp())
146 << " which requires " << UsedSlots
147 << " slots, but only " << UCs.size()
148 << " remaining in buffer";
149 return ;
150 }
Craig Topper0013be12015-09-21 05:32:41 +0000151 printUnwindCode(makeArrayRef(I, E));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000152 I += UsedSlots;
153 }
154}
155
156// Given a symbol sym this functions returns the address and section of it.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000157static std::error_code
158resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
159 const coff_section *&ResolvedSection,
160 uint64_t &ResolvedAddr) {
Kevin Enderby931cb652016-06-24 18:24:42 +0000161 Expected<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
162 if (!ResolvedAddrOrErr)
163 return errorToErrorCode(ResolvedAddrOrErr.takeError());
Rafael Espindolaed067c42015-07-03 18:19:00 +0000164 ResolvedAddr = *ResolvedAddrOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000165 Expected<section_iterator> Iter = Sym.getSection();
166 if (!Iter)
167 return errorToErrorCode(Iter.takeError());
Rafael Espindola8bab8892015-08-07 23:27:14 +0000168 ResolvedSection = Obj->getCOFFSection(**Iter);
Rui Ueyama7d099192015-06-09 15:20:42 +0000169 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000170}
171
172// Given a vector of relocations for a section and an offset into this section
173// the function returns the symbol used for the relocation at the offset.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000174static std::error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
175 uint64_t Offset, SymbolRef &Sym) {
Davide Italianoc7d771d2016-09-30 23:22:42 +0000176 for (auto &R : Rels) {
177 uint64_t Ofs = R.getOffset();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000178 if (Ofs == Offset) {
Davide Italianoc7d771d2016-09-30 23:22:42 +0000179 Sym = *R.getSymbol();
Rui Ueyama7d099192015-06-09 15:20:42 +0000180 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000181 }
182 }
Rui Ueyamacf397842014-02-28 05:21:29 +0000183 return object_error::parse_failed;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000184}
185
186// Given a vector of relocations for a section and an offset into this section
187// the function resolves the symbol used for the relocation at the offset and
188// returns the section content and the address inside the content pointed to
189// by the symbol.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000190static std::error_code
191getSectionContents(const COFFObjectFile *Obj,
192 const std::vector<RelocationRef> &Rels, uint64_t Offset,
193 ArrayRef<uint8_t> &Contents, uint64_t &Addr) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000194 SymbolRef Sym;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000195 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
Rui Ueyama1618fe22014-02-28 05:21:26 +0000196 return EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000197 const coff_section *Section;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000198 if (std::error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000199 return EC;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000200 if (std::error_code EC = Obj->getSectionContents(Section, Contents))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000201 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000202 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000203}
204
205// Given a vector of relocations for a section and an offset into this section
206// the function returns the name of the symbol used for the relocation at the
207// offset.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000208static std::error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
209 uint64_t Offset, StringRef &Name) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000210 SymbolRef Sym;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000211 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000212 return EC;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000213 Expected<StringRef> NameOrErr = Sym.getName();
214 if (!NameOrErr)
215 return errorToErrorCode(NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000216 Name = *NameOrErr;
Rui Ueyama7d099192015-06-09 15:20:42 +0000217 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000218}
219
220static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
221 const std::vector<RelocationRef> &Rels,
222 uint64_t Offset, uint32_t Disp) {
223 StringRef Sym;
Rui Ueyamacf397842014-02-28 05:21:29 +0000224 if (!resolveSymbolName(Rels, Offset, Sym)) {
225 Out << Sym;
226 if (Disp > 0)
227 Out << format(" + 0x%04x", Disp);
228 } else {
229 Out << format("0x%04x", Disp);
230 }
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000231}
232
Rui Ueyama215a5862014-02-20 06:51:07 +0000233static void
234printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) {
235 if (Count == 0)
236 return;
237
238 const pe32_header *PE32Header;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000239 error(Obj->getPE32Header(PE32Header));
Rui Ueyama215a5862014-02-20 06:51:07 +0000240 uint32_t ImageBase = PE32Header->ImageBase;
241 uintptr_t IntPtr = 0;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000242 error(Obj->getVaPtr(TableVA, IntPtr));
Rui Ueyama215a5862014-02-20 06:51:07 +0000243 const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr;
244 outs() << "SEH Table:";
245 for (int I = 0; I < Count; ++I)
246 outs() << format(" 0x%x", P[I] + ImageBase);
247 outs() << "\n\n";
248}
249
David Majnemer0ab61bf2016-03-15 06:14:01 +0000250template <typename T>
251static void printTLSDirectoryT(const coff_tls_directory<T> *TLSDir) {
252 size_t FormatWidth = sizeof(T) * 2;
253 outs() << "TLS directory:"
254 << "\n StartAddressOfRawData: "
255 << format_hex(TLSDir->StartAddressOfRawData, FormatWidth)
256 << "\n EndAddressOfRawData: "
257 << format_hex(TLSDir->EndAddressOfRawData, FormatWidth)
258 << "\n AddressOfIndex: "
259 << format_hex(TLSDir->AddressOfIndex, FormatWidth)
260 << "\n AddressOfCallBacks: "
261 << format_hex(TLSDir->AddressOfCallBacks, FormatWidth)
262 << "\n SizeOfZeroFill: "
263 << TLSDir->SizeOfZeroFill
264 << "\n Characteristics: "
265 << TLSDir->Characteristics
266 << "\n Alignment: "
267 << TLSDir->getAlignment()
268 << "\n\n";
269}
270
271static void printTLSDirectory(const COFFObjectFile *Obj) {
272 const pe32_header *PE32Header;
273 error(Obj->getPE32Header(PE32Header));
274
275 const pe32plus_header *PE32PlusHeader;
276 error(Obj->getPE32PlusHeader(PE32PlusHeader));
277
278 // Skip if it's not executable.
279 if (!PE32Header && !PE32PlusHeader)
280 return;
281
282 const data_directory *DataDir;
283 error(Obj->getDataDirectory(COFF::TLS_TABLE, DataDir));
284 uintptr_t IntPtr = 0;
285 if (DataDir->RelativeVirtualAddress == 0)
286 return;
287 error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
288
289 if (PE32Header) {
290 auto *TLSDir = reinterpret_cast<const coff_tls_directory32 *>(IntPtr);
291 printTLSDirectoryT(TLSDir);
292 } else {
293 auto *TLSDir = reinterpret_cast<const coff_tls_directory64 *>(IntPtr);
294 printTLSDirectoryT(TLSDir);
295 }
296
297 outs() << "\n";
298}
299
Rui Ueyamac514a802014-02-19 03:53:11 +0000300static void printLoadConfiguration(const COFFObjectFile *Obj) {
Rui Ueyama5cf32852014-02-21 20:27:15 +0000301 // Skip if it's not executable.
302 const pe32_header *PE32Header;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000303 error(Obj->getPE32Header(PE32Header));
Rui Ueyama5cf32852014-02-21 20:27:15 +0000304 if (!PE32Header)
305 return;
306
Rui Ueyamac514a802014-02-19 03:53:11 +0000307 // Currently only x86 is supported
David Majnemer44f51e52014-09-10 12:51:52 +0000308 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_I386)
Rui Ueyamac514a802014-02-19 03:53:11 +0000309 return;
310
311 const data_directory *DataDir;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000312 error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir));
Rui Ueyamac514a802014-02-19 03:53:11 +0000313 uintptr_t IntPtr = 0;
314 if (DataDir->RelativeVirtualAddress == 0)
315 return;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000316 error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
Rui Ueyama215a5862014-02-20 06:51:07 +0000317
Rui Ueyama8c1d4c92014-03-04 04:15:59 +0000318 auto *LoadConf = reinterpret_cast<const coff_load_configuration32 *>(IntPtr);
Rui Ueyamac514a802014-02-19 03:53:11 +0000319 outs() << "Load configuration:"
320 << "\n Timestamp: " << LoadConf->TimeDateStamp
321 << "\n Major Version: " << LoadConf->MajorVersion
322 << "\n Minor Version: " << LoadConf->MinorVersion
323 << "\n GlobalFlags Clear: " << LoadConf->GlobalFlagsClear
324 << "\n GlobalFlags Set: " << LoadConf->GlobalFlagsSet
325 << "\n Critical Section Default Timeout: " << LoadConf->CriticalSectionDefaultTimeout
326 << "\n Decommit Free Block Threshold: " << LoadConf->DeCommitFreeBlockThreshold
327 << "\n Decommit Total Free Threshold: " << LoadConf->DeCommitTotalFreeThreshold
328 << "\n Lock Prefix Table: " << LoadConf->LockPrefixTable
329 << "\n Maximum Allocation Size: " << LoadConf->MaximumAllocationSize
330 << "\n Virtual Memory Threshold: " << LoadConf->VirtualMemoryThreshold
331 << "\n Process Affinity Mask: " << LoadConf->ProcessAffinityMask
332 << "\n Process Heap Flags: " << LoadConf->ProcessHeapFlags
333 << "\n CSD Version: " << LoadConf->CSDVersion
334 << "\n Security Cookie: " << LoadConf->SecurityCookie
335 << "\n SEH Table: " << LoadConf->SEHandlerTable
336 << "\n SEH Count: " << LoadConf->SEHandlerCount
337 << "\n\n";
Rui Ueyama215a5862014-02-20 06:51:07 +0000338 printSEHTable(Obj, LoadConf->SEHandlerTable, LoadConf->SEHandlerCount);
339 outs() << "\n";
Rui Ueyamac514a802014-02-19 03:53:11 +0000340}
341
Rui Ueyamac2bed422013-09-27 21:04:00 +0000342// Prints import tables. The import table is a table containing the list of
343// DLL name and symbol names which will be linked by the loader.
344static void printImportTables(const COFFObjectFile *Obj) {
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000345 import_directory_iterator I = Obj->import_directory_begin();
346 import_directory_iterator E = Obj->import_directory_end();
347 if (I == E)
Rui Ueyama908dfcd2014-01-15 23:46:18 +0000348 return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000349 outs() << "The Import Tables:\n";
David Majnemerad7b7e72016-06-26 04:36:32 +0000350 for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) {
David Majnemer1c0aa042016-07-31 19:25:21 +0000351 const coff_import_directory_table_entry *Dir;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000352 StringRef Name;
David Majnemerad7b7e72016-06-26 04:36:32 +0000353 if (DirRef.getImportTableEntry(Dir)) return;
354 if (DirRef.getName(Name)) return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000355
356 outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n",
357 static_cast<uint32_t>(Dir->ImportLookupTableRVA),
358 static_cast<uint32_t>(Dir->TimeDateStamp),
359 static_cast<uint32_t>(Dir->ForwarderChain),
360 static_cast<uint32_t>(Dir->NameRVA),
361 static_cast<uint32_t>(Dir->ImportAddressTableRVA));
362 outs() << " DLL Name: " << Name << "\n";
363 outs() << " Hint/Ord Name\n";
David Majnemerad7b7e72016-06-26 04:36:32 +0000364 for (const ImportedSymbolRef &Entry : DirRef.imported_symbols()) {
365 bool IsOrdinal;
366 if (Entry.isOrdinal(IsOrdinal))
367 return;
368 if (IsOrdinal) {
369 uint16_t Ordinal;
370 if (Entry.getOrdinal(Ordinal))
371 return;
372 outs() << format(" % 6d\n", Ordinal);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000373 continue;
374 }
David Majnemerad7b7e72016-06-26 04:36:32 +0000375 uint32_t HintNameRVA;
376 if (Entry.getHintNameRVA(HintNameRVA))
377 return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000378 uint16_t Hint;
379 StringRef Name;
David Majnemerad7b7e72016-06-26 04:36:32 +0000380 if (Obj->getHintName(HintNameRVA, Hint, Name))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000381 return;
382 outs() << format(" % 6d ", Hint) << Name << "\n";
383 }
384 outs() << "\n";
385 }
386}
387
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000388// Prints export tables. The export table is a table containing the list of
389// exported symbol from the DLL.
390static void printExportTable(const COFFObjectFile *Obj) {
391 outs() << "Export Table:\n";
392 export_directory_iterator I = Obj->export_directory_begin();
393 export_directory_iterator E = Obj->export_directory_end();
394 if (I == E)
395 return;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000396 StringRef DllName;
Rui Ueyamae5df6092014-01-17 22:02:24 +0000397 uint32_t OrdinalBase;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000398 if (I->getDllName(DllName))
399 return;
Rui Ueyamae5df6092014-01-17 22:02:24 +0000400 if (I->getOrdinalBase(OrdinalBase))
401 return;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000402 outs() << " DLL name: " << DllName << "\n";
Rui Ueyamae5df6092014-01-17 22:02:24 +0000403 outs() << " Ordinal base: " << OrdinalBase << "\n";
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000404 outs() << " Ordinal RVA Name\n";
Rafael Espindola5e812af2014-01-30 02:49:50 +0000405 for (; I != E; I = ++I) {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000406 uint32_t Ordinal;
407 if (I->getOrdinal(Ordinal))
408 return;
409 uint32_t RVA;
410 if (I->getExportRVA(RVA))
411 return;
Rui Ueyama6161b382016-01-12 23:28:42 +0000412 bool IsForwarder;
413 if (I->isForwarder(IsForwarder))
414 return;
415
416 if (IsForwarder) {
417 // Export table entries can be used to re-export symbols that
418 // this COFF file is imported from some DLLs. This is rare.
419 // In most cases IsForwarder is false.
420 outs() << format(" % 4d ", Ordinal);
421 } else {
422 outs() << format(" % 4d %# 8x", Ordinal, RVA);
423 }
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000424
425 StringRef Name;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000426 if (I->getSymbolName(Name))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000427 continue;
428 if (!Name.empty())
429 outs() << " " << Name;
Rui Ueyama6161b382016-01-12 23:28:42 +0000430 if (IsForwarder) {
431 StringRef S;
432 if (I->getForwardTo(S))
433 return;
434 outs() << " (forwarded to " << S << ")";
435 }
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000436 outs() << "\n";
437 }
438}
439
Rui Ueyama39f1b972014-03-04 00:31:48 +0000440// Given the COFF object file, this function returns the relocations for .pdata
441// and the pointer to "runtime function" structs.
442static bool getPDataSection(const COFFObjectFile *Obj,
443 std::vector<RelocationRef> &Rels,
444 const RuntimeFunction *&RFStart, int &NumRFs) {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000445 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000446 StringRef Name;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000447 error(Section.getName(Name));
Rui Ueyama39f1b972014-03-04 00:31:48 +0000448 if (Name != ".pdata")
449 continue;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000450
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000451 const coff_section *Pdata = Obj->getCOFFSection(Section);
452 for (const RelocationRef &Reloc : Section.relocations())
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000453 Rels.push_back(Reloc);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000454
455 // Sort relocations by address.
Mandeep Singh Grang8db564e2018-04-01 21:24:53 +0000456 llvm::sort(Rels.begin(), Rels.end(), RelocAddressLess);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000457
458 ArrayRef<uint8_t> Contents;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000459 error(Obj->getSectionContents(Pdata, Contents));
Rui Ueyama39f1b972014-03-04 00:31:48 +0000460 if (Contents.empty())
461 continue;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000462
Rui Ueyama39f1b972014-03-04 00:31:48 +0000463 RFStart = reinterpret_cast<const RuntimeFunction *>(Contents.data());
464 NumRFs = Contents.size() / sizeof(RuntimeFunction);
465 return true;
466 }
467 return false;
468}
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000469
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000470static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) {
Rui Ueyama39f1b972014-03-04 00:31:48 +0000471 // The casts to int are required in order to output the value as number.
472 // Without the casts the value would be interpreted as char data (which
473 // results in garbage output).
Rui Ueyama595932f2014-03-04 19:23:56 +0000474 outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n";
475 outs() << " Flags: " << static_cast<int>(UI->getFlags());
Rui Ueyama39f1b972014-03-04 00:31:48 +0000476 if (UI->getFlags()) {
477 if (UI->getFlags() & UNW_ExceptionHandler)
478 outs() << " UNW_ExceptionHandler";
479 if (UI->getFlags() & UNW_TerminateHandler)
480 outs() << " UNW_TerminateHandler";
481 if (UI->getFlags() & UNW_ChainInfo)
482 outs() << " UNW_ChainInfo";
483 }
484 outs() << "\n";
Rui Ueyama595932f2014-03-04 19:23:56 +0000485 outs() << " Size of prolog: " << static_cast<int>(UI->PrologSize) << "\n";
486 outs() << " Number of Codes: " << static_cast<int>(UI->NumCodes) << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000487 // Maybe this should move to output of UOP_SetFPReg?
488 if (UI->getFrameRegister()) {
Rui Ueyama595932f2014-03-04 19:23:56 +0000489 outs() << " Frame register: "
Rui Ueyama39f1b972014-03-04 00:31:48 +0000490 << getUnwindRegisterName(UI->getFrameRegister()) << "\n";
Rui Ueyama595932f2014-03-04 19:23:56 +0000491 outs() << " Frame offset: " << 16 * UI->getFrameOffset() << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000492 } else {
Rui Ueyama595932f2014-03-04 19:23:56 +0000493 outs() << " No frame pointer used\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000494 }
495 if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
496 // FIXME: Output exception handler data
497 } else if (UI->getFlags() & UNW_ChainInfo) {
498 // FIXME: Output chained unwind info
499 }
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000500
Rui Ueyama39f1b972014-03-04 00:31:48 +0000501 if (UI->NumCodes)
Rui Ueyama595932f2014-03-04 19:23:56 +0000502 outs() << " Unwind Codes:\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000503
Craig Topper0013be12015-09-21 05:32:41 +0000504 printAllUnwindCodes(makeArrayRef(&UI->UnwindCodes[0], UI->NumCodes));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000505
Rui Ueyama595932f2014-03-04 19:23:56 +0000506 outs() << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000507 outs().flush();
508}
509
Rui Ueyamafd3cb9e2014-03-04 04:22:41 +0000510/// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
Rui Ueyama9c674e62014-03-04 04:00:55 +0000511/// pointing to an executable file.
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000512static void printRuntimeFunction(const COFFObjectFile *Obj,
Rui Ueyama9c674e62014-03-04 04:00:55 +0000513 const RuntimeFunction &RF) {
514 if (!RF.StartAddress)
515 return;
516 outs() << "Function Table:\n"
Rui Ueyamaad807652014-03-05 23:03:37 +0000517 << format(" Start Address: 0x%04x\n",
518 static_cast<uint32_t>(RF.StartAddress))
519 << format(" End Address: 0x%04x\n",
520 static_cast<uint32_t>(RF.EndAddress))
521 << format(" Unwind Info Address: 0x%04x\n",
522 static_cast<uint32_t>(RF.UnwindInfoOffset));
Rui Ueyama9c674e62014-03-04 04:00:55 +0000523 uintptr_t addr;
524 if (Obj->getRvaPtr(RF.UnwindInfoOffset, addr))
525 return;
526 printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo *>(addr));
527}
528
Rui Ueyamafd3cb9e2014-03-04 04:22:41 +0000529/// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
530/// pointing to an object file. Unlike executable, fields in RuntimeFunction
Rui Ueyama9c674e62014-03-04 04:00:55 +0000531/// struct are filled with zeros, but instead there are relocations pointing to
532/// them so that the linker will fill targets' RVAs to the fields at link
533/// time. This function interprets the relocations to find the data to be used
534/// in the resulting executable.
535static void printRuntimeFunctionRels(const COFFObjectFile *Obj,
536 const RuntimeFunction &RF,
537 uint64_t SectionOffset,
538 const std::vector<RelocationRef> &Rels) {
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000539 outs() << "Function Table:\n";
540 outs() << " Start Address: ";
541 printCOFFSymbolAddress(outs(), Rels,
542 SectionOffset +
543 /*offsetof(RuntimeFunction, StartAddress)*/ 0,
544 RF.StartAddress);
545 outs() << "\n";
546
547 outs() << " End Address: ";
548 printCOFFSymbolAddress(outs(), Rels,
549 SectionOffset +
550 /*offsetof(RuntimeFunction, EndAddress)*/ 4,
551 RF.EndAddress);
552 outs() << "\n";
553
554 outs() << " Unwind Info Address: ";
555 printCOFFSymbolAddress(outs(), Rels,
556 SectionOffset +
557 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
558 RF.UnwindInfoOffset);
559 outs() << "\n";
560
561 ArrayRef<uint8_t> XContents;
562 uint64_t UnwindInfoOffset = 0;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000563 error(getSectionContents(
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000564 Obj, Rels, SectionOffset +
565 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
Davide Italianoccd53fe2015-08-05 07:18:31 +0000566 XContents, UnwindInfoOffset));
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000567 if (XContents.empty())
568 return;
569
570 UnwindInfoOffset += RF.UnwindInfoOffset;
571 if (UnwindInfoOffset > XContents.size())
572 return;
573
Rui Ueyama8c1d4c92014-03-04 04:15:59 +0000574 auto *UI = reinterpret_cast<const Win64EH::UnwindInfo *>(XContents.data() +
575 UnwindInfoOffset);
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000576 printWin64EHUnwindInfo(UI);
577}
578
Rui Ueyama39f1b972014-03-04 00:31:48 +0000579void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
David Majnemer44f51e52014-09-10 12:51:52 +0000580 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64) {
Rui Ueyama39f1b972014-03-04 00:31:48 +0000581 errs() << "Unsupported image machine type "
582 "(currently only AMD64 is supported).\n";
583 return;
584 }
585
586 std::vector<RelocationRef> Rels;
587 const RuntimeFunction *RFStart;
588 int NumRFs;
589 if (!getPDataSection(Obj, Rels, RFStart, NumRFs))
590 return;
591 ArrayRef<RuntimeFunction> RFs(RFStart, NumRFs);
592
Rui Ueyama9c674e62014-03-04 04:00:55 +0000593 bool IsExecutable = Rels.empty();
594 if (IsExecutable) {
595 for (const RuntimeFunction &RF : RFs)
596 printRuntimeFunction(Obj, RF);
597 return;
598 }
599
Rui Ueyama39f1b972014-03-04 00:31:48 +0000600 for (const RuntimeFunction &RF : RFs) {
601 uint64_t SectionOffset =
602 std::distance(RFs.begin(), &RF) * sizeof(RuntimeFunction);
Rui Ueyama9c674e62014-03-04 04:00:55 +0000603 printRuntimeFunctionRels(Obj, RF, SectionOffset, Rels);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000604 }
605}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000606
607void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000608 const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj);
David Majnemer0ab61bf2016-03-15 06:14:01 +0000609 printTLSDirectory(file);
Rui Ueyamac514a802014-02-19 03:53:11 +0000610 printLoadConfiguration(file);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000611 printImportTables(file);
612 printExportTable(file);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000613}
Davide Italianoe85abf72015-12-20 09:54:34 +0000614
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +0000615void llvm::printCOFFSymbolTable(const object::COFFImportFile *i) {
616 unsigned Index = 0;
617 bool IsCode = i->getCOFFImportHeader()->getType() == COFF::IMPORT_CODE;
618
619 for (const object::BasicSymbolRef &Sym : i->symbols()) {
620 std::string Name;
621 raw_string_ostream NS(Name);
622
623 Sym.printName(NS);
624 NS.flush();
625
626 outs() << "[" << format("%2d", Index) << "]"
627 << "(sec " << format("%2d", 0) << ")"
628 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
629 << "(ty " << format("%3x", (IsCode && Index) ? 32 : 0) << ")"
630 << "(scl " << format("%3x", 0) << ") "
631 << "(nx " << 0 << ") "
632 << "0x" << format("%08x", 0) << " " << Name << '\n';
633
634 ++Index;
635 }
636}
637
Davide Italianoe85abf72015-12-20 09:54:34 +0000638void llvm::printCOFFSymbolTable(const COFFObjectFile *coff) {
639 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
Rafael Espindola74894922017-10-11 17:23:15 +0000640 Expected<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Davide Italianoe85abf72015-12-20 09:54:34 +0000641 StringRef Name;
Rafael Espindola74894922017-10-11 17:23:15 +0000642 error(errorToErrorCode(Symbol.takeError()));
Davide Italianoe85abf72015-12-20 09:54:34 +0000643 error(coff->getSymbolName(*Symbol, Name));
644
645 outs() << "[" << format("%2d", SI) << "]"
646 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
647 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
648 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
649 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
650 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
651 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
652 << Name << "\n";
653
654 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
655 if (Symbol->isSectionDefinition()) {
656 const coff_aux_section_definition *asd;
657 error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd));
658
659 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
660
661 outs() << "AUX "
662 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
663 , unsigned(asd->Length)
664 , unsigned(asd->NumberOfRelocations)
665 , unsigned(asd->NumberOfLinenumbers)
666 , unsigned(asd->CheckSum))
667 << format("assoc %d comdat %d\n"
668 , unsigned(AuxNumber)
669 , unsigned(asd->Selection));
670 } else if (Symbol->isFileRecord()) {
671 const char *FileName;
672 error(coff->getAuxSymbol<char>(SI + 1, FileName));
673
674 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
675 coff->getSymbolTableEntrySize());
676 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
677
678 SI = SI + Symbol->getNumberOfAuxSymbols();
679 break;
Saleem Abdulrasoolfbf920f2016-05-26 01:45:12 +0000680 } else if (Symbol->isWeakExternal()) {
681 const coff_aux_weak_external *awe;
682 error(coff->getAuxSymbol<coff_aux_weak_external>(SI + 1, awe));
683
684 outs() << "AUX " << format("indx %d srch %d\n",
685 static_cast<uint32_t>(awe->TagIndex),
686 static_cast<uint32_t>(awe->Characteristics));
Davide Italianoe85abf72015-12-20 09:54:34 +0000687 } else {
688 outs() << "AUX Unknown\n";
689 }
690 }
691 }
692}