30 using namespace dwarf;
39 while (*Offset < EndOffset) {
51 "Invalid primary CFI opcode 0x%" PRIx8,
53 case DW_CFA_advance_loc:
55 addInstruction(Primary, Op1);
58 addInstruction(Primary, Op1, Data.
getULEB128(Offset));
66 "Invalid extended CFI opcode 0x%" PRIx8,
69 case DW_CFA_remember_state:
70 case DW_CFA_restore_state:
71 case DW_CFA_GNU_window_save:
73 addInstruction(Opcode);
79 case DW_CFA_advance_loc1:
83 case DW_CFA_advance_loc2:
87 case DW_CFA_advance_loc4:
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);
269 Instr.
Expression->print(OS, MRI,
nullptr, IsEH);
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: %016" PRIx64
"\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: %016" PRIx64
"\n", *LSDAAddress);
323 CFIs.dump(OS, MRI, IsEH);
328 bool IsEH, uint64_t EHFrameAddress)
329 : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
334 uint64_t
Offset,
int Length) {
336 for (
int i = 0; i < Length; ++i) {
337 uint8_t c = Data.
getU8(&Offset);
348 const char *ErrorMsg) {
351 OS <<
format(ErrorMsg, StartOffset);
361 uint64_t StartOffset =
Offset;
363 bool IsDWARF64 =
false;
379 uint64_t StartStructureOffset =
Offset;
380 uint64_t EndStructureOffset = Offset + Length;
383 Id = Data.
getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4);
389 const char *Augmentation = Data.
getCStr(&Offset);
390 StringRef AugmentationString(Augmentation ? Augmentation :
"");
394 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.
getU8(&Offset);
395 uint64_t CodeAlignmentFactor = Data.
getULEB128(&Offset);
396 int64_t DataAlignmentFactor = Data.
getSLEB128(&Offset);
397 uint64_t ReturnAddressRegister =
408 uint64_t StartAugmentationOffset;
409 uint64_t EndAugmentationOffset;
412 for (
unsigned i = 0,
e = AugmentationString.
size(); i !=
e; ++i) {
413 switch (AugmentationString[i]) {
417 "Unknown augmentation character in entry at %" PRIx64);
419 LSDAPointerEncoding = Data.
getU8(&Offset);
424 "Duplicate personality in entry at %" PRIx64);
425 PersonalityEncoding = Data.
getU8(&Offset);
427 &Offset, *PersonalityEncoding,
428 EHFrameAddress ? EHFrameAddress + Offset : 0);
432 FDEPointerEncoding = Data.
getU8(&Offset);
440 "'z' must be the first character at %" PRIx64);
443 AugmentationLength = Data.
getULEB128(&Offset);
444 StartAugmentationOffset =
Offset;
445 EndAugmentationOffset = Offset + *AugmentationLength;
454 if (AugmentationLength.
hasValue()) {
455 if (Offset != EndAugmentationOffset)
457 "Parsing augmentation data at %" PRIx64
" failed");
459 AugmentationData = Data.
getData().
slice(StartAugmentationOffset,
460 EndAugmentationOffset);
464 auto Cie = std::make_unique<CIE>(
465 StartOffset, Length,
Version, AugmentationString, AddressSize,
466 SegmentDescriptorSize, CodeAlignmentFactor, DataAlignmentFactor,
467 ReturnAddressRegister, AugmentationData, FDEPointerEncoding,
468 LSDAPointerEncoding, Personality, PersonalityEncoding, Arch);
469 CIEs[StartOffset] = Cie.get();
470 Entries.emplace_back(std::move(Cie));
473 uint64_t CIEPointer =
Id;
474 uint64_t InitialLocation = 0;
475 uint64_t AddressRange = 0;
477 CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
482 ReportError(StartOffset,
"Parsing FDE data at %" PRIx64
483 " failed due to missing CIE");
487 EHFrameAddress ? EHFrameAddress +
Offset : 0)) {
488 InitialLocation = *Val;
496 if (!AugmentationString.
empty()) {
498 uint64_t AugmentationLength = Data.
getULEB128(&Offset);
500 uint64_t EndAugmentationOffset = Offset + AugmentationLength;
506 EHFrameAddress ? Offset + EHFrameAddress : 0);
509 if (Offset != EndAugmentationOffset)
511 "Parsing augmentation data at %" PRIx64
" failed");
518 Entries.emplace_back(
new FDE(StartOffset, Length, CIEPointer,
519 InitialLocation, AddressRange,
520 Cie, LSDAAddress, Arch));
524 Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) {
528 if (Offset != EndStructureOffset)
530 "Parsing entry instructions at %" PRIx64
" failed");
535 auto It =
partition_point(Entries, [=](
const std::unique_ptr<FrameEntry> &
E) {
536 return E->getOffset() <
Offset;
538 if (It != Entries.end() && (*It)->getOffset() ==
Offset)
546 if (
auto *
Entry = getEntryAtOffset(*Offset))
547 Entry->dump(OS, MRI, IsEH);
552 for (
const auto &
Entry : Entries)
553 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.
This class represents lattice values for constants.
static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, uint64_t Offset, int Length)
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...
DWARFDebugFrame(Triple::ArchType Arch, bool IsEH=false, uint64_t EHFrameAddress=0)
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
StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch)
std::string toString(Error E)
Write all error messages (if any) in E to a string.
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
auto partition_point(R &&Range, Predicate P) -> decltype(adl_begin(Range))
Binary search for the first iterator in a range where a predicate is false.
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
#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...
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint64_t StartOffset, const char *ErrorMsg)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint32_t getFDEPointerEncoding() const
#define LLVM_ATTRIBUTE_UNUSED
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.
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const override
Dump the instructions in this CFI fragment.
#define LLVM_ATTRIBUTE_NORETURN
DWARF Common Information Entry (CIE)
This file contains constants used for implementing Dwarf debug support.
#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.
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.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
const uint64_t DW64_CIE_ID