1 //===- DWARFDebugFrame.h - Parsing of .debug_frame ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
10 #include "llvm/ADT/DenseMap.h"
11 #include "llvm/ADT/Optional.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/Dwarf.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/DataExtractor.h"
18 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <cinttypes>
25 #include <cstdint>
26 #include <string>
27 #include <vector>
28 
29 using namespace llvm;
30 using namespace dwarf;
31 
32 
33 // See DWARF standard v3, section 7.23
34 const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
35 const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
36 
37 Error CFIProgram::parse(DWARFDataExtractor Data, uint64_t *Offset,
38                         uint64_t EndOffset) {
39   while (*Offset < EndOffset) {
40     uint8_t Opcode = Data.getRelocatedValue(1, Offset);
41     // Some instructions have a primary opcode encoded in the top bits.
42     uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
43 
44     if (Primary) {
45       // If it's a primary opcode, the first operand is encoded in the bottom
46       // bits of the opcode itself.
47       uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
48       switch (Primary) {
49       default:
50         return createStringError(errc::illegal_byte_sequence,
51                                  "Invalid primary CFI opcode 0x%" PRIx8,
52                                  Primary);
53       case DW_CFA_advance_loc:
54       case DW_CFA_restore:
55         addInstruction(Primary, Op1);
56         break;
57       case DW_CFA_offset:
58         addInstruction(Primary, Op1, Data.getULEB128(Offset));
59         break;
60       }
61     } else {
62       // Extended opcode - its value is Opcode itself.
63       switch (Opcode) {
64       default:
65         return createStringError(errc::illegal_byte_sequence,
66                                  "Invalid extended CFI opcode 0x%" PRIx8,
67                                  Opcode);
68       case DW_CFA_nop:
69       case DW_CFA_remember_state:
70       case DW_CFA_restore_state:
71       case DW_CFA_GNU_window_save:
72         // No operands
73         addInstruction(Opcode);
74         break;
75       case DW_CFA_set_loc:
76         // Operands: Address
77         addInstruction(Opcode, Data.getRelocatedAddress(Offset));
78         break;
79       case DW_CFA_advance_loc1:
80         // Operands: 1-byte delta
81         addInstruction(Opcode, Data.getRelocatedValue(1, Offset));
82         break;
83       case DW_CFA_advance_loc2:
84         // Operands: 2-byte delta
85         addInstruction(Opcode, Data.getRelocatedValue(2, Offset));
86         break;
87       case DW_CFA_advance_loc4:
88         // Operands: 4-byte delta
89         addInstruction(Opcode, Data.getRelocatedValue(4, Offset));
90         break;
91       case DW_CFA_restore_extended:
92       case DW_CFA_undefined:
93       case DW_CFA_same_value:
94       case DW_CFA_def_cfa_register:
95       case DW_CFA_def_cfa_offset:
96       case DW_CFA_GNU_args_size:
97         // Operands: ULEB128
98         addInstruction(Opcode, Data.getULEB128(Offset));
99         break;
100       case DW_CFA_def_cfa_offset_sf:
101         // Operands: SLEB128
102         addInstruction(Opcode, Data.getSLEB128(Offset));
103         break;
104       case DW_CFA_offset_extended:
105       case DW_CFA_register:
106       case DW_CFA_def_cfa:
107       case DW_CFA_val_offset: {
108         // Operands: ULEB128, ULEB128
109         // Note: We can not embed getULEB128 directly into function
110         // argument list. getULEB128 changes Offset and order of evaluation
111         // for arguments is unspecified.
112         auto op1 = Data.getULEB128(Offset);
113         auto op2 = Data.getULEB128(Offset);
114         addInstruction(Opcode, op1, op2);
115         break;
116         }
117         case DW_CFA_offset_extended_sf:
118         case DW_CFA_def_cfa_sf:
119         case DW_CFA_val_offset_sf: {
120           // Operands: ULEB128, SLEB128
121           // Note: see comment for the previous case
122           auto op1 = Data.getULEB128(Offset);
123           auto op2 = (uint64_t)Data.getSLEB128(Offset);
124           addInstruction(Opcode, op1, op2);
125           break;
126         }
127         case DW_CFA_def_cfa_expression: {
128           uint32_t ExprLength = Data.getULEB128(Offset);
129           addInstruction(Opcode, 0);
130           DataExtractor Extractor(
131               Data.getData().slice(*Offset, *Offset + ExprLength),
132               Data.isLittleEndian(), Data.getAddressSize());
133           // Note. We do not pass the DWARF format to DWARFExpression, because
134           // DW_OP_call_ref, the only operation which depends on the format, is
135           // prohibited in call frame instructions, see sec. 6.4.2 in DWARFv5.
136           Instructions.back().Expression =
137               DWARFExpression(Extractor, Data.getAddressSize());
138           *Offset += ExprLength;
139           break;
140         }
141         case DW_CFA_expression:
142         case DW_CFA_val_expression: {
143           auto RegNum = Data.getULEB128(Offset);
144           auto BlockLength = Data.getULEB128(Offset);
145           addInstruction(Opcode, RegNum, 0);
146           DataExtractor Extractor(
147               Data.getData().slice(*Offset, *Offset + BlockLength),
148               Data.isLittleEndian(), Data.getAddressSize());
149           // Note. We do not pass the DWARF format to DWARFExpression, because
150           // DW_OP_call_ref, the only operation which depends on the format, is
151           // prohibited in call frame instructions, see sec. 6.4.2 in DWARFv5.
152           Instructions.back().Expression =
153               DWARFExpression(Extractor, Data.getAddressSize());
154           *Offset += BlockLength;
155           break;
156         }
157       }
158     }
159   }
160 
161   return Error::success();
162 }
163 
164 namespace {
165 
166 
167 } // end anonymous namespace
168 
169 ArrayRef<CFIProgram::OperandType[2]> CFIProgram::getOperandTypes() {
170   static OperandType OpTypes[DW_CFA_restore+1][2];
171   static bool Initialized = false;
172   if (Initialized) {
173     return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
174   }
175   Initialized = true;
176 
177 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1)       \
178   do {                                          \
179     OpTypes[OP][0] = OPTYPE0;                   \
180     OpTypes[OP][1] = OPTYPE1;                   \
181   } while (false)
182 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
183 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
184 
185   DECLARE_OP1(DW_CFA_set_loc, OT_Address);
186   DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
187   DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
188   DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
189   DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
190   DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
191   DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
192   DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
193   DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
194   DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
195   DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
196   DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
197   DECLARE_OP1(DW_CFA_undefined, OT_Register);
198   DECLARE_OP1(DW_CFA_same_value, OT_Register);
199   DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
200   DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
201   DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
202   DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
203   DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
204   DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
205   DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
206   DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
207   DECLARE_OP1(DW_CFA_restore, OT_Register);
208   DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
209   DECLARE_OP0(DW_CFA_remember_state);
210   DECLARE_OP0(DW_CFA_restore_state);
211   DECLARE_OP0(DW_CFA_GNU_window_save);
212   DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
213   DECLARE_OP0(DW_CFA_nop);
214 
215 #undef DECLARE_OP0
216 #undef DECLARE_OP1
217 #undef DECLARE_OP2
218 
219   return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
220 }
221 
222 /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
223 void CFIProgram::printOperand(raw_ostream &OS, const MCRegisterInfo *MRI,
224                               bool IsEH, const Instruction &Instr,
225                               unsigned OperandIdx, uint64_t Operand) const {
226   assert(OperandIdx < 2);
227   uint8_t Opcode = Instr.Opcode;
228   OperandType Type = getOperandTypes()[Opcode][OperandIdx];
229 
230   switch (Type) {
231   case OT_Unset: {
232     OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
233     auto OpcodeName = CallFrameString(Opcode, Arch);
234     if (!OpcodeName.empty())
235       OS << " " << OpcodeName;
236     else
237       OS << format(" Opcode %x",  Opcode);
238     break;
239   }
240   case OT_None:
241     break;
242   case OT_Address:
243     OS << format(" %" PRIx64, Operand);
244     break;
245   case OT_Offset:
246     // The offsets are all encoded in a unsigned form, but in practice
247     // consumers use them signed. It's most certainly legacy due to
248     // the lack of signed variants in the first Dwarf standards.
249     OS << format(" %+" PRId64, int64_t(Operand));
250     break;
251   case OT_FactoredCodeOffset: // Always Unsigned
252     if (CodeAlignmentFactor)
253       OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
254     else
255       OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
256     break;
257   case OT_SignedFactDataOffset:
258     if (DataAlignmentFactor)
259       OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
260     else
261       OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
262     break;
263   case OT_UnsignedFactDataOffset:
264     if (DataAlignmentFactor)
265       OS << format(" %" PRId64, Operand * DataAlignmentFactor);
266     else
267       OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
268     break;
269   case OT_Register:
270     OS << format(" reg%" PRId64, Operand);
271     break;
272   case OT_Expression:
273     assert(Instr.Expression && "missing DWARFExpression object");
274     OS << " ";
275     Instr.Expression->print(OS, MRI, nullptr, IsEH);
276     break;
277   }
278 }
279 
280 void CFIProgram::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
281                       unsigned IndentLevel) const {
282   for (const auto &Instr : Instructions) {
283     uint8_t Opcode = Instr.Opcode;
284     if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
285       Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
286     OS.indent(2 * IndentLevel);
287     OS << CallFrameString(Opcode, Arch) << ":";
288     for (unsigned i = 0; i < Instr.Ops.size(); ++i)
289       printOperand(OS, MRI, IsEH, Instr, i, Instr.Ops[i]);
290     OS << '\n';
291   }
292 }
293 
294 // Returns the CIE identifier to be used by the requested format.
295 // CIE ids for .debug_frame sections are defined in Section 7.24 of DWARFv5.
296 // For CIE ID in .eh_frame sections see
297 // https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
298 constexpr uint64_t getCIEId(bool IsDWARF64, bool IsEH) {
299   if (IsEH)
300     return 0;
301   if (IsDWARF64)
302     return DW64_CIE_ID;
303   return DW_CIE_ID;
304 }
305 
306 void CIE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const {
307   // A CIE with a zero length is a terminator entry in the .eh_frame sextion.
308   if (IsEH && Length == 0) {
309     OS << format("%08" PRIx64, Offset) << " ZERO terminator\n";
310     return;
311   }
312 
313   OS << format("%08" PRIx64, Offset)
314      << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length)
315      << format(" %0*" PRIx64, IsDWARF64 && !IsEH ? 16 : 8,
316                getCIEId(IsDWARF64, IsEH))
317      << " CIE\n";
318   OS << format("  Version:               %d\n", Version);
319   OS << "  Augmentation:          \"" << Augmentation << "\"\n";
320   if (Version >= 4) {
321     OS << format("  Address size:          %u\n", (uint32_t)AddressSize);
322     OS << format("  Segment desc size:     %u\n",
323                  (uint32_t)SegmentDescriptorSize);
324   }
325   OS << format("  Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor);
326   OS << format("  Data alignment factor: %d\n", (int32_t)DataAlignmentFactor);
327   OS << format("  Return address column: %d\n", (int32_t)ReturnAddressRegister);
328   if (Personality)
329     OS << format("  Personality Address: %016" PRIx64 "\n", *Personality);
330   if (!AugmentationData.empty()) {
331     OS << "  Augmentation data:    ";
332     for (uint8_t Byte : AugmentationData)
333       OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
334     OS << "\n";
335   }
336   OS << "\n";
337   CFIs.dump(OS, MRI, IsEH);
338   OS << "\n";
339 }
340 
341 void FDE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const {
342   OS << format("%08" PRIx64, Offset)
343      << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length)
344      << format(" %0*" PRIx64, IsDWARF64 && !IsEH ? 16 : 8, CIEPointer)
345      << " FDE cie=";
346   if (LinkedCIE)
347     OS << format("%08" PRIx64, LinkedCIE->getOffset());
348   else
349     OS << "<invalid offset>";
350   OS << format(" pc=%08" PRIx64 "...%08" PRIx64 "\n", InitialLocation,
351                InitialLocation + AddressRange);
352   if (LSDAAddress)
353     OS << format("  LSDA Address: %016" PRIx64 "\n", *LSDAAddress);
354   CFIs.dump(OS, MRI, IsEH);
355   OS << "\n";
356 }
357 
358 DWARFDebugFrame::DWARFDebugFrame(Triple::ArchType Arch,
359     bool IsEH, uint64_t EHFrameAddress)
360     : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
361 
362 DWARFDebugFrame::~DWARFDebugFrame() = default;
363 
364 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
365                                               uint64_t Offset, int Length) {
366   errs() << "DUMP: ";
367   for (int i = 0; i < Length; ++i) {
368     uint8_t c = Data.getU8(&Offset);
369     errs().write_hex(c); errs() << " ";
370   }
371   errs() << "\n";
372 }
373 
374 Error DWARFDebugFrame::parse(DWARFDataExtractor Data) {
375   uint64_t Offset = 0;
376   DenseMap<uint64_t, CIE *> CIEs;
377 
378   while (Data.isValidOffset(Offset)) {
379     uint64_t StartOffset = Offset;
380 
381     uint64_t Length;
382     DwarfFormat Format;
383     std::tie(Length, Format) = Data.getInitialLength(&Offset);
384     bool IsDWARF64 = Format == DWARF64;
385 
386     // If the Length is 0, then this CIE is a terminator. We add it because some
387     // dumper tools might need it to print something special for such entries
388     // (e.g. llvm-objdump --dwarf=frames prints "ZERO terminator").
389     if (Length == 0) {
390       auto Cie = std::make_unique<CIE>(
391           IsDWARF64, StartOffset, 0, 0, SmallString<8>(), 0, 0, 0, 0, 0,
392           SmallString<8>(), 0, 0, None, None, Arch);
393       CIEs[StartOffset] = Cie.get();
394       Entries.push_back(std::move(Cie));
395       break;
396     }
397 
398     // At this point, Offset points to the next field after Length.
399     // Length is the structure size excluding itself. Compute an offset one
400     // past the end of the structure (needed to know how many instructions to
401     // read).
402     uint64_t StartStructureOffset = Offset;
403     uint64_t EndStructureOffset = Offset + Length;
404 
405     // The Id field's size depends on the DWARF format
406     Error Err = Error::success();
407     uint64_t Id = Data.getRelocatedValue((IsDWARF64 && !IsEH) ? 8 : 4, &Offset,
408                                          /*SectionIndex=*/nullptr, &Err);
409     if (Err)
410       return Err;
411 
412     if (Id == getCIEId(IsDWARF64, IsEH)) {
413       uint8_t Version = Data.getU8(&Offset);
414       const char *Augmentation = Data.getCStr(&Offset);
415       StringRef AugmentationString(Augmentation ? Augmentation : "");
416       uint8_t AddressSize = Version < 4 ? Data.getAddressSize() :
417                                           Data.getU8(&Offset);
418       Data.setAddressSize(AddressSize);
419       uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
420       uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
421       int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
422       uint64_t ReturnAddressRegister =
423           Version == 1 ? Data.getU8(&Offset) : Data.getULEB128(&Offset);
424 
425       // Parse the augmentation data for EH CIEs
426       StringRef AugmentationData("");
427       uint32_t FDEPointerEncoding = DW_EH_PE_absptr;
428       uint32_t LSDAPointerEncoding = DW_EH_PE_omit;
429       Optional<uint64_t> Personality;
430       Optional<uint32_t> PersonalityEncoding;
431       if (IsEH) {
432         Optional<uint64_t> AugmentationLength;
433         uint64_t StartAugmentationOffset;
434         uint64_t EndAugmentationOffset;
435 
436         // Walk the augmentation string to get all the augmentation data.
437         for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) {
438           switch (AugmentationString[i]) {
439           default:
440             return createStringError(
441                 errc::invalid_argument,
442                 "unknown augmentation character in entry at 0x%" PRIx64,
443                 StartOffset);
444           case 'L':
445             LSDAPointerEncoding = Data.getU8(&Offset);
446             break;
447           case 'P': {
448             if (Personality)
449               return createStringError(
450                   errc::invalid_argument,
451                   "duplicate personality in entry at 0x%" PRIx64, StartOffset);
452             PersonalityEncoding = Data.getU8(&Offset);
453             Personality = Data.getEncodedPointer(
454                 &Offset, *PersonalityEncoding,
455                 EHFrameAddress ? EHFrameAddress + Offset : 0);
456             break;
457           }
458           case 'R':
459             FDEPointerEncoding = Data.getU8(&Offset);
460             break;
461           case 'S':
462             // Current frame is a signal trampoline.
463             break;
464           case 'z':
465             if (i)
466               return createStringError(
467                   errc::invalid_argument,
468                   "'z' must be the first character at 0x%" PRIx64, StartOffset);
469             // Parse the augmentation length first.  We only parse it if
470             // the string contains a 'z'.
471             AugmentationLength = Data.getULEB128(&Offset);
472             StartAugmentationOffset = Offset;
473             EndAugmentationOffset = Offset + *AugmentationLength;
474             break;
475           case 'B':
476             // B-Key is used for signing functions associated with this
477             // augmentation string
478             break;
479           }
480         }
481 
482         if (AugmentationLength.hasValue()) {
483           if (Offset != EndAugmentationOffset)
484             return createStringError(errc::invalid_argument,
485                                      "parsing augmentation data at 0x%" PRIx64
486                                      " failed",
487                                      StartOffset);
488           AugmentationData = Data.getData().slice(StartAugmentationOffset,
489                                                   EndAugmentationOffset);
490         }
491       }
492 
493       auto Cie = std::make_unique<CIE>(
494           IsDWARF64, StartOffset, Length, Version, AugmentationString,
495           AddressSize, SegmentDescriptorSize, CodeAlignmentFactor,
496           DataAlignmentFactor, ReturnAddressRegister, AugmentationData,
497           FDEPointerEncoding, LSDAPointerEncoding, Personality,
498           PersonalityEncoding, Arch);
499       CIEs[StartOffset] = Cie.get();
500       Entries.emplace_back(std::move(Cie));
501     } else {
502       // FDE
503       uint64_t CIEPointer = Id;
504       uint64_t InitialLocation = 0;
505       uint64_t AddressRange = 0;
506       Optional<uint64_t> LSDAAddress;
507       CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
508 
509       if (IsEH) {
510         // The address size is encoded in the CIE we reference.
511         if (!Cie)
512           return createStringError(errc::invalid_argument,
513                                    "parsing FDE data at 0x%" PRIx64
514                                    " failed due to missing CIE",
515                                    StartOffset);
516         if (auto Val = Data.getEncodedPointer(
517                 &Offset, Cie->getFDEPointerEncoding(),
518                 EHFrameAddress ? EHFrameAddress + Offset : 0)) {
519           InitialLocation = *Val;
520         }
521         if (auto Val = Data.getEncodedPointer(
522                 &Offset, Cie->getFDEPointerEncoding(), 0)) {
523           AddressRange = *Val;
524         }
525 
526         StringRef AugmentationString = Cie->getAugmentationString();
527         if (!AugmentationString.empty()) {
528           // Parse the augmentation length and data for this FDE.
529           uint64_t AugmentationLength = Data.getULEB128(&Offset);
530 
531           uint64_t EndAugmentationOffset = Offset + AugmentationLength;
532 
533           // Decode the LSDA if the CIE augmentation string said we should.
534           if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) {
535             LSDAAddress = Data.getEncodedPointer(
536                 &Offset, Cie->getLSDAPointerEncoding(),
537                 EHFrameAddress ? Offset + EHFrameAddress : 0);
538           }
539 
540           if (Offset != EndAugmentationOffset)
541             return createStringError(errc::invalid_argument,
542                                      "parsing augmentation data at 0x%" PRIx64
543                                      " failed",
544                                      StartOffset);
545         }
546       } else {
547         InitialLocation = Data.getRelocatedAddress(&Offset);
548         AddressRange = Data.getRelocatedAddress(&Offset);
549       }
550 
551       Entries.emplace_back(new FDE(IsDWARF64, StartOffset, Length, CIEPointer,
552                                    InitialLocation, AddressRange, Cie,
553                                    LSDAAddress, Arch));
554     }
555 
556     if (Error E =
557             Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset))
558       return E;
559 
560     if (Offset != EndStructureOffset)
561       return createStringError(
562           errc::invalid_argument,
563           "parsing entry instructions at 0x%" PRIx64 " failed", StartOffset);
564   }
565 
566   return Error::success();
567 }
568 
569 FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
570   auto It = partition_point(Entries, [=](const std::unique_ptr<FrameEntry> &E) {
571     return E->getOffset() < Offset;
572   });
573   if (It != Entries.end() && (*It)->getOffset() == Offset)
574     return It->get();
575   return nullptr;
576 }
577 
578 void DWARFDebugFrame::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
579                            Optional<uint64_t> Offset) const {
580   if (Offset) {
581     if (auto *Entry = getEntryAtOffset(*Offset))
582       Entry->dump(OS, MRI, IsEH);
583     return;
584   }
585 
586   OS << "\n";
587   for (const auto &Entry : Entries)
588     Entry->dump(OS, MRI, IsEH);
589 }
590