dwarfdump: Use the index to find the right abbrev offset in DWP files
[oota-llvm.git] / lib / DebugInfo / DWARF / DWARFDebugFrame.cpp
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, uint8_t AddressSize,
193       uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor,
194       int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
195       : FrameEntry(FK_CIE, Offset, Length), Version(Version),
196         Augmentation(std::move(Augmentation)),
197         AddressSize(AddressSize),
198         SegmentDescriptorSize(SegmentDescriptorSize),
199         CodeAlignmentFactor(CodeAlignmentFactor),
200         DataAlignmentFactor(DataAlignmentFactor),
201         ReturnAddressRegister(ReturnAddressRegister) {}
202
203   ~CIE() override {}
204
205   uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
206   int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
207
208   void dumpHeader(raw_ostream &OS) const override {
209     OS << format("%08x %08x %08x CIE",
210                  (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
211        << "\n";
212     OS << format("  Version:               %d\n", Version);
213     OS << "  Augmentation:          \"" << Augmentation << "\"\n";
214     if (Version >= 4) {
215       OS << format("  Address size:          %u\n",
216                    (uint32_t)AddressSize);
217       OS << format("  Segment desc size:     %u\n",
218                    (uint32_t)SegmentDescriptorSize);
219     }
220     OS << format("  Code alignment factor: %u\n",
221                  (uint32_t)CodeAlignmentFactor);
222     OS << format("  Data alignment factor: %d\n",
223                  (int32_t)DataAlignmentFactor);
224     OS << format("  Return address column: %d\n",
225                  (int32_t)ReturnAddressRegister);
226     OS << "\n";
227   }
228
229   static bool classof(const FrameEntry *FE) {
230     return FE->getKind() == FK_CIE;
231   }
232
233 private:
234   /// The following fields are defined in section 6.4.1 of the DWARF standard v4
235   uint8_t Version;
236   SmallString<8> Augmentation;
237   uint8_t AddressSize;
238   uint8_t SegmentDescriptorSize;
239   uint64_t CodeAlignmentFactor;
240   int64_t DataAlignmentFactor;
241   uint64_t ReturnAddressRegister;
242 };
243
244
245 /// \brief DWARF Frame Description Entry (FDE)
246 class FDE : public FrameEntry {
247 public:
248   // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
249   // an offset to the CIE (provided by parsing the FDE header). The CIE itself
250   // is obtained lazily once it's actually required.
251   FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset,
252       uint64_t InitialLocation, uint64_t AddressRange,
253       CIE *Cie)
254       : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
255         InitialLocation(InitialLocation), AddressRange(AddressRange),
256         LinkedCIE(Cie) {}
257
258   ~FDE() override {}
259
260   CIE *getLinkedCIE() const { return LinkedCIE; }
261
262   void dumpHeader(raw_ostream &OS) const override {
263     OS << format("%08x %08x %08x FDE ",
264                  (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
265     OS << format("cie=%08x pc=%08x...%08x\n",
266                  (int32_t)LinkedCIEOffset,
267                  (uint32_t)InitialLocation,
268                  (uint32_t)InitialLocation + (uint32_t)AddressRange);
269   }
270
271   static bool classof(const FrameEntry *FE) {
272     return FE->getKind() == FK_FDE;
273   }
274
275 private:
276   /// The following fields are defined in section 6.4.1 of the DWARF standard v3
277   uint64_t LinkedCIEOffset;
278   uint64_t InitialLocation;
279   uint64_t AddressRange;
280   CIE *LinkedCIE;
281 };
282
283 /// \brief Types of operands to CF instructions.
284 enum OperandType {
285   OT_Unset,
286   OT_None,
287   OT_Address,
288   OT_Offset,
289   OT_FactoredCodeOffset,
290   OT_SignedFactDataOffset,
291   OT_UnsignedFactDataOffset,
292   OT_Register,
293   OT_Expression
294 };
295
296 } // end anonymous namespace
297
298 /// \brief Initialize the array describing the types of operands.
299 static ArrayRef<OperandType[2]> getOperandTypes() {
300   static OperandType OpTypes[DW_CFA_restore+1][2];
301
302 #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1)       \
303   do {                                          \
304     OpTypes[OP][0] = OPTYPE0;                   \
305     OpTypes[OP][1] = OPTYPE1;                   \
306   } while (0)
307 #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
308 #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
309
310   DECLARE_OP1(DW_CFA_set_loc, OT_Address);
311   DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
312   DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
313   DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
314   DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
315   DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
316   DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
317   DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
318   DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
319   DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
320   DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
321   DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
322   DECLARE_OP1(DW_CFA_undefined, OT_Register);
323   DECLARE_OP1(DW_CFA_same_value, OT_Register);
324   DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
325   DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
326   DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
327   DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
328   DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
329   DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
330   DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
331   DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
332   DECLARE_OP1(DW_CFA_restore, OT_Register);
333   DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
334   DECLARE_OP0(DW_CFA_remember_state);
335   DECLARE_OP0(DW_CFA_restore_state);
336   DECLARE_OP0(DW_CFA_GNU_window_save);
337   DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
338   DECLARE_OP0(DW_CFA_nop);
339
340 #undef DECLARE_OP0
341 #undef DECLARE_OP1
342 #undef DECLARE_OP2
343   return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
344 }
345
346 static ArrayRef<OperandType[2]> OpTypes = getOperandTypes();
347
348 /// \brief Print \p Opcode's operand number \p OperandIdx which has
349 /// value \p Operand.
350 static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx,
351                          uint64_t Operand, uint64_t CodeAlignmentFactor,
352                          int64_t DataAlignmentFactor) {
353   assert(OperandIdx < 2);
354   OperandType Type = OpTypes[Opcode][OperandIdx];
355
356   switch (Type) {
357   case OT_Unset:
358     OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
359     if (const char *OpcodeName = CallFrameString(Opcode))
360       OS << " " << OpcodeName;
361     else
362       OS << format(" Opcode %x",  Opcode);
363     break;
364   case OT_None:
365     break;
366   case OT_Address:
367     OS << format(" %" PRIx64, Operand);
368     break;
369   case OT_Offset:
370     // The offsets are all encoded in a unsigned form, but in practice
371     // consumers use them signed. It's most certainly legacy due to
372     // the lack of signed variants in the first Dwarf standards.
373     OS << format(" %+" PRId64, int64_t(Operand));
374     break;
375   case OT_FactoredCodeOffset: // Always Unsigned
376     if (CodeAlignmentFactor)
377       OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
378     else
379       OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
380     break;
381   case OT_SignedFactDataOffset:
382     if (DataAlignmentFactor)
383       OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
384     else
385       OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
386     break;
387   case OT_UnsignedFactDataOffset:
388     if (DataAlignmentFactor)
389       OS << format(" %" PRId64, Operand * DataAlignmentFactor);
390     else
391       OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
392     break;
393   case OT_Register:
394     OS << format(" reg%" PRId64, Operand);
395     break;
396   case OT_Expression:
397     OS << " expression";
398     break;
399   }
400 }
401
402 void FrameEntry::dumpInstructions(raw_ostream &OS) const {
403   uint64_t CodeAlignmentFactor = 0;
404   int64_t DataAlignmentFactor = 0;
405   const CIE *Cie = dyn_cast<CIE>(this);
406
407   if (!Cie)
408     Cie = cast<FDE>(this)->getLinkedCIE();
409   if (Cie) {
410     CodeAlignmentFactor = Cie->getCodeAlignmentFactor();
411     DataAlignmentFactor = Cie->getDataAlignmentFactor();
412   }
413
414   for (const auto &Instr : Instructions) {
415     uint8_t Opcode = Instr.Opcode;
416     if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
417       Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
418     OS << "  " << CallFrameString(Opcode) << ":";
419     for (unsigned i = 0; i < Instr.Ops.size(); ++i)
420       printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor,
421                    DataAlignmentFactor);
422     OS << '\n';
423   }
424 }
425
426 DWARFDebugFrame::DWARFDebugFrame() {
427 }
428
429 DWARFDebugFrame::~DWARFDebugFrame() {
430 }
431
432 static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
433                                               uint32_t Offset, int Length) {
434   errs() << "DUMP: ";
435   for (int i = 0; i < Length; ++i) {
436     uint8_t c = Data.getU8(&Offset);
437     errs().write_hex(c); errs() << " ";
438   }
439   errs() << "\n";
440 }
441
442
443 void DWARFDebugFrame::parse(DataExtractor Data) {
444   uint32_t Offset = 0;
445   DenseMap<uint32_t, CIE *> CIEs;
446
447   while (Data.isValidOffset(Offset)) {
448     uint32_t StartOffset = Offset;
449
450     bool IsDWARF64 = false;
451     uint64_t Length = Data.getU32(&Offset);
452     uint64_t Id;
453
454     if (Length == UINT32_MAX) {
455       // DWARF-64 is distinguished by the first 32 bits of the initial length
456       // field being 0xffffffff. Then, the next 64 bits are the actual entry
457       // length.
458       IsDWARF64 = true;
459       Length = Data.getU64(&Offset);
460     }
461
462     // At this point, Offset points to the next field after Length.
463     // Length is the structure size excluding itself. Compute an offset one
464     // past the end of the structure (needed to know how many instructions to
465     // read).
466     // TODO: For honest DWARF64 support, DataExtractor will have to treat
467     //       offset_ptr as uint64_t*
468     uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
469
470     // The Id field's size depends on the DWARF format
471     Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
472     bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
473
474     if (IsCIE) {
475       uint8_t Version = Data.getU8(&Offset);
476       const char *Augmentation = Data.getCStr(&Offset);
477       uint8_t AddressSize = Version < 4 ? Data.getAddressSize() : Data.getU8(&Offset);
478       Data.setAddressSize(AddressSize);
479       uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
480       uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
481       int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
482       uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
483
484       auto Cie = make_unique<CIE>(StartOffset, Length, Version,
485                                   StringRef(Augmentation), AddressSize,
486                                   SegmentDescriptorSize, CodeAlignmentFactor,
487                                   DataAlignmentFactor, ReturnAddressRegister);
488       CIEs[StartOffset] = Cie.get();
489       Entries.emplace_back(std::move(Cie));
490     } else {
491       // FDE
492       uint64_t CIEPointer = Id;
493       uint64_t InitialLocation = Data.getAddress(&Offset);
494       uint64_t AddressRange = Data.getAddress(&Offset);
495
496       Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
497                                    InitialLocation, AddressRange,
498                                    CIEs[CIEPointer]));
499     }
500
501     Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset);
502
503     if (Offset != EndStructureOffset) {
504       std::string Str;
505       raw_string_ostream OS(Str);
506       OS << format("Parsing entry instructions at %lx failed", StartOffset);
507       report_fatal_error(Str);
508     }
509   }
510 }
511
512
513 void DWARFDebugFrame::dump(raw_ostream &OS) const {
514   OS << "\n";
515   for (const auto &Entry : Entries) {
516     Entry->dumpHeader(OS);
517     Entry->dumpInstructions(OS);
518     OS << "\n";
519   }
520 }
521