30 using namespace dwarf;
39 while (*Offset < EndOffset) {
40 uint8_t Opcode = Data.
getU8(Offset);
50 return make_error<StringError>(
51 "Invalid primary CFI opcode",
53 case DW_CFA_advance_loc:
55 addInstruction(Primary, Op1);
58 addInstruction(Primary, Op1, Data.
getULEB128(Offset));
65 return make_error<StringError>(
66 "Invalid extended CFI opcode",
69 case DW_CFA_remember_state:
70 case DW_CFA_restore_state:
71 case DW_CFA_GNU_window_save:
73 addInstruction(Opcode);
77 addInstruction(Opcode, Data.
getAddress(Offset));
79 case DW_CFA_advance_loc1:
81 addInstruction(Opcode, Data.
getU8(Offset));
83 case DW_CFA_advance_loc2:
85 addInstruction(Opcode, Data.
getU16(Offset));
87 case DW_CFA_advance_loc4:
89 addInstruction(Opcode, Data.
getU32(Offset));
91 case DW_CFA_restore_extended:
92 case DW_CFA_undefined:
93 case DW_CFA_same_value:
94 case DW_CFA_def_cfa_register:
95 case DW_CFA_def_cfa_offset:
96 case DW_CFA_GNU_args_size:
98 addInstruction(Opcode, Data.
getULEB128(Offset));
100 case DW_CFA_def_cfa_offset_sf:
102 addInstruction(Opcode, Data.
getSLEB128(Offset));
104 case DW_CFA_offset_extended:
105 case DW_CFA_register:
107 case DW_CFA_val_offset: {
114 addInstruction(Opcode, op1, op2);
117 case DW_CFA_offset_extended_sf:
118 case DW_CFA_def_cfa_sf:
119 case DW_CFA_val_offset_sf: {
124 addInstruction(Opcode, op1, op2);
127 case DW_CFA_def_cfa_expression: {
129 addInstruction(Opcode, 0);
135 *Offset += ExprLength;
138 case DW_CFA_expression:
139 case DW_CFA_val_expression: {
142 addInstruction(Opcode, RegNum, 0);
148 *Offset += BlockLength;
164 static OperandType OpTypes[DW_CFA_restore+1][2];
165 static bool Initialized =
false;
171 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \ 173 OpTypes[OP][0] = OPTYPE0; \ 174 OpTypes[OP][1] = OPTYPE1; \ 176 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None) 177 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None) 180 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
181 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
182 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
183 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
184 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
185 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
186 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
189 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
190 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
193 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
194 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
195 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
196 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
197 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
198 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
199 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
200 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
219 unsigned OperandIdx, uint64_t Operand)
const {
221 uint8_t Opcode = Instr.
Opcode;
222 OperandType
Type = getOperandTypes()[Opcode][OperandIdx];
226 OS <<
" Unsupported " << (OperandIdx ?
"second" :
"first") <<
" operand to";
228 if (!OpcodeName.empty())
229 OS <<
" " << OpcodeName;
231 OS <<
format(
" Opcode %x", Opcode);
237 OS <<
format(
" %" PRIx64, Operand);
243 OS <<
format(
" %+" PRId64, int64_t(Operand));
245 case OT_FactoredCodeOffset:
246 if (CodeAlignmentFactor)
247 OS <<
format(
" %" PRId64, Operand * CodeAlignmentFactor);
249 OS <<
format(
" %" PRId64
"*code_alignment_factor" , Operand);
251 case OT_SignedFactDataOffset:
252 if (DataAlignmentFactor)
253 OS <<
format(
" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
255 OS <<
format(
" %" PRId64
"*data_alignment_factor" , int64_t(Operand));
257 case OT_UnsignedFactDataOffset:
258 if (DataAlignmentFactor)
259 OS <<
format(
" %" PRId64, Operand * DataAlignmentFactor);
261 OS <<
format(
" %" PRId64
"*data_alignment_factor" , Operand);
264 OS <<
format(
" reg%" PRId64, Operand);
275 unsigned IndentLevel)
const {
276 for (
const auto &Instr : Instructions) {
277 uint8_t Opcode = Instr.
Opcode;
280 OS.
indent(2 * IndentLevel);
282 for (
unsigned i = 0; i < Instr.
Ops.
size(); ++i)
283 printOperand(OS, MRI, IsEH, Instr, i, Instr.
Ops[i]);
293 OS <<
" Augmentation: \"" << Augmentation <<
"\"\n";
296 OS <<
format(
" Segment desc size: %u\n",
299 OS <<
format(
" Code alignment factor: %u\n", (
uint32_t)CodeAlignmentFactor);
300 OS <<
format(
" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor);
301 OS <<
format(
" Return address column: %d\n", (int32_t)ReturnAddressRegister);
303 OS <<
format(
" Personality Address: %08x\n", *Personality);
304 if (!AugmentationData.empty()) {
305 OS <<
" Augmentation data: ";
306 for (uint8_t Byte : AugmentationData)
307 OS << ' ' << hexdigit(Byte >> 4) <<
hexdigit(Byte & 0xf);
311 CFIs.dump(OS, MRI, IsEH);
317 (int32_t)LinkedCIEOffset);
318 OS <<
format(
"cie=%08x pc=%08x...%08x\n", (int32_t)LinkedCIEOffset,
322 OS <<
format(
" LSDA Address: %08x\n", *LSDAAddress);
323 CFIs.dump(OS, MRI, IsEH);
328 : IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
335 for (
int i = 0; i < Length; ++i) {
336 uint8_t c = Data.
getU8(&Offset);
347 const char *ErrorMsg) {
350 OS <<
format(ErrorMsg, StartOffset);
362 bool IsDWARF64 =
false;
363 uint64_t Length = Data.
getU32(&Offset);
366 if (Length == UINT32_MAX) {
371 Length = Data.
getU64(&Offset);
384 Id = Data.
getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4);
390 const char *Augmentation = Data.
getCStr(&Offset);
391 StringRef AugmentationString(Augmentation ? Augmentation :
"");
395 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.
getU8(&Offset);
396 uint64_t CodeAlignmentFactor = Data.
getULEB128(&Offset);
397 int64_t DataAlignmentFactor = Data.
getSLEB128(&Offset);
398 uint64_t ReturnAddressRegister = Data.
getULEB128(&Offset);
412 for (
unsigned i = 0, e = AugmentationString.
size(); i != e; ++i) {
413 switch (AugmentationString[i]) {
416 "Unknown augmentation character in entry at %lx");
418 LSDAPointerEncoding = Data.
getU8(&Offset);
423 "Duplicate personality in entry at %lx");
424 PersonalityEncoding = Data.
getU8(&Offset);
426 &Offset, *PersonalityEncoding,
427 EHFrameAddress ? EHFrameAddress + Offset : 0);
431 FDEPointerEncoding = Data.
getU8(&Offset);
439 "'z' must be the first character at %lx");
442 AugmentationLength = Data.
getULEB128(&Offset);
443 StartAugmentationOffset =
Offset;
444 EndAugmentationOffset = Offset +
445 static_cast<uint32_t>(*AugmentationLength);
449 if (AugmentationLength.
hasValue()) {
450 if (Offset != EndAugmentationOffset)
451 ReportError(StartOffset,
"Parsing augmentation data at %lx failed");
453 AugmentationData = Data.
getData().
slice(StartAugmentationOffset,
454 EndAugmentationOffset);
458 auto Cie = llvm::make_unique<CIE>(
459 StartOffset, Length,
Version, AugmentationString, AddressSize,
460 SegmentDescriptorSize, CodeAlignmentFactor, DataAlignmentFactor,
461 ReturnAddressRegister, AugmentationData, FDEPointerEncoding,
462 LSDAPointerEncoding, Personality, PersonalityEncoding);
463 CIEs[StartOffset] = Cie.get();
464 Entries.emplace_back(std::move(Cie));
467 uint64_t CIEPointer =
Id;
468 uint64_t InitialLocation = 0;
469 uint64_t AddressRange = 0;
471 CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
477 "Parsing FDE data at %lx failed due to missing CIE");
481 EHFrameAddress ? EHFrameAddress +
Offset : 0)) {
482 InitialLocation = *Val;
490 if (!AugmentationString.
empty()) {
492 uint64_t AugmentationLength = Data.
getULEB128(&Offset);
495 Offset +
static_cast<uint32_t>(AugmentationLength);
501 EHFrameAddress ? Offset + EHFrameAddress : 0);
504 if (Offset != EndAugmentationOffset)
505 ReportError(StartOffset,
"Parsing augmentation data at %lx failed");
512 Entries.emplace_back(
new FDE(StartOffset, Length, CIEPointer,
513 InitialLocation, AddressRange,
518 Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) {
522 if (Offset != EndStructureOffset)
523 ReportError(StartOffset,
"Parsing entry instructions at %lx failed");
530 [](
const std::unique_ptr<FrameEntry> &
E,
532 if (It != Entries.end() && (*It)->getOffset() ==
Offset)
540 if (
auto *Entry = getEntryAtOffset(*Offset))
541 Entry->dump(OS, MRI, IsEH);
546 for (
const auto &Entry : Entries)
547 Entry->dump(OS, MRI, IsEH);
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Error parse(DataExtractor Data, uint32_t *Offset, uint32_t EndOffset)
Parse and store a sequence of CFI instructions from Data, starting at *Offset and ending at EndOffset...
An entry in either debug_frame or eh_frame.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK
uint32_t getLSDAPointerEncoding() const
std::string toString(Error E)
Write all error messages (if any) in E to a string.
std::error_code make_error_code(BitcodeError E)
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
#define DECLARE_OP2(OP, OPTYPE0, OPTYPE1)
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16)...
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, Optional< uint64_t > Offset) const
Dump the section data into the given stream.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
unsigned const MachineRegisterInfo * MRI
DWARF Frame Description Entry (FDE)
The instances of the Type class are immutable: once they are created, they are never changed...
StringRef CallFrameString(unsigned Encoding)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint32_t getFDEPointerEncoding() const
#define LLVM_ATTRIBUTE_NORETURN
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH, unsigned IndentLevel=1) const
const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const override
Dump the instructions in this CFI fragment.
const uint32_t DW_CIE_ID
Special ID values that distinguish a CIE from a FDE in DWARF CFI.
static ErrorSuccess success()
Create a success value.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const override
Dump the instructions in this CFI fragment.
DWARF Common Information Entry (CIE)
This file contains constants used for implementing Dwarf debug support.
DWARFDebugFrame(bool IsEH=false, uint64_t EHFrameAddress=0)
#define LLVM_ATTRIBUTE_UNUSED
#define DECLARE_OP1(OP, OPTYPE0)
StringRef getAugmentationString() const
An instruction consists of a DWARF CFI opcode and an optional sequence of operands.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, uint32_t Offset, int Length)
Lightweight error class with error context and mandatory checking.
Optional< DWARFExpression > Expression
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
void parse(DWARFDataExtractor Data)
Parse the section from raw data.
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset, const char *ErrorMsg)
const uint64_t DW64_CIE_ID