1 //===-- DWARFDebugFrame.h - Parsing of .debug_frame -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
17 using namespace dwarf;
20 class llvm::FrameEntry {
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)
27 FrameKind getKind() const { return Kind; }
29 virtual void dumpHeader(raw_ostream &OS) const = 0;
38 class CIE : public FrameEntry {
40 // CIEs (and FDEs) are simply container classes, so the only sensible way to
41 // create them is by providing the full parsed contents in the constructor.
42 CIE(DataExtractor D, uint64_t Offset, uint64_t Length, uint8_t Version,
43 SmallString<8> Augmentation, uint64_t CodeAlignmentFactor,
44 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
45 : FrameEntry(FK_CIE, D, Offset, Length), Version(Version),
46 Augmentation(Augmentation), CodeAlignmentFactor(CodeAlignmentFactor),
47 DataAlignmentFactor(DataAlignmentFactor),
48 ReturnAddressRegister(ReturnAddressRegister)
51 void dumpHeader(raw_ostream &OS) const {
52 OS << format("%08x %08x %08x CIE", Offset, Length, DW_CIE_ID) << "\n";
53 OS << format(" Version: %d\n", Version);
54 OS << " Augmentation: \"" << Augmentation << "\"\n";
55 OS << format(" Code alignment factor: %u\n", CodeAlignmentFactor);
56 OS << format(" Data alignment factor: %d\n", DataAlignmentFactor);
57 OS << format(" Return address column: %d\n", ReturnAddressRegister);
61 static bool classof(const FrameEntry *FE) {
62 return FE->getKind() == FK_CIE;
66 SmallString<8> Augmentation;
67 uint64_t CodeAlignmentFactor;
68 int64_t DataAlignmentFactor;
69 uint64_t ReturnAddressRegister;
73 class FDE : public FrameEntry {
75 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
76 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
77 // is obtained lazily once it's actually required.
78 FDE(DataExtractor D, uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset,
79 uint64_t InitialLocation, uint64_t AddressRange)
80 : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
81 InitialLocation(InitialLocation), AddressRange(AddressRange),
85 void dumpHeader(raw_ostream &OS) const {
86 OS << format("%08x %08x %08x FDE ", Offset, Length, LinkedCIEOffset);
87 OS << format("cie=%08x pc=%08x...%08x\n",
88 LinkedCIEOffset, InitialLocation,
89 InitialLocation + AddressRange);
93 static bool classof(const FrameEntry *FE) {
94 return FE->getKind() == FK_FDE;
97 uint64_t LinkedCIEOffset;
98 uint64_t InitialLocation;
99 uint64_t AddressRange;
104 DWARFDebugFrame::DWARFDebugFrame()
109 DWARFDebugFrame::~DWARFDebugFrame()
111 for (EntryVector::iterator I = Entries.begin(), E = Entries.end();
118 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
119 uint32_t Offset, int Length) {
121 for (int i = 0; i < Length; ++i) {
122 uint8_t c = Data.getU8(&Offset);
123 errs().write_hex(c); errs() << " ";
129 void DWARFDebugFrame::parse(DataExtractor Data) {
132 while (Data.isValidOffset(Offset)) {
133 uint32_t StartOffset = Offset;
135 bool IsDWARF64 = false;
136 uint64_t Length = Data.getU32(&Offset);
139 if (Length == UINT32_MAX) {
140 // DWARF-64 is distinguished by the first 32 bits of the initial length
141 // field being 0xffffffff. Then, the next 64 bits are the actual entry
144 Length = Data.getU64(&Offset);
147 // At this point, Offset points to the next field after Length.
148 // Length is the structure size excluding itself. Compute an offset one
149 // past the end of the structure (needed to know how many instructions to
151 // TODO: For honest DWARF64 support, DataExtractor will have to treat
152 // offset_ptr as uint64_t*
153 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
155 // The Id field's size depends on the DWARF format
156 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
157 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
160 // Note: this is specifically DWARFv3 CIE header structure. It was
161 // changed in DWARFv4.
162 uint8_t Version = Data.getU8(&Offset);
163 const char *Augmentation = Data.getCStr(&Offset);
164 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
165 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
166 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
168 CIE *NewCIE = new CIE(Data, StartOffset, Length, Version,
169 StringRef(Augmentation), CodeAlignmentFactor,
170 DataAlignmentFactor, ReturnAddressRegister);
171 Entries.push_back(NewCIE);
174 uint64_t CIEPointer = Id;
175 uint64_t InitialLocation = Data.getAddress(&Offset);
176 uint64_t AddressRange = Data.getAddress(&Offset);
178 FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer,
179 InitialLocation, AddressRange);
180 Entries.push_back(NewFDE);
183 Offset = EndStructureOffset;
188 void DWARFDebugFrame::dump(raw_ostream &OS) const {
190 for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end();
192 (*I)->dumpHeader(OS);