Eli Bendersky | fd08bc1 | 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 | |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/ArrayRef.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/DenseMap.h" |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Optional.h" |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Casting.h" |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 16 | #include "llvm/Support/DataTypes.h" |
| 17 | #include "llvm/Support/Dwarf.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include <string> |
| 22 | #include <vector> |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace dwarf; |
| 26 | |
| 27 | |
Eli Bendersky | 5823206 | 2013-02-06 16:20:31 +0000 | [diff] [blame] | 28 | /// \brief Abstract frame entry defining the common interface concrete |
| 29 | /// entries implement. |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 30 | class llvm::FrameEntry { |
| 31 | public: |
| 32 | enum FrameKind {FK_CIE, FK_FDE}; |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 33 | FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length) |
| 34 | : Kind(K), Offset(Offset), Length(Length) {} |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 35 | |
Eli Bendersky | 0b04cb0 | 2013-02-06 03:08:02 +0000 | [diff] [blame] | 36 | virtual ~FrameEntry() { |
| 37 | } |
| 38 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 39 | FrameKind getKind() const { return Kind; } |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 40 | virtual uint64_t getOffset() const { return Offset; } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 41 | |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 42 | /// \brief Parse and store a sequence of CFI instructions from Data, |
| 43 | /// starting at *Offset and ending at EndOffset. If everything |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 44 | /// goes well, *Offset should be equal to EndOffset when this method |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 45 | /// returns. Otherwise, an error occurred. |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 46 | virtual void parseInstructions(DataExtractor Data, uint32_t *Offset, |
| 47 | uint32_t EndOffset); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 48 | |
| 49 | /// \brief Dump the entry header to the given output stream. |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 50 | virtual void dumpHeader(raw_ostream &OS) const = 0; |
Eli Bendersky | 1aa265d | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 51 | |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 52 | /// \brief Dump the entry's instructions to the given output stream. |
| 53 | virtual void dumpInstructions(raw_ostream &OS) const; |
| 54 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 55 | protected: |
| 56 | const FrameKind Kind; |
Eli Bendersky | 1aa265d | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 57 | |
Eli Bendersky | 1aa265d | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 58 | /// \brief Offset of this entry in the section. |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 59 | uint64_t Offset; |
Eli Bendersky | 1aa265d | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 60 | |
| 61 | /// \brief Entry length as specified in DWARF. |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 62 | uint64_t Length; |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 63 | |
| 64 | /// An entry may contain CFI instructions. An instruction consists of an |
| 65 | /// opcode and an optional sequence of operands. |
| 66 | typedef std::vector<uint64_t> Operands; |
| 67 | struct Instruction { |
| 68 | Instruction(uint8_t Opcode) |
| 69 | : Opcode(Opcode) |
| 70 | {} |
| 71 | |
| 72 | uint8_t Opcode; |
| 73 | Operands Ops; |
| 74 | }; |
| 75 | |
| 76 | std::vector<Instruction> Instructions; |
| 77 | |
| 78 | /// Convenience methods to add a new instruction with the given opcode and |
| 79 | /// operands to the Instructions vector. |
| 80 | void addInstruction(uint8_t Opcode) { |
| 81 | Instructions.push_back(Instruction(Opcode)); |
| 82 | } |
| 83 | |
| 84 | void addInstruction(uint8_t Opcode, uint64_t Operand1) { |
| 85 | Instructions.push_back(Instruction(Opcode)); |
| 86 | Instructions.back().Ops.push_back(Operand1); |
| 87 | } |
| 88 | |
| 89 | void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) { |
| 90 | Instructions.push_back(Instruction(Opcode)); |
| 91 | Instructions.back().Ops.push_back(Operand1); |
| 92 | Instructions.back().Ops.push_back(Operand2); |
| 93 | } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 96 | |
| 97 | // See DWARF standard v3, section 7.23 |
| 98 | const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0; |
| 99 | const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f; |
| 100 | |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 101 | void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset, |
| 102 | uint32_t EndOffset) { |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 103 | while (*Offset < EndOffset) { |
| 104 | uint8_t Opcode = Data.getU8(Offset); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 105 | // Some instructions have a primary opcode encoded in the top bits. |
| 106 | uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK; |
| 107 | |
| 108 | if (Primary) { |
| 109 | // If it's a primary opcode, the first operand is encoded in the bottom |
| 110 | // bits of the opcode itself. |
| 111 | uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK; |
| 112 | switch (Primary) { |
| 113 | default: llvm_unreachable("Impossible primary CFI opcode"); |
| 114 | case DW_CFA_advance_loc: |
| 115 | case DW_CFA_restore: |
| 116 | addInstruction(Primary, Op1); |
| 117 | break; |
| 118 | case DW_CFA_offset: |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 119 | addInstruction(Primary, Op1, Data.getULEB128(Offset)); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | } else { |
| 123 | // Extended opcode - its value is Opcode itself. |
| 124 | switch (Opcode) { |
| 125 | default: llvm_unreachable("Invalid extended CFI opcode"); |
| 126 | case DW_CFA_nop: |
| 127 | case DW_CFA_remember_state: |
| 128 | case DW_CFA_restore_state: |
Venkatraman Govindaraju | a302d29 | 2014-01-26 05:13:44 +0000 | [diff] [blame] | 129 | case DW_CFA_GNU_window_save: |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 130 | // No operands |
| 131 | addInstruction(Opcode); |
| 132 | break; |
| 133 | case DW_CFA_set_loc: |
| 134 | // Operands: Address |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 135 | addInstruction(Opcode, Data.getAddress(Offset)); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 136 | break; |
| 137 | case DW_CFA_advance_loc1: |
| 138 | // Operands: 1-byte delta |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 139 | addInstruction(Opcode, Data.getU8(Offset)); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 140 | break; |
| 141 | case DW_CFA_advance_loc2: |
| 142 | // Operands: 2-byte delta |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 143 | addInstruction(Opcode, Data.getU16(Offset)); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 144 | break; |
| 145 | case DW_CFA_advance_loc4: |
| 146 | // Operands: 4-byte delta |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 147 | addInstruction(Opcode, Data.getU32(Offset)); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 148 | break; |
| 149 | case DW_CFA_restore_extended: |
| 150 | case DW_CFA_undefined: |
| 151 | case DW_CFA_same_value: |
| 152 | case DW_CFA_def_cfa_register: |
| 153 | case DW_CFA_def_cfa_offset: |
| 154 | // Operands: ULEB128 |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 155 | addInstruction(Opcode, Data.getULEB128(Offset)); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 156 | break; |
| 157 | case DW_CFA_def_cfa_offset_sf: |
| 158 | // Operands: SLEB128 |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 159 | addInstruction(Opcode, Data.getSLEB128(Offset)); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 160 | break; |
| 161 | case DW_CFA_offset_extended: |
| 162 | case DW_CFA_register: |
| 163 | case DW_CFA_def_cfa: |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 164 | case DW_CFA_val_offset: { |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 165 | // Operands: ULEB128, ULEB128 |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 166 | // Note: We can not embed getULEB128 directly into function |
| 167 | // argument list. getULEB128 changes Offset and order of evaluation |
| 168 | // for arguments is unspecified. |
| 169 | auto op1 = Data.getULEB128(Offset); |
| 170 | auto op2 = Data.getULEB128(Offset); |
| 171 | addInstruction(Opcode, op1, op2); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 172 | break; |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 173 | } |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 174 | case DW_CFA_offset_extended_sf: |
| 175 | case DW_CFA_def_cfa_sf: |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 176 | case DW_CFA_val_offset_sf: { |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 177 | // Operands: ULEB128, SLEB128 |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 178 | // Note: see comment for the previous case |
| 179 | auto op1 = Data.getULEB128(Offset); |
| 180 | auto op2 = (uint64_t)Data.getSLEB128(Offset); |
| 181 | addInstruction(Opcode, op1, op2); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 182 | break; |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 183 | } |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 184 | case DW_CFA_def_cfa_expression: |
| 185 | case DW_CFA_expression: |
| 186 | case DW_CFA_val_expression: |
| 187 | // TODO: implement this |
| 188 | report_fatal_error("Values with expressions not implemented yet!"); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Benjamin Kramer | 6ecb1e7 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 194 | namespace { |
Eli Bendersky | 5823206 | 2013-02-06 16:20:31 +0000 | [diff] [blame] | 195 | /// \brief DWARF Common Information Entry (CIE) |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 196 | class CIE : public FrameEntry { |
| 197 | public: |
| 198 | // CIEs (and FDEs) are simply container classes, so the only sensible way to |
| 199 | // create them is by providing the full parsed contents in the constructor. |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 200 | CIE(uint64_t Offset, uint64_t Length, uint8_t Version, |
Keith Walker | ea9483f | 2015-05-12 15:25:08 +0000 | [diff] [blame] | 201 | SmallString<8> Augmentation, uint8_t AddressSize, |
| 202 | uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor, |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 203 | int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister, |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 204 | SmallString<8> AugmentationData, uint32_t FDEPointerEncoding, |
| 205 | uint32_t LSDAPointerEncoding) |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 206 | : FrameEntry(FK_CIE, Offset, Length), Version(Version), |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 207 | Augmentation(std::move(Augmentation)), |
Keith Walker | ea9483f | 2015-05-12 15:25:08 +0000 | [diff] [blame] | 208 | AddressSize(AddressSize), |
| 209 | SegmentDescriptorSize(SegmentDescriptorSize), |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 210 | CodeAlignmentFactor(CodeAlignmentFactor), |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 211 | DataAlignmentFactor(DataAlignmentFactor), |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 212 | ReturnAddressRegister(ReturnAddressRegister), |
| 213 | AugmentationData(AugmentationData), |
| 214 | FDEPointerEncoding(FDEPointerEncoding), |
| 215 | LSDAPointerEncoding(LSDAPointerEncoding) { } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 216 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 217 | ~CIE() override {} |
Eli Bendersky | 0b04cb0 | 2013-02-06 03:08:02 +0000 | [diff] [blame] | 218 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 219 | StringRef getAugmentationString() const { return Augmentation; } |
Frederic Riss | 41bb2c6 | 2015-02-25 21:30:13 +0000 | [diff] [blame] | 220 | uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; } |
| 221 | int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; } |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 222 | uint32_t getFDEPointerEncoding() const { |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 223 | return FDEPointerEncoding; |
| 224 | } |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 225 | uint32_t getLSDAPointerEncoding() const { |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 226 | return LSDAPointerEncoding; |
| 227 | } |
Frederic Riss | 41bb2c6 | 2015-02-25 21:30:13 +0000 | [diff] [blame] | 228 | |
Craig Topper | 8548299 | 2014-03-05 07:52:44 +0000 | [diff] [blame] | 229 | void dumpHeader(raw_ostream &OS) const override { |
NAKAMURA Takumi | 56ac51a | 2013-02-07 02:02:27 +0000 | [diff] [blame] | 230 | OS << format("%08x %08x %08x CIE", |
| 231 | (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID) |
| 232 | << "\n"; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 233 | OS << format(" Version: %d\n", Version); |
| 234 | OS << " Augmentation: \"" << Augmentation << "\"\n"; |
Keith Walker | ea9483f | 2015-05-12 15:25:08 +0000 | [diff] [blame] | 235 | if (Version >= 4) { |
| 236 | OS << format(" Address size: %u\n", |
| 237 | (uint32_t)AddressSize); |
| 238 | OS << format(" Segment desc size: %u\n", |
| 239 | (uint32_t)SegmentDescriptorSize); |
| 240 | } |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 241 | OS << format(" Code alignment factor: %u\n", |
| 242 | (uint32_t)CodeAlignmentFactor); |
| 243 | OS << format(" Data alignment factor: %d\n", |
| 244 | (int32_t)DataAlignmentFactor); |
| 245 | OS << format(" Return address column: %d\n", |
| 246 | (int32_t)ReturnAddressRegister); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 247 | if (!AugmentationData.empty()) |
| 248 | OS << " Augmentation data: " << AugmentationData << "\n"; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 249 | OS << "\n"; |
| 250 | } |
| 251 | |
| 252 | static bool classof(const FrameEntry *FE) { |
| 253 | return FE->getKind() == FK_CIE; |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 254 | } |
Eli Bendersky | 1aa265d | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 255 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 256 | private: |
Keith Walker | ea9483f | 2015-05-12 15:25:08 +0000 | [diff] [blame] | 257 | /// The following fields are defined in section 6.4.1 of the DWARF standard v4 |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 258 | uint8_t Version; |
| 259 | SmallString<8> Augmentation; |
Keith Walker | ea9483f | 2015-05-12 15:25:08 +0000 | [diff] [blame] | 260 | uint8_t AddressSize; |
| 261 | uint8_t SegmentDescriptorSize; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 262 | uint64_t CodeAlignmentFactor; |
| 263 | int64_t DataAlignmentFactor; |
| 264 | uint64_t ReturnAddressRegister; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 265 | |
| 266 | // The following are used when the CIE represents an EH frame entry. |
| 267 | SmallString<8> AugmentationData; |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 268 | uint32_t FDEPointerEncoding; |
| 269 | uint32_t LSDAPointerEncoding; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | |
Eli Bendersky | 5823206 | 2013-02-06 16:20:31 +0000 | [diff] [blame] | 273 | /// \brief DWARF Frame Description Entry (FDE) |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 274 | class FDE : public FrameEntry { |
| 275 | public: |
| 276 | // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with |
| 277 | // an offset to the CIE (provided by parsing the FDE header). The CIE itself |
| 278 | // is obtained lazily once it's actually required. |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 279 | FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset, |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 280 | uint64_t InitialLocation, uint64_t AddressRange, |
| 281 | CIE *Cie) |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 282 | : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset), |
| 283 | InitialLocation(InitialLocation), AddressRange(AddressRange), |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 284 | LinkedCIE(Cie) {} |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 285 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 286 | ~FDE() override {} |
Eli Bendersky | 0b04cb0 | 2013-02-06 03:08:02 +0000 | [diff] [blame] | 287 | |
Frederic Riss | 41bb2c6 | 2015-02-25 21:30:13 +0000 | [diff] [blame] | 288 | CIE *getLinkedCIE() const { return LinkedCIE; } |
| 289 | |
Craig Topper | 8548299 | 2014-03-05 07:52:44 +0000 | [diff] [blame] | 290 | void dumpHeader(raw_ostream &OS) const override { |
NAKAMURA Takumi | 56ac51a | 2013-02-07 02:02:27 +0000 | [diff] [blame] | 291 | OS << format("%08x %08x %08x FDE ", |
NAKAMURA Takumi | 14727d7 | 2013-02-07 14:54:42 +0000 | [diff] [blame] | 292 | (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 293 | OS << format("cie=%08x pc=%08x...%08x\n", |
NAKAMURA Takumi | 14727d7 | 2013-02-07 14:54:42 +0000 | [diff] [blame] | 294 | (int32_t)LinkedCIEOffset, |
NAKAMURA Takumi | 94651f9 | 2013-02-07 10:57:42 +0000 | [diff] [blame] | 295 | (uint32_t)InitialLocation, |
| 296 | (uint32_t)InitialLocation + (uint32_t)AddressRange); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static bool classof(const FrameEntry *FE) { |
| 300 | return FE->getKind() == FK_FDE; |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 301 | } |
Eli Bendersky | 1aa265d | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 302 | |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 303 | private: |
Eli Bendersky | 1aa265d | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 304 | /// The following fields are defined in section 6.4.1 of the DWARF standard v3 |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 305 | uint64_t LinkedCIEOffset; |
| 306 | uint64_t InitialLocation; |
| 307 | uint64_t AddressRange; |
| 308 | CIE *LinkedCIE; |
| 309 | }; |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 310 | |
| 311 | /// \brief Types of operands to CF instructions. |
| 312 | enum OperandType { |
| 313 | OT_Unset, |
| 314 | OT_None, |
| 315 | OT_Address, |
| 316 | OT_Offset, |
| 317 | OT_FactoredCodeOffset, |
| 318 | OT_SignedFactDataOffset, |
| 319 | OT_UnsignedFactDataOffset, |
| 320 | OT_Register, |
| 321 | OT_Expression |
| 322 | }; |
| 323 | |
Benjamin Kramer | 6ecb1e7 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 324 | } // end anonymous namespace |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 325 | |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 326 | /// \brief Initialize the array describing the types of operands. |
| 327 | static ArrayRef<OperandType[2]> getOperandTypes() { |
| 328 | static OperandType OpTypes[DW_CFA_restore+1][2]; |
| 329 | |
| 330 | #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \ |
| 331 | do { \ |
| 332 | OpTypes[OP][0] = OPTYPE0; \ |
| 333 | OpTypes[OP][1] = OPTYPE1; \ |
| 334 | } while (0) |
| 335 | #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None) |
| 336 | #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None) |
| 337 | |
| 338 | DECLARE_OP1(DW_CFA_set_loc, OT_Address); |
| 339 | DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset); |
| 340 | DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset); |
| 341 | DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset); |
| 342 | DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset); |
| 343 | DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset); |
| 344 | DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset); |
| 345 | DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset); |
| 346 | DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register); |
| 347 | DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset); |
| 348 | DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset); |
| 349 | DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression); |
| 350 | DECLARE_OP1(DW_CFA_undefined, OT_Register); |
| 351 | DECLARE_OP1(DW_CFA_same_value, OT_Register); |
| 352 | DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset); |
| 353 | DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset); |
| 354 | DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset); |
| 355 | DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset); |
| 356 | DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset); |
| 357 | DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register); |
| 358 | DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression); |
| 359 | DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression); |
| 360 | DECLARE_OP1(DW_CFA_restore, OT_Register); |
| 361 | DECLARE_OP1(DW_CFA_restore_extended, OT_Register); |
| 362 | DECLARE_OP0(DW_CFA_remember_state); |
| 363 | DECLARE_OP0(DW_CFA_restore_state); |
| 364 | DECLARE_OP0(DW_CFA_GNU_window_save); |
| 365 | DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset); |
| 366 | DECLARE_OP0(DW_CFA_nop); |
| 367 | |
| 368 | #undef DECLARE_OP0 |
| 369 | #undef DECLARE_OP1 |
| 370 | #undef DECLARE_OP2 |
Frederic Riss | ac10b0d | 2015-02-25 22:07:43 +0000 | [diff] [blame] | 371 | return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1); |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static ArrayRef<OperandType[2]> OpTypes = getOperandTypes(); |
| 375 | |
| 376 | /// \brief Print \p Opcode's operand number \p OperandIdx which has |
| 377 | /// value \p Operand. |
| 378 | static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx, |
| 379 | uint64_t Operand, uint64_t CodeAlignmentFactor, |
| 380 | int64_t DataAlignmentFactor) { |
| 381 | assert(OperandIdx < 2); |
| 382 | OperandType Type = OpTypes[Opcode][OperandIdx]; |
| 383 | |
| 384 | switch (Type) { |
| 385 | case OT_Unset: |
| 386 | OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to"; |
| 387 | if (const char *OpcodeName = CallFrameString(Opcode)) |
| 388 | OS << " " << OpcodeName; |
| 389 | else |
| 390 | OS << format(" Opcode %x", Opcode); |
| 391 | break; |
| 392 | case OT_None: |
| 393 | break; |
| 394 | case OT_Address: |
| 395 | OS << format(" %" PRIx64, Operand); |
| 396 | break; |
| 397 | case OT_Offset: |
| 398 | // The offsets are all encoded in a unsigned form, but in practice |
| 399 | // consumers use them signed. It's most certainly legacy due to |
| 400 | // the lack of signed variants in the first Dwarf standards. |
| 401 | OS << format(" %+" PRId64, int64_t(Operand)); |
| 402 | break; |
| 403 | case OT_FactoredCodeOffset: // Always Unsigned |
| 404 | if (CodeAlignmentFactor) |
| 405 | OS << format(" %" PRId64, Operand * CodeAlignmentFactor); |
| 406 | else |
| 407 | OS << format(" %" PRId64 "*code_alignment_factor" , Operand); |
| 408 | break; |
| 409 | case OT_SignedFactDataOffset: |
| 410 | if (DataAlignmentFactor) |
| 411 | OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor); |
| 412 | else |
| 413 | OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand)); |
| 414 | break; |
| 415 | case OT_UnsignedFactDataOffset: |
| 416 | if (DataAlignmentFactor) |
| 417 | OS << format(" %" PRId64, Operand * DataAlignmentFactor); |
| 418 | else |
| 419 | OS << format(" %" PRId64 "*data_alignment_factor" , Operand); |
| 420 | break; |
| 421 | case OT_Register: |
Frederic Riss | de37434 | 2015-02-25 22:30:09 +0000 | [diff] [blame] | 422 | OS << format(" reg%" PRId64, Operand); |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 423 | break; |
| 424 | case OT_Expression: |
| 425 | OS << " expression"; |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
Frederic Riss | 056ad05 | 2015-02-25 21:30:16 +0000 | [diff] [blame] | 430 | void FrameEntry::dumpInstructions(raw_ostream &OS) const { |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 431 | uint64_t CodeAlignmentFactor = 0; |
| 432 | int64_t DataAlignmentFactor = 0; |
| 433 | const CIE *Cie = dyn_cast<CIE>(this); |
| 434 | |
| 435 | if (!Cie) |
| 436 | Cie = cast<FDE>(this)->getLinkedCIE(); |
| 437 | if (Cie) { |
| 438 | CodeAlignmentFactor = Cie->getCodeAlignmentFactor(); |
| 439 | DataAlignmentFactor = Cie->getDataAlignmentFactor(); |
| 440 | } |
| 441 | |
Frederic Riss | 056ad05 | 2015-02-25 21:30:16 +0000 | [diff] [blame] | 442 | for (const auto &Instr : Instructions) { |
| 443 | uint8_t Opcode = Instr.Opcode; |
| 444 | if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK) |
| 445 | Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK; |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 446 | OS << " " << CallFrameString(Opcode) << ":"; |
| 447 | for (unsigned i = 0; i < Instr.Ops.size(); ++i) |
| 448 | printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor, |
| 449 | DataAlignmentFactor); |
| 450 | OS << '\n'; |
Frederic Riss | 056ad05 | 2015-02-25 21:30:16 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 453 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 454 | DWARFDebugFrame::DWARFDebugFrame(bool IsEH) : IsEH(IsEH) { |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Eli Bendersky | 1aa265d | 2013-02-06 00:20:38 +0000 | [diff] [blame] | 457 | DWARFDebugFrame::~DWARFDebugFrame() { |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 460 | static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, |
| 461 | uint32_t Offset, int Length) { |
| 462 | errs() << "DUMP: "; |
| 463 | for (int i = 0; i < Length; ++i) { |
| 464 | uint8_t c = Data.getU8(&Offset); |
| 465 | errs().write_hex(c); errs() << " "; |
| 466 | } |
| 467 | errs() << "\n"; |
| 468 | } |
| 469 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 470 | static unsigned getSizeForEncoding(const DataExtractor &Data, |
| 471 | unsigned symbolEncoding) { |
| 472 | unsigned format = symbolEncoding & 0x0f; |
| 473 | switch (format) { |
| 474 | default: llvm_unreachable("Unknown Encoding"); |
| 475 | case dwarf::DW_EH_PE_absptr: |
| 476 | case dwarf::DW_EH_PE_signed: |
| 477 | return Data.getAddressSize(); |
| 478 | case dwarf::DW_EH_PE_udata2: |
| 479 | case dwarf::DW_EH_PE_sdata2: |
| 480 | return 2; |
| 481 | case dwarf::DW_EH_PE_udata4: |
| 482 | case dwarf::DW_EH_PE_sdata4: |
| 483 | return 4; |
| 484 | case dwarf::DW_EH_PE_udata8: |
| 485 | case dwarf::DW_EH_PE_sdata8: |
| 486 | return 8; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | static uint64_t readPointer(const DataExtractor &Data, uint32_t &Offset, |
| 491 | unsigned Encoding) { |
| 492 | switch (getSizeForEncoding(Data, Encoding)) { |
| 493 | case 2: |
| 494 | return Data.getU16(&Offset); |
| 495 | case 4: |
| 496 | return Data.getU32(&Offset); |
| 497 | case 8: |
| 498 | return Data.getU64(&Offset); |
| 499 | default: |
| 500 | llvm_unreachable("Illegal data size"); |
| 501 | } |
| 502 | } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 503 | |
| 504 | void DWARFDebugFrame::parse(DataExtractor Data) { |
| 505 | uint32_t Offset = 0; |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 506 | DenseMap<uint32_t, CIE *> CIEs; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 507 | |
| 508 | while (Data.isValidOffset(Offset)) { |
| 509 | uint32_t StartOffset = Offset; |
| 510 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 511 | auto ReportError = [StartOffset](const char *ErrorMsg) { |
| 512 | std::string Str; |
| 513 | raw_string_ostream OS(Str); |
| 514 | OS << format(ErrorMsg, StartOffset); |
| 515 | OS.flush(); |
| 516 | report_fatal_error(Str); |
| 517 | }; |
| 518 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 519 | bool IsDWARF64 = false; |
| 520 | uint64_t Length = Data.getU32(&Offset); |
| 521 | uint64_t Id; |
| 522 | |
| 523 | if (Length == UINT32_MAX) { |
| 524 | // DWARF-64 is distinguished by the first 32 bits of the initial length |
| 525 | // field being 0xffffffff. Then, the next 64 bits are the actual entry |
| 526 | // length. |
| 527 | IsDWARF64 = true; |
| 528 | Length = Data.getU64(&Offset); |
| 529 | } |
| 530 | |
| 531 | // At this point, Offset points to the next field after Length. |
| 532 | // Length is the structure size excluding itself. Compute an offset one |
| 533 | // past the end of the structure (needed to know how many instructions to |
| 534 | // read). |
| 535 | // TODO: For honest DWARF64 support, DataExtractor will have to treat |
| 536 | // offset_ptr as uint64_t* |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 537 | uint32_t StartStructureOffset = Offset; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 538 | uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length); |
| 539 | |
| 540 | // The Id field's size depends on the DWARF format |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 541 | Id = Data.getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4); |
| 542 | bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || |
| 543 | Id == DW_CIE_ID || |
| 544 | (IsEH && !Id)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 545 | |
| 546 | if (IsCIE) { |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 547 | uint8_t Version = Data.getU8(&Offset); |
| 548 | const char *Augmentation = Data.getCStr(&Offset); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 549 | StringRef AugmentationString(Augmentation ? Augmentation : ""); |
| 550 | uint8_t AddressSize = Version < 4 ? Data.getAddressSize() : |
| 551 | Data.getU8(&Offset); |
Keith Walker | ea9483f | 2015-05-12 15:25:08 +0000 | [diff] [blame] | 552 | Data.setAddressSize(AddressSize); |
| 553 | uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 554 | uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset); |
| 555 | int64_t DataAlignmentFactor = Data.getSLEB128(&Offset); |
| 556 | uint64_t ReturnAddressRegister = Data.getULEB128(&Offset); |
| 557 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 558 | // Parse the augmentation data for EH CIEs |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 559 | StringRef AugmentationData(""); |
| 560 | uint32_t FDEPointerEncoding = DW_EH_PE_omit; |
| 561 | uint32_t LSDAPointerEncoding = DW_EH_PE_omit; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 562 | if (IsEH) { |
| 563 | Optional<uint32_t> PersonalityEncoding; |
| 564 | Optional<uint64_t> Personality; |
| 565 | |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 566 | Optional<uint64_t> AugmentationLength; |
| 567 | uint32_t StartAugmentationOffset; |
| 568 | uint32_t EndAugmentationOffset; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 569 | |
| 570 | // Walk the augmentation string to get all the augmentation data. |
| 571 | for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) { |
| 572 | switch (AugmentationString[i]) { |
| 573 | default: |
| 574 | ReportError("Unknown augmentation character in entry at %lx"); |
| 575 | case 'L': |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 576 | LSDAPointerEncoding = Data.getU8(&Offset); |
| 577 | break; |
| 578 | case 'P': { |
| 579 | if (Personality) |
| 580 | ReportError("Duplicate personality in entry at %lx"); |
| 581 | PersonalityEncoding = Data.getU8(&Offset); |
| 582 | Personality = readPointer(Data, Offset, *PersonalityEncoding); |
| 583 | break; |
| 584 | } |
| 585 | case 'R': |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 586 | FDEPointerEncoding = Data.getU8(&Offset); |
| 587 | break; |
| 588 | case 'z': |
| 589 | if (i) |
| 590 | ReportError("'z' must be the first character at %lx"); |
| 591 | // Parse the augmentation length first. We only parse it if |
| 592 | // the string contains a 'z'. |
| 593 | AugmentationLength = Data.getULEB128(&Offset); |
| 594 | StartAugmentationOffset = Offset; |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 595 | EndAugmentationOffset = Offset + |
| 596 | static_cast<uint32_t>(*AugmentationLength); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 600 | if (AugmentationLength.hasValue()) { |
| 601 | if (Offset != EndAugmentationOffset) |
| 602 | ReportError("Parsing augmentation data at %lx failed"); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 603 | |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 604 | AugmentationData = Data.getData().slice(StartAugmentationOffset, |
| 605 | EndAugmentationOffset); |
| 606 | } |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 609 | auto Cie = make_unique<CIE>(StartOffset, Length, Version, |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 610 | AugmentationString, AddressSize, |
Keith Walker | ea9483f | 2015-05-12 15:25:08 +0000 | [diff] [blame] | 611 | SegmentDescriptorSize, CodeAlignmentFactor, |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 612 | DataAlignmentFactor, ReturnAddressRegister, |
| 613 | AugmentationData, FDEPointerEncoding, |
| 614 | LSDAPointerEncoding); |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 615 | CIEs[StartOffset] = Cie.get(); |
| 616 | Entries.emplace_back(std::move(Cie)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 617 | } else { |
| 618 | // FDE |
| 619 | uint64_t CIEPointer = Id; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 620 | uint64_t InitialLocation = 0; |
| 621 | uint64_t AddressRange = 0; |
| 622 | CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer]; |
| 623 | |
| 624 | if (IsEH) { |
| 625 | // The address size is encoded in the CIE we reference. |
| 626 | if (!Cie) |
| 627 | ReportError("Parsing FDE data at %lx failed due to missing CIE"); |
| 628 | |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 629 | InitialLocation = readPointer(Data, Offset, |
| 630 | Cie->getFDEPointerEncoding()); |
| 631 | AddressRange = readPointer(Data, Offset, |
| 632 | Cie->getFDEPointerEncoding()); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 633 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 634 | StringRef AugmentationString = Cie->getAugmentationString(); |
| 635 | if (!AugmentationString.empty()) { |
| 636 | // Parse the augmentation length and data for this FDE. |
| 637 | uint64_t AugmentationLength = Data.getULEB128(&Offset); |
| 638 | |
| 639 | uint32_t EndAugmentationOffset = |
| 640 | Offset + static_cast<uint32_t>(AugmentationLength); |
| 641 | |
| 642 | // Decode the LSDA if the CIE augmentation string said we should. |
| 643 | uint64_t LSDA = 0; |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame^] | 644 | if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) |
| 645 | LSDA = readPointer(Data, Offset, Cie->getLSDAPointerEncoding()); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 646 | |
| 647 | if (Offset != EndAugmentationOffset) |
| 648 | ReportError("Parsing augmentation data at %lx failed"); |
| 649 | } |
| 650 | } else { |
| 651 | InitialLocation = Data.getAddress(&Offset); |
| 652 | AddressRange = Data.getAddress(&Offset); |
| 653 | } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 654 | |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 655 | Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer, |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 656 | InitialLocation, AddressRange, |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 657 | Cie)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 660 | Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 661 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 662 | if (Offset != EndStructureOffset) |
| 663 | ReportError("Parsing entry instructions at %lx failed"); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | |
| 668 | void DWARFDebugFrame::dump(raw_ostream &OS) const { |
| 669 | OS << "\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 670 | for (const auto &Entry : Entries) { |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 671 | Entry->dumpHeader(OS); |
| 672 | Entry->dumpInstructions(OS); |
| 673 | OS << "\n"; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |