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