1//===- DWARFCFIProgram.cpp - Parsing 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/LowLevel/DWARFCFIProgram.h"
10#include "llvm/Support/Compiler.h"
11#include "llvm/Support/Errc.h"
12#include "llvm/Support/ErrorHandling.h"
13#include "llvm/Support/raw_ostream.h"
14#include <array>
15#include <cassert>
16#include <cinttypes>
17#include <cstdint>
18
19using namespace llvm;
20using namespace dwarf;
21
22StringRef CFIProgram::callFrameString(unsigned Opcode) const {
23 return dwarf::CallFrameString(Encoding: Opcode, Arch);
24}
25
26const char *CFIProgram::operandTypeString(CFIProgram::OperandType OT) {
27#define ENUM_TO_CSTR(e) \
28 case e: \
29 return #e;
30 switch (OT) {
31 ENUM_TO_CSTR(OT_Unset);
32 ENUM_TO_CSTR(OT_None);
33 ENUM_TO_CSTR(OT_Address);
34 ENUM_TO_CSTR(OT_Offset);
35 ENUM_TO_CSTR(OT_FactoredCodeOffset);
36 ENUM_TO_CSTR(OT_SignedFactCodeOffset);
37 ENUM_TO_CSTR(OT_SignedFactDataOffset);
38 ENUM_TO_CSTR(OT_UnsignedFactDataOffset);
39 ENUM_TO_CSTR(OT_Register);
40 ENUM_TO_CSTR(OT_AddressSpace);
41 ENUM_TO_CSTR(OT_Expression);
42 ENUM_TO_CSTR(OT_RAState);
43 }
44 return "<unknown CFIProgram::OperandType>";
45}
46
47llvm::Expected<uint64_t>
48CFIProgram::Instruction::getOperandAsUnsigned(const CFIProgram &CFIP,
49 uint32_t OperandIdx) const {
50 if (OperandIdx >= MaxOperands)
51 return createStringError(EC: errc::invalid_argument,
52 Fmt: "operand index %" PRIu32 " is not valid",
53 Vals: OperandIdx);
54 OperandType Type = CFIP.getOperandTypes()[Opcode][OperandIdx];
55 uint64_t Operand = Ops[OperandIdx];
56 switch (Type) {
57 case OT_Unset:
58 case OT_None:
59 case OT_Expression:
60 return createStringError(EC: errc::invalid_argument,
61 Fmt: "op[%" PRIu32 "] has type %s which has no value",
62 Vals: OperandIdx, Vals: CFIProgram::operandTypeString(OT: Type));
63
64 case OT_Offset:
65 case OT_SignedFactDataOffset:
66 case OT_UnsignedFactDataOffset:
67 case OT_SignedFactCodeOffset:
68 return createStringError(
69 EC: errc::invalid_argument,
70 Fmt: "op[%" PRIu32 "] has OperandType OT_Offset which produces a signed "
71 "result, call getOperandAsSigned instead",
72 Vals: OperandIdx);
73
74 case OT_Address:
75 case OT_Register:
76 case OT_AddressSpace:
77 case OT_RAState:
78 return Operand;
79
80 case OT_FactoredCodeOffset: {
81 const uint64_t CodeAlignmentFactor = CFIP.codeAlign();
82 if (CodeAlignmentFactor == 0)
83 return createStringError(
84 EC: errc::invalid_argument,
85 Fmt: "op[%" PRIu32 "] has type OT_FactoredCodeOffset but code alignment "
86 "is zero",
87 Vals: OperandIdx);
88 return Operand * CodeAlignmentFactor;
89 }
90 }
91 llvm_unreachable("invalid operand type");
92}
93
94llvm::Expected<int64_t>
95CFIProgram::Instruction::getOperandAsSigned(const CFIProgram &CFIP,
96 uint32_t OperandIdx) const {
97 if (OperandIdx >= MaxOperands)
98 return createStringError(EC: errc::invalid_argument,
99 Fmt: "operand index %" PRIu32 " is not valid",
100 Vals: OperandIdx);
101 OperandType Type = CFIP.getOperandTypes()[Opcode][OperandIdx];
102 uint64_t Operand = Ops[OperandIdx];
103 switch (Type) {
104 case OT_Unset:
105 case OT_None:
106 case OT_Expression:
107 return createStringError(EC: errc::invalid_argument,
108 Fmt: "op[%" PRIu32 "] has type %s which has no value",
109 Vals: OperandIdx, Vals: CFIProgram::operandTypeString(OT: Type));
110
111 case OT_Address:
112 case OT_Register:
113 case OT_AddressSpace:
114 case OT_RAState:
115 return createStringError(
116 EC: errc::invalid_argument,
117 Fmt: "op[%" PRIu32 "] has OperandType %s which produces an unsigned result, "
118 "call getOperandAsUnsigned instead",
119 Vals: OperandIdx, Vals: CFIProgram::operandTypeString(OT: Type));
120
121 case OT_Offset:
122 return (int64_t)Operand;
123
124 case OT_FactoredCodeOffset:
125 case OT_SignedFactDataOffset: {
126 const int64_t DataAlignmentFactor = CFIP.dataAlign();
127 if (DataAlignmentFactor == 0)
128 return createStringError(EC: errc::invalid_argument,
129 Fmt: "op[%" PRIu32 "] has type %s but data "
130 "alignment is zero",
131 Vals: OperandIdx, Vals: CFIProgram::operandTypeString(OT: Type));
132 return int64_t(Operand) * DataAlignmentFactor;
133 }
134
135 case OT_SignedFactCodeOffset: {
136 const int64_t CodeAlignmentFactor = CFIP.codeAlign();
137 if (CodeAlignmentFactor == 0)
138 return createStringError(EC: errc::invalid_argument,
139 Fmt: "op[%" PRIu32 "] has type %s but code "
140 "alignment is zero",
141 Vals: OperandIdx, Vals: CFIProgram::operandTypeString(OT: Type));
142 return int64_t(Operand) * CodeAlignmentFactor;
143 }
144
145 case OT_UnsignedFactDataOffset: {
146 const int64_t DataAlignmentFactor = CFIP.dataAlign();
147 if (DataAlignmentFactor == 0)
148 return createStringError(EC: errc::invalid_argument,
149 Fmt: "op[%" PRIu32
150 "] has type OT_UnsignedFactDataOffset but data "
151 "alignment is zero",
152 Vals: OperandIdx);
153 return Operand * DataAlignmentFactor;
154 }
155 }
156 llvm_unreachable("invalid operand type");
157}
158
159ArrayRef<CFIProgram::OperandType[CFIProgram::MaxOperands]>
160CFIProgram::getOperandTypes() {
161 static constexpr auto Table = [] {
162 std::array<OperandType[MaxOperands], DW_CFA_restore + 1> OpTypes{};
163
164#define DECLARE_OP3(OP, OPTYPE0, OPTYPE1, OPTYPE2) \
165 do { \
166 OpTypes[OP][0] = OPTYPE0; \
167 OpTypes[OP][1] = OPTYPE1; \
168 OpTypes[OP][2] = OPTYPE2; \
169 } while (false)
170#define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
171 DECLARE_OP3(OP, OPTYPE0, OPTYPE1, OT_None)
172#define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
173#define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
174
175 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
176 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
177 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
178 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
179 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
180 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
181 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
182 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
183 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
184 DECLARE_OP3(DW_CFA_LLVM_def_aspace_cfa, OT_Register, OT_Offset,
185 OT_AddressSpace);
186 DECLARE_OP3(DW_CFA_LLVM_def_aspace_cfa_sf, OT_Register,
187 OT_SignedFactDataOffset, OT_AddressSpace);
188 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
189 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
190 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
191 DECLARE_OP1(DW_CFA_undefined, OT_Register);
192 DECLARE_OP1(DW_CFA_same_value, OT_Register);
193 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
194 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
195 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register,
196 OT_SignedFactDataOffset);
197 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
198 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
199 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
200 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
201 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
202 DECLARE_OP1(DW_CFA_restore, OT_Register);
203 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
204 DECLARE_OP0(DW_CFA_remember_state);
205 DECLARE_OP0(DW_CFA_restore_state);
206 DECLARE_OP0(DW_CFA_GNU_window_save);
207 DECLARE_OP0(DW_CFA_AARCH64_negate_ra_state_with_pc);
208 DECLARE_OP2(DW_CFA_AARCH64_set_ra_state, OT_RAState,
209 OT_SignedFactCodeOffset);
210 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
211 DECLARE_OP0(DW_CFA_nop);
212
213#undef DECLARE_OP0
214#undef DECLARE_OP1
215#undef DECLARE_OP2
216
217 return OpTypes;
218 }();
219 return Table;
220}
221