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/Format.h"
17 using namespace dwarf;
20 /// \brief Abstract frame entry defining the common interface concrete
21 /// entries implement.
22 class llvm::FrameEntry {
24 enum FrameKind {FK_CIE, FK_FDE};
25 FrameEntry(FrameKind K, DataExtractor D, uint64_t Offset, uint64_t Length)
26 : Kind(K), Data(D), Offset(Offset), Length(Length) {}
28 virtual ~FrameEntry() {
31 FrameKind getKind() const { return Kind; }
33 virtual void dumpHeader(raw_ostream &OS) const = 0;
38 /// \brief The data stream holding the section from which the entry was
42 /// \brief Offset of this entry in the section.
45 /// \brief Entry length as specified in DWARF.
50 /// \brief DWARF Common Information Entry (CIE)
51 class CIE : public FrameEntry {
53 // CIEs (and FDEs) are simply container classes, so the only sensible way to
54 // create them is by providing the full parsed contents in the constructor.
55 CIE(DataExtractor D, uint64_t Offset, uint64_t Length, uint8_t Version,
56 SmallString<8> Augmentation, uint64_t CodeAlignmentFactor,
57 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
58 : FrameEntry(FK_CIE, D, Offset, Length), Version(Version),
59 Augmentation(Augmentation), CodeAlignmentFactor(CodeAlignmentFactor),
60 DataAlignmentFactor(DataAlignmentFactor),
61 ReturnAddressRegister(ReturnAddressRegister) {}
66 void dumpHeader(raw_ostream &OS) const {
67 OS << format("%08x %08x %08x CIE",
68 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
70 OS << format(" Version: %d\n", Version);
71 OS << " Augmentation: \"" << Augmentation << "\"\n";
72 OS << format(" Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor);
73 OS << format(" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor);
74 OS << format(" Return address column: %d\n", (int32_t)ReturnAddressRegister);
78 static bool classof(const FrameEntry *FE) {
79 return FE->getKind() == FK_CIE;
83 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
85 SmallString<8> Augmentation;
86 uint64_t CodeAlignmentFactor;
87 int64_t DataAlignmentFactor;
88 uint64_t ReturnAddressRegister;
92 /// \brief DWARF Frame Description Entry (FDE)
93 class FDE : public FrameEntry {
95 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
96 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
97 // is obtained lazily once it's actually required.
98 FDE(DataExtractor D, uint64_t Offset, uint64_t Length,
99 int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange)
100 : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
101 InitialLocation(InitialLocation), AddressRange(AddressRange),
107 void dumpHeader(raw_ostream &OS) const {
108 OS << format("%08x %08x %08x FDE ",
109 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
110 OS << format("cie=%08x pc=%08x...%08x\n",
111 (int32_t)LinkedCIEOffset,
112 (uint32_t)InitialLocation,
113 (uint32_t)InitialLocation + (uint32_t)AddressRange);
116 OS << format("%p\n", LinkedCIE);
120 static bool classof(const FrameEntry *FE) {
121 return FE->getKind() == FK_FDE;
125 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
126 uint64_t LinkedCIEOffset;
127 uint64_t InitialLocation;
128 uint64_t AddressRange;
131 } // end anonymous namespace
134 DWARFDebugFrame::DWARFDebugFrame() {
138 DWARFDebugFrame::~DWARFDebugFrame() {
139 for (EntryVector::iterator I = Entries.begin(), E = Entries.end();
146 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
147 uint32_t Offset, int Length) {
149 for (int i = 0; i < Length; ++i) {
150 uint8_t c = Data.getU8(&Offset);
151 errs().write_hex(c); errs() << " ";
157 void DWARFDebugFrame::parse(DataExtractor Data) {
160 while (Data.isValidOffset(Offset)) {
161 uint32_t StartOffset = Offset;
163 bool IsDWARF64 = false;
164 uint64_t Length = Data.getU32(&Offset);
167 if (Length == UINT32_MAX) {
168 // DWARF-64 is distinguished by the first 32 bits of the initial length
169 // field being 0xffffffff. Then, the next 64 bits are the actual entry
172 Length = Data.getU64(&Offset);
175 // At this point, Offset points to the next field after Length.
176 // Length is the structure size excluding itself. Compute an offset one
177 // past the end of the structure (needed to know how many instructions to
179 // TODO: For honest DWARF64 support, DataExtractor will have to treat
180 // offset_ptr as uint64_t*
181 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
183 // The Id field's size depends on the DWARF format
184 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
185 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
188 // Note: this is specifically DWARFv3 CIE header structure. It was
189 // changed in DWARFv4.
190 uint8_t Version = Data.getU8(&Offset);
191 const char *Augmentation = Data.getCStr(&Offset);
192 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
193 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
194 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
196 CIE *NewCIE = new CIE(Data, StartOffset, Length, Version,
197 StringRef(Augmentation), CodeAlignmentFactor,
198 DataAlignmentFactor, ReturnAddressRegister);
199 Entries.push_back(NewCIE);
202 uint64_t CIEPointer = Id;
203 uint64_t InitialLocation = Data.getAddress(&Offset);
204 uint64_t AddressRange = Data.getAddress(&Offset);
206 FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer,
207 InitialLocation, AddressRange);
208 Entries.push_back(NewFDE);
211 Offset = EndStructureOffset;
216 void DWARFDebugFrame::dump(raw_ostream &OS) const {
218 for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end();
220 (*I)->dumpHeader(OS);