Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame^] | 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 | |
| 27 | FrameKind getKind() const { return Kind; } |
| 28 | |
| 29 | virtual void dumpHeader(raw_ostream &OS) const = 0; |
| 30 | protected: |
| 31 | const FrameKind Kind; |
| 32 | DataExtractor Data; |
| 33 | uint64_t Offset; |
| 34 | uint64_t Length; |
| 35 | }; |
| 36 | |
| 37 | |
| 38 | class CIE : public FrameEntry { |
| 39 | public: |
| 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) |
| 49 | {} |
| 50 | |
| 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); |
| 58 | OS << "\n"; |
| 59 | } |
| 60 | |
| 61 | static bool classof(const FrameEntry *FE) { |
| 62 | return FE->getKind() == FK_CIE; |
| 63 | } |
| 64 | private: |
| 65 | uint8_t Version; |
| 66 | SmallString<8> Augmentation; |
| 67 | uint64_t CodeAlignmentFactor; |
| 68 | int64_t DataAlignmentFactor; |
| 69 | uint64_t ReturnAddressRegister; |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | class FDE : public FrameEntry { |
| 74 | public: |
| 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), |
| 82 | LinkedCIE(NULL) |
| 83 | {} |
| 84 | |
| 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); |
| 90 | OS << "\n"; |
| 91 | } |
| 92 | |
| 93 | static bool classof(const FrameEntry *FE) { |
| 94 | return FE->getKind() == FK_FDE; |
| 95 | } |
| 96 | private: |
| 97 | uint64_t LinkedCIEOffset; |
| 98 | uint64_t InitialLocation; |
| 99 | uint64_t AddressRange; |
| 100 | CIE *LinkedCIE; |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | DWARFDebugFrame::DWARFDebugFrame() |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | |
| 109 | DWARFDebugFrame::~DWARFDebugFrame() |
| 110 | { |
| 111 | for (EntryVector::iterator I = Entries.begin(), E = Entries.end(); |
| 112 | I != E; ++I) { |
| 113 | delete *I; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, |
| 119 | uint32_t Offset, int Length) { |
| 120 | errs() << "DUMP: "; |
| 121 | for (int i = 0; i < Length; ++i) { |
| 122 | uint8_t c = Data.getU8(&Offset); |
| 123 | errs().write_hex(c); errs() << " "; |
| 124 | } |
| 125 | errs() << "\n"; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void DWARFDebugFrame::parse(DataExtractor Data) { |
| 130 | uint32_t Offset = 0; |
| 131 | |
| 132 | while (Data.isValidOffset(Offset)) { |
| 133 | uint32_t StartOffset = Offset; |
| 134 | |
| 135 | bool IsDWARF64 = false; |
| 136 | uint64_t Length = Data.getU32(&Offset); |
| 137 | uint64_t Id; |
| 138 | |
| 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 |
| 142 | // length. |
| 143 | IsDWARF64 = true; |
| 144 | Length = Data.getU64(&Offset); |
| 145 | } |
| 146 | |
| 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 |
| 150 | // read). |
| 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); |
| 154 | |
| 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); |
| 158 | |
| 159 | if (IsCIE) { |
| 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); |
| 167 | |
| 168 | CIE *NewCIE = new CIE(Data, StartOffset, Length, Version, |
| 169 | StringRef(Augmentation), CodeAlignmentFactor, |
| 170 | DataAlignmentFactor, ReturnAddressRegister); |
| 171 | Entries.push_back(NewCIE); |
| 172 | } else { |
| 173 | // FDE |
| 174 | uint64_t CIEPointer = Id; |
| 175 | uint64_t InitialLocation = Data.getAddress(&Offset); |
| 176 | uint64_t AddressRange = Data.getAddress(&Offset); |
| 177 | |
| 178 | FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer, |
| 179 | InitialLocation, AddressRange); |
| 180 | Entries.push_back(NewFDE); |
| 181 | } |
| 182 | |
| 183 | Offset = EndStructureOffset; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | void DWARFDebugFrame::dump(raw_ostream &OS) const { |
| 189 | OS << "\n"; |
| 190 | for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end(); |
| 191 | I != E; ++I) { |
| 192 | (*I)->dumpHeader(OS); |
| 193 | } |
| 194 | } |
| 195 | |