1 //===-- DWARFDebugFrame.h - Parsing of .debug_frame -------------*- C++ -*-===//
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/ArrayRef.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/Support/Casting.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/Dwarf.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
23 using namespace dwarf;
26 /// \brief Abstract frame entry defining the common interface concrete
27 /// entries implement.
28 class llvm::FrameEntry {
30 enum FrameKind {FK_CIE, FK_FDE};
31 FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length)
32 : Kind(K), Offset(Offset), Length(Length) {}
34 virtual ~FrameEntry() {
37 FrameKind getKind() const { return Kind; }
38 virtual uint64_t getOffset() const { return Offset; }
40 /// \brief Parse and store a sequence of CFI instructions from Data,
41 /// starting at *Offset and ending at EndOffset. If everything
42 /// goes well, *Offset should be equal to EndOffset when this method
43 /// returns. Otherwise, an error occurred.
44 virtual void parseInstructions(DataExtractor Data, uint32_t *Offset,
47 /// \brief Dump the entry header to the given output stream.
48 virtual void dumpHeader(raw_ostream &OS) const = 0;
50 /// \brief Dump the entry's instructions to the given output stream.
51 virtual void dumpInstructions(raw_ostream &OS) const;
56 /// \brief Offset of this entry in the section.
59 /// \brief Entry length as specified in DWARF.
62 /// An entry may contain CFI instructions. An instruction consists of an
63 /// opcode and an optional sequence of operands.
64 typedef std::vector<uint64_t> Operands;
66 Instruction(uint8_t Opcode)
74 std::vector<Instruction> Instructions;
76 /// Convenience methods to add a new instruction with the given opcode and
77 /// operands to the Instructions vector.
78 void addInstruction(uint8_t Opcode) {
79 Instructions.push_back(Instruction(Opcode));
82 void addInstruction(uint8_t Opcode, uint64_t Operand1) {
83 Instructions.push_back(Instruction(Opcode));
84 Instructions.back().Ops.push_back(Operand1);
87 void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
88 Instructions.push_back(Instruction(Opcode));
89 Instructions.back().Ops.push_back(Operand1);
90 Instructions.back().Ops.push_back(Operand2);
95 // See DWARF standard v3, section 7.23
96 const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
97 const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
99 void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset,
100 uint32_t EndOffset) {
101 while (*Offset < EndOffset) {
102 uint8_t Opcode = Data.getU8(Offset);
103 // Some instructions have a primary opcode encoded in the top bits.
104 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
107 // If it's a primary opcode, the first operand is encoded in the bottom
108 // bits of the opcode itself.
109 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
111 default: llvm_unreachable("Impossible primary CFI opcode");
112 case DW_CFA_advance_loc:
114 addInstruction(Primary, Op1);
117 addInstruction(Primary, Op1, Data.getULEB128(Offset));
121 // Extended opcode - its value is Opcode itself.
123 default: llvm_unreachable("Invalid extended CFI opcode");
125 case DW_CFA_remember_state:
126 case DW_CFA_restore_state:
127 case DW_CFA_GNU_window_save:
129 addInstruction(Opcode);
133 addInstruction(Opcode, Data.getAddress(Offset));
135 case DW_CFA_advance_loc1:
136 // Operands: 1-byte delta
137 addInstruction(Opcode, Data.getU8(Offset));
139 case DW_CFA_advance_loc2:
140 // Operands: 2-byte delta
141 addInstruction(Opcode, Data.getU16(Offset));
143 case DW_CFA_advance_loc4:
144 // Operands: 4-byte delta
145 addInstruction(Opcode, Data.getU32(Offset));
147 case DW_CFA_restore_extended:
148 case DW_CFA_undefined:
149 case DW_CFA_same_value:
150 case DW_CFA_def_cfa_register:
151 case DW_CFA_def_cfa_offset:
153 addInstruction(Opcode, Data.getULEB128(Offset));
155 case DW_CFA_def_cfa_offset_sf:
157 addInstruction(Opcode, Data.getSLEB128(Offset));
159 case DW_CFA_offset_extended:
160 case DW_CFA_register:
162 case DW_CFA_val_offset:
163 // Operands: ULEB128, ULEB128
164 addInstruction(Opcode, Data.getULEB128(Offset),
165 Data.getULEB128(Offset));
167 case DW_CFA_offset_extended_sf:
168 case DW_CFA_def_cfa_sf:
169 case DW_CFA_val_offset_sf:
170 // Operands: ULEB128, SLEB128
171 addInstruction(Opcode, Data.getULEB128(Offset),
172 Data.getSLEB128(Offset));
174 case DW_CFA_def_cfa_expression:
175 case DW_CFA_expression:
176 case DW_CFA_val_expression:
177 // TODO: implement this
178 report_fatal_error("Values with expressions not implemented yet!");
185 /// \brief DWARF Common Information Entry (CIE)
186 class CIE : public FrameEntry {
188 // CIEs (and FDEs) are simply container classes, so the only sensible way to
189 // create them is by providing the full parsed contents in the constructor.
190 CIE(uint64_t Offset, uint64_t Length, uint8_t Version,
191 SmallString<8> Augmentation, uint64_t CodeAlignmentFactor,
192 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
193 : FrameEntry(FK_CIE, Offset, Length), Version(Version),
194 Augmentation(std::move(Augmentation)),
195 CodeAlignmentFactor(CodeAlignmentFactor),
196 DataAlignmentFactor(DataAlignmentFactor),
197 ReturnAddressRegister(ReturnAddressRegister) {}
202 uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
203 int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
205 void dumpHeader(raw_ostream &OS) const override {
206 OS << format("%08x %08x %08x CIE",
207 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
209 OS << format(" Version: %d\n", Version);
210 OS << " Augmentation: \"" << Augmentation << "\"\n";
211 OS << format(" Code alignment factor: %u\n",
212 (uint32_t)CodeAlignmentFactor);
213 OS << format(" Data alignment factor: %d\n",
214 (int32_t)DataAlignmentFactor);
215 OS << format(" Return address column: %d\n",
216 (int32_t)ReturnAddressRegister);
220 static bool classof(const FrameEntry *FE) {
221 return FE->getKind() == FK_CIE;
225 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
227 SmallString<8> Augmentation;
228 uint64_t CodeAlignmentFactor;
229 int64_t DataAlignmentFactor;
230 uint64_t ReturnAddressRegister;
234 /// \brief DWARF Frame Description Entry (FDE)
235 class FDE : public FrameEntry {
237 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
238 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
239 // is obtained lazily once it's actually required.
240 FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset,
241 uint64_t InitialLocation, uint64_t AddressRange,
243 : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
244 InitialLocation(InitialLocation), AddressRange(AddressRange),
250 CIE *getLinkedCIE() const { return LinkedCIE; }
252 void dumpHeader(raw_ostream &OS) const override {
253 OS << format("%08x %08x %08x FDE ",
254 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
255 OS << format("cie=%08x pc=%08x...%08x\n",
256 (int32_t)LinkedCIEOffset,
257 (uint32_t)InitialLocation,
258 (uint32_t)InitialLocation + (uint32_t)AddressRange);
261 static bool classof(const FrameEntry *FE) {
262 return FE->getKind() == FK_FDE;
266 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
267 uint64_t LinkedCIEOffset;
268 uint64_t InitialLocation;
269 uint64_t AddressRange;
273 /// \brief Types of operands to CF instructions.
279 OT_FactoredCodeOffset,
280 OT_SignedFactDataOffset,
281 OT_UnsignedFactDataOffset,
286 } // end anonymous namespace
288 /// \brief Initialize the array describing the types of operands.
289 static ArrayRef<OperandType[2]> getOperandTypes() {
290 static OperandType OpTypes[DW_CFA_restore+1][2];
292 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
294 OpTypes[OP][0] = OPTYPE0; \
295 OpTypes[OP][1] = OPTYPE1; \
297 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
298 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
300 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
301 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
302 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
303 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
304 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
305 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
306 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
307 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
308 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
309 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
310 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
311 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
312 DECLARE_OP1(DW_CFA_undefined, OT_Register);
313 DECLARE_OP1(DW_CFA_same_value, OT_Register);
314 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
315 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
316 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
317 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
318 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
319 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
320 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
321 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
322 DECLARE_OP1(DW_CFA_restore, OT_Register);
323 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
324 DECLARE_OP0(DW_CFA_remember_state);
325 DECLARE_OP0(DW_CFA_restore_state);
326 DECLARE_OP0(DW_CFA_GNU_window_save);
327 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
328 DECLARE_OP0(DW_CFA_nop);
333 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
336 static ArrayRef<OperandType[2]> OpTypes = getOperandTypes();
338 /// \brief Print \p Opcode's operand number \p OperandIdx which has
339 /// value \p Operand.
340 static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx,
341 uint64_t Operand, uint64_t CodeAlignmentFactor,
342 int64_t DataAlignmentFactor) {
343 assert(OperandIdx < 2);
344 OperandType Type = OpTypes[Opcode][OperandIdx];
348 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
349 if (const char *OpcodeName = CallFrameString(Opcode))
350 OS << " " << OpcodeName;
352 OS << format(" Opcode %x", Opcode);
357 OS << format(" %" PRIx64, Operand);
360 // The offsets are all encoded in a unsigned form, but in practice
361 // consumers use them signed. It's most certainly legacy due to
362 // the lack of signed variants in the first Dwarf standards.
363 OS << format(" %+" PRId64, int64_t(Operand));
365 case OT_FactoredCodeOffset: // Always Unsigned
366 if (CodeAlignmentFactor)
367 OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
369 OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
371 case OT_SignedFactDataOffset:
372 if (DataAlignmentFactor)
373 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
375 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
377 case OT_UnsignedFactDataOffset:
378 if (DataAlignmentFactor)
379 OS << format(" %" PRId64, Operand * DataAlignmentFactor);
381 OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
384 OS << format(" reg%" PRId64, Operand);
392 void FrameEntry::dumpInstructions(raw_ostream &OS) const {
393 uint64_t CodeAlignmentFactor = 0;
394 int64_t DataAlignmentFactor = 0;
395 const CIE *Cie = dyn_cast<CIE>(this);
398 Cie = cast<FDE>(this)->getLinkedCIE();
400 CodeAlignmentFactor = Cie->getCodeAlignmentFactor();
401 DataAlignmentFactor = Cie->getDataAlignmentFactor();
404 for (const auto &Instr : Instructions) {
405 uint8_t Opcode = Instr.Opcode;
406 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
407 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
408 OS << " " << CallFrameString(Opcode) << ":";
409 for (unsigned i = 0; i < Instr.Ops.size(); ++i)
410 printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor,
411 DataAlignmentFactor);
416 DWARFDebugFrame::DWARFDebugFrame() {
419 DWARFDebugFrame::~DWARFDebugFrame() {
422 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
423 uint32_t Offset, int Length) {
425 for (int i = 0; i < Length; ++i) {
426 uint8_t c = Data.getU8(&Offset);
427 errs().write_hex(c); errs() << " ";
433 void DWARFDebugFrame::parse(DataExtractor Data) {
435 DenseMap<uint32_t, CIE *> CIEs;
437 while (Data.isValidOffset(Offset)) {
438 uint32_t StartOffset = Offset;
440 bool IsDWARF64 = false;
441 uint64_t Length = Data.getU32(&Offset);
444 if (Length == UINT32_MAX) {
445 // DWARF-64 is distinguished by the first 32 bits of the initial length
446 // field being 0xffffffff. Then, the next 64 bits are the actual entry
449 Length = Data.getU64(&Offset);
452 // At this point, Offset points to the next field after Length.
453 // Length is the structure size excluding itself. Compute an offset one
454 // past the end of the structure (needed to know how many instructions to
456 // TODO: For honest DWARF64 support, DataExtractor will have to treat
457 // offset_ptr as uint64_t*
458 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
460 // The Id field's size depends on the DWARF format
461 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
462 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
465 // Note: this is specifically DWARFv3 CIE header structure. It was
466 // changed in DWARFv4. We currently don't support reading DWARFv4
467 // here because LLVM itself does not emit it (and LLDB doesn't
468 // support it either).
469 uint8_t Version = Data.getU8(&Offset);
470 const char *Augmentation = Data.getCStr(&Offset);
471 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
472 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
473 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
475 auto Cie = make_unique<CIE>(StartOffset, Length, Version,
476 StringRef(Augmentation), CodeAlignmentFactor,
477 DataAlignmentFactor, ReturnAddressRegister);
478 CIEs[StartOffset] = Cie.get();
479 Entries.emplace_back(std::move(Cie));
482 uint64_t CIEPointer = Id;
483 uint64_t InitialLocation = Data.getAddress(&Offset);
484 uint64_t AddressRange = Data.getAddress(&Offset);
486 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
487 InitialLocation, AddressRange,
491 Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset);
493 if (Offset != EndStructureOffset) {
495 raw_string_ostream OS(Str);
496 OS << format("Parsing entry instructions at %lx failed", StartOffset);
497 report_fatal_error(Str);
503 void DWARFDebugFrame::dump(raw_ostream &OS) const {
505 for (const auto &Entry : Entries) {
506 Entry->dumpHeader(OS);
507 Entry->dumpInstructions(OS);