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/MC/MCRegisterInfo.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/DataExtractor.h"
19 #include "llvm/Support/Errc.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <algorithm>
24 #include <cassert>
25 #include <cinttypes>
26 #include <cstdint>
27 #include <string>
28 
29 using namespace llvm;
30 using namespace dwarf;
31 
32 static void printRegister(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
33                           unsigned RegNum) {
34   if (MRI) {
35     if (Optional<unsigned> LLVMRegNum = MRI->getLLVMRegNum(RegNum, IsEH)) {
36       if (const char *RegName = MRI->getName(*LLVMRegNum)) {
37         OS << RegName;
38         return;
39       }
40     }
41   }
42   OS << "reg" << RegNum;
43 }
44 
45 UnwindLocation UnwindLocation::createUnspecified() { return {Unspecified}; }
46 
47 UnwindLocation UnwindLocation::createUndefined() { return {Undefined}; }
48 
49 UnwindLocation UnwindLocation::createSame() { return {Same}; }
50 
51 UnwindLocation UnwindLocation::createIsConstant(int32_t Value) {
52   return {Constant, InvalidRegisterNumber, Value, false};
53 }
54 
55 UnwindLocation UnwindLocation::createIsCFAPlusOffset(int32_t Offset) {
56   return {CFAPlusOffset, InvalidRegisterNumber, Offset, false};
57 }
58 
59 UnwindLocation UnwindLocation::createAtCFAPlusOffset(int32_t Offset) {
60   return {CFAPlusOffset, InvalidRegisterNumber, Offset, true};
61 }
62 
63 UnwindLocation UnwindLocation::createIsRegisterPlusOffset(uint32_t RegNum,
64                                                           int32_t Offset) {
65   return {RegPlusOffset, RegNum, Offset, false};
66 }
67 UnwindLocation UnwindLocation::createAtRegisterPlusOffset(uint32_t RegNum,
68                                                           int32_t Offset) {
69   return {RegPlusOffset, RegNum, Offset, true};
70 }
71 
72 UnwindLocation UnwindLocation::createIsDWARFExpression(DWARFExpression Expr) {
73   return {Expr, false};
74 }
75 
76 UnwindLocation UnwindLocation::createAtDWARFExpression(DWARFExpression Expr) {
77   return {Expr, true};
78 }
79 
80 void UnwindLocation::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
81                           bool IsEH) const {
82   if (Dereference)
83     OS << '[';
84   switch (Kind) {
85   case Unspecified:
86     OS << "unspecified";
87     break;
88   case Undefined:
89     OS << "undefined";
90     break;
91   case Same:
92     OS << "same";
93     break;
94   case CFAPlusOffset:
95     OS << "CFA";
96     if (Offset == 0)
97       break;
98     if (Offset > 0)
99       OS << "+";
100     OS << Offset;
101     break;
102   case RegPlusOffset:
103     printRegister(OS, MRI, IsEH, RegNum);
104     if (Offset == 0)
105       break;
106     if (Offset > 0)
107       OS << "+";
108     OS << Offset;
109     break;
110   case DWARFExpr:
111     Expr->print(OS, DIDumpOptions(), MRI, nullptr, IsEH);
112     break;
113   case Constant:
114     OS << Offset;
115     break;
116   }
117   if (Dereference)
118     OS << ']';
119 }
120 
121 raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS,
122                                      const UnwindLocation &UL) {
123   UL.dump(OS, nullptr, false);
124   return OS;
125 }
126 
127 bool UnwindLocation::operator==(const UnwindLocation &RHS) const {
128   if (Kind != RHS.Kind)
129     return false;
130   switch (Kind) {
131   case Unspecified:
132   case Undefined:
133   case Same:
134     return true;
135   case CFAPlusOffset:
136     return Offset == RHS.Offset && Dereference == RHS.Dereference;
137   case RegPlusOffset:
138     return RegNum == RHS.RegNum && Offset == RHS.Offset &&
139            Dereference == RHS.Dereference;
140   case DWARFExpr:
141     return *Expr == *RHS.Expr && Dereference == RHS.Dereference;
142   case Constant:
143     return Offset == RHS.Offset;
144   }
145   return false;
146 }
147 
148 void RegisterLocations::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
149                              bool IsEH) const {
150   bool First = true;
151   for (const auto &RegLocPair : Locations) {
152     if (First)
153       First = false;
154     else
155       OS << ", ";
156     printRegister(OS, MRI, IsEH, RegLocPair.first);
157     OS << '=';
158     RegLocPair.second.dump(OS, MRI, IsEH);
159   }
160 }
161 
162 raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS,
163                                      const RegisterLocations &RL) {
164   RL.dump(OS, nullptr, false);
165   return OS;
166 }
167 
168 void UnwindRow::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
169                      unsigned IndentLevel) const {
170   OS.indent(2 * IndentLevel);
171   if (hasAddress())
172     OS << format("0x%" PRIx64 ": ", *Address);
173   OS << "CFA=";
174   CFAValue.dump(OS, MRI, IsEH);
175   if (RegLocs.hasLocations()) {
176     OS << ": ";
177     RegLocs.dump(OS, MRI, IsEH);
178   }
179   OS << "\n";
180 }
181 
182 raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindRow &Row) {
183   Row.dump(OS, nullptr, false, 0);
184   return OS;
185 }
186 
187 void UnwindTable::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
188                        unsigned IndentLevel) const {
189   for (const UnwindRow &Row : Rows)
190     Row.dump(OS, MRI, IsEH, IndentLevel);
191 }
192 
193 raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindTable &Rows) {
194   Rows.dump(OS, nullptr, false, 0);
195   return OS;
196 }
197 
198 Expected<UnwindTable> UnwindTable::create(const FDE *Fde) {
199   UnwindTable UT;
200   UnwindRow Row;
201   Row.setAddress(Fde->getInitialLocation());
202   UT.EndAddress = Fde->getInitialLocation() + Fde->getAddressRange();
203 
204   const CIE *Cie = Fde->getLinkedCIE();
205   if (Cie == nullptr)
206     return createStringError(errc::invalid_argument,
207                              "unable to get CIE for FDE at offset 0x%" PRIx64,
208                              Fde->getOffset());
209 
210   if (Error CieError = UT.parseRows(Cie->cfis(), Row, nullptr))
211     return std::move(CieError);
212   // We need to save the initial locations of registers from the CIE parsing
213   // in case we run into DW_CFA_restore or DW_CFA_restore_extended opcodes.
214   const RegisterLocations InitialLocs = Row.getRegisterLocations();
215   if (Error FdeError = UT.parseRows(Fde->cfis(), Row, &InitialLocs))
216     return std::move(FdeError);
217   UT.Rows.push_back(Row);
218   return UT;
219 }
220 
221 Expected<UnwindTable> UnwindTable::create(const CIE *Cie) {
222   UnwindTable UT;
223   UnwindRow Row;
224   if (Error CieError = UT.parseRows(Cie->cfis(), Row, nullptr))
225     return std::move(CieError);
226   UT.Rows.push_back(Row);
227   return UT;
228 }
229 
230 // See DWARF standard v3, section 7.23
231 const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
232 const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
233 
234 Error CFIProgram::parse(DWARFDataExtractor Data, uint64_t *Offset,
235                         uint64_t EndOffset) {
236   DataExtractor::Cursor C(*Offset);
237   while (C && C.tell() < EndOffset) {
238     uint8_t Opcode = Data.getRelocatedValue(C, 1);
239     if (!C)
240       break;
241 
242     // Some instructions have a primary opcode encoded in the top bits.
243     if (uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK) {
244       // If it's a primary opcode, the first operand is encoded in the bottom
245       // bits of the opcode itself.
246       uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
247       switch (Primary) {
248       case DW_CFA_advance_loc:
249       case DW_CFA_restore:
250         addInstruction(Primary, Op1);
251         break;
252       case DW_CFA_offset:
253         addInstruction(Primary, Op1, Data.getULEB128(C));
254         break;
255       default:
256         llvm_unreachable("invalid primary CFI opcode");
257       }
258       continue;
259     }
260 
261     // Extended opcode - its value is Opcode itself.
262     switch (Opcode) {
263     default:
264       return createStringError(errc::illegal_byte_sequence,
265                                "invalid extended CFI opcode 0x%" PRIx8, Opcode);
266     case DW_CFA_nop:
267     case DW_CFA_remember_state:
268     case DW_CFA_restore_state:
269     case DW_CFA_GNU_window_save:
270       // No operands
271       addInstruction(Opcode);
272       break;
273     case DW_CFA_set_loc:
274       // Operands: Address
275       addInstruction(Opcode, Data.getRelocatedAddress(C));
276       break;
277     case DW_CFA_advance_loc1:
278       // Operands: 1-byte delta
279       addInstruction(Opcode, Data.getRelocatedValue(C, 1));
280       break;
281     case DW_CFA_advance_loc2:
282       // Operands: 2-byte delta
283       addInstruction(Opcode, Data.getRelocatedValue(C, 2));
284       break;
285     case DW_CFA_advance_loc4:
286       // Operands: 4-byte delta
287       addInstruction(Opcode, Data.getRelocatedValue(C, 4));
288       break;
289     case DW_CFA_restore_extended:
290     case DW_CFA_undefined:
291     case DW_CFA_same_value:
292     case DW_CFA_def_cfa_register:
293     case DW_CFA_def_cfa_offset:
294     case DW_CFA_GNU_args_size:
295       // Operands: ULEB128
296       addInstruction(Opcode, Data.getULEB128(C));
297       break;
298     case DW_CFA_def_cfa_offset_sf:
299       // Operands: SLEB128
300       addInstruction(Opcode, Data.getSLEB128(C));
301       break;
302     case DW_CFA_offset_extended:
303     case DW_CFA_register:
304     case DW_CFA_def_cfa:
305     case DW_CFA_val_offset: {
306       // Operands: ULEB128, ULEB128
307       // Note: We can not embed getULEB128 directly into function
308       // argument list. getULEB128 changes Offset and order of evaluation
309       // for arguments is unspecified.
310       uint64_t op1 = Data.getULEB128(C);
311       uint64_t op2 = Data.getULEB128(C);
312       addInstruction(Opcode, op1, op2);
313       break;
314     }
315     case DW_CFA_offset_extended_sf:
316     case DW_CFA_def_cfa_sf:
317     case DW_CFA_val_offset_sf: {
318       // Operands: ULEB128, SLEB128
319       // Note: see comment for the previous case
320       uint64_t op1 = Data.getULEB128(C);
321       uint64_t op2 = (uint64_t)Data.getSLEB128(C);
322       addInstruction(Opcode, op1, op2);
323       break;
324     }
325     case DW_CFA_def_cfa_expression: {
326       uint64_t ExprLength = Data.getULEB128(C);
327       addInstruction(Opcode, 0);
328       StringRef Expression = Data.getBytes(C, ExprLength);
329 
330       DataExtractor Extractor(Expression, Data.isLittleEndian(),
331                               Data.getAddressSize());
332       // Note. We do not pass the DWARF format to DWARFExpression, because
333       // DW_OP_call_ref, the only operation which depends on the format, is
334       // prohibited in call frame instructions, see sec. 6.4.2 in DWARFv5.
335       Instructions.back().Expression =
336           DWARFExpression(Extractor, Data.getAddressSize());
337       break;
338     }
339     case DW_CFA_expression:
340     case DW_CFA_val_expression: {
341       uint64_t RegNum = Data.getULEB128(C);
342       addInstruction(Opcode, RegNum, 0);
343 
344       uint64_t BlockLength = Data.getULEB128(C);
345       StringRef Expression = Data.getBytes(C, BlockLength);
346       DataExtractor Extractor(Expression, Data.isLittleEndian(),
347                               Data.getAddressSize());
348       // Note. We do not pass the DWARF format to DWARFExpression, because
349       // DW_OP_call_ref, the only operation which depends on the format, is
350       // prohibited in call frame instructions, see sec. 6.4.2 in DWARFv5.
351       Instructions.back().Expression =
352           DWARFExpression(Extractor, Data.getAddressSize());
353       break;
354     }
355     }
356   }
357 
358   *Offset = C.tell();
359   return C.takeError();
360 }
361 
362 StringRef CFIProgram::callFrameString(unsigned Opcode) const {
363   return dwarf::CallFrameString(Opcode, Arch);
364 }
365 
366 const char *CFIProgram::operandTypeString(CFIProgram::OperandType OT) {
367 #define ENUM_TO_CSTR(e)                                                        \
368   case e:                                                                      \
369     return #e;
370   switch (OT) {
371     ENUM_TO_CSTR(OT_Unset);
372     ENUM_TO_CSTR(OT_None);
373     ENUM_TO_CSTR(OT_Address);
374     ENUM_TO_CSTR(OT_Offset);
375     ENUM_TO_CSTR(OT_FactoredCodeOffset);
376     ENUM_TO_CSTR(OT_SignedFactDataOffset);
377     ENUM_TO_CSTR(OT_UnsignedFactDataOffset);
378     ENUM_TO_CSTR(OT_Register);
379     ENUM_TO_CSTR(OT_Expression);
380   }
381   return "<unknown CFIProgram::OperandType>";
382 }
383 
384 llvm::Expected<uint64_t>
385 CFIProgram::Instruction::getOperandAsUnsigned(const CFIProgram &CFIP,
386                                               uint32_t OperandIdx) const {
387   if (OperandIdx >= 2)
388     return createStringError(errc::invalid_argument,
389                              "operand index %" PRIu32 " is not valid",
390                              OperandIdx);
391   OperandType Type = CFIP.getOperandTypes()[Opcode][OperandIdx];
392   uint64_t Operand = Ops[OperandIdx];
393   switch (Type) {
394   case OT_Unset:
395   case OT_None:
396   case OT_Expression:
397     return createStringError(errc::invalid_argument,
398                              "op[%" PRIu32 "] has type %s which has no value",
399                              OperandIdx, CFIProgram::operandTypeString(Type));
400 
401   case OT_Offset:
402   case OT_SignedFactDataOffset:
403   case OT_UnsignedFactDataOffset:
404     return createStringError(
405         errc::invalid_argument,
406         "op[%" PRIu32 "] has OperandType OT_Offset which produces a signed "
407         "result, call getOperandAsSigned instead",
408         OperandIdx);
409 
410   case OT_Address:
411   case OT_Register:
412     return Operand;
413 
414   case OT_FactoredCodeOffset: {
415     const uint64_t CodeAlignmentFactor = CFIP.codeAlign();
416     if (CodeAlignmentFactor == 0)
417       return createStringError(
418           errc::invalid_argument,
419           "op[%" PRIu32 "] has type OT_FactoredCodeOffset but code alignment "
420           "is zero",
421           OperandIdx);
422     return Operand * CodeAlignmentFactor;
423   }
424   }
425   llvm_unreachable("invalid operand type");
426 }
427 
428 llvm::Expected<int64_t>
429 CFIProgram::Instruction::getOperandAsSigned(const CFIProgram &CFIP,
430                                             uint32_t OperandIdx) const {
431   if (OperandIdx >= 2)
432     return createStringError(errc::invalid_argument,
433                              "operand index %" PRIu32 " is not valid",
434                              OperandIdx);
435   OperandType Type = CFIP.getOperandTypes()[Opcode][OperandIdx];
436   uint64_t Operand = Ops[OperandIdx];
437   switch (Type) {
438   case OT_Unset:
439   case OT_None:
440   case OT_Expression:
441     return createStringError(errc::invalid_argument,
442                              "op[%" PRIu32 "] has type %s which has no value",
443                              OperandIdx, CFIProgram::operandTypeString(Type));
444 
445   case OT_Address:
446   case OT_Register:
447     return createStringError(
448         errc::invalid_argument,
449         "op[%" PRIu32 "] has OperandType %s which produces an unsigned result, "
450         "call getOperandAsUnsigned instead",
451         OperandIdx, CFIProgram::operandTypeString(Type));
452 
453   case OT_Offset:
454     return (int64_t)Operand;
455 
456   case OT_FactoredCodeOffset:
457   case OT_SignedFactDataOffset: {
458     const int64_t DataAlignmentFactor = CFIP.dataAlign();
459     if (DataAlignmentFactor == 0)
460       return createStringError(errc::invalid_argument,
461                                "op[%" PRIu32 "] has type %s but data "
462                                "alignment is zero",
463                                OperandIdx, CFIProgram::operandTypeString(Type));
464     return int64_t(Operand) * DataAlignmentFactor;
465   }
466 
467   case OT_UnsignedFactDataOffset: {
468     const int64_t DataAlignmentFactor = CFIP.dataAlign();
469     if (DataAlignmentFactor == 0)
470       return createStringError(errc::invalid_argument,
471                                "op[%" PRIu32
472                                "] has type OT_UnsignedFactDataOffset but data "
473                                "alignment is zero",
474                                OperandIdx);
475     return Operand * DataAlignmentFactor;
476   }
477   }
478   llvm_unreachable("invalid operand type");
479 }
480 
481 Error UnwindTable::parseRows(const CFIProgram &CFIP, UnwindRow &Row,
482                              const RegisterLocations *InitialLocs) {
483   std::vector<RegisterLocations> RegisterStates;
484   for (const CFIProgram::Instruction &Inst : CFIP) {
485     switch (Inst.Opcode) {
486     case dwarf::DW_CFA_set_loc: {
487       // The DW_CFA_set_loc instruction takes a single operand that
488       // represents a target address. The required action is to create a new
489       // table row using the specified address as the location. All other
490       // values in the new row are initially identical to the current row.
491       // The new location value is always greater than the current one. If
492       // the segment_size field of this FDE's CIE is non- zero, the initial
493       // location is preceded by a segment selector of the given length
494       llvm::Expected<uint64_t> NewAddress = Inst.getOperandAsUnsigned(CFIP, 0);
495       if (!NewAddress)
496         return NewAddress.takeError();
497       if (*NewAddress <= Row.getAddress())
498         return createStringError(
499             errc::invalid_argument,
500             "%s with adrress 0x%" PRIx64 " which must be greater than the "
501             "current row address 0x%" PRIx64,
502             CFIP.callFrameString(Inst.Opcode).str().c_str(), *NewAddress,
503             Row.getAddress());
504       Rows.push_back(Row);
505       Row.setAddress(*NewAddress);
506       break;
507     }
508 
509     case dwarf::DW_CFA_advance_loc:
510     case dwarf::DW_CFA_advance_loc1:
511     case dwarf::DW_CFA_advance_loc2:
512     case dwarf::DW_CFA_advance_loc4: {
513       // The DW_CFA_advance instruction takes a single operand that
514       // represents a constant delta. The required action is to create a new
515       // table row with a location value that is computed by taking the
516       // current entry’s location value and adding the value of delta *
517       // code_alignment_factor. All other values in the new row are initially
518       // identical to the current row.
519       Rows.push_back(Row);
520       llvm::Expected<uint64_t> Offset = Inst.getOperandAsUnsigned(CFIP, 0);
521       if (!Offset)
522         return Offset.takeError();
523       Row.slideAddress(*Offset);
524       break;
525     }
526 
527     case dwarf::DW_CFA_restore:
528     case dwarf::DW_CFA_restore_extended: {
529       // The DW_CFA_restore instruction takes a single operand (encoded with
530       // the opcode) that represents a register number. The required action
531       // is to change the rule for the indicated register to the rule
532       // assigned it by the initial_instructions in the CIE.
533       if (InitialLocs == nullptr)
534         return createStringError(
535             errc::invalid_argument, "%s encountered while parsing a CIE",
536             CFIP.callFrameString(Inst.Opcode).str().c_str());
537       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
538       if (!RegNum)
539         return RegNum.takeError();
540       if (Optional<UnwindLocation> O =
541               InitialLocs->getRegisterLocation(*RegNum))
542         Row.getRegisterLocations().setRegisterLocation(*RegNum, *O);
543       else
544         Row.getRegisterLocations().removeRegisterLocation(*RegNum);
545       break;
546     }
547 
548     case dwarf::DW_CFA_offset:
549     case dwarf::DW_CFA_offset_extended:
550     case dwarf::DW_CFA_offset_extended_sf: {
551       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
552       if (!RegNum)
553         return RegNum.takeError();
554       llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1);
555       if (!Offset)
556         return Offset.takeError();
557       Row.getRegisterLocations().setRegisterLocation(
558           *RegNum, UnwindLocation::createAtCFAPlusOffset(*Offset));
559       break;
560     }
561 
562     case dwarf::DW_CFA_nop:
563       break;
564 
565     case dwarf::DW_CFA_remember_state:
566       RegisterStates.push_back(Row.getRegisterLocations());
567       break;
568 
569     case dwarf::DW_CFA_restore_state:
570       if (RegisterStates.empty())
571         return createStringError(errc::invalid_argument,
572                                  "DW_CFA_restore_state without a matching "
573                                  "previous DW_CFA_remember_state");
574       Row.getRegisterLocations() = RegisterStates.back();
575       RegisterStates.pop_back();
576       break;
577 
578     case dwarf::DW_CFA_GNU_window_save:
579       switch (CFIP.triple()) {
580       case Triple::aarch64:
581       case Triple::aarch64_be:
582       case Triple::aarch64_32: {
583         // DW_CFA_GNU_window_save is used for different things on different
584         // architectures. For aarch64 it is known as
585         // DW_CFA_AARCH64_negate_ra_state. The action is to toggle the
586         // value of the return address state between 1 and 0. If there is
587         // no rule for the AARCH64_DWARF_PAUTH_RA_STATE register, then it
588         // should be initially set to 1.
589         constexpr uint32_t AArch64DWARFPAuthRaState = 34;
590         auto LRLoc = Row.getRegisterLocations().getRegisterLocation(
591             AArch64DWARFPAuthRaState);
592         if (LRLoc) {
593           if (LRLoc->getLocation() == UnwindLocation::Constant) {
594             // Toggle the constant value from 0 to 1 or 1 to 0.
595             LRLoc->setConstant(LRLoc->getConstant() ^ 1);
596           } else {
597             return createStringError(
598                 errc::invalid_argument,
599                 "%s encountered when existing rule for this register is not "
600                 "a constant",
601                 CFIP.callFrameString(Inst.Opcode).str().c_str());
602           }
603         } else {
604           Row.getRegisterLocations().setRegisterLocation(
605               AArch64DWARFPAuthRaState, UnwindLocation::createIsConstant(1));
606         }
607         break;
608       }
609 
610       case Triple::sparc:
611       case Triple::sparcv9:
612       case Triple::sparcel:
613         for (uint32_t RegNum = 16; RegNum < 32; ++RegNum) {
614           Row.getRegisterLocations().setRegisterLocation(
615               RegNum, UnwindLocation::createAtCFAPlusOffset((RegNum - 16) * 8));
616         }
617         break;
618 
619       default: {
620         return createStringError(
621             errc::not_supported,
622             "DW_CFA opcode %#x is not supported for architecture %s",
623             Inst.Opcode, Triple::getArchTypeName(CFIP.triple()).str().c_str());
624 
625         break;
626       }
627       }
628       break;
629 
630     case dwarf::DW_CFA_undefined: {
631       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
632       if (!RegNum)
633         return RegNum.takeError();
634       Row.getRegisterLocations().setRegisterLocation(
635           *RegNum, UnwindLocation::createUndefined());
636       break;
637     }
638 
639     case dwarf::DW_CFA_same_value: {
640       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
641       if (!RegNum)
642         return RegNum.takeError();
643       Row.getRegisterLocations().setRegisterLocation(
644           *RegNum, UnwindLocation::createSame());
645       break;
646     }
647 
648     case dwarf::DW_CFA_GNU_args_size:
649       break;
650 
651     case dwarf::DW_CFA_register: {
652       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
653       if (!RegNum)
654         return RegNum.takeError();
655       llvm::Expected<uint64_t> NewRegNum = Inst.getOperandAsUnsigned(CFIP, 1);
656       if (!NewRegNum)
657         return NewRegNum.takeError();
658       Row.getRegisterLocations().setRegisterLocation(
659           *RegNum, UnwindLocation::createIsRegisterPlusOffset(*NewRegNum, 0));
660       break;
661     }
662 
663     case dwarf::DW_CFA_val_offset:
664     case dwarf::DW_CFA_val_offset_sf: {
665       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
666       if (!RegNum)
667         return RegNum.takeError();
668       llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1);
669       if (!Offset)
670         return Offset.takeError();
671       Row.getRegisterLocations().setRegisterLocation(
672           *RegNum, UnwindLocation::createIsCFAPlusOffset(*Offset));
673       break;
674     }
675 
676     case dwarf::DW_CFA_expression: {
677       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
678       if (!RegNum)
679         return RegNum.takeError();
680       Row.getRegisterLocations().setRegisterLocation(
681           *RegNum, UnwindLocation::createAtDWARFExpression(*Inst.Expression));
682       break;
683     }
684 
685     case dwarf::DW_CFA_val_expression: {
686       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
687       if (!RegNum)
688         return RegNum.takeError();
689       Row.getRegisterLocations().setRegisterLocation(
690           *RegNum, UnwindLocation::createIsDWARFExpression(*Inst.Expression));
691       break;
692     }
693 
694     case dwarf::DW_CFA_def_cfa_register: {
695       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
696       if (!RegNum)
697         return RegNum.takeError();
698       if (Row.getCFAValue().getLocation() != UnwindLocation::RegPlusOffset)
699         Row.getCFAValue() =
700             UnwindLocation::createIsRegisterPlusOffset(*RegNum, 0);
701       else
702         Row.getCFAValue().setRegister(*RegNum);
703       break;
704     }
705 
706     case dwarf::DW_CFA_def_cfa_offset:
707     case dwarf::DW_CFA_def_cfa_offset_sf: {
708       llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 0);
709       if (!Offset)
710         return Offset.takeError();
711       if (Row.getCFAValue().getLocation() != UnwindLocation::RegPlusOffset) {
712         return createStringError(
713             errc::invalid_argument,
714             "%s found when CFA rule was not RegPlusOffset",
715             CFIP.callFrameString(Inst.Opcode).str().c_str());
716       }
717       Row.getCFAValue().setOffset(*Offset);
718       break;
719     }
720 
721     case dwarf::DW_CFA_def_cfa:
722     case dwarf::DW_CFA_def_cfa_sf: {
723       llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
724       if (!RegNum)
725         return RegNum.takeError();
726       llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1);
727       if (!Offset)
728         return Offset.takeError();
729       Row.getCFAValue() =
730           UnwindLocation::createIsRegisterPlusOffset(*RegNum, *Offset);
731       break;
732     }
733 
734     case dwarf::DW_CFA_def_cfa_expression:
735       Row.getCFAValue() =
736           UnwindLocation::createIsDWARFExpression(*Inst.Expression);
737       break;
738     }
739   }
740   return Error::success();
741 }
742 
743 ArrayRef<CFIProgram::OperandType[2]> CFIProgram::getOperandTypes() {
744   static OperandType OpTypes[DW_CFA_restore+1][2];
745   static bool Initialized = false;
746   if (Initialized) {
747     return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
748   }
749   Initialized = true;
750 
751 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1)       \
752   do {                                          \
753     OpTypes[OP][0] = OPTYPE0;                   \
754     OpTypes[OP][1] = OPTYPE1;                   \
755   } while (false)
756 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
757 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
758 
759   DECLARE_OP1(DW_CFA_set_loc, OT_Address);
760   DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
761   DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
762   DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
763   DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
764   DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
765   DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
766   DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
767   DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
768   DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
769   DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
770   DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
771   DECLARE_OP1(DW_CFA_undefined, OT_Register);
772   DECLARE_OP1(DW_CFA_same_value, OT_Register);
773   DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
774   DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
775   DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
776   DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
777   DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
778   DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
779   DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
780   DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
781   DECLARE_OP1(DW_CFA_restore, OT_Register);
782   DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
783   DECLARE_OP0(DW_CFA_remember_state);
784   DECLARE_OP0(DW_CFA_restore_state);
785   DECLARE_OP0(DW_CFA_GNU_window_save);
786   DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
787   DECLARE_OP0(DW_CFA_nop);
788 
789 #undef DECLARE_OP0
790 #undef DECLARE_OP1
791 #undef DECLARE_OP2
792 
793   return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
794 }
795 
796 /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
797 void CFIProgram::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
798                               const MCRegisterInfo *MRI, bool IsEH,
799                               const Instruction &Instr, unsigned OperandIdx,
800                               uint64_t Operand) const {
801   assert(OperandIdx < 2);
802   uint8_t Opcode = Instr.Opcode;
803   OperandType Type = getOperandTypes()[Opcode][OperandIdx];
804 
805   switch (Type) {
806   case OT_Unset: {
807     OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
808     auto OpcodeName = callFrameString(Opcode);
809     if (!OpcodeName.empty())
810       OS << " " << OpcodeName;
811     else
812       OS << format(" Opcode %x",  Opcode);
813     break;
814   }
815   case OT_None:
816     break;
817   case OT_Address:
818     OS << format(" %" PRIx64, Operand);
819     break;
820   case OT_Offset:
821     // The offsets are all encoded in a unsigned form, but in practice
822     // consumers use them signed. It's most certainly legacy due to
823     // the lack of signed variants in the first Dwarf standards.
824     OS << format(" %+" PRId64, int64_t(Operand));
825     break;
826   case OT_FactoredCodeOffset: // Always Unsigned
827     if (CodeAlignmentFactor)
828       OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
829     else
830       OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
831     break;
832   case OT_SignedFactDataOffset:
833     if (DataAlignmentFactor)
834       OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
835     else
836       OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
837     break;
838   case OT_UnsignedFactDataOffset:
839     if (DataAlignmentFactor)
840       OS << format(" %" PRId64, Operand * DataAlignmentFactor);
841     else
842       OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
843     break;
844   case OT_Register:
845     OS << ' ';
846     printRegister(OS, MRI, IsEH, Operand);
847     break;
848   case OT_Expression:
849     assert(Instr.Expression && "missing DWARFExpression object");
850     OS << " ";
851     Instr.Expression->print(OS, DumpOpts, MRI, nullptr, IsEH);
852     break;
853   }
854 }
855 
856 void CFIProgram::dump(raw_ostream &OS, DIDumpOptions DumpOpts,
857                       const MCRegisterInfo *MRI, bool IsEH,
858                       unsigned IndentLevel) const {
859   for (const auto &Instr : Instructions) {
860     uint8_t Opcode = Instr.Opcode;
861     OS.indent(2 * IndentLevel);
862     OS << callFrameString(Opcode) << ":";
863     for (unsigned i = 0; i < Instr.Ops.size(); ++i)
864       printOperand(OS, DumpOpts, MRI, IsEH, Instr, i, Instr.Ops[i]);
865     OS << '\n';
866   }
867 }
868 
869 // Returns the CIE identifier to be used by the requested format.
870 // CIE ids for .debug_frame sections are defined in Section 7.24 of DWARFv5.
871 // For CIE ID in .eh_frame sections see
872 // https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
873 constexpr uint64_t getCIEId(bool IsDWARF64, bool IsEH) {
874   if (IsEH)
875     return 0;
876   if (IsDWARF64)
877     return DW64_CIE_ID;
878   return DW_CIE_ID;
879 }
880 
881 void CIE::dump(raw_ostream &OS, DIDumpOptions DumpOpts,
882                const MCRegisterInfo *MRI, bool IsEH) const {
883   // A CIE with a zero length is a terminator entry in the .eh_frame section.
884   if (IsEH && Length == 0) {
885     OS << format("%08" PRIx64, Offset) << " ZERO terminator\n";
886     return;
887   }
888 
889   OS << format("%08" PRIx64, Offset)
890      << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length)
891      << format(" %0*" PRIx64, IsDWARF64 && !IsEH ? 16 : 8,
892                getCIEId(IsDWARF64, IsEH))
893      << " CIE\n"
894      << "  Format:                " << FormatString(IsDWARF64) << "\n";
895   if (IsEH && Version != 1)
896     OS << "WARNING: unsupported CIE version\n";
897   OS << format("  Version:               %d\n", Version)
898      << "  Augmentation:          \"" << Augmentation << "\"\n";
899   if (Version >= 4) {
900     OS << format("  Address size:          %u\n", (uint32_t)AddressSize);
901     OS << format("  Segment desc size:     %u\n",
902                  (uint32_t)SegmentDescriptorSize);
903   }
904   OS << format("  Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor);
905   OS << format("  Data alignment factor: %d\n", (int32_t)DataAlignmentFactor);
906   OS << format("  Return address column: %d\n", (int32_t)ReturnAddressRegister);
907   if (Personality)
908     OS << format("  Personality Address: %016" PRIx64 "\n", *Personality);
909   if (!AugmentationData.empty()) {
910     OS << "  Augmentation data:    ";
911     for (uint8_t Byte : AugmentationData)
912       OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
913     OS << "\n";
914   }
915   OS << "\n";
916   CFIs.dump(OS, DumpOpts, MRI, IsEH);
917   OS << "\n";
918 
919   if (Expected<UnwindTable> RowsOrErr = UnwindTable::create(this))
920     RowsOrErr->dump(OS, MRI, IsEH, 1);
921   else {
922     DumpOpts.RecoverableErrorHandler(joinErrors(
923         createStringError(errc::invalid_argument,
924                           "decoding the CIE opcodes into rows failed"),
925         RowsOrErr.takeError()));
926   }
927   OS << "\n";
928 }
929 
930 void FDE::dump(raw_ostream &OS, DIDumpOptions DumpOpts,
931                const MCRegisterInfo *MRI, bool IsEH) const {
932   OS << format("%08" PRIx64, Offset)
933      << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length)
934      << format(" %0*" PRIx64, IsDWARF64 && !IsEH ? 16 : 8, CIEPointer)
935      << " FDE cie=";
936   if (LinkedCIE)
937     OS << format("%08" PRIx64, LinkedCIE->getOffset());
938   else
939     OS << "<invalid offset>";
940   OS << format(" pc=%08" PRIx64 "...%08" PRIx64 "\n", InitialLocation,
941                InitialLocation + AddressRange);
942   OS << "  Format:       " << FormatString(IsDWARF64) << "\n";
943   if (LSDAAddress)
944     OS << format("  LSDA Address: %016" PRIx64 "\n", *LSDAAddress);
945   CFIs.dump(OS, DumpOpts, MRI, IsEH);
946   OS << "\n";
947 
948   if (Expected<UnwindTable> RowsOrErr = UnwindTable::create(this))
949     RowsOrErr->dump(OS, MRI, IsEH, 1);
950   else {
951     DumpOpts.RecoverableErrorHandler(joinErrors(
952         createStringError(errc::invalid_argument,
953                           "decoding the FDE opcodes into rows failed"),
954         RowsOrErr.takeError()));
955   }
956   OS << "\n";
957 }
958 
959 DWARFDebugFrame::DWARFDebugFrame(Triple::ArchType Arch,
960     bool IsEH, uint64_t EHFrameAddress)
961     : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
962 
963 DWARFDebugFrame::~DWARFDebugFrame() = default;
964 
965 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
966                                               uint64_t Offset, int Length) {
967   errs() << "DUMP: ";
968   for (int i = 0; i < Length; ++i) {
969     uint8_t c = Data.getU8(&Offset);
970     errs().write_hex(c); errs() << " ";
971   }
972   errs() << "\n";
973 }
974 
975 Error DWARFDebugFrame::parse(DWARFDataExtractor Data) {
976   uint64_t Offset = 0;
977   DenseMap<uint64_t, CIE *> CIEs;
978 
979   while (Data.isValidOffset(Offset)) {
980     uint64_t StartOffset = Offset;
981 
982     uint64_t Length;
983     DwarfFormat Format;
984     std::tie(Length, Format) = Data.getInitialLength(&Offset);
985     bool IsDWARF64 = Format == DWARF64;
986 
987     // If the Length is 0, then this CIE is a terminator. We add it because some
988     // dumper tools might need it to print something special for such entries
989     // (e.g. llvm-objdump --dwarf=frames prints "ZERO terminator").
990     if (Length == 0) {
991       auto Cie = std::make_unique<CIE>(
992           IsDWARF64, StartOffset, 0, 0, SmallString<8>(), 0, 0, 0, 0, 0,
993           SmallString<8>(), 0, 0, None, None, Arch);
994       CIEs[StartOffset] = Cie.get();
995       Entries.push_back(std::move(Cie));
996       break;
997     }
998 
999     // At this point, Offset points to the next field after Length.
1000     // Length is the structure size excluding itself. Compute an offset one
1001     // past the end of the structure (needed to know how many instructions to
1002     // read).
1003     uint64_t StartStructureOffset = Offset;
1004     uint64_t EndStructureOffset = Offset + Length;
1005 
1006     // The Id field's size depends on the DWARF format
1007     Error Err = Error::success();
1008     uint64_t Id = Data.getRelocatedValue((IsDWARF64 && !IsEH) ? 8 : 4, &Offset,
1009                                          /*SectionIndex=*/nullptr, &Err);
1010     if (Err)
1011       return Err;
1012 
1013     if (Id == getCIEId(IsDWARF64, IsEH)) {
1014       uint8_t Version = Data.getU8(&Offset);
1015       const char *Augmentation = Data.getCStr(&Offset);
1016       StringRef AugmentationString(Augmentation ? Augmentation : "");
1017       uint8_t AddressSize = Version < 4 ? Data.getAddressSize() :
1018                                           Data.getU8(&Offset);
1019       Data.setAddressSize(AddressSize);
1020       uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
1021       uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
1022       int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
1023       uint64_t ReturnAddressRegister =
1024           Version == 1 ? Data.getU8(&Offset) : Data.getULEB128(&Offset);
1025 
1026       // Parse the augmentation data for EH CIEs
1027       StringRef AugmentationData("");
1028       uint32_t FDEPointerEncoding = DW_EH_PE_absptr;
1029       uint32_t LSDAPointerEncoding = DW_EH_PE_omit;
1030       Optional<uint64_t> Personality;
1031       Optional<uint32_t> PersonalityEncoding;
1032       if (IsEH) {
1033         Optional<uint64_t> AugmentationLength;
1034         uint64_t StartAugmentationOffset;
1035         uint64_t EndAugmentationOffset;
1036 
1037         // Walk the augmentation string to get all the augmentation data.
1038         for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) {
1039           switch (AugmentationString[i]) {
1040           default:
1041             return createStringError(
1042                 errc::invalid_argument,
1043                 "unknown augmentation character in entry at 0x%" PRIx64,
1044                 StartOffset);
1045           case 'L':
1046             LSDAPointerEncoding = Data.getU8(&Offset);
1047             break;
1048           case 'P': {
1049             if (Personality)
1050               return createStringError(
1051                   errc::invalid_argument,
1052                   "duplicate personality in entry at 0x%" PRIx64, StartOffset);
1053             PersonalityEncoding = Data.getU8(&Offset);
1054             Personality = Data.getEncodedPointer(
1055                 &Offset, *PersonalityEncoding,
1056                 EHFrameAddress ? EHFrameAddress + Offset : 0);
1057             break;
1058           }
1059           case 'R':
1060             FDEPointerEncoding = Data.getU8(&Offset);
1061             break;
1062           case 'S':
1063             // Current frame is a signal trampoline.
1064             break;
1065           case 'z':
1066             if (i)
1067               return createStringError(
1068                   errc::invalid_argument,
1069                   "'z' must be the first character at 0x%" PRIx64, StartOffset);
1070             // Parse the augmentation length first.  We only parse it if
1071             // the string contains a 'z'.
1072             AugmentationLength = Data.getULEB128(&Offset);
1073             StartAugmentationOffset = Offset;
1074             EndAugmentationOffset = Offset + *AugmentationLength;
1075             break;
1076           case 'B':
1077             // B-Key is used for signing functions associated with this
1078             // augmentation string
1079             break;
1080           }
1081         }
1082 
1083         if (AugmentationLength.hasValue()) {
1084           if (Offset != EndAugmentationOffset)
1085             return createStringError(errc::invalid_argument,
1086                                      "parsing augmentation data at 0x%" PRIx64
1087                                      " failed",
1088                                      StartOffset);
1089           AugmentationData = Data.getData().slice(StartAugmentationOffset,
1090                                                   EndAugmentationOffset);
1091         }
1092       }
1093 
1094       auto Cie = std::make_unique<CIE>(
1095           IsDWARF64, StartOffset, Length, Version, AugmentationString,
1096           AddressSize, SegmentDescriptorSize, CodeAlignmentFactor,
1097           DataAlignmentFactor, ReturnAddressRegister, AugmentationData,
1098           FDEPointerEncoding, LSDAPointerEncoding, Personality,
1099           PersonalityEncoding, Arch);
1100       CIEs[StartOffset] = Cie.get();
1101       Entries.emplace_back(std::move(Cie));
1102     } else {
1103       // FDE
1104       uint64_t CIEPointer = Id;
1105       uint64_t InitialLocation = 0;
1106       uint64_t AddressRange = 0;
1107       Optional<uint64_t> LSDAAddress;
1108       CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
1109 
1110       if (IsEH) {
1111         // The address size is encoded in the CIE we reference.
1112         if (!Cie)
1113           return createStringError(errc::invalid_argument,
1114                                    "parsing FDE data at 0x%" PRIx64
1115                                    " failed due to missing CIE",
1116                                    StartOffset);
1117         if (auto Val =
1118                 Data.getEncodedPointer(&Offset, Cie->getFDEPointerEncoding(),
1119                                        EHFrameAddress + Offset)) {
1120           InitialLocation = *Val;
1121         }
1122         if (auto Val = Data.getEncodedPointer(
1123                 &Offset, Cie->getFDEPointerEncoding(), 0)) {
1124           AddressRange = *Val;
1125         }
1126 
1127         StringRef AugmentationString = Cie->getAugmentationString();
1128         if (!AugmentationString.empty()) {
1129           // Parse the augmentation length and data for this FDE.
1130           uint64_t AugmentationLength = Data.getULEB128(&Offset);
1131 
1132           uint64_t EndAugmentationOffset = Offset + AugmentationLength;
1133 
1134           // Decode the LSDA if the CIE augmentation string said we should.
1135           if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) {
1136             LSDAAddress = Data.getEncodedPointer(
1137                 &Offset, Cie->getLSDAPointerEncoding(),
1138                 EHFrameAddress ? Offset + EHFrameAddress : 0);
1139           }
1140 
1141           if (Offset != EndAugmentationOffset)
1142             return createStringError(errc::invalid_argument,
1143                                      "parsing augmentation data at 0x%" PRIx64
1144                                      " failed",
1145                                      StartOffset);
1146         }
1147       } else {
1148         InitialLocation = Data.getRelocatedAddress(&Offset);
1149         AddressRange = Data.getRelocatedAddress(&Offset);
1150       }
1151 
1152       Entries.emplace_back(new FDE(IsDWARF64, StartOffset, Length, CIEPointer,
1153                                    InitialLocation, AddressRange, Cie,
1154                                    LSDAAddress, Arch));
1155     }
1156 
1157     if (Error E =
1158             Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset))
1159       return E;
1160 
1161     if (Offset != EndStructureOffset)
1162       return createStringError(
1163           errc::invalid_argument,
1164           "parsing entry instructions at 0x%" PRIx64 " failed", StartOffset);
1165   }
1166 
1167   return Error::success();
1168 }
1169 
1170 FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
1171   auto It = partition_point(Entries, [=](const std::unique_ptr<FrameEntry> &E) {
1172     return E->getOffset() < Offset;
1173   });
1174   if (It != Entries.end() && (*It)->getOffset() == Offset)
1175     return It->get();
1176   return nullptr;
1177 }
1178 
1179 void DWARFDebugFrame::dump(raw_ostream &OS, DIDumpOptions DumpOpts,
1180                            const MCRegisterInfo *MRI,
1181                            Optional<uint64_t> Offset) const {
1182   if (Offset) {
1183     if (auto *Entry = getEntryAtOffset(*Offset))
1184       Entry->dump(OS, DumpOpts, MRI, IsEH);
1185     return;
1186   }
1187 
1188   OS << "\n";
1189   for (const auto &Entry : Entries)
1190     Entry->dump(OS, DumpOpts, MRI, IsEH);
1191 }
1192