blob: 9b6a9a788230d2ec25328773ea99f5bad126efc3 [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"
Simon Atanasyanf69c7e52016-03-01 18:38:05 +000015#include "llvm/ADT/StringExtras.h"
Frederic Rissc0dd7242015-02-25 21:30:22 +000016#include "llvm/Support/Casting.h"
Eli Benderskyfd08bc12013-02-05 23:30:58 +000017#include "llvm/Support/DataTypes.h"
18#include "llvm/Support/Dwarf.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/Support/ErrorHandling.h"
Eli Benderskyfd08bc12013-02-05 23:30:58 +000020#include "llvm/Support/Format.h"
Eli Bendersky705085d2013-02-21 22:53:19 +000021#include "llvm/Support/raw_ostream.h"
22#include <string>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000023#include <utility>
Eli Bendersky705085d2013-02-21 22:53:19 +000024#include <vector>
Eli Benderskyfd08bc12013-02-05 23:30:58 +000025
26using namespace llvm;
27using namespace dwarf;
28
29
Eli Bendersky58232062013-02-06 16:20:31 +000030/// \brief Abstract frame entry defining the common interface concrete
31/// entries implement.
Eli Benderskyfd08bc12013-02-05 23:30:58 +000032class llvm::FrameEntry {
33public:
34 enum FrameKind {FK_CIE, FK_FDE};
Alexey Samsonova08b1612014-04-28 23:00:06 +000035 FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length)
36 : Kind(K), Offset(Offset), Length(Length) {}
Eli Benderskyfd08bc12013-02-05 23:30:58 +000037
Eli Bendersky0b04cb02013-02-06 03:08:02 +000038 virtual ~FrameEntry() {
39 }
40
Eli Benderskyfd08bc12013-02-05 23:30:58 +000041 FrameKind getKind() const { return Kind; }
Eli Bendersky705085d2013-02-21 22:53:19 +000042 virtual uint64_t getOffset() const { return Offset; }
Eli Benderskyfd08bc12013-02-05 23:30:58 +000043
Alexey Samsonova08b1612014-04-28 23:00:06 +000044 /// \brief Parse and store a sequence of CFI instructions from Data,
45 /// starting at *Offset and ending at EndOffset. If everything
Eli Bendersky8e352e32013-02-22 00:50:48 +000046 /// goes well, *Offset should be equal to EndOffset when this method
Eli Bendersky705085d2013-02-21 22:53:19 +000047 /// returns. Otherwise, an error occurred.
Alexey Samsonova08b1612014-04-28 23:00:06 +000048 virtual void parseInstructions(DataExtractor Data, uint32_t *Offset,
49 uint32_t EndOffset);
Eli Bendersky705085d2013-02-21 22:53:19 +000050
51 /// \brief Dump the entry header to the given output stream.
Eli Benderskyfd08bc12013-02-05 23:30:58 +000052 virtual void dumpHeader(raw_ostream &OS) const = 0;
Eli Bendersky1aa265d2013-02-06 00:20:38 +000053
Eli Bendersky705085d2013-02-21 22:53:19 +000054 /// \brief Dump the entry's instructions to the given output stream.
55 virtual void dumpInstructions(raw_ostream &OS) const;
56
Eli Benderskyfd08bc12013-02-05 23:30:58 +000057protected:
58 const FrameKind Kind;
Eli Bendersky1aa265d2013-02-06 00:20:38 +000059
Eli Bendersky1aa265d2013-02-06 00:20:38 +000060 /// \brief Offset of this entry in the section.
Eli Benderskyfd08bc12013-02-05 23:30:58 +000061 uint64_t Offset;
Eli Bendersky1aa265d2013-02-06 00:20:38 +000062
63 /// \brief Entry length as specified in DWARF.
Eli Benderskyfd08bc12013-02-05 23:30:58 +000064 uint64_t Length;
Eli Bendersky705085d2013-02-21 22:53:19 +000065
66 /// An entry may contain CFI instructions. An instruction consists of an
67 /// opcode and an optional sequence of operands.
68 typedef std::vector<uint64_t> Operands;
69 struct Instruction {
70 Instruction(uint8_t Opcode)
71 : Opcode(Opcode)
72 {}
73
74 uint8_t Opcode;
75 Operands Ops;
76 };
77
78 std::vector<Instruction> Instructions;
79
80 /// Convenience methods to add a new instruction with the given opcode and
81 /// operands to the Instructions vector.
82 void addInstruction(uint8_t Opcode) {
83 Instructions.push_back(Instruction(Opcode));
84 }
85
86 void addInstruction(uint8_t Opcode, uint64_t Operand1) {
87 Instructions.push_back(Instruction(Opcode));
88 Instructions.back().Ops.push_back(Operand1);
89 }
90
91 void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
92 Instructions.push_back(Instruction(Opcode));
93 Instructions.back().Ops.push_back(Operand1);
94 Instructions.back().Ops.push_back(Operand2);
95 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +000096};
97
Eli Bendersky705085d2013-02-21 22:53:19 +000098
99// See DWARF standard v3, section 7.23
100const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
101const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
102
Alexey Samsonova08b1612014-04-28 23:00:06 +0000103void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset,
104 uint32_t EndOffset) {
Eli Bendersky8e352e32013-02-22 00:50:48 +0000105 while (*Offset < EndOffset) {
106 uint8_t Opcode = Data.getU8(Offset);
Eli Bendersky705085d2013-02-21 22:53:19 +0000107 // Some instructions have a primary opcode encoded in the top bits.
108 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
109
110 if (Primary) {
111 // If it's a primary opcode, the first operand is encoded in the bottom
112 // bits of the opcode itself.
113 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
114 switch (Primary) {
115 default: llvm_unreachable("Impossible primary CFI opcode");
116 case DW_CFA_advance_loc:
117 case DW_CFA_restore:
118 addInstruction(Primary, Op1);
119 break;
120 case DW_CFA_offset:
Eli Bendersky8e352e32013-02-22 00:50:48 +0000121 addInstruction(Primary, Op1, Data.getULEB128(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000122 break;
123 }
124 } else {
125 // Extended opcode - its value is Opcode itself.
126 switch (Opcode) {
127 default: llvm_unreachable("Invalid extended CFI opcode");
128 case DW_CFA_nop:
129 case DW_CFA_remember_state:
130 case DW_CFA_restore_state:
Venkatraman Govindarajua302d292014-01-26 05:13:44 +0000131 case DW_CFA_GNU_window_save:
Eli Bendersky705085d2013-02-21 22:53:19 +0000132 // No operands
133 addInstruction(Opcode);
134 break;
135 case DW_CFA_set_loc:
136 // Operands: Address
Eli Bendersky8e352e32013-02-22 00:50:48 +0000137 addInstruction(Opcode, Data.getAddress(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000138 break;
139 case DW_CFA_advance_loc1:
140 // Operands: 1-byte delta
Eli Bendersky8e352e32013-02-22 00:50:48 +0000141 addInstruction(Opcode, Data.getU8(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000142 break;
143 case DW_CFA_advance_loc2:
144 // Operands: 2-byte delta
Eli Bendersky8e352e32013-02-22 00:50:48 +0000145 addInstruction(Opcode, Data.getU16(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000146 break;
147 case DW_CFA_advance_loc4:
148 // Operands: 4-byte delta
Eli Bendersky8e352e32013-02-22 00:50:48 +0000149 addInstruction(Opcode, Data.getU32(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000150 break;
151 case DW_CFA_restore_extended:
152 case DW_CFA_undefined:
153 case DW_CFA_same_value:
154 case DW_CFA_def_cfa_register:
155 case DW_CFA_def_cfa_offset:
156 // Operands: ULEB128
Eli Bendersky8e352e32013-02-22 00:50:48 +0000157 addInstruction(Opcode, Data.getULEB128(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000158 break;
159 case DW_CFA_def_cfa_offset_sf:
160 // Operands: SLEB128
Eli Bendersky8e352e32013-02-22 00:50:48 +0000161 addInstruction(Opcode, Data.getSLEB128(Offset));
Eli Bendersky705085d2013-02-21 22:53:19 +0000162 break;
163 case DW_CFA_offset_extended:
164 case DW_CFA_register:
165 case DW_CFA_def_cfa:
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000166 case DW_CFA_val_offset: {
Eli Bendersky705085d2013-02-21 22:53:19 +0000167 // Operands: ULEB128, ULEB128
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000168 // Note: We can not embed getULEB128 directly into function
169 // argument list. getULEB128 changes Offset and order of evaluation
170 // for arguments is unspecified.
171 auto op1 = Data.getULEB128(Offset);
172 auto op2 = Data.getULEB128(Offset);
173 addInstruction(Opcode, op1, op2);
Eli Bendersky705085d2013-02-21 22:53:19 +0000174 break;
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000175 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000176 case DW_CFA_offset_extended_sf:
177 case DW_CFA_def_cfa_sf:
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000178 case DW_CFA_val_offset_sf: {
Eli Bendersky705085d2013-02-21 22:53:19 +0000179 // Operands: ULEB128, SLEB128
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000180 // Note: see comment for the previous case
181 auto op1 = Data.getULEB128(Offset);
182 auto op2 = (uint64_t)Data.getSLEB128(Offset);
183 addInstruction(Opcode, op1, op2);
Eli Bendersky705085d2013-02-21 22:53:19 +0000184 break;
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000185 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000186 case DW_CFA_def_cfa_expression:
187 case DW_CFA_expression:
188 case DW_CFA_val_expression:
189 // TODO: implement this
190 report_fatal_error("Values with expressions not implemented yet!");
191 }
192 }
193 }
194}
195
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000196namespace {
Eli Bendersky58232062013-02-06 16:20:31 +0000197/// \brief DWARF Common Information Entry (CIE)
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000198class CIE : public FrameEntry {
199public:
200 // CIEs (and FDEs) are simply container classes, so the only sensible way to
201 // create them is by providing the full parsed contents in the constructor.
Alexey Samsonova08b1612014-04-28 23:00:06 +0000202 CIE(uint64_t Offset, uint64_t Length, uint8_t Version,
Keith Walkerea9483f2015-05-12 15:25:08 +0000203 SmallString<8> Augmentation, uint8_t AddressSize,
204 uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor,
Igor Laevsky03a670c2016-01-26 15:09:42 +0000205 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister,
Igor Laevskyff291b52016-01-27 14:05:35 +0000206 SmallString<8> AugmentationData, uint32_t FDEPointerEncoding,
207 uint32_t LSDAPointerEncoding)
Alexey Samsonova08b1612014-04-28 23:00:06 +0000208 : FrameEntry(FK_CIE, Offset, Length), Version(Version),
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000209 Augmentation(std::move(Augmentation)), AddressSize(AddressSize),
Keith Walkerea9483f2015-05-12 15:25:08 +0000210 SegmentDescriptorSize(SegmentDescriptorSize),
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000211 CodeAlignmentFactor(CodeAlignmentFactor),
Alexey Samsonova08b1612014-04-28 23:00:06 +0000212 DataAlignmentFactor(DataAlignmentFactor),
Igor Laevsky03a670c2016-01-26 15:09:42 +0000213 ReturnAddressRegister(ReturnAddressRegister),
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000214 AugmentationData(std::move(AugmentationData)),
Igor Laevsky03a670c2016-01-26 15:09:42 +0000215 FDEPointerEncoding(FDEPointerEncoding),
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000216 LSDAPointerEncoding(LSDAPointerEncoding) {}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000217
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000218 ~CIE() override {}
Eli Bendersky0b04cb02013-02-06 03:08:02 +0000219
Igor Laevsky03a670c2016-01-26 15:09:42 +0000220 StringRef getAugmentationString() const { return Augmentation; }
Frederic Riss41bb2c62015-02-25 21:30:13 +0000221 uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
222 int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
Igor Laevskyff291b52016-01-27 14:05:35 +0000223 uint32_t getFDEPointerEncoding() const {
Igor Laevsky03a670c2016-01-26 15:09:42 +0000224 return FDEPointerEncoding;
225 }
Igor Laevskyff291b52016-01-27 14:05:35 +0000226 uint32_t getLSDAPointerEncoding() const {
Igor Laevsky03a670c2016-01-26 15:09:42 +0000227 return LSDAPointerEncoding;
228 }
Frederic Riss41bb2c62015-02-25 21:30:13 +0000229
Craig Topper85482992014-03-05 07:52:44 +0000230 void dumpHeader(raw_ostream &OS) const override {
NAKAMURA Takumi56ac51a2013-02-07 02:02:27 +0000231 OS << format("%08x %08x %08x CIE",
232 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
233 << "\n";
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000234 OS << format(" Version: %d\n", Version);
235 OS << " Augmentation: \"" << Augmentation << "\"\n";
Keith Walkerea9483f2015-05-12 15:25:08 +0000236 if (Version >= 4) {
237 OS << format(" Address size: %u\n",
238 (uint32_t)AddressSize);
239 OS << format(" Segment desc size: %u\n",
240 (uint32_t)SegmentDescriptorSize);
241 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000242 OS << format(" Code alignment factor: %u\n",
243 (uint32_t)CodeAlignmentFactor);
244 OS << format(" Data alignment factor: %d\n",
245 (int32_t)DataAlignmentFactor);
246 OS << format(" Return address column: %d\n",
247 (int32_t)ReturnAddressRegister);
Simon Atanasyanf69c7e52016-03-01 18:38:05 +0000248 if (!AugmentationData.empty()) {
249 OS << " Augmentation data: ";
250 for (uint8_t Byte : AugmentationData)
251 OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
252 OS << "\n";
253 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000254 OS << "\n";
255 }
256
257 static bool classof(const FrameEntry *FE) {
258 return FE->getKind() == FK_CIE;
Alexey Samsonova08b1612014-04-28 23:00:06 +0000259 }
Eli Bendersky1aa265d2013-02-06 00:20:38 +0000260
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000261private:
Keith Walkerea9483f2015-05-12 15:25:08 +0000262 /// The following fields are defined in section 6.4.1 of the DWARF standard v4
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000263 uint8_t Version;
264 SmallString<8> Augmentation;
Keith Walkerea9483f2015-05-12 15:25:08 +0000265 uint8_t AddressSize;
266 uint8_t SegmentDescriptorSize;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000267 uint64_t CodeAlignmentFactor;
268 int64_t DataAlignmentFactor;
269 uint64_t ReturnAddressRegister;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000270
271 // The following are used when the CIE represents an EH frame entry.
272 SmallString<8> AugmentationData;
Igor Laevskyff291b52016-01-27 14:05:35 +0000273 uint32_t FDEPointerEncoding;
274 uint32_t LSDAPointerEncoding;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000275};
276
277
Eli Bendersky58232062013-02-06 16:20:31 +0000278/// \brief DWARF Frame Description Entry (FDE)
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000279class FDE : public FrameEntry {
280public:
281 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
282 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
283 // is obtained lazily once it's actually required.
Alexey Samsonova08b1612014-04-28 23:00:06 +0000284 FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset,
Frederic Rissbaf195f2015-02-25 21:30:09 +0000285 uint64_t InitialLocation, uint64_t AddressRange,
286 CIE *Cie)
Alexey Samsonova08b1612014-04-28 23:00:06 +0000287 : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
288 InitialLocation(InitialLocation), AddressRange(AddressRange),
Frederic Rissbaf195f2015-02-25 21:30:09 +0000289 LinkedCIE(Cie) {}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000290
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000291 ~FDE() override {}
Eli Bendersky0b04cb02013-02-06 03:08:02 +0000292
Frederic Riss41bb2c62015-02-25 21:30:13 +0000293 CIE *getLinkedCIE() const { return LinkedCIE; }
294
Craig Topper85482992014-03-05 07:52:44 +0000295 void dumpHeader(raw_ostream &OS) const override {
NAKAMURA Takumi56ac51a2013-02-07 02:02:27 +0000296 OS << format("%08x %08x %08x FDE ",
NAKAMURA Takumi14727d72013-02-07 14:54:42 +0000297 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000298 OS << format("cie=%08x pc=%08x...%08x\n",
NAKAMURA Takumi14727d72013-02-07 14:54:42 +0000299 (int32_t)LinkedCIEOffset,
NAKAMURA Takumi94651f92013-02-07 10:57:42 +0000300 (uint32_t)InitialLocation,
301 (uint32_t)InitialLocation + (uint32_t)AddressRange);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000302 }
303
304 static bool classof(const FrameEntry *FE) {
305 return FE->getKind() == FK_FDE;
Alexey Samsonova08b1612014-04-28 23:00:06 +0000306 }
Eli Bendersky1aa265d2013-02-06 00:20:38 +0000307
Alexey Samsonova08b1612014-04-28 23:00:06 +0000308private:
Eli Bendersky1aa265d2013-02-06 00:20:38 +0000309 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000310 uint64_t LinkedCIEOffset;
311 uint64_t InitialLocation;
312 uint64_t AddressRange;
313 CIE *LinkedCIE;
314};
Frederic Rissc0dd7242015-02-25 21:30:22 +0000315
316/// \brief Types of operands to CF instructions.
317enum OperandType {
318 OT_Unset,
319 OT_None,
320 OT_Address,
321 OT_Offset,
322 OT_FactoredCodeOffset,
323 OT_SignedFactDataOffset,
324 OT_UnsignedFactDataOffset,
325 OT_Register,
326 OT_Expression
327};
328
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000329} // end anonymous namespace
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000330
Frederic Rissc0dd7242015-02-25 21:30:22 +0000331/// \brief Initialize the array describing the types of operands.
332static ArrayRef<OperandType[2]> getOperandTypes() {
333 static OperandType OpTypes[DW_CFA_restore+1][2];
334
335#define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
336 do { \
337 OpTypes[OP][0] = OPTYPE0; \
338 OpTypes[OP][1] = OPTYPE1; \
339 } while (0)
340#define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
341#define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
342
343 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
344 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
345 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
346 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
347 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
348 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
349 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
350 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
351 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
352 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
353 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
354 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
355 DECLARE_OP1(DW_CFA_undefined, OT_Register);
356 DECLARE_OP1(DW_CFA_same_value, OT_Register);
357 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
358 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
359 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
360 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
361 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
362 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
363 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
364 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
365 DECLARE_OP1(DW_CFA_restore, OT_Register);
366 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
367 DECLARE_OP0(DW_CFA_remember_state);
368 DECLARE_OP0(DW_CFA_restore_state);
369 DECLARE_OP0(DW_CFA_GNU_window_save);
370 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
371 DECLARE_OP0(DW_CFA_nop);
372
373#undef DECLARE_OP0
374#undef DECLARE_OP1
375#undef DECLARE_OP2
Frederic Rissac10b0d2015-02-25 22:07:43 +0000376 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
Frederic Rissc0dd7242015-02-25 21:30:22 +0000377}
378
379static ArrayRef<OperandType[2]> OpTypes = getOperandTypes();
380
381/// \brief Print \p Opcode's operand number \p OperandIdx which has
382/// value \p Operand.
383static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx,
384 uint64_t Operand, uint64_t CodeAlignmentFactor,
385 int64_t DataAlignmentFactor) {
386 assert(OperandIdx < 2);
387 OperandType Type = OpTypes[Opcode][OperandIdx];
388
389 switch (Type) {
390 case OT_Unset:
391 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
392 if (const char *OpcodeName = CallFrameString(Opcode))
393 OS << " " << OpcodeName;
394 else
395 OS << format(" Opcode %x", Opcode);
396 break;
397 case OT_None:
398 break;
399 case OT_Address:
400 OS << format(" %" PRIx64, Operand);
401 break;
402 case OT_Offset:
403 // The offsets are all encoded in a unsigned form, but in practice
404 // consumers use them signed. It's most certainly legacy due to
405 // the lack of signed variants in the first Dwarf standards.
406 OS << format(" %+" PRId64, int64_t(Operand));
407 break;
408 case OT_FactoredCodeOffset: // Always Unsigned
409 if (CodeAlignmentFactor)
410 OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
411 else
412 OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
413 break;
414 case OT_SignedFactDataOffset:
415 if (DataAlignmentFactor)
416 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
417 else
418 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
419 break;
420 case OT_UnsignedFactDataOffset:
421 if (DataAlignmentFactor)
422 OS << format(" %" PRId64, Operand * DataAlignmentFactor);
423 else
424 OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
425 break;
426 case OT_Register:
Frederic Rissde374342015-02-25 22:30:09 +0000427 OS << format(" reg%" PRId64, Operand);
Frederic Rissc0dd7242015-02-25 21:30:22 +0000428 break;
429 case OT_Expression:
430 OS << " expression";
431 break;
432 }
433}
434
Frederic Riss056ad052015-02-25 21:30:16 +0000435void FrameEntry::dumpInstructions(raw_ostream &OS) const {
Frederic Rissc0dd7242015-02-25 21:30:22 +0000436 uint64_t CodeAlignmentFactor = 0;
437 int64_t DataAlignmentFactor = 0;
438 const CIE *Cie = dyn_cast<CIE>(this);
439
440 if (!Cie)
441 Cie = cast<FDE>(this)->getLinkedCIE();
442 if (Cie) {
443 CodeAlignmentFactor = Cie->getCodeAlignmentFactor();
444 DataAlignmentFactor = Cie->getDataAlignmentFactor();
445 }
446
Frederic Riss056ad052015-02-25 21:30:16 +0000447 for (const auto &Instr : Instructions) {
448 uint8_t Opcode = Instr.Opcode;
449 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
450 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
Frederic Rissc0dd7242015-02-25 21:30:22 +0000451 OS << " " << CallFrameString(Opcode) << ":";
452 for (unsigned i = 0; i < Instr.Ops.size(); ++i)
453 printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor,
454 DataAlignmentFactor);
455 OS << '\n';
Frederic Riss056ad052015-02-25 21:30:16 +0000456 }
457}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000458
Igor Laevsky03a670c2016-01-26 15:09:42 +0000459DWARFDebugFrame::DWARFDebugFrame(bool IsEH) : IsEH(IsEH) {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000460}
461
Eli Bendersky1aa265d2013-02-06 00:20:38 +0000462DWARFDebugFrame::~DWARFDebugFrame() {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000463}
464
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000465static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
466 uint32_t Offset, int Length) {
467 errs() << "DUMP: ";
468 for (int i = 0; i < Length; ++i) {
469 uint8_t c = Data.getU8(&Offset);
470 errs().write_hex(c); errs() << " ";
471 }
472 errs() << "\n";
473}
474
Igor Laevsky03a670c2016-01-26 15:09:42 +0000475static unsigned getSizeForEncoding(const DataExtractor &Data,
476 unsigned symbolEncoding) {
477 unsigned format = symbolEncoding & 0x0f;
478 switch (format) {
479 default: llvm_unreachable("Unknown Encoding");
480 case dwarf::DW_EH_PE_absptr:
481 case dwarf::DW_EH_PE_signed:
482 return Data.getAddressSize();
483 case dwarf::DW_EH_PE_udata2:
484 case dwarf::DW_EH_PE_sdata2:
485 return 2;
486 case dwarf::DW_EH_PE_udata4:
487 case dwarf::DW_EH_PE_sdata4:
488 return 4;
489 case dwarf::DW_EH_PE_udata8:
490 case dwarf::DW_EH_PE_sdata8:
491 return 8;
492 }
493}
494
495static uint64_t readPointer(const DataExtractor &Data, uint32_t &Offset,
496 unsigned Encoding) {
497 switch (getSizeForEncoding(Data, Encoding)) {
498 case 2:
499 return Data.getU16(&Offset);
500 case 4:
501 return Data.getU32(&Offset);
502 case 8:
503 return Data.getU64(&Offset);
504 default:
505 llvm_unreachable("Illegal data size");
506 }
507}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000508
509void DWARFDebugFrame::parse(DataExtractor Data) {
510 uint32_t Offset = 0;
Frederic Rissbaf195f2015-02-25 21:30:09 +0000511 DenseMap<uint32_t, CIE *> CIEs;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000512
513 while (Data.isValidOffset(Offset)) {
514 uint32_t StartOffset = Offset;
515
Igor Laevsky03a670c2016-01-26 15:09:42 +0000516 auto ReportError = [StartOffset](const char *ErrorMsg) {
517 std::string Str;
518 raw_string_ostream OS(Str);
519 OS << format(ErrorMsg, StartOffset);
520 OS.flush();
521 report_fatal_error(Str);
522 };
523
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000524 bool IsDWARF64 = false;
525 uint64_t Length = Data.getU32(&Offset);
526 uint64_t Id;
527
528 if (Length == UINT32_MAX) {
529 // DWARF-64 is distinguished by the first 32 bits of the initial length
530 // field being 0xffffffff. Then, the next 64 bits are the actual entry
531 // length.
532 IsDWARF64 = true;
533 Length = Data.getU64(&Offset);
534 }
535
536 // At this point, Offset points to the next field after Length.
537 // Length is the structure size excluding itself. Compute an offset one
538 // past the end of the structure (needed to know how many instructions to
539 // read).
540 // TODO: For honest DWARF64 support, DataExtractor will have to treat
541 // offset_ptr as uint64_t*
Igor Laevsky03a670c2016-01-26 15:09:42 +0000542 uint32_t StartStructureOffset = Offset;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000543 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
544
545 // The Id field's size depends on the DWARF format
Igor Laevsky03a670c2016-01-26 15:09:42 +0000546 Id = Data.getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4);
547 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) ||
548 Id == DW_CIE_ID ||
549 (IsEH && !Id));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000550
551 if (IsCIE) {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000552 uint8_t Version = Data.getU8(&Offset);
553 const char *Augmentation = Data.getCStr(&Offset);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000554 StringRef AugmentationString(Augmentation ? Augmentation : "");
555 uint8_t AddressSize = Version < 4 ? Data.getAddressSize() :
556 Data.getU8(&Offset);
Keith Walkerea9483f2015-05-12 15:25:08 +0000557 Data.setAddressSize(AddressSize);
558 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000559 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
560 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
561 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
562
Igor Laevsky03a670c2016-01-26 15:09:42 +0000563 // Parse the augmentation data for EH CIEs
Igor Laevskyff291b52016-01-27 14:05:35 +0000564 StringRef AugmentationData("");
565 uint32_t FDEPointerEncoding = DW_EH_PE_omit;
566 uint32_t LSDAPointerEncoding = DW_EH_PE_omit;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000567 if (IsEH) {
568 Optional<uint32_t> PersonalityEncoding;
569 Optional<uint64_t> Personality;
570
Igor Laevskyff291b52016-01-27 14:05:35 +0000571 Optional<uint64_t> AugmentationLength;
572 uint32_t StartAugmentationOffset;
573 uint32_t EndAugmentationOffset;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000574
575 // Walk the augmentation string to get all the augmentation data.
576 for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) {
577 switch (AugmentationString[i]) {
578 default:
579 ReportError("Unknown augmentation character in entry at %lx");
580 case 'L':
Igor Laevsky03a670c2016-01-26 15:09:42 +0000581 LSDAPointerEncoding = Data.getU8(&Offset);
582 break;
583 case 'P': {
584 if (Personality)
585 ReportError("Duplicate personality in entry at %lx");
586 PersonalityEncoding = Data.getU8(&Offset);
587 Personality = readPointer(Data, Offset, *PersonalityEncoding);
588 break;
589 }
590 case 'R':
Igor Laevsky03a670c2016-01-26 15:09:42 +0000591 FDEPointerEncoding = Data.getU8(&Offset);
592 break;
593 case 'z':
594 if (i)
595 ReportError("'z' must be the first character at %lx");
596 // Parse the augmentation length first. We only parse it if
597 // the string contains a 'z'.
598 AugmentationLength = Data.getULEB128(&Offset);
599 StartAugmentationOffset = Offset;
Igor Laevskyff291b52016-01-27 14:05:35 +0000600 EndAugmentationOffset = Offset +
601 static_cast<uint32_t>(*AugmentationLength);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000602 }
603 }
604
Igor Laevskyff291b52016-01-27 14:05:35 +0000605 if (AugmentationLength.hasValue()) {
606 if (Offset != EndAugmentationOffset)
607 ReportError("Parsing augmentation data at %lx failed");
Igor Laevsky03a670c2016-01-26 15:09:42 +0000608
Igor Laevskyff291b52016-01-27 14:05:35 +0000609 AugmentationData = Data.getData().slice(StartAugmentationOffset,
610 EndAugmentationOffset);
611 }
Igor Laevsky03a670c2016-01-26 15:09:42 +0000612 }
613
Frederic Rissbaf195f2015-02-25 21:30:09 +0000614 auto Cie = make_unique<CIE>(StartOffset, Length, Version,
Igor Laevskyff291b52016-01-27 14:05:35 +0000615 AugmentationString, AddressSize,
Keith Walkerea9483f2015-05-12 15:25:08 +0000616 SegmentDescriptorSize, CodeAlignmentFactor,
Igor Laevsky03a670c2016-01-26 15:09:42 +0000617 DataAlignmentFactor, ReturnAddressRegister,
618 AugmentationData, FDEPointerEncoding,
619 LSDAPointerEncoding);
Frederic Rissbaf195f2015-02-25 21:30:09 +0000620 CIEs[StartOffset] = Cie.get();
621 Entries.emplace_back(std::move(Cie));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000622 } else {
623 // FDE
624 uint64_t CIEPointer = Id;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000625 uint64_t InitialLocation = 0;
626 uint64_t AddressRange = 0;
627 CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
628
629 if (IsEH) {
630 // The address size is encoded in the CIE we reference.
631 if (!Cie)
632 ReportError("Parsing FDE data at %lx failed due to missing CIE");
633
Igor Laevskyff291b52016-01-27 14:05:35 +0000634 InitialLocation = readPointer(Data, Offset,
635 Cie->getFDEPointerEncoding());
636 AddressRange = readPointer(Data, Offset,
637 Cie->getFDEPointerEncoding());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000638
Igor Laevsky03a670c2016-01-26 15:09:42 +0000639 StringRef AugmentationString = Cie->getAugmentationString();
640 if (!AugmentationString.empty()) {
641 // Parse the augmentation length and data for this FDE.
642 uint64_t AugmentationLength = Data.getULEB128(&Offset);
643
644 uint32_t EndAugmentationOffset =
645 Offset + static_cast<uint32_t>(AugmentationLength);
646
647 // Decode the LSDA if the CIE augmentation string said we should.
Igor Laevskyff291b52016-01-27 14:05:35 +0000648 if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit)
Dmitry Polukhinf4124a72016-02-05 09:24:34 +0000649 readPointer(Data, Offset, Cie->getLSDAPointerEncoding());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000650
651 if (Offset != EndAugmentationOffset)
652 ReportError("Parsing augmentation data at %lx failed");
653 }
654 } else {
655 InitialLocation = Data.getAddress(&Offset);
656 AddressRange = Data.getAddress(&Offset);
657 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000658
Alexey Samsonova08b1612014-04-28 23:00:06 +0000659 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
Frederic Rissbaf195f2015-02-25 21:30:09 +0000660 InitialLocation, AddressRange,
Igor Laevsky03a670c2016-01-26 15:09:42 +0000661 Cie));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000662 }
663
Alexey Samsonova08b1612014-04-28 23:00:06 +0000664 Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset);
Eli Bendersky705085d2013-02-21 22:53:19 +0000665
Igor Laevsky03a670c2016-01-26 15:09:42 +0000666 if (Offset != EndStructureOffset)
667 ReportError("Parsing entry instructions at %lx failed");
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000668 }
669}
670
671
672void DWARFDebugFrame::dump(raw_ostream &OS) const {
673 OS << "\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000674 for (const auto &Entry : Entries) {
Eli Bendersky705085d2013-02-21 22:53:19 +0000675 Entry->dumpHeader(OS);
676 Entry->dumpInstructions(OS);
677 OS << "\n";
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000678 }
679}
680