blob: 3ddce21836c0445c60e00980df97a823eb138bda [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"
20#include "llvm/Object/ObjectFile.h"
21#include "llvm/Support/Format.h"
22#include "llvm/Support/SourceMgr.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#include <algorithm>
26#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000027#include <system_error>
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000028
29using namespace llvm;
30using namespace object;
31using namespace llvm::Win64EH;
32
33// Returns the name of the unwind code.
34static StringRef getUnwindCodeTypeName(uint8_t Code) {
35 switch(Code) {
36 default: llvm_unreachable("Invalid unwind code");
37 case UOP_PushNonVol: return "UOP_PushNonVol";
38 case UOP_AllocLarge: return "UOP_AllocLarge";
39 case UOP_AllocSmall: return "UOP_AllocSmall";
40 case UOP_SetFPReg: return "UOP_SetFPReg";
41 case UOP_SaveNonVol: return "UOP_SaveNonVol";
42 case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig";
43 case UOP_SaveXMM128: return "UOP_SaveXMM128";
44 case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big";
45 case UOP_PushMachFrame: return "UOP_PushMachFrame";
46 }
47}
48
49// Returns the name of a referenced register.
50static StringRef getUnwindRegisterName(uint8_t Reg) {
51 switch(Reg) {
52 default: llvm_unreachable("Invalid register");
53 case 0: return "RAX";
54 case 1: return "RCX";
55 case 2: return "RDX";
56 case 3: return "RBX";
57 case 4: return "RSP";
58 case 5: return "RBP";
59 case 6: return "RSI";
60 case 7: return "RDI";
61 case 8: return "R8";
62 case 9: return "R9";
63 case 10: return "R10";
64 case 11: return "R11";
65 case 12: return "R12";
66 case 13: return "R13";
67 case 14: return "R14";
68 case 15: return "R15";
69 }
70}
71
72// Calculates the number of array slots required for the unwind code.
73static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
74 switch (UnwindCode.getUnwindOp()) {
75 default: llvm_unreachable("Invalid unwind code");
76 case UOP_PushNonVol:
77 case UOP_AllocSmall:
78 case UOP_SetFPReg:
79 case UOP_PushMachFrame:
80 return 1;
81 case UOP_SaveNonVol:
82 case UOP_SaveXMM128:
83 return 2;
84 case UOP_SaveNonVolBig:
85 case UOP_SaveXMM128Big:
86 return 3;
87 case UOP_AllocLarge:
88 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
89 }
90}
91
92// Prints one unwind code. Because an unwind code can occupy up to 3 slots in
93// the unwind codes array, this function requires that the correct number of
94// slots is provided.
95static void printUnwindCode(ArrayRef<UnwindCode> UCs) {
96 assert(UCs.size() >= getNumUsedSlots(UCs[0]));
Rui Ueyama595932f2014-03-04 19:23:56 +000097 outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset))
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000098 << getUnwindCodeTypeName(UCs[0].getUnwindOp());
99 switch (UCs[0].getUnwindOp()) {
100 case UOP_PushNonVol:
101 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo());
102 break;
103 case UOP_AllocLarge:
104 if (UCs[0].getOpInfo() == 0) {
105 outs() << " " << UCs[1].FrameOffset;
106 } else {
107 outs() << " " << UCs[1].FrameOffset
108 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
109 }
110 break;
111 case UOP_AllocSmall:
112 outs() << " " << ((UCs[0].getOpInfo() + 1) * 8);
113 break;
114 case UOP_SetFPReg:
115 outs() << " ";
116 break;
117 case UOP_SaveNonVol:
118 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
119 << format(" [0x%04x]", 8 * UCs[1].FrameOffset);
120 break;
121 case UOP_SaveNonVolBig:
122 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
123 << format(" [0x%08x]", UCs[1].FrameOffset
124 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
125 break;
126 case UOP_SaveXMM128:
127 outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
128 << format(" [0x%04x]", 16 * UCs[1].FrameOffset);
129 break;
130 case UOP_SaveXMM128Big:
131 outs() << " XMM" << UCs[0].getOpInfo()
132 << format(" [0x%08x]", UCs[1].FrameOffset
133 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16));
134 break;
135 case UOP_PushMachFrame:
136 outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w")
137 << " error code";
138 break;
139 }
140 outs() << "\n";
141}
142
143static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) {
144 for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) {
145 unsigned UsedSlots = getNumUsedSlots(*I);
146 if (UsedSlots > UCs.size()) {
147 outs() << "Unwind data corrupted: Encountered unwind op "
148 << getUnwindCodeTypeName((*I).getUnwindOp())
149 << " which requires " << UsedSlots
150 << " slots, but only " << UCs.size()
151 << " remaining in buffer";
152 return ;
153 }
154 printUnwindCode(ArrayRef<UnwindCode>(I, E));
155 I += UsedSlots;
156 }
157}
158
159// Given a symbol sym this functions returns the address and section of it.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000160static std::error_code
161resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
162 const coff_section *&ResolvedSection,
163 uint64_t &ResolvedAddr) {
164 if (std::error_code EC = Sym.getAddress(ResolvedAddr))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000165 return EC;
Rafael Espindolab5155a52014-02-10 20:24:04 +0000166 section_iterator iter(Obj->section_begin());
Rafael Espindola4453e42942014-06-13 03:07:50 +0000167 if (std::error_code EC = Sym.getSection(iter))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000168 return EC;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000169 ResolvedSection = Obj->getCOFFSection(*iter);
Rui Ueyama7d099192015-06-09 15:20:42 +0000170 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000171}
172
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.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000175static std::error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
176 uint64_t Offset, SymbolRef &Sym) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000177 for (std::vector<RelocationRef>::const_iterator I = Rels.begin(),
178 E = Rels.end();
179 I != E; ++I) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000180 uint64_t Ofs = I->getOffset();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000181 if (Ofs == Offset) {
Rafael Espindola806f0062013-06-05 01:33:53 +0000182 Sym = *I->getSymbol();
Rui Ueyama7d099192015-06-09 15:20:42 +0000183 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000184 }
185 }
Rui Ueyamacf397842014-02-28 05:21:29 +0000186 return object_error::parse_failed;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000187}
188
189// Given a vector of relocations for a section and an offset into this section
190// the function resolves the symbol used for the relocation at the offset and
191// returns the section content and the address inside the content pointed to
192// by the symbol.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000193static std::error_code
194getSectionContents(const COFFObjectFile *Obj,
195 const std::vector<RelocationRef> &Rels, uint64_t Offset,
196 ArrayRef<uint8_t> &Contents, uint64_t &Addr) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000197 SymbolRef Sym;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000198 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
Rui Ueyama1618fe22014-02-28 05:21:26 +0000199 return EC;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000200 const coff_section *Section;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000201 if (std::error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000202 return EC;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000203 if (std::error_code EC = Obj->getSectionContents(Section, Contents))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000204 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000205 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000206}
207
208// Given a vector of relocations for a section and an offset into this section
209// the function returns the name of the symbol used for the relocation at the
210// offset.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000211static std::error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
212 uint64_t Offset, StringRef &Name) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000213 SymbolRef Sym;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000214 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000215 return EC;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000216 if (std::error_code EC = Sym.getName(Name))
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000217 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000218 return std::error_code();
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000219}
220
221static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
222 const std::vector<RelocationRef> &Rels,
223 uint64_t Offset, uint32_t Disp) {
224 StringRef Sym;
Rui Ueyamacf397842014-02-28 05:21:29 +0000225 if (!resolveSymbolName(Rels, Offset, Sym)) {
226 Out << Sym;
227 if (Disp > 0)
228 Out << format(" + 0x%04x", Disp);
229 } else {
230 Out << format("0x%04x", Disp);
231 }
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000232}
233
Rui Ueyama215a5862014-02-20 06:51:07 +0000234static void
235printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) {
236 if (Count == 0)
237 return;
238
239 const pe32_header *PE32Header;
240 if (error(Obj->getPE32Header(PE32Header)))
241 return;
242 uint32_t ImageBase = PE32Header->ImageBase;
243 uintptr_t IntPtr = 0;
244 if (error(Obj->getVaPtr(TableVA, IntPtr)))
245 return;
246 const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr;
247 outs() << "SEH Table:";
248 for (int I = 0; I < Count; ++I)
249 outs() << format(" 0x%x", P[I] + ImageBase);
250 outs() << "\n\n";
251}
252
Rui Ueyamac514a802014-02-19 03:53:11 +0000253static void printLoadConfiguration(const COFFObjectFile *Obj) {
Rui Ueyama5cf32852014-02-21 20:27:15 +0000254 // Skip if it's not executable.
255 const pe32_header *PE32Header;
256 if (error(Obj->getPE32Header(PE32Header)))
257 return;
258 if (!PE32Header)
259 return;
260
Rui Ueyamac514a802014-02-19 03:53:11 +0000261 // Currently only x86 is supported
David Majnemer44f51e52014-09-10 12:51:52 +0000262 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_I386)
Rui Ueyamac514a802014-02-19 03:53:11 +0000263 return;
264
265 const data_directory *DataDir;
266 if (error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir)))
267 return;
268 uintptr_t IntPtr = 0;
269 if (DataDir->RelativeVirtualAddress == 0)
270 return;
271 if (error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)))
272 return;
Rui Ueyama215a5862014-02-20 06:51:07 +0000273
Rui Ueyama8c1d4c92014-03-04 04:15:59 +0000274 auto *LoadConf = reinterpret_cast<const coff_load_configuration32 *>(IntPtr);
Rui Ueyamac514a802014-02-19 03:53:11 +0000275 outs() << "Load configuration:"
276 << "\n Timestamp: " << LoadConf->TimeDateStamp
277 << "\n Major Version: " << LoadConf->MajorVersion
278 << "\n Minor Version: " << LoadConf->MinorVersion
279 << "\n GlobalFlags Clear: " << LoadConf->GlobalFlagsClear
280 << "\n GlobalFlags Set: " << LoadConf->GlobalFlagsSet
281 << "\n Critical Section Default Timeout: " << LoadConf->CriticalSectionDefaultTimeout
282 << "\n Decommit Free Block Threshold: " << LoadConf->DeCommitFreeBlockThreshold
283 << "\n Decommit Total Free Threshold: " << LoadConf->DeCommitTotalFreeThreshold
284 << "\n Lock Prefix Table: " << LoadConf->LockPrefixTable
285 << "\n Maximum Allocation Size: " << LoadConf->MaximumAllocationSize
286 << "\n Virtual Memory Threshold: " << LoadConf->VirtualMemoryThreshold
287 << "\n Process Affinity Mask: " << LoadConf->ProcessAffinityMask
288 << "\n Process Heap Flags: " << LoadConf->ProcessHeapFlags
289 << "\n CSD Version: " << LoadConf->CSDVersion
290 << "\n Security Cookie: " << LoadConf->SecurityCookie
291 << "\n SEH Table: " << LoadConf->SEHandlerTable
292 << "\n SEH Count: " << LoadConf->SEHandlerCount
293 << "\n\n";
Rui Ueyama215a5862014-02-20 06:51:07 +0000294 printSEHTable(Obj, LoadConf->SEHandlerTable, LoadConf->SEHandlerCount);
295 outs() << "\n";
Rui Ueyamac514a802014-02-19 03:53:11 +0000296}
297
Rui Ueyamac2bed422013-09-27 21:04:00 +0000298// Prints import tables. The import table is a table containing the list of
299// DLL name and symbol names which will be linked by the loader.
300static void printImportTables(const COFFObjectFile *Obj) {
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000301 import_directory_iterator I = Obj->import_directory_begin();
302 import_directory_iterator E = Obj->import_directory_end();
303 if (I == E)
Rui Ueyama908dfcd2014-01-15 23:46:18 +0000304 return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000305 outs() << "The Import Tables:\n";
Rafael Espindola5e812af2014-01-30 02:49:50 +0000306 for (; I != E; I = ++I) {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000307 const import_directory_table_entry *Dir;
308 StringRef Name;
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000309 if (I->getImportTableEntry(Dir)) return;
310 if (I->getName(Name)) return;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000311
312 outs() << format(" lookup %08x time %08x fwd %08x name %08x addr %08x\n\n",
313 static_cast<uint32_t>(Dir->ImportLookupTableRVA),
314 static_cast<uint32_t>(Dir->TimeDateStamp),
315 static_cast<uint32_t>(Dir->ForwarderChain),
316 static_cast<uint32_t>(Dir->NameRVA),
317 static_cast<uint32_t>(Dir->ImportAddressTableRVA));
318 outs() << " DLL Name: " << Name << "\n";
319 outs() << " Hint/Ord Name\n";
320 const import_lookup_table_entry32 *entry;
Rui Ueyamaef8dede2014-01-16 20:57:55 +0000321 if (I->getImportLookupEntry(entry))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000322 return;
Rui Ueyama350fe8e2014-10-02 22:13:44 +0000323 for (; entry->Data; ++entry) {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000324 if (entry->isOrdinal()) {
325 outs() << format(" % 6d\n", entry->getOrdinal());
326 continue;
327 }
328 uint16_t Hint;
329 StringRef Name;
330 if (Obj->getHintName(entry->getHintNameRVA(), Hint, Name))
331 return;
332 outs() << format(" % 6d ", Hint) << Name << "\n";
333 }
334 outs() << "\n";
335 }
336}
337
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000338// Prints export tables. The export table is a table containing the list of
339// exported symbol from the DLL.
340static void printExportTable(const COFFObjectFile *Obj) {
341 outs() << "Export Table:\n";
342 export_directory_iterator I = Obj->export_directory_begin();
343 export_directory_iterator E = Obj->export_directory_end();
344 if (I == E)
345 return;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000346 StringRef DllName;
Rui Ueyamae5df6092014-01-17 22:02:24 +0000347 uint32_t OrdinalBase;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000348 if (I->getDllName(DllName))
349 return;
Rui Ueyamae5df6092014-01-17 22:02:24 +0000350 if (I->getOrdinalBase(OrdinalBase))
351 return;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000352 outs() << " DLL name: " << DllName << "\n";
Rui Ueyamae5df6092014-01-17 22:02:24 +0000353 outs() << " Ordinal base: " << OrdinalBase << "\n";
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000354 outs() << " Ordinal RVA Name\n";
Rafael Espindola5e812af2014-01-30 02:49:50 +0000355 for (; I != E; I = ++I) {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000356 uint32_t Ordinal;
357 if (I->getOrdinal(Ordinal))
358 return;
359 uint32_t RVA;
360 if (I->getExportRVA(RVA))
361 return;
362 outs() << format(" % 4d %# 8x", Ordinal, RVA);
363
364 StringRef Name;
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000365 if (I->getSymbolName(Name))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000366 continue;
367 if (!Name.empty())
368 outs() << " " << Name;
369 outs() << "\n";
370 }
371}
372
Rui Ueyama39f1b972014-03-04 00:31:48 +0000373// Given the COFF object file, this function returns the relocations for .pdata
374// and the pointer to "runtime function" structs.
375static bool getPDataSection(const COFFObjectFile *Obj,
376 std::vector<RelocationRef> &Rels,
377 const RuntimeFunction *&RFStart, int &NumRFs) {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000378 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000379 StringRef Name;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000380 if (error(Section.getName(Name)))
Rui Ueyama39f1b972014-03-04 00:31:48 +0000381 continue;
382 if (Name != ".pdata")
383 continue;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000384
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000385 const coff_section *Pdata = Obj->getCOFFSection(Section);
386 for (const RelocationRef &Reloc : Section.relocations())
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000387 Rels.push_back(Reloc);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000388
389 // Sort relocations by address.
390 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
391
392 ArrayRef<uint8_t> Contents;
Rui Ueyama39f1b972014-03-04 00:31:48 +0000393 if (error(Obj->getSectionContents(Pdata, Contents)))
394 continue;
395 if (Contents.empty())
396 continue;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000397
Rui Ueyama39f1b972014-03-04 00:31:48 +0000398 RFStart = reinterpret_cast<const RuntimeFunction *>(Contents.data());
399 NumRFs = Contents.size() / sizeof(RuntimeFunction);
400 return true;
401 }
402 return false;
403}
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000404
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000405static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) {
Rui Ueyama39f1b972014-03-04 00:31:48 +0000406 // The casts to int are required in order to output the value as number.
407 // Without the casts the value would be interpreted as char data (which
408 // results in garbage output).
Rui Ueyama595932f2014-03-04 19:23:56 +0000409 outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n";
410 outs() << " Flags: " << static_cast<int>(UI->getFlags());
Rui Ueyama39f1b972014-03-04 00:31:48 +0000411 if (UI->getFlags()) {
412 if (UI->getFlags() & UNW_ExceptionHandler)
413 outs() << " UNW_ExceptionHandler";
414 if (UI->getFlags() & UNW_TerminateHandler)
415 outs() << " UNW_TerminateHandler";
416 if (UI->getFlags() & UNW_ChainInfo)
417 outs() << " UNW_ChainInfo";
418 }
419 outs() << "\n";
Rui Ueyama595932f2014-03-04 19:23:56 +0000420 outs() << " Size of prolog: " << static_cast<int>(UI->PrologSize) << "\n";
421 outs() << " Number of Codes: " << static_cast<int>(UI->NumCodes) << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000422 // Maybe this should move to output of UOP_SetFPReg?
423 if (UI->getFrameRegister()) {
Rui Ueyama595932f2014-03-04 19:23:56 +0000424 outs() << " Frame register: "
Rui Ueyama39f1b972014-03-04 00:31:48 +0000425 << getUnwindRegisterName(UI->getFrameRegister()) << "\n";
Rui Ueyama595932f2014-03-04 19:23:56 +0000426 outs() << " Frame offset: " << 16 * UI->getFrameOffset() << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000427 } else {
Rui Ueyama595932f2014-03-04 19:23:56 +0000428 outs() << " No frame pointer used\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000429 }
430 if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
431 // FIXME: Output exception handler data
432 } else if (UI->getFlags() & UNW_ChainInfo) {
433 // FIXME: Output chained unwind info
434 }
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000435
Rui Ueyama39f1b972014-03-04 00:31:48 +0000436 if (UI->NumCodes)
Rui Ueyama595932f2014-03-04 19:23:56 +0000437 outs() << " Unwind Codes:\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000438
Rui Ueyama39f1b972014-03-04 00:31:48 +0000439 printAllUnwindCodes(ArrayRef<UnwindCode>(&UI->UnwindCodes[0], UI->NumCodes));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000440
Rui Ueyama595932f2014-03-04 19:23:56 +0000441 outs() << "\n";
Rui Ueyama39f1b972014-03-04 00:31:48 +0000442 outs().flush();
443}
444
Rui Ueyamafd3cb9e2014-03-04 04:22:41 +0000445/// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
Rui Ueyama9c674e62014-03-04 04:00:55 +0000446/// pointing to an executable file.
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000447static void printRuntimeFunction(const COFFObjectFile *Obj,
Rui Ueyama9c674e62014-03-04 04:00:55 +0000448 const RuntimeFunction &RF) {
449 if (!RF.StartAddress)
450 return;
451 outs() << "Function Table:\n"
Rui Ueyamaad807652014-03-05 23:03:37 +0000452 << format(" Start Address: 0x%04x\n",
453 static_cast<uint32_t>(RF.StartAddress))
454 << format(" End Address: 0x%04x\n",
455 static_cast<uint32_t>(RF.EndAddress))
456 << format(" Unwind Info Address: 0x%04x\n",
457 static_cast<uint32_t>(RF.UnwindInfoOffset));
Rui Ueyama9c674e62014-03-04 04:00:55 +0000458 uintptr_t addr;
459 if (Obj->getRvaPtr(RF.UnwindInfoOffset, addr))
460 return;
461 printWin64EHUnwindInfo(reinterpret_cast<const Win64EH::UnwindInfo *>(addr));
462}
463
Rui Ueyamafd3cb9e2014-03-04 04:22:41 +0000464/// Prints out the given RuntimeFunction struct for x64, assuming that Obj is
465/// pointing to an object file. Unlike executable, fields in RuntimeFunction
Rui Ueyama9c674e62014-03-04 04:00:55 +0000466/// struct are filled with zeros, but instead there are relocations pointing to
467/// them so that the linker will fill targets' RVAs to the fields at link
468/// time. This function interprets the relocations to find the data to be used
469/// in the resulting executable.
470static void printRuntimeFunctionRels(const COFFObjectFile *Obj,
471 const RuntimeFunction &RF,
472 uint64_t SectionOffset,
473 const std::vector<RelocationRef> &Rels) {
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000474 outs() << "Function Table:\n";
475 outs() << " Start Address: ";
476 printCOFFSymbolAddress(outs(), Rels,
477 SectionOffset +
478 /*offsetof(RuntimeFunction, StartAddress)*/ 0,
479 RF.StartAddress);
480 outs() << "\n";
481
482 outs() << " End Address: ";
483 printCOFFSymbolAddress(outs(), Rels,
484 SectionOffset +
485 /*offsetof(RuntimeFunction, EndAddress)*/ 4,
486 RF.EndAddress);
487 outs() << "\n";
488
489 outs() << " Unwind Info Address: ";
490 printCOFFSymbolAddress(outs(), Rels,
491 SectionOffset +
492 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
493 RF.UnwindInfoOffset);
494 outs() << "\n";
495
496 ArrayRef<uint8_t> XContents;
497 uint64_t UnwindInfoOffset = 0;
498 if (error(getSectionContents(
499 Obj, Rels, SectionOffset +
500 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
501 XContents, UnwindInfoOffset)))
502 return;
503 if (XContents.empty())
504 return;
505
506 UnwindInfoOffset += RF.UnwindInfoOffset;
507 if (UnwindInfoOffset > XContents.size())
508 return;
509
Rui Ueyama8c1d4c92014-03-04 04:15:59 +0000510 auto *UI = reinterpret_cast<const Win64EH::UnwindInfo *>(XContents.data() +
511 UnwindInfoOffset);
Rui Ueyama6dfd4e12014-03-04 03:08:45 +0000512 printWin64EHUnwindInfo(UI);
513}
514
Rui Ueyama39f1b972014-03-04 00:31:48 +0000515void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
David Majnemer44f51e52014-09-10 12:51:52 +0000516 if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64) {
Rui Ueyama39f1b972014-03-04 00:31:48 +0000517 errs() << "Unsupported image machine type "
518 "(currently only AMD64 is supported).\n";
519 return;
520 }
521
522 std::vector<RelocationRef> Rels;
523 const RuntimeFunction *RFStart;
524 int NumRFs;
525 if (!getPDataSection(Obj, Rels, RFStart, NumRFs))
526 return;
527 ArrayRef<RuntimeFunction> RFs(RFStart, NumRFs);
528
Rui Ueyama9c674e62014-03-04 04:00:55 +0000529 bool IsExecutable = Rels.empty();
530 if (IsExecutable) {
531 for (const RuntimeFunction &RF : RFs)
532 printRuntimeFunction(Obj, RF);
533 return;
534 }
535
Rui Ueyama39f1b972014-03-04 00:31:48 +0000536 for (const RuntimeFunction &RF : RFs) {
537 uint64_t SectionOffset =
538 std::distance(RFs.begin(), &RF) * sizeof(RuntimeFunction);
Rui Ueyama9c674e62014-03-04 04:00:55 +0000539 printRuntimeFunctionRels(Obj, RF, SectionOffset, Rels);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000540 }
541}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000542
543void llvm::printCOFFFileHeader(const object::ObjectFile *Obj) {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000544 const COFFObjectFile *file = dyn_cast<const COFFObjectFile>(Obj);
Rui Ueyamac514a802014-02-19 03:53:11 +0000545 printLoadConfiguration(file);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000546 printImportTables(file);
547 printExportTable(file);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000548}