blob: dc59b1f466fd64c743b2dd0beea52657eea4fe93 [file] [log] [blame]
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001//===- DWARFDebugFrame.h - Parsing of .debug_frame ------------------------===//
Eli Benderskyfd08bc12013-02-05 23:30:58 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Eli Benderskyfd08bc12013-02-05 23:30:58 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000010#include "llvm/ADT/DenseMap.h"
Igor Laevsky03a670c2016-01-26 15:09:42 +000011#include "llvm/ADT/Optional.h"
Simon Atanasyanf69c7e52016-03-01 18:38:05 +000012#include "llvm/ADT/StringExtras.h"
Eugene Zelenko61a72d82016-08-18 17:56:27 +000013#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/Dwarf.h"
Frederic Rissc0dd7242015-02-25 21:30:22 +000015#include "llvm/Support/Casting.h"
Eugene Zelenko61a72d82016-08-18 17:56:27 +000016#include "llvm/Support/Compiler.h"
17#include "llvm/Support/DataExtractor.h"
Victor Leschukcba595d2018-08-20 09:59:08 +000018#include "llvm/Support/Errc.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"
Eugene Zelenko61a72d82016-08-18 17:56:27 +000022#include <algorithm>
23#include <cassert>
24#include <cinttypes>
25#include <cstdint>
Eli Bendersky705085d2013-02-21 22:53:19 +000026#include <string>
27#include <vector>
Eli Benderskyfd08bc12013-02-05 23:30:58 +000028
29using namespace llvm;
30using namespace dwarf;
31
Eli Benderskyfd08bc12013-02-05 23:30:58 +000032
Eli Bendersky705085d2013-02-21 22:53:19 +000033// See DWARF standard v3, section 7.23
34const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
35const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
36
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000037Error CFIProgram::parse(DWARFDataExtractor Data, uint32_t *Offset,
Rafael Auler86fb7bf2018-03-08 00:46:53 +000038 uint32_t EndOffset) {
Eli Bendersky8e352e32013-02-22 00:50:48 +000039 while (*Offset < EndOffset) {
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000040 uint8_t Opcode = Data.getRelocatedValue(1, Offset);
Eli Bendersky705085d2013-02-21 22:53:19 +000041 // Some instructions have a primary opcode encoded in the top bits.
42 uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
43
44 if (Primary) {
45 // If it's a primary opcode, the first operand is encoded in the bottom
46 // bits of the opcode itself.
47 uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
48 switch (Primary) {
Rafael Auler86fb7bf2018-03-08 00:46:53 +000049 default:
Victor Leschukcba595d2018-08-20 09:59:08 +000050 return createStringError(errc::illegal_byte_sequence,
51 "Invalid primary CFI opcode 0x%" PRIx8,
52 Primary);
Rafael Auler86fb7bf2018-03-08 00:46:53 +000053 case DW_CFA_advance_loc:
54 case DW_CFA_restore:
55 addInstruction(Primary, Op1);
56 break;
57 case DW_CFA_offset:
58 addInstruction(Primary, Op1, Data.getULEB128(Offset));
59 break;
Eli Bendersky705085d2013-02-21 22:53:19 +000060 }
61 } else {
62 // Extended opcode - its value is Opcode itself.
63 switch (Opcode) {
Rafael Auler86fb7bf2018-03-08 00:46:53 +000064 default:
Victor Leschukcba595d2018-08-20 09:59:08 +000065 return createStringError(errc::illegal_byte_sequence,
66 "Invalid extended CFI opcode 0x%" PRIx8,
67 Opcode);
Rafael Auler86fb7bf2018-03-08 00:46:53 +000068 case DW_CFA_nop:
69 case DW_CFA_remember_state:
70 case DW_CFA_restore_state:
71 case DW_CFA_GNU_window_save:
72 // No operands
73 addInstruction(Opcode);
74 break;
75 case DW_CFA_set_loc:
76 // Operands: Address
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000077 addInstruction(Opcode, Data.getRelocatedAddress(Offset));
Rafael Auler86fb7bf2018-03-08 00:46:53 +000078 break;
79 case DW_CFA_advance_loc1:
80 // Operands: 1-byte delta
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000081 addInstruction(Opcode, Data.getRelocatedValue(1, Offset));
Rafael Auler86fb7bf2018-03-08 00:46:53 +000082 break;
83 case DW_CFA_advance_loc2:
84 // Operands: 2-byte delta
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000085 addInstruction(Opcode, Data.getRelocatedValue(2, Offset));
Rafael Auler86fb7bf2018-03-08 00:46:53 +000086 break;
87 case DW_CFA_advance_loc4:
88 // Operands: 4-byte delta
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +000089 addInstruction(Opcode, Data.getRelocatedValue(4, Offset));
Rafael Auler86fb7bf2018-03-08 00:46:53 +000090 break;
91 case DW_CFA_restore_extended:
92 case DW_CFA_undefined:
93 case DW_CFA_same_value:
94 case DW_CFA_def_cfa_register:
95 case DW_CFA_def_cfa_offset:
96 case DW_CFA_GNU_args_size:
97 // Operands: ULEB128
98 addInstruction(Opcode, Data.getULEB128(Offset));
99 break;
100 case DW_CFA_def_cfa_offset_sf:
101 // Operands: SLEB128
102 addInstruction(Opcode, Data.getSLEB128(Offset));
103 break;
104 case DW_CFA_offset_extended:
105 case DW_CFA_register:
106 case DW_CFA_def_cfa:
107 case DW_CFA_val_offset: {
108 // Operands: ULEB128, ULEB128
109 // Note: We can not embed getULEB128 directly into function
110 // argument list. getULEB128 changes Offset and order of evaluation
111 // for arguments is unspecified.
112 auto op1 = Data.getULEB128(Offset);
113 auto op2 = Data.getULEB128(Offset);
114 addInstruction(Opcode, op1, op2);
115 break;
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000116 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000117 case DW_CFA_offset_extended_sf:
118 case DW_CFA_def_cfa_sf:
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000119 case DW_CFA_val_offset_sf: {
Eli Bendersky705085d2013-02-21 22:53:19 +0000120 // Operands: ULEB128, SLEB128
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000121 // Note: see comment for the previous case
122 auto op1 = Data.getULEB128(Offset);
123 auto op2 = (uint64_t)Data.getSLEB128(Offset);
124 addInstruction(Opcode, op1, op2);
Eli Bendersky705085d2013-02-21 22:53:19 +0000125 break;
Igor Laevsky0e1605a2016-01-26 13:31:11 +0000126 }
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000127 case DW_CFA_def_cfa_expression: {
128 uint32_t ExprLength = Data.getULEB128(Offset);
129 addInstruction(Opcode, 0);
130 DataExtractor Extractor(
131 Data.getData().slice(*Offset, *Offset + ExprLength),
132 Data.isLittleEndian(), Data.getAddressSize());
133 Instructions.back().Expression = DWARFExpression(
134 Extractor, Data.getAddressSize(), dwarf::DWARF_VERSION);
135 *Offset += ExprLength;
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000136 break;
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000137 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000138 case DW_CFA_expression:
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000139 case DW_CFA_val_expression: {
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000140 auto RegNum = Data.getULEB128(Offset);
141 auto BlockLength = Data.getULEB128(Offset);
142 addInstruction(Opcode, RegNum, 0);
143 DataExtractor Extractor(
144 Data.getData().slice(*Offset, *Offset + BlockLength),
145 Data.isLittleEndian(), Data.getAddressSize());
146 Instructions.back().Expression = DWARFExpression(
147 Extractor, Data.getAddressSize(), dwarf::DWARF_VERSION);
148 *Offset += BlockLength;
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000149 break;
150 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000151 }
152 }
153 }
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000154
155 return Error::success();
Eli Bendersky705085d2013-02-21 22:53:19 +0000156}
157
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000158namespace {
Eugene Zelenko61a72d82016-08-18 17:56:27 +0000159
Frederic Rissc0dd7242015-02-25 21:30:22 +0000160
Benjamin Kramer6ecb1e72013-02-15 12:30:38 +0000161} // end anonymous namespace
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000162
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000163ArrayRef<CFIProgram::OperandType[2]> CFIProgram::getOperandTypes() {
Frederic Rissc0dd7242015-02-25 21:30:22 +0000164 static OperandType OpTypes[DW_CFA_restore+1][2];
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000165 static bool Initialized = false;
166 if (Initialized) {
167 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
168 }
169 Initialized = true;
Frederic Rissc0dd7242015-02-25 21:30:22 +0000170
171#define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
172 do { \
173 OpTypes[OP][0] = OPTYPE0; \
174 OpTypes[OP][1] = OPTYPE1; \
Eugene Zelenko61a72d82016-08-18 17:56:27 +0000175 } while (false)
Frederic Rissc0dd7242015-02-25 21:30:22 +0000176#define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
177#define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
178
179 DECLARE_OP1(DW_CFA_set_loc, OT_Address);
180 DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
181 DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
182 DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
183 DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
184 DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
185 DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
186 DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
187 DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
188 DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
189 DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
190 DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
191 DECLARE_OP1(DW_CFA_undefined, OT_Register);
192 DECLARE_OP1(DW_CFA_same_value, OT_Register);
193 DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
194 DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
195 DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
196 DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
197 DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
198 DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
199 DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
200 DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
201 DECLARE_OP1(DW_CFA_restore, OT_Register);
202 DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
203 DECLARE_OP0(DW_CFA_remember_state);
204 DECLARE_OP0(DW_CFA_restore_state);
205 DECLARE_OP0(DW_CFA_GNU_window_save);
206 DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
207 DECLARE_OP0(DW_CFA_nop);
208
209#undef DECLARE_OP0
210#undef DECLARE_OP1
211#undef DECLARE_OP2
Eugene Zelenko61a72d82016-08-18 17:56:27 +0000212
Frederic Rissac10b0d2015-02-25 22:07:43 +0000213 return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
Frederic Rissc0dd7242015-02-25 21:30:22 +0000214}
215
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000216/// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
217void CFIProgram::printOperand(raw_ostream &OS, const MCRegisterInfo *MRI,
218 bool IsEH, const Instruction &Instr,
219 unsigned OperandIdx, uint64_t Operand) const {
Frederic Rissc0dd7242015-02-25 21:30:22 +0000220 assert(OperandIdx < 2);
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000221 uint8_t Opcode = Instr.Opcode;
222 OperandType Type = getOperandTypes()[Opcode][OperandIdx];
Frederic Rissc0dd7242015-02-25 21:30:22 +0000223
224 switch (Type) {
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000225 case OT_Unset: {
Frederic Rissc0dd7242015-02-25 21:30:22 +0000226 OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000227 auto OpcodeName = CallFrameString(Opcode, Arch);
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000228 if (!OpcodeName.empty())
Frederic Rissc0dd7242015-02-25 21:30:22 +0000229 OS << " " << OpcodeName;
230 else
231 OS << format(" Opcode %x", Opcode);
232 break;
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000233 }
Frederic Rissc0dd7242015-02-25 21:30:22 +0000234 case OT_None:
235 break;
236 case OT_Address:
237 OS << format(" %" PRIx64, Operand);
238 break;
239 case OT_Offset:
240 // The offsets are all encoded in a unsigned form, but in practice
241 // consumers use them signed. It's most certainly legacy due to
242 // the lack of signed variants in the first Dwarf standards.
243 OS << format(" %+" PRId64, int64_t(Operand));
244 break;
245 case OT_FactoredCodeOffset: // Always Unsigned
246 if (CodeAlignmentFactor)
247 OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
248 else
249 OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
250 break;
251 case OT_SignedFactDataOffset:
252 if (DataAlignmentFactor)
253 OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
254 else
255 OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
256 break;
257 case OT_UnsignedFactDataOffset:
258 if (DataAlignmentFactor)
259 OS << format(" %" PRId64, Operand * DataAlignmentFactor);
260 else
261 OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
262 break;
263 case OT_Register:
Frederic Rissde374342015-02-25 22:30:09 +0000264 OS << format(" reg%" PRId64, Operand);
Frederic Rissc0dd7242015-02-25 21:30:22 +0000265 break;
266 case OT_Expression:
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000267 assert(Instr.Expression && "missing DWARFExpression object");
268 OS << " ";
Markus Lavin76dda212019-02-21 08:20:24 +0000269 Instr.Expression->print(OS, MRI, nullptr, IsEH);
Frederic Rissc0dd7242015-02-25 21:30:22 +0000270 break;
271 }
272}
273
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000274void CFIProgram::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH,
275 unsigned IndentLevel) const {
Frederic Riss056ad052015-02-25 21:30:16 +0000276 for (const auto &Instr : Instructions) {
277 uint8_t Opcode = Instr.Opcode;
278 if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
279 Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000280 OS.indent(2 * IndentLevel);
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000281 OS << CallFrameString(Opcode, Arch) << ":";
Frederic Rissc0dd7242015-02-25 21:30:22 +0000282 for (unsigned i = 0; i < Instr.Ops.size(); ++i)
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000283 printOperand(OS, MRI, IsEH, Instr, i, Instr.Ops[i]);
Frederic Rissc0dd7242015-02-25 21:30:22 +0000284 OS << '\n';
Frederic Riss056ad052015-02-25 21:30:16 +0000285 }
286}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000287
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000288void CIE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const {
289 OS << format("%08x %08x %08x CIE", (uint32_t)Offset, (uint32_t)Length,
290 DW_CIE_ID)
291 << "\n";
292 OS << format(" Version: %d\n", Version);
293 OS << " Augmentation: \"" << Augmentation << "\"\n";
294 if (Version >= 4) {
295 OS << format(" Address size: %u\n", (uint32_t)AddressSize);
296 OS << format(" Segment desc size: %u\n",
297 (uint32_t)SegmentDescriptorSize);
298 }
299 OS << format(" Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor);
300 OS << format(" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor);
301 OS << format(" Return address column: %d\n", (int32_t)ReturnAddressRegister);
302 if (Personality)
Petar Jovanovic95817d32019-03-07 16:31:08 +0000303 OS << format(" Personality Address: %016" PRIx64 "\n", *Personality);
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000304 if (!AugmentationData.empty()) {
305 OS << " Augmentation data: ";
306 for (uint8_t Byte : AugmentationData)
307 OS << ' ' << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
308 OS << "\n";
309 }
310 OS << "\n";
311 CFIs.dump(OS, MRI, IsEH);
312 OS << "\n";
313}
314
315void FDE::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const {
316 OS << format("%08x %08x %08x FDE ", (uint32_t)Offset, (uint32_t)Length,
317 (int32_t)LinkedCIEOffset);
318 OS << format("cie=%08x pc=%08x...%08x\n", (int32_t)LinkedCIEOffset,
319 (uint32_t)InitialLocation,
320 (uint32_t)InitialLocation + (uint32_t)AddressRange);
321 if (LSDAAddress)
Petar Jovanovic95817d32019-03-07 16:31:08 +0000322 OS << format(" LSDA Address: %016" PRIx64 "\n", *LSDAAddress);
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000323 CFIs.dump(OS, MRI, IsEH);
324 OS << "\n";
325}
326
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000327DWARFDebugFrame::DWARFDebugFrame(Triple::ArchType Arch,
328 bool IsEH, uint64_t EHFrameAddress)
329 : Arch(Arch), IsEH(IsEH), EHFrameAddress(EHFrameAddress) {}
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000330
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000331DWARFDebugFrame::~DWARFDebugFrame() = default;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000332
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000333static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
334 uint32_t Offset, int Length) {
335 errs() << "DUMP: ";
336 for (int i = 0; i < Length; ++i) {
337 uint8_t c = Data.getU8(&Offset);
338 errs().write_hex(c); errs() << " ";
339 }
340 errs() << "\n";
341}
342
Galina Kistanova3c0505d2017-06-14 17:32:53 +0000343// This is a workaround for old compilers which do not allow
344// noreturn attribute usage in lambdas. Once the support for those
345// compilers are phased out, we can remove this and return back to
346// a ReportError lambda: [StartOffset](const char *ErrorMsg).
David Blaikie6ab0eb42017-06-19 19:01:08 +0000347static void LLVM_ATTRIBUTE_NORETURN ReportError(uint32_t StartOffset,
348 const char *ErrorMsg) {
349 std::string Str;
350 raw_string_ostream OS(Str);
351 OS << format(ErrorMsg, StartOffset);
352 OS.flush();
353 report_fatal_error(Str);
Galina Kistanova3c0505d2017-06-14 17:32:53 +0000354}
355
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000356void DWARFDebugFrame::parse(DWARFDataExtractor Data) {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000357 uint32_t Offset = 0;
Frederic Rissbaf195f2015-02-25 21:30:09 +0000358 DenseMap<uint32_t, CIE *> CIEs;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000359
360 while (Data.isValidOffset(Offset)) {
361 uint32_t StartOffset = Offset;
362
363 bool IsDWARF64 = false;
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +0000364 uint64_t Length = Data.getRelocatedValue(4, &Offset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000365 uint64_t Id;
366
Igor Kudrin3daefb02019-07-24 11:34:29 +0000367 if (Length == dwarf::DW_LENGTH_DWARF64) {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000368 // DWARF-64 is distinguished by the first 32 bits of the initial length
369 // field being 0xffffffff. Then, the next 64 bits are the actual entry
370 // length.
371 IsDWARF64 = true;
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +0000372 Length = Data.getRelocatedValue(8, &Offset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000373 }
374
375 // At this point, Offset points to the next field after Length.
376 // Length is the structure size excluding itself. Compute an offset one
377 // past the end of the structure (needed to know how many instructions to
378 // read).
379 // TODO: For honest DWARF64 support, DataExtractor will have to treat
380 // offset_ptr as uint64_t*
Igor Laevsky03a670c2016-01-26 15:09:42 +0000381 uint32_t StartStructureOffset = Offset;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000382 uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
383
384 // The Id field's size depends on the DWARF format
Igor Laevsky03a670c2016-01-26 15:09:42 +0000385 Id = Data.getUnsigned(&Offset, (IsDWARF64 && !IsEH) ? 8 : 4);
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000386 bool IsCIE =
387 ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID || (IsEH && !Id));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000388
389 if (IsCIE) {
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000390 uint8_t Version = Data.getU8(&Offset);
391 const char *Augmentation = Data.getCStr(&Offset);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000392 StringRef AugmentationString(Augmentation ? Augmentation : "");
393 uint8_t AddressSize = Version < 4 ? Data.getAddressSize() :
394 Data.getU8(&Offset);
Keith Walkerea9483f2015-05-12 15:25:08 +0000395 Data.setAddressSize(AddressSize);
396 uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000397 uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
398 int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
George Rimar7e981f32018-12-04 10:01:39 +0000399 uint64_t ReturnAddressRegister =
400 Version == 1 ? Data.getU8(&Offset) : Data.getULEB128(&Offset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000401
Igor Laevsky03a670c2016-01-26 15:09:42 +0000402 // Parse the augmentation data for EH CIEs
Igor Laevskyff291b52016-01-27 14:05:35 +0000403 StringRef AugmentationData("");
Maksim Panchenkofa762cc2018-07-09 18:45:38 +0000404 uint32_t FDEPointerEncoding = DW_EH_PE_absptr;
Igor Laevskyff291b52016-01-27 14:05:35 +0000405 uint32_t LSDAPointerEncoding = DW_EH_PE_omit;
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000406 Optional<uint64_t> Personality;
407 Optional<uint32_t> PersonalityEncoding;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000408 if (IsEH) {
Igor Laevskyff291b52016-01-27 14:05:35 +0000409 Optional<uint64_t> AugmentationLength;
410 uint32_t StartAugmentationOffset;
411 uint32_t EndAugmentationOffset;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000412
413 // Walk the augmentation string to get all the augmentation data.
414 for (unsigned i = 0, e = AugmentationString.size(); i != e; ++i) {
415 switch (AugmentationString[i]) {
416 default:
David Blaikie6ab0eb42017-06-19 19:01:08 +0000417 ReportError(StartOffset,
418 "Unknown augmentation character in entry at %lx");
Igor Laevsky03a670c2016-01-26 15:09:42 +0000419 case 'L':
Igor Laevsky03a670c2016-01-26 15:09:42 +0000420 LSDAPointerEncoding = Data.getU8(&Offset);
421 break;
422 case 'P': {
423 if (Personality)
David Blaikie6ab0eb42017-06-19 19:01:08 +0000424 ReportError(StartOffset,
425 "Duplicate personality in entry at %lx");
Igor Laevsky03a670c2016-01-26 15:09:42 +0000426 PersonalityEncoding = Data.getU8(&Offset);
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000427 Personality = Data.getEncodedPointer(
428 &Offset, *PersonalityEncoding,
429 EHFrameAddress ? EHFrameAddress + Offset : 0);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000430 break;
431 }
432 case 'R':
Igor Laevsky03a670c2016-01-26 15:09:42 +0000433 FDEPointerEncoding = Data.getU8(&Offset);
434 break;
Fangrui Songbd088562018-05-08 06:21:12 +0000435 case 'S':
436 // Current frame is a signal trampoline.
437 break;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000438 case 'z':
439 if (i)
David Blaikie6ab0eb42017-06-19 19:01:08 +0000440 ReportError(StartOffset,
441 "'z' must be the first character at %lx");
Igor Laevsky03a670c2016-01-26 15:09:42 +0000442 // Parse the augmentation length first. We only parse it if
443 // the string contains a 'z'.
444 AugmentationLength = Data.getULEB128(&Offset);
445 StartAugmentationOffset = Offset;
Igor Laevskyff291b52016-01-27 14:05:35 +0000446 EndAugmentationOffset = Offset +
447 static_cast<uint32_t>(*AugmentationLength);
Luke Cheeseman41a9e532018-12-21 10:45:08 +0000448 break;
449 case 'B':
450 // B-Key is used for signing functions associated with this
451 // augmentation string
452 break;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000453 }
454 }
455
Igor Laevskyff291b52016-01-27 14:05:35 +0000456 if (AugmentationLength.hasValue()) {
457 if (Offset != EndAugmentationOffset)
David Blaikie6ab0eb42017-06-19 19:01:08 +0000458 ReportError(StartOffset, "Parsing augmentation data at %lx failed");
Igor Laevsky03a670c2016-01-26 15:09:42 +0000459
Igor Laevskyff291b52016-01-27 14:05:35 +0000460 AugmentationData = Data.getData().slice(StartAugmentationOffset,
461 EndAugmentationOffset);
462 }
Igor Laevsky03a670c2016-01-26 15:09:42 +0000463 }
464
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000465 auto Cie = llvm::make_unique<CIE>(
466 StartOffset, Length, Version, AugmentationString, AddressSize,
467 SegmentDescriptorSize, CodeAlignmentFactor, DataAlignmentFactor,
468 ReturnAddressRegister, AugmentationData, FDEPointerEncoding,
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000469 LSDAPointerEncoding, Personality, PersonalityEncoding, Arch);
Frederic Rissbaf195f2015-02-25 21:30:09 +0000470 CIEs[StartOffset] = Cie.get();
471 Entries.emplace_back(std::move(Cie));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000472 } else {
473 // FDE
474 uint64_t CIEPointer = Id;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000475 uint64_t InitialLocation = 0;
476 uint64_t AddressRange = 0;
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000477 Optional<uint64_t> LSDAAddress;
Igor Laevsky03a670c2016-01-26 15:09:42 +0000478 CIE *Cie = CIEs[IsEH ? (StartStructureOffset - CIEPointer) : CIEPointer];
479
480 if (IsEH) {
481 // The address size is encoded in the CIE we reference.
482 if (!Cie)
David Blaikie6ab0eb42017-06-19 19:01:08 +0000483 ReportError(StartOffset,
484 "Parsing FDE data at %lx failed due to missing CIE");
Igor Laevsky03a670c2016-01-26 15:09:42 +0000485
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000486 if (auto Val = Data.getEncodedPointer(
487 &Offset, Cie->getFDEPointerEncoding(),
488 EHFrameAddress ? EHFrameAddress + Offset : 0)) {
489 InitialLocation = *Val;
490 }
491 if (auto Val = Data.getEncodedPointer(
492 &Offset, Cie->getFDEPointerEncoding(), 0)) {
493 AddressRange = *Val;
494 }
Igor Laevsky03a670c2016-01-26 15:09:42 +0000495
Igor Laevsky03a670c2016-01-26 15:09:42 +0000496 StringRef AugmentationString = Cie->getAugmentationString();
497 if (!AugmentationString.empty()) {
498 // Parse the augmentation length and data for this FDE.
499 uint64_t AugmentationLength = Data.getULEB128(&Offset);
500
501 uint32_t EndAugmentationOffset =
502 Offset + static_cast<uint32_t>(AugmentationLength);
503
504 // Decode the LSDA if the CIE augmentation string said we should.
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000505 if (Cie->getLSDAPointerEncoding() != DW_EH_PE_omit) {
506 LSDAAddress = Data.getEncodedPointer(
507 &Offset, Cie->getLSDAPointerEncoding(),
508 EHFrameAddress ? Offset + EHFrameAddress : 0);
509 }
Igor Laevsky03a670c2016-01-26 15:09:42 +0000510
511 if (Offset != EndAugmentationOffset)
David Blaikie6ab0eb42017-06-19 19:01:08 +0000512 ReportError(StartOffset, "Parsing augmentation data at %lx failed");
Igor Laevsky03a670c2016-01-26 15:09:42 +0000513 }
514 } else {
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +0000515 InitialLocation = Data.getRelocatedAddress(&Offset);
516 AddressRange = Data.getRelocatedAddress(&Offset);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000517 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000518
Alexey Samsonova08b1612014-04-28 23:00:06 +0000519 Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
Frederic Rissbaf195f2015-02-25 21:30:09 +0000520 InitialLocation, AddressRange,
Luke Cheesemanf57d7d82018-12-18 10:37:42 +0000521 Cie, LSDAAddress, Arch));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000522 }
523
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000524 if (Error E =
525 Entries.back()->cfis().parse(Data, &Offset, EndStructureOffset)) {
526 report_fatal_error(toString(std::move(E)));
527 }
Eli Bendersky705085d2013-02-21 22:53:19 +0000528
Igor Laevsky03a670c2016-01-26 15:09:42 +0000529 if (Offset != EndStructureOffset)
David Blaikie6ab0eb42017-06-19 19:01:08 +0000530 ReportError(StartOffset, "Parsing entry instructions at %lx failed");
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000531 }
532}
533
Adrian Prantl62528e62017-09-21 18:52:03 +0000534FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
Fangrui Song78ee2fb2019-06-30 11:19:56 +0000535 auto It = partition_point(Entries, [=](const std::unique_ptr<FrameEntry> &E) {
536 return E->getOffset() < Offset;
Fangrui Songdc8de602019-06-21 05:40:31 +0000537 });
Adrian Prantl62528e62017-09-21 18:52:03 +0000538 if (It != Entries.end() && (*It)->getOffset() == Offset)
539 return It->get();
540 return nullptr;
541}
542
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000543void DWARFDebugFrame::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
544 Optional<uint64_t> Offset) const {
Adrian Prantl62528e62017-09-21 18:52:03 +0000545 if (Offset) {
546 if (auto *Entry = getEntryAtOffset(*Offset))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000547 Entry->dump(OS, MRI, IsEH);
Adrian Prantl62528e62017-09-21 18:52:03 +0000548 return;
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000549 }
Adrian Prantl62528e62017-09-21 18:52:03 +0000550
551 OS << "\n";
552 for (const auto &Entry : Entries)
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000553 Entry->dump(OS, MRI, IsEH);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000554}