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