1 //===- DWARFDebugFrame.h - Parsing of .debug_frame ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" 11 #include "llvm/ADT/ArrayRef.h" 12 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/ADT/Optional.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/BinaryFormat/Dwarf.h" 19 #include "llvm/Support/Casting.h" 20 #include "llvm/Support/Compiler.h" 21 #include "llvm/Support/DataExtractor.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/Format.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include <algorithm> 26 #include <cassert> 27 #include <cinttypes> 28 #include <cstdint> 29 #include <string> 30 #include <vector> 31 32 using namespace llvm; 33 using namespace dwarf; 34 35 /// \brief Abstract frame entry defining the common interface concrete 36 /// entries implement. 37 class llvm::FrameEntry { 38 public: 39 enum FrameKind {FK_CIE, FK_FDE}; 40 41 FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length) 42 : Kind(K), Offset(Offset), Length(Length) {} 43 44 virtual ~FrameEntry() = default; 45 46 FrameKind getKind() const { return Kind; } 47 virtual uint64_t getOffset() const { return Offset; } 48 49 /// \brief Parse and store a sequence of CFI instructions from Data, 50 /// starting at *Offset and ending at EndOffset. If everything 51 /// goes well, *Offset should be equal to EndOffset when this method 52 /// returns. Otherwise, an error occurred. 53 virtual void parseInstructions(DataExtractor Data, uint32_t *Offset, 54 uint32_t EndOffset); 55 56 /// \brief Dump the entry header to the given output stream. 57 virtual void dumpHeader(raw_ostream &OS) const = 0; 58 59 /// \brief Dump the entry's instructions to the given output stream. 60 virtual void dumpInstructions(raw_ostream &OS) const; 61 62 protected: 63 const FrameKind Kind; 64 65 /// \brief Offset of this entry in the section. 66 uint64_t Offset; 67 68 /// \brief Entry length as specified in DWARF. 69 uint64_t Length; 70 71 /// An entry may contain CFI instructions. An instruction consists of an 72 /// opcode and an optional sequence of operands. 73 using Operands = std::vector<uint64_t>; 74 struct Instruction { 75 Instruction(uint8_t Opcode) 76 : Opcode(Opcode) 77 {} 78 79 uint8_t Opcode; 80 Operands Ops; 81 }; 82 83 std::vector<Instruction> Instructions; 84 85 /// Convenience methods to add a new instruction with the given opcode and 86 /// operands to the Instructions vector. 87 void addInstruction(uint8_t Opcode) { 88 Instructions.push_back(Instruction(Opcode)); 89 } 90 91 void addInstruction(uint8_t Opcode, uint64_t Operand1) { 92 Instructions.push_back(Instruction(Opcode)); 93 Instructions.back().Ops.push_back(Operand1); 94 } 95 96 void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) { 97 Instructions.push_back(Instruction(Opcode)); 98 Instructions.back().Ops.push_back(Operand1); 99 Instructions.back().Ops.push_back(Operand2); 100 } 101 }; 102 103 // See DWARF standard v3, section 7.23 104 const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0; 105 const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f; 106 107 void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset, 108 uint32_t EndOffset) { 109 while (*Offset < EndOffset) { 110 uint8_t Opcode = Data.getU8(Offset); 111 // Some instructions have a primary opcode encoded in the top bits. 112 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK; 113 114 if (Primary) { 115 // If it's a primary opcode, the first operand is encoded in the bottom 116 // bits of the opcode itself. 117 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK; 118 switch (Primary) { 119 default: llvm_unreachable("Impossible primary CFI opcode"); 120 case DW_CFA_advance_loc: 121 case DW_CFA_restore: 122 addInstruction(Primary, Op1); 123 break; 124 case DW_CFA_offset: 125 addInstruction(Primary, Op1, Data.getULEB128(Offset)); 126 break; 127 } 128 } else { 129 // Extended opcode - its value is Opcode itself. 130 switch (Opcode) { 131 default: llvm_unreachable("Invalid extended CFI opcode"); 132 case DW_CFA_nop: 133 case DW_CFA_remember_state: 134 case DW_CFA_restore_state: 135 case DW_CFA_GNU_window_save: 136 // No operands 137 addInstruction(Opcode); 138 break; 139 case DW_CFA_set_loc: 140 // Operands: Address 141 addInstruction(Opcode, Data.getAddress(Offset)); 142 break; 143 case DW_CFA_advance_loc1: 144 // Operands: 1-byte delta 145 addInstruction(Opcode, Data.getU8(Offset)); 146 break; 147 case DW_CFA_advance_loc2: 148 // Operands: 2-byte delta 149 addInstruction(Opcode, Data.getU16(Offset)); 150 break; 151 case DW_CFA_advance_loc4: 152 // Operands: 4-byte delta 153 addInstruction(Opcode, Data.getU32(Offset)); 154 break; 155 case DW_CFA_restore_extended: 156 case DW_CFA_undefined: 157 case DW_CFA_same_value: 158 case DW_CFA_def_cfa_register: 159 case DW_CFA_def_cfa_offset: 160 // Operands: ULEB128 161 addInstruction(Opcode, Data.getULEB128(Offset)); 162 break; 163 case DW_CFA_def_cfa_offset_sf: 164 // Operands: SLEB128 165 addInstruction(Opcode, Data.getSLEB128(Offset)); 166 break; 167 case DW_CFA_offset_extended: 168 case DW_CFA_register: 169 case DW_CFA_def_cfa: 170 case DW_CFA_val_offset: { 171 // Operands: ULEB128, ULEB128 172 // Note: We can not embed getULEB128 directly into function 173 // argument list. getULEB128 changes Offset and order of evaluation 174 // for arguments is unspecified. 175 auto op1 = Data.getULEB128(Offset); 176 auto op2 = Data.getULEB128(Offset); 177 addInstruction(Opcode, op1, op2); 178 break; 179 } 180 case DW_CFA_offset_extended_sf: 181 case DW_CFA_def_cfa_sf: 182 case DW_CFA_val_offset_sf: { 183 // Operands: ULEB128, SLEB128 184 // Note: see comment for the previous case 185 auto op1 = Data.getULEB128(Offset); 186 auto op2 = (uint64_t)Data.getSLEB128(Offset); 187 addInstruction(Opcode, op1, op2); 188 break; 189 } 190 case DW_CFA_def_cfa_expression: 191 // FIXME: Parse the actual instruction. 192 *Offset += Data.getULEB128(Offset); 193 break; 194 case DW_CFA_expression: 195 case DW_CFA_val_expression: { 196 // FIXME: Parse the actual instruction. 197 Data.getULEB128(Offset); 198 *Offset += Data.getULEB128(Offset); 199 break; 200 } 201 } 202 } 203 } 204 } 205 206 namespace { 207 208 /// \brief DWARF Common Information Entry (CIE) 209 class CIE : public FrameEntry { 210 public: 211 // CIEs (and FDEs) are simply container classes, so the only sensible way to 212 // create them is by providing the full parsed contents in the constructor. 213 CIE(uint64_t Offset, uint64_t Length, uint8_t Version, 214 SmallString<8> Augmentation, uint8_t AddressSize, 215 uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor, 216 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister, 217 SmallString<8> AugmentationData, uint32_t FDEPointerEncoding, 218 uint32_t LSDAPointerEncoding) 219 : FrameEntry(FK_CIE, Offset, Length), Version(Version), 220 Augmentation(std::move(Augmentation)), AddressSize(AddressSize), 221 SegmentDescriptorSize(SegmentDescriptorSize), 222 CodeAlignmentFactor(CodeAlignmentFactor), 223 DataAlignmentFactor(DataAlignmentFactor), 224 ReturnAddressRegister(ReturnAddressRegister), 225 AugmentationData(std::move(AugmentationData)), 226 FDEPointerEncoding(FDEPointerEncoding), 227 LSDAPointerEncoding(LSDAPointerEncoding) {} 228 229 ~CIE() override = default; 230 231 StringRef getAugmentationString() const { return Augmentation; } 232 uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; } 233 int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; } 234 235 uint32_t getFDEPointerEncoding() const { 236 return FDEPointerEncoding; 237 } 238 239 uint32_t getLSDAPointerEncoding() const { 240 return LSDAPointerEncoding; 241 } 242 243 void dumpHeader(raw_ostream &OS) const override { 244 OS << format("%08x %08x %08x CIE", 245 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID) 246 << "\n"; 247 OS << format(" Version: %d\n", Version); 248 OS << " Augmentation: \"" << Augmentation << "\"\n"; 249 if (Version >= 4) { 250 OS << format(" Address size: %u\n", 251 (uint32_t)AddressSize); 252 OS << format(" Segment desc size: %u\n", 253 (uint32_t)SegmentDescriptorSize); 254 } 255 OS << format(" Code alignment factor: %u\n", 256 (uint32_t)CodeAlignmentFactor); 257 OS << format(" Data alignment factor: %d\n", 258 (int32_t)DataAlignmentFactor); 259 OS << format(" Return address column: %d\n", 260 (int32_t)ReturnAddressRegister); 261 if (!AugmentationData.empty()) { 262 OS << " Augmentation data: "; 263 for (uint8_t Byte : AugmentationData) 264 OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf); 265 OS << "\n"; 266 } 267 OS << "\n"; 268 } 269 270 static bool classof(const FrameEntry *FE) { 271 return FE->getKind() == FK_CIE; 272 } 273 274 private: 275 /// The following fields are defined in section 6.4.1 of the DWARF standard v4 276 uint8_t Version; 277 SmallString<8> Augmentation; 278 uint8_t AddressSize; 279 uint8_t SegmentDescriptorSize; 280 uint64_t CodeAlignmentFactor; 281 int64_t DataAlignmentFactor; 282 uint64_t ReturnAddressRegister; 283 284 // The following are used when the CIE represents an EH frame entry. 285 SmallString<8> AugmentationData; 286 uint32_t FDEPointerEncoding; 287 uint32_t LSDAPointerEncoding; 288 }; 289 290 /// \brief DWARF Frame Description Entry (FDE) 291 class FDE : public FrameEntry { 292 public: 293 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with 294 // an offset to the CIE (provided by parsing the FDE header). The CIE itself 295 // is obtained lazily once it's actually required. 296 FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset, 297 uint64_t InitialLocation, uint64_t AddressRange, 298 CIE *Cie) 299 : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset), 300 InitialLocation(InitialLocation), AddressRange(AddressRange), 301 LinkedCIE(Cie) {} 302 303 ~FDE() override = default; 304 305 CIE *getLinkedCIE() const { return LinkedCIE; } 306 307 void dumpHeader(raw_ostream &OS) const override { 308 OS << format("%08x %08x %08x FDE ", 309 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset); 310 OS << format("cie=%08x pc=%08x...%08x\n", 311 (int32_t)LinkedCIEOffset, 312 (uint32_t)InitialLocation, 313 (uint32_t)InitialLocation + (uint32_t)AddressRange); 314 } 315 316 static bool classof(const FrameEntry *FE) { 317 return FE->getKind() == FK_FDE; 318 } 319 320 private: 321 /// The following fields are defined in section 6.4.1 of the DWARF standard v3 322 uint64_t LinkedCIEOffset; 323 uint64_t InitialLocation; 324 uint64_t AddressRange; 325 CIE *LinkedCIE; 326 }; 327 328 /// \brief Types of operands to CF instructions. 329 enum OperandType { 330 OT_Unset, 331 OT_None, 332 OT_Address, 333 OT_Offset, 334 OT_FactoredCodeOffset, 335 OT_SignedFactDataOffset, 336 OT_UnsignedFactDataOffset, 337 OT_Register, 338 OT_Expression 339 }; 340 341 } // end anonymous namespace 342 343 /// \brief Initialize the array describing the types of operands. 344 static ArrayRef<OperandType[2]> getOperandTypes() { 345 static OperandType OpTypes[DW_CFA_restore+1][2]; 346 347 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \ 348 do { \ 349 OpTypes[OP][0] = OPTYPE0; \ 350 OpTypes[OP][1] = OPTYPE1; \ 351 } while (false) 352 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None) 353 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None) 354 355 DECLARE_OP1(DW_CFA_set_loc, OT_Address); 356 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset); 357 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset); 358 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset); 359 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset); 360 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset); 361 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset); 362 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset); 363 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register); 364 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset); 365 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset); 366 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression); 367 DECLARE_OP1(DW_CFA_undefined, OT_Register); 368 DECLARE_OP1(DW_CFA_same_value, OT_Register); 369 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset); 370 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset); 371 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset); 372 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset); 373 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset); 374 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register); 375 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression); 376 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression); 377 DECLARE_OP1(DW_CFA_restore, OT_Register); 378 DECLARE_OP1(DW_CFA_restore_extended, OT_Register); 379 DECLARE_OP0(DW_CFA_remember_state); 380 DECLARE_OP0(DW_CFA_restore_state); 381 DECLARE_OP0(DW_CFA_GNU_window_save); 382 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset); 383 DECLARE_OP0(DW_CFA_nop); 384 385 #undef DECLARE_OP0 386 #undef DECLARE_OP1 387 #undef DECLARE_OP2 388 389 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1); 390 } 391 392 static ArrayRef<OperandType[2]> OpTypes = getOperandTypes(); 393 394 /// \brief Print \p Opcode's operand number \p OperandIdx which has 395 /// value \p Operand. 396 static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx, 397 uint64_t Operand, uint64_t CodeAlignmentFactor, 398 int64_t DataAlignmentFactor) { 399 assert(OperandIdx < 2); 400 OperandType Type = OpTypes[Opcode][OperandIdx]; 401 402 switch (Type) { 403 case OT_Unset: { 404 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to"; 405 auto OpcodeName = CallFrameString(Opcode); 406 if (!OpcodeName.empty()) 407 OS << " " << OpcodeName; 408 else 409 OS << format(" Opcode %x", Opcode); 410 break; 411 } 412 case OT_None: 413 break; 414 case OT_Address: 415 OS << format(" %" PRIx64, Operand); 416 break; 417 case OT_Offset: 418 // The offsets are all encoded in a unsigned form, but in practice 419 // consumers use them signed. It's most certainly legacy due to 420 // the lack of signed variants in the first Dwarf standards. 421 OS << format(" %+" PRId64, int64_t(Operand)); 422 break; 423 case OT_FactoredCodeOffset: // Always Unsigned 424 if (CodeAlignmentFactor) 425 OS << format(" %" PRId64, Operand * CodeAlignmentFactor); 426 else 427 OS << format(" %" PRId64 "*code_alignment_factor" , Operand); 428 break; 429 case OT_SignedFactDataOffset: 430 if (DataAlignmentFactor) 431 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor); 432 else 433 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand)); 434 break; 435 case OT_UnsignedFactDataOffset: 436 if (DataAlignmentFactor) 437 OS << format(" %" PRId64, Operand * DataAlignmentFactor); 438 else 439 OS << format(" %" PRId64 "*data_alignment_factor" , Operand); 440 break; 441 case OT_Register: 442 OS << format(" reg%" PRId64, Operand); 443 break; 444 case OT_Expression: 445 OS << " expression"; 446 break; 447 } 448 } 449 450 void FrameEntry::dumpInstructions(raw_ostream &OS) const { 451 uint64_t CodeAlignmentFactor = 0; 452 int64_t DataAlignmentFactor = 0; 453 const CIE *Cie = dyn_cast<CIE>(this); 454 455 if (!Cie) 456 Cie = cast<FDE>(this)->getLinkedCIE(); 457 if (Cie) { 458 CodeAlignmentFactor = Cie->getCodeAlignmentFactor(); 459 DataAlignmentFactor = Cie->getDataAlignmentFactor(); 460 } 461 462 for (const auto &Instr : Instructions) { 463 uint8_t Opcode = Instr.Opcode; 464 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK) 465 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK; 466 OS << " " << CallFrameString(Opcode) << ":"; 467 for (unsigned i = 0; i < Instr.Ops.size(); ++i) 468 printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor, 469 DataAlignmentFactor); 470 OS << '\n'; 471 } 472 } 473 474 DWARFDebugFrame::DWARFDebugFrame(bool IsEH) : IsEH(IsEH) {} 475 476 DWARFDebugFrame::~DWARFDebugFrame() = default; 477 478 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, 479 uint32_t Offset, int Length) { 480 errs() << "DUMP: "; 481 for (int i = 0; i < Length; ++i) { 482 uint8_t c = Data.getU8(&Offset); 483 errs().write_hex(c); errs() << " "; 484 } 485 errs() << "\n"; 486 } 487 488 static unsigned getSizeForEncoding(const DataExtractor &Data, 489 unsigned symbolEncoding) { 490 unsigned format = symbolEncoding & 0x0f; 491 switch (format) { 492 default: llvm_unreachable("Unknown Encoding"); 493 case DW_EH_PE_absptr: 494 case DW_EH_PE_signed: 495 return Data.getAddressSize(); 496 case DW_EH_PE_udata2: 497 case DW_EH_PE_sdata2: 498 return 2; 499 case DW_EH_PE_udata4: 500 case DW_EH_PE_sdata4: 501 return 4; 502 case DW_EH_PE_udata8: 503 case DW_EH_PE_sdata8: 504 return 8; 505 } 506 } 507 508 static uint64_t readPointer(const DataExtractor &Data, uint32_t &Offset, 509 unsigned Encoding) { 510 switch (getSizeForEncoding(Data, Encoding)) { 511 case 2: 512 return Data.getU16(&Offset); 513 case 4: 514 return Data.getU32(&Offset); 515 case 8: 516 return Data.getU64(&Offset); 517 default: 518 llvm_unreachable("Illegal data size"); 519 } 520 } 521 522 // This is a workaround for old compilers which do not allow 523 // noreturn attribute usage in lambdas. Once the support for those 524 // compilers are phased out, we can remove this and return back to 525 // a ReportError lambda: [StartOffset](const char *ErrorMsg). 526 static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset, 527 const char *ErrorMsg) { 528 std::string Str; 529 raw_string_ostream OS(Str); 530 OS << format(ErrorMsg, StartOffset); 531 OS.flush(); 532 report_fatal_error(Str); 533 } 534 535 void DWARFDebugFrame::parse(DataExtractor Data) { 536 uint32_t Offset = 0; 537 DenseMap<uint32_t, CIE *> CIEs; 538 539 while (Data.isValidOffset(Offset)) { 540 uint32_t StartOffset = Offset; 541 542 bool IsDWARF64 = false; 543 uint64_t Length = Data.getU32(&Offset); 544 uint64_t Id; 545 546 if (Length == UINT32_MAX) { 547 // DWARF-64 is distinguished by the first 32 bits of the initial length 548 // field being 0xffffffff. Then, the next 64 bits are the actual entry 549 // length. 550 IsDWARF64 = true; 551 Length = Data.getU64(&Offset); 552 } 553 554 // At this point, Offset points to the next field after Length. 555 // Length is the structure size excluding itself. Compute an offset one 556 // past the end of the structure (needed to know how many instructions to 557 // read). 558 // TODO: For honest DWARF64 support, DataExtractor will have to treat 559 // offset_ptr as uint64_t* 560 uint32_t StartStructureOffset = Offset; 561 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length); 562 563 // The Id field's size depends on the DWARF format 564 Id = Data.getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4); 565 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || 566 Id == DW_CIE_ID || 567 (IsEH && !Id)); 568 569 if (IsCIE) { 570 uint8_t Version = Data.getU8(&Offset); 571 const char *Augmentation = Data.getCStr(&Offset); 572 StringRef AugmentationString(Augmentation ? Augmentation : ""); 573 uint8_t AddressSize = Version < 4 ? Data.getAddressSize() : 574 Data.getU8(&Offset); 575 Data.setAddressSize(AddressSize); 576 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset); 577 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset); 578 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset); 579 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset); 580 581 // Parse the augmentation data for EH CIEs 582 StringRef AugmentationData(""); 583 uint32_t FDEPointerEncoding = DW_EH_PE_omit; 584 uint32_t LSDAPointerEncoding = DW_EH_PE_omit; 585 if (IsEH) { 586 Optional<uint32_t> PersonalityEncoding; 587 Optional<uint64_t> Personality; 588 589 Optional<uint64_t> AugmentationLength; 590 uint32_t StartAugmentationOffset; 591 uint32_t EndAugmentationOffset; 592 593 // Walk the augmentation string to get all the augmentation data. 594 for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) { 595 switch (AugmentationString[i]) { 596 default: 597 ReportError(StartOffset, 598 "Unknown augmentation character in entry at %lx"); 599 case 'L': 600 LSDAPointerEncoding = Data.getU8(&Offset); 601 break; 602 case 'P': { 603 if (Personality) 604 ReportError(StartOffset, 605 "Duplicate personality in entry at %lx"); 606 PersonalityEncoding = Data.getU8(&Offset); 607 Personality = readPointer(Data, Offset, *PersonalityEncoding); 608 break; 609 } 610 case 'R': 611 FDEPointerEncoding = Data.getU8(&Offset); 612 break; 613 case 'z': 614 if (i) 615 ReportError(StartOffset, 616 "'z' must be the first character at %lx"); 617 // Parse the augmentation length first. We only parse it if 618 // the string contains a 'z'. 619 AugmentationLength = Data.getULEB128(&Offset); 620 StartAugmentationOffset = Offset; 621 EndAugmentationOffset = Offset + 622 static_cast<uint32_t>(*AugmentationLength); 623 } 624 } 625 626 if (AugmentationLength.hasValue()) { 627 if (Offset != EndAugmentationOffset) 628 ReportError(StartOffset, "Parsing augmentation data at %lx failed"); 629 630 AugmentationData = Data.getData().slice(StartAugmentationOffset, 631 EndAugmentationOffset); 632 } 633 } 634 635 auto Cie = llvm::make_unique<CIE>(StartOffset, Length, Version, 636 AugmentationString, AddressSize, 637 SegmentDescriptorSize, 638 CodeAlignmentFactor, 639 DataAlignmentFactor, 640 ReturnAddressRegister, 641 AugmentationData, FDEPointerEncoding, 642 LSDAPointerEncoding); 643 CIEs[StartOffset] = Cie.get(); 644 Entries.emplace_back(std::move(Cie)); 645 } else { 646 // FDE 647 uint64_t CIEPointer = Id; 648 uint64_t InitialLocation = 0; 649 uint64_t AddressRange = 0; 650 CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer]; 651 652 if (IsEH) { 653 // The address size is encoded in the CIE we reference. 654 if (!Cie) 655 ReportError(StartOffset, 656 "Parsing FDE data at %lx failed due to missing CIE"); 657 658 InitialLocation = readPointer(Data, Offset, 659 Cie->getFDEPointerEncoding()); 660 AddressRange = readPointer(Data, Offset, 661 Cie->getFDEPointerEncoding()); 662 663 StringRef AugmentationString = Cie->getAugmentationString(); 664 if (!AugmentationString.empty()) { 665 // Parse the augmentation length and data for this FDE. 666 uint64_t AugmentationLength = Data.getULEB128(&Offset); 667 668 uint32_t EndAugmentationOffset = 669 Offset + static_cast<uint32_t>(AugmentationLength); 670 671 // Decode the LSDA if the CIE augmentation string said we should. 672 if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) 673 readPointer(Data, Offset, Cie->getLSDAPointerEncoding()); 674 675 if (Offset != EndAugmentationOffset) 676 ReportError(StartOffset, "Parsing augmentation data at %lx failed"); 677 } 678 } else { 679 InitialLocation = Data.getAddress(&Offset); 680 AddressRange = Data.getAddress(&Offset); 681 } 682 683 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer, 684 InitialLocation, AddressRange, 685 Cie)); 686 } 687 688 Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset); 689 690 if (Offset != EndStructureOffset) 691 ReportError(StartOffset, "Parsing entry instructions at %lx failed"); 692 } 693 } 694 695 void DWARFDebugFrame::dump(raw_ostream &OS) const { 696 OS << "\n"; 697 for (const auto &Entry : Entries) { 698 Entry->dumpHeader(OS); 699 Entry->dumpInstructions(OS); 700 OS << "\n"; 701 } 702 } 703