blob: 4b823b816c35a1d67f35ab74905d1b10bc9ceab7 [file] [log] [blame]
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001//===-- ARMWinEHPrinter.cpp - Windows on ARM EH Data Printer ----*- 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
Saleem Abdulrasool3ddcc492014-06-07 19:23:07 +000010// Windows on ARM uses a series of serialised data structures (RuntimeFunction)
11// to create a table of information for unwinding. In order to conserve space,
12// there are two different ways that this data is represented.
13//
14// For functions with canonical forms for the prologue and epilogue, the data
15// can be stored in a "packed" form. In this case, the data is packed into the
16// RuntimeFunction's remaining 30-bits and can fully describe the entire frame.
17//
18// +---------------------------------------+
19// | Function Entry Address |
20// +---------------------------------------+
21// | Packed Form Data |
22// +---------------------------------------+
23//
24// This layout is parsed by Decoder::dumpPackedEntry. No unwind bytecode is
25// associated with such a frame as they can be derived from the provided data.
26// The decoder does not synthesize this data as it is unnecessary for the
27// purposes of validation, with the synthesis being required only by a proper
28// unwinder.
29//
30// For functions that are large or do not match canonical forms, the data is
31// split up into two portions, with the actual data residing in the "exception
32// data" table (.xdata) with a reference to the entry from the "procedure data"
33// (.pdata) entry.
34//
35// The exception data contains information about the frame setup, all of the
Eric Christophera15b3af2018-03-29 21:59:04 +000036// epilogue scopes (for functions for which there are multiple exit points) and
Saleem Abdulrasool3ddcc492014-06-07 19:23:07 +000037// the associated exception handler. Additionally, the entry contains byte-code
38// describing how to unwind the function (c.f. Decoder::decodeOpcodes).
39//
40// +---------------------------------------+
41// | Function Entry Address |
42// +---------------------------------------+
43// | Exception Data Entry Address |
44// +---------------------------------------+
45//
46// This layout is parsed by Decoder::dumpUnpackedEntry. Such an entry must
47// first resolve the exception data entry address. This structure
48// (ExceptionDataRecord) has a variable sized header
49// (c.f. ARM::WinEH::HeaderWords) and encodes most of the same information as
50// the packed form. However, because this information is insufficient to
51// synthesize the unwinding, there are associated unwinding bytecode which make
52// up the bulk of the Decoder.
53//
54// The decoder itself is table-driven, using the first byte to determine the
55// opcode and dispatching to the associated printing routine. The bytecode
56// itself is a variable length instruction encoding that can fully describe the
57// state of the stack and the necessary operations for unwinding to the
58// beginning of the frame.
59//
60// The byte-code maintains a 1-1 instruction mapping, indicating both the width
61// of the instruction (Thumb2 instructions are variable length, 16 or 32 bits
62// wide) allowing the program to unwind from any point in the prologue, body, or
63// epilogue of the function.
64
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +000065#include "ARMWinEHPrinter.h"
66#include "Error.h"
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +000067#include "llvm/ADT/STLExtras.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000068#include "llvm/ADT/StringExtras.h"
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +000069#include "llvm/Support/ARMWinEH.h"
70#include "llvm/Support/Format.h"
71
72using namespace llvm;
73using namespace llvm::object;
74using namespace llvm::support;
75
76namespace llvm {
77raw_ostream &operator<<(raw_ostream &OS, const ARM::WinEH::ReturnType &RT) {
78 switch (RT) {
79 case ARM::WinEH::ReturnType::RT_POP:
80 OS << "pop {pc}";
81 break;
82 case ARM::WinEH::ReturnType::RT_B:
83 OS << "b target";
84 break;
85 case ARM::WinEH::ReturnType::RT_BW:
86 OS << "b.w target";
87 break;
88 case ARM::WinEH::ReturnType::RT_NoEpilogue:
89 OS << "(no epilogue)";
90 break;
91 }
92 return OS;
93}
94}
95
96static std::string formatSymbol(StringRef Name, uint64_t Address,
97 uint64_t Offset = 0) {
Alp Toker8dd8d5c2014-06-26 22:52:05 +000098 std::string Buffer;
99 raw_string_ostream OS(Buffer);
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000100
101 if (!Name.empty())
102 OS << Name << " ";
103
104 if (Offset)
105 OS << format("+0x%X (0x%" PRIX64 ")", Offset, Address);
106 else if (!Name.empty())
107 OS << format("(0x%" PRIX64 ")", Address);
108 else
109 OS << format("0x%" PRIX64, Address);
110
111 return OS.str();
112}
113
114namespace llvm {
115namespace ARM {
116namespace WinEH {
117const size_t Decoder::PDataEntrySize = sizeof(RuntimeFunction);
118
Saleem Abdulrasool3ddcc492014-06-07 19:23:07 +0000119// TODO name the uops more appropriately
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000120const Decoder::RingEntry Decoder::Ring[] = {
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000121 { 0x80, 0x00, 1, &Decoder::opcode_0xxxxxxx }, // UOP_STACK_FREE (16-bit)
122 { 0xc0, 0x80, 2, &Decoder::opcode_10Lxxxxx }, // UOP_POP (32-bit)
123 { 0xf0, 0xc0, 1, &Decoder::opcode_1100xxxx }, // UOP_STACK_SAVE (16-bit)
124 { 0xf8, 0xd0, 1, &Decoder::opcode_11010Lxx }, // UOP_POP (16-bit)
125 { 0xf8, 0xd8, 1, &Decoder::opcode_11011Lxx }, // UOP_POP (32-bit)
126 { 0xf8, 0xe0, 1, &Decoder::opcode_11100xxx }, // UOP_VPOP (32-bit)
127 { 0xfc, 0xe8, 2, &Decoder::opcode_111010xx }, // UOP_STACK_FREE (32-bit)
128 { 0xfe, 0xec, 2, &Decoder::opcode_1110110L }, // UOP_POP (16-bit)
129 { 0xff, 0xee, 2, &Decoder::opcode_11101110 }, // UOP_MICROSOFT_SPECIFIC (16-bit)
Saleem Abdulrasool3ddcc492014-06-07 19:23:07 +0000130 // UOP_PUSH_MACHINE_FRAME
131 // UOP_PUSH_CONTEXT
132 // UOP_PUSH_TRAP_FRAME
133 // UOP_REDZONE_RESTORE_LR
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000134 { 0xff, 0xef, 2, &Decoder::opcode_11101111 }, // UOP_LDRPC_POSTINC (32-bit)
135 { 0xff, 0xf5, 2, &Decoder::opcode_11110101 }, // UOP_VPOP (32-bit)
136 { 0xff, 0xf6, 2, &Decoder::opcode_11110110 }, // UOP_VPOP (32-bit)
137 { 0xff, 0xf7, 3, &Decoder::opcode_11110111 }, // UOP_STACK_RESTORE (16-bit)
138 { 0xff, 0xf8, 4, &Decoder::opcode_11111000 }, // UOP_STACK_RESTORE (16-bit)
139 { 0xff, 0xf9, 3, &Decoder::opcode_11111001 }, // UOP_STACK_RESTORE (32-bit)
140 { 0xff, 0xfa, 4, &Decoder::opcode_11111010 }, // UOP_STACK_RESTORE (32-bit)
141 { 0xff, 0xfb, 1, &Decoder::opcode_11111011 }, // UOP_NOP (16-bit)
142 { 0xff, 0xfc, 1, &Decoder::opcode_11111100 }, // UOP_NOP (32-bit)
143 { 0xff, 0xfd, 1, &Decoder::opcode_11111101 }, // UOP_NOP (16-bit) / END
144 { 0xff, 0xfe, 1, &Decoder::opcode_11111110 }, // UOP_NOP (32-bit) / END
145 { 0xff, 0xff, 1, &Decoder::opcode_11111111 }, // UOP_END
146};
147
148
149// Unwind opcodes for ARM64.
150// https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
151const Decoder::RingEntry Decoder::Ring64[] = {
152 { 0xe0, 0x00, 1, &Decoder::opcode_alloc_s },
153 { 0xe0, 0x20, 1, &Decoder::opcode_save_r19r20_x },
154 { 0xc0, 0x40, 1, &Decoder::opcode_save_fplr },
155 { 0xc0, 0x80, 1, &Decoder::opcode_save_fplr_x },
156 { 0xf8, 0xc0, 2, &Decoder::opcode_alloc_m },
157 { 0xfc, 0xc8, 2, &Decoder::opcode_save_regp },
158 { 0xfc, 0xcc, 2, &Decoder::opcode_save_regp_x },
159 { 0xfc, 0xd0, 2, &Decoder::opcode_save_reg },
160 { 0xfe, 0xd4, 2, &Decoder::opcode_save_reg_x },
161 { 0xfe, 0xd6, 2, &Decoder::opcode_save_lrpair },
162 { 0xfe, 0xd8, 2, &Decoder::opcode_save_fregp },
163 { 0xfe, 0xda, 2, &Decoder::opcode_save_fregp_x },
164 { 0xfe, 0xdc, 2, &Decoder::opcode_save_freg },
165 { 0xff, 0xde, 2, &Decoder::opcode_save_freg_x },
166 { 0xff, 0xe0, 4, &Decoder::opcode_alloc_l },
167 { 0xff, 0xe1, 1, &Decoder::opcode_setfp },
168 { 0xff, 0xe2, 2, &Decoder::opcode_addfp },
169 { 0xff, 0xe3, 1, &Decoder::opcode_nop },
170 { 0xff, 0xe4, 1, &Decoder::opcode_end },
171 { 0xff, 0xe5, 1, &Decoder::opcode_end_c },
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000172};
173
174void Decoder::printRegisters(const std::pair<uint16_t, uint32_t> &RegisterMask) {
175 static const char * const GPRRegisterNames[16] = {
176 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
177 "r11", "ip", "sp", "lr", "pc",
178 };
179
180 const uint16_t GPRMask = std::get<0>(RegisterMask);
181 const uint16_t VFPMask = std::get<1>(RegisterMask);
182
183 OS << '{';
184 bool Comma = false;
185 for (unsigned RI = 0, RE = 11; RI < RE; ++RI) {
186 if (GPRMask & (1 << RI)) {
187 if (Comma)
188 OS << ", ";
189 OS << GPRRegisterNames[RI];
190 Comma = true;
191 }
192 }
193 for (unsigned RI = 0, RE = 32; RI < RE; ++RI) {
194 if (VFPMask & (1 << RI)) {
195 if (Comma)
196 OS << ", ";
197 OS << "d" << unsigned(RI);
198 Comma = true;
199 }
200 }
201 for (unsigned RI = 11, RE = 16; RI < RE; ++RI) {
202 if (GPRMask & (1 << RI)) {
203 if (Comma)
204 OS << ", ";
205 OS << GPRRegisterNames[RI];
206 Comma = true;
207 }
208 }
209 OS << '}';
210}
211
212ErrorOr<object::SectionRef>
213Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
214 for (const auto &Section : COFF.sections()) {
Rafael Espindola8175be52014-10-08 15:28:58 +0000215 uint64_t Address = Section.getAddress();
216 uint64_t Size = Section.getSize();
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000217
218 if (VA >= Address && (VA - Address) <= Size)
219 return Section;
220 }
221 return readobj_error::unknown_symbol;
222}
223
224ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
225 uint64_t VA, bool FunctionOnly) {
226 for (const auto &Symbol : COFF.symbols()) {
Kevin Enderbya486dca2016-05-02 20:28:12 +0000227 Expected<SymbolRef::Type> Type = Symbol.getType();
228 if (!Type)
229 return errorToErrorCode(Type.takeError());
Kevin Enderby46e35ed2016-03-23 20:27:00 +0000230 if (FunctionOnly && *Type != SymbolRef::ST_Function)
Rafael Espindola50bea402015-06-26 12:18:49 +0000231 continue;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000232
Kevin Enderby317de7c2016-06-24 18:24:42 +0000233 Expected<uint64_t> Address = Symbol.getAddress();
234 if (!Address)
235 return errorToErrorCode(Address.takeError());
Rafael Espindola5954faa2015-07-03 18:19:00 +0000236 if (*Address == VA)
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000237 return Symbol;
238 }
239 return readobj_error::unknown_symbol;
240}
241
242ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
243 const SectionRef &Section,
244 uint64_t Offset) {
245 for (const auto &Relocation : Section.relocations()) {
Rafael Espindolaff676292015-06-29 23:29:12 +0000246 uint64_t RelocationOffset = Relocation.getOffset();
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000247 if (RelocationOffset == Offset)
248 return *Relocation.getSymbol();
249 }
250 return readobj_error::unknown_symbol;
251}
252
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000253bool Decoder::opcode_0xxxxxxx(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000254 unsigned Length, bool Prologue) {
255 uint8_t Imm = OC[Offset] & 0x7f;
256 SW.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
257 OC[Offset],
258 static_cast<const char *>(Prologue ? "sub" : "add"),
259 Imm);
260 ++Offset;
261 return false;
262}
263
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000264bool Decoder::opcode_10Lxxxxx(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000265 unsigned Length, bool Prologue) {
266 unsigned Link = (OC[Offset] & 0x20) >> 5;
267 uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
268 | ((OC[Offset + 0] & 0x1f) << 8)
269 | ((OC[Offset + 1] & 0xff) << 0);
270 assert((~RegisterMask & (1 << 13)) && "sp must not be set");
271 assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
272
273 SW.startLine() << format("0x%02x 0x%02x ; %s.w ",
274 OC[Offset + 0], OC[Offset + 1],
275 Prologue ? "push" : "pop");
276 printRegisters(std::make_pair(RegisterMask, 0));
277 OS << '\n';
278
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000279 Offset += 2;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000280 return false;
281}
282
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000283bool Decoder::opcode_1100xxxx(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000284 unsigned Length, bool Prologue) {
285 if (Prologue)
286 SW.startLine() << format("0x%02x ; mov r%u, sp\n",
287 OC[Offset], OC[Offset] & 0xf);
288 else
289 SW.startLine() << format("0x%02x ; mov sp, r%u\n",
290 OC[Offset], OC[Offset] & 0xf);
291 ++Offset;
292 return false;
293}
294
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000295bool Decoder::opcode_11010Lxx(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000296 unsigned Length, bool Prologue) {
297 unsigned Link = (OC[Offset] & 0x4) >> 3;
298 unsigned Count = (OC[Offset] & 0x3);
299
300 uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
301 | (((1 << (Count + 1)) - 1) << 4);
302
303 SW.startLine() << format("0x%02x ; %s ", OC[Offset],
304 Prologue ? "push" : "pop");
305 printRegisters(std::make_pair(GPRMask, 0));
306 OS << '\n';
307
308 ++Offset;
309 return false;
310}
311
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000312bool Decoder::opcode_11011Lxx(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000313 unsigned Length, bool Prologue) {
314 unsigned Link = (OC[Offset] & 0x4) >> 2;
315 unsigned Count = (OC[Offset] & 0x3) + 4;
316
317 uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
318 | (((1 << (Count + 1)) - 1) << 4);
319
320 SW.startLine() << format("0x%02x ; %s.w ", OC[Offset],
321 Prologue ? "push" : "pop");
322 printRegisters(std::make_pair(GPRMask, 0));
323 OS << '\n';
324
325 ++Offset;
326 return false;
327}
328
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000329bool Decoder::opcode_11100xxx(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000330 unsigned Length, bool Prologue) {
331 unsigned High = (OC[Offset] & 0x7);
332 uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
333
334 SW.startLine() << format("0x%02x ; %s ", OC[Offset],
335 Prologue ? "vpush" : "vpop");
336 printRegisters(std::make_pair(0, VFPMask));
337 OS << '\n';
338
339 ++Offset;
340 return false;
341}
342
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000343bool Decoder::opcode_111010xx(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000344 unsigned Length, bool Prologue) {
345 uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
346
347 SW.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
348 OC[Offset + 0], OC[Offset + 1],
349 static_cast<const char *>(Prologue ? "sub" : "add"),
350 Imm);
351
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000352 Offset += 2;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000353 return false;
354}
355
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000356bool Decoder::opcode_1110110L(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000357 unsigned Length, bool Prologue) {
358 uint8_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
359 | ((OC[Offset + 1] & 0xff) << 0);
360
361 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
362 OC[Offset + 1], Prologue ? "push" : "pop");
363 printRegisters(std::make_pair(GPRMask, 0));
364 OS << '\n';
365
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000366 Offset += 2;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000367 return false;
368}
369
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000370bool Decoder::opcode_11101110(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000371 unsigned Length, bool Prologue) {
372 assert(!Prologue && "may not be used in prologue");
373
374 if (OC[Offset + 1] & 0xf0)
375 SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
376 OC[Offset + 0], OC[Offset + 1]);
377 else
378 SW.startLine()
379 << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
380 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] & 0x0f);
381
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000382 Offset += 2;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000383 return false;
384}
385
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000386bool Decoder::opcode_11101111(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000387 unsigned Length, bool Prologue) {
388 assert(!Prologue && "may not be used in prologue");
389
390 if (OC[Offset + 1] & 0xf0)
391 SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
392 OC[Offset + 0], OC[Offset + 1]);
393 else
394 SW.startLine()
395 << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
396 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
397
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000398 Offset += 2;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000399 return false;
400}
401
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000402bool Decoder::opcode_11110101(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000403 unsigned Length, bool Prologue) {
404 unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
405 unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
406 uint32_t VFPMask = ((1 << (End - Start)) - 1) << Start;
407
408 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
409 OC[Offset + 1], Prologue ? "vpush" : "vpop");
410 printRegisters(std::make_pair(0, VFPMask));
411 OS << '\n';
412
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000413 Offset += 2;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000414 return false;
415}
416
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000417bool Decoder::opcode_11110110(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000418 unsigned Length, bool Prologue) {
419 unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
420 unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
421 uint32_t VFPMask = ((1 << (End - Start)) - 1) << 16;
422
423 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
424 OC[Offset + 1], Prologue ? "vpush" : "vpop");
425 printRegisters(std::make_pair(0, VFPMask));
426 OS << '\n';
427
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000428 Offset += 2;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000429 return false;
430}
431
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000432bool Decoder::opcode_11110111(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000433 unsigned Length, bool Prologue) {
434 uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
435
436 SW.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
437 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
438 static_cast<const char *>(Prologue ? "sub" : "add"),
439 Imm);
440
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000441 Offset += 3;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000442 return false;
443}
444
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000445bool Decoder::opcode_11111000(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000446 unsigned Length, bool Prologue) {
447 uint32_t Imm = (OC[Offset + 1] << 16)
448 | (OC[Offset + 2] << 8)
449 | (OC[Offset + 3] << 0);
450
451 SW.startLine()
452 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
453 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
454 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
455
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000456 Offset += 4;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000457 return false;
458}
459
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000460bool Decoder::opcode_11111001(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000461 unsigned Length, bool Prologue) {
462 uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
463
464 SW.startLine()
465 << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
466 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
467 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
468
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000469 Offset += 3;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000470 return false;
471}
472
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000473bool Decoder::opcode_11111010(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000474 unsigned Length, bool Prologue) {
475 uint32_t Imm = (OC[Offset + 1] << 16)
476 | (OC[Offset + 2] << 8)
477 | (OC[Offset + 3] << 0);
478
479 SW.startLine()
480 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
481 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
482 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
483
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000484 Offset += 4;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000485 return false;
486}
487
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000488bool Decoder::opcode_11111011(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000489 unsigned Length, bool Prologue) {
490 SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
491 ++Offset;
492 return false;
493}
494
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000495bool Decoder::opcode_11111100(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000496 unsigned Length, bool Prologue) {
497 SW.startLine() << format("0x%02x ; nop.w\n", OC[Offset]);
498 ++Offset;
499 return false;
500}
501
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000502bool Decoder::opcode_11111101(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000503 unsigned Length, bool Prologue) {
504 SW.startLine() << format("0x%02x ; b\n", OC[Offset]);
505 ++Offset;
506 return true;
507}
508
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000509bool Decoder::opcode_11111110(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000510 unsigned Length, bool Prologue) {
511 SW.startLine() << format("0x%02x ; b.w\n", OC[Offset]);
512 ++Offset;
513 return true;
514}
515
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000516bool Decoder::opcode_11111111(const uint8_t *OC, unsigned &Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000517 unsigned Length, bool Prologue) {
518 ++Offset;
519 return true;
520}
521
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000522// ARM64 unwind codes start here.
523bool Decoder::opcode_alloc_s(const uint8_t *OC, unsigned &Offset,
524 unsigned Length, bool Prologue) {
525 uint32_t NumBytes = (OC[Offset] & 0x1F) << 4;
526 SW.startLine() << format("0x%02x ; %s sp, #%u\n", OC[Offset],
527 static_cast<const char *>(Prologue ? "sub" : "add"),
528 NumBytes);
529 ++Offset;
530 return false;
531}
532
533bool Decoder::opcode_save_r19r20_x(const uint8_t *OC, unsigned &Offset,
534 unsigned Length, bool Prologue) {
535 uint32_t Off = (OC[Offset] & 0x1F) << 3;
536 if (Prologue)
537 SW.startLine() << format(
538 "0x%02x ; stp x19, x20, [sp, #-%u]!\n", OC[Offset], Off);
539 else
540 SW.startLine() << format(
541 "0x%02x ; ldp x19, x20, [sp], #%u\n", OC[Offset], Off);
542 ++Offset;
543 return false;
544}
545
546bool Decoder::opcode_save_fplr(const uint8_t *OC, unsigned &Offset,
547 unsigned Length, bool Prologue) {
548 uint32_t Off = (OC[Offset] & 0x3F) << 3;
549 SW.startLine() << format(
550 "0x%02x ; %s x29, x30, [sp, #%u]\n", OC[Offset],
551 static_cast<const char *>(Prologue ? "stp" : "ldp"), Off);
552 ++Offset;
553 return false;
554}
555
556bool Decoder::opcode_save_fplr_x(const uint8_t *OC, unsigned &Offset,
557 unsigned Length, bool Prologue) {
558 uint32_t Off = ((OC[Offset] & 0x3F) + 1) << 3;
559 if (Prologue)
560 SW.startLine() << format(
561 "0x%02x ; stp x29, x30, [sp, #-%u]!\n", OC[Offset], Off);
562 else
563 SW.startLine() << format(
564 "0x%02x ; ldp x29, x30, [sp], #%u\n", OC[Offset], Off);
565 ++Offset;
566 return false;
567}
568
569bool Decoder::opcode_alloc_m(const uint8_t *OC, unsigned &Offset,
570 unsigned Length, bool Prologue) {
571 uint32_t NumBytes = ((OC[Offset] & 0x07) << 8);
572 NumBytes |= (OC[Offset + 1] & 0xFF);
573 NumBytes <<= 4;
574 SW.startLine() << format("0x%02x%02x ; %s sp, #%u\n",
575 OC[Offset], OC[Offset + 1],
576 static_cast<const char *>(Prologue ? "sub" : "add"),
577 NumBytes);
578 Offset += 2;
579 return false;
580}
581
582bool Decoder::opcode_save_regp(const uint8_t *OC, unsigned &Offset,
583 unsigned Length, bool Prologue) {
584 uint32_t Reg = ((OC[Offset] & 0x03) << 8);
585 Reg |= (OC[Offset + 1] & 0xC0);
586 Reg >>= 6;
587 Reg += 19;
588 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
589 SW.startLine() << format(
590 "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
591 OC[Offset], OC[Offset + 1],
592 static_cast<const char *>(Prologue ? "stp" : "ldp"), Reg, Reg + 1, Off);
593 Offset += 2;
594 return false;
595}
596
597bool Decoder::opcode_save_regp_x(const uint8_t *OC, unsigned &Offset,
598 unsigned Length, bool Prologue) {
599 uint32_t Reg = ((OC[Offset] & 0x03) << 8);
600 Reg |= (OC[Offset + 1] & 0xC0);
601 Reg >>= 6;
602 Reg += 19;
603 uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
604 if (Prologue)
605 SW.startLine() << format(
606 "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
607 OC[Offset], OC[Offset + 1], Reg,
608 Reg + 1, Off);
609 else
610 SW.startLine() << format(
611 "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
612 OC[Offset], OC[Offset + 1], Reg,
613 Reg + 1, Off);
614 Offset += 2;
615 return false;
616}
617
618bool Decoder::opcode_save_reg(const uint8_t *OC, unsigned &Offset,
619 unsigned Length, bool Prologue) {
620 uint32_t Reg = (OC[Offset] & 0x03) << 8;
621 Reg |= (OC[Offset + 1] & 0xC0);
622 Reg >>= 6;
623 Reg += 19;
624 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
625 SW.startLine() << format("0x%02x%02x ; %s x%u, [sp, #%u]\n",
626 OC[Offset], OC[Offset + 1],
627 static_cast<const char *>(Prologue ? "str" : "ldr"),
628 Reg, Off);
629 Offset += 2;
630 return false;
631}
632
633bool Decoder::opcode_save_reg_x(const uint8_t *OC, unsigned &Offset,
634 unsigned Length, bool Prologue) {
635 uint32_t Reg = (OC[Offset] & 0x01) << 8;
636 Reg |= (OC[Offset + 1] & 0xE0);
637 Reg >>= 5;
638 Reg += 19;
639 uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
640 if (Prologue)
641 SW.startLine() << format("0x%02x%02x ; str x%u, [sp, #%u]!\n",
642 OC[Offset], OC[Offset + 1], Reg, Off);
643 else
644 SW.startLine() << format("0x%02x%02x ; ldr x%u, [sp], #%u\n",
645 OC[Offset], OC[Offset + 1], Reg, Off);
646 Offset += 2;
647 return false;
648}
649
650bool Decoder::opcode_save_lrpair(const uint8_t *OC, unsigned &Offset,
651 unsigned Length, bool Prologue) {
652 uint32_t Reg = (OC[Offset] & 0x01) << 8;
653 Reg |= (OC[Offset + 1] & 0xC0);
654 Reg >>= 6;
655 Reg *= 2;
656 Reg += 19;
657 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
658 SW.startLine() << format("0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
659 OC[Offset], OC[Offset + 1],
660 static_cast<const char *>(Prologue ? "stp" : "ldp"),
661 Reg, Off);
662 Offset += 2;
663 return false;
664}
665
666bool Decoder::opcode_save_fregp(const uint8_t *OC, unsigned &Offset,
667 unsigned Length, bool Prologue) {
668 uint32_t Reg = (OC[Offset] & 0x01) << 8;
669 Reg |= (OC[Offset + 1] & 0xC0);
670 Reg >>= 6;
671 Reg += 8;
672 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
673 SW.startLine() << format("0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
674 OC[Offset], OC[Offset + 1],
675 static_cast<const char *>(Prologue ? "stp" : "ldp"),
676 Reg, Reg + 1, Off);
677 Offset += 2;
678 return false;
679}
680
681bool Decoder::opcode_save_fregp_x(const uint8_t *OC, unsigned &Offset,
682 unsigned Length, bool Prologue) {
683 uint32_t Reg = (OC[Offset] & 0x01) << 8;
684 Reg |= (OC[Offset + 1] & 0xC0);
685 Reg >>= 6;
686 Reg += 8;
687 uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
688 if (Prologue)
689 SW.startLine() << format(
690 "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", OC[Offset],
691 OC[Offset + 1], Reg, Reg + 1, Off);
692 else
693 SW.startLine() << format(
694 "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", OC[Offset],
695 OC[Offset + 1], Reg, Reg + 1, Off);
696 Offset += 2;
697 return false;
698}
699
700bool Decoder::opcode_save_freg(const uint8_t *OC, unsigned &Offset,
701 unsigned Length, bool Prologue) {
702 uint32_t Reg = (OC[Offset] & 0x01) << 8;
703 Reg |= (OC[Offset + 1] & 0xC0);
704 Reg >>= 6;
705 Reg += 8;
706 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
707 SW.startLine() << format("0x%02x%02x ; %s d%u, [sp, #%u]\n",
708 OC[Offset], OC[Offset + 1],
709 static_cast<const char *>(Prologue ? "str" : "ldr"),
710 Reg, Off);
711 Offset += 2;
712 return false;
713}
714
715bool Decoder::opcode_save_freg_x(const uint8_t *OC, unsigned &Offset,
716 unsigned Length, bool Prologue) {
717 uint32_t Reg = ((OC[Offset + 1] & 0xE0) >> 5) + 8;
718 uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
719 if (Prologue)
720 SW.startLine() << format(
721 "0x%02x%02x ; str d%u, [sp, #-%u]!\n", OC[Offset],
722 OC[Offset + 1], Reg, Off);
723 else
724 SW.startLine() << format(
725 "0x%02x%02x ; ldr d%u, [sp], #%u\n", OC[Offset],
726 OC[Offset + 1], Reg, Off);
727 Offset += 2;
728 return false;
729}
730
731bool Decoder::opcode_alloc_l(const uint8_t *OC, unsigned &Offset,
732 unsigned Length, bool Prologue) {
733 unsigned Off =
734 (OC[Offset + 1] << 16) | (OC[Offset + 2] << 8) | (OC[Offset + 3] << 0);
735 Off <<= 4;
736 SW.startLine() << format(
737 "0x%02x%02x%02x%02x ; %s sp, #%u\n", OC[Offset], OC[Offset + 1],
738 OC[Offset + 2], OC[Offset + 3],
739 static_cast<const char *>(Prologue ? "sub" : "add"), Off);
740 Offset += 4;
741 return false;
742}
743
744bool Decoder::opcode_setfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
745 bool Prologue) {
746 SW.startLine() << format("0x%02x ; mov fp, sp\n", OC[Offset]);
747 ++Offset;
748 return false;
749}
750
751bool Decoder::opcode_addfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
752 bool Prologue) {
753 unsigned NumBytes = OC[Offset + 1] << 3;
754 SW.startLine() << format("0x%02x%02x ; add fp, sp, #%u\n",
755 OC[Offset], OC[Offset + 1], NumBytes);
756 Offset += 2;
757 return false;
758}
759
760bool Decoder::opcode_nop(const uint8_t *OC, unsigned &Offset, unsigned Length,
761 bool Prologue) {
762 SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
763 ++Offset;
764 return false;
765}
766
767bool Decoder::opcode_end(const uint8_t *OC, unsigned &Offset, unsigned Length,
768 bool Prologue) {
769 SW.startLine() << format("0x%02x ; end\n", OC[Offset]);
770 ++Offset;
771 return true;
772}
773
774bool Decoder::opcode_end_c(const uint8_t *OC, unsigned &Offset, unsigned Length,
775 bool Prologue) {
776 SW.startLine() << format("0x%02x ; end_c\n", OC[Offset]);
777 ++Offset;
778 return true;
779}
780
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000781void Decoder::decodeOpcodes(ArrayRef<uint8_t> Opcodes, unsigned Offset,
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000782 bool Prologue) {
Saleem Abdulrasool510315c2014-06-04 16:03:20 +0000783 assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000784 const RingEntry* DecodeRing = isAArch64 ? Ring64 : Ring;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000785 bool Terminated = false;
786 for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
Benjamin Kramer4232f4e2014-07-01 14:46:44 +0000787 for (unsigned DI = 0;; ++DI) {
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000788 if ((isAArch64 && (DI >= array_lengthof(Ring64))) ||
789 (!isAArch64 && (DI >= array_lengthof(Ring)))) {
790 SW.startLine() << format("0x%02x ; Bad opcode!\n",
Eli Friedman34975422018-11-02 19:59:08 +0000791 Opcodes.data()[OI]);
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000792 ++OI;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000793 break;
794 }
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000795
796 if ((Opcodes[OI] & DecodeRing[DI].Mask) == DecodeRing[DI].Value) {
797 if (OI + DecodeRing[DI].Length > OE) {
798 SW.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
799 Opcodes[OI]);
800 OI += DecodeRing[DI].Length;
801 break;
802 }
803 Terminated =
804 (this->*DecodeRing[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
805 break;
806 }
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000807 }
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000808 }
809}
810
811bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
812 const SectionRef &Section,
813 uint64_t FunctionAddress, uint64_t VA) {
814 ArrayRef<uint8_t> Contents;
815 if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
816 return false;
817
Rafael Espindola8175be52014-10-08 15:28:58 +0000818 uint64_t SectionVA = Section.getAddress();
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000819 uint64_t Offset = VA - SectionVA;
820 const ulittle32_t *Data =
821 reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000822
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000823 // Sanity check to ensure that the .xdata header is present.
824 // A header is one or two words, followed by at least one word to describe
825 // the unwind codes. Applicable to both ARM and AArch64.
826 if (Contents.size() - Offset < 8)
827 report_fatal_error(".xdata must be at least 8 bytes in size");
828
829 const ExceptionDataRecord XData(Data, isAArch64);
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000830 DictScope XRS(SW, "ExceptionData");
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000831 SW.printNumber("FunctionLength",
832 isAArch64 ? XData.FunctionLengthInBytesAArch64() :
833 XData.FunctionLengthInBytesARM());
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000834 SW.printNumber("Version", XData.Vers());
835 SW.printBoolean("ExceptionData", XData.X());
836 SW.printBoolean("EpiloguePacked", XData.E());
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000837 if (!isAArch64)
838 SW.printBoolean("Fragment", XData.F());
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000839 SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
840 XData.EpilogueCount());
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000841 uint64_t ByteCodeLength = XData.CodeWords() * sizeof(uint32_t);
842 SW.printNumber("ByteCodeLength", ByteCodeLength);
843
844 if ((int64_t)(Contents.size() - Offset - 4 * HeaderWords(XData) -
845 (XData.E() ? 0 : XData.EpilogueCount() * 4) -
846 (XData.X() ? 8 : 0)) < (int64_t)ByteCodeLength)
847 report_fatal_error("Malformed unwind data");
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000848
849 if (XData.E()) {
Rui Ueyamaf70f3d42014-09-11 21:46:33 +0000850 ArrayRef<uint8_t> UC = XData.UnwindByteCode();
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000851 if (isAArch64 || !XData.F()) {
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000852 ListScope PS(SW, "Prologue");
853 decodeOpcodes(UC, 0, /*Prologue=*/true);
854 }
855 if (XData.EpilogueCount()) {
856 ListScope ES(SW, "Epilogue");
857 decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
858 }
859 } else {
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000860 {
861 ListScope PS(SW, "Prologue");
862 decodeOpcodes(XData.UnwindByteCode(), 0, /*Prologue=*/true);
863 }
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000864 ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
865 ListScope ESS(SW, "EpilogueScopes");
866 for (const EpilogueScope ES : EpilogueScopes) {
867 DictScope ESES(SW, "EpilogueScope");
868 SW.printNumber("StartOffset", ES.EpilogueStartOffset());
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000869 if (!isAArch64)
870 SW.printNumber("Condition", ES.Condition());
871 SW.printNumber("EpilogueStartIndex",
872 isAArch64 ? ES.EpilogueStartIndexAArch64()
873 : ES.EpilogueStartIndexARM());
Eli Friedman34975422018-11-02 19:59:08 +0000874 if (ES.ES & ~0xffc3ffff)
875 SW.printNumber("ReservedBits", (ES.ES >> 18) & 0xF);
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000876
877 ListScope Opcodes(SW, "Opcodes");
Sanjin Sijarice10866a2018-10-24 00:03:34 +0000878 decodeOpcodes(XData.UnwindByteCode(),
879 isAArch64 ? ES.EpilogueStartIndexAArch64()
880 : ES.EpilogueStartIndexARM(),
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000881 /*Prologue=*/false);
882 }
883 }
884
885 if (XData.X()) {
886 const uint32_t Address = XData.ExceptionHandlerRVA();
887 const uint32_t Parameter = XData.ExceptionHandlerParameter();
888 const size_t HandlerOffset = HeaderWords(XData)
889 + (XData.E() ? 0 : XData.EpilogueCount())
890 + XData.CodeWords();
891
Eli Friedman34975422018-11-02 19:59:08 +0000892 ErrorOr<SymbolRef> Symbol = getRelocatedSymbol(
893 COFF, Section, Offset + HandlerOffset * sizeof(uint32_t));
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000894 if (!Symbol)
895 Symbol = getSymbol(COFF, Address, /*FunctionOnly=*/true);
Eli Friedman34975422018-11-02 19:59:08 +0000896 if (!Symbol) {
897 ListScope EHS(SW, "ExceptionHandler");
898 SW.printString("Routine", "(null)");
899 return true;
900 }
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000901
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000902 Expected<StringRef> Name = Symbol->getName();
903 if (!Name) {
904 std::string Buf;
905 llvm::raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000906 logAllUnhandledErrors(Name.takeError(), OS);
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000907 OS.flush();
908 report_fatal_error(Buf);
909 }
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000910
911 ListScope EHS(SW, "ExceptionHandler");
Rafael Espindola8a806412015-07-02 20:55:21 +0000912 SW.printString("Routine", formatSymbol(*Name, Address));
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000913 SW.printHex("Parameter", Parameter);
914 }
915
916 return true;
917}
918
919bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
920 const SectionRef Section, uint64_t Offset,
921 unsigned Index, const RuntimeFunction &RF) {
922 assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
923 "packed entry cannot be treated as an unpacked entry");
924
925 ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
926 if (!Function)
927 Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
928
929 ErrorOr<SymbolRef> XDataRecord = getRelocatedSymbol(COFF, Section, Offset + 4);
930 if (!XDataRecord)
931 XDataRecord = getSymbol(COFF, RF.ExceptionInformationRVA());
932
933 if (!RF.BeginAddress && !Function)
934 return false;
935 if (!RF.UnwindData && !XDataRecord)
936 return false;
937
938 StringRef FunctionName;
939 uint64_t FunctionAddress;
940 if (Function) {
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000941 Expected<StringRef> FunctionNameOrErr = Function->getName();
942 if (!FunctionNameOrErr) {
943 std::string Buf;
944 llvm::raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000945 logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000946 OS.flush();
947 report_fatal_error(Buf);
948 }
Rafael Espindola8a806412015-07-02 20:55:21 +0000949 FunctionName = *FunctionNameOrErr;
Kevin Enderby317de7c2016-06-24 18:24:42 +0000950 Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
951 if (!FunctionAddressOrErr) {
952 std::string Buf;
953 llvm::raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000954 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
Kevin Enderby317de7c2016-06-24 18:24:42 +0000955 OS.flush();
956 report_fatal_error(Buf);
957 }
Rafael Espindola5954faa2015-07-03 18:19:00 +0000958 FunctionAddress = *FunctionAddressOrErr;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000959 } else {
Eli Friedman34975422018-11-02 19:59:08 +0000960 FunctionAddress = COFF.getImageBase() + RF.BeginAddress;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000961 }
962
963 SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
964
965 if (XDataRecord) {
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000966 Expected<StringRef> Name = XDataRecord->getName();
967 if (!Name) {
968 std::string Buf;
969 llvm::raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000970 logAllUnhandledErrors(Name.takeError(), OS);
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000971 OS.flush();
972 report_fatal_error(Buf);
973 }
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000974
Kevin Enderby317de7c2016-06-24 18:24:42 +0000975 Expected<uint64_t> AddressOrErr = XDataRecord->getAddress();
976 if (!AddressOrErr) {
977 std::string Buf;
978 llvm::raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000979 logAllUnhandledErrors(AddressOrErr.takeError(), OS);
Kevin Enderby317de7c2016-06-24 18:24:42 +0000980 OS.flush();
981 report_fatal_error(Buf);
982 }
Rafael Espindola5954faa2015-07-03 18:19:00 +0000983 uint64_t Address = *AddressOrErr;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000984
Rafael Espindola8a806412015-07-02 20:55:21 +0000985 SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000986
Kevin Enderbya486dca2016-05-02 20:28:12 +0000987 Expected<section_iterator> SIOrErr = XDataRecord->getSection();
988 if (!SIOrErr) {
989 // TODO: Actually report errors helpfully.
990 consumeError(SIOrErr.takeError());
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000991 return false;
Kevin Enderbya486dca2016-05-02 20:28:12 +0000992 }
Rafael Espindolae84d8c12015-08-07 23:27:14 +0000993 section_iterator SI = *SIOrErr;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000994
Eli Friedman34975422018-11-02 19:59:08 +0000995 // FIXME: Do we need to add an offset from the relocation?
996 return dumpXDataRecord(COFF, *SI, FunctionAddress,
997 RF.ExceptionInformationRVA());
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +0000998 } else {
Eli Friedman34975422018-11-02 19:59:08 +0000999 uint64_t Address = COFF.getImageBase() + RF.ExceptionInformationRVA();
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001000 SW.printString("ExceptionRecord", formatSymbol("", Address));
1001
Eli Friedman34975422018-11-02 19:59:08 +00001002 ErrorOr<SectionRef> Section = getSectionContaining(COFF, Address);
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001003 if (!Section)
1004 return false;
1005
Eli Friedman34975422018-11-02 19:59:08 +00001006 return dumpXDataRecord(COFF, *Section, FunctionAddress, Address);
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001007 }
1008}
1009
1010bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
1011 const SectionRef Section, uint64_t Offset,
1012 unsigned Index, const RuntimeFunction &RF) {
1013 assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
1014 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
1015 "unpacked entry cannot be treated as a packed entry");
1016
1017 ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
1018 if (!Function)
1019 Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
1020
1021 StringRef FunctionName;
1022 uint64_t FunctionAddress;
1023 if (Function) {
Kevin Enderby813e0cf2016-04-20 21:24:34 +00001024 Expected<StringRef> FunctionNameOrErr = Function->getName();
1025 if (!FunctionNameOrErr) {
1026 std::string Buf;
1027 llvm::raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +00001028 logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
Kevin Enderby813e0cf2016-04-20 21:24:34 +00001029 OS.flush();
1030 report_fatal_error(Buf);
1031 }
Rafael Espindola8a806412015-07-02 20:55:21 +00001032 FunctionName = *FunctionNameOrErr;
Kevin Enderby317de7c2016-06-24 18:24:42 +00001033 Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
1034 if (!FunctionAddressOrErr) {
1035 std::string Buf;
1036 llvm::raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +00001037 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
Kevin Enderby317de7c2016-06-24 18:24:42 +00001038 OS.flush();
1039 report_fatal_error(Buf);
1040 }
Rafael Espindola5954faa2015-07-03 18:19:00 +00001041 FunctionAddress = *FunctionAddressOrErr;
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001042 } else {
1043 const pe32_header *PEHeader;
1044 if (COFF.getPE32Header(PEHeader))
1045 return false;
1046 FunctionAddress = PEHeader->ImageBase + RF.BeginAddress;
1047 }
1048
1049 SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
Sanjin Sijarice10866a2018-10-24 00:03:34 +00001050 if (!isAArch64)
1051 SW.printBoolean("Fragment",
1052 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001053 SW.printNumber("FunctionLength", RF.FunctionLength());
1054 SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
1055 SW.printBoolean("HomedParameters", RF.H());
1056 SW.startLine() << "SavedRegisters: ";
1057 printRegisters(SavedRegisterMask(RF));
1058 OS << '\n';
1059 SW.printNumber("StackAdjustment", StackAdjustment(RF) << 2);
1060
1061 return true;
1062}
1063
1064bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
1065 const SectionRef Section, unsigned Index,
1066 ArrayRef<uint8_t> Contents) {
1067 uint64_t Offset = PDataEntrySize * Index;
1068 const ulittle32_t *Data =
1069 reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
1070
1071 const RuntimeFunction Entry(Data);
1072 DictScope RFS(SW, "RuntimeFunction");
1073 if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
1074 return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
Sanjin Sijarice10866a2018-10-24 00:03:34 +00001075 if (isAArch64) {
Eli Friedman34975422018-11-02 19:59:08 +00001076 SW.startLine() << "Packed unwind data not yet supported for ARM64\n";
1077 return true;
Sanjin Sijarice10866a2018-10-24 00:03:34 +00001078 }
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001079 return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
1080}
1081
1082void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
1083 const SectionRef Section) {
1084 ArrayRef<uint8_t> Contents;
1085 if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
1086 return;
1087
1088 if (Contents.size() % PDataEntrySize) {
1089 errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
1090 return;
1091 }
1092
1093 for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
1094 if (!dumpProcedureDataEntry(COFF, Section, EI, Contents))
1095 break;
1096}
1097
Rafael Espindolaa20bcb92014-06-13 01:25:41 +00001098std::error_code Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001099 for (const auto &Section : COFF.sections()) {
1100 StringRef SectionName;
Rafael Espindolaa20bcb92014-06-13 01:25:41 +00001101 if (std::error_code EC =
1102 COFF.getSectionName(COFF.getCOFFSection(Section), SectionName))
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001103 return EC;
1104
1105 if (SectionName.startswith(".pdata"))
1106 dumpProcedureData(COFF, Section);
1107 }
Rafael Espindolaa20bcb92014-06-13 01:25:41 +00001108 return std::error_code();
Saleem Abdulrasoolfe3b74e2014-06-04 15:47:15 +00001109}
1110}
1111}
1112}