1//===-- ARMWinEHPrinter.cpp - Windows on ARM EH Data Printer ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// Windows on ARM uses a series of serialised data structures (RuntimeFunction)
10// to create a table of information for unwinding. In order to conserve space,
11// there are two different ways that this data is represented.
12//
13// For functions with canonical forms for the prologue and epilogue, the data
14// can be stored in a "packed" form. In this case, the data is packed into the
15// RuntimeFunction's remaining 30-bits and can fully describe the entire frame.
16//
17// +---------------------------------------+
18// | Function Entry Address |
19// +---------------------------------------+
20// | Packed Form Data |
21// +---------------------------------------+
22//
23// This layout is parsed by Decoder::dumpPackedEntry. No unwind bytecode is
24// associated with such a frame as they can be derived from the provided data.
25// The decoder does not synthesize this data as it is unnecessary for the
26// purposes of validation, with the synthesis being required only by a proper
27// unwinder.
28//
29// For functions that are large or do not match canonical forms, the data is
30// split up into two portions, with the actual data residing in the "exception
31// data" table (.xdata) with a reference to the entry from the "procedure data"
32// (.pdata) entry.
33//
34// The exception data contains information about the frame setup, all of the
35// epilogue scopes (for functions for which there are multiple exit points) and
36// the associated exception handler. Additionally, the entry contains byte-code
37// describing how to unwind the function (c.f. Decoder::decodeOpcodes).
38//
39// +---------------------------------------+
40// | Function Entry Address |
41// +---------------------------------------+
42// | Exception Data Entry Address |
43// +---------------------------------------+
44//
45// This layout is parsed by Decoder::dumpUnpackedEntry. Such an entry must
46// first resolve the exception data entry address. This structure
47// (ExceptionDataRecord) has a variable sized header
48// (c.f. ARM::WinEH::HeaderWords) and encodes most of the same information as
49// the packed form. However, because this information is insufficient to
50// synthesize the unwinding, there are associated unwinding bytecode which make
51// up the bulk of the Decoder.
52//
53// The decoder itself is table-driven, using the first byte to determine the
54// opcode and dispatching to the associated printing routine. The bytecode
55// itself is a variable length instruction encoding that can fully describe the
56// state of the stack and the necessary operations for unwinding to the
57// beginning of the frame.
58//
59// The byte-code maintains a 1-1 instruction mapping, indicating both the width
60// of the instruction (Thumb2 instructions are variable length, 16 or 32 bits
61// wide) allowing the program to unwind from any point in the prologue, body, or
62// epilogue of the function.
63
64#include "ARMWinEHPrinter.h"
65#include "llvm/ADT/StringExtras.h"
66#include "llvm/Support/ARMWinEH.h"
67#include "llvm/Support/Format.h"
68
69using namespace llvm;
70using namespace llvm::object;
71using namespace llvm::support;
72
73namespace llvm {
74raw_ostream &operator<<(raw_ostream &OS, const ARM::WinEH::ReturnType &RT) {
75 switch (RT) {
76 case ARM::WinEH::ReturnType::RT_POP:
77 OS << "pop {pc}";
78 break;
79 case ARM::WinEH::ReturnType::RT_B:
80 OS << "bx <reg>";
81 break;
82 case ARM::WinEH::ReturnType::RT_BW:
83 OS << "b.w <target>";
84 break;
85 case ARM::WinEH::ReturnType::RT_NoEpilogue:
86 OS << "(no epilogue)";
87 break;
88 }
89 return OS;
90}
91}
92
93static std::string formatSymbol(StringRef Name, uint64_t Address,
94 uint64_t Offset = 0) {
95 std::string Buffer;
96 raw_string_ostream OS(Buffer);
97
98 if (!Name.empty())
99 OS << Name << " ";
100
101 if (Offset)
102 OS << format(Fmt: "+0x%" PRIX64 " (0x%" PRIX64 ")", Vals: Offset, Vals: Address);
103 else if (!Name.empty())
104 OS << format(Fmt: "(0x%" PRIX64 ")", Vals: Address);
105 else
106 OS << format(Fmt: "0x%" PRIX64, Vals: Address);
107
108 return Buffer;
109}
110
111namespace llvm {
112namespace ARM {
113namespace WinEH {
114const size_t Decoder::PDataEntrySize = sizeof(RuntimeFunction);
115
116// TODO name the uops more appropriately
117const Decoder::RingEntry Decoder::Ring[] = {
118 { .Mask: 0x80, .Value: 0x00, .Length: 1, .Routine: &Decoder::opcode_0xxxxxxx }, // UOP_STACK_FREE (16-bit)
119 { .Mask: 0xc0, .Value: 0x80, .Length: 2, .Routine: &Decoder::opcode_10Lxxxxx }, // UOP_POP (32-bit)
120 { .Mask: 0xf0, .Value: 0xc0, .Length: 1, .Routine: &Decoder::opcode_1100xxxx }, // UOP_STACK_SAVE (16-bit)
121 { .Mask: 0xf8, .Value: 0xd0, .Length: 1, .Routine: &Decoder::opcode_11010Lxx }, // UOP_POP (16-bit)
122 { .Mask: 0xf8, .Value: 0xd8, .Length: 1, .Routine: &Decoder::opcode_11011Lxx }, // UOP_POP (32-bit)
123 { .Mask: 0xf8, .Value: 0xe0, .Length: 1, .Routine: &Decoder::opcode_11100xxx }, // UOP_VPOP (32-bit)
124 { .Mask: 0xfc, .Value: 0xe8, .Length: 2, .Routine: &Decoder::opcode_111010xx }, // UOP_STACK_FREE (32-bit)
125 { .Mask: 0xfe, .Value: 0xec, .Length: 2, .Routine: &Decoder::opcode_1110110L }, // UOP_POP (16-bit)
126 { .Mask: 0xff, .Value: 0xee, .Length: 2, .Routine: &Decoder::opcode_11101110 }, // UOP_MICROSOFT_SPECIFIC (16-bit)
127 // UOP_PUSH_MACHINE_FRAME
128 // UOP_PUSH_CONTEXT
129 // UOP_PUSH_TRAP_FRAME
130 // UOP_REDZONE_RESTORE_LR
131 { .Mask: 0xff, .Value: 0xef, .Length: 2, .Routine: &Decoder::opcode_11101111 }, // UOP_LDRPC_POSTINC (32-bit)
132 { .Mask: 0xff, .Value: 0xf5, .Length: 2, .Routine: &Decoder::opcode_11110101 }, // UOP_VPOP (32-bit)
133 { .Mask: 0xff, .Value: 0xf6, .Length: 2, .Routine: &Decoder::opcode_11110110 }, // UOP_VPOP (32-bit)
134 { .Mask: 0xff, .Value: 0xf7, .Length: 3, .Routine: &Decoder::opcode_11110111 }, // UOP_STACK_RESTORE (16-bit)
135 { .Mask: 0xff, .Value: 0xf8, .Length: 4, .Routine: &Decoder::opcode_11111000 }, // UOP_STACK_RESTORE (16-bit)
136 { .Mask: 0xff, .Value: 0xf9, .Length: 3, .Routine: &Decoder::opcode_11111001 }, // UOP_STACK_RESTORE (32-bit)
137 { .Mask: 0xff, .Value: 0xfa, .Length: 4, .Routine: &Decoder::opcode_11111010 }, // UOP_STACK_RESTORE (32-bit)
138 { .Mask: 0xff, .Value: 0xfb, .Length: 1, .Routine: &Decoder::opcode_11111011 }, // UOP_NOP (16-bit)
139 { .Mask: 0xff, .Value: 0xfc, .Length: 1, .Routine: &Decoder::opcode_11111100 }, // UOP_NOP (32-bit)
140 { .Mask: 0xff, .Value: 0xfd, .Length: 1, .Routine: &Decoder::opcode_11111101 }, // UOP_NOP (16-bit) / END
141 { .Mask: 0xff, .Value: 0xfe, .Length: 1, .Routine: &Decoder::opcode_11111110 }, // UOP_NOP (32-bit) / END
142 { .Mask: 0xff, .Value: 0xff, .Length: 1, .Routine: &Decoder::opcode_11111111 }, // UOP_END
143};
144
145// Unwind opcodes for ARM64.
146// https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
147const Decoder::RingEntry Decoder::Ring64[] = {
148 {.Mask: 0xe0, .Value: 0x00, .Length: 1, .Routine: &Decoder::opcode_alloc_s},
149 {.Mask: 0xe0, .Value: 0x20, .Length: 1, .Routine: &Decoder::opcode_save_r19r20_x},
150 {.Mask: 0xc0, .Value: 0x40, .Length: 1, .Routine: &Decoder::opcode_save_fplr},
151 {.Mask: 0xc0, .Value: 0x80, .Length: 1, .Routine: &Decoder::opcode_save_fplr_x},
152 {.Mask: 0xf8, .Value: 0xc0, .Length: 2, .Routine: &Decoder::opcode_alloc_m},
153 {.Mask: 0xfc, .Value: 0xc8, .Length: 2, .Routine: &Decoder::opcode_save_regp},
154 {.Mask: 0xfc, .Value: 0xcc, .Length: 2, .Routine: &Decoder::opcode_save_regp_x},
155 {.Mask: 0xfc, .Value: 0xd0, .Length: 2, .Routine: &Decoder::opcode_save_reg},
156 {.Mask: 0xfe, .Value: 0xd4, .Length: 2, .Routine: &Decoder::opcode_save_reg_x},
157 {.Mask: 0xfe, .Value: 0xd6, .Length: 2, .Routine: &Decoder::opcode_save_lrpair},
158 {.Mask: 0xfe, .Value: 0xd8, .Length: 2, .Routine: &Decoder::opcode_save_fregp},
159 {.Mask: 0xfe, .Value: 0xda, .Length: 2, .Routine: &Decoder::opcode_save_fregp_x},
160 {.Mask: 0xfe, .Value: 0xdc, .Length: 2, .Routine: &Decoder::opcode_save_freg},
161 {.Mask: 0xff, .Value: 0xde, .Length: 2, .Routine: &Decoder::opcode_save_freg_x},
162 {.Mask: 0xff, .Value: 0xdf, .Length: 2, .Routine: &Decoder::opcode_alloc_z},
163 {.Mask: 0xff, .Value: 0xe0, .Length: 4, .Routine: &Decoder::opcode_alloc_l},
164 {.Mask: 0xff, .Value: 0xe1, .Length: 1, .Routine: &Decoder::opcode_setfp},
165 {.Mask: 0xff, .Value: 0xe2, .Length: 2, .Routine: &Decoder::opcode_addfp},
166 {.Mask: 0xff, .Value: 0xe3, .Length: 1, .Routine: &Decoder::opcode_nop},
167 {.Mask: 0xff, .Value: 0xe4, .Length: 1, .Routine: &Decoder::opcode_end},
168 {.Mask: 0xff, .Value: 0xe5, .Length: 1, .Routine: &Decoder::opcode_end_c},
169 {.Mask: 0xff, .Value: 0xe6, .Length: 1, .Routine: &Decoder::opcode_save_next},
170 {.Mask: 0xff, .Value: 0xe7, .Length: 3, .Routine: &Decoder::opcode_e7},
171 {.Mask: 0xff, .Value: 0xe8, .Length: 1, .Routine: &Decoder::opcode_trap_frame},
172 {.Mask: 0xff, .Value: 0xe9, .Length: 1, .Routine: &Decoder::opcode_machine_frame},
173 {.Mask: 0xff, .Value: 0xea, .Length: 1, .Routine: &Decoder::opcode_context},
174 {.Mask: 0xff, .Value: 0xeb, .Length: 1, .Routine: &Decoder::opcode_ec_context},
175 {.Mask: 0xff, .Value: 0xec, .Length: 1, .Routine: &Decoder::opcode_clear_unwound_to_call},
176 {.Mask: 0xff, .Value: 0xfc, .Length: 1, .Routine: &Decoder::opcode_pac_sign_lr},
177};
178
179static void printRange(raw_ostream &OS, ListSeparator &LS, unsigned First,
180 unsigned Last, char Letter) {
181 if (First == Last)
182 OS << LS << Letter << First;
183 else
184 OS << LS << Letter << First << "-" << Letter << Last;
185}
186
187static void printRange(raw_ostream &OS, uint32_t Mask, ListSeparator &LS,
188 unsigned Start, unsigned End, char Letter) {
189 int First = -1;
190 for (unsigned RI = Start; RI <= End; ++RI) {
191 if (Mask & (1 << RI)) {
192 if (First < 0)
193 First = RI;
194 } else {
195 if (First >= 0) {
196 printRange(OS, LS, First, Last: RI - 1, Letter);
197 First = -1;
198 }
199 }
200 }
201 if (First >= 0)
202 printRange(OS, LS, First, Last: End, Letter);
203}
204
205void Decoder::printGPRMask(uint16_t GPRMask) {
206 OS << '{';
207 ListSeparator LS;
208 printRange(OS, Mask: GPRMask, LS, Start: 0, End: 12, Letter: 'r');
209 if (GPRMask & (1 << 14))
210 OS << LS << "lr";
211 if (GPRMask & (1 << 15))
212 OS << LS << "pc";
213 OS << '}';
214}
215
216void Decoder::printVFPMask(uint32_t VFPMask) {
217 OS << '{';
218 ListSeparator LS;
219 printRange(OS, Mask: VFPMask, LS, Start: 0, End: 31, Letter: 'd');
220 OS << '}';
221}
222
223ErrorOr<object::SectionRef>
224Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
225 for (const auto &Section : COFF.sections()) {
226 uint64_t Address = Section.getAddress();
227 uint64_t Size = Section.getSize();
228
229 if (VA >= Address && (VA - Address) <= Size)
230 return Section;
231 }
232 return inconvertibleErrorCode();
233}
234
235ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
236 uint64_t VA, bool FunctionOnly) {
237 for (const auto &Symbol : COFF.symbols()) {
238 Expected<SymbolRef::Type> Type = Symbol.getType();
239 if (!Type)
240 return errorToErrorCode(Err: Type.takeError());
241 if (FunctionOnly && *Type != SymbolRef::ST_Function)
242 continue;
243
244 Expected<uint64_t> Address = Symbol.getAddress();
245 if (!Address)
246 return errorToErrorCode(Err: Address.takeError());
247 if (*Address == VA)
248 return Symbol;
249 }
250 return inconvertibleErrorCode();
251}
252
253ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
254 const SectionRef &Section,
255 uint64_t Offset) {
256 for (const auto &Relocation : Section.relocations()) {
257 uint64_t RelocationOffset = Relocation.getOffset();
258 if (RelocationOffset == Offset)
259 return *Relocation.getSymbol();
260 }
261 return inconvertibleErrorCode();
262}
263
264SymbolRef Decoder::getPreferredSymbol(const COFFObjectFile &COFF, SymbolRef Sym,
265 uint64_t &SymbolOffset) {
266 // The symbol resolved by getRelocatedSymbol can be any internal
267 // nondescriptive symbol; try to resolve a more descriptive one.
268 COFFSymbolRef CoffSym = COFF.getCOFFSymbol(Symbol: Sym);
269 if (CoffSym.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL &&
270 CoffSym.getSectionDefinition() == nullptr)
271 return Sym;
272 for (const auto &S : COFF.symbols()) {
273 COFFSymbolRef CS = COFF.getCOFFSymbol(Symbol: S);
274 if (CS.getSectionNumber() == CoffSym.getSectionNumber() &&
275 CS.getValue() <= CoffSym.getValue() + SymbolOffset &&
276 CS.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL &&
277 CS.getSectionDefinition() == nullptr) {
278 uint32_t Offset = CoffSym.getValue() + SymbolOffset - CS.getValue();
279 if (Offset <= SymbolOffset) {
280 SymbolOffset = Offset;
281 Sym = S;
282 CoffSym = CS;
283 if (CS.isExternal() && SymbolOffset == 0)
284 return Sym;
285 }
286 }
287 }
288 return Sym;
289}
290
291ErrorOr<SymbolRef> Decoder::getSymbolForLocation(
292 const COFFObjectFile &COFF, const SectionRef &Section,
293 uint64_t OffsetInSection, uint64_t ImmediateOffset, uint64_t &SymbolAddress,
294 uint64_t &SymbolOffset, bool FunctionOnly) {
295 // Try to locate a relocation that points at the offset in the section
296 ErrorOr<SymbolRef> SymOrErr =
297 getRelocatedSymbol(COFF, Section, Offset: OffsetInSection);
298 if (SymOrErr) {
299 // We found a relocation symbol; the immediate offset needs to be added
300 // to the symbol address.
301 SymbolOffset = ImmediateOffset;
302
303 Expected<uint64_t> AddressOrErr = SymOrErr->getAddress();
304 if (!AddressOrErr) {
305 std::string Buf;
306 llvm::raw_string_ostream OS(Buf);
307 logAllUnhandledErrors(E: AddressOrErr.takeError(), OS);
308 reportFatalUsageError(reason: Twine(Buf));
309 }
310 // We apply SymbolOffset here directly. We return it separately to allow
311 // the caller to print it as an offset on the symbol name.
312 SymbolAddress = *AddressOrErr + SymbolOffset;
313
314 if (FunctionOnly) // Resolve label/section symbols into function names.
315 SymOrErr = getPreferredSymbol(COFF, Sym: *SymOrErr, SymbolOffset);
316 } else {
317 // No matching relocation found; operating on a linked image. Try to
318 // find a descriptive symbol if possible. The immediate offset contains
319 // the image relative address, and we shouldn't add any offset to the
320 // symbol.
321 SymbolAddress = COFF.getImageBase() + ImmediateOffset;
322 SymbolOffset = 0;
323 SymOrErr = getSymbol(COFF, VA: SymbolAddress, FunctionOnly);
324 }
325 return SymOrErr;
326}
327
328bool Decoder::opcode_0xxxxxxx(const uint8_t *OC, unsigned &Offset,
329 unsigned Length, bool Prologue) {
330 uint8_t Imm = OC[Offset] & 0x7f;
331 SW.startLine() << format(Fmt: "0x%02x ; %s sp, #(%u * 4)\n",
332 Vals: OC[Offset],
333 Vals: static_cast<const char *>(Prologue ? "sub" : "add"),
334 Vals: Imm);
335 ++Offset;
336 return false;
337}
338
339bool Decoder::opcode_10Lxxxxx(const uint8_t *OC, unsigned &Offset,
340 unsigned Length, bool Prologue) {
341 unsigned Link = (OC[Offset] & 0x20) >> 5;
342 uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
343 | ((OC[Offset + 0] & 0x1f) << 8)
344 | ((OC[Offset + 1] & 0xff) << 0);
345 assert((~RegisterMask & (1 << 13)) && "sp must not be set");
346 assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
347
348 SW.startLine() << format(Fmt: "0x%02x 0x%02x ; %s.w ",
349 Vals: OC[Offset + 0], Vals: OC[Offset + 1],
350 Vals: Prologue ? "push" : "pop");
351 printGPRMask(GPRMask: RegisterMask);
352 OS << '\n';
353
354 Offset += 2;
355 return false;
356}
357
358bool Decoder::opcode_1100xxxx(const uint8_t *OC, unsigned &Offset,
359 unsigned Length, bool Prologue) {
360 if (Prologue)
361 SW.startLine() << format(Fmt: "0x%02x ; mov r%u, sp\n",
362 Vals: OC[Offset], Vals: OC[Offset] & 0xf);
363 else
364 SW.startLine() << format(Fmt: "0x%02x ; mov sp, r%u\n",
365 Vals: OC[Offset], Vals: OC[Offset] & 0xf);
366 ++Offset;
367 return false;
368}
369
370bool Decoder::opcode_11010Lxx(const uint8_t *OC, unsigned &Offset,
371 unsigned Length, bool Prologue) {
372 unsigned Link = (OC[Offset] & 0x4) >> 2;
373 unsigned Count = (OC[Offset] & 0x3);
374
375 uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
376 | (((1 << (Count + 1)) - 1) << 4);
377
378 SW.startLine() << format(Fmt: "0x%02x ; %s ", Vals: OC[Offset],
379 Vals: Prologue ? "push" : "pop");
380 printGPRMask(GPRMask);
381 OS << '\n';
382
383 ++Offset;
384 return false;
385}
386
387bool Decoder::opcode_11011Lxx(const uint8_t *OC, unsigned &Offset,
388 unsigned Length, bool Prologue) {
389 unsigned Link = (OC[Offset] & 0x4) >> 2;
390 unsigned Count = (OC[Offset] & 0x3) + 4;
391
392 uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
393 | (((1 << (Count + 1)) - 1) << 4);
394
395 SW.startLine() << format(Fmt: "0x%02x ; %s.w ", Vals: OC[Offset],
396 Vals: Prologue ? "push" : "pop");
397 printGPRMask(GPRMask);
398 OS << '\n';
399
400 ++Offset;
401 return false;
402}
403
404bool Decoder::opcode_11100xxx(const uint8_t *OC, unsigned &Offset,
405 unsigned Length, bool Prologue) {
406 unsigned High = (OC[Offset] & 0x7);
407 uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
408
409 SW.startLine() << format(Fmt: "0x%02x ; %s ", Vals: OC[Offset],
410 Vals: Prologue ? "vpush" : "vpop");
411 printVFPMask(VFPMask);
412 OS << '\n';
413
414 ++Offset;
415 return false;
416}
417
418bool Decoder::opcode_111010xx(const uint8_t *OC, unsigned &Offset,
419 unsigned Length, bool Prologue) {
420 uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
421
422 SW.startLine() << format(Fmt: "0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
423 Vals: OC[Offset + 0], Vals: OC[Offset + 1],
424 Vals: static_cast<const char *>(Prologue ? "sub" : "add"),
425 Vals: Imm);
426
427 Offset += 2;
428 return false;
429}
430
431bool Decoder::opcode_1110110L(const uint8_t *OC, unsigned &Offset,
432 unsigned Length, bool Prologue) {
433 uint16_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
434 | ((OC[Offset + 1] & 0xff) << 0);
435
436 SW.startLine() << format(Fmt: "0x%02x 0x%02x ; %s ", Vals: OC[Offset + 0],
437 Vals: OC[Offset + 1], Vals: Prologue ? "push" : "pop");
438 printGPRMask(GPRMask);
439 OS << '\n';
440
441 Offset += 2;
442 return false;
443}
444
445bool Decoder::opcode_11101110(const uint8_t *OC, unsigned &Offset,
446 unsigned Length, bool Prologue) {
447 assert(!Prologue && "may not be used in prologue");
448
449 if (OC[Offset + 1] & 0xf0)
450 SW.startLine() << format(Fmt: "0x%02x 0x%02x ; reserved\n",
451 Vals: OC[Offset + 0], Vals: OC[Offset + 1]);
452 else
453 SW.startLine()
454 << format(Fmt: "0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
455 Vals: OC[Offset + 0], Vals: OC[Offset + 1], Vals: OC[Offset + 1] & 0x0f);
456
457 Offset += 2;
458 return false;
459}
460
461bool Decoder::opcode_11101111(const uint8_t *OC, unsigned &Offset,
462 unsigned Length, bool Prologue) {
463 if (OC[Offset + 1] & 0xf0)
464 SW.startLine() << format(Fmt: "0x%02x 0x%02x ; reserved\n",
465 Vals: OC[Offset + 0], Vals: OC[Offset + 1]);
466 else if (Prologue)
467 SW.startLine()
468 << format(Fmt: "0x%02x 0x%02x ; str.w lr, [sp, #-%u]!\n",
469 Vals: OC[Offset + 0], Vals: OC[Offset + 1], Vals: OC[Offset + 1] << 2);
470 else
471 SW.startLine()
472 << format(Fmt: "0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
473 Vals: OC[Offset + 0], Vals: OC[Offset + 1], Vals: OC[Offset + 1] << 2);
474
475 Offset += 2;
476 return false;
477}
478
479bool Decoder::opcode_11110101(const uint8_t *OC, unsigned &Offset,
480 unsigned Length, bool Prologue) {
481 unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
482 unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
483 uint32_t VFPMask = ((1 << (End + 1 - Start)) - 1) << Start;
484
485 SW.startLine() << format(Fmt: "0x%02x 0x%02x ; %s ", Vals: OC[Offset + 0],
486 Vals: OC[Offset + 1], Vals: Prologue ? "vpush" : "vpop");
487 printVFPMask(VFPMask);
488 OS << '\n';
489
490 Offset += 2;
491 return false;
492}
493
494bool Decoder::opcode_11110110(const uint8_t *OC, unsigned &Offset,
495 unsigned Length, bool Prologue) {
496 unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
497 unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
498 uint32_t VFPMask = ((1 << (End + 1 - Start)) - 1) << (16 + Start);
499
500 SW.startLine() << format(Fmt: "0x%02x 0x%02x ; %s ", Vals: OC[Offset + 0],
501 Vals: OC[Offset + 1], Vals: Prologue ? "vpush" : "vpop");
502 printVFPMask(VFPMask);
503 OS << '\n';
504
505 Offset += 2;
506 return false;
507}
508
509bool Decoder::opcode_11110111(const uint8_t *OC, unsigned &Offset,
510 unsigned Length, bool Prologue) {
511 uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
512
513 SW.startLine() << format(Fmt: "0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
514 Vals: OC[Offset + 0], Vals: OC[Offset + 1], Vals: OC[Offset + 2],
515 Vals: static_cast<const char *>(Prologue ? "sub" : "add"),
516 Vals: Imm);
517
518 Offset += 3;
519 return false;
520}
521
522bool Decoder::opcode_11111000(const uint8_t *OC, unsigned &Offset,
523 unsigned Length, bool Prologue) {
524 uint32_t Imm = (OC[Offset + 1] << 16)
525 | (OC[Offset + 2] << 8)
526 | (OC[Offset + 3] << 0);
527
528 SW.startLine()
529 << format(Fmt: "0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
530 Vals: OC[Offset + 0], Vals: OC[Offset + 1], Vals: OC[Offset + 2], Vals: OC[Offset + 3],
531 Vals: static_cast<const char *>(Prologue ? "sub" : "add"), Vals: Imm);
532
533 Offset += 4;
534 return false;
535}
536
537bool Decoder::opcode_11111001(const uint8_t *OC, unsigned &Offset,
538 unsigned Length, bool Prologue) {
539 uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
540
541 SW.startLine()
542 << format(Fmt: "0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
543 Vals: OC[Offset + 0], Vals: OC[Offset + 1], Vals: OC[Offset + 2],
544 Vals: static_cast<const char *>(Prologue ? "sub" : "add"), Vals: Imm);
545
546 Offset += 3;
547 return false;
548}
549
550bool Decoder::opcode_11111010(const uint8_t *OC, unsigned &Offset,
551 unsigned Length, bool Prologue) {
552 uint32_t Imm = (OC[Offset + 1] << 16)
553 | (OC[Offset + 2] << 8)
554 | (OC[Offset + 3] << 0);
555
556 SW.startLine()
557 << format(Fmt: "0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
558 Vals: OC[Offset + 0], Vals: OC[Offset + 1], Vals: OC[Offset + 2], Vals: OC[Offset + 3],
559 Vals: static_cast<const char *>(Prologue ? "sub" : "add"), Vals: Imm);
560
561 Offset += 4;
562 return false;
563}
564
565bool Decoder::opcode_11111011(const uint8_t *OC, unsigned &Offset,
566 unsigned Length, bool Prologue) {
567 SW.startLine() << format(Fmt: "0x%02x ; nop\n", Vals: OC[Offset]);
568 ++Offset;
569 return false;
570}
571
572bool Decoder::opcode_11111100(const uint8_t *OC, unsigned &Offset,
573 unsigned Length, bool Prologue) {
574 SW.startLine() << format(Fmt: "0x%02x ; nop.w\n", Vals: OC[Offset]);
575 ++Offset;
576 return false;
577}
578
579bool Decoder::opcode_11111101(const uint8_t *OC, unsigned &Offset,
580 unsigned Length, bool Prologue) {
581 SW.startLine() << format(Fmt: "0x%02x ; bx <reg>\n", Vals: OC[Offset]);
582 ++Offset;
583 return true;
584}
585
586bool Decoder::opcode_11111110(const uint8_t *OC, unsigned &Offset,
587 unsigned Length, bool Prologue) {
588 SW.startLine() << format(Fmt: "0x%02x ; b.w <target>\n", Vals: OC[Offset]);
589 ++Offset;
590 return true;
591}
592
593bool Decoder::opcode_11111111(const uint8_t *OC, unsigned &Offset,
594 unsigned Length, bool Prologue) {
595 ++Offset;
596 return true;
597}
598
599// ARM64 unwind codes start here.
600bool Decoder::opcode_alloc_s(const uint8_t *OC, unsigned &Offset,
601 unsigned Length, bool Prologue) {
602 uint32_t NumBytes = (OC[Offset] & 0x1F) << 4;
603 SW.startLine() << format(Fmt: "0x%02x ; %s sp, #%u\n", Vals: OC[Offset],
604 Vals: static_cast<const char *>(Prologue ? "sub" : "add"),
605 Vals: NumBytes);
606 ++Offset;
607 return false;
608}
609
610bool Decoder::opcode_save_r19r20_x(const uint8_t *OC, unsigned &Offset,
611 unsigned Length, bool Prologue) {
612 uint32_t Off = (OC[Offset] & 0x1F) << 3;
613 if (Prologue)
614 SW.startLine() << format(
615 Fmt: "0x%02x ; stp x19, x20, [sp, #-%u]!\n", Vals: OC[Offset], Vals: Off);
616 else
617 SW.startLine() << format(
618 Fmt: "0x%02x ; ldp x19, x20, [sp], #%u\n", Vals: OC[Offset], Vals: Off);
619 ++Offset;
620 return false;
621}
622
623bool Decoder::opcode_save_fplr(const uint8_t *OC, unsigned &Offset,
624 unsigned Length, bool Prologue) {
625 uint32_t Off = (OC[Offset] & 0x3F) << 3;
626 SW.startLine() << format(
627 Fmt: "0x%02x ; %s x29, x30, [sp, #%u]\n", Vals: OC[Offset],
628 Vals: static_cast<const char *>(Prologue ? "stp" : "ldp"), Vals: Off);
629 ++Offset;
630 return false;
631}
632
633bool Decoder::opcode_save_fplr_x(const uint8_t *OC, unsigned &Offset,
634 unsigned Length, bool Prologue) {
635 uint32_t Off = ((OC[Offset] & 0x3F) + 1) << 3;
636 if (Prologue)
637 SW.startLine() << format(
638 Fmt: "0x%02x ; stp x29, x30, [sp, #-%u]!\n", Vals: OC[Offset], Vals: Off);
639 else
640 SW.startLine() << format(
641 Fmt: "0x%02x ; ldp x29, x30, [sp], #%u\n", Vals: OC[Offset], Vals: Off);
642 ++Offset;
643 return false;
644}
645
646bool Decoder::opcode_alloc_m(const uint8_t *OC, unsigned &Offset,
647 unsigned Length, bool Prologue) {
648 uint32_t NumBytes = ((OC[Offset] & 0x07) << 8);
649 NumBytes |= (OC[Offset + 1] & 0xFF);
650 NumBytes <<= 4;
651 SW.startLine() << format(Fmt: "0x%02x%02x ; %s sp, #%u\n",
652 Vals: OC[Offset], Vals: OC[Offset + 1],
653 Vals: static_cast<const char *>(Prologue ? "sub" : "add"),
654 Vals: NumBytes);
655 Offset += 2;
656 return false;
657}
658
659bool Decoder::opcode_save_regp(const uint8_t *OC, unsigned &Offset,
660 unsigned Length, bool Prologue) {
661 uint32_t Reg = ((OC[Offset] & 0x03) << 8);
662 Reg |= (OC[Offset + 1] & 0xC0);
663 Reg >>= 6;
664 Reg += 19;
665 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
666 SW.startLine() << format(
667 Fmt: "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
668 Vals: OC[Offset], Vals: OC[Offset + 1],
669 Vals: static_cast<const char *>(Prologue ? "stp" : "ldp"), Vals: Reg, Vals: Reg + 1, Vals: Off);
670 Offset += 2;
671 return false;
672}
673
674bool Decoder::opcode_save_regp_x(const uint8_t *OC, unsigned &Offset,
675 unsigned Length, bool Prologue) {
676 uint32_t Reg = ((OC[Offset] & 0x03) << 8);
677 Reg |= (OC[Offset + 1] & 0xC0);
678 Reg >>= 6;
679 Reg += 19;
680 uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
681 if (Prologue)
682 SW.startLine() << format(
683 Fmt: "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
684 Vals: OC[Offset], Vals: OC[Offset + 1], Vals: Reg,
685 Vals: Reg + 1, Vals: Off);
686 else
687 SW.startLine() << format(
688 Fmt: "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
689 Vals: OC[Offset], Vals: OC[Offset + 1], Vals: Reg,
690 Vals: Reg + 1, Vals: Off);
691 Offset += 2;
692 return false;
693}
694
695bool Decoder::opcode_save_reg(const uint8_t *OC, unsigned &Offset,
696 unsigned Length, bool Prologue) {
697 uint32_t Reg = (OC[Offset] & 0x03) << 8;
698 Reg |= (OC[Offset + 1] & 0xC0);
699 Reg >>= 6;
700 Reg += 19;
701 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
702 SW.startLine() << format(Fmt: "0x%02x%02x ; %s x%u, [sp, #%u]\n",
703 Vals: OC[Offset], Vals: OC[Offset + 1],
704 Vals: static_cast<const char *>(Prologue ? "str" : "ldr"),
705 Vals: Reg, Vals: Off);
706 Offset += 2;
707 return false;
708}
709
710bool Decoder::opcode_save_reg_x(const uint8_t *OC, unsigned &Offset,
711 unsigned Length, bool Prologue) {
712 uint32_t Reg = (OC[Offset] & 0x01) << 8;
713 Reg |= (OC[Offset + 1] & 0xE0);
714 Reg >>= 5;
715 Reg += 19;
716 uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
717 if (Prologue)
718 SW.startLine() << format(Fmt: "0x%02x%02x ; str x%u, [sp, #-%u]!\n",
719 Vals: OC[Offset], Vals: OC[Offset + 1], Vals: Reg, Vals: Off);
720 else
721 SW.startLine() << format(Fmt: "0x%02x%02x ; ldr x%u, [sp], #%u\n",
722 Vals: OC[Offset], Vals: OC[Offset + 1], Vals: Reg, Vals: Off);
723 Offset += 2;
724 return false;
725}
726
727bool Decoder::opcode_save_lrpair(const uint8_t *OC, unsigned &Offset,
728 unsigned Length, bool Prologue) {
729 uint32_t Reg = (OC[Offset] & 0x01) << 8;
730 Reg |= (OC[Offset + 1] & 0xC0);
731 Reg >>= 6;
732 Reg *= 2;
733 Reg += 19;
734 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
735 SW.startLine() << format(Fmt: "0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
736 Vals: OC[Offset], Vals: OC[Offset + 1],
737 Vals: static_cast<const char *>(Prologue ? "stp" : "ldp"),
738 Vals: Reg, Vals: Off);
739 Offset += 2;
740 return false;
741}
742
743bool Decoder::opcode_save_fregp(const uint8_t *OC, unsigned &Offset,
744 unsigned Length, bool Prologue) {
745 uint32_t Reg = (OC[Offset] & 0x01) << 8;
746 Reg |= (OC[Offset + 1] & 0xC0);
747 Reg >>= 6;
748 Reg += 8;
749 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
750 SW.startLine() << format(Fmt: "0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
751 Vals: OC[Offset], Vals: OC[Offset + 1],
752 Vals: static_cast<const char *>(Prologue ? "stp" : "ldp"),
753 Vals: Reg, Vals: Reg + 1, Vals: Off);
754 Offset += 2;
755 return false;
756}
757
758bool Decoder::opcode_save_fregp_x(const uint8_t *OC, unsigned &Offset,
759 unsigned Length, bool Prologue) {
760 uint32_t Reg = (OC[Offset] & 0x01) << 8;
761 Reg |= (OC[Offset + 1] & 0xC0);
762 Reg >>= 6;
763 Reg += 8;
764 uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
765 if (Prologue)
766 SW.startLine() << format(
767 Fmt: "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", Vals: OC[Offset],
768 Vals: OC[Offset + 1], Vals: Reg, Vals: Reg + 1, Vals: Off);
769 else
770 SW.startLine() << format(
771 Fmt: "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", Vals: OC[Offset],
772 Vals: OC[Offset + 1], Vals: Reg, Vals: Reg + 1, Vals: Off);
773 Offset += 2;
774 return false;
775}
776
777bool Decoder::opcode_save_freg(const uint8_t *OC, unsigned &Offset,
778 unsigned Length, bool Prologue) {
779 uint32_t Reg = (OC[Offset] & 0x01) << 8;
780 Reg |= (OC[Offset + 1] & 0xC0);
781 Reg >>= 6;
782 Reg += 8;
783 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
784 SW.startLine() << format(Fmt: "0x%02x%02x ; %s d%u, [sp, #%u]\n",
785 Vals: OC[Offset], Vals: OC[Offset + 1],
786 Vals: static_cast<const char *>(Prologue ? "str" : "ldr"),
787 Vals: Reg, Vals: Off);
788 Offset += 2;
789 return false;
790}
791
792bool Decoder::opcode_save_freg_x(const uint8_t *OC, unsigned &Offset,
793 unsigned Length, bool Prologue) {
794 uint32_t Reg = ((OC[Offset + 1] & 0xE0) >> 5) + 8;
795 uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
796 if (Prologue)
797 SW.startLine() << format(
798 Fmt: "0x%02x%02x ; str d%u, [sp, #-%u]!\n", Vals: OC[Offset],
799 Vals: OC[Offset + 1], Vals: Reg, Vals: Off);
800 else
801 SW.startLine() << format(
802 Fmt: "0x%02x%02x ; ldr d%u, [sp], #%u\n", Vals: OC[Offset],
803 Vals: OC[Offset + 1], Vals: Reg, Vals: Off);
804 Offset += 2;
805 return false;
806}
807
808bool Decoder::opcode_alloc_z(const uint8_t *OC, unsigned &Offset,
809 unsigned Length, bool Prologue) {
810 unsigned Off = OC[Offset + 1];
811 SW.startLine() << format(Fmt: "0x%02x%02x ; addvl sp, #%d\n",
812 Vals: OC[Offset], Vals: OC[Offset + 1],
813 Vals: Prologue ? -(int)Off : (int)Off);
814 Offset += 2;
815 return false;
816}
817
818bool Decoder::opcode_alloc_l(const uint8_t *OC, unsigned &Offset,
819 unsigned Length, bool Prologue) {
820 unsigned Off =
821 (OC[Offset + 1] << 16) | (OC[Offset + 2] << 8) | (OC[Offset + 3] << 0);
822 Off <<= 4;
823 SW.startLine() << format(
824 Fmt: "0x%02x%02x%02x%02x ; %s sp, #%u\n", Vals: OC[Offset], Vals: OC[Offset + 1],
825 Vals: OC[Offset + 2], Vals: OC[Offset + 3],
826 Vals: static_cast<const char *>(Prologue ? "sub" : "add"), Vals: Off);
827 Offset += 4;
828 return false;
829}
830
831bool Decoder::opcode_setfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
832 bool Prologue) {
833 SW.startLine() << format(Fmt: "0x%02x ; mov %s, %s\n", Vals: OC[Offset],
834 Vals: static_cast<const char *>(Prologue ? "fp" : "sp"),
835 Vals: static_cast<const char *>(Prologue ? "sp" : "fp"));
836 ++Offset;
837 return false;
838}
839
840bool Decoder::opcode_addfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
841 bool Prologue) {
842 unsigned NumBytes = OC[Offset + 1] << 3;
843 SW.startLine() << format(
844 Fmt: "0x%02x%02x ; %s %s, %s, #%u\n", Vals: OC[Offset], Vals: OC[Offset + 1],
845 Vals: static_cast<const char *>(Prologue ? "add" : "sub"),
846 Vals: static_cast<const char *>(Prologue ? "fp" : "sp"),
847 Vals: static_cast<const char *>(Prologue ? "sp" : "fp"), Vals: NumBytes);
848 Offset += 2;
849 return false;
850}
851
852bool Decoder::opcode_nop(const uint8_t *OC, unsigned &Offset, unsigned Length,
853 bool Prologue) {
854 SW.startLine() << format(Fmt: "0x%02x ; nop\n", Vals: OC[Offset]);
855 ++Offset;
856 return false;
857}
858
859bool Decoder::opcode_end(const uint8_t *OC, unsigned &Offset, unsigned Length,
860 bool Prologue) {
861 SW.startLine() << format(Fmt: "0x%02x ; end\n", Vals: OC[Offset]);
862 ++Offset;
863 return true;
864}
865
866bool Decoder::opcode_end_c(const uint8_t *OC, unsigned &Offset, unsigned Length,
867 bool Prologue) {
868 SW.startLine() << format(Fmt: "0x%02x ; end_c\n", Vals: OC[Offset]);
869 ++Offset;
870 return false;
871}
872
873bool Decoder::opcode_save_next(const uint8_t *OC, unsigned &Offset,
874 unsigned Length, bool Prologue) {
875 if (Prologue)
876 SW.startLine() << format(Fmt: "0x%02x ; save next\n", Vals: OC[Offset]);
877 else
878 SW.startLine() << format(Fmt: "0x%02x ; restore next\n",
879 Vals: OC[Offset]);
880 ++Offset;
881 return false;
882}
883
884bool Decoder::opcode_e7(const uint8_t *OC, unsigned &Offset, unsigned Length,
885 bool Prologue) {
886 // The e7 opcode has unusual decoding rules; write out the logic.
887 if ((OC[Offset + 1] & 0x80) == 0x80) {
888 SW.getOStream() << "reserved encoding\n";
889 Offset += 3;
890 return false;
891 }
892
893 if ((OC[Offset + 2] & 0xC0) == 0xC0) {
894 if ((OC[Offset + 1] & 0x10) == 0)
895 return opcode_save_zreg(Opcodes: OC, Offset, Length, Prologue);
896 return opcode_save_preg(Opcodes: OC, Offset, Length, Prologue);
897 }
898
899 return opcode_save_any_reg(Opcodes: OC, Offset, Length, Prologue);
900}
901
902bool Decoder::opcode_save_any_reg(const uint8_t *OC, unsigned &Offset,
903 unsigned Length, bool Prologue) {
904 // Whether the instruction has writeback
905 bool Writeback = (OC[Offset + 1] & 0x20) == 0x20;
906 // Whether the instruction is paired. (Paired instructions are required
907 // to save/restore adjacent registers.)
908 bool Paired = (OC[Offset + 1] & 0x40) == 0x40;
909 // The kind of register saved:
910 // - 0 is an x register
911 // - 1 is the low half of a q register
912 // - 2 is a whole q register
913 int RegKind = (OC[Offset + 2] & 0xC0) >> 6;
914 // Encoded register name (0 -> x0/q0, 1 -> x1/q1, etc.)
915 int Reg = OC[Offset + 1] & 0x1F;
916 // Encoded stack offset of load/store instruction; decoding varies by mode.
917 int StackOffset = OC[Offset + 2] & 0x3F;
918 if (Writeback)
919 StackOffset++;
920 if (!Writeback && !Paired && RegKind != 2)
921 StackOffset *= 8;
922 else
923 StackOffset *= 16;
924
925 SW.startLine() << format(Fmt: "0x%02x%02x%02x ; ", Vals: OC[Offset],
926 Vals: OC[Offset + 1], Vals: OC[Offset + 2]);
927
928 // Verify the encoding is in a form we understand. The high bit of the first
929 // byte, and mode 3 for the register kind are apparently reserved. The
930 // encoded register must refer to a valid register.
931 int MaxReg = 0x1F;
932 if (Paired)
933 --MaxReg;
934 if (RegKind == 0)
935 --MaxReg;
936 if ((OC[Offset + 1] & 0x80) == 0x80 || RegKind == 3 || Reg > MaxReg) {
937 SW.getOStream() << "invalid save_any_reg encoding\n";
938 Offset += 3;
939 return false;
940 }
941
942 if (Paired) {
943 if (Prologue)
944 SW.getOStream() << "stp ";
945 else
946 SW.getOStream() << "ldp ";
947 } else {
948 if (Prologue)
949 SW.getOStream() << "str ";
950 else
951 SW.getOStream() << "ldr ";
952 }
953
954 char RegChar = 'x';
955 if (RegKind == 1) {
956 RegChar = 'd';
957 } else if (RegKind == 2) {
958 RegChar = 'q';
959 }
960
961 if (Paired)
962 SW.getOStream() << format(Fmt: "%c%d, %c%d, ", Vals: RegChar, Vals: Reg, Vals: RegChar, Vals: Reg + 1);
963 else
964 SW.getOStream() << format(Fmt: "%c%d, ", Vals: RegChar, Vals: Reg);
965
966 if (Writeback) {
967 if (Prologue)
968 SW.getOStream() << format(Fmt: "[sp, #-%d]!\n", Vals: StackOffset);
969 else
970 SW.getOStream() << format(Fmt: "[sp], #%d\n", Vals: StackOffset);
971 } else {
972 SW.getOStream() << format(Fmt: "[sp, #%d]\n", Vals: StackOffset);
973 }
974
975 Offset += 3;
976 return false;
977}
978
979bool Decoder::opcode_save_zreg(const uint8_t *OC, unsigned &Offset,
980 unsigned Length, bool Prologue) {
981 uint32_t Reg = (OC[Offset + 1] & 0x0F) + 8;
982 uint32_t Off = ((OC[Offset + 1] & 0x60) << 1) | (OC[Offset + 2] & 0x3F);
983 SW.startLine() << format(
984 Fmt: "0x%02x%02x%02x ; %s z%u, [sp, #%u, mul vl]\n", Vals: OC[Offset],
985 Vals: OC[Offset + 1], Vals: OC[Offset + 2],
986 Vals: static_cast<const char *>(Prologue ? "str" : "ldr"), Vals: Reg, Vals: Off);
987 Offset += 3;
988 return false;
989}
990
991bool Decoder::opcode_save_preg(const uint8_t *OC, unsigned &Offset,
992 unsigned Length, bool Prologue) {
993 uint32_t Reg = (OC[Offset + 1] & 0x0F);
994 uint32_t Off = ((OC[Offset + 1] & 0x60) << 1) | (OC[Offset + 2] & 0x3F);
995 SW.startLine() << format(
996 Fmt: "0x%02x%02x%02x ; %s p%u, [sp, #%u, mul vl]\n", Vals: OC[Offset],
997 Vals: OC[Offset + 1], Vals: OC[Offset + 2],
998 Vals: static_cast<const char *>(Prologue ? "str" : "ldr"), Vals: Reg, Vals: Off);
999 Offset += 3;
1000 return false;
1001}
1002
1003bool Decoder::opcode_trap_frame(const uint8_t *OC, unsigned &Offset,
1004 unsigned Length, bool Prologue) {
1005 SW.startLine() << format(Fmt: "0x%02x ; trap frame\n", Vals: OC[Offset]);
1006 ++Offset;
1007 return false;
1008}
1009
1010bool Decoder::opcode_machine_frame(const uint8_t *OC, unsigned &Offset,
1011 unsigned Length, bool Prologue) {
1012 SW.startLine() << format(Fmt: "0x%02x ; machine frame\n",
1013 Vals: OC[Offset]);
1014 ++Offset;
1015 return false;
1016}
1017
1018bool Decoder::opcode_context(const uint8_t *OC, unsigned &Offset,
1019 unsigned Length, bool Prologue) {
1020 SW.startLine() << format(Fmt: "0x%02x ; context\n", Vals: OC[Offset]);
1021 ++Offset;
1022 return false;
1023}
1024
1025bool Decoder::opcode_ec_context(const uint8_t *OC, unsigned &Offset,
1026 unsigned Length, bool Prologue) {
1027 SW.startLine() << format(Fmt: "0x%02x ; EC context\n", Vals: OC[Offset]);
1028 ++Offset;
1029 return false;
1030}
1031
1032bool Decoder::opcode_clear_unwound_to_call(const uint8_t *OC, unsigned &Offset,
1033 unsigned Length, bool Prologue) {
1034 SW.startLine() << format(Fmt: "0x%02x ; clear unwound to call\n",
1035 Vals: OC[Offset]);
1036 ++Offset;
1037 return false;
1038}
1039
1040bool Decoder::opcode_pac_sign_lr(const uint8_t *OC, unsigned &Offset,
1041 unsigned Length, bool Prologue) {
1042 if (Prologue)
1043 SW.startLine() << format(Fmt: "0x%02x ; pacibsp\n", Vals: OC[Offset]);
1044 else
1045 SW.startLine() << format(Fmt: "0x%02x ; autibsp\n", Vals: OC[Offset]);
1046 ++Offset;
1047 return false;
1048}
1049
1050void Decoder::decodeOpcodes(ArrayRef<uint8_t> Opcodes, unsigned Offset,
1051 bool Prologue) {
1052 assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
1053 const RingEntry* DecodeRing = isAArch64 ? Ring64 : Ring;
1054 bool Terminated = false;
1055 for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
1056 for (unsigned DI = 0;; ++DI) {
1057 if ((isAArch64 && (DI >= std::size(Ring64))) ||
1058 (!isAArch64 && (DI >= std::size(Ring)))) {
1059 SW.startLine() << format(Fmt: "0x%02x ; Bad opcode!\n",
1060 Vals: Opcodes.data()[OI]);
1061 ++OI;
1062 break;
1063 }
1064
1065 if ((Opcodes[OI] & DecodeRing[DI].Mask) == DecodeRing[DI].Value) {
1066 if (OI + DecodeRing[DI].Length > OE) {
1067 SW.startLine() << format(Fmt: "Opcode 0x%02x goes past the unwind data\n",
1068 Vals: Opcodes[OI]);
1069 OI += DecodeRing[DI].Length;
1070 break;
1071 }
1072 Terminated =
1073 (this->*DecodeRing[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
1074 break;
1075 }
1076 }
1077 }
1078}
1079
1080bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
1081 const SectionRef &Section,
1082 uint64_t FunctionAddress, uint64_t VA) {
1083 ArrayRef<uint8_t> Contents;
1084 if (COFF.getSectionContents(Sec: COFF.getCOFFSection(Section), Res&: Contents))
1085 return false;
1086
1087 uint64_t SectionVA = Section.getAddress();
1088 uint64_t Offset = VA - SectionVA;
1089 const ulittle32_t *Data =
1090 reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
1091
1092 // Sanity check to ensure that the .xdata header is present.
1093 // A header is one or two words, followed by at least one word to describe
1094 // the unwind codes. Applicable to both ARM and AArch64.
1095 if (Contents.size() - Offset < 8)
1096 reportFatalUsageError(reason: ".xdata must be at least 8 bytes in size");
1097
1098 const ExceptionDataRecord XData(Data, isAArch64);
1099 DictScope XRS(SW, "ExceptionData");
1100 SW.printNumber(Label: "FunctionLength",
1101 Value: isAArch64 ? XData.FunctionLengthInBytesAArch64() :
1102 XData.FunctionLengthInBytesARM());
1103 SW.printNumber(Label: "Version", Value: XData.Vers());
1104 SW.printBoolean(Label: "ExceptionData", Value: XData.X());
1105 SW.printBoolean(Label: "EpiloguePacked", Value: XData.E());
1106 if (!isAArch64)
1107 SW.printBoolean(Label: "Fragment", Value: XData.F());
1108 SW.printNumber(Label: XData.E() ? "EpilogueOffset" : "EpilogueScopes",
1109 Value: XData.EpilogueCount());
1110 uint64_t ByteCodeLength = XData.CodeWords() * sizeof(uint32_t);
1111 SW.printNumber(Label: "ByteCodeLength", Value: ByteCodeLength);
1112
1113 if ((int64_t)(Contents.size() - Offset - 4 * HeaderWords(XR: XData) -
1114 (XData.E() ? 0 : XData.EpilogueCount() * 4) -
1115 (XData.X() ? 8 : 0)) < (int64_t)ByteCodeLength) {
1116 SW.flush();
1117 reportFatalUsageError(reason: "Malformed unwind data");
1118 }
1119
1120 if (XData.E()) {
1121 ArrayRef<uint8_t> UC = XData.UnwindByteCode();
1122 {
1123 ListScope PS(SW, "Prologue");
1124 decodeOpcodes(Opcodes: UC, Offset: 0, /*Prologue=*/true);
1125 }
1126 if (XData.EpilogueCount()) {
1127 ListScope ES(SW, "Epilogue");
1128 decodeOpcodes(Opcodes: UC, Offset: XData.EpilogueCount(), /*Prologue=*/false);
1129 }
1130 } else {
1131 {
1132 ListScope PS(SW, "Prologue");
1133 decodeOpcodes(Opcodes: XData.UnwindByteCode(), Offset: 0, /*Prologue=*/true);
1134 }
1135 ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
1136 ListScope ESS(SW, "EpilogueScopes");
1137 for (const EpilogueScope ES : EpilogueScopes) {
1138 DictScope ESES(SW, "EpilogueScope");
1139 SW.printNumber(Label: "StartOffset", Value: ES.EpilogueStartOffset());
1140 if (!isAArch64)
1141 SW.printNumber(Label: "Condition", Value: ES.Condition());
1142 SW.printNumber(Label: "EpilogueStartIndex",
1143 Value: isAArch64 ? ES.EpilogueStartIndexAArch64()
1144 : ES.EpilogueStartIndexARM());
1145 unsigned ReservedMask = isAArch64 ? 0xF : 0x3;
1146 if ((ES.ES >> 18) & ReservedMask)
1147 SW.printNumber(Label: "ReservedBits", Value: (ES.ES >> 18) & ReservedMask);
1148
1149 ListScope Opcodes(SW, "Opcodes");
1150 decodeOpcodes(Opcodes: XData.UnwindByteCode(),
1151 Offset: isAArch64 ? ES.EpilogueStartIndexAArch64()
1152 : ES.EpilogueStartIndexARM(),
1153 /*Prologue=*/false);
1154 }
1155 }
1156
1157 if (XData.X()) {
1158 const uint32_t Parameter = XData.ExceptionHandlerParameter();
1159 const size_t HandlerOffset = HeaderWords(XR: XData) +
1160 (XData.E() ? 0 : XData.EpilogueCount()) +
1161 XData.CodeWords();
1162
1163 uint64_t Address, SymbolOffset;
1164 ErrorOr<SymbolRef> Symbol = getSymbolForLocation(
1165 COFF, Section, OffsetInSection: Offset + HandlerOffset * sizeof(uint32_t),
1166 ImmediateOffset: XData.ExceptionHandlerRVA(), SymbolAddress&: Address, SymbolOffset,
1167 /*FunctionOnly=*/true);
1168 if (!Symbol) {
1169 ListScope EHS(SW, "ExceptionHandler");
1170 SW.printHex(Label: "Routine", Value: Address);
1171 SW.printHex(Label: "Parameter", Value: Parameter);
1172 return true;
1173 }
1174
1175 Expected<StringRef> Name = Symbol->getName();
1176 if (!Name) {
1177 std::string Buf;
1178 llvm::raw_string_ostream OS(Buf);
1179 logAllUnhandledErrors(E: Name.takeError(), OS);
1180 reportFatalUsageError(reason: Twine(Buf));
1181 }
1182
1183 ListScope EHS(SW, "ExceptionHandler");
1184 SW.printString(Label: "Routine", Value: formatSymbol(Name: *Name, Address, Offset: SymbolOffset));
1185 SW.printHex(Label: "Parameter", Value: Parameter);
1186 }
1187
1188 return true;
1189}
1190
1191bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
1192 const SectionRef Section, uint64_t Offset,
1193 unsigned Index, const RuntimeFunction &RF) {
1194 assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
1195 "packed entry cannot be treated as an unpacked entry");
1196
1197 uint64_t FunctionAddress, FunctionOffset;
1198 ErrorOr<SymbolRef> Function = getSymbolForLocation(
1199 COFF, Section, OffsetInSection: Offset, ImmediateOffset: RF.BeginAddress, SymbolAddress&: FunctionAddress, SymbolOffset&: FunctionOffset,
1200 /*FunctionOnly=*/true);
1201
1202 uint64_t XDataAddress, XDataOffset;
1203 ErrorOr<SymbolRef> XDataRecord = getSymbolForLocation(
1204 COFF, Section, OffsetInSection: Offset + 4, ImmediateOffset: RF.ExceptionInformationRVA(), SymbolAddress&: XDataAddress,
1205 SymbolOffset&: XDataOffset);
1206
1207 if (!RF.BeginAddress && !Function)
1208 return false;
1209 if (!RF.UnwindData && !XDataRecord)
1210 return false;
1211
1212 StringRef FunctionName;
1213 if (Function) {
1214 Expected<StringRef> FunctionNameOrErr = Function->getName();
1215 if (!FunctionNameOrErr) {
1216 std::string Buf;
1217 llvm::raw_string_ostream OS(Buf);
1218 logAllUnhandledErrors(E: FunctionNameOrErr.takeError(), OS);
1219 reportFatalUsageError(reason: Twine(Buf));
1220 }
1221 FunctionName = *FunctionNameOrErr;
1222 }
1223
1224 SW.printString(Label: "Function",
1225 Value: formatSymbol(Name: FunctionName, Address: FunctionAddress, Offset: FunctionOffset));
1226
1227 if (XDataRecord) {
1228 Expected<StringRef> Name = XDataRecord->getName();
1229 if (!Name) {
1230 std::string Buf;
1231 llvm::raw_string_ostream OS(Buf);
1232 logAllUnhandledErrors(E: Name.takeError(), OS);
1233 reportFatalUsageError(reason: Twine(Buf));
1234 }
1235
1236 SW.printString(Label: "ExceptionRecord",
1237 Value: formatSymbol(Name: *Name, Address: XDataAddress, Offset: XDataOffset));
1238
1239 Expected<section_iterator> SIOrErr = XDataRecord->getSection();
1240 if (!SIOrErr) {
1241 // TODO: Actually report errors helpfully.
1242 consumeError(Err: SIOrErr.takeError());
1243 return false;
1244 }
1245 section_iterator SI = *SIOrErr;
1246
1247 return dumpXDataRecord(COFF, Section: *SI, FunctionAddress, VA: XDataAddress);
1248 } else {
1249 SW.printString(Label: "ExceptionRecord", Value: formatSymbol(Name: "", Address: XDataAddress));
1250
1251 ErrorOr<SectionRef> Section = getSectionContaining(COFF, VA: XDataAddress);
1252 if (!Section)
1253 return false;
1254
1255 return dumpXDataRecord(COFF, Section: *Section, FunctionAddress, VA: XDataAddress);
1256 }
1257}
1258
1259bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
1260 const SectionRef Section, uint64_t Offset,
1261 unsigned Index, const RuntimeFunction &RF) {
1262 assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
1263 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
1264 "unpacked entry cannot be treated as a packed entry");
1265
1266 uint64_t FunctionAddress, FunctionOffset;
1267 ErrorOr<SymbolRef> Function = getSymbolForLocation(
1268 COFF, Section, OffsetInSection: Offset, ImmediateOffset: RF.BeginAddress, SymbolAddress&: FunctionAddress, SymbolOffset&: FunctionOffset,
1269 /*FunctionOnly=*/true);
1270
1271 StringRef FunctionName;
1272 if (Function) {
1273 Expected<StringRef> FunctionNameOrErr = Function->getName();
1274 if (!FunctionNameOrErr) {
1275 std::string Buf;
1276 llvm::raw_string_ostream OS(Buf);
1277 logAllUnhandledErrors(E: FunctionNameOrErr.takeError(), OS);
1278 reportFatalUsageError(reason: Twine(Buf));
1279 }
1280 FunctionName = *FunctionNameOrErr;
1281 }
1282
1283 SW.printString(Label: "Function",
1284 Value: formatSymbol(Name: FunctionName, Address: FunctionAddress, Offset: FunctionOffset));
1285 SW.printBoolean(Label: "Fragment",
1286 Value: RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
1287 SW.printNumber(Label: "FunctionLength", Value: RF.FunctionLength());
1288 SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
1289 SW.printBoolean(Label: "HomedParameters", Value: RF.H());
1290 SW.printNumber(Label: "Reg", Value: RF.Reg());
1291 SW.printNumber(Label: "R", Value: RF.R());
1292 SW.printBoolean(Label: "LinkRegister", Value: RF.L());
1293 SW.printBoolean(Label: "Chaining", Value: RF.C());
1294 SW.printNumber(Label: "StackAdjustment", Value: StackAdjustment(RF) << 2);
1295
1296 {
1297 ListScope PS(SW, "Prologue");
1298
1299 uint16_t GPRMask, VFPMask;
1300 std::tie(args&: GPRMask, args&: VFPMask) = SavedRegisterMask(RF, /*Prologue=*/true);
1301
1302 if (StackAdjustment(RF) && !PrologueFolding(RF))
1303 SW.startLine() << "sub sp, sp, #" << StackAdjustment(RF) * 4 << "\n";
1304 if (VFPMask) {
1305 SW.startLine() << "vpush ";
1306 printVFPMask(VFPMask);
1307 OS << "\n";
1308 }
1309 if (RF.C()) {
1310 // Count the number of registers pushed below R11
1311 int FpOffset = 4 * llvm::popcount(Value: GPRMask & ((1U << 11) - 1));
1312 if (FpOffset)
1313 SW.startLine() << "add.w r11, sp, #" << FpOffset << "\n";
1314 else
1315 SW.startLine() << "mov r11, sp\n";
1316 }
1317 if (GPRMask) {
1318 SW.startLine() << "push ";
1319 printGPRMask(GPRMask);
1320 OS << "\n";
1321 }
1322 if (RF.H())
1323 SW.startLine() << "push {r0-r3}\n";
1324 }
1325
1326 if (RF.Ret() != ReturnType::RT_NoEpilogue) {
1327 ListScope PS(SW, "Epilogue");
1328
1329 uint16_t GPRMask, VFPMask;
1330 std::tie(args&: GPRMask, args&: VFPMask) = SavedRegisterMask(RF, /*Prologue=*/false);
1331
1332 if (StackAdjustment(RF) && !EpilogueFolding(RF))
1333 SW.startLine() << "add sp, sp, #" << StackAdjustment(RF) * 4 << "\n";
1334 if (VFPMask) {
1335 SW.startLine() << "vpop ";
1336 printVFPMask(VFPMask);
1337 OS << "\n";
1338 }
1339 if (GPRMask) {
1340 SW.startLine() << "pop ";
1341 printGPRMask(GPRMask);
1342 OS << "\n";
1343 }
1344 if (RF.H()) {
1345 if (RF.L() == 0 || RF.Ret() != ReturnType::RT_POP)
1346 SW.startLine() << "add sp, sp, #16\n";
1347 else
1348 SW.startLine() << "ldr pc, [sp], #20\n";
1349 }
1350 if (RF.Ret() != ReturnType::RT_POP)
1351 SW.startLine() << RF.Ret() << '\n';
1352 }
1353
1354 return true;
1355}
1356
1357bool Decoder::dumpPackedARM64Entry(const object::COFFObjectFile &COFF,
1358 const SectionRef Section, uint64_t Offset,
1359 unsigned Index,
1360 const RuntimeFunctionARM64 &RF) {
1361 assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
1362 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
1363 "unpacked entry cannot be treated as a packed entry");
1364
1365 uint64_t FunctionAddress, FunctionOffset;
1366 ErrorOr<SymbolRef> Function = getSymbolForLocation(
1367 COFF, Section, OffsetInSection: Offset, ImmediateOffset: RF.BeginAddress, SymbolAddress&: FunctionAddress, SymbolOffset&: FunctionOffset,
1368 /*FunctionOnly=*/true);
1369
1370 StringRef FunctionName;
1371 if (Function) {
1372 Expected<StringRef> FunctionNameOrErr = Function->getName();
1373 if (!FunctionNameOrErr) {
1374 std::string Buf;
1375 llvm::raw_string_ostream OS(Buf);
1376 logAllUnhandledErrors(E: FunctionNameOrErr.takeError(), OS);
1377 reportFatalUsageError(reason: Twine(Buf));
1378 }
1379 FunctionName = *FunctionNameOrErr;
1380 }
1381
1382 SW.printString(Label: "Function",
1383 Value: formatSymbol(Name: FunctionName, Address: FunctionAddress, Offset: FunctionOffset));
1384 SW.printBoolean(Label: "Fragment",
1385 Value: RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
1386 SW.printNumber(Label: "FunctionLength", Value: RF.FunctionLength());
1387 SW.printNumber(Label: "RegF", Value: RF.RegF());
1388 SW.printNumber(Label: "RegI", Value: RF.RegI());
1389 SW.printBoolean(Label: "HomedParameters", Value: RF.H());
1390 SW.printNumber(Label: "CR", Value: RF.CR());
1391 SW.printNumber(Label: "FrameSize", Value: RF.FrameSize() << 4);
1392 ListScope PS(SW, "Prologue");
1393
1394 // Synthesize the equivalent prologue according to the documentation
1395 // at https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling,
1396 // printed in reverse order compared to the docs, to match how prologues
1397 // are printed for the non-packed case.
1398 int IntSZ = 8 * RF.RegI();
1399 if (RF.CR() == 1)
1400 IntSZ += 8;
1401 int FpSZ = 8 * RF.RegF();
1402 if (RF.RegF())
1403 FpSZ += 8;
1404 int SavSZ = (IntSZ + FpSZ + 8 * 8 * RF.H() + 0xf) & ~0xf;
1405 int LocSZ = (RF.FrameSize() << 4) - SavSZ;
1406 bool Homing = RF.H();
1407
1408 if (RF.H() && RF.RegI() == 0 && RF.RegF() == 0 && RF.CR() != 1) {
1409 LocSZ += SavSZ;
1410 Homing = false;
1411 }
1412
1413 if (RF.CR() == 2 || RF.CR() == 3) {
1414 SW.startLine() << "mov x29, sp\n";
1415 if (LocSZ <= 512) {
1416 SW.startLine() << format(Fmt: "stp x29, lr, [sp, #-%d]!\n", Vals: LocSZ);
1417 } else {
1418 SW.startLine() << "stp x29, lr, [sp, #0]\n";
1419 }
1420 }
1421 if (LocSZ > 4080) {
1422 SW.startLine() << format(Fmt: "sub sp, sp, #%d\n", Vals: LocSZ - 4080);
1423 SW.startLine() << "sub sp, sp, #4080\n";
1424 } else if ((RF.CR() != 3 && RF.CR() != 2 && LocSZ > 0) || LocSZ > 512) {
1425 SW.startLine() << format(Fmt: "sub sp, sp, #%d\n", Vals: LocSZ);
1426 }
1427 if (Homing) {
1428 SW.startLine() << format(Fmt: "stp x6, x7, [sp, #%d]\n", Vals: SavSZ - 16);
1429 SW.startLine() << format(Fmt: "stp x4, x5, [sp, #%d]\n", Vals: SavSZ - 32);
1430 SW.startLine() << format(Fmt: "stp x2, x3, [sp, #%d]\n", Vals: SavSZ - 48);
1431 SW.startLine() << format(Fmt: "stp x0, x1, [sp, #%d]\n", Vals: SavSZ - 64);
1432 }
1433 int FloatRegs = RF.RegF() > 0 ? RF.RegF() + 1 : 0;
1434 for (int I = (FloatRegs + 1) / 2 - 1; I >= 0; I--) {
1435 if (I == (FloatRegs + 1) / 2 - 1 && FloatRegs % 2 == 1) {
1436 // The last register, an odd register without a pair
1437 SW.startLine() << format(Fmt: "str d%d, [sp, #%d]\n", Vals: 8 + 2 * I,
1438 Vals: IntSZ + 16 * I);
1439 } else if (I == 0 && RF.RegI() == 0 && RF.CR() != 1) {
1440 SW.startLine() << format(Fmt: "stp d%d, d%d, [sp, #-%d]!\n", Vals: 8 + 2 * I,
1441 Vals: 8 + 2 * I + 1, Vals: SavSZ);
1442 } else {
1443 SW.startLine() << format(Fmt: "stp d%d, d%d, [sp, #%d]\n", Vals: 8 + 2 * I,
1444 Vals: 8 + 2 * I + 1, Vals: IntSZ + 16 * I);
1445 }
1446 }
1447 if (RF.CR() == 1 && (RF.RegI() % 2) == 0) {
1448 if (RF.RegI() == 0)
1449 SW.startLine() << format(Fmt: "str lr, [sp, #-%d]!\n", Vals: SavSZ);
1450 else
1451 SW.startLine() << format(Fmt: "str lr, [sp, #%d]\n", Vals: IntSZ - 8);
1452 }
1453 for (int I = (RF.RegI() + 1) / 2 - 1; I >= 0; I--) {
1454 if (I == (RF.RegI() + 1) / 2 - 1 && RF.RegI() % 2 == 1) {
1455 // The last register, an odd register without a pair
1456 if (RF.CR() == 1) {
1457 if (I == 0) { // If this is the only register pair
1458 // CR=1 combined with RegI=1 maps to a special case; there's
1459 // no unwind info opcode that saves a GPR together with LR
1460 // with writeback to sp (no save_lrpair_x).
1461 // Instead, this case expands to two instructions; a preceding
1462 // (in prologue execution order) "sub sp, sp, #16", followed
1463 // by a regular "stp x19, lr, [sp]" (save_lrpair).
1464 SW.startLine() << format(Fmt: "stp x%d, lr, [sp]\n", Vals: 19);
1465 SW.startLine() << format(Fmt: "sub sp, sp, #%d\n", Vals: SavSZ);
1466 } else
1467 SW.startLine() << format(Fmt: "stp x%d, lr, [sp, #%d]\n", Vals: 19 + 2 * I,
1468 Vals: 16 * I);
1469 } else {
1470 if (I == 0)
1471 SW.startLine() << format(Fmt: "str x%d, [sp, #-%d]!\n", Vals: 19 + 2 * I, Vals: SavSZ);
1472 else
1473 SW.startLine() << format(Fmt: "str x%d, [sp, #%d]\n", Vals: 19 + 2 * I, Vals: 16 * I);
1474 }
1475 } else if (I == 0) {
1476 // The first register pair
1477 SW.startLine() << format(Fmt: "stp x19, x20, [sp, #-%d]!\n", Vals: SavSZ);
1478 } else {
1479 SW.startLine() << format(Fmt: "stp x%d, x%d, [sp, #%d]\n", Vals: 19 + 2 * I,
1480 Vals: 19 + 2 * I + 1, Vals: 16 * I);
1481 }
1482 }
1483 if (RF.CR() == 2)
1484 SW.startLine() << "pacibsp\n";
1485 SW.startLine() << "end\n";
1486
1487 return true;
1488}
1489
1490bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
1491 const SectionRef Section, unsigned Index,
1492 ArrayRef<uint8_t> Contents) {
1493 uint64_t Offset = PDataEntrySize * Index;
1494 const ulittle32_t *Data =
1495 reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
1496
1497 const RuntimeFunction Entry(Data);
1498 DictScope RFS(SW, "RuntimeFunction");
1499 if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
1500 return dumpUnpackedEntry(COFF, Section, Offset, Index, RF: Entry);
1501 if (isAArch64) {
1502 const RuntimeFunctionARM64 EntryARM64(Data);
1503 return dumpPackedARM64Entry(COFF, Section, Offset, Index, RF: EntryARM64);
1504 }
1505 return dumpPackedEntry(COFF, Section, Offset, Index, RF: Entry);
1506}
1507
1508void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
1509 const SectionRef Section) {
1510 ArrayRef<uint8_t> Contents;
1511 if (COFF.getSectionContents(Sec: COFF.getCOFFSection(Section), Res&: Contents))
1512 return;
1513
1514 if (Contents.size() % PDataEntrySize) {
1515 errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
1516 return;
1517 }
1518
1519 for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
1520 if (!dumpProcedureDataEntry(COFF, Section, Index: EI, Contents))
1521 break;
1522}
1523
1524Error Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
1525 for (const auto &Section : COFF.sections()) {
1526 Expected<StringRef> NameOrErr =
1527 COFF.getSectionName(Sec: COFF.getCOFFSection(Section));
1528 if (!NameOrErr)
1529 return NameOrErr.takeError();
1530
1531 if (NameOrErr->starts_with(Prefix: ".pdata"))
1532 dumpProcedureData(COFF, Section);
1533 }
1534 return Error::success();
1535}
1536}
1537}
1538}
1539