1 //===- DWARFDebugFrame.h - Parsing of .debug_frame ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
11 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/BinaryFormat/Dwarf.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/DataExtractor.h"
19 #include "llvm/Support/Errc.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/raw_ostream.h"
31 using namespace dwarf;
34 // See DWARF standard v3, section 7.23
35 const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
36 const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
38 Error CFIProgram::parse(DataExtractor Data, uint32_t *Offset,
40 while (*Offset < EndOffset) {
41 uint8_t Opcode = Data.getU8(Offset);
42 // Some instructions have a primary opcode encoded in the top bits.
43 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
46 // If it's a primary opcode, the first operand is encoded in the bottom
47 // bits of the opcode itself.
48 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
51 return createStringError(errc::illegal_byte_sequence,
52 "Invalid primary CFI opcode 0x%" PRIx8,
54 case DW_CFA_advance_loc:
56 addInstruction(Primary, Op1);
59 addInstruction(Primary, Op1, Data.getULEB128(Offset));
63 // Extended opcode - its value is Opcode itself.
66 return createStringError(errc::illegal_byte_sequence,
67 "Invalid extended CFI opcode 0x%" PRIx8,
70 case DW_CFA_remember_state:
71 case DW_CFA_restore_state:
72 case DW_CFA_GNU_window_save:
74 addInstruction(Opcode);
78 addInstruction(Opcode, Data.getAddress(Offset));
80 case DW_CFA_advance_loc1:
81 // Operands: 1-byte delta
82 addInstruction(Opcode, Data.getU8(Offset));
84 case DW_CFA_advance_loc2:
85 // Operands: 2-byte delta
86 addInstruction(Opcode, Data.getU16(Offset));
88 case DW_CFA_advance_loc4:
89 // Operands: 4-byte delta
90 addInstruction(Opcode, Data.getU32(Offset));
92 case DW_CFA_restore_extended:
93 case DW_CFA_undefined:
94 case DW_CFA_same_value:
95 case DW_CFA_def_cfa_register:
96 case DW_CFA_def_cfa_offset:
97 case DW_CFA_GNU_args_size:
99 addInstruction(Opcode, Data.getULEB128(Offset));
101 case DW_CFA_def_cfa_offset_sf:
103 addInstruction(Opcode, Data.getSLEB128(Offset));
105 case DW_CFA_offset_extended:
106 case DW_CFA_register:
108 case DW_CFA_val_offset: {
109 // Operands: ULEB128, ULEB128
110 // Note: We can not embed getULEB128 directly into function
111 // argument list. getULEB128 changes Offset and order of evaluation
112 // for arguments is unspecified.
113 auto op1 = Data.getULEB128(Offset);
114 auto op2 = Data.getULEB128(Offset);
115 addInstruction(Opcode, op1, op2);
118 case DW_CFA_offset_extended_sf:
119 case DW_CFA_def_cfa_sf:
120 case DW_CFA_val_offset_sf: {
121 // Operands: ULEB128, SLEB128
122 // Note: see comment for the previous case
123 auto op1 = Data.getULEB128(Offset);
124 auto op2 = (uint64_t)Data.getSLEB128(Offset);
125 addInstruction(Opcode, op1, op2);
128 case DW_CFA_def_cfa_expression: {
129 uint32_t ExprLength = Data.getULEB128(Offset);
130 addInstruction(Opcode, 0);
131 DataExtractor Extractor(
132 Data.getData().slice(*Offset, *Offset + ExprLength),
133 Data.isLittleEndian(), Data.getAddressSize());
134 Instructions.back().Expression = DWARFExpression(
135 Extractor, Data.getAddressSize(), dwarf::DWARF_VERSION);
136 *Offset += ExprLength;
139 case DW_CFA_expression:
140 case DW_CFA_val_expression: {
141 auto RegNum = Data.getULEB128(Offset);
142 auto BlockLength = Data.getULEB128(Offset);
143 addInstruction(Opcode, RegNum, 0);
144 DataExtractor Extractor(
145 Data.getData().slice(*Offset, *Offset + BlockLength),
146 Data.isLittleEndian(), Data.getAddressSize());
147 Instructions.back().Expression = DWARFExpression(
148 Extractor, Data.getAddressSize(), dwarf::DWARF_VERSION);
149 *Offset += BlockLength;
156 return Error::success();
162 } // end anonymous namespace
164 ArrayRef<CFIProgram::OperandType[2]> CFIProgram::getOperandTypes() {
165 static OperandType OpTypes[DW_CFA_restore+1][2];
166 static bool Initialized = false;
168 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
172 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
174 OpTypes[OP][0] = OPTYPE0; \
175 OpTypes[OP][1] = OPTYPE1; \
177 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
178 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
180 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
181 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
182 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
183 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
184 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
185 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
186 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
187 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
188 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
189 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
190 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
191 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
192 DECLARE_OP1(DW_CFA_undefined, OT_Register);
193 DECLARE_OP1(DW_CFA_same_value, OT_Register);
194 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
195 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
196 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
197 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
198 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
199 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
200 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
201 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
202 DECLARE_OP1(DW_CFA_restore, OT_Register);
203 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
204 DECLARE_OP0(DW_CFA_remember_state);
205 DECLARE_OP0(DW_CFA_restore_state);
206 DECLARE_OP0(DW_CFA_GNU_window_save);
207 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
208 DECLARE_OP0(DW_CFA_nop);
214 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
217 /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
218 void CFIProgram::printOperand(raw_ostream &OS, const MCRegisterInfo *MRI,
219 bool IsEH, const Instruction &Instr,
220 unsigned OperandIdx, uint64_t Operand) const {
221 assert(OperandIdx < 2);
222 uint8_t Opcode = Instr.Opcode;
223 OperandType Type = getOperandTypes()[Opcode][OperandIdx];
227 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
228 auto OpcodeName = CallFrameString(Opcode, Arch);
229 if (!OpcodeName.empty())
230 OS << " " << OpcodeName;
232 OS << format(" Opcode %x", Opcode);
238 OS << format(" %" PRIx64, Operand);
241 // The offsets are all encoded in a unsigned form, but in practice
242 // consumers use them signed. It's most certainly legacy due to
243 // the lack of signed variants in the first Dwarf standards.
244 OS << format(" %+" PRId64, int64_t(Operand));
246 case OT_FactoredCodeOffset: // Always Unsigned
247 if (CodeAlignmentFactor)
248 OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
250 OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
252 case OT_SignedFactDataOffset:
253 if (DataAlignmentFactor)
254 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
256 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
258 case OT_UnsignedFactDataOffset:
259 if (DataAlignmentFactor)
260 OS << format(" %" PRId64, Operand * DataAlignmentFactor);
262 OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
265 OS << format(" reg%" PRId64, Operand);
268 assert(Instr.Expression && "missing DWARFExpression object");
270 Instr.Expression->print(OS, MRI, IsEH);
275 void CFIProgram::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
276 unsigned IndentLevel) const {
277 for (const auto &Instr : Instructions) {
278 uint8_t Opcode = Instr.Opcode;
279 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
280 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
281 OS.indent(2 * IndentLevel);
282 OS << CallFrameString(Opcode, Arch) << ":";
283 for (unsigned i = 0; i < Instr.Ops.size(); ++i)
284 printOperand(OS, MRI, IsEH, Instr, i, Instr.Ops[i]);
289 void CIE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const {
290 OS << format("%08x %08x %08x CIE", (uint32_t)Offset, (uint32_t)Length,
293 OS << format(" Version: %d\n", Version);
294 OS << " Augmentation: \"" << Augmentation << "\"\n";
296 OS << format(" Address size: %u\n", (uint32_t)AddressSize);
297 OS << format(" Segment desc size: %u\n",
298 (uint32_t)SegmentDescriptorSize);
300 OS << format(" Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor);
301 OS << format(" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor);
302 OS << format(" Return address column: %d\n", (int32_t)ReturnAddressRegister);
304 OS << format(" Personality Address: %08x\n", *Personality);
305 if (!AugmentationData.empty()) {
306 OS << " Augmentation data: ";
307 for (uint8_t Byte : AugmentationData)
308 OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
312 CFIs.dump(OS, MRI, IsEH);
316 void FDE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const {
317 OS << format("%08x %08x %08x FDE ", (uint32_t)Offset, (uint32_t)Length,
318 (int32_t)LinkedCIEOffset);
319 OS << format("cie=%08x pc=%08x...%08x\n", (int32_t)LinkedCIEOffset,
320 (uint32_t)InitialLocation,
321 (uint32_t)InitialLocation + (uint32_t)AddressRange);
323 OS << format(" LSDA Address: %08x\n", *LSDAAddress);
324 CFIs.dump(OS, MRI, IsEH);
328 DWARFDebugFrame::DWARFDebugFrame(Triple::ArchType Arch,
329 bool IsEH, uint64_t EHFrameAddress)
330 : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
332 DWARFDebugFrame::~DWARFDebugFrame() = default;
334 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
335 uint32_t Offset, int Length) {
337 for (int i = 0; i < Length; ++i) {
338 uint8_t c = Data.getU8(&Offset);
339 errs().write_hex(c); errs() << " ";
344 // This is a workaround for old compilers which do not allow
345 // noreturn attribute usage in lambdas. Once the support for those
346 // compilers are phased out, we can remove this and return back to
347 // a ReportError lambda: [StartOffset](const char *ErrorMsg).
348 static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset,
349 const char *ErrorMsg) {
351 raw_string_ostream OS(Str);
352 OS << format(ErrorMsg, StartOffset);
354 report_fatal_error(Str);
357 void DWARFDebugFrame::parse(DWARFDataExtractor Data) {
359 DenseMap<uint32_t, CIE *> CIEs;
361 while (Data.isValidOffset(Offset)) {
362 uint32_t StartOffset = Offset;
364 bool IsDWARF64 = false;
365 uint64_t Length = Data.getU32(&Offset);
368 if (Length == UINT32_MAX) {
369 // DWARF-64 is distinguished by the first 32 bits of the initial length
370 // field being 0xffffffff. Then, the next 64 bits are the actual entry
373 Length = Data.getU64(&Offset);
376 // At this point, Offset points to the next field after Length.
377 // Length is the structure size excluding itself. Compute an offset one
378 // past the end of the structure (needed to know how many instructions to
380 // TODO: For honest DWARF64 support, DataExtractor will have to treat
381 // offset_ptr as uint64_t*
382 uint32_t StartStructureOffset = Offset;
383 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
385 // The Id field's size depends on the DWARF format
386 Id = Data.getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4);
388 ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID || (IsEH && !Id));
391 uint8_t Version = Data.getU8(&Offset);
392 const char *Augmentation = Data.getCStr(&Offset);
393 StringRef AugmentationString(Augmentation ? Augmentation : "");
394 uint8_t AddressSize = Version < 4 ? Data.getAddressSize() :
396 Data.setAddressSize(AddressSize);
397 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
398 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
399 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
400 uint64_t ReturnAddressRegister =
401 Version == 1 ? Data.getU8(&Offset) : Data.getULEB128(&Offset);
403 // Parse the augmentation data for EH CIEs
404 StringRef AugmentationData("");
405 uint32_t FDEPointerEncoding = DW_EH_PE_absptr;
406 uint32_t LSDAPointerEncoding = DW_EH_PE_omit;
407 Optional<uint64_t> Personality;
408 Optional<uint32_t> PersonalityEncoding;
410 Optional<uint64_t> AugmentationLength;
411 uint32_t StartAugmentationOffset;
412 uint32_t EndAugmentationOffset;
414 // Walk the augmentation string to get all the augmentation data.
415 for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) {
416 switch (AugmentationString[i]) {
418 ReportError(StartOffset,
419 "Unknown augmentation character in entry at %lx");
421 LSDAPointerEncoding = Data.getU8(&Offset);
425 ReportError(StartOffset,
426 "Duplicate personality in entry at %lx");
427 PersonalityEncoding = Data.getU8(&Offset);
428 Personality = Data.getEncodedPointer(
429 &Offset, *PersonalityEncoding,
430 EHFrameAddress ? EHFrameAddress + Offset : 0);
434 FDEPointerEncoding = Data.getU8(&Offset);
437 // Current frame is a signal trampoline.
441 ReportError(StartOffset,
442 "'z' must be the first character at %lx");
443 // Parse the augmentation length first. We only parse it if
444 // the string contains a 'z'.
445 AugmentationLength = Data.getULEB128(&Offset);
446 StartAugmentationOffset = Offset;
447 EndAugmentationOffset = Offset +
448 static_cast<uint32_t>(*AugmentationLength);
451 // B-Key is used for signing functions associated with this
452 // augmentation string
457 if (AugmentationLength.hasValue()) {
458 if (Offset != EndAugmentationOffset)
459 ReportError(StartOffset, "Parsing augmentation data at %lx failed");
461 AugmentationData = Data.getData().slice(StartAugmentationOffset,
462 EndAugmentationOffset);
466 auto Cie = llvm::make_unique<CIE>(
467 StartOffset, Length, Version, AugmentationString, AddressSize,
468 SegmentDescriptorSize, CodeAlignmentFactor, DataAlignmentFactor,
469 ReturnAddressRegister, AugmentationData, FDEPointerEncoding,
470 LSDAPointerEncoding, Personality, PersonalityEncoding, Arch);
471 CIEs[StartOffset] = Cie.get();
472 Entries.emplace_back(std::move(Cie));
475 uint64_t CIEPointer = Id;
476 uint64_t InitialLocation = 0;
477 uint64_t AddressRange = 0;
478 Optional<uint64_t> LSDAAddress;
479 CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
482 // The address size is encoded in the CIE we reference.
484 ReportError(StartOffset,
485 "Parsing FDE data at %lx failed due to missing CIE");
487 if (auto Val = Data.getEncodedPointer(
488 &Offset, Cie->getFDEPointerEncoding(),
489 EHFrameAddress ? EHFrameAddress + Offset : 0)) {
490 InitialLocation = *Val;
492 if (auto Val = Data.getEncodedPointer(
493 &Offset, Cie->getFDEPointerEncoding(), 0)) {
497 StringRef AugmentationString = Cie->getAugmentationString();
498 if (!AugmentationString.empty()) {
499 // Parse the augmentation length and data for this FDE.
500 uint64_t AugmentationLength = Data.getULEB128(&Offset);
502 uint32_t EndAugmentationOffset =
503 Offset + static_cast<uint32_t>(AugmentationLength);
505 // Decode the LSDA if the CIE augmentation string said we should.
506 if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) {
507 LSDAAddress = Data.getEncodedPointer(
508 &Offset, Cie->getLSDAPointerEncoding(),
509 EHFrameAddress ? Offset + EHFrameAddress : 0);
512 if (Offset != EndAugmentationOffset)
513 ReportError(StartOffset, "Parsing augmentation data at %lx failed");
516 InitialLocation = Data.getAddress(&Offset);
517 AddressRange = Data.getAddress(&Offset);
520 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
521 InitialLocation, AddressRange,
522 Cie, LSDAAddress, Arch));
526 Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) {
527 report_fatal_error(toString(std::move(E)));
530 if (Offset != EndStructureOffset)
531 ReportError(StartOffset, "Parsing entry instructions at %lx failed");
535 FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
537 std::lower_bound(Entries.begin(), Entries.end(), Offset,
538 [](const std::unique_ptr<FrameEntry> &E,
539 uint64_t Offset) { return E->getOffset() < Offset; });
540 if (It != Entries.end() && (*It)->getOffset() == Offset)
545 void DWARFDebugFrame::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
546 Optional<uint64_t> Offset) const {
548 if (auto *Entry = getEntryAtOffset(*Offset))
549 Entry->dump(OS, MRI, IsEH);
554 for (const auto &Entry : Entries)
555 Entry->dump(OS, MRI, IsEH);