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 "DWARFDebugFrame.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "llvm/Support/Dwarf.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/raw_ostream.h"
21 using namespace dwarf;
24 /// \brief Abstract frame entry defining the common interface concrete
25 /// entries implement.
26 class llvm::FrameEntry {
28 enum FrameKind {FK_CIE, FK_FDE};
29 FrameEntry(FrameKind K, DataExtractor D, uint64_t Offset, uint64_t Length)
30 : Kind(K), Data(D), Offset(Offset), Length(Length) {}
32 virtual ~FrameEntry() {
35 FrameKind getKind() const { return Kind; }
36 virtual uint64_t getOffset() const { return Offset; }
38 /// \brief Parse and store a sequence of CFI instructions from our data
39 /// stream, starting at *Offset and ending at EndOffset. If everything
40 /// goes well, *Offset should be equal to EndOffset when this method
41 /// returns. Otherwise, an error occurred.
42 virtual void parseInstructions(uint32_t *Offset, uint32_t EndOffset);
44 /// \brief Dump the entry header to the given output stream.
45 virtual void dumpHeader(raw_ostream &OS) const = 0;
47 /// \brief Dump the entry's instructions to the given output stream.
48 virtual void dumpInstructions(raw_ostream &OS) const;
53 /// \brief The data stream holding the section from which the entry was
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;
101 void FrameEntry::parseInstructions(uint32_t *Offset, 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 void FrameEntry::dumpInstructions(raw_ostream &OS) const {
187 // TODO: at the moment only instruction names are dumped. Expand this to
188 // dump operands as well.
189 for (std::vector<Instruction>::const_iterator I = Instructions.begin(),
190 E = Instructions.end();
192 uint8_t Opcode = I->Opcode;
193 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
194 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
195 OS << " " << CallFrameString(Opcode) << ":\n";
201 /// \brief DWARF Common Information Entry (CIE)
202 class CIE : public FrameEntry {
204 // CIEs (and FDEs) are simply container classes, so the only sensible way to
205 // create them is by providing the full parsed contents in the constructor.
206 CIE(DataExtractor D, uint64_t Offset, uint64_t Length, uint8_t Version,
207 SmallString<8> Augmentation, uint64_t CodeAlignmentFactor,
208 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
209 : FrameEntry(FK_CIE, D, Offset, Length), Version(Version),
210 Augmentation(Augmentation), CodeAlignmentFactor(CodeAlignmentFactor),
211 DataAlignmentFactor(DataAlignmentFactor),
212 ReturnAddressRegister(ReturnAddressRegister) {}
217 void dumpHeader(raw_ostream &OS) const override {
218 OS << format("%08x %08x %08x CIE",
219 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
221 OS << format(" Version: %d\n", Version);
222 OS << " Augmentation: \"" << Augmentation << "\"\n";
223 OS << format(" Code alignment factor: %u\n",
224 (uint32_t)CodeAlignmentFactor);
225 OS << format(" Data alignment factor: %d\n",
226 (int32_t)DataAlignmentFactor);
227 OS << format(" Return address column: %d\n",
228 (int32_t)ReturnAddressRegister);
232 static bool classof(const FrameEntry *FE) {
233 return FE->getKind() == FK_CIE;
237 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
239 SmallString<8> Augmentation;
240 uint64_t CodeAlignmentFactor;
241 int64_t DataAlignmentFactor;
242 uint64_t ReturnAddressRegister;
246 /// \brief DWARF Frame Description Entry (FDE)
247 class FDE : public FrameEntry {
249 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
250 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
251 // is obtained lazily once it's actually required.
252 FDE(DataExtractor D, uint64_t Offset, uint64_t Length,
253 int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange)
254 : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
255 InitialLocation(InitialLocation), AddressRange(AddressRange),
261 void dumpHeader(raw_ostream &OS) const override {
262 OS << format("%08x %08x %08x FDE ",
263 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
264 OS << format("cie=%08x pc=%08x...%08x\n",
265 (int32_t)LinkedCIEOffset,
266 (uint32_t)InitialLocation,
267 (uint32_t)InitialLocation + (uint32_t)AddressRange);
269 OS << format("%p\n", LinkedCIE);
273 static bool classof(const FrameEntry *FE) {
274 return FE->getKind() == FK_FDE;
278 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
279 uint64_t LinkedCIEOffset;
280 uint64_t InitialLocation;
281 uint64_t AddressRange;
284 } // end anonymous namespace
287 DWARFDebugFrame::DWARFDebugFrame() {
291 DWARFDebugFrame::~DWARFDebugFrame() {
292 for (EntryVector::iterator I = Entries.begin(), E = Entries.end();
299 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
300 uint32_t Offset, int Length) {
302 for (int i = 0; i < Length; ++i) {
303 uint8_t c = Data.getU8(&Offset);
304 errs().write_hex(c); errs() << " ";
310 void DWARFDebugFrame::parse(DataExtractor Data) {
313 while (Data.isValidOffset(Offset)) {
314 uint32_t StartOffset = Offset;
316 bool IsDWARF64 = false;
317 uint64_t Length = Data.getU32(&Offset);
320 if (Length == UINT32_MAX) {
321 // DWARF-64 is distinguished by the first 32 bits of the initial length
322 // field being 0xffffffff. Then, the next 64 bits are the actual entry
325 Length = Data.getU64(&Offset);
328 // At this point, Offset points to the next field after Length.
329 // Length is the structure size excluding itself. Compute an offset one
330 // past the end of the structure (needed to know how many instructions to
332 // TODO: For honest DWARF64 support, DataExtractor will have to treat
333 // offset_ptr as uint64_t*
334 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
336 // The Id field's size depends on the DWARF format
337 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
338 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
340 FrameEntry *Entry = 0;
342 // Note: this is specifically DWARFv3 CIE header structure. It was
343 // changed in DWARFv4. We currently don't support reading DWARFv4
344 // here because LLVM itself does not emit it (and LLDB doesn't
345 // support it either).
346 uint8_t Version = Data.getU8(&Offset);
347 const char *Augmentation = Data.getCStr(&Offset);
348 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
349 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
350 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
352 Entry = new CIE(Data, StartOffset, Length, Version,
353 StringRef(Augmentation), CodeAlignmentFactor,
354 DataAlignmentFactor, ReturnAddressRegister);
357 uint64_t CIEPointer = Id;
358 uint64_t InitialLocation = Data.getAddress(&Offset);
359 uint64_t AddressRange = Data.getAddress(&Offset);
361 Entry = new FDE(Data, StartOffset, Length, CIEPointer,
362 InitialLocation, AddressRange);
365 assert(Entry && "Expected Entry to be populated with CIE or FDE");
366 Entry->parseInstructions(&Offset, EndStructureOffset);
368 if (Offset == EndStructureOffset) {
369 // Entry instrucitons parsed successfully.
370 Entries.push_back(Entry);
373 raw_string_ostream OS(Str);
374 OS << format("Parsing entry instructions at %lx failed",
376 report_fatal_error(Str);
382 void DWARFDebugFrame::dump(raw_ostream &OS) const {
384 for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end();
386 FrameEntry *Entry = *I;
387 Entry->dumpHeader(OS);
388 Entry->dumpInstructions(OS);