blob: 3f8ed43151c43d05844c338b2020a95d7e05f1b3 [file] [log] [blame]
Eli Benderskyfd08bc12013-02-05 23:30:58 +00001//===-- 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
Zachary Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
Frederic Rissc0dd7242015-02-25 21:30:22 +000011#include "llvm/ADT/ArrayRef.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000012#include "llvm/ADT/DenseMap.h"
Igor Laevsky03a670c2016-01-26 15:09:42 +000013#include "llvm/ADT/Optional.h"
Eli Benderskyfd08bc12013-02-05 23:30:58 +000014#include "llvm/ADT/SmallString.h"
Frederic Rissc0dd7242015-02-25 21:30:22 +000015#include "llvm/Support/Casting.h"
Eli Benderskyfd08bc12013-02-05 23:30:58 +000016#include "llvm/Support/DataTypes.h"
17#include "llvm/Support/Dwarf.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "llvm/Support/ErrorHandling.h"
Eli Benderskyfd08bc12013-02-05 23:30:58 +000019#include "llvm/Support/Format.h"
Eli Bendersky705085d2013-02-21 22:53:19 +000020#include "llvm/Support/raw_ostream.h"
21#include <string>
22#include <vector>
Eli Benderskyfd08bc12013-02-05 23:30:58 +000023
24using namespace llvm;
25using namespace dwarf;
26
27
Eli Bendersky58232062013-02-06 16:20:31 +000028/// \brief Abstract frame entry defining the common interface concrete
29/// entries implement.
Eli Benderskyfd08bc12013-02-05 23:30:58 +000030class llvm::FrameEntry {
31public:
32 enum FrameKind {FK_CIE, FK_FDE};
Alexey Samsonova08b1612014-04-28 23:00:06 +000033 FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length)
34 : Kind(K), Offset(Offset), Length(Length) {}
Eli Benderskyfd08bc12013-02-05 23:30:58 +000035
Eli Bendersky0b04cb02013-02-06 03:08:02 +000036 virtual ~FrameEntry() {
37 }
38
Eli Benderskyfd08bc12013-02-05 23:30:58 +000039 FrameKind getKind() const { return Kind; }
Eli Bendersky705085d2013-02-21 22:53:19 +000040 virtual uint64_t getOffset() const { return Offset; }
Eli Benderskyfd08bc12013-02-05 23:30:58 +000041
Alexey Samsonova08b1612014-04-28 23:00:06 +000042 /// \brief Parse and store a sequence of CFI instructions from Data,
43 /// starting at *Offset and ending at EndOffset. If everything
Eli Bendersky8e352e32013-02-22 00:50:48 +000044 /// goes well, *Offset should be equal to EndOffset when this method
Eli Bendersky705085d2013-02-21 22:53:19 +000045 /// returns. Otherwise, an error occurred.
Alexey Samsonova08b1612014-04-28 23:00:06 +000046 virtual void parseInstructions(DataExtractor Data, uint32_t *Offset,
47 uint32_t EndOffset);
Eli Bendersky705085d2013-02-21 22:53:19 +000048
49 /// \brief Dump the entry header to the given output stream.
Eli Benderskyfd08bc12013-02-05 23:30:58 +000050 virtual void dumpHeader(raw_ostream &OS) const = 0;
Eli Bendersky1aa265d2013-02-06 00:20:38 +000051
Eli Bendersky705085d2013-02-21 22:53:19 +000052 /// \brief Dump the entry's instructions to the given output stream.
53 virtual void dumpInstructions(raw_ostream &OS) const;
54
Eli Benderskyfd08bc12013-02-05 23:30:58 +000055protected:
56 const FrameKind Kind;
Eli Bendersky1aa265d2013-02-06 00:20:38 +000057
Eli Bendersky1aa265d2013-02-06 00:20:38 +000058 /// \brief Offset of this entry in the section.
Eli Benderskyfd08bc12013-02-05 23:30:58 +000059 uint64_t Offset;
Eli Bendersky1aa265d2013-02-06 00:20:38 +000060
61 /// \brief Entry length as specified in DWARF.
Eli Benderskyfd08bc12013-02-05 23:30:58 +000062 uint64_t Length;
Eli Bendersky705085d2013-02-21 22:53:19 +000063
64 /// An entry may contain CFI instructions. An instruction consists of an
65 /// opcode and an optional sequence of operands.
66 typedef std::vector<uint64_t> Operands;
67 struct Instruction {
68 Instruction(uint8_t Opcode)
69 : Opcode(Opcode)
70 {}
71
72 uint8_t Opcode;
73 Operands Ops;
74 };
75
76 std::vector<Instruction> Instructions;
77
78 /// Convenience methods to add a new instruction with the given opcode and
79 /// operands to the Instructions vector.
80 void addInstruction(uint8_t Opcode) {
81 Instructions.push_back(Instruction(Opcode));
82 }
83
84 void addInstruction(uint8_t Opcode, uint64_t Operand1) {
85 Instructions.push_back(Instruction(Opcode));
86 Instructions.back().Ops.push_back(Operand1);
87 }
88
89 void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
90 Instructions.push_back(Instruction(Opcode));
91 Instructions.back().Ops.push_back(Operand1);
92 Instructions.back().Ops.push_back(Operand2);
93 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +000094};
95
Eli Bendersky705085d2013-02-21 22:53:19 +000096
97// See DWARF standard v3, section 7.23
98const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
99const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
100
Alexey Samsonova08b1612014-04-28 23:00:06 +0000101void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset,
102 uint32_t EndOffset) {
Eli Bendersky8e352e32013-02-22 00:50:48 +0000103 while (*Offset < EndOffset) {
104 uint8_t Opcode = Data.getU8(Offset);
Eli Bendersky705085d2013-02-21 22:53:19 +0000105 // Some instructions have a primary opcode encoded in the top bits.
106 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
107
108 if (Primary) {
109 // If it's a primary opcode, the first operand is encoded in the bottom
110 // bits of the opcode itself.
111 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
112 switch (Primary) {
113 default: llvm_unreachable("Impossible primary CFI opcode");
114 case DW_CFA_advance_loc:
115 case DW_CFA_restore:
116 addInstruction(Primary, Op1);
117 break;
118 case DW_CFA_offset:
Eli Bendersky8e352e32013-02-22 00:50:48 +0000119 addInstruction(Primary, Op1, Data.getULEB128(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000120 break;
121 }
122 } else {
123 // Extended opcode - its value is Opcode itself.
124 switch (Opcode) {
125 default: llvm_unreachable("Invalid extended CFI opcode");
126 case DW_CFA_nop:
127 case DW_CFA_remember_state:
128 case DW_CFA_restore_state:
Venkatraman Govindarajua302d292014-01-26 05:13:44 +0000129 case DW_CFA_GNU_window_save:
Eli Bendersky705085d2013-02-21 22:53:19 +0000130 // No operands
131 addInstruction(Opcode);
132 break;
133 case DW_CFA_set_loc:
134 // Operands: Address
Eli Bendersky8e352e32013-02-22 00:50:48 +0000135 addInstruction(Opcode, Data.getAddress(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000136 break;
137 case DW_CFA_advance_loc1:
138 // Operands: 1-byte delta
Eli Bendersky8e352e32013-02-22 00:50:48 +0000139 addInstruction(Opcode, Data.getU8(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000140 break;
141 case DW_CFA_advance_loc2:
142 // Operands: 2-byte delta
Eli Bendersky8e352e32013-02-22 00:50:48 +0000143 addInstruction(Opcode, Data.getU16(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000144 break;
145 case DW_CFA_advance_loc4:
146 // Operands: 4-byte delta
Eli Bendersky8e352e32013-02-22 00:50:48 +0000147 addInstruction(Opcode, Data.getU32(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000148 break;
149 case DW_CFA_restore_extended:
150 case DW_CFA_undefined:
151 case DW_CFA_same_value:
152 case DW_CFA_def_cfa_register:
153 case DW_CFA_def_cfa_offset:
154 // Operands: ULEB128
Eli Bendersky8e352e32013-02-22 00:50:48 +0000155 addInstruction(Opcode, Data.getULEB128(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000156 break;
157 case DW_CFA_def_cfa_offset_sf:
158 // Operands: SLEB128
Eli Bendersky8e352e32013-02-22 00:50:48 +0000159 addInstruction(Opcode, Data.getSLEB128(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000160 break;
161 case DW_CFA_offset_extended:
162 case DW_CFA_register:
163 case DW_CFA_def_cfa:
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000164 case DW_CFA_val_offset: {
Eli Bendersky705085d2013-02-21 22:53:19 +0000165 // Operands: ULEB128, ULEB128
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000166 // Note: We can not embed getULEB128 directly into function
167 // argument list. getULEB128 changes Offset and order of evaluation
168 // for arguments is unspecified.
169 auto op1 = Data.getULEB128(Offset);
170 auto op2 = Data.getULEB128(Offset);
171 addInstruction(Opcode, op1, op2);
Eli Bendersky705085d2013-02-21 22:53:19 +0000172 break;
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000173 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000174 case DW_CFA_offset_extended_sf:
175 case DW_CFA_def_cfa_sf:
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000176 case DW_CFA_val_offset_sf: {
Eli Bendersky705085d2013-02-21 22:53:19 +0000177 // Operands: ULEB128, SLEB128
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000178 // Note: see comment for the previous case
179 auto op1 = Data.getULEB128(Offset);
180 auto op2 = (uint64_t)Data.getSLEB128(Offset);
181 addInstruction(Opcode, op1, op2);
Eli Bendersky705085d2013-02-21 22:53:19 +0000182 break;
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000183 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000184 case DW_CFA_def_cfa_expression:
185 case DW_CFA_expression:
186 case DW_CFA_val_expression:
187 // TODO: implement this
188 report_fatal_error("Values with expressions not implemented yet!");
189 }
190 }
191 }
192}
193
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000194namespace {
Eli Bendersky58232062013-02-06 16:20:31 +0000195/// \brief DWARF Common Information Entry (CIE)
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000196class CIE : public FrameEntry {
197public:
198 // CIEs (and FDEs) are simply container classes, so the only sensible way to
199 // create them is by providing the full parsed contents in the constructor.
Alexey Samsonova08b1612014-04-28 23:00:06 +0000200 CIE(uint64_t Offset, uint64_t Length, uint8_t Version,
Keith Walkerea9483f2015-05-12 15:25:08 +0000201 SmallString<8> Augmentation, uint8_t AddressSize,
202 uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor,
Igor Laevsky03a670c2016-01-26 15:09:42 +0000203 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister,
Igor Laevskyff291b52016-01-27 14:05:35 +0000204 SmallString<8> AugmentationData, uint32_t FDEPointerEncoding,
205 uint32_t LSDAPointerEncoding)
Alexey Samsonova08b1612014-04-28 23:00:06 +0000206 : FrameEntry(FK_CIE, Offset, Length), Version(Version),
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000207 Augmentation(std::move(Augmentation)),
Keith Walkerea9483f2015-05-12 15:25:08 +0000208 AddressSize(AddressSize),
209 SegmentDescriptorSize(SegmentDescriptorSize),
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000210 CodeAlignmentFactor(CodeAlignmentFactor),
Alexey Samsonova08b1612014-04-28 23:00:06 +0000211 DataAlignmentFactor(DataAlignmentFactor),
Igor Laevsky03a670c2016-01-26 15:09:42 +0000212 ReturnAddressRegister(ReturnAddressRegister),
213 AugmentationData(AugmentationData),
214 FDEPointerEncoding(FDEPointerEncoding),
215 LSDAPointerEncoding(LSDAPointerEncoding) { }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000216
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000217 ~CIE() override {}
Eli Bendersky0b04cb02013-02-06 03:08:02 +0000218
Igor Laevsky03a670c2016-01-26 15:09:42 +0000219 StringRef getAugmentationString() const { return Augmentation; }
Frederic Riss41bb2c62015-02-25 21:30:13 +0000220 uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
221 int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
Igor Laevskyff291b52016-01-27 14:05:35 +0000222 uint32_t getFDEPointerEncoding() const {
Igor Laevsky03a670c2016-01-26 15:09:42 +0000223 return FDEPointerEncoding;
224 }
Igor Laevskyff291b52016-01-27 14:05:35 +0000225 uint32_t getLSDAPointerEncoding() const {
Igor Laevsky03a670c2016-01-26 15:09:42 +0000226 return LSDAPointerEncoding;
227 }
Frederic Riss41bb2c62015-02-25 21:30:13 +0000228
Craig Topper85482992014-03-05 07:52:44 +0000229 void dumpHeader(raw_ostream &OS) const override {
NAKAMURA Takumi56ac51a2013-02-07 02:02:27 +0000230 OS << format("%08x %08x %08x CIE",
231 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
232 << "\n";
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000233 OS << format(" Version: %d\n", Version);
234 OS << " Augmentation: \"" << Augmentation << "\"\n";
Keith Walkerea9483f2015-05-12 15:25:08 +0000235 if (Version >= 4) {
236 OS << format(" Address size: %u\n",
237 (uint32_t)AddressSize);
238 OS << format(" Segment desc size: %u\n",
239 (uint32_t)SegmentDescriptorSize);
240 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000241 OS << format(" Code alignment factor: %u\n",
242 (uint32_t)CodeAlignmentFactor);
243 OS << format(" Data alignment factor: %d\n",
244 (int32_t)DataAlignmentFactor);
245 OS << format(" Return address column: %d\n",
246 (int32_t)ReturnAddressRegister);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000247 if (!AugmentationData.empty())
248 OS << " Augmentation data: " << AugmentationData << "\n";
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000249 OS << "\n";
250 }
251
252 static bool classof(const FrameEntry *FE) {
253 return FE->getKind() == FK_CIE;
Alexey Samsonova08b1612014-04-28 23:00:06 +0000254 }
Eli Bendersky1aa265d2013-02-06 00:20:38 +0000255
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000256private:
Keith Walkerea9483f2015-05-12 15:25:08 +0000257 /// The following fields are defined in section 6.4.1 of the DWARF standard v4
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000258 uint8_t Version;
259 SmallString<8> Augmentation;
Keith Walkerea9483f2015-05-12 15:25:08 +0000260 uint8_t AddressSize;
261 uint8_t SegmentDescriptorSize;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000262 uint64_t CodeAlignmentFactor;
263 int64_t DataAlignmentFactor;
264 uint64_t ReturnAddressRegister;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000265
266 // The following are used when the CIE represents an EH frame entry.
267 SmallString<8> AugmentationData;
Igor Laevskyff291b52016-01-27 14:05:35 +0000268 uint32_t FDEPointerEncoding;
269 uint32_t LSDAPointerEncoding;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000270};
271
272
Eli Bendersky58232062013-02-06 16:20:31 +0000273/// \brief DWARF Frame Description Entry (FDE)
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000274class FDE : public FrameEntry {
275public:
276 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
277 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
278 // is obtained lazily once it's actually required.
Alexey Samsonova08b1612014-04-28 23:00:06 +0000279 FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset,
Frederic Rissbaf195f2015-02-25 21:30:09 +0000280 uint64_t InitialLocation, uint64_t AddressRange,
281 CIE *Cie)
Alexey Samsonova08b1612014-04-28 23:00:06 +0000282 : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
283 InitialLocation(InitialLocation), AddressRange(AddressRange),
Frederic Rissbaf195f2015-02-25 21:30:09 +0000284 LinkedCIE(Cie) {}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000285
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000286 ~FDE() override {}
Eli Bendersky0b04cb02013-02-06 03:08:02 +0000287
Frederic Riss41bb2c62015-02-25 21:30:13 +0000288 CIE *getLinkedCIE() const { return LinkedCIE; }
289
Craig Topper85482992014-03-05 07:52:44 +0000290 void dumpHeader(raw_ostream &OS) const override {
NAKAMURA Takumi56ac51a2013-02-07 02:02:27 +0000291 OS << format("%08x %08x %08x FDE ",
NAKAMURA Takumi14727d72013-02-07 14:54:42 +0000292 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000293 OS << format("cie=%08x pc=%08x...%08x\n",
NAKAMURA Takumi14727d72013-02-07 14:54:42 +0000294 (int32_t)LinkedCIEOffset,
NAKAMURA Takumi94651f92013-02-07 10:57:42 +0000295 (uint32_t)InitialLocation,
296 (uint32_t)InitialLocation + (uint32_t)AddressRange);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000297 }
298
299 static bool classof(const FrameEntry *FE) {
300 return FE->getKind() == FK_FDE;
Alexey Samsonova08b1612014-04-28 23:00:06 +0000301 }
Eli Bendersky1aa265d2013-02-06 00:20:38 +0000302
Alexey Samsonova08b1612014-04-28 23:00:06 +0000303private:
Eli Bendersky1aa265d2013-02-06 00:20:38 +0000304 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000305 uint64_t LinkedCIEOffset;
306 uint64_t InitialLocation;
307 uint64_t AddressRange;
308 CIE *LinkedCIE;
309};
Frederic Rissc0dd7242015-02-25 21:30:22 +0000310
311/// \brief Types of operands to CF instructions.
312enum OperandType {
313 OT_Unset,
314 OT_None,
315 OT_Address,
316 OT_Offset,
317 OT_FactoredCodeOffset,
318 OT_SignedFactDataOffset,
319 OT_UnsignedFactDataOffset,
320 OT_Register,
321 OT_Expression
322};
323
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000324} // end anonymous namespace
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000325
Frederic Rissc0dd7242015-02-25 21:30:22 +0000326/// \brief Initialize the array describing the types of operands.
327static ArrayRef<OperandType[2]> getOperandTypes() {
328 static OperandType OpTypes[DW_CFA_restore+1][2];
329
330#define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
331 do { \
332 OpTypes[OP][0] = OPTYPE0; \
333 OpTypes[OP][1] = OPTYPE1; \
334 } while (0)
335#define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
336#define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
337
338 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
339 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
340 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
341 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
342 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
343 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
344 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
345 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
346 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
347 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
348 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
349 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
350 DECLARE_OP1(DW_CFA_undefined, OT_Register);
351 DECLARE_OP1(DW_CFA_same_value, OT_Register);
352 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
353 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
354 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
355 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
356 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
357 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
358 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
359 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
360 DECLARE_OP1(DW_CFA_restore, OT_Register);
361 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
362 DECLARE_OP0(DW_CFA_remember_state);
363 DECLARE_OP0(DW_CFA_restore_state);
364 DECLARE_OP0(DW_CFA_GNU_window_save);
365 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
366 DECLARE_OP0(DW_CFA_nop);
367
368#undef DECLARE_OP0
369#undef DECLARE_OP1
370#undef DECLARE_OP2
Frederic Rissac10b0d2015-02-25 22:07:43 +0000371 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
Frederic Rissc0dd7242015-02-25 21:30:22 +0000372}
373
374static ArrayRef<OperandType[2]> OpTypes = getOperandTypes();
375
376/// \brief Print \p Opcode's operand number \p OperandIdx which has
377/// value \p Operand.
378static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx,
379 uint64_t Operand, uint64_t CodeAlignmentFactor,
380 int64_t DataAlignmentFactor) {
381 assert(OperandIdx < 2);
382 OperandType Type = OpTypes[Opcode][OperandIdx];
383
384 switch (Type) {
385 case OT_Unset:
386 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
387 if (const char *OpcodeName = CallFrameString(Opcode))
388 OS << " " << OpcodeName;
389 else
390 OS << format(" Opcode %x", Opcode);
391 break;
392 case OT_None:
393 break;
394 case OT_Address:
395 OS << format(" %" PRIx64, Operand);
396 break;
397 case OT_Offset:
398 // The offsets are all encoded in a unsigned form, but in practice
399 // consumers use them signed. It's most certainly legacy due to
400 // the lack of signed variants in the first Dwarf standards.
401 OS << format(" %+" PRId64, int64_t(Operand));
402 break;
403 case OT_FactoredCodeOffset: // Always Unsigned
404 if (CodeAlignmentFactor)
405 OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
406 else
407 OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
408 break;
409 case OT_SignedFactDataOffset:
410 if (DataAlignmentFactor)
411 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
412 else
413 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
414 break;
415 case OT_UnsignedFactDataOffset:
416 if (DataAlignmentFactor)
417 OS << format(" %" PRId64, Operand * DataAlignmentFactor);
418 else
419 OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
420 break;
421 case OT_Register:
Frederic Rissde374342015-02-25 22:30:09 +0000422 OS << format(" reg%" PRId64, Operand);
Frederic Rissc0dd7242015-02-25 21:30:22 +0000423 break;
424 case OT_Expression:
425 OS << " expression";
426 break;
427 }
428}
429
Frederic Riss056ad052015-02-25 21:30:16 +0000430void FrameEntry::dumpInstructions(raw_ostream &OS) const {
Frederic Rissc0dd7242015-02-25 21:30:22 +0000431 uint64_t CodeAlignmentFactor = 0;
432 int64_t DataAlignmentFactor = 0;
433 const CIE *Cie = dyn_cast<CIE>(this);
434
435 if (!Cie)
436 Cie = cast<FDE>(this)->getLinkedCIE();
437 if (Cie) {
438 CodeAlignmentFactor = Cie->getCodeAlignmentFactor();
439 DataAlignmentFactor = Cie->getDataAlignmentFactor();
440 }
441
Frederic Riss056ad052015-02-25 21:30:16 +0000442 for (const auto &Instr : Instructions) {
443 uint8_t Opcode = Instr.Opcode;
444 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
445 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
Frederic Rissc0dd7242015-02-25 21:30:22 +0000446 OS << " " << CallFrameString(Opcode) << ":";
447 for (unsigned i = 0; i < Instr.Ops.size(); ++i)
448 printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor,
449 DataAlignmentFactor);
450 OS << '\n';
Frederic Riss056ad052015-02-25 21:30:16 +0000451 }
452}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000453
Igor Laevsky03a670c2016-01-26 15:09:42 +0000454DWARFDebugFrame::DWARFDebugFrame(bool IsEH) : IsEH(IsEH) {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000455}
456
Eli Bendersky1aa265d2013-02-06 00:20:38 +0000457DWARFDebugFrame::~DWARFDebugFrame() {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000458}
459
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000460static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
461 uint32_t Offset, int Length) {
462 errs() << "DUMP: ";
463 for (int i = 0; i < Length; ++i) {
464 uint8_t c = Data.getU8(&Offset);
465 errs().write_hex(c); errs() << " ";
466 }
467 errs() << "\n";
468}
469
Igor Laevsky03a670c2016-01-26 15:09:42 +0000470static unsigned getSizeForEncoding(const DataExtractor &Data,
471 unsigned symbolEncoding) {
472 unsigned format = symbolEncoding & 0x0f;
473 switch (format) {
474 default: llvm_unreachable("Unknown Encoding");
475 case dwarf::DW_EH_PE_absptr:
476 case dwarf::DW_EH_PE_signed:
477 return Data.getAddressSize();
478 case dwarf::DW_EH_PE_udata2:
479 case dwarf::DW_EH_PE_sdata2:
480 return 2;
481 case dwarf::DW_EH_PE_udata4:
482 case dwarf::DW_EH_PE_sdata4:
483 return 4;
484 case dwarf::DW_EH_PE_udata8:
485 case dwarf::DW_EH_PE_sdata8:
486 return 8;
487 }
488}
489
490static uint64_t readPointer(const DataExtractor &Data, uint32_t &Offset,
491 unsigned Encoding) {
492 switch (getSizeForEncoding(Data, Encoding)) {
493 case 2:
494 return Data.getU16(&Offset);
495 case 4:
496 return Data.getU32(&Offset);
497 case 8:
498 return Data.getU64(&Offset);
499 default:
500 llvm_unreachable("Illegal data size");
501 }
502}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000503
504void DWARFDebugFrame::parse(DataExtractor Data) {
505 uint32_t Offset = 0;
Frederic Rissbaf195f2015-02-25 21:30:09 +0000506 DenseMap<uint32_t, CIE *> CIEs;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000507
508 while (Data.isValidOffset(Offset)) {
509 uint32_t StartOffset = Offset;
510
Igor Laevsky03a670c2016-01-26 15:09:42 +0000511 auto ReportError = [StartOffset](const char *ErrorMsg) {
512 std::string Str;
513 raw_string_ostream OS(Str);
514 OS << format(ErrorMsg, StartOffset);
515 OS.flush();
516 report_fatal_error(Str);
517 };
518
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000519 bool IsDWARF64 = false;
520 uint64_t Length = Data.getU32(&Offset);
521 uint64_t Id;
522
523 if (Length == UINT32_MAX) {
524 // DWARF-64 is distinguished by the first 32 bits of the initial length
525 // field being 0xffffffff. Then, the next 64 bits are the actual entry
526 // length.
527 IsDWARF64 = true;
528 Length = Data.getU64(&Offset);
529 }
530
531 // At this point, Offset points to the next field after Length.
532 // Length is the structure size excluding itself. Compute an offset one
533 // past the end of the structure (needed to know how many instructions to
534 // read).
535 // TODO: For honest DWARF64 support, DataExtractor will have to treat
536 // offset_ptr as uint64_t*
Igor Laevsky03a670c2016-01-26 15:09:42 +0000537 uint32_t StartStructureOffset = Offset;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000538 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
539
540 // The Id field's size depends on the DWARF format
Igor Laevsky03a670c2016-01-26 15:09:42 +0000541 Id = Data.getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4);
542 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) ||
543 Id == DW_CIE_ID ||
544 (IsEH && !Id));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000545
546 if (IsCIE) {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000547 uint8_t Version = Data.getU8(&Offset);
548 const char *Augmentation = Data.getCStr(&Offset);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000549 StringRef AugmentationString(Augmentation ? Augmentation : "");
550 uint8_t AddressSize = Version < 4 ? Data.getAddressSize() :
551 Data.getU8(&Offset);
Keith Walkerea9483f2015-05-12 15:25:08 +0000552 Data.setAddressSize(AddressSize);
553 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000554 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
555 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
556 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
557
Igor Laevsky03a670c2016-01-26 15:09:42 +0000558 // Parse the augmentation data for EH CIEs
Igor Laevskyff291b52016-01-27 14:05:35 +0000559 StringRef AugmentationData("");
560 uint32_t FDEPointerEncoding = DW_EH_PE_omit;
561 uint32_t LSDAPointerEncoding = DW_EH_PE_omit;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000562 if (IsEH) {
563 Optional<uint32_t> PersonalityEncoding;
564 Optional<uint64_t> Personality;
565
Igor Laevskyff291b52016-01-27 14:05:35 +0000566 Optional<uint64_t> AugmentationLength;
567 uint32_t StartAugmentationOffset;
568 uint32_t EndAugmentationOffset;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000569
570 // Walk the augmentation string to get all the augmentation data.
571 for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) {
572 switch (AugmentationString[i]) {
573 default:
574 ReportError("Unknown augmentation character in entry at %lx");
575 case 'L':
Igor Laevsky03a670c2016-01-26 15:09:42 +0000576 LSDAPointerEncoding = Data.getU8(&Offset);
577 break;
578 case 'P': {
579 if (Personality)
580 ReportError("Duplicate personality in entry at %lx");
581 PersonalityEncoding = Data.getU8(&Offset);
582 Personality = readPointer(Data, Offset, *PersonalityEncoding);
583 break;
584 }
585 case 'R':
Igor Laevsky03a670c2016-01-26 15:09:42 +0000586 FDEPointerEncoding = Data.getU8(&Offset);
587 break;
588 case 'z':
589 if (i)
590 ReportError("'z' must be the first character at %lx");
591 // Parse the augmentation length first. We only parse it if
592 // the string contains a 'z'.
593 AugmentationLength = Data.getULEB128(&Offset);
594 StartAugmentationOffset = Offset;
Igor Laevskyff291b52016-01-27 14:05:35 +0000595 EndAugmentationOffset = Offset +
596 static_cast<uint32_t>(*AugmentationLength);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000597 }
598 }
599
Igor Laevskyff291b52016-01-27 14:05:35 +0000600 if (AugmentationLength.hasValue()) {
601 if (Offset != EndAugmentationOffset)
602 ReportError("Parsing augmentation data at %lx failed");
Igor Laevsky03a670c2016-01-26 15:09:42 +0000603
Igor Laevskyff291b52016-01-27 14:05:35 +0000604 AugmentationData = Data.getData().slice(StartAugmentationOffset,
605 EndAugmentationOffset);
606 }
Igor Laevsky03a670c2016-01-26 15:09:42 +0000607 }
608
Frederic Rissbaf195f2015-02-25 21:30:09 +0000609 auto Cie = make_unique<CIE>(StartOffset, Length, Version,
Igor Laevskyff291b52016-01-27 14:05:35 +0000610 AugmentationString, AddressSize,
Keith Walkerea9483f2015-05-12 15:25:08 +0000611 SegmentDescriptorSize, CodeAlignmentFactor,
Igor Laevsky03a670c2016-01-26 15:09:42 +0000612 DataAlignmentFactor, ReturnAddressRegister,
613 AugmentationData, FDEPointerEncoding,
614 LSDAPointerEncoding);
Frederic Rissbaf195f2015-02-25 21:30:09 +0000615 CIEs[StartOffset] = Cie.get();
616 Entries.emplace_back(std::move(Cie));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000617 } else {
618 // FDE
619 uint64_t CIEPointer = Id;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000620 uint64_t InitialLocation = 0;
621 uint64_t AddressRange = 0;
622 CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
623
624 if (IsEH) {
625 // The address size is encoded in the CIE we reference.
626 if (!Cie)
627 ReportError("Parsing FDE data at %lx failed due to missing CIE");
628
Igor Laevskyff291b52016-01-27 14:05:35 +0000629 InitialLocation = readPointer(Data, Offset,
630 Cie->getFDEPointerEncoding());
631 AddressRange = readPointer(Data, Offset,
632 Cie->getFDEPointerEncoding());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000633
Igor Laevsky03a670c2016-01-26 15:09:42 +0000634 StringRef AugmentationString = Cie->getAugmentationString();
635 if (!AugmentationString.empty()) {
636 // Parse the augmentation length and data for this FDE.
637 uint64_t AugmentationLength = Data.getULEB128(&Offset);
638
639 uint32_t EndAugmentationOffset =
640 Offset + static_cast<uint32_t>(AugmentationLength);
641
642 // Decode the LSDA if the CIE augmentation string said we should.
643 uint64_t LSDA = 0;
Igor Laevskyff291b52016-01-27 14:05:35 +0000644 if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit)
645 LSDA = readPointer(Data, Offset, Cie->getLSDAPointerEncoding());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000646
647 if (Offset != EndAugmentationOffset)
648 ReportError("Parsing augmentation data at %lx failed");
649 }
650 } else {
651 InitialLocation = Data.getAddress(&Offset);
652 AddressRange = Data.getAddress(&Offset);
653 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000654
Alexey Samsonova08b1612014-04-28 23:00:06 +0000655 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
Frederic Rissbaf195f2015-02-25 21:30:09 +0000656 InitialLocation, AddressRange,
Igor Laevsky03a670c2016-01-26 15:09:42 +0000657 Cie));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000658 }
659
Alexey Samsonova08b1612014-04-28 23:00:06 +0000660 Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset);
Eli Bendersky705085d2013-02-21 22:53:19 +0000661
Igor Laevsky03a670c2016-01-26 15:09:42 +0000662 if (Offset != EndStructureOffset)
663 ReportError("Parsing entry instructions at %lx failed");
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000664 }
665}
666
667
668void DWARFDebugFrame::dump(raw_ostream &OS) const {
669 OS << "\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000670 for (const auto &Entry : Entries) {
Eli Bendersky705085d2013-02-21 22:53:19 +0000671 Entry->dumpHeader(OS);
672 Entry->dumpInstructions(OS);
673 OS << "\n";
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000674 }
675}
676