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