DWARFDebugFrame.cpp revision 90e01ac0ea5bdc6dd6bccd9c59c3acb04e339666
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===-- DWARFDebugFrame.h - Parsing of .debug_frame -------------*- C++ -*-===//
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// License. See LICENSE.TXT for details.
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===//
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "DWARFDebugFrame.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "llvm/ADT/SmallString.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "llvm/Support/DataTypes.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "llvm/Support/Dwarf.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "llvm/Support/Format.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using namespace llvm;
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using namespace dwarf;
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// \brief Abstract frame entry defining the common interface concrete
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// entries implement.
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class llvm::FrameEntry {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)public:
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  enum FrameKind {FK_CIE, FK_FDE};
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FrameEntry(FrameKind K, DataExtractor D, uint64_t Offset, uint64_t Length)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : Kind(K), Data(D), Offset(Offset), Length(Length) {}
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~FrameEntry() {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FrameKind getKind() const { return Kind; }
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void dumpHeader(raw_ostream &OS) const = 0;
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)protected:
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const FrameKind Kind;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// \brief The data stream holding the section from which the entry was
39  /// parsed.
40  DataExtractor Data;
41
42  /// \brief Offset of this entry in the section.
43  uint64_t Offset;
44
45  /// \brief Entry length as specified in DWARF.
46  uint64_t Length;
47};
48
49
50/// \brief DWARF Common Information Entry (CIE)
51class 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),
61     ReturnAddressRegister(ReturnAddressRegister) {}
62
63  ~CIE() {
64  }
65
66  void dumpHeader(raw_ostream &OS) const {
67    OS << format("%08x %08x %08x CIE",
68                 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
69       << "\n";
70    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  }
81
82private:
83  /// The following fields are defined in section 6.4.1 of the DWARF standard v3
84  uint8_t Version;
85  SmallString<8> Augmentation;
86  uint64_t CodeAlignmentFactor;
87  int64_t DataAlignmentFactor;
88  uint64_t ReturnAddressRegister;
89};
90
91
92/// \brief DWARF Frame Description Entry (FDE)
93class 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.
98  FDE(DataExtractor D, uint64_t Offset, uint64_t Length,
99      int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange)
100   : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
101     InitialLocation(InitialLocation), AddressRange(AddressRange),
102     LinkedCIE(NULL) {}
103
104  ~FDE() {
105  }
106
107  void dumpHeader(raw_ostream &OS) const {
108    OS << format("%08x %08x %08x FDE ",
109                 (uint32_t)Offset, (uint32_t)Length, LinkedCIEOffset);
110    OS << format("cie=%08x pc=%08x...%08x\n",
111                 (uint32_t)LinkedCIEOffset, (uint32_t)InitialLocation,
112                 InitialLocation + AddressRange);
113    OS << "\n";
114    if (LinkedCIE) {
115      OS << format("%p\n", LinkedCIE);
116    }
117  }
118
119  static bool classof(const FrameEntry *FE) {
120    return FE->getKind() == FK_FDE;
121  }
122private:
123
124  /// The following fields are defined in section 6.4.1 of the DWARF standard v3
125  uint64_t LinkedCIEOffset;
126  uint64_t InitialLocation;
127  uint64_t AddressRange;
128  CIE *LinkedCIE;
129};
130
131
132DWARFDebugFrame::DWARFDebugFrame() {
133}
134
135
136DWARFDebugFrame::~DWARFDebugFrame() {
137  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
222