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