Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 1 | //===- DWARFDebugFrame.h - Parsing of .debug_frame ------------------------===// |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/DenseMap.h" |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Optional.h" |
Simon Atanasyan | f69c7e5 | 2016-03-01 18:38:05 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 61a72d8 | 2016-08-18 17:56:27 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 15 | #include "llvm/BinaryFormat/Dwarf.h" |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Casting.h" |
Eugene Zelenko | 61a72d8 | 2016-08-18 17:56:27 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/DataExtractor.h" |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Errc.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Format.h" |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 61a72d8 | 2016-08-18 17:56:27 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <cassert> |
| 25 | #include <cinttypes> |
| 26 | #include <cstdint> |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <vector> |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | using namespace dwarf; |
| 32 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 33 | |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 34 | // See DWARF standard v3, section 7.23 |
| 35 | const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0; |
| 36 | const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f; |
| 37 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 38 | Error CFIProgram::parse(DataExtractor Data, uint32_t *Offset, |
| 39 | uint32_t EndOffset) { |
Eli Bendersky | 8e352e3 | 2013-02-22 00:50:48 +0000 | [diff] [blame] | 40 | while (*Offset < EndOffset) { |
| 41 | uint8_t Opcode = Data.getU8(Offset); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 42 | // Some instructions have a primary opcode encoded in the top bits. |
| 43 | uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK; |
| 44 | |
| 45 | if (Primary) { |
| 46 | // If it's a primary opcode, the first operand is encoded in the bottom |
| 47 | // bits of the opcode itself. |
| 48 | uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK; |
| 49 | switch (Primary) { |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 50 | default: |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 51 | return createStringError(errc::illegal_byte_sequence, |
| 52 | "Invalid primary CFI opcode 0x%" PRIx8, |
| 53 | Primary); |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 54 | case DW_CFA_advance_loc: |
| 55 | case DW_CFA_restore: |
| 56 | addInstruction(Primary, Op1); |
| 57 | break; |
| 58 | case DW_CFA_offset: |
| 59 | addInstruction(Primary, Op1, Data.getULEB128(Offset)); |
| 60 | break; |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 61 | } |
| 62 | } else { |
| 63 | // Extended opcode - its value is Opcode itself. |
| 64 | switch (Opcode) { |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 65 | default: |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 66 | return createStringError(errc::illegal_byte_sequence, |
| 67 | "Invalid extended CFI opcode 0x%" PRIx8, |
| 68 | Opcode); |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 69 | case DW_CFA_nop: |
| 70 | case DW_CFA_remember_state: |
| 71 | case DW_CFA_restore_state: |
| 72 | case DW_CFA_GNU_window_save: |
| 73 | // No operands |
| 74 | addInstruction(Opcode); |
| 75 | break; |
| 76 | case DW_CFA_set_loc: |
| 77 | // Operands: Address |
| 78 | addInstruction(Opcode, Data.getAddress(Offset)); |
| 79 | break; |
| 80 | case DW_CFA_advance_loc1: |
| 81 | // Operands: 1-byte delta |
| 82 | addInstruction(Opcode, Data.getU8(Offset)); |
| 83 | break; |
| 84 | case DW_CFA_advance_loc2: |
| 85 | // Operands: 2-byte delta |
| 86 | addInstruction(Opcode, Data.getU16(Offset)); |
| 87 | break; |
| 88 | case DW_CFA_advance_loc4: |
| 89 | // Operands: 4-byte delta |
| 90 | addInstruction(Opcode, Data.getU32(Offset)); |
| 91 | break; |
| 92 | case DW_CFA_restore_extended: |
| 93 | case DW_CFA_undefined: |
| 94 | case DW_CFA_same_value: |
| 95 | case DW_CFA_def_cfa_register: |
| 96 | case DW_CFA_def_cfa_offset: |
| 97 | case DW_CFA_GNU_args_size: |
| 98 | // Operands: ULEB128 |
| 99 | addInstruction(Opcode, Data.getULEB128(Offset)); |
| 100 | break; |
| 101 | case DW_CFA_def_cfa_offset_sf: |
| 102 | // Operands: SLEB128 |
| 103 | addInstruction(Opcode, Data.getSLEB128(Offset)); |
| 104 | break; |
| 105 | case DW_CFA_offset_extended: |
| 106 | case DW_CFA_register: |
| 107 | case DW_CFA_def_cfa: |
| 108 | case DW_CFA_val_offset: { |
| 109 | // Operands: ULEB128, ULEB128 |
| 110 | // Note: We can not embed getULEB128 directly into function |
| 111 | // argument list. getULEB128 changes Offset and order of evaluation |
| 112 | // for arguments is unspecified. |
| 113 | auto op1 = Data.getULEB128(Offset); |
| 114 | auto op2 = Data.getULEB128(Offset); |
| 115 | addInstruction(Opcode, op1, op2); |
| 116 | break; |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 117 | } |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 118 | case DW_CFA_offset_extended_sf: |
| 119 | case DW_CFA_def_cfa_sf: |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 120 | case DW_CFA_val_offset_sf: { |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 121 | // Operands: ULEB128, SLEB128 |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 122 | // Note: see comment for the previous case |
| 123 | auto op1 = Data.getULEB128(Offset); |
| 124 | auto op2 = (uint64_t)Data.getSLEB128(Offset); |
| 125 | addInstruction(Opcode, op1, op2); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 126 | break; |
Igor Laevsky | 0e1605a | 2016-01-26 13:31:11 +0000 | [diff] [blame] | 127 | } |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 128 | case DW_CFA_def_cfa_expression: { |
| 129 | uint32_t ExprLength = Data.getULEB128(Offset); |
| 130 | addInstruction(Opcode, 0); |
| 131 | DataExtractor Extractor( |
| 132 | Data.getData().slice(*Offset, *Offset + ExprLength), |
| 133 | Data.isLittleEndian(), Data.getAddressSize()); |
| 134 | Instructions.back().Expression = DWARFExpression( |
| 135 | Extractor, Data.getAddressSize(), dwarf::DWARF_VERSION); |
| 136 | *Offset += ExprLength; |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 137 | break; |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 138 | } |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 139 | case DW_CFA_expression: |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 140 | case DW_CFA_val_expression: { |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 141 | auto RegNum = Data.getULEB128(Offset); |
| 142 | auto BlockLength = Data.getULEB128(Offset); |
| 143 | addInstruction(Opcode, RegNum, 0); |
| 144 | DataExtractor Extractor( |
| 145 | Data.getData().slice(*Offset, *Offset + BlockLength), |
| 146 | Data.isLittleEndian(), Data.getAddressSize()); |
| 147 | Instructions.back().Expression = DWARFExpression( |
| 148 | Extractor, Data.getAddressSize(), dwarf::DWARF_VERSION); |
| 149 | *Offset += BlockLength; |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 155 | |
| 156 | return Error::success(); |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Benjamin Kramer | 6ecb1e7 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 159 | namespace { |
Eugene Zelenko | 61a72d8 | 2016-08-18 17:56:27 +0000 | [diff] [blame] | 160 | |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 161 | |
Benjamin Kramer | 6ecb1e7 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 162 | } // end anonymous namespace |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 163 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 164 | ArrayRef<CFIProgram::OperandType[2]> CFIProgram::getOperandTypes() { |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 165 | static OperandType OpTypes[DW_CFA_restore+1][2]; |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 166 | static bool Initialized = false; |
| 167 | if (Initialized) { |
| 168 | return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1); |
| 169 | } |
| 170 | Initialized = true; |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 171 | |
| 172 | #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \ |
| 173 | do { \ |
| 174 | OpTypes[OP][0] = OPTYPE0; \ |
| 175 | OpTypes[OP][1] = OPTYPE1; \ |
Eugene Zelenko | 61a72d8 | 2016-08-18 17:56:27 +0000 | [diff] [blame] | 176 | } while (false) |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 177 | #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None) |
| 178 | #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None) |
| 179 | |
| 180 | DECLARE_OP1(DW_CFA_set_loc, OT_Address); |
| 181 | DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset); |
| 182 | DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset); |
| 183 | DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset); |
| 184 | DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset); |
| 185 | DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset); |
| 186 | DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset); |
| 187 | DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset); |
| 188 | DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register); |
| 189 | DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset); |
| 190 | DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset); |
| 191 | DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression); |
| 192 | DECLARE_OP1(DW_CFA_undefined, OT_Register); |
| 193 | DECLARE_OP1(DW_CFA_same_value, OT_Register); |
| 194 | DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset); |
| 195 | DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset); |
| 196 | DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset); |
| 197 | DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset); |
| 198 | DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset); |
| 199 | DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register); |
| 200 | DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression); |
| 201 | DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression); |
| 202 | DECLARE_OP1(DW_CFA_restore, OT_Register); |
| 203 | DECLARE_OP1(DW_CFA_restore_extended, OT_Register); |
| 204 | DECLARE_OP0(DW_CFA_remember_state); |
| 205 | DECLARE_OP0(DW_CFA_restore_state); |
| 206 | DECLARE_OP0(DW_CFA_GNU_window_save); |
| 207 | DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset); |
| 208 | DECLARE_OP0(DW_CFA_nop); |
| 209 | |
| 210 | #undef DECLARE_OP0 |
| 211 | #undef DECLARE_OP1 |
| 212 | #undef DECLARE_OP2 |
Eugene Zelenko | 61a72d8 | 2016-08-18 17:56:27 +0000 | [diff] [blame] | 213 | |
Frederic Riss | ac10b0d | 2015-02-25 22:07:43 +0000 | [diff] [blame] | 214 | return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1); |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 217 | /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand. |
| 218 | void CFIProgram::printOperand(raw_ostream &OS, const MCRegisterInfo *MRI, |
| 219 | bool IsEH, const Instruction &Instr, |
| 220 | unsigned OperandIdx, uint64_t Operand) const { |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 221 | assert(OperandIdx < 2); |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 222 | uint8_t Opcode = Instr.Opcode; |
| 223 | OperandType Type = getOperandTypes()[Opcode][OperandIdx]; |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 224 | |
| 225 | switch (Type) { |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 226 | case OT_Unset: { |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 227 | OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to"; |
Luke Cheeseman | 6db3a6a | 2018-11-23 17:13:06 +0000 | [diff] [blame] | 228 | auto OpcodeName = CallFrameString(Opcode); |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 229 | if (!OpcodeName.empty()) |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 230 | OS << " " << OpcodeName; |
| 231 | else |
| 232 | OS << format(" Opcode %x", Opcode); |
| 233 | break; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 234 | } |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 235 | case OT_None: |
| 236 | break; |
| 237 | case OT_Address: |
| 238 | OS << format(" %" PRIx64, Operand); |
| 239 | break; |
| 240 | case OT_Offset: |
| 241 | // The offsets are all encoded in a unsigned form, but in practice |
| 242 | // consumers use them signed. It's most certainly legacy due to |
| 243 | // the lack of signed variants in the first Dwarf standards. |
| 244 | OS << format(" %+" PRId64, int64_t(Operand)); |
| 245 | break; |
| 246 | case OT_FactoredCodeOffset: // Always Unsigned |
| 247 | if (CodeAlignmentFactor) |
| 248 | OS << format(" %" PRId64, Operand * CodeAlignmentFactor); |
| 249 | else |
| 250 | OS << format(" %" PRId64 "*code_alignment_factor" , Operand); |
| 251 | break; |
| 252 | case OT_SignedFactDataOffset: |
| 253 | if (DataAlignmentFactor) |
| 254 | OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor); |
| 255 | else |
| 256 | OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand)); |
| 257 | break; |
| 258 | case OT_UnsignedFactDataOffset: |
| 259 | if (DataAlignmentFactor) |
| 260 | OS << format(" %" PRId64, Operand * DataAlignmentFactor); |
| 261 | else |
| 262 | OS << format(" %" PRId64 "*data_alignment_factor" , Operand); |
| 263 | break; |
| 264 | case OT_Register: |
Frederic Riss | de37434 | 2015-02-25 22:30:09 +0000 | [diff] [blame] | 265 | OS << format(" reg%" PRId64, Operand); |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 266 | break; |
| 267 | case OT_Expression: |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 268 | assert(Instr.Expression && "missing DWARFExpression object"); |
| 269 | OS << " "; |
| 270 | Instr.Expression->print(OS, MRI, IsEH); |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 275 | void CFIProgram::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH, |
| 276 | unsigned IndentLevel) const { |
Frederic Riss | 056ad05 | 2015-02-25 21:30:16 +0000 | [diff] [blame] | 277 | for (const auto &Instr : Instructions) { |
| 278 | uint8_t Opcode = Instr.Opcode; |
| 279 | if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK) |
| 280 | Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK; |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 281 | OS.indent(2 * IndentLevel); |
Luke Cheeseman | 6db3a6a | 2018-11-23 17:13:06 +0000 | [diff] [blame] | 282 | OS << CallFrameString(Opcode) << ":"; |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 283 | for (unsigned i = 0; i < Instr.Ops.size(); ++i) |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 284 | printOperand(OS, MRI, IsEH, Instr, i, Instr.Ops[i]); |
Frederic Riss | c0dd724 | 2015-02-25 21:30:22 +0000 | [diff] [blame] | 285 | OS << '\n'; |
Frederic Riss | 056ad05 | 2015-02-25 21:30:16 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 288 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 289 | void CIE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const { |
| 290 | OS << format("%08x %08x %08x CIE", (uint32_t)Offset, (uint32_t)Length, |
| 291 | DW_CIE_ID) |
| 292 | << "\n"; |
| 293 | OS << format(" Version: %d\n", Version); |
| 294 | OS << " Augmentation: \"" << Augmentation << "\"\n"; |
| 295 | if (Version >= 4) { |
| 296 | OS << format(" Address size: %u\n", (uint32_t)AddressSize); |
| 297 | OS << format(" Segment desc size: %u\n", |
| 298 | (uint32_t)SegmentDescriptorSize); |
| 299 | } |
| 300 | OS << format(" Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor); |
| 301 | OS << format(" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor); |
| 302 | OS << format(" Return address column: %d\n", (int32_t)ReturnAddressRegister); |
| 303 | if (Personality) |
| 304 | OS << format(" Personality Address: %08x\n", *Personality); |
| 305 | if (!AugmentationData.empty()) { |
| 306 | OS << " Augmentation data: "; |
| 307 | for (uint8_t Byte : AugmentationData) |
| 308 | OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf); |
| 309 | OS << "\n"; |
| 310 | } |
| 311 | OS << "\n"; |
| 312 | CFIs.dump(OS, MRI, IsEH); |
| 313 | OS << "\n"; |
| 314 | } |
| 315 | |
| 316 | void FDE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const { |
| 317 | OS << format("%08x %08x %08x FDE ", (uint32_t)Offset, (uint32_t)Length, |
| 318 | (int32_t)LinkedCIEOffset); |
| 319 | OS << format("cie=%08x pc=%08x...%08x\n", (int32_t)LinkedCIEOffset, |
| 320 | (uint32_t)InitialLocation, |
| 321 | (uint32_t)InitialLocation + (uint32_t)AddressRange); |
| 322 | if (LSDAAddress) |
| 323 | OS << format(" LSDA Address: %08x\n", *LSDAAddress); |
| 324 | CFIs.dump(OS, MRI, IsEH); |
| 325 | OS << "\n"; |
| 326 | } |
| 327 | |
Luke Cheeseman | 6db3a6a | 2018-11-23 17:13:06 +0000 | [diff] [blame] | 328 | DWARFDebugFrame::DWARFDebugFrame(bool IsEH, uint64_t EHFrameAddress) |
| 329 | : IsEH(IsEH), EHFrameAddress(EHFrameAddress) {} |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 330 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 331 | DWARFDebugFrame::~DWARFDebugFrame() = default; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 332 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 333 | static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, |
| 334 | uint32_t Offset, int Length) { |
| 335 | errs() << "DUMP: "; |
| 336 | for (int i = 0; i < Length; ++i) { |
| 337 | uint8_t c = Data.getU8(&Offset); |
| 338 | errs().write_hex(c); errs() << " "; |
| 339 | } |
| 340 | errs() << "\n"; |
| 341 | } |
| 342 | |
Galina Kistanova | 3c0505d | 2017-06-14 17:32:53 +0000 | [diff] [blame] | 343 | // This is a workaround for old compilers which do not allow |
| 344 | // noreturn attribute usage in lambdas. Once the support for those |
| 345 | // compilers are phased out, we can remove this and return back to |
| 346 | // a ReportError lambda: [StartOffset](const char *ErrorMsg). |
David Blaikie | 6ab0eb4 | 2017-06-19 19:01:08 +0000 | [diff] [blame] | 347 | static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset, |
| 348 | const char *ErrorMsg) { |
| 349 | std::string Str; |
| 350 | raw_string_ostream OS(Str); |
| 351 | OS << format(ErrorMsg, StartOffset); |
| 352 | OS.flush(); |
| 353 | report_fatal_error(Str); |
Galina Kistanova | 3c0505d | 2017-06-14 17:32:53 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 356 | void DWARFDebugFrame::parse(DWARFDataExtractor Data) { |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 357 | uint32_t Offset = 0; |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 358 | DenseMap<uint32_t, CIE *> CIEs; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 359 | |
| 360 | while (Data.isValidOffset(Offset)) { |
| 361 | uint32_t StartOffset = Offset; |
| 362 | |
| 363 | bool IsDWARF64 = false; |
| 364 | uint64_t Length = Data.getU32(&Offset); |
| 365 | uint64_t Id; |
| 366 | |
| 367 | if (Length == UINT32_MAX) { |
| 368 | // DWARF-64 is distinguished by the first 32 bits of the initial length |
| 369 | // field being 0xffffffff. Then, the next 64 bits are the actual entry |
| 370 | // length. |
| 371 | IsDWARF64 = true; |
| 372 | Length = Data.getU64(&Offset); |
| 373 | } |
| 374 | |
| 375 | // At this point, Offset points to the next field after Length. |
| 376 | // Length is the structure size excluding itself. Compute an offset one |
| 377 | // past the end of the structure (needed to know how many instructions to |
| 378 | // read). |
| 379 | // TODO: For honest DWARF64 support, DataExtractor will have to treat |
| 380 | // offset_ptr as uint64_t* |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 381 | uint32_t StartStructureOffset = Offset; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 382 | uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length); |
| 383 | |
| 384 | // The Id field's size depends on the DWARF format |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 385 | Id = Data.getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4); |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 386 | bool IsCIE = |
| 387 | ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID || (IsEH && !Id)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 388 | |
| 389 | if (IsCIE) { |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 390 | uint8_t Version = Data.getU8(&Offset); |
| 391 | const char *Augmentation = Data.getCStr(&Offset); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 392 | StringRef AugmentationString(Augmentation ? Augmentation : ""); |
| 393 | uint8_t AddressSize = Version < 4 ? Data.getAddressSize() : |
| 394 | Data.getU8(&Offset); |
Keith Walker | ea9483f | 2015-05-12 15:25:08 +0000 | [diff] [blame] | 395 | Data.setAddressSize(AddressSize); |
| 396 | uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 397 | uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset); |
| 398 | int64_t DataAlignmentFactor = Data.getSLEB128(&Offset); |
George Rimar | 7e981f3 | 2018-12-04 10:01:39 +0000 | [diff] [blame^] | 399 | uint64_t ReturnAddressRegister = |
| 400 | Version == 1 ? Data.getU8(&Offset) : Data.getULEB128(&Offset); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 401 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 402 | // Parse the augmentation data for EH CIEs |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame] | 403 | StringRef AugmentationData(""); |
Maksim Panchenko | fa762cc | 2018-07-09 18:45:38 +0000 | [diff] [blame] | 404 | uint32_t FDEPointerEncoding = DW_EH_PE_absptr; |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame] | 405 | uint32_t LSDAPointerEncoding = DW_EH_PE_omit; |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 406 | Optional<uint64_t> Personality; |
| 407 | Optional<uint32_t> PersonalityEncoding; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 408 | if (IsEH) { |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame] | 409 | Optional<uint64_t> AugmentationLength; |
| 410 | uint32_t StartAugmentationOffset; |
| 411 | uint32_t EndAugmentationOffset; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 412 | |
| 413 | // Walk the augmentation string to get all the augmentation data. |
| 414 | for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) { |
| 415 | switch (AugmentationString[i]) { |
| 416 | default: |
David Blaikie | 6ab0eb4 | 2017-06-19 19:01:08 +0000 | [diff] [blame] | 417 | ReportError(StartOffset, |
| 418 | "Unknown augmentation character in entry at %lx"); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 419 | case 'L': |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 420 | LSDAPointerEncoding = Data.getU8(&Offset); |
| 421 | break; |
| 422 | case 'P': { |
| 423 | if (Personality) |
David Blaikie | 6ab0eb4 | 2017-06-19 19:01:08 +0000 | [diff] [blame] | 424 | ReportError(StartOffset, |
| 425 | "Duplicate personality in entry at %lx"); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 426 | PersonalityEncoding = Data.getU8(&Offset); |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 427 | Personality = Data.getEncodedPointer( |
| 428 | &Offset, *PersonalityEncoding, |
| 429 | EHFrameAddress ? EHFrameAddress + Offset : 0); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | case 'R': |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 433 | FDEPointerEncoding = Data.getU8(&Offset); |
| 434 | break; |
Fangrui Song | bd08856 | 2018-05-08 06:21:12 +0000 | [diff] [blame] | 435 | case 'S': |
| 436 | // Current frame is a signal trampoline. |
| 437 | break; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 438 | case 'z': |
| 439 | if (i) |
David Blaikie | 6ab0eb4 | 2017-06-19 19:01:08 +0000 | [diff] [blame] | 440 | ReportError(StartOffset, |
| 441 | "'z' must be the first character at %lx"); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 442 | // Parse the augmentation length first. We only parse it if |
| 443 | // the string contains a 'z'. |
| 444 | AugmentationLength = Data.getULEB128(&Offset); |
| 445 | StartAugmentationOffset = Offset; |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame] | 446 | EndAugmentationOffset = Offset + |
| 447 | static_cast<uint32_t>(*AugmentationLength); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame] | 451 | if (AugmentationLength.hasValue()) { |
| 452 | if (Offset != EndAugmentationOffset) |
David Blaikie | 6ab0eb4 | 2017-06-19 19:01:08 +0000 | [diff] [blame] | 453 | ReportError(StartOffset, "Parsing augmentation data at %lx failed"); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 454 | |
Igor Laevsky | ff291b5 | 2016-01-27 14:05:35 +0000 | [diff] [blame] | 455 | AugmentationData = Data.getData().slice(StartAugmentationOffset, |
| 456 | EndAugmentationOffset); |
| 457 | } |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 460 | auto Cie = llvm::make_unique<CIE>( |
| 461 | StartOffset, Length, Version, AugmentationString, AddressSize, |
| 462 | SegmentDescriptorSize, CodeAlignmentFactor, DataAlignmentFactor, |
| 463 | ReturnAddressRegister, AugmentationData, FDEPointerEncoding, |
Luke Cheeseman | 6db3a6a | 2018-11-23 17:13:06 +0000 | [diff] [blame] | 464 | LSDAPointerEncoding, Personality, PersonalityEncoding); |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 465 | CIEs[StartOffset] = Cie.get(); |
| 466 | Entries.emplace_back(std::move(Cie)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 467 | } else { |
| 468 | // FDE |
| 469 | uint64_t CIEPointer = Id; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 470 | uint64_t InitialLocation = 0; |
| 471 | uint64_t AddressRange = 0; |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 472 | Optional<uint64_t> LSDAAddress; |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 473 | CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer]; |
| 474 | |
| 475 | if (IsEH) { |
| 476 | // The address size is encoded in the CIE we reference. |
| 477 | if (!Cie) |
David Blaikie | 6ab0eb4 | 2017-06-19 19:01:08 +0000 | [diff] [blame] | 478 | ReportError(StartOffset, |
| 479 | "Parsing FDE data at %lx failed due to missing CIE"); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 480 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 481 | if (auto Val = Data.getEncodedPointer( |
| 482 | &Offset, Cie->getFDEPointerEncoding(), |
| 483 | EHFrameAddress ? EHFrameAddress + Offset : 0)) { |
| 484 | InitialLocation = *Val; |
| 485 | } |
| 486 | if (auto Val = Data.getEncodedPointer( |
| 487 | &Offset, Cie->getFDEPointerEncoding(), 0)) { |
| 488 | AddressRange = *Val; |
| 489 | } |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 490 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 491 | StringRef AugmentationString = Cie->getAugmentationString(); |
| 492 | if (!AugmentationString.empty()) { |
| 493 | // Parse the augmentation length and data for this FDE. |
| 494 | uint64_t AugmentationLength = Data.getULEB128(&Offset); |
| 495 | |
| 496 | uint32_t EndAugmentationOffset = |
| 497 | Offset + static_cast<uint32_t>(AugmentationLength); |
| 498 | |
| 499 | // Decode the LSDA if the CIE augmentation string said we should. |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 500 | if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) { |
| 501 | LSDAAddress = Data.getEncodedPointer( |
| 502 | &Offset, Cie->getLSDAPointerEncoding(), |
| 503 | EHFrameAddress ? Offset + EHFrameAddress : 0); |
| 504 | } |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 505 | |
| 506 | if (Offset != EndAugmentationOffset) |
David Blaikie | 6ab0eb4 | 2017-06-19 19:01:08 +0000 | [diff] [blame] | 507 | ReportError(StartOffset, "Parsing augmentation data at %lx failed"); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 508 | } |
| 509 | } else { |
| 510 | InitialLocation = Data.getAddress(&Offset); |
| 511 | AddressRange = Data.getAddress(&Offset); |
| 512 | } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 513 | |
Alexey Samsonov | a08b161 | 2014-04-28 23:00:06 +0000 | [diff] [blame] | 514 | Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer, |
Frederic Riss | baf195f | 2015-02-25 21:30:09 +0000 | [diff] [blame] | 515 | InitialLocation, AddressRange, |
Luke Cheeseman | 6db3a6a | 2018-11-23 17:13:06 +0000 | [diff] [blame] | 516 | Cie, LSDAAddress)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 519 | if (Error E = |
| 520 | Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) { |
| 521 | report_fatal_error(toString(std::move(E))); |
| 522 | } |
Eli Bendersky | 705085d | 2013-02-21 22:53:19 +0000 | [diff] [blame] | 523 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 524 | if (Offset != EndStructureOffset) |
David Blaikie | 6ab0eb4 | 2017-06-19 19:01:08 +0000 | [diff] [blame] | 525 | ReportError(StartOffset, "Parsing entry instructions at %lx failed"); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Adrian Prantl | 62528e6 | 2017-09-21 18:52:03 +0000 | [diff] [blame] | 529 | FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const { |
| 530 | auto It = |
| 531 | std::lower_bound(Entries.begin(), Entries.end(), Offset, |
| 532 | [](const std::unique_ptr<FrameEntry> &E, |
| 533 | uint64_t Offset) { return E->getOffset() < Offset; }); |
| 534 | if (It != Entries.end() && (*It)->getOffset() == Offset) |
| 535 | return It->get(); |
| 536 | return nullptr; |
| 537 | } |
| 538 | |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 539 | void DWARFDebugFrame::dump(raw_ostream &OS, const MCRegisterInfo *MRI, |
| 540 | Optional<uint64_t> Offset) const { |
Adrian Prantl | 62528e6 | 2017-09-21 18:52:03 +0000 | [diff] [blame] | 541 | if (Offset) { |
| 542 | if (auto *Entry = getEntryAtOffset(*Offset)) |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 543 | Entry->dump(OS, MRI, IsEH); |
Adrian Prantl | 62528e6 | 2017-09-21 18:52:03 +0000 | [diff] [blame] | 544 | return; |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 545 | } |
Adrian Prantl | 62528e6 | 2017-09-21 18:52:03 +0000 | [diff] [blame] | 546 | |
| 547 | OS << "\n"; |
| 548 | for (const auto &Entry : Entries) |
Rafael Auler | 86fb7bf | 2018-03-08 00:46:53 +0000 | [diff] [blame] | 549 | Entry->dump(OS, MRI, IsEH); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 550 | } |