blob: 6781da6b1e3398d8aeb41bab5a47c6b3b3af09b2 [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
Eli Bendersky2e402d52013-02-06 16:20:31 +000020/// \brief Abstract frame entry defining the common interface concrete
21/// entries implement.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000022class llvm::FrameEntry {
23public:
24 enum FrameKind {FK_CIE, FK_FDE};
25 FrameEntry(FrameKind K, DataExtractor D, uint64_t Offset, uint64_t Length)
Eli Benderskyba426252013-02-06 00:20:38 +000026 : Kind(K), Data(D), Offset(Offset), Length(Length) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000027
Eli Bendersky8a0329e2013-02-06 03:08:02 +000028 virtual ~FrameEntry() {
29 }
30
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000031 FrameKind getKind() const { return Kind; }
32
33 virtual void dumpHeader(raw_ostream &OS) const = 0;
Eli Benderskyba426252013-02-06 00:20:38 +000034
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000035protected:
36 const FrameKind Kind;
Eli Benderskyba426252013-02-06 00:20:38 +000037
38 /// \brief The data stream holding the section from which the entry was
39 /// parsed.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000040 DataExtractor Data;
Eli Benderskyba426252013-02-06 00:20:38 +000041
42 /// \brief Offset of this entry in the section.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000043 uint64_t Offset;
Eli Benderskyba426252013-02-06 00:20:38 +000044
45 /// \brief Entry length as specified in DWARF.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000046 uint64_t Length;
47};
48
49
Eli Bendersky2e402d52013-02-06 16:20:31 +000050/// \brief DWARF Common Information Entry (CIE)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000051class CIE : public FrameEntry {
52public:
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),
Eli Benderskyba426252013-02-06 00:20:38 +000061 ReturnAddressRegister(ReturnAddressRegister) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000062
Eli Bendersky8a0329e2013-02-06 03:08:02 +000063 ~CIE() {
64 }
65
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000066 void dumpHeader(raw_ostream &OS) const {
NAKAMURA Takumi90e01ac2013-02-07 02:02:27 +000067 OS << format("%08x %08x %08x CIE",
68 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
69 << "\n";
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000070 OS << format(" Version: %d\n", Version);
71 OS << " Augmentation: \"" << Augmentation << "\"\n";
72 OS << format(" Code alignment factor: %u\n", CodeAlignmentFactor);
73 OS << format(" Data alignment factor: %d\n", DataAlignmentFactor);
74 OS << format(" Return address column: %d\n", ReturnAddressRegister);
75 OS << "\n";
76 }
77
78 static bool classof(const FrameEntry *FE) {
79 return FE->getKind() == FK_CIE;
80 }
Eli Benderskyba426252013-02-06 00:20:38 +000081
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000082private:
Eli Benderskyba426252013-02-06 00:20:38 +000083 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000084 uint8_t Version;
85 SmallString<8> Augmentation;
86 uint64_t CodeAlignmentFactor;
87 int64_t DataAlignmentFactor;
88 uint64_t ReturnAddressRegister;
89};
90
91
Eli Bendersky2e402d52013-02-06 16:20:31 +000092/// \brief DWARF Frame Description Entry (FDE)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000093class FDE : public FrameEntry {
94public:
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.
Eli Benderskyba426252013-02-06 00:20:38 +000098 FDE(DataExtractor D, uint64_t Offset, uint64_t Length,
99 int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000100 : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
101 InitialLocation(InitialLocation), AddressRange(AddressRange),
Eli Benderskyba426252013-02-06 00:20:38 +0000102 LinkedCIE(NULL) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000103
Eli Bendersky8a0329e2013-02-06 03:08:02 +0000104 ~FDE() {
105 }
106
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000107 void dumpHeader(raw_ostream &OS) const {
NAKAMURA Takumi90e01ac2013-02-07 02:02:27 +0000108 OS << format("%08x %08x %08x FDE ",
109 (uint32_t)Offset, (uint32_t)Length, LinkedCIEOffset);
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000110 OS << format("cie=%08x pc=%08x...%08x\n",
NAKAMURA Takumi90e01ac2013-02-07 02:02:27 +0000111 (uint32_t)LinkedCIEOffset, (uint32_t)InitialLocation,
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000112 InitialLocation + AddressRange);
113 OS << "\n";
Eli Benderskyb2ac7c02013-02-06 05:37:46 +0000114 if (LinkedCIE) {
115 OS << format("%p\n", LinkedCIE);
116 }
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000117 }
118
119 static bool classof(const FrameEntry *FE) {
120 return FE->getKind() == FK_FDE;
121 }
122private:
Eli Benderskyba426252013-02-06 00:20:38 +0000123
124 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000125 uint64_t LinkedCIEOffset;
126 uint64_t InitialLocation;
127 uint64_t AddressRange;
128 CIE *LinkedCIE;
129};
130
131
Eli Benderskyba426252013-02-06 00:20:38 +0000132DWARFDebugFrame::DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000133}
134
135
Eli Benderskyba426252013-02-06 00:20:38 +0000136DWARFDebugFrame::~DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000137 for (EntryVector::iterator I = Entries.begin(), E = Entries.end();
138 I != E; ++I) {
139 delete *I;
140 }
141}
142
143
144static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
145 uint32_t Offset, int Length) {
146 errs() << "DUMP: ";
147 for (int i = 0; i < Length; ++i) {
148 uint8_t c = Data.getU8(&Offset);
149 errs().write_hex(c); errs() << " ";
150 }
151 errs() << "\n";
152}
153
154
155void DWARFDebugFrame::parse(DataExtractor Data) {
156 uint32_t Offset = 0;
157
158 while (Data.isValidOffset(Offset)) {
159 uint32_t StartOffset = Offset;
160
161 bool IsDWARF64 = false;
162 uint64_t Length = Data.getU32(&Offset);
163 uint64_t Id;
164
165 if (Length == UINT32_MAX) {
166 // DWARF-64 is distinguished by the first 32 bits of the initial length
167 // field being 0xffffffff. Then, the next 64 bits are the actual entry
168 // length.
169 IsDWARF64 = true;
170 Length = Data.getU64(&Offset);
171 }
172
173 // At this point, Offset points to the next field after Length.
174 // Length is the structure size excluding itself. Compute an offset one
175 // past the end of the structure (needed to know how many instructions to
176 // read).
177 // TODO: For honest DWARF64 support, DataExtractor will have to treat
178 // offset_ptr as uint64_t*
179 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
180
181 // The Id field's size depends on the DWARF format
182 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
183 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
184
185 if (IsCIE) {
186 // Note: this is specifically DWARFv3 CIE header structure. It was
187 // changed in DWARFv4.
188 uint8_t Version = Data.getU8(&Offset);
189 const char *Augmentation = Data.getCStr(&Offset);
190 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
191 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
192 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
193
194 CIE *NewCIE = new CIE(Data, StartOffset, Length, Version,
195 StringRef(Augmentation), CodeAlignmentFactor,
196 DataAlignmentFactor, ReturnAddressRegister);
197 Entries.push_back(NewCIE);
198 } else {
199 // FDE
200 uint64_t CIEPointer = Id;
201 uint64_t InitialLocation = Data.getAddress(&Offset);
202 uint64_t AddressRange = Data.getAddress(&Offset);
203
204 FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer,
205 InitialLocation, AddressRange);
206 Entries.push_back(NewFDE);
207 }
208
209 Offset = EndStructureOffset;
210 }
211}
212
213
214void DWARFDebugFrame::dump(raw_ostream &OS) const {
215 OS << "\n";
216 for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end();
217 I != E; ++I) {
218 (*I)->dumpHeader(OS);
219 }
220}
221