Add virtual desctructor to FrameEntry to avoid error on delete-non-virtual-dtor
[oota-llvm.git] / lib / DebugInfo / DWARFDebugFrame.cpp
1 //===-- 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
16 using namespace llvm;
17 using namespace dwarf;
18
19
20 class llvm::FrameEntry {
21 public:
22   enum FrameKind {FK_CIE, FK_FDE};
23   FrameEntry(FrameKind K, DataExtractor D, uint64_t Offset, uint64_t Length)
24     : Kind(K), Data(D), Offset(Offset), Length(Length) {}
25
26   virtual ~FrameEntry() {
27   }
28
29   FrameKind getKind() const { return Kind; }
30
31   virtual void dumpHeader(raw_ostream &OS) const = 0;
32
33 protected:
34   const FrameKind Kind;
35
36   /// \brief The data stream holding the section from which the entry was
37   /// parsed.
38   DataExtractor Data;
39
40   /// \brief Offset of this entry in the section.
41   uint64_t Offset;
42
43   /// \brief Entry length as specified in DWARF.
44   uint64_t Length;
45 };
46
47
48 class CIE : public FrameEntry {
49 public:
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),
58      ReturnAddressRegister(ReturnAddressRegister) {}
59
60   ~CIE() {
61   }
62
63   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   } 
76
77 private:
78   /// The following fields are defined in section 6.4.1 of the DWARF standard v3
79   uint8_t Version;
80   SmallString<8> Augmentation;
81   uint64_t CodeAlignmentFactor;
82   int64_t DataAlignmentFactor;
83   uint64_t ReturnAddressRegister;
84 };
85
86
87 class FDE : public FrameEntry {
88 public:
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.
92   FDE(DataExtractor D, uint64_t Offset, uint64_t Length,
93       int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange)
94    : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
95      InitialLocation(InitialLocation), AddressRange(AddressRange),
96      LinkedCIE(NULL) {}
97
98   ~FDE() {
99   }
100
101   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";
107   }
108
109   static bool classof(const FrameEntry *FE) {
110     return FE->getKind() == FK_FDE;
111   } 
112 private:
113
114   /// The following fields are defined in section 6.4.1 of the DWARF standard v3
115   uint64_t LinkedCIEOffset;
116   uint64_t InitialLocation;
117   uint64_t AddressRange;
118   CIE *LinkedCIE;
119 };
120
121
122 DWARFDebugFrame::DWARFDebugFrame() {
123 }
124
125
126 DWARFDebugFrame::~DWARFDebugFrame() {
127   for (EntryVector::iterator I = Entries.begin(), E = Entries.end();
128        I != E; ++I) {
129     delete *I;
130   }
131 }
132
133
134 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
135                                               uint32_t Offset, int Length) {
136   errs() << "DUMP: ";
137   for (int i = 0; i < Length; ++i) {
138     uint8_t c = Data.getU8(&Offset);
139     errs().write_hex(c); errs() << " ";
140   }
141   errs() << "\n";
142 }
143
144
145 void DWARFDebugFrame::parse(DataExtractor Data) {
146   uint32_t Offset = 0;
147
148   while (Data.isValidOffset(Offset)) {
149     uint32_t StartOffset = Offset;
150
151     bool IsDWARF64 = false;
152     uint64_t Length = Data.getU32(&Offset);
153     uint64_t Id;
154
155     if (Length == UINT32_MAX) {
156       // DWARF-64 is distinguished by the first 32 bits of the initial length
157       // field being 0xffffffff. Then, the next 64 bits are the actual entry
158       // length.
159       IsDWARF64 = true;
160       Length = Data.getU64(&Offset);
161     }
162
163     // At this point, Offset points to the next field after Length.
164     // Length is the structure size excluding itself. Compute an offset one
165     // past the end of the structure (needed to know how many instructions to
166     // read).
167     // TODO: For honest DWARF64 support, DataExtractor will have to treat
168     //       offset_ptr as uint64_t*
169     uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
170
171     // The Id field's size depends on the DWARF format
172     Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
173     bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
174
175     if (IsCIE) {
176       // Note: this is specifically DWARFv3 CIE header structure. It was
177       // changed in DWARFv4.
178       uint8_t Version = Data.getU8(&Offset);
179       const char *Augmentation = Data.getCStr(&Offset);
180       uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
181       int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
182       uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
183
184       CIE *NewCIE = new CIE(Data, StartOffset, Length, Version,
185                             StringRef(Augmentation), CodeAlignmentFactor,
186                             DataAlignmentFactor, ReturnAddressRegister);
187       Entries.push_back(NewCIE);
188     } else {
189       // FDE
190       uint64_t CIEPointer = Id;
191       uint64_t InitialLocation = Data.getAddress(&Offset);
192       uint64_t AddressRange = Data.getAddress(&Offset);
193
194       FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer,
195                             InitialLocation, AddressRange);
196       Entries.push_back(NewFDE);
197     }
198
199     Offset = EndStructureOffset;
200   }
201 }
202
203
204 void DWARFDebugFrame::dump(raw_ostream &OS) const {
205   OS << "\n";
206   for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end();
207        I != E; ++I) {
208     (*I)->dumpHeader(OS);
209   }
210 }
211