17a7e6055SDimitry Andric //===- DWARFDebugFrame.h - Parsing of .debug_frame ------------------------===//
2ff0cc061SDimitry Andric //
3ff0cc061SDimitry Andric // The LLVM Compiler Infrastructure
4ff0cc061SDimitry Andric //
5ff0cc061SDimitry Andric // This file is distributed under the University of Illinois Open Source
6ff0cc061SDimitry Andric // License. See LICENSE.TXT for details.
7ff0cc061SDimitry Andric //
8ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
9ff0cc061SDimitry Andric
10db17bf38SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
11ff0cc061SDimitry Andric #include "llvm/ADT/DenseMap.h"
123ca95b02SDimitry Andric #include "llvm/ADT/Optional.h"
133ca95b02SDimitry Andric #include "llvm/ADT/StringExtras.h"
14d88c1a5aSDimitry Andric #include "llvm/ADT/StringRef.h"
15db17bf38SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
16ff0cc061SDimitry Andric #include "llvm/Support/Casting.h"
17d88c1a5aSDimitry Andric #include "llvm/Support/Compiler.h"
18d88c1a5aSDimitry Andric #include "llvm/Support/DataExtractor.h"
19b5893f02SDimitry Andric #include "llvm/Support/Errc.h"
20ff0cc061SDimitry Andric #include "llvm/Support/ErrorHandling.h"
21ff0cc061SDimitry Andric #include "llvm/Support/Format.h"
22ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h"
23d88c1a5aSDimitry Andric #include <algorithm>
24d88c1a5aSDimitry Andric #include <cassert>
25d88c1a5aSDimitry Andric #include <cinttypes>
26d88c1a5aSDimitry Andric #include <cstdint>
27ff0cc061SDimitry Andric #include <string>
28ff0cc061SDimitry Andric #include <vector>
29ff0cc061SDimitry Andric
30ff0cc061SDimitry Andric using namespace llvm;
31ff0cc061SDimitry Andric using namespace dwarf;
32ff0cc061SDimitry Andric
33ff0cc061SDimitry Andric
34ff0cc061SDimitry Andric // See DWARF standard v3, section 7.23
35ff0cc061SDimitry Andric const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
36ff0cc061SDimitry Andric const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
37ff0cc061SDimitry Andric
parse(DataExtractor Data,uint32_t * Offset,uint32_t EndOffset)384ba319b5SDimitry Andric Error CFIProgram::parse(DataExtractor Data, uint32_t *Offset,
39ff0cc061SDimitry Andric uint32_t EndOffset) {
40ff0cc061SDimitry Andric while (*Offset < EndOffset) {
41ff0cc061SDimitry Andric uint8_t Opcode = Data.getU8(Offset);
42ff0cc061SDimitry Andric // Some instructions have a primary opcode encoded in the top bits.
43ff0cc061SDimitry Andric uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
44ff0cc061SDimitry Andric
45ff0cc061SDimitry Andric if (Primary) {
46ff0cc061SDimitry Andric // If it's a primary opcode, the first operand is encoded in the bottom
47ff0cc061SDimitry Andric // bits of the opcode itself.
48ff0cc061SDimitry Andric uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
49ff0cc061SDimitry Andric switch (Primary) {
504ba319b5SDimitry Andric default:
51b5893f02SDimitry Andric return createStringError(errc::illegal_byte_sequence,
52b5893f02SDimitry Andric "Invalid primary CFI opcode 0x%" PRIx8,
53b5893f02SDimitry Andric Primary);
54ff0cc061SDimitry Andric case DW_CFA_advance_loc:
55ff0cc061SDimitry Andric case DW_CFA_restore:
56ff0cc061SDimitry Andric addInstruction(Primary, Op1);
57ff0cc061SDimitry Andric break;
58ff0cc061SDimitry Andric case DW_CFA_offset:
59ff0cc061SDimitry Andric addInstruction(Primary, Op1, Data.getULEB128(Offset));
60ff0cc061SDimitry Andric break;
61ff0cc061SDimitry Andric }
62ff0cc061SDimitry Andric } else {
63ff0cc061SDimitry Andric // Extended opcode - its value is Opcode itself.
64ff0cc061SDimitry Andric switch (Opcode) {
654ba319b5SDimitry Andric default:
66b5893f02SDimitry Andric return createStringError(errc::illegal_byte_sequence,
67b5893f02SDimitry Andric "Invalid extended CFI opcode 0x%" PRIx8,
68b5893f02SDimitry Andric Opcode);
69ff0cc061SDimitry Andric case DW_CFA_nop:
70ff0cc061SDimitry Andric case DW_CFA_remember_state:
71ff0cc061SDimitry Andric case DW_CFA_restore_state:
72ff0cc061SDimitry Andric case DW_CFA_GNU_window_save:
73ff0cc061SDimitry Andric // No operands
74ff0cc061SDimitry Andric addInstruction(Opcode);
75ff0cc061SDimitry Andric break;
76ff0cc061SDimitry Andric case DW_CFA_set_loc:
77ff0cc061SDimitry Andric // Operands: Address
78ff0cc061SDimitry Andric addInstruction(Opcode, Data.getAddress(Offset));
79ff0cc061SDimitry Andric break;
80ff0cc061SDimitry Andric case DW_CFA_advance_loc1:
81ff0cc061SDimitry Andric // Operands: 1-byte delta
82ff0cc061SDimitry Andric addInstruction(Opcode, Data.getU8(Offset));
83ff0cc061SDimitry Andric break;
84ff0cc061SDimitry Andric case DW_CFA_advance_loc2:
85ff0cc061SDimitry Andric // Operands: 2-byte delta
86ff0cc061SDimitry Andric addInstruction(Opcode, Data.getU16(Offset));
87ff0cc061SDimitry Andric break;
88ff0cc061SDimitry Andric case DW_CFA_advance_loc4:
89ff0cc061SDimitry Andric // Operands: 4-byte delta
90ff0cc061SDimitry Andric addInstruction(Opcode, Data.getU32(Offset));
91ff0cc061SDimitry Andric break;
92ff0cc061SDimitry Andric case DW_CFA_restore_extended:
93ff0cc061SDimitry Andric case DW_CFA_undefined:
94ff0cc061SDimitry Andric case DW_CFA_same_value:
95ff0cc061SDimitry Andric case DW_CFA_def_cfa_register:
96ff0cc061SDimitry Andric case DW_CFA_def_cfa_offset:
972cab237bSDimitry Andric case DW_CFA_GNU_args_size:
98ff0cc061SDimitry Andric // Operands: ULEB128
99ff0cc061SDimitry Andric addInstruction(Opcode, Data.getULEB128(Offset));
100ff0cc061SDimitry Andric break;
101ff0cc061SDimitry Andric case DW_CFA_def_cfa_offset_sf:
102ff0cc061SDimitry Andric // Operands: SLEB128
103ff0cc061SDimitry Andric addInstruction(Opcode, Data.getSLEB128(Offset));
104ff0cc061SDimitry Andric break;
105ff0cc061SDimitry Andric case DW_CFA_offset_extended:
106ff0cc061SDimitry Andric case DW_CFA_register:
107ff0cc061SDimitry Andric case DW_CFA_def_cfa:
1083ca95b02SDimitry Andric case DW_CFA_val_offset: {
109ff0cc061SDimitry Andric // Operands: ULEB128, ULEB128
1103ca95b02SDimitry Andric // Note: We can not embed getULEB128 directly into function
1113ca95b02SDimitry Andric // argument list. getULEB128 changes Offset and order of evaluation
1123ca95b02SDimitry Andric // for arguments is unspecified.
1133ca95b02SDimitry Andric auto op1 = Data.getULEB128(Offset);
1143ca95b02SDimitry Andric auto op2 = Data.getULEB128(Offset);
1153ca95b02SDimitry Andric addInstruction(Opcode, op1, op2);
116ff0cc061SDimitry Andric break;
1173ca95b02SDimitry Andric }
118ff0cc061SDimitry Andric case DW_CFA_offset_extended_sf:
119ff0cc061SDimitry Andric case DW_CFA_def_cfa_sf:
1203ca95b02SDimitry Andric case DW_CFA_val_offset_sf: {
121ff0cc061SDimitry Andric // Operands: ULEB128, SLEB128
1223ca95b02SDimitry Andric // Note: see comment for the previous case
1233ca95b02SDimitry Andric auto op1 = Data.getULEB128(Offset);
1243ca95b02SDimitry Andric auto op2 = (uint64_t)Data.getSLEB128(Offset);
1253ca95b02SDimitry Andric addInstruction(Opcode, op1, op2);
126ff0cc061SDimitry Andric break;
1273ca95b02SDimitry Andric }
1284ba319b5SDimitry Andric case DW_CFA_def_cfa_expression: {
1294ba319b5SDimitry Andric uint32_t ExprLength = Data.getULEB128(Offset);
1304ba319b5SDimitry Andric addInstruction(Opcode, 0);
1314ba319b5SDimitry Andric DataExtractor Extractor(
1324ba319b5SDimitry Andric Data.getData().slice(*Offset, *Offset + ExprLength),
1334ba319b5SDimitry Andric Data.isLittleEndian(), Data.getAddressSize());
1344ba319b5SDimitry Andric Instructions.back().Expression = DWARFExpression(
1354ba319b5SDimitry Andric Extractor, Data.getAddressSize(), dwarf::DWARF_VERSION);
1364ba319b5SDimitry Andric *Offset += ExprLength;
1372cab237bSDimitry Andric break;
1384ba319b5SDimitry Andric }
139ff0cc061SDimitry Andric case DW_CFA_expression:
1402cab237bSDimitry Andric case DW_CFA_val_expression: {
1414ba319b5SDimitry Andric auto RegNum = Data.getULEB128(Offset);
1424ba319b5SDimitry Andric auto BlockLength = Data.getULEB128(Offset);
1434ba319b5SDimitry Andric addInstruction(Opcode, RegNum, 0);
1444ba319b5SDimitry Andric DataExtractor Extractor(
1454ba319b5SDimitry Andric Data.getData().slice(*Offset, *Offset + BlockLength),
1464ba319b5SDimitry Andric Data.isLittleEndian(), Data.getAddressSize());
1474ba319b5SDimitry Andric Instructions.back().Expression = DWARFExpression(
1484ba319b5SDimitry Andric Extractor, Data.getAddressSize(), dwarf::DWARF_VERSION);
1494ba319b5SDimitry Andric *Offset += BlockLength;
1502cab237bSDimitry Andric break;
1512cab237bSDimitry Andric }
152ff0cc061SDimitry Andric }
153ff0cc061SDimitry Andric }
154ff0cc061SDimitry Andric }
1554ba319b5SDimitry Andric
1564ba319b5SDimitry Andric return Error::success();
157ff0cc061SDimitry Andric }
158ff0cc061SDimitry Andric
159ff0cc061SDimitry Andric namespace {
160d88c1a5aSDimitry Andric
161ff0cc061SDimitry Andric
162ff0cc061SDimitry Andric } // end anonymous namespace
163ff0cc061SDimitry Andric
getOperandTypes()1644ba319b5SDimitry Andric ArrayRef<CFIProgram::OperandType[2]> CFIProgram::getOperandTypes() {
165ff0cc061SDimitry Andric static OperandType OpTypes[DW_CFA_restore+1][2];
1664ba319b5SDimitry Andric static bool Initialized = false;
1674ba319b5SDimitry Andric if (Initialized) {
1684ba319b5SDimitry Andric return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
1694ba319b5SDimitry Andric }
1704ba319b5SDimitry Andric Initialized = true;
171ff0cc061SDimitry Andric
172ff0cc061SDimitry Andric #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
173ff0cc061SDimitry Andric do { \
174ff0cc061SDimitry Andric OpTypes[OP][0] = OPTYPE0; \
175ff0cc061SDimitry Andric OpTypes[OP][1] = OPTYPE1; \
176d88c1a5aSDimitry Andric } while (false)
177ff0cc061SDimitry Andric #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
178ff0cc061SDimitry Andric #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
179ff0cc061SDimitry Andric
180ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_set_loc, OT_Address);
181ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
182ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
183ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
184ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
185ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
186ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
187ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
188ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
189ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
190ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
191ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
192ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_undefined, OT_Register);
193ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_same_value, OT_Register);
194ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
195ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
196ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
197ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
198ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
199ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
200ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
201ff0cc061SDimitry Andric DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
202ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_restore, OT_Register);
203ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
204ff0cc061SDimitry Andric DECLARE_OP0(DW_CFA_remember_state);
205ff0cc061SDimitry Andric DECLARE_OP0(DW_CFA_restore_state);
206ff0cc061SDimitry Andric DECLARE_OP0(DW_CFA_GNU_window_save);
207ff0cc061SDimitry Andric DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
208ff0cc061SDimitry Andric DECLARE_OP0(DW_CFA_nop);
209ff0cc061SDimitry Andric
210ff0cc061SDimitry Andric #undef DECLARE_OP0
211ff0cc061SDimitry Andric #undef DECLARE_OP1
212ff0cc061SDimitry Andric #undef DECLARE_OP2
213d88c1a5aSDimitry Andric
214ff0cc061SDimitry Andric return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
215ff0cc061SDimitry Andric }
216ff0cc061SDimitry Andric
2174ba319b5SDimitry Andric /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
printOperand(raw_ostream & OS,const MCRegisterInfo * MRI,bool IsEH,const Instruction & Instr,unsigned OperandIdx,uint64_t Operand) const2184ba319b5SDimitry Andric void CFIProgram::printOperand(raw_ostream &OS, const MCRegisterInfo *MRI,
2194ba319b5SDimitry Andric bool IsEH, const Instruction &Instr,
2204ba319b5SDimitry Andric unsigned OperandIdx, uint64_t Operand) const {
221ff0cc061SDimitry Andric assert(OperandIdx < 2);
2224ba319b5SDimitry Andric uint8_t Opcode = Instr.Opcode;
2234ba319b5SDimitry Andric OperandType Type = getOperandTypes()[Opcode][OperandIdx];
224ff0cc061SDimitry Andric
225ff0cc061SDimitry Andric switch (Type) {
226d88c1a5aSDimitry Andric case OT_Unset: {
227ff0cc061SDimitry Andric OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
228b5893f02SDimitry Andric auto OpcodeName = CallFrameString(Opcode, Arch);
229d88c1a5aSDimitry Andric if (!OpcodeName.empty())
230ff0cc061SDimitry Andric OS << " " << OpcodeName;
231ff0cc061SDimitry Andric else
232ff0cc061SDimitry Andric OS << format(" Opcode %x", Opcode);
233ff0cc061SDimitry Andric break;
234d88c1a5aSDimitry Andric }
235ff0cc061SDimitry Andric case OT_None:
236ff0cc061SDimitry Andric break;
237ff0cc061SDimitry Andric case OT_Address:
238ff0cc061SDimitry Andric OS << format(" %" PRIx64, Operand);
239ff0cc061SDimitry Andric break;
240ff0cc061SDimitry Andric case OT_Offset:
241ff0cc061SDimitry Andric // The offsets are all encoded in a unsigned form, but in practice
242ff0cc061SDimitry Andric // consumers use them signed. It's most certainly legacy due to
243ff0cc061SDimitry Andric // the lack of signed variants in the first Dwarf standards.
244ff0cc061SDimitry Andric OS << format(" %+" PRId64, int64_t(Operand));
245ff0cc061SDimitry Andric break;
246ff0cc061SDimitry Andric case OT_FactoredCodeOffset: // Always Unsigned
247ff0cc061SDimitry Andric if (CodeAlignmentFactor)
248ff0cc061SDimitry Andric OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
249ff0cc061SDimitry Andric else
250ff0cc061SDimitry Andric OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
251ff0cc061SDimitry Andric break;
252ff0cc061SDimitry Andric case OT_SignedFactDataOffset:
253ff0cc061SDimitry Andric if (DataAlignmentFactor)
254ff0cc061SDimitry Andric OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
255ff0cc061SDimitry Andric else
256ff0cc061SDimitry Andric OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
257ff0cc061SDimitry Andric break;
258ff0cc061SDimitry Andric case OT_UnsignedFactDataOffset:
259ff0cc061SDimitry Andric if (DataAlignmentFactor)
260ff0cc061SDimitry Andric OS << format(" %" PRId64, Operand * DataAlignmentFactor);
261ff0cc061SDimitry Andric else
262ff0cc061SDimitry Andric OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
263ff0cc061SDimitry Andric break;
264ff0cc061SDimitry Andric case OT_Register:
265ff0cc061SDimitry Andric OS << format(" reg%" PRId64, Operand);
266ff0cc061SDimitry Andric break;
267ff0cc061SDimitry Andric case OT_Expression:
2684ba319b5SDimitry Andric assert(Instr.Expression && "missing DWARFExpression object");
2694ba319b5SDimitry Andric OS << " ";
2704ba319b5SDimitry Andric Instr.Expression->print(OS, MRI, IsEH);
271ff0cc061SDimitry Andric break;
272ff0cc061SDimitry Andric }
273ff0cc061SDimitry Andric }
274ff0cc061SDimitry Andric
dump(raw_ostream & OS,const MCRegisterInfo * MRI,bool IsEH,unsigned IndentLevel) const2754ba319b5SDimitry Andric void CFIProgram::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
2764ba319b5SDimitry Andric unsigned IndentLevel) const {
277ff0cc061SDimitry Andric for (const auto &Instr : Instructions) {
278ff0cc061SDimitry Andric uint8_t Opcode = Instr.Opcode;
279ff0cc061SDimitry Andric if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
280ff0cc061SDimitry Andric Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
2814ba319b5SDimitry Andric OS.indent(2 * IndentLevel);
282b5893f02SDimitry Andric OS << CallFrameString(Opcode, Arch) << ":";
283ff0cc061SDimitry Andric for (unsigned i = 0; i < Instr.Ops.size(); ++i)
2844ba319b5SDimitry Andric printOperand(OS, MRI, IsEH, Instr, i, Instr.Ops[i]);
285ff0cc061SDimitry Andric OS << '\n';
286ff0cc061SDimitry Andric }
287ff0cc061SDimitry Andric }
288ff0cc061SDimitry Andric
dump(raw_ostream & OS,const MCRegisterInfo * MRI,bool IsEH) const2894ba319b5SDimitry Andric void CIE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const {
2904ba319b5SDimitry Andric OS << format("%08x %08x %08x CIE", (uint32_t)Offset, (uint32_t)Length,
2914ba319b5SDimitry Andric DW_CIE_ID)
2924ba319b5SDimitry Andric << "\n";
2934ba319b5SDimitry Andric OS << format(" Version: %d\n", Version);
2944ba319b5SDimitry Andric OS << " Augmentation: \"" << Augmentation << "\"\n";
2954ba319b5SDimitry Andric if (Version >= 4) {
2964ba319b5SDimitry Andric OS << format(" Address size: %u\n", (uint32_t)AddressSize);
2974ba319b5SDimitry Andric OS << format(" Segment desc size: %u\n",
2984ba319b5SDimitry Andric (uint32_t)SegmentDescriptorSize);
2994ba319b5SDimitry Andric }
3004ba319b5SDimitry Andric OS << format(" Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor);
3014ba319b5SDimitry Andric OS << format(" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor);
3024ba319b5SDimitry Andric OS << format(" Return address column: %d\n", (int32_t)ReturnAddressRegister);
3034ba319b5SDimitry Andric if (Personality)
304*85573313SDimitry Andric OS << format(" Personality Address: %016" PRIx64 "\n", *Personality);
3054ba319b5SDimitry Andric if (!AugmentationData.empty()) {
3064ba319b5SDimitry Andric OS << " Augmentation data: ";
3074ba319b5SDimitry Andric for (uint8_t Byte : AugmentationData)
3084ba319b5SDimitry Andric OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
3094ba319b5SDimitry Andric OS << "\n";
3104ba319b5SDimitry Andric }
3114ba319b5SDimitry Andric OS << "\n";
3124ba319b5SDimitry Andric CFIs.dump(OS, MRI, IsEH);
3134ba319b5SDimitry Andric OS << "\n";
3144ba319b5SDimitry Andric }
3154ba319b5SDimitry Andric
dump(raw_ostream & OS,const MCRegisterInfo * MRI,bool IsEH) const3164ba319b5SDimitry Andric void FDE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const {
3174ba319b5SDimitry Andric OS << format("%08x %08x %08x FDE ", (uint32_t)Offset, (uint32_t)Length,
3184ba319b5SDimitry Andric (int32_t)LinkedCIEOffset);
3194ba319b5SDimitry Andric OS << format("cie=%08x pc=%08x...%08x\n", (int32_t)LinkedCIEOffset,
3204ba319b5SDimitry Andric (uint32_t)InitialLocation,
3214ba319b5SDimitry Andric (uint32_t)InitialLocation + (uint32_t)AddressRange);
3224ba319b5SDimitry Andric if (LSDAAddress)
323*85573313SDimitry Andric OS << format(" LSDA Address: %016" PRIx64 "\n", *LSDAAddress);
3244ba319b5SDimitry Andric CFIs.dump(OS, MRI, IsEH);
3254ba319b5SDimitry Andric OS << "\n";
3264ba319b5SDimitry Andric }
3274ba319b5SDimitry Andric
DWARFDebugFrame(Triple::ArchType Arch,bool IsEH,uint64_t EHFrameAddress)328b5893f02SDimitry Andric DWARFDebugFrame::DWARFDebugFrame(Triple::ArchType Arch,
329b5893f02SDimitry Andric bool IsEH, uint64_t EHFrameAddress)
330b5893f02SDimitry Andric : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
331ff0cc061SDimitry Andric
332d88c1a5aSDimitry Andric DWARFDebugFrame::~DWARFDebugFrame() = default;
333ff0cc061SDimitry Andric
dumpDataAux(DataExtractor Data,uint32_t Offset,int Length)334ff0cc061SDimitry Andric static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
335ff0cc061SDimitry Andric uint32_t Offset, int Length) {
336ff0cc061SDimitry Andric errs() << "DUMP: ";
337ff0cc061SDimitry Andric for (int i = 0; i < Length; ++i) {
338ff0cc061SDimitry Andric uint8_t c = Data.getU8(&Offset);
339ff0cc061SDimitry Andric errs().write_hex(c); errs() << " ";
340ff0cc061SDimitry Andric }
341ff0cc061SDimitry Andric errs() << "\n";
342ff0cc061SDimitry Andric }
343ff0cc061SDimitry Andric
34424d58133SDimitry Andric // This is a workaround for old compilers which do not allow
34524d58133SDimitry Andric // noreturn attribute usage in lambdas. Once the support for those
34624d58133SDimitry Andric // compilers are phased out, we can remove this and return back to
34724d58133SDimitry Andric // a ReportError lambda: [StartOffset](const char *ErrorMsg).
ReportError(uint32_t StartOffset,const char * ErrorMsg)348edd7eaddSDimitry Andric static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset,
349edd7eaddSDimitry Andric const char *ErrorMsg) {
35024d58133SDimitry Andric std::string Str;
35124d58133SDimitry Andric raw_string_ostream OS(Str);
35224d58133SDimitry Andric OS << format(ErrorMsg, StartOffset);
35324d58133SDimitry Andric OS.flush();
35424d58133SDimitry Andric report_fatal_error(Str);
35524d58133SDimitry Andric }
35624d58133SDimitry Andric
parse(DWARFDataExtractor Data)3574ba319b5SDimitry Andric void DWARFDebugFrame::parse(DWARFDataExtractor Data) {
358ff0cc061SDimitry Andric uint32_t Offset = 0;
359ff0cc061SDimitry Andric DenseMap<uint32_t, CIE *> CIEs;
360ff0cc061SDimitry Andric
361ff0cc061SDimitry Andric while (Data.isValidOffset(Offset)) {
362ff0cc061SDimitry Andric uint32_t StartOffset = Offset;
363ff0cc061SDimitry Andric
364ff0cc061SDimitry Andric bool IsDWARF64 = false;
365ff0cc061SDimitry Andric uint64_t Length = Data.getU32(&Offset);
366ff0cc061SDimitry Andric uint64_t Id;
367ff0cc061SDimitry Andric
368ff0cc061SDimitry Andric if (Length == UINT32_MAX) {
369ff0cc061SDimitry Andric // DWARF-64 is distinguished by the first 32 bits of the initial length
370ff0cc061SDimitry Andric // field being 0xffffffff. Then, the next 64 bits are the actual entry
371ff0cc061SDimitry Andric // length.
372ff0cc061SDimitry Andric IsDWARF64 = true;
373ff0cc061SDimitry Andric Length = Data.getU64(&Offset);
374ff0cc061SDimitry Andric }
375ff0cc061SDimitry Andric
376ff0cc061SDimitry Andric // At this point, Offset points to the next field after Length.
377ff0cc061SDimitry Andric // Length is the structure size excluding itself. Compute an offset one
378ff0cc061SDimitry Andric // past the end of the structure (needed to know how many instructions to
379ff0cc061SDimitry Andric // read).
380ff0cc061SDimitry Andric // TODO: For honest DWARF64 support, DataExtractor will have to treat
381ff0cc061SDimitry Andric // offset_ptr as uint64_t*
3823ca95b02SDimitry Andric uint32_t StartStructureOffset = Offset;
383ff0cc061SDimitry Andric uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
384ff0cc061SDimitry Andric
385ff0cc061SDimitry Andric // The Id field's size depends on the DWARF format
3863ca95b02SDimitry Andric Id = Data.getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4);
3874ba319b5SDimitry Andric bool IsCIE =
3884ba319b5SDimitry Andric ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID || (IsEH && !Id));
389ff0cc061SDimitry Andric
390ff0cc061SDimitry Andric if (IsCIE) {
391ff0cc061SDimitry Andric uint8_t Version = Data.getU8(&Offset);
392ff0cc061SDimitry Andric const char *Augmentation = Data.getCStr(&Offset);
3933ca95b02SDimitry Andric StringRef AugmentationString(Augmentation ? Augmentation : "");
3943ca95b02SDimitry Andric uint8_t AddressSize = Version < 4 ? Data.getAddressSize() :
3953ca95b02SDimitry Andric Data.getU8(&Offset);
396ff0cc061SDimitry Andric Data.setAddressSize(AddressSize);
397ff0cc061SDimitry Andric uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
398ff0cc061SDimitry Andric uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
399ff0cc061SDimitry Andric int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
400b5893f02SDimitry Andric uint64_t ReturnAddressRegister =
401b5893f02SDimitry Andric Version == 1 ? Data.getU8(&Offset) : Data.getULEB128(&Offset);
402ff0cc061SDimitry Andric
4033ca95b02SDimitry Andric // Parse the augmentation data for EH CIEs
4043ca95b02SDimitry Andric StringRef AugmentationData("");
4054ba319b5SDimitry Andric uint32_t FDEPointerEncoding = DW_EH_PE_absptr;
4063ca95b02SDimitry Andric uint32_t LSDAPointerEncoding = DW_EH_PE_omit;
4073ca95b02SDimitry Andric Optional<uint64_t> Personality;
4084ba319b5SDimitry Andric Optional<uint32_t> PersonalityEncoding;
4094ba319b5SDimitry Andric if (IsEH) {
4103ca95b02SDimitry Andric Optional<uint64_t> AugmentationLength;
4113ca95b02SDimitry Andric uint32_t StartAugmentationOffset;
4123ca95b02SDimitry Andric uint32_t EndAugmentationOffset;
4133ca95b02SDimitry Andric
4143ca95b02SDimitry Andric // Walk the augmentation string to get all the augmentation data.
4153ca95b02SDimitry Andric for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) {
4163ca95b02SDimitry Andric switch (AugmentationString[i]) {
4173ca95b02SDimitry Andric default:
418edd7eaddSDimitry Andric ReportError(StartOffset,
419edd7eaddSDimitry Andric "Unknown augmentation character in entry at %lx");
4203ca95b02SDimitry Andric case 'L':
4213ca95b02SDimitry Andric LSDAPointerEncoding = Data.getU8(&Offset);
4223ca95b02SDimitry Andric break;
4233ca95b02SDimitry Andric case 'P': {
4243ca95b02SDimitry Andric if (Personality)
425edd7eaddSDimitry Andric ReportError(StartOffset,
426edd7eaddSDimitry Andric "Duplicate personality in entry at %lx");
4273ca95b02SDimitry Andric PersonalityEncoding = Data.getU8(&Offset);
4284ba319b5SDimitry Andric Personality = Data.getEncodedPointer(
4294ba319b5SDimitry Andric &Offset, *PersonalityEncoding,
4304ba319b5SDimitry Andric EHFrameAddress ? EHFrameAddress + Offset : 0);
4313ca95b02SDimitry Andric break;
4323ca95b02SDimitry Andric }
4333ca95b02SDimitry Andric case 'R':
4343ca95b02SDimitry Andric FDEPointerEncoding = Data.getU8(&Offset);
4353ca95b02SDimitry Andric break;
4364ba319b5SDimitry Andric case 'S':
4374ba319b5SDimitry Andric // Current frame is a signal trampoline.
4384ba319b5SDimitry Andric break;
4393ca95b02SDimitry Andric case 'z':
4403ca95b02SDimitry Andric if (i)
441edd7eaddSDimitry Andric ReportError(StartOffset,
442edd7eaddSDimitry Andric "'z' must be the first character at %lx");
4433ca95b02SDimitry Andric // Parse the augmentation length first. We only parse it if
4443ca95b02SDimitry Andric // the string contains a 'z'.
4453ca95b02SDimitry Andric AugmentationLength = Data.getULEB128(&Offset);
4463ca95b02SDimitry Andric StartAugmentationOffset = Offset;
4473ca95b02SDimitry Andric EndAugmentationOffset = Offset +
4483ca95b02SDimitry Andric static_cast<uint32_t>(*AugmentationLength);
449b5893f02SDimitry Andric break;
450b5893f02SDimitry Andric case 'B':
451b5893f02SDimitry Andric // B-Key is used for signing functions associated with this
452b5893f02SDimitry Andric // augmentation string
453b5893f02SDimitry Andric break;
4543ca95b02SDimitry Andric }
4553ca95b02SDimitry Andric }
4563ca95b02SDimitry Andric
4573ca95b02SDimitry Andric if (AugmentationLength.hasValue()) {
4583ca95b02SDimitry Andric if (Offset != EndAugmentationOffset)
459edd7eaddSDimitry Andric ReportError(StartOffset, "Parsing augmentation data at %lx failed");
4603ca95b02SDimitry Andric
4613ca95b02SDimitry Andric AugmentationData = Data.getData().slice(StartAugmentationOffset,
4623ca95b02SDimitry Andric EndAugmentationOffset);
4633ca95b02SDimitry Andric }
4643ca95b02SDimitry Andric }
4653ca95b02SDimitry Andric
4664ba319b5SDimitry Andric auto Cie = llvm::make_unique<CIE>(
4674ba319b5SDimitry Andric StartOffset, Length, Version, AugmentationString, AddressSize,
4684ba319b5SDimitry Andric SegmentDescriptorSize, CodeAlignmentFactor, DataAlignmentFactor,
4694ba319b5SDimitry Andric ReturnAddressRegister, AugmentationData, FDEPointerEncoding,
470b5893f02SDimitry Andric LSDAPointerEncoding, Personality, PersonalityEncoding, Arch);
471ff0cc061SDimitry Andric CIEs[StartOffset] = Cie.get();
472ff0cc061SDimitry Andric Entries.emplace_back(std::move(Cie));
473ff0cc061SDimitry Andric } else {
474ff0cc061SDimitry Andric // FDE
475ff0cc061SDimitry Andric uint64_t CIEPointer = Id;
4763ca95b02SDimitry Andric uint64_t InitialLocation = 0;
4773ca95b02SDimitry Andric uint64_t AddressRange = 0;
4784ba319b5SDimitry Andric Optional<uint64_t> LSDAAddress;
4793ca95b02SDimitry Andric CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
4803ca95b02SDimitry Andric
4813ca95b02SDimitry Andric if (IsEH) {
4823ca95b02SDimitry Andric // The address size is encoded in the CIE we reference.
4833ca95b02SDimitry Andric if (!Cie)
484edd7eaddSDimitry Andric ReportError(StartOffset,
485edd7eaddSDimitry Andric "Parsing FDE data at %lx failed due to missing CIE");
4863ca95b02SDimitry Andric
4874ba319b5SDimitry Andric if (auto Val = Data.getEncodedPointer(
4884ba319b5SDimitry Andric &Offset, Cie->getFDEPointerEncoding(),
4894ba319b5SDimitry Andric EHFrameAddress ? EHFrameAddress + Offset : 0)) {
4904ba319b5SDimitry Andric InitialLocation = *Val;
4914ba319b5SDimitry Andric }
4924ba319b5SDimitry Andric if (auto Val = Data.getEncodedPointer(
4934ba319b5SDimitry Andric &Offset, Cie->getFDEPointerEncoding(), 0)) {
4944ba319b5SDimitry Andric AddressRange = *Val;
4954ba319b5SDimitry Andric }
4963ca95b02SDimitry Andric
4973ca95b02SDimitry Andric StringRef AugmentationString = Cie->getAugmentationString();
4983ca95b02SDimitry Andric if (!AugmentationString.empty()) {
4993ca95b02SDimitry Andric // Parse the augmentation length and data for this FDE.
5003ca95b02SDimitry Andric uint64_t AugmentationLength = Data.getULEB128(&Offset);
5013ca95b02SDimitry Andric
5023ca95b02SDimitry Andric uint32_t EndAugmentationOffset =
5033ca95b02SDimitry Andric Offset + static_cast<uint32_t>(AugmentationLength);
5043ca95b02SDimitry Andric
5053ca95b02SDimitry Andric // Decode the LSDA if the CIE augmentation string said we should.
5064ba319b5SDimitry Andric if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) {
5074ba319b5SDimitry Andric LSDAAddress = Data.getEncodedPointer(
5084ba319b5SDimitry Andric &Offset, Cie->getLSDAPointerEncoding(),
5094ba319b5SDimitry Andric EHFrameAddress ? Offset + EHFrameAddress : 0);
5104ba319b5SDimitry Andric }
5113ca95b02SDimitry Andric
5123ca95b02SDimitry Andric if (Offset != EndAugmentationOffset)
513edd7eaddSDimitry Andric ReportError(StartOffset, "Parsing augmentation data at %lx failed");
5143ca95b02SDimitry Andric }
5153ca95b02SDimitry Andric } else {
5163ca95b02SDimitry Andric InitialLocation = Data.getAddress(&Offset);
5173ca95b02SDimitry Andric AddressRange = Data.getAddress(&Offset);
5183ca95b02SDimitry Andric }
519ff0cc061SDimitry Andric
520ff0cc061SDimitry Andric Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
521ff0cc061SDimitry Andric InitialLocation, AddressRange,
522b5893f02SDimitry Andric Cie, LSDAAddress, Arch));
523ff0cc061SDimitry Andric }
524ff0cc061SDimitry Andric
5254ba319b5SDimitry Andric if (Error E =
5264ba319b5SDimitry Andric Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) {
5274ba319b5SDimitry Andric report_fatal_error(toString(std::move(E)));
5284ba319b5SDimitry Andric }
529ff0cc061SDimitry Andric
5303ca95b02SDimitry Andric if (Offset != EndStructureOffset)
531edd7eaddSDimitry Andric ReportError(StartOffset, "Parsing entry instructions at %lx failed");
532ff0cc061SDimitry Andric }
533ff0cc061SDimitry Andric }
534ff0cc061SDimitry Andric
getEntryAtOffset(uint64_t Offset) const5352cab237bSDimitry Andric FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
5362cab237bSDimitry Andric auto It =
5372cab237bSDimitry Andric std::lower_bound(Entries.begin(), Entries.end(), Offset,
5382cab237bSDimitry Andric [](const std::unique_ptr<FrameEntry> &E,
5392cab237bSDimitry Andric uint64_t Offset) { return E->getOffset() < Offset; });
5402cab237bSDimitry Andric if (It != Entries.end() && (*It)->getOffset() == Offset)
5412cab237bSDimitry Andric return It->get();
5422cab237bSDimitry Andric return nullptr;
543ff0cc061SDimitry Andric }
5442cab237bSDimitry Andric
dump(raw_ostream & OS,const MCRegisterInfo * MRI,Optional<uint64_t> Offset) const5454ba319b5SDimitry Andric void DWARFDebugFrame::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
5464ba319b5SDimitry Andric Optional<uint64_t> Offset) const {
5472cab237bSDimitry Andric if (Offset) {
5482cab237bSDimitry Andric if (auto *Entry = getEntryAtOffset(*Offset))
5494ba319b5SDimitry Andric Entry->dump(OS, MRI, IsEH);
5502cab237bSDimitry Andric return;
5512cab237bSDimitry Andric }
5522cab237bSDimitry Andric
5532cab237bSDimitry Andric OS << "\n";
5542cab237bSDimitry Andric for (const auto &Entry : Entries)
5554ba319b5SDimitry Andric Entry->dump(OS, MRI, IsEH);
556ff0cc061SDimitry Andric }
557