blob: 62e485625126f1b10618d9087a388118943c341c [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 {
67 OS << format("%08x %08x %08x CIE", Offset, Length, DW_CIE_ID) << "\n";
68 OS << format(" Version: %d\n", Version);
69 OS << " Augmentation: \"" << Augmentation << "\"\n";
70 OS << format(" Code alignment factor: %u\n", CodeAlignmentFactor);
71 OS << format(" Data alignment factor: %d\n", DataAlignmentFactor);
72 OS << format(" Return address column: %d\n", ReturnAddressRegister);
73 OS << "\n";
74 }
75
76 static bool classof(const FrameEntry *FE) {
77 return FE->getKind() == FK_CIE;
78 }
Eli Benderskyba426252013-02-06 00:20:38 +000079
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000080private:
Eli Benderskyba426252013-02-06 00:20:38 +000081 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000082 uint8_t Version;
83 SmallString<8> Augmentation;
84 uint64_t CodeAlignmentFactor;
85 int64_t DataAlignmentFactor;
86 uint64_t ReturnAddressRegister;
87};
88
89
Eli Bendersky2e402d52013-02-06 16:20:31 +000090/// \brief DWARF Frame Description Entry (FDE)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000091class FDE : public FrameEntry {
92public:
93 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
94 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
95 // is obtained lazily once it's actually required.
Eli Benderskyba426252013-02-06 00:20:38 +000096 FDE(DataExtractor D, uint64_t Offset, uint64_t Length,
97 int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000098 : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
99 InitialLocation(InitialLocation), AddressRange(AddressRange),
Eli Benderskyba426252013-02-06 00:20:38 +0000100 LinkedCIE(NULL) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000101
Eli Bendersky8a0329e2013-02-06 03:08:02 +0000102 ~FDE() {
103 }
104
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000105 void dumpHeader(raw_ostream &OS) const {
106 OS << format("%08x %08x %08x FDE ", Offset, Length, LinkedCIEOffset);
107 OS << format("cie=%08x pc=%08x...%08x\n",
108 LinkedCIEOffset, InitialLocation,
109 InitialLocation + AddressRange);
110 OS << "\n";
Eli Benderskyb2ac7c02013-02-06 05:37:46 +0000111 if (LinkedCIE) {
112 OS << format("%p\n", LinkedCIE);
113 }
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000114 }
115
116 static bool classof(const FrameEntry *FE) {
117 return FE->getKind() == FK_FDE;
118 }
119private:
Eli Benderskyba426252013-02-06 00:20:38 +0000120
121 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000122 uint64_t LinkedCIEOffset;
123 uint64_t InitialLocation;
124 uint64_t AddressRange;
125 CIE *LinkedCIE;
126};
127
128
Eli Benderskyba426252013-02-06 00:20:38 +0000129DWARFDebugFrame::DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000130}
131
132
Eli Benderskyba426252013-02-06 00:20:38 +0000133DWARFDebugFrame::~DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000134 for (EntryVector::iterator I = Entries.begin(), E = Entries.end();
135 I != E; ++I) {
136 delete *I;
137 }
138}
139
140
141static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
142 uint32_t Offset, int Length) {
143 errs() << "DUMP: ";
144 for (int i = 0; i < Length; ++i) {
145 uint8_t c = Data.getU8(&Offset);
146 errs().write_hex(c); errs() << " ";
147 }
148 errs() << "\n";
149}
150
151
152void DWARFDebugFrame::parse(DataExtractor Data) {
153 uint32_t Offset = 0;
154
155 while (Data.isValidOffset(Offset)) {
156 uint32_t StartOffset = Offset;
157
158 bool IsDWARF64 = false;
159 uint64_t Length = Data.getU32(&Offset);
160 uint64_t Id;
161
162 if (Length == UINT32_MAX) {
163 // DWARF-64 is distinguished by the first 32 bits of the initial length
164 // field being 0xffffffff. Then, the next 64 bits are the actual entry
165 // length.
166 IsDWARF64 = true;
167 Length = Data.getU64(&Offset);
168 }
169
170 // At this point, Offset points to the next field after Length.
171 // Length is the structure size excluding itself. Compute an offset one
172 // past the end of the structure (needed to know how many instructions to
173 // read).
174 // TODO: For honest DWARF64 support, DataExtractor will have to treat
175 // offset_ptr as uint64_t*
176 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
177
178 // The Id field's size depends on the DWARF format
179 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
180 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
181
182 if (IsCIE) {
183 // Note: this is specifically DWARFv3 CIE header structure. It was
184 // changed in DWARFv4.
185 uint8_t Version = Data.getU8(&Offset);
186 const char *Augmentation = Data.getCStr(&Offset);
187 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
188 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
189 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
190
191 CIE *NewCIE = new CIE(Data, StartOffset, Length, Version,
192 StringRef(Augmentation), CodeAlignmentFactor,
193 DataAlignmentFactor, ReturnAddressRegister);
194 Entries.push_back(NewCIE);
195 } else {
196 // FDE
197 uint64_t CIEPointer = Id;
198 uint64_t InitialLocation = Data.getAddress(&Offset);
199 uint64_t AddressRange = Data.getAddress(&Offset);
200
201 FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer,
202 InitialLocation, AddressRange);
203 Entries.push_back(NewFDE);
204 }
205
206 Offset = EndStructureOffset;
207 }
208}
209
210
211void DWARFDebugFrame::dump(raw_ostream &OS) const {
212 OS << "\n";
213 for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end();
214 I != E; ++I) {
215 (*I)->dumpHeader(OS);
216 }
217}
218