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, uint64_t CodeAlignmentFactor,
193 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
194 : FrameEntry(FK_CIE, Offset, Length), Version(Version),
195 Augmentation(std::move(Augmentation)),
196 CodeAlignmentFactor(CodeAlignmentFactor),
197 DataAlignmentFactor(DataAlignmentFactor),
198 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),
249 CIE *getLinkedCIE() const { return LinkedCIE; }
251 void dumpHeader(raw_ostream &OS) const override {
252 OS << format("%08x %08x %08x FDE ",
253 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
254 OS << format("cie=%08x pc=%08x...%08x\n",
255 (int32_t)LinkedCIEOffset,
256 (uint32_t)InitialLocation,
257 (uint32_t)InitialLocation + (uint32_t)AddressRange);
260 static bool classof(const FrameEntry *FE) {
261 return FE->getKind() == FK_FDE;
265 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
266 uint64_t LinkedCIEOffset;
267 uint64_t InitialLocation;
268 uint64_t AddressRange;
272 /// \brief Types of operands to CF instructions.
278 OT_FactoredCodeOffset,
279 OT_SignedFactDataOffset,
280 OT_UnsignedFactDataOffset,
285 } // end anonymous namespace
287 /// \brief Initialize the array describing the types of operands.
288 static ArrayRef<OperandType[2]> getOperandTypes() {
289 static OperandType OpTypes[DW_CFA_restore+1][2];
291 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
293 OpTypes[OP][0] = OPTYPE0; \
294 OpTypes[OP][1] = OPTYPE1; \
296 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
297 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
299 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
300 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
301 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
302 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
303 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
304 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
305 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
306 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
307 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
308 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
309 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
310 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
311 DECLARE_OP1(DW_CFA_undefined, OT_Register);
312 DECLARE_OP1(DW_CFA_same_value, OT_Register);
313 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
314 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
315 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
316 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
317 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
318 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
319 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
320 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
321 DECLARE_OP1(DW_CFA_restore, OT_Register);
322 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
323 DECLARE_OP0(DW_CFA_remember_state);
324 DECLARE_OP0(DW_CFA_restore_state);
325 DECLARE_OP0(DW_CFA_GNU_window_save);
326 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
327 DECLARE_OP0(DW_CFA_nop);
332 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
335 static ArrayRef<OperandType[2]> OpTypes = getOperandTypes();
337 /// \brief Print \p Opcode's operand number \p OperandIdx which has
338 /// value \p Operand.
339 static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx,
340 uint64_t Operand, uint64_t CodeAlignmentFactor,
341 int64_t DataAlignmentFactor) {
342 assert(OperandIdx < 2);
343 OperandType Type = OpTypes[Opcode][OperandIdx];
347 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
348 if (const char *OpcodeName = CallFrameString(Opcode))
349 OS << " " << OpcodeName;
351 OS << format(" Opcode %x", Opcode);
356 OS << format(" %" PRIx64, Operand);
359 // The offsets are all encoded in a unsigned form, but in practice
360 // consumers use them signed. It's most certainly legacy due to
361 // the lack of signed variants in the first Dwarf standards.
362 OS << format(" %+" PRId64, int64_t(Operand));
364 case OT_FactoredCodeOffset: // Always Unsigned
365 if (CodeAlignmentFactor)
366 OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
368 OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
370 case OT_SignedFactDataOffset:
371 if (DataAlignmentFactor)
372 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
374 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
376 case OT_UnsignedFactDataOffset:
377 if (DataAlignmentFactor)
378 OS << format(" %" PRId64, Operand * DataAlignmentFactor);
380 OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
383 OS << format(" reg%" PRId64, Operand);
391 void FrameEntry::dumpInstructions(raw_ostream &OS) const {
392 uint64_t CodeAlignmentFactor = 0;
393 int64_t DataAlignmentFactor = 0;
394 const CIE *Cie = dyn_cast<CIE>(this);
397 Cie = cast<FDE>(this)->getLinkedCIE();
399 CodeAlignmentFactor = Cie->getCodeAlignmentFactor();
400 DataAlignmentFactor = Cie->getDataAlignmentFactor();
403 for (const auto &Instr : Instructions) {
404 uint8_t Opcode = Instr.Opcode;
405 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
406 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
407 OS << " " << CallFrameString(Opcode) << ":";
408 for (unsigned i = 0; i < Instr.Ops.size(); ++i)
409 printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor,
410 DataAlignmentFactor);
415 DWARFDebugFrame::DWARFDebugFrame() {
418 DWARFDebugFrame::~DWARFDebugFrame() {
421 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
422 uint32_t Offset, int Length) {
424 for (int i = 0; i < Length; ++i) {
425 uint8_t c = Data.getU8(&Offset);
426 errs().write_hex(c); errs() << " ";
432 void DWARFDebugFrame::parse(DataExtractor Data) {
434 DenseMap<uint32_t, CIE *> CIEs;
436 while (Data.isValidOffset(Offset)) {
437 uint32_t StartOffset = Offset;
439 bool IsDWARF64 = false;
440 uint64_t Length = Data.getU32(&Offset);
443 if (Length == UINT32_MAX) {
444 // DWARF-64 is distinguished by the first 32 bits of the initial length
445 // field being 0xffffffff. Then, the next 64 bits are the actual entry
448 Length = Data.getU64(&Offset);
451 // At this point, Offset points to the next field after Length.
452 // Length is the structure size excluding itself. Compute an offset one
453 // past the end of the structure (needed to know how many instructions to
455 // TODO: For honest DWARF64 support, DataExtractor will have to treat
456 // offset_ptr as uint64_t*
457 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
459 // The Id field's size depends on the DWARF format
460 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
461 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
464 // Note: this is specifically DWARFv3 CIE header structure. It was
465 // changed in DWARFv4. We currently don't support reading DWARFv4
466 // here because LLVM itself does not emit it (and LLDB doesn't
467 // support it either).
468 uint8_t Version = Data.getU8(&Offset);
469 const char *Augmentation = Data.getCStr(&Offset);
470 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
471 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
472 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
474 auto Cie = make_unique<CIE>(StartOffset, Length, Version,
475 StringRef(Augmentation), CodeAlignmentFactor,
476 DataAlignmentFactor, ReturnAddressRegister);
477 CIEs[StartOffset] = Cie.get();
478 Entries.emplace_back(std::move(Cie));
481 uint64_t CIEPointer = Id;
482 uint64_t InitialLocation = Data.getAddress(&Offset);
483 uint64_t AddressRange = Data.getAddress(&Offset);
485 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
486 InitialLocation, AddressRange,
490 Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset);
492 if (Offset != EndStructureOffset) {
494 raw_string_ostream OS(Str);
495 OS << format("Parsing entry instructions at %lx failed", StartOffset);
496 report_fatal_error(Str);
502 void DWARFDebugFrame::dump(raw_ostream &OS) const {
504 for (const auto &Entry : Entries) {
505 Entry->dumpHeader(OS);
506 Entry->dumpInstructions(OS);