LLVM 20.0.0git
DWARFDebugFrame.cpp
Go to the documentation of this file.
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
10#include "llvm/ADT/DenseMap.h"
12#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Errc.h"
20#include "llvm/Support/Format.h"
22#include <cassert>
23#include <cinttypes>
24#include <cstdint>
25#include <optional>
26
27using namespace llvm;
28using namespace dwarf;
29
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
43
45
47
49 return {Constant, InvalidRegisterNumber, Value, std::nullopt, false};
50}
51
53 return {CFAPlusOffset, InvalidRegisterNumber, Offset, std::nullopt, false};
54}
55
57 return {CFAPlusOffset, InvalidRegisterNumber, Offset, std::nullopt, true};
58}
59
62 std::optional<uint32_t> AddrSpace) {
63 return {RegPlusOffset, RegNum, Offset, AddrSpace, false};
64}
65
68 std::optional<uint32_t> AddrSpace) {
69 return {RegPlusOffset, RegNum, Offset, AddrSpace, true};
70}
71
73 return {Expr, false};
74}
75
77 return {Expr, true};
78}
79
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
124 const UnwindLocation &UL) {
125 auto DumpOpts = DIDumpOptions();
126 UL.dump(OS, DumpOpts);
127 return OS;
128}
129
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
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
165 const RegisterLocations &RL) {
166 auto DumpOpts = DIDumpOptions();
167 RL.dump(OS, DumpOpts);
168 return OS;
169}
170
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
186 auto DumpOpts = DIDumpOptions();
187 Row.dump(OS, DumpOpts, 0);
188 return OS;
189}
190
192 unsigned IndentLevel) const {
193 for (const UnwindRow &Row : Rows)
194 Row.dump(OS, DumpOpts, IndentLevel);
195}
196
198 auto DumpOpts = DIDumpOptions();
199 Rows.dump(OS, DumpOpts, 0);
200 return OS;
201}
202
204 const CIE *Cie = Fde->getLinkedCIE();
205 if (Cie == nullptr)
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
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
253
255 uint64_t EndOffset) {
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.
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:
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
394 return dwarf::CallFrameString(Opcode, Arch);
395}
396
397const 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
418 uint32_t OperandIdx) const {
419 if (OperandIdx >= MaxOperands)
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:
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(
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(
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
463 uint32_t OperandIdx) const {
464 if (OperandIdx >= MaxOperands)
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:
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(
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)
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)
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
515Error 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(
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(
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())
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:
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(
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(
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(
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(
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(
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() =
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(
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() =
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();
808 *RegNum, *Offset, *CFAAddrSpace);
809 break;
810 }
811
812 case dwarf::DW_CFA_def_cfa_expression:
813 Row.getCFAValue() =
815 break;
816 }
817 }
818 return Error::success();
819}
820
822CFIProgram::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.
884void 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
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
968constexpr 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
976void 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 {
1018 "decoding the CIE opcodes into rows failed"),
1019 RowsOrErr.takeError()));
1020 }
1021 OS << "\n";
1022}
1023
1024void 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 {
1046 "decoding the FDE opcodes into rows failed"),
1047 RowsOrErr.takeError()));
1048 }
1049 OS << "\n";
1050}
1051
1053 bool IsEH, uint64_t EHFrameAddress)
1054 : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
1055
1057
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
1069 uint64_t Offset = 0;
1071
1072 while (Data.isValidOffset(Offset)) {
1073 uint64_t StartOffset = Offset;
1074
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(
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(
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(
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)
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;
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)
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(
1235 EHFrameAddress ? Offset + EHFrameAddress : 0);
1236 }
1237
1238 if (Offset != EndAugmentationOffset)
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(
1261 "parsing entry instructions at 0x%" PRIx64 " failed", StartOffset);
1262 }
1263
1264 return Error::success();
1265}
1266
1267FrameEntry *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
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}
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:282
const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK
static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, uint64_t Offset, int Length)
static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned RegNum)
const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK
#define DECLARE_OP3(OP, OPTYPE0, OPTYPE1, OPTYPE2)
#define DECLARE_OP2(OP, OPTYPE0, OPTYPE1)
#define DECLARE_OP0(OP)
#define ENUM_TO_CSTR(e)
#define DECLARE_OP1(OP, OPTYPE0)
constexpr uint64_t getCIEId(bool IsDWARF64, bool IsEH)
This file defines the DenseMap class.
This file contains constants used for implementing Dwarf debug support.
#define RegName(no)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
Value * RHS
A class that represents an address range.
Definition: AddressRanges.h:22
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
DWARFDebugFrame(Triple::ArchType Arch, bool IsEH=false, uint64_t EHFrameAddress=0)
void dump(raw_ostream &OS, DIDumpOptions DumpOpts, std::optional< uint64_t > Offset) const
Dump the section data into the given stream.
Error parse(DWARFDataExtractor Data)
Parse the section from raw data.
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
Class representing an expression and its matching format.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
@ aarch64_be
Definition: Triple.h:52
@ aarch64_32
Definition: Triple.h:53
static StringRef getArchTypeName(ArchType Kind)
Get the canonical name for the Kind architecture.
Definition: Triple.cpp:24
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
Represent a sequence of Call Frame Information instructions that, when read in order,...
void dump(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel, std::optional< uint64_t > InitialLocation) const
uint64_t codeAlign() const
Error parse(DWARFDataExtractor Data, uint64_t *Offset, uint64_t EndOffset)
Parse and store a sequence of CFI instructions from Data, starting at *Offset and ending at EndOffset...
static constexpr size_t MaxOperands
void addInstruction(const Instruction &I)
int64_t dataAlign() const
StringRef callFrameString(unsigned Opcode) const
Get a DWARF CFI call frame string for the given DW_CFA opcode.
DWARF Common Information Entry (CIE)
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const override
Dump the instructions in this CFI fragment.
uint32_t getLSDAPointerEncoding() const
uint32_t getFDEPointerEncoding() const
StringRef getAugmentationString() const
DWARF Frame Description Entry (FDE)
uint64_t getAddressRange() const
uint64_t getInitialLocation() const
const CIE * getLinkedCIE() const
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const override
Dump the instructions in this CFI fragment.
An entry in either debug_frame or eh_frame.
const CFIProgram & cfis() const
uint64_t getOffset() const
A class that can track all registers with locations in a UnwindRow object.
std::optional< UnwindLocation > getRegisterLocation(uint32_t RegNum) const
Return the location for the register in RegNum if there is a location.
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const
Dump all registers + locations that are currently defined in this object.
bool hasLocations() const
Returns true if we have any register locations in this object.
A class that represents a location for the Call Frame Address (CFA) or a register.
static UnwindLocation createUndefined()
Create a location where the value is undefined and not available.
static UnwindLocation createAtRegisterPlusOffset(uint32_t Reg, int32_t Off, std::optional< uint32_t > AddrSpace=std::nullopt)
static UnwindLocation createIsRegisterPlusOffset(uint32_t Reg, int32_t Off, std::optional< uint32_t > AddrSpace=std::nullopt)
Create a location where the saved value is in (Deref == false) or at (Deref == true) a regiser plus a...
static UnwindLocation createAtDWARFExpression(DWARFExpression Expr)
static UnwindLocation createUnspecified()
Create a location whose rule is set to Unspecified.
bool operator==(const UnwindLocation &RHS) const
static UnwindLocation createIsDWARFExpression(DWARFExpression Expr)
Create a location whose value is the result of evaluating a DWARF expression.
@ Undefined
Register is not available and can't be recovered.
@ Constant
Value is a constant value contained in "Offset": reg = Offset.
@ DWARFExpr
Register or CFA value is in or at a value found by evaluating a DWARF expression: reg = eval(dwarf_ex...
@ Same
Register value is in the register, nothing needs to be done to unwind it: reg = reg.
@ CFAPlusOffset
Register is in or at the CFA plus an offset: reg = CFA + offset reg = defef(CFA + offset)
@ RegPlusOffset
Register or CFA is in or at a register plus offset, optionally in an address space: reg = reg + offse...
static UnwindLocation createIsConstant(int32_t Value)
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const
Dump a location expression as text and use the register information if some is provided.
static UnwindLocation createAtCFAPlusOffset(int32_t Off)
static UnwindLocation createSame()
Create a location where the value is known to be in the register itself.
static UnwindLocation createIsCFAPlusOffset(int32_t Off)
Create a location that is in (Deref == false) or at (Deref == true) the CFA plus an offset.
A class that represents a single row in the unwind table that is decoded by parsing the DWARF Call Fr...
void dump(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel=0) const
Dump the UnwindRow to the stream.
bool hasAddress() const
Returns true if the address is valid in this object.
A class that contains all UnwindRow objects for an FDE or a single unwind row for a CIE.
static Expected< UnwindTable > create(const CIE *Cie)
Create an UnwindTable from a Common Information Entry (CIE).
void dump(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel=0) const
Dump the UnwindTable to the stream.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch)
Definition: Dwarf.cpp:613
StringRef FormatString(DwarfFormat Format)
Definition: Dwarf.cpp:868
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const uint32_t DW_CIE_ID
Special ID values that distinguish a CIE from a FDE in DWARF CFI.
Definition: Dwarf.h:96
const uint64_t DW64_CIE_ID
Definition: Dwarf.h:97
constexpr uint32_t InvalidRegisterNumber
raw_ostream & operator<<(raw_ostream &OS, const UnwindLocation &R)
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:91
@ DWARF64
Definition: Dwarf.h:91
@ DW_EH_PE_absptr
Definition: Dwarf.h:847
@ DW_EH_PE_omit
Definition: Dwarf.h:848
NodeAddr< InstrNode * > Instr
Definition: RDFGraph.h:389
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
@ Length
Definition: DWP.cpp:480
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:2050
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1291
@ illegal_byte_sequence
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:438
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:196
std::function< void(Error)> RecoverableErrorHandler
Definition: DIContext.h:234
std::function< llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)> GetNameForDWARFReg
Definition: DIContext.h:214
An instruction consists of a DWARF CFI opcode and an optional sequence of operands.
Expected< uint64_t > getOperandAsUnsigned(const CFIProgram &CFIP, uint32_t OperandIdx) const
Expected< int64_t > getOperandAsSigned(const CFIProgram &CFIP, uint32_t OperandIdx) const