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