1 //===-- DWARFDebugFrame.h - Parsing of .debug_frame -------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" 11 #include "llvm/ADT/ArrayRef.h" 12 #include "llvm/ADT/SmallString.h" 13 #include "llvm/Support/Casting.h" 14 #include "llvm/Support/DataTypes.h" 15 #include "llvm/Support/Dwarf.h" 16 #include "llvm/Support/ErrorHandling.h" 17 #include "llvm/Support/Format.h" 18 #include "llvm/Support/raw_ostream.h" 19 #include <string> 20 #include <vector> 21 22 using namespace llvm; 23 using namespace dwarf; 24 25 26 /// \brief Abstract frame entry defining the common interface concrete 27 /// entries implement. 28 class llvm::FrameEntry { 29 public: 30 enum FrameKind {FK_CIE, FK_FDE}; 31 FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length) 32 : Kind(K), Offset(Offset), Length(Length) {} 33 34 virtual ~FrameEntry() { 35 } 36 37 FrameKind getKind() const { return Kind; } 38 virtual uint64_t getOffset() const { return Offset; } 39 40 /// \brief Parse and store a sequence of CFI instructions from Data, 41 /// starting at *Offset and ending at EndOffset. If everything 42 /// goes well, *Offset should be equal to EndOffset when this method 43 /// returns. Otherwise, an error occurred. 44 virtual void parseInstructions(DataExtractor Data, uint32_t *Offset, 45 uint32_t EndOffset); 46 47 /// \brief Dump the entry header to the given output stream. 48 virtual void dumpHeader(raw_ostream &OS) const = 0; 49 50 /// \brief Dump the entry's instructions to the given output stream. 51 virtual void dumpInstructions(raw_ostream &OS) const; 52 53 protected: 54 const FrameKind Kind; 55 56 /// \brief Offset of this entry in the section. 57 uint64_t Offset; 58 59 /// \brief Entry length as specified in DWARF. 60 uint64_t Length; 61 62 /// An entry may contain CFI instructions. An instruction consists of an 63 /// opcode and an optional sequence of operands. 64 typedef std::vector<uint64_t> Operands; 65 struct Instruction { 66 Instruction(uint8_t Opcode) 67 : Opcode(Opcode) 68 {} 69 70 uint8_t Opcode; 71 Operands Ops; 72 }; 73 74 std::vector<Instruction> Instructions; 75 76 /// Convenience methods to add a new instruction with the given opcode and 77 /// operands to the Instructions vector. 78 void addInstruction(uint8_t Opcode) { 79 Instructions.push_back(Instruction(Opcode)); 80 } 81 82 void addInstruction(uint8_t Opcode, uint64_t Operand1) { 83 Instructions.push_back(Instruction(Opcode)); 84 Instructions.back().Ops.push_back(Operand1); 85 } 86 87 void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) { 88 Instructions.push_back(Instruction(Opcode)); 89 Instructions.back().Ops.push_back(Operand1); 90 Instructions.back().Ops.push_back(Operand2); 91 } 92 }; 93 94 95 // See DWARF standard v3, section 7.23 96 const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0; 97 const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f; 98 99 void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset, 100 uint32_t EndOffset) { 101 while (*Offset < EndOffset) { 102 uint8_t Opcode = Data.getU8(Offset); 103 // Some instructions have a primary opcode encoded in the top bits. 104 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK; 105 106 if (Primary) { 107 // If it's a primary opcode, the first operand is encoded in the bottom 108 // bits of the opcode itself. 109 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK; 110 switch (Primary) { 111 default: llvm_unreachable("Impossible primary CFI opcode"); 112 case DW_CFA_advance_loc: 113 case DW_CFA_restore: 114 addInstruction(Primary, Op1); 115 break; 116 case DW_CFA_offset: 117 addInstruction(Primary, Op1, Data.getULEB128(Offset)); 118 break; 119 } 120 } else { 121 // Extended opcode - its value is Opcode itself. 122 switch (Opcode) { 123 default: llvm_unreachable("Invalid extended CFI opcode"); 124 case DW_CFA_nop: 125 case DW_CFA_remember_state: 126 case DW_CFA_restore_state: 127 case DW_CFA_GNU_window_save: 128 // No operands 129 addInstruction(Opcode); 130 break; 131 case DW_CFA_set_loc: 132 // Operands: Address 133 addInstruction(Opcode, Data.getAddress(Offset)); 134 break; 135 case DW_CFA_advance_loc1: 136 // Operands: 1-byte delta 137 addInstruction(Opcode, Data.getU8(Offset)); 138 break; 139 case DW_CFA_advance_loc2: 140 // Operands: 2-byte delta 141 addInstruction(Opcode, Data.getU16(Offset)); 142 break; 143 case DW_CFA_advance_loc4: 144 // Operands: 4-byte delta 145 addInstruction(Opcode, Data.getU32(Offset)); 146 break; 147 case DW_CFA_restore_extended: 148 case DW_CFA_undefined: 149 case DW_CFA_same_value: 150 case DW_CFA_def_cfa_register: 151 case DW_CFA_def_cfa_offset: 152 // Operands: ULEB128 153 addInstruction(Opcode, Data.getULEB128(Offset)); 154 break; 155 case DW_CFA_def_cfa_offset_sf: 156 // Operands: SLEB128 157 addInstruction(Opcode, Data.getSLEB128(Offset)); 158 break; 159 case DW_CFA_offset_extended: 160 case DW_CFA_register: 161 case DW_CFA_def_cfa: 162 case DW_CFA_val_offset: 163 // Operands: ULEB128, ULEB128 164 addInstruction(Opcode, Data.getULEB128(Offset), 165 Data.getULEB128(Offset)); 166 break; 167 case DW_CFA_offset_extended_sf: 168 case DW_CFA_def_cfa_sf: 169 case DW_CFA_val_offset_sf: 170 // Operands: ULEB128, SLEB128 171 addInstruction(Opcode, Data.getULEB128(Offset), 172 Data.getSLEB128(Offset)); 173 break; 174 case DW_CFA_def_cfa_expression: 175 case DW_CFA_expression: 176 case DW_CFA_val_expression: 177 // TODO: implement this 178 report_fatal_error("Values with expressions not implemented yet!"); 179 } 180 } 181 } 182 } 183 184 namespace { 185 /// \brief DWARF Common Information Entry (CIE) 186 class CIE : public FrameEntry { 187 public: 188 // CIEs (and FDEs) are simply container classes, so the only sensible way to 189 // create them is by providing the full parsed contents in the constructor. 190 CIE(uint64_t Offset, uint64_t Length, uint8_t Version, 191 SmallString<8> Augmentation, uint64_t CodeAlignmentFactor, 192 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister) 193 : FrameEntry(FK_CIE, Offset, Length), Version(Version), 194 Augmentation(std::move(Augmentation)), 195 CodeAlignmentFactor(CodeAlignmentFactor), 196 DataAlignmentFactor(DataAlignmentFactor), 197 ReturnAddressRegister(ReturnAddressRegister) {} 198 199 ~CIE() { 200 } 201 202 uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; } 203 int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; } 204 205 void dumpHeader(raw_ostream &OS) const override { 206 OS << format("%08x %08x %08x CIE", 207 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID) 208 << "\n"; 209 OS << format(" Version: %d\n", Version); 210 OS << " Augmentation: \"" << Augmentation << "\"\n"; 211 OS << format(" Code alignment factor: %u\n", 212 (uint32_t)CodeAlignmentFactor); 213 OS << format(" Data alignment factor: %d\n", 214 (int32_t)DataAlignmentFactor); 215 OS << format(" Return address column: %d\n", 216 (int32_t)ReturnAddressRegister); 217 OS << "\n"; 218 } 219 220 static bool classof(const FrameEntry *FE) { 221 return FE->getKind() == FK_CIE; 222 } 223 224 private: 225 /// The following fields are defined in section 6.4.1 of the DWARF standard v3 226 uint8_t Version; 227 SmallString<8> Augmentation; 228 uint64_t CodeAlignmentFactor; 229 int64_t DataAlignmentFactor; 230 uint64_t ReturnAddressRegister; 231 }; 232 233 234 /// \brief DWARF Frame Description Entry (FDE) 235 class FDE : public FrameEntry { 236 public: 237 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with 238 // an offset to the CIE (provided by parsing the FDE header). The CIE itself 239 // is obtained lazily once it's actually required. 240 FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset, 241 uint64_t InitialLocation, uint64_t AddressRange, 242 CIE *Cie) 243 : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset), 244 InitialLocation(InitialLocation), AddressRange(AddressRange), 245 LinkedCIE(Cie) {} 246 247 ~FDE() { 248 } 249 250 CIE *getLinkedCIE() const { return LinkedCIE; } 251 252 void dumpHeader(raw_ostream &OS) const override { 253 OS << format("%08x %08x %08x FDE ", 254 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset); 255 OS << format("cie=%08x pc=%08x...%08x\n", 256 (int32_t)LinkedCIEOffset, 257 (uint32_t)InitialLocation, 258 (uint32_t)InitialLocation + (uint32_t)AddressRange); 259 } 260 261 static bool classof(const FrameEntry *FE) { 262 return FE->getKind() == FK_FDE; 263 } 264 265 private: 266 /// The following fields are defined in section 6.4.1 of the DWARF standard v3 267 uint64_t LinkedCIEOffset; 268 uint64_t InitialLocation; 269 uint64_t AddressRange; 270 CIE *LinkedCIE; 271 }; 272 273 /// \brief Types of operands to CF instructions. 274 enum OperandType { 275 OT_Unset, 276 OT_None, 277 OT_Address, 278 OT_Offset, 279 OT_FactoredCodeOffset, 280 OT_SignedFactDataOffset, 281 OT_UnsignedFactDataOffset, 282 OT_Register, 283 OT_Expression 284 }; 285 286 } // end anonymous namespace 287 288 /// \brief Initialize the array describing the types of operands. 289 static ArrayRef<OperandType[2]> getOperandTypes() { 290 static OperandType OpTypes[DW_CFA_restore+1][2]; 291 292 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \ 293 do { \ 294 OpTypes[OP][0] = OPTYPE0; \ 295 OpTypes[OP][1] = OPTYPE1; \ 296 } while (0) 297 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None) 298 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None) 299 300 DECLARE_OP1(DW_CFA_set_loc, OT_Address); 301 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset); 302 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset); 303 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset); 304 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset); 305 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset); 306 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset); 307 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset); 308 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register); 309 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset); 310 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset); 311 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression); 312 DECLARE_OP1(DW_CFA_undefined, OT_Register); 313 DECLARE_OP1(DW_CFA_same_value, OT_Register); 314 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset); 315 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset); 316 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset); 317 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset); 318 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset); 319 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register); 320 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression); 321 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression); 322 DECLARE_OP1(DW_CFA_restore, OT_Register); 323 DECLARE_OP1(DW_CFA_restore_extended, OT_Register); 324 DECLARE_OP0(DW_CFA_remember_state); 325 DECLARE_OP0(DW_CFA_restore_state); 326 DECLARE_OP0(DW_CFA_GNU_window_save); 327 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset); 328 DECLARE_OP0(DW_CFA_nop); 329 330 #undef DECLARE_OP0 331 #undef DECLARE_OP1 332 #undef DECLARE_OP2 333 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1); 334 } 335 336 static ArrayRef<OperandType[2]> OpTypes = getOperandTypes(); 337 338 /// \brief Print \p Opcode's operand number \p OperandIdx which has 339 /// value \p Operand. 340 static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx, 341 uint64_t Operand, uint64_t CodeAlignmentFactor, 342 int64_t DataAlignmentFactor) { 343 assert(OperandIdx < 2); 344 OperandType Type = OpTypes[Opcode][OperandIdx]; 345 346 switch (Type) { 347 case OT_Unset: 348 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to"; 349 if (const char *OpcodeName = CallFrameString(Opcode)) 350 OS << " " << OpcodeName; 351 else 352 OS << format(" Opcode %x", Opcode); 353 break; 354 case OT_None: 355 break; 356 case OT_Address: 357 OS << format(" %" PRIx64, Operand); 358 break; 359 case OT_Offset: 360 // The offsets are all encoded in a unsigned form, but in practice 361 // consumers use them signed. It's most certainly legacy due to 362 // the lack of signed variants in the first Dwarf standards. 363 OS << format(" %+" PRId64, int64_t(Operand)); 364 break; 365 case OT_FactoredCodeOffset: // Always Unsigned 366 if (CodeAlignmentFactor) 367 OS << format(" %" PRId64, Operand * CodeAlignmentFactor); 368 else 369 OS << format(" %" PRId64 "*code_alignment_factor" , Operand); 370 break; 371 case OT_SignedFactDataOffset: 372 if (DataAlignmentFactor) 373 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor); 374 else 375 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand)); 376 break; 377 case OT_UnsignedFactDataOffset: 378 if (DataAlignmentFactor) 379 OS << format(" %" PRId64, Operand * DataAlignmentFactor); 380 else 381 OS << format(" %" PRId64 "*data_alignment_factor" , Operand); 382 break; 383 case OT_Register: 384 OS << format(" reg%" PRId64, Operand); 385 break; 386 case OT_Expression: 387 OS << " expression"; 388 break; 389 } 390 } 391 392 void FrameEntry::dumpInstructions(raw_ostream &OS) const { 393 uint64_t CodeAlignmentFactor = 0; 394 int64_t DataAlignmentFactor = 0; 395 const CIE *Cie = dyn_cast<CIE>(this); 396 397 if (!Cie) 398 Cie = cast<FDE>(this)->getLinkedCIE(); 399 if (Cie) { 400 CodeAlignmentFactor = Cie->getCodeAlignmentFactor(); 401 DataAlignmentFactor = Cie->getDataAlignmentFactor(); 402 } 403 404 for (const auto &Instr : Instructions) { 405 uint8_t Opcode = Instr.Opcode; 406 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK) 407 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK; 408 OS << " " << CallFrameString(Opcode) << ":"; 409 for (unsigned i = 0; i < Instr.Ops.size(); ++i) 410 printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor, 411 DataAlignmentFactor); 412 OS << '\n'; 413 } 414 } 415 416 DWARFDebugFrame::DWARFDebugFrame() { 417 } 418 419 DWARFDebugFrame::~DWARFDebugFrame() { 420 } 421 422 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data, 423 uint32_t Offset, int Length) { 424 errs() << "DUMP: "; 425 for (int i = 0; i < Length; ++i) { 426 uint8_t c = Data.getU8(&Offset); 427 errs().write_hex(c); errs() << " "; 428 } 429 errs() << "\n"; 430 } 431 432 433 void DWARFDebugFrame::parse(DataExtractor Data) { 434 uint32_t Offset = 0; 435 DenseMap<uint32_t, CIE *> CIEs; 436 437 while (Data.isValidOffset(Offset)) { 438 uint32_t StartOffset = Offset; 439 440 bool IsDWARF64 = false; 441 uint64_t Length = Data.getU32(&Offset); 442 uint64_t Id; 443 444 if (Length == UINT32_MAX) { 445 // DWARF-64 is distinguished by the first 32 bits of the initial length 446 // field being 0xffffffff. Then, the next 64 bits are the actual entry 447 // length. 448 IsDWARF64 = true; 449 Length = Data.getU64(&Offset); 450 } 451 452 // At this point, Offset points to the next field after Length. 453 // Length is the structure size excluding itself. Compute an offset one 454 // past the end of the structure (needed to know how many instructions to 455 // read). 456 // TODO: For honest DWARF64 support, DataExtractor will have to treat 457 // offset_ptr as uint64_t* 458 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length); 459 460 // The Id field's size depends on the DWARF format 461 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4); 462 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID); 463 464 if (IsCIE) { 465 // Note: this is specifically DWARFv3 CIE header structure. It was 466 // changed in DWARFv4. We currently don't support reading DWARFv4 467 // here because LLVM itself does not emit it (and LLDB doesn't 468 // support it either). 469 uint8_t Version = Data.getU8(&Offset); 470 const char *Augmentation = Data.getCStr(&Offset); 471 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset); 472 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset); 473 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset); 474 475 auto Cie = make_unique<CIE>(StartOffset, Length, Version, 476 StringRef(Augmentation), CodeAlignmentFactor, 477 DataAlignmentFactor, ReturnAddressRegister); 478 CIEs[StartOffset] = Cie.get(); 479 Entries.emplace_back(std::move(Cie)); 480 } else { 481 // FDE 482 uint64_t CIEPointer = Id; 483 uint64_t InitialLocation = Data.getAddress(&Offset); 484 uint64_t AddressRange = Data.getAddress(&Offset); 485 486 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer, 487 InitialLocation, AddressRange, 488 CIEs[CIEPointer])); 489 } 490 491 Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset); 492 493 if (Offset != EndStructureOffset) { 494 std::string Str; 495 raw_string_ostream OS(Str); 496 OS << format("Parsing entry instructions at %lx failed", StartOffset); 497 report_fatal_error(Str); 498 } 499 } 500 } 501 502 503 void DWARFDebugFrame::dump(raw_ostream &OS) const { 504 OS << "\n"; 505 for (const auto &Entry : Entries) { 506 Entry->dumpHeader(OS); 507 Entry->dumpInstructions(OS); 508 OS << "\n"; 509 } 510 } 511 512