1 //===-- ARMWinEHPrinter.cpp - Windows on ARM EH Data Printer ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ARMWinEHPrinter.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/ARMWinEH.h"
15 #include "llvm/Support/Format.h"
18 using namespace llvm::object;
19 using namespace llvm::support;
22 raw_ostream &operator<<(raw_ostream &OS, const ARM::WinEH::ReturnType &RT) {
24 case ARM::WinEH::ReturnType::RT_POP:
27 case ARM::WinEH::ReturnType::RT_B:
30 case ARM::WinEH::ReturnType::RT_BW:
33 case ARM::WinEH::ReturnType::RT_NoEpilogue:
34 OS << "(no epilogue)";
41 static std::string formatSymbol(StringRef Name, uint64_t Address,
42 uint64_t Offset = 0) {
44 raw_string_ostream OS(Buffer);
50 OS << format("+0x%X (0x%" PRIX64 ")", Offset, Address);
51 else if (!Name.empty())
52 OS << format("(0x%" PRIX64 ")", Address);
54 OS << format("0x%" PRIX64, Address);
62 const size_t Decoder::PDataEntrySize = sizeof(RuntimeFunction);
64 const Decoder::RingEntry Decoder::Ring[] = {
65 { 0x80, 0x00, &Decoder::opcode_0xxxxxxx },
66 { 0xc0, 0x80, &Decoder::opcode_10Lxxxxx },
67 { 0xf0, 0xc0, &Decoder::opcode_1100xxxx },
68 { 0xf8, 0xd0, &Decoder::opcode_11010Lxx },
69 { 0xf8, 0xd8, &Decoder::opcode_11011Lxx },
70 { 0xf8, 0xe0, &Decoder::opcode_11100xxx },
71 { 0xfc, 0xe8, &Decoder::opcode_111010xx },
72 { 0xfe, 0xec, &Decoder::opcode_1110110L },
73 { 0xff, 0xee, &Decoder::opcode_11101110 },
74 { 0xff, 0xef, &Decoder::opcode_11101111 },
75 { 0xff, 0xf5, &Decoder::opcode_11110101 },
76 { 0xff, 0xf6, &Decoder::opcode_11110110 },
77 { 0xff, 0xf7, &Decoder::opcode_11110111 },
78 { 0xff, 0xf8, &Decoder::opcode_11111000 },
79 { 0xff, 0xf9, &Decoder::opcode_11111001 },
80 { 0xff, 0xfa, &Decoder::opcode_11111010 },
81 { 0xff, 0xfb, &Decoder::opcode_11111011 },
82 { 0xff, 0xfc, &Decoder::opcode_11111100 },
83 { 0xff, 0xfd, &Decoder::opcode_11111101 },
84 { 0xff, 0xfe, &Decoder::opcode_11111110 },
85 { 0xff, 0xff, &Decoder::opcode_11111111 },
88 void Decoder::printRegisters(const std::pair<uint16_t, uint32_t> &RegisterMask) {
89 static const char * const GPRRegisterNames[16] = {
90 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
91 "r11", "ip", "sp", "lr", "pc",
94 const uint16_t GPRMask = std::get<0>(RegisterMask);
95 const uint16_t VFPMask = std::get<1>(RegisterMask);
99 for (unsigned RI = 0, RE = 11; RI < RE; ++RI) {
100 if (GPRMask & (1 << RI)) {
103 OS << GPRRegisterNames[RI];
107 for (unsigned RI = 0, RE = 32; RI < RE; ++RI) {
108 if (VFPMask & (1 << RI)) {
111 OS << "d" << unsigned(RI);
115 for (unsigned RI = 11, RE = 16; RI < RE; ++RI) {
116 if (GPRMask & (1 << RI)) {
119 OS << GPRRegisterNames[RI];
126 ErrorOr<object::SectionRef>
127 Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) {
128 for (const auto &Section : COFF.sections()) {
132 if (error_code EC = Section.getAddress(Address))
134 if (error_code EC = Section.getSize(Size))
137 if (VA >= Address && (VA - Address) <= Size)
140 return readobj_error::unknown_symbol;
143 ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
144 uint64_t VA, bool FunctionOnly) {
145 for (const auto &Symbol : COFF.symbols()) {
147 SymbolRef::Type Type;
148 if (error_code EC = Symbol.getType(Type))
150 if (Type != SymbolRef::ST_Function)
155 if (error_code EC = Symbol.getAddress(Address))
160 return readobj_error::unknown_symbol;
163 ErrorOr<SymbolRef> Decoder::getRelocatedSymbol(const COFFObjectFile &,
164 const SectionRef &Section,
166 for (const auto &Relocation : Section.relocations()) {
167 uint64_t RelocationOffset;
168 if (auto Error = Relocation.getOffset(RelocationOffset))
170 if (RelocationOffset == Offset)
171 return *Relocation.getSymbol();
173 return readobj_error::unknown_symbol;
176 bool Decoder::opcode_0xxxxxxx(const ulittle8_t *OC, unsigned &Offset,
177 unsigned Length, bool Prologue) {
178 uint8_t Imm = OC[Offset] & 0x7f;
179 SW.startLine() << format("0x%02x ; %s sp, #(%u * 4)\n",
181 static_cast<const char *>(Prologue ? "sub" : "add"),
187 bool Decoder::opcode_10Lxxxxx(const ulittle8_t *OC, unsigned &Offset,
188 unsigned Length, bool Prologue) {
189 unsigned Link = (OC[Offset] & 0x20) >> 5;
190 uint16_t RegisterMask = (Link << (Prologue ? 14 : 15))
191 | ((OC[Offset + 0] & 0x1f) << 8)
192 | ((OC[Offset + 1] & 0xff) << 0);
193 assert((~RegisterMask & (1 << 13)) && "sp must not be set");
194 assert((~RegisterMask & (1 << (Prologue ? 15 : 14))) && "pc must not be set");
196 SW.startLine() << format("0x%02x 0x%02x ; %s.w ",
197 OC[Offset + 0], OC[Offset + 1],
198 Prologue ? "push" : "pop");
199 printRegisters(std::make_pair(RegisterMask, 0));
206 bool Decoder::opcode_1100xxxx(const ulittle8_t *OC, unsigned &Offset,
207 unsigned Length, bool Prologue) {
209 SW.startLine() << format("0x%02x ; mov r%u, sp\n",
210 OC[Offset], OC[Offset] & 0xf);
212 SW.startLine() << format("0x%02x ; mov sp, r%u\n",
213 OC[Offset], OC[Offset] & 0xf);
218 bool Decoder::opcode_11010Lxx(const ulittle8_t *OC, unsigned &Offset,
219 unsigned Length, bool Prologue) {
220 unsigned Link = (OC[Offset] & 0x4) >> 3;
221 unsigned Count = (OC[Offset] & 0x3);
223 uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
224 | (((1 << (Count + 1)) - 1) << 4);
226 SW.startLine() << format("0x%02x ; %s ", OC[Offset],
227 Prologue ? "push" : "pop");
228 printRegisters(std::make_pair(GPRMask, 0));
235 bool Decoder::opcode_11011Lxx(const ulittle8_t *OC, unsigned &Offset,
236 unsigned Length, bool Prologue) {
237 unsigned Link = (OC[Offset] & 0x4) >> 2;
238 unsigned Count = (OC[Offset] & 0x3) + 4;
240 uint16_t GPRMask = (Link << (Prologue ? 14 : 15))
241 | (((1 << (Count + 1)) - 1) << 4);
243 SW.startLine() << format("0x%02x ; %s.w ", OC[Offset],
244 Prologue ? "push" : "pop");
245 printRegisters(std::make_pair(GPRMask, 0));
252 bool Decoder::opcode_11100xxx(const ulittle8_t *OC, unsigned &Offset,
253 unsigned Length, bool Prologue) {
254 unsigned High = (OC[Offset] & 0x7);
255 uint32_t VFPMask = (((1 << (High + 1)) - 1) << 8);
257 SW.startLine() << format("0x%02x ; %s ", OC[Offset],
258 Prologue ? "vpush" : "vpop");
259 printRegisters(std::make_pair(0, VFPMask));
266 bool Decoder::opcode_111010xx(const ulittle8_t *OC, unsigned &Offset,
267 unsigned Length, bool Prologue) {
268 uint16_t Imm = ((OC[Offset + 0] & 0x03) << 8) | ((OC[Offset + 1] & 0xff) << 0);
270 SW.startLine() << format("0x%02x 0x%02x ; %s.w sp, #(%u * 4)\n",
271 OC[Offset + 0], OC[Offset + 1],
272 static_cast<const char *>(Prologue ? "sub" : "add"),
279 bool Decoder::opcode_1110110L(const ulittle8_t *OC, unsigned &Offset,
280 unsigned Length, bool Prologue) {
281 uint8_t GPRMask = ((OC[Offset + 0] & 0x01) << (Prologue ? 14 : 15))
282 | ((OC[Offset + 1] & 0xff) << 0);
284 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
285 OC[Offset + 1], Prologue ? "push" : "pop");
286 printRegisters(std::make_pair(GPRMask, 0));
293 bool Decoder::opcode_11101110(const ulittle8_t *OC, unsigned &Offset,
294 unsigned Length, bool Prologue) {
295 assert(!Prologue && "may not be used in prologue");
297 if (OC[Offset + 1] & 0xf0)
298 SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
299 OC[Offset + 0], OC[Offset + 1]);
302 << format("0x%02x 0x%02x ; microsoft-specific (type: %u)\n",
303 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] & 0x0f);
309 bool Decoder::opcode_11101111(const ulittle8_t *OC, unsigned &Offset,
310 unsigned Length, bool Prologue) {
311 assert(!Prologue && "may not be used in prologue");
313 if (OC[Offset + 1] & 0xf0)
314 SW.startLine() << format("0x%02x 0x%02x ; reserved\n",
315 OC[Offset + 0], OC[Offset + 1]);
318 << format("0x%02x 0x%02x ; ldr.w lr, [sp], #%u\n",
319 OC[Offset + 0], OC[Offset + 1], OC[Offset + 1] << 2);
325 bool Decoder::opcode_11110101(const ulittle8_t *OC, unsigned &Offset,
326 unsigned Length, bool Prologue) {
327 unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
328 unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
329 uint32_t VFPMask = ((1 << (End - Start)) - 1) << Start;
331 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
332 OC[Offset + 1], Prologue ? "vpush" : "vpop");
333 printRegisters(std::make_pair(0, VFPMask));
340 bool Decoder::opcode_11110110(const ulittle8_t *OC, unsigned &Offset,
341 unsigned Length, bool Prologue) {
342 unsigned Start = (OC[Offset + 1] & 0xf0) >> 4;
343 unsigned End = (OC[Offset + 1] & 0x0f) >> 0;
344 uint32_t VFPMask = ((1 << (End - Start)) - 1) << 16;
346 SW.startLine() << format("0x%02x 0x%02x ; %s ", OC[Offset + 0],
347 OC[Offset + 1], Prologue ? "vpush" : "vpop");
348 printRegisters(std::make_pair(0, VFPMask));
355 bool Decoder::opcode_11110111(const ulittle8_t *OC, unsigned &Offset,
356 unsigned Length, bool Prologue) {
357 uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
359 SW.startLine() << format("0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
360 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
361 static_cast<const char *>(Prologue ? "sub" : "add"),
364 ++Offset, ++Offset, ++Offset;
368 bool Decoder::opcode_11111000(const ulittle8_t *OC, unsigned &Offset,
369 unsigned Length, bool Prologue) {
370 uint32_t Imm = (OC[Offset + 1] << 16)
371 | (OC[Offset + 2] << 8)
372 | (OC[Offset + 3] << 0);
375 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s sp, sp, #(%u * 4)\n",
376 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
377 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
379 ++Offset, ++Offset, ++Offset, ++Offset;
383 bool Decoder::opcode_11111001(const ulittle8_t *OC, unsigned &Offset,
384 unsigned Length, bool Prologue) {
385 uint32_t Imm = (OC[Offset + 1] << 8) | (OC[Offset + 2] << 0);
388 << format("0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
389 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2],
390 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
392 ++Offset, ++Offset, ++Offset;
396 bool Decoder::opcode_11111010(const ulittle8_t *OC, unsigned &Offset,
397 unsigned Length, bool Prologue) {
398 uint32_t Imm = (OC[Offset + 1] << 16)
399 | (OC[Offset + 2] << 8)
400 | (OC[Offset + 3] << 0);
403 << format("0x%02x 0x%02x 0x%02x 0x%02x ; %s.w sp, sp, #(%u * 4)\n",
404 OC[Offset + 0], OC[Offset + 1], OC[Offset + 2], OC[Offset + 3],
405 static_cast<const char *>(Prologue ? "sub" : "add"), Imm);
407 ++Offset, ++Offset, ++Offset, ++Offset;
411 bool Decoder::opcode_11111011(const ulittle8_t *OC, unsigned &Offset,
412 unsigned Length, bool Prologue) {
413 SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
418 bool Decoder::opcode_11111100(const ulittle8_t *OC, unsigned &Offset,
419 unsigned Length, bool Prologue) {
420 SW.startLine() << format("0x%02x ; nop.w\n", OC[Offset]);
425 bool Decoder::opcode_11111101(const ulittle8_t *OC, unsigned &Offset,
426 unsigned Length, bool Prologue) {
427 SW.startLine() << format("0x%02x ; b\n", OC[Offset]);
432 bool Decoder::opcode_11111110(const ulittle8_t *OC, unsigned &Offset,
433 unsigned Length, bool Prologue) {
434 SW.startLine() << format("0x%02x ; b.w\n", OC[Offset]);
439 bool Decoder::opcode_11111111(const ulittle8_t *OC, unsigned &Offset,
440 unsigned Length, bool Prologue) {
445 void Decoder::decodeOpcodes(ArrayRef<ulittle8_t> Opcodes, unsigned Offset,
447 assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
449 bool Terminated = false;
450 for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
451 bool Decoded = false;
452 for (unsigned DI = 0, DE = array_lengthof(Ring); DI < DE; ++DI) {
453 if ((Opcodes[OI] & Ring[DI].Mask) == Ring[DI].Value) {
454 Terminated = (this->*Ring[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
459 assert(Decoded && "unhandled opcode");
463 bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
464 const SectionRef &Section,
465 uint64_t FunctionAddress, uint64_t VA) {
466 ArrayRef<uint8_t> Contents;
467 if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
471 if (Section.getAddress(SectionVA))
474 uint64_t Offset = VA - SectionVA;
475 const ulittle32_t *Data =
476 reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
477 const ExceptionDataRecord XData(Data);
479 DictScope XRS(SW, "ExceptionData");
480 SW.printNumber("FunctionLength", XData.FunctionLength() << 1);
481 SW.printNumber("Version", XData.Vers());
482 SW.printBoolean("ExceptionData", XData.X());
483 SW.printBoolean("EpiloguePacked", XData.E());
484 SW.printBoolean("Fragment", XData.F());
485 SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
486 XData.EpilogueCount());
487 SW.printNumber("ByteCodeLength",
488 static_cast<uint64_t>(XData.CodeWords() * sizeof(uint32_t)));
491 ArrayRef<ulittle8_t> UC = XData.UnwindByteCode();
493 ListScope PS(SW, "Prologue");
494 decodeOpcodes(UC, 0, /*Prologue=*/true);
496 if (XData.EpilogueCount()) {
497 ListScope ES(SW, "Epilogue");
498 decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
501 ArrayRef<ulittle32_t> EpilogueScopes = XData.EpilogueScopes();
502 ListScope ESS(SW, "EpilogueScopes");
503 for (const EpilogueScope ES : EpilogueScopes) {
504 DictScope ESES(SW, "EpilogueScope");
505 SW.printNumber("StartOffset", ES.EpilogueStartOffset());
506 SW.printNumber("Condition", ES.Condition());
507 SW.printNumber("EpilogueStartIndex", ES.EpilogueStartIndex());
509 ListScope Opcodes(SW, "Opcodes");
510 decodeOpcodes(XData.UnwindByteCode(), ES.EpilogueStartIndex(),
516 const uint32_t Address = XData.ExceptionHandlerRVA();
517 const uint32_t Parameter = XData.ExceptionHandlerParameter();
518 const size_t HandlerOffset = HeaderWords(XData)
519 + (XData.E() ? 0 : XData.EpilogueCount())
522 ErrorOr<SymbolRef> Symbol =
523 getRelocatedSymbol(COFF, Section, HandlerOffset * sizeof(uint32_t));
525 Symbol = getSymbol(COFF, Address, /*FunctionOnly=*/true);
529 Symbol->getName(Name);
531 ListScope EHS(SW, "ExceptionHandler");
532 SW.printString("Routine", formatSymbol(Name, Address));
533 SW.printHex("Parameter", Parameter);
539 bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
540 const SectionRef Section, uint64_t Offset,
541 unsigned Index, const RuntimeFunction &RF) {
542 assert(RF.Flag() == RuntimeFunctionFlag::RFF_Unpacked &&
543 "packed entry cannot be treated as an unpacked entry");
545 ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
547 Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
549 ErrorOr<SymbolRef> XDataRecord = getRelocatedSymbol(COFF, Section, Offset + 4);
551 XDataRecord = getSymbol(COFF, RF.ExceptionInformationRVA());
553 if (!RF.BeginAddress && !Function)
555 if (!RF.UnwindData && !XDataRecord)
558 StringRef FunctionName;
559 uint64_t FunctionAddress;
561 Function->getName(FunctionName);
562 Function->getAddress(FunctionAddress);
564 const pe32_header *PEHeader;
565 if (COFF.getPE32Header(PEHeader))
567 FunctionAddress = PEHeader->ImageBase + RF.BeginAddress;
570 SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
576 XDataRecord->getName(Name);
577 XDataRecord->getAddress(Address);
579 SW.printString("ExceptionRecord", formatSymbol(Name, Address));
581 section_iterator SI = COFF.section_end();
582 if (XDataRecord->getSection(SI))
585 return dumpXDataRecord(COFF, *SI, FunctionAddress, Address);
587 const pe32_header *PEHeader;
588 if (COFF.getPE32Header(PEHeader))
591 uint64_t Address = PEHeader->ImageBase + RF.ExceptionInformationRVA();
592 SW.printString("ExceptionRecord", formatSymbol("", Address));
594 ErrorOr<SectionRef> Section =
595 getSectionContaining(COFF, RF.ExceptionInformationRVA());
599 return dumpXDataRecord(COFF, *Section, FunctionAddress,
600 RF.ExceptionInformationRVA());
604 bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
605 const SectionRef Section, uint64_t Offset,
606 unsigned Index, const RuntimeFunction &RF) {
607 assert((RF.Flag() == RuntimeFunctionFlag::RFF_Packed ||
608 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment) &&
609 "unpacked entry cannot be treated as a packed entry");
611 ErrorOr<SymbolRef> Function = getRelocatedSymbol(COFF, Section, Offset);
613 Function = getSymbol(COFF, RF.BeginAddress, /*FunctionOnly=*/true);
615 StringRef FunctionName;
616 uint64_t FunctionAddress;
618 Function->getName(FunctionName);
619 Function->getAddress(FunctionAddress);
621 const pe32_header *PEHeader;
622 if (COFF.getPE32Header(PEHeader))
624 FunctionAddress = PEHeader->ImageBase + RF.BeginAddress;
627 SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
628 SW.printBoolean("Fragment",
629 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
630 SW.printNumber("FunctionLength", RF.FunctionLength());
631 SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
632 SW.printBoolean("HomedParameters", RF.H());
633 SW.startLine() << "SavedRegisters: ";
634 printRegisters(SavedRegisterMask(RF));
636 SW.printNumber("StackAdjustment", StackAdjustment(RF) << 2);
641 bool Decoder::dumpProcedureDataEntry(const COFFObjectFile &COFF,
642 const SectionRef Section, unsigned Index,
643 ArrayRef<uint8_t> Contents) {
644 uint64_t Offset = PDataEntrySize * Index;
645 const ulittle32_t *Data =
646 reinterpret_cast<const ulittle32_t *>(Contents.data() + Offset);
648 const RuntimeFunction Entry(Data);
649 DictScope RFS(SW, "RuntimeFunction");
650 if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
651 return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
652 return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
655 void Decoder::dumpProcedureData(const COFFObjectFile &COFF,
656 const SectionRef Section) {
657 ArrayRef<uint8_t> Contents;
658 if (COFF.getSectionContents(COFF.getCOFFSection(Section), Contents))
661 if (Contents.size() % PDataEntrySize) {
662 errs() << ".pdata content is not " << PDataEntrySize << "-byte aligned\n";
666 for (unsigned EI = 0, EE = Contents.size() / PDataEntrySize; EI < EE; ++EI)
667 if (!dumpProcedureDataEntry(COFF, Section, EI, Contents))
671 error_code Decoder::dumpProcedureData(const COFFObjectFile &COFF) {
672 for (const auto &Section : COFF.sections()) {
673 StringRef SectionName;
674 if (error_code EC = COFF.getSectionName(COFF.getCOFFSection(Section),
678 if (SectionName.startswith(".pdata"))
679 dumpProcedureData(COFF, Section);