1 //===-- COFFDump.cpp - COFF-specific dumper ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief 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 decribed in MSDN:
14 /// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx
16 //===----------------------------------------------------------------------===//
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"
23 #include "llvm/Support/Win64EH.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/system_error.h"
30 using namespace object;
31 using namespace llvm::Win64EH;
33 // Returns the name of the unwind code.
34 static StringRef getUnwindCodeTypeName(uint8_t 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";
49 // Returns the name of a referenced register.
50 static StringRef getUnwindRegisterName(uint8_t Reg) {
52 default: llvm_unreachable("Invalid register");
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";
72 // Calculates the number of array slots required for the unwind code.
73 static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
74 switch (UnwindCode.getUnwindOp()) {
75 default: llvm_unreachable("Invalid unwind code");
79 case UOP_PushMachFrame:
84 case UOP_SaveNonVolBig:
85 case UOP_SaveXMM128Big:
88 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
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
95 static void printUnwindCode(ArrayRef<UnwindCode> UCs) {
96 assert(UCs.size() >= getNumUsedSlots(UCs[0]));
97 outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset))
98 << getUnwindCodeTypeName(UCs[0].getUnwindOp());
99 switch (UCs[0].getUnwindOp()) {
101 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo());
104 if (UCs[0].getOpInfo() == 0) {
105 outs() << " " << UCs[1].FrameOffset;
107 outs() << " " << UCs[1].FrameOffset
108 + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
112 outs() << " " << ((UCs[0].getOpInfo() + 1) * 8);
118 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
119 << format(" [0x%04x]", 8 * UCs[1].FrameOffset);
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));
127 outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
128 << format(" [0x%04x]", 16 * UCs[1].FrameOffset);
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));
135 case UOP_PushMachFrame:
136 outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w")
143 static 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";
154 printUnwindCode(ArrayRef<UnwindCode>(I, E));
159 // Given a symbol sym this functions returns the address and section of it.
160 static error_code resolveSectionAndAddress(const COFFObjectFile *Obj,
161 const SymbolRef &Sym,
162 const coff_section *&ResolvedSection,
163 uint64_t &ResolvedAddr) {
164 if (error_code ec = Sym.getAddress(ResolvedAddr)) return ec;
165 section_iterator iter(Obj->begin_sections());
166 if (error_code ec = Sym.getSection(iter)) return ec;
167 ResolvedSection = Obj->getCOFFSection(iter);
168 return object_error::success;
171 // Given a vector of relocations for a section and an offset into this section
172 // the function returns the symbol used for the relocation at the offset.
173 static error_code resolveSymbol(const std::vector<RelocationRef> &Rels,
174 uint64_t Offset, SymbolRef &Sym) {
175 for (std::vector<RelocationRef>::const_iterator I = Rels.begin(),
179 if (error_code ec = I->getOffset(Ofs)) return ec;
181 Sym = *I->getSymbol();
185 return object_error::success;
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
192 static error_code getSectionContents(const COFFObjectFile *Obj,
193 const std::vector<RelocationRef> &Rels,
195 ArrayRef<uint8_t> &Contents,
198 if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec;
199 const coff_section *Section;
200 if (error_code ec = resolveSectionAndAddress(Obj, Sym, Section, Addr))
202 if (error_code ec = Obj->getSectionContents(Section, Contents)) return ec;
203 return object_error::success;
206 // Given a vector of relocations for a section and an offset into this section
207 // the function returns the name of the symbol used for the relocation at the
209 static error_code resolveSymbolName(const std::vector<RelocationRef> &Rels,
210 uint64_t Offset, StringRef &Name) {
212 if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec;
213 if (error_code ec = Sym.getName(Name)) return ec;
214 return object_error::success;
217 static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
218 const std::vector<RelocationRef> &Rels,
219 uint64_t Offset, uint32_t Disp) {
221 if (error_code ec = resolveSymbolName(Rels, Offset, Sym)) {
227 Out << format(" + 0x%04x", Disp);
230 void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
231 const coff_file_header *Header;
232 if (error(Obj->getCOFFHeader(Header))) return;
234 if (Header->Machine != COFF::IMAGE_FILE_MACHINE_AMD64) {
235 errs() << "Unsupported image machine type "
236 "(currently only AMD64 is supported).\n";
240 const coff_section *Pdata = 0;
243 for (section_iterator SI = Obj->begin_sections(),
244 SE = Obj->end_sections();
245 SI != SE; SI.increment(ec)) {
246 if (error(ec)) return;
249 if (error(SI->getName(Name))) continue;
251 if (Name != ".pdata") continue;
253 Pdata = Obj->getCOFFSection(SI);
254 std::vector<RelocationRef> Rels;
255 for (relocation_iterator RI = SI->begin_relocations(),
256 RE = SI->end_relocations();
257 RI != RE; RI.increment(ec)) {
258 if (error(ec)) break;
262 // Sort relocations by address.
263 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
265 ArrayRef<uint8_t> Contents;
266 if (error(Obj->getSectionContents(Pdata, Contents))) continue;
267 if (Contents.empty()) continue;
269 ArrayRef<RuntimeFunction> RFs(
270 reinterpret_cast<const RuntimeFunction *>(Contents.data()),
271 Contents.size() / sizeof(RuntimeFunction));
272 for (const RuntimeFunction *I = RFs.begin(), *E = RFs.end(); I < E; ++I) {
273 const uint64_t SectionOffset = std::distance(RFs.begin(), I)
274 * sizeof(RuntimeFunction);
276 outs() << "Function Table:\n";
278 outs() << " Start Address: ";
279 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
280 /*offsetof(RuntimeFunction, StartAddress)*/ 0,
284 outs() << " End Address: ";
285 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
286 /*offsetof(RuntimeFunction, EndAddress)*/ 4,
290 outs() << " Unwind Info Address: ";
291 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
292 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
293 I->UnwindInfoOffset);
296 ArrayRef<uint8_t> XContents;
297 uint64_t UnwindInfoOffset = 0;
298 if (error(getSectionContents(Obj, Rels, SectionOffset +
299 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
300 XContents, UnwindInfoOffset))) continue;
301 if (XContents.empty()) continue;
303 UnwindInfoOffset += I->UnwindInfoOffset;
304 if (UnwindInfoOffset > XContents.size()) continue;
306 const Win64EH::UnwindInfo *UI =
307 reinterpret_cast<const Win64EH::UnwindInfo *>
308 (XContents.data() + UnwindInfoOffset);
310 // The casts to int are required in order to output the value as number.
311 // Without the casts the value would be interpreted as char data (which
312 // results in garbage output).
313 outs() << " Version: " << static_cast<int>(UI->getVersion()) << "\n";
314 outs() << " Flags: " << static_cast<int>(UI->getFlags());
315 if (UI->getFlags()) {
316 if (UI->getFlags() & UNW_ExceptionHandler)
317 outs() << " UNW_ExceptionHandler";
318 if (UI->getFlags() & UNW_TerminateHandler)
319 outs() << " UNW_TerminateHandler";
320 if (UI->getFlags() & UNW_ChainInfo)
321 outs() << " UNW_ChainInfo";
324 outs() << " Size of prolog: "
325 << static_cast<int>(UI->PrologSize) << "\n";
326 outs() << " Number of Codes: "
327 << static_cast<int>(UI->NumCodes) << "\n";
328 // Maybe this should move to output of UOP_SetFPReg?
329 if (UI->getFrameRegister()) {
330 outs() << " Frame register: "
331 << getUnwindRegisterName(UI->getFrameRegister())
333 outs() << " Frame offset: "
334 << 16 * UI->getFrameOffset()
337 outs() << " No frame pointer used\n";
339 if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
340 // FIXME: Output exception handler data
341 } else if (UI->getFlags() & UNW_ChainInfo) {
342 // FIXME: Output chained unwind info
346 outs() << " Unwind Codes:\n";
348 printAllUnwindCodes(ArrayRef<UnwindCode>(&UI->UnwindCodes[0],