blob: db549bbe3eec77afd1de35f0c05f1819f20323fb [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"
23#include "llvm/Support/SourceMgr.h"
Chandler Carruthb034cb72013-01-02 10:26:28 +000024#include "llvm/Support/Win64EH.h"
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000025#include "llvm/Support/raw_ostream.h"
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000026#include <algorithm>
27#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000028#include <system_error>
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000029
30using namespace llvm;
31using namespace object;
32using namespace llvm::Win64EH;
33
34// Returns the name of the unwind code.
35static StringRef getUnwindCodeTypeName(uint8_t Code) {
36 switch(Code) {
37 default: llvm_unreachable("Invalid unwind code");
38 case UOP_PushNonVol: return "UOP_PushNonVol";
39 case UOP_AllocLarge: return "UOP_AllocLarge";
40 case UOP_AllocSmall: return "UOP_AllocSmall";
41 case UOP_SetFPReg: return "UOP_SetFPReg";
42 case UOP_SaveNonVol: return "UOP_SaveNonVol";
43 case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig";
44 case UOP_SaveXMM128: return "UOP_SaveXMM128";
45 case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big";
46 case UOP_PushMachFrame: return "UOP_PushMachFrame";
47 }
48}
49
50// Returns the name of a referenced register.
51static StringRef getUnwindRegisterName(uint8_t Reg) {
52 switch(Reg) {
53 default: llvm_unreachable("Invalid register");
54 case 0: return "RAX";
55 case 1: return "RCX";
56 case 2: return "RDX";
57 case 3: return "RBX";
58 case 4: return "RSP";
59 case 5: return "RBP";
60 case 6: return "RSI";
61 case 7: return "RDI";
62 case 8: return "R8";
63 case 9: return "R9";
64 case 10: return "R10";
65 case 11: return "R11";
66 case 12: return "R12";
67 case 13: return "R13";
68 case 14: return "R14";
69 case 15: return "R15";
70 }
71}
72
73// Calculates the number of array slots required for the unwind code.
74static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
75 switch (UnwindCode.getUnwindOp()) {
76 default: llvm_unreachable("Invalid unwind code");
77 case UOP_PushNonVol:
78 case UOP_AllocSmall:
79 case UOP_SetFPReg:
80 case UOP_PushMachFrame:
81 return 1;
82 case UOP_SaveNonVol:
83 case UOP_SaveXMM128:
84 return 2;
85 case UOP_SaveNonVolBig:
86 case UOP_SaveXMM128Big:
87 return 3;
88 case UOP_AllocLarge:
89 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
90 }
91}
92
93// Prints one unwind code. Because an unwind code can occupy up to 3 slots in
94// the unwind codes array, this function requires that the correct number of
95// slots is provided.
96static void printUnwindCode(ArrayRef<UnwindCode> UCs) {
97 assert(UCs.size() >= getNumUsedSlots(UCs[0]));
Rui Ueyama595932f2014-03-04 19:23:56 +000098 outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset))
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000099 << getUnwindCodeTypeName(UCs[0].getUnwindOp());
100 switch (UCs[0].getUnwindOp()) {
101 case UOP_PushNonVol:
102 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo());
103 break;
104 case UOP_AllocLarge:
105 if (UCs[0].getOpInfo() == 0) {
106 outs() << " " << UCs[1].FrameOffset;
107 } else {
108 outs() << " " << UCs[1].FrameOffset
109 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
110 }
111 break;
112 case UOP_AllocSmall:
113 outs() << " " << ((UCs[0].getOpInfo() + 1) * 8);
114 break;
115 case UOP_SetFPReg:
116 outs() << " ";
117 break;
118 case UOP_SaveNonVol:
119 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
120 << format(" [0x%04x]", 8 * UCs[1].FrameOffset);
121 break;
122 case UOP_SaveNonVolBig:
123 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
124 << format(" [0x%08x]", UCs[1].FrameOffset
125 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
126 break;
127 case UOP_SaveXMM128:
128 outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
129 << format(" [0x%04x]", 16 * UCs[1].FrameOffset);
130 break;
131 case UOP_SaveXMM128Big:
132 outs() << " XMM" << UCs[0].getOpInfo()
133 << format(" [0x%08x]", UCs[1].FrameOffset
134 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
135 break;
136 case UOP_PushMachFrame:
137 outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w")
138 << " error code";
139 break;
140 }
141 outs() << "\n";
142}
143
144static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) {
145 for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) {
146 unsigned UsedSlots = getNumUsedSlots(*I);
147 if (UsedSlots > UCs.size()) {
148 outs() << "Unwind data corrupted: Encountered unwind op "
149 << getUnwindCodeTypeName((*I).getUnwindOp())
150 << " which requires " << UsedSlots
151 << " slots, but only " << UCs.size()
152 << " remaining in buffer";
153 return ;
154 }
Craig Topper0013be12015-09-21 05:32:41 +0000155 printUnwindCode(makeArrayRef(I, E));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000156 I += UsedSlots;
157 }
158}
159
160// Given a symbol sym this functions returns the address and section of it.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000161static std::error_code
162resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
163 const coff_section *&ResolvedSection,
164 uint64_t &ResolvedAddr) {
Kevin Enderby931cb652016-06-24 18:24:42 +0000165 Expected<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
166 if (!ResolvedAddrOrErr)
167 return errorToErrorCode(ResolvedAddrOrErr.takeError());
Rafael Espindolaed067c42015-07-03 18:19:00 +0000168 ResolvedAddr = *ResolvedAddrOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000169 Expected<section_iterator> Iter = Sym.getSection();
170 if (!Iter)
171 return errorToErrorCode(Iter.takeError());
Rafael Espindola8bab8892015-08-07 23:27:14 +0000172 ResolvedSection = Obj->getCOFFSection(**Iter);
Rui Ueyama7d099192015-06-09 15:20:42 +0000173 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000174}
175
176// Given a vector of relocations for a section and an offset into this section
177// the function returns the symbol used for the relocation at the offset.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000178static std::error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
179 uint64_t Offset, SymbolRef &Sym) {
Davide Italianoc7d771d2016-09-30 23:22:42 +0000180 for (auto &R : Rels) {
181 uint64_t Ofs = R.getOffset();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000182 if (Ofs == Offset) {
Davide Italianoc7d771d2016-09-30 23:22:42 +0000183 Sym = *R.getSymbol();
Rui Ueyama7d099192015-06-09 15:20:42 +0000184 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000185 }
186 }
Rui Ueyamacf397842014-02-28 05:21:29 +0000187 return object_error::parse_failed;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000188}
189
190// Given a vector of relocations for a section and an offset into this section
191// the function resolves the symbol used for the relocation at the offset and
192// returns the section content and the address inside the content pointed to
193// by the symbol.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000194static std::error_code
195getSectionContents(const COFFObjectFile *Obj,
196 const std::vector<RelocationRef> &Rels, uint64_t Offset,
197 ArrayRef<uint8_t> &Contents, uint64_t &Addr) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000198 SymbolRef Sym;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000199 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
Rui Ueyama1618fe22014-02-28 05:21:26 +0000200 return EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000201 const coff_section *Section;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000202 if (std::error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000203 return EC;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000204 if (std::error_code EC = Obj->getSectionContents(Section, Contents))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000205 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000206 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000207}
208
209// Given a vector of relocations for a section and an offset into this section
210// the function returns the name of the symbol used for the relocation at the
211// offset.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000212static std::error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
213 uint64_t Offset, StringRef &Name) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000214 SymbolRef Sym;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000215 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000216 return EC;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000217 Expected<StringRef> NameOrErr = Sym.getName();
218 if (!NameOrErr)
219 return errorToErrorCode(NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000220 Name = *NameOrErr;
Rui Ueyama7d099192015-06-09 15:20:42 +0000221 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000222}
223
224static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
225 const std::vector<RelocationRef> &Rels,
226 uint64_t Offset, uint32_t Disp) {
227 StringRef Sym;
Rui Ueyamacf397842014-02-28 05:21:29 +0000228 if (!resolveSymbolName(Rels, Offset, Sym)) {
229 Out << Sym;
230 if (Disp > 0)
231 Out << format(" + 0x%04x", Disp);
232 } else {
233 Out << format("0x%04x", Disp);
234 }
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000235}
236
Rui Ueyama215a5862014-02-20 06:51:07 +0000237static void
238printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) {
239 if (Count == 0)
240 return;
241
242 const pe32_header *PE32Header;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000243 error(Obj->getPE32Header(PE32Header));
Rui Ueyama215a5862014-02-20 06:51:07 +0000244 uint32_t ImageBase = PE32Header->ImageBase;
245 uintptr_t IntPtr = 0;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000246 error(Obj->getVaPtr(TableVA, IntPtr));
Rui Ueyama215a5862014-02-20 06:51:07 +0000247 const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr;
248 outs() << "SEH Table:";
249 for (int I = 0; I < Count; ++I)
250 outs() << format(" 0x%x", P[I] + ImageBase);
251 outs() << "\n\n";
252}
253
David Majnemer0ab61bf2016-03-15 06:14:01 +0000254template <typename T>
255static void printTLSDirectoryT(const coff_tls_directory<T> *TLSDir) {
256 size_t FormatWidth = sizeof(T) * 2;
257 outs() << "TLS directory:"
258 << "\n StartAddressOfRawData: "
259 << format_hex(TLSDir->StartAddressOfRawData, FormatWidth)
260 << "\n EndAddressOfRawData: "
261 << format_hex(TLSDir->EndAddressOfRawData, FormatWidth)
262 << "\n AddressOfIndex: "
263 << format_hex(TLSDir->AddressOfIndex, FormatWidth)
264 << "\n AddressOfCallBacks: "
265 << format_hex(TLSDir->AddressOfCallBacks, FormatWidth)
266 << "\n SizeOfZeroFill: "
267 << TLSDir->SizeOfZeroFill
268 << "\n Characteristics: "
269 << TLSDir->Characteristics
270 << "\n Alignment: "
271 << TLSDir->getAlignment()
272 << "\n\n";
273}
274
275static void printTLSDirectory(const COFFObjectFile *Obj) {
276 const pe32_header *PE32Header;
277 error(Obj->getPE32Header(PE32Header));
278
279 const pe32plus_header *PE32PlusHeader;
280 error(Obj->getPE32PlusHeader(PE32PlusHeader));
281
282 // Skip if it's not executable.
283 if (!PE32Header && !PE32PlusHeader)
284 return;
285
286 const data_directory *DataDir;
287 error(Obj->getDataDirectory(COFF::TLS_TABLE, DataDir));
288 uintptr_t IntPtr = 0;
289 if (DataDir->RelativeVirtualAddress == 0)
290 return;
291 error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
292
293 if (PE32Header) {
294 auto *TLSDir = reinterpret_cast<const coff_tls_directory32 *>(IntPtr);
295 printTLSDirectoryT(TLSDir);
296 } else {
297 auto *TLSDir = reinterpret_cast<const coff_tls_directory64 *>(IntPtr);
298 printTLSDirectoryT(TLSDir);
299 }
300
301 outs() << "\n";
302}
303
Rui Ueyamac514a802014-02-19 03:53:11 +0000304static void printLoadConfiguration(const COFFObjectFile *Obj) {
Rui Ueyama5cf32852014-02-21 20:27:15 +0000305 // Skip if it's not executable.
306 const pe32_header *PE32Header;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000307 error(Obj->getPE32Header(PE32Header));
Rui Ueyama5cf32852014-02-21 20:27:15 +0000308 if (!PE32Header)
309 return;
310
Rui Ueyamac514a802014-02-19 03:53:11 +0000311 // Currently only x86 is supported
David Majnemer44f51e52014-09-10 12:51:52 +0000312 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_I386)
Rui Ueyamac514a802014-02-19 03:53:11 +0000313 return;
314
315 const data_directory *DataDir;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000316 error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir));
Rui Ueyamac514a802014-02-19 03:53:11 +0000317 uintptr_t IntPtr = 0;
318 if (DataDir->RelativeVirtualAddress == 0)
319 return;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000320 error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
Rui Ueyama215a5862014-02-20 06:51:07 +0000321
Rui Ueyama8c1d4c92014-03-04 04:15:59 +0000322 auto *LoadConf = reinterpret_cast<const coff_load_configuration32 *>(IntPtr);
Rui Ueyamac514a802014-02-19 03:53:11 +0000323 outs() << "Load configuration:"
324 << "\n Timestamp: " << LoadConf->TimeDateStamp
325 << "\n Major Version: " << LoadConf->MajorVersion
326 << "\n Minor Version: " << LoadConf->MinorVersion
327 << "\n GlobalFlags Clear: " << LoadConf->GlobalFlagsClear
328 << "\n GlobalFlags Set: " << LoadConf->GlobalFlagsSet
329 << "\n Critical Section Default Timeout: " << LoadConf->CriticalSectionDefaultTimeout
330 << "\n Decommit Free Block Threshold: " << LoadConf->DeCommitFreeBlockThreshold
331 << "\n Decommit Total Free Threshold: " << LoadConf->DeCommitTotalFreeThreshold
332 << "\n Lock Prefix Table: " << LoadConf->LockPrefixTable
333 << "\n Maximum Allocation Size: " << LoadConf->MaximumAllocationSize
334 << "\n Virtual Memory Threshold: " << LoadConf->VirtualMemoryThreshold
335 << "\n Process Affinity Mask: " << LoadConf->ProcessAffinityMask
336 << "\n Process Heap Flags: " << LoadConf->ProcessHeapFlags
337 << "\n CSD Version: " << LoadConf->CSDVersion
338 << "\n Security Cookie: " << LoadConf->SecurityCookie
339 << "\n SEH Table: " << LoadConf->SEHandlerTable
340 << "\n SEH Count: " << LoadConf->SEHandlerCount
341 << "\n\n";
Rui Ueyama215a5862014-02-20 06:51:07 +0000342 printSEHTable(Obj, LoadConf->SEHandlerTable, LoadConf->SEHandlerCount);
343 outs() << "\n";
Rui Ueyamac514a802014-02-19 03:53:11 +0000344}
345
Rui Ueyamac2bed422013-09-27 21:04:00 +0000346// Prints import tables. The import table is a table containing the list of
347// DLL name and symbol names which will be linked by the loader.
348static void printImportTables(const COFFObjectFile *Obj) {
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000349 import_directory_iterator I = Obj->import_directory_begin();
350 import_directory_iterator E = Obj->import_directory_end();
351 if (I == E)
Rui Ueyama908dfcd2014-01-15 23:46:18 +0000352 return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000353 outs() << "The Import Tables:\n";
David Majnemerad7b7e72016-06-26 04:36:32 +0000354 for (const ImportDirectoryEntryRef &DirRef : Obj->import_directories()) {
David Majnemer1c0aa042016-07-31 19:25:21 +0000355 const coff_import_directory_table_entry *Dir;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000356 StringRef Name;
David Majnemerad7b7e72016-06-26 04:36:32 +0000357 if (DirRef.getImportTableEntry(Dir)) return;
358 if (DirRef.getName(Name)) return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000359
360 outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n",
361 static_cast<uint32_t>(Dir->ImportLookupTableRVA),
362 static_cast<uint32_t>(Dir->TimeDateStamp),
363 static_cast<uint32_t>(Dir->ForwarderChain),
364 static_cast<uint32_t>(Dir->NameRVA),
365 static_cast<uint32_t>(Dir->ImportAddressTableRVA));
366 outs() << " DLL Name: " << Name << "\n";
367 outs() << " Hint/Ord Name\n";
David Majnemerad7b7e72016-06-26 04:36:32 +0000368 for (const ImportedSymbolRef &Entry : DirRef.imported_symbols()) {
369 bool IsOrdinal;
370 if (Entry.isOrdinal(IsOrdinal))
371 return;
372 if (IsOrdinal) {
373 uint16_t Ordinal;
374 if (Entry.getOrdinal(Ordinal))
375 return;
376 outs() << format(" % 6d\n", Ordinal);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000377 continue;
378 }
David Majnemerad7b7e72016-06-26 04:36:32 +0000379 uint32_t HintNameRVA;
380 if (Entry.getHintNameRVA(HintNameRVA))
381 return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000382 uint16_t Hint;
383 StringRef Name;
David Majnemerad7b7e72016-06-26 04:36:32 +0000384 if (Obj->getHintName(HintNameRVA, Hint, Name))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000385 return;
386 outs() << format(" % 6d ", Hint) << Name << "\n";
387 }
388 outs() << "\n";
389 }
390}
391
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000392// Prints export tables. The export table is a table containing the list of
393// exported symbol from the DLL.
394static void printExportTable(const COFFObjectFile *Obj) {
395 outs() << "Export Table:\n";
396 export_directory_iterator I = Obj->export_directory_begin();
397 export_directory_iterator E = Obj->export_directory_end();
398 if (I == E)
399 return;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000400 StringRef DllName;
Rui Ueyamae5df6092014-01-17 22:02:24 +0000401 uint32_t OrdinalBase;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000402 if (I->getDllName(DllName))
403 return;
Rui Ueyamae5df6092014-01-17 22:02:24 +0000404 if (I->getOrdinalBase(OrdinalBase))
405 return;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000406 outs() << " DLL name: " << DllName << "\n";
Rui Ueyamae5df6092014-01-17 22:02:24 +0000407 outs() << " Ordinal base: " << OrdinalBase << "\n";
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000408 outs() << " Ordinal RVA Name\n";
Rafael Espindola5e812af2014-01-30 02:49:50 +0000409 for (; I != E; I = ++I) {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000410 uint32_t Ordinal;
411 if (I->getOrdinal(Ordinal))
412 return;
413 uint32_t RVA;
414 if (I->getExportRVA(RVA))
415 return;
Rui Ueyama6161b382016-01-12 23:28:42 +0000416 bool IsForwarder;
417 if (I->isForwarder(IsForwarder))
418 return;
419
420 if (IsForwarder) {
421 // Export table entries can be used to re-export symbols that
422 // this COFF file is imported from some DLLs. This is rare.
423 // In most cases IsForwarder is false.
424 outs() << format(" % 4d ", Ordinal);
425 } else {
426 outs() << format(" % 4d %# 8x", Ordinal, RVA);
427 }
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000428
429 StringRef Name;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000430 if (I->getSymbolName(Name))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000431 continue;
432 if (!Name.empty())
433 outs() << " " << Name;
Rui Ueyama6161b382016-01-12 23:28:42 +0000434 if (IsForwarder) {
435 StringRef S;
436 if (I->getForwardTo(S))
437 return;
438 outs() << " (forwarded to " << S << ")";
439 }
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000440 outs() << "\n";
441 }
442}
443
Rui Ueyama39f1b972014-03-04 00:31:48 +0000444// Given the COFF object file, this function returns the relocations for .pdata
445// and the pointer to "runtime function" structs.
446static bool getPDataSection(const COFFObjectFile *Obj,
447 std::vector<RelocationRef> &Rels,
448 const RuntimeFunction *&RFStart, int &NumRFs) {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000449 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000450 StringRef Name;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000451 error(Section.getName(Name));
Rui Ueyama39f1b972014-03-04 00:31:48 +0000452 if (Name != ".pdata")
453 continue;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000454
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000455 const coff_section *Pdata = Obj->getCOFFSection(Section);
456 for (const RelocationRef &Reloc : Section.relocations())
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000457 Rels.push_back(Reloc);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000458
459 // Sort relocations by address.
460 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
461
462 ArrayRef<uint8_t> Contents;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000463 error(Obj->getSectionContents(Pdata, Contents));
Rui Ueyama39f1b972014-03-04 00:31:48 +0000464 if (Contents.empty())
465 continue;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000466
Rui Ueyama39f1b972014-03-04 00:31:48 +0000467 RFStart = reinterpret_cast<const RuntimeFunction *>(Contents.data());
468 NumRFs = Contents.size() / sizeof(RuntimeFunction);
469 return true;
470 }
471 return false;
472}
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000473
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000474static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) {
Rui Ueyama39f1b972014-03-04 00:31:48 +0000475 // The casts to int are required in order to output the value as number.
476 // Without the casts the value would be interpreted as char data (which
477 // results in garbage output).
Rui Ueyama595932f2014-03-04 19:23:56 +0000478 outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n";
479 outs() << " Flags: " << static_cast<int>(UI->getFlags());
Rui Ueyama39f1b972014-03-04 00:31:48 +0000480 if (UI->getFlags()) {
481 if (UI->getFlags() & UNW_ExceptionHandler)
482 outs() << " UNW_ExceptionHandler";
483 if (UI->getFlags() & UNW_TerminateHandler)
484 outs() << " UNW_TerminateHandler";
485 if (UI->getFlags() & UNW_ChainInfo)
486 outs() << " UNW_ChainInfo";
487 }
488 outs() << "\n";
Rui Ueyama595932f2014-03-04 19:23:56 +0000489 outs() << " Size of prolog: " << static_cast<int>(UI->PrologSize) << "\n";
490 outs() << " Number of Codes: " << static_cast<int>(UI->NumCodes) << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000491 // Maybe this should move to output of UOP_SetFPReg?
492 if (UI->getFrameRegister()) {
Rui Ueyama595932f2014-03-04 19:23:56 +0000493 outs() << " Frame register: "
Rui Ueyama39f1b972014-03-04 00:31:48 +0000494 << getUnwindRegisterName(UI->getFrameRegister()) << "\n";
Rui Ueyama595932f2014-03-04 19:23:56 +0000495 outs() << " Frame offset: " << 16 * UI->getFrameOffset() << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000496 } else {
Rui Ueyama595932f2014-03-04 19:23:56 +0000497 outs() << " No frame pointer used\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000498 }
499 if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
500 // FIXME: Output exception handler data
501 } else if (UI->getFlags() & UNW_ChainInfo) {
502 // FIXME: Output chained unwind info
503 }
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000504
Rui Ueyama39f1b972014-03-04 00:31:48 +0000505 if (UI->NumCodes)
Rui Ueyama595932f2014-03-04 19:23:56 +0000506 outs() << " Unwind Codes:\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000507
Craig Topper0013be12015-09-21 05:32:41 +0000508 printAllUnwindCodes(makeArrayRef(&UI->UnwindCodes[0], UI->NumCodes));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000509
Rui Ueyama595932f2014-03-04 19:23:56 +0000510 outs() << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000511 outs().flush();
512}
513
Rui Ueyamafd3cb9e2014-03-04 04:22:41 +0000514/// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
Rui Ueyama9c674e62014-03-04 04:00:55 +0000515/// pointing to an executable file.
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000516static void printRuntimeFunction(const COFFObjectFile *Obj,
Rui Ueyama9c674e62014-03-04 04:00:55 +0000517 const RuntimeFunction &RF) {
518 if (!RF.StartAddress)
519 return;
520 outs() << "Function Table:\n"
Rui Ueyamaad807652014-03-05 23:03:37 +0000521 << format(" Start Address: 0x%04x\n",
522 static_cast<uint32_t>(RF.StartAddress))
523 << format(" End Address: 0x%04x\n",
524 static_cast<uint32_t>(RF.EndAddress))
525 << format(" Unwind Info Address: 0x%04x\n",
526 static_cast<uint32_t>(RF.UnwindInfoOffset));
Rui Ueyama9c674e62014-03-04 04:00:55 +0000527 uintptr_t addr;
528 if (Obj->getRvaPtr(RF.UnwindInfoOffset, addr))
529 return;
530 printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo *>(addr));
531}
532
Rui Ueyamafd3cb9e2014-03-04 04:22:41 +0000533/// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
534/// pointing to an object file. Unlike executable, fields in RuntimeFunction
Rui Ueyama9c674e62014-03-04 04:00:55 +0000535/// struct are filled with zeros, but instead there are relocations pointing to
536/// them so that the linker will fill targets' RVAs to the fields at link
537/// time. This function interprets the relocations to find the data to be used
538/// in the resulting executable.
539static void printRuntimeFunctionRels(const COFFObjectFile *Obj,
540 const RuntimeFunction &RF,
541 uint64_t SectionOffset,
542 const std::vector<RelocationRef> &Rels) {
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000543 outs() << "Function Table:\n";
544 outs() << " Start Address: ";
545 printCOFFSymbolAddress(outs(), Rels,
546 SectionOffset +
547 /*offsetof(RuntimeFunction, StartAddress)*/ 0,
548 RF.StartAddress);
549 outs() << "\n";
550
551 outs() << " End Address: ";
552 printCOFFSymbolAddress(outs(), Rels,
553 SectionOffset +
554 /*offsetof(RuntimeFunction, EndAddress)*/ 4,
555 RF.EndAddress);
556 outs() << "\n";
557
558 outs() << " Unwind Info Address: ";
559 printCOFFSymbolAddress(outs(), Rels,
560 SectionOffset +
561 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
562 RF.UnwindInfoOffset);
563 outs() << "\n";
564
565 ArrayRef<uint8_t> XContents;
566 uint64_t UnwindInfoOffset = 0;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000567 error(getSectionContents(
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000568 Obj, Rels, SectionOffset +
569 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
Davide Italianoccd53fe2015-08-05 07:18:31 +0000570 XContents, UnwindInfoOffset));
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000571 if (XContents.empty())
572 return;
573
574 UnwindInfoOffset += RF.UnwindInfoOffset;
575 if (UnwindInfoOffset > XContents.size())
576 return;
577
Rui Ueyama8c1d4c92014-03-04 04:15:59 +0000578 auto *UI = reinterpret_cast<const Win64EH::UnwindInfo *>(XContents.data() +
579 UnwindInfoOffset);
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000580 printWin64EHUnwindInfo(UI);
581}
582
Rui Ueyama39f1b972014-03-04 00:31:48 +0000583void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
David Majnemer44f51e52014-09-10 12:51:52 +0000584 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64) {
Rui Ueyama39f1b972014-03-04 00:31:48 +0000585 errs() << "Unsupported image machine type "
586 "(currently only AMD64 is supported).\n";
587 return;
588 }
589
590 std::vector<RelocationRef> Rels;
591 const RuntimeFunction *RFStart;
592 int NumRFs;
593 if (!getPDataSection(Obj, Rels, RFStart, NumRFs))
594 return;
595 ArrayRef<RuntimeFunction> RFs(RFStart, NumRFs);
596
Rui Ueyama9c674e62014-03-04 04:00:55 +0000597 bool IsExecutable = Rels.empty();
598 if (IsExecutable) {
599 for (const RuntimeFunction &RF : RFs)
600 printRuntimeFunction(Obj, RF);
601 return;
602 }
603
Rui Ueyama39f1b972014-03-04 00:31:48 +0000604 for (const RuntimeFunction &RF : RFs) {
605 uint64_t SectionOffset =
606 std::distance(RFs.begin(), &RF) * sizeof(RuntimeFunction);
Rui Ueyama9c674e62014-03-04 04:00:55 +0000607 printRuntimeFunctionRels(Obj, RF, SectionOffset, Rels);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000608 }
609}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000610
611void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000612 const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj);
David Majnemer0ab61bf2016-03-15 06:14:01 +0000613 printTLSDirectory(file);
Rui Ueyamac514a802014-02-19 03:53:11 +0000614 printLoadConfiguration(file);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000615 printImportTables(file);
616 printExportTable(file);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000617}
Davide Italianoe85abf72015-12-20 09:54:34 +0000618
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +0000619void llvm::printCOFFSymbolTable(const object::COFFImportFile *i) {
620 unsigned Index = 0;
621 bool IsCode = i->getCOFFImportHeader()->getType() == COFF::IMPORT_CODE;
622
623 for (const object::BasicSymbolRef &Sym : i->symbols()) {
624 std::string Name;
625 raw_string_ostream NS(Name);
626
627 Sym.printName(NS);
628 NS.flush();
629
630 outs() << "[" << format("%2d", Index) << "]"
631 << "(sec " << format("%2d", 0) << ")"
632 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
633 << "(ty " << format("%3x", (IsCode && Index) ? 32 : 0) << ")"
634 << "(scl " << format("%3x", 0) << ") "
635 << "(nx " << 0 << ") "
636 << "0x" << format("%08x", 0) << " " << Name << '\n';
637
638 ++Index;
639 }
640}
641
Davide Italianoe85abf72015-12-20 09:54:34 +0000642void llvm::printCOFFSymbolTable(const COFFObjectFile *coff) {
643 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
644 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
645 StringRef Name;
646 error(Symbol.getError());
647 error(coff->getSymbolName(*Symbol, Name));
648
649 outs() << "[" << format("%2d", SI) << "]"
650 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
651 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
652 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
653 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
654 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
655 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
656 << Name << "\n";
657
658 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
659 if (Symbol->isSectionDefinition()) {
660 const coff_aux_section_definition *asd;
661 error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd));
662
663 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
664
665 outs() << "AUX "
666 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
667 , unsigned(asd->Length)
668 , unsigned(asd->NumberOfRelocations)
669 , unsigned(asd->NumberOfLinenumbers)
670 , unsigned(asd->CheckSum))
671 << format("assoc %d comdat %d\n"
672 , unsigned(AuxNumber)
673 , unsigned(asd->Selection));
674 } else if (Symbol->isFileRecord()) {
675 const char *FileName;
676 error(coff->getAuxSymbol<char>(SI + 1, FileName));
677
678 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
679 coff->getSymbolTableEntrySize());
680 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
681
682 SI = SI + Symbol->getNumberOfAuxSymbols();
683 break;
Saleem Abdulrasoolfbf920f2016-05-26 01:45:12 +0000684 } else if (Symbol->isWeakExternal()) {
685 const coff_aux_weak_external *awe;
686 error(coff->getAuxSymbol<coff_aux_weak_external>(SI + 1, awe));
687
688 outs() << "AUX " << format("indx %d srch %d\n",
689 static_cast<uint32_t>(awe->TagIndex),
690 static_cast<uint32_t>(awe->Characteristics));
Davide Italianoe85abf72015-12-20 09:54:34 +0000691 } else {
692 outs() << "AUX Unknown\n";
693 }
694 }
695 }
696}