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