LLVM 19.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 <algorithm>
23#include <cassert>
24#include <cinttypes>
25#include <cstdint>
26#include <optional>
27
28using namespace llvm;
29using namespace dwarf;
30
32 unsigned RegNum) {
33 if (DumpOpts.GetNameForDWARFReg) {
34 auto RegName = DumpOpts.GetNameForDWARFReg(RegNum, DumpOpts.IsEH);
35 if (!RegName.empty()) {
36 OS << RegName;
37 return;
38 }
39 }
40 OS << "reg" << RegNum;
41}
42
44
46
48
50 return {Constant, InvalidRegisterNumber, Value, std::nullopt, false};
51}
52
54 return {CFAPlusOffset, InvalidRegisterNumber, Offset, std::nullopt, false};
55}
56
58 return {CFAPlusOffset, InvalidRegisterNumber, Offset, std::nullopt, true};
59}
60
63 std::optional<uint32_t> AddrSpace) {
64 return {RegPlusOffset, RegNum, Offset, AddrSpace, false};
65}
66
69 std::optional<uint32_t> AddrSpace) {
70 return {RegPlusOffset, RegNum, Offset, AddrSpace, true};
71}
72
74 return {Expr, false};
75}
76
78 return {Expr, true};
79}
80
82 if (Dereference)
83 OS << '[';
84 switch (Kind) {
85 case Unspecified:
86 OS << "unspecified";
87 break;
88 case Undefined:
89 OS << "undefined";
90 break;
91 case Same:
92 OS << "same";
93 break;
94 case CFAPlusOffset:
95 OS << "CFA";
96 if (Offset == 0)
97 break;
98 if (Offset > 0)
99 OS << "+";
100 OS << Offset;
101 break;
102 case RegPlusOffset:
103 printRegister(OS, DumpOpts, RegNum);
104 if (Offset == 0 && !AddrSpace)
105 break;
106 if (Offset >= 0)
107 OS << "+";
108 OS << Offset;
109 if (AddrSpace)
110 OS << " in addrspace" << *AddrSpace;
111 break;
112 case DWARFExpr: {
113 Expr->print(OS, DumpOpts, nullptr);
114 break;
115 }
116 case Constant:
117 OS << Offset;
118 break;
119 }
120 if (Dereference)
121 OS << ']';
122}
123
125 const UnwindLocation &UL) {
126 auto DumpOpts = DIDumpOptions();
127 UL.dump(OS, DumpOpts);
128 return OS;
129}
130
132 if (Kind != RHS.Kind)
133 return false;
134 switch (Kind) {
135 case Unspecified:
136 case Undefined:
137 case Same:
138 return true;
139 case CFAPlusOffset:
140 return Offset == RHS.Offset && Dereference == RHS.Dereference;
141 case RegPlusOffset:
142 return RegNum == RHS.RegNum && Offset == RHS.Offset &&
143 Dereference == RHS.Dereference;
144 case DWARFExpr:
145 return *Expr == *RHS.Expr && Dereference == RHS.Dereference;
146 case Constant:
147 return Offset == RHS.Offset;
148 }
149 return false;
150}
151
153 bool First = true;
154 for (const auto &RegLocPair : Locations) {
155 if (First)
156 First = false;
157 else
158 OS << ", ";
159 printRegister(OS, DumpOpts, RegLocPair.first);
160 OS << '=';
161 RegLocPair.second.dump(OS, DumpOpts);
162 }
163}
164
166 const RegisterLocations &RL) {
167 auto DumpOpts = DIDumpOptions();
168 RL.dump(OS, DumpOpts);
169 return OS;
170}
171
173 unsigned IndentLevel) const {
174 OS.indent(2 * IndentLevel);
175 if (hasAddress())
176 OS << format("0x%" PRIx64 ": ", *Address);
177 OS << "CFA=";
178 CFAValue.dump(OS, DumpOpts);
179 if (RegLocs.hasLocations()) {
180 OS << ": ";
181 RegLocs.dump(OS, DumpOpts);
182 }
183 OS << "\n";
184}
185
187 auto DumpOpts = DIDumpOptions();
188 Row.dump(OS, DumpOpts, 0);
189 return OS;
190}
191
193 unsigned IndentLevel) const {
194 for (const UnwindRow &Row : Rows)
195 Row.dump(OS, DumpOpts, IndentLevel);
196}
197
199 auto DumpOpts = DIDumpOptions();
200 Rows.dump(OS, DumpOpts, 0);
201 return OS;
202}
203
205 const CIE *Cie = Fde->getLinkedCIE();
206 if (Cie == nullptr)
208 "unable to get CIE for FDE at offset 0x%" PRIx64,
209 Fde->getOffset());
210
211 // Rows will be empty if there are no CFI instructions.
212 if (Cie->cfis().empty() && Fde->cfis().empty())
213 return UnwindTable();
214
215 UnwindTable UT;
216 UnwindRow Row;
217 Row.setAddress(Fde->getInitialLocation());
218 UT.EndAddress = Fde->getInitialLocation() + Fde->getAddressRange();
219 if (Error CieError = UT.parseRows(Cie->cfis(), Row, nullptr))
220 return std::move(CieError);
221 // We need to save the initial locations of registers from the CIE parsing
222 // in case we run into DW_CFA_restore or DW_CFA_restore_extended opcodes.
223 const RegisterLocations InitialLocs = Row.getRegisterLocations();
224 if (Error FdeError = UT.parseRows(Fde->cfis(), Row, &InitialLocs))
225 return std::move(FdeError);
226 // May be all the CFI instructions were DW_CFA_nop amd Row becomes empty.
227 // Do not add that to the unwind table.
228 if (Row.getRegisterLocations().hasLocations() ||
229 Row.getCFAValue().getLocation() != UnwindLocation::Unspecified)
230 UT.Rows.push_back(Row);
231 return UT;
232}
233
235 // Rows will be empty if there are no CFI instructions.
236 if (Cie->cfis().empty())
237 return UnwindTable();
238
239 UnwindTable UT;
240 UnwindRow Row;
241 if (Error CieError = UT.parseRows(Cie->cfis(), Row, nullptr))
242 return std::move(CieError);
243 // May be all the CFI instructions were DW_CFA_nop amd Row becomes empty.
244 // Do not add that to the unwind table.
245 if (Row.getRegisterLocations().hasLocations() ||
246 Row.getCFAValue().getLocation() != UnwindLocation::Unspecified)
247 UT.Rows.push_back(Row);
248 return UT;
249}
250
251// See DWARF standard v3, section 7.23
252const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
254
256 uint64_t EndOffset) {
258 while (C && C.tell() < EndOffset) {
259 uint8_t Opcode = Data.getRelocatedValue(C, 1);
260 if (!C)
261 break;
262
263 // Some instructions have a primary opcode encoded in the top bits.
264 if (uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK) {
265 // If it's a primary opcode, the first operand is encoded in the bottom
266 // bits of the opcode itself.
268 switch (Primary) {
269 case DW_CFA_advance_loc:
270 case DW_CFA_restore:
271 addInstruction(Primary, Op1);
272 break;
273 case DW_CFA_offset:
274 addInstruction(Primary, Op1, Data.getULEB128(C));
275 break;
276 default:
277 llvm_unreachable("invalid primary CFI opcode");
278 }
279 continue;
280 }
281
282 // Extended opcode - its value is Opcode itself.
283 switch (Opcode) {
284 default:
286 "invalid extended CFI opcode 0x%" PRIx8, Opcode);
287 case DW_CFA_nop:
288 case DW_CFA_remember_state:
289 case DW_CFA_restore_state:
290 case DW_CFA_GNU_window_save:
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_undefined: {
670 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
671 if (!RegNum)
672 return RegNum.takeError();
673 Row.getRegisterLocations().setRegisterLocation(
675 break;
676 }
677
678 case dwarf::DW_CFA_same_value: {
679 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
680 if (!RegNum)
681 return RegNum.takeError();
682 Row.getRegisterLocations().setRegisterLocation(
683 *RegNum, UnwindLocation::createSame());
684 break;
685 }
686
687 case dwarf::DW_CFA_GNU_args_size:
688 break;
689
690 case dwarf::DW_CFA_register: {
691 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
692 if (!RegNum)
693 return RegNum.takeError();
694 llvm::Expected<uint64_t> NewRegNum = Inst.getOperandAsUnsigned(CFIP, 1);
695 if (!NewRegNum)
696 return NewRegNum.takeError();
697 Row.getRegisterLocations().setRegisterLocation(
698 *RegNum, UnwindLocation::createIsRegisterPlusOffset(*NewRegNum, 0));
699 break;
700 }
701
702 case dwarf::DW_CFA_val_offset:
703 case dwarf::DW_CFA_val_offset_sf: {
704 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
705 if (!RegNum)
706 return RegNum.takeError();
707 llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1);
708 if (!Offset)
709 return Offset.takeError();
710 Row.getRegisterLocations().setRegisterLocation(
712 break;
713 }
714
715 case dwarf::DW_CFA_expression: {
716 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
717 if (!RegNum)
718 return RegNum.takeError();
719 Row.getRegisterLocations().setRegisterLocation(
720 *RegNum, UnwindLocation::createAtDWARFExpression(*Inst.Expression));
721 break;
722 }
723
724 case dwarf::DW_CFA_val_expression: {
725 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
726 if (!RegNum)
727 return RegNum.takeError();
728 Row.getRegisterLocations().setRegisterLocation(
729 *RegNum, UnwindLocation::createIsDWARFExpression(*Inst.Expression));
730 break;
731 }
732
733 case dwarf::DW_CFA_def_cfa_register: {
734 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
735 if (!RegNum)
736 return RegNum.takeError();
737 if (Row.getCFAValue().getLocation() != UnwindLocation::RegPlusOffset)
738 Row.getCFAValue() =
740 else
741 Row.getCFAValue().setRegister(*RegNum);
742 break;
743 }
744
745 case dwarf::DW_CFA_def_cfa_offset:
746 case dwarf::DW_CFA_def_cfa_offset_sf: {
747 llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 0);
748 if (!Offset)
749 return Offset.takeError();
750 if (Row.getCFAValue().getLocation() != UnwindLocation::RegPlusOffset) {
751 return createStringError(
753 "%s found when CFA rule was not RegPlusOffset",
754 CFIP.callFrameString(Inst.Opcode).str().c_str());
755 }
756 Row.getCFAValue().setOffset(*Offset);
757 break;
758 }
759
760 case dwarf::DW_CFA_def_cfa:
761 case dwarf::DW_CFA_def_cfa_sf: {
762 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
763 if (!RegNum)
764 return RegNum.takeError();
765 llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1);
766 if (!Offset)
767 return Offset.takeError();
768 Row.getCFAValue() =
770 break;
771 }
772
773 case dwarf::DW_CFA_LLVM_def_aspace_cfa:
774 case dwarf::DW_CFA_LLVM_def_aspace_cfa_sf: {
775 llvm::Expected<uint64_t> RegNum = Inst.getOperandAsUnsigned(CFIP, 0);
776 if (!RegNum)
777 return RegNum.takeError();
778 llvm::Expected<int64_t> Offset = Inst.getOperandAsSigned(CFIP, 1);
779 if (!Offset)
780 return Offset.takeError();
781 llvm::Expected<uint32_t> CFAAddrSpace =
782 Inst.getOperandAsUnsigned(CFIP, 2);
783 if (!CFAAddrSpace)
784 return CFAAddrSpace.takeError();
786 *RegNum, *Offset, *CFAAddrSpace);
787 break;
788 }
789
790 case dwarf::DW_CFA_def_cfa_expression:
791 Row.getCFAValue() =
793 break;
794 }
795 }
796 return Error::success();
797}
798
800CFIProgram::getOperandTypes() {
801 static OperandType OpTypes[DW_CFA_restore + 1][MaxOperands];
802 static bool Initialized = false;
803 if (Initialized) {
804 return ArrayRef<OperandType[MaxOperands]>(&OpTypes[0], DW_CFA_restore + 1);
805 }
806 Initialized = true;
807
808#define DECLARE_OP3(OP, OPTYPE0, OPTYPE1, OPTYPE2) \
809 do { \
810 OpTypes[OP][0] = OPTYPE0; \
811 OpTypes[OP][1] = OPTYPE1; \
812 OpTypes[OP][2] = OPTYPE2; \
813 } while (false)
814#define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
815 DECLARE_OP3(OP, OPTYPE0, OPTYPE1, OT_None)
816#define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
817#define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
818
819 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
820 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
821 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
822 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
823 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
824 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
825 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
826 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
827 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
828 DECLARE_OP3(DW_CFA_LLVM_def_aspace_cfa, OT_Register, OT_Offset,
829 OT_AddressSpace);
830 DECLARE_OP3(DW_CFA_LLVM_def_aspace_cfa_sf, OT_Register,
831 OT_SignedFactDataOffset, OT_AddressSpace);
832 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
833 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
834 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
835 DECLARE_OP1(DW_CFA_undefined, OT_Register);
836 DECLARE_OP1(DW_CFA_same_value, OT_Register);
837 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
838 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
839 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
840 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
841 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
842 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
843 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
844 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
845 DECLARE_OP1(DW_CFA_restore, OT_Register);
846 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
847 DECLARE_OP0(DW_CFA_remember_state);
848 DECLARE_OP0(DW_CFA_restore_state);
849 DECLARE_OP0(DW_CFA_GNU_window_save);
850 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
851 DECLARE_OP0(DW_CFA_nop);
852
853#undef DECLARE_OP0
854#undef DECLARE_OP1
855#undef DECLARE_OP2
856
857 return ArrayRef<OperandType[MaxOperands]>(&OpTypes[0], DW_CFA_restore + 1);
858}
859
860/// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
861void CFIProgram::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
862 const Instruction &Instr, unsigned OperandIdx,
863 uint64_t Operand,
864 std::optional<uint64_t> &Address) const {
865 assert(OperandIdx < MaxOperands);
866 uint8_t Opcode = Instr.Opcode;
867 OperandType Type = getOperandTypes()[Opcode][OperandIdx];
868
869 switch (Type) {
870 case OT_Unset: {
871 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
872 auto OpcodeName = callFrameString(Opcode);
873 if (!OpcodeName.empty())
874 OS << " " << OpcodeName;
875 else
876 OS << format(" Opcode %x", Opcode);
877 break;
878 }
879 case OT_None:
880 break;
881 case OT_Address:
882 OS << format(" %" PRIx64, Operand);
883 Address = Operand;
884 break;
885 case OT_Offset:
886 // The offsets are all encoded in a unsigned form, but in practice
887 // consumers use them signed. It's most certainly legacy due to
888 // the lack of signed variants in the first Dwarf standards.
889 OS << format(" %+" PRId64, int64_t(Operand));
890 break;
891 case OT_FactoredCodeOffset: // Always Unsigned
892 if (CodeAlignmentFactor)
893 OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
894 else
895 OS << format(" %" PRId64 "*code_alignment_factor", Operand);
896 if (Address && CodeAlignmentFactor) {
897 *Address += Operand * CodeAlignmentFactor;
898 OS << format(" to 0x%" PRIx64, *Address);
899 }
900 break;
901 case OT_SignedFactDataOffset:
902 if (DataAlignmentFactor)
903 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
904 else
905 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
906 break;
907 case OT_UnsignedFactDataOffset:
908 if (DataAlignmentFactor)
909 OS << format(" %" PRId64, Operand * DataAlignmentFactor);
910 else
911 OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
912 break;
913 case OT_Register:
914 OS << ' ';
915 printRegister(OS, DumpOpts, Operand);
916 break;
917 case OT_AddressSpace:
918 OS << format(" in addrspace%" PRId64, Operand);
919 break;
920 case OT_Expression:
921 assert(Instr.Expression && "missing DWARFExpression object");
922 OS << " ";
923 Instr.Expression->print(OS, DumpOpts, nullptr);
924 break;
925 }
926}
927
929 unsigned IndentLevel,
930 std::optional<uint64_t> Address) const {
931 for (const auto &Instr : Instructions) {
932 uint8_t Opcode = Instr.Opcode;
933 OS.indent(2 * IndentLevel);
934 OS << callFrameString(Opcode) << ":";
935 for (unsigned i = 0; i < Instr.Ops.size(); ++i)
936 printOperand(OS, DumpOpts, Instr, i, Instr.Ops[i], Address);
937 OS << '\n';
938 }
939}
940
941// Returns the CIE identifier to be used by the requested format.
942// CIE ids for .debug_frame sections are defined in Section 7.24 of DWARFv5.
943// For CIE ID in .eh_frame sections see
944// https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
945constexpr uint64_t getCIEId(bool IsDWARF64, bool IsEH) {
946 if (IsEH)
947 return 0;
948 if (IsDWARF64)
949 return DW64_CIE_ID;
950 return DW_CIE_ID;
951}
952
953void CIE::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
954 // A CIE with a zero length is a terminator entry in the .eh_frame section.
955 if (DumpOpts.IsEH && Length == 0) {
956 OS << format("%08" PRIx64, Offset) << " ZERO terminator\n";
957 return;
958 }
959
960 OS << format("%08" PRIx64, Offset)
961 << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length)
962 << format(" %0*" PRIx64, IsDWARF64 && !DumpOpts.IsEH ? 16 : 8,
963 getCIEId(IsDWARF64, DumpOpts.IsEH))
964 << " CIE\n"
965 << " Format: " << FormatString(IsDWARF64) << "\n";
966 if (DumpOpts.IsEH && Version != 1)
967 OS << "WARNING: unsupported CIE version\n";
968 OS << format(" Version: %d\n", Version)
969 << " Augmentation: \"" << Augmentation << "\"\n";
970 if (Version >= 4) {
971 OS << format(" Address size: %u\n", (uint32_t)AddressSize);
972 OS << format(" Segment desc size: %u\n",
973 (uint32_t)SegmentDescriptorSize);
974 }
975 OS << format(" Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor);
976 OS << format(" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor);
977 OS << format(" Return address column: %d\n", (int32_t)ReturnAddressRegister);
978 if (Personality)
979 OS << format(" Personality Address: %016" PRIx64 "\n", *Personality);
980 if (!AugmentationData.empty()) {
981 OS << " Augmentation data: ";
982 for (uint8_t Byte : AugmentationData)
983 OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
984 OS << "\n";
985 }
986 OS << "\n";
987 CFIs.dump(OS, DumpOpts, /*IndentLevel=*/1, /*InitialLocation=*/{});
988 OS << "\n";
989
990 if (Expected<UnwindTable> RowsOrErr = UnwindTable::create(this))
991 RowsOrErr->dump(OS, DumpOpts, 1);
992 else {
995 "decoding the CIE opcodes into rows failed"),
996 RowsOrErr.takeError()));
997 }
998 OS << "\n";
999}
1000
1001void FDE::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
1002 OS << format("%08" PRIx64, Offset)
1003 << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length)
1004 << format(" %0*" PRIx64, IsDWARF64 && !DumpOpts.IsEH ? 16 : 8, CIEPointer)
1005 << " FDE cie=";
1006 if (LinkedCIE)
1007 OS << format("%08" PRIx64, LinkedCIE->getOffset());
1008 else
1009 OS << "<invalid offset>";
1010 OS << format(" pc=%08" PRIx64 "...%08" PRIx64 "\n", InitialLocation,
1011 InitialLocation + AddressRange);
1012 OS << " Format: " << FormatString(IsDWARF64) << "\n";
1013 if (LSDAAddress)
1014 OS << format(" LSDA Address: %016" PRIx64 "\n", *LSDAAddress);
1015 CFIs.dump(OS, DumpOpts, /*IndentLevel=*/1, InitialLocation);
1016 OS << "\n";
1017
1018 if (Expected<UnwindTable> RowsOrErr = UnwindTable::create(this))
1019 RowsOrErr->dump(OS, DumpOpts, 1);
1020 else {
1023 "decoding the FDE opcodes into rows failed"),
1024 RowsOrErr.takeError()));
1025 }
1026 OS << "\n";
1027}
1028
1030 bool IsEH, uint64_t EHFrameAddress)
1031 : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
1032
1034
1036 uint64_t Offset, int Length) {
1037 errs() << "DUMP: ";
1038 for (int i = 0; i < Length; ++i) {
1039 uint8_t c = Data.getU8(&Offset);
1040 errs().write_hex(c); errs() << " ";
1041 }
1042 errs() << "\n";
1043}
1044
1046 uint64_t Offset = 0;
1048
1049 while (Data.isValidOffset(Offset)) {
1050 uint64_t StartOffset = Offset;
1051
1054 std::tie(Length, Format) = Data.getInitialLength(&Offset);
1055 bool IsDWARF64 = Format == DWARF64;
1056
1057 // If the Length is 0, then this CIE is a terminator. We add it because some
1058 // dumper tools might need it to print something special for such entries
1059 // (e.g. llvm-objdump --dwarf=frames prints "ZERO terminator").
1060 if (Length == 0) {
1061 auto Cie = std::make_unique<CIE>(
1062 IsDWARF64, StartOffset, 0, 0, SmallString<8>(), 0, 0, 0, 0, 0,
1063 SmallString<8>(), 0, 0, std::nullopt, std::nullopt, Arch);
1064 CIEs[StartOffset] = Cie.get();
1065 Entries.push_back(std::move(Cie));
1066 break;
1067 }
1068
1069 // At this point, Offset points to the next field after Length.
1070 // Length is the structure size excluding itself. Compute an offset one
1071 // past the end of the structure (needed to know how many instructions to
1072 // read).
1073 uint64_t StartStructureOffset = Offset;
1074 uint64_t EndStructureOffset = Offset + Length;
1075
1076 // The Id field's size depends on the DWARF format
1077 Error Err = Error::success();
1078 uint64_t Id = Data.getRelocatedValue((IsDWARF64 && !IsEH) ? 8 : 4, &Offset,
1079 /*SectionIndex=*/nullptr, &Err);
1080 if (Err)
1081 return Err;
1082
1083 if (Id == getCIEId(IsDWARF64, IsEH)) {
1084 uint8_t Version = Data.getU8(&Offset);
1085 const char *Augmentation = Data.getCStr(&Offset);
1086 StringRef AugmentationString(Augmentation ? Augmentation : "");
1087 uint8_t AddressSize = Version < 4 ? Data.getAddressSize() :
1088 Data.getU8(&Offset);
1089 Data.setAddressSize(AddressSize);
1090 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
1091 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
1092 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
1093 uint64_t ReturnAddressRegister =
1094 Version == 1 ? Data.getU8(&Offset) : Data.getULEB128(&Offset);
1095
1096 // Parse the augmentation data for EH CIEs
1097 StringRef AugmentationData("");
1098 uint32_t FDEPointerEncoding = DW_EH_PE_absptr;
1099 uint32_t LSDAPointerEncoding = DW_EH_PE_omit;
1100 std::optional<uint64_t> Personality;
1101 std::optional<uint32_t> PersonalityEncoding;
1102 if (IsEH) {
1103 std::optional<uint64_t> AugmentationLength;
1104 uint64_t StartAugmentationOffset;
1105 uint64_t EndAugmentationOffset;
1106
1107 // Walk the augmentation string to get all the augmentation data.
1108 for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) {
1109 switch (AugmentationString[i]) {
1110 default:
1111 return createStringError(
1113 "unknown augmentation character %c in entry at 0x%" PRIx64,
1114 AugmentationString[i], StartOffset);
1115 case 'L':
1116 LSDAPointerEncoding = Data.getU8(&Offset);
1117 break;
1118 case 'P': {
1119 if (Personality)
1120 return createStringError(
1122 "duplicate personality in entry at 0x%" PRIx64, StartOffset);
1123 PersonalityEncoding = Data.getU8(&Offset);
1124 Personality = Data.getEncodedPointer(
1125 &Offset, *PersonalityEncoding,
1126 EHFrameAddress ? EHFrameAddress + Offset : 0);
1127 break;
1128 }
1129 case 'R':
1130 FDEPointerEncoding = Data.getU8(&Offset);
1131 break;
1132 case 'S':
1133 // Current frame is a signal trampoline.
1134 break;
1135 case 'z':
1136 if (i)
1137 return createStringError(
1139 "'z' must be the first character at 0x%" PRIx64, StartOffset);
1140 // Parse the augmentation length first. We only parse it if
1141 // the string contains a 'z'.
1142 AugmentationLength = Data.getULEB128(&Offset);
1143 StartAugmentationOffset = Offset;
1144 EndAugmentationOffset = Offset + *AugmentationLength;
1145 break;
1146 case 'B':
1147 // B-Key is used for signing functions associated with this
1148 // augmentation string
1149 break;
1150 // This stack frame contains MTE tagged data, so needs to be
1151 // untagged on unwind.
1152 case 'G':
1153 break;
1154 }
1155 }
1156
1157 if (AugmentationLength) {
1158 if (Offset != EndAugmentationOffset)
1160 "parsing augmentation data at 0x%" PRIx64
1161 " failed",
1162 StartOffset);
1163 AugmentationData = Data.getData().slice(StartAugmentationOffset,
1164 EndAugmentationOffset);
1165 }
1166 }
1167
1168 auto Cie = std::make_unique<CIE>(
1169 IsDWARF64, StartOffset, Length, Version, AugmentationString,
1170 AddressSize, SegmentDescriptorSize, CodeAlignmentFactor,
1171 DataAlignmentFactor, ReturnAddressRegister, AugmentationData,
1172 FDEPointerEncoding, LSDAPointerEncoding, Personality,
1173 PersonalityEncoding, Arch);
1174 CIEs[StartOffset] = Cie.get();
1175 Entries.emplace_back(std::move(Cie));
1176 } else {
1177 // FDE
1178 uint64_t CIEPointer = Id;
1179 uint64_t InitialLocation = 0;
1181 std::optional<uint64_t> LSDAAddress;
1182 CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
1183
1184 if (IsEH) {
1185 // The address size is encoded in the CIE we reference.
1186 if (!Cie)
1188 "parsing FDE data at 0x%" PRIx64
1189 " failed due to missing CIE",
1190 StartOffset);
1191 if (auto Val =
1192 Data.getEncodedPointer(&Offset, Cie->getFDEPointerEncoding(),
1193 EHFrameAddress + Offset)) {
1194 InitialLocation = *Val;
1195 }
1196 if (auto Val = Data.getEncodedPointer(
1197 &Offset, Cie->getFDEPointerEncoding(), 0)) {
1198 AddressRange = *Val;
1199 }
1200
1201 StringRef AugmentationString = Cie->getAugmentationString();
1202 if (!AugmentationString.empty()) {
1203 // Parse the augmentation length and data for this FDE.
1204 uint64_t AugmentationLength = Data.getULEB128(&Offset);
1205
1206 uint64_t EndAugmentationOffset = Offset + AugmentationLength;
1207
1208 // Decode the LSDA if the CIE augmentation string said we should.
1209 if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) {
1210 LSDAAddress = Data.getEncodedPointer(
1212 EHFrameAddress ? Offset + EHFrameAddress : 0);
1213 }
1214
1215 if (Offset != EndAugmentationOffset)
1217 "parsing augmentation data at 0x%" PRIx64
1218 " failed",
1219 StartOffset);
1220 }
1221 } else {
1222 InitialLocation = Data.getRelocatedAddress(&Offset);
1223 AddressRange = Data.getRelocatedAddress(&Offset);
1224 }
1225
1226 Entries.emplace_back(new FDE(IsDWARF64, StartOffset, Length, CIEPointer,
1227 InitialLocation, AddressRange, Cie,
1228 LSDAAddress, Arch));
1229 }
1230
1231 if (Error E =
1232 Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset))
1233 return E;
1234
1235 if (Offset != EndStructureOffset)
1236 return createStringError(
1238 "parsing entry instructions at 0x%" PRIx64 " failed", StartOffset);
1239 }
1240
1241 return Error::success();
1242}
1243
1244FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
1245 auto It = partition_point(Entries, [=](const std::unique_ptr<FrameEntry> &E) {
1246 return E->getOffset() < Offset;
1247 });
1248 if (It != Entries.end() && (*It)->getOffset() == Offset)
1249 return It->get();
1250 return nullptr;
1251}
1252
1254 std::optional<uint64_t> Offset) const {
1255 DumpOpts.IsEH = IsEH;
1256 if (Offset) {
1257 if (auto *Entry = getEntryAtOffset(*Offset))
1258 Entry->dump(OS, DumpOpts);
1259 return;
1260 }
1261
1262 OS << "\n";
1263 for (const auto &Entry : Entries)
1264 Entry->dump(OS, DumpOpts);
1265}
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:203
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:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
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:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:222
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
@ 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:571
StringRef FormatString(DwarfFormat Format)
Definition: Dwarf.cpp:826
#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:523
@ DW_EH_PE_omit
Definition: Dwarf.h:524
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:456
@ Length
Definition: DWP.cpp:456
AddressSpace
Definition: NVPTXBaseInfo.h:21
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:2008
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1258
@ illegal_byte_sequence
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:431
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:193
std::function< void(Error)> RecoverableErrorHandler
Definition: DIContext.h:231
std::function< llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)> GetNameForDWARFReg
Definition: DIContext.h:211
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