blob: 4bfd0451ed5a63920cc524e96282cd1cec6fcfef [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===--------------------------- DwarfParser.hpp --------------------------===//
2//
Chandler Carruth61860a52019-01-19 10:56:40 +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
Saleem Abdulrasool17552662015-04-24 19:39:17 +00006//
7//
8// Parses DWARF CFIs (FDEs and CIEs).
9//
10//===----------------------------------------------------------------------===//
11
12#ifndef __DWARF_PARSER_HPP__
13#define __DWARF_PARSER_HPP__
14
15#include <inttypes.h>
16#include <stdint.h>
17#include <stdio.h>
18#include <stdlib.h>
19
Saleem Abdulrasool17552662015-04-24 19:39:17 +000020#include "libunwind.h"
21#include "dwarf2.h"
Daniel Cederman9f2f07a2019-01-14 10:15:20 +000022#include "Registers.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000023
Saleem Abdulrasool286a9162017-01-25 02:27:45 +000024#include "config.h"
Saleem Abdulrasoold1be5b02017-01-21 16:22:57 +000025
Saleem Abdulrasool17552662015-04-24 19:39:17 +000026namespace libunwind {
27
28/// CFI_Parser does basic parsing of a CFI (Call Frame Information) records.
Ed Maste4c43c3d2016-07-19 17:15:50 +000029/// See DWARF Spec for details:
Saleem Abdulrasool17552662015-04-24 19:39:17 +000030/// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
31///
32template <typename A>
33class CFI_Parser {
34public:
35 typedef typename A::pint_t pint_t;
36
37 /// Information encoded in a CIE (Common Information Entry)
38 struct CIE_Info {
39 pint_t cieStart;
40 pint_t cieLength;
41 pint_t cieInstructions;
42 uint8_t pointerEncoding;
43 uint8_t lsdaEncoding;
44 uint8_t personalityEncoding;
45 uint8_t personalityOffsetInCIE;
46 pint_t personality;
47 uint32_t codeAlignFactor;
48 int dataAlignFactor;
49 bool isSignalFrame;
50 bool fdesHaveAugmentationData;
51 uint8_t returnAddressRegister;
Luke Cheesemancba83c32018-12-17 11:43:24 +000052#if defined(_LIBUNWIND_TARGET_AARCH64)
53 bool addressesSignedWithBKey;
54#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000055 };
56
57 /// Information about an FDE (Frame Description Entry)
58 struct FDE_Info {
59 pint_t fdeStart;
60 pint_t fdeLength;
61 pint_t fdeInstructions;
62 pint_t pcStart;
63 pint_t pcEnd;
64 pint_t lsda;
65 };
66
67 enum {
Ed Maste567984c2016-07-20 15:19:09 +000068 kMaxRegisterNumber = _LIBUNWIND_HIGHEST_DWARF_REGISTER
Saleem Abdulrasool17552662015-04-24 19:39:17 +000069 };
70 enum RegisterSavedWhere {
71 kRegisterUnused,
Daniel Kiss163101b2020-09-16 23:03:19 +020072 kRegisterUndefined,
Saleem Abdulrasool17552662015-04-24 19:39:17 +000073 kRegisterInCFA,
74 kRegisterOffsetFromCFA,
75 kRegisterInRegister,
76 kRegisterAtExpression,
77 kRegisterIsExpression
78 };
79 struct RegisterLocation {
80 RegisterSavedWhere location;
Sterling Augustinec0ef0db2020-03-04 16:29:58 -080081 bool initialStateSaved;
Saleem Abdulrasool17552662015-04-24 19:39:17 +000082 int64_t value;
83 };
84 /// Information about a frame layout and registers saved determined
Ed Maste4c43c3d2016-07-19 17:15:50 +000085 /// by "running" the DWARF FDE "instructions"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000086 struct PrologInfo {
87 uint32_t cfaRegister;
88 int32_t cfaRegisterOffset; // CFA = (cfaRegister)+cfaRegisterOffset
89 int64_t cfaExpression; // CFA = expression
90 uint32_t spExtraArgSize;
91 uint32_t codeOffsetAtStackDecrement;
92 bool registersInOtherRegisters;
93 bool sameValueUsed;
Martin Storsjof10f3c92017-10-27 07:59:01 +000094 RegisterLocation savedRegisters[kMaxRegisterNumber + 1];
Sterling Augustinec0ef0db2020-03-04 16:29:58 -080095 enum class InitializeTime { kLazy, kNormal };
96
97 // When saving registers, this data structure is lazily initialized.
98 PrologInfo(InitializeTime IT = InitializeTime::kNormal) {
99 if (IT == InitializeTime::kNormal)
100 memset(this, 0, sizeof(*this));
101 }
102 void checkSaveRegister(uint64_t reg, PrologInfo &initialState) {
103 if (!savedRegisters[reg].initialStateSaved) {
104 initialState.savedRegisters[reg] = savedRegisters[reg];
105 savedRegisters[reg].initialStateSaved = true;
106 }
107 }
108 void setRegister(uint64_t reg, RegisterSavedWhere newLocation,
109 int64_t newValue, PrologInfo &initialState) {
110 checkSaveRegister(reg, initialState);
111 savedRegisters[reg].location = newLocation;
112 savedRegisters[reg].value = newValue;
113 }
114 void setRegisterLocation(uint64_t reg, RegisterSavedWhere newLocation,
115 PrologInfo &initialState) {
116 checkSaveRegister(reg, initialState);
117 savedRegisters[reg].location = newLocation;
118 }
119 void setRegisterValue(uint64_t reg, int64_t newValue,
120 PrologInfo &initialState) {
121 checkSaveRegister(reg, initialState);
122 savedRegisters[reg].value = newValue;
123 }
124 void restoreRegisterToInitialState(uint64_t reg, PrologInfo &initialState) {
125 if (savedRegisters[reg].initialStateSaved)
126 savedRegisters[reg] = initialState.savedRegisters[reg];
127 // else the register still holds its initial state
128 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000129 };
130
131 struct PrologInfoStackEntry {
132 PrologInfoStackEntry(PrologInfoStackEntry *n, const PrologInfo &i)
133 : next(n), info(i) {}
134 PrologInfoStackEntry *next;
135 PrologInfo info;
136 };
137
Daniel Kiss6f670c82020-10-30 17:42:23 +0100138 struct RememberStack {
139 PrologInfoStackEntry *entry;
140 RememberStack() : entry(nullptr) {}
141 ~RememberStack() {
142#if defined(_LIBUNWIND_REMEMBER_CLEANUP_NEEDED)
143 // Clean up rememberStack. Even in the case where every
144 // DW_CFA_remember_state is paired with a DW_CFA_restore_state,
145 // parseInstructions can skip restore opcodes if it reaches the target PC
146 // and stops interpreting, so we have to make sure we don't leak memory.
147 while (entry) {
148 PrologInfoStackEntry *next = entry->next;
149 _LIBUNWIND_REMEMBER_FREE(entry);
150 entry = next;
151 }
152#endif
153 }
154 };
155
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000156 static bool findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
Ryan Prichard0cff8572020-09-16 01:22:55 -0700157 uintptr_t sectionLength, pint_t fdeHint, FDE_Info *fdeInfo,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000158 CIE_Info *cieInfo);
159 static const char *decodeFDE(A &addressSpace, pint_t fdeStart,
160 FDE_Info *fdeInfo, CIE_Info *cieInfo);
161 static bool parseFDEInstructions(A &addressSpace, const FDE_Info &fdeInfo,
162 const CIE_Info &cieInfo, pint_t upToPC,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000163 int arch, PrologInfo *results);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000164
165 static const char *parseCIE(A &addressSpace, pint_t cie, CIE_Info *cieInfo);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000166};
167
168/// Parse a FDE into a CIE_Info and an FDE_Info
169template <typename A>
170const char *CFI_Parser<A>::decodeFDE(A &addressSpace, pint_t fdeStart,
171 FDE_Info *fdeInfo, CIE_Info *cieInfo) {
172 pint_t p = fdeStart;
173 pint_t cfiLength = (pint_t)addressSpace.get32(p);
174 p += 4;
175 if (cfiLength == 0xffffffff) {
176 // 0xffffffff means length is really next 8 bytes
177 cfiLength = (pint_t)addressSpace.get64(p);
178 p += 8;
179 }
180 if (cfiLength == 0)
Ryan Prichard0cff8572020-09-16 01:22:55 -0700181 return "FDE has zero length"; // zero terminator
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000182 uint32_t ciePointer = addressSpace.get32(p);
183 if (ciePointer == 0)
184 return "FDE is really a CIE"; // this is a CIE not an FDE
185 pint_t nextCFI = p + cfiLength;
186 pint_t cieStart = p - ciePointer;
187 const char *err = parseCIE(addressSpace, cieStart, cieInfo);
188 if (err != NULL)
189 return err;
190 p += 4;
Ed Maste1b651132016-07-19 17:28:38 +0000191 // Parse pc begin and range.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000192 pint_t pcStart =
193 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
194 pint_t pcRange =
195 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding & 0x0F);
Ed Maste1b651132016-07-19 17:28:38 +0000196 // Parse rest of info.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000197 fdeInfo->lsda = 0;
Ed Maste1b651132016-07-19 17:28:38 +0000198 // Check for augmentation length.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000199 if (cieInfo->fdesHaveAugmentationData) {
200 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
201 pint_t endOfAug = p + augLen;
202 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
Ed Maste1b651132016-07-19 17:28:38 +0000203 // Peek at value (without indirection). Zero means no LSDA.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000204 pint_t lsdaStart = p;
205 if (addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding & 0x0F) !=
206 0) {
Ed Maste1b651132016-07-19 17:28:38 +0000207 // Reset pointer and re-parse LSDA address.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000208 p = lsdaStart;
209 fdeInfo->lsda =
210 addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
211 }
212 }
213 p = endOfAug;
214 }
215 fdeInfo->fdeStart = fdeStart;
216 fdeInfo->fdeLength = nextCFI - fdeStart;
217 fdeInfo->fdeInstructions = p;
218 fdeInfo->pcStart = pcStart;
219 fdeInfo->pcEnd = pcStart + pcRange;
220 return NULL; // success
221}
222
223/// Scan an eh_frame section to find an FDE for a pc
224template <typename A>
225bool CFI_Parser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
Ryan Prichard0cff8572020-09-16 01:22:55 -0700226 uintptr_t sectionLength, pint_t fdeHint,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000227 FDE_Info *fdeInfo, CIE_Info *cieInfo) {
228 //fprintf(stderr, "findFDE(0x%llX)\n", (long long)pc);
229 pint_t p = (fdeHint != 0) ? fdeHint : ehSectionStart;
Ryan Prichard0cff8572020-09-16 01:22:55 -0700230 const pint_t ehSectionEnd = (sectionLength == UINTPTR_MAX)
231 ? static_cast<pint_t>(-1)
232 : (ehSectionStart + sectionLength);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000233 while (p < ehSectionEnd) {
234 pint_t currentCFI = p;
235 //fprintf(stderr, "findFDE() CFI at 0x%llX\n", (long long)p);
236 pint_t cfiLength = addressSpace.get32(p);
237 p += 4;
238 if (cfiLength == 0xffffffff) {
239 // 0xffffffff means length is really next 8 bytes
240 cfiLength = (pint_t)addressSpace.get64(p);
241 p += 8;
242 }
243 if (cfiLength == 0)
Ryan Prichard0cff8572020-09-16 01:22:55 -0700244 return false; // zero terminator
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000245 uint32_t id = addressSpace.get32(p);
246 if (id == 0) {
Ed Maste1b651132016-07-19 17:28:38 +0000247 // Skip over CIEs.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000248 p += cfiLength;
249 } else {
Ed Maste1b651132016-07-19 17:28:38 +0000250 // Process FDE to see if it covers pc.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000251 pint_t nextCFI = p + cfiLength;
252 uint32_t ciePointer = addressSpace.get32(p);
253 pint_t cieStart = p - ciePointer;
Ed Maste1b651132016-07-19 17:28:38 +0000254 // Validate pointer to CIE is within section.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000255 if ((ehSectionStart <= cieStart) && (cieStart < ehSectionEnd)) {
256 if (parseCIE(addressSpace, cieStart, cieInfo) == NULL) {
257 p += 4;
Ed Maste1b651132016-07-19 17:28:38 +0000258 // Parse pc begin and range.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000259 pint_t pcStart =
260 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
261 pint_t pcRange = addressSpace.getEncodedP(
262 p, nextCFI, cieInfo->pointerEncoding & 0x0F);
Ed Maste1b651132016-07-19 17:28:38 +0000263 // Test if pc is within the function this FDE covers.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000264 if ((pcStart < pc) && (pc <= pcStart + pcRange)) {
265 // parse rest of info
266 fdeInfo->lsda = 0;
267 // check for augmentation length
268 if (cieInfo->fdesHaveAugmentationData) {
269 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
270 pint_t endOfAug = p + augLen;
271 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
Ed Maste1b651132016-07-19 17:28:38 +0000272 // Peek at value (without indirection). Zero means no LSDA.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000273 pint_t lsdaStart = p;
274 if (addressSpace.getEncodedP(
275 p, nextCFI, cieInfo->lsdaEncoding & 0x0F) != 0) {
Ed Maste1b651132016-07-19 17:28:38 +0000276 // Reset pointer and re-parse LSDA address.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000277 p = lsdaStart;
278 fdeInfo->lsda = addressSpace
279 .getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
280 }
281 }
282 p = endOfAug;
283 }
284 fdeInfo->fdeStart = currentCFI;
285 fdeInfo->fdeLength = nextCFI - currentCFI;
286 fdeInfo->fdeInstructions = p;
287 fdeInfo->pcStart = pcStart;
288 fdeInfo->pcEnd = pcStart + pcRange;
289 return true;
290 } else {
291 // pc is not in begin/range, skip this FDE
292 }
293 } else {
Ed Maste1b651132016-07-19 17:28:38 +0000294 // Malformed CIE, now augmentation describing pc range encoding.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000295 }
296 } else {
297 // malformed FDE. CIE is bad
298 }
299 p = nextCFI;
300 }
301 }
302 return false;
303}
304
305/// Extract info from a CIE
306template <typename A>
307const char *CFI_Parser<A>::parseCIE(A &addressSpace, pint_t cie,
308 CIE_Info *cieInfo) {
309 cieInfo->pointerEncoding = 0;
310 cieInfo->lsdaEncoding = DW_EH_PE_omit;
311 cieInfo->personalityEncoding = 0;
312 cieInfo->personalityOffsetInCIE = 0;
313 cieInfo->personality = 0;
314 cieInfo->codeAlignFactor = 0;
315 cieInfo->dataAlignFactor = 0;
316 cieInfo->isSignalFrame = false;
317 cieInfo->fdesHaveAugmentationData = false;
Luke Cheesemancba83c32018-12-17 11:43:24 +0000318#if defined(_LIBUNWIND_TARGET_AARCH64)
319 cieInfo->addressesSignedWithBKey = false;
320#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000321 cieInfo->cieStart = cie;
322 pint_t p = cie;
323 pint_t cieLength = (pint_t)addressSpace.get32(p);
324 p += 4;
325 pint_t cieContentEnd = p + cieLength;
326 if (cieLength == 0xffffffff) {
327 // 0xffffffff means length is really next 8 bytes
328 cieLength = (pint_t)addressSpace.get64(p);
329 p += 8;
330 cieContentEnd = p + cieLength;
331 }
332 if (cieLength == 0)
333 return NULL;
334 // CIE ID is always 0
335 if (addressSpace.get32(p) != 0)
336 return "CIE ID is not zero";
337 p += 4;
338 // Version is always 1 or 3
339 uint8_t version = addressSpace.get8(p);
340 if ((version != 1) && (version != 3))
341 return "CIE version is not 1 or 3";
342 ++p;
343 // save start of augmentation string and find end
344 pint_t strStart = p;
345 while (addressSpace.get8(p) != 0)
346 ++p;
347 ++p;
348 // parse code aligment factor
349 cieInfo->codeAlignFactor = (uint32_t)addressSpace.getULEB128(p, cieContentEnd);
350 // parse data alignment factor
351 cieInfo->dataAlignFactor = (int)addressSpace.getSLEB128(p, cieContentEnd);
352 // parse return address register
Ryan Prichard58b19e62020-07-13 22:06:22 -0700353 uint64_t raReg = (version == 1) ? addressSpace.get8(p++)
354 : addressSpace.getULEB128(p, cieContentEnd);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000355 assert(raReg < 255 && "return address register too large");
356 cieInfo->returnAddressRegister = (uint8_t)raReg;
357 // parse augmentation data based on augmentation string
358 const char *result = NULL;
359 if (addressSpace.get8(strStart) == 'z') {
360 // parse augmentation data length
361 addressSpace.getULEB128(p, cieContentEnd);
362 for (pint_t s = strStart; addressSpace.get8(s) != '\0'; ++s) {
363 switch (addressSpace.get8(s)) {
364 case 'z':
365 cieInfo->fdesHaveAugmentationData = true;
366 break;
367 case 'P':
368 cieInfo->personalityEncoding = addressSpace.get8(p);
369 ++p;
370 cieInfo->personalityOffsetInCIE = (uint8_t)(p - cie);
371 cieInfo->personality = addressSpace
372 .getEncodedP(p, cieContentEnd, cieInfo->personalityEncoding);
373 break;
374 case 'L':
375 cieInfo->lsdaEncoding = addressSpace.get8(p);
376 ++p;
377 break;
378 case 'R':
379 cieInfo->pointerEncoding = addressSpace.get8(p);
380 ++p;
381 break;
382 case 'S':
383 cieInfo->isSignalFrame = true;
384 break;
Luke Cheesemancba83c32018-12-17 11:43:24 +0000385#if defined(_LIBUNWIND_TARGET_AARCH64)
386 case 'B':
387 cieInfo->addressesSignedWithBKey = true;
388 break;
389#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000390 default:
391 // ignore unknown letters
392 break;
393 }
394 }
395 }
396 cieInfo->cieLength = cieContentEnd - cieInfo->cieStart;
397 cieInfo->cieInstructions = p;
398 return result;
399}
400
401
Ed Maste4c43c3d2016-07-19 17:15:50 +0000402/// "run" the DWARF instructions and create the abstact PrologInfo for an FDE
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000403template <typename A>
404bool CFI_Parser<A>::parseFDEInstructions(A &addressSpace,
405 const FDE_Info &fdeInfo,
406 const CIE_Info &cieInfo, pint_t upToPC,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000407 int arch, PrologInfo *results) {
Daniel Kiss6f670c82020-10-30 17:42:23 +0100408 // Alloca is used for the allocation of the rememberStack entries. It removes
409 // the dependency on new/malloc but the below for loop can not be refactored
410 // into functions. Entry could be saved during the processing of a CIE and
411 // restored by an FDE.
412 RememberStack rememberStack;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000413
Daniel Kiss6f670c82020-10-30 17:42:23 +0100414 struct ParseInfo {
415 pint_t instructions;
416 pint_t instructionsEnd;
417 pint_t pcoffset;
418 };
Jorge Gorbe Moyafd8399e2020-02-18 11:48:02 -0800419
Daniel Kiss6f670c82020-10-30 17:42:23 +0100420 ParseInfo parseInfoArray[] = {
421 {cieInfo.cieInstructions, cieInfo.cieStart + cieInfo.cieLength,
422 (pint_t)(-1)},
423 {fdeInfo.fdeInstructions, fdeInfo.fdeStart + fdeInfo.fdeLength,
424 upToPC - fdeInfo.pcStart}};
Jorge Gorbe Moyafd8399e2020-02-18 11:48:02 -0800425
Daniel Kiss6f670c82020-10-30 17:42:23 +0100426 for (const auto &info : parseInfoArray) {
427 pint_t p = info.instructions;
428 pint_t instructionsEnd = info.instructionsEnd;
429 pint_t pcoffset = info.pcoffset;
430 pint_t codeOffset = 0;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000431
Daniel Kiss6f670c82020-10-30 17:42:23 +0100432 // initialState initialized as registers in results are modified. Use
433 // PrologInfo accessor functions to avoid reading uninitialized data.
434 PrologInfo initialState(PrologInfo::InitializeTime::kLazy);
Saleem Abdulrasoold1be5b02017-01-21 16:22:57 +0000435
Daniel Kiss6f670c82020-10-30 17:42:23 +0100436 _LIBUNWIND_TRACE_DWARF("parseFDEInstructions(instructions=0x%0" PRIx64
437 ")\n",
438 static_cast<uint64_t>(instructionsEnd));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000439
Daniel Kiss6f670c82020-10-30 17:42:23 +0100440 // see DWARF Spec, section 6.4.2 for details on unwind opcodes
441 while ((p < instructionsEnd) && (codeOffset < pcoffset)) {
442 uint64_t reg;
443 uint64_t reg2;
444 int64_t offset;
445 uint64_t length;
446 uint8_t opcode = addressSpace.get8(p);
447 uint8_t operand;
448
449 ++p;
450 switch (opcode) {
451 case DW_CFA_nop:
452 _LIBUNWIND_TRACE_DWARF("DW_CFA_nop\n");
453 break;
454 case DW_CFA_set_loc:
455 codeOffset = addressSpace.getEncodedP(p, instructionsEnd,
456 cieInfo.pointerEncoding);
457 _LIBUNWIND_TRACE_DWARF("DW_CFA_set_loc\n");
458 break;
459 case DW_CFA_advance_loc1:
460 codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
461 p += 1;
462 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
463 static_cast<uint64_t>(codeOffset));
464 break;
465 case DW_CFA_advance_loc2:
466 codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
467 p += 2;
468 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
469 static_cast<uint64_t>(codeOffset));
470 break;
471 case DW_CFA_advance_loc4:
472 codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
473 p += 4;
474 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
475 static_cast<uint64_t>(codeOffset));
476 break;
477 case DW_CFA_offset_extended:
478 reg = addressSpace.getULEB128(p, instructionsEnd);
479 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
480 cieInfo.dataAlignFactor;
481 if (reg > kMaxRegisterNumber) {
482 _LIBUNWIND_LOG0(
483 "malformed DW_CFA_offset_extended DWARF unwind, reg too big");
484 return false;
485 }
486 results->setRegister(reg, kRegisterInCFA, offset, initialState);
487 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended(reg=%" PRIu64 ", "
488 "offset=%" PRId64 ")\n",
489 reg, offset);
490 break;
491 case DW_CFA_restore_extended:
492 reg = addressSpace.getULEB128(p, instructionsEnd);
493 if (reg > kMaxRegisterNumber) {
494 _LIBUNWIND_LOG0(
495 "malformed DW_CFA_restore_extended DWARF unwind, reg too big");
496 return false;
497 }
498 results->restoreRegisterToInitialState(reg, initialState);
499 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_extended(reg=%" PRIu64 ")\n",
500 reg);
501 break;
502 case DW_CFA_undefined:
503 reg = addressSpace.getULEB128(p, instructionsEnd);
504 if (reg > kMaxRegisterNumber) {
505 _LIBUNWIND_LOG0(
506 "malformed DW_CFA_undefined DWARF unwind, reg too big");
507 return false;
508 }
509 results->setRegisterLocation(reg, kRegisterUndefined, initialState);
510 _LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
511 break;
512 case DW_CFA_same_value:
513 reg = addressSpace.getULEB128(p, instructionsEnd);
514 if (reg > kMaxRegisterNumber) {
515 _LIBUNWIND_LOG0(
516 "malformed DW_CFA_same_value DWARF unwind, reg too big");
517 return false;
518 }
519 // <rdar://problem/8456377> DW_CFA_same_value unsupported
520 // "same value" means register was stored in frame, but its current
521 // value has not changed, so no need to restore from frame.
522 // We model this as if the register was never saved.
523 results->setRegisterLocation(reg, kRegisterUnused, initialState);
524 // set flag to disable conversion to compact unwind
525 results->sameValueUsed = true;
526 _LIBUNWIND_TRACE_DWARF("DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
527 break;
528 case DW_CFA_register:
529 reg = addressSpace.getULEB128(p, instructionsEnd);
530 reg2 = addressSpace.getULEB128(p, instructionsEnd);
531 if (reg > kMaxRegisterNumber) {
532 _LIBUNWIND_LOG0(
533 "malformed DW_CFA_register DWARF unwind, reg too big");
534 return false;
535 }
536 if (reg2 > kMaxRegisterNumber) {
537 _LIBUNWIND_LOG0(
538 "malformed DW_CFA_register DWARF unwind, reg2 too big");
539 return false;
540 }
541 results->setRegister(reg, kRegisterInRegister, (int64_t)reg2,
542 initialState);
543 // set flag to disable conversion to compact unwind
544 results->registersInOtherRegisters = true;
545 _LIBUNWIND_TRACE_DWARF(
546 "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n", reg, reg2);
547 break;
548 case DW_CFA_remember_state: {
549 // Avoid operator new because that would be an upward dependency.
550 // Avoid malloc because it needs heap allocation.
551 PrologInfoStackEntry *entry =
552 (PrologInfoStackEntry *)_LIBUNWIND_REMEMBER_ALLOC(
553 sizeof(PrologInfoStackEntry));
554 if (entry != NULL) {
555 entry->next = rememberStack.entry;
556 entry->info = *results;
557 rememberStack.entry = entry;
558 } else {
559 return false;
560 }
561 _LIBUNWIND_TRACE_DWARF("DW_CFA_remember_state\n");
562 break;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000563 }
Daniel Kiss6f670c82020-10-30 17:42:23 +0100564 case DW_CFA_restore_state:
565 if (rememberStack.entry != NULL) {
566 PrologInfoStackEntry *top = rememberStack.entry;
567 *results = top->info;
568 rememberStack.entry = top->next;
569 _LIBUNWIND_REMEMBER_FREE(top);
570 } else {
571 return false;
572 }
573 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_state\n");
574 break;
575 case DW_CFA_def_cfa:
576 reg = addressSpace.getULEB128(p, instructionsEnd);
577 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd);
578 if (reg > kMaxRegisterNumber) {
579 _LIBUNWIND_LOG0("malformed DW_CFA_def_cfa DWARF unwind, reg too big");
580 return false;
581 }
582 results->cfaRegister = (uint32_t)reg;
583 results->cfaRegisterOffset = (int32_t)offset;
584 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64
585 ")\n",
586 reg, offset);
587 break;
588 case DW_CFA_def_cfa_register:
589 reg = addressSpace.getULEB128(p, instructionsEnd);
590 if (reg > kMaxRegisterNumber) {
591 _LIBUNWIND_LOG0(
592 "malformed DW_CFA_def_cfa_register DWARF unwind, reg too big");
593 return false;
594 }
595 results->cfaRegister = (uint32_t)reg;
596 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
597 break;
598 case DW_CFA_def_cfa_offset:
599 results->cfaRegisterOffset =
600 (int32_t)addressSpace.getULEB128(p, instructionsEnd);
601 results->codeOffsetAtStackDecrement = (uint32_t)codeOffset;
602 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset(%d)\n",
603 results->cfaRegisterOffset);
604 break;
605 case DW_CFA_def_cfa_expression:
606 results->cfaRegister = 0;
607 results->cfaExpression = (int64_t)p;
608 length = addressSpace.getULEB128(p, instructionsEnd);
609 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
610 p += static_cast<pint_t>(length);
611 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_expression(expression=0x%" PRIx64
612 ", length=%" PRIu64 ")\n",
613 results->cfaExpression, length);
614 break;
615 case DW_CFA_expression:
616 reg = addressSpace.getULEB128(p, instructionsEnd);
617 if (reg > kMaxRegisterNumber) {
618 _LIBUNWIND_LOG0(
619 "malformed DW_CFA_expression DWARF unwind, reg too big");
620 return false;
621 }
622 results->setRegister(reg, kRegisterAtExpression, (int64_t)p,
623 initialState);
624 length = addressSpace.getULEB128(p, instructionsEnd);
625 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
626 p += static_cast<pint_t>(length);
627 _LIBUNWIND_TRACE_DWARF("DW_CFA_expression(reg=%" PRIu64 ", "
628 "expression=0x%" PRIx64 ", "
629 "length=%" PRIu64 ")\n",
630 reg, results->savedRegisters[reg].value, length);
631 break;
632 case DW_CFA_offset_extended_sf:
633 reg = addressSpace.getULEB128(p, instructionsEnd);
634 if (reg > kMaxRegisterNumber) {
635 _LIBUNWIND_LOG0(
636 "malformed DW_CFA_offset_extended_sf DWARF unwind, reg too big");
637 return false;
638 }
639 offset = addressSpace.getSLEB128(p, instructionsEnd) *
640 cieInfo.dataAlignFactor;
641 results->setRegister(reg, kRegisterInCFA, offset, initialState);
642 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended_sf(reg=%" PRIu64 ", "
643 "offset=%" PRId64 ")\n",
644 reg, offset);
645 break;
646 case DW_CFA_def_cfa_sf:
647 reg = addressSpace.getULEB128(p, instructionsEnd);
648 offset = addressSpace.getSLEB128(p, instructionsEnd) *
649 cieInfo.dataAlignFactor;
650 if (reg > kMaxRegisterNumber) {
651 _LIBUNWIND_LOG0(
652 "malformed DW_CFA_def_cfa_sf DWARF unwind, reg too big");
653 return false;
654 }
655 results->cfaRegister = (uint32_t)reg;
656 results->cfaRegisterOffset = (int32_t)offset;
657 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_sf(reg=%" PRIu64 ", "
658 "offset=%" PRId64 ")\n",
659 reg, offset);
660 break;
661 case DW_CFA_def_cfa_offset_sf:
662 results->cfaRegisterOffset =
663 (int32_t)(addressSpace.getSLEB128(p, instructionsEnd) *
664 cieInfo.dataAlignFactor);
665 results->codeOffsetAtStackDecrement = (uint32_t)codeOffset;
666 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset_sf(%d)\n",
667 results->cfaRegisterOffset);
668 break;
669 case DW_CFA_val_offset:
670 reg = addressSpace.getULEB128(p, instructionsEnd);
671 if (reg > kMaxRegisterNumber) {
672 _LIBUNWIND_LOG(
673 "malformed DW_CFA_val_offset DWARF unwind, reg (%" PRIu64
674 ") out of range\n",
675 reg);
676 return false;
677 }
678 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
679 cieInfo.dataAlignFactor;
680 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
681 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset(reg=%" PRIu64 ", "
682 "offset=%" PRId64 "\n",
683 reg, offset);
684 break;
685 case DW_CFA_val_offset_sf:
686 reg = addressSpace.getULEB128(p, instructionsEnd);
687 if (reg > kMaxRegisterNumber) {
688 _LIBUNWIND_LOG0(
689 "malformed DW_CFA_val_offset_sf DWARF unwind, reg too big");
690 return false;
691 }
692 offset = addressSpace.getSLEB128(p, instructionsEnd) *
693 cieInfo.dataAlignFactor;
694 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
695 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset_sf(reg=%" PRIu64 ", "
696 "offset=%" PRId64 "\n",
697 reg, offset);
698 break;
699 case DW_CFA_val_expression:
700 reg = addressSpace.getULEB128(p, instructionsEnd);
701 if (reg > kMaxRegisterNumber) {
702 _LIBUNWIND_LOG0(
703 "malformed DW_CFA_val_expression DWARF unwind, reg too big");
704 return false;
705 }
706 results->setRegister(reg, kRegisterIsExpression, (int64_t)p,
707 initialState);
708 length = addressSpace.getULEB128(p, instructionsEnd);
709 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
710 p += static_cast<pint_t>(length);
711 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_expression(reg=%" PRIu64 ", "
712 "expression=0x%" PRIx64 ", length=%" PRIu64
713 ")\n",
714 reg, results->savedRegisters[reg].value, length);
715 break;
716 case DW_CFA_GNU_args_size:
717 length = addressSpace.getULEB128(p, instructionsEnd);
718 results->spExtraArgSize = (uint32_t)length;
719 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
720 break;
721 case DW_CFA_GNU_negative_offset_extended:
722 reg = addressSpace.getULEB128(p, instructionsEnd);
723 if (reg > kMaxRegisterNumber) {
724 _LIBUNWIND_LOG0("malformed DW_CFA_GNU_negative_offset_extended DWARF "
725 "unwind, reg too big");
726 return false;
727 }
728 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
729 cieInfo.dataAlignFactor;
730 results->setRegister(reg, kRegisterInCFA, -offset, initialState);
731 _LIBUNWIND_TRACE_DWARF(
732 "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
733 break;
Luke Cheeseman4d8e4312018-12-14 11:30:12 +0000734
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000735#if defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_SPARC)
Daniel Kiss6f670c82020-10-30 17:42:23 +0100736 // The same constant is used to represent different instructions on
737 // AArch64 (negate_ra_state) and SPARC (window_save).
738 static_assert(DW_CFA_AARCH64_negate_ra_state == DW_CFA_GNU_window_save,
739 "uses the same constant");
740 case DW_CFA_AARCH64_negate_ra_state:
741 switch (arch) {
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000742#if defined(_LIBUNWIND_TARGET_AARCH64)
Sterling Augustinec0ef0db2020-03-04 16:29:58 -0800743 case REGISTERS_ARM64: {
744 int64_t value =
745 results->savedRegisters[UNW_ARM64_RA_SIGN_STATE].value ^ 0x1;
Daniel Kiss6f670c82020-10-30 17:42:23 +0100746 results->setRegisterValue(UNW_ARM64_RA_SIGN_STATE, value,
747 initialState);
Sterling Augustinec0ef0db2020-03-04 16:29:58 -0800748 _LIBUNWIND_TRACE_DWARF("DW_CFA_AARCH64_negate_ra_state\n");
749 } break;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000750#endif
Sterling Augustinec0ef0db2020-03-04 16:29:58 -0800751
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000752#if defined(_LIBUNWIND_TARGET_SPARC)
Daniel Kiss6f670c82020-10-30 17:42:23 +0100753 // case DW_CFA_GNU_window_save:
754 case REGISTERS_SPARC:
755 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_window_save()\n");
756 for (reg = UNW_SPARC_O0; reg <= UNW_SPARC_O7; reg++) {
757 results->setRegister(reg, kRegisterInRegister,
758 ((int64_t)reg - UNW_SPARC_O0) + UNW_SPARC_I0,
759 initialState);
760 }
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000761
Daniel Kiss6f670c82020-10-30 17:42:23 +0100762 for (reg = UNW_SPARC_L0; reg <= UNW_SPARC_I7; reg++) {
763 results->setRegister(reg, kRegisterInCFA,
764 ((int64_t)reg - UNW_SPARC_L0) * 4,
765 initialState);
766 }
767 break;
768#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000769 }
770 break;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000771#else
Daniel Kiss6f670c82020-10-30 17:42:23 +0100772 (void)arch;
Luke Cheeseman4d8e4312018-12-14 11:30:12 +0000773#endif
774
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000775 default:
Daniel Kiss6f670c82020-10-30 17:42:23 +0100776 operand = opcode & 0x3F;
777 switch (opcode & 0xC0) {
778 case DW_CFA_offset:
779 reg = operand;
780 if (reg > kMaxRegisterNumber) {
781 _LIBUNWIND_LOG("malformed DW_CFA_offset DWARF unwind, reg (%" PRIu64
782 ") out of range",
783 reg);
784 return false;
785 }
786 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
787 cieInfo.dataAlignFactor;
788 results->setRegister(reg, kRegisterInCFA, offset, initialState);
789 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset(reg=%d, offset=%" PRId64 ")\n",
790 operand, offset);
791 break;
792 case DW_CFA_advance_loc:
793 codeOffset += operand * cieInfo.codeAlignFactor;
794 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc: new offset=%" PRIu64 "\n",
795 static_cast<uint64_t>(codeOffset));
796 break;
797 case DW_CFA_restore:
798 reg = operand;
799 if (reg > kMaxRegisterNumber) {
800 _LIBUNWIND_LOG(
801 "malformed DW_CFA_restore DWARF unwind, reg (%" PRIu64
802 ") out of range",
803 reg);
804 return false;
805 }
806 results->restoreRegisterToInitialState(reg, initialState);
807 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore(reg=%" PRIu64 ")\n",
808 static_cast<uint64_t>(operand));
809 break;
810 default:
811 _LIBUNWIND_TRACE_DWARF("unknown CFA opcode 0x%02X\n", opcode);
812 return false;
813 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000814 }
815 }
816 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000817 return true;
818}
819
820} // namespace libunwind
821
822#endif // __DWARF_PARSER_HPP__