blob: 76fdb79e3ef85f8a63671d01278b998644a3207b [file] [log] [blame]
Eli Bendersky60bdc5b2013-02-05 23:30:58 +00001//===-- DWARFDebugFrame.h - Parsing of .debug_frame -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
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"
15
16using namespace llvm;
17using namespace dwarf;
18
19
20class llvm::FrameEntry {
21public:
22 enum FrameKind {FK_CIE, FK_FDE};
23 FrameEntry(FrameKind K, DataExtractor D, uint64_t Offset, uint64_t Length)
Eli Benderskyba426252013-02-06 00:20:38 +000024 : Kind(K), Data(D), Offset(Offset), Length(Length) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000025
26 FrameKind getKind() const { return Kind; }
27
28 virtual void dumpHeader(raw_ostream &OS) const = 0;
Eli Benderskyba426252013-02-06 00:20:38 +000029
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000030protected:
31 const FrameKind Kind;
Eli Benderskyba426252013-02-06 00:20:38 +000032
33 /// \brief The data stream holding the section from which the entry was
34 /// parsed.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000035 DataExtractor Data;
Eli Benderskyba426252013-02-06 00:20:38 +000036
37 /// \brief Offset of this entry in the section.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000038 uint64_t Offset;
Eli Benderskyba426252013-02-06 00:20:38 +000039
40 /// \brief Entry length as specified in DWARF.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000041 uint64_t Length;
42};
43
44
45class CIE : public FrameEntry {
46public:
47 // CIEs (and FDEs) are simply container classes, so the only sensible way to
48 // create them is by providing the full parsed contents in the constructor.
49 CIE(DataExtractor D, uint64_t Offset, uint64_t Length, uint8_t Version,
50 SmallString<8> Augmentation, uint64_t CodeAlignmentFactor,
51 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
52 : FrameEntry(FK_CIE, D, Offset, Length), Version(Version),
53 Augmentation(Augmentation), CodeAlignmentFactor(CodeAlignmentFactor),
54 DataAlignmentFactor(DataAlignmentFactor),
Eli Benderskyba426252013-02-06 00:20:38 +000055 ReturnAddressRegister(ReturnAddressRegister) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000056
57 void dumpHeader(raw_ostream &OS) const {
58 OS << format("%08x %08x %08x CIE", Offset, Length, DW_CIE_ID) << "\n";
59 OS << format(" Version: %d\n", Version);
60 OS << " Augmentation: \"" << Augmentation << "\"\n";
61 OS << format(" Code alignment factor: %u\n", CodeAlignmentFactor);
62 OS << format(" Data alignment factor: %d\n", DataAlignmentFactor);
63 OS << format(" Return address column: %d\n", ReturnAddressRegister);
64 OS << "\n";
65 }
66
67 static bool classof(const FrameEntry *FE) {
68 return FE->getKind() == FK_CIE;
69 }
Eli Benderskyba426252013-02-06 00:20:38 +000070
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000071private:
Eli Benderskyba426252013-02-06 00:20:38 +000072 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000073 uint8_t Version;
74 SmallString<8> Augmentation;
75 uint64_t CodeAlignmentFactor;
76 int64_t DataAlignmentFactor;
77 uint64_t ReturnAddressRegister;
78};
79
80
81class FDE : public FrameEntry {
82public:
83 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
84 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
85 // is obtained lazily once it's actually required.
Eli Benderskyba426252013-02-06 00:20:38 +000086 FDE(DataExtractor D, uint64_t Offset, uint64_t Length,
87 int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000088 : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
89 InitialLocation(InitialLocation), AddressRange(AddressRange),
Eli Benderskyba426252013-02-06 00:20:38 +000090 LinkedCIE(NULL) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000091
92 void dumpHeader(raw_ostream &OS) const {
93 OS << format("%08x %08x %08x FDE ", Offset, Length, LinkedCIEOffset);
94 OS << format("cie=%08x pc=%08x...%08x\n",
95 LinkedCIEOffset, InitialLocation,
96 InitialLocation + AddressRange);
97 OS << "\n";
98 }
99
100 static bool classof(const FrameEntry *FE) {
101 return FE->getKind() == FK_FDE;
102 }
103private:
Eli Benderskyba426252013-02-06 00:20:38 +0000104
105 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000106 uint64_t LinkedCIEOffset;
107 uint64_t InitialLocation;
108 uint64_t AddressRange;
109 CIE *LinkedCIE;
110};
111
112
Eli Benderskyba426252013-02-06 00:20:38 +0000113DWARFDebugFrame::DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000114}
115
116
Eli Benderskyba426252013-02-06 00:20:38 +0000117DWARFDebugFrame::~DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000118 for (EntryVector::iterator I = Entries.begin(), E = Entries.end();
119 I != E; ++I) {
120 delete *I;
121 }
122}
123
124
125static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
126 uint32_t Offset, int Length) {
127 errs() << "DUMP: ";
128 for (int i = 0; i < Length; ++i) {
129 uint8_t c = Data.getU8(&Offset);
130 errs().write_hex(c); errs() << " ";
131 }
132 errs() << "\n";
133}
134
135
136void DWARFDebugFrame::parse(DataExtractor Data) {
137 uint32_t Offset = 0;
138
139 while (Data.isValidOffset(Offset)) {
140 uint32_t StartOffset = Offset;
141
142 bool IsDWARF64 = false;
143 uint64_t Length = Data.getU32(&Offset);
144 uint64_t Id;
145
146 if (Length == UINT32_MAX) {
147 // DWARF-64 is distinguished by the first 32 bits of the initial length
148 // field being 0xffffffff. Then, the next 64 bits are the actual entry
149 // length.
150 IsDWARF64 = true;
151 Length = Data.getU64(&Offset);
152 }
153
154 // At this point, Offset points to the next field after Length.
155 // Length is the structure size excluding itself. Compute an offset one
156 // past the end of the structure (needed to know how many instructions to
157 // read).
158 // TODO: For honest DWARF64 support, DataExtractor will have to treat
159 // offset_ptr as uint64_t*
160 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
161
162 // The Id field's size depends on the DWARF format
163 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
164 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
165
166 if (IsCIE) {
167 // Note: this is specifically DWARFv3 CIE header structure. It was
168 // changed in DWARFv4.
169 uint8_t Version = Data.getU8(&Offset);
170 const char *Augmentation = Data.getCStr(&Offset);
171 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
172 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
173 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
174
175 CIE *NewCIE = new CIE(Data, StartOffset, Length, Version,
176 StringRef(Augmentation), CodeAlignmentFactor,
177 DataAlignmentFactor, ReturnAddressRegister);
178 Entries.push_back(NewCIE);
179 } else {
180 // FDE
181 uint64_t CIEPointer = Id;
182 uint64_t InitialLocation = Data.getAddress(&Offset);
183 uint64_t AddressRange = Data.getAddress(&Offset);
184
185 FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer,
186 InitialLocation, AddressRange);
187 Entries.push_back(NewFDE);
188 }
189
190 Offset = EndStructureOffset;
191 }
192}
193
194
195void DWARFDebugFrame::dump(raw_ostream &OS) const {
196 OS << "\n";
197 for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end();
198 I != E; ++I) {
199 (*I)->dumpHeader(OS);
200 }
201}
202