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 | |
Eli Bendersky | 2e402d5 | 2013-02-06 16:20:31 +0000 | [diff] [blame] | 20 | /// \brief Abstract frame entry defining the common interface concrete |
| 21 | /// entries implement. |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 22 | class llvm::FrameEntry { |
| 23 | public: |
| 24 | enum FrameKind {FK_CIE, FK_FDE}; |
| 25 | FrameEntry(FrameKind K, DataExtractor D, uint64_t Offset, uint64_t Length) |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 26 | : Kind(K), Data(D), Offset(Offset), Length(Length) {} |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 27 | |
Eli Bendersky | 8a0329e | 2013-02-06 03:08:02 +0000 | [diff] [blame] | 28 | virtual ~FrameEntry() { |
| 29 | } |
| 30 | |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 31 | FrameKind getKind() const { return Kind; } |
| 32 | |
| 33 | virtual void dumpHeader(raw_ostream &OS) const = 0; |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 34 | |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 35 | protected: |
| 36 | const FrameKind Kind; |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 37 | |
| 38 | /// \brief The data stream holding the section from which the entry was |
| 39 | /// parsed. |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 40 | DataExtractor Data; |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 41 | |
| 42 | /// \brief Offset of this entry in the section. |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 43 | uint64_t Offset; |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 44 | |
| 45 | /// \brief Entry length as specified in DWARF. |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 46 | uint64_t Length; |
| 47 | }; |
| 48 | |
| 49 | |
Eli Bendersky | 2e402d5 | 2013-02-06 16:20:31 +0000 | [diff] [blame] | 50 | /// \brief DWARF Common Information Entry (CIE) |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 51 | class CIE : public FrameEntry { |
| 52 | public: |
| 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), |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 61 | ReturnAddressRegister(ReturnAddressRegister) {} |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 62 | |
Eli Bendersky | 8a0329e | 2013-02-06 03:08:02 +0000 | [diff] [blame] | 63 | ~CIE() { |
| 64 | } |
| 65 | |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 66 | void dumpHeader(raw_ostream &OS) const { |
NAKAMURA Takumi | 90e01ac | 2013-02-07 02:02:27 +0000 | [diff] [blame] | 67 | OS << format("%08x %08x %08x CIE", |
| 68 | (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID) |
| 69 | << "\n"; |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 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 | } |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 81 | |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 82 | private: |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 83 | /// The following fields are defined in section 6.4.1 of the DWARF standard v3 |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 84 | uint8_t Version; |
| 85 | SmallString<8> Augmentation; |
| 86 | uint64_t CodeAlignmentFactor; |
| 87 | int64_t DataAlignmentFactor; |
| 88 | uint64_t ReturnAddressRegister; |
| 89 | }; |
| 90 | |
| 91 | |
Eli Bendersky | 2e402d5 | 2013-02-06 16:20:31 +0000 | [diff] [blame] | 92 | /// \brief DWARF Frame Description Entry (FDE) |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 93 | class FDE : public FrameEntry { |
| 94 | public: |
| 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. |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 98 | FDE(DataExtractor D, uint64_t Offset, uint64_t Length, |
| 99 | int64_t LinkedCIEOffset, uint64_t InitialLocation, uint64_t AddressRange) |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 100 | : FrameEntry(FK_FDE, D, Offset, Length), LinkedCIEOffset(LinkedCIEOffset), |
| 101 | InitialLocation(InitialLocation), AddressRange(AddressRange), |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 102 | LinkedCIE(NULL) {} |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 103 | |
Eli Bendersky | 8a0329e | 2013-02-06 03:08:02 +0000 | [diff] [blame] | 104 | ~FDE() { |
| 105 | } |
| 106 | |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 107 | void dumpHeader(raw_ostream &OS) const { |
NAKAMURA Takumi | 90e01ac | 2013-02-07 02:02:27 +0000 | [diff] [blame] | 108 | OS << format("%08x %08x %08x FDE ", |
NAKAMURA Takumi | d9a8d43 | 2013-02-07 14:54:42 +0000 | [diff] [blame^] | 109 | (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset); |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 110 | OS << format("cie=%08x pc=%08x...%08x\n", |
NAKAMURA Takumi | d9a8d43 | 2013-02-07 14:54:42 +0000 | [diff] [blame^] | 111 | (int32_t)LinkedCIEOffset, |
NAKAMURA Takumi | 8ff0631 | 2013-02-07 10:57:42 +0000 | [diff] [blame] | 112 | (uint32_t)InitialLocation, |
| 113 | (uint32_t)InitialLocation + (uint32_t)AddressRange); |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 114 | OS << "\n"; |
Eli Bendersky | b2ac7c0 | 2013-02-06 05:37:46 +0000 | [diff] [blame] | 115 | if (LinkedCIE) { |
| 116 | OS << format("%p\n", LinkedCIE); |
| 117 | } |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static bool classof(const FrameEntry *FE) { |
| 121 | return FE->getKind() == FK_FDE; |
| 122 | } |
| 123 | private: |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 124 | |
| 125 | /// The following fields are defined in section 6.4.1 of the DWARF standard v3 |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 126 | uint64_t LinkedCIEOffset; |
| 127 | uint64_t InitialLocation; |
| 128 | uint64_t AddressRange; |
| 129 | CIE *LinkedCIE; |
| 130 | }; |
| 131 | |
| 132 | |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 133 | DWARFDebugFrame::DWARFDebugFrame() { |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
Eli Bendersky | ba42625 | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 137 | DWARFDebugFrame::~DWARFDebugFrame() { |
Eli Bendersky | 60bdc5b | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 138 | for (EntryVector::iterator I = Entries.begin(), E = Entries.end(); |
| 139 | I != E; ++I) { |
| 140 | delete *I; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, |
| 146 | uint32_t Offset, int Length) { |
| 147 | errs() << "DUMP: "; |
| 148 | for (int i = 0; i < Length; ++i) { |
| 149 | uint8_t c = Data.getU8(&Offset); |
| 150 | errs().write_hex(c); errs() << " "; |
| 151 | } |
| 152 | errs() << "\n"; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | void DWARFDebugFrame::parse(DataExtractor Data) { |
| 157 | uint32_t Offset = 0; |
| 158 | |
| 159 | while (Data.isValidOffset(Offset)) { |
| 160 | uint32_t StartOffset = Offset; |
| 161 | |
| 162 | bool IsDWARF64 = false; |
| 163 | uint64_t Length = Data.getU32(&Offset); |
| 164 | uint64_t Id; |
| 165 | |
| 166 | if (Length == UINT32_MAX) { |
| 167 | // DWARF-64 is distinguished by the first 32 bits of the initial length |
| 168 | // field being 0xffffffff. Then, the next 64 bits are the actual entry |
| 169 | // length. |
| 170 | IsDWARF64 = true; |
| 171 | Length = Data.getU64(&Offset); |
| 172 | } |
| 173 | |
| 174 | // At this point, Offset points to the next field after Length. |
| 175 | // Length is the structure size excluding itself. Compute an offset one |
| 176 | // past the end of the structure (needed to know how many instructions to |
| 177 | // read). |
| 178 | // TODO: For honest DWARF64 support, DataExtractor will have to treat |
| 179 | // offset_ptr as uint64_t* |
| 180 | uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length); |
| 181 | |
| 182 | // The Id field's size depends on the DWARF format |
| 183 | Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4); |
| 184 | bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID); |
| 185 | |
| 186 | if (IsCIE) { |
| 187 | // Note: this is specifically DWARFv3 CIE header structure. It was |
| 188 | // changed in DWARFv4. |
| 189 | uint8_t Version = Data.getU8(&Offset); |
| 190 | const char *Augmentation = Data.getCStr(&Offset); |
| 191 | uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset); |
| 192 | int64_t DataAlignmentFactor = Data.getSLEB128(&Offset); |
| 193 | uint64_t ReturnAddressRegister = Data.getULEB128(&Offset); |
| 194 | |
| 195 | CIE *NewCIE = new CIE(Data, StartOffset, Length, Version, |
| 196 | StringRef(Augmentation), CodeAlignmentFactor, |
| 197 | DataAlignmentFactor, ReturnAddressRegister); |
| 198 | Entries.push_back(NewCIE); |
| 199 | } else { |
| 200 | // FDE |
| 201 | uint64_t CIEPointer = Id; |
| 202 | uint64_t InitialLocation = Data.getAddress(&Offset); |
| 203 | uint64_t AddressRange = Data.getAddress(&Offset); |
| 204 | |
| 205 | FDE *NewFDE = new FDE(Data, StartOffset, Length, CIEPointer, |
| 206 | InitialLocation, AddressRange); |
| 207 | Entries.push_back(NewFDE); |
| 208 | } |
| 209 | |
| 210 | Offset = EndStructureOffset; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void DWARFDebugFrame::dump(raw_ostream &OS) const { |
| 216 | OS << "\n"; |
| 217 | for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end(); |
| 218 | I != E; ++I) { |
| 219 | (*I)->dumpHeader(OS); |
| 220 | } |
| 221 | } |
| 222 | |