llvm.org GIT mirror llvm / eef7b62
Add dump of Win64 EH unwind data. The new command line option -unwind-info dumps the Win64 EH unwind data to the console. This is a nice feature if you need to debug generated EH data (e.g. from LLVM). Includes a test case. Initial patch by João Matos, extensions and rework by Kai Nacke. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169415 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 7 years ago
5 changed file(s) with 497 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
0 // This test checks that the unwind data is dumped by llvm-objdump.
1 // RUN: llvm-mc -triple x86_64-pc-win32 -filetype=obj %s | llvm-objdump -u - | FileCheck %s
2
3 // CHECK: Unwind info:
4 // CHECK: Function Table:
5 // CHECK-NEXT: Start Address: .text
6 // CHECK-NEXT: End Address: .text + 0x001b
7 // CHECK-NEXT: Unwind Info Address: .xdata
8 // CHECK-NEXT: Version: 1
9 // CHECK-NEXT: Flags: 1 UNW_ExceptionHandler
10 // CHECK-NEXT: Size of prolog: 18
11 // CHECK-NEXT: Number of Codes: 8
12 // CHECK-NEXT: Frame register: RBX
13 // CHECK-NEXT: Frame offset: 0
14 // CHECK-NEXT: Unwind Codes:
15 // CHECK-NEXT: 0x00: UOP_SetFPReg
16 // CHECK-NEXT: 0x0f: UOP_PushNonVol RBX
17 // CHECK-NEXT: 0x0e: UOP_SaveXMM128 XMM8 [0x0000]
18 // CHECK-NEXT: 0x09: UOP_SaveNonVol RSI [0x0010]
19 // CHECK-NEXT: 0x04: UOP_AllocSmall 24
20 // CHECK-NEXT: 0x00: UOP_PushMachFrame w/o error code
21 // CHECK: Function Table:
22 // CHECK-NEXT: Start Address: .text + 0x0012
23 // CHECK-NEXT: End Address: .text + 0x0012
24 // CHECK-NEXT: Unwind Info Address: .xdata + 0x001c
25 // CHECK-NEXT: Version: 1
26 // CHECK-NEXT: Flags: 4 UNW_ChainInfo
27 // CHECK-NEXT: Size of prolog: 0
28 // CHECK-NEXT: Number of Codes: 0
29 // CHECK-NEXT: No frame pointer used
30 // CHECK: Function Table:
31 // CHECK-NEXT: Start Address: .text + 0x001b
32 // CHECK-NEXT: End Address: .text + 0x001c
33 // CHECK-NEXT: Unwind Info Address: .xdata + 0x002c
34 // CHECK-NEXT: Version: 1
35 // CHECK-NEXT: Flags: 0
36 // CHECK-NEXT: Size of prolog: 0
37 // CHECK-NEXT: Number of Codes: 0
38 // CHECK-NEXT: No frame pointer used
39 // CHECK: Function Table:
40 // CHECK-NEXT: Start Address: .text + 0x001c
41 // CHECK-NEXT: End Address: .text + 0x0039
42 // CHECK-NEXT: Unwind Info Address: .xdata + 0x0034
43 // CHECK-NEXT: Version: 1
44 // CHECK-NEXT: Flags: 0
45 // CHECK-NEXT: Size of prolog: 14
46 // CHECK-NEXT: Number of Codes: 6
47 // CHECK-NEXT: No frame pointer used
48 // CHECK-NEXT: Unwind Codes:
49 // CHECK-NEXT: 0x0e: UOP_AllocLarge 8454128
50 // CHECK-NEXT: 0x07: UOP_AllocLarge 8190
51 // CHECK-NEXT: 0x00: UOP_PushMachFrame w/o error code
52
53 .text
54 .globl func
55 .def func; .scl 2; .type 32; .endef
56 .seh_proc func
57 func:
58 .seh_pushframe @code
59 subq $24, %rsp
60 .seh_stackalloc 24
61 movq %rsi, 16(%rsp)
62 .seh_savereg %rsi, 16
63 movups %xmm8, (%rsp)
64 .seh_savexmm %xmm8, 0
65 pushq %rbx
66 .seh_pushreg 3
67 mov %rsp, %rbx
68 .seh_setframe 3, 0
69 .seh_endprologue
70 .seh_handler __C_specific_handler, @except
71 .seh_handlerdata
72 .long 0
73 .text
74 .seh_startchained
75 .seh_endprologue
76 .seh_endchained
77 lea (%rbx), %rsp
78 pop %rbx
79 addq $24, %rsp
80 ret
81 .seh_endproc
82
83 // Test emission of small functions.
84 .globl smallFunc
85 .def smallFunc; .scl 2; .type 32; .endef
86 .seh_proc smallFunc
87 smallFunc:
88 ret
89 .seh_endproc
90
91 // Function with big stack allocation.
92 .globl smallFunc
93 .def allocFunc; .scl 2; .type 32; .endef
94 .seh_proc smallFunc
95 allocFunc:
96 .seh_pushframe @code
97 subq $65520, %rsp
98 .seh_stackalloc 65520
99 sub $8454128, %rsp
100 .seh_stackalloc 8454128
101 .seh_endprologue
102 add $8454128, %rsp
103 addq $65520, %rsp
104 ret
105 .seh_endproc
88
99 add_llvm_tool(llvm-objdump
1010 llvm-objdump.cpp
11 COFFDump.cpp
1112 MachODump.cpp
1213 MCFunction.cpp
1314 )
0 //===-- COFFDump.cpp - COFF-specific dumper ---------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// \brief This file implements the COFF-specific dumper for llvm-objdump.
11 /// It outputs the Win64 EH data structures as plain text.
12 /// The encoding of the unwind codes is decribed in MSDN:
13 /// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm-objdump.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/system_error.h"
24 #include "llvm/Support/Win64EH.h"
25 #include
26 #include
27
28 using namespace llvm;
29 using namespace object;
30 using namespace llvm::Win64EH;
31
32 // Returns the name of the unwind code.
33 static StringRef getUnwindCodeTypeName(uint8_t Code) {
34 switch(Code) {
35 default: llvm_unreachable("Invalid unwind code");
36 case UOP_PushNonVol: return "UOP_PushNonVol";
37 case UOP_AllocLarge: return "UOP_AllocLarge";
38 case UOP_AllocSmall: return "UOP_AllocSmall";
39 case UOP_SetFPReg: return "UOP_SetFPReg";
40 case UOP_SaveNonVol: return "UOP_SaveNonVol";
41 case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig";
42 case UOP_SaveXMM128: return "UOP_SaveXMM128";
43 case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big";
44 case UOP_PushMachFrame: return "UOP_PushMachFrame";
45 }
46 }
47
48 // Returns the name of a referenced register.
49 static StringRef getUnwindRegisterName(uint8_t Reg) {
50 switch(Reg) {
51 default: llvm_unreachable("Invalid register");
52 case 0: return "RAX";
53 case 1: return "RCX";
54 case 2: return "RDX";
55 case 3: return "RBX";
56 case 4: return "RSP";
57 case 5: return "RBP";
58 case 6: return "RSI";
59 case 7: return "RDI";
60 case 8: return "R8";
61 case 9: return "R9";
62 case 10: return "R10";
63 case 11: return "R11";
64 case 12: return "R12";
65 case 13: return "R13";
66 case 14: return "R14";
67 case 15: return "R15";
68 }
69 }
70
71 // Calculates the number of array slots required for the unwind code.
72 static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
73 switch (UnwindCode.getUnwindOp()) {
74 default: llvm_unreachable("Invalid unwind code");
75 case UOP_PushNonVol:
76 case UOP_AllocSmall:
77 case UOP_SetFPReg:
78 case UOP_PushMachFrame:
79 return 1;
80 case UOP_SaveNonVol:
81 case UOP_SaveXMM128:
82 return 2;
83 case UOP_SaveNonVolBig:
84 case UOP_SaveXMM128Big:
85 return 3;
86 case UOP_AllocLarge:
87 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
88 }
89 }
90
91 // Prints one unwind code. Because an unwind code can occupy up to 3 slots in
92 // the unwind codes array, this function requires that the correct number of
93 // slots is provided.
94 static void printUnwindCode(ArrayRef UCs) {
95 assert(UCs.size() >= getNumUsedSlots(UCs[0]));
96 outs() << format(" 0x%02x: ", unsigned(UCs[0].u.CodeOffset))
97 << getUnwindCodeTypeName(UCs[0].getUnwindOp());
98 switch (UCs[0].getUnwindOp()) {
99 case UOP_PushNonVol:
100 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo());
101 break;
102 case UOP_AllocLarge:
103 if (UCs[0].getOpInfo() == 0) {
104 outs() << " " << UCs[1].FrameOffset;
105 } else {
106 outs() << " " << UCs[1].FrameOffset
107 + (static_cast(UCs[2].FrameOffset) << 16);
108 }
109 break;
110 case UOP_AllocSmall:
111 outs() << " " << ((UCs[0].getOpInfo() + 1) * 8);
112 break;
113 case UOP_SetFPReg:
114 outs() << " ";
115 break;
116 case UOP_SaveNonVol:
117 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
118 << format(" [0x%04x]", 8 * UCs[1].FrameOffset);
119 break;
120 case UOP_SaveNonVolBig:
121 outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo())
122 << format(" [0x%08x]", UCs[1].FrameOffset
123 + (static_cast(UCs[2].FrameOffset) << 16));
124 break;
125 case UOP_SaveXMM128:
126 outs() << " XMM" << static_cast(UCs[0].getOpInfo())
127 << format(" [0x%04x]", 16 * UCs[1].FrameOffset);
128 break;
129 case UOP_SaveXMM128Big:
130 outs() << " XMM" << UCs[0].getOpInfo()
131 << format(" [0x%08x]", UCs[1].FrameOffset
132 + (static_cast(UCs[2].FrameOffset) << 16));
133 break;
134 case UOP_PushMachFrame:
135 outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w")
136 << " error code";
137 break;
138 }
139 outs() << "\n";
140 }
141
142 static void printAllUnwindCodes(ArrayRef UCs) {
143 for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) {
144 unsigned UsedSlots = getNumUsedSlots(*I);
145 if (UsedSlots > UCs.size()) {
146 outs() << "Unwind data corrupted: Encountered unwind op "
147 << getUnwindCodeTypeName((*I).getUnwindOp())
148 << " which requires " << UsedSlots
149 << " slots, but only " << UCs.size()
150 << " remaining in buffer";
151 return ;
152 }
153 printUnwindCode(ArrayRef(I, E));
154 I += UsedSlots;
155 }
156 }
157
158 // Given a symbol sym this functions returns the address and section of it.
159 static error_code resolveSectionAndAddress(const COFFObjectFile *Obj,
160 const SymbolRef &Sym,
161 const coff_section *&ResolvedSection,
162 uint64_t &ResolvedAddr) {
163 if (error_code ec = Sym.getAddress(ResolvedAddr)) return ec;
164 section_iterator iter(Obj->begin_sections());
165 if (error_code ec = Sym.getSection(iter)) return ec;
166 ResolvedSection = Obj->getCOFFSection(iter);
167 return object_error::success;
168 }
169
170 // Given a vector of relocations for a section and an offset into this section
171 // the function returns the symbol used for the relocation at the offset.
172 static error_code resolveSymbol(const std::vector &Rels,
173 uint64_t Offset, SymbolRef &Sym) {
174 for (std::vector::const_iterator I = Rels.begin(),
175 E = Rels.end();
176 I != E; ++I) {
177 uint64_t Ofs;
178 if (error_code ec = I->getOffset(Ofs)) return ec;
179 if (Ofs == Offset) {
180 if (error_code ec = I->getSymbol(Sym)) return ec;
181 break;
182 }
183 }
184 return object_error::success;
185 }
186
187 // Given a vector of relocations for a section and an offset into this section
188 // the function resolves the symbol used for the relocation at the offset and
189 // returns the section content and the address inside the content pointed to
190 // by the symbol.
191 static error_code getSectionContents(const COFFObjectFile *Obj,
192 const std::vector &Rels,
193 uint64_t Offset,
194 ArrayRef &Contents,
195 uint64_t &Addr) {
196 SymbolRef Sym;
197 if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec;
198 const coff_section *Section;
199 if (error_code ec = resolveSectionAndAddress(Obj, Sym, Section, Addr))
200 return ec;
201 if (error_code ec = Obj->getSectionContents(Section, Contents)) return ec;
202 return object_error::success;
203 }
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.
208 static error_code resolveSymbolName(const std::vector &Rels,
209 uint64_t Offset, StringRef &Name) {
210 SymbolRef Sym;
211 if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec;
212 if (error_code ec = Sym.getName(Name)) return ec;
213 return object_error::success;
214 }
215
216 static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
217 const std::vector &Rels,
218 uint64_t Offset, uint32_t Disp) {
219 StringRef Sym;
220 if (error_code ec = resolveSymbolName(Rels, Offset, Sym)) {
221 error(ec);
222 return ;
223 }
224 Out << Sym;
225 if (Disp > 0)
226 Out << format(" + 0x%04x", Disp);
227 }
228
229 void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) {
230 const coff_file_header *Header;
231 if (error(Obj->getHeader(Header))) return;
232
233 if (Header->Machine != COFF::IMAGE_FILE_MACHINE_AMD64) {
234 errs() << "Unsupported image machine type "
235 "(currently only AMD64 is supported).\n";
236 return;
237 }
238
239 const coff_section *Pdata = 0;
240
241 error_code ec;
242 for (section_iterator SI = Obj->begin_sections(),
243 SE = Obj->end_sections();
244 SI != SE; SI.increment(ec)) {
245 if (error(ec)) return;
246
247 StringRef Name;
248 if (error(SI->getName(Name))) continue;
249
250 if (Name != ".pdata") continue;
251
252 Pdata = Obj->getCOFFSection(SI);
253 std::vector Rels;
254 for (relocation_iterator RI = SI->begin_relocations(),
255 RE = SI->end_relocations();
256 RI != RE; RI.increment(ec)) {
257 if (error(ec)) break;
258 Rels.push_back(*RI);
259 }
260
261 // Sort relocations by address.
262 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
263
264 ArrayRef Contents;
265 if (error(Obj->getSectionContents(Pdata, Contents))) continue;
266 if (Contents.empty()) continue;
267
268 ArrayRef RFs(
269 reinterpret_cast(Contents.data()),
270 Contents.size() / sizeof(RuntimeFunction));
271 for (const RuntimeFunction *I = RFs.begin(), *E = RFs.end(); I < E; ++I) {
272 const uint64_t SectionOffset = std::distance(RFs.begin(), I)
273 * sizeof(RuntimeFunction);
274
275 outs() << "Function Table:\n";
276
277 outs() << " Start Address: ";
278 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
279 offsetof(RuntimeFunction, StartAddress),
280 I->StartAddress);
281 outs() << "\n";
282
283 outs() << " End Address: ";
284 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
285 offsetof(RuntimeFunction, EndAddress),
286 I->EndAddress);
287 outs() << "\n";
288
289 outs() << " Unwind Info Address: ";
290 printCOFFSymbolAddress(outs(), Rels, SectionOffset +
291 offsetof(RuntimeFunction, UnwindInfoOffset),
292 I->UnwindInfoOffset);
293 outs() << "\n";
294
295 ArrayRef XContents;
296 uint64_t UnwindInfoOffset = 0;
297 if (error(getSectionContents(Obj, Rels, SectionOffset +
298 offsetof(RuntimeFunction, UnwindInfoOffset),
299 XContents, UnwindInfoOffset))) continue;
300 if (XContents.empty()) continue;
301
302 UnwindInfoOffset += I->UnwindInfoOffset;
303 if (UnwindInfoOffset > XContents.size()) continue;
304
305 const Win64EH::UnwindInfo *UI =
306 reinterpret_cast
307 (XContents.data() + UnwindInfoOffset);
308
309 // The casts to int are required in order to output the value as number.
310 // Without the casts the value would be interpreted as char data (which
311 // results in garbage output).
312 outs() << " Version: " << static_cast(UI->getVersion()) << "\n";
313 outs() << " Flags: " << static_cast(UI->getFlags());
314 if (UI->getFlags()) {
315 if (UI->getFlags() & UNW_ExceptionHandler)
316 outs() << " UNW_ExceptionHandler";
317 if (UI->getFlags() & UNW_TerminateHandler)
318 outs() << " UNW_TerminateHandler";
319 if (UI->getFlags() & UNW_ChainInfo)
320 outs() << " UNW_ChainInfo";
321 }
322 outs() << "\n";
323 outs() << " Size of prolog: "
324 << static_cast(UI->PrologSize) << "\n";
325 outs() << " Number of Codes: "
326 << static_cast(UI->NumCodes) << "\n";
327 // Maybe this should move to output of UOP_SetFPReg?
328 if (UI->getFrameRegister()) {
329 outs() << " Frame register: "
330 << getUnwindRegisterName(UI->getFrameRegister())
331 << "\n";
332 outs() << " Frame offset: "
333 << 16 * UI->getFrameOffset()
334 << "\n";
335 } else {
336 outs() << " No frame pointer used\n";
337 }
338 if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
339 // FIXME: Output exception handler data
340 } else if (UI->getFlags() & UNW_ChainInfo) {
341 // FIXME: Output chained unwind info
342 }
343
344 if (UI->NumCodes)
345 outs() << " Unwind Codes:\n";
346
347 printAllUnwindCodes(ArrayRef(&UI->UnwindCodes[0],
348 UI->NumCodes));
349
350 outs() << "\n\n";
351 outs().flush();
352 }
353 }
354 }
103103 NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, "
104104 "do not print the instruction bytes."));
105105
106 static cl::opt
107 UnwindInfo("unwind-info", cl::desc("Display unwind information"));
108
109 static cl::alias
110 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
111 cl::aliasopt(UnwindInfo));
112
106113 static StringRef ToolName;
107114
108 static bool error(error_code ec) {
115 bool llvm::error(error_code ec) {
109116 if (!ec) return false;
110117
111118 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
164171 outs() << output;
165172 }
166173
167 static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
174 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
168175 uint64_t a_addr, b_addr;
169176 if (error(a.getAddress(a_addr))) return false;
170177 if (error(b.getAddress(b_addr))) return false;
572579 }
573580 }
574581
582 static void PrintUnwindInfo(const ObjectFile *o) {
583 outs() << "Unwind info:\n\n";
584
585 if (const COFFObjectFile *coff = dyn_cast(o)) {
586 printCOFFUnwindInfo(coff);
587 } else {
588 // TODO: Extract DWARF dump tool to objdump.
589 errs() << "This operation is only currently supported "
590 "for COFF object files.\n";
591 return;
592 }
593 }
594
575595 static void DumpObject(const ObjectFile *o) {
576596 outs() << '\n';
577597 outs() << o->getFileName()
587607 PrintSectionContents(o);
588608 if (SymbolTable)
589609 PrintSymbolTable(o);
610 if (UnwindInfo)
611 PrintUnwindInfo(o);
590612 }
591613
592614 /// @brief Dump each object file in \a a;
665687 && !Relocations
666688 && !SectionHeaders
667689 && !SectionContents
668 && !SymbolTable) {
690 && !SymbolTable
691 && !UnwindInfo) {
669692 cl::PrintHelpMessage();
670693 return 2;
671694 }
1616
1717 namespace llvm {
1818
19 namespace object {
20 class COFFObjectFile;
21 class RelocationRef;
22 }
23 class error_code;
24
1925 extern cl::opt TripleName;
2026 extern cl::opt ArchName;
2127
2228 // Various helper functions.
29 bool error(error_code ec);
30 bool RelocAddressLess(object::RelocationRef a, object::RelocationRef b);
2331 void DumpBytes(StringRef bytes);
2432 void DisassembleInputMachO(StringRef Filename);
33 void printCOFFUnwindInfo(const object::COFFObjectFile* o);
2534
2635 class StringRefMemoryObject : public MemoryObject {
2736 virtual void anchor();