10b57cec5SDimitry Andric //===- DWARFDebugFrame.h - Parsing of .debug_frame ------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" 100b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 110b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 120b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 130b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 1481ad6265SDimitry Andric #include "llvm/DebugInfo/DIContext.h" 1581ad6265SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" 160b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 170b57cec5SDimitry Andric #include "llvm/Support/DataExtractor.h" 180b57cec5SDimitry Andric #include "llvm/Support/Errc.h" 190b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 200b57cec5SDimitry Andric #include "llvm/Support/Format.h" 210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 220b57cec5SDimitry Andric #include <algorithm> 230b57cec5SDimitry Andric #include <cassert> 240b57cec5SDimitry Andric #include <cinttypes> 250b57cec5SDimitry Andric #include <cstdint> 26bdd1243dSDimitry Andric #include <optional> 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric using namespace llvm; 290b57cec5SDimitry Andric using namespace dwarf; 300b57cec5SDimitry Andric 31bdd1243dSDimitry Andric static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts, 32e8d8bef9SDimitry Andric unsigned RegNum) { 33bdd1243dSDimitry Andric if (DumpOpts.GetNameForDWARFReg) { 34bdd1243dSDimitry Andric auto RegName = DumpOpts.GetNameForDWARFReg(RegNum, DumpOpts.IsEH); 35bdd1243dSDimitry Andric if (!RegName.empty()) { 36e8d8bef9SDimitry Andric OS << RegName; 37e8d8bef9SDimitry Andric return; 38e8d8bef9SDimitry Andric } 39e8d8bef9SDimitry Andric } 40e8d8bef9SDimitry Andric OS << "reg" << RegNum; 41e8d8bef9SDimitry Andric } 420b57cec5SDimitry Andric 43fe6060f1SDimitry Andric UnwindLocation UnwindLocation::createUnspecified() { return {Unspecified}; } 44fe6060f1SDimitry Andric 45fe6060f1SDimitry Andric UnwindLocation UnwindLocation::createUndefined() { return {Undefined}; } 46fe6060f1SDimitry Andric 47fe6060f1SDimitry Andric UnwindLocation UnwindLocation::createSame() { return {Same}; } 48fe6060f1SDimitry Andric 49fe6060f1SDimitry Andric UnwindLocation UnwindLocation::createIsConstant(int32_t Value) { 50bdd1243dSDimitry Andric return {Constant, InvalidRegisterNumber, Value, std::nullopt, false}; 51fe6060f1SDimitry Andric } 52fe6060f1SDimitry Andric 53fe6060f1SDimitry Andric UnwindLocation UnwindLocation::createIsCFAPlusOffset(int32_t Offset) { 54bdd1243dSDimitry Andric return {CFAPlusOffset, InvalidRegisterNumber, Offset, std::nullopt, false}; 55fe6060f1SDimitry Andric } 56fe6060f1SDimitry Andric 57fe6060f1SDimitry Andric UnwindLocation UnwindLocation::createAtCFAPlusOffset(int32_t Offset) { 58bdd1243dSDimitry Andric return {CFAPlusOffset, InvalidRegisterNumber, Offset, std::nullopt, true}; 59fe6060f1SDimitry Andric } 60fe6060f1SDimitry Andric 61fe6060f1SDimitry Andric UnwindLocation 62fe6060f1SDimitry Andric UnwindLocation::createIsRegisterPlusOffset(uint32_t RegNum, int32_t Offset, 63bdd1243dSDimitry Andric std::optional<uint32_t> AddrSpace) { 64fe6060f1SDimitry Andric return {RegPlusOffset, RegNum, Offset, AddrSpace, false}; 65fe6060f1SDimitry Andric } 66fe6060f1SDimitry Andric 67fe6060f1SDimitry Andric UnwindLocation 68fe6060f1SDimitry Andric UnwindLocation::createAtRegisterPlusOffset(uint32_t RegNum, int32_t Offset, 69bdd1243dSDimitry Andric std::optional<uint32_t> AddrSpace) { 70fe6060f1SDimitry Andric return {RegPlusOffset, RegNum, Offset, AddrSpace, true}; 71fe6060f1SDimitry Andric } 72fe6060f1SDimitry Andric 73fe6060f1SDimitry Andric UnwindLocation UnwindLocation::createIsDWARFExpression(DWARFExpression Expr) { 74fe6060f1SDimitry Andric return {Expr, false}; 75fe6060f1SDimitry Andric } 76fe6060f1SDimitry Andric 77fe6060f1SDimitry Andric UnwindLocation UnwindLocation::createAtDWARFExpression(DWARFExpression Expr) { 78fe6060f1SDimitry Andric return {Expr, true}; 79fe6060f1SDimitry Andric } 80fe6060f1SDimitry Andric 81bdd1243dSDimitry Andric void UnwindLocation::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { 82fe6060f1SDimitry Andric if (Dereference) 83fe6060f1SDimitry Andric OS << '['; 84fe6060f1SDimitry Andric switch (Kind) { 85fe6060f1SDimitry Andric case Unspecified: 86fe6060f1SDimitry Andric OS << "unspecified"; 87fe6060f1SDimitry Andric break; 88fe6060f1SDimitry Andric case Undefined: 89fe6060f1SDimitry Andric OS << "undefined"; 90fe6060f1SDimitry Andric break; 91fe6060f1SDimitry Andric case Same: 92fe6060f1SDimitry Andric OS << "same"; 93fe6060f1SDimitry Andric break; 94fe6060f1SDimitry Andric case CFAPlusOffset: 95fe6060f1SDimitry Andric OS << "CFA"; 96fe6060f1SDimitry Andric if (Offset == 0) 97fe6060f1SDimitry Andric break; 98fe6060f1SDimitry Andric if (Offset > 0) 99fe6060f1SDimitry Andric OS << "+"; 100fe6060f1SDimitry Andric OS << Offset; 101fe6060f1SDimitry Andric break; 102fe6060f1SDimitry Andric case RegPlusOffset: 103bdd1243dSDimitry Andric printRegister(OS, DumpOpts, RegNum); 104fe6060f1SDimitry Andric if (Offset == 0 && !AddrSpace) 105fe6060f1SDimitry Andric break; 106fe6060f1SDimitry Andric if (Offset >= 0) 107fe6060f1SDimitry Andric OS << "+"; 108fe6060f1SDimitry Andric OS << Offset; 109fe6060f1SDimitry Andric if (AddrSpace) 110fe6060f1SDimitry Andric OS << " in addrspace" << *AddrSpace; 111fe6060f1SDimitry Andric break; 112bdd1243dSDimitry Andric case DWARFExpr: { 113bdd1243dSDimitry Andric Expr->print(OS, DumpOpts, nullptr); 114fe6060f1SDimitry Andric break; 115bdd1243dSDimitry Andric } 116fe6060f1SDimitry Andric case Constant: 117fe6060f1SDimitry Andric OS << Offset; 118fe6060f1SDimitry Andric break; 119fe6060f1SDimitry Andric } 120fe6060f1SDimitry Andric if (Dereference) 121fe6060f1SDimitry Andric OS << ']'; 122fe6060f1SDimitry Andric } 123fe6060f1SDimitry Andric 124fe6060f1SDimitry Andric raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, 125fe6060f1SDimitry Andric const UnwindLocation &UL) { 126bdd1243dSDimitry Andric auto DumpOpts = DIDumpOptions(); 127bdd1243dSDimitry Andric UL.dump(OS, DumpOpts); 128fe6060f1SDimitry Andric return OS; 129fe6060f1SDimitry Andric } 130fe6060f1SDimitry Andric 131fe6060f1SDimitry Andric bool UnwindLocation::operator==(const UnwindLocation &RHS) const { 132fe6060f1SDimitry Andric if (Kind != RHS.Kind) 133fe6060f1SDimitry Andric return false; 134fe6060f1SDimitry Andric switch (Kind) { 135fe6060f1SDimitry Andric case Unspecified: 136fe6060f1SDimitry Andric case Undefined: 137fe6060f1SDimitry Andric case Same: 138fe6060f1SDimitry Andric return true; 139fe6060f1SDimitry Andric case CFAPlusOffset: 140fe6060f1SDimitry Andric return Offset == RHS.Offset && Dereference == RHS.Dereference; 141fe6060f1SDimitry Andric case RegPlusOffset: 142fe6060f1SDimitry Andric return RegNum == RHS.RegNum && Offset == RHS.Offset && 143fe6060f1SDimitry Andric Dereference == RHS.Dereference; 144fe6060f1SDimitry Andric case DWARFExpr: 145fe6060f1SDimitry Andric return *Expr == *RHS.Expr && Dereference == RHS.Dereference; 146fe6060f1SDimitry Andric case Constant: 147fe6060f1SDimitry Andric return Offset == RHS.Offset; 148fe6060f1SDimitry Andric } 149fe6060f1SDimitry Andric return false; 150fe6060f1SDimitry Andric } 151fe6060f1SDimitry Andric 152bdd1243dSDimitry Andric void RegisterLocations::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { 153fe6060f1SDimitry Andric bool First = true; 154fe6060f1SDimitry Andric for (const auto &RegLocPair : Locations) { 155fe6060f1SDimitry Andric if (First) 156fe6060f1SDimitry Andric First = false; 157fe6060f1SDimitry Andric else 158fe6060f1SDimitry Andric OS << ", "; 159bdd1243dSDimitry Andric printRegister(OS, DumpOpts, RegLocPair.first); 160fe6060f1SDimitry Andric OS << '='; 161bdd1243dSDimitry Andric RegLocPair.second.dump(OS, DumpOpts); 162fe6060f1SDimitry Andric } 163fe6060f1SDimitry Andric } 164fe6060f1SDimitry Andric 165fe6060f1SDimitry Andric raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, 166fe6060f1SDimitry Andric const RegisterLocations &RL) { 167bdd1243dSDimitry Andric auto DumpOpts = DIDumpOptions(); 168bdd1243dSDimitry Andric RL.dump(OS, DumpOpts); 169fe6060f1SDimitry Andric return OS; 170fe6060f1SDimitry Andric } 171fe6060f1SDimitry Andric 172bdd1243dSDimitry Andric void UnwindRow::dump(raw_ostream &OS, DIDumpOptions DumpOpts, 173fe6060f1SDimitry Andric unsigned IndentLevel) const { 174fe6060f1SDimitry Andric OS.indent(2 * IndentLevel); 175fe6060f1SDimitry Andric if (hasAddress()) 176fe6060f1SDimitry Andric OS << format("0x%" PRIx64 ": ", *Address); 177fe6060f1SDimitry Andric OS << "CFA="; 178bdd1243dSDimitry Andric CFAValue.dump(OS, DumpOpts); 179fe6060f1SDimitry Andric if (RegLocs.hasLocations()) { 180fe6060f1SDimitry Andric OS << ": "; 181bdd1243dSDimitry Andric RegLocs.dump(OS, DumpOpts); 182fe6060f1SDimitry Andric } 183fe6060f1SDimitry Andric OS << "\n"; 184fe6060f1SDimitry Andric } 185fe6060f1SDimitry Andric 186fe6060f1SDimitry Andric raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindRow &Row) { 187bdd1243dSDimitry Andric auto DumpOpts = DIDumpOptions(); 188bdd1243dSDimitry Andric Row.dump(OS, DumpOpts, 0); 189fe6060f1SDimitry Andric return OS; 190fe6060f1SDimitry Andric } 191fe6060f1SDimitry Andric 192bdd1243dSDimitry Andric void UnwindTable::dump(raw_ostream &OS, DIDumpOptions DumpOpts, 193fe6060f1SDimitry Andric unsigned IndentLevel) const { 194fe6060f1SDimitry Andric for (const UnwindRow &Row : Rows) 195bdd1243dSDimitry Andric Row.dump(OS, DumpOpts, IndentLevel); 196fe6060f1SDimitry Andric } 197fe6060f1SDimitry Andric 198fe6060f1SDimitry Andric raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindTable &Rows) { 199bdd1243dSDimitry Andric auto DumpOpts = DIDumpOptions(); 200bdd1243dSDimitry Andric Rows.dump(OS, DumpOpts, 0); 201fe6060f1SDimitry Andric return OS; 202fe6060f1SDimitry Andric } 203fe6060f1SDimitry Andric 204fe6060f1SDimitry Andric Expected<UnwindTable> UnwindTable::create(const FDE *Fde) { 205fe6060f1SDimitry Andric const CIE *Cie = Fde->getLinkedCIE(); 206fe6060f1SDimitry Andric if (Cie == nullptr) 207fe6060f1SDimitry Andric return createStringError(errc::invalid_argument, 208fe6060f1SDimitry Andric "unable to get CIE for FDE at offset 0x%" PRIx64, 209fe6060f1SDimitry Andric Fde->getOffset()); 210fe6060f1SDimitry Andric 211fe6060f1SDimitry Andric // Rows will be empty if there are no CFI instructions. 212fe6060f1SDimitry Andric if (Cie->cfis().empty() && Fde->cfis().empty()) 213fe6060f1SDimitry Andric return UnwindTable(); 214fe6060f1SDimitry Andric 215fe6060f1SDimitry Andric UnwindTable UT; 216fe6060f1SDimitry Andric UnwindRow Row; 217fe6060f1SDimitry Andric Row.setAddress(Fde->getInitialLocation()); 218fe6060f1SDimitry Andric UT.EndAddress = Fde->getInitialLocation() + Fde->getAddressRange(); 219fe6060f1SDimitry Andric if (Error CieError = UT.parseRows(Cie->cfis(), Row, nullptr)) 220fe6060f1SDimitry Andric return std::move(CieError); 221fe6060f1SDimitry Andric // We need to save the initial locations of registers from the CIE parsing 222fe6060f1SDimitry Andric // in case we run into DW_CFA_restore or DW_CFA_restore_extended opcodes. 223fe6060f1SDimitry Andric const RegisterLocations InitialLocs = Row.getRegisterLocations(); 224fe6060f1SDimitry Andric if (Error FdeError = UT.parseRows(Fde->cfis(), Row, &InitialLocs)) 225fe6060f1SDimitry Andric return std::move(FdeError); 226fe6060f1SDimitry Andric // May be all the CFI instructions were DW_CFA_nop amd Row becomes empty. 227fe6060f1SDimitry Andric // Do not add that to the unwind table. 228fe6060f1SDimitry Andric if (Row.getRegisterLocations().hasLocations() || 229fe6060f1SDimitry Andric Row.getCFAValue().getLocation() != UnwindLocation::Unspecified) 230fe6060f1SDimitry Andric UT.Rows.push_back(Row); 231fe6060f1SDimitry Andric return UT; 232fe6060f1SDimitry Andric } 233fe6060f1SDimitry Andric 234fe6060f1SDimitry Andric Expected<UnwindTable> UnwindTable::create(const CIE *Cie) { 235fe6060f1SDimitry Andric // Rows will be empty if there are no CFI instructions. 236fe6060f1SDimitry Andric if (Cie->cfis().empty()) 237fe6060f1SDimitry Andric return UnwindTable(); 238fe6060f1SDimitry Andric 239fe6060f1SDimitry Andric UnwindTable UT; 240fe6060f1SDimitry Andric UnwindRow Row; 241fe6060f1SDimitry Andric if (Error CieError = UT.parseRows(Cie->cfis(), Row, nullptr)) 242fe6060f1SDimitry Andric return std::move(CieError); 243fe6060f1SDimitry Andric // May be all the CFI instructions were DW_CFA_nop amd Row becomes empty. 244fe6060f1SDimitry Andric // Do not add that to the unwind table. 245fe6060f1SDimitry Andric if (Row.getRegisterLocations().hasLocations() || 246fe6060f1SDimitry Andric Row.getCFAValue().getLocation() != UnwindLocation::Unspecified) 247fe6060f1SDimitry Andric UT.Rows.push_back(Row); 248fe6060f1SDimitry Andric return UT; 249fe6060f1SDimitry Andric } 250fe6060f1SDimitry Andric 2510b57cec5SDimitry Andric // See DWARF standard v3, section 7.23 2520b57cec5SDimitry Andric const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0; 2530b57cec5SDimitry Andric const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f; 2540b57cec5SDimitry Andric 2558bcb0991SDimitry Andric Error CFIProgram::parse(DWARFDataExtractor Data, uint64_t *Offset, 2568bcb0991SDimitry Andric uint64_t EndOffset) { 2575ffd83dbSDimitry Andric DataExtractor::Cursor C(*Offset); 2585ffd83dbSDimitry Andric while (C && C.tell() < EndOffset) { 2595ffd83dbSDimitry Andric uint8_t Opcode = Data.getRelocatedValue(C, 1); 2605ffd83dbSDimitry Andric if (!C) 2615ffd83dbSDimitry Andric break; 2620b57cec5SDimitry Andric 2635ffd83dbSDimitry Andric // Some instructions have a primary opcode encoded in the top bits. 2645ffd83dbSDimitry Andric if (uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK) { 2650b57cec5SDimitry Andric // If it's a primary opcode, the first operand is encoded in the bottom 2660b57cec5SDimitry Andric // bits of the opcode itself. 2670b57cec5SDimitry Andric uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK; 2680b57cec5SDimitry Andric switch (Primary) { 2690b57cec5SDimitry Andric case DW_CFA_advance_loc: 2700b57cec5SDimitry Andric case DW_CFA_restore: 2710b57cec5SDimitry Andric addInstruction(Primary, Op1); 2720b57cec5SDimitry Andric break; 2730b57cec5SDimitry Andric case DW_CFA_offset: 2745ffd83dbSDimitry Andric addInstruction(Primary, Op1, Data.getULEB128(C)); 2750b57cec5SDimitry Andric break; 2765ffd83dbSDimitry Andric default: 2775ffd83dbSDimitry Andric llvm_unreachable("invalid primary CFI opcode"); 2780b57cec5SDimitry Andric } 2795ffd83dbSDimitry Andric continue; 2805ffd83dbSDimitry Andric } 2815ffd83dbSDimitry Andric 2820b57cec5SDimitry Andric // Extended opcode - its value is Opcode itself. 2830b57cec5SDimitry Andric switch (Opcode) { 2840b57cec5SDimitry Andric default: 2850b57cec5SDimitry Andric return createStringError(errc::illegal_byte_sequence, 2865ffd83dbSDimitry Andric "invalid extended CFI opcode 0x%" PRIx8, Opcode); 2870b57cec5SDimitry Andric case DW_CFA_nop: 2880b57cec5SDimitry Andric case DW_CFA_remember_state: 2890b57cec5SDimitry Andric case DW_CFA_restore_state: 2900b57cec5SDimitry Andric case DW_CFA_GNU_window_save: 2910b57cec5SDimitry Andric // No operands 2920b57cec5SDimitry Andric addInstruction(Opcode); 2930b57cec5SDimitry Andric break; 2940b57cec5SDimitry Andric case DW_CFA_set_loc: 2950b57cec5SDimitry Andric // Operands: Address 2965ffd83dbSDimitry Andric addInstruction(Opcode, Data.getRelocatedAddress(C)); 2970b57cec5SDimitry Andric break; 2980b57cec5SDimitry Andric case DW_CFA_advance_loc1: 2990b57cec5SDimitry Andric // Operands: 1-byte delta 3005ffd83dbSDimitry Andric addInstruction(Opcode, Data.getRelocatedValue(C, 1)); 3010b57cec5SDimitry Andric break; 3020b57cec5SDimitry Andric case DW_CFA_advance_loc2: 3030b57cec5SDimitry Andric // Operands: 2-byte delta 3045ffd83dbSDimitry Andric addInstruction(Opcode, Data.getRelocatedValue(C, 2)); 3050b57cec5SDimitry Andric break; 3060b57cec5SDimitry Andric case DW_CFA_advance_loc4: 3070b57cec5SDimitry Andric // Operands: 4-byte delta 3085ffd83dbSDimitry Andric addInstruction(Opcode, Data.getRelocatedValue(C, 4)); 3090b57cec5SDimitry Andric break; 3100b57cec5SDimitry Andric case DW_CFA_restore_extended: 3110b57cec5SDimitry Andric case DW_CFA_undefined: 3120b57cec5SDimitry Andric case DW_CFA_same_value: 3130b57cec5SDimitry Andric case DW_CFA_def_cfa_register: 3140b57cec5SDimitry Andric case DW_CFA_def_cfa_offset: 3150b57cec5SDimitry Andric case DW_CFA_GNU_args_size: 3160b57cec5SDimitry Andric // Operands: ULEB128 3175ffd83dbSDimitry Andric addInstruction(Opcode, Data.getULEB128(C)); 3180b57cec5SDimitry Andric break; 3190b57cec5SDimitry Andric case DW_CFA_def_cfa_offset_sf: 3200b57cec5SDimitry Andric // Operands: SLEB128 3215ffd83dbSDimitry Andric addInstruction(Opcode, Data.getSLEB128(C)); 3220b57cec5SDimitry Andric break; 323fe6060f1SDimitry Andric case DW_CFA_LLVM_def_aspace_cfa: 324fe6060f1SDimitry Andric case DW_CFA_LLVM_def_aspace_cfa_sf: { 325fe6060f1SDimitry Andric auto RegNum = Data.getULEB128(C); 326fe6060f1SDimitry Andric auto CfaOffset = Opcode == DW_CFA_LLVM_def_aspace_cfa 327fe6060f1SDimitry Andric ? Data.getULEB128(C) 328fe6060f1SDimitry Andric : Data.getSLEB128(C); 329fe6060f1SDimitry Andric auto AddressSpace = Data.getULEB128(C); 330fe6060f1SDimitry Andric addInstruction(Opcode, RegNum, CfaOffset, AddressSpace); 331fe6060f1SDimitry Andric break; 332fe6060f1SDimitry Andric } 3330b57cec5SDimitry Andric case DW_CFA_offset_extended: 3340b57cec5SDimitry Andric case DW_CFA_register: 3350b57cec5SDimitry Andric case DW_CFA_def_cfa: 3360b57cec5SDimitry Andric case DW_CFA_val_offset: { 3370b57cec5SDimitry Andric // Operands: ULEB128, ULEB128 3380b57cec5SDimitry Andric // Note: We can not embed getULEB128 directly into function 3390b57cec5SDimitry Andric // argument list. getULEB128 changes Offset and order of evaluation 3400b57cec5SDimitry Andric // for arguments is unspecified. 3415ffd83dbSDimitry Andric uint64_t op1 = Data.getULEB128(C); 3425ffd83dbSDimitry Andric uint64_t op2 = Data.getULEB128(C); 3430b57cec5SDimitry Andric addInstruction(Opcode, op1, op2); 3440b57cec5SDimitry Andric break; 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric case DW_CFA_offset_extended_sf: 3470b57cec5SDimitry Andric case DW_CFA_def_cfa_sf: 3480b57cec5SDimitry Andric case DW_CFA_val_offset_sf: { 3490b57cec5SDimitry Andric // Operands: ULEB128, SLEB128 3500b57cec5SDimitry Andric // Note: see comment for the previous case 3515ffd83dbSDimitry Andric uint64_t op1 = Data.getULEB128(C); 3525ffd83dbSDimitry Andric uint64_t op2 = (uint64_t)Data.getSLEB128(C); 3530b57cec5SDimitry Andric addInstruction(Opcode, op1, op2); 3540b57cec5SDimitry Andric break; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric case DW_CFA_def_cfa_expression: { 3575ffd83dbSDimitry Andric uint64_t ExprLength = Data.getULEB128(C); 3580b57cec5SDimitry Andric addInstruction(Opcode, 0); 3595ffd83dbSDimitry Andric StringRef Expression = Data.getBytes(C, ExprLength); 3605ffd83dbSDimitry Andric 3615ffd83dbSDimitry Andric DataExtractor Extractor(Expression, Data.isLittleEndian(), 3625ffd83dbSDimitry Andric Data.getAddressSize()); 3635ffd83dbSDimitry Andric // Note. We do not pass the DWARF format to DWARFExpression, because 3645ffd83dbSDimitry Andric // DW_OP_call_ref, the only operation which depends on the format, is 3655ffd83dbSDimitry Andric // prohibited in call frame instructions, see sec. 6.4.2 in DWARFv5. 3665ffd83dbSDimitry Andric Instructions.back().Expression = 3675ffd83dbSDimitry Andric DWARFExpression(Extractor, Data.getAddressSize()); 3680b57cec5SDimitry Andric break; 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric case DW_CFA_expression: 3710b57cec5SDimitry Andric case DW_CFA_val_expression: { 3725ffd83dbSDimitry Andric uint64_t RegNum = Data.getULEB128(C); 3730b57cec5SDimitry Andric addInstruction(Opcode, RegNum, 0); 3745ffd83dbSDimitry Andric 3755ffd83dbSDimitry Andric uint64_t BlockLength = Data.getULEB128(C); 3765ffd83dbSDimitry Andric StringRef Expression = Data.getBytes(C, BlockLength); 3775ffd83dbSDimitry Andric DataExtractor Extractor(Expression, Data.isLittleEndian(), 3785ffd83dbSDimitry Andric Data.getAddressSize()); 3795ffd83dbSDimitry Andric // Note. We do not pass the DWARF format to DWARFExpression, because 3805ffd83dbSDimitry Andric // DW_OP_call_ref, the only operation which depends on the format, is 3815ffd83dbSDimitry Andric // prohibited in call frame instructions, see sec. 6.4.2 in DWARFv5. 3825ffd83dbSDimitry Andric Instructions.back().Expression = 3835ffd83dbSDimitry Andric DWARFExpression(Extractor, Data.getAddressSize()); 3840b57cec5SDimitry Andric break; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric } 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3895ffd83dbSDimitry Andric *Offset = C.tell(); 3905ffd83dbSDimitry Andric return C.takeError(); 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric 393fe6060f1SDimitry Andric StringRef CFIProgram::callFrameString(unsigned Opcode) const { 394fe6060f1SDimitry Andric return dwarf::CallFrameString(Opcode, Arch); 395fe6060f1SDimitry Andric } 3960b57cec5SDimitry Andric 397fe6060f1SDimitry Andric const char *CFIProgram::operandTypeString(CFIProgram::OperandType OT) { 398fe6060f1SDimitry Andric #define ENUM_TO_CSTR(e) \ 399fe6060f1SDimitry Andric case e: \ 400fe6060f1SDimitry Andric return #e; 401fe6060f1SDimitry Andric switch (OT) { 402fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_Unset); 403fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_None); 404fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_Address); 405fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_Offset); 406fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_FactoredCodeOffset); 407fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_SignedFactDataOffset); 408fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_UnsignedFactDataOffset); 409fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_Register); 410fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_AddressSpace); 411fe6060f1SDimitry Andric ENUM_TO_CSTR(OT_Expression); 412fe6060f1SDimitry Andric } 413fe6060f1SDimitry Andric return "<unknown CFIProgram::OperandType>"; 414fe6060f1SDimitry Andric } 4150b57cec5SDimitry Andric 416fe6060f1SDimitry Andric llvm::Expected<uint64_t> 417fe6060f1SDimitry Andric CFIProgram::Instruction::getOperandAsUnsigned(const CFIProgram &CFIP, 418fe6060f1SDimitry Andric uint32_t OperandIdx) const { 419fe6060f1SDimitry Andric if (OperandIdx >= MaxOperands) 420fe6060f1SDimitry Andric return createStringError(errc::invalid_argument, 421fe6060f1SDimitry Andric "operand index %" PRIu32 " is not valid", 422fe6060f1SDimitry Andric OperandIdx); 423fe6060f1SDimitry Andric OperandType Type = CFIP.getOperandTypes()[Opcode][OperandIdx]; 424fe6060f1SDimitry Andric uint64_t Operand = Ops[OperandIdx]; 425fe6060f1SDimitry Andric switch (Type) { 426fe6060f1SDimitry Andric case OT_Unset: 427fe6060f1SDimitry Andric case OT_None: 428fe6060f1SDimitry Andric case OT_Expression: 429fe6060f1SDimitry Andric return createStringError(errc::invalid_argument, 430fe6060f1SDimitry Andric "op[%" PRIu32 "] has type %s which has no value", 431fe6060f1SDimitry Andric OperandIdx, CFIProgram::operandTypeString(Type)); 4320b57cec5SDimitry Andric 433fe6060f1SDimitry Andric case OT_Offset: 434fe6060f1SDimitry Andric case OT_SignedFactDataOffset: 435fe6060f1SDimitry Andric case OT_UnsignedFactDataOffset: 436fe6060f1SDimitry Andric return createStringError( 437fe6060f1SDimitry Andric errc::invalid_argument, 438fe6060f1SDimitry Andric "op[%" PRIu32 "] has OperandType OT_Offset which produces a signed " 439fe6060f1SDimitry Andric "result, call getOperandAsSigned instead", 440fe6060f1SDimitry Andric OperandIdx); 441fe6060f1SDimitry Andric 442fe6060f1SDimitry Andric case OT_Address: 443fe6060f1SDimitry Andric case OT_Register: 444fe6060f1SDimitry Andric case OT_AddressSpace: 445fe6060f1SDimitry Andric return Operand; 446fe6060f1SDimitry Andric 447fe6060f1SDimitry Andric case OT_FactoredCodeOffset: { 448fe6060f1SDimitry Andric const uint64_t CodeAlignmentFactor = CFIP.codeAlign(); 449fe6060f1SDimitry Andric if (CodeAlignmentFactor == 0) 450fe6060f1SDimitry Andric return createStringError( 451fe6060f1SDimitry Andric errc::invalid_argument, 452fe6060f1SDimitry Andric "op[%" PRIu32 "] has type OT_FactoredCodeOffset but code alignment " 453fe6060f1SDimitry Andric "is zero", 454fe6060f1SDimitry Andric OperandIdx); 455fe6060f1SDimitry Andric return Operand * CodeAlignmentFactor; 456fe6060f1SDimitry Andric } 457fe6060f1SDimitry Andric } 458fe6060f1SDimitry Andric llvm_unreachable("invalid operand type"); 459fe6060f1SDimitry Andric } 460fe6060f1SDimitry Andric 461fe6060f1SDimitry Andric llvm::Expected<int64_t> 462fe6060f1SDimitry Andric CFIProgram::Instruction::getOperandAsSigned(const CFIProgram &CFIP, 463fe6060f1SDimitry Andric uint32_t OperandIdx) const { 464fe6060f1SDimitry Andric if (OperandIdx >= MaxOperands) 465fe6060f1SDimitry Andric return createStringError(errc::invalid_argument, 466fe6060f1SDimitry Andric "operand index %" PRIu32 " is not valid", 467fe6060f1SDimitry Andric OperandIdx); 468fe6060f1SDimitry Andric OperandType Type = CFIP.getOperandTypes()[Opcode][OperandIdx]; 469fe6060f1SDimitry Andric uint64_t Operand = Ops[OperandIdx]; 470fe6060f1SDimitry Andric switch (Type) { 471fe6060f1SDimitry Andric case OT_Unset: 472fe6060f1SDimitry Andric case OT_None: 473fe6060f1SDimitry Andric case OT_Expression: 474fe6060f1SDimitry Andric return createStringError(errc::invalid_argument, 475fe6060f1SDimitry Andric "op[%" PRIu32 "] has type %s which has no value", 476fe6060f1SDimitry Andric OperandIdx, CFIProgram::operandTypeString(Type)); 477fe6060f1SDimitry Andric 478fe6060f1SDimitry Andric case OT_Address: 479fe6060f1SDimitry Andric case OT_Register: 480fe6060f1SDimitry Andric case OT_AddressSpace: 481fe6060f1SDimitry Andric return createStringError( 482fe6060f1SDimitry Andric errc::invalid_argument, 483fe6060f1SDimitry Andric "op[%" PRIu32 "] has OperandType %s which produces an unsigned result, " 484fe6060f1SDimitry Andric "call getOperandAsUnsigned instead", 485fe6060f1SDimitry Andric OperandIdx, CFIProgram::operandTypeString(Type)); 486fe6060f1SDimitry Andric 487fe6060f1SDimitry Andric case OT_Offset: 488fe6060f1SDimitry Andric return (int64_t)Operand; 489fe6060f1SDimitry Andric 490fe6060f1SDimitry Andric case OT_FactoredCodeOffset: 491fe6060f1SDimitry Andric case OT_SignedFactDataOffset: { 492fe6060f1SDimitry Andric const int64_t DataAlignmentFactor = CFIP.dataAlign(); 493fe6060f1SDimitry Andric if (DataAlignmentFactor == 0) 494fe6060f1SDimitry Andric return createStringError(errc::invalid_argument, 495fe6060f1SDimitry Andric "op[%" PRIu32 "] has type %s but data " 496fe6060f1SDimitry Andric "alignment is zero", 497fe6060f1SDimitry Andric OperandIdx, CFIProgram::operandTypeString(Type)); 498fe6060f1SDimitry Andric return int64_t(Operand) * DataAlignmentFactor; 499fe6060f1SDimitry Andric } 500fe6060f1SDimitry Andric 501fe6060f1SDimitry Andric case OT_UnsignedFactDataOffset: { 502fe6060f1SDimitry Andric const int64_t DataAlignmentFactor = CFIP.dataAlign(); 503fe6060f1SDimitry Andric if (DataAlignmentFactor == 0) 504fe6060f1SDimitry Andric return createStringError(errc::invalid_argument, 505fe6060f1SDimitry Andric "op[%" PRIu32 506fe6060f1SDimitry Andric "] has type OT_UnsignedFactDataOffset but data " 507fe6060f1SDimitry Andric "alignment is zero", 508fe6060f1SDimitry Andric OperandIdx); 509fe6060f1SDimitry Andric return Operand * DataAlignmentFactor; 510fe6060f1SDimitry Andric } 511fe6060f1SDimitry Andric } 512fe6060f1SDimitry Andric llvm_unreachable("invalid operand type"); 513fe6060f1SDimitry Andric } 514fe6060f1SDimitry Andric 515fe6060f1SDimitry Andric Error UnwindTable::parseRows(const CFIProgram &CFIP, UnwindRow &Row, 516fe6060f1SDimitry Andric const RegisterLocations *InitialLocs) { 517bdd1243dSDimitry Andric // State consists of CFA value and register locations. 518bdd1243dSDimitry Andric std::vector<std::pair<UnwindLocation, RegisterLocations>> States; 519fe6060f1SDimitry Andric for (const CFIProgram::Instruction &Inst : CFIP) { 520fe6060f1SDimitry Andric switch (Inst.Opcode) { 521fe6060f1SDimitry Andric case dwarf::DW_CFA_set_loc: { 522fe6060f1SDimitry Andric // The DW_CFA_set_loc instruction takes a single operand that 523fe6060f1SDimitry Andric // represents a target address. The required action is to create a new 524fe6060f1SDimitry Andric // table row using the specified address as the location. All other 525fe6060f1SDimitry Andric // values in the new row are initially identical to the current row. 526fe6060f1SDimitry Andric // The new location value is always greater than the current one. If 527fe6060f1SDimitry Andric // the segment_size field of this FDE's CIE is non- zero, the initial 528fe6060f1SDimitry Andric // location is preceded by a segment selector of the given length 529fe6060f1SDimitry Andric llvm::Expected<uint64_t> NewAddress = Inst.getOperandAsUnsigned(CFIP, 0); 530fe6060f1SDimitry Andric if (!NewAddress) 531fe6060f1SDimitry Andric return NewAddress.takeError(); 532fe6060f1SDimitry Andric if (*NewAddress <= Row.getAddress()) 533fe6060f1SDimitry Andric return createStringError( 534fe6060f1SDimitry Andric errc::invalid_argument, 535fe6060f1SDimitry Andric "%s with adrress 0x%" PRIx64 " which must be greater than the " 536fe6060f1SDimitry Andric "current row address 0x%" PRIx64, 537fe6060f1SDimitry Andric CFIP.callFrameString(Inst.Opcode).str().c_str(), *NewAddress, 538fe6060f1SDimitry Andric Row.getAddress()); 539fe6060f1SDimitry Andric Rows.push_back(Row); 540fe6060f1SDimitry Andric Row.setAddress(*NewAddress); 541fe6060f1SDimitry Andric break; 542fe6060f1SDimitry Andric } 543fe6060f1SDimitry Andric 544fe6060f1SDimitry Andric case dwarf::DW_CFA_advance_loc: 545fe6060f1SDimitry Andric case dwarf::DW_CFA_advance_loc1: 546fe6060f1SDimitry Andric case dwarf::DW_CFA_advance_loc2: 547fe6060f1SDimitry Andric case dwarf::DW_CFA_advance_loc4: { 548fe6060f1SDimitry Andric // The DW_CFA_advance instruction takes a single operand that 549fe6060f1SDimitry Andric // represents a constant delta. The required action is to create a new 550fe6060f1SDimitry Andric // table row with a location value that is computed by taking the 551fe6060f1SDimitry Andric // current entry’s location value and adding the value of delta * 552fe6060f1SDimitry Andric // code_alignment_factor. All other values in the new row are initially 553fe6060f1SDimitry Andric // identical to the current row. 554fe6060f1SDimitry Andric Rows.push_back(Row); 555fe6060f1SDimitry Andric llvm::Expected<uint64_t> Offset = Inst.getOperandAsUnsigned(CFIP, 0); 556fe6060f1SDimitry Andric if (!Offset) 557fe6060f1SDimitry Andric return Offset.takeError(); 558fe6060f1SDimitry Andric Row.slideAddress(*Offset); 559fe6060f1SDimitry Andric break; 560fe6060f1SDimitry Andric } 561fe6060f1SDimitry Andric 562fe6060f1SDimitry Andric case dwarf::DW_CFA_restore: 563fe6060f1SDimitry Andric case dwarf::DW_CFA_restore_extended: { 564fe6060f1SDimitry Andric // The DW_CFA_restore instruction takes a single operand (encoded with 565fe6060f1SDimitry Andric // the opcode) that represents a register number. The required action 566fe6060f1SDimitry Andric // is to change the rule for the indicated register to the rule 567fe6060f1SDimitry Andric // assigned it by the initial_instructions in the CIE. 568fe6060f1SDimitry Andric if (InitialLocs == nullptr) 569fe6060f1SDimitry Andric return createStringError( 570fe6060f1SDimitry Andric errc::invalid_argument, "%s encountered while parsing a CIE", 571fe6060f1SDimitry Andric CFIP.callFrameString(Inst.Opcode).str().c_str()); 572fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 573fe6060f1SDimitry Andric if (!RegNum) 574fe6060f1SDimitry Andric return RegNum.takeError(); 575bdd1243dSDimitry Andric if (std::optional<UnwindLocation> O = 576fe6060f1SDimitry Andric InitialLocs->getRegisterLocation(*RegNum)) 577fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation(*RegNum, *O); 578fe6060f1SDimitry Andric else 579fe6060f1SDimitry Andric Row.getRegisterLocations().removeRegisterLocation(*RegNum); 580fe6060f1SDimitry Andric break; 581fe6060f1SDimitry Andric } 582fe6060f1SDimitry Andric 583fe6060f1SDimitry Andric case dwarf::DW_CFA_offset: 584fe6060f1SDimitry Andric case dwarf::DW_CFA_offset_extended: 585fe6060f1SDimitry Andric case dwarf::DW_CFA_offset_extended_sf: { 586fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 587fe6060f1SDimitry Andric if (!RegNum) 588fe6060f1SDimitry Andric return RegNum.takeError(); 589fe6060f1SDimitry Andric llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1); 590fe6060f1SDimitry Andric if (!Offset) 591fe6060f1SDimitry Andric return Offset.takeError(); 592fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 593fe6060f1SDimitry Andric *RegNum, UnwindLocation::createAtCFAPlusOffset(*Offset)); 594fe6060f1SDimitry Andric break; 595fe6060f1SDimitry Andric } 596fe6060f1SDimitry Andric 597fe6060f1SDimitry Andric case dwarf::DW_CFA_nop: 598fe6060f1SDimitry Andric break; 599fe6060f1SDimitry Andric 600fe6060f1SDimitry Andric case dwarf::DW_CFA_remember_state: 601bdd1243dSDimitry Andric States.push_back( 602bdd1243dSDimitry Andric std::make_pair(Row.getCFAValue(), Row.getRegisterLocations())); 603fe6060f1SDimitry Andric break; 604fe6060f1SDimitry Andric 605fe6060f1SDimitry Andric case dwarf::DW_CFA_restore_state: 606bdd1243dSDimitry Andric if (States.empty()) 607fe6060f1SDimitry Andric return createStringError(errc::invalid_argument, 608fe6060f1SDimitry Andric "DW_CFA_restore_state without a matching " 609fe6060f1SDimitry Andric "previous DW_CFA_remember_state"); 610bdd1243dSDimitry Andric Row.getCFAValue() = States.back().first; 611bdd1243dSDimitry Andric Row.getRegisterLocations() = States.back().second; 612bdd1243dSDimitry Andric States.pop_back(); 613fe6060f1SDimitry Andric break; 614fe6060f1SDimitry Andric 615fe6060f1SDimitry Andric case dwarf::DW_CFA_GNU_window_save: 616fe6060f1SDimitry Andric switch (CFIP.triple()) { 617fe6060f1SDimitry Andric case Triple::aarch64: 618fe6060f1SDimitry Andric case Triple::aarch64_be: 619fe6060f1SDimitry Andric case Triple::aarch64_32: { 620fe6060f1SDimitry Andric // DW_CFA_GNU_window_save is used for different things on different 621fe6060f1SDimitry Andric // architectures. For aarch64 it is known as 622fe6060f1SDimitry Andric // DW_CFA_AARCH64_negate_ra_state. The action is to toggle the 623fe6060f1SDimitry Andric // value of the return address state between 1 and 0. If there is 624fe6060f1SDimitry Andric // no rule for the AARCH64_DWARF_PAUTH_RA_STATE register, then it 625fe6060f1SDimitry Andric // should be initially set to 1. 626fe6060f1SDimitry Andric constexpr uint32_t AArch64DWARFPAuthRaState = 34; 627fe6060f1SDimitry Andric auto LRLoc = Row.getRegisterLocations().getRegisterLocation( 628fe6060f1SDimitry Andric AArch64DWARFPAuthRaState); 629fe6060f1SDimitry Andric if (LRLoc) { 630fe6060f1SDimitry Andric if (LRLoc->getLocation() == UnwindLocation::Constant) { 631fe6060f1SDimitry Andric // Toggle the constant value from 0 to 1 or 1 to 0. 632fe6060f1SDimitry Andric LRLoc->setConstant(LRLoc->getConstant() ^ 1); 633*0fca6ea1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 634*0fca6ea1SDimitry Andric AArch64DWARFPAuthRaState, *LRLoc); 635fe6060f1SDimitry Andric } else { 636fe6060f1SDimitry Andric return createStringError( 637fe6060f1SDimitry Andric errc::invalid_argument, 638fe6060f1SDimitry Andric "%s encountered when existing rule for this register is not " 639fe6060f1SDimitry Andric "a constant", 640fe6060f1SDimitry Andric CFIP.callFrameString(Inst.Opcode).str().c_str()); 641fe6060f1SDimitry Andric } 642fe6060f1SDimitry Andric } else { 643fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 644fe6060f1SDimitry Andric AArch64DWARFPAuthRaState, UnwindLocation::createIsConstant(1)); 645fe6060f1SDimitry Andric } 646fe6060f1SDimitry Andric break; 647fe6060f1SDimitry Andric } 648fe6060f1SDimitry Andric 649fe6060f1SDimitry Andric case Triple::sparc: 650fe6060f1SDimitry Andric case Triple::sparcv9: 651fe6060f1SDimitry Andric case Triple::sparcel: 652fe6060f1SDimitry Andric for (uint32_t RegNum = 16; RegNum < 32; ++RegNum) { 653fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 654fe6060f1SDimitry Andric RegNum, UnwindLocation::createAtCFAPlusOffset((RegNum - 16) * 8)); 655fe6060f1SDimitry Andric } 656fe6060f1SDimitry Andric break; 657fe6060f1SDimitry Andric 658fe6060f1SDimitry Andric default: { 659fe6060f1SDimitry Andric return createStringError( 660fe6060f1SDimitry Andric errc::not_supported, 661fe6060f1SDimitry Andric "DW_CFA opcode %#x is not supported for architecture %s", 662fe6060f1SDimitry Andric Inst.Opcode, Triple::getArchTypeName(CFIP.triple()).str().c_str()); 663fe6060f1SDimitry Andric 664fe6060f1SDimitry Andric break; 665fe6060f1SDimitry Andric } 666fe6060f1SDimitry Andric } 667fe6060f1SDimitry Andric break; 668fe6060f1SDimitry Andric 669fe6060f1SDimitry Andric case dwarf::DW_CFA_undefined: { 670fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 671fe6060f1SDimitry Andric if (!RegNum) 672fe6060f1SDimitry Andric return RegNum.takeError(); 673fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 674fe6060f1SDimitry Andric *RegNum, UnwindLocation::createUndefined()); 675fe6060f1SDimitry Andric break; 676fe6060f1SDimitry Andric } 677fe6060f1SDimitry Andric 678fe6060f1SDimitry Andric case dwarf::DW_CFA_same_value: { 679fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 680fe6060f1SDimitry Andric if (!RegNum) 681fe6060f1SDimitry Andric return RegNum.takeError(); 682fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 683fe6060f1SDimitry Andric *RegNum, UnwindLocation::createSame()); 684fe6060f1SDimitry Andric break; 685fe6060f1SDimitry Andric } 686fe6060f1SDimitry Andric 687fe6060f1SDimitry Andric case dwarf::DW_CFA_GNU_args_size: 688fe6060f1SDimitry Andric break; 689fe6060f1SDimitry Andric 690fe6060f1SDimitry Andric case dwarf::DW_CFA_register: { 691fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 692fe6060f1SDimitry Andric if (!RegNum) 693fe6060f1SDimitry Andric return RegNum.takeError(); 694fe6060f1SDimitry Andric llvm::Expected<uint64_t> NewRegNum = Inst.getOperandAsUnsigned(CFIP, 1); 695fe6060f1SDimitry Andric if (!NewRegNum) 696fe6060f1SDimitry Andric return NewRegNum.takeError(); 697fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 698fe6060f1SDimitry Andric *RegNum, UnwindLocation::createIsRegisterPlusOffset(*NewRegNum, 0)); 699fe6060f1SDimitry Andric break; 700fe6060f1SDimitry Andric } 701fe6060f1SDimitry Andric 702fe6060f1SDimitry Andric case dwarf::DW_CFA_val_offset: 703fe6060f1SDimitry Andric case dwarf::DW_CFA_val_offset_sf: { 704fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 705fe6060f1SDimitry Andric if (!RegNum) 706fe6060f1SDimitry Andric return RegNum.takeError(); 707fe6060f1SDimitry Andric llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1); 708fe6060f1SDimitry Andric if (!Offset) 709fe6060f1SDimitry Andric return Offset.takeError(); 710fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 711fe6060f1SDimitry Andric *RegNum, UnwindLocation::createIsCFAPlusOffset(*Offset)); 712fe6060f1SDimitry Andric break; 713fe6060f1SDimitry Andric } 714fe6060f1SDimitry Andric 715fe6060f1SDimitry Andric case dwarf::DW_CFA_expression: { 716fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 717fe6060f1SDimitry Andric if (!RegNum) 718fe6060f1SDimitry Andric return RegNum.takeError(); 719fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 720fe6060f1SDimitry Andric *RegNum, UnwindLocation::createAtDWARFExpression(*Inst.Expression)); 721fe6060f1SDimitry Andric break; 722fe6060f1SDimitry Andric } 723fe6060f1SDimitry Andric 724fe6060f1SDimitry Andric case dwarf::DW_CFA_val_expression: { 725fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 726fe6060f1SDimitry Andric if (!RegNum) 727fe6060f1SDimitry Andric return RegNum.takeError(); 728fe6060f1SDimitry Andric Row.getRegisterLocations().setRegisterLocation( 729fe6060f1SDimitry Andric *RegNum, UnwindLocation::createIsDWARFExpression(*Inst.Expression)); 730fe6060f1SDimitry Andric break; 731fe6060f1SDimitry Andric } 732fe6060f1SDimitry Andric 733fe6060f1SDimitry Andric case dwarf::DW_CFA_def_cfa_register: { 734fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 735fe6060f1SDimitry Andric if (!RegNum) 736fe6060f1SDimitry Andric return RegNum.takeError(); 737fe6060f1SDimitry Andric if (Row.getCFAValue().getLocation() != UnwindLocation::RegPlusOffset) 738fe6060f1SDimitry Andric Row.getCFAValue() = 739fe6060f1SDimitry Andric UnwindLocation::createIsRegisterPlusOffset(*RegNum, 0); 740fe6060f1SDimitry Andric else 741fe6060f1SDimitry Andric Row.getCFAValue().setRegister(*RegNum); 742fe6060f1SDimitry Andric break; 743fe6060f1SDimitry Andric } 744fe6060f1SDimitry Andric 745fe6060f1SDimitry Andric case dwarf::DW_CFA_def_cfa_offset: 746fe6060f1SDimitry Andric case dwarf::DW_CFA_def_cfa_offset_sf: { 747fe6060f1SDimitry Andric llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 0); 748fe6060f1SDimitry Andric if (!Offset) 749fe6060f1SDimitry Andric return Offset.takeError(); 750fe6060f1SDimitry Andric if (Row.getCFAValue().getLocation() != UnwindLocation::RegPlusOffset) { 751fe6060f1SDimitry Andric return createStringError( 752fe6060f1SDimitry Andric errc::invalid_argument, 753fe6060f1SDimitry Andric "%s found when CFA rule was not RegPlusOffset", 754fe6060f1SDimitry Andric CFIP.callFrameString(Inst.Opcode).str().c_str()); 755fe6060f1SDimitry Andric } 756fe6060f1SDimitry Andric Row.getCFAValue().setOffset(*Offset); 757fe6060f1SDimitry Andric break; 758fe6060f1SDimitry Andric } 759fe6060f1SDimitry Andric 760fe6060f1SDimitry Andric case dwarf::DW_CFA_def_cfa: 761fe6060f1SDimitry Andric case dwarf::DW_CFA_def_cfa_sf: { 762fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 763fe6060f1SDimitry Andric if (!RegNum) 764fe6060f1SDimitry Andric return RegNum.takeError(); 765fe6060f1SDimitry Andric llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1); 766fe6060f1SDimitry Andric if (!Offset) 767fe6060f1SDimitry Andric return Offset.takeError(); 768fe6060f1SDimitry Andric Row.getCFAValue() = 769fe6060f1SDimitry Andric UnwindLocation::createIsRegisterPlusOffset(*RegNum, *Offset); 770fe6060f1SDimitry Andric break; 771fe6060f1SDimitry Andric } 772fe6060f1SDimitry Andric 773fe6060f1SDimitry Andric case dwarf::DW_CFA_LLVM_def_aspace_cfa: 774fe6060f1SDimitry Andric case dwarf::DW_CFA_LLVM_def_aspace_cfa_sf: { 775fe6060f1SDimitry Andric llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0); 776fe6060f1SDimitry Andric if (!RegNum) 777fe6060f1SDimitry Andric return RegNum.takeError(); 778fe6060f1SDimitry Andric llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1); 779fe6060f1SDimitry Andric if (!Offset) 780fe6060f1SDimitry Andric return Offset.takeError(); 781fe6060f1SDimitry Andric llvm::Expected<uint32_t> CFAAddrSpace = 782fe6060f1SDimitry Andric Inst.getOperandAsUnsigned(CFIP, 2); 783fe6060f1SDimitry Andric if (!CFAAddrSpace) 784fe6060f1SDimitry Andric return CFAAddrSpace.takeError(); 785fe6060f1SDimitry Andric Row.getCFAValue() = UnwindLocation::createIsRegisterPlusOffset( 786fe6060f1SDimitry Andric *RegNum, *Offset, *CFAAddrSpace); 787fe6060f1SDimitry Andric break; 788fe6060f1SDimitry Andric } 789fe6060f1SDimitry Andric 790fe6060f1SDimitry Andric case dwarf::DW_CFA_def_cfa_expression: 791fe6060f1SDimitry Andric Row.getCFAValue() = 792fe6060f1SDimitry Andric UnwindLocation::createIsDWARFExpression(*Inst.Expression); 793fe6060f1SDimitry Andric break; 794fe6060f1SDimitry Andric } 795fe6060f1SDimitry Andric } 796fe6060f1SDimitry Andric return Error::success(); 797fe6060f1SDimitry Andric } 798fe6060f1SDimitry Andric 799fe6060f1SDimitry Andric ArrayRef<CFIProgram::OperandType[CFIProgram::MaxOperands]> 800fe6060f1SDimitry Andric CFIProgram::getOperandTypes() { 801fe6060f1SDimitry Andric static OperandType OpTypes[DW_CFA_restore + 1][MaxOperands]; 8020b57cec5SDimitry Andric static bool Initialized = false; 8030b57cec5SDimitry Andric if (Initialized) { 804fe6060f1SDimitry Andric return ArrayRef<OperandType[MaxOperands]>(&OpTypes[0], DW_CFA_restore + 1); 8050b57cec5SDimitry Andric } 8060b57cec5SDimitry Andric Initialized = true; 8070b57cec5SDimitry Andric 808fe6060f1SDimitry Andric #define DECLARE_OP3(OP, OPTYPE0, OPTYPE1, OPTYPE2) \ 8090b57cec5SDimitry Andric do { \ 8100b57cec5SDimitry Andric OpTypes[OP][0] = OPTYPE0; \ 8110b57cec5SDimitry Andric OpTypes[OP][1] = OPTYPE1; \ 812fe6060f1SDimitry Andric OpTypes[OP][2] = OPTYPE2; \ 8130b57cec5SDimitry Andric } while (false) 814fe6060f1SDimitry Andric #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \ 815fe6060f1SDimitry Andric DECLARE_OP3(OP, OPTYPE0, OPTYPE1, OT_None) 8160b57cec5SDimitry Andric #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None) 8170b57cec5SDimitry Andric #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None) 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_set_loc, OT_Address); 8200b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset); 8210b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset); 8220b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset); 8230b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset); 8240b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset); 8250b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset); 8260b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset); 8270b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register); 828fe6060f1SDimitry Andric DECLARE_OP3(DW_CFA_LLVM_def_aspace_cfa, OT_Register, OT_Offset, 829fe6060f1SDimitry Andric OT_AddressSpace); 830fe6060f1SDimitry Andric DECLARE_OP3(DW_CFA_LLVM_def_aspace_cfa_sf, OT_Register, 831fe6060f1SDimitry Andric OT_SignedFactDataOffset, OT_AddressSpace); 8320b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset); 8330b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset); 8340b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression); 8350b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_undefined, OT_Register); 8360b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_same_value, OT_Register); 8370b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset); 8380b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset); 8390b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset); 8400b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset); 8410b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset); 8420b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register); 8430b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression); 8440b57cec5SDimitry Andric DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression); 8450b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_restore, OT_Register); 8460b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_restore_extended, OT_Register); 8470b57cec5SDimitry Andric DECLARE_OP0(DW_CFA_remember_state); 8480b57cec5SDimitry Andric DECLARE_OP0(DW_CFA_restore_state); 8490b57cec5SDimitry Andric DECLARE_OP0(DW_CFA_GNU_window_save); 8500b57cec5SDimitry Andric DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset); 8510b57cec5SDimitry Andric DECLARE_OP0(DW_CFA_nop); 8520b57cec5SDimitry Andric 8530b57cec5SDimitry Andric #undef DECLARE_OP0 8540b57cec5SDimitry Andric #undef DECLARE_OP1 8550b57cec5SDimitry Andric #undef DECLARE_OP2 8560b57cec5SDimitry Andric 857fe6060f1SDimitry Andric return ArrayRef<OperandType[MaxOperands]>(&OpTypes[0], DW_CFA_restore + 1); 8580b57cec5SDimitry Andric } 8590b57cec5SDimitry Andric 8600b57cec5SDimitry Andric /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand. 861e8d8bef9SDimitry Andric void CFIProgram::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts, 862e8d8bef9SDimitry Andric const Instruction &Instr, unsigned OperandIdx, 863*0fca6ea1SDimitry Andric uint64_t Operand, 864*0fca6ea1SDimitry Andric std::optional<uint64_t> &Address) const { 865fe6060f1SDimitry Andric assert(OperandIdx < MaxOperands); 8660b57cec5SDimitry Andric uint8_t Opcode = Instr.Opcode; 8670b57cec5SDimitry Andric OperandType Type = getOperandTypes()[Opcode][OperandIdx]; 8680b57cec5SDimitry Andric 8690b57cec5SDimitry Andric switch (Type) { 8700b57cec5SDimitry Andric case OT_Unset: { 8710b57cec5SDimitry Andric OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to"; 872fe6060f1SDimitry Andric auto OpcodeName = callFrameString(Opcode); 8730b57cec5SDimitry Andric if (!OpcodeName.empty()) 8740b57cec5SDimitry Andric OS << " " << OpcodeName; 8750b57cec5SDimitry Andric else 8760b57cec5SDimitry Andric OS << format(" Opcode %x", Opcode); 8770b57cec5SDimitry Andric break; 8780b57cec5SDimitry Andric } 8790b57cec5SDimitry Andric case OT_None: 8800b57cec5SDimitry Andric break; 8810b57cec5SDimitry Andric case OT_Address: 8820b57cec5SDimitry Andric OS << format(" %" PRIx64, Operand); 883*0fca6ea1SDimitry Andric Address = Operand; 8840b57cec5SDimitry Andric break; 8850b57cec5SDimitry Andric case OT_Offset: 8860b57cec5SDimitry Andric // The offsets are all encoded in a unsigned form, but in practice 8870b57cec5SDimitry Andric // consumers use them signed. It's most certainly legacy due to 8880b57cec5SDimitry Andric // the lack of signed variants in the first Dwarf standards. 8890b57cec5SDimitry Andric OS << format(" %+" PRId64, int64_t(Operand)); 8900b57cec5SDimitry Andric break; 8910b57cec5SDimitry Andric case OT_FactoredCodeOffset: // Always Unsigned 8920b57cec5SDimitry Andric if (CodeAlignmentFactor) 8930b57cec5SDimitry Andric OS << format(" %" PRId64, Operand * CodeAlignmentFactor); 8940b57cec5SDimitry Andric else 8950b57cec5SDimitry Andric OS << format(" %" PRId64 "*code_alignment_factor", Operand); 896*0fca6ea1SDimitry Andric if (Address && CodeAlignmentFactor) { 897*0fca6ea1SDimitry Andric *Address += Operand * CodeAlignmentFactor; 898*0fca6ea1SDimitry Andric OS << format(" to 0x%" PRIx64, *Address); 899*0fca6ea1SDimitry Andric } 9000b57cec5SDimitry Andric break; 9010b57cec5SDimitry Andric case OT_SignedFactDataOffset: 9020b57cec5SDimitry Andric if (DataAlignmentFactor) 9030b57cec5SDimitry Andric OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor); 9040b57cec5SDimitry Andric else 9050b57cec5SDimitry Andric OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand)); 9060b57cec5SDimitry Andric break; 9070b57cec5SDimitry Andric case OT_UnsignedFactDataOffset: 9080b57cec5SDimitry Andric if (DataAlignmentFactor) 9090b57cec5SDimitry Andric OS << format(" %" PRId64, Operand * DataAlignmentFactor); 9100b57cec5SDimitry Andric else 9110b57cec5SDimitry Andric OS << format(" %" PRId64 "*data_alignment_factor" , Operand); 9120b57cec5SDimitry Andric break; 9130b57cec5SDimitry Andric case OT_Register: 914e8d8bef9SDimitry Andric OS << ' '; 915bdd1243dSDimitry Andric printRegister(OS, DumpOpts, Operand); 9160b57cec5SDimitry Andric break; 917fe6060f1SDimitry Andric case OT_AddressSpace: 918fe6060f1SDimitry Andric OS << format(" in addrspace%" PRId64, Operand); 919fe6060f1SDimitry Andric break; 9200b57cec5SDimitry Andric case OT_Expression: 9210b57cec5SDimitry Andric assert(Instr.Expression && "missing DWARFExpression object"); 9220b57cec5SDimitry Andric OS << " "; 923bdd1243dSDimitry Andric Instr.Expression->print(OS, DumpOpts, nullptr); 9240b57cec5SDimitry Andric break; 9250b57cec5SDimitry Andric } 9260b57cec5SDimitry Andric } 9270b57cec5SDimitry Andric 928e8d8bef9SDimitry Andric void CFIProgram::dump(raw_ostream &OS, DIDumpOptions DumpOpts, 929*0fca6ea1SDimitry Andric unsigned IndentLevel, 930*0fca6ea1SDimitry Andric std::optional<uint64_t> Address) const { 9310b57cec5SDimitry Andric for (const auto &Instr : Instructions) { 9320b57cec5SDimitry Andric uint8_t Opcode = Instr.Opcode; 9330b57cec5SDimitry Andric OS.indent(2 * IndentLevel); 934fe6060f1SDimitry Andric OS << callFrameString(Opcode) << ":"; 9350b57cec5SDimitry Andric for (unsigned i = 0; i < Instr.Ops.size(); ++i) 936*0fca6ea1SDimitry Andric printOperand(OS, DumpOpts, Instr, i, Instr.Ops[i], Address); 9370b57cec5SDimitry Andric OS << '\n'; 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric 9415ffd83dbSDimitry Andric // Returns the CIE identifier to be used by the requested format. 9425ffd83dbSDimitry Andric // CIE ids for .debug_frame sections are defined in Section 7.24 of DWARFv5. 9435ffd83dbSDimitry Andric // For CIE ID in .eh_frame sections see 9445ffd83dbSDimitry Andric // https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html 9455ffd83dbSDimitry Andric constexpr uint64_t getCIEId(bool IsDWARF64, bool IsEH) { 9465ffd83dbSDimitry Andric if (IsEH) 9475ffd83dbSDimitry Andric return 0; 9485ffd83dbSDimitry Andric if (IsDWARF64) 9495ffd83dbSDimitry Andric return DW64_CIE_ID; 9505ffd83dbSDimitry Andric return DW_CIE_ID; 9515ffd83dbSDimitry Andric } 9525ffd83dbSDimitry Andric 953bdd1243dSDimitry Andric void CIE::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { 9545ffd83dbSDimitry Andric // A CIE with a zero length is a terminator entry in the .eh_frame section. 955bdd1243dSDimitry Andric if (DumpOpts.IsEH && Length == 0) { 9565ffd83dbSDimitry Andric OS << format("%08" PRIx64, Offset) << " ZERO terminator\n"; 9575ffd83dbSDimitry Andric return; 9585ffd83dbSDimitry Andric } 9595ffd83dbSDimitry Andric 9605ffd83dbSDimitry Andric OS << format("%08" PRIx64, Offset) 9615ffd83dbSDimitry Andric << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length) 962bdd1243dSDimitry Andric << format(" %0*" PRIx64, IsDWARF64 && !DumpOpts.IsEH ? 16 : 8, 963bdd1243dSDimitry Andric getCIEId(IsDWARF64, DumpOpts.IsEH)) 9645ffd83dbSDimitry Andric << " CIE\n" 965fe6060f1SDimitry Andric << " Format: " << FormatString(IsDWARF64) << "\n"; 966bdd1243dSDimitry Andric if (DumpOpts.IsEH && Version != 1) 967fe6060f1SDimitry Andric OS << "WARNING: unsupported CIE version\n"; 968fe6060f1SDimitry Andric OS << format(" Version: %d\n", Version) 9695ffd83dbSDimitry Andric << " Augmentation: \"" << Augmentation << "\"\n"; 9700b57cec5SDimitry Andric if (Version >= 4) { 9710b57cec5SDimitry Andric OS << format(" Address size: %u\n", (uint32_t)AddressSize); 9720b57cec5SDimitry Andric OS << format(" Segment desc size: %u\n", 9730b57cec5SDimitry Andric (uint32_t)SegmentDescriptorSize); 9740b57cec5SDimitry Andric } 9750b57cec5SDimitry Andric OS << format(" Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor); 9760b57cec5SDimitry Andric OS << format(" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor); 9770b57cec5SDimitry Andric OS << format(" Return address column: %d\n", (int32_t)ReturnAddressRegister); 9780b57cec5SDimitry Andric if (Personality) 9790b57cec5SDimitry Andric OS << format(" Personality Address: %016" PRIx64 "\n", *Personality); 9800b57cec5SDimitry Andric if (!AugmentationData.empty()) { 9810b57cec5SDimitry Andric OS << " Augmentation data: "; 9820b57cec5SDimitry Andric for (uint8_t Byte : AugmentationData) 9830b57cec5SDimitry Andric OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf); 9840b57cec5SDimitry Andric OS << "\n"; 9850b57cec5SDimitry Andric } 9860b57cec5SDimitry Andric OS << "\n"; 987*0fca6ea1SDimitry Andric CFIs.dump(OS, DumpOpts, /*IndentLevel=*/1, /*InitialLocation=*/{}); 9880b57cec5SDimitry Andric OS << "\n"; 989fe6060f1SDimitry Andric 990fe6060f1SDimitry Andric if (Expected<UnwindTable> RowsOrErr = UnwindTable::create(this)) 991bdd1243dSDimitry Andric RowsOrErr->dump(OS, DumpOpts, 1); 992fe6060f1SDimitry Andric else { 993fe6060f1SDimitry Andric DumpOpts.RecoverableErrorHandler(joinErrors( 994fe6060f1SDimitry Andric createStringError(errc::invalid_argument, 995fe6060f1SDimitry Andric "decoding the CIE opcodes into rows failed"), 996fe6060f1SDimitry Andric RowsOrErr.takeError())); 997fe6060f1SDimitry Andric } 998fe6060f1SDimitry Andric OS << "\n"; 9990b57cec5SDimitry Andric } 10000b57cec5SDimitry Andric 1001bdd1243dSDimitry Andric void FDE::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { 10025ffd83dbSDimitry Andric OS << format("%08" PRIx64, Offset) 10035ffd83dbSDimitry Andric << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length) 1004bdd1243dSDimitry Andric << format(" %0*" PRIx64, IsDWARF64 && !DumpOpts.IsEH ? 16 : 8, CIEPointer) 10055ffd83dbSDimitry Andric << " FDE cie="; 10065ffd83dbSDimitry Andric if (LinkedCIE) 10075ffd83dbSDimitry Andric OS << format("%08" PRIx64, LinkedCIE->getOffset()); 10085ffd83dbSDimitry Andric else 10095ffd83dbSDimitry Andric OS << "<invalid offset>"; 10105ffd83dbSDimitry Andric OS << format(" pc=%08" PRIx64 "...%08" PRIx64 "\n", InitialLocation, 10115ffd83dbSDimitry Andric InitialLocation + AddressRange); 10125ffd83dbSDimitry Andric OS << " Format: " << FormatString(IsDWARF64) << "\n"; 10130b57cec5SDimitry Andric if (LSDAAddress) 10140b57cec5SDimitry Andric OS << format(" LSDA Address: %016" PRIx64 "\n", *LSDAAddress); 1015*0fca6ea1SDimitry Andric CFIs.dump(OS, DumpOpts, /*IndentLevel=*/1, InitialLocation); 10160b57cec5SDimitry Andric OS << "\n"; 1017fe6060f1SDimitry Andric 1018fe6060f1SDimitry Andric if (Expected<UnwindTable> RowsOrErr = UnwindTable::create(this)) 1019bdd1243dSDimitry Andric RowsOrErr->dump(OS, DumpOpts, 1); 1020fe6060f1SDimitry Andric else { 1021fe6060f1SDimitry Andric DumpOpts.RecoverableErrorHandler(joinErrors( 1022fe6060f1SDimitry Andric createStringError(errc::invalid_argument, 1023fe6060f1SDimitry Andric "decoding the FDE opcodes into rows failed"), 1024fe6060f1SDimitry Andric RowsOrErr.takeError())); 1025fe6060f1SDimitry Andric } 1026fe6060f1SDimitry Andric OS << "\n"; 10270b57cec5SDimitry Andric } 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric DWARFDebugFrame::DWARFDebugFrame(Triple::ArchType Arch, 10300b57cec5SDimitry Andric bool IsEH, uint64_t EHFrameAddress) 10310b57cec5SDimitry Andric : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {} 10320b57cec5SDimitry Andric 10330b57cec5SDimitry Andric DWARFDebugFrame::~DWARFDebugFrame() = default; 10340b57cec5SDimitry Andric 10350b57cec5SDimitry Andric static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, 10368bcb0991SDimitry Andric uint64_t Offset, int Length) { 10370b57cec5SDimitry Andric errs() << "DUMP: "; 10380b57cec5SDimitry Andric for (int i = 0; i < Length; ++i) { 10390b57cec5SDimitry Andric uint8_t c = Data.getU8(&Offset); 10400b57cec5SDimitry Andric errs().write_hex(c); errs() << " "; 10410b57cec5SDimitry Andric } 10420b57cec5SDimitry Andric errs() << "\n"; 10430b57cec5SDimitry Andric } 10440b57cec5SDimitry Andric 10455ffd83dbSDimitry Andric Error DWARFDebugFrame::parse(DWARFDataExtractor Data) { 10468bcb0991SDimitry Andric uint64_t Offset = 0; 10478bcb0991SDimitry Andric DenseMap<uint64_t, CIE *> CIEs; 10480b57cec5SDimitry Andric 10490b57cec5SDimitry Andric while (Data.isValidOffset(Offset)) { 10508bcb0991SDimitry Andric uint64_t StartOffset = Offset; 10510b57cec5SDimitry Andric 10525ffd83dbSDimitry Andric uint64_t Length; 10535ffd83dbSDimitry Andric DwarfFormat Format; 10545ffd83dbSDimitry Andric std::tie(Length, Format) = Data.getInitialLength(&Offset); 10555ffd83dbSDimitry Andric bool IsDWARF64 = Format == DWARF64; 10560b57cec5SDimitry Andric 10575ffd83dbSDimitry Andric // If the Length is 0, then this CIE is a terminator. We add it because some 10585ffd83dbSDimitry Andric // dumper tools might need it to print something special for such entries 10595ffd83dbSDimitry Andric // (e.g. llvm-objdump --dwarf=frames prints "ZERO terminator"). 10605ffd83dbSDimitry Andric if (Length == 0) { 10615ffd83dbSDimitry Andric auto Cie = std::make_unique<CIE>( 10625ffd83dbSDimitry Andric IsDWARF64, StartOffset, 0, 0, SmallString<8>(), 0, 0, 0, 0, 0, 1063bdd1243dSDimitry Andric SmallString<8>(), 0, 0, std::nullopt, std::nullopt, Arch); 10645ffd83dbSDimitry Andric CIEs[StartOffset] = Cie.get(); 10655ffd83dbSDimitry Andric Entries.push_back(std::move(Cie)); 10665ffd83dbSDimitry Andric break; 10670b57cec5SDimitry Andric } 10680b57cec5SDimitry Andric 10690b57cec5SDimitry Andric // At this point, Offset points to the next field after Length. 10700b57cec5SDimitry Andric // Length is the structure size excluding itself. Compute an offset one 10710b57cec5SDimitry Andric // past the end of the structure (needed to know how many instructions to 10720b57cec5SDimitry Andric // read). 10738bcb0991SDimitry Andric uint64_t StartStructureOffset = Offset; 10748bcb0991SDimitry Andric uint64_t EndStructureOffset = Offset + Length; 10750b57cec5SDimitry Andric 10760b57cec5SDimitry Andric // The Id field's size depends on the DWARF format 10775ffd83dbSDimitry Andric Error Err = Error::success(); 10785ffd83dbSDimitry Andric uint64_t Id = Data.getRelocatedValue((IsDWARF64 && !IsEH) ? 8 : 4, &Offset, 10795ffd83dbSDimitry Andric /*SectionIndex=*/nullptr, &Err); 10805ffd83dbSDimitry Andric if (Err) 10815ffd83dbSDimitry Andric return Err; 10820b57cec5SDimitry Andric 10835ffd83dbSDimitry Andric if (Id == getCIEId(IsDWARF64, IsEH)) { 10840b57cec5SDimitry Andric uint8_t Version = Data.getU8(&Offset); 10850b57cec5SDimitry Andric const char *Augmentation = Data.getCStr(&Offset); 10860b57cec5SDimitry Andric StringRef AugmentationString(Augmentation ? Augmentation : ""); 10870b57cec5SDimitry Andric uint8_t AddressSize = Version < 4 ? Data.getAddressSize() : 10880b57cec5SDimitry Andric Data.getU8(&Offset); 10890b57cec5SDimitry Andric Data.setAddressSize(AddressSize); 10900b57cec5SDimitry Andric uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset); 10910b57cec5SDimitry Andric uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset); 10920b57cec5SDimitry Andric int64_t DataAlignmentFactor = Data.getSLEB128(&Offset); 10930b57cec5SDimitry Andric uint64_t ReturnAddressRegister = 10940b57cec5SDimitry Andric Version == 1 ? Data.getU8(&Offset) : Data.getULEB128(&Offset); 10950b57cec5SDimitry Andric 10960b57cec5SDimitry Andric // Parse the augmentation data for EH CIEs 10970b57cec5SDimitry Andric StringRef AugmentationData(""); 10980b57cec5SDimitry Andric uint32_t FDEPointerEncoding = DW_EH_PE_absptr; 10990b57cec5SDimitry Andric uint32_t LSDAPointerEncoding = DW_EH_PE_omit; 1100bdd1243dSDimitry Andric std::optional<uint64_t> Personality; 1101bdd1243dSDimitry Andric std::optional<uint32_t> PersonalityEncoding; 11020b57cec5SDimitry Andric if (IsEH) { 1103bdd1243dSDimitry Andric std::optional<uint64_t> AugmentationLength; 11048bcb0991SDimitry Andric uint64_t StartAugmentationOffset; 11058bcb0991SDimitry Andric uint64_t EndAugmentationOffset; 11060b57cec5SDimitry Andric 11070b57cec5SDimitry Andric // Walk the augmentation string to get all the augmentation data. 11080b57cec5SDimitry Andric for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) { 11090b57cec5SDimitry Andric switch (AugmentationString[i]) { 11100b57cec5SDimitry Andric default: 11115ffd83dbSDimitry Andric return createStringError( 11125ffd83dbSDimitry Andric errc::invalid_argument, 111381ad6265SDimitry Andric "unknown augmentation character %c in entry at 0x%" PRIx64, 111481ad6265SDimitry Andric AugmentationString[i], StartOffset); 11150b57cec5SDimitry Andric case 'L': 11160b57cec5SDimitry Andric LSDAPointerEncoding = Data.getU8(&Offset); 11170b57cec5SDimitry Andric break; 11180b57cec5SDimitry Andric case 'P': { 11190b57cec5SDimitry Andric if (Personality) 11205ffd83dbSDimitry Andric return createStringError( 11215ffd83dbSDimitry Andric errc::invalid_argument, 11225ffd83dbSDimitry Andric "duplicate personality in entry at 0x%" PRIx64, StartOffset); 11230b57cec5SDimitry Andric PersonalityEncoding = Data.getU8(&Offset); 11240b57cec5SDimitry Andric Personality = Data.getEncodedPointer( 11250b57cec5SDimitry Andric &Offset, *PersonalityEncoding, 11260b57cec5SDimitry Andric EHFrameAddress ? EHFrameAddress + Offset : 0); 11270b57cec5SDimitry Andric break; 11280b57cec5SDimitry Andric } 11290b57cec5SDimitry Andric case 'R': 11300b57cec5SDimitry Andric FDEPointerEncoding = Data.getU8(&Offset); 11310b57cec5SDimitry Andric break; 11320b57cec5SDimitry Andric case 'S': 11330b57cec5SDimitry Andric // Current frame is a signal trampoline. 11340b57cec5SDimitry Andric break; 11350b57cec5SDimitry Andric case 'z': 11360b57cec5SDimitry Andric if (i) 11375ffd83dbSDimitry Andric return createStringError( 11385ffd83dbSDimitry Andric errc::invalid_argument, 11395ffd83dbSDimitry Andric "'z' must be the first character at 0x%" PRIx64, StartOffset); 11400b57cec5SDimitry Andric // Parse the augmentation length first. We only parse it if 11410b57cec5SDimitry Andric // the string contains a 'z'. 11420b57cec5SDimitry Andric AugmentationLength = Data.getULEB128(&Offset); 11430b57cec5SDimitry Andric StartAugmentationOffset = Offset; 11448bcb0991SDimitry Andric EndAugmentationOffset = Offset + *AugmentationLength; 11450b57cec5SDimitry Andric break; 11460b57cec5SDimitry Andric case 'B': 11470b57cec5SDimitry Andric // B-Key is used for signing functions associated with this 11480b57cec5SDimitry Andric // augmentation string 11490b57cec5SDimitry Andric break; 115081ad6265SDimitry Andric // This stack frame contains MTE tagged data, so needs to be 115181ad6265SDimitry Andric // untagged on unwind. 115281ad6265SDimitry Andric case 'G': 115381ad6265SDimitry Andric break; 11540b57cec5SDimitry Andric } 11550b57cec5SDimitry Andric } 11560b57cec5SDimitry Andric 115781ad6265SDimitry Andric if (AugmentationLength) { 11580b57cec5SDimitry Andric if (Offset != EndAugmentationOffset) 11595ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 11605ffd83dbSDimitry Andric "parsing augmentation data at 0x%" PRIx64 11615ffd83dbSDimitry Andric " failed", 11625ffd83dbSDimitry Andric StartOffset); 11630b57cec5SDimitry Andric AugmentationData = Data.getData().slice(StartAugmentationOffset, 11640b57cec5SDimitry Andric EndAugmentationOffset); 11650b57cec5SDimitry Andric } 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric 11688bcb0991SDimitry Andric auto Cie = std::make_unique<CIE>( 11695ffd83dbSDimitry Andric IsDWARF64, StartOffset, Length, Version, AugmentationString, 11705ffd83dbSDimitry Andric AddressSize, SegmentDescriptorSize, CodeAlignmentFactor, 11715ffd83dbSDimitry Andric DataAlignmentFactor, ReturnAddressRegister, AugmentationData, 11725ffd83dbSDimitry Andric FDEPointerEncoding, LSDAPointerEncoding, Personality, 11735ffd83dbSDimitry Andric PersonalityEncoding, Arch); 11740b57cec5SDimitry Andric CIEs[StartOffset] = Cie.get(); 11750b57cec5SDimitry Andric Entries.emplace_back(std::move(Cie)); 11760b57cec5SDimitry Andric } else { 11770b57cec5SDimitry Andric // FDE 11780b57cec5SDimitry Andric uint64_t CIEPointer = Id; 11790b57cec5SDimitry Andric uint64_t InitialLocation = 0; 11800b57cec5SDimitry Andric uint64_t AddressRange = 0; 1181bdd1243dSDimitry Andric std::optional<uint64_t> LSDAAddress; 11820b57cec5SDimitry Andric CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer]; 11830b57cec5SDimitry Andric 11840b57cec5SDimitry Andric if (IsEH) { 11850b57cec5SDimitry Andric // The address size is encoded in the CIE we reference. 11860b57cec5SDimitry Andric if (!Cie) 11875ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 11885ffd83dbSDimitry Andric "parsing FDE data at 0x%" PRIx64 11895ffd83dbSDimitry Andric " failed due to missing CIE", 11905ffd83dbSDimitry Andric StartOffset); 1191e8d8bef9SDimitry Andric if (auto Val = 1192e8d8bef9SDimitry Andric Data.getEncodedPointer(&Offset, Cie->getFDEPointerEncoding(), 1193e8d8bef9SDimitry Andric EHFrameAddress + Offset)) { 11940b57cec5SDimitry Andric InitialLocation = *Val; 11950b57cec5SDimitry Andric } 11960b57cec5SDimitry Andric if (auto Val = Data.getEncodedPointer( 11970b57cec5SDimitry Andric &Offset, Cie->getFDEPointerEncoding(), 0)) { 11980b57cec5SDimitry Andric AddressRange = *Val; 11990b57cec5SDimitry Andric } 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric StringRef AugmentationString = Cie->getAugmentationString(); 12020b57cec5SDimitry Andric if (!AugmentationString.empty()) { 12030b57cec5SDimitry Andric // Parse the augmentation length and data for this FDE. 12040b57cec5SDimitry Andric uint64_t AugmentationLength = Data.getULEB128(&Offset); 12050b57cec5SDimitry Andric 12068bcb0991SDimitry Andric uint64_t EndAugmentationOffset = Offset + AugmentationLength; 12070b57cec5SDimitry Andric 12080b57cec5SDimitry Andric // Decode the LSDA if the CIE augmentation string said we should. 12090b57cec5SDimitry Andric if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) { 12100b57cec5SDimitry Andric LSDAAddress = Data.getEncodedPointer( 12110b57cec5SDimitry Andric &Offset, Cie->getLSDAPointerEncoding(), 12120b57cec5SDimitry Andric EHFrameAddress ? Offset + EHFrameAddress : 0); 12130b57cec5SDimitry Andric } 12140b57cec5SDimitry Andric 12150b57cec5SDimitry Andric if (Offset != EndAugmentationOffset) 12165ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 12175ffd83dbSDimitry Andric "parsing augmentation data at 0x%" PRIx64 12185ffd83dbSDimitry Andric " failed", 12195ffd83dbSDimitry Andric StartOffset); 12200b57cec5SDimitry Andric } 12210b57cec5SDimitry Andric } else { 12220b57cec5SDimitry Andric InitialLocation = Data.getRelocatedAddress(&Offset); 12230b57cec5SDimitry Andric AddressRange = Data.getRelocatedAddress(&Offset); 12240b57cec5SDimitry Andric } 12250b57cec5SDimitry Andric 12265ffd83dbSDimitry Andric Entries.emplace_back(new FDE(IsDWARF64, StartOffset, Length, CIEPointer, 12275ffd83dbSDimitry Andric InitialLocation, AddressRange, Cie, 12285ffd83dbSDimitry Andric LSDAAddress, Arch)); 12290b57cec5SDimitry Andric } 12300b57cec5SDimitry Andric 12310b57cec5SDimitry Andric if (Error E = 12325ffd83dbSDimitry Andric Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) 12335ffd83dbSDimitry Andric return E; 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric if (Offset != EndStructureOffset) 12365ffd83dbSDimitry Andric return createStringError( 12375ffd83dbSDimitry Andric errc::invalid_argument, 12385ffd83dbSDimitry Andric "parsing entry instructions at 0x%" PRIx64 " failed", StartOffset); 12390b57cec5SDimitry Andric } 12405ffd83dbSDimitry Andric 12415ffd83dbSDimitry Andric return Error::success(); 12420b57cec5SDimitry Andric } 12430b57cec5SDimitry Andric 12440b57cec5SDimitry Andric FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const { 12450b57cec5SDimitry Andric auto It = partition_point(Entries, [=](const std::unique_ptr<FrameEntry> &E) { 12460b57cec5SDimitry Andric return E->getOffset() < Offset; 12470b57cec5SDimitry Andric }); 12480b57cec5SDimitry Andric if (It != Entries.end() && (*It)->getOffset() == Offset) 12490b57cec5SDimitry Andric return It->get(); 12500b57cec5SDimitry Andric return nullptr; 12510b57cec5SDimitry Andric } 12520b57cec5SDimitry Andric 1253e8d8bef9SDimitry Andric void DWARFDebugFrame::dump(raw_ostream &OS, DIDumpOptions DumpOpts, 1254bdd1243dSDimitry Andric std::optional<uint64_t> Offset) const { 1255bdd1243dSDimitry Andric DumpOpts.IsEH = IsEH; 12560b57cec5SDimitry Andric if (Offset) { 12570b57cec5SDimitry Andric if (auto *Entry = getEntryAtOffset(*Offset)) 1258bdd1243dSDimitry Andric Entry->dump(OS, DumpOpts); 12590b57cec5SDimitry Andric return; 12600b57cec5SDimitry Andric } 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric OS << "\n"; 12630b57cec5SDimitry Andric for (const auto &Entry : Entries) 1264bdd1243dSDimitry Andric Entry->dump(OS, DumpOpts); 12650b57cec5SDimitry Andric } 1266