blob: 974cecc3868ffe617e75c52d94cc81134544586b [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
Eli Bendersky8a0329e2013-02-06 03:08:02 +000026 virtual ~FrameEntry() {
27 }
28
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000029 FrameKind getKind() const { return Kind; }
30
31 virtual void dumpHeader(raw_ostream &OS) const = 0;
Eli Benderskyba426252013-02-06 00:20:38 +000032
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000033protected:
34 const FrameKind Kind;
Eli Benderskyba426252013-02-06 00:20:38 +000035
36 /// \brief The data stream holding the section from which the entry was
37 /// parsed.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000038 DataExtractor Data;
Eli Benderskyba426252013-02-06 00:20:38 +000039
40 /// \brief Offset of this entry in the section.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000041 uint64_t Offset;
Eli Benderskyba426252013-02-06 00:20:38 +000042
43 /// \brief Entry length as specified in DWARF.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000044 uint64_t Length;
45};
46
47
48class CIE : public FrameEntry {
49public:
50 // CIEs (and FDEs) are simply container classes, so the only sensible way to
51 // create them is by providing the full parsed contents in the constructor.
52 CIE(DataExtractor D, uint64_t Offset, uint64_t Length, uint8_t Version,
53 SmallString<8> Augmentation, uint64_t CodeAlignmentFactor,
54 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
55 : FrameEntry(FK_CIE, D, Offset, Length), Version(Version),
56 Augmentation(Augmentation), CodeAlignmentFactor(CodeAlignmentFactor),
57 DataAlignmentFactor(DataAlignmentFactor),
Eli Benderskyba426252013-02-06 00:20:38 +000058 ReturnAddressRegister(ReturnAddressRegister) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000059
Eli Bendersky8a0329e2013-02-06 03:08:02 +000060 ~CIE() {
61 }
62
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000063 void dumpHeader(raw_ostream &OS) const {
64 OS << format("%08x %08x %08x CIE", Offset, Length, DW_CIE_ID) << "\n";
65 OS << format(" Version: %d\n", Version);
66 OS << " Augmentation: \"" << Augmentation << "\"\n";
67 OS << format(" Code alignment factor: %u\n", CodeAlignmentFactor);
68 OS << format(" Data alignment factor: %d\n", DataAlignmentFactor);
69 OS << format(" Return address column: %d\n", ReturnAddressRegister);
70 OS << "\n";
71 }
72
73 static bool classof(const FrameEntry *FE) {
74 return FE->getKind() == FK_CIE;
75 }
Eli Benderskyba426252013-02-06 00:20:38 +000076
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000077private:
Eli Benderskyba426252013-02-06 00:20:38 +000078 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000079 uint8_t Version;
80 SmallString<8> Augmentation;
81 uint64_t CodeAlignmentFactor;
82 int64_t DataAlignmentFactor;
83 uint64_t ReturnAddressRegister;
84};
85
86
87class FDE : public FrameEntry {
88public:
89 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
90 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
91 // is obtained lazily once it's actually required.
Eli Benderskyba426252013-02-06 00:20:38 +000092 FDE(DataExtractor D, uint64_t Offset, uint64_t Length,
93 int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000094 : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
95 InitialLocation(InitialLocation), AddressRange(AddressRange),
Eli Benderskyba426252013-02-06 00:20:38 +000096 LinkedCIE(NULL) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000097
Eli Bendersky8a0329e2013-02-06 03:08:02 +000098 ~FDE() {
99 }
100
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000101 void dumpHeader(raw_ostream &OS) const {
102 OS << format("%08x %08x %08x FDE ", Offset, Length, LinkedCIEOffset);
103 OS << format("cie=%08x pc=%08x...%08x\n",
104 LinkedCIEOffset, InitialLocation,
105 InitialLocation + AddressRange);
106 OS << "\n";
Eli Benderskyb2ac7c02013-02-06 05:37:46 +0000107 if (LinkedCIE) {
108 OS << format("%p\n", LinkedCIE);
109 }
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000110 }
111
112 static bool classof(const FrameEntry *FE) {
113 return FE->getKind() == FK_FDE;
114 }
115private:
Eli Benderskyba426252013-02-06 00:20:38 +0000116
117 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000118 uint64_t LinkedCIEOffset;
119 uint64_t InitialLocation;
120 uint64_t AddressRange;
121 CIE *LinkedCIE;
122};
123
124
Eli Benderskyba426252013-02-06 00:20:38 +0000125DWARFDebugFrame::DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000126}
127
128
Eli Benderskyba426252013-02-06 00:20:38 +0000129DWARFDebugFrame::~DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000130 for (EntryVector::iterator I = Entries.begin(), E = Entries.end();
131 I != E; ++I) {
132 delete *I;
133 }
134}
135
136
137static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
138 uint32_t Offset, int Length) {
139 errs() << "DUMP: ";
140 for (int i = 0; i < Length; ++i) {
141 uint8_t c = Data.getU8(&Offset);
142 errs().write_hex(c); errs() << " ";
143 }
144 errs() << "\n";
145}
146
147
148void DWARFDebugFrame::parse(DataExtractor Data) {
149 uint32_t Offset = 0;
150
151 while (Data.isValidOffset(Offset)) {
152 uint32_t StartOffset = Offset;
153
154 bool IsDWARF64 = false;
155 uint64_t Length = Data.getU32(&Offset);
156 uint64_t Id;
157
158 if (Length == UINT32_MAX) {
159 // DWARF-64 is distinguished by the first 32 bits of the initial length
160 // field being 0xffffffff. Then, the next 64 bits are the actual entry
161 // length.
162 IsDWARF64 = true;
163 Length = Data.getU64(&Offset);
164 }
165
166 // At this point, Offset points to the next field after Length.
167 // Length is the structure size excluding itself. Compute an offset one
168 // past the end of the structure (needed to know how many instructions to
169 // read).
170 // TODO: For honest DWARF64 support, DataExtractor will have to treat
171 // offset_ptr as uint64_t*
172 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
173
174 // The Id field's size depends on the DWARF format
175 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
176 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
177
178 if (IsCIE) {
179 // Note: this is specifically DWARFv3 CIE header structure. It was
180 // changed in DWARFv4.
181 uint8_t Version = Data.getU8(&Offset);
182 const char *Augmentation = Data.getCStr(&Offset);
183 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
184 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
185 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
186
187 CIE *NewCIE = new CIE(Data, StartOffset, Length, Version,
188 StringRef(Augmentation), CodeAlignmentFactor,
189 DataAlignmentFactor, ReturnAddressRegister);
190 Entries.push_back(NewCIE);
191 } else {
192 // FDE
193 uint64_t CIEPointer = Id;
194 uint64_t InitialLocation = Data.getAddress(&Offset);
195 uint64_t AddressRange = Data.getAddress(&Offset);
196
197 FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer,
198 InitialLocation, AddressRange);
199 Entries.push_back(NewFDE);
200 }
201
202 Offset = EndStructureOffset;
203 }
204}
205
206
207void DWARFDebugFrame::dump(raw_ostream &OS) const {
208 OS << "\n";
209 for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end();
210 I != E; ++I) {
211 (*I)->dumpHeader(OS);
212 }
213}
214