blob: a33548e95b0b99a700a72ddc800ea8d010deb65f [file] [log] [blame]
Eli Bendersky60bdc5b2013-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
10#include "DWARFDebugFrame.h"
11#include "llvm/ADT/SmallString.h"
12#include "llvm/Support/DataTypes.h"
13#include "llvm/Support/Dwarf.h"
Stephen Hines36b56882014-04-23 16:57:46 -070014#include "llvm/Support/ErrorHandling.h"
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000015#include "llvm/Support/Format.h"
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +000016#include "llvm/Support/raw_ostream.h"
17#include <string>
18#include <vector>
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000019
20using namespace llvm;
21using namespace dwarf;
22
23
Eli Bendersky2e402d52013-02-06 16:20:31 +000024/// \brief Abstract frame entry defining the common interface concrete
25/// entries implement.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000026class llvm::FrameEntry {
27public:
28 enum FrameKind {FK_CIE, FK_FDE};
Stephen Hinesdce4a402014-05-29 02:49:00 -070029 FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length)
30 : Kind(K), Offset(Offset), Length(Length) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000031
Eli Bendersky8a0329e2013-02-06 03:08:02 +000032 virtual ~FrameEntry() {
33 }
34
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000035 FrameKind getKind() const { return Kind; }
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +000036 virtual uint64_t getOffset() const { return Offset; }
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000037
Stephen Hinesdce4a402014-05-29 02:49:00 -070038 /// \brief Parse and store a sequence of CFI instructions from Data,
39 /// starting at *Offset and ending at EndOffset. If everything
Eli Bendersky46e0d1d2013-02-22 00:50:48 +000040 /// goes well, *Offset should be equal to EndOffset when this method
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +000041 /// returns. Otherwise, an error occurred.
Stephen Hinesdce4a402014-05-29 02:49:00 -070042 virtual void parseInstructions(DataExtractor Data, uint32_t *Offset,
43 uint32_t EndOffset);
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +000044
45 /// \brief Dump the entry header to the given output stream.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000046 virtual void dumpHeader(raw_ostream &OS) const = 0;
Eli Benderskyba426252013-02-06 00:20:38 +000047
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +000048 /// \brief Dump the entry's instructions to the given output stream.
49 virtual void dumpInstructions(raw_ostream &OS) const;
50
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000051protected:
52 const FrameKind Kind;
Eli Benderskyba426252013-02-06 00:20:38 +000053
Eli Benderskyba426252013-02-06 00:20:38 +000054 /// \brief Offset of this entry in the section.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000055 uint64_t Offset;
Eli Benderskyba426252013-02-06 00:20:38 +000056
57 /// \brief Entry length as specified in DWARF.
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000058 uint64_t Length;
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +000059
60 /// An entry may contain CFI instructions. An instruction consists of an
61 /// opcode and an optional sequence of operands.
62 typedef std::vector<uint64_t> Operands;
63 struct Instruction {
64 Instruction(uint8_t Opcode)
65 : Opcode(Opcode)
66 {}
67
68 uint8_t Opcode;
69 Operands Ops;
70 };
71
72 std::vector<Instruction> Instructions;
73
74 /// Convenience methods to add a new instruction with the given opcode and
75 /// operands to the Instructions vector.
76 void addInstruction(uint8_t Opcode) {
77 Instructions.push_back(Instruction(Opcode));
78 }
79
80 void addInstruction(uint8_t Opcode, uint64_t Operand1) {
81 Instructions.push_back(Instruction(Opcode));
82 Instructions.back().Ops.push_back(Operand1);
83 }
84
85 void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
86 Instructions.push_back(Instruction(Opcode));
87 Instructions.back().Ops.push_back(Operand1);
88 Instructions.back().Ops.push_back(Operand2);
89 }
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000090};
91
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +000092
93// See DWARF standard v3, section 7.23
94const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
95const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
96
Stephen Hinesdce4a402014-05-29 02:49:00 -070097void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset,
98 uint32_t EndOffset) {
Eli Bendersky46e0d1d2013-02-22 00:50:48 +000099 while (*Offset < EndOffset) {
100 uint8_t Opcode = Data.getU8(Offset);
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000101 // Some instructions have a primary opcode encoded in the top bits.
102 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
103
104 if (Primary) {
105 // If it's a primary opcode, the first operand is encoded in the bottom
106 // bits of the opcode itself.
107 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
108 switch (Primary) {
109 default: llvm_unreachable("Impossible primary CFI opcode");
110 case DW_CFA_advance_loc:
111 case DW_CFA_restore:
112 addInstruction(Primary, Op1);
113 break;
114 case DW_CFA_offset:
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000115 addInstruction(Primary, Op1, Data.getULEB128(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000116 break;
117 }
118 } else {
119 // Extended opcode - its value is Opcode itself.
120 switch (Opcode) {
121 default: llvm_unreachable("Invalid extended CFI opcode");
122 case DW_CFA_nop:
123 case DW_CFA_remember_state:
124 case DW_CFA_restore_state:
Stephen Hines36b56882014-04-23 16:57:46 -0700125 case DW_CFA_GNU_window_save:
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000126 // No operands
127 addInstruction(Opcode);
128 break;
129 case DW_CFA_set_loc:
130 // Operands: Address
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000131 addInstruction(Opcode, Data.getAddress(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000132 break;
133 case DW_CFA_advance_loc1:
134 // Operands: 1-byte delta
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000135 addInstruction(Opcode, Data.getU8(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000136 break;
137 case DW_CFA_advance_loc2:
138 // Operands: 2-byte delta
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000139 addInstruction(Opcode, Data.getU16(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000140 break;
141 case DW_CFA_advance_loc4:
142 // Operands: 4-byte delta
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000143 addInstruction(Opcode, Data.getU32(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000144 break;
145 case DW_CFA_restore_extended:
146 case DW_CFA_undefined:
147 case DW_CFA_same_value:
148 case DW_CFA_def_cfa_register:
149 case DW_CFA_def_cfa_offset:
150 // Operands: ULEB128
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000151 addInstruction(Opcode, Data.getULEB128(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000152 break;
153 case DW_CFA_def_cfa_offset_sf:
154 // Operands: SLEB128
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000155 addInstruction(Opcode, Data.getSLEB128(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000156 break;
157 case DW_CFA_offset_extended:
158 case DW_CFA_register:
159 case DW_CFA_def_cfa:
160 case DW_CFA_val_offset:
161 // Operands: ULEB128, ULEB128
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000162 addInstruction(Opcode, Data.getULEB128(Offset),
163 Data.getULEB128(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000164 break;
165 case DW_CFA_offset_extended_sf:
166 case DW_CFA_def_cfa_sf:
167 case DW_CFA_val_offset_sf:
168 // Operands: ULEB128, SLEB128
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000169 addInstruction(Opcode, Data.getULEB128(Offset),
170 Data.getSLEB128(Offset));
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000171 break;
172 case DW_CFA_def_cfa_expression:
173 case DW_CFA_expression:
174 case DW_CFA_val_expression:
175 // TODO: implement this
176 report_fatal_error("Values with expressions not implemented yet!");
177 }
178 }
179 }
180}
181
182
183void FrameEntry::dumpInstructions(raw_ostream &OS) const {
184 // TODO: at the moment only instruction names are dumped. Expand this to
185 // dump operands as well.
Stephen Hines36b56882014-04-23 16:57:46 -0700186 for (const auto &Instr : Instructions) {
187 uint8_t Opcode = Instr.Opcode;
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000188 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
189 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
190 OS << " " << CallFrameString(Opcode) << ":\n";
191 }
192}
193
194
Benjamin Kramer74b3c8d2013-02-15 12:30:38 +0000195namespace {
Eli Bendersky2e402d52013-02-06 16:20:31 +0000196/// \brief DWARF Common Information Entry (CIE)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000197class CIE : public FrameEntry {
198public:
199 // CIEs (and FDEs) are simply container classes, so the only sensible way to
200 // create them is by providing the full parsed contents in the constructor.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700201 CIE(uint64_t Offset, uint64_t Length, uint8_t Version,
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000202 SmallString<8> Augmentation, uint64_t CodeAlignmentFactor,
203 int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700204 : FrameEntry(FK_CIE, Offset, Length), Version(Version),
205 Augmentation(Augmentation), CodeAlignmentFactor(CodeAlignmentFactor),
206 DataAlignmentFactor(DataAlignmentFactor),
207 ReturnAddressRegister(ReturnAddressRegister) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000208
Eli Bendersky8a0329e2013-02-06 03:08:02 +0000209 ~CIE() {
210 }
211
Stephen Hines36b56882014-04-23 16:57:46 -0700212 void dumpHeader(raw_ostream &OS) const override {
NAKAMURA Takumi90e01ac2013-02-07 02:02:27 +0000213 OS << format("%08x %08x %08x CIE",
214 (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
215 << "\n";
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000216 OS << format(" Version: %d\n", Version);
217 OS << " Augmentation: \"" << Augmentation << "\"\n";
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000218 OS << format(" Code alignment factor: %u\n",
219 (uint32_t)CodeAlignmentFactor);
220 OS << format(" Data alignment factor: %d\n",
221 (int32_t)DataAlignmentFactor);
222 OS << format(" Return address column: %d\n",
223 (int32_t)ReturnAddressRegister);
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000224 OS << "\n";
225 }
226
227 static bool classof(const FrameEntry *FE) {
228 return FE->getKind() == FK_CIE;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700229 }
Eli Benderskyba426252013-02-06 00:20:38 +0000230
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000231private:
Eli Benderskyba426252013-02-06 00:20:38 +0000232 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000233 uint8_t Version;
234 SmallString<8> Augmentation;
235 uint64_t CodeAlignmentFactor;
236 int64_t DataAlignmentFactor;
237 uint64_t ReturnAddressRegister;
238};
239
240
Eli Bendersky2e402d52013-02-06 16:20:31 +0000241/// \brief DWARF Frame Description Entry (FDE)
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000242class FDE : public FrameEntry {
243public:
244 // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
245 // an offset to the CIE (provided by parsing the FDE header). The CIE itself
246 // is obtained lazily once it's actually required.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700247 FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset,
248 uint64_t InitialLocation, uint64_t AddressRange)
249 : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
250 InitialLocation(InitialLocation), AddressRange(AddressRange),
251 LinkedCIE(nullptr) {}
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000252
Eli Bendersky8a0329e2013-02-06 03:08:02 +0000253 ~FDE() {
254 }
255
Stephen Hines36b56882014-04-23 16:57:46 -0700256 void dumpHeader(raw_ostream &OS) const override {
NAKAMURA Takumi90e01ac2013-02-07 02:02:27 +0000257 OS << format("%08x %08x %08x FDE ",
NAKAMURA Takumid9a8d432013-02-07 14:54:42 +0000258 (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000259 OS << format("cie=%08x pc=%08x...%08x\n",
NAKAMURA Takumid9a8d432013-02-07 14:54:42 +0000260 (int32_t)LinkedCIEOffset,
NAKAMURA Takumi8ff06312013-02-07 10:57:42 +0000261 (uint32_t)InitialLocation,
262 (uint32_t)InitialLocation + (uint32_t)AddressRange);
Eli Benderskyb2ac7c02013-02-06 05:37:46 +0000263 if (LinkedCIE) {
264 OS << format("%p\n", LinkedCIE);
265 }
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000266 }
267
268 static bool classof(const FrameEntry *FE) {
269 return FE->getKind() == FK_FDE;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700270 }
Eli Benderskyba426252013-02-06 00:20:38 +0000271
Stephen Hinesdce4a402014-05-29 02:49:00 -0700272private:
Eli Benderskyba426252013-02-06 00:20:38 +0000273 /// The following fields are defined in section 6.4.1 of the DWARF standard v3
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000274 uint64_t LinkedCIEOffset;
275 uint64_t InitialLocation;
276 uint64_t AddressRange;
277 CIE *LinkedCIE;
278};
Benjamin Kramer74b3c8d2013-02-15 12:30:38 +0000279} // end anonymous namespace
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000280
281
Eli Benderskyba426252013-02-06 00:20:38 +0000282DWARFDebugFrame::DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000283}
284
Eli Benderskyba426252013-02-06 00:20:38 +0000285DWARFDebugFrame::~DWARFDebugFrame() {
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000286}
287
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000288static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
289 uint32_t Offset, int Length) {
290 errs() << "DUMP: ";
291 for (int i = 0; i < Length; ++i) {
292 uint8_t c = Data.getU8(&Offset);
293 errs().write_hex(c); errs() << " ";
294 }
295 errs() << "\n";
296}
297
298
299void DWARFDebugFrame::parse(DataExtractor Data) {
300 uint32_t Offset = 0;
301
302 while (Data.isValidOffset(Offset)) {
303 uint32_t StartOffset = Offset;
304
305 bool IsDWARF64 = false;
306 uint64_t Length = Data.getU32(&Offset);
307 uint64_t Id;
308
309 if (Length == UINT32_MAX) {
310 // DWARF-64 is distinguished by the first 32 bits of the initial length
311 // field being 0xffffffff. Then, the next 64 bits are the actual entry
312 // length.
313 IsDWARF64 = true;
314 Length = Data.getU64(&Offset);
315 }
316
317 // At this point, Offset points to the next field after Length.
318 // Length is the structure size excluding itself. Compute an offset one
319 // past the end of the structure (needed to know how many instructions to
320 // read).
321 // TODO: For honest DWARF64 support, DataExtractor will have to treat
322 // offset_ptr as uint64_t*
323 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
324
325 // The Id field's size depends on the DWARF format
326 Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
327 bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
328
329 if (IsCIE) {
330 // Note: this is specifically DWARFv3 CIE header structure. It was
Eli Bendersky46e0d1d2013-02-22 00:50:48 +0000331 // changed in DWARFv4. We currently don't support reading DWARFv4
332 // here because LLVM itself does not emit it (and LLDB doesn't
333 // support it either).
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000334 uint8_t Version = Data.getU8(&Offset);
335 const char *Augmentation = Data.getCStr(&Offset);
336 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
337 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
338 uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
339
Stephen Hinesdce4a402014-05-29 02:49:00 -0700340 Entries.emplace_back(new CIE(StartOffset, Length, Version,
341 StringRef(Augmentation), CodeAlignmentFactor,
342 DataAlignmentFactor, ReturnAddressRegister));
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000343 } else {
344 // FDE
345 uint64_t CIEPointer = Id;
346 uint64_t InitialLocation = Data.getAddress(&Offset);
347 uint64_t AddressRange = Data.getAddress(&Offset);
348
Stephen Hinesdce4a402014-05-29 02:49:00 -0700349 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
350 InitialLocation, AddressRange));
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000351 }
352
Stephen Hinesdce4a402014-05-29 02:49:00 -0700353 Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset);
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000354
Stephen Hinesdce4a402014-05-29 02:49:00 -0700355 if (Offset != EndStructureOffset) {
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000356 std::string Str;
357 raw_string_ostream OS(Str);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700358 OS << format("Parsing entry instructions at %lx failed", StartOffset);
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000359 report_fatal_error(Str);
360 }
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000361 }
362}
363
364
365void DWARFDebugFrame::dump(raw_ostream &OS) const {
366 OS << "\n";
Stephen Hines36b56882014-04-23 16:57:46 -0700367 for (const auto &Entry : Entries) {
Eli Bendersky7bf3d6a2013-02-21 22:53:19 +0000368 Entry->dumpHeader(OS);
369 Entry->dumpInstructions(OS);
370 OS << "\n";
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000371 }
372}
373