1 //===----------------------------------------------------------------------===//
3 // 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
8 // Parses DWARF CFIs (FDEs and CIEs).
10 //===----------------------------------------------------------------------===//
12 #ifndef __DWARF_PARSER_HPP__
13 #define __DWARF_PARSER_HPP__
20 #include "libunwind.h"
22 #include "Registers.hpp"
28 /// CFI_Parser does basic parsing of a CFI (Call Frame Information) records.
29 /// See DWARF Spec for details:
30 /// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
35 typedef typename A::pint_t pint_t;
37 /// Information encoded in a CIE (Common Information Entry)
41 pint_t cieInstructions;
42 uint8_t pointerEncoding;
44 uint8_t personalityEncoding;
45 uint8_t personalityOffsetInCIE;
47 uint32_t codeAlignFactor;
50 bool fdesHaveAugmentationData;
51 uint8_t returnAddressRegister;
52 #if defined(_LIBUNWIND_TARGET_AARCH64)
53 bool addressesSignedWithBKey;
57 /// Information about an FDE (Frame Description Entry)
61 pint_t fdeInstructions;
68 kMaxRegisterNumber = _LIBUNWIND_HIGHEST_DWARF_REGISTER
70 enum RegisterSavedWhere {
74 kRegisterOffsetFromCFA,
76 kRegisterAtExpression,
79 struct RegisterLocation {
80 RegisterSavedWhere location;
81 bool initialStateSaved;
84 /// Information about a frame layout and registers saved determined
85 /// by "running" the DWARF FDE "instructions"
88 int32_t cfaRegisterOffset; // CFA = (cfaRegister)+cfaRegisterOffset
89 int64_t cfaExpression; // CFA = expression
90 uint32_t spExtraArgSize;
91 RegisterLocation savedRegisters[kMaxRegisterNumber + 1];
92 enum class InitializeTime { kLazy, kNormal };
94 // When saving registers, this data structure is lazily initialized.
95 PrologInfo(InitializeTime IT = InitializeTime::kNormal) {
96 if (IT == InitializeTime::kNormal)
97 memset(this, 0, sizeof(*this));
99 void checkSaveRegister(uint64_t reg, PrologInfo &initialState) {
100 if (!savedRegisters[reg].initialStateSaved) {
101 initialState.savedRegisters[reg] = savedRegisters[reg];
102 savedRegisters[reg].initialStateSaved = true;
105 void setRegister(uint64_t reg, RegisterSavedWhere newLocation,
106 int64_t newValue, PrologInfo &initialState) {
107 checkSaveRegister(reg, initialState);
108 savedRegisters[reg].location = newLocation;
109 savedRegisters[reg].value = newValue;
111 void setRegisterLocation(uint64_t reg, RegisterSavedWhere newLocation,
112 PrologInfo &initialState) {
113 checkSaveRegister(reg, initialState);
114 savedRegisters[reg].location = newLocation;
116 void setRegisterValue(uint64_t reg, int64_t newValue,
117 PrologInfo &initialState) {
118 checkSaveRegister(reg, initialState);
119 savedRegisters[reg].value = newValue;
121 void restoreRegisterToInitialState(uint64_t reg, PrologInfo &initialState) {
122 if (savedRegisters[reg].initialStateSaved)
123 savedRegisters[reg] = initialState.savedRegisters[reg];
124 // else the register still holds its initial state
128 struct PrologInfoStackEntry {
129 PrologInfoStackEntry(PrologInfoStackEntry *n, const PrologInfo &i)
130 : next(n), info(i) {}
131 PrologInfoStackEntry *next;
135 struct RememberStack {
136 PrologInfoStackEntry *entry;
137 RememberStack() : entry(nullptr) {}
139 #if defined(_LIBUNWIND_REMEMBER_CLEANUP_NEEDED)
140 // Clean up rememberStack. Even in the case where every
141 // DW_CFA_remember_state is paired with a DW_CFA_restore_state,
142 // parseInstructions can skip restore opcodes if it reaches the target PC
143 // and stops interpreting, so we have to make sure we don't leak memory.
145 PrologInfoStackEntry *next = entry->next;
146 _LIBUNWIND_REMEMBER_FREE(entry);
153 static bool findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
154 uintptr_t sectionLength, pint_t fdeHint, FDE_Info *fdeInfo,
156 static const char *decodeFDE(A &addressSpace, pint_t fdeStart,
157 FDE_Info *fdeInfo, CIE_Info *cieInfo,
158 bool useCIEInfo = false);
159 static bool parseFDEInstructions(A &addressSpace, const FDE_Info &fdeInfo,
160 const CIE_Info &cieInfo, pint_t upToPC,
161 int arch, PrologInfo *results);
163 static const char *parseCIE(A &addressSpace, pint_t cie, CIE_Info *cieInfo);
166 /// Parse a FDE into a CIE_Info and an FDE_Info. If useCIEInfo is
167 /// true, treat cieInfo as already-parsed CIE_Info (whose start offset
168 /// must match the one specified by the FDE) rather than parsing the
169 /// one indicated within the FDE.
170 template <typename A>
171 const char *CFI_Parser<A>::decodeFDE(A &addressSpace, pint_t fdeStart,
172 FDE_Info *fdeInfo, CIE_Info *cieInfo,
175 pint_t cfiLength = (pint_t)addressSpace.get32(p);
177 if (cfiLength == 0xffffffff) {
178 // 0xffffffff means length is really next 8 bytes
179 cfiLength = (pint_t)addressSpace.get64(p);
183 return "FDE has zero length"; // zero terminator
184 uint32_t ciePointer = addressSpace.get32(p);
186 return "FDE is really a CIE"; // this is a CIE not an FDE
187 pint_t nextCFI = p + cfiLength;
188 pint_t cieStart = p - ciePointer;
190 if (cieInfo->cieStart != cieStart)
191 return "CIE start does not match";
193 const char *err = parseCIE(addressSpace, cieStart, cieInfo);
198 // Parse pc begin and range.
200 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
202 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding & 0x0F);
203 // Parse rest of info.
205 // Check for augmentation length.
206 if (cieInfo->fdesHaveAugmentationData) {
207 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
208 pint_t endOfAug = p + augLen;
209 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
210 // Peek at value (without indirection). Zero means no LSDA.
211 pint_t lsdaStart = p;
212 if (addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding & 0x0F) !=
214 // Reset pointer and re-parse LSDA address.
217 addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
222 fdeInfo->fdeStart = fdeStart;
223 fdeInfo->fdeLength = nextCFI - fdeStart;
224 fdeInfo->fdeInstructions = p;
225 fdeInfo->pcStart = pcStart;
226 fdeInfo->pcEnd = pcStart + pcRange;
227 return NULL; // success
230 /// Scan an eh_frame section to find an FDE for a pc
231 template <typename A>
232 bool CFI_Parser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
233 uintptr_t sectionLength, pint_t fdeHint,
234 FDE_Info *fdeInfo, CIE_Info *cieInfo) {
235 //fprintf(stderr, "findFDE(0x%llX)\n", (long long)pc);
236 pint_t p = (fdeHint != 0) ? fdeHint : ehSectionStart;
237 const pint_t ehSectionEnd = (sectionLength == UINTPTR_MAX)
238 ? static_cast<pint_t>(-1)
239 : (ehSectionStart + sectionLength);
240 while (p < ehSectionEnd) {
241 pint_t currentCFI = p;
242 //fprintf(stderr, "findFDE() CFI at 0x%llX\n", (long long)p);
243 pint_t cfiLength = addressSpace.get32(p);
245 if (cfiLength == 0xffffffff) {
246 // 0xffffffff means length is really next 8 bytes
247 cfiLength = (pint_t)addressSpace.get64(p);
251 return false; // zero terminator
252 uint32_t id = addressSpace.get32(p);
257 // Process FDE to see if it covers pc.
258 pint_t nextCFI = p + cfiLength;
259 uint32_t ciePointer = addressSpace.get32(p);
260 pint_t cieStart = p - ciePointer;
261 // Validate pointer to CIE is within section.
262 if ((ehSectionStart <= cieStart) && (cieStart < ehSectionEnd)) {
263 if (parseCIE(addressSpace, cieStart, cieInfo) == NULL) {
265 // Parse pc begin and range.
267 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
268 pint_t pcRange = addressSpace.getEncodedP(
269 p, nextCFI, cieInfo->pointerEncoding & 0x0F);
270 // Test if pc is within the function this FDE covers.
271 if ((pcStart < pc) && (pc <= pcStart + pcRange)) {
272 // parse rest of info
274 // check for augmentation length
275 if (cieInfo->fdesHaveAugmentationData) {
276 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
277 pint_t endOfAug = p + augLen;
278 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
279 // Peek at value (without indirection). Zero means no LSDA.
280 pint_t lsdaStart = p;
281 if (addressSpace.getEncodedP(
282 p, nextCFI, cieInfo->lsdaEncoding & 0x0F) != 0) {
283 // Reset pointer and re-parse LSDA address.
285 fdeInfo->lsda = addressSpace
286 .getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
291 fdeInfo->fdeStart = currentCFI;
292 fdeInfo->fdeLength = nextCFI - currentCFI;
293 fdeInfo->fdeInstructions = p;
294 fdeInfo->pcStart = pcStart;
295 fdeInfo->pcEnd = pcStart + pcRange;
298 // pc is not in begin/range, skip this FDE
301 // Malformed CIE, now augmentation describing pc range encoding.
304 // malformed FDE. CIE is bad
312 /// Extract info from a CIE
313 template <typename A>
314 const char *CFI_Parser<A>::parseCIE(A &addressSpace, pint_t cie,
316 cieInfo->pointerEncoding = 0;
317 cieInfo->lsdaEncoding = DW_EH_PE_omit;
318 cieInfo->personalityEncoding = 0;
319 cieInfo->personalityOffsetInCIE = 0;
320 cieInfo->personality = 0;
321 cieInfo->codeAlignFactor = 0;
322 cieInfo->dataAlignFactor = 0;
323 cieInfo->isSignalFrame = false;
324 cieInfo->fdesHaveAugmentationData = false;
325 #if defined(_LIBUNWIND_TARGET_AARCH64)
326 cieInfo->addressesSignedWithBKey = false;
328 cieInfo->cieStart = cie;
330 pint_t cieLength = (pint_t)addressSpace.get32(p);
332 pint_t cieContentEnd = p + cieLength;
333 if (cieLength == 0xffffffff) {
334 // 0xffffffff means length is really next 8 bytes
335 cieLength = (pint_t)addressSpace.get64(p);
337 cieContentEnd = p + cieLength;
341 // CIE ID is always 0
342 if (addressSpace.get32(p) != 0)
343 return "CIE ID is not zero";
345 // Version is always 1 or 3
346 uint8_t version = addressSpace.get8(p);
347 if ((version != 1) && (version != 3))
348 return "CIE version is not 1 or 3";
350 // save start of augmentation string and find end
352 while (addressSpace.get8(p) != 0)
355 // parse code aligment factor
356 cieInfo->codeAlignFactor = (uint32_t)addressSpace.getULEB128(p, cieContentEnd);
357 // parse data alignment factor
358 cieInfo->dataAlignFactor = (int)addressSpace.getSLEB128(p, cieContentEnd);
359 // parse return address register
360 uint64_t raReg = (version == 1) ? addressSpace.get8(p++)
361 : addressSpace.getULEB128(p, cieContentEnd);
362 assert(raReg < 255 && "return address register too large");
363 cieInfo->returnAddressRegister = (uint8_t)raReg;
364 // parse augmentation data based on augmentation string
365 const char *result = NULL;
366 if (addressSpace.get8(strStart) == 'z') {
367 // parse augmentation data length
368 addressSpace.getULEB128(p, cieContentEnd);
369 for (pint_t s = strStart; addressSpace.get8(s) != '\0'; ++s) {
370 switch (addressSpace.get8(s)) {
372 cieInfo->fdesHaveAugmentationData = true;
375 cieInfo->personalityEncoding = addressSpace.get8(p);
377 cieInfo->personalityOffsetInCIE = (uint8_t)(p - cie);
378 cieInfo->personality = addressSpace
379 .getEncodedP(p, cieContentEnd, cieInfo->personalityEncoding);
382 cieInfo->lsdaEncoding = addressSpace.get8(p);
386 cieInfo->pointerEncoding = addressSpace.get8(p);
390 cieInfo->isSignalFrame = true;
392 #if defined(_LIBUNWIND_TARGET_AARCH64)
394 cieInfo->addressesSignedWithBKey = true;
398 // ignore unknown letters
403 cieInfo->cieLength = cieContentEnd - cieInfo->cieStart;
404 cieInfo->cieInstructions = p;
409 /// "run" the DWARF instructions and create the abstact PrologInfo for an FDE
410 template <typename A>
411 bool CFI_Parser<A>::parseFDEInstructions(A &addressSpace,
412 const FDE_Info &fdeInfo,
413 const CIE_Info &cieInfo, pint_t upToPC,
414 int arch, PrologInfo *results) {
415 // Alloca is used for the allocation of the rememberStack entries. It removes
416 // the dependency on new/malloc but the below for loop can not be refactored
417 // into functions. Entry could be saved during the processing of a CIE and
418 // restored by an FDE.
419 RememberStack rememberStack;
423 pint_t instructionsEnd;
427 ParseInfo parseInfoArray[] = {
428 {cieInfo.cieInstructions, cieInfo.cieStart + cieInfo.cieLength,
430 {fdeInfo.fdeInstructions, fdeInfo.fdeStart + fdeInfo.fdeLength,
431 upToPC - fdeInfo.pcStart}};
433 for (const auto &info : parseInfoArray) {
434 pint_t p = info.instructions;
435 pint_t instructionsEnd = info.instructionsEnd;
436 pint_t pcoffset = info.pcoffset;
437 pint_t codeOffset = 0;
439 // initialState initialized as registers in results are modified. Use
440 // PrologInfo accessor functions to avoid reading uninitialized data.
441 PrologInfo initialState(PrologInfo::InitializeTime::kLazy);
443 _LIBUNWIND_TRACE_DWARF("parseFDEInstructions(instructions=0x%0" PRIx64
445 static_cast<uint64_t>(instructionsEnd));
447 // see DWARF Spec, section 6.4.2 for details on unwind opcodes
448 while ((p < instructionsEnd) && (codeOffset < pcoffset)) {
453 uint8_t opcode = addressSpace.get8(p);
459 _LIBUNWIND_TRACE_DWARF("DW_CFA_nop\n");
462 codeOffset = addressSpace.getEncodedP(p, instructionsEnd,
463 cieInfo.pointerEncoding);
464 _LIBUNWIND_TRACE_DWARF("DW_CFA_set_loc\n");
466 case DW_CFA_advance_loc1:
467 codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
469 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
470 static_cast<uint64_t>(codeOffset));
472 case DW_CFA_advance_loc2:
473 codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
475 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
476 static_cast<uint64_t>(codeOffset));
478 case DW_CFA_advance_loc4:
479 codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
481 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
482 static_cast<uint64_t>(codeOffset));
484 case DW_CFA_offset_extended:
485 reg = addressSpace.getULEB128(p, instructionsEnd);
486 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
487 cieInfo.dataAlignFactor;
488 if (reg > kMaxRegisterNumber) {
490 "malformed DW_CFA_offset_extended DWARF unwind, reg too big");
493 results->setRegister(reg, kRegisterInCFA, offset, initialState);
494 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended(reg=%" PRIu64 ", "
495 "offset=%" PRId64 ")\n",
498 case DW_CFA_restore_extended:
499 reg = addressSpace.getULEB128(p, instructionsEnd);
500 if (reg > kMaxRegisterNumber) {
502 "malformed DW_CFA_restore_extended DWARF unwind, reg too big");
505 results->restoreRegisterToInitialState(reg, initialState);
506 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_extended(reg=%" PRIu64 ")\n",
509 case DW_CFA_undefined:
510 reg = addressSpace.getULEB128(p, instructionsEnd);
511 if (reg > kMaxRegisterNumber) {
513 "malformed DW_CFA_undefined DWARF unwind, reg too big");
516 results->setRegisterLocation(reg, kRegisterUndefined, initialState);
517 _LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
519 case DW_CFA_same_value:
520 reg = addressSpace.getULEB128(p, instructionsEnd);
521 if (reg > kMaxRegisterNumber) {
523 "malformed DW_CFA_same_value DWARF unwind, reg too big");
526 // <rdar://problem/8456377> DW_CFA_same_value unsupported
527 // "same value" means register was stored in frame, but its current
528 // value has not changed, so no need to restore from frame.
529 // We model this as if the register was never saved.
530 results->setRegisterLocation(reg, kRegisterUnused, initialState);
531 _LIBUNWIND_TRACE_DWARF("DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
533 case DW_CFA_register:
534 reg = addressSpace.getULEB128(p, instructionsEnd);
535 reg2 = addressSpace.getULEB128(p, instructionsEnd);
536 if (reg > kMaxRegisterNumber) {
538 "malformed DW_CFA_register DWARF unwind, reg too big");
541 if (reg2 > kMaxRegisterNumber) {
543 "malformed DW_CFA_register DWARF unwind, reg2 too big");
546 results->setRegister(reg, kRegisterInRegister, (int64_t)reg2,
548 _LIBUNWIND_TRACE_DWARF(
549 "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n", reg, reg2);
551 case DW_CFA_remember_state: {
552 // Avoid operator new because that would be an upward dependency.
553 // Avoid malloc because it needs heap allocation.
554 PrologInfoStackEntry *entry =
555 (PrologInfoStackEntry *)_LIBUNWIND_REMEMBER_ALLOC(
556 sizeof(PrologInfoStackEntry));
558 entry->next = rememberStack.entry;
559 entry->info = *results;
560 rememberStack.entry = entry;
564 _LIBUNWIND_TRACE_DWARF("DW_CFA_remember_state\n");
567 case DW_CFA_restore_state:
568 if (rememberStack.entry != NULL) {
569 PrologInfoStackEntry *top = rememberStack.entry;
570 *results = top->info;
571 rememberStack.entry = top->next;
572 _LIBUNWIND_REMEMBER_FREE(top);
576 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_state\n");
579 reg = addressSpace.getULEB128(p, instructionsEnd);
580 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd);
581 if (reg > kMaxRegisterNumber) {
582 _LIBUNWIND_LOG0("malformed DW_CFA_def_cfa DWARF unwind, reg too big");
585 results->cfaRegister = (uint32_t)reg;
586 results->cfaRegisterOffset = (int32_t)offset;
587 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64
591 case DW_CFA_def_cfa_register:
592 reg = addressSpace.getULEB128(p, instructionsEnd);
593 if (reg > kMaxRegisterNumber) {
595 "malformed DW_CFA_def_cfa_register DWARF unwind, reg too big");
598 results->cfaRegister = (uint32_t)reg;
599 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
601 case DW_CFA_def_cfa_offset:
602 results->cfaRegisterOffset =
603 (int32_t)addressSpace.getULEB128(p, instructionsEnd);
604 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset(%d)\n",
605 results->cfaRegisterOffset);
607 case DW_CFA_def_cfa_expression:
608 results->cfaRegister = 0;
609 results->cfaExpression = (int64_t)p;
610 length = addressSpace.getULEB128(p, instructionsEnd);
611 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
612 p += static_cast<pint_t>(length);
613 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_expression(expression=0x%" PRIx64
614 ", length=%" PRIu64 ")\n",
615 results->cfaExpression, length);
617 case DW_CFA_expression:
618 reg = addressSpace.getULEB128(p, instructionsEnd);
619 if (reg > kMaxRegisterNumber) {
621 "malformed DW_CFA_expression DWARF unwind, reg too big");
624 results->setRegister(reg, kRegisterAtExpression, (int64_t)p,
626 length = addressSpace.getULEB128(p, instructionsEnd);
627 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
628 p += static_cast<pint_t>(length);
629 _LIBUNWIND_TRACE_DWARF("DW_CFA_expression(reg=%" PRIu64 ", "
630 "expression=0x%" PRIx64 ", "
631 "length=%" PRIu64 ")\n",
632 reg, results->savedRegisters[reg].value, length);
634 case DW_CFA_offset_extended_sf:
635 reg = addressSpace.getULEB128(p, instructionsEnd);
636 if (reg > kMaxRegisterNumber) {
638 "malformed DW_CFA_offset_extended_sf DWARF unwind, reg too big");
641 offset = addressSpace.getSLEB128(p, instructionsEnd) *
642 cieInfo.dataAlignFactor;
643 results->setRegister(reg, kRegisterInCFA, offset, initialState);
644 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended_sf(reg=%" PRIu64 ", "
645 "offset=%" PRId64 ")\n",
648 case DW_CFA_def_cfa_sf:
649 reg = addressSpace.getULEB128(p, instructionsEnd);
650 offset = addressSpace.getSLEB128(p, instructionsEnd) *
651 cieInfo.dataAlignFactor;
652 if (reg > kMaxRegisterNumber) {
654 "malformed DW_CFA_def_cfa_sf DWARF unwind, reg too big");
657 results->cfaRegister = (uint32_t)reg;
658 results->cfaRegisterOffset = (int32_t)offset;
659 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_sf(reg=%" PRIu64 ", "
660 "offset=%" PRId64 ")\n",
663 case DW_CFA_def_cfa_offset_sf:
664 results->cfaRegisterOffset =
665 (int32_t)(addressSpace.getSLEB128(p, instructionsEnd) *
666 cieInfo.dataAlignFactor);
667 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset_sf(%d)\n",
668 results->cfaRegisterOffset);
670 case DW_CFA_val_offset:
671 reg = addressSpace.getULEB128(p, instructionsEnd);
672 if (reg > kMaxRegisterNumber) {
674 "malformed DW_CFA_val_offset DWARF unwind, reg (%" PRIu64
679 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
680 cieInfo.dataAlignFactor;
681 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
682 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset(reg=%" PRIu64 ", "
683 "offset=%" PRId64 "\n",
686 case DW_CFA_val_offset_sf:
687 reg = addressSpace.getULEB128(p, instructionsEnd);
688 if (reg > kMaxRegisterNumber) {
690 "malformed DW_CFA_val_offset_sf DWARF unwind, reg too big");
693 offset = addressSpace.getSLEB128(p, instructionsEnd) *
694 cieInfo.dataAlignFactor;
695 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
696 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset_sf(reg=%" PRIu64 ", "
697 "offset=%" PRId64 "\n",
700 case DW_CFA_val_expression:
701 reg = addressSpace.getULEB128(p, instructionsEnd);
702 if (reg > kMaxRegisterNumber) {
704 "malformed DW_CFA_val_expression DWARF unwind, reg too big");
707 results->setRegister(reg, kRegisterIsExpression, (int64_t)p,
709 length = addressSpace.getULEB128(p, instructionsEnd);
710 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
711 p += static_cast<pint_t>(length);
712 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_expression(reg=%" PRIu64 ", "
713 "expression=0x%" PRIx64 ", length=%" PRIu64
715 reg, results->savedRegisters[reg].value, length);
717 case DW_CFA_GNU_args_size:
718 length = addressSpace.getULEB128(p, instructionsEnd);
719 results->spExtraArgSize = (uint32_t)length;
720 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
722 case DW_CFA_GNU_negative_offset_extended:
723 reg = addressSpace.getULEB128(p, instructionsEnd);
724 if (reg > kMaxRegisterNumber) {
725 _LIBUNWIND_LOG0("malformed DW_CFA_GNU_negative_offset_extended DWARF "
726 "unwind, reg too big");
729 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
730 cieInfo.dataAlignFactor;
731 results->setRegister(reg, kRegisterInCFA, -offset, initialState);
732 _LIBUNWIND_TRACE_DWARF(
733 "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
736 #if defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_SPARC)
737 // The same constant is used to represent different instructions on
738 // AArch64 (negate_ra_state) and SPARC (window_save).
739 static_assert(DW_CFA_AARCH64_negate_ra_state == DW_CFA_GNU_window_save,
740 "uses the same constant");
741 case DW_CFA_AARCH64_negate_ra_state:
743 #if defined(_LIBUNWIND_TARGET_AARCH64)
744 case REGISTERS_ARM64: {
746 results->savedRegisters[UNW_AARCH64_RA_SIGN_STATE].value ^ 0x1;
747 results->setRegisterValue(UNW_AARCH64_RA_SIGN_STATE, value,
749 _LIBUNWIND_TRACE_DWARF("DW_CFA_AARCH64_negate_ra_state\n");
753 #if defined(_LIBUNWIND_TARGET_SPARC)
754 // case DW_CFA_GNU_window_save:
755 case REGISTERS_SPARC:
756 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_window_save()\n");
757 for (reg = UNW_SPARC_O0; reg <= UNW_SPARC_O7; reg++) {
758 results->setRegister(reg, kRegisterInRegister,
759 ((int64_t)reg - UNW_SPARC_O0) + UNW_SPARC_I0,
763 for (reg = UNW_SPARC_L0; reg <= UNW_SPARC_I7; reg++) {
764 results->setRegister(reg, kRegisterInCFA,
765 ((int64_t)reg - UNW_SPARC_L0) * 4,
777 operand = opcode & 0x3F;
778 switch (opcode & 0xC0) {
781 if (reg > kMaxRegisterNumber) {
782 _LIBUNWIND_LOG("malformed DW_CFA_offset DWARF unwind, reg (%" PRIu64
787 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
788 cieInfo.dataAlignFactor;
789 results->setRegister(reg, kRegisterInCFA, offset, initialState);
790 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset(reg=%d, offset=%" PRId64 ")\n",
793 case DW_CFA_advance_loc:
794 codeOffset += operand * cieInfo.codeAlignFactor;
795 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc: new offset=%" PRIu64 "\n",
796 static_cast<uint64_t>(codeOffset));
800 if (reg > kMaxRegisterNumber) {
802 "malformed DW_CFA_restore DWARF unwind, reg (%" PRIu64
807 results->restoreRegisterToInitialState(reg, initialState);
808 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore(reg=%" PRIu64 ")\n",
809 static_cast<uint64_t>(operand));
812 _LIBUNWIND_TRACE_DWARF("unknown CFA opcode 0x%02X\n", opcode);
821 } // namespace libunwind
823 #endif // __DWARF_PARSER_HPP__