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/DenseMap.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/Support/Casting.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/Dwarf.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/raw_ostream.h"
24 using namespace dwarf;
27 /// \brief Abstract frame entry defining the common interface concrete
28 /// entries implement.
29 class llvm::FrameEntry {
31 enum FrameKind {FK_CIE, FK_FDE};
32 FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length)
33 : Kind(K), Offset(Offset), Length(Length) {}
35 virtual ~FrameEntry() {
38 FrameKind getKind() const { return Kind; }
39 virtual uint64_t getOffset() const { return Offset; }
41 /// \brief Parse and store a sequence of CFI instructions from Data,
42 /// starting at *Offset and ending at EndOffset. If everything
43 /// goes well, *Offset should be equal to EndOffset when this method
44 /// returns. Otherwise, an error occurred.
45 virtual void parseInstructions(DataExtractor Data, uint32_t *Offset,
48 /// \brief Dump the entry header to the given output stream.
49 virtual void dumpHeader(raw_ostream &OS) const = 0;
51 /// \brief Dump the entry's instructions to the given output stream.
52 virtual void dumpInstructions(raw_ostream &OS) const;
57 /// \brief Offset of this entry in the section.
60 /// \brief Entry length as specified in DWARF.
63 /// An entry may contain CFI instructions. An instruction consists of an
64 /// opcode and an optional sequence of operands.
65 typedef std::vector<uint64_t> Operands;
67 Instruction(uint8_t Opcode)
75 std::vector<Instruction> Instructions;
77 /// Convenience methods to add a new instruction with the given opcode and
78 /// operands to the Instructions vector.
79 void addInstruction(uint8_t Opcode) {
80 Instructions.push_back(Instruction(Opcode));
83 void addInstruction(uint8_t Opcode, uint64_t Operand1) {
84 Instructions.push_back(Instruction(Opcode));
85 Instructions.back().Ops.push_back(Operand1);
88 void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
89 Instructions.push_back(Instruction(Opcode));
90 Instructions.back().Ops.push_back(Operand1);
91 Instructions.back().Ops.push_back(Operand2);
96 // See DWARF standard v3, section 7.23
97 const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
98 const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
100 void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset,
101 uint32_t EndOffset) {
102 while (*Offset < EndOffset) {
103 uint8_t Opcode = Data.getU8(Offset);
104 // Some instructions have a primary opcode encoded in the top bits.
105 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
108 // If it's a primary opcode, the first operand is encoded in the bottom
109 // bits of the opcode itself.
110 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
112 default: llvm_unreachable("Impossible primary CFI opcode");
113 case DW_CFA_advance_loc:
115 addInstruction(Primary, Op1);
118 addInstruction(Primary, Op1, Data.getULEB128(Offset));
122 // Extended opcode - its value is Opcode itself.
124 default: llvm_unreachable("Invalid extended CFI opcode");
126 case DW_CFA_remember_state:
127 case DW_CFA_restore_state:
128 case DW_CFA_GNU_window_save:
130 addInstruction(Opcode);
134 addInstruction(Opcode, Data.getAddress(Offset));
136 case DW_CFA_advance_loc1:
137 // Operands: 1-byte delta
138 addInstruction(Opcode, Data.getU8(Offset));
140 case DW_CFA_advance_loc2:
141 // Operands: 2-byte delta
142 addInstruction(Opcode, Data.getU16(Offset));
144 case DW_CFA_advance_loc4:
145 // Operands: 4-byte delta
146 addInstruction(Opcode, Data.getU32(Offset));
148 case DW_CFA_restore_extended:
149 case DW_CFA_undefined:
150 case DW_CFA_same_value:
151 case DW_CFA_def_cfa_register:
152 case DW_CFA_def_cfa_offset:
154 addInstruction(Opcode, Data.getULEB128(Offset));
156 case DW_CFA_def_cfa_offset_sf:
158 addInstruction(Opcode, Data.getSLEB128(Offset));
160 case DW_CFA_offset_extended:
161 case DW_CFA_register:
163 case DW_CFA_val_offset:
164 // Operands: ULEB128, ULEB128
165 addInstruction(Opcode, Data.getULEB128(Offset),
166 Data.getULEB128(Offset));
168 case DW_CFA_offset_extended_sf:
169 case DW_CFA_def_cfa_sf:
170 case DW_CFA_val_offset_sf:
171 // Operands: ULEB128, SLEB128
172 addInstruction(Opcode, Data.getULEB128(Offset),
173 Data.getSLEB128(Offset));
175 case DW_CFA_def_cfa_expression:
176 case DW_CFA_expression:
177 case DW_CFA_val_expression:
178 // TODO: implement this
179 report_fatal_error("Values with expressions not implemented yet!");
186 /// \brief DWARF Common Information Entry (CIE)
187 class CIE : public FrameEntry {
189 // CIEs (and FDEs) are simply container classes, so the only sensible way to
190 // create them is by providing the full parsed contents in the constructor.
191 CIE(uint64_t Offset, uint64_t Length, uint8_t Version,
192 SmallString<8> Augmentation, uint8_t AddressSize,
193 uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor,
194 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
195 : FrameEntry(FK_CIE, Offset, Length), Version(Version),
196 Augmentation(std::move(Augmentation)),
197 AddressSize(AddressSize),
198 SegmentDescriptorSize(SegmentDescriptorSize),
199 CodeAlignmentFactor(CodeAlignmentFactor),
200 DataAlignmentFactor(DataAlignmentFactor),
201 ReturnAddressRegister(ReturnAddressRegister) {}
205 uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
206 int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
208 void dumpHeader(raw_ostream &OS) const override {
209 OS << format("%08x %08x %08x CIE",
210 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
212 OS << format(" Version: %d\n", Version);
213 OS << " Augmentation: \"" << Augmentation << "\"\n";
215 OS << format(" Address size: %u\n",
216 (uint32_t)AddressSize);
217 OS << format(" Segment desc size: %u\n",
218 (uint32_t)SegmentDescriptorSize);
220 OS << format(" Code alignment factor: %u\n",
221 (uint32_t)CodeAlignmentFactor);
222 OS << format(" Data alignment factor: %d\n",
223 (int32_t)DataAlignmentFactor);
224 OS << format(" Return address column: %d\n",
225 (int32_t)ReturnAddressRegister);
229 static bool classof(const FrameEntry *FE) {
230 return FE->getKind() == FK_CIE;
234 /// The following fields are defined in section 6.4.1 of the DWARF standard v4
236 SmallString<8> Augmentation;
238 uint8_t SegmentDescriptorSize;
239 uint64_t CodeAlignmentFactor;
240 int64_t DataAlignmentFactor;
241 uint64_t ReturnAddressRegister;
245 /// \brief DWARF Frame Description Entry (FDE)
246 class FDE : public FrameEntry {
248 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
249 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
250 // is obtained lazily once it's actually required.
251 FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset,
252 uint64_t InitialLocation, uint64_t AddressRange,
254 : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
255 InitialLocation(InitialLocation), AddressRange(AddressRange),
260 CIE *getLinkedCIE() const { return LinkedCIE; }
262 void dumpHeader(raw_ostream &OS) const override {
263 OS << format("%08x %08x %08x FDE ",
264 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
265 OS << format("cie=%08x pc=%08x...%08x\n",
266 (int32_t)LinkedCIEOffset,
267 (uint32_t)InitialLocation,
268 (uint32_t)InitialLocation + (uint32_t)AddressRange);
271 static bool classof(const FrameEntry *FE) {
272 return FE->getKind() == FK_FDE;
276 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
277 uint64_t LinkedCIEOffset;
278 uint64_t InitialLocation;
279 uint64_t AddressRange;
283 /// \brief Types of operands to CF instructions.
289 OT_FactoredCodeOffset,
290 OT_SignedFactDataOffset,
291 OT_UnsignedFactDataOffset,
296 } // end anonymous namespace
298 /// \brief Initialize the array describing the types of operands.
299 static ArrayRef<OperandType[2]> getOperandTypes() {
300 static OperandType OpTypes[DW_CFA_restore+1][2];
302 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
304 OpTypes[OP][0] = OPTYPE0; \
305 OpTypes[OP][1] = OPTYPE1; \
307 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
308 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
310 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
311 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
312 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
313 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
314 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
315 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
316 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
317 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
318 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
319 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
320 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
321 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
322 DECLARE_OP1(DW_CFA_undefined, OT_Register);
323 DECLARE_OP1(DW_CFA_same_value, OT_Register);
324 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
325 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
326 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
327 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
328 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
329 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
330 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
331 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
332 DECLARE_OP1(DW_CFA_restore, OT_Register);
333 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
334 DECLARE_OP0(DW_CFA_remember_state);
335 DECLARE_OP0(DW_CFA_restore_state);
336 DECLARE_OP0(DW_CFA_GNU_window_save);
337 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
338 DECLARE_OP0(DW_CFA_nop);
343 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
346 static ArrayRef<OperandType[2]> OpTypes = getOperandTypes();
348 /// \brief Print \p Opcode's operand number \p OperandIdx which has
349 /// value \p Operand.
350 static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx,
351 uint64_t Operand, uint64_t CodeAlignmentFactor,
352 int64_t DataAlignmentFactor) {
353 assert(OperandIdx < 2);
354 OperandType Type = OpTypes[Opcode][OperandIdx];
358 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
359 if (const char *OpcodeName = CallFrameString(Opcode))
360 OS << " " << OpcodeName;
362 OS << format(" Opcode %x", Opcode);
367 OS << format(" %" PRIx64, Operand);
370 // The offsets are all encoded in a unsigned form, but in practice
371 // consumers use them signed. It's most certainly legacy due to
372 // the lack of signed variants in the first Dwarf standards.
373 OS << format(" %+" PRId64, int64_t(Operand));
375 case OT_FactoredCodeOffset: // Always Unsigned
376 if (CodeAlignmentFactor)
377 OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
379 OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
381 case OT_SignedFactDataOffset:
382 if (DataAlignmentFactor)
383 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
385 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
387 case OT_UnsignedFactDataOffset:
388 if (DataAlignmentFactor)
389 OS << format(" %" PRId64, Operand * DataAlignmentFactor);
391 OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
394 OS << format(" reg%" PRId64, Operand);
402 void FrameEntry::dumpInstructions(raw_ostream &OS) const {
403 uint64_t CodeAlignmentFactor = 0;
404 int64_t DataAlignmentFactor = 0;
405 const CIE *Cie = dyn_cast<CIE>(this);
408 Cie = cast<FDE>(this)->getLinkedCIE();
410 CodeAlignmentFactor = Cie->getCodeAlignmentFactor();
411 DataAlignmentFactor = Cie->getDataAlignmentFactor();
414 for (const auto &Instr : Instructions) {
415 uint8_t Opcode = Instr.Opcode;
416 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
417 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
418 OS << " " << CallFrameString(Opcode) << ":";
419 for (unsigned i = 0; i < Instr.Ops.size(); ++i)
420 printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor,
421 DataAlignmentFactor);
426 DWARFDebugFrame::DWARFDebugFrame() {
429 DWARFDebugFrame::~DWARFDebugFrame() {
432 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
433 uint32_t Offset, int Length) {
435 for (int i = 0; i < Length; ++i) {
436 uint8_t c = Data.getU8(&Offset);
437 errs().write_hex(c); errs() << " ";
443 void DWARFDebugFrame::parse(DataExtractor Data) {
445 DenseMap<uint32_t, CIE *> CIEs;
447 while (Data.isValidOffset(Offset)) {
448 uint32_t StartOffset = Offset;
450 bool IsDWARF64 = false;
451 uint64_t Length = Data.getU32(&Offset);
454 if (Length == UINT32_MAX) {
455 // DWARF-64 is distinguished by the first 32 bits of the initial length
456 // field being 0xffffffff. Then, the next 64 bits are the actual entry
459 Length = Data.getU64(&Offset);
462 // At this point, Offset points to the next field after Length.
463 // Length is the structure size excluding itself. Compute an offset one
464 // past the end of the structure (needed to know how many instructions to
466 // TODO: For honest DWARF64 support, DataExtractor will have to treat
467 // offset_ptr as uint64_t*
468 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
470 // The Id field's size depends on the DWARF format
471 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
472 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
475 uint8_t Version = Data.getU8(&Offset);
476 const char *Augmentation = Data.getCStr(&Offset);
477 uint8_t AddressSize = Version < 4 ? Data.getAddressSize() : Data.getU8(&Offset);
478 Data.setAddressSize(AddressSize);
479 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
480 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
481 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
482 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
484 auto Cie = make_unique<CIE>(StartOffset, Length, Version,
485 StringRef(Augmentation), AddressSize,
486 SegmentDescriptorSize, CodeAlignmentFactor,
487 DataAlignmentFactor, ReturnAddressRegister);
488 CIEs[StartOffset] = Cie.get();
489 Entries.emplace_back(std::move(Cie));
492 uint64_t CIEPointer = Id;
493 uint64_t InitialLocation = Data.getAddress(&Offset);
494 uint64_t AddressRange = Data.getAddress(&Offset);
496 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
497 InitialLocation, AddressRange,
501 Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset);
503 if (Offset != EndStructureOffset) {
505 raw_string_ostream OS(Str);
506 OS << format("Parsing entry instructions at %lx failed", StartOffset);
507 report_fatal_error(Str);
513 void DWARFDebugFrame::dump(raw_ostream &OS) const {
515 for (const auto &Entry : Entries) {
516 Entry->dumpHeader(OS);
517 Entry->dumpInstructions(OS);