|
| 1 | +//===- DWARFCFIPrinter.cpp - Print the cfi-portions of .debug_frame -------===// |
| 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 | +#include "llvm/DebugInfo/DWARF/DWARFCFIPrinter.h" |
| 10 | +#include "llvm/DebugInfo/DIContext.h" |
| 11 | +#include "llvm/DebugInfo/DWARF/DWARFCFIProgram.h" |
| 12 | +#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" |
| 13 | +#include "llvm/Support/Compiler.h" |
| 14 | +#include "llvm/Support/DataExtractor.h" |
| 15 | +#include "llvm/Support/Errc.h" |
| 16 | +#include "llvm/Support/ErrorHandling.h" |
| 17 | +#include "llvm/Support/Format.h" |
| 18 | +#include "llvm/Support/raw_ostream.h" |
| 19 | +#include <cassert> |
| 20 | +#include <cinttypes> |
| 21 | +#include <cstdint> |
| 22 | +#include <optional> |
| 23 | + |
| 24 | +using namespace llvm; |
| 25 | +using namespace dwarf; |
| 26 | + |
| 27 | + |
| 28 | +void CFIPrinter::print(const CFIProgram &P, raw_ostream &OS, |
| 29 | + DIDumpOptions DumpOpts, unsigned IndentLevel, |
| 30 | + std::optional<uint64_t> Address) { |
| 31 | + for (const auto &Instr : P) { |
| 32 | + uint8_t Opcode = Instr.Opcode; |
| 33 | + OS.indent(2 * IndentLevel); |
| 34 | + OS << P.callFrameString(Opcode) << ":"; |
| 35 | + for (size_t i = 0; i < Instr.Ops.size(); ++i) |
| 36 | + printOperand(OS, DumpOpts, P, Instr, i, Instr.Ops[i], Address); |
| 37 | + OS << '\n'; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts, |
| 42 | + unsigned RegNum) { |
| 43 | + if (DumpOpts.GetNameForDWARFReg) { |
| 44 | + auto RegName = DumpOpts.GetNameForDWARFReg(RegNum, DumpOpts.IsEH); |
| 45 | + if (!RegName.empty()) { |
| 46 | + OS << RegName; |
| 47 | + return; |
| 48 | + } |
| 49 | + } |
| 50 | + OS << "reg" << RegNum; |
| 51 | +} |
| 52 | + |
| 53 | +/// Print \p Opcode's operand number \p OperandIdx which has value \p Operand. |
| 54 | +void CFIPrinter::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts, |
| 55 | + const CFIProgram &P, |
| 56 | + const CFIProgram::Instruction &Instr, |
| 57 | + unsigned OperandIdx, uint64_t Operand, |
| 58 | + std::optional<uint64_t> &Address) { |
| 59 | + assert(OperandIdx < CFIProgram::MaxOperands); |
| 60 | + uint8_t Opcode = Instr.Opcode; |
| 61 | + CFIProgram::OperandType Type = P.getOperandTypes()[Opcode][OperandIdx]; |
| 62 | + |
| 63 | + switch (Type) { |
| 64 | + case CFIProgram::OT_Unset: { |
| 65 | + OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to"; |
| 66 | + auto OpcodeName = P.callFrameString(Opcode); |
| 67 | + if (!OpcodeName.empty()) |
| 68 | + OS << " " << OpcodeName; |
| 69 | + else |
| 70 | + OS << format(" Opcode %x", Opcode); |
| 71 | + break; |
| 72 | + } |
| 73 | + case CFIProgram::OT_None: |
| 74 | + break; |
| 75 | + case CFIProgram::OT_Address: |
| 76 | + OS << format(" %" PRIx64, Operand); |
| 77 | + Address = Operand; |
| 78 | + break; |
| 79 | + case CFIProgram::OT_Offset: |
| 80 | + // The offsets are all encoded in a unsigned form, but in practice |
| 81 | + // consumers use them signed. It's most certainly legacy due to |
| 82 | + // the lack of signed variants in the first Dwarf standards. |
| 83 | + OS << format(" %+" PRId64, int64_t(Operand)); |
| 84 | + break; |
| 85 | + case CFIProgram::OT_FactoredCodeOffset: // Always Unsigned |
| 86 | + if (P.CodeAlignmentFactor) |
| 87 | + OS << format(" %" PRId64, Operand * P.CodeAlignmentFactor); |
| 88 | + else |
| 89 | + OS << format(" %" PRId64 "*code_alignment_factor", Operand); |
| 90 | + if (Address && P.CodeAlignmentFactor) { |
| 91 | + *Address += Operand * P.CodeAlignmentFactor; |
| 92 | + OS << format(" to 0x%" PRIx64, *Address); |
| 93 | + } |
| 94 | + break; |
| 95 | + case CFIProgram::OT_SignedFactDataOffset: |
| 96 | + if (P.DataAlignmentFactor) |
| 97 | + OS << format(" %" PRId64, int64_t(Operand) * P.DataAlignmentFactor); |
| 98 | + else |
| 99 | + OS << format(" %" PRId64 "*data_alignment_factor", int64_t(Operand)); |
| 100 | + break; |
| 101 | + case CFIProgram::OT_UnsignedFactDataOffset: |
| 102 | + if (P.DataAlignmentFactor) |
| 103 | + OS << format(" %" PRId64, Operand * P.DataAlignmentFactor); |
| 104 | + else |
| 105 | + OS << format(" %" PRId64 "*data_alignment_factor", Operand); |
| 106 | + break; |
| 107 | + case CFIProgram::OT_Register: |
| 108 | + OS << ' '; |
| 109 | + printRegister(OS, DumpOpts, Operand); |
| 110 | + break; |
| 111 | + case CFIProgram::OT_AddressSpace: |
| 112 | + OS << format(" in addrspace%" PRId64, Operand); |
| 113 | + break; |
| 114 | + case CFIProgram::OT_Expression: |
| 115 | + assert(Instr.Expression && "missing DWARFExpression object"); |
| 116 | + OS << " "; |
| 117 | + DWARFExpressionPrinter::print(&Instr.Expression.value(), OS, DumpOpts, |
| 118 | + nullptr); |
| 119 | + break; |
| 120 | + } |
| 121 | +} |
0 commit comments